16 |
|
#include "lookup.h" |
17 |
|
#include "calcomp.h" |
18 |
|
|
19 |
+ |
#ifndef MAXMODLIST |
20 |
|
#define MAXMODLIST 1024 /* maximum modifiers we'll track */ |
21 |
+ |
#endif |
22 |
|
|
23 |
|
int treebufsiz = BUFSIZ; /* current tree buffer size */ |
24 |
|
|
51 |
|
|
52 |
|
LUTAB ofiletab = LU_SINIT(free,closefile); /* output file table */ |
53 |
|
|
54 |
+ |
#define OF_MODIFIER 01 |
55 |
+ |
#define OF_BIN 02 |
56 |
+ |
|
57 |
|
FILE *getofile(const char *ospec, const char *mname, int bn); |
58 |
+ |
int ofname(char *oname, const char *ospec, const char *mname, int bn); |
59 |
+ |
void printheader(FILE *fout, const char *info); |
60 |
+ |
void printresolu(FILE *fout); |
61 |
|
|
62 |
|
/* |
63 |
|
* The rcont structure is used to manage i/o with a particular |
75 |
|
}; /* rtrace process buffer */ |
76 |
|
|
77 |
|
/* rtrace command and defaults */ |
78 |
< |
char *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3", |
79 |
< |
"-ab", "1", "-ad", "128", "-lr", "-10", }; |
80 |
< |
int rtargc = 11; |
78 |
> |
char *rtargv[256+2*MAXMODLIST] = { "rtrace", |
79 |
> |
"-dj", ".5", "-dr", "3", |
80 |
> |
"-ab", "1", "-ad", "128", }; |
81 |
> |
int rtargc = 9; |
82 |
|
/* overriding rtrace options */ |
83 |
|
char *myrtopts[] = { "-o~~TmWdp", "-h-", "-x", "1", "-y", "0", |
84 |
|
"-dt", "0", "-as", "0", "-aa", "0", NULL }; |
102 |
|
int xres = 0; /* horiz. output resolution */ |
103 |
|
int yres = 0; /* vert. output resolution */ |
104 |
|
|
105 |
< |
long raysleft; /* number of rays left to trace */ |
105 |
> |
unsigned long raysleft; /* number of rays left to trace */ |
106 |
|
long waitflush; /* how long until next flush */ |
107 |
|
|
108 |
|
unsigned long lastray = 0; /* last ray number sent */ |
113 |
|
const char *modname[MAXMODLIST]; /* ordered modifier name list */ |
114 |
|
int nmods = 0; /* number of modifiers */ |
115 |
|
|
116 |
+ |
#define queue_length() (lastray - lastdone) |
117 |
+ |
|
118 |
|
MODCONT *addmodifier(char *modn, char *outf, char *binv); |
119 |
+ |
void addmodfile(char *fname, char *outf, char *binv); |
120 |
|
|
121 |
|
void init(int np); |
122 |
|
int done_rprocs(struct rtproc *rtp); |
123 |
< |
void trace_contribs(FILE *fp); |
123 |
> |
void recover_output(FILE *fin); |
124 |
> |
void trace_contribs(FILE *fin); |
125 |
|
struct rtproc *wait_rproc(void); |
126 |
|
struct rtproc *get_rproc(void); |
127 |
|
void queue_raytree(struct rtproc *rtp); |
128 |
|
void process_queue(void); |
129 |
|
|
130 |
< |
void putcontrib(const DCOLOR cnt, FILE *fout); |
130 |
> |
void put_contrib(const DCOLOR cnt, FILE *fout); |
131 |
|
void add_contrib(const char *modn); |
132 |
|
void done_contrib(void); |
133 |
|
|
134 |
+ |
/* return number of open rtrace processes */ |
135 |
+ |
static int |
136 |
+ |
nrtprocs(void) |
137 |
+ |
{ |
138 |
+ |
int nrtp = 0; |
139 |
+ |
struct rtproc *rtp; |
140 |
+ |
|
141 |
+ |
for (rtp = &rt0; rtp != NULL; rtp = rtp->next) |
142 |
+ |
nrtp += rtp->pd.running; |
143 |
+ |
return(nrtp); |
144 |
+ |
} |
145 |
+ |
|
146 |
|
/* set input/output format */ |
147 |
|
static void |
148 |
|
setformat(const char *fmt) |
183 |
|
main(int argc, char *argv[]) |
184 |
|
{ |
185 |
|
int nprocs = 1; |
186 |
+ |
int recover = 0; |
187 |
|
char *curout = NULL; |
188 |
|
char *binval = NULL; |
189 |
|
char fmt[8]; |
190 |
|
int i, j; |
191 |
+ |
/* need at least one argument */ |
192 |
+ |
if (argc < 2) { |
193 |
+ |
fprintf(stderr, |
194 |
+ |
"Usage: %s [-n nprocs][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n", |
195 |
+ |
argv[0]); |
196 |
+ |
exit(1); |
197 |
+ |
} |
198 |
|
/* global program name */ |
199 |
|
gargv = argv; |
200 |
< |
/* set up calcomp mode */ |
200 |
> |
/* initialize calcomp routines */ |
201 |
|
esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW; |
202 |
|
esupport &= ~(E_OUTCHAN); |
203 |
+ |
varset("PI", ':', PI); |
204 |
|
/* get our options */ |
205 |
|
for (i = 1; i < argc-1; i++) { |
206 |
|
/* expand arguments */ |
213 |
|
} |
214 |
|
if (argv[i][0] == '-') |
215 |
|
switch (argv[i][1]) { |
216 |
+ |
case 'r': /* recover output */ |
217 |
+ |
if (argv[i][2]) break; |
218 |
+ |
recover++; |
219 |
+ |
continue; |
220 |
|
case 'n': /* number of processes */ |
221 |
< |
if (argv[i][2] || i >= argc-1) break; |
221 |
> |
if (argv[i][2] || i >= argc-2) break; |
222 |
|
nprocs = atoi(argv[++i]); |
223 |
|
if (nprocs <= 0) |
224 |
|
error(USER, "illegal number of processes"); |
243 |
|
case 'f': /* file or i/o format */ |
244 |
|
if (!argv[i][2]) { |
245 |
|
char *fpath; |
246 |
< |
if (i >= argc-1) break; |
246 |
> |
if (i >= argc-2) break; |
247 |
|
fpath = getpath(argv[++i], |
248 |
|
getrlibpath(), R_OK); |
249 |
|
if (fpath == NULL) { |
258 |
|
setformat(argv[i]+2); |
259 |
|
continue; |
260 |
|
case 'e': /* expression */ |
261 |
< |
if (argv[i][2] || i >= argc-1) break; |
261 |
> |
if (argv[i][2] || i >= argc-2) break; |
262 |
|
scompile(argv[++i], NULL, 0); |
263 |
|
continue; |
264 |
|
case 'o': /* output file spec. */ |
265 |
< |
if (argv[i][2] || i >= argc-1) break; |
265 |
> |
if (argv[i][2] || i >= argc-2) break; |
266 |
|
curout = argv[++i]; |
267 |
|
continue; |
268 |
|
case 'x': /* horiz. output resolution */ |
269 |
< |
if (argv[i][2] || i >= argc-1) break; |
269 |
> |
if (argv[i][2] || i >= argc-2) break; |
270 |
|
xres = atoi(argv[++i]); |
271 |
|
continue; |
272 |
|
case 'y': /* vert. output resolution */ |
273 |
< |
if (argv[i][2] || i >= argc-1) break; |
273 |
> |
if (argv[i][2] || i >= argc-2) break; |
274 |
|
yres = atoi(argv[++i]); |
275 |
|
continue; |
276 |
|
case 'b': /* bin expression */ |
277 |
< |
if (argv[i][2] || i >= argc-1) break; |
277 |
> |
if (argv[i][2] || i >= argc-2) break; |
278 |
|
binval = argv[++i]; |
279 |
|
continue; |
280 |
|
case 'm': /* modifier name */ |
281 |
< |
if (argv[i][2] || i >= argc-1) break; |
281 |
> |
if (argv[i][2] || i >= argc-2) break; |
282 |
|
rtargv[rtargc++] = "-ti"; |
283 |
|
rtargv[rtargc++] = argv[++i]; |
284 |
|
addmodifier(argv[i], curout, binval); |
285 |
|
continue; |
286 |
+ |
case 'M': /* modifier file */ |
287 |
+ |
if (argv[i][2] || i >= argc-2) break; |
288 |
+ |
rtargv[rtargc++] = "-tI"; |
289 |
+ |
rtargv[rtargc++] = argv[++i]; |
290 |
+ |
addmodfile(argv[i], curout, binval); |
291 |
+ |
continue; |
292 |
|
} |
293 |
|
rtargv[rtargc++] = argv[i]; /* assume rtrace option */ |
294 |
|
} |
337 |
|
error(USER, "missing octree argument"); |
338 |
|
rtargv[rtargc++] = octree = argv[i]; |
339 |
|
rtargv[rtargc] = NULL; |
340 |
< |
/* start rtrace & compute contributions */ |
340 |
> |
/* start rtrace */ |
341 |
|
init(nprocs); |
342 |
< |
trace_contribs(stdin); |
342 |
> |
if (recover) /* perform recovery if requested */ |
343 |
> |
recover_output(stdin); |
344 |
> |
trace_contribs(stdin); /* compute contributions */ |
345 |
|
quit(0); |
346 |
|
} |
347 |
|
|
348 |
+ |
#ifndef SIGALRM |
349 |
+ |
#define SIGALRM SIGTERM |
350 |
+ |
#endif |
351 |
|
/* kill persistent rtrace process */ |
352 |
|
static void |
353 |
|
killpersist(void) |
354 |
|
{ |
355 |
|
FILE *fp = fopen(persistfn, "r"); |
356 |
< |
int pid; |
356 |
> |
RT_PID pid; |
357 |
|
|
358 |
|
if (fp == NULL) |
359 |
|
return; |
449 |
|
rtp->next = NULL; /* terminate list */ |
450 |
|
if (yres > 0) { |
451 |
|
if (xres > 0) |
452 |
< |
raysleft = xres*yres; |
452 |
> |
raysleft = (unsigned long)xres*yres; |
453 |
|
else |
454 |
|
raysleft = yres; |
455 |
|
} else |
487 |
|
return mp; |
488 |
|
} |
489 |
|
|
490 |
+ |
/* add modifiers from a file list */ |
491 |
+ |
void |
492 |
+ |
addmodfile(char *fname, char *outf, char *binv) |
493 |
+ |
{ |
494 |
+ |
char *mname[MAXMODLIST]; |
495 |
+ |
int i; |
496 |
+ |
/* find the file & store strings */ |
497 |
+ |
if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) { |
498 |
+ |
sprintf(errmsg, "cannot find modifier file '%s'", fname); |
499 |
+ |
error(SYSTEM, errmsg); |
500 |
+ |
} |
501 |
+ |
for (i = 0; mname[i]; i++) /* add each one */ |
502 |
+ |
addmodifier(mname[i], outf, binv); |
503 |
+ |
} |
504 |
+ |
|
505 |
|
/* put string to stderr */ |
506 |
|
void |
507 |
|
eputs(char *s) |
517 |
|
midline = s[strlen(s)-1] != '\n'; |
518 |
|
} |
519 |
|
|
520 |
+ |
/* construct output file name and return flags whether modifier/bin present */ |
521 |
+ |
int |
522 |
+ |
ofname(char *oname, const char *ospec, const char *mname, int bn) |
523 |
+ |
{ |
524 |
+ |
const char *mnp = NULL; |
525 |
+ |
const char *bnp = NULL; |
526 |
+ |
const char *cp; |
527 |
+ |
|
528 |
+ |
if (ospec == NULL) |
529 |
+ |
return -1; |
530 |
+ |
for (cp = ospec; *cp; cp++) /* check format position(s) */ |
531 |
+ |
if (*cp == '%') { |
532 |
+ |
do |
533 |
+ |
++cp; |
534 |
+ |
while (isdigit(*cp)); |
535 |
+ |
switch (*cp) { |
536 |
+ |
case '%': |
537 |
+ |
break; |
538 |
+ |
case 's': |
539 |
+ |
if (mnp != NULL) |
540 |
+ |
return -1; |
541 |
+ |
mnp = cp; |
542 |
+ |
break; |
543 |
+ |
case 'd': |
544 |
+ |
if (bnp != NULL) |
545 |
+ |
return -1; |
546 |
+ |
bnp = cp; |
547 |
+ |
break; |
548 |
+ |
default: |
549 |
+ |
return -1; |
550 |
+ |
} |
551 |
+ |
} |
552 |
+ |
if (mnp != NULL) { /* create file name */ |
553 |
+ |
if (bnp != NULL) { |
554 |
+ |
if (bnp > mnp) |
555 |
+ |
sprintf(oname, ospec, mname, bn); |
556 |
+ |
else |
557 |
+ |
sprintf(oname, ospec, bn, mname); |
558 |
+ |
return OF_MODIFIER|OF_BIN; |
559 |
+ |
} |
560 |
+ |
sprintf(oname, ospec, mname); |
561 |
+ |
return OF_MODIFIER; |
562 |
+ |
} |
563 |
+ |
if (bnp != NULL) { |
564 |
+ |
sprintf(oname, ospec, bn); |
565 |
+ |
return OF_BIN; |
566 |
+ |
} |
567 |
+ |
strcpy(oname, ospec); |
568 |
+ |
return 0; |
569 |
+ |
} |
570 |
+ |
|
571 |
|
/* write header to the given output stream */ |
572 |
|
void |
573 |
< |
printheader(FILE *fout) |
573 |
> |
printheader(FILE *fout, const char *info) |
574 |
|
{ |
575 |
|
extern char VersionID[]; |
576 |
|
FILE *fin = fopen(octree, "r"); |
582 |
|
printargs(gargc-1, gargv, fout); /* add our command */ |
583 |
|
fprintf(fout, "SOFTWARE= %s\n", VersionID); |
584 |
|
fputnow(fout); |
585 |
+ |
if (info != NULL) /* add extra info if given */ |
586 |
+ |
fputs(info, fout); |
587 |
|
switch (outfmt) { /* add output format */ |
588 |
|
case 'a': |
589 |
|
fputformat("ascii", fout); |
599 |
|
break; |
600 |
|
} |
601 |
|
fputc('\n', fout); |
602 |
+ |
} |
603 |
+ |
|
604 |
+ |
/* write resolution string to given output stream */ |
605 |
+ |
void |
606 |
+ |
printresolu(FILE *fout) |
607 |
+ |
{ |
608 |
|
if (xres > 0) { |
609 |
|
if (yres > 0) /* resolution string */ |
610 |
|
fprtresolu(xres, yres, fout); |
616 |
|
FILE * |
617 |
|
getofile(const char *ospec, const char *mname, int bn) |
618 |
|
{ |
619 |
< |
const char *mnp = NULL; |
620 |
< |
const char *bnp = NULL; |
498 |
< |
const char *cp; |
499 |
< |
char ofname[1024]; |
619 |
> |
int ofl; |
620 |
> |
char oname[1024]; |
621 |
|
LUENT *lep; |
622 |
|
|
623 |
|
if (ospec == NULL) { /* use stdout? */ |
625 |
|
if (outfmt != 'a') |
626 |
|
SET_FILE_BINARY(stdout); |
627 |
|
if (header) |
628 |
< |
printheader(stdout); |
628 |
> |
printheader(stdout, NULL); |
629 |
> |
printresolu(stdout); |
630 |
|
} |
631 |
|
using_stdout = 1; |
632 |
|
return stdout; |
633 |
|
} |
634 |
< |
for (cp = ospec; *cp; cp++) /* check format position(s) */ |
635 |
< |
if (*cp == '%') { |
636 |
< |
do |
637 |
< |
++cp; |
638 |
< |
while (isdigit(*cp)); |
639 |
< |
switch (*cp) { |
518 |
< |
case '%': |
519 |
< |
break; |
520 |
< |
case 's': |
521 |
< |
if (mnp != NULL) |
522 |
< |
goto badspec; |
523 |
< |
mnp = cp; |
524 |
< |
break; |
525 |
< |
case 'd': |
526 |
< |
if (bnp != NULL) |
527 |
< |
goto badspec; |
528 |
< |
bnp = cp; |
529 |
< |
break; |
530 |
< |
default: |
531 |
< |
goto badspec; |
532 |
< |
} |
533 |
< |
} |
534 |
< |
if (mnp != NULL) { /* create file name */ |
535 |
< |
if (bnp != NULL) { |
536 |
< |
if (bnp > mnp) |
537 |
< |
sprintf(ofname, ospec, mname, bn); |
538 |
< |
else |
539 |
< |
sprintf(ofname, ospec, bn, mname); |
540 |
< |
} else |
541 |
< |
sprintf(ofname, ospec, mname); |
542 |
< |
} else if (bnp != NULL) |
543 |
< |
sprintf(ofname, ospec, bn); |
544 |
< |
else |
545 |
< |
strcpy(ofname, ospec); |
546 |
< |
lep = lu_find(&ofiletab, ofname); /* look it up */ |
634 |
> |
ofl = ofname(oname, ospec, mname, bn); /* get output name */ |
635 |
> |
if (ofl < 0) { |
636 |
> |
sprintf(errmsg, "bad output format '%s'", ospec); |
637 |
> |
error(USER, errmsg); |
638 |
> |
} |
639 |
> |
lep = lu_find(&ofiletab, oname); /* look it up */ |
640 |
|
if (lep->key == NULL) /* new entry */ |
641 |
< |
lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname); |
641 |
> |
lep->key = strcpy((char *)malloc(strlen(oname)+1), oname); |
642 |
|
if (lep->data == NULL) { /* open output file */ |
643 |
|
FILE *fp; |
644 |
|
int i; |
645 |
< |
if (ofname[0] == '!') /* output to command */ |
646 |
< |
fp = popen(ofname+1, "w"); |
645 |
> |
if (oname[0] == '!') /* output to command */ |
646 |
> |
fp = popen(oname+1, "w"); |
647 |
|
else |
648 |
< |
fp = fopen(ofname, "w"); |
648 |
> |
fp = fopen(oname, "w"); |
649 |
|
if (fp == NULL) { |
650 |
< |
sprintf(errmsg, "cannot open '%s' for writing", ofname); |
650 |
> |
sprintf(errmsg, "cannot open '%s' for writing", oname); |
651 |
|
error(SYSTEM, errmsg); |
652 |
|
} |
653 |
|
if (outfmt != 'a') |
654 |
|
SET_FILE_BINARY(fp); |
655 |
< |
if (header) |
656 |
< |
printheader(fp); |
655 |
> |
if (header) { |
656 |
> |
char info[512]; |
657 |
> |
char *cp = info; |
658 |
> |
if (ofl & OF_MODIFIER) { |
659 |
> |
sprintf(cp, "MODIFIER=%s\n", mname); |
660 |
> |
while (*cp) ++cp; |
661 |
> |
} |
662 |
> |
if (ofl & OF_BIN) { |
663 |
> |
sprintf(cp, "BIN=%d\n", bn); |
664 |
> |
while (*cp) ++cp; |
665 |
> |
} |
666 |
> |
*cp = '\0'; |
667 |
> |
printheader(fp, info); |
668 |
> |
} |
669 |
> |
printresolu(fp); |
670 |
|
/* play catch-up */ |
671 |
|
for (i = 0; i < lastdone; i++) { |
672 |
|
static const DCOLOR nocontrib = BLKCOLOR; |
673 |
< |
putcontrib(nocontrib, fp); |
673 |
> |
put_contrib(nocontrib, fp); |
674 |
|
if (outfmt == 'a') |
675 |
|
putc('\n', fp); |
676 |
|
} |
679 |
|
lep->data = (char *)fp; |
680 |
|
} |
681 |
|
return (FILE *)lep->data; /* return open file pointer */ |
576 |
– |
badspec: |
577 |
– |
sprintf(errmsg, "bad output format '%s'", ospec); |
578 |
– |
error(USER, errmsg); |
579 |
– |
return NULL; /* pro forma return */ |
682 |
|
} |
683 |
|
|
684 |
|
/* read input ray into buffer */ |
741 |
|
bn = (int)(evalue(mp->binv) + .5); |
742 |
|
if (bn <= 0) |
743 |
|
bn = 0; |
744 |
< |
else if (bn > mp->nbins) { /* new bin */ |
744 |
> |
else if (bn >= mp->nbins) { /* new bin */ |
745 |
|
mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + |
746 |
|
bn*sizeof(DCOLOR)); |
747 |
|
if (mp == NULL) |
772 |
|
|
773 |
|
/* put out ray contribution to file */ |
774 |
|
void |
775 |
< |
putcontrib(const DCOLOR cnt, FILE *fout) |
775 |
> |
put_contrib(const DCOLOR cnt, FILE *fout) |
776 |
|
{ |
777 |
|
float fv[3]; |
778 |
|
COLR cv; |
805 |
|
{ |
806 |
|
int i, j; |
807 |
|
MODCONT *mp; |
808 |
+ |
FILE *fp; |
809 |
|
/* output modifiers in order */ |
810 |
|
for (i = 0; i < nmods; i++) { |
811 |
|
mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; |
812 |
< |
for (j = 0; j < mp->nbins; j++) |
813 |
< |
putcontrib(mp->cbin[j], |
812 |
> |
fp = getofile(mp->outspec, mp->modname, 0); |
813 |
> |
put_contrib(mp->cbin[0], fp); |
814 |
> |
if (mp->nbins > 3 && /* minor optimization */ |
815 |
> |
fp == getofile(mp->outspec, mp->modname, 1)) |
816 |
> |
for (j = 1; j < mp->nbins; j++) |
817 |
> |
put_contrib(mp->cbin[j], fp); |
818 |
> |
else |
819 |
> |
for (j = 1; j < mp->nbins; j++) |
820 |
> |
put_contrib(mp->cbin[j], |
821 |
|
getofile(mp->outspec, mp->modname, j)); |
822 |
|
/* clear for next ray tree */ |
823 |
|
memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); |
940 |
|
if (rt->bsiz + BUFSIZ <= treebufsiz) |
941 |
|
rt->bsiz = treebufsiz; |
942 |
|
else |
943 |
< |
rt->bsiz = treebufsiz += BUFSIZ; |
943 |
> |
treebufsiz = rt->bsiz += BUFSIZ; |
944 |
|
rt->buf = (char *)realloc(rt->buf, rt->bsiz); |
945 |
|
} |
946 |
|
if (rt->buf == NULL) |
981 |
|
struct rtproc *rtp; |
982 |
|
/* loop over input */ |
983 |
|
while ((iblen = getinp(inpbuf, fin)) > 0) { |
984 |
< |
if (lastray+1 < lastray) { /* counter rollover? */ |
984 |
> |
if (lastray+1 < lastray || /* need reset? */ |
985 |
> |
queue_length() > 5*nrtprocs()) { |
986 |
|
while (wait_rproc() != NULL) |
987 |
|
process_queue(); |
988 |
|
lastdone = lastray = 0; |
990 |
|
rtp = get_rproc(); /* get avail. rtrace process */ |
991 |
|
rtp->raynum = ++lastray; /* assign ray to it */ |
992 |
|
writebuf(rtp->pd.w, inpbuf, iblen); |
993 |
< |
if (!--raysleft) |
993 |
> |
if (raysleft && !--raysleft) |
994 |
|
break; |
995 |
|
process_queue(); /* catch up with results */ |
996 |
|
} |
997 |
|
while (wait_rproc() != NULL) /* process outstanding rays */ |
998 |
|
process_queue(); |
999 |
< |
if (raysleft > 0) |
999 |
> |
if (raysleft) |
1000 |
|
error(USER, "unexpected EOF on input"); |
1001 |
+ |
lu_done(&ofiletab); /* close output files */ |
1002 |
+ |
} |
1003 |
+ |
|
1004 |
+ |
/* seek on the given output file */ |
1005 |
+ |
static int |
1006 |
+ |
myseeko(const LUENT *e, void *p) |
1007 |
+ |
{ |
1008 |
+ |
if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) { |
1009 |
+ |
sprintf(errmsg, "seek error on file '%s'", e->key); |
1010 |
+ |
error(SYSTEM, errmsg); |
1011 |
+ |
} |
1012 |
+ |
} |
1013 |
+ |
|
1014 |
+ |
/* recover output if possible */ |
1015 |
+ |
void |
1016 |
+ |
recover_output(FILE *fin) |
1017 |
+ |
{ |
1018 |
+ |
off_t lastout = -1; |
1019 |
+ |
int outvsiz; |
1020 |
+ |
char *outvfmt; |
1021 |
+ |
int i, j; |
1022 |
+ |
MODCONT *mp; |
1023 |
+ |
int ofl; |
1024 |
+ |
char oname[1024]; |
1025 |
+ |
LUENT *ment, *oent; |
1026 |
+ |
FILE *fp; |
1027 |
+ |
off_t nvals; |
1028 |
+ |
|
1029 |
+ |
switch (outfmt) { |
1030 |
+ |
case 'a': |
1031 |
+ |
error(USER, "cannot recover ASCII output"); |
1032 |
+ |
return; |
1033 |
+ |
case 'f': |
1034 |
+ |
outvsiz = sizeof(float)*3; |
1035 |
+ |
outvfmt = "float"; |
1036 |
+ |
break; |
1037 |
+ |
case 'd': |
1038 |
+ |
outvsiz = sizeof(double)*3; |
1039 |
+ |
outvfmt = "double"; |
1040 |
+ |
break; |
1041 |
+ |
case 'c': |
1042 |
+ |
outvsiz = sizeof(COLR); |
1043 |
+ |
outvfmt = COLRFMT; |
1044 |
+ |
break; |
1045 |
+ |
default: |
1046 |
+ |
error(INTERNAL, "botched output format"); |
1047 |
+ |
return; |
1048 |
+ |
} |
1049 |
+ |
/* check modifier outputs */ |
1050 |
+ |
for (i = 0; i < nmods; i++) { |
1051 |
+ |
ment = lu_find(&modconttab,modname[i]); |
1052 |
+ |
mp = (MODCONT *)ment->data; |
1053 |
+ |
if (mp->outspec == NULL) |
1054 |
+ |
error(USER, "cannot recover from stdout"); |
1055 |
+ |
for (j = 0; ; j++) { /* check each bin's file */ |
1056 |
+ |
ofl = ofname(oname, mp->outspec, mp->modname, j); |
1057 |
+ |
if (ofl < 0) |
1058 |
+ |
error(USER, "bad output file specification"); |
1059 |
+ |
oent = lu_find(&ofiletab, oname); |
1060 |
+ |
if (oent->data != NULL) { /* saw this one already */ |
1061 |
+ |
if (ofl & OF_BIN) |
1062 |
+ |
continue; |
1063 |
+ |
break; |
1064 |
+ |
} |
1065 |
+ |
if (oname[0] == '!') |
1066 |
+ |
error(USER, "cannot recover from command"); |
1067 |
+ |
/* open output */ |
1068 |
+ |
fp = fopen(oname, "rb+"); |
1069 |
+ |
if (fp == NULL) { |
1070 |
+ |
if (j) |
1071 |
+ |
break; /* assume end of modifier */ |
1072 |
+ |
sprintf(errmsg, "missing recover file '%s'", |
1073 |
+ |
oname); |
1074 |
+ |
error(USER, errmsg); |
1075 |
+ |
} |
1076 |
+ |
nvals = lseek(fileno(fp), 0, SEEK_END); |
1077 |
+ |
if (nvals <= 0) { |
1078 |
+ |
lastout = 0; /* empty output, quit here */ |
1079 |
+ |
fclose(fp); |
1080 |
+ |
break; |
1081 |
+ |
} |
1082 |
+ |
lseek(fileno(fp), 0, SEEK_SET); |
1083 |
+ |
if (header) { |
1084 |
+ |
int xr, yr; |
1085 |
+ |
if (checkheader(fp, outvfmt, NULL) != 1) { |
1086 |
+ |
sprintf(errmsg, |
1087 |
+ |
"format mismatch for '%s'", |
1088 |
+ |
oname); |
1089 |
+ |
error(USER, errmsg); |
1090 |
+ |
} |
1091 |
+ |
if (xres > 0 && yres > 0 && |
1092 |
+ |
(!fscnresolu(&xr, &yr, fp) || |
1093 |
+ |
xr != xres || |
1094 |
+ |
yr != yres)) { |
1095 |
+ |
sprintf(errmsg, |
1096 |
+ |
"resolution mismatch for '%s'", |
1097 |
+ |
oname); |
1098 |
+ |
error(USER, errmsg); |
1099 |
+ |
} |
1100 |
+ |
} |
1101 |
+ |
nvals = (nvals - (off_t)ftell(fp)) / outvsiz; |
1102 |
+ |
if (lastout < 0 || nvals < lastout) |
1103 |
+ |
lastout = nvals; |
1104 |
+ |
if (oent->key == NULL) /* new entry */ |
1105 |
+ |
oent->key = strcpy((char *) |
1106 |
+ |
malloc(strlen(oname)+1), oname); |
1107 |
+ |
oent->data = (char *)fp; |
1108 |
+ |
if (!(ofl & OF_BIN)) |
1109 |
+ |
break; /* no bin separation */ |
1110 |
+ |
} |
1111 |
+ |
if (!lastout) { /* empty output */ |
1112 |
+ |
error(WARNING, "no previous data to recover"); |
1113 |
+ |
lu_done(&ofiletab); /* reclose all outputs */ |
1114 |
+ |
return; |
1115 |
+ |
} |
1116 |
+ |
if (j > mp->nbins) { /* preallocate modifier bins */ |
1117 |
+ |
mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + |
1118 |
+ |
(j-1)*sizeof(DCOLOR)); |
1119 |
+ |
if (mp == NULL) |
1120 |
+ |
error(SYSTEM, "out of memory in recover_output"); |
1121 |
+ |
memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); |
1122 |
+ |
mp->nbins = j; |
1123 |
+ |
ment->data = (char *)mp; |
1124 |
+ |
} |
1125 |
+ |
} |
1126 |
+ |
if (lastout < 0) { |
1127 |
+ |
error(WARNING, "no output files to recover"); |
1128 |
+ |
return; |
1129 |
+ |
} |
1130 |
+ |
if (raysleft && lastout >= raysleft) { |
1131 |
+ |
error(WARNING, "output appears to be complete"); |
1132 |
+ |
/* XXX should read & discard input? */ |
1133 |
+ |
quit(0); |
1134 |
+ |
} |
1135 |
+ |
/* seek on all files */ |
1136 |
+ |
nvals = lastout * outvsiz; |
1137 |
+ |
lu_doall(&ofiletab, myseeko, &nvals); |
1138 |
+ |
/* skip repeated input */ |
1139 |
+ |
for (nvals = 0; nvals < lastout; nvals++) |
1140 |
+ |
if (getinp(oname, fin) <= 0) |
1141 |
+ |
error(USER, "unexpected EOF on input"); |
1142 |
+ |
lastray = lastdone = (unsigned long)lastout; |
1143 |
+ |
if (raysleft) |
1144 |
+ |
raysleft -= lastray; |
1145 |
|
} |