ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/devcomm.c
(Generate patch)

Comparing ray/src/rt/devcomm.c (file contents):
Revision 1.4 by greg, Fri Jun 2 17:21:24 1989 UTC vs.
Revision 2.9 by greg, Thu Jul 3 15:00:19 2003 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines