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

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: rtmain.c,v 2.8 2003/10/21 19:19:28 schorsch Exp $";
3 #endif
4 /*
5 * rtmain.c - main for rtrace per-ray calculation program
6 */
7
8 #include "copyright.h"
9
10 #include <sys/types.h>
11 #include <signal.h>
12
13 #include "platform.h"
14 #include "rtprocess.h" /* getpid() */
15 #include "resolu.h"
16 #include "ray.h"
17 #include "source.h"
18 #include "ambient.h"
19 #include "random.h"
20 #include "paths.h"
21
22 /* persistent processes define */
23 #ifdef F_SETLKW
24 #define PERSIST 1 /* normal persist */
25 #define PARALLEL 2 /* parallel persist */
26 #define PCHILD 3 /* child of normal persist */
27 #endif
28
29 char *progname; /* argv[0] */
30 char *octname; /* octree name */
31 char *sigerr[NSIG]; /* signal error messages */
32 char *shm_boundary = NULL; /* boundary of shared memory */
33 char *errfile = NULL; /* error output file */
34
35 extern char *formstr(); /* string from format */
36 extern int inform; /* input format */
37 extern int outform; /* output format */
38 extern char *outvals; /* output values */
39
40 extern int hresolu; /* horizontal resolution */
41 extern int vresolu; /* vertical resolution */
42
43 extern int imm_irrad; /* compute immediate irradiance? */
44 extern int lim_dist; /* limit distance? */
45
46 extern char *tralist[]; /* list of modifers to trace (or no) */
47 extern int traincl; /* include == 1, exclude == 0 */
48
49 static void onsig(int signo);
50 static void sigdie(int signo, char *msg);
51 static void printdefaults(void);
52
53
54 int
55 main(int argc, char *argv[])
56 {
57 #define check(ol,al) if (argv[i][ol] || \
58 badarg(argc-i-1,argv+i+1,al)) \
59 goto badopt
60 #define bool(olen,var) switch (argv[i][olen]) { \
61 case '\0': var = !var; break; \
62 case 'y': case 'Y': case 't': case 'T': \
63 case '+': case '1': var = 1; break; \
64 case 'n': case 'N': case 'f': case 'F': \
65 case '-': case '0': var = 0; break; \
66 default: goto badopt; }
67 int loadflags = ~IO_FILES;
68 int persist = 0;
69 char **tralp;
70 int duped1;
71 int rval;
72 int i;
73 /* global program name */
74 progname = argv[0] = fixargv0(argv[0]);
75 /* option city */
76 for (i = 1; i < argc; i++) {
77 /* expand arguments */
78 while ((rval = expandarg(&argc, &argv, i)) > 0)
79 ;
80 if (rval < 0) {
81 sprintf(errmsg, "cannot expand '%s'", argv[i]);
82 error(SYSTEM, errmsg);
83 }
84 if (argv[i] == NULL || argv[i][0] != '-')
85 break; /* break from options */
86 if (!strcmp(argv[i], "-version")) {
87 puts(VersionID);
88 quit(0);
89 }
90 if (!strcmp(argv[i], "-defaults") ||
91 !strcmp(argv[i], "-help")) {
92 printdefaults();
93 quit(0);
94 }
95 rval = getrenderopt(argc-i, argv+i);
96 if (rval >= 0) {
97 i += rval;
98 continue;
99 }
100 switch (argv[i][1]) {
101 case 'x': /* x resolution */
102 check(2,"i");
103 hresolu = atoi(argv[++i]);
104 break;
105 case 'y': /* y resolution */
106 check(2,"i");
107 vresolu = atoi(argv[++i]);
108 break;
109 case 'w': /* warnings */
110 rval = erract[WARNING].pf != NULL;
111 bool(2,rval);
112 if (rval) erract[WARNING].pf = wputs;
113 else erract[WARNING].pf = NULL;
114 break;
115 case 'e': /* error file */
116 check(2,"s");
117 errfile = argv[++i];
118 break;
119 case 'l': /* limit distance */
120 if (argv[i][2] != 'd')
121 goto badopt;
122 bool(3,lim_dist);
123 break;
124 case 'I': /* immed. irradiance */
125 bool(2,imm_irrad);
126 break;
127 case 'f': /* format i/o */
128 switch (argv[i][2]) {
129 case 'a': /* ascii */
130 case 'f': /* float */
131 case 'd': /* double */
132 inform = argv[i][2];
133 break;
134 default:
135 goto badopt;
136 }
137 switch (argv[i][3]) {
138 case '\0':
139 outform = inform;
140 break;
141 case 'a': /* ascii */
142 case 'f': /* float */
143 case 'd': /* double */
144 case 'c': /* color */
145 check(4,"");
146 outform = argv[i][3];
147 break;
148 default:
149 goto badopt;
150 }
151 break;
152 case 'o': /* output */
153 outvals = argv[i]+2;
154 break;
155 case 'h': /* header output */
156 rval = loadflags & IO_INFO;
157 bool(2,rval);
158 loadflags = rval ? loadflags | IO_INFO :
159 loadflags & ~IO_INFO;
160 break;
161 case 't': /* trace */
162 switch (argv[i][2]) {
163 case 'i': /* include */
164 case 'I':
165 check(3,"s");
166 if (traincl != 1) {
167 traincl = 1;
168 tralp = tralist;
169 }
170 if (argv[i][2] == 'I') { /* file */
171 rval = wordfile(tralp,
172 getpath(argv[++i],getrlibpath(),R_OK));
173 if (rval < 0) {
174 sprintf(errmsg,
175 "cannot open trace include file \"%s\"",
176 argv[i]);
177 error(SYSTEM, errmsg);
178 }
179 tralp += rval;
180 } else {
181 *tralp++ = argv[++i];
182 *tralp = NULL;
183 }
184 break;
185 case 'e': /* exclude */
186 case 'E':
187 check(3,"s");
188 if (traincl != 0) {
189 traincl = 0;
190 tralp = tralist;
191 }
192 if (argv[i][2] == 'E') { /* file */
193 rval = wordfile(tralp,
194 getpath(argv[++i],getrlibpath(),R_OK));
195 if (rval < 0) {
196 sprintf(errmsg,
197 "cannot open trace exclude file \"%s\"",
198 argv[i]);
199 error(SYSTEM, errmsg);
200 }
201 tralp += rval;
202 } else {
203 *tralp++ = argv[++i];
204 *tralp = NULL;
205 }
206 break;
207 default:
208 goto badopt;
209 }
210 break;
211 #ifdef PERSIST
212 case 'P': /* persist file */
213 if (argv[i][2] == 'P') {
214 check(3,"s");
215 persist = PARALLEL;
216 } else {
217 check(2,"s");
218 persist = PERSIST;
219 }
220 persistfile(argv[++i]);
221 break;
222 #endif
223 default:
224 goto badopt;
225 }
226 }
227 /* initialize object types */
228 initotypes();
229 /* initialize urand */
230 initurand(2048);
231 /* set up signal handling */
232 sigdie(SIGINT, "Interrupt");
233 #ifdef SIGHUP
234 sigdie(SIGHUP, "Hangup");
235 #endif
236 sigdie(SIGTERM, "Terminate");
237 #ifdef SIGPIPE
238 sigdie(SIGPIPE, "Broken pipe");
239 #endif
240 #ifdef SIGALRM
241 sigdie(SIGALRM, "Alarm clock");
242 #endif
243 #ifdef SIGXCPU
244 sigdie(SIGXCPU, "CPU limit exceeded");
245 sigdie(SIGXFSZ, "File size exceeded");
246 #endif
247 /* open error file */
248 if (errfile != NULL) {
249 if (freopen(errfile, "a", stderr) == NULL)
250 quit(2);
251 fprintf(stderr, "**************\n*** PID %5d: ",
252 getpid());
253 printargs(argc, argv, stderr);
254 putc('\n', stderr);
255 fflush(stderr);
256 }
257 #ifdef NICE
258 nice(NICE); /* lower priority */
259 #endif
260 /* get octree */
261 if (i == argc)
262 octname = NULL;
263 else if (i == argc-1)
264 octname = argv[i];
265 else
266 goto badopt;
267 if (octname == NULL)
268 error(USER, "missing octree argument");
269 /* set up output */
270 #ifdef PERSIST
271 if (persist) {
272 duped1 = dup(fileno(stdout)); /* don't lose our output */
273 openheader();
274 }
275 #endif
276 #ifdef _WIN32
277 if (outform != 'a')
278 SET_FILE_BINARY(stdout);
279 if (octname == NULL)
280 SET_FILE_BINARY(stdin);
281 #endif
282 readoct(octname, loadflags, &thescene, NULL);
283 nsceneobjs = nobjects;
284
285 if (loadflags & IO_INFO) { /* print header */
286 printargs(i, argv, stdout);
287 printf("SOFTWARE= %s\n", VersionID);
288 fputnow(stdout);
289 fputformat(formstr(outform), stdout);
290 putchar('\n');
291 }
292
293 marksources(); /* find and mark sources */
294
295 setambient(); /* initialize ambient calculation */
296
297 #ifdef PERSIST
298 if (persist) {
299 fflush(stdout);
300 /* reconnect stdout */
301 dup2(duped1, fileno(stdout));
302 close(duped1);
303 if (persist == PARALLEL) { /* multiprocessing */
304 preload_objs(); /* preload scene */
305 shm_boundary = (char *)malloc(16);
306 strcpy(shm_boundary, "SHM_BOUNDARY");
307 while ((rval=fork()) == 0) { /* keep on forkin' */
308 pflock(1);
309 pfhold();
310 }
311 if (rval < 0)
312 error(SYSTEM, "cannot fork child for persist function");
313 pfdetach(); /* parent exits */
314 }
315 }
316 runagain:
317 if (persist)
318 dupheader(); /* send header to stdout */
319 #endif
320 /* trace rays */
321 rtrace(NULL);
322 /* flush ambient file */
323 ambsync();
324 #ifdef PERSIST
325 if (persist == PERSIST) { /* first run-through */
326 if ((rval=fork()) == 0) { /* child loops until killed */
327 pflock(1);
328 persist = PCHILD;
329 } else { /* original process exits */
330 if (rval < 0)
331 error(SYSTEM, "cannot fork child for persist function");
332 pfdetach(); /* parent exits */
333 }
334 }
335 if (persist == PCHILD) { /* wait for a signal then go again */
336 close(duped1); /* release output handle */
337 pfhold();
338 raynum = nrays = 0; /* reinitialize */
339 goto runagain;
340 }
341 #endif
342 quit(0);
343
344 badopt:
345 sprintf(errmsg, "command line error at '%s'", argv[i]);
346 error(USER, errmsg);
347 return 1; /* pro forma return */
348
349 #undef check
350 #undef bool
351 }
352
353
354 void
355 wputs( /* warning output function */
356 char *s
357 )
358 {
359 int lasterrno = errno;
360 eputs(s);
361 errno = lasterrno;
362 }
363
364
365 void
366 eputs( /* put string to stderr */
367 register char *s
368 )
369 {
370 static int midline = 0;
371
372 if (!*s)
373 return;
374 if (!midline++) {
375 fputs(progname, stderr);
376 fputs(": ", stderr);
377 }
378 fputs(s, stderr);
379 if (s[strlen(s)-1] == '\n') {
380 fflush(stderr);
381 midline = 0;
382 }
383 }
384
385
386 static void
387 onsig( /* fatal signal */
388 int signo
389 )
390 {
391 static int gotsig = 0;
392
393 if (gotsig++) /* two signals and we're gone! */
394 _exit(signo);
395
396 #ifdef SIGALRM
397 alarm(15); /* allow 15 seconds to clean up */
398 signal(SIGALRM, SIG_DFL); /* make certain we do die */
399 #endif
400 eputs("signal - ");
401 eputs(sigerr[signo]);
402 eputs("\n");
403 quit(3);
404 }
405
406
407 static void
408 sigdie( /* set fatal signal */
409 int signo,
410 char *msg
411 )
412 {
413 if (signal(signo, onsig) == SIG_IGN)
414 signal(signo, SIG_IGN);
415 sigerr[signo] = msg;
416 }
417
418
419 static void
420 printdefaults(void) /* print default values to stdout */
421 {
422 register char *cp;
423
424 if (imm_irrad)
425 printf("-I+\t\t\t\t# immediate irradiance on\n");
426 printf("-x %-9d\t\t\t# x resolution\n", hresolu);
427 printf("-y %-9d\t\t\t# y resolution\n", vresolu);
428 printf(lim_dist ? "-ld+\t\t\t\t# limit distance on\n" :
429 "-ld-\t\t\t\t# limit distance off\n");
430 printf("-f%c%c\t\t\t\t# format input/output = %s/%s\n",
431 inform, outform, formstr(inform), formstr(outform));
432 printf("-o%s\t\t\t\t# output", outvals);
433 for (cp = outvals; *cp; cp++)
434 switch (*cp) {
435 case 't': printf(" trace"); break;
436 case 'o': printf(" origin"); break;
437 case 'd': printf(" direction"); break;
438 case 'v': printf(" value"); break;
439 case 'l': printf(" length"); break;
440 case 'L': printf(" first_length"); break;
441 case 'p': printf(" point"); break;
442 case 'n': printf(" normal"); break;
443 case 'N': printf(" unperturbed_normal"); break;
444 case 's': printf(" surface"); break;
445 case 'w': printf(" weight"); break;
446 case 'm': printf(" modifier"); break;
447 }
448 putchar('\n');
449 printf(erract[WARNING].pf != NULL ?
450 "-w+\t\t\t\t# warning messages on\n" :
451 "-w-\t\t\t\t# warning messages off\n");
452 print_rdefaults();
453 }