ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
Revision: 2.122
Committed: Thu Oct 22 18:09:37 2015 UTC (9 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.121: +2 -1 lines
Log Message:
Fixed problem with global photon map argument (garbage characters)

File Contents

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