ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.133
Committed: Mon Dec 23 01:39:27 2024 UTC (4 months, 1 week ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.132: +4 -3 lines
Log Message:
feat(rad): Increased command-line length limits

File Contents

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