ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.129
Committed: Sun Feb 6 17:19:55 2022 UTC (10 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.128: +10 -4 lines
Log Message:
feat(rad): added report of unknown setting on command line

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.129 static const char RCSid[] = "$Id: rad.c,v 2.128 2021/01/21 21:36:28 greg Exp $";
3 greg 2.1 #endif
4     /*
5     * Executive program for oconv, rpict and pfilt
6     */
7    
8     #include "standard.h"
9 schorsch 2.64
10     #include <ctype.h>
11 schorsch 2.69 #include <time.h>
12 greg 2.119 #include <signal.h>
13 schorsch 2.64
14     #include "platform.h"
15 schorsch 2.74 #include "rtprocess.h"
16 gregl 2.55 #include "view.h"
17 greg 2.1 #include "paths.h"
18 greg 2.48 #include "vars.h"
19 greg 2.1
20 schorsch 2.124 #if defined(_WIN32) || defined(_WIN64)
21 schorsch 2.69 #define DELCMD "del"
22     #define RENAMECMD "rename"
23     #else
24     #define DELCMD "rm -f"
25     #define RENAMECMD "mv"
26 schorsch 2.76 #include <sys/types.h>
27     #include <sys/wait.h>
28 greg 2.111 #include <signal.h>
29 schorsch 2.69 #endif
30    
31 gwlarson 2.56 /* variables (alphabetical by name) */
32     #define AMBFILE 0 /* ambient file name */
33     #define DETAIL 1 /* level of scene detail */
34     #define EXPOSURE 2 /* picture exposure setting */
35 gwlarson 2.57 #define EYESEP 3 /* interocular distance */
36     #define ILLUM 4 /* mkillum input files */
37     #define INDIRECT 5 /* indirection in lighting */
38     #define MATERIAL 6 /* material files */
39     #define MKILLUM 7 /* mkillum options */
40 greg 2.112 #define MKPMAP 8 /* mkpmap options */
41     #define OBJECT 9 /* object files */
42     #define OCONV 10 /* oconv options */
43     #define OCTREE 11 /* octree file name */
44     #define OPTFILE 12 /* rendering options file */
45     #define PCMAP 13 /* caustic photon map */
46     #define PENUMBRAS 14 /* shadow penumbras are desired */
47     #define PFILT 15 /* pfilt options */
48     #define PGMAP 16 /* global photon map */
49     #define PICTURE 17 /* picture file root name */
50     #define QUALITY 18 /* desired rendering quality */
51     #define RAWFILE 19 /* raw picture file root name */
52     #define RENDER 20 /* rendering options */
53     #define REPORT 21 /* report frequency and errfile */
54     #define RESOLUTION 22 /* maximum picture resolution */
55     #define RPICT 23 /* rpict parameters */
56     #define RVU 24 /* rvu parameters */
57     #define SCENE 25 /* scene files */
58     #define UP 26 /* view up (X, Y or Z) */
59     #define VARIABILITY 27 /* level of light variability */
60     #define VIEWS 28 /* view(s) for picture(s) */
61     #define ZFILE 29 /* distance file root name */
62     #define ZONE 30 /* simulation zone */
63 greg 2.1 /* total number of variables */
64 greg 2.112 int NVARS = 31;
65 greg 2.1
66 greg 2.48 VARIABLE vv[] = { /* variable-value pairs */
67 gwlarson 2.56 {"AMBFILE", 3, 0, NULL, onevalue},
68     {"DETAIL", 3, 0, NULL, qualvalue},
69     {"EXPOSURE", 3, 0, NULL, fltvalue},
70 gwlarson 2.57 {"EYESEP", 3, 0, NULL, fltvalue},
71 gwlarson 2.56 {"illum", 3, 0, NULL, catvalues},
72     {"INDIRECT", 3, 0, NULL, intvalue},
73 greg 2.1 {"materials", 3, 0, NULL, catvalues},
74 greg 2.26 {"mkillum", 3, 0, NULL, catvalues},
75 greg 2.112 {"mkpmap", 3, 0, NULL, catvalues},
76 gwlarson 2.56 {"objects", 3, 0, NULL, catvalues},
77 greg 2.1 {"oconv", 3, 0, NULL, catvalues},
78 gwlarson 2.56 {"OCTREE", 3, 0, NULL, onevalue},
79     {"OPTFILE", 3, 0, NULL, onevalue},
80 greg 2.112 {"PCMAP", 2, 0, NULL, onevalue},
81 gwlarson 2.56 {"PENUMBRAS", 3, 0, NULL, boolvalue},
82 greg 2.1 {"pfilt", 2, 0, NULL, catvalues},
83 greg 2.112 {"PGMAP", 2, 0, NULL, onevalue},
84 gwlarson 2.56 {"PICTURE", 3, 0, NULL, onevalue},
85 greg 2.30 {"QUALITY", 3, 0, NULL, qualvalue},
86 gwlarson 2.56 {"RAWFILE", 3, 0, NULL, onevalue},
87     {"render", 3, 0, NULL, catvalues},
88     {"REPORT", 3, 0, NULL, onevalue},
89 greg 2.1 {"RESOLUTION", 3, 0, NULL, onevalue},
90 greg 2.91 {"rpict", 3, 0, NULL, catvalues},
91     {"rvu", 3, 0, NULL, catvalues},
92 gwlarson 2.56 {"scene", 3, 0, NULL, catvalues},
93 greg 2.1 {"UP", 2, 0, NULL, onevalue},
94 greg 2.30 {"VARIABILITY", 3, 0, NULL, qualvalue},
95 gwlarson 2.56 {"view", 2, 0, NULL, NULL},
96 greg 2.45 {"ZFILE", 2, 0, NULL, onevalue},
97 gwlarson 2.56 {"ZONE", 2, 0, NULL, onevalue},
98 greg 2.1 };
99    
100 greg 2.6 /* overture calculation file */
101 schorsch 2.65 #ifdef NULL_DEVICE
102     char overfile[] = NULL_DEVICE;
103     #else
104 greg 2.45 char overfile[] = "overture.unf";
105 greg 2.6 #endif
106    
107 greg 2.1
108 greg 2.35 time_t scenedate; /* date of latest scene or object file */
109     time_t octreedate; /* date of octree */
110     time_t matdate; /* date of latest material file */
111     time_t illumdate; /* date of last illum file */
112 greg 2.1
113 greg 2.26 char *oct0name; /* name of pre-mkillum octree */
114 greg 2.35 time_t oct0date; /* date of pre-mkillum octree */
115 greg 2.26 char *oct1name; /* name of post-mkillum octree */
116 greg 2.35 time_t oct1date; /* date of post-mkillum octree (>= matdate) */
117 greg 2.26
118 greg 2.112 char *pgmapname; /* name of global photon map */
119     time_t pgmapdate; /* date of global photon map (>= oct1date) */
120     char *pcmapname; /* name of caustic photon map */
121     time_t pcmapdate; /* date of caustic photon map (>= oct1date) */
122    
123 greg 2.37 int nowarn = 0; /* no warnings */
124 greg 2.1 int explicate = 0; /* explicate variables */
125     int silent = 0; /* do work silently */
126 greg 2.41 int touchonly = 0; /* touch files only */
127 greg 2.66 int nprocs = 1; /* maximum executing processes */
128 greg 2.21 int sayview = 0; /* print view out */
129 greg 2.75 char *rvdevice = NULL; /* rvu output device */
130 greg 2.1 char *viewselect = NULL; /* specific view only */
131    
132 greg 2.93 #define DEF_RPICT_PATH "rpict" /* default rpict path */
133    
134 greg 2.91 /* command paths */
135     char c_oconv[256] = "oconv";
136     char c_mkillum[256] = "mkillum";
137 greg 2.112 char c_mkpmap[256] = "mkpmap";
138 greg 2.91 char c_rvu[256] = "rvu";
139 greg 2.93 char c_rpict[256] = DEF_RPICT_PATH;
140     char c_rpiece[] = "rpiece";
141 greg 2.91 char c_pfilt[256] = "pfilt";
142    
143 greg 2.1 int overture = 0; /* overture calculation needed */
144    
145 greg 2.66 int children_running = 0; /* set negative in children */
146    
147 greg 2.1 char *progname; /* global argv[0] */
148 greg 2.22 char *rifname; /* global rad input file name */
149 greg 2.1
150 schorsch 2.65 char radname[PATH_MAX]; /* root Radiance file name */
151 greg 2.1
152 greg 2.67 #define inchild() (children_running < 0)
153    
154 schorsch 2.76 static void rootname(char *rn, char *fn);
155     static time_t checklast(char *fnames);
156     static char * newfname(char *orig, int pred);
157     static void checkfiles(void);
158     static void getoctcube(double org[3], double *sizp);
159     static void setdefaults(void);
160     static void oconv(void);
161 greg 2.112 static void mkpmap(void);
162 schorsch 2.76 static char * addarg(char *op, char *arg);
163     static void oconvopts(char *oo);
164     static void mkillumopts(char *mo);
165 greg 2.112 static void mkpmapopts(char *mo);
166 schorsch 2.76 static void checkambfile(void);
167     static double ambval(void);
168     static void renderopts(char *op, char *po);
169     static void lowqopts(char *op, char *po);
170     static void medqopts(char *op, char *po);
171     static void hiqopts(char *op, char *po);
172     static void xferopts(char *ro);
173     static void pfiltopts(char *po);
174     static int matchword(char *s1, char *s2);
175     static char * specview(char *vs);
176     static char * getview(int n, char *vn);
177     static int myprintview(char *vopts, FILE *fp);
178     static void rvu(char *opts, char *po);
179     static void rpict(char *opts, char *po);
180     static int touch(char *fn);
181     static int runcom(char *cs);
182     static int rmfile(char *fn);
183     static int mvfile(char *fold, char *fnew);
184 greg 2.98 static int next_process(int reserve);
185 schorsch 2.76 static void wait_process(int all);
186     static void finish_process(void);
187     static void badvalue(int vc);
188     static void syserr(char *s);
189 greg 2.1
190 schorsch 2.76
191     int
192     main(
193     int argc,
194     char *argv[]
195     )
196 greg 2.1 {
197     char ropts[512];
198 greg 2.38 char popts[64];
199 greg 2.1 int i;
200    
201     progname = argv[0];
202     /* get options */
203     for (i = 1; i < argc && argv[i][0] == '-'; i++)
204     switch (argv[i][1]) {
205     case 's':
206     silent++;
207     break;
208     case 'n':
209 greg 2.66 nprocs = 0;
210     break;
211     case 'N':
212     nprocs = atoi(argv[++i]);
213     if (nprocs < 0)
214     nprocs = 0;
215 greg 2.1 break;
216 greg 2.41 case 't':
217     touchonly++;
218     break;
219 greg 2.1 case 'e':
220     explicate++;
221     break;
222     case 'o':
223     rvdevice = argv[++i];
224     break;
225 greg 2.20 case 'V':
226 greg 2.21 sayview++;
227     break;
228 greg 2.1 case 'v':
229     viewselect = argv[++i];
230     break;
231 greg 2.37 case 'w':
232     nowarn++;
233     break;
234 greg 2.1 default:
235     goto userr;
236     }
237     if (i >= argc)
238     goto userr;
239 greg 2.22 rifname = argv[i];
240 greg 2.1 /* assign Radiance root file name */
241 greg 2.22 rootname(radname, rifname);
242 greg 2.1 /* load variable values */
243 greg 2.48 loadvars(rifname);
244 greg 2.1 /* get any additional assignments */
245 greg 2.129 for (i++; i < argc; i++) {
246     int rv = setvariable(argv[i], matchvar);
247     if (rv < 0) {
248     fprintf(stderr, "%s: unknown setting: %s\n",
249 greg 2.53 progname, argv[i]);
250     quit(1);
251     }
252 greg 2.129 if (!rv)
253     fprintf(stderr,
254     "%s: bad variable assignment: %s (ignored)\n",
255     progname, argv[i]);
256     }
257 greg 2.1 /* check assignments */
258     checkvalues();
259     /* check files and dates */
260     checkfiles();
261     /* set default values as necessary */
262     setdefaults();
263     /* print all values if requested */
264     if (explicate)
265 greg 2.48 printvars(stdout);
266 greg 2.26 /* build octree (and run mkillum) */
267 greg 2.7 oconv();
268 greg 2.112 /* run mkpmap if indicated */
269     mkpmap();
270 greg 2.7 /* check date on ambient file */
271     checkambfile();
272 greg 2.1 /* run simulation */
273 greg 2.38 renderopts(ropts, popts);
274 greg 2.1 xferopts(ropts);
275     if (rvdevice != NULL)
276 greg 2.75 rvu(ropts, popts);
277 greg 2.1 else
278 greg 2.38 rpict(ropts, popts);
279 greg 2.53 quit(0);
280 greg 2.1 userr:
281     fprintf(stderr,
282 greg 2.66 "Usage: %s [-w][-s][-n|-N npr][-t][-e][-V][-v view][-o dev] rfile [VAR=value ..]\n",
283 greg 2.1 progname);
284 greg 2.53 quit(1);
285 schorsch 2.76 return 1; /* pro forma return */
286 greg 2.1 }
287    
288    
289 schorsch 2.76 static void
290     rootname( /* remove tail from end of fn */
291 greg 2.109 char *rn,
292     char *fn
293 schorsch 2.76 )
294 greg 2.1 {
295     char *tp, *dp;
296    
297 schorsch 2.71 for (tp = NULL, dp = rn; (*rn = *fn++); rn++)
298 greg 2.1 if (ISDIRSEP(*rn))
299     dp = rn;
300     else if (*rn == '.')
301     tp = rn;
302     if (tp != NULL && tp > dp)
303     *tp = '\0';
304     }
305    
306    
307 schorsch 2.76 static time_t
308     checklast( /* check files and find most recent */
309 greg 2.109 char *fnames
310 schorsch 2.76 )
311 greg 2.1 {
312 schorsch 2.65 char thisfile[PATH_MAX];
313 greg 2.35 time_t thisdate, lastdate = 0;
314 greg 2.1
315 greg 2.26 if (fnames == NULL)
316     return(0);
317 schorsch 2.65 while ((fnames = nextword(thisfile, PATH_MAX, fnames)) != NULL) {
318 gwlarson 2.60 if (thisfile[0] == '!' ||
319 greg 2.77 (thisfile[0] == '\\' && thisfile[1] == '!')) {
320     if (!lastdate)
321     lastdate = 1;
322 gwlarson 2.60 continue;
323 greg 2.77 }
324 greg 2.26 if (!(thisdate = fdate(thisfile)))
325 greg 2.3 syserr(thisfile);
326 greg 2.1 if (thisdate > lastdate)
327     lastdate = thisdate;
328     }
329     return(lastdate);
330     }
331    
332    
333 schorsch 2.76 static char *
334     newfname( /* create modified file name */
335     char *orig,
336     int pred
337     )
338 greg 2.26 {
339 greg 2.109 char *cp;
340     int n;
341 greg 2.26 int suffix;
342    
343 greg 2.27 n = 0; cp = orig; suffix = -1; /* suffix position, length */
344     while (*cp) {
345     if (*cp == '.') suffix = n;
346     else if (ISDIRSEP(*cp)) suffix = -1;
347     cp++; n++;
348     }
349     if (suffix == -1) suffix = n;
350 greg 2.26 if ((cp = bmalloc(n+2)) == NULL)
351     syserr(progname);
352     strncpy(cp, orig, suffix);
353     cp[suffix] = pred; /* root name + pred + suffix */
354     strcpy(cp+suffix+1, orig+suffix);
355     return(cp);
356     }
357    
358    
359 schorsch 2.76 static void
360     checkfiles(void) /* check for existence and modified times */
361 greg 2.1 {
362 greg 2.112 char fntemp[256];
363 greg 2.35 time_t objdate;
364 greg 2.1
365 greg 2.2 if (!vdef(OCTREE)) {
366 greg 2.26 if ((vval(OCTREE) = bmalloc(strlen(radname)+5)) == NULL)
367 greg 2.3 syserr(progname);
368 greg 2.26 sprintf(vval(OCTREE), "%s.oct", radname);
369 greg 2.2 vdef(OCTREE)++;
370 greg 2.61 } else if (vval(OCTREE)[0] == '!') {
371     fprintf(stderr, "%s: illegal '%s' specification\n",
372     progname, vnam(OCTREE));
373     quit(1);
374 greg 2.2 }
375     octreedate = fdate(vval(OCTREE));
376 greg 2.26 if (vdef(ILLUM)) { /* illum requires secondary octrees */
377     oct0name = newfname(vval(OCTREE), '0');
378     oct1name = newfname(vval(OCTREE), '1');
379     oct0date = fdate(oct0name);
380     oct1date = fdate(oct1name);
381     } else
382     oct0name = oct1name = vval(OCTREE);
383     if ((scenedate = checklast(vval(SCENE))) &&
384     (objdate = checklast(vval(OBJECT))) > scenedate)
385     scenedate = objdate;
386     illumdate = checklast(vval(ILLUM));
387     if (!octreedate & !scenedate & !illumdate) {
388     fprintf(stderr, "%s: need '%s' or '%s' or '%s'\n", progname,
389     vnam(OCTREE), vnam(SCENE), vnam(ILLUM));
390 greg 2.53 quit(1);
391 greg 2.1 }
392 greg 2.112 if (vdef(PGMAP)) {
393     if (!*sskip2(vval(PGMAP),1)) {
394     fprintf(stderr, "%s: '%s' missing # photons argument\n",
395     progname, vnam(PGMAP));
396     quit(1);
397     }
398     atos(fntemp, sizeof(fntemp), vval(PGMAP));
399     pgmapname = savqstr(fntemp);
400     pgmapdate = fdate(pgmapname);
401     }
402     if (vdef(PCMAP)) {
403     if (!*sskip2(vval(PCMAP),1)) {
404     fprintf(stderr, "%s: '%s' missing # photons argument\n",
405     progname, vnam(PCMAP));
406     quit(1);
407     }
408     atos(fntemp, sizeof(fntemp), vval(PCMAP));
409     pcmapname = savqstr(fntemp);
410     pcmapdate = fdate(pcmapname);
411     }
412 greg 2.26 matdate = checklast(vval(MATERIAL));
413 greg 2.1 }
414    
415    
416 schorsch 2.76 static void
417     getoctcube( /* get octree bounding cube */
418     double org[3],
419     double *sizp
420     )
421 greg 2.1 {
422 greg 2.2 static double oorg[3], osiz = 0.;
423 greg 2.26 double min[3], max[3];
424 greg 2.46 char buf[1024];
425 greg 2.1 FILE *fp;
426 greg 2.109 int i;
427 greg 2.1
428 schorsch 2.70 if (osiz <= FTINY) {
429 greg 2.66 if (!nprocs && fdate(oct1name) <
430 greg 2.26 (scenedate>illumdate?scenedate:illumdate)) {
431     /* run getbbox */
432     sprintf(buf, "getbbox -w -h %s",
433     vdef(SCENE) ? vval(SCENE) : vval(ILLUM));
434     if ((fp = popen(buf, "r")) == NULL)
435     syserr("getbbox");
436     if (fscanf(fp, "%lf %lf %lf %lf %lf %lf",
437     &min[0], &max[0], &min[1], &max[1],
438     &min[2], &max[2]) != 6) {
439     fprintf(stderr,
440     "%s: error reading bounding box from getbbox\n",
441     progname);
442 greg 2.53 quit(1);
443 greg 2.26 }
444     for (i = 0; i < 3; i++)
445     if (max[i] - min[i] > osiz)
446     osiz = max[i] - min[i];
447     for (i = 0; i < 3; i++)
448     oorg[i] = (max[i]+min[i]-osiz)*.5;
449     pclose(fp);
450     } else { /* from octree */
451     oconv(); /* does nothing if done already */
452     sprintf(buf, "getinfo -d < %s", oct1name);
453     if ((fp = popen(buf, "r")) == NULL)
454     syserr("getinfo");
455     if (fscanf(fp, "%lf %lf %lf %lf", &oorg[0], &oorg[1],
456     &oorg[2], &osiz) != 4) {
457     fprintf(stderr,
458 greg 2.2 "%s: error reading bounding cube from getinfo\n",
459 greg 2.26 progname);
460 greg 2.53 quit(1);
461 greg 2.26 }
462     pclose(fp);
463 greg 2.2 }
464 schorsch 2.70 }
465 greg 2.2 org[0] = oorg[0]; org[1] = oorg[1]; org[2] = oorg[2]; *sizp = osiz;
466     }
467    
468    
469 schorsch 2.76 static void
470     setdefaults(void) /* set default values for unassigned var's */
471 greg 2.2 {
472 gwlarson 2.58 double org[3], lim[3], size;
473 greg 2.2 char buf[128];
474    
475 greg 2.1 if (!vdef(ZONE)) {
476 greg 2.2 getoctcube(org, &size);
477     sprintf(buf, "E %g %g %g %g %g %g", org[0], org[0]+size,
478     org[1], org[1]+size, org[2], org[2]+size);
479 greg 2.1 vval(ZONE) = savqstr(buf);
480     vdef(ZONE)++;
481 gwlarson 2.58 }
482     if (!vdef(EYESEP)) {
483     if (sscanf(vval(ZONE), "%*s %lf %lf %lf %lf %lf %lf",
484     &org[0], &lim[0], &org[1], &lim[1],
485     &org[2], &lim[2]) != 6)
486     badvalue(ZONE);
487     sprintf(buf, "%f",
488     0.01*(lim[0]-org[0]+lim[1]-org[1]+lim[2]-org[2]));
489     vval(EYESEP) = savqstr(buf);
490     vdef(EYESEP)++;
491 greg 2.1 }
492     if (!vdef(INDIRECT)) {
493     vval(INDIRECT) = "0";
494     vdef(INDIRECT)++;
495     }
496     if (!vdef(QUALITY)) {
497     vval(QUALITY) = "L";
498     vdef(QUALITY)++;
499     }
500     if (!vdef(RESOLUTION)) {
501     vval(RESOLUTION) = "512";
502     vdef(RESOLUTION)++;
503     }
504     if (!vdef(PICTURE)) {
505     vval(PICTURE) = radname;
506     vdef(PICTURE)++;
507     }
508 gregl 2.55 if (!vdef(VIEWS)) {
509     vval(VIEWS) = "X";
510     vdef(VIEWS)++;
511 greg 2.1 }
512     if (!vdef(DETAIL)) {
513     vval(DETAIL) = "M";
514     vdef(DETAIL)++;
515     }
516     if (!vdef(PENUMBRAS)) {
517     vval(PENUMBRAS) = "F";
518     vdef(PENUMBRAS)++;
519     }
520     if (!vdef(VARIABILITY)) {
521     vval(VARIABILITY) = "L";
522     vdef(VARIABILITY)++;
523     }
524     }
525    
526    
527 schorsch 2.76 static void
528     oconv(void) /* run oconv and mkillum if necessary */
529 greg 2.1 {
530 greg 2.26 static char illumtmp[] = "ilXXXXXX";
531 greg 2.78 char combuf[PATH_MAX], ocopts[64], mkopts[1024];
532 greg 2.1
533 greg 2.26 oconvopts(ocopts); /* get options */
534     if (octreedate < scenedate) { /* check date on original octree */
535 greg 2.41 if (touchonly && octreedate)
536     touch(vval(OCTREE));
537     else { /* build command */
538     if (vdef(MATERIAL))
539 greg 2.91 sprintf(combuf, "%s%s %s %s > %s", c_oconv,
540     ocopts, vval(MATERIAL),
541     vval(SCENE), vval(OCTREE));
542 greg 2.41 else
543 greg 2.91 sprintf(combuf, "%s%s %s > %s", c_oconv, ocopts,
544 greg 2.41 vval(SCENE), vval(OCTREE));
545    
546     if (runcom(combuf)) { /* run it */
547     fprintf(stderr,
548 greg 2.26 "%s: error generating octree\n\t%s removed\n",
549 greg 2.41 progname, vval(OCTREE));
550     unlink(vval(OCTREE));
551 greg 2.53 quit(1);
552 greg 2.41 }
553 greg 2.26 }
554 greg 2.35 octreedate = time((time_t *)NULL);
555 greg 2.40 if (octreedate < scenedate) /* in case clock is off */
556     octreedate = scenedate;
557 greg 2.26 }
558     if (oct1name == vval(OCTREE)) /* no mkillum? */
559     oct1date = octreedate > matdate ? octreedate : matdate;
560 schorsch 2.71 if ((oct1date >= octreedate) & (oct1date >= matdate)
561     & (oct1date >= illumdate)) /* all done */
562 greg 2.1 return;
563 greg 2.26 /* make octree0 */
564 schorsch 2.71 if ((oct0date < scenedate) | (oct0date < illumdate)) {
565 greg 2.126 if (touchonly && (oct0date || oct1date)) {
566     if (oct0date)
567     touch(oct0name);
568     } else { /* build command */
569 greg 2.41 if (octreedate)
570 greg 2.91 sprintf(combuf, "%s%s -i %s %s > %s", c_oconv,
571     ocopts, vval(OCTREE),
572     vval(ILLUM), oct0name);
573 greg 2.41 else if (vdef(MATERIAL))
574 greg 2.91 sprintf(combuf, "%s%s %s %s > %s", c_oconv,
575     ocopts, vval(MATERIAL),
576     vval(ILLUM), oct0name);
577 greg 2.41 else
578 greg 2.91 sprintf(combuf, "%s%s %s > %s", c_oconv,
579     ocopts, vval(ILLUM), oct0name);
580 greg 2.41 if (runcom(combuf)) { /* run it */
581     fprintf(stderr,
582     "%s: error generating octree\n\t%s removed\n",
583     progname, oct0name);
584     unlink(oct0name);
585 greg 2.53 quit(1);
586 greg 2.41 }
587     }
588     oct0date = time((time_t *)NULL);
589     if (oct0date < octreedate) /* in case clock is off */
590     oct0date = octreedate;
591     if (oct0date < illumdate) /* ditto */
592     oct0date = illumdate;
593 greg 2.126 }
594 greg 2.41 if (touchonly && oct1date)
595     touch(oct1name);
596     else {
597     mkillumopts(mkopts); /* build mkillum command */
598     mktemp(illumtmp);
599 greg 2.91 sprintf(combuf, "%s%s %s \"<\" %s > %s", c_mkillum, mkopts,
600 greg 2.41 oct0name, vval(ILLUM), illumtmp);
601     if (runcom(combuf)) { /* run it */
602 greg 2.91 fprintf(stderr, "%s: error running %s\n",
603     progname, c_mkillum);
604 greg 2.41 unlink(illumtmp);
605 greg 2.53 quit(1);
606 greg 2.41 }
607 greg 2.108 rmfile(oct0name);
608 greg 2.41 /* make octree1 (frozen) */
609 greg 2.26 if (octreedate)
610 greg 2.91 sprintf(combuf, "%s%s -f -i %s %s > %s", c_oconv,
611     ocopts, vval(OCTREE), illumtmp, oct1name);
612 greg 2.26 else if (vdef(MATERIAL))
613 greg 2.91 sprintf(combuf, "%s%s -f %s %s > %s", c_oconv,
614     ocopts, vval(MATERIAL), illumtmp, oct1name);
615 greg 2.26 else
616 greg 2.91 sprintf(combuf, "%s%s -f %s > %s", c_oconv, ocopts,
617 greg 2.41 illumtmp, oct1name);
618 greg 2.26 if (runcom(combuf)) { /* run it */
619     fprintf(stderr,
620     "%s: error generating octree\n\t%s removed\n",
621 greg 2.41 progname, oct1name);
622     unlink(oct1name);
623 greg 2.46 unlink(illumtmp);
624 greg 2.53 quit(1);
625 greg 2.26 }
626 greg 2.41 rmfile(illumtmp);
627 greg 2.26 }
628 greg 2.35 oct1date = time((time_t *)NULL);
629 greg 2.40 if (oct1date < oct0date) /* in case clock is off */
630     oct1date = oct0date;
631 greg 2.1 }
632    
633    
634 greg 2.112 static void
635     mkpmap(void) /* run mkpmap if indicated */
636     {
637     char combuf[2048], *cp;
638     time_t tnow;
639     /* nothing to do? */
640     if ((pgmapname == NULL) | (pgmapdate >= oct1date) &&
641     (pcmapname == NULL) | (pcmapdate >= oct1date))
642     return;
643     /* just update existing file dates? */
644     if (touchonly && (pgmapname == NULL) | (pgmapdate > 0) &&
645     (pcmapname == NULL) | (pcmapdate > 0)) {
646     if (pgmapname != NULL)
647     touch(pgmapname);
648     if (pcmapname != NULL)
649     touch(pcmapname);
650     } else { /* else need to (re)run pkpmap */
651     strcpy(combuf, c_mkpmap);
652     for (cp = combuf; *cp; cp++)
653     ;
654     mkpmapopts(cp);
655 greg 2.118 /* force file overwrite */
656     cp = addarg(cp, "-fo+");
657 greg 2.112 if (vdef(REPORT)) {
658     char errfile[256];
659     int n;
660     double minutes;
661     n = sscanf(vval(REPORT), "%lf %s", &minutes, errfile);
662     if (n == 2)
663     sprintf(cp, " -t %d -e %s", (int)(minutes*60), errfile);
664     else if (n == 1)
665     sprintf(cp, " -t %d", (int)(minutes*60));
666     else
667     badvalue(REPORT);
668     }
669 greg 2.113 if (pgmapname != NULL && pgmapdate < oct1date) {
670 greg 2.112 cp = addarg(cp, "-apg");
671     addarg(cp, vval(PGMAP));
672     cp = sskip(sskip(cp)); /* remove any bandwidth */
673     *cp = '\0';
674     }
675 greg 2.113 if (pcmapname != NULL && pcmapdate < oct1date) {
676 greg 2.112 cp = addarg(cp, "-apc");
677     addarg(cp, vval(PCMAP));
678     cp = sskip(sskip(cp)); /* remove any bandwidth */
679     *cp = '\0';
680     }
681     cp = addarg(cp, oct1name);
682     if (runcom(combuf)) {
683     fprintf(stderr, "%s: error running %s\n",
684     progname, c_mkpmap);
685 greg 2.115 if (pgmapname != NULL && pgmapdate < oct1date)
686 greg 2.112 unlink(pgmapname);
687 greg 2.115 if (pcmapname != NULL && pcmapdate < oct1date)
688 greg 2.112 unlink(pcmapname);
689     quit(1);
690     }
691     }
692     tnow = time((time_t *)NULL);
693     if (pgmapname != NULL)
694     pgmapdate = tnow;
695     if (pcmapname != NULL)
696     pcmapdate = tnow;
697     oct1date = tnow; /* trigger ambient file removal if needed */
698     }
699    
700    
701 schorsch 2.76 static char *
702 greg 2.91 addarg( /* append argument and advance pointer */
703 greg 2.109 char *op,
704     char *arg
705 schorsch 2.76 )
706 greg 2.1 {
707 greg 2.91 while (*op)
708     op++;
709 greg 2.1 *op = ' ';
710 schorsch 2.71 while ( (*++op = *arg++) )
711 greg 2.1 ;
712     return(op);
713     }
714    
715    
716 schorsch 2.76 static void
717     oconvopts( /* get oconv options */
718 greg 2.109 char *oo
719 schorsch 2.76 )
720 greg 2.1 {
721 greg 2.2 /* BEWARE: This may be called via setdefaults(), so no assumptions */
722    
723 greg 2.1 *oo = '\0';
724 greg 2.112 if (!vdef(OCONV))
725     return;
726     if (vval(OCONV)[0] != '-') {
727     atos(c_oconv, sizeof(c_oconv), vval(OCONV));
728     oo = addarg(oo, sskip2(vval(OCONV), 1));
729     } else
730     oo = addarg(oo, vval(OCONV));
731 greg 2.1 }
732    
733    
734 schorsch 2.76 static void
735     mkillumopts( /* get mkillum options */
736 greg 2.78 char *mo
737 schorsch 2.76 )
738 greg 2.26 {
739     /* BEWARE: This may be called via setdefaults(), so no assumptions */
740    
741 greg 2.91 if (nprocs > 1)
742 greg 2.78 sprintf(mo, " -n %d", nprocs);
743 greg 2.91 else
744 greg 2.78 *mo = '\0';
745 greg 2.112 if (!vdef(MKILLUM))
746     return;
747     if (vval(MKILLUM)[0] != '-') {
748     atos(c_mkillum, sizeof(c_mkillum), vval(MKILLUM));
749     mo = addarg(mo, sskip2(vval(MKILLUM), 1));
750     } else
751     mo = addarg(mo, vval(MKILLUM));
752     }
753    
754    
755     static void
756     mkpmapopts( /* get mkpmap options */
757     char *mo
758     )
759     {
760     /* BEWARE: This may be called via setdefaults(), so no assumptions */
761    
762 greg 2.125 if (nprocs > 1)
763     sprintf(mo, " -n %d", nprocs);
764     else
765     *mo = '\0';
766 greg 2.112 if (!vdef(MKPMAP))
767     return;
768     if (vval(MKPMAP)[0] != '-') {
769     atos(c_mkpmap, sizeof(c_mkpmap), vval(MKPMAP));
770     mo = addarg(mo, sskip2(vval(MKPMAP), 1));
771     } else
772     mo = addarg(mo, vval(MKPMAP));
773 greg 2.26 }
774    
775    
776 schorsch 2.76 static void
777     checkambfile(void) /* check date on ambient file */
778 greg 2.7 {
779 greg 2.35 time_t afdate;
780 greg 2.7
781 greg 2.11 if (!vdef(AMBFILE))
782     return;
783 greg 2.26 if (!(afdate = fdate(vval(AMBFILE))))
784 greg 2.11 return;
785 schorsch 2.70 if (oct1date > afdate) {
786 greg 2.41 if (touchonly)
787     touch(vval(AMBFILE));
788     else
789     rmfile(vval(AMBFILE));
790 schorsch 2.70 }
791 greg 2.7 }
792    
793    
794 schorsch 2.76 static double
795     ambval(void) /* compute ambient value */
796 greg 2.1 {
797 greg 2.3 if (vdef(EXPOSURE)) {
798 greg 2.2 if (vval(EXPOSURE)[0] == '+' || vval(EXPOSURE)[0] == '-')
799 greg 2.128 return(.18/pow(2.,vflt(EXPOSURE)));
800     return(.18/vflt(EXPOSURE));
801 greg 2.3 }
802 greg 2.2 if (vlet(ZONE) == 'E')
803     return(10.);
804 greg 2.3 if (vlet(ZONE) == 'I')
805 greg 2.2 return(.01);
806 greg 2.3 badvalue(ZONE);
807 schorsch 2.76 return 0; /* pro forma return */
808 greg 2.30 }
809    
810    
811 schorsch 2.76 static void
812     renderopts( /* set rendering options */
813     char *op,
814     char *po
815     )
816 greg 2.30 {
817 greg 2.112 char pmapf[256], *bw;
818    
819 greg 2.121 if (vdef(PGMAP)) {
820 greg 2.122 *op = '\0';
821 greg 2.121 bw = sskip2(vval(PGMAP), 2);
822     atos(pmapf, sizeof(pmapf), vval(PGMAP));
823     op = addarg(addarg(op, "-ap"), pmapf);
824     if (atoi(bw) > 0) op = addarg(op, bw);
825     }
826 greg 2.30 switch(vscale(QUALITY)) {
827     case LOW:
828 greg 2.38 lowqopts(op, po);
829 greg 2.30 break;
830     case MEDIUM:
831 greg 2.38 medqopts(op, po);
832 greg 2.30 break;
833     case HIGH:
834 greg 2.38 hiqopts(op, po);
835 greg 2.30 break;
836     }
837 greg 2.112 if (vdef(PCMAP)) {
838     bw = sskip2(vval(PCMAP), 2);
839     atos(pmapf, sizeof(pmapf), vval(PCMAP));
840     op = addarg(addarg(op, "-ap"), pmapf);
841     if (atoi(bw) > 0) op = addarg(op, bw);
842     }
843 greg 2.127 if (vdef(RENDER)) {
844 greg 2.91 op = addarg(op, vval(RENDER));
845 greg 2.127 bw = strstr(vval(RENDER), "-aa ");
846     if (bw != NULL && atof(bw+4) <= FTINY)
847     overture = 0;
848     }
849 greg 2.91 if (rvdevice != NULL) {
850 greg 2.120 if (vdef(RVU)) {
851 greg 2.91 if (vval(RVU)[0] != '-') {
852     atos(c_rvu, sizeof(c_rvu), vval(RVU));
853     po = addarg(po, sskip2(vval(RVU), 1));
854     } else
855     po = addarg(po, vval(RVU));
856 greg 2.120 }
857 greg 2.91 } else {
858 greg 2.120 if (vdef(RPICT)) {
859 greg 2.91 if (vval(RPICT)[0] != '-') {
860     atos(c_rpict, sizeof(c_rpict), vval(RPICT));
861     po = addarg(po, sskip2(vval(RPICT), 1));
862     } else
863     po = addarg(po, vval(RPICT));
864 greg 2.120 }
865 greg 2.91 }
866 greg 2.2 }
867 greg 2.1
868 greg 2.2
869 schorsch 2.76 static void
870     lowqopts( /* low quality rendering options */
871 greg 2.109 char *op,
872 schorsch 2.76 char *po
873     )
874 greg 2.2 {
875     double d, org[3], siz[3];
876    
877 greg 2.1 *op = '\0';
878 greg 2.38 *po = '\0';
879 greg 2.2 if (sscanf(vval(ZONE), "%*s %lf %lf %lf %lf %lf %lf", &org[0],
880 greg 2.3 &siz[0], &org[1], &siz[1], &org[2], &siz[2]) != 6)
881     badvalue(ZONE);
882 greg 2.2 siz[0] -= org[0]; siz[1] -= org[1]; siz[2] -= org[2];
883 schorsch 2.71 if ((siz[0] <= FTINY) | (siz[1] <= FTINY) | (siz[2] <= FTINY))
884 greg 2.39 badvalue(ZONE);
885 greg 2.2 getoctcube(org, &d);
886     d *= 3./(siz[0]+siz[1]+siz[2]);
887     switch (vscale(DETAIL)) {
888     case LOW:
889 greg 2.38 po = addarg(po, "-ps 16");
890     op = addarg(op, "-dp 64");
891 greg 2.72 sprintf(op, " -ar %d", (int)(8*d));
892 greg 2.2 op += strlen(op);
893     break;
894     case MEDIUM:
895 greg 2.38 po = addarg(po, "-ps 8");
896     op = addarg(op, "-dp 128");
897 greg 2.72 sprintf(op, " -ar %d", (int)(16*d));
898 greg 2.2 op += strlen(op);
899     break;
900     case HIGH:
901 greg 2.38 po = addarg(po, "-ps 4");
902     op = addarg(op, "-dp 256");
903 greg 2.72 sprintf(op, " -ar %d", (int)(32*d));
904 greg 2.2 op += strlen(op);
905     break;
906     }
907 greg 2.38 po = addarg(po, "-pt .16");
908 greg 2.2 if (vbool(PENUMBRAS))
909     op = addarg(op, "-ds .4");
910 greg 2.4 else
911     op = addarg(op, "-ds 0");
912 greg 2.99 op = addarg(op, "-dt .2 -dc .25 -dr 0 -ss 0 -st .5");
913 greg 2.2 if (vdef(AMBFILE)) {
914     sprintf(op, " -af %s", vval(AMBFILE));
915     op += strlen(op);
916     } else
917     overture = 0;
918     switch (vscale(VARIABILITY)) {
919     case LOW:
920 greg 2.72 op = addarg(op, "-aa .3 -ad 256");
921 greg 2.2 break;
922     case MEDIUM:
923 greg 2.72 op = addarg(op, "-aa .25 -ad 512");
924 greg 2.2 break;
925     case HIGH:
926 greg 2.72 op = addarg(op, "-aa .2 -ad 1024");
927 greg 2.2 break;
928     }
929     op = addarg(op, "-as 0");
930     d = ambval();
931     sprintf(op, " -av %.2g %.2g %.2g", d, d, d);
932     op += strlen(op);
933 greg 2.105 op = addarg(op, "-lr 6 -lw .003");
934 greg 2.1 }
935    
936    
937 schorsch 2.76 static void
938     medqopts( /* medium quality rendering options */
939 greg 2.109 char *op,
940 schorsch 2.76 char *po
941     )
942 greg 2.1 {
943 greg 2.47 double d, org[3], siz[3], asz;
944 greg 2.1
945     *op = '\0';
946 greg 2.38 *po = '\0';
947 greg 2.2 if (sscanf(vval(ZONE), "%*s %lf %lf %lf %lf %lf %lf", &org[0],
948 greg 2.3 &siz[0], &org[1], &siz[1], &org[2], &siz[2]) != 6)
949     badvalue(ZONE);
950 greg 2.2 siz[0] -= org[0]; siz[1] -= org[1]; siz[2] -= org[2];
951 schorsch 2.71 if ((siz[0] <= FTINY) | (siz[1] <= FTINY) | (siz[2] <= FTINY))
952 greg 2.39 badvalue(ZONE);
953 greg 2.2 getoctcube(org, &d);
954 greg 2.47 asz = (siz[0]+siz[1]+siz[2])/3.;
955     d /= asz;
956 greg 2.2 switch (vscale(DETAIL)) {
957     case LOW:
958 greg 2.38 po = addarg(po, vbool(PENUMBRAS) ? "-ps 4" : "-ps 8");
959 greg 2.23 op = addarg(op, "-dp 256");
960 greg 2.72 sprintf(op, " -ar %d", (int)(16*d));
961 greg 2.2 op += strlen(op);
962 greg 2.47 sprintf(op, " -ms %.2g", asz/20.);
963     op += strlen(op);
964 greg 2.2 break;
965     case MEDIUM:
966 greg 2.38 po = addarg(po, vbool(PENUMBRAS) ? "-ps 3" : "-ps 6");
967 greg 2.23 op = addarg(op, "-dp 512");
968 greg 2.72 sprintf(op, " -ar %d", (int)(32*d));
969 greg 2.2 op += strlen(op);
970 greg 2.47 sprintf(op, " -ms %.2g", asz/40.);
971     op += strlen(op);
972 greg 2.2 break;
973     case HIGH:
974 greg 2.38 po = addarg(po, vbool(PENUMBRAS) ? "-ps 2" : "-ps 4");
975 greg 2.23 op = addarg(op, "-dp 1024");
976 greg 2.72 sprintf(op, " -ar %d", (int)(64*d));
977 greg 2.2 op += strlen(op);
978 greg 2.47 sprintf(op, " -ms %.2g", asz/80.);
979     op += strlen(op);
980 greg 2.2 break;
981     }
982 greg 2.38 po = addarg(po, "-pt .08");
983 greg 2.4 if (vbool(PENUMBRAS))
984 greg 2.89 op = addarg(op, "-ds .2 -dj .9");
985 greg 2.4 else
986 greg 2.2 op = addarg(op, "-ds .3");
987 greg 2.100 op = addarg(op, "-dt .1 -dc .5 -dr 1 -ss 1 -st .1");
988 schorsch 2.71 if ( (overture = vint(INDIRECT)) ) {
989 greg 2.5 sprintf(op, " -ab %d", overture);
990     op += strlen(op);
991     }
992 greg 2.2 if (vdef(AMBFILE)) {
993     sprintf(op, " -af %s", vval(AMBFILE));
994     op += strlen(op);
995     } else
996     overture = 0;
997     switch (vscale(VARIABILITY)) {
998     case LOW:
999 greg 2.72 op = addarg(op, "-aa .2 -ad 329 -as 42");
1000 greg 2.2 break;
1001     case MEDIUM:
1002 greg 2.72 op = addarg(op, "-aa .15 -ad 800 -as 128");
1003 greg 2.2 break;
1004     case HIGH:
1005 greg 2.72 op = addarg(op, "-aa .1 -ad 1536 -as 392");
1006 greg 2.2 break;
1007     }
1008     d = ambval();
1009     sprintf(op, " -av %.2g %.2g %.2g", d, d, d);
1010     op += strlen(op);
1011 greg 2.105 op = addarg(op, "-lr 8 -lw 1e-4");
1012 greg 2.1 }
1013    
1014    
1015 schorsch 2.76 static void
1016     hiqopts( /* high quality rendering options */
1017 greg 2.109 char *op,
1018 schorsch 2.76 char *po
1019     )
1020 greg 2.1 {
1021 greg 2.47 double d, org[3], siz[3], asz;
1022 greg 2.1
1023     *op = '\0';
1024 greg 2.38 *po = '\0';
1025 greg 2.2 if (sscanf(vval(ZONE), "%*s %lf %lf %lf %lf %lf %lf", &org[0],
1026 greg 2.3 &siz[0], &org[1], &siz[1], &org[2], &siz[2]) != 6)
1027     badvalue(ZONE);
1028 greg 2.2 siz[0] -= org[0]; siz[1] -= org[1]; siz[2] -= org[2];
1029 schorsch 2.71 if ((siz[0] <= FTINY) | (siz[1] <= FTINY) | (siz[2] <= FTINY))
1030 greg 2.39 badvalue(ZONE);
1031 greg 2.2 getoctcube(org, &d);
1032 greg 2.47 asz = (siz[0]+siz[1]+siz[2])/3.;
1033     d /= asz;
1034 greg 2.2 switch (vscale(DETAIL)) {
1035     case LOW:
1036 greg 2.38 po = addarg(po, vbool(PENUMBRAS) ? "-ps 1" : "-ps 8");
1037 greg 2.23 op = addarg(op, "-dp 1024");
1038 greg 2.72 sprintf(op, " -ar %d", (int)(32*d));
1039 greg 2.2 op += strlen(op);
1040 greg 2.47 sprintf(op, " -ms %.2g", asz/40.);
1041     op += strlen(op);
1042 greg 2.2 break;
1043     case MEDIUM:
1044 greg 2.38 po = addarg(po, vbool(PENUMBRAS) ? "-ps 1" : "-ps 5");
1045 greg 2.23 op = addarg(op, "-dp 2048");
1046 greg 2.72 sprintf(op, " -ar %d", (int)(64*d));
1047 greg 2.2 op += strlen(op);
1048 greg 2.47 sprintf(op, " -ms %.2g", asz/80.);
1049     op += strlen(op);
1050 greg 2.2 break;
1051     case HIGH:
1052 greg 2.38 po = addarg(po, vbool(PENUMBRAS) ? "-ps 1" : "-ps 3");
1053 greg 2.23 op = addarg(op, "-dp 4096");
1054 greg 2.72 sprintf(op, " -ar %d", (int)(128*d));
1055 greg 2.47 op += strlen(op);
1056     sprintf(op, " -ms %.2g", asz/160.);
1057 greg 2.2 op += strlen(op);
1058     break;
1059     }
1060 greg 2.38 po = addarg(po, "-pt .04");
1061 greg 2.4 if (vbool(PENUMBRAS))
1062 greg 2.90 op = addarg(op, "-ds .1 -dj .9");
1063 greg 2.4 else
1064 greg 2.2 op = addarg(op, "-ds .2");
1065 greg 2.101 op = addarg(op, "-dt .05 -dc .75 -dr 3 -ss 16 -st .01");
1066 greg 2.2 sprintf(op, " -ab %d", overture=vint(INDIRECT)+1);
1067     op += strlen(op);
1068     if (vdef(AMBFILE)) {
1069     sprintf(op, " -af %s", vval(AMBFILE));
1070     op += strlen(op);
1071     } else
1072     overture = 0;
1073     switch (vscale(VARIABILITY)) {
1074     case LOW:
1075 greg 2.72 op = addarg(op, "-aa .125 -ad 512 -as 64");
1076 greg 2.2 break;
1077     case MEDIUM:
1078 greg 2.72 op = addarg(op, "-aa .1 -ad 1536 -as 768");
1079 greg 2.2 break;
1080     case HIGH:
1081 greg 2.72 op = addarg(op, "-aa .075 -ad 4096 -as 2048");
1082 greg 2.2 break;
1083     }
1084     d = ambval();
1085     sprintf(op, " -av %.2g %.2g %.2g", d, d, d);
1086     op += strlen(op);
1087 greg 2.105 op = addarg(op, "-lr 12 -lw 1e-5");
1088 greg 2.1 }
1089    
1090    
1091 schorsch 2.124 #if defined(_WIN32) || defined(_WIN64)
1092 greg 2.109 static void
1093     setenv( /* set an environment variable */
1094     char *vname,
1095     char *value
1096     )
1097     {
1098     char *evp;
1099    
1100     evp = bmalloc(strlen(vname)+strlen(value)+2);
1101     if (evp == NULL)
1102     syserr(progname);
1103     sprintf(evp, "%s=%s", vname, value);
1104     if (putenv(evp) != 0) {
1105     fprintf(stderr, "%s: out of environment space\n", progname);
1106     quit(1);
1107     }
1108     if (!silent)
1109     printf("set %s\n", evp);
1110     }
1111     #endif
1112    
1113    
1114 schorsch 2.76 static void
1115     xferopts( /* transfer options if indicated */
1116     char *ro
1117     )
1118 greg 2.1 {
1119     int fd, n;
1120 greg 2.109 char *cp;
1121 greg 2.1
1122     n = strlen(ro);
1123     if (n < 2)
1124     return;
1125     if (vdef(OPTFILE)) {
1126 greg 2.10 for (cp = ro; cp[1]; cp++)
1127 greg 2.49 if (isspace(cp[1]) && (cp[2] == '@' ||
1128     (cp[2] == '-' && isalpha(cp[3]))))
1129 greg 2.10 *cp = '\n';
1130     else
1131     *cp = cp[1];
1132     *cp = '\n';
1133     fd = open(vval(OPTFILE), O_WRONLY|O_CREAT|O_TRUNC, 0666);
1134     if (fd < 0 || write(fd, ro, n) != n || close(fd) < 0)
1135 greg 2.3 syserr(vval(OPTFILE));
1136 greg 2.15 sprintf(ro, " @%s", vval(OPTFILE));
1137 greg 2.1 }
1138 schorsch 2.124 #if defined(_WIN32) || defined(_WIN64)
1139 greg 2.1 else if (n > 50) {
1140 greg 2.8 setenv("ROPT", ro+1);
1141 greg 2.1 strcpy(ro, " $ROPT");
1142     }
1143     #endif
1144     }
1145    
1146    
1147 schorsch 2.76 static void
1148     pfiltopts( /* get pfilt options */
1149 greg 2.109 char *po
1150 schorsch 2.76 )
1151 greg 2.1 {
1152     *po = '\0';
1153     if (vdef(EXPOSURE)) {
1154     po = addarg(po, "-1 -e");
1155     po = addarg(po, vval(EXPOSURE));
1156     }
1157 greg 2.12 switch (vscale(QUALITY)) {
1158     case MEDIUM:
1159 greg 2.61 po = addarg(po, "-r .6");
1160 greg 2.12 break;
1161     case HIGH:
1162 greg 2.14 po = addarg(po, "-m .25");
1163 greg 2.12 break;
1164     }
1165 greg 2.120 if (vdef(PFILT)) {
1166 greg 2.91 if (vval(PFILT)[0] != '-') {
1167     atos(c_pfilt, sizeof(c_pfilt), vval(PFILT));
1168     po = addarg(po, sskip2(vval(PFILT), 1));
1169     } else
1170     po = addarg(po, vval(PFILT));
1171 greg 2.120 }
1172 greg 2.1 }
1173    
1174    
1175 schorsch 2.76 static int
1176     matchword( /* match white-delimited words */
1177 greg 2.109 char *s1,
1178     char *s2
1179 schorsch 2.76 )
1180 greg 2.1 {
1181     while (isspace(*s1)) s1++;
1182     while (isspace(*s2)) s2++;
1183     while (*s1 && !isspace(*s1))
1184     if (*s1++ != *s2++)
1185     return(0);
1186     return(!*s2 || isspace(*s2));
1187     }
1188    
1189    
1190 schorsch 2.76 static char *
1191     specview( /* get proper view spec from vs */
1192 greg 2.109 char *vs
1193 schorsch 2.76 )
1194 greg 2.1 {
1195 greg 2.3 static char vup[7][12] = {"-vu 0 0 -1","-vu 0 -1 0","-vu -1 0 0",
1196     "-vu 0 0 1", "-vu 1 0 0","-vu 0 1 0","-vu 0 0 1"};
1197 greg 2.1 static char viewopts[128];
1198 greg 2.109 char *cp;
1199 greg 2.3 int xpos, ypos, zpos, viewtype, upax;
1200 greg 2.109 int i;
1201 greg 2.1 double cent[3], dim[3], mult, d;
1202    
1203 greg 2.9 if (vs == NULL || *vs == '-')
1204 greg 2.1 return(vs);
1205 greg 2.3 upax = 0; /* get the up vector */
1206     if (vdef(UP)) {
1207     if (vval(UP)[0] == '-' || vval(UP)[0] == '+')
1208     upax = 1-'X'+UPPER(vval(UP)[1]);
1209     else
1210     upax = 1-'X'+vlet(UP);
1211 schorsch 2.71 if ((upax < 1) | (upax > 3))
1212 greg 2.3 badvalue(UP);
1213     if (vval(UP)[0] == '-')
1214     upax = -upax;
1215     }
1216 greg 2.1 /* check standard view names */
1217     xpos = ypos = zpos = 0;
1218     if (*vs == 'X') {
1219     xpos = 1; vs++;
1220     } else if (*vs == 'x') {
1221     xpos = -1; vs++;
1222     }
1223     if (*vs == 'Y') {
1224     ypos = 1; vs++;
1225     } else if (*vs == 'y') {
1226     ypos = -1; vs++;
1227     }
1228     if (*vs == 'Z') {
1229     zpos = 1; vs++;
1230     } else if (*vs == 'z') {
1231     zpos = -1; vs++;
1232     }
1233 greg 2.84 switch (*vs) {
1234     case VT_PER:
1235     case VT_PAR:
1236     case VT_ANG:
1237     case VT_HEM:
1238     case VT_PLS:
1239     case VT_CYL:
1240 greg 2.1 viewtype = *vs++;
1241 greg 2.84 break;
1242     default:
1243     viewtype = VT_PER;
1244     break;
1245     }
1246 greg 2.1 cp = viewopts;
1247 greg 2.3 if ((!*vs || isspace(*vs)) && (xpos|ypos|zpos)) { /* got one! */
1248 greg 2.1 *cp++ = '-'; *cp++ = 'v'; *cp++ = 't'; *cp++ = viewtype;
1249     if (sscanf(vval(ZONE), "%*s %lf %lf %lf %lf %lf %lf",
1250     &cent[0], &dim[0], &cent[1], &dim[1],
1251 greg 2.3 &cent[2], &dim[2]) != 6)
1252     badvalue(ZONE);
1253     for (i = 0; i < 3; i++) {
1254     dim[i] -= cent[i];
1255 greg 2.39 if (dim[i] <= FTINY)
1256     badvalue(ZONE);
1257 greg 2.3 cent[i] += .5*dim[i];
1258 greg 2.1 }
1259 greg 2.3 mult = vlet(ZONE)=='E' ? 2. : .45 ;
1260 greg 2.110 sprintf(cp, " -vp %.3g %.3g %.3g -vd %.3g %.3g %.3g",
1261 greg 2.1 cent[0]+xpos*mult*dim[0],
1262     cent[1]+ypos*mult*dim[1],
1263     cent[2]+zpos*mult*dim[2],
1264     -xpos*dim[0], -ypos*dim[1], -zpos*dim[2]);
1265     cp += strlen(cp);
1266 greg 2.3 /* redirect up axis if necessary */
1267     switch (upax) {
1268     case 3: /* plus or minus Z axis */
1269     case -3:
1270     case 0:
1271     if (!(xpos|ypos))
1272     upax = 2;
1273 greg 2.1 break;
1274 greg 2.3 case 2: /* plus or minus Y axis */
1275     case -2:
1276     if (!(xpos|zpos))
1277     upax = 1;
1278     break;
1279     case 1: /* plus or minus X axis */
1280     case -1:
1281     if (!(ypos|zpos))
1282     upax = 3;
1283     break;
1284 greg 2.1 }
1285 greg 2.3 cp = addarg(cp, vup[upax+3]);
1286 greg 2.1 switch (viewtype) {
1287 greg 2.84 case VT_PER:
1288 greg 2.1 cp = addarg(cp, "-vh 45 -vv 45");
1289     break;
1290 greg 2.84 case VT_PAR:
1291 greg 2.1 d = sqrt(dim[0]*dim[0]+dim[1]*dim[1]+dim[2]*dim[2]);
1292 greg 2.110 sprintf(cp, " -vh %.3g -vv %.3g", d, d);
1293 greg 2.1 cp += strlen(cp);
1294     break;
1295 greg 2.84 case VT_ANG:
1296     case VT_HEM:
1297     case VT_PLS:
1298 greg 2.1 cp = addarg(cp, "-vh 180 -vv 180");
1299 greg 2.44 break;
1300 greg 2.84 case VT_CYL:
1301 greg 2.44 cp = addarg(cp, "-vh 180 -vv 90");
1302 greg 2.1 break;
1303     }
1304 greg 2.3 } else {
1305 greg 2.4 while (!isspace(*vs)) /* else skip id */
1306     if (!*vs++)
1307     return(NULL);
1308 greg 2.92 if (upax) { /* prepend up vector */
1309 greg 2.3 strcpy(cp, vup[upax+3]);
1310     cp += strlen(cp);
1311     }
1312     }
1313 greg 2.26 if (cp == viewopts) /* append any additional options */
1314     vs++; /* skip prefixed space if unneeded */
1315     strcpy(cp, vs);
1316 schorsch 2.124 #if defined(_WIN32) || defined(_WIN64)
1317 greg 2.8 if (strlen(viewopts) > 40) {
1318     setenv("VIEW", viewopts);
1319     return("$VIEW");
1320     }
1321     #endif
1322 greg 2.1 return(viewopts);
1323     }
1324    
1325    
1326 schorsch 2.76 static char *
1327     getview( /* get view n, or NULL if none */
1328     int n,
1329     char *vn /* returned view name */
1330     )
1331 greg 2.1 {
1332 greg 2.109 char *mv;
1333 greg 2.1
1334 greg 2.8 if (viewselect != NULL) { /* command-line selected */
1335 greg 2.1 if (n) /* only do one */
1336     return(NULL);
1337 greg 2.92
1338     if (isint(viewselect)) { /* view number? */
1339     n = atoi(viewselect)-1;
1340     goto numview;
1341     }
1342 greg 2.1 if (viewselect[0] == '-') { /* already specified */
1343 greg 2.93 if (vn != NULL)
1344     strcpy(vn, "0");
1345 greg 2.1 return(viewselect);
1346     }
1347     if (vn != NULL) {
1348     for (mv = viewselect; *mv && !isspace(*mv);
1349     *vn++ = *mv++)
1350     ;
1351     *vn = '\0';
1352     }
1353     /* check list */
1354 gregl 2.55 while ((mv = nvalue(VIEWS, n++)) != NULL)
1355 greg 2.1 if (matchword(viewselect, mv))
1356     return(specview(mv));
1357 greg 2.92
1358 greg 2.1 return(specview(viewselect)); /* standard view? */
1359     }
1360 greg 2.92 numview:
1361 gregl 2.55 mv = nvalue(VIEWS, n); /* use view n */
1362 greg 2.123 if ((vn != NULL) & (mv != NULL)) {
1363 greg 2.92 if (*mv != '-') {
1364 greg 2.109 char *mv2 = mv;
1365 greg 2.19 while (*mv2 && !isspace(*mv2))
1366     *vn++ = *mv2++;
1367 greg 2.92 *vn = '\0';
1368     } else
1369     sprintf(vn, "%d", n+1);
1370 greg 2.123 }
1371 greg 2.8 return(specview(mv));
1372 greg 2.20 }
1373    
1374    
1375 schorsch 2.76 static int
1376     myprintview( /* print out selected view */
1377 greg 2.109 char *vopts,
1378 schorsch 2.76 FILE *fp
1379     )
1380 greg 2.20 {
1381 gregl 2.55 VIEW vwr;
1382     char buf[128];
1383 greg 2.109 char *cp;
1384 schorsch 2.124 #if defined(_WIN32) || defined(_WIN64)
1385 schorsch 2.80 /* XXX Should we allow something like this for all platforms? */
1386     /* XXX Or is it still required at all? */
1387     again:
1388     #endif
1389 greg 2.20 if (vopts == NULL)
1390     return(-1);
1391 schorsch 2.124 #if defined(_WIN32) || defined(_WIN64)
1392 gregl 2.55 if (vopts[0] == '$') {
1393     vopts = getenv(vopts+1);
1394     goto again;
1395     }
1396 greg 2.24 #endif
1397 schorsch 2.70 vwr = stdview;
1398 gwlarson 2.59 sscanview(&vwr, cp=vopts); /* set initial options */
1399 gregl 2.55 while ((cp = strstr(cp, "-vf ")) != NULL &&
1400 gwlarson 2.59 *atos(buf, sizeof(buf), cp += 4)) {
1401     viewfile(buf, &vwr, NULL); /* load -vf file */
1402     sscanview(&vwr, cp); /* reset tail */
1403     }
1404 greg 2.67 fputs(VIEWSTR, fp);
1405     fprintview(&vwr, fp); /* print full spec. */
1406     fputc('\n', fp);
1407 greg 2.20 return(0);
1408 greg 2.1 }
1409    
1410    
1411 schorsch 2.76 static void
1412     rvu( /* run rvu with first view */
1413     char *opts,
1414     char *po
1415     )
1416 greg 2.1 {
1417 greg 2.21 char *vw;
1418 schorsch 2.65 char combuf[PATH_MAX];
1419 greg 2.1 /* build command */
1420 greg 2.41 if (touchonly || (vw = getview(0, NULL)) == NULL)
1421 greg 2.21 return;
1422     if (sayview)
1423 greg 2.67 myprintview(vw, stdout);
1424 greg 2.91 sprintf(combuf, "%s %s%s%s -R %s ", c_rvu, vw, opts, po, rifname);
1425 greg 2.86 if (nprocs > 1)
1426     sprintf(combuf+strlen(combuf), "-n %d ", nprocs);
1427 greg 2.1 if (rvdevice != NULL)
1428     sprintf(combuf+strlen(combuf), "-o %s ", rvdevice);
1429 greg 2.29 if (vdef(EXPOSURE))
1430     sprintf(combuf+strlen(combuf), "-pe %s ", vval(EXPOSURE));
1431 greg 2.26 strcat(combuf, oct1name);
1432 greg 2.4 if (runcom(combuf)) { /* run it */
1433 greg 2.91 fprintf(stderr, "%s: error running %s\n", progname, c_rvu);
1434 greg 2.53 quit(1);
1435 greg 2.1 }
1436     }
1437    
1438    
1439 greg 2.106 static int
1440     syncf_done( /* check if an rpiece sync file is complete */
1441     char *sfname
1442     )
1443     {
1444     FILE *fp = fopen(sfname, "r");
1445     int todo = 1;
1446     int x, y;
1447    
1448     if (fp == NULL)
1449     return(0);
1450     if (fscanf(fp, "%d %d", &x, &y) != 2)
1451     goto checked;
1452     todo = x*y; /* total number of tiles */
1453     if (fscanf(fp, "%d %d", &x, &y) != 2 || (x != 0) | (y != 0))
1454     goto checked;
1455     /* XXX assume no redundant tiles */
1456     while (fscanf(fp, "%d %d", &x, &y) == 2)
1457     if (!--todo)
1458     break;
1459     checked:
1460     fclose(fp);
1461     return(!todo);
1462     }
1463    
1464    
1465 schorsch 2.76 static void
1466     rpict( /* run rpict and pfilt for each view */
1467     char *opts,
1468     char *po
1469     )
1470 greg 2.1 {
1471 greg 2.93 #define do_rpiece (sfile[0]!='\0')
1472 greg 2.102 char combuf[5*PATH_MAX+512];
1473 schorsch 2.65 char rawfile[PATH_MAX], picfile[PATH_MAX];
1474     char zopt[PATH_MAX+4], rep[PATH_MAX+16], res[32];
1475 greg 2.102 char rppopt[32], sfile[PATH_MAX], *pfile = NULL;
1476 greg 2.3 char pfopts[128];
1477 greg 2.1 char vs[32], *vw;
1478     int vn, mult;
1479 greg 2.67 FILE *fp;
1480 greg 2.41 time_t rfdt, pfdt;
1481 greg 2.104 int xres, yres;
1482     double aspect;
1483     int n;
1484 greg 2.1 /* get pfilt options */
1485     pfiltopts(pfopts);
1486     /* get resolution, reporting */
1487 greg 2.31 switch (vscale(QUALITY)) {
1488     case LOW:
1489     mult = 1;
1490     break;
1491     case MEDIUM:
1492     mult = 2;
1493     break;
1494     case HIGH:
1495     mult = 3;
1496     break;
1497     }
1498 greg 2.104 n = sscanf(vval(RESOLUTION), "%d %d %lf", &xres, &yres, &aspect);
1499     if (n == 3)
1500     sprintf(res, "-x %d -y %d -pa %.3f",
1501     mult*xres, mult*yres, aspect);
1502     else if (n) {
1503     aspect = 1.;
1504     if (n == 1) yres = xres;
1505     sprintf(res, "-x %d -y %d", mult*xres, mult*yres);
1506     } else
1507     badvalue(RESOLUTION);
1508 greg 2.1 rep[0] = '\0';
1509     if (vdef(REPORT)) {
1510     double minutes;
1511     n = sscanf(vval(REPORT), "%lf %s", &minutes, rawfile);
1512     if (n == 2)
1513     sprintf(rep, " -t %d -e %s", (int)(minutes*60), rawfile);
1514     else if (n == 1)
1515     sprintf(rep, " -t %d", (int)(minutes*60));
1516 greg 2.3 else
1517     badvalue(REPORT);
1518 greg 2.1 }
1519 greg 2.67 /* set up parallel rendering */
1520 greg 2.93 sfile[0] = '\0';
1521     if ((nprocs > 1) & !touchonly & !vdef(ZFILE) &&
1522     getview(0, vs) != NULL) {
1523     if (!strcmp(c_rpict, DEF_RPICT_PATH) &&
1524     getview(1, NULL) == NULL) {
1525 greg 2.102 sprintf(sfile, "%s_%s_rpsync.txt",
1526     vdef(RAWFILE) ? vval(RAWFILE) : vval(PICTURE),
1527     vs);
1528 greg 2.93 strcpy(rppopt, "-PP pfXXXXXX");
1529     } else {
1530     strcpy(rppopt, "-S 1 -PP pfXXXXXX");
1531     }
1532     pfile = rppopt + strlen(rppopt) - 8;
1533 greg 2.94 if (mktemp(pfile) == NULL) {
1534 greg 2.93 if (do_rpiece) {
1535     fprintf(stderr, "%s: cannot create\n", pfile);
1536     quit(1);
1537 greg 2.94 }
1538 greg 2.123 pfile[-5] = '\0';
1539 greg 2.94 pfile = NULL;
1540     }
1541 greg 2.67 }
1542 greg 2.66 vn = 0; /* do each view */
1543 greg 2.1 while ((vw = getview(vn++, vs)) != NULL) {
1544 greg 2.21 if (sayview)
1545 greg 2.67 myprintview(vw, stdout);
1546 greg 2.87 sprintf(picfile, "%s_%s.hdr", vval(PICTURE), vs);
1547 greg 2.45 if (vdef(ZFILE))
1548     sprintf(zopt, " -z %s_%s.zbf", vval(ZFILE), vs);
1549     else
1550     zopt[0] = '\0';
1551 greg 2.1 /* check date on picture */
1552 greg 2.41 pfdt = fdate(picfile);
1553     if (pfdt >= oct1date)
1554 greg 2.1 continue;
1555 greg 2.41 /* get raw file name */
1556 greg 2.45 sprintf(rawfile, "%s_%s.unf",
1557     vdef(RAWFILE) ? vval(RAWFILE) : vval(PICTURE), vs);
1558 greg 2.41 rfdt = fdate(rawfile);
1559     if (touchonly) { /* update times only */
1560     if (rfdt) {
1561     if (rfdt < oct1date)
1562     touch(rawfile);
1563     } else if (pfdt && pfdt < oct1date)
1564     touch(picfile);
1565     continue;
1566     }
1567 greg 2.93 /* parallel running? */
1568     if (do_rpiece) {
1569 greg 2.94 if (rfdt < oct1date || !fdate(sfile)) {
1570 greg 2.95 int xdiv = 8+nprocs/3, ydiv = 8+nprocs/3;
1571 greg 2.102 if (rfdt >= oct1date) {
1572     fprintf(stderr,
1573 greg 2.107 "%s: partial output not created with %s\n", rawfile, c_rpiece);
1574 greg 2.102 quit(1);
1575     }
1576 greg 2.103 if (rfdt) { /* start fresh */
1577     rmfile(rawfile);
1578     rfdt = 0;
1579     }
1580 greg 2.95 if (!silent)
1581 greg 2.96 printf("\techo %d %d > %s\n",
1582 greg 2.95 xdiv, ydiv, sfile);
1583 greg 2.93 if ((fp = fopen(sfile, "w")) == NULL) {
1584     fprintf(stderr, "%s: cannot create\n",
1585     sfile);
1586     quit(1);
1587     }
1588 greg 2.95 fprintf(fp, "%d %d\n", xdiv, ydiv);
1589 greg 2.93 fclose(fp);
1590     }
1591 greg 2.98 } else if (next_process(0)) {
1592 greg 2.67 if (pfile != NULL)
1593 greg 2.93 sleep(10);
1594 greg 2.66 continue;
1595 greg 2.93 } else if (!inchild())
1596     pfile = NULL;
1597 greg 2.66 /* XXX Remember to call finish_process() */
1598 greg 2.1 /* build rpict command */
1599 greg 2.93 if (rfdt >= oct1date) { /* already in progress */
1600     if (do_rpiece) {
1601     sprintf(combuf, "%s -R %s %s%s %s %s%s%s -o %s %s",
1602     c_rpiece, sfile, rppopt, rep, vw,
1603     res, opts, po, rawfile, oct1name);
1604 greg 2.98 while (next_process(1)) {
1605 greg 2.93 sleep(10);
1606     combuf[strlen(c_rpiece)+2] = 'F';
1607     }
1608     } else
1609     sprintf(combuf, "%s%s%s%s%s -ro %s %s", c_rpict,
1610     rep, opts, po, zopt, rawfile, oct1name);
1611     if (runcom(combuf)) /* run rpict/rpiece */
1612 greg 2.67 goto rperror;
1613     } else {
1614 greg 2.1 if (overture) { /* run overture calculation */
1615     sprintf(combuf,
1616 greg 2.101 "%s%s %s%s -x 64 -y 64 -ps 1 %s > %s",
1617 greg 2.91 c_rpict, rep, vw, opts,
1618 greg 2.26 oct1name, overfile);
1619 greg 2.101 if (!do_rpiece || !next_process(0)) {
1620     if (runcom(combuf)) {
1621     fprintf(stderr,
1622 greg 2.6 "%s: error in overture for view %s\n",
1623 greg 2.101 progname, vs);
1624     quit(1);
1625     }
1626 schorsch 2.65 #ifndef NULL_DEVICE
1627 greg 2.101 rmfile(overfile);
1628 greg 2.6 #endif
1629 greg 2.101 } else if (do_rpiece)
1630     sleep(20);
1631 greg 2.1 }
1632 greg 2.93 if (do_rpiece) {
1633     sprintf(combuf, "%s -F %s %s%s %s %s%s%s -o %s %s",
1634     c_rpiece, sfile, rppopt, rep, vw,
1635     res, opts, po, rawfile, oct1name);
1636 greg 2.98 while (next_process(1))
1637 greg 2.93 sleep(10);
1638     } else {
1639     sprintf(combuf, "%s%s %s %s%s%s%s %s > %s",
1640     c_rpict, rep, vw, res, opts, po,
1641     zopt, oct1name, rawfile);
1642     }
1643     if ((pfile != NULL) & !do_rpiece) {
1644 greg 2.82 if (!silent)
1645 greg 2.67 printf("\t%s\n", combuf);
1646 greg 2.82 fflush(stdout);
1647 greg 2.91 sprintf(combuf, "%s%s %s %s%s%s %s > %s",
1648 greg 2.93 c_rpict, rep, rppopt, res,
1649     opts, po, oct1name, rawfile);
1650 greg 2.67 fp = popen(combuf, "w");
1651     if (fp == NULL)
1652     goto rperror;
1653     myprintview(vw, fp);
1654     if (pclose(fp))
1655     goto rperror;
1656 greg 2.93 } else if (runcom(combuf))
1657     goto rperror;
1658     }
1659     if (do_rpiece) { /* need to finish raw, first */
1660     finish_process();
1661     wait_process(1);
1662 greg 2.106 if (!syncf_done(sfile)) {
1663     fprintf(stderr,
1664 greg 2.107 "%s: %s did not complete rendering of view %s\n",
1665     progname, c_rpiece, vs);
1666 greg 2.106 quit(1);
1667     }
1668 greg 2.1 }
1669 greg 2.45 if (!vdef(RAWFILE) || strcmp(vval(RAWFILE),vval(PICTURE))) {
1670 greg 2.1 /* build pfilt command */
1671 greg 2.104 if (do_rpiece)
1672     sprintf(combuf,
1673     "%s%s -x %d -y %d -p %.3f %s > %s",
1674     c_pfilt, pfopts, xres, yres, aspect,
1675     rawfile, picfile);
1676     else if (mult > 1)
1677 greg 2.91 sprintf(combuf, "%s%s -x /%d -y /%d %s > %s",
1678     c_pfilt, pfopts, mult, mult,
1679     rawfile, picfile);
1680 greg 2.45 else
1681 greg 2.91 sprintf(combuf, "%s%s %s > %s", c_pfilt,
1682     pfopts, rawfile, picfile);
1683 greg 2.67 if (runcom(combuf)) { /* run pfilt */
1684 greg 2.45 fprintf(stderr,
1685     "%s: error filtering view %s\n\t%s removed\n",
1686     progname, vs, picfile);
1687     unlink(picfile);
1688 greg 2.53 quit(1);
1689 greg 2.45 }
1690 greg 2.1 }
1691 greg 2.43 /* remove/rename raw file */
1692 greg 2.45 if (vdef(RAWFILE)) {
1693 greg 2.87 sprintf(combuf, "%s_%s.hdr", vval(RAWFILE), vs);
1694 greg 2.43 mvfile(rawfile, combuf);
1695     } else
1696     rmfile(rawfile);
1697 greg 2.93 if (do_rpiece) /* done with sync file */
1698     rmfile(sfile);
1699     else
1700     finish_process(); /* exit if child */
1701 greg 2.4 }
1702 greg 2.66 wait_process(1); /* wait for children to finish */
1703 greg 2.94 if (pfile != NULL) { /* clean up persistent rpict */
1704 schorsch 2.81 RT_PID pid;
1705 greg 2.67 fp = fopen(pfile, "r");
1706     if (fp != NULL) {
1707     if (fscanf(fp, "%*s %d", &pid) != 1 ||
1708 greg 2.73 kill(pid, 1) < 0)
1709 greg 2.67 unlink(pfile);
1710     fclose(fp);
1711     }
1712     }
1713     return;
1714     rperror:
1715     fprintf(stderr, "%s: error rendering view %s\n", progname, vs);
1716     quit(1);
1717 greg 2.93 #undef do_rpiece
1718 greg 2.41 }
1719    
1720    
1721 schorsch 2.76 static int
1722     touch( /* update a file */
1723     char *fn
1724     )
1725 greg 2.41 {
1726     if (!silent)
1727     printf("\ttouch %s\n", fn);
1728 greg 2.66 if (!nprocs)
1729 greg 2.41 return(0);
1730     return(setfdate(fn, time((time_t *)NULL)));
1731 greg 2.4 }
1732    
1733    
1734 schorsch 2.76 static int
1735     runcom( /* run command */
1736     char *cs
1737     )
1738 greg 2.4 {
1739     if (!silent) /* echo it */
1740     printf("\t%s\n", cs);
1741 greg 2.66 if (!nprocs)
1742 greg 2.4 return(0);
1743 greg 2.82 fflush(NULL); /* flush output and pass to shell */
1744 greg 2.4 return(system(cs));
1745     }
1746    
1747    
1748 schorsch 2.76 static int
1749     rmfile( /* remove a file */
1750     char *fn
1751     )
1752 greg 2.4 {
1753     if (!silent)
1754 schorsch 2.69 printf("\t%s %s\n", DELCMD, fn);
1755 greg 2.66 if (!nprocs)
1756 greg 2.4 return(0);
1757     return(unlink(fn));
1758 greg 2.43 }
1759    
1760    
1761 schorsch 2.76 static int
1762     mvfile( /* move a file */
1763     char *fold,
1764     char *fnew
1765     )
1766 greg 2.43 {
1767     if (!silent)
1768 schorsch 2.69 printf("\t%s %s %s\n", RENAMECMD, fold, fnew);
1769 greg 2.66 if (!nprocs)
1770 greg 2.43 return(0);
1771     return(rename(fold, fnew));
1772 greg 2.3 }
1773 greg 2.8
1774 greg 2.66
1775     #ifdef RHAS_FORK_EXEC
1776 schorsch 2.76 static int
1777 greg 2.98 next_process(int reserve) /* fork the next process */
1778 greg 2.66 {
1779 schorsch 2.81 RT_PID child_pid;
1780 greg 2.66
1781     if (nprocs <= 1)
1782     return(0); /* it's us or no one */
1783 greg 2.67 if (inchild()) {
1784 greg 2.73 fprintf(stderr, "%s: internal error 1 in next_process()\n",
1785 greg 2.66 progname);
1786     quit(1);
1787     }
1788 greg 2.98 if (reserve > 0 && children_running >= nprocs-reserve)
1789     return(0); /* caller holding back process(es) */
1790 greg 2.66 if (children_running >= nprocs)
1791     wait_process(0); /* wait for someone to finish */
1792 greg 2.82 fflush(NULL); /* flush output */
1793 greg 2.66 child_pid = fork(); /* split process */
1794     if (child_pid == 0) { /* we're the child */
1795     children_running = -1;
1796 greg 2.101 nprocs = 1;
1797 greg 2.66 return(0);
1798     }
1799     if (child_pid > 0) { /* we're the parent */
1800     ++children_running;
1801     return(1);
1802     }
1803     fprintf(stderr, "%s: warning -- fork() failed\n", progname);
1804     return(0);
1805     }
1806    
1807 schorsch 2.76 static void
1808     wait_process( /* wait for process(es) to finish */
1809     int all
1810     )
1811 greg 2.66 {
1812 schorsch 2.85 int ourstatus = 0, status;
1813     RT_PID pid;
1814 greg 2.66
1815     if (all)
1816     all = children_running;
1817     else if (children_running > 0)
1818     all = 1;
1819     while (all-- > 0) {
1820     pid = wait(&status);
1821     if (pid < 0)
1822     syserr(progname);
1823     status = status>>8 & 0xff;
1824     --children_running;
1825     if (status != 0) { /* child's problem is our problem */
1826 schorsch 2.71 if ((ourstatus == 0) & (children_running > 0))
1827 greg 2.66 fprintf(stderr, "%s: waiting for remaining processes\n",
1828     progname);
1829     ourstatus = status;
1830     all = children_running;
1831     }
1832     }
1833     if (ourstatus != 0)
1834     quit(ourstatus); /* bad status from child */
1835     }
1836     #else /* ! RHAS_FORK_EXEC */
1837 schorsch 2.76 static int
1838 greg 2.98 next_process(int reserve)
1839 greg 2.66 {
1840     return(0); /* cannot start new process */
1841     }
1842 schorsch 2.76 static void
1843 greg 2.66 wait_process(all)
1844     int all;
1845     {
1846     (void)all; /* no one to wait for */
1847 schorsch 2.68 }
1848     int
1849     kill(pid, sig) /* win|unix_process.c should also wait and kill */
1850 schorsch 2.85 RT_PID pid;
1851     int sig;
1852 schorsch 2.68 {
1853     return 0;
1854 greg 2.66 }
1855     #endif /* ! RHAS_FORK_EXEC */
1856    
1857 schorsch 2.76 static void
1858     finish_process(void) /* exit a child process */
1859 greg 2.66 {
1860 greg 2.67 if (!inchild())
1861 greg 2.66 return; /* in parent -- noop */
1862     exit(0);
1863     }
1864 greg 2.8
1865 greg 2.3
1866 schorsch 2.76 static void
1867     badvalue( /* report bad variable value and exit */
1868     int vc
1869     )
1870 greg 2.3 {
1871     fprintf(stderr, "%s: bad value for variable '%s'\n",
1872     progname, vnam(vc));
1873 greg 2.53 quit(1);
1874 greg 2.3 }
1875    
1876    
1877 schorsch 2.76 static void
1878     syserr( /* report a system error and exit */
1879     char *s
1880     )
1881 greg 2.3 {
1882     perror(s);
1883 greg 2.53 quit(1);
1884     }
1885    
1886    
1887 greg 2.61 void
1888 greg 2.53 quit(ec) /* exit program */
1889     int ec;
1890     {
1891     exit(ec);
1892 greg 2.1 }