ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/rad.c
(Generate patch)

Comparing ray/src/util/rad.c (file contents):
Revision 2.53 by greg, Thu Mar 20 12:50:07 1997 UTC vs.
Revision 2.117 by greg, Wed May 27 14:26:04 2015 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1995 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Executive program for oconv, rpict and pfilt
6   */
7  
8   #include "standard.h"
9 +
10 + #include <ctype.h>
11 + #include <time.h>
12 +
13 + #include "platform.h"
14 + #include "rtprocess.h"
15 + #include "view.h"
16   #include "paths.h"
17   #include "vars.h"
14 #include <ctype.h>
15 #include <sys/types.h>
18  
19 <                                /* variables */
20 < #define OBJECT          0               /* object files */
21 < #define SCENE           1               /* scene files */
22 < #define MATERIAL        2               /* material files */
23 < #define ILLUM           3               /* mkillum input files */
24 < #define MKILLUM         4               /* mkillum options */
25 < #define RENDER          5               /* rendering options */
26 < #define OCONV           6               /* oconv options */
27 < #define PFILT           7               /* pfilt options */
28 < #define VIEW            8               /* view(s) for picture(s) */
29 < #define ZONE            9               /* simulation zone */
30 < #define QUALITY         10              /* desired rendering quality */
19 > #ifdef _WIN32
20 >  #define DELCMD "del"
21 >  #define RENAMECMD "rename"
22 > #else
23 >  #define DELCMD "rm -f"
24 >  #define RENAMECMD "mv"
25 >  #include <sys/types.h>
26 >  #include <sys/wait.h>
27 >  #include <signal.h>
28 > #endif
29 >
30 >                                /* 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 > #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 > #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 PICTURE         12              /* picture file root name */
44 < #define AMBFILE         13              /* ambient file name */
45 < #define OPTFILE         14              /* rendering options file */
46 < #define EXPOSURE        15              /* picture exposure setting */
47 < #define RESOLUTION      16              /* maximum picture resolution */
48 < #define UP              17              /* view up (X, Y or Z) */
49 < #define INDIRECT        18              /* indirection in lighting */
50 < #define DETAIL          19              /* level of scene detail */
51 < #define PENUMBRAS       20              /* shadow penumbras are desired */
52 < #define VARIABILITY     21              /* level of light variability */
53 < #define REPORT          22              /* report frequency and errfile */
54 < #define RAWFILE         23              /* raw picture file root name */
55 < #define ZFILE           24              /* distance file root 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                                  /* total number of variables */
63 < int NVARS = 25;
63 > int NVARS = 31;
64  
65   VARIABLE        vv[] = {                /* variable-value pairs */
66 <        {"objects",     3,      0,      NULL,   catvalues},
67 <        {"scene",       3,      0,      NULL,   catvalues},
68 <        {"materials",   3,      0,      NULL,   catvalues},
66 >        {"AMBFILE",     3,      0,      NULL,   onevalue},
67 >        {"DETAIL",      3,      0,      NULL,   qualvalue},
68 >        {"EXPOSURE",    3,      0,      NULL,   fltvalue},
69 >        {"EYESEP",      3,      0,      NULL,   fltvalue},
70          {"illum",       3,      0,      NULL,   catvalues},
71 +        {"INDIRECT",    3,      0,      NULL,   intvalue},
72 +        {"materials",   3,      0,      NULL,   catvalues},
73          {"mkillum",     3,      0,      NULL,   catvalues},
74 <        {"render",      3,      0,      NULL,   catvalues},
74 >        {"mkpmap",      3,      0,      NULL,   catvalues},
75 >        {"objects",     3,      0,      NULL,   catvalues},
76          {"oconv",       3,      0,      NULL,   catvalues},
54        {"pfilt",       2,      0,      NULL,   catvalues},
55        {"view",        2,      0,      NULL,   NULL},
56        {"ZONE",        2,      0,      NULL,   onevalue},
57        {"QUALITY",     3,      0,      NULL,   qualvalue},
77          {"OCTREE",      3,      0,      NULL,   onevalue},
59        {"PICTURE",     3,      0,      NULL,   onevalue},
60        {"AMBFILE",     3,      0,      NULL,   onevalue},
78          {"OPTFILE",     3,      0,      NULL,   onevalue},
79 <        {"EXPOSURE",    3,      0,      NULL,   fltvalue},
79 >        {"PCMAP",       2,      0,      NULL,   onevalue},
80 >        {"PENUMBRAS",   3,      0,      NULL,   boolvalue},
81 >        {"pfilt",       2,      0,      NULL,   catvalues},
82 >        {"PGMAP",       2,      0,      NULL,   onevalue},
83 >        {"PICTURE",     3,      0,      NULL,   onevalue},
84 >        {"QUALITY",     3,      0,      NULL,   qualvalue},
85 >        {"RAWFILE",     3,      0,      NULL,   onevalue},
86 >        {"render",      3,      0,      NULL,   catvalues},
87 >        {"REPORT",      3,      0,      NULL,   onevalue},
88          {"RESOLUTION",  3,      0,      NULL,   onevalue},
89 +        {"rpict",       3,      0,      NULL,   catvalues},
90 +        {"rvu",         3,      0,      NULL,   catvalues},
91 +        {"scene",       3,      0,      NULL,   catvalues},
92          {"UP",          2,      0,      NULL,   onevalue},
65        {"INDIRECT",    3,      0,      NULL,   intvalue},
66        {"DETAIL",      3,      0,      NULL,   qualvalue},
67        {"PENUMBRAS",   3,      0,      NULL,   boolvalue},
93          {"VARIABILITY", 3,      0,      NULL,   qualvalue},
94 <        {"REPORT",      3,      0,      NULL,   onevalue},
70 <        {"RAWFILE",     3,      0,      NULL,   onevalue},
94 >        {"view",        2,      0,      NULL,   NULL},
95          {"ZFILE",       2,      0,      NULL,   onevalue},
96 +        {"ZONE",        2,      0,      NULL,   onevalue},
97   };
98  
99                                  /* overture calculation file */
100 < #ifdef NIX
101 < char    overfile[] = "overture.unf";
100 > #ifdef NULL_DEVICE
101 > char    overfile[] = NULL_DEVICE;
102   #else
103 < char    overfile[] = "/dev/null";
103 > char    overfile[] = "overture.unf";
104   #endif
105  
81 extern time_t   fdate(), time();
106  
107   time_t  scenedate;              /* date of latest scene or object file */
108   time_t  octreedate;             /* date of octree */
# Line 90 | Line 114 | time_t oct0date;               /* date of pre-mkillum octree */
114   char    *oct1name;              /* name of post-mkillum octree */
115   time_t  oct1date;               /* date of post-mkillum octree (>= matdate) */
116  
117 + 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   int     nowarn = 0;             /* no warnings */
123   int     explicate = 0;          /* explicate variables */
124   int     silent = 0;             /* do work silently */
125   int     touchonly = 0;          /* touch files only */
126 < int     noaction = 0;           /* don't do anything */
126 > int     nprocs = 1;             /* maximum executing processes */
127   int     sayview = 0;            /* print view out */
128 < char    *rvdevice = NULL;       /* rview output device */
128 > char    *rvdevice = NULL;       /* rvu output device */
129   char    *viewselect = NULL;     /* specific view only */
130  
131 + #define DEF_RPICT_PATH  "rpict"         /* default rpict path */
132 +
133 +                                /* command paths */
134 + char    c_oconv[256] = "oconv";
135 + char    c_mkillum[256] = "mkillum";
136 + char    c_mkpmap[256] = "mkpmap";
137 + char    c_rvu[256] = "rvu";
138 + char    c_rpict[256] = DEF_RPICT_PATH;
139 + char    c_rpiece[] = "rpiece";
140 + char    c_pfilt[256] = "pfilt";
141 +
142   int     overture = 0;           /* overture calculation needed */
143  
144 + int     children_running = 0;   /* set negative in children */
145 +
146   char    *progname;              /* global argv[0] */
147   char    *rifname;               /* global rad input file name */
148  
149 < char    radname[MAXPATH];       /* root Radiance file name */
149 > char    radname[PATH_MAX];      /* root Radiance file name */
150  
151 + #define inchild()       (children_running < 0)
152  
153 < main(argc, argv)
154 < int     argc;
155 < char    *argv[];
153 > 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 > static void mkpmap(void);
161 > static char * addarg(char       *op, char       *arg);
162 > static void oconvopts(char      *oo);
163 > static void mkillumopts(char    *mo);
164 > static void mkpmapopts(char     *mo);
165 > 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 > static int next_process(int     reserve);
184 > 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 >
189 >
190 > int
191 > main(
192 >        int     argc,
193 >        char    *argv[]
194 > )
195   {
196          char    ropts[512];
197          char    popts[64];
# Line 123 | Line 205 | char   *argv[];
205                          silent++;
206                          break;
207                  case 'n':
208 <                        noaction++;
208 >                        nprocs = 0;
209                          break;
210 +                case 'N':
211 +                        nprocs = atoi(argv[++i]);
212 +                        if (nprocs < 0)
213 +                                nprocs = 0;
214 +                        break;
215                  case 't':
216                          touchonly++;
217                          break;
# Line 171 | Line 258 | char   *argv[];
258                  printvars(stdout);
259                                  /* build octree (and run mkillum) */
260          oconv();
261 +                                /* run mkpmap if indicated */
262 +        mkpmap();
263                                  /* check date on ambient file */
264          checkambfile();
265                                  /* run simulation */
266          renderopts(ropts, popts);
267          xferopts(ropts);
268          if (rvdevice != NULL)
269 <                rview(ropts, popts);
269 >                rvu(ropts, popts);
270          else
271                  rpict(ropts, popts);
272          quit(0);
273   userr:
274          fprintf(stderr,
275 < "Usage: %s [-s][-n][-t][-e][-V][-v view][-o dev] rfile [VAR=value ..]\n",
275 > "Usage: %s [-w][-s][-n|-N npr][-t][-e][-V][-v view][-o dev] rfile [VAR=value ..]\n",
276                          progname);
277          quit(1);
278 +        return 1; /* pro forma return */
279   }
280  
281  
282 < rootname(rn, fn)                /* remove tail from end of fn */
283 < register char   *rn, *fn;
282 > static void
283 > rootname(               /* remove tail from end of fn */
284 >        char    *rn,
285 >        char    *fn
286 > )
287   {
288          char    *tp, *dp;
289  
290 <        for (tp = NULL, dp = rn; *rn = *fn++; rn++)
290 >        for (tp = NULL, dp = rn; (*rn = *fn++); rn++)
291                  if (ISDIRSEP(*rn))
292                          dp = rn;
293                  else if (*rn == '.')
# Line 204 | Line 297 | register char  *rn, *fn;
297   }
298  
299  
300 < time_t
301 < checklast(fnames)                       /* check files and find most recent */
302 < register char   *fnames;
300 > static time_t
301 > checklast(                      /* check files and find most recent */
302 >        char    *fnames
303 > )
304   {
305 <        char    thisfile[MAXPATH];
305 >        char    thisfile[PATH_MAX];
306          time_t  thisdate, lastdate = 0;
213        register char   *cp;
307  
308          if (fnames == NULL)
309                  return(0);
310 <        while (*fnames) {
311 <                while (isspace(*fnames)) fnames++;
312 <                cp = thisfile;
313 <                while (*fnames && !isspace(*fnames))
314 <                        *cp++ = *fnames++;
315 <                *cp = '\0';
310 >        while ((fnames = nextword(thisfile, PATH_MAX, fnames)) != NULL) {
311 >                if (thisfile[0] == '!' ||
312 >                                (thisfile[0] == '\\' && thisfile[1] == '!')) {
313 >                        if (!lastdate)
314 >                                lastdate = 1;
315 >                        continue;
316 >                }
317                  if (!(thisdate = fdate(thisfile)))
318                          syserr(thisfile);
319                  if (thisdate > lastdate)
# Line 229 | Line 323 | register char  *fnames;
323   }
324  
325  
326 < char *
327 < newfname(orig, pred)            /* create modified file name */
328 < char    *orig;
329 < int     pred;
326 > static char *
327 > newfname(               /* create modified file name */
328 >        char    *orig,
329 >        int     pred
330 > )
331   {
332 <        extern char     *rindex();
333 <        register char   *cp;
239 <        register int    n;
332 >        char    *cp;
333 >        int     n;
334          int     suffix;
335  
336          n = 0; cp = orig; suffix = -1;          /* suffix position, length */
# Line 255 | Line 349 | int    pred;
349   }
350  
351  
352 < checkfiles()                    /* check for existence and modified times */
352 > static void
353 > checkfiles(void)                        /* check for existence and modified times */
354   {
355 +        char    fntemp[256];
356          time_t  objdate;
357  
358          if (!vdef(OCTREE)) {
# Line 264 | Line 360 | checkfiles()                   /* check for existence and modified tim
360                          syserr(progname);
361                  sprintf(vval(OCTREE), "%s.oct", radname);
362                  vdef(OCTREE)++;
363 +        } else if (vval(OCTREE)[0] == '!') {
364 +                fprintf(stderr, "%s: illegal '%s' specification\n",
365 +                                progname, vnam(OCTREE));
366 +                quit(1);
367          }
368          octreedate = fdate(vval(OCTREE));
369          if (vdef(ILLUM)) {              /* illum requires secondary octrees */
# Line 282 | Line 382 | checkfiles()                   /* check for existence and modified tim
382                                  vnam(OCTREE), vnam(SCENE), vnam(ILLUM));
383                  quit(1);
384          }
385 +        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          matdate = checklast(vval(MATERIAL));
406   }      
407  
408  
409 < getoctcube(org, sizp)           /* get octree bounding cube */
410 < double  org[3], *sizp;
409 > static void
410 > getoctcube(             /* get octree bounding cube */
411 >        double  org[3],
412 >        double  *sizp
413 > )
414   {
292        extern FILE     *popen();
415          static double   oorg[3], osiz = 0.;
416          double  min[3], max[3];
417          char    buf[1024];
418          FILE    *fp;
419 <        register int    i;
419 >        int     i;
420  
421 <        if (osiz <= FTINY)
422 <                if (noaction && fdate(oct1name) <
421 >        if (osiz <= FTINY) {
422 >                if (!nprocs && fdate(oct1name) <
423                                  (scenedate>illumdate?scenedate:illumdate)) {
424                                                          /* run getbbox */
425                          sprintf(buf, "getbbox -w -h %s",
# Line 332 | Line 454 | double org[3], *sizp;
454                          }
455                          pclose(fp);
456                  }
457 +        }
458          org[0] = oorg[0]; org[1] = oorg[1]; org[2] = oorg[2]; *sizp = osiz;
459   }
460  
461  
462 < setdefaults()                   /* set default values for unassigned var's */
462 > static void
463 > setdefaults(void)                       /* set default values for unassigned var's */
464   {
465 <        double  org[3], size;
465 >        double  org[3], lim[3], size;
466          char    buf[128];
467  
468          if (!vdef(ZONE)) {
# Line 348 | Line 472 | setdefaults()                  /* set default values for unassigned v
472                  vval(ZONE) = savqstr(buf);
473                  vdef(ZONE)++;
474          }
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 +        }
485          if (!vdef(INDIRECT)) {
486                  vval(INDIRECT) = "0";
487                  vdef(INDIRECT)++;
# Line 364 | Line 498 | setdefaults()                  /* set default values for unassigned v
498                  vval(PICTURE) = radname;
499                  vdef(PICTURE)++;
500          }
501 <        if (!vdef(VIEW)) {
502 <                vval(VIEW) = "X";
503 <                vdef(VIEW)++;
501 >        if (!vdef(VIEWS)) {
502 >                vval(VIEWS) = "X";
503 >                vdef(VIEWS)++;
504          }
505          if (!vdef(DETAIL)) {
506                  vval(DETAIL) = "M";
# Line 383 | Line 517 | setdefaults()                  /* set default values for unassigned v
517   }
518  
519  
520 < oconv()                         /* run oconv and mkillum if necessary */
520 > static void
521 > oconv(void)                             /* run oconv and mkillum if necessary */
522   {
523          static char     illumtmp[] = "ilXXXXXX";
524 <        char    combuf[1024], ocopts[64], mkopts[64];
524 >        char    combuf[PATH_MAX], ocopts[64], mkopts[1024];
525  
526          oconvopts(ocopts);              /* get options */
527          if (octreedate < scenedate) {   /* check date on original octree */
# Line 394 | Line 529 | oconv()                                /* run oconv and mkillum if necessary */
529                          touch(vval(OCTREE));
530                  else {                          /* build command */
531                          if (vdef(MATERIAL))
532 <                                sprintf(combuf, "oconv%s %s %s > %s", ocopts,
533 <                                                vval(MATERIAL), vval(SCENE),
534 <                                                vval(OCTREE));
532 >                                sprintf(combuf, "%s%s %s %s > %s", c_oconv,
533 >                                                ocopts, vval(MATERIAL),
534 >                                                vval(SCENE), vval(OCTREE));
535                          else
536 <                                sprintf(combuf, "oconv%s %s > %s", ocopts,
536 >                                sprintf(combuf, "%s%s %s > %s", c_oconv, ocopts,
537                                                  vval(SCENE), vval(OCTREE));
538                          
539                          if (runcom(combuf)) {           /* run it */
# Line 415 | Line 550 | oconv()                                /* run oconv and mkillum if necessary */
550          }
551          if (oct1name == vval(OCTREE))           /* no mkillum? */
552                  oct1date = octreedate > matdate ? octreedate : matdate;
553 <        if (oct1date >= octreedate & oct1date >= matdate
554 <                        & oct1date >= illumdate)        /* all done */
553 >        if ((oct1date >= octreedate) & (oct1date >= matdate)
554 >                        & (oct1date >= illumdate))      /* all done */
555                  return;
556                                                  /* make octree0 */
557 <        if (oct0date < scenedate | oct0date < illumdate) {
557 >        if ((oct0date < scenedate) | (oct0date < illumdate)) {
558                  if (touchonly && oct0date)
559                          touch(oct0name);
560                  else {                          /* build command */
561                          if (octreedate)
562 <                                sprintf(combuf, "oconv%s -i %s %s > %s", ocopts,
563 <                                        vval(OCTREE), vval(ILLUM), oct0name);
562 >                                sprintf(combuf, "%s%s -i %s %s > %s", c_oconv,
563 >                                        ocopts, vval(OCTREE),
564 >                                        vval(ILLUM), oct0name);
565                          else if (vdef(MATERIAL))
566 <                                sprintf(combuf, "oconv%s %s %s > %s", ocopts,
567 <                                        vval(MATERIAL), vval(ILLUM), oct0name);
432 <                        else
433 <                                sprintf(combuf, "oconv%s %s > %s", ocopts,
566 >                                sprintf(combuf, "%s%s %s %s > %s", c_oconv,
567 >                                        ocopts, vval(MATERIAL),
568                                          vval(ILLUM), oct0name);
569 +                        else
570 +                                sprintf(combuf, "%s%s %s > %s", c_oconv,
571 +                                        ocopts, vval(ILLUM), oct0name);
572                          if (runcom(combuf)) {           /* run it */
573                                  fprintf(stderr,
574                                  "%s: error generating octree\n\t%s removed\n",
# Line 451 | Line 588 | oconv()                                /* run oconv and mkillum if necessary */
588          else {
589                  mkillumopts(mkopts);            /* build mkillum command */
590                  mktemp(illumtmp);
591 <                sprintf(combuf, "mkillum%s %s \"<\" %s > %s", mkopts,
591 >                sprintf(combuf, "%s%s %s \"<\" %s > %s", c_mkillum, mkopts,
592                                  oct0name, vval(ILLUM), illumtmp);
593                  if (runcom(combuf)) {                   /* run it */
594 <                        fprintf(stderr, "%s: error running mkillum\n",
595 <                                        progname);
594 >                        fprintf(stderr, "%s: error running %s\n",
595 >                                        progname, c_mkillum);
596                          unlink(illumtmp);
597                          quit(1);
598                  }
599 +                rmfile(oct0name);
600                                                  /* make octree1 (frozen) */
601                  if (octreedate)
602 <                        sprintf(combuf, "oconv%s -f -i %s %s > %s", ocopts,
603 <                                vval(OCTREE), illumtmp, oct1name);
602 >                        sprintf(combuf, "%s%s -f -i %s %s > %s", c_oconv,
603 >                                ocopts, vval(OCTREE), illumtmp, oct1name);
604                  else if (vdef(MATERIAL))
605 <                        sprintf(combuf, "oconv%s -f %s %s > %s", ocopts,
606 <                                vval(MATERIAL), illumtmp, oct1name);
605 >                        sprintf(combuf, "%s%s -f %s %s > %s", c_oconv,
606 >                                ocopts, vval(MATERIAL), illumtmp, oct1name);
607                  else
608 <                        sprintf(combuf, "oconv%s -f %s > %s", ocopts,
608 >                        sprintf(combuf, "%s%s -f %s > %s", c_oconv, ocopts,
609                                  illumtmp, oct1name);
610                  if (runcom(combuf)) {           /* run it */
611                          fprintf(stderr,
# Line 485 | Line 623 | oconv()                                /* run oconv and mkillum if necessary */
623   }
624  
625  
626 < char *
627 < addarg(op, arg)                         /* add argument and advance pointer */
490 < register char   *op, *arg;
626 > 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 +                if (pgmapname != NULL && pgmapdate < oct1date) {
660 +                        if (pgmapdate)
661 +                                rmfile(pgmapname);
662 +                        cp = addarg(cp, "-apg");
663 +                        addarg(cp, vval(PGMAP));
664 +                        cp = sskip(sskip(cp));  /* remove any bandwidth */
665 +                        *cp = '\0';
666 +                }
667 +                if (pcmapname != NULL && pcmapdate < oct1date) {
668 +                        if (pcmapdate)
669 +                                rmfile(pcmapname);
670 +                        cp = addarg(cp, "-apc");
671 +                        addarg(cp, vval(PCMAP));
672 +                        cp = sskip(sskip(cp));  /* remove any bandwidth */
673 +                        *cp = '\0';
674 +                }
675 +                cp = addarg(cp, oct1name);
676 +                if (runcom(combuf)) {
677 +                        fprintf(stderr, "%s: error running %s\n",
678 +                                        progname, c_mkpmap);
679 +                        if (pgmapname != NULL && pgmapdate < oct1date)
680 +                                unlink(pgmapname);
681 +                        if (pcmapname != NULL && pcmapdate < oct1date)
682 +                                unlink(pcmapname);
683 +                        quit(1);
684 +                }
685 +        }
686 +        tnow = time((time_t *)NULL);
687 +        if (pgmapname != NULL)
688 +                pgmapdate = tnow;
689 +        if (pcmapname != NULL)
690 +                pcmapdate = tnow;
691 +        oct1date = tnow;        /* trigger ambient file removal if needed */
692 + }
693 +
694 +
695 + static char *
696 + addarg(                         /* append argument and advance pointer */
697 + char    *op,
698 + char    *arg
699 + )
700 + {
701 +        while (*op)
702 +                op++;
703          *op = ' ';
704 <        while (*++op = *arg++)
704 >        while ( (*++op = *arg++) )
705                  ;
706          return(op);
707   }
708  
709  
710 < oconvopts(oo)                           /* get oconv options */
711 < register char   *oo;
710 > static void
711 > oconvopts(                              /* get oconv options */
712 >        char    *oo
713 > )
714   {
715          /* BEWARE:  This may be called via setdefaults(), so no assumptions */
716  
717          *oo = '\0';
718 <        if (vdef(OCONV))
719 <                addarg(oo, vval(OCONV));
718 >        if (!vdef(OCONV))
719 >                return;
720 >        if (vval(OCONV)[0] != '-') {
721 >                atos(c_oconv, sizeof(c_oconv), vval(OCONV));
722 >                oo = addarg(oo, sskip2(vval(OCONV), 1));
723 >        } else
724 >                oo = addarg(oo, vval(OCONV));
725   }
726  
727  
728 < mkillumopts(mo)                         /* get mkillum options */
729 < register char   *mo;
728 > static void
729 > mkillumopts(                            /* get mkillum options */
730 >        char    *mo
731 > )
732   {
733          /* BEWARE:  This may be called via setdefaults(), so no assumptions */
734  
735 +        if (nprocs > 1)
736 +                sprintf(mo, " -n %d", nprocs);
737 +        else
738 +                *mo = '\0';
739 +        if (!vdef(MKILLUM))
740 +                return;
741 +        if (vval(MKILLUM)[0] != '-') {
742 +                atos(c_mkillum, sizeof(c_mkillum), vval(MKILLUM));
743 +                mo = addarg(mo, sskip2(vval(MKILLUM), 1));
744 +        } else
745 +                mo = addarg(mo, vval(MKILLUM));
746 + }
747 +
748 +
749 + static void
750 + mkpmapopts(                             /* get mkpmap options */
751 +        char    *mo
752 + )
753 + {
754 +        /* BEWARE:  This may be called via setdefaults(), so no assumptions */
755 +
756          *mo = '\0';
757 <        if (vdef(MKILLUM))
758 <                addarg(mo, vval(MKILLUM));
757 >        if (!vdef(MKPMAP))
758 >                return;
759 >        if (vval(MKPMAP)[0] != '-') {
760 >                atos(c_mkpmap, sizeof(c_mkpmap), vval(MKPMAP));
761 >                mo = addarg(mo, sskip2(vval(MKPMAP), 1));
762 >        } else
763 >                mo = addarg(mo, vval(MKPMAP));
764   }
765  
766  
767 < checkambfile()                  /* check date on ambient file */
767 > static void
768 > checkambfile(void)                      /* check date on ambient file */
769   {
770          time_t  afdate;
771  
# Line 526 | Line 773 | checkambfile()                 /* check date on ambient file */
773                  return;
774          if (!(afdate = fdate(vval(AMBFILE))))
775                  return;
776 <        if (oct1date > afdate)
776 >        if (oct1date > afdate) {
777                  if (touchonly)
778                          touch(vval(AMBFILE));
779                  else
780                          rmfile(vval(AMBFILE));
781 +        }
782   }
783  
784  
785 < double
786 < ambval()                                /* compute ambient value */
785 > static double
786 > ambval(void)                            /* compute ambient value */
787   {
788          if (vdef(EXPOSURE)) {
789                  if (vval(EXPOSURE)[0] == '+' || vval(EXPOSURE)[0] == '-')
# Line 547 | Line 795 | ambval()                               /* compute ambient value */
795          if (vlet(ZONE) == 'I')
796                  return(.01);
797          badvalue(ZONE);
798 +        return 0; /* pro forma return */
799   }
800  
801  
802 < renderopts(op, po)                      /* set rendering options */
803 < char    *op, *po;
802 > static void
803 > renderopts(                     /* set rendering options */
804 >        char    *op,
805 >        char    *po
806 > )
807   {
808 +        char    pmapf[256], *bw;
809 +
810          switch(vscale(QUALITY)) {
811          case LOW:
812                  lowqopts(op, po);
# Line 564 | Line 818 | char   *op, *po;
818                  hiqopts(op, po);
819                  break;
820          }
821 +        if (vdef(PGMAP)) {
822 +                bw = sskip2(vval(PGMAP), 2);
823 +                atos(pmapf, sizeof(pmapf), vval(PGMAP));
824 +                op = addarg(addarg(op, "-ap"), pmapf);
825 +                if (atoi(bw) > 0) op = addarg(op, bw);
826 +        }
827 +        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 +        if (vdef(RENDER))
834 +                op = addarg(op, vval(RENDER));
835 +        if (rvdevice != NULL) {
836 +                if (vdef(RVU))
837 +                        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 +        } else {
843 +                if (vdef(RPICT))
844 +                        if (vval(RPICT)[0] != '-') {
845 +                                atos(c_rpict, sizeof(c_rpict), vval(RPICT));
846 +                                po = addarg(po, sskip2(vval(RPICT), 1));
847 +                        } else
848 +                                po = addarg(po, vval(RPICT));
849 +        }
850   }
851  
852  
853 < lowqopts(op, po)                        /* low quality rendering options */
854 < register char   *op;
855 < char    *po;
853 > static void
854 > lowqopts(                       /* low quality rendering options */
855 >        char    *op,
856 >        char    *po
857 > )
858   {
859          double  d, org[3], siz[3];
860  
# Line 579 | Line 864 | char   *po;
864                          &siz[0], &org[1], &siz[1], &org[2], &siz[2]) != 6)
865                  badvalue(ZONE);
866          siz[0] -= org[0]; siz[1] -= org[1]; siz[2] -= org[2];
867 <        if (siz[0] <= FTINY | siz[1] <= FTINY | siz[2] <= FTINY)
867 >        if ((siz[0] <= FTINY) | (siz[1] <= FTINY) | (siz[2] <= FTINY))
868                  badvalue(ZONE);
869          getoctcube(org, &d);
870          d *= 3./(siz[0]+siz[1]+siz[2]);
# Line 587 | Line 872 | char   *po;
872          case LOW:
873                  po = addarg(po, "-ps 16");
874                  op = addarg(op, "-dp 64");
875 <                sprintf(op, " -ar %d", (int)(4*d));
875 >                sprintf(op, " -ar %d", (int)(8*d));
876                  op += strlen(op);
877                  break;
878          case MEDIUM:
879                  po = addarg(po, "-ps 8");
880                  op = addarg(op, "-dp 128");
881 <                sprintf(op, " -ar %d", (int)(8*d));
881 >                sprintf(op, " -ar %d", (int)(16*d));
882                  op += strlen(op);
883                  break;
884          case HIGH:
885                  po = addarg(po, "-ps 4");
886                  op = addarg(op, "-dp 256");
887 <                sprintf(op, " -ar %d", (int)(16*d));
887 >                sprintf(op, " -ar %d", (int)(32*d));
888                  op += strlen(op);
889                  break;
890          }
# Line 608 | Line 893 | char   *po;
893                  op = addarg(op, "-ds .4");
894          else
895                  op = addarg(op, "-ds 0");
896 <        op = addarg(op, "-dt .2 -dc .25 -dr 0 -sj 0 -st .5");
896 >        op = addarg(op, "-dt .2 -dc .25 -dr 0 -ss 0 -st .5");
897          if (vdef(AMBFILE)) {
898                  sprintf(op, " -af %s", vval(AMBFILE));
899                  op += strlen(op);
# Line 616 | Line 901 | char   *po;
901                  overture = 0;
902          switch (vscale(VARIABILITY)) {
903          case LOW:
904 <                op = addarg(op, "-aw 128 -aa .4 -ad 64");
904 >                op = addarg(op, "-aa .3 -ad 256");
905                  break;
906          case MEDIUM:
907 <                op = addarg(op, "-aw 1024 -aa .3 -ad 128");
907 >                op = addarg(op, "-aa .25 -ad 512");
908                  break;
909          case HIGH:
910 <                op = addarg(op, "-aa .25 -ad 256");
910 >                op = addarg(op, "-aa .2 -ad 1024");
911                  break;
912          }
913          op = addarg(op, "-as 0");
914          d = ambval();
915          sprintf(op, " -av %.2g %.2g %.2g", d, d, d);
916          op += strlen(op);
917 <        op = addarg(op, "-lr 3 -lw .02");
633 <        if (vdef(RENDER))
634 <                op = addarg(op, vval(RENDER));
917 >        op = addarg(op, "-lr 6 -lw .003");
918   }
919  
920  
921 < medqopts(op, po)                        /* medium quality rendering options */
922 < register char   *op;
923 < char    *po;
921 > static void
922 > medqopts(                       /* medium quality rendering options */
923 >        char    *op,
924 >        char    *po
925 > )
926   {
927          double  d, org[3], siz[3], asz;
928  
# Line 647 | Line 932 | char   *po;
932                          &siz[0], &org[1], &siz[1], &org[2], &siz[2]) != 6)
933                  badvalue(ZONE);
934          siz[0] -= org[0]; siz[1] -= org[1]; siz[2] -= org[2];
935 <        if (siz[0] <= FTINY | siz[1] <= FTINY | siz[2] <= FTINY)
935 >        if ((siz[0] <= FTINY) | (siz[1] <= FTINY) | (siz[2] <= FTINY))
936                  badvalue(ZONE);
937          getoctcube(org, &d);
938          asz = (siz[0]+siz[1]+siz[2])/3.;
# Line 656 | Line 941 | char   *po;
941          case LOW:
942                  po = addarg(po, vbool(PENUMBRAS) ? "-ps 4" : "-ps 8");
943                  op = addarg(op, "-dp 256");
944 <                sprintf(op, " -ar %d", (int)(8*d));
944 >                sprintf(op, " -ar %d", (int)(16*d));
945                  op += strlen(op);
946                  sprintf(op, " -ms %.2g", asz/20.);
947                  op += strlen(op);
# Line 664 | Line 949 | char   *po;
949          case MEDIUM:
950                  po = addarg(po, vbool(PENUMBRAS) ? "-ps 3" : "-ps 6");
951                  op = addarg(op, "-dp 512");
952 <                sprintf(op, " -ar %d", (int)(16*d));
952 >                sprintf(op, " -ar %d", (int)(32*d));
953                  op += strlen(op);
954                  sprintf(op, " -ms %.2g", asz/40.);
955                  op += strlen(op);
# Line 672 | Line 957 | char   *po;
957          case HIGH:
958                  po = addarg(po, vbool(PENUMBRAS) ? "-ps 2" : "-ps 4");
959                  op = addarg(op, "-dp 1024");
960 <                sprintf(op, " -ar %d", (int)(32*d));
960 >                sprintf(op, " -ar %d", (int)(64*d));
961                  op += strlen(op);
962                  sprintf(op, " -ms %.2g", asz/80.);
963                  op += strlen(op);
# Line 680 | Line 965 | char   *po;
965          }
966          po = addarg(po, "-pt .08");
967          if (vbool(PENUMBRAS))
968 <                op = addarg(op, "-ds .2 -dj .5");
968 >                op = addarg(op, "-ds .2 -dj .9");
969          else
970                  op = addarg(op, "-ds .3");
971 <        op = addarg(op, "-dt .1 -dc .5 -dr 1 -sj .7 -st .1");
972 <        if (overture = vint(INDIRECT)) {
971 >        op = addarg(op, "-dt .1 -dc .5 -dr 1 -ss 1 -st .1");
972 >        if ( (overture = vint(INDIRECT)) ) {
973                  sprintf(op, " -ab %d", overture);
974                  op += strlen(op);
975          }
# Line 695 | Line 980 | char   *po;
980                  overture = 0;
981          switch (vscale(VARIABILITY)) {
982          case LOW:
983 <                op = addarg(op, "-aw 128 -aa .25 -ad 196 -as 0");
983 >                op = addarg(op, "-aa .2 -ad 329 -as 42");
984                  break;
985          case MEDIUM:
986 <                op = addarg(op, "-aw 1024 -aa .2 -ad 400 -as 64");
986 >                op = addarg(op, "-aa .15 -ad 800 -as 128");
987                  break;
988          case HIGH:
989 <                op = addarg(op, "-aa .15 -ad 768 -as 196");
989 >                op = addarg(op, "-aa .1 -ad 1536 -as 392");
990                  break;
991          }
992          d = ambval();
993          sprintf(op, " -av %.2g %.2g %.2g", d, d, d);
994          op += strlen(op);
995 <        op = addarg(op, "-lr 6 -lw .002");
711 <        if (vdef(RENDER))
712 <                op = addarg(op, vval(RENDER));
995 >        op = addarg(op, "-lr 8 -lw 1e-4");
996   }
997  
998  
999 < hiqopts(op, po)                         /* high quality rendering options */
1000 < register char   *op;
1001 < char    *po;
999 > static void
1000 > hiqopts(                                /* high quality rendering options */
1001 >        char    *op,
1002 >        char    *po
1003 > )
1004   {
1005          double  d, org[3], siz[3], asz;
1006  
# Line 725 | Line 1010 | char   *po;
1010                          &siz[0], &org[1], &siz[1], &org[2], &siz[2]) != 6)
1011                  badvalue(ZONE);
1012          siz[0] -= org[0]; siz[1] -= org[1]; siz[2] -= org[2];
1013 <        if (siz[0] <= FTINY | siz[1] <= FTINY | siz[2] <= FTINY)
1013 >        if ((siz[0] <= FTINY) | (siz[1] <= FTINY) | (siz[2] <= FTINY))
1014                  badvalue(ZONE);
1015          getoctcube(org, &d);
1016          asz = (siz[0]+siz[1]+siz[2])/3.;
# Line 734 | Line 1019 | char   *po;
1019          case LOW:
1020                  po = addarg(po, vbool(PENUMBRAS) ? "-ps 1" : "-ps 8");
1021                  op = addarg(op, "-dp 1024");
1022 <                sprintf(op, " -ar %d", (int)(16*d));
1022 >                sprintf(op, " -ar %d", (int)(32*d));
1023                  op += strlen(op);
1024                  sprintf(op, " -ms %.2g", asz/40.);
1025                  op += strlen(op);
# Line 742 | Line 1027 | char   *po;
1027          case MEDIUM:
1028                  po = addarg(po, vbool(PENUMBRAS) ? "-ps 1" : "-ps 5");
1029                  op = addarg(op, "-dp 2048");
1030 <                sprintf(op, " -ar %d", (int)(32*d));
1030 >                sprintf(op, " -ar %d", (int)(64*d));
1031                  op += strlen(op);
1032                  sprintf(op, " -ms %.2g", asz/80.);
1033                  op += strlen(op);
# Line 750 | Line 1035 | char   *po;
1035          case HIGH:
1036                  po = addarg(po, vbool(PENUMBRAS) ? "-ps 1" : "-ps 3");
1037                  op = addarg(op, "-dp 4096");
1038 <                sprintf(op, " -ar %d", (int)(64*d));
1038 >                sprintf(op, " -ar %d", (int)(128*d));
1039                  op += strlen(op);
1040                  sprintf(op, " -ms %.2g", asz/160.);
1041                  op += strlen(op);
# Line 758 | Line 1043 | char   *po;
1043          }
1044          po = addarg(po, "-pt .04");
1045          if (vbool(PENUMBRAS))
1046 <                op = addarg(op, "-ds .1 -dj .65");
1046 >                op = addarg(op, "-ds .1 -dj .9");
1047          else
1048                  op = addarg(op, "-ds .2");
1049 <        op = addarg(op, "-dt .05 -dc .75 -dr 3 -sj 1 -st .01");
1049 >        op = addarg(op, "-dt .05 -dc .75 -dr 3 -ss 16 -st .01");
1050          sprintf(op, " -ab %d", overture=vint(INDIRECT)+1);
1051          op += strlen(op);
1052          if (vdef(AMBFILE)) {
# Line 771 | Line 1056 | char   *po;
1056                  overture = 0;
1057          switch (vscale(VARIABILITY)) {
1058          case LOW:
1059 <                op = addarg(op, "-aw 128 -aa .15 -ad 256 -as 0");
1059 >                op = addarg(op, "-aa .125 -ad 512 -as 64");
1060                  break;
1061          case MEDIUM:
1062 <                op = addarg(op, "-aw 1024 -aa .125 -ad 512 -as 256");
1062 >                op = addarg(op, "-aa .1 -ad 1536 -as 768");
1063                  break;
1064          case HIGH:
1065 <                op = addarg(op, "-aa .08 -ad 1024 -as 512");
1065 >                op = addarg(op, "-aa .075 -ad 4096 -as 2048");
1066                  break;
1067          }
1068          d = ambval();
1069          sprintf(op, " -av %.2g %.2g %.2g", d, d, d);
1070          op += strlen(op);
1071 <        op = addarg(op, "-lr 12 -lw .0005");
787 <        if (vdef(RENDER))
788 <                op = addarg(op, vval(RENDER));
1071 >        op = addarg(op, "-lr 12 -lw 1e-5");
1072   }
1073  
1074  
1075 < xferopts(ro)                            /* transfer options if indicated */
1076 < char    *ro;
1075 > #ifdef _WIN32
1076 > static void
1077 > setenv(                 /* set an environment variable */
1078 >        char    *vname,
1079 >        char    *value
1080 > )
1081   {
1082 +        char    *evp;
1083 +
1084 +        evp = bmalloc(strlen(vname)+strlen(value)+2);
1085 +        if (evp == NULL)
1086 +                syserr(progname);
1087 +        sprintf(evp, "%s=%s", vname, value);
1088 +        if (putenv(evp) != 0) {
1089 +                fprintf(stderr, "%s: out of environment space\n", progname);
1090 +                quit(1);
1091 +        }
1092 +        if (!silent)
1093 +                printf("set %s\n", evp);
1094 + }
1095 + #endif
1096 +
1097 +
1098 + static void
1099 + xferopts(                               /* transfer options if indicated */
1100 +        char    *ro
1101 + )
1102 + {
1103          int     fd, n;
1104 <        register char   *cp;
1104 >        char    *cp;
1105          
1106          n = strlen(ro);
1107          if (n < 2)
# Line 811 | Line 1119 | char   *ro;
1119                          syserr(vval(OPTFILE));
1120                  sprintf(ro, " @%s", vval(OPTFILE));
1121          }
1122 < #ifdef MSDOS
1122 > #ifdef _WIN32
1123          else if (n > 50) {
1124                  setenv("ROPT", ro+1);
1125                  strcpy(ro, " $ROPT");
# Line 820 | Line 1128 | char   *ro;
1128   }
1129  
1130  
1131 < pfiltopts(po)                           /* get pfilt options */
1132 < register char   *po;
1131 > static void
1132 > pfiltopts(                              /* get pfilt options */
1133 >        char    *po
1134 > )
1135   {
1136          *po = '\0';
1137          if (vdef(EXPOSURE)) {
# Line 830 | Line 1140 | register char  *po;
1140          }
1141          switch (vscale(QUALITY)) {
1142          case MEDIUM:
1143 <                po = addarg(po, "-r 1");
1143 >                po = addarg(po, "-r .6");
1144                  break;
1145          case HIGH:
1146                  po = addarg(po, "-m .25");
1147                  break;
1148          }
1149          if (vdef(PFILT))
1150 <                po = addarg(po, vval(PFILT));
1150 >                if (vval(PFILT)[0] != '-') {
1151 >                        atos(c_pfilt, sizeof(c_pfilt), vval(PFILT));
1152 >                        po = addarg(po, sskip2(vval(PFILT), 1));
1153 >                } else
1154 >                        po = addarg(po, vval(PFILT));
1155   }
1156  
1157  
1158 < matchword(s1, s2)                       /* match white-delimited words */
1159 < register char   *s1, *s2;
1158 > static int
1159 > matchword(                      /* match white-delimited words */
1160 >        char    *s1,
1161 >        char    *s2
1162 > )
1163   {
1164          while (isspace(*s1)) s1++;
1165          while (isspace(*s2)) s2++;
# Line 853 | Line 1170 | register char  *s1, *s2;
1170   }
1171  
1172  
1173 < char *
1174 < specview(vs)                            /* get proper view spec from vs */
1175 < register char   *vs;
1173 > static char *
1174 > specview(                               /* get proper view spec from vs */
1175 >        char    *vs
1176 > )
1177   {
1178          static char     vup[7][12] = {"-vu 0 0 -1","-vu 0 -1 0","-vu -1 0 0",
1179                          "-vu 0 0 1", "-vu 1 0 0","-vu 0 1 0","-vu 0 0 1"};
1180          static char     viewopts[128];
1181 <        register char   *cp;
1181 >        char    *cp;
1182          int     xpos, ypos, zpos, viewtype, upax;
1183 <        register int    i;
1183 >        int     i;
1184          double  cent[3], dim[3], mult, d;
1185  
1186          if (vs == NULL || *vs == '-')
# Line 873 | Line 1191 | register char  *vs;
1191                          upax = 1-'X'+UPPER(vval(UP)[1]);
1192                  else
1193                          upax = 1-'X'+vlet(UP);
1194 <                if (upax < 1 | upax > 3)
1194 >                if ((upax < 1) | (upax > 3))
1195                          badvalue(UP);
1196                  if (vval(UP)[0] == '-')
1197                          upax = -upax;
# Line 895 | Line 1213 | register char  *vs;
1213          } else if (*vs == 'z') {
1214                  zpos = -1; vs++;
1215          }
1216 <        viewtype = 'v';
1217 <        if (*vs == 'v' | *vs == 'l' | *vs == 'a' | *vs == 'h' | *vs == 'c')
1216 >        switch (*vs) {
1217 >        case VT_PER:
1218 >        case VT_PAR:
1219 >        case VT_ANG:
1220 >        case VT_HEM:
1221 >        case VT_PLS:
1222 >        case VT_CYL:
1223                  viewtype = *vs++;
1224 +                break;
1225 +        default:
1226 +                viewtype = VT_PER;
1227 +                break;
1228 +        }
1229          cp = viewopts;
1230          if ((!*vs || isspace(*vs)) && (xpos|ypos|zpos)) {       /* got one! */
1231                  *cp++ = '-'; *cp++ = 'v'; *cp++ = 't'; *cp++ = viewtype;
# Line 912 | Line 1240 | register char  *vs;
1240                          cent[i] += .5*dim[i];
1241                  }
1242                  mult = vlet(ZONE)=='E' ? 2. : .45 ;
1243 <                sprintf(cp, " -vp %.2g %.2g %.2g -vd %.2g %.2g %.2g",
1243 >                sprintf(cp, " -vp %.3g %.3g %.3g -vd %.3g %.3g %.3g",
1244                                  cent[0]+xpos*mult*dim[0],
1245                                  cent[1]+ypos*mult*dim[1],
1246                                  cent[2]+zpos*mult*dim[2],
# Line 939 | Line 1267 | register char  *vs;
1267                  }
1268                  cp = addarg(cp, vup[upax+3]);
1269                  switch (viewtype) {
1270 <                case 'v':
1270 >                case VT_PER:
1271                          cp = addarg(cp, "-vh 45 -vv 45");
1272                          break;
1273 <                case 'l':
1273 >                case VT_PAR:
1274                          d = sqrt(dim[0]*dim[0]+dim[1]*dim[1]+dim[2]*dim[2]);
1275 <                        sprintf(cp, " -vh %.2g -vv %.2g", d, d);
1275 >                        sprintf(cp, " -vh %.3g -vv %.3g", d, d);
1276                          cp += strlen(cp);
1277                          break;
1278 <                case 'a':
1279 <                case 'h':
1278 >                case VT_ANG:
1279 >                case VT_HEM:
1280 >                case VT_PLS:
1281                          cp = addarg(cp, "-vh 180 -vv 180");
1282                          break;
1283 <                case 'c':
1283 >                case VT_CYL:
1284                          cp = addarg(cp, "-vh 180 -vv 90");
1285                          break;
1286                  }
# Line 959 | Line 1288 | register char  *vs;
1288                  while (!isspace(*vs))           /* else skip id */
1289                          if (!*vs++)
1290                                  return(NULL);
1291 <                if (upax) {                     /* specify up vector */
1291 >                if (upax) {                     /* prepend up vector */
1292                          strcpy(cp, vup[upax+3]);
1293                          cp += strlen(cp);
1294                  }
# Line 967 | Line 1296 | register char  *vs;
1296          if (cp == viewopts)             /* append any additional options */
1297                  vs++;           /* skip prefixed space if unneeded */
1298          strcpy(cp, vs);
1299 < #ifdef MSDOS
1299 > #ifdef _WIN32
1300          if (strlen(viewopts) > 40) {
1301                  setenv("VIEW", viewopts);
1302                  return("$VIEW");
# Line 977 | Line 1306 | register char  *vs;
1306   }
1307  
1308  
1309 < char *
1310 < getview(n, vn)                          /* get view n, or NULL if none */
1311 < int     n;
1312 < char    *vn;            /* returned view name */
1309 > static char *
1310 > getview(                                /* get view n, or NULL if none */
1311 >        int     n,
1312 >        char    *vn             /* returned view name */
1313 > )
1314   {
1315 <        register char   *mv;
1315 >        char    *mv;
1316  
1317          if (viewselect != NULL) {               /* command-line selected */
1318                  if (n)                          /* only do one */
1319                          return(NULL);
1320 +                                        
1321 +                if (isint(viewselect)) {        /* view number? */
1322 +                        n = atoi(viewselect)-1;
1323 +                        goto numview;
1324 +                }
1325                  if (viewselect[0] == '-') {     /* already specified */
1326 <                        if (vn != NULL) *vn = '\0';
1326 >                        if (vn != NULL)
1327 >                                strcpy(vn, "0");
1328                          return(viewselect);
1329                  }
1330                  if (vn != NULL) {
# Line 997 | Line 1333 | char   *vn;            /* returned view name */
1333                                  ;
1334                          *vn = '\0';
1335                  }
1000                                                /* view number? */
1001                if (isint(viewselect))
1002                        return(specview(nvalue(VIEW, atoi(viewselect)-1)));
1336                                                  /* check list */
1337 <                while ((mv = nvalue(VIEW, n++)) != NULL)
1337 >                while ((mv = nvalue(VIEWS, n++)) != NULL)
1338                          if (matchword(viewselect, mv))
1339                                  return(specview(mv));
1340 +
1341                  return(specview(viewselect));   /* standard view? */
1342          }
1343 <        mv = nvalue(VIEW, n);           /* use view n */
1344 <        if (vn != NULL & mv != NULL) {
1345 <                register char   *mv2 = mv;
1346 <                if (*mv2 != '-')
1343 > numview:
1344 >        mv = nvalue(VIEWS, n);          /* use view n */
1345 >        if ((vn != NULL) & (mv != NULL))
1346 >                if (*mv != '-') {
1347 >                        char    *mv2 = mv;
1348                          while (*mv2 && !isspace(*mv2))
1349                                  *vn++ = *mv2++;
1350 <                *vn = '\0';
1351 <        }
1350 >                        *vn = '\0';
1351 >                } else
1352 >                        sprintf(vn, "%d", n+1);
1353 >
1354          return(specview(mv));
1355   }
1356  
1357  
1358 < printview(vopts)                        /* print out selected view */
1359 < register char   *vopts;
1358 > static int
1359 > myprintview(                    /* print out selected view */
1360 >        char    *vopts,
1361 >        FILE    *fp
1362 > )
1363   {
1364 <        extern char     *atos(), *getenv();
1365 <        char    buf[256];
1366 <        FILE    *fp;
1367 <        register char   *cp;
1368 <
1364 >        VIEW    vwr;
1365 >        char    buf[128];
1366 >        char    *cp;
1367 > #ifdef _WIN32
1368 > /* XXX Should we allow something like this for all platforms? */
1369 > /* XXX Or is it still required at all? */
1370 > again:
1371 > #endif
1372          if (vopts == NULL)
1373                  return(-1);
1374 <        fputs("VIEW=", stdout);
1375 <        do {
1376 <                if (matchword(vopts, "-vf")) {          /* expand view file */
1377 <                        vopts = sskip(vopts);
1378 <                        if (!*atos(buf, sizeof(buf), vopts))
1036 <                                return(-1);
1037 <                        if ((fp = fopen(buf, "r")) == NULL)
1038 <                                return(-1);
1039 <                        for (buf[sizeof(buf)-2] = '\n';
1040 <                                        fgets(buf, sizeof(buf), fp) != NULL &&
1041 <                                                buf[0] != '\n';
1042 <                                        buf[sizeof(buf)-2] = '\n') {
1043 <                                if (buf[sizeof(buf)-2] != '\n') {
1044 <                                        ungetc(buf[sizeof(buf)-2], fp);
1045 <                                        buf[sizeof(buf)-2] = '\0';
1046 <                                }
1047 <                                if (matchword(buf, "VIEW=") ||
1048 <                                                matchword(buf, "rview")) {
1049 <                                        for (cp = sskip(buf); *cp && *cp != '\n'; cp++)
1050 <                                                putchar(*cp);
1051 <                                }
1052 <                        }
1053 <                        fclose(fp);
1054 <                        vopts = sskip(vopts);
1055 <                } else {
1056 <                        while (isspace(*vopts))
1057 <                                vopts++;
1058 <                        putchar(' ');
1059 < #ifdef MSDOS
1060 <                        if (*vopts == '$') {            /* expand env. var. */
1061 <                                if (!*atos(buf, sizeof(buf), vopts+1))
1062 <                                        return(-1);
1063 <                                if ((cp = getenv(buf)) == NULL)
1064 <                                        return(-1);
1065 <                                fputs(cp, stdout);
1066 <                                vopts = sskip(vopts);
1067 <                        } else
1374 > #ifdef _WIN32
1375 >        if (vopts[0] == '$') {
1376 >                vopts = getenv(vopts+1);
1377 >                goto again;
1378 >        }
1379   #endif
1380 <                                while (*vopts && !isspace(*vopts))
1381 <                                        putchar(*vopts++);
1382 <                }
1383 <        } while (*vopts++);
1384 <        putchar('\n');
1380 >        vwr = stdview;
1381 >        sscanview(&vwr, cp=vopts);              /* set initial options */
1382 >        while ((cp = strstr(cp, "-vf ")) != NULL &&
1383 >                        *atos(buf, sizeof(buf), cp += 4)) {
1384 >                viewfile(buf, &vwr, NULL);      /* load -vf file */
1385 >                sscanview(&vwr, cp);            /* reset tail */
1386 >        }
1387 >        fputs(VIEWSTR, fp);
1388 >        fprintview(&vwr, fp);                   /* print full spec. */
1389 >        fputc('\n', fp);
1390          return(0);
1391   }
1392  
1393  
1394 < rview(opts, po)                         /* run rview with first view */
1395 < char    *opts, *po;
1394 > static void
1395 > rvu(                            /* run rvu with first view */
1396 >        char    *opts,
1397 >        char    *po
1398 > )
1399   {
1400          char    *vw;
1401 <        char    combuf[512];
1401 >        char    combuf[PATH_MAX];
1402                                          /* build command */
1403          if (touchonly || (vw = getview(0, NULL)) == NULL)
1404                  return;
1405          if (sayview)
1406 <                printview(vw);
1407 <        sprintf(combuf, "rview %s%s%s -R %s ", vw, po, opts, rifname);
1406 >                myprintview(vw, stdout);
1407 >        sprintf(combuf, "%s %s%s%s -R %s ", c_rvu, vw, opts, po, rifname);
1408 >        if (nprocs > 1)
1409 >                sprintf(combuf+strlen(combuf), "-n %d ", nprocs);
1410          if (rvdevice != NULL)
1411                  sprintf(combuf+strlen(combuf), "-o %s ", rvdevice);
1412          if (vdef(EXPOSURE))
1413                  sprintf(combuf+strlen(combuf), "-pe %s ", vval(EXPOSURE));
1414          strcat(combuf, oct1name);
1415          if (runcom(combuf)) {           /* run it */
1416 <                fprintf(stderr, "%s: error running rview\n", progname);
1416 >                fprintf(stderr, "%s: error running %s\n", progname, c_rvu);
1417                  quit(1);
1418          }
1419   }
1420  
1421  
1422 < rpict(opts, po)                         /* run rpict and pfilt for each view */
1423 < char    *opts, *po;
1422 > static int
1423 > syncf_done(                     /* check if an rpiece sync file is complete */
1424 >        char *sfname
1425 > )
1426   {
1427 <        char    combuf[1024];
1428 <        char    rawfile[MAXPATH], picfile[MAXPATH];
1429 <        char    zopt[MAXPATH+4], rep[MAXPATH+16], res[32];
1427 >        FILE    *fp = fopen(sfname, "r");
1428 >        int     todo = 1;
1429 >        int     x, y;
1430 >
1431 >        if (fp == NULL)
1432 >                return(0);
1433 >        if (fscanf(fp, "%d %d", &x, &y) != 2)
1434 >                goto checked;
1435 >        todo = x*y;             /* total number of tiles */
1436 >        if (fscanf(fp, "%d %d", &x, &y) != 2 || (x != 0) | (y != 0))
1437 >                goto checked;
1438 >                                /* XXX assume no redundant tiles */
1439 >        while (fscanf(fp, "%d %d", &x, &y) == 2)
1440 >                if (!--todo)
1441 >                        break;
1442 > checked:
1443 >        fclose(fp);
1444 >        return(!todo);
1445 > }
1446 >
1447 >
1448 > static void
1449 > rpict(                          /* run rpict and pfilt for each view */
1450 >        char    *opts,
1451 >        char    *po
1452 > )
1453 > {
1454 > #define do_rpiece       (sfile[0]!='\0')
1455 >        char    combuf[5*PATH_MAX+512];
1456 >        char    rawfile[PATH_MAX], picfile[PATH_MAX];
1457 >        char    zopt[PATH_MAX+4], rep[PATH_MAX+16], res[32];
1458 >        char    rppopt[32], sfile[PATH_MAX], *pfile = NULL;
1459          char    pfopts[128];
1460          char    vs[32], *vw;
1461          int     vn, mult;
1462 +        FILE    *fp;
1463          time_t  rfdt, pfdt;
1464 +        int     xres, yres;
1465 +        double  aspect;
1466 +        int     n;
1467                                          /* get pfilt options */
1468          pfiltopts(pfopts);
1469                                          /* get resolution, reporting */
# Line 1122 | Line 1478 | char   *opts, *po;
1478                  mult = 3;
1479                  break;
1480          }
1481 <        {
1482 <                int     xres, yres;
1483 <                double  aspect;
1484 <                int     n;
1485 <                n = sscanf(vval(RESOLUTION), "%d %d %lf", &xres, &yres, &aspect);
1486 <                if (n == 3)
1487 <                        sprintf(res, "-x %d -y %d -pa %.3f",
1488 <                                        mult*xres, mult*yres, aspect);
1489 <                else if (n) {
1490 <                        if (n == 1) yres = xres;
1135 <                        sprintf(res, "-x %d -y %d", mult*xres, mult*yres);
1136 <                } else
1137 <                        badvalue(RESOLUTION);
1138 <        }
1481 >        n = sscanf(vval(RESOLUTION), "%d %d %lf", &xres, &yres, &aspect);
1482 >        if (n == 3)
1483 >                sprintf(res, "-x %d -y %d -pa %.3f",
1484 >                                mult*xres, mult*yres, aspect);
1485 >        else if (n) {
1486 >                aspect = 1.;
1487 >                if (n == 1) yres = xres;
1488 >                sprintf(res, "-x %d -y %d", mult*xres, mult*yres);
1489 >        } else
1490 >                badvalue(RESOLUTION);
1491          rep[0] = '\0';
1492          if (vdef(REPORT)) {
1493                  double  minutes;
1142                int     n;
1494                  n = sscanf(vval(REPORT), "%lf %s", &minutes, rawfile);
1495                  if (n == 2)
1496                          sprintf(rep, " -t %d -e %s", (int)(minutes*60), rawfile);
# Line 1148 | Line 1499 | char   *opts, *po;
1499                  else
1500                          badvalue(REPORT);
1501          }
1502 <                                        /* do each view */
1503 <        vn = 0;
1502 >                                        /* set up parallel rendering */
1503 >        sfile[0] = '\0';
1504 >        if ((nprocs > 1) & !touchonly & !vdef(ZFILE) &&
1505 >                                        getview(0, vs) != NULL) {
1506 >                if (!strcmp(c_rpict, DEF_RPICT_PATH) &&
1507 >                                getview(1, NULL) == NULL) {
1508 >                        sprintf(sfile, "%s_%s_rpsync.txt",
1509 >                                vdef(RAWFILE) ? vval(RAWFILE) : vval(PICTURE),
1510 >                                        vs);
1511 >                        strcpy(rppopt, "-PP pfXXXXXX");
1512 >                } else {
1513 >                        strcpy(rppopt, "-S 1 -PP pfXXXXXX");
1514 >                }
1515 >                pfile = rppopt + strlen(rppopt) - 8;
1516 >                if (mktemp(pfile) == NULL) {
1517 >                        if (do_rpiece) {
1518 >                                fprintf(stderr, "%s: cannot create\n", pfile);
1519 >                                quit(1);
1520 >                        }
1521 >                        pfile = NULL;
1522 >                }
1523 >        }
1524 >        vn = 0;                                 /* do each view */
1525          while ((vw = getview(vn++, vs)) != NULL) {
1526                  if (sayview)
1527 <                        printview(vw);
1528 <                if (!vs[0])
1157 <                        sprintf(vs, "%d", vn);
1158 <                sprintf(picfile, "%s_%s.pic", vval(PICTURE), vs);
1527 >                        myprintview(vw, stdout);
1528 >                sprintf(picfile, "%s_%s.hdr", vval(PICTURE), vs);
1529                  if (vdef(ZFILE))
1530                          sprintf(zopt, " -z %s_%s.zbf", vval(ZFILE), vs);
1531                  else
# Line 1176 | Line 1546 | char   *opts, *po;
1546                                  touch(picfile);
1547                          continue;
1548                  }
1549 +                                                /* parallel running? */
1550 +                if (do_rpiece) {
1551 +                        if (rfdt < oct1date || !fdate(sfile)) {
1552 +                                int     xdiv = 8+nprocs/3, ydiv = 8+nprocs/3;
1553 +                                if (rfdt >= oct1date) {
1554 +                                        fprintf(stderr,
1555 +                "%s: partial output not created with %s\n", rawfile, c_rpiece);
1556 +                                        quit(1);
1557 +                                }
1558 +                                if (rfdt) {     /* start fresh */
1559 +                                        rmfile(rawfile);
1560 +                                        rfdt = 0;
1561 +                                }
1562 +                                if (!silent)
1563 +                                        printf("\techo %d %d > %s\n",
1564 +                                                        xdiv, ydiv, sfile);
1565 +                                if ((fp = fopen(sfile, "w")) == NULL) {
1566 +                                        fprintf(stderr, "%s: cannot create\n",
1567 +                                                        sfile);
1568 +                                        quit(1);
1569 +                                }
1570 +                                fprintf(fp, "%d %d\n", xdiv, ydiv);
1571 +                                fclose(fp);
1572 +                        }
1573 +                } else if (next_process(0)) {
1574 +                        if (pfile != NULL)
1575 +                                sleep(10);
1576 +                        continue;
1577 +                } else if (!inchild())
1578 +                        pfile = NULL;
1579 +                /* XXX Remember to call finish_process() */
1580                                                  /* build rpict command */
1581 <                if (rfdt >= oct1date)           /* recover */
1582 <                        sprintf(combuf, "rpict%s%s%s%s -ro %s %s",
1583 <                                rep, po, opts, zopt, rawfile, oct1name);
1584 <                else {
1581 >                if (rfdt >= oct1date) {         /* already in progress */
1582 >                        if (do_rpiece) {
1583 >                                sprintf(combuf, "%s -R %s %s%s %s %s%s%s -o %s %s",
1584 >                                                c_rpiece, sfile, rppopt, rep, vw,
1585 >                                                res, opts, po, rawfile, oct1name);
1586 >                                while (next_process(1)) {
1587 >                                        sleep(10);
1588 >                                        combuf[strlen(c_rpiece)+2] = 'F';
1589 >                                }
1590 >                        } else
1591 >                                sprintf(combuf, "%s%s%s%s%s -ro %s %s", c_rpict,
1592 >                                        rep, opts, po, zopt, rawfile, oct1name);
1593 >                        if (runcom(combuf))     /* run rpict/rpiece */
1594 >                                goto rperror;
1595 >                } else {
1596                          if (overture) {         /* run overture calculation */
1597                                  sprintf(combuf,
1598 <                                "rpict%s %s%s -x 64 -y 64 -ps 1 %s > %s",
1599 <                                                rep, vw, opts,
1598 >                                        "%s%s %s%s -x 64 -y 64 -ps 1 %s > %s",
1599 >                                                c_rpict, rep, vw, opts,
1600                                                  oct1name, overfile);
1601 <                                if (runcom(combuf)) {
1602 <                                        fprintf(stderr,
1601 >                                if (!do_rpiece || !next_process(0)) {
1602 >                                        if (runcom(combuf)) {
1603 >                                                fprintf(stderr,
1604                                          "%s: error in overture for view %s\n",
1605 <                                                progname, vs);
1606 <                                        quit(1);
1607 <                                }
1608 < #ifdef NIX
1609 <                                rmfile(overfile);
1605 >                                                        progname, vs);
1606 >                                                quit(1);
1607 >                                        }
1608 > #ifndef NULL_DEVICE
1609 >                                        rmfile(overfile);
1610   #endif
1611 +                                } else if (do_rpiece)
1612 +                                        sleep(20);
1613                          }
1614 <                        sprintf(combuf, "rpict%s %s %s%s%s%s %s > %s",
1615 <                                        rep, vw, res, po, opts, zopt,
1616 <                                        oct1name, rawfile);
1614 >                        if (do_rpiece) {
1615 >                                sprintf(combuf, "%s -F %s %s%s %s %s%s%s -o %s %s",
1616 >                                                c_rpiece, sfile, rppopt, rep, vw,
1617 >                                                res, opts, po, rawfile, oct1name);
1618 >                                while (next_process(1))
1619 >                                        sleep(10);
1620 >                        } else {
1621 >                                sprintf(combuf, "%s%s %s %s%s%s%s %s > %s",
1622 >                                                c_rpict, rep, vw, res, opts, po,
1623 >                                                zopt, oct1name, rawfile);
1624 >                        }
1625 >                        if ((pfile != NULL) & !do_rpiece) {
1626 >                                if (!silent)
1627 >                                        printf("\t%s\n", combuf);
1628 >                                fflush(stdout);
1629 >                                sprintf(combuf, "%s%s %s %s%s%s %s > %s",
1630 >                                                c_rpict, rep, rppopt, res,
1631 >                                                opts, po, oct1name, rawfile);
1632 >                                fp = popen(combuf, "w");
1633 >                                if (fp == NULL)
1634 >                                        goto rperror;
1635 >                                myprintview(vw, fp);
1636 >                                if (pclose(fp))
1637 >                                        goto rperror;
1638 >                        } else if (runcom(combuf))
1639 >                                goto rperror;
1640                  }
1641 <                if (runcom(combuf)) {           /* run rpict */
1642 <                        fprintf(stderr, "%s: error rendering view %s\n",
1643 <                                        progname, vs);
1644 <                        quit(1);
1641 >                if (do_rpiece) {                /* need to finish raw, first */
1642 >                        finish_process();
1643 >                        wait_process(1);
1644 >                        if (!syncf_done(sfile)) {
1645 >                                fprintf(stderr,
1646 >                        "%s: %s did not complete rendering of view %s\n",
1647 >                                                progname, c_rpiece, vs);
1648 >                                quit(1);
1649 >                        }
1650                  }
1651                  if (!vdef(RAWFILE) || strcmp(vval(RAWFILE),vval(PICTURE))) {
1652                                                  /* build pfilt command */
1653 <                        if (mult > 1)
1654 <                                sprintf(combuf, "pfilt%s -x /%d -y /%d %s > %s",
1655 <                                        pfopts, mult, mult, rawfile, picfile);
1653 >                        if (do_rpiece)
1654 >                                sprintf(combuf,
1655 >                                        "%s%s -x %d -y %d -p %.3f %s > %s",
1656 >                                        c_pfilt, pfopts, xres, yres, aspect,
1657 >                                        rawfile, picfile);
1658 >                        else if (mult > 1)
1659 >                                sprintf(combuf, "%s%s -x /%d -y /%d %s > %s",
1660 >                                        c_pfilt, pfopts, mult, mult,
1661 >                                        rawfile, picfile);
1662                          else
1663 <                                sprintf(combuf, "pfilt%s %s > %s", pfopts,
1664 <                                                rawfile, picfile);
1665 <                        if (runcom(combuf)) {           /* run pfilt */
1663 >                                sprintf(combuf, "%s%s %s > %s", c_pfilt,
1664 >                                        pfopts, rawfile, picfile);
1665 >                        if (runcom(combuf)) {   /* run pfilt */
1666                                  fprintf(stderr,
1667                                  "%s: error filtering view %s\n\t%s removed\n",
1668                                                  progname, vs, picfile);
# Line 1223 | Line 1672 | char   *opts, *po;
1672                  }
1673                                                  /* remove/rename raw file */
1674                  if (vdef(RAWFILE)) {
1675 <                        sprintf(combuf, "%s_%s.pic", vval(RAWFILE), vs);
1675 >                        sprintf(combuf, "%s_%s.hdr", vval(RAWFILE), vs);
1676                          mvfile(rawfile, combuf);
1677                  } else
1678                          rmfile(rawfile);
1679 +                if (do_rpiece)                  /* done with sync file */
1680 +                        rmfile(sfile);
1681 +                else
1682 +                        finish_process();       /* exit if child */
1683          }
1684 +        wait_process(1);                /* wait for children to finish */
1685 +        if (pfile != NULL) {            /* clean up persistent rpict */
1686 +                RT_PID  pid;
1687 +                fp = fopen(pfile, "r");
1688 +                if (fp != NULL) {
1689 +                        if (fscanf(fp, "%*s %d", &pid) != 1 ||
1690 +                                        kill(pid, 1) < 0)
1691 +                                unlink(pfile);
1692 +                        fclose(fp);
1693 +                }
1694 +        }
1695 +        return;
1696 + rperror:
1697 +        fprintf(stderr, "%s: error rendering view %s\n", progname, vs);
1698 +        quit(1);
1699 + #undef do_rpiece
1700   }
1701  
1702  
1703 < touch(fn)                       /* update a file */
1704 < char    *fn;
1703 > static int
1704 > touch(                  /* update a file */
1705 >        char    *fn
1706 > )
1707   {
1708          if (!silent)
1709                  printf("\ttouch %s\n", fn);
1710 <        if (noaction)
1710 >        if (!nprocs)
1711                  return(0);
1241 #ifdef notused
1242        if (access(fn, F_OK) == -1)             /* create it */
1243                if (close(open(fn, O_WRONLY|O_CREAT, 0666)) == -1)
1244                        return(-1);
1245 #endif
1712          return(setfdate(fn, time((time_t *)NULL)));
1713   }
1714  
1715  
1716 < runcom(cs)                      /* run command */
1717 < char    *cs;
1716 > static int
1717 > runcom(                 /* run command */
1718 >        char    *cs
1719 > )
1720   {
1721          if (!silent)            /* echo it */
1722                  printf("\t%s\n", cs);
1723 <        if (noaction)
1723 >        if (!nprocs)
1724                  return(0);
1725 <        fflush(stdout);         /* flush output and pass to shell */
1725 >        fflush(NULL);           /* flush output and pass to shell */
1726          return(system(cs));
1727   }
1728  
1729  
1730 < rmfile(fn)                      /* remove a file */
1731 < char    *fn;
1730 > static int
1731 > rmfile(                 /* remove a file */
1732 >        char    *fn
1733 > )
1734   {
1735          if (!silent)
1736 < #ifdef MSDOS
1737 <                printf("\tdel %s\n", fn);
1268 < #else
1269 <                printf("\trm -f %s\n", fn);
1270 < #endif
1271 <        if (noaction)
1736 >                printf("\t%s %s\n", DELCMD, fn);
1737 >        if (!nprocs)
1738                  return(0);
1739          return(unlink(fn));
1740   }
1741  
1742  
1743 < mvfile(fold, fnew)              /* move a file */
1744 < char    *fold, *fnew;
1743 > static int
1744 > mvfile(         /* move a file */
1745 >        char    *fold,
1746 >        char    *fnew
1747 > )
1748   {
1749          if (!silent)
1750 < #ifdef MSDOS
1751 <                printf("\trename %s %s\n", fold, fnew);
1283 < #else
1284 <                printf("\tmv %s %s\n", fold, fnew);
1285 < #endif
1286 <        if (noaction)
1750 >                printf("\t%s %s %s\n", RENAMECMD, fold, fnew);
1751 >        if (!nprocs)
1752                  return(0);
1753          return(rename(fold, fnew));
1754   }
1755  
1756  
1757 < #ifdef MSDOS
1758 < setenv(vname, value)            /* set an environment variable */
1759 < char    *vname, *value;
1757 > #ifdef RHAS_FORK_EXEC
1758 > static int
1759 > next_process(int reserve)               /* fork the next process */
1760   {
1761 <        register char   *evp;
1761 >        RT_PID  child_pid;
1762  
1763 <        evp = bmalloc(strlen(vname)+strlen(value)+2);
1764 <        if (evp == NULL)
1765 <                syserr(progname);
1766 <        sprintf(evp, "%s=%s", vname, value);
1767 <        if (putenv(evp) != 0) {
1303 <                fprintf(stderr, "%s: out of environment space\n", progname);
1763 >        if (nprocs <= 1)
1764 >                return(0);              /* it's us or no one */
1765 >        if (inchild()) {
1766 >                fprintf(stderr, "%s: internal error 1 in next_process()\n",
1767 >                                progname);
1768                  quit(1);
1769          }
1770 <        if (!silent)
1771 <                printf("set %s\n", evp);
1770 >        if (reserve > 0 && children_running >= nprocs-reserve)
1771 >                return(0);              /* caller holding back process(es) */
1772 >        if (children_running >= nprocs)
1773 >                wait_process(0);        /* wait for someone to finish */
1774 >        fflush(NULL);                   /* flush output */
1775 >        child_pid = fork();             /* split process */
1776 >        if (child_pid == 0) {           /* we're the child */
1777 >                children_running = -1;
1778 >                nprocs = 1;
1779 >                return(0);
1780 >        }
1781 >        if (child_pid > 0) {            /* we're the parent */
1782 >                ++children_running;
1783 >                return(1);
1784 >        }
1785 >        fprintf(stderr, "%s: warning -- fork() failed\n", progname);
1786 >        return(0);
1787   }
1309 #endif
1788  
1789 + static void
1790 + wait_process(                   /* wait for process(es) to finish */
1791 +        int     all
1792 + )
1793 + {
1794 +        int     ourstatus = 0, status;
1795 +        RT_PID  pid;
1796  
1797 < badvalue(vc)                    /* report bad variable value and exit */
1798 < int     vc;
1797 >        if (all)
1798 >                all = children_running;
1799 >        else if (children_running > 0)
1800 >                all = 1;
1801 >        while (all-- > 0) {
1802 >                pid = wait(&status);
1803 >                if (pid < 0)
1804 >                        syserr(progname);
1805 >                status = status>>8 & 0xff;
1806 >                --children_running;
1807 >                if (status != 0) {      /* child's problem is our problem */
1808 >                        if ((ourstatus == 0) & (children_running > 0))
1809 >                                fprintf(stderr, "%s: waiting for remaining processes\n",
1810 >                                                progname);
1811 >                        ourstatus = status;
1812 >                        all = children_running;
1813 >                }
1814 >        }
1815 >        if (ourstatus != 0)
1816 >                quit(ourstatus);        /* bad status from child */
1817 > }
1818 > #else   /* ! RHAS_FORK_EXEC */
1819 > static int
1820 > next_process(int reserve)
1821   {
1822 +        return(0);                      /* cannot start new process */
1823 + }
1824 + static void
1825 + wait_process(all)
1826 + int     all;
1827 + {
1828 +        (void)all;                      /* no one to wait for */
1829 + }
1830 + int
1831 + kill(pid, sig) /* win|unix_process.c should also wait and kill */
1832 + RT_PID pid;
1833 + int sig;
1834 + {
1835 +        return 0;
1836 + }
1837 + #endif  /* ! RHAS_FORK_EXEC */
1838 +
1839 + static void
1840 + finish_process(void)                    /* exit a child process */
1841 + {
1842 +        if (!inchild())
1843 +                return;                 /* in parent -- noop */
1844 +        exit(0);
1845 + }
1846 +
1847 +
1848 + static void
1849 + badvalue(                       /* report bad variable value and exit */
1850 +        int     vc
1851 + )
1852 + {
1853          fprintf(stderr, "%s: bad value for variable '%s'\n",
1854                          progname, vnam(vc));
1855          quit(1);
1856   }
1857  
1858  
1859 < syserr(s)                       /* report a system error and exit */
1860 < char    *s;
1859 > static void
1860 > syserr(                 /* report a system error and exit */
1861 >        char    *s
1862 > )
1863   {
1864          perror(s);
1865          quit(1);
1866   }
1867  
1868  
1869 + void
1870   quit(ec)                        /* exit program */
1871   int     ec;
1872   {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines