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