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

Comparing ray/src/util/rtcontrib.c (file contents):
Revision 1.4 by greg, Thu May 26 17:53:28 2005 UTC vs.
Revision 1.29 by greg, Wed Oct 5 05:20:21 2005 UTC

# Line 7 | Line 7 | static const char RCSid[] = "$Id$";
7  
8   #include  "standard.h"
9   #include  <ctype.h>
10 + #include  <signal.h>
11   #include  "platform.h"
12   #include  "rtprocess.h"
13   #include  "selcall.h"
# Line 15 | Line 16 | static const char RCSid[] = "$Id$";
16   #include  "lookup.h"
17   #include  "calcomp.h"
18  
19 + #ifndef MAXMODLIST
20   #define MAXMODLIST      1024            /* maximum modifiers we'll track */
21 + #endif
22  
23   int     treebufsiz = BUFSIZ;            /* current tree buffer size */
24  
# Line 48 | Line 51 | static void closefile(void *p) { fclose((FILE *)p); }
51  
52   LUTAB   ofiletab = LU_SINIT(free,closefile);    /* output file table */
53  
54 + #define OF_MODIFIER     01
55 + #define OF_BIN          02
56 +
57   FILE *getofile(const char *ospec, const char *mname, int bn);
58 + int ofname(char *oname, const char *ospec, const char *mname, int bn);
59 + void printheader(FILE *fout, const char *info);
60  
61   /*
62   * The rcont structure is used to manage i/o with a particular
# Line 63 | Line 71 | struct rtproc {
71          int             bsiz;           /* ray tree buffer length */
72          char            *buf;           /* ray tree buffer */
73          int             nbr;            /* number of bytes from rtrace */
74 < };                              /* rtrace process */
74 > };                              /* rtrace process buffer */
75  
76                                          /* rtrace command and defaults */
77 < char            *rtargv[256] = { "rtrace", "-dt", "0", "-dj", ".5", "-dr", "3",
77 > char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
78 >                                "-dj", ".5", "-dr", "3",
79                                  "-ab", "1", "-ad", "128", };
80 < int  rtargc = 11;
80 > int  rtargc = 9;
81                                          /* overriding rtrace options */
82 < char            *myrtopts[] = { "-o~~TmWdp", "-h-",
83 <                                "-x", "1", "-y", "0",
75 <                                "-as", "0", "-aa", "0", NULL };
82 > char            *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0",
83 >                                "-dt", "0", "-as", "0", "-aa", "0", NULL };
84  
85   struct rtproc   rt0;                    /* head of rtrace process list */
86  
87   struct rtproc   *rt_unproc = NULL;      /* unprocessed ray trees */
88  
89   char    persistfn[] = "pfXXXXXX";       /* persist file name */
82 char    fmt[8];                         /* rtrace i/o format */
90  
91   int             gargc;                  /* global argc */
92   char            **gargv;                /* global argv */
# Line 94 | Line 101 | int            header = 1;             /* output header? */
101   int             xres = 0;               /* horiz. output resolution */
102   int             yres = 0;               /* vert. output resolution */
103  
104 < long            raysleft;               /* number of rays left to trace */
104 > unsigned long   raysleft;               /* number of rays left to trace */
105   long            waitflush;              /* how long until next flush */
106  
107   unsigned long   lastray = 0;            /* last ray number sent */
# Line 105 | Line 112 | int            using_stdout = 0;       /* are we using stdout? */
112   const char      *modname[MAXMODLIST];   /* ordered modifier name list */
113   int             nmods = 0;              /* number of modifiers */
114  
115 + #define queue_length()          (lastray - lastdone)
116 +
117   MODCONT *addmodifier(char *modn, char *outf, char *binv);
118 + void addmodfile(char *fname, char *outf, char *binv);
119  
110 int done_rprocs(struct rtproc *rtp);
120   void init(int np);
121 < void tracecontribs(FILE *fp);
121 > int done_rprocs(struct rtproc *rtp);
122 > void recover_output(FILE *fin);
123 > void trace_contribs(FILE *fin);
124   struct rtproc *wait_rproc(void);
125   struct rtproc *get_rproc(void);
126   void queue_raytree(struct rtproc *rtp);
127   void process_queue(void);
128  
129 < void putcontrib(const DCOLOR cnt, FILE *fout);
129 > void put_contrib(const DCOLOR cnt, FILE *fout);
130   void add_contrib(const char *modn);
131   void done_contrib(void);
132  
133 + /* return number of open rtrace processes */
134 + static int
135 + nrtprocs(void)
136 + {
137 +        int     nrtp = 0;
138 +        struct rtproc   *rtp;
139 +
140 +        for (rtp = &rt0; rtp != NULL; rtp = rtp->next)
141 +                nrtp += rtp->pd.running;
142 +        return(nrtp);
143 + }
144 +
145   /* set input/output format */
146   static void
147   setformat(const char *fmt)
148   {
149          switch (fmt[0]) {
127        case 'a':
150          case 'f':
151          case 'd':
152 +                SET_FILE_BINARY(stdin);
153 +                /* fall through */
154 +        case 'a':
155                  inpfmt = fmt[0];
156                  break;
157          default:
# Line 157 | Line 182 | int
182   main(int argc, char *argv[])
183   {
184          int     nprocs = 1;
185 +        int     recover = 0;
186          char    *curout = NULL;
187          char    *binval = NULL;
188 +        char    fmt[8];
189          int     i, j;
190 +                                /* need at least one argument */
191 +        if (argc < 2) {
192 +                fprintf(stderr,
193 + "Usage: %s [-n nprocs][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n",
194 +                        argv[0]);
195 +                exit(1);
196 +        }
197                                  /* global program name */
198          gargv = argv;
199 <                                /* set up calcomp mode */
199 >                                /* initialize calcomp routines */
200          esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW;
201          esupport &= ~(E_OUTCHAN);
202 +        varset("PI", ':', PI);
203                                  /* get our options */
204          for (i = 1; i < argc-1; i++) {
205                                                  /* expand arguments */
# Line 177 | Line 212 | main(int argc, char *argv[])
212                  }
213                  if (argv[i][0] == '-')
214                          switch (argv[i][1]) {
215 +                        case 'r':               /* recover output */
216 +                                if (argv[i][2]) break;
217 +                                recover++;
218 +                                continue;
219                          case 'n':               /* number of processes */
220 <                                if (argv[i][2] || i >= argc-1) break;
220 >                                if (argv[i][2] || i >= argc-2) break;
221                                  nprocs = atoi(argv[++i]);
222                                  if (nprocs <= 0)
223                                          error(USER, "illegal number of processes");
# Line 202 | Line 241 | main(int argc, char *argv[])
241                                  break;
242                          case 'f':               /* file or i/o format */
243                                  if (!argv[i][2]) {
244 <                                        if (i >= argc-1) break;
245 <                                        fcompile(argv[++i]);
244 >                                        char    *fpath;
245 >                                        if (i >= argc-2) break;
246 >                                        fpath = getpath(argv[++i],
247 >                                                        getrlibpath(), R_OK);
248 >                                        if (fpath == NULL) {
249 >                                                sprintf(errmsg,
250 >                                                        "cannot find file '%s'",
251 >                                                                argv[i]);
252 >                                                error(USER, errmsg);
253 >                                        }
254 >                                        fcompile(fpath);
255                                          continue;
256                                  }
257                                  setformat(argv[i]+2);
258                                  continue;
259                          case 'e':               /* expression */
260 <                                if (argv[i][2] || i >= argc-1) break;
260 >                                if (argv[i][2] || i >= argc-2) break;
261                                  scompile(argv[++i], NULL, 0);
262                                  continue;
263                          case 'o':               /* output file spec. */
264 <                                if (argv[i][2] || i >= argc-1) break;
264 >                                if (argv[i][2] || i >= argc-2) break;
265                                  curout = argv[++i];
266                                  continue;
267                          case 'x':               /* horiz. output resolution */
268 <                                if (argv[i][2] || i >= argc-1) break;
268 >                                if (argv[i][2] || i >= argc-2) break;
269                                  xres = atoi(argv[++i]);
270                                  continue;
271                          case 'y':               /* vert. output resolution */
272 <                                if (argv[i][2] || i >= argc-1) break;
272 >                                if (argv[i][2] || i >= argc-2) break;
273                                  yres = atoi(argv[++i]);
274                                  continue;
275                          case 'b':               /* bin expression */
276 <                                if (argv[i][2] || i >= argc-1) break;
276 >                                if (argv[i][2] || i >= argc-2) break;
277                                  binval = argv[++i];
278                                  continue;
279                          case 'm':               /* modifier name */
280 <                                if (argv[i][2] || i >= argc-1) break;
280 >                                if (argv[i][2] || i >= argc-2) break;
281                                  rtargv[rtargc++] = "-ti";
282                                  rtargv[rtargc++] = argv[++i];
283                                  addmodifier(argv[i], curout, binval);
284                                  continue;
285 +                        case 'M':               /* modifier file */
286 +                                if (argv[i][2] || i >= argc-2) break;
287 +                                rtargv[rtargc++] = "-tI";
288 +                                rtargv[rtargc++] = argv[++i];
289 +                                addmodfile(argv[i], curout, binval);
290 +                                continue;
291                          }
292                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
293          }
# Line 269 | Line 323 | main(int argc, char *argv[])
323                  execv(rtpath, rtargv);
324                  perror(rtpath); /* execv() should not return */
325                  exit(1);
326 <        } else if (nprocs > 1) {        /* add persist file if parallel */
326 >        }
327 >        if (nprocs > 1) {       /* add persist file if parallel */
328                  rtargv[rtargc++] = "-PP";
329                  rtargv[rtargc++] = mktemp(persistfn);
330          }
# Line 281 | Line 336 | main(int argc, char *argv[])
336                  error(USER, "missing octree argument");
337          rtargv[rtargc++] = octree = argv[i];
338          rtargv[rtargc] = NULL;
339 <                                /* start rtrace & compute contributions */
339 >                                /* start rtrace */
340          init(nprocs);
341 <        tracecontribs(stdin);
341 >        if (recover)            /* perform recovery if requested */
342 >                recover_output(stdin);
343 >        trace_contribs(stdin);  /* compute contributions */
344          quit(0);
345   }
346  
347 + #ifndef SIGALRM
348 + #define SIGALRM SIGTERM
349 + #endif
350   /* kill persistent rtrace process */
351   static void
352   killpersist(void)
353   {
354          FILE    *fp = fopen(persistfn, "r");
355 <        int     pid;
355 >        RT_PID  pid;
356  
357          if (fp == NULL)
358                  return;
# Line 335 | Line 395 | quit(int status)
395          exit(status);                   /* flushes all output streams */
396   }
397  
398 < /* start rtrace and initialize buffers */
398 > /* start rtrace processes and initialize */
399   void
400   init(int np)
401   {
# Line 388 | Line 448 | init(int np)
448          rtp->next = NULL;               /* terminate list */
449          if (yres > 0) {
450                  if (xres > 0)
451 <                        raysleft = xres*yres;
451 >                        raysleft = (unsigned long)xres*yres;
452                  else
453                          raysleft = yres;
454          } else
# Line 426 | Line 486 | addmodifier(char *modn, char *outf, char *binv)
486          return mp;
487   }
488  
489 + /* add modifiers from a file list */
490 + void
491 + addmodfile(char *fname, char *outf, char *binv)
492 + {
493 +        char    *mname[MAXMODLIST];
494 +        int     i;
495 +                                        /* find the file & store strings */
496 +        if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) {
497 +                sprintf(errmsg, "cannot find modifier file '%s'", fname);
498 +                error(SYSTEM, errmsg);
499 +        }
500 +        for (i = 0; mname[i]; i++)      /* add each one */
501 +                addmodifier(mname[i], outf, binv);
502 + }
503 +
504   /* put string to stderr */
505   void
506   eputs(char  *s)
# Line 441 | Line 516 | eputs(char  *s)
516          midline = s[strlen(s)-1] != '\n';
517   }
518  
519 + /* construct output file name and return flags whether modifier/bin present */
520 + int
521 + ofname(char *oname, const char *ospec, const char *mname, int bn)
522 + {
523 +        const char      *mnp = NULL;
524 +        const char      *bnp = NULL;
525 +        const char      *cp;
526 +        
527 +        if (ospec == NULL)
528 +                return -1;
529 +        for (cp = ospec; *cp; cp++)             /* check format position(s) */
530 +                if (*cp == '%') {
531 +                        do
532 +                                ++cp;
533 +                        while (isdigit(*cp));
534 +                        switch (*cp) {
535 +                        case '%':
536 +                                break;
537 +                        case 's':
538 +                                if (mnp != NULL)
539 +                                        return -1;
540 +                                mnp = cp;
541 +                                break;
542 +                        case 'd':
543 +                                if (bnp != NULL)
544 +                                        return -1;
545 +                                bnp = cp;
546 +                                break;
547 +                        default:
548 +                                return -1;
549 +                        }
550 +                }
551 +        if (mnp != NULL) {                      /* create file name */
552 +                if (bnp != NULL) {
553 +                        if (bnp > mnp)
554 +                                sprintf(oname, ospec, mname, bn);
555 +                        else
556 +                                sprintf(oname, ospec, bn, mname);
557 +                        return OF_MODIFIER|OF_BIN;
558 +                }
559 +                sprintf(oname, ospec, mname);
560 +                return OF_MODIFIER;
561 +        }
562 +        if (bnp != NULL) {
563 +                sprintf(oname, ospec, bn);
564 +                return OF_BIN;
565 +        }
566 +        strcpy(oname, ospec);
567 +        return 0;
568 + }
569 +
570   /* write header to the given output stream */
571   void
572 < printheader(FILE *fout)
572 > printheader(FILE *fout, const char *info)
573   {
574          extern char     VersionID[];
575          FILE            *fin = fopen(octree, "r");
# Line 455 | Line 581 | printheader(FILE *fout)
581          printargs(gargc-1, gargv, fout);        /* add our command */
582          fprintf(fout, "SOFTWARE= %s\n", VersionID);
583          fputnow(fout);
584 +        if (info != NULL)                       /* add extra info if given */
585 +                fputs(info, fout);
586          switch (outfmt) {                       /* add output format */
587          case 'a':
588                  fputformat("ascii", fout);
# Line 481 | Line 609 | printheader(FILE *fout)
609   FILE *
610   getofile(const char *ospec, const char *mname, int bn)
611   {
612 <        const char      *mnp = NULL;
613 <        const char      *bnp = NULL;
486 <        const char      *cp;
487 <        char            ofname[1024];
612 >        int             ofl;
613 >        char            oname[1024];
614          LUENT           *lep;
615          
616          if (ospec == NULL) {                    /* use stdout? */
617 <                if (!using_stdout && header)
618 <                        printheader(stdout);
617 >                if (!using_stdout) {
618 >                        if (outfmt != 'a')
619 >                                SET_FILE_BINARY(stdout);
620 >                        if (header)
621 >                                printheader(stdout, NULL);
622 >                }
623                  using_stdout = 1;
624                  return stdout;
625          }
626 <        for (cp = ospec; *cp; cp++)             /* check format position(s) */
627 <                if (*cp == '%') {
628 <                        do
629 <                                ++cp;
630 <                        while (isdigit(*cp));
631 <                        switch (*cp) {
502 <                        case '%':
503 <                                break;
504 <                        case 's':
505 <                                if (mnp != NULL)
506 <                                        goto badspec;
507 <                                mnp = cp;
508 <                                break;
509 <                        case 'd':
510 <                                if (bnp != NULL)
511 <                                        goto badspec;
512 <                                bnp = cp;
513 <                                break;
514 <                        default:
515 <                                goto badspec;
516 <                        }
517 <                }
518 <        if (mnp != NULL) {                      /* create file name */
519 <                if (bnp != NULL) {
520 <                        if (bnp > mnp)
521 <                                sprintf(ofname, ospec, mname, bn);
522 <                        else
523 <                                sprintf(ofname, ospec, bn, mname);
524 <                } else
525 <                        sprintf(ofname, ospec, mname);
526 <        } else if (bnp != NULL)
527 <                sprintf(ofname, ospec, bn);
528 <        else
529 <                strcpy(ofname, ospec);
530 <        lep = lu_find(&ofiletab, ofname);       /* look it up */
626 >        ofl = ofname(oname, ospec, mname, bn);  /* get output name */
627 >        if (ofl < 0) {
628 >                sprintf(errmsg, "bad output format '%s'", ospec);
629 >                error(USER, errmsg);
630 >        }
631 >        lep = lu_find(&ofiletab, oname);        /* look it up */
632          if (lep->key == NULL)                   /* new entry */
633 <                lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname);
633 >                lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
634          if (lep->data == NULL) {                /* open output file */
635 <                FILE            *fp = fopen(ofname, "w");
635 >                FILE            *fp;
636                  int             i;
637 +                if (oname[0] == '!')            /* output to command */
638 +                        fp = popen(oname+1, "w");
639 +                else
640 +                        fp = fopen(oname, "w");
641                  if (fp == NULL) {
642 <                        sprintf(errmsg, "cannot open '%s' for writing", ofname);
642 >                        sprintf(errmsg, "cannot open '%s' for writing", oname);
643                          error(SYSTEM, errmsg);
644                  }
645 <                if (header)
646 <                        printheader(fp);
645 >                if (outfmt != 'a')
646 >                        SET_FILE_BINARY(fp);
647 >                if (header) {
648 >                        char    info[512];
649 >                        char    *cp = info;
650 >                        sprintf(cp, "MODIFIER=%s\n", mname);
651 >                        while (*cp) ++cp;
652 >                        if (ofl & OF_BIN) {
653 >                                sprintf(cp, "BIN=%d\n", bn);
654 >                                while (*cp) ++cp;
655 >                        }
656 >                        *cp = '\0';
657 >                        printheader(fp, info);
658 >                }
659                                                  /* play catch-up */
660                  for (i = 0; i < lastdone; i++) {
661                          static const DCOLOR     nocontrib = BLKCOLOR;
662 <                        putcontrib(nocontrib, fp);
662 >                        put_contrib(nocontrib, fp);
663                          if (outfmt == 'a')
664                                  putc('\n', fp);
665                  }
# Line 551 | Line 668 | getofile(const char *ospec, const char *mname, int bn)
668                  lep->data = (char *)fp;
669          }
670          return (FILE *)lep->data;               /* return open file pointer */
554 badspec:
555        sprintf(errmsg, "bad output format '%s'", ospec);
556        error(USER, errmsg);
557        return NULL;            /* pro forma return */
671   }
672  
673   /* read input ray into buffer */
# Line 617 | Line 730 | add_contrib(const char *modn)
730          bn = (int)(evalue(mp->binv) + .5);
731          if (bn <= 0)
732                  bn = 0;
733 <        else if (bn > mp->nbins) {      /* new bin */
733 >        else if (bn >= mp->nbins) {     /* new bin */
734                  mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
735                                                  bn*sizeof(DCOLOR));
736                  if (mp == NULL)
# Line 648 | Line 761 | puteol(const LUENT *e, void *p)
761  
762   /* put out ray contribution to file */
763   void
764 < putcontrib(const DCOLOR cnt, FILE *fout)
764 > put_contrib(const DCOLOR cnt, FILE *fout)
765   {
766          float   fv[3];
767          COLR    cv;
# Line 681 | Line 794 | done_contrib(void)
794   {
795          int     i, j;
796          MODCONT *mp;
797 +        FILE    *fp;
798                                                  /* output modifiers in order */
799          for (i = 0; i < nmods; i++) {
800                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
801 <                for (j = 0; j < mp->nbins; j++)
802 <                        putcontrib(mp->cbin[j],
801 >                fp = getofile(mp->outspec, mp->modname, 0);
802 >                put_contrib(mp->cbin[0], fp);
803 >                if (mp->nbins > 3 &&            /* minor optimization */
804 >                                fp == getofile(mp->outspec, mp->modname, 1))
805 >                        for (j = 1; j < mp->nbins; j++)
806 >                                put_contrib(mp->cbin[j], fp);
807 >                else
808 >                        for (j = 1; j < mp->nbins; j++)
809 >                                put_contrib(mp->cbin[j],
810                                          getofile(mp->outspec, mp->modname, j));
811                                                  /* clear for next ray tree */
812                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
# Line 808 | Line 929 | wait_rproc(void)
929                                  if (rt->bsiz + BUFSIZ <= treebufsiz)
930                                          rt->bsiz = treebufsiz;
931                                  else
932 <                                        rt->bsiz = treebufsiz += BUFSIZ;
932 >                                        treebufsiz = rt->bsiz += BUFSIZ;
933                                  rt->buf = (char *)realloc(rt->buf, rt->bsiz);
934                          }
935                          if (rt->buf == NULL)
# Line 842 | Line 963 | get_rproc(void)
963  
964   /* trace ray contributions (main loop) */
965   void
966 < tracecontribs(FILE *fin)
966 > trace_contribs(FILE *fin)
967   {
968          char            inpbuf[128];
969          int             iblen;
970          struct rtproc   *rtp;
971                                                  /* loop over input */
972          while ((iblen = getinp(inpbuf, fin)) > 0) {
973 <                if (lastray+1 < lastray) {      /* counter rollover? */
973 >                if (lastray+1 < lastray ||      /* need reset? */
974 >                                queue_length() > 5*nrtprocs()) {
975                          while (wait_rproc() != NULL)
976                                  process_queue();
977                          lastdone = lastray = 0;
# Line 857 | Line 979 | tracecontribs(FILE *fin)
979                  rtp = get_rproc();              /* get avail. rtrace process */
980                  rtp->raynum = ++lastray;        /* assign ray to it */
981                  writebuf(rtp->pd.w, inpbuf, iblen);
982 <                if (!--raysleft)
982 >                if (raysleft && !--raysleft)
983                          break;
984                  process_queue();                /* catch up with results */
985          }
986          while (wait_rproc() != NULL)            /* process outstanding rays */
987                  process_queue();
988 <        if (raysleft > 0)
988 >        if (raysleft)
989                  error(USER, "unexpected EOF on input");
990 +        lu_done(&ofiletab);                     /* close output files */
991 + }
992 +
993 + /* seek on the given output file */
994 + static int
995 + myseeko(const LUENT *e, void *p)
996 + {
997 +        if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) {
998 +                sprintf(errmsg, "seek error on file '%s'", e->key);
999 +                error(SYSTEM, errmsg);
1000 +        }
1001 + }
1002 +
1003 + /* recover output if possible */
1004 + void
1005 + recover_output(FILE *fin)
1006 + {
1007 +        off_t   lastout = -1;
1008 +        int     outvsiz;
1009 +        char    *outvfmt;
1010 +        int     i, j;
1011 +        MODCONT *mp;
1012 +        int     ofl;
1013 +        char    oname[1024];
1014 +        LUENT   *ment, *oent;
1015 +        FILE    *fp;
1016 +        off_t   nvals;
1017 +
1018 +        switch (outfmt) {
1019 +        case 'a':
1020 +                error(USER, "cannot recover ASCII output");
1021 +                return;
1022 +        case 'f':
1023 +                outvsiz = sizeof(float)*3;
1024 +                outvfmt = "float";
1025 +                break;
1026 +        case 'd':
1027 +                outvsiz = sizeof(double)*3;
1028 +                outvfmt = "double";
1029 +                break;
1030 +        case 'c':
1031 +                outvsiz = sizeof(COLR);
1032 +                outvfmt = COLRFMT;
1033 +                break;
1034 +        default:
1035 +                error(INTERNAL, "botched output format");
1036 +                return;
1037 +        }
1038 +                                                /* check modifier outputs */
1039 +        for (i = 0; i < nmods; i++) {
1040 +                ment = lu_find(&modconttab,modname[i]);
1041 +                mp = (MODCONT *)ment->data;
1042 +                if (mp->outspec == NULL)
1043 +                        error(USER, "cannot recover from stdout");
1044 +                for (j = 0; ; j++) {            /* check each bin's file */
1045 +                        ofl = ofname(oname, mp->outspec, mp->modname, j);
1046 +                        if (ofl < 0)
1047 +                                error(USER, "bad output file specification");
1048 +                        oent = lu_find(&ofiletab, oname);
1049 +                        if (oent->data != NULL) { /* saw this one already */
1050 +                                if (ofl & OF_BIN)
1051 +                                        continue;
1052 +                                break;
1053 +                        }
1054 +                        if (oname[0] == '!')
1055 +                                error(USER, "cannot recover from command");
1056 +                                                /* open output */
1057 +                        fp = fopen(oname, "rb+");
1058 +                        if (fp == NULL) {
1059 +                                if (j)
1060 +                                        break;  /* assume end of modifier */
1061 +                                sprintf(errmsg, "missing recover file '%s'",
1062 +                                                oname);
1063 +                                error(USER, errmsg);
1064 +                        }
1065 +                        nvals = lseek(fileno(fp), 0, SEEK_END);
1066 +                        if (nvals <= 0) {
1067 +                                lastout = 0;    /* empty output, quit here */
1068 +                                fclose(fp);
1069 +                                break;
1070 +                        }
1071 +                        lseek(fileno(fp), 0, SEEK_SET);
1072 +                        if (header) {
1073 +                                int     xr, yr;
1074 +                                if (checkheader(fp, outvfmt, NULL) != 1) {
1075 +                                        sprintf(errmsg,
1076 +                                                "format mismatch for '%s'",
1077 +                                                        oname);
1078 +                                        error(USER, errmsg);
1079 +                                }
1080 +                                if (xres > 0 && yres > 0 &&
1081 +                                                (!fscnresolu(&xr, &yr, fp) ||
1082 +                                                        xr != xres ||
1083 +                                                        yr != yres)) {
1084 +                                        sprintf(errmsg,
1085 +                                                "resolution mismatch for '%s'",
1086 +                                                        oname);
1087 +                                        error(USER, errmsg);
1088 +                                }
1089 +                        }
1090 +                        nvals = (nvals - (off_t)ftell(fp)) / outvsiz;
1091 +                        if (lastout < 0 || nvals < lastout)
1092 +                                lastout = nvals;
1093 +                        if (oent->key == NULL) /* new entry */
1094 +                                oent->key = strcpy((char *)
1095 +                                                malloc(strlen(oname)+1), oname);
1096 +                        oent->data = (char *)fp;
1097 +                        if (!(ofl & OF_BIN))
1098 +                                break;          /* no bin separation */
1099 +                }
1100 +                if (!lastout) {                 /* empty output */
1101 +                        error(WARNING, "no previous data to recover");
1102 +                        lu_done(&ofiletab);     /* reclose all outputs */
1103 +                        return;
1104 +                }
1105 +                if (j > mp->nbins) {            /* preallocate modifier bins */
1106 +                        mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
1107 +                                                        (j-1)*sizeof(DCOLOR));
1108 +                        if (mp == NULL)
1109 +                                error(SYSTEM, "out of memory in recover_output");
1110 +                        memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
1111 +                        mp->nbins = j;
1112 +                        ment->data = (char *)mp;
1113 +                }
1114 +        }
1115 +        if (lastout < 0) {
1116 +                error(WARNING, "no output files to recover");
1117 +                return;
1118 +        }
1119 +        if (raysleft && lastout >= raysleft) {
1120 +                error(WARNING, "output appears to be complete");
1121 +                /* XXX should read & discard input? */
1122 +                quit(0);
1123 +        }
1124 +                                                /* seek on all files */
1125 +        nvals = lastout * outvsiz;
1126 +        lu_doall(&ofiletab, myseeko, &nvals);
1127 +                                                /* skip repeated input */
1128 +        for (nvals = 0; nvals < lastout; nvals++)
1129 +                if (getinp(oname, fin) <= 0)
1130 +                        error(USER, "unexpected EOF on input");
1131 +        lastray = lastdone = (unsigned long)lastout;
1132 +        if (raysleft)
1133 +                raysleft -= lastray;
1134   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines