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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines