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.1 by greg, Thu Mar 11 09:11:00 1993 UTC vs.
Revision 2.77 by greg, Thu Sep 9 20:08:21 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1993 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Executive program for oconv, rpict and pfilt
6   */
7  
8   #include "standard.h"
9 < #include "paths.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"
18  
19 < typedef struct {
20 <        char    *name;          /* variable name */
21 <        short   nick;           /* # characters required for nickname */
22 <        short   nass;           /* # assignments made */
23 <        char    *value;         /* assigned value(s) */
24 <        int     (*fixval)();    /* assignment checking function */
25 < } VARIABLE;
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 > #endif
28  
29 < int     onevalue(), catvalues();
30 <
31 <                                /* variables */
32 < #define OBJECT          0               /* object files */
33 < #define SCENE           1               /* scene files */
34 < #define MATERIAL        2               /* material files */
35 < #define RENDER          3               /* rendering options */
36 < #define OCONV           4               /* oconv options */
37 < #define PFILT           5               /* pfilt options */
38 < #define VIEW            6               /* view(s) for picture(s) */
39 < #define ZONE            7               /* simulation zone */
40 < #define QUALITY         8               /* desired rendering quality */
41 < #define OCTREE          9               /* octree file name */
42 < #define PICTURE         10              /* picture file name */
43 < #define AMBFILE         11              /* ambient file name */
44 < #define OPTFILE         12              /* rendering options file */
45 < #define EXPOSURE        13              /* picture exposure setting */
46 < #define RESOLUTION      14              /* maximum picture resolution */
47 < #define UP              15              /* view up (X, Y or Z) */
48 < #define INDIRECT        16              /* indirection in lighting */
49 < #define DETAIL          17              /* level of scene detail */
50 < #define PENUMBRAS       18              /* shadow penumbras are desired */
51 < #define VARIABILITY     19              /* level of light variability */
52 < #define REPORT          20              /* report frequency and errfile */
29 >                                /* variables (alphabetical by name) */
30 > #define AMBFILE         0               /* ambient file name */
31 > #define DETAIL          1               /* level of scene detail */
32 > #define EXPOSURE        2               /* picture exposure setting */
33 > #define EYESEP          3               /* interocular distance */
34 > #define ILLUM           4               /* mkillum input files */
35 > #define INDIRECT        5               /* indirection in lighting */
36 > #define MATERIAL        6               /* material files */
37 > #define MKILLUM         7               /* mkillum options */
38 > #define OBJECT          8               /* object files */
39 > #define OCONV           9               /* oconv options */
40 > #define OCTREE          10              /* octree file name */
41 > #define OPTFILE         11              /* rendering options file */
42 > #define PENUMBRAS       12              /* shadow penumbras are desired */
43 > #define PFILT           13              /* pfilt options */
44 > #define PICTURE         14              /* picture file root name */
45 > #define QUALITY         15              /* desired rendering quality */
46 > #define RAWFILE         16              /* raw picture file root name */
47 > #define RENDER          17              /* rendering options */
48 > #define REPORT          18              /* report frequency and errfile */
49 > #define RESOLUTION      19              /* maximum picture resolution */
50 > #define SCENE           20              /* scene files */
51 > #define UP              21              /* view up (X, Y or Z) */
52 > #define VARIABILITY     22              /* level of light variability */
53 > #define VIEWS           23              /* view(s) for picture(s) */
54 > #define ZFILE           24              /* distance file root name */
55 > #define ZONE            25              /* simulation zone */
56                                  /* total number of variables */
57 < #define NVARS           21
57 > int NVARS = 26;
58  
59 < VARIABLE        vv[NVARS] = {           /* variable-value pairs */
60 <        {"objects",     3,      0,      NULL,   catvalues},
61 <        {"scene",       3,      0,      NULL,   catvalues},
59 > VARIABLE        vv[] = {                /* variable-value pairs */
60 >        {"AMBFILE",     3,      0,      NULL,   onevalue},
61 >        {"DETAIL",      3,      0,      NULL,   qualvalue},
62 >        {"EXPOSURE",    3,      0,      NULL,   fltvalue},
63 >        {"EYESEP",      3,      0,      NULL,   fltvalue},
64 >        {"illum",       3,      0,      NULL,   catvalues},
65 >        {"INDIRECT",    3,      0,      NULL,   intvalue},
66          {"materials",   3,      0,      NULL,   catvalues},
67 <        {"render",      3,      0,      NULL,   catvalues},
67 >        {"mkillum",     3,      0,      NULL,   catvalues},
68 >        {"objects",     3,      0,      NULL,   catvalues},
69          {"oconv",       3,      0,      NULL,   catvalues},
57        {"pfilt",       2,      0,      NULL,   catvalues},
58        {"view",        2,      0,      NULL,   NULL},
59        {"ZONE",        2,      0,      NULL,   onevalue},
60        {"QUALITY",     3,      0,      NULL,   onevalue},
70          {"OCTREE",      3,      0,      NULL,   onevalue},
62        {"PICTURE",     3,      0,      NULL,   onevalue},
63        {"AMBFILE",     3,      0,      NULL,   onevalue},
71          {"OPTFILE",     3,      0,      NULL,   onevalue},
72 <        {"EXPOSURE",    3,      0,      NULL,   onevalue},
72 >        {"PENUMBRAS",   3,      0,      NULL,   boolvalue},
73 >        {"pfilt",       2,      0,      NULL,   catvalues},
74 >        {"PICTURE",     3,      0,      NULL,   onevalue},
75 >        {"QUALITY",     3,      0,      NULL,   qualvalue},
76 >        {"RAWFILE",     3,      0,      NULL,   onevalue},
77 >        {"render",      3,      0,      NULL,   catvalues},
78 >        {"REPORT",      3,      0,      NULL,   onevalue},
79          {"RESOLUTION",  3,      0,      NULL,   onevalue},
80 +        {"scene",       3,      0,      NULL,   catvalues},
81          {"UP",          2,      0,      NULL,   onevalue},
82 <        {"INDIRECT",    3,      0,      NULL,   onevalue},
83 <        {"DETAIL",      3,      0,      NULL,   onevalue},
84 <        {"PENUMBRAS",   3,      0,      NULL,   onevalue},
85 <        {"VARIABILITY", 3,      0,      NULL,   onevalue},
72 <        {"REPORT",      3,      0,      NULL,   onevalue},
82 >        {"VARIABILITY", 3,      0,      NULL,   qualvalue},
83 >        {"view",        2,      0,      NULL,   NULL},
84 >        {"ZFILE",       2,      0,      NULL,   onevalue},
85 >        {"ZONE",        2,      0,      NULL,   onevalue},
86   };
87  
88 < VARIABLE        *matchvar();
89 < char    *nvalue();
90 < int     vscale();
88 >                                /* overture calculation file */
89 > #ifdef NULL_DEVICE
90 > char    overfile[] = NULL_DEVICE;
91 > #else
92 > char    overfile[] = "overture.unf";
93 > #endif
94  
79 #define vnam(vc)        (vv[vc].name)
80 #define vdef(vc)        (vv[vc].nass)
81 #define vval(vc)        (vv[vc].value)
82 #define vint(vc)        atoi(vval(vc))
83 #define vlet(vc)        (vval(vc)[0]&~0x20)
84 #define vbool(vc)       (vlet(vc)=='T')
95  
96 < #define HIGH            2
97 < #define MEDIUM          1
98 < #define LOW             0
96 > time_t  scenedate;              /* date of latest scene or object file */
97 > time_t  octreedate;             /* date of octree */
98 > time_t  matdate;                /* date of latest material file */
99 > time_t  illumdate;              /* date of last illum file */
100  
101 < int     lowqopts(), medqopts(), hiqopts();
102 < int     (*setqopts[3])() = {lowqopts, medqopts, hiqopts};
101 > char    *oct0name;              /* name of pre-mkillum octree */
102 > time_t  oct0date;               /* date of pre-mkillum octree */
103 > char    *oct1name;              /* name of post-mkillum octree */
104 > time_t  oct1date;               /* date of post-mkillum octree (>= matdate) */
105  
106 < #define renderopts      (*setqopts[vscale(QUALITY)])
94 <
95 < extern long     fdate();
96 <
97 < long    scenedate;              /* date of latest scene or object file */
98 < long    octreedate;             /* date of octree */
99 <
106 > int     nowarn = 0;             /* no warnings */
107   int     explicate = 0;          /* explicate variables */
108   int     silent = 0;             /* do work silently */
109 < int     noaction = 0;           /* don't do anything */
110 < char    *rvdevice = NULL;       /* rview output device */
109 > int     touchonly = 0;          /* touch files only */
110 > int     nprocs = 1;             /* maximum executing processes */
111 > int     sayview = 0;            /* print view out */
112 > char    *rvdevice = NULL;       /* rvu output device */
113   char    *viewselect = NULL;     /* specific view only */
114  
115   int     overture = 0;           /* overture calculation needed */
116  
117 + int     children_running = 0;   /* set negative in children */
118 +
119   char    *progname;              /* global argv[0] */
120 + char    *rifname;               /* global rad input file name */
121  
122 < char    radname[MAXPATH];       /* root Radiance file name */
122 > char    radname[PATH_MAX];      /* root Radiance file name */
123  
124 + #define inchild()       (children_running < 0)
125  
126 < main(argc, argv)
127 < int     argc;
128 < char    *argv[];
126 > static void rootname(char       *rn, char       *fn);
127 > static time_t checklast(char    *fnames);
128 > static char * newfname(char     *orig, int      pred);
129 > static void checkfiles(void);
130 > static void getoctcube(double   org[3], double  *sizp);
131 > static void setdefaults(void);
132 > static void oconv(void);
133 > static char * addarg(char       *op, char       *arg);
134 > static void oconvopts(char      *oo);
135 > static void mkillumopts(char    *mo);
136 > static void checkambfile(void);
137 > static double ambval(void);
138 > static void renderopts(char     *op, char       *po);
139 > static void lowqopts(char       *op, char       *po);
140 > static void medqopts(char       *op, char       *po);
141 > static void hiqopts(char        *op, char       *po);
142 > static void xferopts(char       *ro);
143 > static void pfiltopts(char      *po);
144 > static int matchword(char       *s1, char       *s2);
145 > static char * specview(char     *vs);
146 > static char * getview(int       n, char *vn);
147 > static int myprintview(char     *vopts, FILE    *fp);
148 > static void rvu(char    *opts, char     *po);
149 > static void rpict(char  *opts, char     *po);
150 > static int touch(char   *fn);
151 > static int runcom(char  *cs);
152 > static int rmfile(char  *fn);
153 > static int mvfile(char  *fold, char     *fnew);
154 > static int next_process(void);
155 > static void wait_process(int    all);
156 > static void finish_process(void);
157 > static void badvalue(int        vc);
158 > static void syserr(char *s);
159 >
160 >
161 > int
162 > main(
163 >        int     argc,
164 >        char    *argv[]
165 > )
166   {
167          char    ropts[512];
168 +        char    popts[64];
169          int     i;
170  
171          progname = argv[0];
# Line 125 | Line 176 | char   *argv[];
176                          silent++;
177                          break;
178                  case 'n':
179 <                        noaction++;
179 >                        nprocs = 0;
180                          break;
181 +                case 'N':
182 +                        nprocs = atoi(argv[++i]);
183 +                        if (nprocs < 0)
184 +                                nprocs = 0;
185 +                        break;
186 +                case 't':
187 +                        touchonly++;
188 +                        break;
189                  case 'e':
190                          explicate++;
191                          break;
192                  case 'o':
193                          rvdevice = argv[++i];
194                          break;
195 +                case 'V':
196 +                        sayview++;
197 +                        break;
198                  case 'v':
199                          viewselect = argv[++i];
200                          break;
201 +                case 'w':
202 +                        nowarn++;
203 +                        break;
204                  default:
205                          goto userr;
206                  }
207          if (i >= argc)
208                  goto userr;
209 +        rifname = argv[i];
210 +                                /* check command-line options */
211 +        if ((nprocs > 1) & (viewselect != NULL))
212 +                nprocs = 1;
213                                  /* assign Radiance root file name */
214 <        rootname(radname, argv[i]);
214 >        rootname(radname, rifname);
215                                  /* load variable values */
216 <        load(argv[i]);
216 >        loadvars(rifname);
217                                  /* get any additional assignments */
218          for (i++; i < argc; i++)
219 <                setvariable(argv[i]);
219 >                if (setvariable(argv[i], matchvar) < 0) {
220 >                        fprintf(stderr, "%s: unknown variable: %s\n",
221 >                                        progname, argv[i]);
222 >                        quit(1);
223 >                }
224                                  /* check assignments */
225          checkvalues();
226                                  /* check files and dates */
# Line 156 | Line 229 | char   *argv[];
229          setdefaults();
230                                  /* print all values if requested */
231          if (explicate)
232 <                printvals();
233 <                                /* run simulation */
232 >                printvars(stdout);
233 >                                /* build octree (and run mkillum) */
234          oconv();
235 <        renderopts(ropts);
235 >                                /* check date on ambient file */
236 >        checkambfile();
237 >                                /* run simulation */
238 >        renderopts(ropts, popts);
239          xferopts(ropts);
240          if (rvdevice != NULL)
241 <                rview(ropts);
241 >                rvu(ropts, popts);
242          else
243 <                rpict(ropts);
244 <        exit(0);
243 >                rpict(ropts, popts);
244 >        quit(0);
245   userr:
246          fprintf(stderr,
247 <        "Usage: %s [-s][-n][-v view][-o dev] rfile [VAR=value ..]\n",
247 > "Usage: %s [-w][-s][-n|-N npr][-t][-e][-V][-v view][-o dev] rfile [VAR=value ..]\n",
248                          progname);
249 <        exit(1);
249 >        quit(1);
250 >        return 1; /* pro forma return */
251   }
252  
253  
254 < rootname(rn, fn)                /* remove tail from end of fn */
255 < register char   *rn, *fn;
254 > static void
255 > rootname(               /* remove tail from end of fn */
256 >        register char   *rn,
257 >        register char   *fn
258 > )
259   {
260          char    *tp, *dp;
261  
262 <        for (tp = NULL, dp = rn; *rn = *fn++; rn++)
262 >        for (tp = NULL, dp = rn; (*rn = *fn++); rn++)
263                  if (ISDIRSEP(*rn))
264                          dp = rn;
265                  else if (*rn == '.')
# Line 189 | Line 269 | register char  *rn, *fn;
269   }
270  
271  
272 < load(rfname)                    /* load Radiance simulation file */
273 < char    *rfname;
272 > static time_t
273 > checklast(                      /* check files and find most recent */
274 >        register char   *fnames
275 > )
276   {
277 <        FILE    *fp;
278 <        char    buf[256];
197 <        register char   *cp;
277 >        char    thisfile[PATH_MAX];
278 >        time_t  thisdate, lastdate = 0;
279  
280 <        if (rfname == NULL)
281 <                fp = stdin;
282 <        else if ((fp = fopen(rfname, "r")) == NULL) {
283 <                perror(rfname);
284 <                exit(1);
285 <        }
286 <        while (fgetline(buf, sizeof(buf), fp) != NULL) {
287 <                for (cp = buf; *cp; cp++) {
207 <                        switch (*cp) {
208 <                        case '\\':
209 <                        case '\n':
210 <                                *cp = ' ';
211 <                                continue;
212 <                        case '#':
213 <                                *cp = '\0';
214 <                                break;
215 <                        }
216 <                        break;
280 >        if (fnames == NULL)
281 >                return(0);
282 >        while ((fnames = nextword(thisfile, PATH_MAX, fnames)) != NULL) {
283 >                if (thisfile[0] == '!' ||
284 >                                (thisfile[0] == '\\' && thisfile[1] == '!')) {
285 >                        if (!lastdate)
286 >                                lastdate = 1;
287 >                        continue;
288                  }
289 <                setvariable(buf);
289 >                if (!(thisdate = fdate(thisfile)))
290 >                        syserr(thisfile);
291 >                if (thisdate > lastdate)
292 >                        lastdate = thisdate;
293          }
294 <        fclose(fp);
294 >        return(lastdate);
295   }
296  
297  
298 < setvariable(ass)                /* assign variable according to string */
299 < register char   *ass;
298 > static char *
299 > newfname(               /* create modified file name */
300 >        char    *orig,
301 >        int     pred
302 > )
303   {
227        char    varname[32];
304          register char   *cp;
305 <        register VARIABLE       *vp;
306 <        register int    i;
231 <        int     n;
305 >        register int    n;
306 >        int     suffix;
307  
308 <        while (isspace(*ass))           /* skip leading space */
309 <                ass++;
310 <        cp = varname;                   /* extract name */
311 <        while (cp < varname+sizeof(varname)-1
312 <                        && *ass && !isspace(*ass) && *ass != '=')
238 <                *cp++ = *ass++;
239 <        *cp = '\0';
240 <        if (!varname[0])
241 <                return;         /* no variable name! */
242 <                                        /* trim value */
243 <        while (isspace(*ass) || *ass == '=')
244 <                ass++;
245 <        cp = ass + strlen(ass);
246 <        do
247 <                *cp-- = '\0';
248 <        while (cp >= ass && isspace(*cp));
249 <        n = cp - ass + 1;
250 <        if (!n) {
251 <                fprintf(stderr, "%s: warning - missing value for variable '%s'\n",
252 <                                progname, varname);
253 <                return;
308 >        n = 0; cp = orig; suffix = -1;          /* suffix position, length */
309 >        while (*cp) {
310 >                if (*cp == '.') suffix = n;
311 >                else if (ISDIRSEP(*cp)) suffix = -1;
312 >                cp++; n++;
313          }
314 <                                        /* match variable from list */
315 <        vp = matchvar(varname);
316 <        if (vp == NULL) {
317 <                fprintf(stderr, "%s: unknown variable '%s'\n",
318 <                                progname, varname);
319 <                exit(1);
261 <        }
262 <                                        /* assign new value */
263 <        cp = vp->value; i = vp->nass;
264 <        while (i--)
265 <                while (*cp++)
266 <                        ;
267 <        i = cp - vp->value;
268 <        vp->value = realloc(vp->value, i+n+1);
269 <        if (vp->value == NULL) {
270 <                perror(progname);
271 <                exit(1);
272 <        }
273 <        strcpy(vp->value+i, ass);
274 <        vp->nass++;
275 < }
276 <
277 <
278 < VARIABLE *
279 < matchvar(nam)                   /* match a variable by its name */
280 < char    *nam;
281 < {
282 <        int     n = strlen(nam);
283 <        register int    i;
284 <
285 <        for (i = 0; i < NVARS; i++)
286 <                if (n >= vv[i].nick && !strncmp(nam, vv[i].name, n))
287 <                        return(vv+i);
288 <        return(NULL);
289 < }
290 <
291 <
292 < char *
293 < nvalue(vp, n)                   /* return nth variable value */
294 < VARIABLE        *vp;
295 < register int    n;
296 < {
297 <        register char   *cp;
298 <
299 <        if (vp == NULL || n < 0 || n >= vp->nass)
300 <                return(NULL);
301 <        cp = vp->value;
302 <        while (n--)
303 <                while (*cp++)
304 <                        ;
314 >        if (suffix == -1) suffix = n;
315 >        if ((cp = bmalloc(n+2)) == NULL)
316 >                syserr(progname);
317 >        strncpy(cp, orig, suffix);
318 >        cp[suffix] = pred;                      /* root name + pred + suffix */
319 >        strcpy(cp+suffix+1, orig+suffix);
320          return(cp);
321   }
322  
323  
324 < int
325 < vscale(vc)                      /* return scale for variable vc */
311 < int     vc;
324 > static void
325 > checkfiles(void)                        /* check for existence and modified times */
326   {
327 <        switch(vlet(vc)) {
314 <        case 'H':
315 <                return(HIGH);
316 <        case 'M':
317 <                return(MEDIUM);
318 <        case 'L':
319 <                return(LOW);
320 <        }
321 <        fprintf(stderr, "%s: illegal value for variable '%s' (%s)\n",
322 <                        progname, vnam(vc), vval(vc));
323 <        exit(1);
324 < }
327 >        time_t  objdate;
328  
329 <
330 < checkvalues()                   /* check assignments */
331 < {
332 <        register int    i;
333 <
334 <        for (i = 0; i < NVARS; i++)
335 <                if (vv[i].fixval != NULL)
336 <                        (*vv[i].fixval)(vv+i);
337 < }
335 <
336 <
337 < onevalue(vp)                    /* only one assignment for this variable */
338 < register VARIABLE       *vp;
339 < {
340 <        if (vp->nass < 2)
341 <                return;
342 <        fprintf(stderr, "%s: warning - multiple assignment of variable '%s'\n",
343 <                        progname, vp->name);
344 <        while (vp->nass-- > 1)
345 <                vp->value += strlen(vp->value)+1;
346 < }
347 <
348 <
349 < catvalues(vp)                   /* concatenate variable values */
350 < register VARIABLE       *vp;
351 < {
352 <        register char   *cp;
353 <
354 <        if (vp->nass < 2)
355 <                return;
356 <        for (cp = vp->value; vp->nass > 1; vp->nass--) {
357 <                while (*cp)
358 <                        cp++;
359 <                *cp++ = ' ';
329 >        if (!vdef(OCTREE)) {
330 >                if ((vval(OCTREE) = bmalloc(strlen(radname)+5)) == NULL)
331 >                        syserr(progname);
332 >                sprintf(vval(OCTREE), "%s.oct", radname);
333 >                vdef(OCTREE)++;
334 >        } else if (vval(OCTREE)[0] == '!') {
335 >                fprintf(stderr, "%s: illegal '%s' specification\n",
336 >                                progname, vnam(OCTREE));
337 >                quit(1);
338          }
339 < }
340 <
341 <
342 < long
343 < checklast(fnames)                       /* check files and find most recent */
344 < register char   *fnames;
345 < {
346 <        char    thisfile[MAXPATH];
347 <        long    thisdate, lastdate = -1;
348 <        register char   *cp;
349 <
350 <        while (*fnames) {
351 <                while (isspace(*fnames)) fnames++;
352 <                cp = thisfile;
353 <                while (*fnames && !isspace(*fnames)) *cp++ = *fnames++;
354 <                *cp = '\0';
377 <                if ((thisdate = fdate(thisfile)) < 0) {
378 <                        perror(thisfile);
379 <                        exit(1);
380 <                }
381 <                if (thisdate > lastdate)
382 <                        lastdate = thisdate;
339 >        octreedate = fdate(vval(OCTREE));
340 >        if (vdef(ILLUM)) {              /* illum requires secondary octrees */
341 >                oct0name = newfname(vval(OCTREE), '0');
342 >                oct1name = newfname(vval(OCTREE), '1');
343 >                oct0date = fdate(oct0name);
344 >                oct1date = fdate(oct1name);
345 >        } else
346 >                oct0name = oct1name = vval(OCTREE);
347 >        if ((scenedate = checklast(vval(SCENE))) &&
348 >                        (objdate = checklast(vval(OBJECT))) > scenedate)
349 >                scenedate = objdate;
350 >        illumdate = checklast(vval(ILLUM));
351 >        if (!octreedate & !scenedate & !illumdate) {
352 >                fprintf(stderr, "%s: need '%s' or '%s' or '%s'\n", progname,
353 >                                vnam(OCTREE), vnam(SCENE), vnam(ILLUM));
354 >                quit(1);
355          }
356 <        return(lastdate);
385 < }
386 <
387 <
388 < checkfiles()                    /* check for existence and modified times */
389 < {
390 <        long    objdate;
391 <
392 <        octreedate = vdef(OCTREE) ? fdate(vval(OCTREE)) : -1;
393 <        scenedate = -1;
394 <        if (vdef(SCENE)) {
395 <                scenedate = checklast(vval(SCENE));
396 <                if (vdef(OBJECT)) {
397 <                        objdate = checklast(vval(OBJECT));
398 <                        if (objdate > scenedate)
399 <                                scenedate = objdate;
400 <                }
401 <        }
402 <        if (octreedate < 0 & scenedate < 0) {
403 <                fprintf(stderr, "%s: need scene or octree\n", progname);
404 <                exit(1);
405 <        }
356 >        matdate = checklast(vval(MATERIAL));
357   }      
358  
359  
360 < setdefaults()                   /* set default values for unassigned var's */
360 > static void
361 > getoctcube(             /* get octree bounding cube */
362 >        double  org[3],
363 >        double  *sizp
364 > )
365   {
366 +        static double   oorg[3], osiz = 0.;
367 +        double  min[3], max[3];
368 +        char    buf[1024];
369          FILE    *fp;
370 <        double  xmin, ymin, zmin, size;
413 <        char    buf[512];
414 <        char    *cp;
370 >        register int    i;
371  
372 <        if (!vdef(OCTREE)) {
373 <                sprintf(buf, "%s.oct", radname);
374 <                vval(OCTREE) = savqstr(buf);
375 <                vdef(OCTREE)++;
376 <        }
377 <        if (!vdef(ZONE)) {
378 <                if (scenedate > octreedate) {
379 <                        sprintf(buf, "getbbox -w -h %s", vval(SCENE));
380 <                        if (!silent) {
381 <                                printf("\t%s\n", buf);
382 <                                fflush(stdout);
372 >        if (osiz <= FTINY) {
373 >                if (!nprocs && fdate(oct1name) <
374 >                                (scenedate>illumdate?scenedate:illumdate)) {
375 >                                                        /* run getbbox */
376 >                        sprintf(buf, "getbbox -w -h %s",
377 >                                vdef(SCENE) ? vval(SCENE) : vval(ILLUM));
378 >                        if ((fp = popen(buf, "r")) == NULL)
379 >                                syserr("getbbox");
380 >                        if (fscanf(fp, "%lf %lf %lf %lf %lf %lf",
381 >                                        &min[0], &max[0], &min[1], &max[1],
382 >                                        &min[2], &max[2]) != 6) {
383 >                                fprintf(stderr,
384 >                        "%s: error reading bounding box from getbbox\n",
385 >                                                progname);
386 >                                quit(1);
387                          }
388 <                        if ((fp = popen(buf, "r")) == NULL) {
389 <                                perror("getbbox");
390 <                                exit(1);
391 <                        }
392 <                        buf[0] = 'E'; buf[1] = ' ';
433 <                        fgetline(buf+2, sizeof(buf)-2, fp);
388 >                        for (i = 0; i < 3; i++)
389 >                                if (max[i] - min[i] > osiz)
390 >                                        osiz = max[i] - min[i];
391 >                        for (i = 0; i < 3; i++)
392 >                                oorg[i] = (max[i]+min[i]-osiz)*.5;
393                          pclose(fp);
394 <                } else {
395 <                        sprintf(buf, "getinfo -d < %s", vval(OCTREE));
396 <                        if ((fp = popen(buf, "r")) == NULL) {
397 <                                perror("getinfo");
398 <                                exit(1);
394 >                } else {                                /* from octree */
395 >                        oconv();        /* does nothing if done already */
396 >                        sprintf(buf, "getinfo -d < %s", oct1name);
397 >                        if ((fp = popen(buf, "r")) == NULL)
398 >                                syserr("getinfo");
399 >                        if (fscanf(fp, "%lf %lf %lf %lf", &oorg[0], &oorg[1],
400 >                                        &oorg[2], &osiz) != 4) {
401 >                                fprintf(stderr,
402 >                        "%s: error reading bounding cube from getinfo\n",
403 >                                                progname);
404 >                                quit(1);
405                          }
441                        fscanf(fp, "%lf %lf %lf %lf",
442                                        &xmin, &ymin, &zmin, &size);
443                        sprintf(buf, "E %g %g %g %g %g %g", xmin, xmin+size,
444                                        ymin, ymin+size, zmin, zmin+size);
406                          pclose(fp);
407                  }
408 +        }
409 +        org[0] = oorg[0]; org[1] = oorg[1]; org[2] = oorg[2]; *sizp = osiz;
410 + }
411 +
412 +
413 + static void
414 + setdefaults(void)                       /* set default values for unassigned var's */
415 + {
416 +        double  org[3], lim[3], size;
417 +        char    buf[128];
418 +
419 +        if (!vdef(ZONE)) {
420 +                getoctcube(org, &size);
421 +                sprintf(buf, "E %g %g %g %g %g %g", org[0], org[0]+size,
422 +                                org[1], org[1]+size, org[2], org[2]+size);
423                  vval(ZONE) = savqstr(buf);
424                  vdef(ZONE)++;
425          }
426 <        if (!vdef(UP)) {
427 <                vval(UP) = "Z";
428 <                vdef(UP)++;
426 >        if (!vdef(EYESEP)) {
427 >                if (sscanf(vval(ZONE), "%*s %lf %lf %lf %lf %lf %lf",
428 >                                &org[0], &lim[0], &org[1], &lim[1],
429 >                                &org[2], &lim[2]) != 6)
430 >                        badvalue(ZONE);
431 >                sprintf(buf, "%f",
432 >                        0.01*(lim[0]-org[0]+lim[1]-org[1]+lim[2]-org[2]));
433 >                vval(EYESEP) = savqstr(buf);
434 >                vdef(EYESEP)++;
435          }
436          if (!vdef(INDIRECT)) {
437                  vval(INDIRECT) = "0";
# Line 467 | Line 449 | setdefaults()                  /* set default values for unassigned v
449                  vval(PICTURE) = radname;
450                  vdef(PICTURE)++;
451          }
452 <        if (!vdef(AMBFILE)) {
453 <                sprintf(buf, "%s.amb", radname);
454 <                vval(AMBFILE) = savqstr(buf);
473 <                vdef(AMBFILE)++;
452 >        if (!vdef(VIEWS)) {
453 >                vval(VIEWS) = "X";
454 >                vdef(VIEWS)++;
455          }
475        if (!vdef(VIEW)) {
476                vval(VIEW) = "X";
477                vdef(VIEW)++;
478        }
456          if (!vdef(DETAIL)) {
457                  vval(DETAIL) = "M";
458                  vdef(DETAIL)++;
# Line 491 | Line 468 | setdefaults()                  /* set default values for unassigned v
468   }
469  
470  
471 < printvals()                     /* print variable values */
471 > static void
472 > oconv(void)                             /* run oconv and mkillum if necessary */
473   {
474 <        register int    i, j;
474 >        static char     illumtmp[] = "ilXXXXXX";
475 >        char    combuf[PATH_MAX], ocopts[64], mkopts[64];
476  
477 <        for (i = 0; i < NVARS; i++)
478 <                for (j = 0; j < vv[i].nass; j++)
479 <                        printf("%s= %s\n", vv[i].name, nvalue(vv+i, j));
480 <        fflush(stdout);
481 < }
482 <
483 <
484 < oconv()                         /* run oconv if necessary */
485 < {
486 <        char    combuf[512], ocopts[64];
487 <
488 <        if (octreedate > scenedate)     /* check dates */
489 <                return;
490 <                                        /* build command */
491 <        oconvopts(ocopts);
492 <        sprintf(combuf, "oconv%s %s > %s", ocopts, vval(SCENE), vval(OCTREE));
493 <        if (!silent) {                  /* echo it */
494 <                printf("\t%s\n", combuf);
495 <                fflush(stdout);
477 >        oconvopts(ocopts);              /* get options */
478 >        if (octreedate < scenedate) {   /* check date on original octree */
479 >                if (touchonly && octreedate)
480 >                        touch(vval(OCTREE));
481 >                else {                          /* build command */
482 >                        if (vdef(MATERIAL))
483 >                                sprintf(combuf, "oconv%s %s %s > %s", ocopts,
484 >                                                vval(MATERIAL), vval(SCENE),
485 >                                                vval(OCTREE));
486 >                        else
487 >                                sprintf(combuf, "oconv%s %s > %s", ocopts,
488 >                                                vval(SCENE), vval(OCTREE));
489 >                        
490 >                        if (runcom(combuf)) {           /* run it */
491 >                                fprintf(stderr,
492 >                                "%s: error generating octree\n\t%s removed\n",
493 >                                                progname, vval(OCTREE));
494 >                                unlink(vval(OCTREE));
495 >                                quit(1);
496 >                        }
497 >                }
498 >                octreedate = time((time_t *)NULL);
499 >                if (octreedate < scenedate)     /* in case clock is off */
500 >                        octreedate = scenedate;
501          }
502 <        if (noaction)
502 >        if (oct1name == vval(OCTREE))           /* no mkillum? */
503 >                oct1date = octreedate > matdate ? octreedate : matdate;
504 >        if ((oct1date >= octreedate) & (oct1date >= matdate)
505 >                        & (oct1date >= illumdate))      /* all done */
506                  return;
507 <        if (system(combuf)) {           /* run it */
508 <                fprintf(stderr, "%s: error generating octree\n\t%s removed\n",
509 <                                progname, vval(OCTREE));
510 <                unlink(vval(OCTREE));
511 <                exit(1);
507 >                                                /* make octree0 */
508 >        if ((oct0date < scenedate) | (oct0date < illumdate)) {
509 >                if (touchonly && oct0date)
510 >                        touch(oct0name);
511 >                else {                          /* build command */
512 >                        if (octreedate)
513 >                                sprintf(combuf, "oconv%s -i %s %s > %s", ocopts,
514 >                                        vval(OCTREE), vval(ILLUM), oct0name);
515 >                        else if (vdef(MATERIAL))
516 >                                sprintf(combuf, "oconv%s %s %s > %s", ocopts,
517 >                                        vval(MATERIAL), vval(ILLUM), oct0name);
518 >                        else
519 >                                sprintf(combuf, "oconv%s %s > %s", ocopts,
520 >                                        vval(ILLUM), oct0name);
521 >                        if (runcom(combuf)) {           /* run it */
522 >                                fprintf(stderr,
523 >                                "%s: error generating octree\n\t%s removed\n",
524 >                                                progname, oct0name);
525 >                                unlink(oct0name);
526 >                                quit(1);
527 >                        }
528 >                }
529 >                oct0date = time((time_t *)NULL);
530 >                if (oct0date < octreedate)      /* in case clock is off */
531 >                        oct0date = octreedate;
532 >                if (oct0date < illumdate)       /* ditto */
533 >                        oct0date = illumdate;
534 >                }
535 >        if (touchonly && oct1date)
536 >                touch(oct1name);
537 >        else {
538 >                mkillumopts(mkopts);            /* build mkillum command */
539 >                mktemp(illumtmp);
540 >                sprintf(combuf, "mkillum%s %s \"<\" %s > %s", mkopts,
541 >                                oct0name, vval(ILLUM), illumtmp);
542 >                if (runcom(combuf)) {                   /* run it */
543 >                        fprintf(stderr, "%s: error running mkillum\n",
544 >                                        progname);
545 >                        unlink(illumtmp);
546 >                        quit(1);
547 >                }
548 >                                                /* make octree1 (frozen) */
549 >                if (octreedate)
550 >                        sprintf(combuf, "oconv%s -f -i %s %s > %s", ocopts,
551 >                                vval(OCTREE), illumtmp, oct1name);
552 >                else if (vdef(MATERIAL))
553 >                        sprintf(combuf, "oconv%s -f %s %s > %s", ocopts,
554 >                                vval(MATERIAL), illumtmp, oct1name);
555 >                else
556 >                        sprintf(combuf, "oconv%s -f %s > %s", ocopts,
557 >                                illumtmp, oct1name);
558 >                if (runcom(combuf)) {           /* run it */
559 >                        fprintf(stderr,
560 >                                "%s: error generating octree\n\t%s removed\n",
561 >                                        progname, oct1name);
562 >                        unlink(oct1name);
563 >                        unlink(illumtmp);
564 >                        quit(1);
565 >                }
566 >                rmfile(illumtmp);
567          }
568 +        oct1date = time((time_t *)NULL);
569 +        if (oct1date < oct0date)        /* in case clock is off */
570 +                oct1date = oct0date;
571   }
572  
573  
574 < char *
575 < addarg(op, arg)                         /* add argument and advance pointer */
576 < register char   *op, *arg;
574 > static char *
575 > addarg(                         /* add argument and advance pointer */
576 > register char   *op,
577 > register char   *arg
578 > )
579   {
580          *op = ' ';
581 <        while (*++op = *arg++)
581 >        while ( (*++op = *arg++) )
582                  ;
583          return(op);
584   }
585  
586  
587 < oconvopts(oo)                           /* get oconv options */
588 < char    *oo;
587 > static void
588 > oconvopts(                              /* get oconv options */
589 >        register char   *oo
590 > )
591   {
592 +        /* BEWARE:  This may be called via setdefaults(), so no assumptions */
593 +
594          *oo = '\0';
595          if (vdef(OCONV))
596                  addarg(oo, vval(OCONV));
597   }
598  
599  
600 < lowqopts(ro)                            /* low quality rendering options */
601 < char    *ro;
600 > static void
601 > mkillumopts(                            /* get mkillum options */
602 >        register char   *mo
603 > )
604   {
605 <        register char   *op = ro;
605 >        /* BEWARE:  This may be called via setdefaults(), so no assumptions */
606  
607 +        *mo = '\0';
608 +        if (vdef(MKILLUM))
609 +                addarg(mo, vval(MKILLUM));
610 + }
611 +
612 +
613 + static void
614 + checkambfile(void)                      /* check date on ambient file */
615 + {
616 +        time_t  afdate;
617 +
618 +        if (!vdef(AMBFILE))
619 +                return;
620 +        if (!(afdate = fdate(vval(AMBFILE))))
621 +                return;
622 +        if (oct1date > afdate) {
623 +                if (touchonly)
624 +                        touch(vval(AMBFILE));
625 +                else
626 +                        rmfile(vval(AMBFILE));
627 +        }
628 + }
629 +
630 +
631 + static double
632 + ambval(void)                            /* compute ambient value */
633 + {
634 +        if (vdef(EXPOSURE)) {
635 +                if (vval(EXPOSURE)[0] == '+' || vval(EXPOSURE)[0] == '-')
636 +                        return(.5/pow(2.,vflt(EXPOSURE)));
637 +                return(.5/vflt(EXPOSURE));
638 +        }
639 +        if (vlet(ZONE) == 'E')
640 +                return(10.);
641 +        if (vlet(ZONE) == 'I')
642 +                return(.01);
643 +        badvalue(ZONE);
644 +        return 0; /* pro forma return */
645 + }
646 +
647 +
648 + static void
649 + renderopts(                     /* set rendering options */
650 +        char    *op,
651 +        char    *po
652 + )
653 + {
654 +        switch(vscale(QUALITY)) {
655 +        case LOW:
656 +                lowqopts(op, po);
657 +                break;
658 +        case MEDIUM:
659 +                medqopts(op, po);
660 +                break;
661 +        case HIGH:
662 +                hiqopts(op, po);
663 +                break;
664 +        }
665 + }
666 +
667 +
668 + static void
669 + lowqopts(                       /* low quality rendering options */
670 +        register char   *op,
671 +        char    *po
672 + )
673 + {
674 +        double  d, org[3], siz[3];
675 +
676          *op = '\0';
677 +        *po = '\0';
678 +        if (sscanf(vval(ZONE), "%*s %lf %lf %lf %lf %lf %lf", &org[0],
679 +                        &siz[0], &org[1], &siz[1], &org[2], &siz[2]) != 6)
680 +                badvalue(ZONE);
681 +        siz[0] -= org[0]; siz[1] -= org[1]; siz[2] -= org[2];
682 +        if ((siz[0] <= FTINY) | (siz[1] <= FTINY) | (siz[2] <= FTINY))
683 +                badvalue(ZONE);
684 +        getoctcube(org, &d);
685 +        d *= 3./(siz[0]+siz[1]+siz[2]);
686 +        switch (vscale(DETAIL)) {
687 +        case LOW:
688 +                po = addarg(po, "-ps 16");
689 +                op = addarg(op, "-dp 64");
690 +                sprintf(op, " -ar %d", (int)(8*d));
691 +                op += strlen(op);
692 +                break;
693 +        case MEDIUM:
694 +                po = addarg(po, "-ps 8");
695 +                op = addarg(op, "-dp 128");
696 +                sprintf(op, " -ar %d", (int)(16*d));
697 +                op += strlen(op);
698 +                break;
699 +        case HIGH:
700 +                po = addarg(po, "-ps 4");
701 +                op = addarg(op, "-dp 256");
702 +                sprintf(op, " -ar %d", (int)(32*d));
703 +                op += strlen(op);
704 +                break;
705 +        }
706 +        po = addarg(po, "-pt .16");
707 +        if (vbool(PENUMBRAS))
708 +                op = addarg(op, "-ds .4");
709 +        else
710 +                op = addarg(op, "-ds 0");
711 +        op = addarg(op, "-dt .2 -dc .25 -dr 0 -sj 0 -st .5");
712 +        if (vdef(AMBFILE)) {
713 +                sprintf(op, " -af %s", vval(AMBFILE));
714 +                op += strlen(op);
715 +        } else
716 +                overture = 0;
717 +        switch (vscale(VARIABILITY)) {
718 +        case LOW:
719 +                op = addarg(op, "-aa .3 -ad 256");
720 +                break;
721 +        case MEDIUM:
722 +                op = addarg(op, "-aa .25 -ad 512");
723 +                break;
724 +        case HIGH:
725 +                op = addarg(op, "-aa .2 -ad 1024");
726 +                break;
727 +        }
728 +        op = addarg(op, "-as 0");
729 +        d = ambval();
730 +        sprintf(op, " -av %.2g %.2g %.2g", d, d, d);
731 +        op += strlen(op);
732 +        op = addarg(op, "-lr 6 -lw .01");
733          if (vdef(RENDER))
734                  op = addarg(op, vval(RENDER));
735   }
736  
737  
738 < medqopts(ro)                            /* medium quality rendering options */
739 < char    *ro;
738 > static void
739 > medqopts(                       /* medium quality rendering options */
740 >        register char   *op,
741 >        char    *po
742 > )
743   {
744 <        register char   *op = ro;
744 >        double  d, org[3], siz[3], asz;
745  
746          *op = '\0';
747 +        *po = '\0';
748 +        if (sscanf(vval(ZONE), "%*s %lf %lf %lf %lf %lf %lf", &org[0],
749 +                        &siz[0], &org[1], &siz[1], &org[2], &siz[2]) != 6)
750 +                badvalue(ZONE);
751 +        siz[0] -= org[0]; siz[1] -= org[1]; siz[2] -= org[2];
752 +        if ((siz[0] <= FTINY) | (siz[1] <= FTINY) | (siz[2] <= FTINY))
753 +                badvalue(ZONE);
754 +        getoctcube(org, &d);
755 +        asz = (siz[0]+siz[1]+siz[2])/3.;
756 +        d /= asz;
757 +        switch (vscale(DETAIL)) {
758 +        case LOW:
759 +                po = addarg(po, vbool(PENUMBRAS) ? "-ps 4" : "-ps 8");
760 +                op = addarg(op, "-dp 256");
761 +                sprintf(op, " -ar %d", (int)(16*d));
762 +                op += strlen(op);
763 +                sprintf(op, " -ms %.2g", asz/20.);
764 +                op += strlen(op);
765 +                break;
766 +        case MEDIUM:
767 +                po = addarg(po, vbool(PENUMBRAS) ? "-ps 3" : "-ps 6");
768 +                op = addarg(op, "-dp 512");
769 +                sprintf(op, " -ar %d", (int)(32*d));
770 +                op += strlen(op);
771 +                sprintf(op, " -ms %.2g", asz/40.);
772 +                op += strlen(op);
773 +                break;
774 +        case HIGH:
775 +                po = addarg(po, vbool(PENUMBRAS) ? "-ps 2" : "-ps 4");
776 +                op = addarg(op, "-dp 1024");
777 +                sprintf(op, " -ar %d", (int)(64*d));
778 +                op += strlen(op);
779 +                sprintf(op, " -ms %.2g", asz/80.);
780 +                op += strlen(op);
781 +                break;
782 +        }
783 +        po = addarg(po, "-pt .08");
784 +        if (vbool(PENUMBRAS))
785 +                op = addarg(op, "-ds .2 -dj .5");
786 +        else
787 +                op = addarg(op, "-ds .3");
788 +        op = addarg(op, "-dt .1 -dc .5 -dr 1 -sj .7 -st .1");
789 +        if ( (overture = vint(INDIRECT)) ) {
790 +                sprintf(op, " -ab %d", overture);
791 +                op += strlen(op);
792 +        }
793 +        if (vdef(AMBFILE)) {
794 +                sprintf(op, " -af %s", vval(AMBFILE));
795 +                op += strlen(op);
796 +        } else
797 +                overture = 0;
798 +        switch (vscale(VARIABILITY)) {
799 +        case LOW:
800 +                op = addarg(op, "-aa .2 -ad 329 -as 42");
801 +                break;
802 +        case MEDIUM:
803 +                op = addarg(op, "-aa .15 -ad 800 -as 128");
804 +                break;
805 +        case HIGH:
806 +                op = addarg(op, "-aa .1 -ad 1536 -as 392");
807 +                break;
808 +        }
809 +        d = ambval();
810 +        sprintf(op, " -av %.2g %.2g %.2g", d, d, d);
811 +        op += strlen(op);
812 +        op = addarg(op, "-lr 8 -lw .002");
813          if (vdef(RENDER))
814                  op = addarg(op, vval(RENDER));
815   }
816  
817  
818 < hiqopts(ro)                             /* high quality rendering options */
819 < char    *ro;
818 > static void
819 > hiqopts(                                /* high quality rendering options */
820 >        register char   *op,
821 >        char    *po
822 > )
823   {
824 <        register char   *op = ro;
824 >        double  d, org[3], siz[3], asz;
825  
826          *op = '\0';
827 +        *po = '\0';
828 +        if (sscanf(vval(ZONE), "%*s %lf %lf %lf %lf %lf %lf", &org[0],
829 +                        &siz[0], &org[1], &siz[1], &org[2], &siz[2]) != 6)
830 +                badvalue(ZONE);
831 +        siz[0] -= org[0]; siz[1] -= org[1]; siz[2] -= org[2];
832 +        if ((siz[0] <= FTINY) | (siz[1] <= FTINY) | (siz[2] <= FTINY))
833 +                badvalue(ZONE);
834 +        getoctcube(org, &d);
835 +        asz = (siz[0]+siz[1]+siz[2])/3.;
836 +        d /= asz;
837 +        switch (vscale(DETAIL)) {
838 +        case LOW:
839 +                po = addarg(po, vbool(PENUMBRAS) ? "-ps 1" : "-ps 8");
840 +                op = addarg(op, "-dp 1024");
841 +                sprintf(op, " -ar %d", (int)(32*d));
842 +                op += strlen(op);
843 +                sprintf(op, " -ms %.2g", asz/40.);
844 +                op += strlen(op);
845 +                break;
846 +        case MEDIUM:
847 +                po = addarg(po, vbool(PENUMBRAS) ? "-ps 1" : "-ps 5");
848 +                op = addarg(op, "-dp 2048");
849 +                sprintf(op, " -ar %d", (int)(64*d));
850 +                op += strlen(op);
851 +                sprintf(op, " -ms %.2g", asz/80.);
852 +                op += strlen(op);
853 +                break;
854 +        case HIGH:
855 +                po = addarg(po, vbool(PENUMBRAS) ? "-ps 1" : "-ps 3");
856 +                op = addarg(op, "-dp 4096");
857 +                sprintf(op, " -ar %d", (int)(128*d));
858 +                op += strlen(op);
859 +                sprintf(op, " -ms %.2g", asz/160.);
860 +                op += strlen(op);
861 +                break;
862 +        }
863 +        po = addarg(po, "-pt .04");
864 +        if (vbool(PENUMBRAS))
865 +                op = addarg(op, "-ds .1 -dj .65");
866 +        else
867 +                op = addarg(op, "-ds .2");
868 +        op = addarg(op, "-dt .05 -dc .75 -dr 3 -sj 1 -st .01");
869 +        sprintf(op, " -ab %d", overture=vint(INDIRECT)+1);
870 +        op += strlen(op);
871 +        if (vdef(AMBFILE)) {
872 +                sprintf(op, " -af %s", vval(AMBFILE));
873 +                op += strlen(op);
874 +        } else
875 +                overture = 0;
876 +        switch (vscale(VARIABILITY)) {
877 +        case LOW:
878 +                op = addarg(op, "-aa .125 -ad 512 -as 64");
879 +                break;
880 +        case MEDIUM:
881 +                op = addarg(op, "-aa .1 -ad 1536 -as 768");
882 +                break;
883 +        case HIGH:
884 +                op = addarg(op, "-aa .075 -ad 4096 -as 2048");
885 +                break;
886 +        }
887 +        d = ambval();
888 +        sprintf(op, " -av %.2g %.2g %.2g", d, d, d);
889 +        op += strlen(op);
890 +        op = addarg(op, "-lr 12 -lw .0005");
891          if (vdef(RENDER))
892                  op = addarg(op, vval(RENDER));
893   }
894  
895  
896 < xferopts(ro)                            /* transfer options if indicated */
897 < char    *ro;
896 > static void
897 > xferopts(                               /* transfer options if indicated */
898 >        char    *ro
899 > )
900   {
901          int     fd, n;
902 +        register char   *cp;
903          
904          n = strlen(ro);
905          if (n < 2)
906                  return;
907          if (vdef(OPTFILE)) {
908 <                if ((fd = open(vval(OPTFILE), O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1) {
909 <                        perror(vval(OPTFILE));
910 <                        exit(1);
911 <                }
912 <                if (write(fd, ro+1, n-1) != n-1) {
913 <                        perror(vval(OPTFILE));
914 <                        exit(1);
915 <                }
916 <                write(fd, "\n", 1);
917 <                close(fd);
918 <                ro[0] = ' ';
602 <                ro[1] = '^';
603 <                strcpy(ro+2, vval(OPTFILE));
908 >                for (cp = ro; cp[1]; cp++)
909 >                        if (isspace(cp[1]) && (cp[2] == '@' ||
910 >                                        (cp[2] == '-' && isalpha(cp[3]))))
911 >                                *cp = '\n';
912 >                        else
913 >                                *cp = cp[1];
914 >                *cp = '\n';
915 >                fd = open(vval(OPTFILE), O_WRONLY|O_CREAT|O_TRUNC, 0666);
916 >                if (fd < 0 || write(fd, ro, n) != n || close(fd) < 0)
917 >                        syserr(vval(OPTFILE));
918 >                sprintf(ro, " @%s", vval(OPTFILE));
919          }
920 < #ifdef MSDOS
920 > #ifdef _WIN32
921          else if (n > 50) {
922 <                register char   *evp = bmalloc(n+6);
608 <                if (evp == NULL) {
609 <                        perror(progname);
610 <                        exit(1);
611 <                }
612 <                strcpy(evp, "ROPT=");
613 <                strcat(evp, ro);
614 <                putenv(evp);
922 >                setenv("ROPT", ro+1);
923                  strcpy(ro, " $ROPT");
924          }
925   #endif
926   }
927  
928  
929 < pfiltopts(po)                           /* get pfilt options */
930 < register char   *po;
929 > static void
930 > pfiltopts(                              /* get pfilt options */
931 >        register char   *po
932 > )
933   {
934          *po = '\0';
935          if (vdef(EXPOSURE)) {
936                  po = addarg(po, "-1 -e");
937                  po = addarg(po, vval(EXPOSURE));
938          }
939 <        if (vscale(QUALITY) == HIGH)
940 <                po = addarg(po, "-r .65");
939 >        switch (vscale(QUALITY)) {
940 >        case MEDIUM:
941 >                po = addarg(po, "-r .6");
942 >                break;
943 >        case HIGH:
944 >                po = addarg(po, "-m .25");
945 >                break;
946 >        }
947          if (vdef(PFILT))
948                  po = addarg(po, vval(PFILT));
949   }
950  
951  
952 < matchword(s1, s2)                       /* match white-delimited words */
953 < register char   *s1, *s2;
952 > static int
953 > matchword(                      /* match white-delimited words */
954 >        register char   *s1,
955 >        register char   *s2
956 > )
957   {
958          while (isspace(*s1)) s1++;
959          while (isspace(*s2)) s2++;
# Line 645 | Line 964 | register char  *s1, *s2;
964   }
965  
966  
967 < char *
968 < specview(vs)                            /* get proper view spec from vs */
969 < register char   *vs;
967 > static char *
968 > specview(                               /* get proper view spec from vs */
969 >        register char   *vs
970 > )
971   {
972 +        static char     vup[7][12] = {"-vu 0 0 -1","-vu 0 -1 0","-vu -1 0 0",
973 +                        "-vu 0 0 1", "-vu 1 0 0","-vu 0 1 0","-vu 0 0 1"};
974          static char     viewopts[128];
975          register char   *cp;
976 <        int     xpos, ypos, zpos, viewtype;
977 <        int     exterior;
976 >        int     xpos, ypos, zpos, viewtype, upax;
977 >        register int    i;
978          double  cent[3], dim[3], mult, d;
979  
980          if (vs == NULL || *vs == '-')
981                  return(vs);
982 +        upax = 0;                       /* get the up vector */
983 +        if (vdef(UP)) {
984 +                if (vval(UP)[0] == '-' || vval(UP)[0] == '+')
985 +                        upax = 1-'X'+UPPER(vval(UP)[1]);
986 +                else
987 +                        upax = 1-'X'+vlet(UP);
988 +                if ((upax < 1) | (upax > 3))
989 +                        badvalue(UP);
990 +                if (vval(UP)[0] == '-')
991 +                        upax = -upax;
992 +        }
993                                          /* check standard view names */
994          xpos = ypos = zpos = 0;
662        viewtype = 0;
995          if (*vs == 'X') {
996                  xpos = 1; vs++;
997          } else if (*vs == 'x') {
# Line 675 | Line 1007 | register char  *vs;
1007          } else if (*vs == 'z') {
1008                  zpos = -1; vs++;
1009          }
1010 <        if (*vs == 'v' | *vs == 'l' | *vs == 'a' | *vs == 'h')
1010 >        viewtype = 'v';
1011 >        if((*vs == 'v') | (*vs == 'l') | (*vs == 'a') | (*vs == 'h') | (*vs == 'c'))
1012                  viewtype = *vs++;
680        else if (!*vs || isspace(*vs))
681                viewtype = 'v';
1013          cp = viewopts;
1014 <        if (viewtype && (xpos|ypos|zpos)) {     /* got standard view */
1014 >        if ((!*vs || isspace(*vs)) && (xpos|ypos|zpos)) {       /* got one! */
1015                  *cp++ = '-'; *cp++ = 'v'; *cp++ = 't'; *cp++ = viewtype;
1016                  if (sscanf(vval(ZONE), "%*s %lf %lf %lf %lf %lf %lf",
1017                                  &cent[0], &dim[0], &cent[1], &dim[1],
1018 <                                &cent[2], &dim[2]) != 6) {
1019 <                        fprintf(stderr, "%s: bad zone specification\n",
1020 <                                        progname);
1021 <                        exit(1);
1018 >                                &cent[2], &dim[2]) != 6)
1019 >                        badvalue(ZONE);
1020 >                for (i = 0; i < 3; i++) {
1021 >                        dim[i] -= cent[i];
1022 >                        if (dim[i] <= FTINY)
1023 >                                badvalue(ZONE);
1024 >                        cent[i] += .5*dim[i];
1025                  }
1026 <                dim[0] -= cent[0];
693 <                dim[1] -= cent[1];
694 <                dim[2] -= cent[2];
695 <                exterior = vlet(ZONE) == 'E';
696 <                mult = exterior ? 2. : .45 ;
1026 >                mult = vlet(ZONE)=='E' ? 2. : .45 ;
1027                  sprintf(cp, " -vp %.2g %.2g %.2g -vd %.2g %.2g %.2g",
1028                                  cent[0]+xpos*mult*dim[0],
1029                                  cent[1]+ypos*mult*dim[1],
1030                                  cent[2]+zpos*mult*dim[2],
1031                                  -xpos*dim[0], -ypos*dim[1], -zpos*dim[2]);
1032                  cp += strlen(cp);
1033 <                switch (vlet(UP)) {
1034 <                case 'Z':
1035 <                        if (xpos|ypos) {
1036 <                                cp = addarg(cp, "-vu 0 0 1");
1037 <                                break;
1038 <                        }
1039 <                /* fall through */
710 <                case 'Y':
711 <                        if (xpos|zpos) {
712 <                                cp = addarg(cp, "-vu 0 1 0");
713 <                                break;
714 <                        }
715 <                /* fall through */
716 <                case 'X':
717 <                        if (ypos|zpos)
718 <                                cp = addarg(cp, "-vu 1 0 0");
719 <                        else
720 <                                cp = addarg(cp, "-vu 0 0 1");
1033 >                                        /* redirect up axis if necessary */
1034 >                switch (upax) {
1035 >                case 3:                 /* plus or minus Z axis */
1036 >                case -3:
1037 >                case 0:
1038 >                        if (!(xpos|ypos))
1039 >                                upax = 2;
1040                          break;
1041 <                default:
1042 <                        fprintf(stderr, "%s: illegal value for variable '%s'\n",
1043 <                                        progname, vnam(UP));
1044 <                        exit(1);
1041 >                case 2:                 /* plus or minus Y axis */
1042 >                case -2:
1043 >                        if (!(xpos|zpos))
1044 >                                upax = 1;
1045 >                        break;
1046 >                case 1:                 /* plus or minus X axis */
1047 >                case -1:
1048 >                        if (!(ypos|zpos))
1049 >                                upax = 3;
1050 >                        break;
1051                  }
1052 +                cp = addarg(cp, vup[upax+3]);
1053                  switch (viewtype) {
1054                  case 'v':
1055                          cp = addarg(cp, "-vh 45 -vv 45");
# Line 737 | Line 1063 | register char  *vs;
1063                  case 'h':
1064                          cp = addarg(cp, "-vh 180 -vv 180");
1065                          break;
1066 +                case 'c':
1067 +                        cp = addarg(cp, "-vh 180 -vv 90");
1068 +                        break;
1069                  }
1070 <        } else
1071 <                while (*vs && !isspace(*vs))    /* else skip id */
1072 <                        vs++;
1073 <                                        /* append any additional options */
1074 <        while (isspace(*vs)) vs++;
1070 >        } else {
1071 >                while (!isspace(*vs))           /* else skip id */
1072 >                        if (!*vs++)
1073 >                                return(NULL);
1074 >                if (upax) {                     /* specify up vector */
1075 >                        strcpy(cp, vup[upax+3]);
1076 >                        cp += strlen(cp);
1077 >                }
1078 >        }
1079 >        if (cp == viewopts)             /* append any additional options */
1080 >                vs++;           /* skip prefixed space if unneeded */
1081          strcpy(cp, vs);
1082 + #ifdef _WIN32
1083 +        if (strlen(viewopts) > 40) {
1084 +                setenv("VIEW", viewopts);
1085 +                return("$VIEW");
1086 +        }
1087 + #endif
1088          return(viewopts);
1089   }
1090  
1091  
1092 < char *
1093 < getview(n, vn)                          /* get view n, or NULL if none */
1094 < int     n;
1095 < char    *vn;
1092 > static char *
1093 > getview(                                /* get view n, or NULL if none */
1094 >        int     n,
1095 >        char    *vn             /* returned view name */
1096 > )
1097   {
1098          register char   *mv;
1099  
1100 <        if (viewselect != NULL) {
1100 >        if (viewselect != NULL) {               /* command-line selected */
1101                  if (n)                          /* only do one */
1102                          return(NULL);
1103                  if (viewselect[0] == '-') {     /* already specified */
# Line 768 | Line 1110 | char   *vn;
1110                                  ;
1111                          *vn = '\0';
1112                  }
1113 +                                                /* view number? */
1114 +                if (isint(viewselect))
1115 +                        return(specview(nvalue(VIEWS, atoi(viewselect)-1)));
1116                                                  /* check list */
1117 <                while ((mv = nvalue(vv+VIEW, n++)) != NULL)
1117 >                while ((mv = nvalue(VIEWS, n++)) != NULL)
1118                          if (matchword(viewselect, mv))
1119                                  return(specview(mv));
1120                  return(specview(viewselect));   /* standard view? */
1121          }
1122 <        if (vn != NULL && (mv = nvalue(vv+VIEW, n)) != NULL) {
1123 <                if (*mv != '-')
1124 <                        while (*mv && !isspace(*mv))
1125 <                                *vn++ = *mv++;
1122 >        mv = nvalue(VIEWS, n);          /* use view n */
1123 >        if ((vn != NULL) & (mv != NULL)) {
1124 >                register char   *mv2 = mv;
1125 >                if (*mv2 != '-')
1126 >                        while (*mv2 && !isspace(*mv2))
1127 >                                *vn++ = *mv2++;
1128                  *vn = '\0';
1129          }
1130 <        return(specview(nvalue(vv+VIEW, n)));   /* use view n */
1130 >        return(specview(mv));
1131   }
1132  
1133  
1134 < rview(opts)                             /* run rview with first view */
1135 < char    *opts;
1134 > static int
1135 > myprintview(                    /* print out selected view */
1136 >        register char   *vopts,
1137 >        FILE    *fp
1138 > )
1139   {
1140 <        char    combuf[512];
1140 >        VIEW    vwr;
1141 >        char    buf[128];
1142 >        register char   *cp;
1143 >        if (vopts == NULL)
1144 >                return(-1);
1145 > #ifdef _WIN32
1146 >        if (vopts[0] == '$') {
1147 >                vopts = getenv(vopts+1);
1148 >                goto again;
1149 >        }
1150 > #endif
1151 >        vwr = stdview;
1152 >        sscanview(&vwr, cp=vopts);              /* set initial options */
1153 >        while ((cp = strstr(cp, "-vf ")) != NULL &&
1154 >                        *atos(buf, sizeof(buf), cp += 4)) {
1155 >                viewfile(buf, &vwr, NULL);      /* load -vf file */
1156 >                sscanview(&vwr, cp);            /* reset tail */
1157 >        }
1158 >        fputs(VIEWSTR, fp);
1159 >        fprintview(&vwr, fp);                   /* print full spec. */
1160 >        fputc('\n', fp);
1161 >        return(0);
1162 > }
1163 >
1164 >
1165 > static void
1166 > rvu(                            /* run rvu with first view */
1167 >        char    *opts,
1168 >        char    *po
1169 > )
1170 > {
1171 >        char    *vw;
1172 >        char    combuf[PATH_MAX];
1173                                          /* build command */
1174 <        sprintf(combuf, "rview %s%s ", getview(0, NULL), opts);
1174 >        if (touchonly || (vw = getview(0, NULL)) == NULL)
1175 >                return;
1176 >        if (sayview)
1177 >                myprintview(vw, stdout);
1178 >        sprintf(combuf, "rvu %s%s%s -R %s ", vw, po, opts, rifname);
1179          if (rvdevice != NULL)
1180                  sprintf(combuf+strlen(combuf), "-o %s ", rvdevice);
1181 <        strcat(combuf, vval(OCTREE));
1182 <        if (!silent) {                  /* echo it */
1183 <                printf("\t%s\n", combuf);
1184 <                fflush(stdout);
1181 >        if (vdef(EXPOSURE))
1182 >                sprintf(combuf+strlen(combuf), "-pe %s ", vval(EXPOSURE));
1183 >        strcat(combuf, oct1name);
1184 >        if (runcom(combuf)) {           /* run it */
1185 >                fprintf(stderr, "%s: error running rvu\n", progname);
1186 >                quit(1);
1187          }
800        if (noaction)
801                return;
802        if (system(combuf)) {           /* run it */
803                fprintf(stderr, "%s: error running rview\n", progname);
804                exit(1);
805        }
1188   }
1189  
1190  
1191 < rpict(opts)                             /* run rpict and pfilt for each view */
1192 < char    *opts;
1191 > static void
1192 > rpict(                          /* run rpict and pfilt for each view */
1193 >        char    *opts,
1194 >        char    *po
1195 > )
1196   {
1197 <        char    combuf[512];
1198 <        char    rawfile[MAXPATH], picfile[MAXPATH], rep[MAXPATH], res[32];
1199 <        char    pfopts[64];
1197 >        char    combuf[PATH_MAX];
1198 >        char    rawfile[PATH_MAX], picfile[PATH_MAX];
1199 >        char    zopt[PATH_MAX+4], rep[PATH_MAX+16], res[32];
1200 >        char    rppopt[128], *pfile = NULL;
1201 >        char    pfopts[128];
1202          char    vs[32], *vw;
1203          int     vn, mult;
1204 +        FILE    *fp;
1205 +        time_t  rfdt, pfdt;
1206                                          /* get pfilt options */
1207          pfiltopts(pfopts);
1208                                          /* get resolution, reporting */
1209 <        mult = vscale(QUALITY)+1;
1209 >        switch (vscale(QUALITY)) {
1210 >        case LOW:
1211 >                mult = 1;
1212 >                break;
1213 >        case MEDIUM:
1214 >                mult = 2;
1215 >                break;
1216 >        case HIGH:
1217 >                mult = 3;
1218 >                break;
1219 >        }
1220          {
1221                  int     xres, yres;
1222                  double  aspect;
# Line 829 | Line 1228 | char   *opts;
1228                  else if (n) {
1229                          if (n == 1) yres = xres;
1230                          sprintf(res, "-x %d -y %d", mult*xres, mult*yres);
1231 <                } else {
1232 <                        fprintf(stderr, "%s: bad value for variable '%s'\n",
834 <                                        progname, vnam(RESOLUTION));
835 <                        exit(1);
836 <                }
1231 >                } else
1232 >                        badvalue(RESOLUTION);
1233          }
1234          rep[0] = '\0';
1235          if (vdef(REPORT)) {
# Line 844 | Line 1240 | char   *opts;
1240                          sprintf(rep, " -t %d -e %s", (int)(minutes*60), rawfile);
1241                  else if (n == 1)
1242                          sprintf(rep, " -t %d", (int)(minutes*60));
1243 <                else {
1244 <                        fprintf(stderr, "%s: bad value for variable '%s'\n",
849 <                                        progname, vnam(REPORT));
850 <                        exit(1);
851 <                }
1243 >                else
1244 >                        badvalue(REPORT);
1245          }
1246 <                                        /* do each view */
1247 <        vn = 0;
1246 >                                        /* set up parallel rendering */
1247 >        if ((nprocs > 1) & (!vdef(ZFILE))) {
1248 >                strcpy(rppopt, "-S 1 -PP pfXXXXXX");
1249 >                pfile = rppopt+9;
1250 >                if (mktemp(pfile) == NULL)
1251 >                        pfile = NULL;
1252 >        }
1253 >        vn = 0;                                 /* do each view */
1254          while ((vw = getview(vn++, vs)) != NULL) {
1255 +                if (sayview)
1256 +                        myprintview(vw, stdout);
1257                  if (!vs[0])
1258                          sprintf(vs, "%d", vn);
1259                  sprintf(picfile, "%s_%s.pic", vval(PICTURE), vs);
1260 +                if (vdef(ZFILE))
1261 +                        sprintf(zopt, " -z %s_%s.zbf", vval(ZFILE), vs);
1262 +                else
1263 +                        zopt[0] = '\0';
1264                                                  /* check date on picture */
1265 <                if (fdate(picfile) > octreedate)
1265 >                pfdt = fdate(picfile);
1266 >                if (pfdt >= oct1date)
1267                          continue;
1268 +                                                /* get raw file name */
1269 +                sprintf(rawfile, "%s_%s.unf",
1270 +                        vdef(RAWFILE) ? vval(RAWFILE) : vval(PICTURE), vs);
1271 +                rfdt = fdate(rawfile);
1272 +                if (touchonly) {                /* update times only */
1273 +                        if (rfdt) {
1274 +                                if (rfdt < oct1date)
1275 +                                        touch(rawfile);
1276 +                        } else if (pfdt && pfdt < oct1date)
1277 +                                touch(picfile);
1278 +                        continue;
1279 +                }
1280 +                if (next_process()) {           /* parallel running? */
1281 +                        if (pfile != NULL)
1282 +                                sleep(20);
1283 +                        continue;
1284 +                }
1285 +                /* XXX Remember to call finish_process() */
1286                                                  /* build rpict command */
1287 <                sprintf(rawfile, "%s_%s.raw", vval(PICTURE), vs);
1288 <                if (fdate(rawfile) > octreedate)        /* recover */
1289 <                        sprintf(combuf, "rpict%s%s -ro %s %s",
1290 <                                        rep, opts, rawfile, vval(OCTREE));
1291 <                else {
1287 >                if (rfdt >= oct1date) {         /* recover */
1288 >                        sprintf(combuf, "rpict%s%s%s%s -ro %s %s",
1289 >                                rep, po, opts, zopt, rawfile, oct1name);
1290 >                        if (runcom(combuf))     /* run rpict */
1291 >                                goto rperror;
1292 >                } else {
1293                          if (overture) {         /* run overture calculation */
1294                                  sprintf(combuf,
1295                                  "rpict%s %s%s -x 64 -y 64 -ps 1 %s > %s",
1296                                                  rep, vw, opts,
1297 <                                                vval(OCTREE), rawfile);
1298 <                                if (!silent) {
874 <                                        printf("\t%s\n", combuf);
875 <                                        fflush(stdout);
876 <                                }
877 <                                if (!noaction && system(combuf)) {
1297 >                                                oct1name, overfile);
1298 >                                if (runcom(combuf)) {
1299                                          fprintf(stderr,
1300 <                        "%s: error in overture for view %s\n\t%s removed\n",
1301 <                                                progname, vs, rawfile);
1302 <                                        unlink(rawfile);
882 <                                        exit(1);
1300 >                                        "%s: error in overture for view %s\n",
1301 >                                                progname, vs);
1302 >                                        quit(1);
1303                                  }
1304 + #ifndef NULL_DEVICE
1305 +                                rmfile(overfile);
1306 + #endif
1307                          }
1308 <                        sprintf(combuf, "rpict%s %s %s%s %s > %s",
1309 <                                        rep, vw, res, opts,
1310 <                                        vval(OCTREE), rawfile);
1308 >                        sprintf(combuf, "rpict%s %s %s%s%s%s %s > %s",
1309 >                                        rep, vw, res, po, opts,
1310 >                                        zopt, oct1name, rawfile);
1311 >                        if (pfile != NULL && inchild()) {
1312 >                                                /* rpict persistent mode */
1313 >                                if (!silent)
1314 >                                        printf("\t%s\n", combuf);
1315 >                                sprintf(combuf, "rpict%s %s %s%s%s %s > %s",
1316 >                                                rep, rppopt, res, po, opts,
1317 >                                                oct1name, rawfile);
1318 >                                fflush(stdout);
1319 >                                fp = popen(combuf, "w");
1320 >                                if (fp == NULL)
1321 >                                        goto rperror;
1322 >                                myprintview(vw, fp);
1323 >                                if (pclose(fp))
1324 >                                        goto rperror;
1325 >                        } else {                /* rpict normal mode */
1326 >                                if (runcom(combuf))
1327 >                                        goto rperror;
1328 >                        }
1329                  }
1330 <                if (!silent) {                  /* echo rpict command */
890 <                        printf("\t%s\n", combuf);
891 <                        fflush(stdout);
892 <                }
893 <                if (!noaction && system(combuf)) {      /* run rpict */
894 <                        fprintf(stderr, "%s: error rendering view %s\n",
895 <                                        progname, vs);
896 <                        exit(1);
897 <                }
1330 >                if (!vdef(RAWFILE) || strcmp(vval(RAWFILE),vval(PICTURE))) {
1331                                                  /* build pfilt command */
1332 <                if (mult > 1)
1333 <                        sprintf(combuf, "pfilt%s -x /%d -y /%d %s > %s",
1332 >                        if (mult > 1)
1333 >                                sprintf(combuf, "pfilt%s -x /%d -y /%d %s > %s",
1334                                          pfopts, mult, mult, rawfile, picfile);
1335 <                else
1336 <                        sprintf(combuf, "pfilt%s %s > %s", pfopts,
1337 <                                        rawfile, picfile);
1338 <                if (!silent) {                  /* echo pfilt command */
1339 <                        printf("\t%s\n", combuf);
1340 <                        fflush(stdout);
1335 >                        else
1336 >                                sprintf(combuf, "pfilt%s %s > %s", pfopts,
1337 >                                                rawfile, picfile);
1338 >                        if (runcom(combuf)) {   /* run pfilt */
1339 >                                fprintf(stderr,
1340 >                                "%s: error filtering view %s\n\t%s removed\n",
1341 >                                                progname, vs, picfile);
1342 >                                unlink(picfile);
1343 >                                quit(1);
1344 >                        }
1345                  }
1346 <                if (!noaction && system(combuf)) {      /* run pfilt */
1347 <                        fprintf(stderr,
1348 <                        "%s: error filtering view %s\n\t%s removed\n",
1349 <                                        progname, vs, picfile);
1350 <                        unlink(picfile);
1351 <                        exit(1);
1346 >                                                /* remove/rename raw file */
1347 >                if (vdef(RAWFILE)) {
1348 >                        sprintf(combuf, "%s_%s.pic", vval(RAWFILE), vs);
1349 >                        mvfile(rawfile, combuf);
1350 >                } else
1351 >                        rmfile(rawfile);
1352 >                finish_process();               /* exit if child */
1353 >        }
1354 >        wait_process(1);                /* wait for children to finish */
1355 >        if (pfile != NULL) {            /* clean up rpict persistent mode */
1356 >                int     pid;
1357 >                fp = fopen(pfile, "r");
1358 >                if (fp != NULL) {
1359 >                        if (fscanf(fp, "%*s %d", &pid) != 1 ||
1360 >                                        kill(pid, 1) < 0)
1361 >                                unlink(pfile);
1362 >                        fclose(fp);
1363                  }
1364 <                                                /* remove raw file */
1365 <                if (!silent)
1366 < #ifdef MSDOS
1367 <                        printf("\tdel %s\n", rawfile);
1368 < #else
1369 <                        printf("\trm %s\n", rawfile);
1364 >        }
1365 >        return;
1366 > rperror:
1367 >        fprintf(stderr, "%s: error rendering view %s\n", progname, vs);
1368 >        quit(1);
1369 > }
1370 >
1371 >
1372 > static int
1373 > touch(                  /* update a file */
1374 >        char    *fn
1375 > )
1376 > {
1377 >        if (!silent)
1378 >                printf("\ttouch %s\n", fn);
1379 >        if (!nprocs)
1380 >                return(0);
1381 > #ifdef notused
1382 >        if (access(fn, F_OK) == -1)             /* create it */
1383 >                if (close(open(fn, O_WRONLY|O_CREAT, 0666)) == -1)
1384 >                        return(-1);
1385   #endif
1386 <                if (!noaction)
1387 <                        unlink(rawfile);
1386 >        return(setfdate(fn, time((time_t *)NULL)));
1387 > }
1388 >
1389 >
1390 > static int
1391 > runcom(                 /* run command */
1392 >        char    *cs
1393 > )
1394 > {
1395 >        if (!silent)            /* echo it */
1396 >                printf("\t%s\n", cs);
1397 >        if (!nprocs)
1398 >                return(0);
1399 >        fflush(stdout);         /* flush output and pass to shell */
1400 >        return(system(cs));
1401 > }
1402 >
1403 >
1404 > static int
1405 > rmfile(                 /* remove a file */
1406 >        char    *fn
1407 > )
1408 > {
1409 >        if (!silent)
1410 >                printf("\t%s %s\n", DELCMD, fn);
1411 >        if (!nprocs)
1412 >                return(0);
1413 >        return(unlink(fn));
1414 > }
1415 >
1416 >
1417 > static int
1418 > mvfile(         /* move a file */
1419 >        char    *fold,
1420 >        char    *fnew
1421 > )
1422 > {
1423 >        if (!silent)
1424 >                printf("\t%s %s %s\n", RENAMECMD, fold, fnew);
1425 >        if (!nprocs)
1426 >                return(0);
1427 >        return(rename(fold, fnew));
1428 > }
1429 >
1430 >
1431 > #ifdef RHAS_FORK_EXEC
1432 > static int
1433 > next_process(void)                      /* fork the next process (max. nprocs) */
1434 > {
1435 >        int     child_pid;
1436 >
1437 >        if (nprocs <= 1)
1438 >                return(0);              /* it's us or no one */
1439 >        if (inchild()) {
1440 >                fprintf(stderr, "%s: internal error 1 in next_process()\n",
1441 >                                progname);
1442 >                quit(1);
1443          }
1444 +        if (children_running >= nprocs)
1445 +                wait_process(0);        /* wait for someone to finish */
1446 +        fflush(stdout);
1447 +        child_pid = fork();             /* split process */
1448 +        if (child_pid == 0) {           /* we're the child */
1449 +                children_running = -1;
1450 +                return(0);
1451 +        }
1452 +        if (child_pid > 0) {            /* we're the parent */
1453 +                ++children_running;
1454 +                return(1);
1455 +        }
1456 +        fprintf(stderr, "%s: warning -- fork() failed\n", progname);
1457 +        return(0);
1458 + }
1459 +
1460 + static void
1461 + wait_process(                   /* wait for process(es) to finish */
1462 +        int     all
1463 + )
1464 + {
1465 +        int     ourstatus = 0;
1466 +        int     pid, status;
1467 +
1468 +        if (all)
1469 +                all = children_running;
1470 +        else if (children_running > 0)
1471 +                all = 1;
1472 +        while (all-- > 0) {
1473 +                pid = wait(&status);
1474 +                if (pid < 0)
1475 +                        syserr(progname);
1476 +                status = status>>8 & 0xff;
1477 +                --children_running;
1478 +                if (status != 0) {      /* child's problem is our problem */
1479 +                        if ((ourstatus == 0) & (children_running > 0))
1480 +                                fprintf(stderr, "%s: waiting for remaining processes\n",
1481 +                                                progname);
1482 +                        ourstatus = status;
1483 +                        all = children_running;
1484 +                }
1485 +        }
1486 +        if (ourstatus != 0)
1487 +                quit(ourstatus);        /* bad status from child */
1488 + }
1489 + #else   /* ! RHAS_FORK_EXEC */
1490 + static int
1491 + next_process(void)
1492 + {
1493 +        return(0);                      /* cannot start new process */
1494 + }
1495 + static void
1496 + wait_process(all)
1497 + int     all;
1498 + {
1499 +        (void)all;                      /* no one to wait for */
1500 + }
1501 + int
1502 + kill(pid, sig) /* win|unix_process.c should also wait and kill */
1503 + int pid, sig;
1504 + {
1505 +        return 0;
1506 + }
1507 + #endif  /* ! RHAS_FORK_EXEC */
1508 +
1509 + static void
1510 + finish_process(void)                    /* exit a child process */
1511 + {
1512 +        if (!inchild())
1513 +                return;                 /* in parent -- noop */
1514 +        exit(0);
1515 + }
1516 +
1517 + #ifdef _WIN32
1518 + setenv(vname, value)            /* set an environment variable */
1519 + char    *vname, *value;
1520 + {
1521 +        register char   *evp;
1522 +
1523 +        evp = bmalloc(strlen(vname)+strlen(value)+2);
1524 +        if (evp == NULL)
1525 +                syserr(progname);
1526 +        sprintf(evp, "%s=%s", vname, value);
1527 +        if (putenv(evp) != 0) {
1528 +                fprintf(stderr, "%s: out of environment space\n", progname);
1529 +                quit(1);
1530 +        }
1531 +        if (!silent)
1532 +                printf("set %s\n", evp);
1533 + }
1534 + #endif
1535 +
1536 +
1537 + static void
1538 + badvalue(                       /* report bad variable value and exit */
1539 +        int     vc
1540 + )
1541 + {
1542 +        fprintf(stderr, "%s: bad value for variable '%s'\n",
1543 +                        progname, vnam(vc));
1544 +        quit(1);
1545 + }
1546 +
1547 +
1548 + static void
1549 + syserr(                 /* report a system error and exit */
1550 +        char    *s
1551 + )
1552 + {
1553 +        perror(s);
1554 +        quit(1);
1555 + }
1556 +
1557 +
1558 + void
1559 + quit(ec)                        /* exit program */
1560 + int     ec;
1561 + {
1562 +        exit(ec);
1563   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines