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