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 |
98 |
|
int xres = 0; /* horiz. output resolution */ |
99 |
|
int yres = 0; /* vert. output resolution */ |
100 |
|
|
101 |
< |
long raysleft; /* number of rays left to trace */ |
101 |
> |
unsigned long raysleft; /* number of rays left to trace */ |
102 |
|
long waitflush; /* how long until next flush */ |
103 |
|
|
104 |
|
unsigned long lastray = 0; /* last ray number sent */ |
110 |
|
int nmods = 0; /* number of modifiers */ |
111 |
|
|
112 |
|
MODCONT *addmodifier(char *modn, char *outf, char *binv); |
113 |
+ |
void addmodfile(char *fname, char *outf, char *binv); |
114 |
|
|
115 |
|
void init(int np); |
116 |
|
int done_rprocs(struct rtproc *rtp); |
117 |
< |
void trace_contribs(FILE *fp); |
117 |
> |
void recover_output(FILE *fin); |
118 |
> |
void trace_contribs(FILE *fin); |
119 |
|
struct rtproc *wait_rproc(void); |
120 |
|
struct rtproc *get_rproc(void); |
121 |
|
void queue_raytree(struct rtproc *rtp); |
122 |
|
void process_queue(void); |
123 |
|
|
124 |
< |
void putcontrib(const DCOLOR cnt, FILE *fout); |
124 |
> |
void put_contrib(const DCOLOR cnt, FILE *fout); |
125 |
|
void add_contrib(const char *modn); |
126 |
|
void done_contrib(void); |
127 |
|
|
165 |
|
main(int argc, char *argv[]) |
166 |
|
{ |
167 |
|
int nprocs = 1; |
168 |
+ |
int recover = 0; |
169 |
|
char *curout = NULL; |
170 |
|
char *binval = NULL; |
171 |
|
char fmt[8]; |
188 |
|
} |
189 |
|
if (argv[i][0] == '-') |
190 |
|
switch (argv[i][1]) { |
191 |
+ |
case 'r': /* recover output */ |
192 |
+ |
if (argv[i][2]) break; |
193 |
+ |
recover++; |
194 |
+ |
continue; |
195 |
|
case 'n': /* number of processes */ |
196 |
|
if (argv[i][2] || i >= argc-1) break; |
197 |
|
nprocs = atoi(argv[++i]); |
258 |
|
rtargv[rtargc++] = argv[++i]; |
259 |
|
addmodifier(argv[i], curout, binval); |
260 |
|
continue; |
261 |
+ |
case 'M': /* modifier file */ |
262 |
+ |
if (argv[i][2] || i >= argc-1) break; |
263 |
+ |
addmodfile(argv[++i], curout, binval); |
264 |
+ |
continue; |
265 |
|
} |
266 |
|
rtargv[rtargc++] = argv[i]; /* assume rtrace option */ |
267 |
|
} |
312 |
|
rtargv[rtargc] = NULL; |
313 |
|
/* start rtrace & compute contributions */ |
314 |
|
init(nprocs); |
315 |
+ |
if (recover) /* perform recovery if requested */ |
316 |
+ |
recover_output(stdin); |
317 |
|
trace_contribs(stdin); |
318 |
|
quit(0); |
319 |
|
} |
419 |
|
rtp->next = NULL; /* terminate list */ |
420 |
|
if (yres > 0) { |
421 |
|
if (xres > 0) |
422 |
< |
raysleft = xres*yres; |
422 |
> |
raysleft = (unsigned long)xres*yres; |
423 |
|
else |
424 |
|
raysleft = yres; |
425 |
|
} else |
457 |
|
return mp; |
458 |
|
} |
459 |
|
|
460 |
+ |
/* add modifiers from a file list */ |
461 |
+ |
void |
462 |
+ |
addmodfile(char *fname, char *outf, char *binv) |
463 |
+ |
{ |
464 |
+ |
char *mname[MAXMODLIST]; |
465 |
+ |
int i; |
466 |
+ |
/* load the file & store strings */ |
467 |
+ |
wordfile(mname, fname); |
468 |
+ |
for (i = 0; mname[i]; i++) /* add each one */ |
469 |
+ |
addmodifier(mname[i], outf, binv); |
470 |
+ |
} |
471 |
+ |
|
472 |
|
/* put string to stderr */ |
473 |
|
void |
474 |
|
eputs(char *s) |
484 |
|
midline = s[strlen(s)-1] != '\n'; |
485 |
|
} |
486 |
|
|
487 |
+ |
/* construct output file name and return flags whether modifier/bin present */ |
488 |
+ |
int |
489 |
+ |
ofname(char *oname, const char *ospec, const char *mname, int bn) |
490 |
+ |
{ |
491 |
+ |
const char *mnp = NULL; |
492 |
+ |
const char *bnp = NULL; |
493 |
+ |
const char *cp; |
494 |
+ |
|
495 |
+ |
if (ospec == NULL) |
496 |
+ |
return -1; |
497 |
+ |
for (cp = ospec; *cp; cp++) /* check format position(s) */ |
498 |
+ |
if (*cp == '%') { |
499 |
+ |
do |
500 |
+ |
++cp; |
501 |
+ |
while (isdigit(*cp)); |
502 |
+ |
switch (*cp) { |
503 |
+ |
case '%': |
504 |
+ |
break; |
505 |
+ |
case 's': |
506 |
+ |
if (mnp != NULL) |
507 |
+ |
return -1; |
508 |
+ |
mnp = cp; |
509 |
+ |
break; |
510 |
+ |
case 'd': |
511 |
+ |
if (bnp != NULL) |
512 |
+ |
return -1; |
513 |
+ |
bnp = cp; |
514 |
+ |
break; |
515 |
+ |
default: |
516 |
+ |
return -1; |
517 |
+ |
} |
518 |
+ |
} |
519 |
+ |
if (mnp != NULL) { /* create file name */ |
520 |
+ |
if (bnp != NULL) { |
521 |
+ |
if (bnp > mnp) |
522 |
+ |
sprintf(oname, ospec, mname, bn); |
523 |
+ |
else |
524 |
+ |
sprintf(oname, ospec, bn, mname); |
525 |
+ |
return OF_MODIFIER|OF_BIN; |
526 |
+ |
} |
527 |
+ |
sprintf(oname, ospec, mname); |
528 |
+ |
return OF_MODIFIER; |
529 |
+ |
} |
530 |
+ |
if (bnp != NULL) { |
531 |
+ |
sprintf(oname, ospec, bn); |
532 |
+ |
return OF_BIN; |
533 |
+ |
} |
534 |
+ |
strcpy(oname, ospec); |
535 |
+ |
return 0; |
536 |
+ |
} |
537 |
+ |
|
538 |
|
/* write header to the given output stream */ |
539 |
|
void |
540 |
< |
printheader(FILE *fout) |
540 |
> |
printheader(FILE *fout, const char *info) |
541 |
|
{ |
542 |
|
extern char VersionID[]; |
543 |
|
FILE *fin = fopen(octree, "r"); |
549 |
|
printargs(gargc-1, gargv, fout); /* add our command */ |
550 |
|
fprintf(fout, "SOFTWARE= %s\n", VersionID); |
551 |
|
fputnow(fout); |
552 |
+ |
if (info != NULL) /* add extra info if given */ |
553 |
+ |
fputs(info, fout); |
554 |
|
switch (outfmt) { /* add output format */ |
555 |
|
case 'a': |
556 |
|
fputformat("ascii", fout); |
577 |
|
FILE * |
578 |
|
getofile(const char *ospec, const char *mname, int bn) |
579 |
|
{ |
580 |
< |
const char *mnp = NULL; |
581 |
< |
const char *bnp = NULL; |
499 |
< |
const char *cp; |
500 |
< |
char ofname[1024]; |
580 |
> |
int ofl; |
581 |
> |
char oname[1024]; |
582 |
|
LUENT *lep; |
583 |
|
|
584 |
|
if (ospec == NULL) { /* use stdout? */ |
586 |
|
if (outfmt != 'a') |
587 |
|
SET_FILE_BINARY(stdout); |
588 |
|
if (header) |
589 |
< |
printheader(stdout); |
589 |
> |
printheader(stdout, NULL); |
590 |
|
} |
591 |
|
using_stdout = 1; |
592 |
|
return stdout; |
593 |
|
} |
594 |
< |
for (cp = ospec; *cp; cp++) /* check format position(s) */ |
595 |
< |
if (*cp == '%') { |
596 |
< |
do |
597 |
< |
++cp; |
598 |
< |
while (isdigit(*cp)); |
599 |
< |
switch (*cp) { |
519 |
< |
case '%': |
520 |
< |
break; |
521 |
< |
case 's': |
522 |
< |
if (mnp != NULL) |
523 |
< |
goto badspec; |
524 |
< |
mnp = cp; |
525 |
< |
break; |
526 |
< |
case 'd': |
527 |
< |
if (bnp != NULL) |
528 |
< |
goto badspec; |
529 |
< |
bnp = cp; |
530 |
< |
break; |
531 |
< |
default: |
532 |
< |
goto badspec; |
533 |
< |
} |
534 |
< |
} |
535 |
< |
if (mnp != NULL) { /* create file name */ |
536 |
< |
if (bnp != NULL) { |
537 |
< |
if (bnp > mnp) |
538 |
< |
sprintf(ofname, ospec, mname, bn); |
539 |
< |
else |
540 |
< |
sprintf(ofname, ospec, bn, mname); |
541 |
< |
} else |
542 |
< |
sprintf(ofname, ospec, mname); |
543 |
< |
} else if (bnp != NULL) |
544 |
< |
sprintf(ofname, ospec, bn); |
545 |
< |
else |
546 |
< |
strcpy(ofname, ospec); |
547 |
< |
lep = lu_find(&ofiletab, ofname); /* look it up */ |
594 |
> |
ofl = ofname(oname, ospec, mname, bn); /* get output name */ |
595 |
> |
if (ofl < 0) { |
596 |
> |
sprintf(errmsg, "bad output format '%s'", ospec); |
597 |
> |
error(USER, errmsg); |
598 |
> |
} |
599 |
> |
lep = lu_find(&ofiletab, oname); /* look it up */ |
600 |
|
if (lep->key == NULL) /* new entry */ |
601 |
< |
lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname); |
601 |
> |
lep->key = strcpy((char *)malloc(strlen(oname)+1), oname); |
602 |
|
if (lep->data == NULL) { /* open output file */ |
603 |
|
FILE *fp; |
604 |
|
int i; |
605 |
< |
if (ofname[0] == '!') /* output to command */ |
606 |
< |
fp = popen(ofname+1, "w"); |
605 |
> |
if (oname[0] == '!') /* output to command */ |
606 |
> |
fp = popen(oname+1, "w"); |
607 |
|
else |
608 |
< |
fp = fopen(ofname, "w"); |
608 |
> |
fp = fopen(oname, "w"); |
609 |
|
if (fp == NULL) { |
610 |
< |
sprintf(errmsg, "cannot open '%s' for writing", ofname); |
610 |
> |
sprintf(errmsg, "cannot open '%s' for writing", oname); |
611 |
|
error(SYSTEM, errmsg); |
612 |
|
} |
613 |
|
if (outfmt != 'a') |
614 |
|
SET_FILE_BINARY(fp); |
615 |
< |
if (header) |
616 |
< |
printheader(fp); |
615 |
> |
if (header) { |
616 |
> |
char info[512]; |
617 |
> |
char *cp = info; |
618 |
> |
if (ofl & OF_MODIFIER) { |
619 |
> |
sprintf(cp, "MODIFIER=%s\n", mname); |
620 |
> |
while (*cp) ++cp; |
621 |
> |
} |
622 |
> |
if (ofl & OF_BIN) { |
623 |
> |
sprintf(cp, "BIN=%d\n", bn); |
624 |
> |
while (*cp) ++cp; |
625 |
> |
} |
626 |
> |
*cp = '\0'; |
627 |
> |
printheader(fp, info); |
628 |
> |
} |
629 |
|
/* play catch-up */ |
630 |
|
for (i = 0; i < lastdone; i++) { |
631 |
|
static const DCOLOR nocontrib = BLKCOLOR; |
632 |
< |
putcontrib(nocontrib, fp); |
632 |
> |
put_contrib(nocontrib, fp); |
633 |
|
if (outfmt == 'a') |
634 |
|
putc('\n', fp); |
635 |
|
} |
638 |
|
lep->data = (char *)fp; |
639 |
|
} |
640 |
|
return (FILE *)lep->data; /* return open file pointer */ |
577 |
– |
badspec: |
578 |
– |
sprintf(errmsg, "bad output format '%s'", ospec); |
579 |
– |
error(USER, errmsg); |
580 |
– |
return NULL; /* pro forma return */ |
641 |
|
} |
642 |
|
|
643 |
|
/* read input ray into buffer */ |
731 |
|
|
732 |
|
/* put out ray contribution to file */ |
733 |
|
void |
734 |
< |
putcontrib(const DCOLOR cnt, FILE *fout) |
734 |
> |
put_contrib(const DCOLOR cnt, FILE *fout) |
735 |
|
{ |
736 |
|
float fv[3]; |
737 |
|
COLR cv; |
764 |
|
{ |
765 |
|
int i, j; |
766 |
|
MODCONT *mp; |
767 |
+ |
FILE *fp; |
768 |
|
/* output modifiers in order */ |
769 |
|
for (i = 0; i < nmods; i++) { |
709 |
– |
FILE *fp; |
770 |
|
mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; |
771 |
|
fp = getofile(mp->outspec, mp->modname, 0); |
772 |
< |
putcontrib(mp->cbin[0], fp); |
772 |
> |
put_contrib(mp->cbin[0], fp); |
773 |
|
if (mp->nbins > 3 && /* minor optimization */ |
774 |
|
fp == getofile(mp->outspec, mp->modname, 1)) |
775 |
|
for (j = 1; j < mp->nbins; j++) |
776 |
< |
putcontrib(mp->cbin[j], fp); |
776 |
> |
put_contrib(mp->cbin[j], fp); |
777 |
|
else |
778 |
|
for (j = 1; j < mp->nbins; j++) |
779 |
< |
putcontrib(mp->cbin[j], |
779 |
> |
put_contrib(mp->cbin[j], |
780 |
|
getofile(mp->outspec, mp->modname, j)); |
781 |
|
/* clear for next ray tree */ |
782 |
|
memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); |
948 |
|
rtp = get_rproc(); /* get avail. rtrace process */ |
949 |
|
rtp->raynum = ++lastray; /* assign ray to it */ |
950 |
|
writebuf(rtp->pd.w, inpbuf, iblen); |
951 |
< |
if (!--raysleft) |
951 |
> |
if (raysleft && !--raysleft) |
952 |
|
break; |
953 |
|
process_queue(); /* catch up with results */ |
954 |
|
} |
955 |
|
while (wait_rproc() != NULL) /* process outstanding rays */ |
956 |
|
process_queue(); |
957 |
< |
if (raysleft > 0) |
957 |
> |
if (raysleft) |
958 |
|
error(USER, "unexpected EOF on input"); |
959 |
+ |
lu_done(&ofiletab); /* close output files */ |
960 |
+ |
} |
961 |
+ |
|
962 |
+ |
/* seek on the given output file */ |
963 |
+ |
static int |
964 |
+ |
myseeko(const LUENT *e, void *p) |
965 |
+ |
{ |
966 |
+ |
if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) { |
967 |
+ |
sprintf(errmsg, "seek error on file '%s'", e->key); |
968 |
+ |
error(SYSTEM, errmsg); |
969 |
+ |
} |
970 |
+ |
} |
971 |
+ |
|
972 |
+ |
/* recover output if possible */ |
973 |
+ |
void |
974 |
+ |
recover_output(FILE *fin) |
975 |
+ |
{ |
976 |
+ |
off_t lastout = -1; |
977 |
+ |
int outvsiz; |
978 |
+ |
char *outvfmt; |
979 |
+ |
int i, j; |
980 |
+ |
MODCONT *mp; |
981 |
+ |
int ofl; |
982 |
+ |
char oname[1024]; |
983 |
+ |
LUENT *ment, *oent; |
984 |
+ |
FILE *fp; |
985 |
+ |
off_t nvals; |
986 |
+ |
|
987 |
+ |
switch (outfmt) { |
988 |
+ |
case 'a': |
989 |
+ |
error(USER, "cannot recover ASCII output"); |
990 |
+ |
return; |
991 |
+ |
case 'f': |
992 |
+ |
outvsiz = sizeof(float)*3; |
993 |
+ |
outvfmt = "float"; |
994 |
+ |
break; |
995 |
+ |
case 'd': |
996 |
+ |
outvsiz = sizeof(double)*3; |
997 |
+ |
outvfmt = "double"; |
998 |
+ |
break; |
999 |
+ |
case 'c': |
1000 |
+ |
outvsiz = sizeof(COLR); |
1001 |
+ |
outvfmt = COLRFMT; |
1002 |
+ |
break; |
1003 |
+ |
default: |
1004 |
+ |
error(INTERNAL, "botched output format"); |
1005 |
+ |
return; |
1006 |
+ |
} |
1007 |
+ |
/* check modifier outputs */ |
1008 |
+ |
for (i = 0; i < nmods; i++) { |
1009 |
+ |
ment = lu_find(&modconttab,modname[i]); |
1010 |
+ |
mp = (MODCONT *)ment->data; |
1011 |
+ |
if (mp->outspec == NULL) |
1012 |
+ |
error(USER, "cannot recover from stdout"); |
1013 |
+ |
for (j = 0; ; j++) { /* check each bin's file */ |
1014 |
+ |
ofl = ofname(oname, mp->outspec, mp->modname, j); |
1015 |
+ |
if (ofl < 0) |
1016 |
+ |
error(USER, "bad output file specification"); |
1017 |
+ |
oent = lu_find(&ofiletab, oname); |
1018 |
+ |
if (oent->data != NULL) { /* saw this one already */ |
1019 |
+ |
if (ofl & OF_BIN) |
1020 |
+ |
continue; |
1021 |
+ |
break; |
1022 |
+ |
} |
1023 |
+ |
if (oname[0] == '!') |
1024 |
+ |
error(USER, "cannot recover from command"); |
1025 |
+ |
/* open output */ |
1026 |
+ |
fp = fopen(oname, "rb+"); |
1027 |
+ |
if (fp == NULL) |
1028 |
+ |
break; /* must be end of modifier */ |
1029 |
+ |
nvals = lseek(fileno(fp), 0, SEEK_END); |
1030 |
+ |
if (nvals <= 0) { |
1031 |
+ |
lastout = 0; /* empty output, quit here */ |
1032 |
+ |
fclose(fp); |
1033 |
+ |
break; |
1034 |
+ |
} |
1035 |
+ |
lseek(fileno(fp), 0, SEEK_SET); |
1036 |
+ |
if (header) { |
1037 |
+ |
int xr, yr; |
1038 |
+ |
if (checkheader(fp, outvfmt, NULL) != 1) { |
1039 |
+ |
sprintf(errmsg, |
1040 |
+ |
"format mismatch for '%s'", |
1041 |
+ |
oname); |
1042 |
+ |
error(USER, errmsg); |
1043 |
+ |
} |
1044 |
+ |
if (xres > 0 && yres > 0 && |
1045 |
+ |
(!fscnresolu(&xr, &yr, fp) || |
1046 |
+ |
xr != xres || |
1047 |
+ |
yr != yres)) { |
1048 |
+ |
sprintf(errmsg, |
1049 |
+ |
"resolution mismatch for '%s'", |
1050 |
+ |
oname); |
1051 |
+ |
error(USER, errmsg); |
1052 |
+ |
} |
1053 |
+ |
} |
1054 |
+ |
nvals = (nvals - (off_t)ftell(fp)) / outvsiz; |
1055 |
+ |
if (lastout < 0 || nvals < lastout) |
1056 |
+ |
lastout = nvals; |
1057 |
+ |
if (oent->key == NULL) /* new entry */ |
1058 |
+ |
oent->key = strcpy((char *) |
1059 |
+ |
malloc(strlen(oname)+1), oname); |
1060 |
+ |
oent->data = (char *)fp; |
1061 |
+ |
if (!(ofl & OF_BIN)) |
1062 |
+ |
break; /* no bin separation */ |
1063 |
+ |
} |
1064 |
+ |
if (!lastout) { /* empty output */ |
1065 |
+ |
error(WARNING, "no previous data to recover"); |
1066 |
+ |
lu_done(&ofiletab); /* reclose all outputs */ |
1067 |
+ |
return; |
1068 |
+ |
} |
1069 |
+ |
if (j > mp->nbins) { /* preallocate modifier bins */ |
1070 |
+ |
mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + |
1071 |
+ |
(j-1)*sizeof(DCOLOR)); |
1072 |
+ |
if (mp == NULL) |
1073 |
+ |
error(SYSTEM, "out of memory in recover_output"); |
1074 |
+ |
memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); |
1075 |
+ |
mp->nbins = j; |
1076 |
+ |
ment->data = (char *)mp; |
1077 |
+ |
} |
1078 |
+ |
} |
1079 |
+ |
if (lastout < 0) { |
1080 |
+ |
error(WARNING, "no output files to recover"); |
1081 |
+ |
return; |
1082 |
+ |
} |
1083 |
+ |
/* seek on all files */ |
1084 |
+ |
nvals = lastout * outvsiz; |
1085 |
+ |
lu_doall(&ofiletab, myseeko, &nvals); |
1086 |
+ |
/* discard input */ |
1087 |
+ |
for (nvals = 0; nvals < lastout; nvals++) |
1088 |
+ |
if (getinp(oname, fin) <= 0) |
1089 |
+ |
error(USER, "unexpected EOF on input"); |
1090 |
+ |
lastray = lastdone = (unsigned long)lastout; |
1091 |
+ |
if (raysleft) |
1092 |
+ |
raysleft -= lastray; |
1093 |
|
} |