1 |
– |
/* Copyright (c) 1988 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* devcomm.c - communication routines for separate drivers. |
6 |
|
* |
7 |
< |
* 10/5/88 |
7 |
> |
* External symbols declared in driver.h |
8 |
|
*/ |
9 |
|
|
10 |
< |
#include <stdio.h> |
10 |
> |
#include "copyright.h" |
11 |
|
|
12 |
< |
#include <signal.h> |
12 |
> |
#include <sys/types.h> |
13 |
> |
#include <sys/wait.h> /* XXX platform specific */ |
14 |
|
|
15 |
< |
#include "color.h" |
16 |
< |
|
15 |
> |
#include "platform.h" |
16 |
> |
#include "standard.h" |
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 |
27 |
< |
#endif |
23 |
> |
FILE *devin, *devout; |
24 |
> |
int devchild; |
25 |
|
|
26 |
< |
#ifndef WFLUSH |
27 |
< |
#define WFLUSH 50 /* flush after this many rays */ |
28 |
< |
#endif |
26 |
> |
static struct driver * final_connect(void); |
27 |
> |
static void mygets(char *s, FILE *fp); |
28 |
> |
static void myputs(char *s, FILE *fp); |
29 |
> |
static void reply_error(char *routine); |
30 |
> |
static void getstate(void); |
31 |
|
|
32 |
< |
extern char *getpath(), *getenv(); |
32 |
> |
static dr_closef_t comm_close; |
33 |
> |
static dr_clearf_t comm_clear; |
34 |
> |
static dr_paintrf_t comm_paintr; |
35 |
> |
static dr_getcurf_t comm_getcur; |
36 |
> |
static dr_comoutf_t comm_comout; |
37 |
> |
static dr_cominf_t comm_comin; |
38 |
> |
static dr_flushf_t comm_flush; |
39 |
|
|
40 |
< |
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 = { |
40 |
> |
struct driver comm_driver = { |
41 |
|
comm_close, comm_clear, comm_paintr, comm_getcur, |
42 |
< |
comm_comout, comm_comin, |
43 |
< |
MAXRES, MAXRES, 0 |
42 |
> |
comm_comout, comm_comin, comm_flush |
43 |
|
}; |
44 |
|
|
46 |
– |
FILE *devin, *devout; |
45 |
|
|
46 |
< |
int devchild; |
46 |
> |
static struct driver * |
47 |
> |
final_connect(void) /* verify and initialize connection */ |
48 |
> |
{ |
49 |
> |
putw(COM_SENDM, devout); |
50 |
> |
fflush(devout); |
51 |
> |
if (getw(devin) != COM_RECVM) |
52 |
> |
return(NULL); |
53 |
> |
/* get driver parameters */ |
54 |
> |
getstate(); |
55 |
> |
/* set error vectors */ |
56 |
> |
erract[COMMAND].pf = comm_comout; |
57 |
> |
if (erract[WARNING].pf != NULL) |
58 |
> |
erract[WARNING].pf = comm_comout; |
59 |
> |
return(&comm_driver); |
60 |
> |
} |
61 |
|
|
62 |
|
|
63 |
< |
struct driver * |
64 |
< |
comm_init(argv) /* set up and execute driver */ |
65 |
< |
char *argv[]; |
63 |
> |
extern struct driver * |
64 |
> |
slave_init( /* run rview in slave mode */ |
65 |
> |
char *dname, |
66 |
> |
char *id |
67 |
> |
) |
68 |
|
{ |
69 |
< |
char *devname; |
70 |
< |
int p1[2], p2[2]; |
69 |
> |
devchild = -1; /* we're the slave here */ |
70 |
> |
devout = stdout; /* use standard input */ |
71 |
> |
devin = stdin; /* and standard output */ |
72 |
> |
return(final_connect()); /* verify initialization */ |
73 |
> |
} |
74 |
|
|
75 |
< |
if ((devname = getpath(argv[0], DEVPATH)) == NULL) { |
76 |
< |
stderr_v(argv[0]); |
77 |
< |
stderr_v(": not found\n"); |
75 |
> |
|
76 |
> |
extern struct driver * |
77 |
> |
comm_init( /* set up and execute driver */ |
78 |
> |
char *dname, |
79 |
> |
char *id |
80 |
> |
) |
81 |
> |
{ |
82 |
> |
char *dvcname; |
83 |
> |
int p1[2], p2[2]; |
84 |
> |
char pin[16], pout[16]; |
85 |
> |
/* find driver program */ |
86 |
> |
if ((dvcname = getpath(dname, DEVPATH, X_OK)) == NULL) { |
87 |
> |
eputs(dname); |
88 |
> |
eputs(": not found\n"); |
89 |
|
return(NULL); |
90 |
|
} |
91 |
+ |
#ifdef RHAS_FORK_EXEC |
92 |
+ |
/* open communication pipes */ |
93 |
|
if (pipe(p1) == -1 || pipe(p2) == -1) |
94 |
|
goto syserr; |
95 |
< |
if ((devchild = vfork()) == 0) { |
95 |
> |
if ((devchild = fork()) == 0) { /* fork driver process */ |
96 |
|
close(p1[1]); |
97 |
|
close(p2[0]); |
98 |
< |
if (p1[0] != 0) { |
99 |
< |
dup2(p1[0], 0); |
100 |
< |
close(p1[0]); |
101 |
< |
} |
72 |
< |
if (p2[1] != 1) { |
73 |
< |
dup2(p2[1], 1); |
74 |
< |
close(p2[1]); |
75 |
< |
} |
76 |
< |
execv(devname, argv); |
77 |
< |
stderr_v(devname); |
78 |
< |
stderr_v(": cannot execute\n"); |
98 |
> |
sprintf(pin, "%d", p1[0]); |
99 |
> |
sprintf(pout, "%d", p2[1]); |
100 |
> |
execl(dvcname, dname, pin, pout, id, 0); |
101 |
> |
perror(dvcname); |
102 |
|
_exit(127); |
103 |
|
} |
104 |
|
if (devchild == -1) |
109 |
|
goto syserr; |
110 |
|
if ((devin = fdopen(p2[0], "r")) == NULL) |
111 |
|
goto syserr; |
112 |
< |
bcopy(&comm_default, &comm_driver, sizeof(comm_driver)); |
90 |
< |
signal(SIGIO, onsigio); |
91 |
< |
cmdvec = comm_comout; /* set error vectors */ |
92 |
< |
if (wrnvec != NULL) |
93 |
< |
wrnvec = comm_comout; |
94 |
< |
return(&comm_driver); |
112 |
> |
return(final_connect()); /* verify initialization */ |
113 |
|
syserr: |
114 |
< |
perror(argv[0]); |
114 |
> |
perror(dname); |
115 |
|
return(NULL); |
116 |
+ |
|
117 |
+ |
#else /* ! RHAS_FORK_EXEC */ |
118 |
+ |
|
119 |
+ |
eputs(dname); |
120 |
+ |
eputs(": no fork/exec\n"); |
121 |
+ |
return(NULL); |
122 |
+ |
|
123 |
+ |
#endif /* ! RHAS_FORK_EXEC */ |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
< |
static |
128 |
< |
comm_close() /* done with driver */ |
127 |
> |
static void |
128 |
> |
comm_close(void) /* done with driver */ |
129 |
|
{ |
130 |
|
int pid; |
131 |
|
|
132 |
< |
cmdvec = NULL; /* reset error vectors */ |
133 |
< |
if (wrnvec != NULL) |
134 |
< |
wrnvec = stderr_v; |
109 |
< |
signal(SIGIO, SIG_DFL); |
132 |
> |
erract[COMMAND].pf = NULL; /* reset error vectors */ |
133 |
> |
if (erract[WARNING].pf != NULL) |
134 |
> |
erract[WARNING].pf = wputs; |
135 |
|
fclose(devout); |
136 |
|
fclose(devin); |
137 |
+ |
if (devchild < 0) |
138 |
+ |
return; |
139 |
|
while ((pid = wait(0)) != -1 && pid != devchild) |
140 |
|
; |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
< |
static |
145 |
< |
comm_clear(xres, yres) /* clear screen */ |
146 |
< |
int xres, yres; |
144 |
> |
static void |
145 |
> |
comm_clear( /* clear screen */ |
146 |
> |
int xres, |
147 |
> |
int yres |
148 |
> |
) |
149 |
|
{ |
150 |
|
putc(COM_CLEAR, devout); |
151 |
< |
fwrite(&xres, sizeof(xres), 1, devout); |
152 |
< |
fwrite(&yres, sizeof(yres), 1, devout); |
151 |
> |
putw(xres, devout); |
152 |
> |
putw(yres, devout); |
153 |
|
fflush(devout); |
154 |
|
} |
155 |
|
|
156 |
|
|
157 |
< |
static |
158 |
< |
comm_paintr(col, xmin, ymin, xmax, ymax) /* paint a rectangle */ |
159 |
< |
COLOR col; |
160 |
< |
int xmin, ymin, xmax, ymax; |
157 |
> |
static void |
158 |
> |
comm_paintr( /* paint a rectangle */ |
159 |
> |
COLOR col, |
160 |
> |
int xmin, |
161 |
> |
int ymin, |
162 |
> |
int xmax, |
163 |
> |
int ymax |
164 |
> |
) |
165 |
|
{ |
133 |
– |
extern long nrays; /* number of rays traced */ |
134 |
– |
static long lastflush = 0; /* ray count at last flush */ |
135 |
– |
|
166 |
|
putc(COM_PAINTR, devout); |
167 |
< |
fwrite(col, sizeof(COLOR), 1, devout); |
168 |
< |
fwrite(&xmin, sizeof(xmin), 1, devout); |
169 |
< |
fwrite(&ymin, sizeof(ymin), 1, devout); |
170 |
< |
fwrite(&xmax, sizeof(xmax), 1, devout); |
171 |
< |
fwrite(&ymax, sizeof(ymax), 1, devout); |
142 |
< |
if (nrays - lastflush >= WFLUSH) { |
143 |
< |
fflush(devout); |
144 |
< |
lastflush = nrays; |
145 |
< |
} |
167 |
> |
fwrite((char *)col, sizeof(COLOR), 1, devout); |
168 |
> |
putw(xmin, devout); |
169 |
> |
putw(ymin, devout); |
170 |
> |
putw(xmax, devout); |
171 |
> |
putw(ymax, devout); |
172 |
|
} |
173 |
|
|
174 |
|
|
175 |
+ |
static void |
176 |
+ |
comm_flush(void) /* flush output to driver */ |
177 |
+ |
{ |
178 |
+ |
putc(COM_FLUSH, devout); |
179 |
+ |
fflush(devout); |
180 |
+ |
if (getc(devin) != COM_FLUSH) |
181 |
+ |
reply_error("flush"); |
182 |
+ |
getstate(); |
183 |
+ |
} |
184 |
+ |
|
185 |
+ |
|
186 |
|
static int |
187 |
< |
comm_getcur(xp, yp) /* get and return cursor position */ |
188 |
< |
int *xp, *yp; |
187 |
> |
comm_getcur( /* get and return cursor position */ |
188 |
> |
int *xp, |
189 |
> |
int *yp |
190 |
> |
) |
191 |
|
{ |
192 |
|
int c; |
193 |
|
|
196 |
|
if (getc(devin) != COM_GETCUR) |
197 |
|
reply_error("getcur"); |
198 |
|
c = getc(devin); |
199 |
< |
fread(xp, sizeof(*xp), 1, devin); |
200 |
< |
fread(yp, sizeof(*yp), 1, devin); |
199 |
> |
*xp = getw(devin); |
200 |
> |
*yp = getw(devin); |
201 |
|
return(c); |
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
< |
static |
206 |
< |
comm_comout(str) /* print string to command line */ |
207 |
< |
char *str; |
205 |
> |
static void |
206 |
> |
comm_comout( /* print string to command line */ |
207 |
> |
char *str |
208 |
> |
) |
209 |
|
{ |
210 |
|
putc(COM_COMOUT, devout); |
211 |
|
myputs(str, devout); |
212 |
< |
fflush(devout); |
212 |
> |
if (str[strlen(str)-1] == '\n') |
213 |
> |
fflush(devout); |
214 |
|
} |
215 |
|
|
216 |
|
|
217 |
< |
static |
218 |
< |
comm_comin(buf) /* read string from command line */ |
219 |
< |
char *buf; |
217 |
> |
static void |
218 |
> |
comm_comin( /* read string from command line */ |
219 |
> |
char *buf, |
220 |
> |
char *prompt |
221 |
> |
) |
222 |
|
{ |
223 |
|
putc(COM_COMIN, devout); |
224 |
+ |
if (prompt == NULL) |
225 |
+ |
putc(0, devout); |
226 |
+ |
else { |
227 |
+ |
putc(1, devout); |
228 |
+ |
myputs(prompt, devout); |
229 |
+ |
} |
230 |
|
fflush(devout); |
231 |
|
if (getc(devin) != COM_COMIN) |
232 |
|
reply_error("comin"); |
233 |
|
mygets(buf, devin); |
234 |
< |
comm_driver.inpready = 0; |
234 |
> |
getstate(); |
235 |
|
} |
236 |
|
|
237 |
|
|
238 |
< |
static |
239 |
< |
comm_errout(str) /* display an error message */ |
240 |
< |
char *str; |
238 |
> |
static void |
239 |
> |
mygets( /* get string from file (with nul) */ |
240 |
> |
register char *s, |
241 |
> |
register FILE *fp |
242 |
> |
) |
243 |
|
{ |
193 |
– |
comm_comout(str); |
194 |
– |
stderr_v(str); /* send to standard error also */ |
195 |
– |
} |
196 |
– |
|
197 |
– |
|
198 |
– |
static |
199 |
– |
mygets(s, fp) /* get string from file (with nul) */ |
200 |
– |
register char *s; |
201 |
– |
register FILE *fp; |
202 |
– |
{ |
244 |
|
register int c; |
245 |
|
|
246 |
|
while ((c = getc(fp)) != EOF) |
250 |
|
} |
251 |
|
|
252 |
|
|
253 |
< |
static |
254 |
< |
myputs(s, fp) /* put string to file (with nul) */ |
255 |
< |
register char *s; |
256 |
< |
register FILE *fp; |
253 |
> |
static void |
254 |
> |
myputs( /* put string to file (with nul) */ |
255 |
> |
register char *s, |
256 |
> |
register FILE *fp |
257 |
> |
) |
258 |
|
{ |
259 |
|
do |
260 |
|
putc(*s, fp); |
262 |
|
} |
263 |
|
|
264 |
|
|
265 |
< |
static |
266 |
< |
reply_error(routine) /* what should we do here? */ |
267 |
< |
char *routine; |
265 |
> |
static void |
266 |
> |
reply_error( /* what should we do here? */ |
267 |
> |
char *routine |
268 |
> |
) |
269 |
|
{ |
270 |
< |
stderr_v(routine); |
271 |
< |
stderr_v(": driver reply error\n"); |
270 |
> |
eputs(routine); |
271 |
> |
eputs(": driver reply error\n"); |
272 |
|
quit(1); |
273 |
|
} |
274 |
|
|
275 |
|
|
276 |
< |
static |
277 |
< |
onsigio() /* input ready */ |
276 |
> |
static void |
277 |
> |
getstate(void) /* get driver state variables */ |
278 |
|
{ |
279 |
< |
comm_driver.inpready++; |
279 |
> |
fread((char *)&comm_driver.pixaspect, |
280 |
> |
sizeof(comm_driver.pixaspect), 1, devin); |
281 |
> |
comm_driver.xsiz = getw(devin); |
282 |
> |
comm_driver.ysiz = getw(devin); |
283 |
> |
comm_driver.inpready = getw(devin); |
284 |
|
} |