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 */ |
113 |
|
|
114 |
|
void init(int np); |
115 |
|
int done_rprocs(struct rtproc *rtp); |
116 |
< |
void trace_contribs(FILE *fp); |
116 |
> |
void recover_output(FILE *fin); |
117 |
> |
void trace_contribs(FILE *fin); |
118 |
|
struct rtproc *wait_rproc(void); |
119 |
|
struct rtproc *get_rproc(void); |
120 |
|
void queue_raytree(struct rtproc *rtp); |
121 |
|
void process_queue(void); |
122 |
|
|
123 |
< |
void putcontrib(const DCOLOR cnt, FILE *fout); |
123 |
> |
void put_contrib(const DCOLOR cnt, FILE *fout); |
124 |
|
void add_contrib(const char *modn); |
125 |
|
void done_contrib(void); |
126 |
|
|
129 |
|
setformat(const char *fmt) |
130 |
|
{ |
131 |
|
switch (fmt[0]) { |
126 |
– |
case 'a': |
132 |
|
case 'f': |
133 |
|
case 'd': |
134 |
+ |
SET_FILE_BINARY(stdin); |
135 |
+ |
/* fall through */ |
136 |
+ |
case 'a': |
137 |
|
inpfmt = fmt[0]; |
138 |
|
break; |
139 |
|
default: |
164 |
|
main(int argc, char *argv[]) |
165 |
|
{ |
166 |
|
int nprocs = 1; |
167 |
+ |
int recover = 0; |
168 |
|
char *curout = NULL; |
169 |
|
char *binval = NULL; |
170 |
|
char fmt[8]; |
171 |
|
int i, j; |
172 |
|
/* global program name */ |
173 |
|
gargv = argv; |
174 |
< |
/* set up calcomp mode */ |
174 |
> |
/* initialize calcomp routines */ |
175 |
|
esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW; |
176 |
|
esupport &= ~(E_OUTCHAN); |
177 |
+ |
varset("PI", ':', PI); |
178 |
|
/* get our options */ |
179 |
|
for (i = 1; i < argc-1; i++) { |
180 |
|
/* expand arguments */ |
187 |
|
} |
188 |
|
if (argv[i][0] == '-') |
189 |
|
switch (argv[i][1]) { |
190 |
+ |
case 'r': /* recover output */ |
191 |
+ |
if (argv[i][2]) break; |
192 |
+ |
recover++; |
193 |
+ |
continue; |
194 |
|
case 'n': /* number of processes */ |
195 |
|
if (argv[i][2] || i >= argc-1) break; |
196 |
|
nprocs = atoi(argv[++i]); |
216 |
|
break; |
217 |
|
case 'f': /* file or i/o format */ |
218 |
|
if (!argv[i][2]) { |
219 |
+ |
char *fpath; |
220 |
|
if (i >= argc-1) break; |
221 |
< |
fcompile(argv[++i]); |
221 |
> |
fpath = getpath(argv[++i], |
222 |
> |
getrlibpath(), R_OK); |
223 |
> |
if (fpath == NULL) { |
224 |
> |
sprintf(errmsg, |
225 |
> |
"cannot find file '%s'", |
226 |
> |
argv[i]); |
227 |
> |
error(USER, errmsg); |
228 |
> |
} |
229 |
> |
fcompile(fpath); |
230 |
|
continue; |
231 |
|
} |
232 |
|
setformat(argv[i]+2); |
307 |
|
rtargv[rtargc] = NULL; |
308 |
|
/* start rtrace & compute contributions */ |
309 |
|
init(nprocs); |
310 |
+ |
if (recover) /* perform recovery if requested */ |
311 |
+ |
recover_output(stdin); |
312 |
|
trace_contribs(stdin); |
313 |
|
quit(0); |
314 |
|
} |
414 |
|
rtp->next = NULL; /* terminate list */ |
415 |
|
if (yres > 0) { |
416 |
|
if (xres > 0) |
417 |
< |
raysleft = xres*yres; |
417 |
> |
raysleft = (unsigned long)xres*yres; |
418 |
|
else |
419 |
|
raysleft = yres; |
420 |
|
} else |
467 |
|
midline = s[strlen(s)-1] != '\n'; |
468 |
|
} |
469 |
|
|
470 |
+ |
/* construct output file name and return flags whether modifier/bin present */ |
471 |
+ |
int |
472 |
+ |
ofname(char *oname, const char *ospec, const char *mname, int bn) |
473 |
+ |
{ |
474 |
+ |
const char *mnp = NULL; |
475 |
+ |
const char *bnp = NULL; |
476 |
+ |
const char *cp; |
477 |
+ |
|
478 |
+ |
if (ospec == NULL) |
479 |
+ |
return -1; |
480 |
+ |
for (cp = ospec; *cp; cp++) /* check format position(s) */ |
481 |
+ |
if (*cp == '%') { |
482 |
+ |
do |
483 |
+ |
++cp; |
484 |
+ |
while (isdigit(*cp)); |
485 |
+ |
switch (*cp) { |
486 |
+ |
case '%': |
487 |
+ |
break; |
488 |
+ |
case 's': |
489 |
+ |
if (mnp != NULL) |
490 |
+ |
return -1; |
491 |
+ |
mnp = cp; |
492 |
+ |
break; |
493 |
+ |
case 'd': |
494 |
+ |
if (bnp != NULL) |
495 |
+ |
return -1; |
496 |
+ |
bnp = cp; |
497 |
+ |
break; |
498 |
+ |
default: |
499 |
+ |
return -1; |
500 |
+ |
} |
501 |
+ |
} |
502 |
+ |
if (mnp != NULL) { /* create file name */ |
503 |
+ |
if (bnp != NULL) { |
504 |
+ |
if (bnp > mnp) |
505 |
+ |
sprintf(oname, ospec, mname, bn); |
506 |
+ |
else |
507 |
+ |
sprintf(oname, ospec, bn, mname); |
508 |
+ |
return OF_MODIFIER|OF_BIN; |
509 |
+ |
} |
510 |
+ |
sprintf(oname, ospec, mname); |
511 |
+ |
return OF_MODIFIER; |
512 |
+ |
} |
513 |
+ |
if (bnp != NULL) { |
514 |
+ |
sprintf(oname, ospec, bn); |
515 |
+ |
return OF_BIN; |
516 |
+ |
} |
517 |
+ |
strcpy(oname, ospec); |
518 |
+ |
return 0; |
519 |
+ |
} |
520 |
+ |
|
521 |
|
/* write header to the given output stream */ |
522 |
|
void |
523 |
< |
printheader(FILE *fout) |
523 |
> |
printheader(FILE *fout, const char *info) |
524 |
|
{ |
525 |
|
extern char VersionID[]; |
526 |
|
FILE *fin = fopen(octree, "r"); |
532 |
|
printargs(gargc-1, gargv, fout); /* add our command */ |
533 |
|
fprintf(fout, "SOFTWARE= %s\n", VersionID); |
534 |
|
fputnow(fout); |
535 |
+ |
if (info != NULL) /* add extra info if given */ |
536 |
+ |
fputs(info, fout); |
537 |
|
switch (outfmt) { /* add output format */ |
538 |
|
case 'a': |
539 |
|
fputformat("ascii", fout); |
560 |
|
FILE * |
561 |
|
getofile(const char *ospec, const char *mname, int bn) |
562 |
|
{ |
563 |
< |
const char *mnp = NULL; |
564 |
< |
const char *bnp = NULL; |
487 |
< |
const char *cp; |
488 |
< |
char ofname[1024]; |
563 |
> |
int ofl; |
564 |
> |
char oname[1024]; |
565 |
|
LUENT *lep; |
566 |
|
|
567 |
|
if (ospec == NULL) { /* use stdout? */ |
568 |
< |
if (!using_stdout && header) |
569 |
< |
printheader(stdout); |
568 |
> |
if (!using_stdout) { |
569 |
> |
if (outfmt != 'a') |
570 |
> |
SET_FILE_BINARY(stdout); |
571 |
> |
if (header) |
572 |
> |
printheader(stdout, NULL); |
573 |
> |
} |
574 |
|
using_stdout = 1; |
575 |
|
return stdout; |
576 |
|
} |
577 |
< |
for (cp = ospec; *cp; cp++) /* check format position(s) */ |
578 |
< |
if (*cp == '%') { |
579 |
< |
do |
580 |
< |
++cp; |
581 |
< |
while (isdigit(*cp)); |
582 |
< |
switch (*cp) { |
503 |
< |
case '%': |
504 |
< |
break; |
505 |
< |
case 's': |
506 |
< |
if (mnp != NULL) |
507 |
< |
goto badspec; |
508 |
< |
mnp = cp; |
509 |
< |
break; |
510 |
< |
case 'd': |
511 |
< |
if (bnp != NULL) |
512 |
< |
goto badspec; |
513 |
< |
bnp = cp; |
514 |
< |
break; |
515 |
< |
default: |
516 |
< |
goto badspec; |
517 |
< |
} |
518 |
< |
} |
519 |
< |
if (mnp != NULL) { /* create file name */ |
520 |
< |
if (bnp != NULL) { |
521 |
< |
if (bnp > mnp) |
522 |
< |
sprintf(ofname, ospec, mname, bn); |
523 |
< |
else |
524 |
< |
sprintf(ofname, ospec, bn, mname); |
525 |
< |
} else |
526 |
< |
sprintf(ofname, ospec, mname); |
527 |
< |
} else if (bnp != NULL) |
528 |
< |
sprintf(ofname, ospec, bn); |
529 |
< |
else |
530 |
< |
strcpy(ofname, ospec); |
531 |
< |
lep = lu_find(&ofiletab, ofname); /* look it up */ |
577 |
> |
ofl = ofname(oname, ospec, mname, bn); /* get output name */ |
578 |
> |
if (ofl < 0) { |
579 |
> |
sprintf(errmsg, "bad output format '%s'", ospec); |
580 |
> |
error(USER, errmsg); |
581 |
> |
} |
582 |
> |
lep = lu_find(&ofiletab, oname); /* look it up */ |
583 |
|
if (lep->key == NULL) /* new entry */ |
584 |
< |
lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname); |
584 |
> |
lep->key = strcpy((char *)malloc(strlen(oname)+1), oname); |
585 |
|
if (lep->data == NULL) { /* open output file */ |
586 |
< |
FILE *fp = fopen(ofname, "w"); |
586 |
> |
FILE *fp; |
587 |
|
int i; |
588 |
+ |
if (oname[0] == '!') /* output to command */ |
589 |
+ |
fp = popen(oname+1, "w"); |
590 |
+ |
else |
591 |
+ |
fp = fopen(oname, "w"); |
592 |
|
if (fp == NULL) { |
593 |
< |
sprintf(errmsg, "cannot open '%s' for writing", ofname); |
593 |
> |
sprintf(errmsg, "cannot open '%s' for writing", oname); |
594 |
|
error(SYSTEM, errmsg); |
595 |
|
} |
596 |
< |
if (header) |
597 |
< |
printheader(fp); |
596 |
> |
if (outfmt != 'a') |
597 |
> |
SET_FILE_BINARY(fp); |
598 |
> |
if (header) { |
599 |
> |
char info[512]; |
600 |
> |
char *cp = info; |
601 |
> |
if (ofl & OF_MODIFIER) { |
602 |
> |
sprintf(cp, "MODIFIER=%s\n", mname); |
603 |
> |
while (*cp) ++cp; |
604 |
> |
} |
605 |
> |
if (ofl & OF_BIN) { |
606 |
> |
sprintf(cp, "BIN=%d\n", bn); |
607 |
> |
while (*cp) ++cp; |
608 |
> |
} |
609 |
> |
*cp = '\0'; |
610 |
> |
printheader(fp, info); |
611 |
> |
} |
612 |
|
/* play catch-up */ |
613 |
|
for (i = 0; i < lastdone; i++) { |
614 |
|
static const DCOLOR nocontrib = BLKCOLOR; |
615 |
< |
putcontrib(nocontrib, fp); |
615 |
> |
put_contrib(nocontrib, fp); |
616 |
|
if (outfmt == 'a') |
617 |
|
putc('\n', fp); |
618 |
|
} |
621 |
|
lep->data = (char *)fp; |
622 |
|
} |
623 |
|
return (FILE *)lep->data; /* return open file pointer */ |
555 |
– |
badspec: |
556 |
– |
sprintf(errmsg, "bad output format '%s'", ospec); |
557 |
– |
error(USER, errmsg); |
558 |
– |
return NULL; /* pro forma return */ |
624 |
|
} |
625 |
|
|
626 |
|
/* read input ray into buffer */ |
714 |
|
|
715 |
|
/* put out ray contribution to file */ |
716 |
|
void |
717 |
< |
putcontrib(const DCOLOR cnt, FILE *fout) |
717 |
> |
put_contrib(const DCOLOR cnt, FILE *fout) |
718 |
|
{ |
719 |
|
float fv[3]; |
720 |
|
COLR cv; |
747 |
|
{ |
748 |
|
int i, j; |
749 |
|
MODCONT *mp; |
750 |
+ |
FILE *fp; |
751 |
|
/* output modifiers in order */ |
752 |
|
for (i = 0; i < nmods; i++) { |
753 |
|
mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; |
754 |
< |
for (j = 0; j < mp->nbins; j++) |
755 |
< |
putcontrib(mp->cbin[j], |
754 |
> |
fp = getofile(mp->outspec, mp->modname, 0); |
755 |
> |
put_contrib(mp->cbin[0], fp); |
756 |
> |
if (mp->nbins > 3 && /* minor optimization */ |
757 |
> |
fp == getofile(mp->outspec, mp->modname, 1)) |
758 |
> |
for (j = 1; j < mp->nbins; j++) |
759 |
> |
put_contrib(mp->cbin[j], fp); |
760 |
> |
else |
761 |
> |
for (j = 1; j < mp->nbins; j++) |
762 |
> |
put_contrib(mp->cbin[j], |
763 |
|
getofile(mp->outspec, mp->modname, j)); |
764 |
|
/* clear for next ray tree */ |
765 |
|
memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); |
931 |
|
rtp = get_rproc(); /* get avail. rtrace process */ |
932 |
|
rtp->raynum = ++lastray; /* assign ray to it */ |
933 |
|
writebuf(rtp->pd.w, inpbuf, iblen); |
934 |
< |
if (!--raysleft) |
934 |
> |
if (raysleft && !--raysleft) |
935 |
|
break; |
936 |
|
process_queue(); /* catch up with results */ |
937 |
|
} |
938 |
|
while (wait_rproc() != NULL) /* process outstanding rays */ |
939 |
|
process_queue(); |
940 |
< |
if (raysleft > 0) |
940 |
> |
if (raysleft) |
941 |
|
error(USER, "unexpected EOF on input"); |
942 |
+ |
lu_done(&ofiletab); /* close output files */ |
943 |
+ |
} |
944 |
+ |
|
945 |
+ |
/* seek on the given output file */ |
946 |
+ |
static int |
947 |
+ |
myseeko(const LUENT *e, void *p) |
948 |
+ |
{ |
949 |
+ |
if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) { |
950 |
+ |
sprintf(errmsg, "seek error on file '%s'", e->key); |
951 |
+ |
error(SYSTEM, errmsg); |
952 |
+ |
} |
953 |
+ |
} |
954 |
+ |
|
955 |
+ |
/* recover output if possible */ |
956 |
+ |
void |
957 |
+ |
recover_output(FILE *fin) |
958 |
+ |
{ |
959 |
+ |
off_t lastout = -1; |
960 |
+ |
int outvsiz; |
961 |
+ |
char *outvfmt; |
962 |
+ |
int i, j; |
963 |
+ |
MODCONT *mp; |
964 |
+ |
int ofl; |
965 |
+ |
char oname[1024]; |
966 |
+ |
LUENT *ment, *oent; |
967 |
+ |
FILE *fp; |
968 |
+ |
off_t nvals; |
969 |
+ |
|
970 |
+ |
switch (outfmt) { |
971 |
+ |
case 'a': |
972 |
+ |
error(USER, "cannot recover ASCII output"); |
973 |
+ |
return; |
974 |
+ |
case 'f': |
975 |
+ |
outvsiz = sizeof(float)*3; |
976 |
+ |
outvfmt = "float"; |
977 |
+ |
break; |
978 |
+ |
case 'd': |
979 |
+ |
outvsiz = sizeof(double)*3; |
980 |
+ |
outvfmt = "double"; |
981 |
+ |
break; |
982 |
+ |
case 'c': |
983 |
+ |
outvsiz = sizeof(COLR); |
984 |
+ |
outvfmt = COLRFMT; |
985 |
+ |
break; |
986 |
+ |
default: |
987 |
+ |
error(INTERNAL, "botched output format"); |
988 |
+ |
return; |
989 |
+ |
} |
990 |
+ |
/* check modifier outputs */ |
991 |
+ |
for (i = 0; i < nmods; i++) { |
992 |
+ |
ment = lu_find(&modconttab,modname[i]); |
993 |
+ |
mp = (MODCONT *)ment->data; |
994 |
+ |
if (mp->outspec == NULL) |
995 |
+ |
error(USER, "cannot recover from stdout"); |
996 |
+ |
for (j = 0; ; j++) { /* check each bin's file */ |
997 |
+ |
ofl = ofname(oname, mp->outspec, mp->modname, j); |
998 |
+ |
if (ofl < 0) |
999 |
+ |
error(USER, "bad output file specification"); |
1000 |
+ |
oent = lu_find(&ofiletab, oname); |
1001 |
+ |
if (oent->data != NULL) { /* saw this one already */ |
1002 |
+ |
if (ofl & OF_BIN) |
1003 |
+ |
continue; |
1004 |
+ |
break; |
1005 |
+ |
} |
1006 |
+ |
if (oname[0] == '!') |
1007 |
+ |
error(USER, "cannot recover from command"); |
1008 |
+ |
/* open output */ |
1009 |
+ |
fp = fopen(oname, "rb+"); |
1010 |
+ |
if (fp == NULL) |
1011 |
+ |
break; /* must be end of modifier */ |
1012 |
+ |
nvals = lseek(fileno(fp), 0, SEEK_END); |
1013 |
+ |
if (nvals <= 0) { |
1014 |
+ |
lastout = 0; /* empty output, quit here */ |
1015 |
+ |
fclose(fp); |
1016 |
+ |
break; |
1017 |
+ |
} |
1018 |
+ |
lseek(fileno(fp), 0, SEEK_SET); |
1019 |
+ |
if (header) { |
1020 |
+ |
int xr, yr; |
1021 |
+ |
if (checkheader(fp, outvfmt, NULL) != 1) { |
1022 |
+ |
sprintf(errmsg, |
1023 |
+ |
"format mismatch for '%s'", |
1024 |
+ |
oname); |
1025 |
+ |
error(USER, errmsg); |
1026 |
+ |
} |
1027 |
+ |
if (xres > 0 && yres > 0 && |
1028 |
+ |
(!fscnresolu(&xr, &yr, fp) || |
1029 |
+ |
xr != xres || |
1030 |
+ |
yr != yres)) { |
1031 |
+ |
sprintf(errmsg, |
1032 |
+ |
"resolution mismatch for '%s'", |
1033 |
+ |
oname); |
1034 |
+ |
error(USER, errmsg); |
1035 |
+ |
} |
1036 |
+ |
} |
1037 |
+ |
nvals = (nvals - (off_t)ftell(fp)) / outvsiz; |
1038 |
+ |
if (lastout < 0 || nvals < lastout) |
1039 |
+ |
lastout = nvals; |
1040 |
+ |
if (oent->key == NULL) /* new entry */ |
1041 |
+ |
oent->key = strcpy((char *) |
1042 |
+ |
malloc(strlen(oname)+1), oname); |
1043 |
+ |
oent->data = (char *)fp; |
1044 |
+ |
if (!(ofl & OF_BIN)) |
1045 |
+ |
break; /* no bin separation */ |
1046 |
+ |
} |
1047 |
+ |
if (!lastout) { /* empty output */ |
1048 |
+ |
error(WARNING, "no data to recover"); |
1049 |
+ |
lu_done(&ofiletab); /* reclose all outputs */ |
1050 |
+ |
return; |
1051 |
+ |
} |
1052 |
+ |
if (j > mp->nbins) { /* preallocate modifier bins */ |
1053 |
+ |
mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + |
1054 |
+ |
(j-1)*sizeof(DCOLOR)); |
1055 |
+ |
if (mp == NULL) |
1056 |
+ |
error(SYSTEM, "out of memory in recover_output"); |
1057 |
+ |
memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); |
1058 |
+ |
mp->nbins = j; |
1059 |
+ |
ment->data = (char *)mp; |
1060 |
+ |
} |
1061 |
+ |
} |
1062 |
+ |
if (lastout < 0) { |
1063 |
+ |
error(WARNING, "no existing output to recover"); |
1064 |
+ |
return; |
1065 |
+ |
} |
1066 |
+ |
/* seek on all files */ |
1067 |
+ |
nvals = lastout * outvsiz; |
1068 |
+ |
lu_doall(&ofiletab, myseeko, &nvals); |
1069 |
+ |
/* discard input */ |
1070 |
+ |
for (nvals = 0; nvals < lastout; nvals++) |
1071 |
+ |
if (getinp(oname, fin) <= 0) |
1072 |
+ |
error(USER, "unexpected EOF on input"); |
1073 |
+ |
lastray = lastdone = (unsigned long)lastout; |
1074 |
+ |
if (raysleft) |
1075 |
+ |
raysleft -= lastray; |
1076 |
|
} |