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