ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/devcomm.c
Revision: 1.2
Committed: Thu May 25 19:33:21 1989 UTC (34 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.1: +2 -5 lines
Log Message:
eliminated duplication of standard error output and assoc. problems

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 <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 #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 = {
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 if ((devname = getpath(argv[0], DEVPATH)) == NULL) {
59 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 cmdvec = comm_comout; /* set error vectors */
92 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 cmdvec = NULL; /* reset error vectors */
107 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 }
173
174
175 static
176 comm_comin(buf) /* read string from command line */
177 char *buf;
178 {
179 putc(COM_COMIN, devout);
180 fflush(devout);
181 if (getc(devin) != COM_COMIN)
182 reply_error("comin");
183 mygets(buf, devin);
184 comm_driver.inpready = 0;
185 }
186
187
188 static
189 comm_errout(str) /* display an error message */
190 char *str;
191 {
192 comm_comout(str);
193 stderr_v(str); /* send to standard error also */
194 }
195
196
197 static
198 mygets(s, fp) /* get string from file (with nul) */
199 register char *s;
200 register FILE *fp;
201 {
202 register int c;
203
204 while ((c = getc(fp)) != EOF)
205 if ((*s++ = c) == '\0')
206 return;
207 *s = '\0';
208 }
209
210
211 static
212 myputs(s, fp) /* put string to file (with nul) */
213 register char *s;
214 register FILE *fp;
215 {
216 do
217 putc(*s, fp);
218 while (*s++);
219 }
220
221
222 static
223 reply_error(routine) /* what should we do here? */
224 char *routine;
225 {
226 stderr_v(routine);
227 stderr_v(": driver reply error\n");
228 quit(1);
229 }
230
231
232 static
233 onsigio() /* input ready */
234 {
235 comm_driver.inpready++;
236 }