1 |
greg |
1.1 |
/* Copyright (c) 1988 Regents of the University of California */ |
2 |
|
|
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* devcomm.c - communication routines for separate drivers. |
9 |
|
|
* |
10 |
|
|
* 10/5/88 |
11 |
|
|
*/ |
12 |
|
|
|
13 |
greg |
1.10 |
#include "standard.h" |
14 |
greg |
1.1 |
|
15 |
|
|
#include "color.h" |
16 |
|
|
|
17 |
|
|
#include "driver.h" |
18 |
|
|
|
19 |
|
|
#ifndef DEVPATH |
20 |
|
|
#define DEVPATH getenv("PATH") /* device search path */ |
21 |
|
|
#endif |
22 |
|
|
|
23 |
greg |
1.15 |
#ifndef DELAY |
24 |
|
|
#define DELAY 20 /* seconds to wait for response */ |
25 |
|
|
#endif |
26 |
|
|
|
27 |
greg |
1.1 |
#ifndef BSD |
28 |
|
|
#define vfork fork |
29 |
|
|
#endif |
30 |
|
|
|
31 |
greg |
1.16 |
static int comm_close(), comm_clear(), comm_paintr(), comm_errout(), |
32 |
greg |
1.12 |
comm_getcur(), comm_comout(), comm_comin(), comm_flush(); |
33 |
greg |
1.1 |
|
34 |
greg |
1.12 |
struct driver comm_driver = { |
35 |
greg |
1.1 |
comm_close, comm_clear, comm_paintr, comm_getcur, |
36 |
greg |
1.12 |
comm_comout, comm_comin, comm_flush |
37 |
greg |
1.1 |
}; |
38 |
|
|
|
39 |
|
|
FILE *devin, *devout; |
40 |
|
|
|
41 |
|
|
int devchild; |
42 |
|
|
|
43 |
|
|
|
44 |
|
|
struct driver * |
45 |
greg |
1.7 |
comm_init(dname, id) /* set up and execute driver */ |
46 |
|
|
char *dname, *id; |
47 |
greg |
1.1 |
{ |
48 |
|
|
char *devname; |
49 |
|
|
int p1[2], p2[2]; |
50 |
greg |
1.7 |
char pin[16], pout[16]; |
51 |
|
|
/* find driver program */ |
52 |
greg |
1.19 |
if ((devname = getpath(dname, DEVPATH, X_OK)) == NULL) { |
53 |
greg |
1.7 |
stderr_v(dname); |
54 |
greg |
1.1 |
stderr_v(": not found\n"); |
55 |
|
|
return(NULL); |
56 |
|
|
} |
57 |
greg |
1.7 |
/* open communication pipes */ |
58 |
greg |
1.1 |
if (pipe(p1) == -1 || pipe(p2) == -1) |
59 |
|
|
goto syserr; |
60 |
greg |
1.7 |
if ((devchild = vfork()) == 0) { /* fork driver process */ |
61 |
greg |
1.1 |
close(p1[1]); |
62 |
|
|
close(p2[0]); |
63 |
greg |
1.7 |
sprintf(pin, "%d", p1[0]); |
64 |
|
|
sprintf(pout, "%d", p2[1]); |
65 |
|
|
execl(devname, dname, pin, pout, id, 0); |
66 |
|
|
perror(devname); |
67 |
greg |
1.1 |
_exit(127); |
68 |
|
|
} |
69 |
|
|
if (devchild == -1) |
70 |
|
|
goto syserr; |
71 |
|
|
close(p1[0]); |
72 |
|
|
close(p2[1]); |
73 |
|
|
if ((devout = fdopen(p1[1], "w")) == NULL) |
74 |
|
|
goto syserr; |
75 |
|
|
if ((devin = fdopen(p2[0], "r")) == NULL) |
76 |
|
|
goto syserr; |
77 |
greg |
1.15 |
/* verify initialization */ |
78 |
greg |
1.7 |
putw(COM_SENDM, devout); |
79 |
|
|
fflush(devout); |
80 |
|
|
if (getw(devin) != COM_RECVM) |
81 |
|
|
return(NULL); |
82 |
greg |
1.15 |
/* get driver parameters */ |
83 |
greg |
1.17 |
getstate(); |
84 |
greg |
1.7 |
/* set error vectors */ |
85 |
|
|
cmdvec = comm_comout; |
86 |
greg |
1.1 |
if (wrnvec != NULL) |
87 |
|
|
wrnvec = comm_comout; |
88 |
|
|
return(&comm_driver); |
89 |
|
|
syserr: |
90 |
greg |
1.7 |
perror(dname); |
91 |
greg |
1.1 |
return(NULL); |
92 |
|
|
} |
93 |
|
|
|
94 |
|
|
|
95 |
|
|
static |
96 |
|
|
comm_close() /* done with driver */ |
97 |
|
|
{ |
98 |
|
|
int pid; |
99 |
|
|
|
100 |
greg |
1.2 |
cmdvec = NULL; /* reset error vectors */ |
101 |
greg |
1.1 |
if (wrnvec != NULL) |
102 |
|
|
wrnvec = stderr_v; |
103 |
|
|
fclose(devout); |
104 |
|
|
fclose(devin); |
105 |
|
|
while ((pid = wait(0)) != -1 && pid != devchild) |
106 |
|
|
; |
107 |
|
|
} |
108 |
|
|
|
109 |
|
|
|
110 |
|
|
static |
111 |
|
|
comm_clear(xres, yres) /* clear screen */ |
112 |
|
|
int xres, yres; |
113 |
|
|
{ |
114 |
|
|
putc(COM_CLEAR, devout); |
115 |
greg |
1.8 |
putw(xres, devout); |
116 |
|
|
putw(yres, devout); |
117 |
greg |
1.1 |
fflush(devout); |
118 |
|
|
} |
119 |
|
|
|
120 |
|
|
|
121 |
|
|
static |
122 |
|
|
comm_paintr(col, xmin, ymin, xmax, ymax) /* paint a rectangle */ |
123 |
|
|
COLOR col; |
124 |
|
|
int xmin, ymin, xmax, ymax; |
125 |
|
|
{ |
126 |
|
|
putc(COM_PAINTR, devout); |
127 |
greg |
1.10 |
fwrite((char *)col, sizeof(COLOR), 1, devout); |
128 |
greg |
1.8 |
putw(xmin, devout); |
129 |
|
|
putw(ymin, devout); |
130 |
|
|
putw(xmax, devout); |
131 |
|
|
putw(ymax, devout); |
132 |
greg |
1.12 |
} |
133 |
|
|
|
134 |
|
|
|
135 |
|
|
static |
136 |
|
|
comm_flush() /* flush output to driver */ |
137 |
|
|
{ |
138 |
|
|
putc(COM_FLUSH, devout); |
139 |
|
|
fflush(devout); |
140 |
greg |
1.13 |
if (getc(devin) != COM_FLUSH) |
141 |
|
|
reply_error("flush"); |
142 |
greg |
1.17 |
getstate(); |
143 |
greg |
1.1 |
} |
144 |
|
|
|
145 |
|
|
|
146 |
|
|
static int |
147 |
|
|
comm_getcur(xp, yp) /* get and return cursor position */ |
148 |
|
|
int *xp, *yp; |
149 |
|
|
{ |
150 |
|
|
int c; |
151 |
|
|
|
152 |
|
|
putc(COM_GETCUR, devout); |
153 |
|
|
fflush(devout); |
154 |
|
|
if (getc(devin) != COM_GETCUR) |
155 |
|
|
reply_error("getcur"); |
156 |
|
|
c = getc(devin); |
157 |
greg |
1.8 |
*xp = getw(devin); |
158 |
|
|
*yp = getw(devin); |
159 |
greg |
1.1 |
return(c); |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
|
163 |
|
|
static |
164 |
|
|
comm_comout(str) /* print string to command line */ |
165 |
|
|
char *str; |
166 |
|
|
{ |
167 |
|
|
putc(COM_COMOUT, devout); |
168 |
|
|
myputs(str, devout); |
169 |
greg |
1.18 |
if (str[strlen(str)-1] == '\n') |
170 |
|
|
fflush(devout); |
171 |
greg |
1.1 |
} |
172 |
|
|
|
173 |
|
|
|
174 |
|
|
static |
175 |
greg |
1.11 |
comm_comin(buf, prompt) /* read string from command line */ |
176 |
greg |
1.1 |
char *buf; |
177 |
greg |
1.11 |
char *prompt; |
178 |
greg |
1.1 |
{ |
179 |
|
|
putc(COM_COMIN, devout); |
180 |
greg |
1.11 |
if (prompt == NULL) |
181 |
|
|
putc(0, devout); |
182 |
|
|
else { |
183 |
|
|
putc(1, devout); |
184 |
|
|
myputs(prompt, devout); |
185 |
|
|
} |
186 |
greg |
1.1 |
fflush(devout); |
187 |
|
|
if (getc(devin) != COM_COMIN) |
188 |
|
|
reply_error("comin"); |
189 |
|
|
mygets(buf, devin); |
190 |
greg |
1.17 |
getstate(); |
191 |
greg |
1.1 |
} |
192 |
|
|
|
193 |
|
|
|
194 |
|
|
static |
195 |
|
|
comm_errout(str) /* display an error message */ |
196 |
|
|
char *str; |
197 |
|
|
{ |
198 |
|
|
comm_comout(str); |
199 |
|
|
stderr_v(str); /* send to standard error also */ |
200 |
|
|
} |
201 |
|
|
|
202 |
|
|
|
203 |
|
|
static |
204 |
|
|
mygets(s, fp) /* get string from file (with nul) */ |
205 |
|
|
register char *s; |
206 |
|
|
register FILE *fp; |
207 |
|
|
{ |
208 |
|
|
register int c; |
209 |
|
|
|
210 |
|
|
while ((c = getc(fp)) != EOF) |
211 |
|
|
if ((*s++ = c) == '\0') |
212 |
|
|
return; |
213 |
|
|
*s = '\0'; |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
|
217 |
|
|
static |
218 |
|
|
myputs(s, fp) /* put string to file (with nul) */ |
219 |
|
|
register char *s; |
220 |
|
|
register FILE *fp; |
221 |
|
|
{ |
222 |
|
|
do |
223 |
|
|
putc(*s, fp); |
224 |
|
|
while (*s++); |
225 |
|
|
} |
226 |
|
|
|
227 |
|
|
|
228 |
|
|
static |
229 |
|
|
reply_error(routine) /* what should we do here? */ |
230 |
|
|
char *routine; |
231 |
|
|
{ |
232 |
|
|
stderr_v(routine); |
233 |
|
|
stderr_v(": driver reply error\n"); |
234 |
|
|
quit(1); |
235 |
greg |
1.17 |
} |
236 |
|
|
|
237 |
|
|
|
238 |
|
|
static |
239 |
|
|
getstate() /* get driver state variables */ |
240 |
|
|
{ |
241 |
|
|
fread((char *)&comm_driver.pixaspect, |
242 |
|
|
sizeof(comm_driver.pixaspect), 1, devin); |
243 |
|
|
comm_driver.xsiz = getw(devin); |
244 |
|
|
comm_driver.ysiz = getw(devin); |
245 |
|
|
comm_driver.inpready = getw(devin); |
246 |
greg |
1.1 |
} |