--- ray/src/util/rtcontrib.c 2005/06/10 20:44:00 1.19 +++ ray/src/util/rtcontrib.c 2006/06/07 17:52:04 1.43 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rtcontrib.c,v 1.19 2005/06/10 20:44:00 greg Exp $"; +static const char RCSid[] = "$Id: rtcontrib.c,v 1.43 2006/06/07 17:52:04 schorsch Exp $"; #endif /* * Gather rtrace output to compute contributions from particular sources @@ -25,9 +25,9 @@ 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 @@ -46,17 +46,42 @@ 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); +} +LUTAB ofiletab = LU_SINIT(free,closestream); /* output file table */ + #define OF_MODIFIER 01 #define OF_BIN 02 -FILE *getofile(const char *ospec, const char *mname, int bn); +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 @@ -76,12 +101,16 @@ struct rtproc { /* rtrace command and defaults */ char *rtargv[256+2*MAXMODLIST] = { "rtrace", "-dj", ".5", "-dr", "3", - "-ab", "1", "-ad", "128", }; + "-ab", "1", "-ad", "350", }; + int rtargc = 9; /* overriding rtrace options */ -char *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0", +char *myrtopts[] = { "-h-", "-x", "1", "-y", "0", "-dt", "0", "-as", "0", "-aa", "0", NULL }; +#define RTCOEFF "-o~~TmWdp" /* compute coefficients only */ +#define RTCONTRIB "-o~~TmVdp" /* compute ray contributions */ + struct rtproc rt0; /* head of rtrace process list */ struct rtproc *rt_unproc = NULL; /* unprocessed ray trees */ @@ -98,6 +127,7 @@ 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 */ @@ -112,9 +142,12 @@ 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); -void addmodfile(char *fname, 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 recover_output(FILE *fin); @@ -128,6 +161,18 @@ 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) @@ -167,12 +212,21 @@ fmterr: int main(int argc, char *argv[]) { + int contrib = 0; 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][-V][-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 */ @@ -191,16 +245,33 @@ 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"); continue; + case 'V': /* output contributions */ + switch (argv[i][2]) { + case '\0': + contrib = !contrib; + continue; + case '+': case '1': + case 'T': case 't': + case 'Y': case 'y': + contrib = 1; + continue; + case '-': case '0': + case 'F': case 'f': + case 'N': case 'n': + contrib = 0; + continue; + } + break; + case 'r': /* recover output */ + if (argv[i][2]) break; + recover++; + continue; case 'h': /* output header? */ switch (argv[i][2]) { case '\0': @@ -218,10 +289,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) { @@ -233,40 +304,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-1) break; + if (argv[i][2] || i >= argc-2) break; rtargv[rtargc++] = "-tI"; rtargv[rtargc++] = argv[++i]; - addmodfile(argv[i], curout, binval); + 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 */ } @@ -275,11 +358,14 @@ main(int argc, char *argv[]) /* add "mandatory" rtrace settings */ for (j = 0; myrtopts[j] != NULL; j++) rtargv[rtargc++] = myrtopts[j]; + rtargv[rtargc++] = contrib ? RTCONTRIB : RTCOEFF; /* just asking for defaults? */ if (!strcmp(argv[i], "-defaults")) { char sxres[16], syres[16]; char *rtpath; printf("-n %-2d\t\t\t\t# number of processes\n", nprocs); + printf("-V%c\t\t\t\t# output %s\n", contrib ? '+' : '-', + contrib ? "contributions" : "coefficients"); fflush(stdout); /* report OUR options */ rtargv[rtargc++] = header ? "-h+" : "-h-"; sprintf(fmt, "-f%c%c", inpfmt, outfmt); @@ -290,7 +376,6 @@ main(int argc, char *argv[]) rtargv[rtargc++] = "-y"; sprintf(syres, "%d", yres); rtargv[rtargc++] = syres; - rtargv[rtargc++] = "-oTW"; rtargv[rtargc++] = "-defaults"; rtargv[rtargc] = NULL; rtpath = getpath(rtargv[0], getenv("PATH"), X_OK); @@ -315,20 +400,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); if (recover) /* perform recovery if requested */ recover_output(stdin); - trace_contribs(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; @@ -432,12 +520,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); @@ -450,7 +553,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) @@ -459,19 +561,30 @@ 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-- > 0; ) + getostream(mp->outspec, mp->modname, i, 1); return mp; } /* add modifiers from a file list */ void -addmodfile(char *fname, char *outf, char *binv) +addmodfile(char *fname, char *outf, char *binv, int bincnt) { char *mname[MAXMODLIST]; int i; - /* load the file & store strings */ - wordfile(mname, fname); + /* 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); + addmodifier(mname[i], outf, binv, bincnt); } /* put string to stderr */ @@ -571,6 +684,12 @@ printheader(FILE *fout, const char *info) 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); @@ -578,23 +697,29 @@ printheader(FILE *fout, const char *info) } } -/* 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) { - int ofl; - char oname[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, NULL); + printresolu(stdout); + using_stdout = 1; } - using_stdout = 1; - return stdout; + stdos.ofp = stdout; + stdos.reclen += noopen; + return &stdos; } ofl = ofname(oname, ospec, mname, bn); /* get output name */ if (ofl < 0) { @@ -604,23 +729,33 @@ getofile(const char *ospec, const char *mname, int bn) lep = lu_find(&ofiletab, oname); /* look it up */ if (lep->key == NULL) /* new entry */ lep->key = strcpy((char *)malloc(strlen(oname)+1), oname); - if (lep->data == NULL) { /* open output file */ - FILE *fp; - int i; + 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 */ - fp = popen(oname+1, "w"); - else - fp = fopen(oname, "w"); - if (fp == NULL) { + 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); + SET_FILE_BINARY(sop->ofp); if (header) { char info[512]; char *cp = info; - if (ofl & OF_MODIFIER) { + if (ofl & OF_MODIFIER || sop->reclen == 1) { sprintf(cp, "MODIFIER=%s\n", mname); while (*cp) ++cp; } @@ -629,26 +764,31 @@ getofile(const char *ospec, const char *mname, int bn) while (*cp) ++cp; } *cp = '\0'; - printheader(fp, info); + 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; - put_contrib(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 */ + 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; @@ -657,25 +797,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 */ @@ -705,15 +855,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); } @@ -721,13 +864,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); } @@ -767,22 +910,22 @@ put_contrib(const DCOLOR cnt, FILE *fout) void done_contrib(void) { - int i, j; - MODCONT *mp; - FILE *fp; + 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; - fp = getofile(mp->outspec, mp->modname, 0); - put_contrib(mp->cbin[0], fp); + sop = getostream(mp->outspec, mp->modname, 0,0); + put_contrib(mp->cbin[0], sop->ofp); if (mp->nbins > 3 && /* minor optimization */ - fp == getofile(mp->outspec, mp->modname, 1)) + sop == getostream(mp->outspec, mp->modname, 1,0)) for (j = 1; j < mp->nbins; j++) - put_contrib(mp->cbin[j], fp); + put_contrib(mp->cbin[j], sop->ofp); else for (j = 1; j < mp->nbins; j++) put_contrib(mp->cbin[j], - getofile(mp->outspec, mp->modname, j)); + getostream(mp->outspec,mp->modname,j,0)->ofp); /* clear for next ray tree */ memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); } @@ -842,7 +985,9 @@ process_queue(void) if (!n || !(isalpha(*cp) | (*cp == '_'))) error(USER, "bad modifier name from rtrace"); /* get modifier name */ - while (n > 0 && *cp != '\t') { + while (n > 1 && *cp != '\t') { + if (mnp - modname >= sizeof(modname)-2) + error(INTERNAL, "modifier name too long"); *mnp++ = *cp++; n--; } *mnp = '\0'; @@ -856,7 +1001,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 */ } @@ -904,7 +1050,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) @@ -944,18 +1090,27 @@ 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 && !--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(); @@ -968,26 +1123,33 @@ trace_contribs(FILE *fin) static int myseeko(const LUENT *e, void *p) { - if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) { + 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); } + return 0; } /* recover output if possible */ void recover_output(FILE *fin) { - off_t lastout = -1; - int outvsiz; - char *outvfmt; - int i, j; - MODCONT *mp; - int ofl; - char oname[1024]; - LUENT *ment, *oent; - FILE *fp; - off_t nvals; + 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': @@ -1015,54 +1177,73 @@ recover_output(FILE *fin) 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) { /* saw this one already */ + 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; } - if (oname[0] == '!') - error(USER, "cannot recover from command"); /* open output */ - fp = fopen(oname, "rb+"); - if (fp == NULL) - break; /* must be end of modifier */ - nvals = lseek(fileno(fp), 0, SEEK_END); + 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(fp); + fclose(sout.ofp); break; } - lseek(fileno(fp), 0, SEEK_SET); - if (header) { - int xr, yr; - if (checkheader(fp, outvfmt, NULL) != 1) { + if (sout.reclen == CNT_UNKNOWN) { + if (!(ofl & OF_BIN)) { sprintf(errmsg, - "format mismatch for '%s'", + "need -bn to recover file '%s'", oname); error(USER, errmsg); } - if (xres > 0 && yres > 0 && - (!fscnresolu(&xr, &yr, fp) || - xr != xres || - yr != yres)) { - sprintf(errmsg, - "resolution mismatch for '%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); } - nvals = (nvals - (off_t)ftell(fp)) / outvsiz; - if (lastout < 0 || nvals < lastout) + 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 */ + if (oent->key == NULL) /* new entry */ oent->key = strcpy((char *) malloc(strlen(oname)+1), oname); - oent->data = (char *)fp; + if (oent->data == NULL) + oent->data = (char *)malloc(sizeof(STREAMOUT)); + *(STREAMOUT *)oent->data = sout; if (!(ofl & OF_BIN)) break; /* no bin separation */ } @@ -1071,26 +1252,24 @@ recover_output(FILE *fin) lu_done(&ofiletab); /* reclose all outputs */ return; } - if (j > mp->nbins) { /* preallocate modifier bins */ - mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + - (j-1)*sizeof(DCOLOR)); - if (mp == NULL) - error(SYSTEM, "out of memory in recover_output"); - memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); - mp->nbins = j; - ment->data = (char *)mp; - } + 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); - /* discard input */ + /* skip repeated input */ for (nvals = 0; nvals < lastout; nvals++) - if (getinp(oname, fin) <= 0) + if (getinp(oname, fin) < 0) error(USER, "unexpected EOF on input"); lastray = lastdone = (unsigned long)lastout; if (raysleft)