ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/rt/rtmain.c
Revision: 2.6
Committed: Mon Jun 30 14:59:12 2003 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.5: +4 -1 lines
Log Message:
Replaced most outdated BSD function calls with their posix equivalents, and cleaned up a few other platform dependencies.

File Contents

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