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 |
|
|
#include <stdio.h> |
14 |
|
|
|
15 |
|
|
#include <signal.h> |
16 |
|
|
|
17 |
|
|
#include "color.h" |
18 |
|
|
|
19 |
|
|
#include "driver.h" |
20 |
|
|
|
21 |
|
|
#ifndef DEVPATH |
22 |
|
|
#define DEVPATH getenv("PATH") /* device search path */ |
23 |
|
|
#endif |
24 |
|
|
|
25 |
|
|
#ifndef BSD |
26 |
|
|
#define vfork fork |
27 |
|
|
#endif |
28 |
|
|
|
29 |
|
|
#ifndef WFLUSH |
30 |
greg |
1.5 |
#define WFLUSH 30 /* flush after this many rays */ |
31 |
greg |
1.1 |
#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 = { |
41 |
|
|
comm_close, comm_clear, comm_paintr, comm_getcur, |
42 |
|
|
comm_comout, comm_comin, |
43 |
|
|
MAXRES, MAXRES, 0 |
44 |
|
|
}; |
45 |
|
|
|
46 |
|
|
FILE *devin, *devout; |
47 |
|
|
|
48 |
|
|
int devchild; |
49 |
|
|
|
50 |
|
|
|
51 |
|
|
struct driver * |
52 |
|
|
comm_init(argv) /* set up and execute driver */ |
53 |
|
|
char *argv[]; |
54 |
|
|
{ |
55 |
|
|
char *devname; |
56 |
|
|
int p1[2], p2[2]; |
57 |
|
|
|
58 |
greg |
1.6 |
if ((devname = getpath(argv[0], DEVPATH, 1)) == NULL) { |
59 |
greg |
1.1 |
stderr_v(argv[0]); |
60 |
|
|
stderr_v(": not found\n"); |
61 |
|
|
return(NULL); |
62 |
|
|
} |
63 |
|
|
if (pipe(p1) == -1 || pipe(p2) == -1) |
64 |
|
|
goto syserr; |
65 |
|
|
if ((devchild = vfork()) == 0) { |
66 |
|
|
close(p1[1]); |
67 |
|
|
close(p2[0]); |
68 |
|
|
if (p1[0] != 0) { |
69 |
|
|
dup2(p1[0], 0); |
70 |
|
|
close(p1[0]); |
71 |
|
|
} |
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"); |
79 |
|
|
_exit(127); |
80 |
|
|
} |
81 |
|
|
if (devchild == -1) |
82 |
|
|
goto syserr; |
83 |
|
|
close(p1[0]); |
84 |
|
|
close(p2[1]); |
85 |
|
|
if ((devout = fdopen(p1[1], "w")) == NULL) |
86 |
|
|
goto syserr; |
87 |
|
|
if ((devin = fdopen(p2[0], "r")) == NULL) |
88 |
|
|
goto syserr; |
89 |
|
|
bcopy(&comm_default, &comm_driver, sizeof(comm_driver)); |
90 |
|
|
signal(SIGIO, onsigio); |
91 |
greg |
1.2 |
cmdvec = comm_comout; /* set error vectors */ |
92 |
greg |
1.1 |
if (wrnvec != NULL) |
93 |
|
|
wrnvec = comm_comout; |
94 |
|
|
return(&comm_driver); |
95 |
|
|
syserr: |
96 |
|
|
perror(argv[0]); |
97 |
|
|
return(NULL); |
98 |
|
|
} |
99 |
|
|
|
100 |
|
|
|
101 |
|
|
static |
102 |
|
|
comm_close() /* done with driver */ |
103 |
|
|
{ |
104 |
|
|
int pid; |
105 |
|
|
|
106 |
greg |
1.2 |
cmdvec = NULL; /* reset error vectors */ |
107 |
greg |
1.1 |
if (wrnvec != NULL) |
108 |
|
|
wrnvec = stderr_v; |
109 |
|
|
signal(SIGIO, SIG_DFL); |
110 |
|
|
fclose(devout); |
111 |
|
|
fclose(devin); |
112 |
|
|
while ((pid = wait(0)) != -1 && pid != devchild) |
113 |
|
|
; |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
|
117 |
|
|
static |
118 |
|
|
comm_clear(xres, yres) /* clear screen */ |
119 |
|
|
int xres, yres; |
120 |
|
|
{ |
121 |
|
|
putc(COM_CLEAR, devout); |
122 |
|
|
fwrite(&xres, sizeof(xres), 1, devout); |
123 |
|
|
fwrite(&yres, sizeof(yres), 1, devout); |
124 |
|
|
fflush(devout); |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
|
128 |
|
|
static |
129 |
|
|
comm_paintr(col, xmin, ymin, xmax, ymax) /* paint a rectangle */ |
130 |
|
|
COLOR col; |
131 |
|
|
int xmin, ymin, xmax, ymax; |
132 |
|
|
{ |
133 |
|
|
extern long nrays; /* number of rays traced */ |
134 |
|
|
static long lastflush = 0; /* ray count at last flush */ |
135 |
|
|
|
136 |
|
|
putc(COM_PAINTR, devout); |
137 |
|
|
fwrite(col, sizeof(COLOR), 1, devout); |
138 |
|
|
fwrite(&xmin, sizeof(xmin), 1, devout); |
139 |
|
|
fwrite(&ymin, sizeof(ymin), 1, devout); |
140 |
|
|
fwrite(&xmax, sizeof(xmax), 1, devout); |
141 |
|
|
fwrite(&ymax, sizeof(ymax), 1, devout); |
142 |
|
|
if (nrays - lastflush >= WFLUSH) { |
143 |
|
|
fflush(devout); |
144 |
|
|
lastflush = nrays; |
145 |
|
|
} |
146 |
|
|
} |
147 |
|
|
|
148 |
|
|
|
149 |
|
|
static int |
150 |
|
|
comm_getcur(xp, yp) /* get and return cursor position */ |
151 |
|
|
int *xp, *yp; |
152 |
|
|
{ |
153 |
|
|
int c; |
154 |
|
|
|
155 |
|
|
putc(COM_GETCUR, devout); |
156 |
|
|
fflush(devout); |
157 |
|
|
if (getc(devin) != COM_GETCUR) |
158 |
|
|
reply_error("getcur"); |
159 |
|
|
c = getc(devin); |
160 |
|
|
fread(xp, sizeof(*xp), 1, devin); |
161 |
|
|
fread(yp, sizeof(*yp), 1, devin); |
162 |
|
|
return(c); |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
|
166 |
|
|
static |
167 |
|
|
comm_comout(str) /* print string to command line */ |
168 |
|
|
char *str; |
169 |
|
|
{ |
170 |
|
|
putc(COM_COMOUT, devout); |
171 |
|
|
myputs(str, devout); |
172 |
greg |
1.3 |
fflush(devout); |
173 |
greg |
1.1 |
} |
174 |
|
|
|
175 |
|
|
|
176 |
|
|
static |
177 |
|
|
comm_comin(buf) /* read string from command line */ |
178 |
|
|
char *buf; |
179 |
|
|
{ |
180 |
|
|
putc(COM_COMIN, devout); |
181 |
|
|
fflush(devout); |
182 |
|
|
if (getc(devin) != COM_COMIN) |
183 |
|
|
reply_error("comin"); |
184 |
|
|
mygets(buf, devin); |
185 |
|
|
comm_driver.inpready = 0; |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
|
189 |
|
|
static |
190 |
|
|
comm_errout(str) /* display an error message */ |
191 |
|
|
char *str; |
192 |
|
|
{ |
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 |
|
|
{ |
203 |
|
|
register int c; |
204 |
|
|
|
205 |
|
|
while ((c = getc(fp)) != EOF) |
206 |
|
|
if ((*s++ = c) == '\0') |
207 |
|
|
return; |
208 |
|
|
*s = '\0'; |
209 |
|
|
} |
210 |
|
|
|
211 |
|
|
|
212 |
|
|
static |
213 |
|
|
myputs(s, fp) /* put string to file (with nul) */ |
214 |
|
|
register char *s; |
215 |
|
|
register FILE *fp; |
216 |
|
|
{ |
217 |
|
|
do |
218 |
|
|
putc(*s, fp); |
219 |
|
|
while (*s++); |
220 |
|
|
} |
221 |
|
|
|
222 |
|
|
|
223 |
|
|
static |
224 |
|
|
reply_error(routine) /* what should we do here? */ |
225 |
|
|
char *routine; |
226 |
|
|
{ |
227 |
|
|
stderr_v(routine); |
228 |
|
|
stderr_v(": driver reply error\n"); |
229 |
|
|
quit(1); |
230 |
|
|
} |
231 |
|
|
|
232 |
|
|
|
233 |
|
|
static |
234 |
|
|
onsigio() /* input ready */ |
235 |
|
|
{ |
236 |
|
|
comm_driver.inpready++; |
237 |
|
|
} |