ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.115
Committed: Wed May 27 14:12:09 2015 UTC (8 years, 10 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.114: +3 -3 lines
Log Message:
Only remove photon map(s) related to job

File Contents

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