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.2 by greg, Thu May 26 06:55:22 2005 UTC vs.
Revision 1.18 by greg, Fri Jun 10 20:38:38 2005 UTC

# Line 5 | Line 5 | static const char RCSid[] = "$Id$";
5   * Gather rtrace output to compute contributions from particular sources
6   */
7  
8 + #include  "standard.h"
9   #include  <ctype.h>
10 < #include  "rtio.h"
10 < #include  "rterror.h"
10 > #include  <signal.h>
11   #include  "platform.h"
12   #include  "rtprocess.h"
13   #include  "selcall.h"
# Line 49 | Line 49 | static void closefile(void *p) { fclose((FILE *)p); }
49  
50   LUTAB   ofiletab = LU_SINIT(free,closefile);    /* output file table */
51  
52 + #define OF_MODIFIER     01
53 + #define OF_BIN          02
54 +
55   FILE *getofile(const char *ospec, const char *mname, int bn);
56 + int ofname(char *oname, const char *ospec, const char *mname, int bn);
57 + void printheader(FILE *fout, const char *info);
58  
59   /*
60   * The rcont structure is used to manage i/o with a particular
# Line 64 | Line 69 | struct rtproc {
69          int             bsiz;           /* ray tree buffer length */
70          char            *buf;           /* ray tree buffer */
71          int             nbr;            /* number of bytes from rtrace */
72 < };                              /* rtrace process */
72 > };                              /* rtrace process buffer */
73  
74                                          /* rtrace command and defaults */
75 < char            *rtargv[256] = { "rtrace", "-dt", "0", "-dj", ".5", "-dr", "3",
76 <                                "-ab", "1", "-ad", "512", };
77 < int  rtargc = 11;
75 > char            *rtargv[256+2*MAXMODLIST] = { "rtrace",
76 >                                "-dj", ".5", "-dr", "3",
77 >                                "-ab", "1", "-ad", "128", };
78 > int  rtargc = 9;
79                                          /* overriding rtrace options */
80 < char            *myrtopts[] = { "-o~~TmWdp", "-h-",
81 <                                "-x", "1", "-y", "0",
76 <                                "-as", "0", "-aa", "0", NULL };
80 > char            *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0",
81 >                                "-dt", "0", "-as", "0", "-aa", "0", NULL };
82  
83   struct rtproc   rt0;                    /* head of rtrace process list */
84  
85   struct rtproc   *rt_unproc = NULL;      /* unprocessed ray trees */
86  
87   char    persistfn[] = "pfXXXXXX";       /* persist file name */
83 char    fmt[8];                         /* rtrace i/o format */
88  
89   int             gargc;                  /* global argc */
90   char            **gargv;                /* global argv */
# Line 95 | Line 99 | int            header = 1;             /* output header? */
99   int             xres = 0;               /* horiz. output resolution */
100   int             yres = 0;               /* vert. output resolution */
101  
102 < long            raysleft;               /* number of rays left to trace */
102 > unsigned long   raysleft;               /* number of rays left to trace */
103   long            waitflush;              /* how long until next flush */
104  
105   unsigned long   lastray = 0;            /* last ray number sent */
# Line 107 | Line 111 | const char     *modname[MAXMODLIST];   /* ordered modifier n
111   int             nmods = 0;              /* number of modifiers */
112  
113   MODCONT *addmodifier(char *modn, char *outf, char *binv);
114 + void addmodfile(char *fname, char *outf, char *binv);
115  
111 int done_rprocs(struct rtproc *rtp);
116   void init(int np);
117 < void tracecontribs(FILE *fp);
117 > int done_rprocs(struct rtproc *rtp);
118 > void recover_output(FILE *fin);
119 > void trace_contribs(FILE *fin);
120   struct rtproc *wait_rproc(void);
121   struct rtproc *get_rproc(void);
122 < void process_rays(struct rtproc *rtp);
122 > void queue_raytree(struct rtproc *rtp);
123 > void process_queue(void);
124  
125 < void putcontrib(const DCOLOR cnt, FILE *fout);
125 > void put_contrib(const DCOLOR cnt, FILE *fout);
126   void add_contrib(const char *modn);
127   void done_contrib(void);
128  
# Line 124 | Line 131 | static void
131   setformat(const char *fmt)
132   {
133          switch (fmt[0]) {
127        case 'a':
134          case 'f':
135          case 'd':
136 +                SET_FILE_BINARY(stdin);
137 +                /* fall through */
138 +        case 'a':
139                  inpfmt = fmt[0];
140                  break;
141          default:
# Line 157 | Line 166 | int
166   main(int argc, char *argv[])
167   {
168          int     nprocs = 1;
169 +        int     recover = 0;
170          char    *curout = NULL;
171          char    *binval = NULL;
172 +        char    fmt[8];
173          int     i, j;
174                                  /* global program name */
175          gargv = argv;
176 <                                /* set up calcomp mode */
176 >                                /* initialize calcomp routines */
177          esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW;
178          esupport &= ~(E_OUTCHAN);
179 +        varset("PI", ':', PI);
180                                  /* get our options */
181          for (i = 1; i < argc-1; i++) {
182                                                  /* expand arguments */
# Line 177 | Line 189 | main(int argc, char *argv[])
189                  }
190                  if (argv[i][0] == '-')
191                          switch (argv[i][1]) {
192 +                        case 'r':               /* recover output */
193 +                                if (argv[i][2]) break;
194 +                                recover++;
195 +                                continue;
196                          case 'n':               /* number of processes */
197                                  if (argv[i][2] || i >= argc-1) break;
198                                  nprocs = atoi(argv[++i]);
# Line 188 | Line 204 | main(int argc, char *argv[])
204                                  case '\0':
205                                          header = !header;
206                                          continue;
207 <                                case '+': case '1': case 'T': case 't':
207 >                                case '+': case '1':
208 >                                case 'T': case 't':
209 >                                case 'Y': case 'y':
210                                          header = 1;
211                                          continue;
212 <                                case '-': case '0': case 'F': case 'f':
212 >                                case '-': case '0':
213 >                                case 'F': case 'f':
214 >                                case 'N': case 'n':
215                                          header = 0;
216                                          continue;
217                                  }
218                                  break;
219                          case 'f':               /* file or i/o format */
220                                  if (!argv[i][2]) {
221 +                                        char    *fpath;
222                                          if (i >= argc-1) break;
223 <                                        fcompile(argv[++i]);
223 >                                        fpath = getpath(argv[++i],
224 >                                                        getrlibpath(), R_OK);
225 >                                        if (fpath == NULL) {
226 >                                                sprintf(errmsg,
227 >                                                        "cannot find file '%s'",
228 >                                                                argv[i]);
229 >                                                error(USER, errmsg);
230 >                                        }
231 >                                        fcompile(fpath);
232                                          continue;
233                                  }
234                                  setformat(argv[i]+2);
# Line 230 | Line 259 | main(int argc, char *argv[])
259                                  rtargv[rtargc++] = argv[++i];
260                                  addmodifier(argv[i], curout, binval);
261                                  continue;
262 +                        case 'M':               /* modifier file */
263 +                                if (argv[i][2] || i >= argc-1) break;
264 +                                addmodfile(argv[++i], curout, binval);
265 +                                continue;
266                          }
267                  rtargv[rtargc++] = argv[i];     /* assume rtrace option */
268          }
# Line 265 | Line 298 | main(int argc, char *argv[])
298                  execv(rtpath, rtargv);
299                  perror(rtpath); /* execv() should not return */
300                  exit(1);
301 <        } else if (nprocs > 1) {        /* add persist file if parallel */
301 >        }
302 >        if (nprocs > 1) {       /* add persist file if parallel */
303                  rtargv[rtargc++] = "-PP";
304                  rtargv[rtargc++] = mktemp(persistfn);
305          }
# Line 279 | Line 313 | main(int argc, char *argv[])
313          rtargv[rtargc] = NULL;
314                                  /* start rtrace & compute contributions */
315          init(nprocs);
316 <        tracecontribs(stdin);
316 >        if (recover)            /* perform recovery if requested */
317 >                recover_output(stdin);
318 >        trace_contribs(stdin);
319          quit(0);
320   }
321  
# Line 331 | Line 367 | quit(int status)
367          exit(status);                   /* flushes all output streams */
368   }
369  
370 < /* start rtrace and initialize buffers */
370 > /* start rtrace processes and initialize */
371   void
372   init(int np)
373   {
# Line 345 | Line 381 | init(int np)
381          scompile("Dx=$1;Dy=$2;Dz=$3;", NULL, 0);
382          scompile("Px=$4;Py=$5;Pz=$6;", NULL, 0);
383                                          /* set up signal handling */
384 < #ifdef SIGPIPE /* not present on Windows */
384 >        signal(SIGINT, quit);
385 > #ifdef SIGHUP
386 >        signal(SIGHUP, quit);
387 > #endif
388 > #ifdef SIGTERM
389 >        signal(SIGTERM, quit);
390 > #endif
391 > #ifdef SIGPIPE
392          signal(SIGPIPE, quit);
393   #endif
394          rtp = &rt0;                     /* start rtrace process(es) */
# Line 377 | Line 420 | init(int np)
420          rtp->next = NULL;               /* terminate list */
421          if (yres > 0) {
422                  if (xres > 0)
423 <                        raysleft = xres*yres;
423 >                        raysleft = (unsigned long)xres*yres;
424                  else
425                          raysleft = yres;
426          } else
# Line 415 | Line 458 | addmodifier(char *modn, char *outf, char *binv)
458          return mp;
459   }
460  
461 + /* add modifiers from a file list */
462 + void
463 + addmodfile(char *fname, char *outf, char *binv)
464 + {
465 +        char    *mname[MAXMODLIST];
466 +        int     i;
467 +                                        /* load the file & store strings */
468 +        wordfile(mname, fname);
469 +        for (i = 0; mname[i]; i++)      /* add each one */
470 +                addmodifier(mname[i], outf, binv);
471 + }
472 +
473   /* put string to stderr */
474   void
475   eputs(char  *s)
# Line 430 | Line 485 | eputs(char  *s)
485          midline = s[strlen(s)-1] != '\n';
486   }
487  
488 + /* construct output file name and return flags whether modifier/bin present */
489 + int
490 + ofname(char *oname, const char *ospec, const char *mname, int bn)
491 + {
492 +        const char      *mnp = NULL;
493 +        const char      *bnp = NULL;
494 +        const char      *cp;
495 +        
496 +        if (ospec == NULL)
497 +                return -1;
498 +        for (cp = ospec; *cp; cp++)             /* check format position(s) */
499 +                if (*cp == '%') {
500 +                        do
501 +                                ++cp;
502 +                        while (isdigit(*cp));
503 +                        switch (*cp) {
504 +                        case '%':
505 +                                break;
506 +                        case 's':
507 +                                if (mnp != NULL)
508 +                                        return -1;
509 +                                mnp = cp;
510 +                                break;
511 +                        case 'd':
512 +                                if (bnp != NULL)
513 +                                        return -1;
514 +                                bnp = cp;
515 +                                break;
516 +                        default:
517 +                                return -1;
518 +                        }
519 +                }
520 +        if (mnp != NULL) {                      /* create file name */
521 +                if (bnp != NULL) {
522 +                        if (bnp > mnp)
523 +                                sprintf(oname, ospec, mname, bn);
524 +                        else
525 +                                sprintf(oname, ospec, bn, mname);
526 +                        return OF_MODIFIER|OF_BIN;
527 +                }
528 +                sprintf(oname, ospec, mname);
529 +                return OF_MODIFIER;
530 +        }
531 +        if (bnp != NULL) {
532 +                sprintf(oname, ospec, bn);
533 +                return OF_BIN;
534 +        }
535 +        strcpy(oname, ospec);
536 +        return 0;
537 + }
538 +
539   /* write header to the given output stream */
540   void
541 < printheader(FILE *fout)
541 > printheader(FILE *fout, const char *info)
542   {
543          extern char     VersionID[];
544          FILE            *fin = fopen(octree, "r");
# Line 444 | Line 550 | printheader(FILE *fout)
550          printargs(gargc-1, gargv, fout);        /* add our command */
551          fprintf(fout, "SOFTWARE= %s\n", VersionID);
552          fputnow(fout);
553 +        if (info != NULL)                       /* add extra info if given */
554 +                fputs(info, fout);
555          switch (outfmt) {                       /* add output format */
556          case 'a':
557                  fputformat("ascii", fout);
# Line 470 | Line 578 | printheader(FILE *fout)
578   FILE *
579   getofile(const char *ospec, const char *mname, int bn)
580   {
581 <        const char      *mnp = NULL;
582 <        const char      *bnp = NULL;
475 <        const char      *cp;
476 <        char            ofname[1024];
581 >        int             ofl;
582 >        char            oname[1024];
583          LUENT           *lep;
584          
585          if (ospec == NULL) {                    /* use stdout? */
586 <                if (!using_stdout && header)
587 <                        printheader(stdout);
586 >                if (!using_stdout) {
587 >                        if (outfmt != 'a')
588 >                                SET_FILE_BINARY(stdout);
589 >                        if (header)
590 >                                printheader(stdout, NULL);
591 >                }
592                  using_stdout = 1;
593                  return stdout;
594          }
595 <        for (cp = ospec; *cp; cp++)             /* check format position(s) */
596 <                if (*cp == '%') {
597 <                        do
598 <                                ++cp;
599 <                        while (isdigit(*cp));
600 <                        switch (*cp) {
491 <                        case '%':
492 <                                break;
493 <                        case 's':
494 <                                if (mnp != NULL)
495 <                                        goto badspec;
496 <                                mnp = cp;
497 <                                break;
498 <                        case 'd':
499 <                                if (bnp != NULL)
500 <                                        goto badspec;
501 <                                bnp = cp;
502 <                                break;
503 <                        default:
504 <                                goto badspec;
505 <                        }
506 <                }
507 <        if (mnp != NULL) {                      /* create file name */
508 <                if (bnp != NULL) {
509 <                        if (bnp > mnp)
510 <                                sprintf(ofname, ospec, mname, bn);
511 <                        else
512 <                                sprintf(ofname, ospec, bn, mname);
513 <                } else
514 <                        sprintf(ofname, ospec, mname);
515 <        } else if (bnp != NULL)
516 <                sprintf(ofname, ospec, bn);
517 <        else
518 <                strcpy(ofname, ospec);
519 <        lep = lu_find(&ofiletab, ofname);       /* look it up */
595 >        ofl = ofname(oname, ospec, mname, bn);  /* get output name */
596 >        if (ofl < 0) {
597 >                sprintf(errmsg, "bad output format '%s'", ospec);
598 >                error(USER, errmsg);
599 >        }
600 >        lep = lu_find(&ofiletab, oname);        /* look it up */
601          if (lep->key == NULL)                   /* new entry */
602 <                lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname);
602 >                lep->key = strcpy((char *)malloc(strlen(oname)+1), oname);
603          if (lep->data == NULL) {                /* open output file */
604 <                FILE            *fp = fopen(ofname, "w");
604 >                FILE            *fp;
605                  int             i;
606 +                if (oname[0] == '!')            /* output to command */
607 +                        fp = popen(oname+1, "w");
608 +                else
609 +                        fp = fopen(oname, "w");
610                  if (fp == NULL) {
611 <                        sprintf(errmsg, "cannot open '%s' for writing", ofname);
611 >                        sprintf(errmsg, "cannot open '%s' for writing", oname);
612                          error(SYSTEM, errmsg);
613                  }
614 <                if (header)
615 <                        printheader(fp);
614 >                if (outfmt != 'a')
615 >                        SET_FILE_BINARY(fp);
616 >                if (header) {
617 >                        char    info[512];
618 >                        char    *cp = info;
619 >                        if (ofl & OF_MODIFIER) {
620 >                                sprintf(cp, "MODIFIER=%s\n", mname);
621 >                                while (*cp) ++cp;
622 >                        }
623 >                        if (ofl & OF_BIN) {
624 >                                sprintf(cp, "BIN=%d\n", bn);
625 >                                while (*cp) ++cp;
626 >                        }
627 >                        *cp = '\0';
628 >                        printheader(fp, info);
629 >                }
630                                                  /* play catch-up */
631                  for (i = 0; i < lastdone; i++) {
632                          static const DCOLOR     nocontrib = BLKCOLOR;
633 <                        putcontrib(nocontrib, fp);
633 >                        put_contrib(nocontrib, fp);
634                          if (outfmt == 'a')
635                                  putc('\n', fp);
636                  }
# Line 540 | Line 639 | getofile(const char *ospec, const char *mname, int bn)
639                  lep->data = (char *)fp;
640          }
641          return (FILE *)lep->data;               /* return open file pointer */
543 badspec:
544        sprintf(errmsg, "bad output format '%s'", ospec);
545        error(USER, errmsg);
546        return NULL;            /* pro forma return */
642   }
643  
644   /* read input ray into buffer */
645   int
646   getinp(char *buf, FILE *fp)
647   {
648 +        char    *cp;
649 +        int     i;
650 +
651          switch (inpfmt) {
652          case 'a':
653 <                if (fgets(buf, 128, fp) == NULL)
654 <                        return 0;
653 >                cp = buf;               /* make sure we get 6 floats */
654 >                for (i = 0; i < 6; i++) {
655 >                        if (fgetword(cp, buf+127-cp, fp) == NULL)
656 >                                return 0;
657 >                        if ((cp = fskip(cp)) == NULL || *cp)
658 >                                return 0;
659 >                        *cp++ = ' ';
660 >                }
661 >                getc(fp);               /* get/put eol */
662 >                *cp-- = '\0'; *cp = '\n';
663                  return strlen(buf);
664          case 'f':
665                  if (fread(buf, sizeof(float), 6, fp) < 6)
# Line 626 | Line 732 | puteol(const LUENT *e, void *p)
732  
733   /* put out ray contribution to file */
734   void
735 < putcontrib(const DCOLOR cnt, FILE *fout)
735 > put_contrib(const DCOLOR cnt, FILE *fout)
736   {
737          float   fv[3];
738          COLR    cv;
# Line 659 | Line 765 | done_contrib(void)
765   {
766          int     i, j;
767          MODCONT *mp;
768 +        FILE    *fp;
769                                                  /* output modifiers in order */
770          for (i = 0; i < nmods; i++) {
771                  mp = (MODCONT *)lu_find(&modconttab,modname[i])->data;
772 <                for (j = 0; j < mp->nbins; j++)
773 <                        putcontrib(mp->cbin[j],
772 >                fp = getofile(mp->outspec, mp->modname, 0);
773 >                put_contrib(mp->cbin[0], fp);
774 >                if (mp->nbins > 3 &&            /* minor optimization */
775 >                                fp == getofile(mp->outspec, mp->modname, 1))
776 >                        for (j = 1; j < mp->nbins; j++)
777 >                                put_contrib(mp->cbin[j], fp);
778 >                else
779 >                        for (j = 1; j < mp->nbins; j++)
780 >                                put_contrib(mp->cbin[j],
781                                          getofile(mp->outspec, mp->modname, j));
782                                                  /* clear for next ray tree */
783                  memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
# Line 679 | Line 793 | done_contrib(void)
793          }
794   }
795  
796 < /* process (or save) ray tree produced by rtrace process */
796 > /* queue completed ray tree produced by rtrace process */
797   void
798 < process_rays(struct rtproc *rtp)
798 > queue_raytree(struct rtproc *rtp)
799   {
800 <        struct rtproc   *rtu;
801 <                                        /* check if time to process it */
802 <        if (rtp->raynum == lastdone+1) {
800 >        struct rtproc   *rtu, *rtl = NULL;
801 >                                        /* insert following ray order */
802 >        for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next)
803 >                if (rtp->raynum < rtu->raynum)
804 >                        break;
805 >        rtu = (struct rtproc *)malloc(sizeof(struct rtproc));
806 >        if (rtu == NULL)
807 >                error(SYSTEM, "out of memory in queue_raytree");
808 >        *rtu = *rtp;
809 >        if (rtl == NULL) {
810 >                rtu->next = rt_unproc;
811 >                rt_unproc = rtu;
812 >        } else {
813 >                rtu->next = rtl->next;
814 >                rtl->next = rtu;
815 >        }
816 >        rtp->raynum = 0;                /* clear path for next ray tree */
817 >        rtp->bsiz = 0;
818 >        rtp->buf = NULL;
819 >        rtp->nbr = 0;
820 > }
821 >
822 > /* process completed ray trees from our queue */
823 > void
824 > process_queue(void)
825 > {
826 >        char    modname[128];
827 >                                        /* ray-ordered queue */
828 >        while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) {
829 >                struct rtproc   *rtp = rt_unproc;
830                  int             n = rtp->nbr;
831                  const char      *cp = rtp->buf;
691                char            modname[128];
832                  while (n > 0) {         /* process rays */
833                          register char   *mnp = modname;
834                                          /* skip leading tabs */
# Line 712 | Line 852 | process_rays(struct rtproc *rtp)
852                  }
853                  done_contrib();         /* sum up contributions & output */
854                  lastdone = rtp->raynum;
855 <                free(rtp->buf);
856 <                                        /* catch up with unprocessed list */
857 <                while (rt_unproc != NULL && rt_unproc->raynum == lastdone+1) {
718 <                        process_rays(rt_unproc);
719 <                        rt_unproc = (rtu=rt_unproc)->next;
720 <                        free(rtu);
721 <                }
722 <        } else {                        /* else insert in unprocessed list */
723 <                struct rtproc   *rtl = NULL;
724 <                for (rtu = rt_unproc; rtu != NULL; rtu = (rtl=rtu)->next)
725 <                        if (rtp->raynum < rtu->raynum)
726 <                                break;
727 <                rtu = (struct rtproc *)malloc(sizeof(struct rtproc));
728 <                if (rtu == NULL)
729 <                        error(SYSTEM, "out of memory in process_rays");
730 <                *rtu = *rtp;
731 <                if (rtl == NULL) {
732 <                        rtu->next = rt_unproc;
733 <                        rt_unproc = rtu;
734 <                } else {
735 <                        rtu->next = rtl->next;
736 <                        rtl->next = rtu;
737 <                }
855 >                free(rtp->buf);         /* free up buffer space */
856 >                rt_unproc = rtp->next;
857 >                free(rtp);              /* done with this ray tree */
858          }
739        rtp->raynum = 0;                /* clear path for next ray tree */
740        rtp->bsiz = 0;
741        rtp->buf = NULL;
742        rtp->nbr = 0;
859   }
860  
861   /* wait for rtrace process to finish with ray tree */
# Line 796 | Line 912 | wait_rproc(void)
912                          if (rt->nbr >= 4 && !memcmp(rt->buf+rt->nbr-4,
913                                                          "~\t~\t", 4)) {
914                                  rt->nbr -= 4;   /* elide terminator */
915 <                                process_rays(rt);
915 >                                queue_raytree(rt);
916                                  rtfree = rt;    /* ready for next ray */
917                          }
918                  }
# Line 818 | Line 934 | get_rproc(void)
934  
935   /* trace ray contributions (main loop) */
936   void
937 < tracecontribs(FILE *fin)
937 > trace_contribs(FILE *fin)
938   {
939          char            inpbuf[128];
940          int             iblen;
# Line 827 | Line 943 | tracecontribs(FILE *fin)
943          while ((iblen = getinp(inpbuf, fin)) > 0) {
944                  if (lastray+1 < lastray) {      /* counter rollover? */
945                          while (wait_rproc() != NULL)
946 <                                ;
946 >                                process_queue();
947                          lastdone = lastray = 0;
948                  }
949                  rtp = get_rproc();              /* get avail. rtrace process */
950 <                rtp->raynum = ++lastray;        /* assign this ray to it */
950 >                rtp->raynum = ++lastray;        /* assign ray to it */
951                  writebuf(rtp->pd.w, inpbuf, iblen);
952 <                if (!--raysleft)
953 <                        break;                  /* explicit EOF */
952 >                if (raysleft && !--raysleft)
953 >                        break;
954 >                process_queue();                /* catch up with results */
955          }
956          while (wait_rproc() != NULL)            /* process outstanding rays */
957 <                ;
958 <        if (raysleft > 0)
957 >                process_queue();
958 >        if (raysleft)
959                  error(USER, "unexpected EOF on input");
960 +        lu_done(&ofiletab);                     /* close output files */
961 + }
962 +
963 + /* seek on the given output file */
964 + static int
965 + myseeko(const LUENT *e, void *p)
966 + {
967 +        if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) {
968 +                sprintf(errmsg, "seek error on file '%s'", e->key);
969 +                error(SYSTEM, errmsg);
970 +        }
971 + }
972 +
973 + /* recover output if possible */
974 + void
975 + recover_output(FILE *fin)
976 + {
977 +        off_t   lastout = -1;
978 +        int     outvsiz;
979 +        char    *outvfmt;
980 +        int     i, j;
981 +        MODCONT *mp;
982 +        int     ofl;
983 +        char    oname[1024];
984 +        LUENT   *ment, *oent;
985 +        FILE    *fp;
986 +        off_t   nvals;
987 +
988 +        switch (outfmt) {
989 +        case 'a':
990 +                error(USER, "cannot recover ASCII output");
991 +                return;
992 +        case 'f':
993 +                outvsiz = sizeof(float)*3;
994 +                outvfmt = "float";
995 +                break;
996 +        case 'd':
997 +                outvsiz = sizeof(double)*3;
998 +                outvfmt = "double";
999 +                break;
1000 +        case 'c':
1001 +                outvsiz = sizeof(COLR);
1002 +                outvfmt = COLRFMT;
1003 +                break;
1004 +        default:
1005 +                error(INTERNAL, "botched output format");
1006 +                return;
1007 +        }
1008 +                                                /* check modifier outputs */
1009 +        for (i = 0; i < nmods; i++) {
1010 +                ment = lu_find(&modconttab,modname[i]);
1011 +                mp = (MODCONT *)ment->data;
1012 +                if (mp->outspec == NULL)
1013 +                        error(USER, "cannot recover from stdout");
1014 +                for (j = 0; ; j++) {            /* check each bin's file */
1015 +                        ofl = ofname(oname, mp->outspec, mp->modname, j);
1016 +                        if (ofl < 0)
1017 +                                error(USER, "bad output file specification");
1018 +                        oent = lu_find(&ofiletab, oname);
1019 +                        if (oent->data != NULL) { /* saw this one already */
1020 +                                if (ofl & OF_BIN)
1021 +                                        continue;
1022 +                                break;
1023 +                        }
1024 +                        if (oname[0] == '!')
1025 +                                error(USER, "cannot recover from command");
1026 +                                                /* open output */
1027 +                        fp = fopen(oname, "rb+");
1028 +                        if (fp == NULL)
1029 +                                break;          /* must be end of modifier */
1030 +                        nvals = lseek(fileno(fp), 0, SEEK_END);
1031 +                        if (nvals <= 0) {
1032 +                                lastout = 0;    /* empty output, quit here */
1033 +                                fclose(fp);
1034 +                                break;
1035 +                        }
1036 +                        lseek(fileno(fp), 0, SEEK_SET);
1037 +                        if (header) {
1038 +                                int     xr, yr;
1039 +                                if (checkheader(fp, outvfmt, NULL) != 1) {
1040 +                                        sprintf(errmsg,
1041 +                                                "format mismatch for '%s'",
1042 +                                                        oname);
1043 +                                        error(USER, errmsg);
1044 +                                }
1045 +                                if (xres > 0 && yres > 0 &&
1046 +                                                (!fscnresolu(&xr, &yr, fp) ||
1047 +                                                        xr != xres ||
1048 +                                                        yr != yres)) {
1049 +                                        sprintf(errmsg,
1050 +                                                "resolution mismatch for '%s'",
1051 +                                                        oname);
1052 +                                        error(USER, errmsg);
1053 +                                }
1054 +                        }
1055 +                        nvals = (nvals - (off_t)ftell(fp)) / outvsiz;
1056 +                        if (lastout < 0 || nvals < lastout)
1057 +                                lastout = nvals;
1058 +                        if (oent->key == NULL) /* new entry */
1059 +                                oent->key = strcpy((char *)
1060 +                                                malloc(strlen(oname)+1), oname);
1061 +                        oent->data = (char *)fp;
1062 +                        if (!(ofl & OF_BIN))
1063 +                                break;          /* no bin separation */
1064 +                }
1065 +                if (!lastout) {                 /* empty output */
1066 +                        error(WARNING, "no previous data to recover");
1067 +                        lu_done(&ofiletab);     /* reclose all outputs */
1068 +                        return;
1069 +                }
1070 +                if (j > mp->nbins) {            /* preallocate modifier bins */
1071 +                        mp = (MODCONT *)realloc(mp, sizeof(MODCONT) +
1072 +                                                        (j-1)*sizeof(DCOLOR));
1073 +                        if (mp == NULL)
1074 +                                error(SYSTEM, "out of memory in recover_output");
1075 +                        memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins);
1076 +                        mp->nbins = j;
1077 +                        ment->data = (char *)mp;
1078 +                }
1079 +        }
1080 +        if (lastout < 0) {
1081 +                error(WARNING, "no output files to recover");
1082 +                return;
1083 +        }
1084 +                                                /* seek on all files */
1085 +        nvals = lastout * outvsiz;
1086 +        lu_doall(&ofiletab, myseeko, &nvals);
1087 +                                                /* discard input */
1088 +        for (nvals = 0; nvals < lastout; nvals++)
1089 +                if (getinp(oname, fin) <= 0)
1090 +                        error(USER, "unexpected EOF on input");
1091 +        lastray = lastdone = (unsigned long)lastout;
1092 +        if (raysleft)
1093 +                raysleft -= lastray;
1094   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines