--- ray/src/util/rtcontrib.c 2005/06/02 18:51:46 1.11 +++ ray/src/util/rtcontrib.c 2005/10/21 01:12:59 1.39 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rtcontrib.c,v 1.11 2005/06/02 18:51:46 greg Exp $"; +static const char RCSid[] = "$Id: rtcontrib.c,v 1.39 2005/10/21 01:12:59 greg Exp $"; #endif /* * Gather rtrace output to compute contributions from particular sources @@ -16,16 +16,18 @@ static const char RCSid[] = "$Id: rtcontrib.c,v 1.11 2 #include "lookup.h" #include "calcomp.h" +#ifndef MAXMODLIST #define MAXMODLIST 1024 /* maximum modifiers we'll track */ +#endif int treebufsiz = BUFSIZ; /* current tree buffer size */ typedef double DCOLOR[3]; /* double-precision color */ /* - * The modcont structure is used to accumulate ray contributions + * The MODCONT structure is used to accumulate ray contributions * for a particular modifier, which may be subdivided into bins - * if binv is non-NULL. If outspec contains a %s in it, this will + * if binv is non-zero. If outspec contains a %s in it, this will * be replaced with the modifier name. If outspec contains a %d in it, * this will be used to create one output file per bin, otherwise all bins * will be written to the same file, in order. If the global outfmt @@ -44,13 +46,43 @@ static void mcfree(void *p) { epfree((*(MODCONT *)p).b LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */ -/* close output stream */ -static void closefile(void *p) { fclose((FILE *)p); } +#define CNT_UNKNOWN 0 /* unknown record length */ +#define CNT_PIPE (-1) /* output to a pipe */ +/* + * The STREAMOUT structure holds an open FILE pointer and a count of + * the number of RGB triplets per record, or CNT_UNKNOWN (0) if + * unknown or CNT_PIPE (-1) if writing to a command. + */ +typedef struct { + FILE *ofp; /* output file pointer */ + int reclen; /* triplets/record */ +} STREAMOUT; -LUTAB ofiletab = LU_SINIT(free,closefile); /* output file table */ +/* close output stream and free record */ +static void +closestream(void *p) +{ + STREAMOUT *sop = (STREAMOUT *)p; + int status; + if (sop->reclen == CNT_PIPE) + status = pclose(sop->ofp); + else + status = fclose(sop->ofp); + if (status) + error(SYSTEM, "error closing output stream"); + free(p); +} -FILE *getofile(const char *ospec, const char *mname, int bn); +LUTAB ofiletab = LU_SINIT(free,closestream); /* output file table */ +#define OF_MODIFIER 01 +#define OF_BIN 02 + +STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int noopen); +int ofname(char *oname, const char *ospec, const char *mname, int bn); +void printheader(FILE *fout, const char *info); +void printresolu(FILE *fout); + /* * The rcont structure is used to manage i/o with a particular * rtrace child process. Input is passed unchanged from stdin, @@ -67,9 +99,10 @@ struct rtproc { }; /* rtrace process buffer */ /* rtrace command and defaults */ -char *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3", - "-ab", "1", "-ad", "128", "-lr", "-10", }; -int rtargc = 11; +char *rtargv[256+2*MAXMODLIST] = { "rtrace", + "-dj", ".5", "-dr", "3", + "-ab", "1", "-ad", "128", }; +int rtargc = 9; /* overriding rtrace options */ char *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0", "-dt", "0", "-as", "0", "-aa", "0", NULL }; @@ -90,10 +123,11 @@ int inpfmt = 'a'; /* input format */ int outfmt = 'a'; /* output format */ int header = 1; /* output header? */ +int force_open = 0; /* truncate existing output? */ int xres = 0; /* horiz. output resolution */ int yres = 0; /* vert. output resolution */ -long raysleft; /* number of rays left to trace */ +unsigned long raysleft; /* number of rays left to trace */ long waitflush; /* how long until next flush */ unsigned long lastray = 0; /* last ray number sent */ @@ -104,20 +138,37 @@ int using_stdout = 0; /* are we using stdout? */ const char *modname[MAXMODLIST]; /* ordered modifier name list */ int nmods = 0; /* number of modifiers */ -MODCONT *addmodifier(char *modn, char *outf, char *binv); +#define queue_length() (lastray - lastdone) +MODCONT *growmodifier(MODCONT *mp, int nb); +MODCONT *addmodifier(char *modn, char *outf, char *binv, int bincnt); +void addmodfile(char *fname, char *outf, char *binv, int bincnt); + void init(int np); int done_rprocs(struct rtproc *rtp); -void trace_contribs(FILE *fp); +void recover_output(FILE *fin); +void trace_contribs(FILE *fin); struct rtproc *wait_rproc(void); struct rtproc *get_rproc(void); void queue_raytree(struct rtproc *rtp); void process_queue(void); -void putcontrib(const DCOLOR cnt, FILE *fout); +void put_contrib(const DCOLOR cnt, FILE *fout); void add_contrib(const char *modn); void done_contrib(void); +/* return number of open rtrace processes */ +static int +nrtprocs(void) +{ + int nrtp = 0; + struct rtproc *rtp; + + for (rtp = &rt0; rtp != NULL; rtp = rtp->next) + nrtp += rtp->pd.running; + return(nrtp); +} + /* set input/output format */ static void setformat(const char *fmt) @@ -158,10 +209,19 @@ int main(int argc, char *argv[]) { int nprocs = 1; + int recover = 0; char *curout = NULL; char *binval = NULL; + int bincnt = 0; char fmt[8]; int i, j; + /* need at least one argument */ + if (argc < 2) { + fprintf(stderr, +"Usage: %s [-n nprocs][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n", + argv[0]); + exit(1); + } /* global program name */ gargv = argv; /* initialize calcomp routines */ @@ -180,8 +240,12 @@ main(int argc, char *argv[]) } if (argv[i][0] == '-') switch (argv[i][1]) { + case 'r': /* recover output */ + if (argv[i][2]) break; + recover++; + continue; case 'n': /* number of processes */ - if (argv[i][2] || i >= argc-1) break; + if (argv[i][2] || i >= argc-2) break; nprocs = atoi(argv[++i]); if (nprocs <= 0) error(USER, "illegal number of processes"); @@ -203,10 +267,10 @@ main(int argc, char *argv[]) continue; } break; - case 'f': /* file or i/o format */ + case 'f': /* file or force or format */ if (!argv[i][2]) { char *fpath; - if (i >= argc-1) break; + if (i >= argc-2) break; fpath = getpath(argv[++i], getrlibpath(), R_OK); if (fpath == NULL) { @@ -218,34 +282,52 @@ main(int argc, char *argv[]) fcompile(fpath); continue; } + if (argv[i][2] == 'o') { + force_open++; + continue; + } setformat(argv[i]+2); continue; case 'e': /* expression */ - if (argv[i][2] || i >= argc-1) break; + if (argv[i][2] || i >= argc-2) break; scompile(argv[++i], NULL, 0); continue; case 'o': /* output file spec. */ - if (argv[i][2] || i >= argc-1) break; + if (argv[i][2] || i >= argc-2) break; curout = argv[++i]; continue; case 'x': /* horiz. output resolution */ - if (argv[i][2] || i >= argc-1) break; + if (argv[i][2] || i >= argc-2) break; xres = atoi(argv[++i]); continue; case 'y': /* vert. output resolution */ - if (argv[i][2] || i >= argc-1) break; + if (argv[i][2] || i >= argc-2) break; yres = atoi(argv[++i]); continue; - case 'b': /* bin expression */ - if (argv[i][2] || i >= argc-1) break; + case 'b': /* bin expression/count */ + if (i >= argc-2) break; + if (argv[i][2] == 'n') { + bincnt = atoi(argv[++i]); + continue; + } + if (argv[i][2]) break; binval = argv[++i]; continue; case 'm': /* modifier name */ - if (argv[i][2] || i >= argc-1) break; + if (argv[i][2] || i >= argc-2) break; rtargv[rtargc++] = "-ti"; rtargv[rtargc++] = argv[++i]; - addmodifier(argv[i], curout, binval); + addmodifier(argv[i], curout, binval, bincnt); continue; + case 'M': /* modifier file */ + if (argv[i][2] || i >= argc-2) break; + rtargv[rtargc++] = "-tI"; + rtargv[rtargc++] = argv[++i]; + addmodfile(argv[i], curout, binval, bincnt); + continue; + case 'P': /* persist file */ + error(USER, "persist file is automatic"); + break; } rtargv[rtargc++] = argv[i]; /* assume rtrace option */ } @@ -294,18 +376,23 @@ main(int argc, char *argv[]) error(USER, "missing octree argument"); rtargv[rtargc++] = octree = argv[i]; rtargv[rtargc] = NULL; - /* start rtrace & compute contributions */ + /* start rtrace */ init(nprocs); - trace_contribs(stdin); + if (recover) /* perform recovery if requested */ + recover_output(stdin); + trace_contribs(stdin); /* compute contributions */ quit(0); } +#ifndef SIGALRM +#define SIGALRM SIGTERM +#endif /* kill persistent rtrace process */ static void killpersist(void) { FILE *fp = fopen(persistfn, "r"); - int pid; + RT_PID pid; if (fp == NULL) return; @@ -401,7 +488,7 @@ init(int np) rtp->next = NULL; /* terminate list */ if (yres > 0) { if (xres > 0) - raysleft = xres*yres; + raysleft = (unsigned long)xres*yres; else raysleft = yres; } else @@ -409,12 +496,27 @@ init(int np) waitflush = xres; } +/* grow modifier to accommodate more bins */ +MODCONT * +growmodifier(MODCONT *mp, int nb) +{ + if (nb <= mp->nbins) + return mp; + mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + sizeof(DCOLOR)*(nb-1)); + if (mp == NULL) + error(SYSTEM, "out of memory in growmodifier"); + memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(nb-mp->nbins)); + mp->nbins = nb; + return mp; +} + /* add modifier to our list to track */ MODCONT * -addmodifier(char *modn, char *outf, char *binv) +addmodifier(char *modn, char *outf, char *binv, int bincnt) { LUENT *lep = lu_find(&modconttab, modn); MODCONT *mp; + int i; if (lep->data != NULL) { sprintf(errmsg, "duplicate modifier '%s'", modn); @@ -427,7 +529,6 @@ addmodifier(char *modn, char *outf, char *binv) mp = (MODCONT *)malloc(sizeof(MODCONT)); if (mp == NULL) error(SYSTEM, "out of memory in addmodifier"); - lep->data = (char *)mp; mp->outspec = outf; /* XXX assumes static string */ mp->modname = modn; /* XXX assumes static string */ if (binv != NULL) @@ -436,9 +537,32 @@ addmodifier(char *modn, char *outf, char *binv) mp->binv = eparse("0"); mp->nbins = 1; setcolor(mp->cbin[0], 0., 0., 0.); + if (mp->binv->type == NUM) /* assume one bin if constant */ + bincnt = 1; + else if (bincnt > 1) + mp = growmodifier(mp, bincnt); + lep->data = (char *)mp; + /* allocate output streams */ + for (i = outf==NULL || outf[0]=='!' ? 0 : bincnt; i--; ) + getostream(mp->outspec, mp->modname, i, 1); return mp; } +/* add modifiers from a file list */ +void +addmodfile(char *fname, char *outf, char *binv, int bincnt) +{ + char *mname[MAXMODLIST]; + int i; + /* find the file & store strings */ + if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) { + sprintf(errmsg, "cannot find modifier file '%s'", fname); + error(SYSTEM, errmsg); + } + for (i = 0; mname[i]; i++) /* add each one */ + addmodifier(mname[i], outf, binv, bincnt); +} + /* put string to stderr */ void eputs(char *s) @@ -454,9 +578,60 @@ eputs(char *s) midline = s[strlen(s)-1] != '\n'; } +/* construct output file name and return flags whether modifier/bin present */ +int +ofname(char *oname, const char *ospec, const char *mname, int bn) +{ + const char *mnp = NULL; + const char *bnp = NULL; + const char *cp; + + if (ospec == NULL) + return -1; + for (cp = ospec; *cp; cp++) /* check format position(s) */ + if (*cp == '%') { + do + ++cp; + while (isdigit(*cp)); + switch (*cp) { + case '%': + break; + case 's': + if (mnp != NULL) + return -1; + mnp = cp; + break; + case 'd': + if (bnp != NULL) + return -1; + bnp = cp; + break; + default: + return -1; + } + } + if (mnp != NULL) { /* create file name */ + if (bnp != NULL) { + if (bnp > mnp) + sprintf(oname, ospec, mname, bn); + else + sprintf(oname, ospec, bn, mname); + return OF_MODIFIER|OF_BIN; + } + sprintf(oname, ospec, mname); + return OF_MODIFIER; + } + if (bnp != NULL) { + sprintf(oname, ospec, bn); + return OF_BIN; + } + strcpy(oname, ospec); + return 0; +} + /* write header to the given output stream */ void -printheader(FILE *fout) +printheader(FILE *fout, const char *info) { extern char VersionID[]; FILE *fin = fopen(octree, "r"); @@ -468,6 +643,8 @@ printheader(FILE *fout) printargs(gargc-1, gargv, fout); /* add our command */ fprintf(fout, "SOFTWARE= %s\n", VersionID); fputnow(fout); + if (info != NULL) /* add extra info if given */ + fputs(info, fout); switch (outfmt) { /* add output format */ case 'a': fputformat("ascii", fout); @@ -483,6 +660,12 @@ printheader(FILE *fout) break; } fputc('\n', fout); +} + +/* write resolution string to given output stream */ +void +printresolu(FILE *fout) +{ if (xres > 0) { if (yres > 0) /* resolution string */ fprtresolu(xres, yres, fout); @@ -490,100 +673,98 @@ printheader(FILE *fout) } } -/* Get output file pointer (open and write header if new) */ -FILE * -getofile(const char *ospec, const char *mname, int bn) +/* Get output stream pointer (open and write header if new and noopen==0) */ +STREAMOUT * +getostream(const char *ospec, const char *mname, int bn, int noopen) { - const char *mnp = NULL; - const char *bnp = NULL; - const char *cp; - char ofname[1024]; - LUENT *lep; + static STREAMOUT stdos; + int ofl; + char oname[1024]; + LUENT *lep; + STREAMOUT *sop; if (ospec == NULL) { /* use stdout? */ - if (!using_stdout) { + if (!noopen && !using_stdout) { + stdos.reclen = 0; if (outfmt != 'a') SET_FILE_BINARY(stdout); if (header) - printheader(stdout); + printheader(stdout, NULL); + printresolu(stdout); + using_stdout = 1; } - using_stdout = 1; - return stdout; + stdos.ofp = stdout; + stdos.reclen += noopen; + return &stdos; } - for (cp = ospec; *cp; cp++) /* check format position(s) */ - if (*cp == '%') { - do - ++cp; - while (isdigit(*cp)); - switch (*cp) { - case '%': - break; - case 's': - if (mnp != NULL) - goto badspec; - mnp = cp; - break; - case 'd': - if (bnp != NULL) - goto badspec; - bnp = cp; - break; - default: - goto badspec; - } - } - if (mnp != NULL) { /* create file name */ - if (bnp != NULL) { - if (bnp > mnp) - sprintf(ofname, ospec, mname, bn); - else - sprintf(ofname, ospec, bn, mname); - } else - sprintf(ofname, ospec, mname); - } else if (bnp != NULL) - sprintf(ofname, ospec, bn); - else - strcpy(ofname, ospec); - lep = lu_find(&ofiletab, ofname); /* look it up */ + ofl = ofname(oname, ospec, mname, bn); /* get output name */ + if (ofl < 0) { + sprintf(errmsg, "bad output format '%s'", ospec); + error(USER, errmsg); + } + lep = lu_find(&ofiletab, oname); /* look it up */ if (lep->key == NULL) /* new entry */ - lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname); - if (lep->data == NULL) { /* open output file */ - FILE *fp; - int i; - if (ofname[0] == '!') /* output to command */ - fp = popen(ofname+1, "w"); - else - fp = fopen(ofname, "w"); - if (fp == NULL) { - sprintf(errmsg, "cannot open '%s' for writing", ofname); + lep->key = strcpy((char *)malloc(strlen(oname)+1), oname); + sop = (STREAMOUT *)lep->data; + if (sop == NULL) { /* allocate stream */ + sop = (STREAMOUT *)malloc(sizeof(STREAMOUT)); + if (sop == NULL) + error(SYSTEM, "out of memory in getostream"); + sop->reclen = oname[0] == '!' ? CNT_PIPE : CNT_UNKNOWN; + sop->ofp = NULL; /* open iff noopen==0 */ + lep->data = (char *)sop; + } + if (!noopen && sop->ofp == NULL) { /* open output stream */ + long i; + if (oname[0] == '!') /* output to command */ + sop->ofp = popen(oname+1, "w"); + else if (!force_open && access(oname, F_OK) == 0) + errno = EEXIST; /* file exists */ + else /* else open it */ + sop->ofp = fopen(oname, "w"); + if (sop->ofp == NULL) { + sprintf(errmsg, "cannot open '%s' for writing", oname); error(SYSTEM, errmsg); } if (outfmt != 'a') - SET_FILE_BINARY(fp); - if (header) - printheader(fp); + SET_FILE_BINARY(sop->ofp); + if (header) { + char info[512]; + char *cp = info; + if (ofl & OF_MODIFIER || sop->reclen == 1) { + sprintf(cp, "MODIFIER=%s\n", mname); + while (*cp) ++cp; + } + if (ofl & OF_BIN) { + sprintf(cp, "BIN=%d\n", bn); + while (*cp) ++cp; + } + *cp = '\0'; + printheader(sop->ofp, info); + } + printresolu(sop->ofp); /* play catch-up */ - for (i = 0; i < lastdone; i++) { + for (i = sop->reclen > 1 ? sop->reclen*lastdone : lastdone; + i--; ) { static const DCOLOR nocontrib = BLKCOLOR; - putcontrib(nocontrib, fp); + put_contrib(nocontrib, sop->ofp); if (outfmt == 'a') - putc('\n', fp); + putc('\n', sop->ofp); } if (xres > 0) - fflush(fp); - lep->data = (char *)fp; + fflush(sop->ofp); } - return (FILE *)lep->data; /* return open file pointer */ -badspec: - sprintf(errmsg, "bad output format '%s'", ospec); - error(USER, errmsg); - return NULL; /* pro forma return */ + if (sop->reclen != CNT_PIPE) /* add to length if noopen */ + sop->reclen += noopen; + return sop; /* return open stream */ } /* read input ray into buffer */ int getinp(char *buf, FILE *fp) { + double dv[3]; + float fv[3]; char *cp; int i; @@ -592,25 +773,35 @@ getinp(char *buf, FILE *fp) cp = buf; /* make sure we get 6 floats */ for (i = 0; i < 6; i++) { if (fgetword(cp, buf+127-cp, fp) == NULL) - return 0; + return -1; + if (i >= 3) + dv[i-3] = atof(cp); if ((cp = fskip(cp)) == NULL || *cp) - return 0; + return -1; *cp++ = ' '; } getc(fp); /* get/put eol */ *cp-- = '\0'; *cp = '\n'; + if (DOT(dv,dv) <= FTINY*FTINY) + return 0; /* dummy ray */ return strlen(buf); case 'f': if (fread(buf, sizeof(float), 6, fp) < 6) - return 0; + return -1; + memcpy(fv, buf+3*sizeof(float), 3*sizeof(float)); + if (DOT(fv,fv) <= FTINY*FTINY) + return 0; /* dummy ray */ return sizeof(float)*6; case 'd': if (fread(buf, sizeof(double), 6, fp) < 6) - return 0; + return -1; + memcpy(dv, buf+3*sizeof(double), 3*sizeof(double)); + if (DOT(dv,dv) <= FTINY*FTINY) + return 0; /* dummy ray */ return sizeof(double)*6; } error(INTERNAL, "botched input format"); - return 0; /* pro forma return */ + return -1; /* pro forma return */ } static float rparams[9]; /* traced ray parameters */ @@ -640,15 +831,8 @@ add_contrib(const char *modn) bn = (int)(evalue(mp->binv) + .5); if (bn <= 0) bn = 0; - else if (bn > mp->nbins) { /* new bin */ - mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + - bn*sizeof(DCOLOR)); - if (mp == NULL) - error(SYSTEM, "out of memory in add_contrib"); - memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins)); - mp->nbins = bn+1; - le->data = (char *)mp; - } + else if (bn >= mp->nbins) /* new bin */ + le->data = (char *)(mp = growmodifier(mp, bn+1)); addcolor(mp->cbin[bn], rparams); } @@ -656,13 +840,13 @@ add_contrib(const char *modn) static int puteol(const LUENT *e, void *p) { - FILE *fp = (FILE *)e->data; + STREAMOUT *sop = (STREAMOUT *)e->data; if (outfmt == 'a') - putc('\n', fp); + putc('\n', sop->ofp); if (!waitflush) - fflush(fp); - if (ferror(fp)) { + fflush(sop->ofp); + if (ferror(sop->ofp)) { sprintf(errmsg, "write error on file '%s'", e->key); error(SYSTEM, errmsg); } @@ -671,7 +855,7 @@ puteol(const LUENT *e, void *p) /* put out ray contribution to file */ void -putcontrib(const DCOLOR cnt, FILE *fout) +put_contrib(const DCOLOR cnt, FILE *fout) { float fv[3]; COLR cv; @@ -702,14 +886,22 @@ putcontrib(const DCOLOR cnt, FILE *fout) void done_contrib(void) { - int i, j; - MODCONT *mp; + int i, j; + MODCONT *mp; + STREAMOUT *sop; /* output modifiers in order */ for (i = 0; i < nmods; i++) { mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; - for (j = 0; j < mp->nbins; j++) - putcontrib(mp->cbin[j], - getofile(mp->outspec, mp->modname, j)); + sop = getostream(mp->outspec, mp->modname, 0,0); + put_contrib(mp->cbin[0], sop->ofp); + if (mp->nbins > 3 && /* minor optimization */ + sop == getostream(mp->outspec, mp->modname, 1,0)) + for (j = 1; j < mp->nbins; j++) + put_contrib(mp->cbin[j], sop->ofp); + else + for (j = 1; j < mp->nbins; j++) + put_contrib(mp->cbin[j], + getostream(mp->outspec,mp->modname,j,0)->ofp); /* clear for next ray tree */ memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); } @@ -783,7 +975,8 @@ process_queue(void) } done_contrib(); /* sum up contributions & output */ lastdone = rtp->raynum; - free(rtp->buf); /* free up buffer space */ + if (rtp->buf != NULL) /* free up buffer space */ + free(rtp->buf); rt_unproc = rtp->next; free(rtp); /* done with this ray tree */ } @@ -831,7 +1024,7 @@ wait_rproc(void) if (rt->bsiz + BUFSIZ <= treebufsiz) rt->bsiz = treebufsiz; else - rt->bsiz = treebufsiz += BUFSIZ; + treebufsiz = rt->bsiz += BUFSIZ; rt->buf = (char *)realloc(rt->buf, rt->bsiz); } if (rt->buf == NULL) @@ -871,21 +1064,187 @@ trace_contribs(FILE *fin) int iblen; struct rtproc *rtp; /* loop over input */ - while ((iblen = getinp(inpbuf, fin)) > 0) { - if (lastray+1 < lastray) { /* counter rollover? */ + while ((iblen = getinp(inpbuf, fin)) >= 0) { + if (!iblen || /* need reset? */ + queue_length() > 10*nrtprocs() || + lastray+1 < lastray) { while (wait_rproc() != NULL) process_queue(); - lastdone = lastray = 0; + if (lastray+1 < lastray) + lastdone = lastray = 0; } rtp = get_rproc(); /* get avail. rtrace process */ - rtp->raynum = ++lastray; /* assign ray to it */ - writebuf(rtp->pd.w, inpbuf, iblen); - if (!--raysleft) - break; + rtp->raynum = ++lastray; /* assign ray */ + if (iblen) { /* trace ray if valid */ + writebuf(rtp->pd.w, inpbuf, iblen); + } else { /* else bypass dummy ray */ + queue_raytree(rtp); /* empty tree */ + if ((yres <= 0) | (waitflush > 1)) + waitflush = 1; /* flush after this */ + } process_queue(); /* catch up with results */ + if (raysleft && !--raysleft) + break; /* preemptive EOI */ } while (wait_rproc() != NULL) /* process outstanding rays */ process_queue(); - if (raysleft > 0) + if (raysleft) error(USER, "unexpected EOF on input"); + lu_done(&ofiletab); /* close output files */ +} + +/* seek on the given output file */ +static int +myseeko(const LUENT *e, void *p) +{ + STREAMOUT *sop = (STREAMOUT *)e->data; + off_t nbytes = *(off_t *)p; + + if (sop->reclen > 1) + nbytes = nbytes * sop->reclen; + if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) { + sprintf(errmsg, "seek error on file '%s'", e->key); + error(SYSTEM, errmsg); + } +} + +/* recover output if possible */ +void +recover_output(FILE *fin) +{ + off_t lastout = -1; + int outvsiz, recsiz; + char *outvfmt; + int i, j; + MODCONT *mp; + int ofl; + char oname[1024]; + LUENT *ment, *oent; + STREAMOUT sout; + off_t nvals; + int xr, yr; + + switch (outfmt) { + case 'a': + error(USER, "cannot recover ASCII output"); + return; + case 'f': + outvsiz = sizeof(float)*3; + outvfmt = "float"; + break; + case 'd': + outvsiz = sizeof(double)*3; + outvfmt = "double"; + break; + case 'c': + outvsiz = sizeof(COLR); + outvfmt = COLRFMT; + break; + default: + error(INTERNAL, "botched output format"); + return; + } + /* check modifier outputs */ + for (i = 0; i < nmods; i++) { + ment = lu_find(&modconttab,modname[i]); + mp = (MODCONT *)ment->data; + if (mp->outspec == NULL) + error(USER, "cannot recover from stdout"); + if (mp->outspec[0] == '!') + error(USER, "cannot recover from command"); + for (j = 0; ; j++) { /* check each bin's file */ + ofl = ofname(oname, mp->outspec, mp->modname, j); + if (ofl < 0) + error(USER, "bad output file specification"); + oent = lu_find(&ofiletab, oname); + if (oent->data != NULL) { + sout = *(STREAMOUT *)oent->data; + } else { + sout.reclen = CNT_UNKNOWN; + sout.ofp = NULL; + } + if (sout.ofp != NULL) { /* already open? */ + if (ofl & OF_BIN) + continue; + break; + } + /* open output */ + sout.ofp = fopen(oname, "rb+"); + if (sout.ofp == NULL) { + if (j) + break; /* assume end of modifier */ + sprintf(errmsg, "missing recover file '%s'", + oname); + error(USER, errmsg); + } + nvals = lseek(fileno(sout.ofp), 0, SEEK_END); + if (nvals <= 0) { + lastout = 0; /* empty output, quit here */ + fclose(sout.ofp); + break; + } + if (sout.reclen == CNT_UNKNOWN) { + if (!(ofl & OF_BIN)) { + sprintf(errmsg, + "need -bn to recover file '%s'", + oname); + error(USER, errmsg); + } + recsiz = outvsiz; + } else + recsiz = outvsiz * sout.reclen; + + lseek(fileno(sout.ofp), 0, SEEK_SET); + if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) { + sprintf(errmsg, "format mismatch for '%s'", + oname); + error(USER, errmsg); + } + if ((xres > 0) & (yres > 0) && + (!fscnresolu(&xr, &yr, sout.ofp) || + xr != xres || + yr != yres)) { + sprintf(errmsg, "resolution mismatch for '%s'", + oname); + error(USER, errmsg); + } + nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz; + if ((lastout < 0) | (nvals < lastout)) + lastout = nvals; + if (oent->key == NULL) /* new entry */ + oent->key = strcpy((char *) + malloc(strlen(oname)+1), oname); + if (oent->data == NULL) + oent->data = (char *)malloc(sizeof(STREAMOUT)); + *(STREAMOUT *)oent->data = sout; + if (!(ofl & OF_BIN)) + break; /* no bin separation */ + } + if (!lastout) { /* empty output */ + error(WARNING, "no previous data to recover"); + lu_done(&ofiletab); /* reclose all outputs */ + return; + } + if (j > mp->nbins) /* reallocate modifier bins */ + ment->data = (char *)(mp = growmodifier(mp, j)); + } + if (lastout < 0) { + error(WARNING, "no output files to recover"); + return; + } + if (raysleft && lastout >= raysleft) { + error(WARNING, "output appears to be complete"); + /* XXX should read & discard input? */ + quit(0); + } + /* seek on all files */ + nvals = lastout * outvsiz; + lu_doall(&ofiletab, myseeko, &nvals); + /* skip repeated input */ + for (nvals = 0; nvals < lastout; nvals++) + if (getinp(oname, fin) < 0) + error(USER, "unexpected EOF on input"); + lastray = lastdone = (unsigned long)lastout; + if (raysleft) + raysleft -= lastray; }