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.59 by gwlarson, Tue Jan 19 17:46:13 1999 UTC vs.
Revision 2.114 by greg, Wed May 27 13:29:57 2015 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1999 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
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"
15 #include <ctype.h>
16 #include <sys/types.h>
18  
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 */
# Line 24 | Line 36 | static char SCCSid[] = "$SunId$ SGI";
36   #define INDIRECT        5               /* indirection in lighting */
37   #define MATERIAL        6               /* material files */
38   #define MKILLUM         7               /* mkillum options */
39 < #define OBJECT          8               /* object files */
40 < #define OCONV           9               /* oconv options */
41 < #define OCTREE          10              /* octree file name */
42 < #define OPTFILE         11              /* rendering options file */
43 < #define PENUMBRAS       12              /* shadow penumbras are desired */
44 < #define PFILT           13              /* pfilt options */
45 < #define PICTURE         14              /* picture file root name */
46 < #define QUALITY         15              /* desired rendering quality */
47 < #define RAWFILE         16              /* raw picture file root name */
48 < #define RENDER          17              /* rendering options */
49 < #define REPORT          18              /* report frequency and errfile */
50 < #define RESOLUTION      19              /* maximum picture resolution */
51 < #define SCENE           20              /* scene files */
52 < #define UP              21              /* view up (X, Y or Z) */
53 < #define VARIABILITY     22              /* level of light variability */
54 < #define VIEWS           23              /* view(s) for picture(s) */
55 < #define ZFILE           24              /* distance file root name */
56 < #define ZONE            25              /* simulation zone */
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 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 = 26;
63 > int NVARS = 31;
64  
65   VARIABLE        vv[] = {                /* variable-value pairs */
66          {"AMBFILE",     3,      0,      NULL,   onevalue},
# Line 54 | Line 71 | VARIABLE       vv[] = {                /* variable-value pairs */
71          {"INDIRECT",    3,      0,      NULL,   intvalue},
72          {"materials",   3,      0,      NULL,   catvalues},
73          {"mkillum",     3,      0,      NULL,   catvalues},
74 +        {"mkpmap",      3,      0,      NULL,   catvalues},
75          {"objects",     3,      0,      NULL,   catvalues},
76          {"oconv",       3,      0,      NULL,   catvalues},
77          {"OCTREE",      3,      0,      NULL,   onevalue},
78          {"OPTFILE",     3,      0,      NULL,   onevalue},
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},
93          {"VARIABILITY", 3,      0,      NULL,   qualvalue},
# Line 75 | Line 97 | VARIABLE       vv[] = {                /* variable-value pairs */
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  
84 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 93 | 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 126 | 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 174 | 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 [-w][-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 207 | 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;
216        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 232 | 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;
242 <        register int    n;
332 >        char    *cp;
333 >        int     n;
334          int     suffix;
335  
336          n = 0; cp = orig; suffix = -1;          /* suffix position, length */
# Line 258 | 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 267 | 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 285 | 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   {
295        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 335 | 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], lim[3], size;
466          char    buf[128];
# Line 396 | 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 407 | 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 428 | 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);
445 <                        else
446 <                                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 464 | 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 498 | Line 623 | oconv()                                /* run oconv and mkillum if necessary */
623   }
624  
625  
626 < char *
627 < addarg(op, arg)                         /* add argument and advance pointer */
503 < 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 +                        cp = addarg(cp, "-apg");
661 +                        addarg(cp, vval(PGMAP));
662 +                        cp = sskip(sskip(cp));  /* remove any bandwidth */
663 +                        *cp = '\0';
664 +                }
665 +                if (pcmapname != NULL && pcmapdate < oct1date) {
666 +                        cp = addarg(cp, "-apc");
667 +                        addarg(cp, vval(PCMAP));
668 +                        cp = sskip(sskip(cp));  /* remove any bandwidth */
669 +                        *cp = '\0';
670 +                }
671 +                cp = addarg(cp, oct1name);
672 +                if (runcom(combuf)) {
673 +                        fprintf(stderr, "%s: error running %s\n",
674 +                                        progname, c_mkpmap);
675 +                        if (pgmapname != NULL)
676 +                                unlink(pgmapname);
677 +                        if (pcmapname != NULL)
678 +                                unlink(pcmapname);
679 +                        quit(1);
680 +                }
681 +        }
682 +        tnow = time((time_t *)NULL);
683 +        if (pgmapname != NULL)
684 +                pgmapdate = tnow;
685 +        if (pcmapname != NULL)
686 +                pcmapdate = tnow;
687 +        oct1date = tnow;        /* trigger ambient file removal if needed */
688 + }
689 +
690 +
691 + static char *
692 + addarg(                         /* append argument and advance pointer */
693 + char    *op,
694 + char    *arg
695 + )
696 + {
697 +        while (*op)
698 +                op++;
699          *op = ' ';
700 <        while (*++op = *arg++)
700 >        while ( (*++op = *arg++) )
701                  ;
702          return(op);
703   }
704  
705  
706 < oconvopts(oo)                           /* get oconv options */
707 < register char   *oo;
706 > static void
707 > oconvopts(                              /* get oconv options */
708 >        char    *oo
709 > )
710   {
711          /* BEWARE:  This may be called via setdefaults(), so no assumptions */
712  
713          *oo = '\0';
714 <        if (vdef(OCONV))
715 <                addarg(oo, vval(OCONV));
714 >        if (!vdef(OCONV))
715 >                return;
716 >        if (vval(OCONV)[0] != '-') {
717 >                atos(c_oconv, sizeof(c_oconv), vval(OCONV));
718 >                oo = addarg(oo, sskip2(vval(OCONV), 1));
719 >        } else
720 >                oo = addarg(oo, vval(OCONV));
721   }
722  
723  
724 < mkillumopts(mo)                         /* get mkillum options */
725 < register char   *mo;
724 > static void
725 > mkillumopts(                            /* get mkillum options */
726 >        char    *mo
727 > )
728   {
729          /* BEWARE:  This may be called via setdefaults(), so no assumptions */
730  
731 +        if (nprocs > 1)
732 +                sprintf(mo, " -n %d", nprocs);
733 +        else
734 +                *mo = '\0';
735 +        if (!vdef(MKILLUM))
736 +                return;
737 +        if (vval(MKILLUM)[0] != '-') {
738 +                atos(c_mkillum, sizeof(c_mkillum), vval(MKILLUM));
739 +                mo = addarg(mo, sskip2(vval(MKILLUM), 1));
740 +        } else
741 +                mo = addarg(mo, vval(MKILLUM));
742 + }
743 +
744 +
745 + static void
746 + mkpmapopts(                             /* get mkpmap options */
747 +        char    *mo
748 + )
749 + {
750 +        /* BEWARE:  This may be called via setdefaults(), so no assumptions */
751 +
752          *mo = '\0';
753 <        if (vdef(MKILLUM))
754 <                addarg(mo, vval(MKILLUM));
753 >        if (!vdef(MKPMAP))
754 >                return;
755 >        if (vval(MKPMAP)[0] != '-') {
756 >                atos(c_mkpmap, sizeof(c_mkpmap), vval(MKPMAP));
757 >                mo = addarg(mo, sskip2(vval(MKPMAP), 1));
758 >        } else
759 >                mo = addarg(mo, vval(MKPMAP));
760   }
761  
762  
763 < checkambfile()                  /* check date on ambient file */
763 > static void
764 > checkambfile(void)                      /* check date on ambient file */
765   {
766          time_t  afdate;
767  
# Line 539 | Line 769 | checkambfile()                 /* check date on ambient file */
769                  return;
770          if (!(afdate = fdate(vval(AMBFILE))))
771                  return;
772 <        if (oct1date > afdate)
772 >        if (oct1date > afdate) {
773                  if (touchonly)
774                          touch(vval(AMBFILE));
775                  else
776                          rmfile(vval(AMBFILE));
777 +        }
778   }
779  
780  
781 < double
782 < ambval()                                /* compute ambient value */
781 > static double
782 > ambval(void)                            /* compute ambient value */
783   {
784          if (vdef(EXPOSURE)) {
785                  if (vval(EXPOSURE)[0] == '+' || vval(EXPOSURE)[0] == '-')
# Line 560 | Line 791 | ambval()                               /* compute ambient value */
791          if (vlet(ZONE) == 'I')
792                  return(.01);
793          badvalue(ZONE);
794 +        return 0; /* pro forma return */
795   }
796  
797  
798 < renderopts(op, po)                      /* set rendering options */
799 < char    *op, *po;
798 > static void
799 > renderopts(                     /* set rendering options */
800 >        char    *op,
801 >        char    *po
802 > )
803   {
804 +        char    pmapf[256], *bw;
805 +
806          switch(vscale(QUALITY)) {
807          case LOW:
808                  lowqopts(op, po);
# Line 577 | Line 814 | char   *op, *po;
814                  hiqopts(op, po);
815                  break;
816          }
817 +        if (vdef(PGMAP)) {
818 +                bw = sskip2(vval(PGMAP), 2);
819 +                atos(pmapf, sizeof(pmapf), vval(PGMAP));
820 +                op = addarg(addarg(op, "-ap"), pmapf);
821 +                if (atoi(bw) > 0) op = addarg(op, bw);
822 +        }
823 +        if (vdef(PCMAP)) {
824 +                bw = sskip2(vval(PCMAP), 2);
825 +                atos(pmapf, sizeof(pmapf), vval(PCMAP));
826 +                op = addarg(addarg(op, "-ap"), pmapf);
827 +                if (atoi(bw) > 0) op = addarg(op, bw);
828 +        }
829 +        if (vdef(RENDER))
830 +                op = addarg(op, vval(RENDER));
831 +        if (rvdevice != NULL) {
832 +                if (vdef(RVU))
833 +                        if (vval(RVU)[0] != '-') {
834 +                                atos(c_rvu, sizeof(c_rvu), vval(RVU));
835 +                                po = addarg(po, sskip2(vval(RVU), 1));
836 +                        } else
837 +                                po = addarg(po, vval(RVU));
838 +        } else {
839 +                if (vdef(RPICT))
840 +                        if (vval(RPICT)[0] != '-') {
841 +                                atos(c_rpict, sizeof(c_rpict), vval(RPICT));
842 +                                po = addarg(po, sskip2(vval(RPICT), 1));
843 +                        } else
844 +                                po = addarg(po, vval(RPICT));
845 +        }
846   }
847  
848  
849 < lowqopts(op, po)                        /* low quality rendering options */
850 < register char   *op;
851 < char    *po;
849 > static void
850 > lowqopts(                       /* low quality rendering options */
851 >        char    *op,
852 >        char    *po
853 > )
854   {
855          double  d, org[3], siz[3];
856  
# Line 592 | Line 860 | char   *po;
860                          &siz[0], &org[1], &siz[1], &org[2], &siz[2]) != 6)
861                  badvalue(ZONE);
862          siz[0] -= org[0]; siz[1] -= org[1]; siz[2] -= org[2];
863 <        if (siz[0] <= FTINY | siz[1] <= FTINY | siz[2] <= FTINY)
863 >        if ((siz[0] <= FTINY) | (siz[1] <= FTINY) | (siz[2] <= FTINY))
864                  badvalue(ZONE);
865          getoctcube(org, &d);
866          d *= 3./(siz[0]+siz[1]+siz[2]);
# Line 600 | Line 868 | char   *po;
868          case LOW:
869                  po = addarg(po, "-ps 16");
870                  op = addarg(op, "-dp 64");
871 <                sprintf(op, " -ar %d", (int)(4*d));
871 >                sprintf(op, " -ar %d", (int)(8*d));
872                  op += strlen(op);
873                  break;
874          case MEDIUM:
875                  po = addarg(po, "-ps 8");
876                  op = addarg(op, "-dp 128");
877 <                sprintf(op, " -ar %d", (int)(8*d));
877 >                sprintf(op, " -ar %d", (int)(16*d));
878                  op += strlen(op);
879                  break;
880          case HIGH:
881                  po = addarg(po, "-ps 4");
882                  op = addarg(op, "-dp 256");
883 <                sprintf(op, " -ar %d", (int)(16*d));
883 >                sprintf(op, " -ar %d", (int)(32*d));
884                  op += strlen(op);
885                  break;
886          }
# Line 621 | Line 889 | char   *po;
889                  op = addarg(op, "-ds .4");
890          else
891                  op = addarg(op, "-ds 0");
892 <        op = addarg(op, "-dt .2 -dc .25 -dr 0 -sj 0 -st .5");
892 >        op = addarg(op, "-dt .2 -dc .25 -dr 0 -ss 0 -st .5");
893          if (vdef(AMBFILE)) {
894                  sprintf(op, " -af %s", vval(AMBFILE));
895                  op += strlen(op);
# Line 629 | Line 897 | char   *po;
897                  overture = 0;
898          switch (vscale(VARIABILITY)) {
899          case LOW:
900 <                op = addarg(op, "-aa .4 -ad 64");
900 >                op = addarg(op, "-aa .3 -ad 256");
901                  break;
902          case MEDIUM:
903 <                op = addarg(op, "-aa .3 -ad 128");
903 >                op = addarg(op, "-aa .25 -ad 512");
904                  break;
905          case HIGH:
906 <                op = addarg(op, "-aa .25 -ad 256");
906 >                op = addarg(op, "-aa .2 -ad 1024");
907                  break;
908          }
909          op = addarg(op, "-as 0");
910          d = ambval();
911          sprintf(op, " -av %.2g %.2g %.2g", d, d, d);
912          op += strlen(op);
913 <        op = addarg(op, "-lr 3 -lw .02");
646 <        if (vdef(RENDER))
647 <                op = addarg(op, vval(RENDER));
913 >        op = addarg(op, "-lr 6 -lw .003");
914   }
915  
916  
917 < medqopts(op, po)                        /* medium quality rendering options */
918 < register char   *op;
919 < char    *po;
917 > static void
918 > medqopts(                       /* medium quality rendering options */
919 >        char    *op,
920 >        char    *po
921 > )
922   {
923          double  d, org[3], siz[3], asz;
924  
# Line 660 | Line 928 | char   *po;
928                          &siz[0], &org[1], &siz[1], &org[2], &siz[2]) != 6)
929                  badvalue(ZONE);
930          siz[0] -= org[0]; siz[1] -= org[1]; siz[2] -= org[2];
931 <        if (siz[0] <= FTINY | siz[1] <= FTINY | siz[2] <= FTINY)
931 >        if ((siz[0] <= FTINY) | (siz[1] <= FTINY) | (siz[2] <= FTINY))
932                  badvalue(ZONE);
933          getoctcube(org, &d);
934          asz = (siz[0]+siz[1]+siz[2])/3.;
# Line 669 | Line 937 | char   *po;
937          case LOW:
938                  po = addarg(po, vbool(PENUMBRAS) ? "-ps 4" : "-ps 8");
939                  op = addarg(op, "-dp 256");
940 <                sprintf(op, " -ar %d", (int)(8*d));
940 >                sprintf(op, " -ar %d", (int)(16*d));
941                  op += strlen(op);
942                  sprintf(op, " -ms %.2g", asz/20.);
943                  op += strlen(op);
# Line 677 | Line 945 | char   *po;
945          case MEDIUM:
946                  po = addarg(po, vbool(PENUMBRAS) ? "-ps 3" : "-ps 6");
947                  op = addarg(op, "-dp 512");
948 <                sprintf(op, " -ar %d", (int)(16*d));
948 >                sprintf(op, " -ar %d", (int)(32*d));
949                  op += strlen(op);
950                  sprintf(op, " -ms %.2g", asz/40.);
951                  op += strlen(op);
# Line 685 | Line 953 | char   *po;
953          case HIGH:
954                  po = addarg(po, vbool(PENUMBRAS) ? "-ps 2" : "-ps 4");
955                  op = addarg(op, "-dp 1024");
956 <                sprintf(op, " -ar %d", (int)(32*d));
956 >                sprintf(op, " -ar %d", (int)(64*d));
957                  op += strlen(op);
958                  sprintf(op, " -ms %.2g", asz/80.);
959                  op += strlen(op);
# Line 693 | Line 961 | char   *po;
961          }
962          po = addarg(po, "-pt .08");
963          if (vbool(PENUMBRAS))
964 <                op = addarg(op, "-ds .2 -dj .5");
964 >                op = addarg(op, "-ds .2 -dj .9");
965          else
966                  op = addarg(op, "-ds .3");
967 <        op = addarg(op, "-dt .1 -dc .5 -dr 1 -sj .7 -st .1");
968 <        if (overture = vint(INDIRECT)) {
967 >        op = addarg(op, "-dt .1 -dc .5 -dr 1 -ss 1 -st .1");
968 >        if ( (overture = vint(INDIRECT)) ) {
969                  sprintf(op, " -ab %d", overture);
970                  op += strlen(op);
971          }
# Line 708 | Line 976 | char   *po;
976                  overture = 0;
977          switch (vscale(VARIABILITY)) {
978          case LOW:
979 <                op = addarg(op, "-aa .25 -ad 196 -as 0");
979 >                op = addarg(op, "-aa .2 -ad 329 -as 42");
980                  break;
981          case MEDIUM:
982 <                op = addarg(op, "-aa .2 -ad 400 -as 64");
982 >                op = addarg(op, "-aa .15 -ad 800 -as 128");
983                  break;
984          case HIGH:
985 <                op = addarg(op, "-aa .15 -ad 768 -as 196");
985 >                op = addarg(op, "-aa .1 -ad 1536 -as 392");
986                  break;
987          }
988          d = ambval();
989          sprintf(op, " -av %.2g %.2g %.2g", d, d, d);
990          op += strlen(op);
991 <        op = addarg(op, "-lr 6 -lw .002");
724 <        if (vdef(RENDER))
725 <                op = addarg(op, vval(RENDER));
991 >        op = addarg(op, "-lr 8 -lw 1e-4");
992   }
993  
994  
995 < hiqopts(op, po)                         /* high quality rendering options */
996 < register char   *op;
997 < char    *po;
995 > static void
996 > hiqopts(                                /* high quality rendering options */
997 >        char    *op,
998 >        char    *po
999 > )
1000   {
1001          double  d, org[3], siz[3], asz;
1002  
# Line 738 | Line 1006 | char   *po;
1006                          &siz[0], &org[1], &siz[1], &org[2], &siz[2]) != 6)
1007                  badvalue(ZONE);
1008          siz[0] -= org[0]; siz[1] -= org[1]; siz[2] -= org[2];
1009 <        if (siz[0] <= FTINY | siz[1] <= FTINY | siz[2] <= FTINY)
1009 >        if ((siz[0] <= FTINY) | (siz[1] <= FTINY) | (siz[2] <= FTINY))
1010                  badvalue(ZONE);
1011          getoctcube(org, &d);
1012          asz = (siz[0]+siz[1]+siz[2])/3.;
# Line 747 | Line 1015 | char   *po;
1015          case LOW:
1016                  po = addarg(po, vbool(PENUMBRAS) ? "-ps 1" : "-ps 8");
1017                  op = addarg(op, "-dp 1024");
1018 <                sprintf(op, " -ar %d", (int)(16*d));
1018 >                sprintf(op, " -ar %d", (int)(32*d));
1019                  op += strlen(op);
1020                  sprintf(op, " -ms %.2g", asz/40.);
1021                  op += strlen(op);
# Line 755 | Line 1023 | char   *po;
1023          case MEDIUM:
1024                  po = addarg(po, vbool(PENUMBRAS) ? "-ps 1" : "-ps 5");
1025                  op = addarg(op, "-dp 2048");
1026 <                sprintf(op, " -ar %d", (int)(32*d));
1026 >                sprintf(op, " -ar %d", (int)(64*d));
1027                  op += strlen(op);
1028                  sprintf(op, " -ms %.2g", asz/80.);
1029                  op += strlen(op);
# Line 763 | Line 1031 | char   *po;
1031          case HIGH:
1032                  po = addarg(po, vbool(PENUMBRAS) ? "-ps 1" : "-ps 3");
1033                  op = addarg(op, "-dp 4096");
1034 <                sprintf(op, " -ar %d", (int)(64*d));
1034 >                sprintf(op, " -ar %d", (int)(128*d));
1035                  op += strlen(op);
1036                  sprintf(op, " -ms %.2g", asz/160.);
1037                  op += strlen(op);
# Line 771 | Line 1039 | char   *po;
1039          }
1040          po = addarg(po, "-pt .04");
1041          if (vbool(PENUMBRAS))
1042 <                op = addarg(op, "-ds .1 -dj .65");
1042 >                op = addarg(op, "-ds .1 -dj .9");
1043          else
1044                  op = addarg(op, "-ds .2");
1045 <        op = addarg(op, "-dt .05 -dc .75 -dr 3 -sj 1 -st .01");
1045 >        op = addarg(op, "-dt .05 -dc .75 -dr 3 -ss 16 -st .01");
1046          sprintf(op, " -ab %d", overture=vint(INDIRECT)+1);
1047          op += strlen(op);
1048          if (vdef(AMBFILE)) {
# Line 784 | Line 1052 | char   *po;
1052                  overture = 0;
1053          switch (vscale(VARIABILITY)) {
1054          case LOW:
1055 <                op = addarg(op, "-aa .15 -ad 256 -as 0");
1055 >                op = addarg(op, "-aa .125 -ad 512 -as 64");
1056                  break;
1057          case MEDIUM:
1058 <                op = addarg(op, "-aa .125 -ad 512 -as 256");
1058 >                op = addarg(op, "-aa .1 -ad 1536 -as 768");
1059                  break;
1060          case HIGH:
1061 <                op = addarg(op, "-aa .08 -ad 1024 -as 512");
1061 >                op = addarg(op, "-aa .075 -ad 4096 -as 2048");
1062                  break;
1063          }
1064          d = ambval();
1065          sprintf(op, " -av %.2g %.2g %.2g", d, d, d);
1066          op += strlen(op);
1067 <        op = addarg(op, "-lr 12 -lw .0005");
800 <        if (vdef(RENDER))
801 <                op = addarg(op, vval(RENDER));
1067 >        op = addarg(op, "-lr 12 -lw 1e-5");
1068   }
1069  
1070  
1071 < xferopts(ro)                            /* transfer options if indicated */
1072 < char    *ro;
1071 > #ifdef _WIN32
1072 > static void
1073 > setenv(                 /* set an environment variable */
1074 >        char    *vname,
1075 >        char    *value
1076 > )
1077   {
1078 +        char    *evp;
1079 +
1080 +        evp = bmalloc(strlen(vname)+strlen(value)+2);
1081 +        if (evp == NULL)
1082 +                syserr(progname);
1083 +        sprintf(evp, "%s=%s", vname, value);
1084 +        if (putenv(evp) != 0) {
1085 +                fprintf(stderr, "%s: out of environment space\n", progname);
1086 +                quit(1);
1087 +        }
1088 +        if (!silent)
1089 +                printf("set %s\n", evp);
1090 + }
1091 + #endif
1092 +
1093 +
1094 + static void
1095 + xferopts(                               /* transfer options if indicated */
1096 +        char    *ro
1097 + )
1098 + {
1099          int     fd, n;
1100 <        register char   *cp;
1100 >        char    *cp;
1101          
1102          n = strlen(ro);
1103          if (n < 2)
# Line 824 | Line 1115 | char   *ro;
1115                          syserr(vval(OPTFILE));
1116                  sprintf(ro, " @%s", vval(OPTFILE));
1117          }
1118 < #ifdef MSDOS
1118 > #ifdef _WIN32
1119          else if (n > 50) {
1120                  setenv("ROPT", ro+1);
1121                  strcpy(ro, " $ROPT");
# Line 833 | Line 1124 | char   *ro;
1124   }
1125  
1126  
1127 < pfiltopts(po)                           /* get pfilt options */
1128 < register char   *po;
1127 > static void
1128 > pfiltopts(                              /* get pfilt options */
1129 >        char    *po
1130 > )
1131   {
1132          *po = '\0';
1133          if (vdef(EXPOSURE)) {
# Line 843 | Line 1136 | register char  *po;
1136          }
1137          switch (vscale(QUALITY)) {
1138          case MEDIUM:
1139 <                po = addarg(po, "-r 1");
1139 >                po = addarg(po, "-r .6");
1140                  break;
1141          case HIGH:
1142                  po = addarg(po, "-m .25");
1143                  break;
1144          }
1145          if (vdef(PFILT))
1146 <                po = addarg(po, vval(PFILT));
1146 >                if (vval(PFILT)[0] != '-') {
1147 >                        atos(c_pfilt, sizeof(c_pfilt), vval(PFILT));
1148 >                        po = addarg(po, sskip2(vval(PFILT), 1));
1149 >                } else
1150 >                        po = addarg(po, vval(PFILT));
1151   }
1152  
1153  
1154 < matchword(s1, s2)                       /* match white-delimited words */
1155 < register char   *s1, *s2;
1154 > static int
1155 > matchword(                      /* match white-delimited words */
1156 >        char    *s1,
1157 >        char    *s2
1158 > )
1159   {
1160          while (isspace(*s1)) s1++;
1161          while (isspace(*s2)) s2++;
# Line 866 | Line 1166 | register char  *s1, *s2;
1166   }
1167  
1168  
1169 < char *
1170 < specview(vs)                            /* get proper view spec from vs */
1171 < register char   *vs;
1169 > static char *
1170 > specview(                               /* get proper view spec from vs */
1171 >        char    *vs
1172 > )
1173   {
1174          static char     vup[7][12] = {"-vu 0 0 -1","-vu 0 -1 0","-vu -1 0 0",
1175                          "-vu 0 0 1", "-vu 1 0 0","-vu 0 1 0","-vu 0 0 1"};
1176          static char     viewopts[128];
1177 <        register char   *cp;
1177 >        char    *cp;
1178          int     xpos, ypos, zpos, viewtype, upax;
1179 <        register int    i;
1179 >        int     i;
1180          double  cent[3], dim[3], mult, d;
1181  
1182          if (vs == NULL || *vs == '-')
# Line 886 | Line 1187 | register char  *vs;
1187                          upax = 1-'X'+UPPER(vval(UP)[1]);
1188                  else
1189                          upax = 1-'X'+vlet(UP);
1190 <                if (upax < 1 | upax > 3)
1190 >                if ((upax < 1) | (upax > 3))
1191                          badvalue(UP);
1192                  if (vval(UP)[0] == '-')
1193                          upax = -upax;
# Line 908 | Line 1209 | register char  *vs;
1209          } else if (*vs == 'z') {
1210                  zpos = -1; vs++;
1211          }
1212 <        viewtype = 'v';
1213 <        if (*vs == 'v' | *vs == 'l' | *vs == 'a' | *vs == 'h' | *vs == 'c')
1212 >        switch (*vs) {
1213 >        case VT_PER:
1214 >        case VT_PAR:
1215 >        case VT_ANG:
1216 >        case VT_HEM:
1217 >        case VT_PLS:
1218 >        case VT_CYL:
1219                  viewtype = *vs++;
1220 +                break;
1221 +        default:
1222 +                viewtype = VT_PER;
1223 +                break;
1224 +        }
1225          cp = viewopts;
1226          if ((!*vs || isspace(*vs)) && (xpos|ypos|zpos)) {       /* got one! */
1227                  *cp++ = '-'; *cp++ = 'v'; *cp++ = 't'; *cp++ = viewtype;
# Line 925 | Line 1236 | register char  *vs;
1236                          cent[i] += .5*dim[i];
1237                  }
1238                  mult = vlet(ZONE)=='E' ? 2. : .45 ;
1239 <                sprintf(cp, " -vp %.2g %.2g %.2g -vd %.2g %.2g %.2g",
1239 >                sprintf(cp, " -vp %.3g %.3g %.3g -vd %.3g %.3g %.3g",
1240                                  cent[0]+xpos*mult*dim[0],
1241                                  cent[1]+ypos*mult*dim[1],
1242                                  cent[2]+zpos*mult*dim[2],
# Line 952 | Line 1263 | register char  *vs;
1263                  }
1264                  cp = addarg(cp, vup[upax+3]);
1265                  switch (viewtype) {
1266 <                case 'v':
1266 >                case VT_PER:
1267                          cp = addarg(cp, "-vh 45 -vv 45");
1268                          break;
1269 <                case 'l':
1269 >                case VT_PAR:
1270                          d = sqrt(dim[0]*dim[0]+dim[1]*dim[1]+dim[2]*dim[2]);
1271 <                        sprintf(cp, " -vh %.2g -vv %.2g", d, d);
1271 >                        sprintf(cp, " -vh %.3g -vv %.3g", d, d);
1272                          cp += strlen(cp);
1273                          break;
1274 <                case 'a':
1275 <                case 'h':
1274 >                case VT_ANG:
1275 >                case VT_HEM:
1276 >                case VT_PLS:
1277                          cp = addarg(cp, "-vh 180 -vv 180");
1278                          break;
1279 <                case 'c':
1279 >                case VT_CYL:
1280                          cp = addarg(cp, "-vh 180 -vv 90");
1281                          break;
1282                  }
# Line 972 | Line 1284 | register char  *vs;
1284                  while (!isspace(*vs))           /* else skip id */
1285                          if (!*vs++)
1286                                  return(NULL);
1287 <                if (upax) {                     /* specify up vector */
1287 >                if (upax) {                     /* prepend up vector */
1288                          strcpy(cp, vup[upax+3]);
1289                          cp += strlen(cp);
1290                  }
# Line 980 | Line 1292 | register char  *vs;
1292          if (cp == viewopts)             /* append any additional options */
1293                  vs++;           /* skip prefixed space if unneeded */
1294          strcpy(cp, vs);
1295 < #ifdef MSDOS
1295 > #ifdef _WIN32
1296          if (strlen(viewopts) > 40) {
1297                  setenv("VIEW", viewopts);
1298                  return("$VIEW");
# Line 990 | Line 1302 | register char  *vs;
1302   }
1303  
1304  
1305 < char *
1306 < getview(n, vn)                          /* get view n, or NULL if none */
1307 < int     n;
1308 < char    *vn;            /* returned view name */
1305 > static char *
1306 > getview(                                /* get view n, or NULL if none */
1307 >        int     n,
1308 >        char    *vn             /* returned view name */
1309 > )
1310   {
1311 <        register char   *mv;
1311 >        char    *mv;
1312  
1313          if (viewselect != NULL) {               /* command-line selected */
1314                  if (n)                          /* only do one */
1315                          return(NULL);
1316 +                                        
1317 +                if (isint(viewselect)) {        /* view number? */
1318 +                        n = atoi(viewselect)-1;
1319 +                        goto numview;
1320 +                }
1321                  if (viewselect[0] == '-') {     /* already specified */
1322 <                        if (vn != NULL) *vn = '\0';
1322 >                        if (vn != NULL)
1323 >                                strcpy(vn, "0");
1324                          return(viewselect);
1325                  }
1326                  if (vn != NULL) {
# Line 1010 | Line 1329 | char   *vn;            /* returned view name */
1329                                  ;
1330                          *vn = '\0';
1331                  }
1013                                                /* view number? */
1014                if (isint(viewselect))
1015                        return(specview(nvalue(VIEWS, atoi(viewselect)-1)));
1332                                                  /* check list */
1333                  while ((mv = nvalue(VIEWS, n++)) != NULL)
1334                          if (matchword(viewselect, mv))
1335                                  return(specview(mv));
1336 +
1337                  return(specview(viewselect));   /* standard view? */
1338          }
1339 + numview:
1340          mv = nvalue(VIEWS, n);          /* use view n */
1341 <        if (vn != NULL & mv != NULL) {
1342 <                register char   *mv2 = mv;
1343 <                if (*mv2 != '-')
1341 >        if ((vn != NULL) & (mv != NULL))
1342 >                if (*mv != '-') {
1343 >                        char    *mv2 = mv;
1344                          while (*mv2 && !isspace(*mv2))
1345                                  *vn++ = *mv2++;
1346 <                *vn = '\0';
1347 <        }
1346 >                        *vn = '\0';
1347 >                } else
1348 >                        sprintf(vn, "%d", n+1);
1349 >
1350          return(specview(mv));
1351   }
1352  
1353  
1354 < printview(vopts)                        /* print out selected view */
1355 < register char   *vopts;
1354 > static int
1355 > myprintview(                    /* print out selected view */
1356 >        char    *vopts,
1357 >        FILE    *fp
1358 > )
1359   {
1037        extern char     *strstr(), *atos(), *getenv();
1360          VIEW    vwr;
1361          char    buf[128];
1362 <        register char   *cp;
1362 >        char    *cp;
1363 > #ifdef _WIN32
1364 > /* XXX Should we allow something like this for all platforms? */
1365 > /* XXX Or is it still required at all? */
1366   again:
1367 + #endif
1368          if (vopts == NULL)
1369                  return(-1);
1370 < #ifdef MSDOS
1370 > #ifdef _WIN32
1371          if (vopts[0] == '$') {
1372                  vopts = getenv(vopts+1);
1373                  goto again;
1374          }
1375   #endif
1376 <        copystruct(&vwr, &stdview);
1376 >        vwr = stdview;
1377          sscanview(&vwr, cp=vopts);              /* set initial options */
1378          while ((cp = strstr(cp, "-vf ")) != NULL &&
1379                          *atos(buf, sizeof(buf), cp += 4)) {
1380                  viewfile(buf, &vwr, NULL);      /* load -vf file */
1381                  sscanview(&vwr, cp);            /* reset tail */
1382          }
1383 <        fputs(VIEWSTR, stdout);
1384 <        fprintview(&vwr, stdout);               /* print full spec. */
1385 <        fputc('\n', stdout);
1383 >        fputs(VIEWSTR, fp);
1384 >        fprintview(&vwr, fp);                   /* print full spec. */
1385 >        fputc('\n', fp);
1386          return(0);
1387   }
1388  
1389  
1390 < rview(opts, po)                         /* run rview with first view */
1391 < char    *opts, *po;
1390 > static void
1391 > rvu(                            /* run rvu with first view */
1392 >        char    *opts,
1393 >        char    *po
1394 > )
1395   {
1396          char    *vw;
1397 <        char    combuf[512];
1397 >        char    combuf[PATH_MAX];
1398                                          /* build command */
1399          if (touchonly || (vw = getview(0, NULL)) == NULL)
1400                  return;
1401          if (sayview)
1402 <                printview(vw);
1403 <        sprintf(combuf, "rview %s%s%s -R %s ", vw, po, opts, rifname);
1402 >                myprintview(vw, stdout);
1403 >        sprintf(combuf, "%s %s%s%s -R %s ", c_rvu, vw, opts, po, rifname);
1404 >        if (nprocs > 1)
1405 >                sprintf(combuf+strlen(combuf), "-n %d ", nprocs);
1406          if (rvdevice != NULL)
1407                  sprintf(combuf+strlen(combuf), "-o %s ", rvdevice);
1408          if (vdef(EXPOSURE))
1409                  sprintf(combuf+strlen(combuf), "-pe %s ", vval(EXPOSURE));
1410          strcat(combuf, oct1name);
1411          if (runcom(combuf)) {           /* run it */
1412 <                fprintf(stderr, "%s: error running rview\n", progname);
1412 >                fprintf(stderr, "%s: error running %s\n", progname, c_rvu);
1413                  quit(1);
1414          }
1415   }
1416  
1417  
1418 < rpict(opts, po)                         /* run rpict and pfilt for each view */
1419 < char    *opts, *po;
1418 > static int
1419 > syncf_done(                     /* check if an rpiece sync file is complete */
1420 >        char *sfname
1421 > )
1422   {
1423 <        char    combuf[1024];
1424 <        char    rawfile[MAXPATH], picfile[MAXPATH];
1425 <        char    zopt[MAXPATH+4], rep[MAXPATH+16], res[32];
1423 >        FILE    *fp = fopen(sfname, "r");
1424 >        int     todo = 1;
1425 >        int     x, y;
1426 >
1427 >        if (fp == NULL)
1428 >                return(0);
1429 >        if (fscanf(fp, "%d %d", &x, &y) != 2)
1430 >                goto checked;
1431 >        todo = x*y;             /* total number of tiles */
1432 >        if (fscanf(fp, "%d %d", &x, &y) != 2 || (x != 0) | (y != 0))
1433 >                goto checked;
1434 >                                /* XXX assume no redundant tiles */
1435 >        while (fscanf(fp, "%d %d", &x, &y) == 2)
1436 >                if (!--todo)
1437 >                        break;
1438 > checked:
1439 >        fclose(fp);
1440 >        return(!todo);
1441 > }
1442 >
1443 >
1444 > static void
1445 > rpict(                          /* run rpict and pfilt for each view */
1446 >        char    *opts,
1447 >        char    *po
1448 > )
1449 > {
1450 > #define do_rpiece       (sfile[0]!='\0')
1451 >        char    combuf[5*PATH_MAX+512];
1452 >        char    rawfile[PATH_MAX], picfile[PATH_MAX];
1453 >        char    zopt[PATH_MAX+4], rep[PATH_MAX+16], res[32];
1454 >        char    rppopt[32], sfile[PATH_MAX], *pfile = NULL;
1455          char    pfopts[128];
1456          char    vs[32], *vw;
1457          int     vn, mult;
1458 +        FILE    *fp;
1459          time_t  rfdt, pfdt;
1460 +        int     xres, yres;
1461 +        double  aspect;
1462 +        int     n;
1463                                          /* get pfilt options */
1464          pfiltopts(pfopts);
1465                                          /* get resolution, reporting */
# Line 1108 | Line 1474 | char   *opts, *po;
1474                  mult = 3;
1475                  break;
1476          }
1477 <        {
1478 <                int     xres, yres;
1479 <                double  aspect;
1480 <                int     n;
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 <                        if (n == 1) yres = xres;
1121 <                        sprintf(res, "-x %d -y %d", mult*xres, mult*yres);
1122 <                } else
1123 <                        badvalue(RESOLUTION);
1124 <        }
1477 >        n = sscanf(vval(RESOLUTION), "%d %d %lf", &xres, &yres, &aspect);
1478 >        if (n == 3)
1479 >                sprintf(res, "-x %d -y %d -pa %.3f",
1480 >                                mult*xres, mult*yres, aspect);
1481 >        else if (n) {
1482 >                aspect = 1.;
1483 >                if (n == 1) yres = xres;
1484 >                sprintf(res, "-x %d -y %d", mult*xres, mult*yres);
1485 >        } else
1486 >                badvalue(RESOLUTION);
1487          rep[0] = '\0';
1488          if (vdef(REPORT)) {
1489                  double  minutes;
1128                int     n;
1490                  n = sscanf(vval(REPORT), "%lf %s", &minutes, rawfile);
1491                  if (n == 2)
1492                          sprintf(rep, " -t %d -e %s", (int)(minutes*60), rawfile);
# Line 1134 | Line 1495 | char   *opts, *po;
1495                  else
1496                          badvalue(REPORT);
1497          }
1498 <                                        /* do each view */
1499 <        vn = 0;
1498 >                                        /* set up parallel rendering */
1499 >        sfile[0] = '\0';
1500 >        if ((nprocs > 1) & !touchonly & !vdef(ZFILE) &&
1501 >                                        getview(0, vs) != NULL) {
1502 >                if (!strcmp(c_rpict, DEF_RPICT_PATH) &&
1503 >                                getview(1, NULL) == NULL) {
1504 >                        sprintf(sfile, "%s_%s_rpsync.txt",
1505 >                                vdef(RAWFILE) ? vval(RAWFILE) : vval(PICTURE),
1506 >                                        vs);
1507 >                        strcpy(rppopt, "-PP pfXXXXXX");
1508 >                } else {
1509 >                        strcpy(rppopt, "-S 1 -PP pfXXXXXX");
1510 >                }
1511 >                pfile = rppopt + strlen(rppopt) - 8;
1512 >                if (mktemp(pfile) == NULL) {
1513 >                        if (do_rpiece) {
1514 >                                fprintf(stderr, "%s: cannot create\n", pfile);
1515 >                                quit(1);
1516 >                        }
1517 >                        pfile = NULL;
1518 >                }
1519 >        }
1520 >        vn = 0;                                 /* do each view */
1521          while ((vw = getview(vn++, vs)) != NULL) {
1522                  if (sayview)
1523 <                        printview(vw);
1524 <                if (!vs[0])
1143 <                        sprintf(vs, "%d", vn);
1144 <                sprintf(picfile, "%s_%s.pic", vval(PICTURE), vs);
1523 >                        myprintview(vw, stdout);
1524 >                sprintf(picfile, "%s_%s.hdr", vval(PICTURE), vs);
1525                  if (vdef(ZFILE))
1526                          sprintf(zopt, " -z %s_%s.zbf", vval(ZFILE), vs);
1527                  else
# Line 1162 | Line 1542 | char   *opts, *po;
1542                                  touch(picfile);
1543                          continue;
1544                  }
1545 +                                                /* parallel running? */
1546 +                if (do_rpiece) {
1547 +                        if (rfdt < oct1date || !fdate(sfile)) {
1548 +                                int     xdiv = 8+nprocs/3, ydiv = 8+nprocs/3;
1549 +                                if (rfdt >= oct1date) {
1550 +                                        fprintf(stderr,
1551 +                "%s: partial output not created with %s\n", rawfile, c_rpiece);
1552 +                                        quit(1);
1553 +                                }
1554 +                                if (rfdt) {     /* start fresh */
1555 +                                        rmfile(rawfile);
1556 +                                        rfdt = 0;
1557 +                                }
1558 +                                if (!silent)
1559 +                                        printf("\techo %d %d > %s\n",
1560 +                                                        xdiv, ydiv, sfile);
1561 +                                if ((fp = fopen(sfile, "w")) == NULL) {
1562 +                                        fprintf(stderr, "%s: cannot create\n",
1563 +                                                        sfile);
1564 +                                        quit(1);
1565 +                                }
1566 +                                fprintf(fp, "%d %d\n", xdiv, ydiv);
1567 +                                fclose(fp);
1568 +                        }
1569 +                } else if (next_process(0)) {
1570 +                        if (pfile != NULL)
1571 +                                sleep(10);
1572 +                        continue;
1573 +                } else if (!inchild())
1574 +                        pfile = NULL;
1575 +                /* XXX Remember to call finish_process() */
1576                                                  /* build rpict command */
1577 <                if (rfdt >= oct1date)           /* recover */
1578 <                        sprintf(combuf, "rpict%s%s%s%s -ro %s %s",
1579 <                                rep, po, opts, zopt, rawfile, oct1name);
1580 <                else {
1577 >                if (rfdt >= oct1date) {         /* already in progress */
1578 >                        if (do_rpiece) {
1579 >                                sprintf(combuf, "%s -R %s %s%s %s %s%s%s -o %s %s",
1580 >                                                c_rpiece, sfile, rppopt, rep, vw,
1581 >                                                res, opts, po, rawfile, oct1name);
1582 >                                while (next_process(1)) {
1583 >                                        sleep(10);
1584 >                                        combuf[strlen(c_rpiece)+2] = 'F';
1585 >                                }
1586 >                        } else
1587 >                                sprintf(combuf, "%s%s%s%s%s -ro %s %s", c_rpict,
1588 >                                        rep, opts, po, zopt, rawfile, oct1name);
1589 >                        if (runcom(combuf))     /* run rpict/rpiece */
1590 >                                goto rperror;
1591 >                } else {
1592                          if (overture) {         /* run overture calculation */
1593                                  sprintf(combuf,
1594 <                                "rpict%s %s%s -x 64 -y 64 -ps 1 %s > %s",
1595 <                                                rep, vw, opts,
1594 >                                        "%s%s %s%s -x 64 -y 64 -ps 1 %s > %s",
1595 >                                                c_rpict, rep, vw, opts,
1596                                                  oct1name, overfile);
1597 <                                if (runcom(combuf)) {
1598 <                                        fprintf(stderr,
1597 >                                if (!do_rpiece || !next_process(0)) {
1598 >                                        if (runcom(combuf)) {
1599 >                                                fprintf(stderr,
1600                                          "%s: error in overture for view %s\n",
1601 <                                                progname, vs);
1602 <                                        quit(1);
1603 <                                }
1604 < #ifdef NIX
1605 <                                rmfile(overfile);
1601 >                                                        progname, vs);
1602 >                                                quit(1);
1603 >                                        }
1604 > #ifndef NULL_DEVICE
1605 >                                        rmfile(overfile);
1606   #endif
1607 +                                } else if (do_rpiece)
1608 +                                        sleep(20);
1609                          }
1610 <                        sprintf(combuf, "rpict%s %s %s%s%s%s %s > %s",
1611 <                                        rep, vw, res, po, opts, zopt,
1612 <                                        oct1name, rawfile);
1610 >                        if (do_rpiece) {
1611 >                                sprintf(combuf, "%s -F %s %s%s %s %s%s%s -o %s %s",
1612 >                                                c_rpiece, sfile, rppopt, rep, vw,
1613 >                                                res, opts, po, rawfile, oct1name);
1614 >                                while (next_process(1))
1615 >                                        sleep(10);
1616 >                        } else {
1617 >                                sprintf(combuf, "%s%s %s %s%s%s%s %s > %s",
1618 >                                                c_rpict, rep, vw, res, opts, po,
1619 >                                                zopt, oct1name, rawfile);
1620 >                        }
1621 >                        if ((pfile != NULL) & !do_rpiece) {
1622 >                                if (!silent)
1623 >                                        printf("\t%s\n", combuf);
1624 >                                fflush(stdout);
1625 >                                sprintf(combuf, "%s%s %s %s%s%s %s > %s",
1626 >                                                c_rpict, rep, rppopt, res,
1627 >                                                opts, po, oct1name, rawfile);
1628 >                                fp = popen(combuf, "w");
1629 >                                if (fp == NULL)
1630 >                                        goto rperror;
1631 >                                myprintview(vw, fp);
1632 >                                if (pclose(fp))
1633 >                                        goto rperror;
1634 >                        } else if (runcom(combuf))
1635 >                                goto rperror;
1636                  }
1637 <                if (runcom(combuf)) {           /* run rpict */
1638 <                        fprintf(stderr, "%s: error rendering view %s\n",
1639 <                                        progname, vs);
1640 <                        quit(1);
1637 >                if (do_rpiece) {                /* need to finish raw, first */
1638 >                        finish_process();
1639 >                        wait_process(1);
1640 >                        if (!syncf_done(sfile)) {
1641 >                                fprintf(stderr,
1642 >                        "%s: %s did not complete rendering of view %s\n",
1643 >                                                progname, c_rpiece, vs);
1644 >                                quit(1);
1645 >                        }
1646                  }
1647                  if (!vdef(RAWFILE) || strcmp(vval(RAWFILE),vval(PICTURE))) {
1648                                                  /* build pfilt command */
1649 <                        if (mult > 1)
1650 <                                sprintf(combuf, "pfilt%s -x /%d -y /%d %s > %s",
1651 <                                        pfopts, mult, mult, rawfile, picfile);
1649 >                        if (do_rpiece)
1650 >                                sprintf(combuf,
1651 >                                        "%s%s -x %d -y %d -p %.3f %s > %s",
1652 >                                        c_pfilt, pfopts, xres, yres, aspect,
1653 >                                        rawfile, picfile);
1654 >                        else if (mult > 1)
1655 >                                sprintf(combuf, "%s%s -x /%d -y /%d %s > %s",
1656 >                                        c_pfilt, pfopts, mult, mult,
1657 >                                        rawfile, picfile);
1658                          else
1659 <                                sprintf(combuf, "pfilt%s %s > %s", pfopts,
1660 <                                                rawfile, picfile);
1661 <                        if (runcom(combuf)) {           /* run pfilt */
1659 >                                sprintf(combuf, "%s%s %s > %s", c_pfilt,
1660 >                                        pfopts, rawfile, picfile);
1661 >                        if (runcom(combuf)) {   /* run pfilt */
1662                                  fprintf(stderr,
1663                                  "%s: error filtering view %s\n\t%s removed\n",
1664                                                  progname, vs, picfile);
# Line 1209 | Line 1668 | char   *opts, *po;
1668                  }
1669                                                  /* remove/rename raw file */
1670                  if (vdef(RAWFILE)) {
1671 <                        sprintf(combuf, "%s_%s.pic", vval(RAWFILE), vs);
1671 >                        sprintf(combuf, "%s_%s.hdr", vval(RAWFILE), vs);
1672                          mvfile(rawfile, combuf);
1673                  } else
1674                          rmfile(rawfile);
1675 +                if (do_rpiece)                  /* done with sync file */
1676 +                        rmfile(sfile);
1677 +                else
1678 +                        finish_process();       /* exit if child */
1679          }
1680 +        wait_process(1);                /* wait for children to finish */
1681 +        if (pfile != NULL) {            /* clean up persistent rpict */
1682 +                RT_PID  pid;
1683 +                fp = fopen(pfile, "r");
1684 +                if (fp != NULL) {
1685 +                        if (fscanf(fp, "%*s %d", &pid) != 1 ||
1686 +                                        kill(pid, 1) < 0)
1687 +                                unlink(pfile);
1688 +                        fclose(fp);
1689 +                }
1690 +        }
1691 +        return;
1692 + rperror:
1693 +        fprintf(stderr, "%s: error rendering view %s\n", progname, vs);
1694 +        quit(1);
1695 + #undef do_rpiece
1696   }
1697  
1698  
1699 < touch(fn)                       /* update a file */
1700 < char    *fn;
1699 > static int
1700 > touch(                  /* update a file */
1701 >        char    *fn
1702 > )
1703   {
1704          if (!silent)
1705                  printf("\ttouch %s\n", fn);
1706 <        if (noaction)
1706 >        if (!nprocs)
1707                  return(0);
1227 #ifdef notused
1228        if (access(fn, F_OK) == -1)             /* create it */
1229                if (close(open(fn, O_WRONLY|O_CREAT, 0666)) == -1)
1230                        return(-1);
1231 #endif
1708          return(setfdate(fn, time((time_t *)NULL)));
1709   }
1710  
1711  
1712 < runcom(cs)                      /* run command */
1713 < char    *cs;
1712 > static int
1713 > runcom(                 /* run command */
1714 >        char    *cs
1715 > )
1716   {
1717          if (!silent)            /* echo it */
1718                  printf("\t%s\n", cs);
1719 <        if (noaction)
1719 >        if (!nprocs)
1720                  return(0);
1721 <        fflush(stdout);         /* flush output and pass to shell */
1721 >        fflush(NULL);           /* flush output and pass to shell */
1722          return(system(cs));
1723   }
1724  
1725  
1726 < rmfile(fn)                      /* remove a file */
1727 < char    *fn;
1726 > static int
1727 > rmfile(                 /* remove a file */
1728 >        char    *fn
1729 > )
1730   {
1731          if (!silent)
1732 < #ifdef MSDOS
1733 <                printf("\tdel %s\n", fn);
1254 < #else
1255 <                printf("\trm -f %s\n", fn);
1256 < #endif
1257 <        if (noaction)
1732 >                printf("\t%s %s\n", DELCMD, fn);
1733 >        if (!nprocs)
1734                  return(0);
1735          return(unlink(fn));
1736   }
1737  
1738  
1739 < mvfile(fold, fnew)              /* move a file */
1740 < char    *fold, *fnew;
1739 > static int
1740 > mvfile(         /* move a file */
1741 >        char    *fold,
1742 >        char    *fnew
1743 > )
1744   {
1745          if (!silent)
1746 < #ifdef MSDOS
1747 <                printf("\trename %s %s\n", fold, fnew);
1269 < #else
1270 <                printf("\tmv %s %s\n", fold, fnew);
1271 < #endif
1272 <        if (noaction)
1746 >                printf("\t%s %s %s\n", RENAMECMD, fold, fnew);
1747 >        if (!nprocs)
1748                  return(0);
1749          return(rename(fold, fnew));
1750   }
1751  
1752  
1753 < #ifdef MSDOS
1754 < setenv(vname, value)            /* set an environment variable */
1755 < char    *vname, *value;
1753 > #ifdef RHAS_FORK_EXEC
1754 > static int
1755 > next_process(int reserve)               /* fork the next process */
1756   {
1757 <        register char   *evp;
1757 >        RT_PID  child_pid;
1758  
1759 <        evp = bmalloc(strlen(vname)+strlen(value)+2);
1760 <        if (evp == NULL)
1761 <                syserr(progname);
1762 <        sprintf(evp, "%s=%s", vname, value);
1763 <        if (putenv(evp) != 0) {
1289 <                fprintf(stderr, "%s: out of environment space\n", progname);
1759 >        if (nprocs <= 1)
1760 >                return(0);              /* it's us or no one */
1761 >        if (inchild()) {
1762 >                fprintf(stderr, "%s: internal error 1 in next_process()\n",
1763 >                                progname);
1764                  quit(1);
1765          }
1766 <        if (!silent)
1767 <                printf("set %s\n", evp);
1766 >        if (reserve > 0 && children_running >= nprocs-reserve)
1767 >                return(0);              /* caller holding back process(es) */
1768 >        if (children_running >= nprocs)
1769 >                wait_process(0);        /* wait for someone to finish */
1770 >        fflush(NULL);                   /* flush output */
1771 >        child_pid = fork();             /* split process */
1772 >        if (child_pid == 0) {           /* we're the child */
1773 >                children_running = -1;
1774 >                nprocs = 1;
1775 >                return(0);
1776 >        }
1777 >        if (child_pid > 0) {            /* we're the parent */
1778 >                ++children_running;
1779 >                return(1);
1780 >        }
1781 >        fprintf(stderr, "%s: warning -- fork() failed\n", progname);
1782 >        return(0);
1783   }
1295 #endif
1784  
1785 + static void
1786 + wait_process(                   /* wait for process(es) to finish */
1787 +        int     all
1788 + )
1789 + {
1790 +        int     ourstatus = 0, status;
1791 +        RT_PID  pid;
1792  
1793 < badvalue(vc)                    /* report bad variable value and exit */
1794 < int     vc;
1793 >        if (all)
1794 >                all = children_running;
1795 >        else if (children_running > 0)
1796 >                all = 1;
1797 >        while (all-- > 0) {
1798 >                pid = wait(&status);
1799 >                if (pid < 0)
1800 >                        syserr(progname);
1801 >                status = status>>8 & 0xff;
1802 >                --children_running;
1803 >                if (status != 0) {      /* child's problem is our problem */
1804 >                        if ((ourstatus == 0) & (children_running > 0))
1805 >                                fprintf(stderr, "%s: waiting for remaining processes\n",
1806 >                                                progname);
1807 >                        ourstatus = status;
1808 >                        all = children_running;
1809 >                }
1810 >        }
1811 >        if (ourstatus != 0)
1812 >                quit(ourstatus);        /* bad status from child */
1813 > }
1814 > #else   /* ! RHAS_FORK_EXEC */
1815 > static int
1816 > next_process(int reserve)
1817   {
1818 +        return(0);                      /* cannot start new process */
1819 + }
1820 + static void
1821 + wait_process(all)
1822 + int     all;
1823 + {
1824 +        (void)all;                      /* no one to wait for */
1825 + }
1826 + int
1827 + kill(pid, sig) /* win|unix_process.c should also wait and kill */
1828 + RT_PID pid;
1829 + int sig;
1830 + {
1831 +        return 0;
1832 + }
1833 + #endif  /* ! RHAS_FORK_EXEC */
1834 +
1835 + static void
1836 + finish_process(void)                    /* exit a child process */
1837 + {
1838 +        if (!inchild())
1839 +                return;                 /* in parent -- noop */
1840 +        exit(0);
1841 + }
1842 +
1843 +
1844 + static void
1845 + badvalue(                       /* report bad variable value and exit */
1846 +        int     vc
1847 + )
1848 + {
1849          fprintf(stderr, "%s: bad value for variable '%s'\n",
1850                          progname, vnam(vc));
1851          quit(1);
1852   }
1853  
1854  
1855 < syserr(s)                       /* report a system error and exit */
1856 < char    *s;
1855 > static void
1856 > syserr(                 /* report a system error and exit */
1857 >        char    *s
1858 > )
1859   {
1860          perror(s);
1861          quit(1);
1862   }
1863  
1864  
1865 + void
1866   quit(ec)                        /* exit program */
1867   int     ec;
1868   {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines