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