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, |
43 |
< |
MAXRES, MAXRES, 0 |
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 |
< |
comm_init(argv) /* set up and execute driver */ |
59 |
< |
char *argv[]; |
58 |
> |
slave_init(dname, id) /* run rview in slave mode */ |
59 |
> |
char *dname, *id; |
60 |
|
{ |
61 |
< |
char *devname; |
62 |
< |
int p1[2], p2[2]; |
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 |
< |
if ((devname = getpath(argv[0], DEVPATH)) == NULL) { |
68 |
< |
stderr_v(argv[0]); |
69 |
< |
stderr_v(": not found\n"); |
67 |
> |
|
68 |
> |
struct driver * |
69 |
> |
comm_init(dname, id) /* set up and execute driver */ |
70 |
> |
char *dname, *id; |
71 |
> |
{ |
72 |
> |
char *dvcname; |
73 |
> |
int p1[2], p2[2]; |
74 |
> |
char pin[16], pout[16]; |
75 |
> |
/* find driver program */ |
76 |
> |
if ((dvcname = getpath(dname, DEVPATH, X_OK)) == NULL) { |
77 |
> |
eputs(dname); |
78 |
> |
eputs(": not found\n"); |
79 |
|
return(NULL); |
80 |
|
} |
81 |
+ |
/* open communication pipes */ |
82 |
|
if (pipe(p1) == -1 || pipe(p2) == -1) |
83 |
|
goto syserr; |
84 |
< |
if ((devchild = vfork()) == 0) { |
84 |
> |
if ((devchild = vfork()) == 0) { /* fork driver process */ |
85 |
|
close(p1[1]); |
86 |
|
close(p2[0]); |
87 |
< |
if (p1[0] != 0) { |
88 |
< |
dup2(p1[0], 0); |
89 |
< |
close(p1[0]); |
90 |
< |
} |
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"); |
87 |
> |
sprintf(pin, "%d", p1[0]); |
88 |
> |
sprintf(pout, "%d", p2[1]); |
89 |
> |
execl(dvcname, dname, pin, pout, id, 0); |
90 |
> |
perror(dvcname); |
91 |
|
_exit(127); |
92 |
|
} |
93 |
|
if (devchild == -1) |
98 |
|
goto syserr; |
99 |
|
if ((devin = fdopen(p2[0], "r")) == NULL) |
100 |
|
goto syserr; |
101 |
< |
bcopy(&comm_default, &comm_driver, sizeof(comm_driver)); |
90 |
< |
signal(SIGIO, onsigio); |
91 |
< |
errvec = comm_errout; /* set error vector */ |
92 |
< |
cmdvec = comm_comout; |
93 |
< |
if (wrnvec != NULL) |
94 |
< |
wrnvec = comm_comout; |
95 |
< |
return(&comm_driver); |
101 |
> |
return(final_connect()); /* verify initialization */ |
102 |
|
syserr: |
103 |
< |
perror(argv[0]); |
103 |
> |
perror(dname); |
104 |
|
return(NULL); |
105 |
|
} |
106 |
|
|
107 |
|
|
108 |
< |
static |
108 |
> |
static void |
109 |
|
comm_close() /* done with driver */ |
110 |
|
{ |
111 |
|
int pid; |
112 |
|
|
113 |
< |
errvec = stderr_v; /* reset error vector */ |
114 |
< |
cmdvec = NULL; |
115 |
< |
if (wrnvec != NULL) |
110 |
< |
wrnvec = stderr_v; |
111 |
< |
signal(SIGIO, SIG_DFL); |
113 |
> |
erract[COMMAND].pf = NULL; /* reset error vectors */ |
114 |
> |
if (erract[WARNING].pf != NULL) |
115 |
> |
erract[WARNING].pf = wputs; |
116 |
|
fclose(devout); |
117 |
|
fclose(devin); |
118 |
+ |
if (devchild < 0) |
119 |
+ |
return; |
120 |
|
while ((pid = wait(0)) != -1 && pid != devchild) |
121 |
|
; |
122 |
|
} |
123 |
|
|
124 |
|
|
125 |
< |
static |
125 |
> |
static void |
126 |
|
comm_clear(xres, yres) /* clear screen */ |
127 |
|
int xres, yres; |
128 |
|
{ |
129 |
|
putc(COM_CLEAR, devout); |
130 |
< |
fwrite(&xres, sizeof(xres), 1, devout); |
131 |
< |
fwrite(&yres, sizeof(yres), 1, devout); |
130 |
> |
putw(xres, devout); |
131 |
> |
putw(yres, devout); |
132 |
|
fflush(devout); |
133 |
|
} |
134 |
|
|
135 |
|
|
136 |
< |
static |
136 |
> |
static void |
137 |
|
comm_paintr(col, xmin, ymin, xmax, ymax) /* paint a rectangle */ |
138 |
|
COLOR col; |
139 |
|
int xmin, ymin, xmax, ymax; |
140 |
|
{ |
135 |
– |
extern long nrays; /* number of rays traced */ |
136 |
– |
static long lastflush = 0; /* ray count at last flush */ |
137 |
– |
|
141 |
|
putc(COM_PAINTR, devout); |
142 |
< |
fwrite(col, sizeof(COLOR), 1, devout); |
143 |
< |
fwrite(&xmin, sizeof(xmin), 1, devout); |
144 |
< |
fwrite(&ymin, sizeof(ymin), 1, devout); |
145 |
< |
fwrite(&xmax, sizeof(xmax), 1, devout); |
146 |
< |
fwrite(&ymax, sizeof(ymax), 1, devout); |
144 |
< |
if (nrays - lastflush >= WFLUSH) { |
145 |
< |
fflush(devout); |
146 |
< |
lastflush = nrays; |
147 |
< |
} |
142 |
> |
fwrite((char *)col, sizeof(COLOR), 1, devout); |
143 |
> |
putw(xmin, devout); |
144 |
> |
putw(ymin, devout); |
145 |
> |
putw(xmax, devout); |
146 |
> |
putw(ymax, devout); |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
+ |
static void |
151 |
+ |
comm_flush() /* flush output to driver */ |
152 |
+ |
{ |
153 |
+ |
putc(COM_FLUSH, devout); |
154 |
+ |
fflush(devout); |
155 |
+ |
if (getc(devin) != COM_FLUSH) |
156 |
+ |
reply_error("flush"); |
157 |
+ |
getstate(); |
158 |
+ |
} |
159 |
+ |
|
160 |
+ |
|
161 |
|
static int |
162 |
|
comm_getcur(xp, yp) /* get and return cursor position */ |
163 |
|
int *xp, *yp; |
169 |
|
if (getc(devin) != COM_GETCUR) |
170 |
|
reply_error("getcur"); |
171 |
|
c = getc(devin); |
172 |
< |
fread(xp, sizeof(*xp), 1, devin); |
173 |
< |
fread(yp, sizeof(*yp), 1, devin); |
172 |
> |
*xp = getw(devin); |
173 |
> |
*yp = getw(devin); |
174 |
|
return(c); |
175 |
|
} |
176 |
|
|
177 |
|
|
178 |
< |
static |
178 |
> |
static void |
179 |
|
comm_comout(str) /* print string to command line */ |
180 |
|
char *str; |
181 |
|
{ |
182 |
|
putc(COM_COMOUT, devout); |
183 |
|
myputs(str, devout); |
184 |
< |
fflush(devout); |
184 |
> |
if (str[strlen(str)-1] == '\n') |
185 |
> |
fflush(devout); |
186 |
|
} |
187 |
|
|
188 |
|
|
189 |
< |
static |
190 |
< |
comm_comin(buf) /* read string from command line */ |
189 |
> |
static void |
190 |
> |
comm_comin(buf, prompt) /* read string from command line */ |
191 |
|
char *buf; |
192 |
+ |
char *prompt; |
193 |
|
{ |
194 |
|
putc(COM_COMIN, devout); |
195 |
+ |
if (prompt == NULL) |
196 |
+ |
putc(0, devout); |
197 |
+ |
else { |
198 |
+ |
putc(1, devout); |
199 |
+ |
myputs(prompt, devout); |
200 |
+ |
} |
201 |
|
fflush(devout); |
202 |
|
if (getc(devin) != COM_COMIN) |
203 |
|
reply_error("comin"); |
204 |
|
mygets(buf, devin); |
205 |
< |
comm_driver.inpready = 0; |
205 |
> |
getstate(); |
206 |
|
} |
207 |
|
|
208 |
|
|
209 |
< |
static |
192 |
< |
comm_errout(str) /* display an error message */ |
193 |
< |
char *str; |
194 |
< |
{ |
195 |
< |
comm_comout(str); |
196 |
< |
stderr_v(str); /* send to standard error also */ |
197 |
< |
} |
198 |
< |
|
199 |
< |
|
200 |
< |
static |
209 |
> |
static void |
210 |
|
mygets(s, fp) /* get string from file (with nul) */ |
211 |
|
register char *s; |
212 |
|
register FILE *fp; |
220 |
|
} |
221 |
|
|
222 |
|
|
223 |
< |
static |
223 |
> |
static void |
224 |
|
myputs(s, fp) /* put string to file (with nul) */ |
225 |
|
register char *s; |
226 |
|
register FILE *fp; |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
< |
static |
234 |
> |
static void |
235 |
|
reply_error(routine) /* what should we do here? */ |
236 |
|
char *routine; |
237 |
|
{ |
238 |
< |
stderr_v(routine); |
239 |
< |
stderr_v(": driver reply error\n"); |
238 |
> |
eputs(routine); |
239 |
> |
eputs(": driver reply error\n"); |
240 |
|
quit(1); |
241 |
|
} |
242 |
|
|
243 |
|
|
244 |
< |
static |
245 |
< |
onsigio() /* input ready */ |
244 |
> |
static void |
245 |
> |
getstate() /* get driver state variables */ |
246 |
|
{ |
247 |
< |
comm_driver.inpready++; |
247 |
> |
fread((char *)&comm_driver.pixaspect, |
248 |
> |
sizeof(comm_driver.pixaspect), 1, devin); |
249 |
> |
comm_driver.xsiz = getw(devin); |
250 |
> |
comm_driver.ysiz = getw(devin); |
251 |
> |
comm_driver.inpready = getw(devin); |
252 |
|
} |