ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/devcomm.c
Revision: 2.12
Committed: Tue Mar 30 16:13:01 2004 UTC (20 years, 1 month ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.11: +67 -42 lines
Log Message:
Continued ANSIfication. There are only bits and pieces left now.

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.12 static const char RCSid[] = "$Id: devcomm.c,v 2.11 2003/11/11 16:24:06 greg Exp $";
3 greg 1.1 #endif
4     /*
5     * devcomm.c - communication routines for separate drivers.
6     *
7 greg 2.6 * External symbols declared in driver.h
8     */
9    
10 greg 2.7 #include "copyright.h"
11 greg 2.9
12 schorsch 2.12 #include <sys/types.h>
13     #include <sys/wait.h> /* XXX platform specific */
14    
15 greg 2.10 #include "platform.h"
16 greg 1.10 #include "standard.h"
17 greg 1.1 #include "driver.h"
18 greg 2.2
19 greg 1.1 #ifndef DEVPATH
20     #define DEVPATH getenv("PATH") /* device search path */
21     #endif
22    
23 schorsch 2.12 FILE *devin, *devout;
24     int devchild;
25    
26     static struct driver * final_connect(void);
27     static void mygets(char *s, FILE *fp);
28     static void myputs(char *s, FILE *fp);
29     static void reply_error(char *routine);
30     static void getstate(void);
31    
32     static dr_closef_t comm_close;
33     static dr_clearf_t comm_clear;
34     static dr_paintrf_t comm_paintr;
35     static dr_getcurf_t comm_getcur;
36     static dr_comoutf_t comm_comout;
37     static dr_cominf_t comm_comin;
38     static dr_flushf_t comm_flush;
39 greg 1.1
40 greg 1.12 struct driver comm_driver = {
41 greg 1.1 comm_close, comm_clear, comm_paintr, comm_getcur,
42 greg 1.12 comm_comout, comm_comin, comm_flush
43 greg 1.1 };
44    
45    
46 greg 2.3 static struct driver *
47 schorsch 2.12 final_connect(void) /* verify and initialize connection */
48 greg 2.3 {
49     putw(COM_SENDM, devout);
50     fflush(devout);
51     if (getw(devin) != COM_RECVM)
52     return(NULL);
53     /* get driver parameters */
54     getstate();
55     /* set error vectors */
56 gregl 2.5 erract[COMMAND].pf = comm_comout;
57     if (erract[WARNING].pf != NULL)
58     erract[WARNING].pf = comm_comout;
59 greg 2.3 return(&comm_driver);
60     }
61    
62    
63 schorsch 2.12 extern struct driver *
64     slave_init( /* run rview in slave mode */
65     char *dname,
66     char *id
67     )
68 greg 2.3 {
69     devchild = -1; /* we're the slave here */
70     devout = stdout; /* use standard input */
71     devin = stdin; /* and standard output */
72     return(final_connect()); /* verify initialization */
73     }
74    
75    
76 schorsch 2.12 extern struct driver *
77     comm_init( /* set up and execute driver */
78     char *dname,
79     char *id
80     )
81 greg 1.1 {
82 greg 2.6 char *dvcname;
83 greg 1.1 int p1[2], p2[2];
84 greg 1.7 char pin[16], pout[16];
85     /* find driver program */
86 greg 2.6 if ((dvcname = getpath(dname, DEVPATH, X_OK)) == NULL) {
87 gregl 2.5 eputs(dname);
88     eputs(": not found\n");
89 greg 1.1 return(NULL);
90     }
91 greg 2.8 #ifdef RHAS_FORK_EXEC
92 greg 1.7 /* open communication pipes */
93 greg 1.1 if (pipe(p1) == -1 || pipe(p2) == -1)
94     goto syserr;
95 greg 2.11 if ((devchild = fork()) == 0) { /* fork driver process */
96 greg 1.1 close(p1[1]);
97     close(p2[0]);
98 greg 1.7 sprintf(pin, "%d", p1[0]);
99     sprintf(pout, "%d", p2[1]);
100 greg 2.6 execl(dvcname, dname, pin, pout, id, 0);
101     perror(dvcname);
102 greg 1.1 _exit(127);
103     }
104     if (devchild == -1)
105     goto syserr;
106     close(p1[0]);
107     close(p2[1]);
108     if ((devout = fdopen(p1[1], "w")) == NULL)
109     goto syserr;
110     if ((devin = fdopen(p2[0], "r")) == NULL)
111     goto syserr;
112 greg 2.3 return(final_connect()); /* verify initialization */
113 greg 1.1 syserr:
114 greg 1.7 perror(dname);
115 greg 1.1 return(NULL);
116 greg 2.8
117     #else /* ! RHAS_FORK_EXEC */
118    
119     eputs(dname);
120     eputs(": no fork/exec\n");
121     return(NULL);
122    
123     #endif /* ! RHAS_FORK_EXEC */
124 greg 1.1 }
125    
126    
127 greg 2.6 static void
128 schorsch 2.12 comm_close(void) /* done with driver */
129 greg 1.1 {
130     int pid;
131    
132 gregl 2.5 erract[COMMAND].pf = NULL; /* reset error vectors */
133     if (erract[WARNING].pf != NULL)
134     erract[WARNING].pf = wputs;
135 greg 1.1 fclose(devout);
136     fclose(devin);
137 greg 2.3 if (devchild < 0)
138     return;
139 greg 1.1 while ((pid = wait(0)) != -1 && pid != devchild)
140     ;
141     }
142    
143    
144 greg 2.6 static void
145 schorsch 2.12 comm_clear( /* clear screen */
146     int xres,
147     int yres
148     )
149 greg 1.1 {
150     putc(COM_CLEAR, devout);
151 greg 1.8 putw(xres, devout);
152     putw(yres, devout);
153 greg 1.1 fflush(devout);
154     }
155    
156    
157 greg 2.6 static void
158 schorsch 2.12 comm_paintr( /* paint a rectangle */
159     COLOR col,
160     int xmin,
161     int ymin,
162     int xmax,
163     int ymax
164     )
165 greg 1.1 {
166     putc(COM_PAINTR, devout);
167 greg 1.10 fwrite((char *)col, sizeof(COLOR), 1, devout);
168 greg 1.8 putw(xmin, devout);
169     putw(ymin, devout);
170     putw(xmax, devout);
171     putw(ymax, devout);
172 greg 1.12 }
173    
174    
175 greg 2.6 static void
176 schorsch 2.12 comm_flush(void) /* flush output to driver */
177 greg 1.12 {
178     putc(COM_FLUSH, devout);
179     fflush(devout);
180 greg 1.13 if (getc(devin) != COM_FLUSH)
181     reply_error("flush");
182 greg 1.17 getstate();
183 greg 1.1 }
184    
185    
186     static int
187 schorsch 2.12 comm_getcur( /* get and return cursor position */
188     int *xp,
189     int *yp
190     )
191 greg 1.1 {
192     int c;
193    
194     putc(COM_GETCUR, devout);
195     fflush(devout);
196     if (getc(devin) != COM_GETCUR)
197     reply_error("getcur");
198     c = getc(devin);
199 greg 1.8 *xp = getw(devin);
200     *yp = getw(devin);
201 greg 1.1 return(c);
202     }
203    
204    
205 greg 2.6 static void
206 schorsch 2.12 comm_comout( /* print string to command line */
207     char *str
208     )
209 greg 1.1 {
210     putc(COM_COMOUT, devout);
211     myputs(str, devout);
212 greg 1.18 if (str[strlen(str)-1] == '\n')
213     fflush(devout);
214 greg 1.1 }
215    
216    
217 greg 2.6 static void
218 schorsch 2.12 comm_comin( /* read string from command line */
219     char *buf,
220     char *prompt
221     )
222 greg 1.1 {
223     putc(COM_COMIN, devout);
224 greg 1.11 if (prompt == NULL)
225     putc(0, devout);
226     else {
227     putc(1, devout);
228     myputs(prompt, devout);
229     }
230 greg 1.1 fflush(devout);
231     if (getc(devin) != COM_COMIN)
232     reply_error("comin");
233     mygets(buf, devin);
234 greg 1.17 getstate();
235 greg 1.1 }
236    
237    
238 greg 2.6 static void
239 schorsch 2.12 mygets( /* get string from file (with nul) */
240     register char *s,
241     register FILE *fp
242     )
243 greg 1.1 {
244     register int c;
245    
246     while ((c = getc(fp)) != EOF)
247     if ((*s++ = c) == '\0')
248     return;
249     *s = '\0';
250     }
251    
252    
253 greg 2.6 static void
254 schorsch 2.12 myputs( /* put string to file (with nul) */
255     register char *s,
256     register FILE *fp
257     )
258 greg 1.1 {
259     do
260     putc(*s, fp);
261     while (*s++);
262     }
263    
264    
265 greg 2.6 static void
266 schorsch 2.12 reply_error( /* what should we do here? */
267     char *routine
268     )
269 greg 1.1 {
270 gregl 2.5 eputs(routine);
271     eputs(": driver reply error\n");
272 greg 1.1 quit(1);
273 greg 1.17 }
274    
275    
276 greg 2.6 static void
277 schorsch 2.12 getstate(void) /* get driver state variables */
278 greg 1.17 {
279     fread((char *)&comm_driver.pixaspect,
280     sizeof(comm_driver.pixaspect), 1, devin);
281     comm_driver.xsiz = getw(devin);
282     comm_driver.ysiz = getw(devin);
283     comm_driver.inpready = getw(devin);
284 greg 1.1 }