| 63 |
|
closestream(void *p) |
| 64 |
|
{ |
| 65 |
|
STREAMOUT *sop = (STREAMOUT *)p; |
| 66 |
+ |
int status; |
| 67 |
|
if (sop->reclen == CNT_PIPE) |
| 68 |
< |
pclose(sop->ofp); |
| 68 |
> |
status = pclose(sop->ofp); |
| 69 |
|
else |
| 70 |
< |
fclose(sop->ofp); |
| 70 |
> |
status = fclose(sop->ofp); |
| 71 |
> |
if (status) |
| 72 |
> |
error(SYSTEM, "error closing output stream"); |
| 73 |
|
free(p); |
| 74 |
|
} |
| 75 |
|
|
| 78 |
|
#define OF_MODIFIER 01 |
| 79 |
|
#define OF_BIN 02 |
| 80 |
|
|
| 81 |
< |
STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int bincnt); |
| 81 |
> |
STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int noopen); |
| 82 |
|
int ofname(char *oname, const char *ospec, const char *mname, int bn); |
| 83 |
|
void printheader(FILE *fout, const char *info); |
| 84 |
|
void printresolu(FILE *fout); |
| 123 |
|
int outfmt = 'a'; /* output format */ |
| 124 |
|
|
| 125 |
|
int header = 1; /* output header? */ |
| 126 |
+ |
int force_open = 0; /* truncate existing output? */ |
| 127 |
|
int xres = 0; /* horiz. output resolution */ |
| 128 |
|
int yres = 0; /* vert. output resolution */ |
| 129 |
|
|
| 267 |
|
continue; |
| 268 |
|
} |
| 269 |
|
break; |
| 270 |
< |
case 'f': /* file or i/o format */ |
| 270 |
> |
case 'f': /* file or force or format */ |
| 271 |
|
if (!argv[i][2]) { |
| 272 |
|
char *fpath; |
| 273 |
|
if (i >= argc-2) break; |
| 282 |
|
fcompile(fpath); |
| 283 |
|
continue; |
| 284 |
|
} |
| 285 |
+ |
if (argv[i][2] == 'o') { |
| 286 |
+ |
force_open++; |
| 287 |
+ |
continue; |
| 288 |
+ |
} |
| 289 |
|
setformat(argv[i]+2); |
| 290 |
|
continue; |
| 291 |
|
case 'e': /* expression */ |
| 513 |
|
{ |
| 514 |
|
LUENT *lep = lu_find(&modconttab, modn); |
| 515 |
|
MODCONT *mp; |
| 516 |
+ |
int i; |
| 517 |
|
|
| 518 |
|
if (lep->data != NULL) { |
| 519 |
|
sprintf(errmsg, "duplicate modifier '%s'", modn); |
| 526 |
|
mp = (MODCONT *)malloc(sizeof(MODCONT)); |
| 527 |
|
if (mp == NULL) |
| 528 |
|
error(SYSTEM, "out of memory in addmodifier"); |
| 520 |
– |
lep->data = (char *)mp; |
| 529 |
|
mp->outspec = outf; /* XXX assumes static string */ |
| 530 |
|
mp->modname = modn; /* XXX assumes static string */ |
| 531 |
|
if (binv != NULL) |
| 538 |
|
bincnt = 1; |
| 539 |
|
else if (bincnt > 1) |
| 540 |
|
mp = growmodifier(mp, bincnt); |
| 541 |
< |
if (bincnt > 0) /* allocate output stream */ |
| 542 |
< |
getostream(mp->outspec, mp->modname, 0, bincnt); |
| 541 |
> |
lep->data = (char *)mp; |
| 542 |
> |
/* allocate output streams */ |
| 543 |
> |
for (i = outf==NULL || outf[0]=='!' ? 0 : bincnt; i--; ) |
| 544 |
> |
getostream(mp->outspec, mp->modname, i, 1); |
| 545 |
|
return mp; |
| 546 |
|
} |
| 547 |
|
|
| 670 |
|
} |
| 671 |
|
} |
| 672 |
|
|
| 673 |
< |
/* Get output stream pointer (open and write header if new and bincnt==0) */ |
| 673 |
> |
/* Get output stream pointer (open and write header if new and noopen==0) */ |
| 674 |
|
STREAMOUT * |
| 675 |
< |
getostream(const char *ospec, const char *mname, int bn, int bincnt) |
| 675 |
> |
getostream(const char *ospec, const char *mname, int bn, int noopen) |
| 676 |
|
{ |
| 677 |
|
static STREAMOUT stdos; |
| 678 |
|
int ofl; |
| 681 |
|
STREAMOUT *sop; |
| 682 |
|
|
| 683 |
|
if (ospec == NULL) { /* use stdout? */ |
| 684 |
< |
if (!bincnt && !using_stdout) { |
| 684 |
> |
if (!noopen && !using_stdout) { |
| 685 |
|
stdos.reclen = 0; |
| 686 |
|
if (outfmt != 'a') |
| 687 |
|
SET_FILE_BINARY(stdout); |
| 691 |
|
using_stdout = 1; |
| 692 |
|
} |
| 693 |
|
stdos.ofp = stdout; |
| 694 |
< |
stdos.reclen += bincnt; |
| 694 |
> |
stdos.reclen += noopen; |
| 695 |
|
return &stdos; |
| 696 |
|
} |
| 697 |
|
ofl = ofname(oname, ospec, mname, bn); /* get output name */ |
| 704 |
|
lep->key = strcpy((char *)malloc(strlen(oname)+1), oname); |
| 705 |
|
sop = (STREAMOUT *)lep->data; |
| 706 |
|
if (sop == NULL) { /* allocate stream */ |
| 697 |
– |
if (ofl & OF_BIN && bincnt > 1) /* don't overcount bins */ |
| 698 |
– |
bincnt = 1; |
| 707 |
|
sop = (STREAMOUT *)malloc(sizeof(STREAMOUT)); |
| 708 |
|
if (sop == NULL) |
| 709 |
|
error(SYSTEM, "out of memory in getostream"); |
| 710 |
< |
sop->reclen = oname[0] == '!' ? CNT_PIPE : 0; |
| 711 |
< |
sop->ofp = NULL; /* open iff bincnt==0 */ |
| 710 |
> |
sop->reclen = oname[0] == '!' ? CNT_PIPE : CNT_UNKNOWN; |
| 711 |
> |
sop->ofp = NULL; /* open iff noopen==0 */ |
| 712 |
|
lep->data = (char *)sop; |
| 713 |
|
} |
| 714 |
< |
if (!bincnt && sop->ofp == NULL) { /* open output stream */ |
| 715 |
< |
int i; |
| 714 |
> |
if (!noopen && sop->ofp == NULL) { /* open output stream */ |
| 715 |
> |
long i; |
| 716 |
|
if (oname[0] == '!') /* output to command */ |
| 717 |
|
sop->ofp = popen(oname+1, "w"); |
| 718 |
< |
else |
| 718 |
> |
else if (!force_open && access(oname, F_OK) == 0) |
| 719 |
> |
errno = EEXIST; /* file exists */ |
| 720 |
> |
else /* else open it */ |
| 721 |
|
sop->ofp = fopen(oname, "w"); |
| 722 |
|
if (sop->ofp == NULL) { |
| 723 |
|
sprintf(errmsg, "cannot open '%s' for writing", oname); |
| 728 |
|
if (header) { |
| 729 |
|
char info[512]; |
| 730 |
|
char *cp = info; |
| 731 |
< |
if (ofl & OF_MODIFIER) { |
| 731 |
> |
if (ofl & OF_MODIFIER || sop->reclen == 1) { |
| 732 |
|
sprintf(cp, "MODIFIER=%s\n", mname); |
| 733 |
|
while (*cp) ++cp; |
| 734 |
|
} |
| 741 |
|
} |
| 742 |
|
printresolu(sop->ofp); |
| 743 |
|
/* play catch-up */ |
| 744 |
< |
for (i = 0; i < lastdone; i++) { |
| 744 |
> |
for (i = sop->reclen > 1 ? sop->reclen*lastdone : lastdone; |
| 745 |
> |
i--; ) { |
| 746 |
|
static const DCOLOR nocontrib = BLKCOLOR; |
| 747 |
|
put_contrib(nocontrib, sop->ofp); |
| 748 |
|
if (outfmt == 'a') |
| 751 |
|
if (xres > 0) |
| 752 |
|
fflush(sop->ofp); |
| 753 |
|
} |
| 754 |
< |
if (sop->reclen != CNT_PIPE) /* add bincnt to count */ |
| 755 |
< |
sop->reclen += bincnt; |
| 754 |
> |
if (sop->reclen != CNT_PIPE) /* add to length if noopen */ |
| 755 |
> |
sop->reclen += noopen; |
| 756 |
|
return sop; /* return open stream */ |
| 757 |
|
} |
| 758 |
|
|
| 760 |
|
int |
| 761 |
|
getinp(char *buf, FILE *fp) |
| 762 |
|
{ |
| 763 |
+ |
double dv[3]; |
| 764 |
+ |
float fv[3]; |
| 765 |
|
char *cp; |
| 766 |
|
int i; |
| 767 |
|
|
| 770 |
|
cp = buf; /* make sure we get 6 floats */ |
| 771 |
|
for (i = 0; i < 6; i++) { |
| 772 |
|
if (fgetword(cp, buf+127-cp, fp) == NULL) |
| 773 |
< |
return 0; |
| 773 |
> |
return -1; |
| 774 |
> |
if (i >= 3) |
| 775 |
> |
dv[i-3] = atof(cp); |
| 776 |
|
if ((cp = fskip(cp)) == NULL || *cp) |
| 777 |
< |
return 0; |
| 777 |
> |
return -1; |
| 778 |
|
*cp++ = ' '; |
| 779 |
|
} |
| 780 |
|
getc(fp); /* get/put eol */ |
| 781 |
|
*cp-- = '\0'; *cp = '\n'; |
| 782 |
+ |
if (DOT(dv,dv) <= FTINY*FTINY) |
| 783 |
+ |
return 0; /* dummy ray */ |
| 784 |
|
return strlen(buf); |
| 785 |
|
case 'f': |
| 786 |
|
if (fread(buf, sizeof(float), 6, fp) < 6) |
| 787 |
< |
return 0; |
| 787 |
> |
return -1; |
| 788 |
> |
memcpy(fv, buf+3*sizeof(float), 3*sizeof(float)); |
| 789 |
> |
if (DOT(fv,fv) <= FTINY*FTINY) |
| 790 |
> |
return 0; /* dummy ray */ |
| 791 |
|
return sizeof(float)*6; |
| 792 |
|
case 'd': |
| 793 |
|
if (fread(buf, sizeof(double), 6, fp) < 6) |
| 794 |
< |
return 0; |
| 794 |
> |
return -1; |
| 795 |
> |
memcpy(dv, buf+3*sizeof(double), 3*sizeof(double)); |
| 796 |
> |
if (DOT(dv,dv) <= FTINY*FTINY) |
| 797 |
> |
return 0; /* dummy ray */ |
| 798 |
|
return sizeof(double)*6; |
| 799 |
|
} |
| 800 |
|
error(INTERNAL, "botched input format"); |
| 801 |
< |
return 0; /* pro forma return */ |
| 801 |
> |
return -1; /* pro forma return */ |
| 802 |
|
} |
| 803 |
|
|
| 804 |
|
static float rparams[9]; /* traced ray parameters */ |
| 972 |
|
} |
| 973 |
|
done_contrib(); /* sum up contributions & output */ |
| 974 |
|
lastdone = rtp->raynum; |
| 975 |
< |
free(rtp->buf); /* free up buffer space */ |
| 975 |
> |
if (rtp->buf != NULL) /* free up buffer space */ |
| 976 |
> |
free(rtp->buf); |
| 977 |
|
rt_unproc = rtp->next; |
| 978 |
|
free(rtp); /* done with this ray tree */ |
| 979 |
|
} |
| 1061 |
|
int iblen; |
| 1062 |
|
struct rtproc *rtp; |
| 1063 |
|
/* loop over input */ |
| 1064 |
< |
while ((iblen = getinp(inpbuf, fin)) > 0) { |
| 1065 |
< |
if (lastray+1 < lastray || /* need reset? */ |
| 1066 |
< |
queue_length() > 5*nrtprocs()) { |
| 1064 |
> |
while ((iblen = getinp(inpbuf, fin)) >= 0) { |
| 1065 |
> |
if (!iblen || /* need reset? */ |
| 1066 |
> |
queue_length() > 10*nrtprocs() || |
| 1067 |
> |
lastray+1 < lastray) { |
| 1068 |
|
while (wait_rproc() != NULL) |
| 1069 |
|
process_queue(); |
| 1070 |
< |
lastdone = lastray = 0; |
| 1070 |
> |
if (lastray+1 < lastray) |
| 1071 |
> |
lastdone = lastray = 0; |
| 1072 |
|
} |
| 1073 |
|
rtp = get_rproc(); /* get avail. rtrace process */ |
| 1074 |
< |
rtp->raynum = ++lastray; /* assign ray to it */ |
| 1075 |
< |
writebuf(rtp->pd.w, inpbuf, iblen); |
| 1074 |
> |
rtp->raynum = ++lastray; /* assign ray */ |
| 1075 |
> |
if (iblen) { /* trace ray if valid */ |
| 1076 |
> |
writebuf(rtp->pd.w, inpbuf, iblen); |
| 1077 |
> |
} else { /* else bypass dummy ray */ |
| 1078 |
> |
queue_raytree(rtp); /* empty tree */ |
| 1079 |
> |
if ((yres <= 0) | (waitflush > 0)) |
| 1080 |
> |
waitflush = 1; /* flush after this */ |
| 1081 |
> |
} |
| 1082 |
|
if (raysleft && !--raysleft) |
| 1083 |
|
break; |
| 1084 |
|
process_queue(); /* catch up with results */ |
| 1157 |
|
if (oent->data != NULL) { |
| 1158 |
|
sout = *(STREAMOUT *)oent->data; |
| 1159 |
|
} else { |
| 1160 |
< |
sout.reclen = 0; |
| 1160 |
> |
sout.reclen = CNT_UNKNOWN; |
| 1161 |
|
sout.ofp = NULL; |
| 1162 |
|
} |
| 1163 |
|
if (sout.ofp != NULL) { /* already open? */ |
| 1180 |
|
fclose(sout.ofp); |
| 1181 |
|
break; |
| 1182 |
|
} |
| 1183 |
< |
if (!sout.reclen) { /* unspecified record length */ |
| 1183 |
> |
if (sout.reclen == CNT_UNKNOWN) { |
| 1184 |
|
if (!(ofl & OF_BIN)) { |
| 1185 |
|
sprintf(errmsg, |
| 1186 |
< |
"guessing 1 value per record in '%s'", |
| 1186 |
> |
"need -bn to recover file '%s'", |
| 1187 |
|
oname); |
| 1188 |
< |
error(WARNING, errmsg); |
| 1188 |
> |
error(USER, errmsg); |
| 1189 |
|
} |
| 1190 |
|
recsiz = outvsiz; |
| 1191 |
|
} else |
| 1239 |
|
lu_doall(&ofiletab, myseeko, &nvals); |
| 1240 |
|
/* skip repeated input */ |
| 1241 |
|
for (nvals = 0; nvals < lastout; nvals++) |
| 1242 |
< |
if (getinp(oname, fin) <= 0) |
| 1242 |
> |
if (getinp(oname, fin) < 0) |
| 1243 |
|
error(USER, "unexpected EOF on input"); |
| 1244 |
|
lastray = lastdone = (unsigned long)lastout; |
| 1245 |
|
if (raysleft) |