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