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