ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/devmain.c
Revision: 1.13
Committed: Thu Aug 2 13:40:23 1990 UTC (33 years, 9 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.12: +3 -1 lines
Log Message:
minor cosmetics for stderr_v()

File Contents

# Content
1 /* Copyright (c) 1989 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * devmain.c - main for independent drivers.
9 *
10 * Redefine your initialization routine to dinit.
11 *
12 * 10/25/89
13 */
14
15 #include <stdio.h>
16
17 #include "color.h"
18
19 #include "driver.h"
20
21 int (*wrnvec)(), (*errvec)(), (*cmdvec)(); /* error vectors, unused */
22
23 struct driver *dev = NULL; /* output device */
24
25 FILE *devin, *devout; /* communications channels */
26
27 char *progname; /* driver name */
28
29 int r_clear(), r_paintr(), r_getcur(), r_comout(), r_comin(), r_flush();
30
31 int (*dev_func[NREQUESTS])() = { /* request handlers */
32 r_clear, r_paintr,
33 r_getcur, r_comout,
34 r_comin, r_flush
35 };
36
37
38 main(argc, argv) /* set up communications and main loop */
39 int argc;
40 char *argv[];
41 {
42 extern struct driver *dinit();
43 int com;
44 /* set up I/O */
45 progname = argv[0];
46 if (argc < 3) {
47 stderr_v("arg count\n");
48 quit(1);
49 }
50 devin = fdopen(atoi(argv[1]), "r");
51 devout = fdopen(atoi(argv[2]), "w");
52 if (devin == NULL || devout == NULL || getw(devin) != COM_SENDM) {
53 stderr_v("connection failure\n");
54 quit(1);
55 }
56 /* open device */
57 if ((dev = dinit(argv[0], argv[3])) == NULL)
58 quit(1);
59 putw(COM_RECVM, devout); /* verify initialization */
60 sendstate();
61 fflush(devout);
62 /* loop on requests */
63 while ((com = getc(devin)) != EOF) {
64 if (com >= NREQUESTS || dev_func[com] == NULL) {
65 stderr_v("invalid request\n");
66 quit(1);
67 }
68 (*dev_func[com])(); /* process request */
69 }
70 quit(0); /* all done, clean up and exit */
71 }
72
73
74 quit(code) /* close device and exit */
75 int code;
76 {
77 if (dev != NULL)
78 (*dev->close)();
79 exit(code);
80 }
81
82
83 r_clear() /* clear screen */
84 {
85 int xres, yres;
86
87 xres = getw(devin);
88 yres = getw(devin);
89 (*dev->clear)(xres, yres);
90 }
91
92
93 r_paintr() /* paint a rectangle */
94 {
95 COLOR col;
96 int xmin, ymin, xmax, ymax;
97
98 fread((char *)col, sizeof(COLOR), 1, devin);
99 xmin = getw(devin); ymin = getw(devin);
100 xmax = getw(devin); ymax = getw(devin);
101 (*dev->paintr)(col, xmin, ymin, xmax, ymax);
102 }
103
104
105 r_flush() /* flush output */
106 {
107 if (dev->flush != NULL)
108 (*dev->flush)();
109 putc(COM_FLUSH, devout);
110 sendstate();
111 fflush(devout);
112 }
113
114
115 r_getcur() /* get and return cursor position */
116 {
117 int c;
118 int x, y;
119 /* get it if we can */
120 if (dev->getcur == NULL) {
121 c = ABORT;
122 x = y = 0;
123 } else
124 c = (*dev->getcur)(&x, &y);
125 /* reply */
126 putc(COM_GETCUR, devout);
127 putc(c, devout);
128 putw(x, devout);
129 putw(y, devout);
130 fflush(devout);
131 }
132
133
134 r_comout() /* print string to command line */
135 {
136 char str[256];
137
138 mygets(str, devin);
139 (*dev->comout)(str);
140 }
141
142
143 r_comin() /* read string from command line */
144 {
145 char buf[256], *prompt;
146 /* get prompt */
147 if (getc(devin)) {
148 mygets(buf, devin);
149 prompt = buf;
150 } else
151 prompt = NULL;
152 /* get string */
153 (*dev->comin)(buf, prompt);
154 /* reply */
155 putc(COM_COMIN, devout);
156 myputs(buf, devout);
157 sendstate();
158 fflush(devout);
159 }
160
161
162 mygets(s, fp) /* get string from file (with nul) */
163 register char *s;
164 register FILE *fp;
165 {
166 register int c;
167
168 while ((c = getc(fp)) != EOF)
169 if ((*s++ = c) == '\0')
170 return;
171 *s = '\0';
172 }
173
174
175 myputs(s, fp) /* put string to file (with nul) */
176 register char *s;
177 register FILE *fp;
178 {
179 do
180 putc(*s, fp);
181 while (*s++);
182 }
183
184
185 stderr_v(s) /* put string to stderr */
186 register char *s;
187 {
188 static int midline = 0;
189
190 if (!*s)
191 return;
192 if (!midline++) {
193 fputs(progname, stderr);
194 fputs(": ", stderr);
195 }
196 fputs(s, stderr);
197 if (s[strlen(s)-1] == '\n') {
198 fflush(stderr);
199 midline = 0;
200 }
201 }
202
203
204 sendstate() /* send driver state variables */
205 {
206 fwrite((char *)&dev->pixaspect, sizeof(dev->pixaspect), 1, devout);
207 putw(dev->xsiz, devout);
208 putw(dev->ysiz, devout);
209 putw(dev->inpready, devout);
210 }