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