ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/devcomm.c
Revision: 1.17
Committed: Wed Mar 21 09:46:55 1990 UTC (34 years, 1 month ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.16: +14 -7 lines
Log Message:
Changed state mainenance calls

File Contents

# Content
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 "standard.h"
14
15 #include "color.h"
16
17 #include "driver.h"
18
19 #ifndef DEVPATH
20 #define DEVPATH getenv("PATH") /* device search path */
21 #endif
22
23 #ifndef DELAY
24 #define DELAY 20 /* seconds to wait for response */
25 #endif
26
27 #ifndef BSD
28 #define vfork fork
29 #endif
30
31 extern char *getpath(), *getenv();
32
33 static int comm_close(), comm_clear(), comm_paintr(), comm_errout(),
34 comm_getcur(), comm_comout(), comm_comin(), comm_flush();
35
36 struct driver comm_driver = {
37 comm_close, comm_clear, comm_paintr, comm_getcur,
38 comm_comout, comm_comin, comm_flush
39 };
40
41 FILE *devin, *devout;
42
43 int devchild;
44
45
46 struct driver *
47 comm_init(dname, id) /* set up and execute driver */
48 char *dname, *id;
49 {
50 char *devname;
51 int p1[2], p2[2];
52 char pin[16], pout[16];
53 /* find driver program */
54 if ((devname = getpath(dname, DEVPATH, 1)) == NULL) {
55 stderr_v(dname);
56 stderr_v(": not found\n");
57 return(NULL);
58 }
59 /* open communication pipes */
60 if (pipe(p1) == -1 || pipe(p2) == -1)
61 goto syserr;
62 if ((devchild = vfork()) == 0) { /* fork driver process */
63 close(p1[1]);
64 close(p2[0]);
65 sprintf(pin, "%d", p1[0]);
66 sprintf(pout, "%d", p2[1]);
67 execl(devname, dname, pin, pout, id, 0);
68 perror(devname);
69 _exit(127);
70 }
71 if (devchild == -1)
72 goto syserr;
73 close(p1[0]);
74 close(p2[1]);
75 if ((devout = fdopen(p1[1], "w")) == NULL)
76 goto syserr;
77 if ((devin = fdopen(p2[0], "r")) == NULL)
78 goto syserr;
79 /* verify initialization */
80 putw(COM_SENDM, devout);
81 fflush(devout);
82 if (getw(devin) != COM_RECVM)
83 return(NULL);
84 /* get driver parameters */
85 getstate();
86 /* set error vectors */
87 cmdvec = comm_comout;
88 if (wrnvec != NULL)
89 wrnvec = comm_comout;
90 return(&comm_driver);
91 syserr:
92 perror(dname);
93 return(NULL);
94 }
95
96
97 static
98 comm_close() /* done with driver */
99 {
100 int pid;
101
102 cmdvec = NULL; /* reset error vectors */
103 if (wrnvec != NULL)
104 wrnvec = stderr_v;
105 fclose(devout);
106 fclose(devin);
107 while ((pid = wait(0)) != -1 && pid != devchild)
108 ;
109 }
110
111
112 static
113 comm_clear(xres, yres) /* clear screen */
114 int xres, yres;
115 {
116 putc(COM_CLEAR, devout);
117 putw(xres, devout);
118 putw(yres, devout);
119 fflush(devout);
120 }
121
122
123 static
124 comm_paintr(col, xmin, ymin, xmax, ymax) /* paint a rectangle */
125 COLOR col;
126 int xmin, ymin, xmax, ymax;
127 {
128 putc(COM_PAINTR, devout);
129 fwrite((char *)col, sizeof(COLOR), 1, devout);
130 putw(xmin, devout);
131 putw(ymin, devout);
132 putw(xmax, devout);
133 putw(ymax, devout);
134 }
135
136
137 static
138 comm_flush() /* flush output to driver */
139 {
140 putc(COM_FLUSH, devout);
141 fflush(devout);
142 if (getc(devin) != COM_FLUSH)
143 reply_error("flush");
144 getstate();
145 }
146
147
148 static int
149 comm_getcur(xp, yp) /* get and return cursor position */
150 int *xp, *yp;
151 {
152 int c;
153
154 putc(COM_GETCUR, devout);
155 fflush(devout);
156 if (getc(devin) != COM_GETCUR)
157 reply_error("getcur");
158 c = getc(devin);
159 *xp = getw(devin);
160 *yp = getw(devin);
161 return(c);
162 }
163
164
165 static
166 comm_comout(str) /* print string to command line */
167 char *str;
168 {
169 putc(COM_COMOUT, devout);
170 myputs(str, devout);
171 }
172
173
174 static
175 comm_comin(buf, prompt) /* read string from command line */
176 char *buf;
177 char *prompt;
178 {
179 putc(COM_COMIN, devout);
180 if (prompt == NULL)
181 putc(0, devout);
182 else {
183 putc(1, devout);
184 myputs(prompt, devout);
185 }
186 fflush(devout);
187 if (getc(devin) != COM_COMIN)
188 reply_error("comin");
189 mygets(buf, devin);
190 getstate();
191 }
192
193
194 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
204 mygets(s, fp) /* get string from file (with nul) */
205 register char *s;
206 register FILE *fp;
207 {
208 register int c;
209
210 while ((c = getc(fp)) != EOF)
211 if ((*s++ = c) == '\0')
212 return;
213 *s = '\0';
214 }
215
216
217 static
218 myputs(s, fp) /* put string to file (with nul) */
219 register char *s;
220 register FILE *fp;
221 {
222 do
223 putc(*s, fp);
224 while (*s++);
225 }
226
227
228 static
229 reply_error(routine) /* what should we do here? */
230 char *routine;
231 {
232 stderr_v(routine);
233 stderr_v(": driver reply error\n");
234 quit(1);
235 }
236
237
238 static
239 getstate() /* get driver state variables */
240 {
241 fread((char *)&comm_driver.pixaspect,
242 sizeof(comm_driver.pixaspect), 1, devin);
243 comm_driver.xsiz = getw(devin);
244 comm_driver.ysiz = getw(devin);
245 comm_driver.inpready = getw(devin);
246 }