--- ray/src/util/rtcontrib.c 2005/10/06 05:49:44 1.31 +++ ray/src/util/rtcontrib.c 2005/10/11 19:02:51 1.36 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: rtcontrib.c,v 1.31 2005/10/06 05:49:44 greg Exp $"; +static const char RCSid[] = "$Id: rtcontrib.c,v 1.36 2005/10/11 19:02:51 greg Exp $"; #endif /* * Gather rtrace output to compute contributions from particular sources @@ -63,10 +63,13 @@ static void closestream(void *p) { STREAMOUT *sop = (STREAMOUT *)p; + int status; if (sop->reclen == CNT_PIPE) - pclose(sop->ofp); + status = pclose(sop->ofp); else - fclose(sop->ofp); + status = fclose(sop->ofp); + if (status) + error(SYSTEM, "error closing output stream"); free(p); } @@ -75,7 +78,7 @@ LUTAB ofiletab = LU_SINIT(free,closestream); /* output #define OF_MODIFIER 01 #define OF_BIN 02 -STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int bincnt); +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); @@ -120,6 +123,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 */ @@ -263,7 +267,7 @@ 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-2) break; @@ -278,6 +282,10 @@ main(int argc, char *argv[]) fcompile(fpath); continue; } + if (argv[i][2] == 'o') { + force_open++; + continue; + } setformat(argv[i]+2); continue; case 'e': /* expression */ @@ -505,6 +513,7 @@ addmodifier(char *modn, char *outf, char *binv, int bi { LUENT *lep = lu_find(&modconttab, modn); MODCONT *mp; + int i; if (lep->data != NULL) { sprintf(errmsg, "duplicate modifier '%s'", modn); @@ -517,7 +526,6 @@ addmodifier(char *modn, char *outf, char *binv, int bi 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) @@ -530,8 +538,10 @@ addmodifier(char *modn, char *outf, char *binv, int bi bincnt = 1; else if (bincnt > 1) mp = growmodifier(mp, bincnt); - if (bincnt > 0) /* allocate output stream */ - getostream(mp->outspec, mp->modname, 0, 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; } @@ -660,9 +670,9 @@ printresolu(FILE *fout) } } -/* Get output stream pointer (open and write header if new and bincnt==0) */ +/* Get output stream pointer (open and write header if new and noopen==0) */ STREAMOUT * -getostream(const char *ospec, const char *mname, int bn, int bincnt) +getostream(const char *ospec, const char *mname, int bn, int noopen) { static STREAMOUT stdos; int ofl; @@ -671,7 +681,7 @@ getostream(const char *ospec, const char *mname, int b STREAMOUT *sop; if (ospec == NULL) { /* use stdout? */ - if (!bincnt && !using_stdout) { + if (!noopen && !using_stdout) { stdos.reclen = 0; if (outfmt != 'a') SET_FILE_BINARY(stdout); @@ -681,7 +691,7 @@ getostream(const char *ospec, const char *mname, int b using_stdout = 1; } stdos.ofp = stdout; - stdos.reclen += bincnt; + stdos.reclen += noopen; return &stdos; } ofl = ofname(oname, ospec, mname, bn); /* get output name */ @@ -694,20 +704,20 @@ getostream(const char *ospec, const char *mname, int b lep->key = strcpy((char *)malloc(strlen(oname)+1), oname); sop = (STREAMOUT *)lep->data; if (sop == NULL) { /* allocate stream */ - if (ofl & OF_BIN && bincnt > 1) /* don't overcount bins */ - bincnt = 1; sop = (STREAMOUT *)malloc(sizeof(STREAMOUT)); if (sop == NULL) error(SYSTEM, "out of memory in getostream"); - sop->reclen = oname[0] == '!' ? CNT_PIPE : 0; - sop->ofp = NULL; /* open iff bincnt==0 */ + sop->reclen = oname[0] == '!' ? CNT_PIPE : CNT_UNKNOWN; + sop->ofp = NULL; /* open iff noopen==0 */ lep->data = (char *)sop; } - if (!bincnt && sop->ofp == NULL) { /* open output stream */ - int i; + if (!noopen && sop->ofp == NULL) { /* open output stream */ + long i; if (oname[0] == '!') /* output to command */ sop->ofp = popen(oname+1, "w"); - else + 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); @@ -718,7 +728,7 @@ getostream(const char *ospec, const char *mname, int b 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; } @@ -731,7 +741,8 @@ getostream(const char *ospec, const char *mname, int b } 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, sop->ofp); if (outfmt == 'a') @@ -740,8 +751,8 @@ getostream(const char *ospec, const char *mname, int b if (xres > 0) fflush(sop->ofp); } - if (sop->reclen != CNT_PIPE) /* add bincnt to count */ - sop->reclen += bincnt; + if (sop->reclen != CNT_PIPE) /* add to length if noopen */ + sop->reclen += noopen; return sop; /* return open stream */ } @@ -749,6 +760,8 @@ getostream(const char *ospec, const char *mname, int b int getinp(char *buf, FILE *fp) { + double dv[3]; + float fv[3]; char *cp; int i; @@ -757,25 +770,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 */ @@ -949,7 +972,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 */ } @@ -1037,16 +1061,24 @@ trace_contribs(FILE *fin) int iblen; struct rtproc *rtp; /* loop over input */ - while ((iblen = getinp(inpbuf, fin)) > 0) { - if (lastray+1 < lastray || /* need reset? */ - queue_length() > 5*nrtprocs()) { + 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); + 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 > 0)) + waitflush = 1; /* flush after this */ + } if (raysleft && !--raysleft) break; process_queue(); /* catch up with results */ @@ -1125,7 +1157,7 @@ recover_output(FILE *fin) if (oent->data != NULL) { sout = *(STREAMOUT *)oent->data; } else { - sout.reclen = 0; + sout.reclen = CNT_UNKNOWN; sout.ofp = NULL; } if (sout.ofp != NULL) { /* already open? */ @@ -1148,12 +1180,12 @@ recover_output(FILE *fin) fclose(sout.ofp); break; } - if (!sout.reclen) { /* unspecified record length */ + if (sout.reclen == CNT_UNKNOWN) { if (!(ofl & OF_BIN)) { sprintf(errmsg, - "guessing 1 value per record in '%s'", + "need -bn to recover file '%s'", oname); - error(WARNING, errmsg); + error(USER, errmsg); } recsiz = outvsiz; } else @@ -1207,7 +1239,7 @@ recover_output(FILE *fin) lu_doall(&ofiletab, myseeko, &nvals); /* 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)