ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.135
Committed: Sat Jun 7 05:09:46 2025 UTC (3 hours, 31 minutes ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.134: +1 -2 lines
Log Message:
refactor: Put some declarations into "paths.h" and included in "platform.h"

File Contents

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