ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/util/ranimate.c
Revision: 2.29
Committed: Sat Feb 22 02:07:30 2003 UTC (21 years, 8 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.28: +135 -22 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

# User Rev Content
1 greg 2.1 #ifndef lint
2 greg 2.29 static const char RCSid[] = "$Id$";
3 greg 2.1 #endif
4     /*
5     * Radiance animation control program
6 greg 2.29 *
7     * The main difference between this program and ranimove is that
8     * we have many optimizations here for camera motion in static
9     * environments, calling rpict and pinterp on multiple processors,
10     * where ranimove puts its emphasis on object motion, and does
11     * not use any external programs for image generation.
12     *
13     * See the ranimate(1) man page for further details.
14     */
15    
16     /* ====================================================================
17     * The Radiance Software License, Version 1.0
18     *
19     * Copyright (c) 1990 - 2002 The Regents of the University of California,
20     * through Lawrence Berkeley National Laboratory. All rights reserved.
21     *
22     * Redistribution and use in source and binary forms, with or without
23     * modification, are permitted provided that the following conditions
24     * are met:
25     *
26     * 1. Redistributions of source code must retain the above copyright
27     * notice, this list of conditions and the following disclaimer.
28     *
29     * 2. Redistributions in binary form must reproduce the above copyright
30     * notice, this list of conditions and the following disclaimer in
31     * the documentation and/or other materials provided with the
32     * distribution.
33     *
34     * 3. The end-user documentation included with the redistribution,
35     * if any, must include the following acknowledgment:
36     * "This product includes Radiance software
37     * (http://radsite.lbl.gov/)
38     * developed by the Lawrence Berkeley National Laboratory
39     * (http://www.lbl.gov/)."
40     * Alternately, this acknowledgment may appear in the software itself,
41     * if and wherever such third-party acknowledgments normally appear.
42     *
43     * 4. The names "Radiance," "Lawrence Berkeley National Laboratory"
44     * and "The Regents of the University of California" must
45     * not be used to endorse or promote products derived from this
46     * software without prior written permission. For written
47     * permission, please contact [email protected].
48     *
49     * 5. Products derived from this software may not be called "Radiance",
50     * nor may "Radiance" appear in their name, without prior written
51     * permission of Lawrence Berkeley National Laboratory.
52     *
53     * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
54     * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
55     * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
56     * DISCLAIMED. IN NO EVENT SHALL Lawrence Berkeley National Laboratory OR
57     * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
58     * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
59     * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
60     * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
61     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
62     * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
63     * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
64     * SUCH DAMAGE.
65     * ====================================================================
66     *
67     * This software consists of voluntary contributions made by many
68     * individuals on behalf of Lawrence Berkeley National Laboratory. For more
69     * information on Lawrence Berkeley National Laboratory, please see
70     * <http://www.lbl.gov/>.
71 greg 2.1 */
72    
73     #include "standard.h"
74 greg 2.2 #include <ctype.h>
75 greg 2.1 #include <sys/stat.h>
76     #include "view.h"
77     #include "vars.h"
78 greg 2.2 #include "netproc.h"
79 greg 2.29 /* default blur samples */
80     #ifndef DEF_NBLUR
81     #define DEF_NBLUR 5
82     #endif
83 greg 2.10 /* default remote shell */
84     #ifdef _AUX_SOURCE
85     #define REMSH "remsh"
86     #else
87     #define REMSH "rsh"
88     #endif
89 gwlarson 2.27 /* input variables (alphabetical by name) */
90     #define ANIMATE 0 /* animation command */
91     #define ARCHIVE 1 /* archiving command */
92     #define BASENAME 2 /* output image base name */
93     #define DIRECTORY 3 /* working (sub)directory */
94     #define DISKSPACE 4 /* how much disk space to use */
95     #define END 5 /* ending frame number */
96     #define EXPOSURE 6 /* how to compute exposure */
97     #define HOST 7 /* rendering host machine */
98     #define INTERP 8 /* # frames to interpolate */
99 greg 2.29 #define MBLUR 9 /* motion blur parameters */
100 gwlarson 2.27 #define NEXTANIM 10 /* next animation file */
101     #define OCTREE 11 /* octree file name */
102     #define OVERSAMP 12 /* # times to oversample image */
103     #define PFILT 13 /* pfilt options */
104     #define PINTERP 14 /* pinterp options */
105     #define RENDER 15 /* rendering options */
106     #define RESOLUTION 16 /* desired final resolution */
107     #define RIF 17 /* rad input file */
108     #define RSH 18 /* remote shell script or program */
109     #define RTRACE 19 /* use rtrace with pinterp? */
110     #define START 20 /* starting frame number */
111     #define TRANSFER 21 /* frame transfer command */
112     #define VIEWFILE 22 /* animation frame views */
113 greg 2.1
114 greg 2.10 int NVARS = 23; /* total number of variables */
115 greg 2.1
116     VARIABLE vv[] = { /* variable-value pairs */
117 gwlarson 2.27 {"ANIMATE", 2, 0, NULL, onevalue},
118     {"ARCHIVE", 2, 0, NULL, onevalue},
119     {"BASENAME", 3, 0, NULL, onevalue},
120 greg 2.1 {"DIRECTORY", 3, 0, NULL, onevalue},
121 gwlarson 2.27 {"DISKSPACE", 3, 0, NULL, fltvalue},
122 greg 2.1 {"END", 2, 0, NULL, intvalue},
123 gwlarson 2.27 {"EXPOSURE", 3, 0, NULL, onevalue},
124     {"host", 4, 0, NULL, NULL},
125     {"INTERPOLATE", 3, 0, NULL, intvalue},
126     {"MBLUR", 2, 0, NULL, onevalue},
127 greg 2.1 {"NEXTANIM", 3, 0, NULL, onevalue},
128 gwlarson 2.27 {"OCTREE", 3, 0, NULL, onevalue},
129 greg 2.5 {"OVERSAMPLE", 2, 0, NULL, fltvalue},
130 gwlarson 2.27 {"pfilt", 2, 0, NULL, catvalues},
131     {"pinterp", 2, 0, NULL, catvalues},
132     {"render", 3, 0, NULL, catvalues},
133 greg 2.1 {"RESOLUTION", 3, 0, NULL, onevalue},
134 gwlarson 2.27 {"RIF", 3, 0, NULL, onevalue},
135 greg 2.10 {"RSH", 3, 0, NULL, onevalue},
136 gwlarson 2.27 {"RTRACE", 2, 0, NULL, boolvalue},
137     {"START", 2, 0, NULL, intvalue},
138     {"TRANSFER", 2, 0, NULL, onevalue},
139     {"VIEWFILE", 2, 0, NULL, onevalue},
140 greg 2.1 };
141    
142     #define SFNAME "STATUS" /* status file name */
143    
144     struct {
145     char host[64]; /* control host name */
146     int pid; /* control process id */
147     char cfname[128]; /* control file name */
148     int rnext; /* next frame to render */
149     int fnext; /* next frame to filter */
150     int tnext; /* next frame to transfer */
151     } astat; /* animation status */
152    
153     char *progname; /* our program name */
154     char *cfname; /* our control file name */
155    
156     int nowarn = 0; /* turn warnings off? */
157     int silent = 0; /* silent mode? */
158     int noaction = 0; /* take no action? */
159    
160 greg 2.10 char *remsh; /* remote shell program/script */
161 greg 2.21 char rendopt[2048]; /* rendering options */
162 greg 2.1 char rresopt[32]; /* rendering resolution options */
163     char fresopt[32]; /* filter resolution options */
164     int pfiltalways; /* always use pfilt? */
165    
166 greg 2.7 char arcargs[10240]; /* files to archive */
167     char *arcfirst, *arcnext; /* pointers to first and next argument */
168    
169 greg 2.2 struct pslot {
170     int pid; /* process ID (0 if empty) */
171     int fout; /* output frame number */
172     int (*rcvf)(); /* recover function */
173     } *pslot; /* process slots */
174     int npslots; /* number of process slots */
175    
176 greg 2.4 #define phostname(ps) ((ps)->hostname[0] ? (ps)->hostname : astat.host)
177    
178 greg 2.2 struct pslot *findpslot();
179    
180 greg 2.18 PSERVER *lastpserver; /* last process server with error */
181    
182 greg 2.1 VIEW *getview();
183 gwlarson 2.25 char *getexp(), *dirfile();
184 greg 2.29 int getblur();
185 greg 2.1
186 greg 2.29 extern time_t time();
187 greg 2.1
188 greg 2.12
189 greg 2.1 main(argc, argv)
190     int argc;
191     char *argv[];
192     {
193     int explicate = 0;
194     int i;
195    
196     progname = argv[0]; /* get arguments */
197     for (i = 1; i < argc && argv[i][0] == '-'; i++)
198     switch (argv[i][1]) {
199     case 'e': /* print variables */
200     explicate++;
201     break;
202     case 'w': /* turn off warnings */
203     nowarn++;
204     break;
205     case 's': /* silent mode */
206     silent++;
207     break;
208     case 'n': /* take no action */
209     noaction++;
210     break;
211     default:
212     goto userr;
213     }
214     if (i != argc-1)
215     goto userr;
216     cfname = argv[i];
217     /* load variables */
218     loadvars(cfname);
219 greg 2.22 /* check variables */
220     checkvalues();
221 greg 2.1 /* did we get DIRECTORY? */
222     checkdir();
223     /* check status */
224     if (getastat() < 0) {
225     fprintf(stderr, "%s: exiting\n", progname);
226     quit(1);
227     }
228     /* pfilt always if options given */
229     pfiltalways = vdef(PFILT);
230     /* load RIF if any */
231     if (vdef(RIF))
232     getradfile(vval(RIF));
233     /* set defaults */
234     setdefaults();
235     /* print variables */
236     if (explicate)
237     printvars(stdout);
238 greg 2.2 /* set up process servers */
239     sethosts();
240 greg 2.1 /* run animation */
241     animate();
242     /* all done */
243     if (vdef(NEXTANIM)) {
244     argv[i] = vval(NEXTANIM); /* just change input file */
245     if (!silent)
246     printargs(argc, argv, stdout);
247 greg 2.11 if ((argv[0] = getpath(progname,getenv("PATH"),X_OK)) == NULL)
248     fprintf(stderr, "%s: command not found\n", progname);
249     else
250     execv(progname, argv);
251     quit(1);
252 greg 2.1 }
253     quit(0);
254     userr:
255     fprintf(stderr, "Usage: %s [-s][-n][-w][-e] anim_file\n", progname);
256     quit(1);
257     }
258    
259    
260     getastat() /* check/set animation status */
261     {
262 greg 2.12 char sfname[256];
263 greg 2.1 FILE *fp;
264    
265 greg 2.12 sprintf(sfname, "%s/%s", vval(DIRECTORY), SFNAME);
266     if ((fp = fopen(sfname, "r")) == NULL) {
267 greg 2.1 if (errno != ENOENT) {
268 greg 2.12 perror(sfname);
269 greg 2.1 return(-1);
270     }
271     astat.rnext = astat.fnext = astat.tnext = 0;
272     goto setours;
273     }
274     if (fscanf(fp, "Control host: %s\n", astat.host) != 1)
275     goto fmterr;
276     if (fscanf(fp, "Control PID: %d\n", &astat.pid) != 1)
277     goto fmterr;
278     if (fscanf(fp, "Control file: %s\n", astat.cfname) != 1)
279     goto fmterr;
280     if (fscanf(fp, "Next render: %d\n", &astat.rnext) != 1)
281     goto fmterr;
282     if (fscanf(fp, "Next filter: %d\n", &astat.fnext) != 1)
283     goto fmterr;
284     if (fscanf(fp, "Next transfer: %d\n", &astat.tnext) != 1)
285     goto fmterr;
286     fclose(fp);
287     if (astat.pid != 0) { /* thinks it's still running */
288 greg 2.9 if (strcmp(myhostname(), astat.host)) {
289 greg 2.1 fprintf(stderr,
290     "%s: process %d may still be running on host %s\n",
291     progname, astat.pid, astat.host);
292     return(-1);
293     }
294     if (kill(astat.pid, 0) != -1 || errno != ESRCH) {
295     fprintf(stderr, "%s: process %d is still running\n",
296     progname, astat.pid);
297     return(-1);
298     }
299     /* assume it is dead */
300     }
301 greg 2.20 if (strcmp(cfname, astat.cfname) && astat.pid != 0) { /* other's */
302 greg 2.1 fprintf(stderr, "%s: unfinished job \"%s\"\n",
303     progname, astat.cfname);
304     return(-1);
305     }
306 greg 2.12 /* check control file mods. */
307     if (!nowarn && fdate(cfname) > fdate(sfname))
308     fprintf(stderr,
309     "%s: warning - control file modified since last run\n",
310     progname);
311 greg 2.1 setours: /* set our values */
312 greg 2.9 strcpy(astat.host, myhostname());
313 greg 2.1 astat.pid = getpid();
314     strcpy(astat.cfname, cfname);
315     return(0);
316     fmterr:
317     fprintf(stderr, "%s: format error in status file \"%s\"\n",
318 greg 2.12 progname, sfname);
319 greg 2.1 fclose(fp);
320     return(-1);
321     }
322    
323    
324     putastat() /* put out current status */
325     {
326     char buf[256];
327     FILE *fp;
328    
329 greg 2.2 if (noaction)
330     return;
331 greg 2.1 sprintf(buf, "%s/%s", vval(DIRECTORY), SFNAME);
332     if ((fp = fopen(buf, "w")) == NULL) {
333     perror(buf);
334     quit(1);
335     }
336     fprintf(fp, "Control host: %s\n", astat.host);
337     fprintf(fp, "Control PID: %d\n", astat.pid);
338     fprintf(fp, "Control file: %s\n", astat.cfname);
339     fprintf(fp, "Next render: %d\n", astat.rnext);
340     fprintf(fp, "Next filter: %d\n", astat.fnext);
341     fprintf(fp, "Next transfer: %d\n", astat.tnext);
342     fclose(fp);
343     }
344    
345    
346     checkdir() /* make sure we have our directory */
347     {
348     struct stat stb;
349    
350     if (!vdef(DIRECTORY)) {
351     fprintf(stderr, "%s: %s undefined\n",
352     progname, vnam(DIRECTORY));
353     quit(1);
354     }
355     if (stat(vval(DIRECTORY), &stb) == -1) {
356     if (errno == ENOENT && mkdir(vval(DIRECTORY), 0777) == 0)
357     return;
358     perror(vval(DIRECTORY));
359     quit(1);
360     }
361     if (!(stb.st_mode & S_IFDIR)) {
362     fprintf(stderr, "%s: not a directory\n", vval(DIRECTORY));
363     quit(1);
364     }
365     }
366    
367    
368     setdefaults() /* set default values */
369     {
370 greg 2.10 extern char *atos();
371 greg 2.19 int decades;
372 greg 2.1 char buf[256];
373    
374 greg 2.2 if (vdef(ANIMATE)) {
375     vval(OCTREE) = NULL;
376     vdef(OCTREE) = 0;
377     } else if (!vdef(OCTREE)) {
378 greg 2.1 fprintf(stderr, "%s: either %s or %s must be defined\n",
379     progname, vnam(OCTREE), vnam(ANIMATE));
380     quit(1);
381     }
382     if (!vdef(VIEWFILE)) {
383     fprintf(stderr, "%s: %s undefined\n", progname, vnam(VIEWFILE));
384     quit(1);
385     }
386 greg 2.2 if (!vdef(HOST)) {
387     vval(HOST) = LHOSTNAME;
388     vdef(HOST)++;
389     }
390 greg 2.1 if (!vdef(START)) {
391     vval(START) = "1";
392     vdef(START)++;
393     }
394     if (!vdef(END)) {
395 greg 2.5 sprintf(buf, "%d", countviews()+vint(START)-1);
396 greg 2.1 vval(END) = savqstr(buf);
397     vdef(END)++;
398     }
399 greg 2.5 if (vint(END) < vint(START)) {
400     fprintf(stderr, "%s: ending frame less than starting frame\n",
401     progname);
402     quit(1);
403     }
404 greg 2.1 if (!vdef(BASENAME)) {
405 greg 2.19 decades = (int)log10((double)vint(END)) + 1;
406     if (decades < 3) decades = 3;
407     sprintf(buf, "%s/frame%%0%dd", vval(DIRECTORY), decades);
408 greg 2.1 vval(BASENAME) = savqstr(buf);
409     vdef(BASENAME)++;
410     }
411     if (!vdef(RESOLUTION)) {
412     vval(RESOLUTION) = "640";
413     vdef(RESOLUTION)++;
414     }
415     if (!vdef(OVERSAMP)) {
416     vval(OVERSAMP) = "2";
417     vdef(OVERSAMP)++;
418     }
419     if (!vdef(INTERP)) {
420     vval(INTERP) = "0";
421     vdef(INTERP)++;
422     }
423     if (!vdef(MBLUR)) {
424     vval(MBLUR) = "0";
425     vdef(MBLUR)++;
426     }
427     if (!vdef(RTRACE)) {
428     vval(RTRACE) = "F";
429     vdef(RTRACE)++;
430     }
431     if (!vdef(DISKSPACE)) {
432     if (!nowarn)
433     fprintf(stderr,
434     "%s: warning - no %s setting, assuming 100 Mbytes available\n",
435     progname, vnam(DISKSPACE));
436     vval(DISKSPACE) = "100";
437     vdef(DISKSPACE)++;
438     }
439 greg 2.10 if (!vdef(RSH)) {
440     vval(RSH) = REMSH;
441     vdef(RSH)++;
442     }
443     /* locate remote shell program */
444     atos(buf, sizeof(buf), vval(RSH));
445     if ((remsh = getpath(buf, getenv("PATH"), X_OK)) != NULL)
446     remsh = savqstr(remsh);
447     else
448     remsh = vval(RSH); /* will generate error if used */
449    
450 greg 2.1 /* append rendering options */
451     if (vdef(RENDER))
452     sprintf(rendopt+strlen(rendopt), " %s", vval(RENDER));
453     }
454    
455    
456 greg 2.2 sethosts() /* set up process servers */
457     {
458     extern char *iskip();
459     char buf[256], *dir, *uname;
460     int np;
461     register char *cp;
462     int i;
463    
464     npslots = 0;
465     if (noaction)
466     return;
467     for (i = 0; i < vdef(HOST); i++) { /* add each host */
468     dir = uname = NULL;
469     np = 1;
470     strcpy(cp=buf, nvalue(HOST, i)); /* copy to buffer */
471     cp = sskip(cp); /* skip host name */
472     while (isspace(*cp))
473     *cp++ = '\0';
474     if (*cp) { /* has # processes? */
475     np = atoi(cp);
476     if ((cp = iskip(cp)) == NULL || (*cp && !isspace(*cp)))
477     badvalue(HOST);
478     while (isspace(*cp))
479     cp++;
480     if (*cp) { /* has directory? */
481     dir = cp;
482     cp = sskip(cp); /* skip dir. */
483     while (isspace(*cp))
484     *cp++ = '\0';
485     if (*cp) { /* has user? */
486     uname = cp;
487     if (*sskip(cp))
488     badvalue(HOST);
489     }
490     }
491     }
492     if (addpserver(buf, dir, uname, np) == NULL) {
493     if (!nowarn)
494     fprintf(stderr,
495     "%s: cannot execute on host \"%s\"\n",
496     progname, buf);
497     } else
498     npslots += np;
499     }
500     if (npslots == 0) {
501     fprintf(stderr, "%s: no working process servers\n", progname);
502     quit(1);
503     }
504     pslot = (struct pslot *)calloc(npslots, sizeof(struct pslot));
505     if (pslot == NULL) {
506     perror("malloc");
507     quit(1);
508     }
509     }
510    
511    
512 greg 2.7 getradfile(rfargs) /* run rad and get needed variables */
513     char *rfargs;
514 greg 2.1 {
515     static short mvar[] = {OCTREE,PFILT,RESOLUTION,EXPOSURE,-1};
516     char combuf[256];
517     register int i;
518     register char *cp;
519 gregl 2.23 char *pippt;
520 greg 2.1 /* create rad command */
521     sprintf(rendopt, " @%s/render.opt", vval(DIRECTORY));
522     sprintf(combuf,
523     "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH",
524 greg 2.7 rfargs, rendopt+2);
525 greg 2.1 cp = combuf;
526 gregl 2.23 while (*cp) {
527     if (*cp == '|') pippt = cp;
528     cp++;
529     } /* match unset variables */
530 greg 2.1 for (i = 0; mvar[i] >= 0; i++)
531     if (!vdef(mvar[i])) {
532     *cp++ = '|';
533     strcpy(cp, vnam(mvar[i]));
534     while (*cp) cp++;
535 gregl 2.23 pippt = NULL;
536 greg 2.1 }
537 gregl 2.23 if (pippt != NULL)
538     strcpy(pippt, "> /dev/null"); /* nothing to match */
539     else {
540     sprintf(cp, ")[ \t]*=' > %s/radset.var", vval(DIRECTORY));
541     cp += 11; /* point to file name */
542     }
543 gwlarson 2.28 system(combuf); /* ignore exit code */
544 gregl 2.23 if (pippt == NULL) { /* load variables and remove file */
545     loadvars(cp);
546     unlink(cp);
547     }
548 greg 2.1 }
549    
550    
551     animate() /* run animation */
552     {
553     int xres, yres;
554     float pa, mult;
555     int frames_batch;
556     register int i;
557     double d1, d2;
558     /* compute rpict resolution */
559     i = sscanf(vval(RESOLUTION), "%d %d %f", &xres, &yres, &pa);
560     mult = vflt(OVERSAMP);
561     if (i == 3) {
562 greg 2.16 sprintf(rresopt, "-x %d -y %d -pa %.3f", (int)(mult*xres),
563 greg 2.1 (int)(mult*yres), pa);
564 greg 2.16 sprintf(fresopt, "-x %d -y %d -pa %.3f", xres, yres, pa);
565 greg 2.1 } else if (i) {
566     if (i == 1) yres = xres;
567     sprintf(rresopt, "-x %d -y %d", (int)(mult*xres),
568     (int)(mult*yres));
569     sprintf(fresopt, "-x %d -y %d -pa 1", xres, yres);
570     } else
571     badvalue(RESOLUTION);
572     /* consistency checks */
573     if (vdef(ANIMATE)) {
574     if (vint(INTERP)) {
575     if (!nowarn)
576     fprintf(stderr,
577     "%s: resetting %s=0 for animation\n",
578     progname, vnam(INTERP));
579     vval(INTERP) = "0";
580     }
581 greg 2.29 if (strcmp(vval(MBLUR),"0")) { /* can't handle this */
582 greg 2.1 if (!nowarn)
583     fprintf(stderr,
584     "%s: resetting %s=0 for animation\n",
585     progname, vnam(MBLUR));
586     vval(MBLUR) = "0";
587     }
588     }
589     /* figure # frames per batch */
590     d1 = mult*xres*mult*yres*4; /* space for orig. picture */
591 greg 2.29 if ((i=vint(INTERP)) || getblur(NULL) > 1)
592 greg 2.13 d1 += mult*xres*mult*yres*sizeof(float); /* Z-buffer */
593 greg 2.1 d2 = xres*yres*4; /* space for final picture */
594     frames_batch = (i+1)*(vflt(DISKSPACE)*1048576.-d1)/(d1+i*d2);
595     if (frames_batch < i+2) {
596     fprintf(stderr, "%s: insufficient disk space allocated\n",
597     progname);
598     quit(1);
599     }
600 greg 2.7 /* initialize archive argument list */
601 gwlarson 2.25 i = vdef(ARCHIVE) ? strlen(vval(ARCHIVE))+132 : 132;
602 greg 2.7 arcnext = arcfirst = arcargs + i;
603 greg 2.1 /* initialize status file */
604     if (astat.rnext == 0)
605     astat.rnext = astat.fnext = astat.tnext = vint(START);
606     putastat();
607     /* render in batches */
608 greg 2.2 while (astat.tnext <= vint(END)) {
609 greg 2.1 renderframes(frames_batch);
610     filterframes();
611     transferframes();
612     }
613     /* mark status as finished */
614     astat.pid = 0;
615     putastat();
616     /* close open files */
617     getview(0);
618     getexp(0);
619     }
620    
621    
622     renderframes(nframes) /* render next nframes frames */
623     int nframes;
624     {
625     static char vendbuf[16];
626     VIEW *vp;
627     FILE *fp = NULL;
628     char vfname[128];
629     int lastframe;
630     register int i;
631    
632     if (astat.tnext < astat.rnext) /* other work to do first */
633     return;
634     /* create batch view file */
635     if (!vdef(ANIMATE)) {
636     sprintf(vfname, "%s/anim.vf", vval(DIRECTORY));
637     if ((fp = fopen(vfname, "w")) == NULL) {
638     perror(vfname);
639     quit(1);
640     }
641     }
642     /* bound batch properly */
643     lastframe = astat.rnext + nframes - 1;
644     if ((lastframe-1) % (vint(INTERP)+1)) /* need even interval */
645     lastframe += vint(INTERP)+1 - ((lastframe-1)%(vint(INTERP)+1));
646     if (lastframe > vint(END)) /* check for end */
647     lastframe = vint(END);
648     /* render each view */
649     for (i = astat.rnext; i <= lastframe; i++) {
650     if ((vp = getview(i)) == NULL) {
651     if (!nowarn)
652     fprintf(stderr,
653     "%s: ran out of views before last frame\n",
654     progname);
655     sprintf(vval(END)=vendbuf, "%d", i-1);
656     lastframe = i - 1;
657     break;
658     }
659     if (vdef(ANIMATE)) /* animate frame */
660     animrend(i, vp);
661     else { /* else record it */
662     fputs(VIEWSTR, fp);
663     fprintview(vp, fp);
664     putc('\n', fp);
665     }
666     }
667     if (vdef(ANIMATE)) /* wait for renderings to finish */
668 greg 2.2 bwait(0);
669 greg 2.1 else { /* else if walk-through */
670     fclose(fp); /* close view file */
671     walkwait(astat.rnext, lastframe, vfname); /* walk it */
672     unlink(vfname); /* remove view file */
673     }
674     astat.rnext = i; /* update status */
675     putastat();
676     }
677    
678    
679     filterframes() /* catch up with filtering */
680     {
681     VIEW *vp;
682     register int i;
683    
684     if (astat.tnext < astat.fnext) /* other work to do first */
685     return;
686     /* filter each view */
687     for (i = astat.fnext; i < astat.rnext; i++) {
688     if ((vp = getview(i)) == NULL) { /* get view i */
689     fprintf(stderr,
690     "%s: unexpected error reading view for frame %d\n",
691     progname, i);
692     quit(1);
693     }
694 greg 2.2 dofilt(i, vp, getexp(i), 0); /* filter frame */
695 greg 2.1 }
696 greg 2.2 bwait(0); /* wait for filter processes */
697 greg 2.7 archive(); /* archive originals */
698 greg 2.1 astat.fnext = i; /* update status */
699     putastat();
700     }
701    
702    
703     transferframes() /* catch up with picture transfers */
704     {
705 gwlarson 2.25 char combuf[10240], *fbase;
706 greg 2.1 register char *cp;
707     register int i;
708    
709     if (astat.tnext >= astat.fnext) /* nothing to do, yet */
710     return;
711     if (!vdef(TRANSFER)) { /* no transfer function -- leave 'em */
712     astat.tnext = astat.fnext;
713     putastat(); /* update status */
714     return;
715     }
716 gwlarson 2.25 strcpy(combuf, "cd "); /* start transfer command */
717     fbase = dirfile(cp = combuf+3, vval(BASENAME));
718     if (*cp) {
719     while (*++cp) ;
720     *cp++ = ';'; *cp++ = ' ';
721     } else
722     cp = combuf;
723     strcpy(cp, vval(TRANSFER));
724     while (*cp) cp++;
725 greg 2.1 /* make argument list */
726     for (i = astat.tnext; i < astat.fnext; i++) {
727     *cp++ = ' ';
728 gwlarson 2.25 sprintf(cp, fbase, i);
729 greg 2.1 while (*cp) cp++;
730     strcpy(cp, ".pic");
731     cp += 4;
732     }
733     if (runcom(combuf)) { /* transfer frames */
734     fprintf(stderr, "%s: error running transfer command\n",
735     progname);
736     quit(1);
737     }
738     astat.tnext = i; /* update status */
739     putastat();
740     }
741    
742    
743     animrend(frame, vp) /* start animation frame */
744     int frame;
745     VIEW *vp;
746     {
747 greg 2.2 extern int recover();
748 greg 2.1 char combuf[2048];
749     char fname[128];
750    
751     sprintf(fname, vval(BASENAME), frame);
752     strcat(fname, ".unf");
753     if (access(fname, F_OK) == 0)
754     return;
755 greg 2.2 sprintf(combuf, "%s %d | rpict%s%s -w0 %s > %s", vval(ANIMATE), frame,
756 greg 2.1 rendopt, viewopt(vp), rresopt, fname);
757 greg 2.2 bruncom(combuf, frame, recover); /* run in background */
758 greg 2.1 }
759    
760    
761     walkwait(first, last, vfn) /* walk-through frames */
762     int first, last;
763     char *vfn;
764     {
765 greg 2.29 double blurf;
766     int nblur = getblur(&blurf);
767 greg 2.1 char combuf[2048];
768 greg 2.29 register char *inspoint;
769 greg 2.1 register int i;
770    
771     if (!noaction && vint(INTERP)) /* create dummy frames */
772     for (i = first; i <= last; i++)
773     if (i < vint(END) && (i-1) % (vint(INTERP)+1)) {
774     sprintf(combuf, vval(BASENAME), i);
775     strcat(combuf, ".unf");
776     close(open(combuf, O_RDONLY|O_CREAT, 0666));
777     }
778     /* create command */
779 gwlarson 2.26 sprintf(combuf, "rpict%s%s -w0", rendopt,
780     viewopt(getview(first>1 ? first-1 : 1)));
781 greg 2.29 inspoint = combuf;
782     while (*inspoint) inspoint++;
783     if (nblur) {
784     sprintf(inspoint, " -pm %.3f", blurf/nblur);
785     while (*inspoint) inspoint++;
786     }
787     if (nblur > 1 || vint(INTERP)) {
788     sprintf(inspoint, " -z %s.zbf", vval(BASENAME));
789     while (*inspoint) inspoint++;
790     }
791     sprintf(inspoint, " -o %s.unf %s -S %d",
792 greg 2.4 vval(BASENAME), rresopt, first);
793 greg 2.29 while (*inspoint) inspoint++;
794 greg 2.4 sprintf(inspoint, " %s < %s", vval(OCTREE), vfn);
795 greg 2.2 /* run in parallel */
796 greg 2.7 i = (last-first+1)/(vint(INTERP)+1);
797     if (i < 1) i = 1;
798     if (pruncom(combuf, inspoint, i)) {
799 greg 2.2 fprintf(stderr, "%s: error rendering frames %d through %d\n",
800 greg 2.1 progname, first, last);
801     quit(1);
802     }
803     if (!noaction && vint(INTERP)) /* remove dummy frames */
804     for (i = first; i <= last; i++)
805     if (i < vint(END) && (i-1) % (vint(INTERP)+1)) {
806     sprintf(combuf, vval(BASENAME), i);
807     strcat(combuf, ".unf");
808     unlink(combuf);
809     }
810     }
811    
812    
813 greg 2.2 int
814 greg 2.1 recover(frame) /* recover the specified frame */
815     int frame;
816     {
817 greg 2.2 static int *rfrm; /* list of recovered frames */
818     static int nrfrms = 0;
819 greg 2.29 double blurf;
820     int nblur = getblur(&blurf);
821 greg 2.1 char combuf[2048];
822     char fname[128];
823     register char *cp;
824 greg 2.2 register int i;
825     /* check to see if recovered already */
826     for (i = nrfrms; i--; )
827     if (rfrm[i] == frame)
828     return(0);
829     /* build command */
830 greg 2.1 sprintf(fname, vval(BASENAME), frame);
831     if (vdef(ANIMATE))
832 greg 2.2 sprintf(combuf, "%s %d | rpict%s -w0",
833 greg 2.1 vval(ANIMATE), frame, rendopt);
834     else
835 greg 2.2 sprintf(combuf, "rpict%s -w0", rendopt);
836 greg 2.29 cp = combuf;
837     while (*cp) cp++;
838     if (nblur) {
839     sprintf(cp, " -pm %.3f", blurf/nblur);
840     while (*cp) cp++;
841     }
842     if (nblur > 1 || vint(INTERP)) {
843 greg 2.1 sprintf(cp, " -z %s.zbf", fname);
844     while (*cp) cp++;
845     }
846     sprintf(cp, " -ro %s.unf", fname);
847     while (*cp) cp++;
848     if (!vdef(ANIMATE)) {
849     *cp++ = ' ';
850     strcpy(cp, vval(OCTREE));
851     }
852 greg 2.2 if (runcom(combuf)) /* run command */
853     return(1);
854     /* add frame to recovered list */
855     if (nrfrms)
856     rfrm = (int *)realloc((char *)rfrm, (nrfrms+1)*sizeof(int));
857     else
858     rfrm = (int *)malloc(sizeof(int));
859     if (rfrm == NULL) {
860     perror("malloc");
861 greg 2.1 quit(1);
862     }
863 greg 2.2 rfrm[nrfrms++] = frame;
864     return(0);
865 greg 2.1 }
866    
867    
868 greg 2.2 int
869     frecover(frame) /* recover filtered frame */
870     int frame;
871     {
872     VIEW *vp;
873     char *ex;
874    
875     vp = getview(frame);
876     ex = getexp(frame);
877     if (dofilt(frame, vp, ex, 2) && dofilt(frame, vp, ex, 1))
878     return(1);
879     return(0);
880     }
881    
882    
883 greg 2.7 archive() /* archive and remove renderings */
884 greg 2.1 {
885 greg 2.2 #define RMCOML (sizeof(rmcom)-1)
886     static char rmcom[] = "rm -f";
887 gwlarson 2.25 char basedir[128];
888     int dlen, alen;
889     register int j;
890 greg 2.1
891 greg 2.7 if (arcnext == arcfirst)
892     return; /* nothing to do */
893 gwlarson 2.25 dirfile(basedir, vval(BASENAME));
894     dlen = strlen(basedir);
895 greg 2.2 if (vdef(ARCHIVE)) { /* run archive command */
896 gwlarson 2.25 alen = strlen(vval(ARCHIVE));
897     if (dlen) {
898     j = alen + dlen + 5;
899     strncpy(arcfirst-j, "cd ", 3);
900     strncpy(arcfirst-j+3, basedir, dlen);
901     (arcfirst-j)[dlen+3] = ';'; (arcfirst-j)[dlen+4] = ' ';
902     } else
903     j = alen;
904     strncpy(arcfirst-alen, vval(ARCHIVE), alen);
905     if (runcom(arcfirst-j)) {
906 greg 2.7 fprintf(stderr, "%s: error running archive command\n",
907     progname);
908 greg 2.2 quit(1);
909     }
910 greg 2.1 }
911 gwlarson 2.25 if (dlen) {
912     j = RMCOML + dlen + 5;
913     strncpy(arcfirst-j, "cd ", 3);
914     strncpy(arcfirst-j+3, basedir, dlen);
915     (arcfirst-j)[dlen+3] = ';'; (arcfirst-j)[dlen+4] = ' ';
916     } else
917     j = RMCOML;
918 greg 2.2 /* run remove command */
919 greg 2.7 strncpy(arcfirst-RMCOML, rmcom, RMCOML);
920 gwlarson 2.25 runcom(arcfirst-j);
921 greg 2.7 arcnext = arcfirst; /* reset argument list */
922 greg 2.2 #undef RMCOML
923 greg 2.1 }
924    
925    
926 greg 2.2 int
927     dofilt(frame, vp, ep, rvr) /* filter frame */
928 greg 2.1 int frame;
929     VIEW *vp;
930     char *ep;
931 greg 2.2 int rvr;
932 greg 2.1 {
933 greg 2.2 extern int frecover();
934 greg 2.6 static int iter = 0;
935 greg 2.29 double blurf;
936     int nblur = getblur(&blurf);
937 gwlarson 2.25 char fnbefore[128], fnafter[128], *fbase;
938 greg 2.6 char combuf[1024], fname0[128], fname1[128];
939 greg 2.7 int usepinterp, usepfilt, nora_rgbe;
940 greg 2.2 int frseq[2];
941 greg 2.1 /* check what is needed */
942 greg 2.29 usepinterp = (nblur > 1);
943 greg 2.1 usepfilt = pfiltalways | ep==NULL;
944 greg 2.7 if (ep != NULL && !strcmp(ep, "1"))
945     ep = "+0";
946     nora_rgbe = strcmp(vval(OVERSAMP),"1") || ep==NULL ||
947     *ep != '+' || *ep != '-' || !isint(ep);
948 greg 2.1 /* compute rendered views */
949 greg 2.2 frseq[0] = frame - ((frame-1) % (vint(INTERP)+1));
950     frseq[1] = frseq[0] + vint(INTERP) + 1;
951 gwlarson 2.25 fbase = dirfile(NULL, vval(BASENAME));
952 greg 2.2 if (frseq[1] > vint(END))
953     frseq[1] = vint(END);
954     if (frseq[1] == frame) { /* pfilt only */
955     frseq[0] = frseq[1];
956 greg 2.1 usepinterp = 0; /* update what's needed */
957 greg 2.7 usepfilt |= nora_rgbe;
958     } else if (frseq[0] == frame) { /* no interpolation needed */
959     if (!rvr && frame > 1+vint(INTERP)) { /* archive previous */
960     *arcnext++ = ' ';
961 gwlarson 2.25 sprintf(arcnext, fbase, frame-vint(INTERP)-1);
962 greg 2.7 while (*arcnext) arcnext++;
963     strcpy(arcnext, ".unf");
964     arcnext += 4;
965 greg 2.13 if (usepinterp || vint(INTERP)) { /* and Z-buf */
966 greg 2.7 *arcnext++ = ' ';
967 gwlarson 2.25 sprintf(arcnext, fbase, frame-vint(INTERP)-1);
968 greg 2.7 while (*arcnext) arcnext++;
969     strcpy(arcnext, ".zbf");
970     arcnext += 4;
971     }
972     }
973     if (!usepinterp) /* update what's needed */
974     usepfilt |= nora_rgbe;
975 greg 2.2 } else /* interpolation needed */
976 greg 2.1 usepinterp++;
977 greg 2.2 if (frseq[1] >= astat.rnext) /* next batch unavailable */
978     frseq[1] = frseq[0];
979     sprintf(fnbefore, vval(BASENAME), frseq[0]);
980     sprintf(fnafter, vval(BASENAME), frseq[1]);
981     if (rvr == 1 && recover(frseq[0])) /* recover before frame? */
982     return(1);
983     /* generate command */
984 greg 2.1 if (usepinterp) { /* using pinterp */
985 greg 2.2 if (rvr == 2 && recover(frseq[1])) /* recover after? */
986     return(1);
987 greg 2.29 if (nblur > 1) { /* with pmblur */
988 greg 2.6 sprintf(fname0, "%s/vw0%c", vval(DIRECTORY),
989     'a'+(iter%26));
990     sprintf(fname1, "%s/vw1%c", vval(DIRECTORY),
991     'a'+(iter%26));
992 gwlarson 2.25 if (!noaction) {
993     FILE *fp; /* motion blurring */
994     if ((fp = fopen(fname0, "w")) == NULL) {
995     perror(fname0); quit(1);
996     }
997     fputs(VIEWSTR, fp);
998     fprintview(vp, fp);
999     putc('\n', fp); fclose(fp);
1000     if ((vp = getview(frame+1)) == NULL) {
1001     fprintf(stderr,
1002     "%s: unexpected error reading view for frame %d\n",
1003     progname, frame+1);
1004     quit(1);
1005     }
1006     if ((fp = fopen(fname1, "w")) == NULL) {
1007     perror(fname1); quit(1);
1008     }
1009     fputs(VIEWSTR, fp);
1010     fprintview(vp, fp);
1011     putc('\n', fp); fclose(fp);
1012 greg 2.1 }
1013     sprintf(combuf,
1014 greg 2.29 "(pmblur %.3f %d %s %s; rm -f %s %s) | pinterp -B",
1015     blurf, nblur,
1016 greg 2.6 fname0, fname1, fname0, fname1);
1017     iter++;
1018 greg 2.1 } else /* no blurring */
1019     strcpy(combuf, "pinterp");
1020     strcat(combuf, viewopt(vp));
1021     if (vbool(RTRACE))
1022 greg 2.2 sprintf(combuf+strlen(combuf), " -ff -fr '%s -w0 %s'",
1023 greg 2.21 rendopt+1, vval(OCTREE));
1024 greg 2.1 if (vdef(PINTERP))
1025     sprintf(combuf+strlen(combuf), " %s", vval(PINTERP));
1026     if (usepfilt)
1027     sprintf(combuf+strlen(combuf), " %s", rresopt);
1028     else
1029     sprintf(combuf+strlen(combuf), " %s -e %s",
1030     fresopt, ep);
1031     sprintf(combuf+strlen(combuf), " %s.unf %s.zbf",
1032     fnbefore, fnbefore);
1033 greg 2.2 if (frseq[1] != frseq[0])
1034 greg 2.1 sprintf(combuf+strlen(combuf), " %s.unf %s.zbf",
1035     fnafter, fnafter);
1036     if (usepfilt) { /* also pfilt */
1037     if (vdef(PFILT))
1038     sprintf(combuf+strlen(combuf), " | pfilt %s",
1039     vval(PFILT));
1040     else
1041     strcat(combuf, " | pfilt");
1042     if (ep != NULL)
1043     sprintf(combuf+strlen(combuf), " -1 -e %s %s",
1044     ep, fresopt);
1045     else
1046     sprintf(combuf+strlen(combuf), " %s", fresopt);
1047     }
1048     } else if (usepfilt) { /* pfilt only */
1049 greg 2.2 if (rvr == 2)
1050     return(1);
1051 greg 2.1 if (vdef(PFILT))
1052     sprintf(combuf, "pfilt %s", vval(PFILT));
1053     else
1054     strcpy(combuf, "pfilt");
1055     if (ep != NULL)
1056     sprintf(combuf+strlen(combuf), " -1 -e %s %s %s.unf",
1057     ep, fresopt, fnbefore);
1058     else
1059     sprintf(combuf+strlen(combuf), " %s %s.unf",
1060     fresopt, fnbefore);
1061     } else { /* else just check it */
1062 greg 2.2 if (rvr == 2)
1063     return(1);
1064 greg 2.7 sprintf(combuf, "ra_rgbe -e %s -r %s.unf", ep, fnbefore);
1065 greg 2.1 }
1066     /* output file name */
1067 greg 2.6 sprintf(fname0, vval(BASENAME), frame);
1068     sprintf(combuf+strlen(combuf), " > %s.pic", fname0);
1069 greg 2.2 if (rvr) /* in recovery */
1070     return(runcom(combuf));
1071     bruncom(combuf, frame, frecover); /* else run in background */
1072     return(0);
1073 greg 2.1 }
1074    
1075    
1076     VIEW *
1077     getview(n) /* get view number n */
1078     int n;
1079     {
1080     static FILE *viewfp = NULL; /* view file pointer */
1081     static int viewnum = 0; /* current view number */
1082     static VIEW curview = STDVIEW; /* current view */
1083     char linebuf[256];
1084    
1085     if (n == 0) { /* signal to close file and clean up */
1086     if (viewfp != NULL) {
1087     fclose(viewfp);
1088     viewfp = NULL;
1089     viewnum = 0;
1090     copystruct(&curview, &stdview);
1091     }
1092     return(NULL);
1093     }
1094 gwlarson 2.24 if (viewfp == NULL) { /* open file */
1095 greg 2.1 if ((viewfp = fopen(vval(VIEWFILE), "r")) == NULL) {
1096     perror(vval(VIEWFILE));
1097     quit(1);
1098     }
1099 gwlarson 2.24 } else if (n > 0 && n < viewnum) { /* rewind file */
1100 greg 2.8 if (viewnum == 1 && feof(viewfp))
1101     return(&curview); /* just one view */
1102 greg 2.1 if (fseek(viewfp, 0L, 0) == EOF) {
1103     perror(vval(VIEWFILE));
1104     quit(1);
1105     }
1106     copystruct(&curview, &stdview);
1107     viewnum = 0;
1108     }
1109 gwlarson 2.24 if (n < 0) { /* get next view */
1110     register int c = getc(viewfp);
1111     if (c == EOF)
1112     return((VIEW *)NULL); /* that's it */
1113     ungetc(c, viewfp);
1114     n = viewnum + 1;
1115     }
1116 greg 2.1 while (n > viewnum) { /* scan to desired view */
1117     if (fgets(linebuf, sizeof(linebuf), viewfp) == NULL)
1118 greg 2.15 return(viewnum==1 ? &curview : (VIEW *)NULL);
1119 greg 2.1 if (isview(linebuf) && sscanview(&curview, linebuf) > 0)
1120     viewnum++;
1121     }
1122     return(&curview); /* return it */
1123     }
1124    
1125    
1126     int
1127     countviews() /* count views in view file */
1128     {
1129 gwlarson 2.24 int n;
1130 greg 2.1
1131 gwlarson 2.24 if (getview(n=1) == NULL)
1132     return(0);
1133     while (getview(-1) != NULL)
1134 greg 2.1 n++;
1135     return(n);
1136     }
1137    
1138    
1139     char *
1140     getexp(n) /* get exposure for nth frame */
1141     int n;
1142     {
1143     extern char *fskip();
1144     static char expval[32];
1145     static FILE *expfp = NULL;
1146     static long *exppos;
1147     static int curfrm;
1148     register char *cp;
1149    
1150     if (n == 0) { /* signal to close file */
1151     if (expfp != NULL) {
1152     fclose(expfp);
1153 greg 2.29 free((void *)exppos);
1154 greg 2.1 expfp = NULL;
1155     }
1156     return(NULL);
1157 greg 2.14 } else if (n > vint(END)) /* request past end (error?) */
1158     return(NULL);
1159 greg 2.1 if (!vdef(EXPOSURE)) /* no setting (auto) */
1160     return(NULL);
1161     if (isflt(vval(EXPOSURE))) /* always the same */
1162     return(vval(EXPOSURE));
1163     if (expfp == NULL) { /* open exposure file */
1164     if ((expfp = fopen(vval(EXPOSURE), "r")) == NULL) {
1165     fprintf(stderr,
1166     "%s: cannot open exposure file \"%s\"\n",
1167     progname, vval(EXPOSURE));
1168     quit(1);
1169     }
1170     curfrm = vint(END) + 1; /* init lookup tab. */
1171     exppos = (long *)malloc(curfrm*sizeof(long *));
1172     if (exppos == NULL) {
1173     perror(progname);
1174     quit(1);
1175     }
1176     while (curfrm--)
1177     exppos[curfrm] = -1L;
1178     curfrm = 0;
1179     }
1180     /* find position in file */
1181     if (n-1 != curfrm && n != curfrm && exppos[n-1] >= 0 &&
1182     fseek(expfp, exppos[curfrm=n-1], 0) == EOF) {
1183     fprintf(stderr, "%s: seek error on exposure file\n", progname);
1184     quit(1);
1185     }
1186     while (n > curfrm) { /* read exposure */
1187     if (exppos[curfrm] < 0)
1188     exppos[curfrm] = ftell(expfp);
1189     if (fgets(expval, sizeof(expval), expfp) == NULL) {
1190     fprintf(stderr, "%s: too few exposures\n",
1191     vval(EXPOSURE));
1192     quit(1);
1193     }
1194     curfrm++;
1195     cp = fskip(expval); /* check format */
1196 greg 2.14 if (cp != NULL)
1197     while (isspace(*cp))
1198     *cp++ = '\0';
1199     if (cp == NULL || *cp) {
1200 greg 2.1 fprintf(stderr,
1201     "%s: exposure format error on line %d\n",
1202     vval(EXPOSURE), curfrm);
1203     quit(1);
1204     }
1205     }
1206     return(expval); /* return value */
1207     }
1208    
1209    
1210 greg 2.2 struct pslot *
1211     findpslot(pid) /* find or allocate a process slot */
1212     int pid;
1213     {
1214     register struct pslot *psempty = NULL;
1215     register int i;
1216    
1217     for (i = 0; i < npslots; i++) { /* look for match */
1218     if (pslot[i].pid == pid)
1219     return(pslot+i);
1220     if (psempty == NULL && pslot[i].pid == 0)
1221     psempty = pslot+i;
1222     }
1223     return(psempty); /* return emtpy slot (error if NULL) */
1224     }
1225    
1226    
1227     int
1228     donecom(ps, pn, status) /* clean up after finished process */
1229     PSERVER *ps;
1230     int pn;
1231     int status;
1232     {
1233     register PROC *pp;
1234 greg 2.18 register struct pslot *psl;
1235 greg 2.2
1236     pp = ps->proc + pn;
1237 greg 2.3 if (pp->elen) { /* pass errors */
1238 greg 2.2 if (ps->hostname[0])
1239 greg 2.3 fprintf(stderr, "%s: ", ps->hostname);
1240     fprintf(stderr, "Error output from: %s\n", pp->com);
1241 greg 2.2 fputs(pp->errs, stderr);
1242     fflush(stderr);
1243     if (ps->hostname[0])
1244     status = 1; /* because rsh doesn't return status */
1245     }
1246 greg 2.18 lastpserver = NULL;
1247     psl = findpslot(pp->pid); /* check for bruncom() slot */
1248     if (psl->pid) {
1249     if (status) {
1250     if (psl->rcvf != NULL) /* attempt recovery */
1251     status = (*psl->rcvf)(psl->fout);
1252     if (status) {
1253     fprintf(stderr,
1254     "%s: error rendering frame %d\n",
1255     progname, psl->fout);
1256     quit(1);
1257     }
1258     lastpserver = ps;
1259     }
1260     psl->pid = 0; /* free process slot */
1261     } else if (status)
1262     lastpserver = ps;
1263 greg 2.2 freestr(pp->com); /* free command string */
1264     return(status);
1265     }
1266    
1267    
1268     int
1269     serverdown() /* check status of last process server */
1270     {
1271 greg 2.18 if (lastpserver == NULL || !lastpserver->hostname[0])
1272     return(0);
1273 greg 2.2 if (pserverOK(lastpserver)) /* server still up? */
1274     return(0);
1275     delpserver(lastpserver); /* else delete it */
1276     if (pslist == NULL) {
1277     fprintf(stderr, "%s: all process servers are down\n",
1278     progname);
1279     quit(1);
1280     }
1281     return(1);
1282     }
1283    
1284    
1285     int
1286     bruncom(com, fout, rf) /* run a command in the background */
1287     char *com;
1288     int fout;
1289     int (*rf)();
1290     {
1291     int pid;
1292     register struct pslot *psl;
1293    
1294 greg 2.5 if (noaction) {
1295     if (!silent)
1296     printf("\t%s\n", com); /* echo command */
1297 greg 2.2 return(0);
1298 greg 2.5 }
1299 greg 2.18 com = savestr(com); /* else start it when we can */
1300     while ((pid = startjob(NULL, com, donecom)) == -1)
1301 greg 2.2 bwait(1);
1302 greg 2.5 if (!silent) { /* echo command */
1303 greg 2.3 PSERVER *ps;
1304     int psn = pid;
1305 greg 2.4 ps = findjob(&psn);
1306 greg 2.5 printf("\t%s\n", com);
1307 greg 2.4 printf("\tProcess started on %s\n", phostname(ps));
1308 greg 2.3 fflush(stdout);
1309     }
1310 greg 2.2 psl = findpslot(pid); /* record info. in appropriate slot */
1311     psl->pid = pid;
1312     psl->fout = fout;
1313     psl->rcvf = rf;
1314     return(pid);
1315     }
1316    
1317    
1318     bwait(ncoms) /* wait for batch job(s) to finish */
1319     int ncoms;
1320     {
1321     int status;
1322    
1323     if (noaction)
1324     return;
1325     while ((status = wait4job(NULL, -1)) != -1) {
1326 greg 2.18 serverdown(); /* update server status */
1327     if (--ncoms == 0)
1328     break; /* done enough */
1329 greg 2.2 }
1330     }
1331    
1332    
1333     int
1334 greg 2.4 pruncom(com, ppins, maxcopies) /* run a command in parallel over network */
1335     char *com, *ppins;
1336 greg 2.2 int maxcopies;
1337     {
1338     int retstatus = 0;
1339 greg 2.3 int hostcopies;
1340 greg 2.18 char buf[10240], *com1, *s;
1341 greg 2.2 int status;
1342 greg 2.17 int pfd;
1343     register int n;
1344 greg 2.2 register PSERVER *ps;
1345    
1346 greg 2.3 if (!silent)
1347 greg 2.6 printf("\t%s\n", com); /* echo command */
1348 greg 2.3 if (noaction)
1349 greg 2.2 return(0);
1350 greg 2.3 fflush(stdout);
1351 greg 2.2 /* start jobs on each server */
1352 greg 2.3 for (ps = pslist; ps != NULL; ps = ps->next) {
1353     hostcopies = 0;
1354 greg 2.4 if (maxcopies > 1 && ps->nprocs > 1 && ppins != NULL) {
1355 greg 2.17 strcpy(com1=buf, com); /* build -PP command */
1356 greg 2.4 sprintf(com1+(ppins-com), " -PP %s/%s.persist",
1357     vval(DIRECTORY), phostname(ps));
1358     strcat(com1, ppins);
1359 greg 2.17 } else
1360 greg 2.4 com1 = com;
1361 greg 2.18 while (maxcopies > 0) {
1362     s = savestr(com1);
1363     if (startjob(ps, s, donecom) != -1) {
1364     sleep(20);
1365     hostcopies++;
1366     maxcopies--;
1367     } else {
1368     freestr(s);
1369     break;
1370     }
1371 greg 2.2 }
1372 greg 2.3 if (!silent && hostcopies) {
1373     if (hostcopies > 1)
1374     printf("\t%d duplicate processes", hostcopies);
1375     else
1376     printf("\tProcess");
1377 greg 2.4 printf(" started on %s\n", phostname(ps));
1378 greg 2.3 fflush(stdout);
1379     }
1380     }
1381 greg 2.2 /* wait for jobs to finish */
1382     while ((status = wait4job(NULL, -1)) != -1)
1383 greg 2.18 retstatus += status && !serverdown();
1384 greg 2.17 /* terminate parallel rpict's */
1385 greg 2.18 for (ps = pslist; ps != NULL; ps = ps->next) {
1386     sprintf(buf, "%s/%s.persist", vval(DIRECTORY), phostname(ps));
1387 greg 2.17 if ((pfd = open(buf, O_RDONLY)) >= 0) {
1388     n = read(pfd, buf, sizeof(buf)-1); /* get PID */
1389     buf[n] = '\0';
1390     close(pfd);
1391     for (n = 0; buf[n] && !isspace(buf[n]); n++)
1392     ;
1393     /* terminate */
1394     sprintf(buf, "kill -ALRM %d", atoi(buf+n));
1395 greg 2.18 wait4job(ps, startjob(ps, buf, NULL));
1396 greg 2.17 }
1397     }
1398 greg 2.2 return(retstatus);
1399     }
1400    
1401    
1402     runcom(cs) /* run a command locally and wait for it */
1403 greg 2.1 char *cs;
1404     {
1405     if (!silent) /* echo it */
1406     printf("\t%s\n", cs);
1407     if (noaction)
1408     return(0);
1409     fflush(stdout); /* flush output and pass to shell */
1410     return(system(cs));
1411     }
1412    
1413    
1414     rmfile(fn) /* remove a file */
1415     char *fn;
1416     {
1417     if (!silent)
1418     #ifdef MSDOS
1419     printf("\tdel %s\n", fn);
1420     #else
1421     printf("\trm -f %s\n", fn);
1422     #endif
1423     if (noaction)
1424     return(0);
1425     return(unlink(fn));
1426     }
1427    
1428    
1429     badvalue(vc) /* report bad variable value and exit */
1430     int vc;
1431     {
1432     fprintf(stderr, "%s: bad value for variable '%s'\n",
1433     progname, vnam(vc));
1434     quit(1);
1435 gwlarson 2.25 }
1436    
1437    
1438     char *
1439     dirfile(df, path) /* separate path into directory and file */
1440     char *df;
1441     register char *path;
1442     {
1443     register int i;
1444     int psep;
1445    
1446     for (i = 0, psep = -1; path[i]; i++)
1447     if (path[i] == '/')
1448     psep = i;
1449     if (df != NULL)
1450     if (psep == 0) {
1451     df[0] = '/';
1452     df[1] = '\0';
1453     } else if (psep > 0) {
1454     strncpy(df, path, psep);
1455     df[psep] = '\0';
1456     } else
1457     df[0] = '\0';
1458     return(path+psep+1);
1459 greg 2.29 }
1460    
1461    
1462     int
1463     getblur(double *bf) /* get # blur samples (and fraction) */
1464     {
1465     double blurf;
1466     int nblur;
1467     char *s;
1468    
1469     if (!vdef(MBLUR)) {
1470     if (bf != NULL)
1471     *bf = 0.0;
1472     return(0);
1473     }
1474     blurf = atof(vval(MBLUR));
1475     if (blurf < 0.0)
1476     blurf = 0.0;
1477     if (bf != NULL)
1478     *bf = blurf;
1479     if (blurf <= FTINY)
1480     return(0);
1481     s = sskip(vval(MBLUR));
1482     if (!*s)
1483     return(DEF_NBLUR);
1484     nblur = atoi(s);
1485     if (nblur <= 0)
1486     return(1);
1487     return(nblur);
1488 greg 2.1 }