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 |
|
|
25 |
|
typedef double DCOLOR[3]; /* double-precision color */ |
26 |
|
|
27 |
|
/* |
28 |
< |
* The modcont structure is used to accumulate ray contributions |
28 |
> |
* The MODCONT structure is used to accumulate ray contributions |
29 |
|
* for a particular modifier, which may be subdivided into bins |
30 |
< |
* if binv is non-NULL. If outspec contains a %s in it, this will |
30 |
> |
* if binv is non-zero. If outspec contains a %s in it, this will |
31 |
|
* be replaced with the modifier name. If outspec contains a %d in it, |
32 |
|
* this will be used to create one output file per bin, otherwise all bins |
33 |
|
* will be written to the same file, in order. If the global outfmt |
46 |
|
|
47 |
|
LUTAB modconttab = LU_SINIT(NULL,mcfree); /* modifier lookup table */ |
48 |
|
|
49 |
< |
/* close output stream */ |
50 |
< |
static void closefile(void *p) { fclose((FILE *)p); } |
49 |
> |
#define CNT_UNKNOWN 0 /* unknown record length */ |
50 |
> |
#define CNT_PIPE (-1) /* output to a pipe */ |
51 |
> |
/* |
52 |
> |
* The STREAMOUT structure holds an open FILE pointer and a count of |
53 |
> |
* the number of RGB triplets per record, or CNT_UNKNOWN (0) if |
54 |
> |
* unknown or CNT_PIPE (-1) if writing to a command. |
55 |
> |
*/ |
56 |
> |
typedef struct { |
57 |
> |
FILE *ofp; /* output file pointer */ |
58 |
> |
int reclen; /* triplets/record */ |
59 |
> |
} STREAMOUT; |
60 |
|
|
61 |
< |
LUTAB ofiletab = LU_SINIT(free,closefile); /* output file table */ |
61 |
> |
/* close output stream and free record */ |
62 |
> |
static void |
63 |
> |
closestream(void *p) |
64 |
> |
{ |
65 |
> |
STREAMOUT *sop = (STREAMOUT *)p; |
66 |
> |
int status; |
67 |
> |
if (sop->reclen == CNT_PIPE) |
68 |
> |
status = pclose(sop->ofp); |
69 |
> |
else |
70 |
> |
status = fclose(sop->ofp); |
71 |
> |
if (status) |
72 |
> |
error(SYSTEM, "error closing output stream"); |
73 |
> |
free(p); |
74 |
> |
} |
75 |
|
|
76 |
+ |
LUTAB ofiletab = LU_SINIT(free,closestream); /* output file table */ |
77 |
+ |
|
78 |
|
#define OF_MODIFIER 01 |
79 |
|
#define OF_BIN 02 |
80 |
|
|
81 |
< |
FILE *getofile(const char *ospec, const char *mname, int bn); |
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); |
85 |
|
|
86 |
|
/* |
87 |
|
* The rcont structure is used to manage i/o with a particular |
99 |
|
}; /* rtrace process buffer */ |
100 |
|
|
101 |
|
/* rtrace command and defaults */ |
102 |
< |
char *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3", |
102 |
> |
char *rtargv[256+2*MAXMODLIST] = { "rtrace", |
103 |
> |
"-dj", ".5", "-dr", "3", |
104 |
|
"-ab", "1", "-ad", "128", }; |
105 |
|
int rtargc = 9; |
106 |
|
/* overriding rtrace options */ |
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 |
|
|
138 |
|
const char *modname[MAXMODLIST]; /* ordered modifier name list */ |
139 |
|
int nmods = 0; /* number of modifiers */ |
140 |
|
|
141 |
< |
MODCONT *addmodifier(char *modn, char *outf, char *binv); |
113 |
< |
void addmodfile(char *fname, char *outf, char *binv); |
141 |
> |
#define queue_length() (lastray - lastdone) |
142 |
|
|
143 |
+ |
MODCONT *growmodifier(MODCONT *mp, int nb); |
144 |
+ |
MODCONT *addmodifier(char *modn, char *outf, char *binv, int bincnt); |
145 |
+ |
void addmodfile(char *fname, char *outf, char *binv, int bincnt); |
146 |
+ |
|
147 |
|
void init(int np); |
148 |
|
int done_rprocs(struct rtproc *rtp); |
149 |
|
void recover_output(FILE *fin); |
157 |
|
void add_contrib(const char *modn); |
158 |
|
void done_contrib(void); |
159 |
|
|
160 |
+ |
/* return number of open rtrace processes */ |
161 |
+ |
static int |
162 |
+ |
nrtprocs(void) |
163 |
+ |
{ |
164 |
+ |
int nrtp = 0; |
165 |
+ |
struct rtproc *rtp; |
166 |
+ |
|
167 |
+ |
for (rtp = &rt0; rtp != NULL; rtp = rtp->next) |
168 |
+ |
nrtp += rtp->pd.running; |
169 |
+ |
return(nrtp); |
170 |
+ |
} |
171 |
+ |
|
172 |
|
/* set input/output format */ |
173 |
|
static void |
174 |
|
setformat(const char *fmt) |
212 |
|
int recover = 0; |
213 |
|
char *curout = NULL; |
214 |
|
char *binval = NULL; |
215 |
+ |
int bincnt = 0; |
216 |
|
char fmt[8]; |
217 |
|
int i, j; |
218 |
+ |
/* need at least one argument */ |
219 |
+ |
if (argc < 2) { |
220 |
+ |
fprintf(stderr, |
221 |
+ |
"Usage: %s [-n nprocs][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n", |
222 |
+ |
argv[0]); |
223 |
+ |
exit(1); |
224 |
+ |
} |
225 |
|
/* global program name */ |
226 |
|
gargv = argv; |
227 |
|
/* initialize calcomp routines */ |
245 |
|
recover++; |
246 |
|
continue; |
247 |
|
case 'n': /* number of processes */ |
248 |
< |
if (argv[i][2] || i >= argc-1) break; |
248 |
> |
if (argv[i][2] || i >= argc-2) break; |
249 |
|
nprocs = atoi(argv[++i]); |
250 |
|
if (nprocs <= 0) |
251 |
|
error(USER, "illegal number of processes"); |
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-1) break; |
273 |
> |
if (i >= argc-2) break; |
274 |
|
fpath = getpath(argv[++i], |
275 |
|
getrlibpath(), R_OK); |
276 |
|
if (fpath == NULL) { |
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 */ |
292 |
< |
if (argv[i][2] || i >= argc-1) break; |
292 |
> |
if (argv[i][2] || i >= argc-2) break; |
293 |
|
scompile(argv[++i], NULL, 0); |
294 |
|
continue; |
295 |
|
case 'o': /* output file spec. */ |
296 |
< |
if (argv[i][2] || i >= argc-1) break; |
296 |
> |
if (argv[i][2] || i >= argc-2) break; |
297 |
|
curout = argv[++i]; |
298 |
|
continue; |
299 |
|
case 'x': /* horiz. output resolution */ |
300 |
< |
if (argv[i][2] || i >= argc-1) break; |
300 |
> |
if (argv[i][2] || i >= argc-2) break; |
301 |
|
xres = atoi(argv[++i]); |
302 |
|
continue; |
303 |
|
case 'y': /* vert. output resolution */ |
304 |
< |
if (argv[i][2] || i >= argc-1) break; |
304 |
> |
if (argv[i][2] || i >= argc-2) break; |
305 |
|
yres = atoi(argv[++i]); |
306 |
|
continue; |
307 |
< |
case 'b': /* bin expression */ |
308 |
< |
if (argv[i][2] || i >= argc-1) break; |
307 |
> |
case 'b': /* bin expression/count */ |
308 |
> |
if (i >= argc-2) break; |
309 |
> |
if (argv[i][2] == 'n') { |
310 |
> |
bincnt = atoi(argv[++i]); |
311 |
> |
continue; |
312 |
> |
} |
313 |
> |
if (argv[i][2]) break; |
314 |
|
binval = argv[++i]; |
315 |
|
continue; |
316 |
|
case 'm': /* modifier name */ |
317 |
< |
if (argv[i][2] || i >= argc-1) break; |
317 |
> |
if (argv[i][2] || i >= argc-2) break; |
318 |
|
rtargv[rtargc++] = "-ti"; |
319 |
|
rtargv[rtargc++] = argv[++i]; |
320 |
< |
addmodifier(argv[i], curout, binval); |
320 |
> |
addmodifier(argv[i], curout, binval, bincnt); |
321 |
|
continue; |
322 |
|
case 'M': /* modifier file */ |
323 |
< |
if (argv[i][2] || i >= argc-1) break; |
324 |
< |
addmodfile(argv[++i], curout, binval); |
323 |
> |
if (argv[i][2] || i >= argc-2) break; |
324 |
> |
rtargv[rtargc++] = "-tI"; |
325 |
> |
rtargv[rtargc++] = argv[++i]; |
326 |
> |
addmodfile(argv[i], curout, binval, bincnt); |
327 |
|
continue; |
328 |
+ |
case 'P': /* persist file */ |
329 |
+ |
error(USER, "persist file is automatic"); |
330 |
+ |
break; |
331 |
|
} |
332 |
|
rtargv[rtargc++] = argv[i]; /* assume rtrace option */ |
333 |
|
} |
376 |
|
error(USER, "missing octree argument"); |
377 |
|
rtargv[rtargc++] = octree = argv[i]; |
378 |
|
rtargv[rtargc] = NULL; |
379 |
< |
/* start rtrace & compute contributions */ |
379 |
> |
/* start rtrace */ |
380 |
|
init(nprocs); |
381 |
|
if (recover) /* perform recovery if requested */ |
382 |
|
recover_output(stdin); |
383 |
< |
trace_contribs(stdin); |
383 |
> |
trace_contribs(stdin); /* compute contributions */ |
384 |
|
quit(0); |
385 |
|
} |
386 |
|
|
387 |
+ |
#ifndef SIGALRM |
388 |
+ |
#define SIGALRM SIGTERM |
389 |
+ |
#endif |
390 |
|
/* kill persistent rtrace process */ |
391 |
|
static void |
392 |
|
killpersist(void) |
393 |
|
{ |
394 |
|
FILE *fp = fopen(persistfn, "r"); |
395 |
< |
int pid; |
395 |
> |
RT_PID pid; |
396 |
|
|
397 |
|
if (fp == NULL) |
398 |
|
return; |
496 |
|
waitflush = xres; |
497 |
|
} |
498 |
|
|
499 |
+ |
/* grow modifier to accommodate more bins */ |
500 |
+ |
MODCONT * |
501 |
+ |
growmodifier(MODCONT *mp, int nb) |
502 |
+ |
{ |
503 |
+ |
if (nb <= mp->nbins) |
504 |
+ |
return mp; |
505 |
+ |
mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + sizeof(DCOLOR)*(nb-1)); |
506 |
+ |
if (mp == NULL) |
507 |
+ |
error(SYSTEM, "out of memory in growmodifier"); |
508 |
+ |
memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(nb-mp->nbins)); |
509 |
+ |
mp->nbins = nb; |
510 |
+ |
return mp; |
511 |
+ |
} |
512 |
+ |
|
513 |
|
/* add modifier to our list to track */ |
514 |
|
MODCONT * |
515 |
< |
addmodifier(char *modn, char *outf, char *binv) |
515 |
> |
addmodifier(char *modn, char *outf, char *binv, int bincnt) |
516 |
|
{ |
517 |
|
LUENT *lep = lu_find(&modconttab, modn); |
518 |
|
MODCONT *mp; |
519 |
+ |
int i; |
520 |
|
|
521 |
|
if (lep->data != NULL) { |
522 |
|
sprintf(errmsg, "duplicate modifier '%s'", modn); |
529 |
|
mp = (MODCONT *)malloc(sizeof(MODCONT)); |
530 |
|
if (mp == NULL) |
531 |
|
error(SYSTEM, "out of memory in addmodifier"); |
448 |
– |
lep->data = (char *)mp; |
532 |
|
mp->outspec = outf; /* XXX assumes static string */ |
533 |
|
mp->modname = modn; /* XXX assumes static string */ |
534 |
|
if (binv != NULL) |
537 |
|
mp->binv = eparse("0"); |
538 |
|
mp->nbins = 1; |
539 |
|
setcolor(mp->cbin[0], 0., 0., 0.); |
540 |
+ |
if (mp->binv->type == NUM) /* assume one bin if constant */ |
541 |
+ |
bincnt = 1; |
542 |
+ |
else if (bincnt > 1) |
543 |
+ |
mp = growmodifier(mp, bincnt); |
544 |
+ |
lep->data = (char *)mp; |
545 |
+ |
/* allocate output streams */ |
546 |
+ |
for (i = outf==NULL || outf[0]=='!' ? 0 : bincnt; i--; ) |
547 |
+ |
getostream(mp->outspec, mp->modname, i, 1); |
548 |
|
return mp; |
549 |
|
} |
550 |
|
|
551 |
|
/* add modifiers from a file list */ |
552 |
|
void |
553 |
< |
addmodfile(char *fname, char *outf, char *binv) |
553 |
> |
addmodfile(char *fname, char *outf, char *binv, int bincnt) |
554 |
|
{ |
555 |
|
char *mname[MAXMODLIST]; |
556 |
|
int i; |
557 |
< |
/* load the file & store strings */ |
558 |
< |
wordfile(mname, fname); |
557 |
> |
/* find the file & store strings */ |
558 |
> |
if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) { |
559 |
> |
sprintf(errmsg, "cannot find modifier file '%s'", fname); |
560 |
> |
error(SYSTEM, errmsg); |
561 |
> |
} |
562 |
|
for (i = 0; mname[i]; i++) /* add each one */ |
563 |
< |
addmodifier(mname[i], outf, binv); |
563 |
> |
addmodifier(mname[i], outf, binv, bincnt); |
564 |
|
} |
565 |
|
|
566 |
|
/* put string to stderr */ |
660 |
|
break; |
661 |
|
} |
662 |
|
fputc('\n', fout); |
663 |
+ |
} |
664 |
+ |
|
665 |
+ |
/* write resolution string to given output stream */ |
666 |
+ |
void |
667 |
+ |
printresolu(FILE *fout) |
668 |
+ |
{ |
669 |
|
if (xres > 0) { |
670 |
|
if (yres > 0) /* resolution string */ |
671 |
|
fprtresolu(xres, yres, fout); |
673 |
|
} |
674 |
|
} |
675 |
|
|
676 |
< |
/* Get output file pointer (open and write header if new) */ |
677 |
< |
FILE * |
678 |
< |
getofile(const char *ospec, const char *mname, int bn) |
676 |
> |
/* Get output stream pointer (open and write header if new and noopen==0) */ |
677 |
> |
STREAMOUT * |
678 |
> |
getostream(const char *ospec, const char *mname, int bn, int noopen) |
679 |
|
{ |
680 |
< |
int ofl; |
681 |
< |
char oname[1024]; |
682 |
< |
LUENT *lep; |
680 |
> |
static STREAMOUT stdos; |
681 |
> |
int ofl; |
682 |
> |
char oname[1024]; |
683 |
> |
LUENT *lep; |
684 |
> |
STREAMOUT *sop; |
685 |
|
|
686 |
|
if (ospec == NULL) { /* use stdout? */ |
687 |
< |
if (!using_stdout) { |
687 |
> |
if (!noopen && !using_stdout) { |
688 |
> |
stdos.reclen = 0; |
689 |
|
if (outfmt != 'a') |
690 |
|
SET_FILE_BINARY(stdout); |
691 |
|
if (header) |
692 |
|
printheader(stdout, NULL); |
693 |
+ |
printresolu(stdout); |
694 |
+ |
using_stdout = 1; |
695 |
|
} |
696 |
< |
using_stdout = 1; |
697 |
< |
return stdout; |
696 |
> |
stdos.ofp = stdout; |
697 |
> |
stdos.reclen += noopen; |
698 |
> |
return &stdos; |
699 |
|
} |
700 |
|
ofl = ofname(oname, ospec, mname, bn); /* get output name */ |
701 |
|
if (ofl < 0) { |
705 |
|
lep = lu_find(&ofiletab, oname); /* look it up */ |
706 |
|
if (lep->key == NULL) /* new entry */ |
707 |
|
lep->key = strcpy((char *)malloc(strlen(oname)+1), oname); |
708 |
< |
if (lep->data == NULL) { /* open output file */ |
709 |
< |
FILE *fp; |
710 |
< |
int i; |
708 |
> |
sop = (STREAMOUT *)lep->data; |
709 |
> |
if (sop == NULL) { /* allocate stream */ |
710 |
> |
sop = (STREAMOUT *)malloc(sizeof(STREAMOUT)); |
711 |
> |
if (sop == NULL) |
712 |
> |
error(SYSTEM, "out of memory in getostream"); |
713 |
> |
sop->reclen = oname[0] == '!' ? CNT_PIPE : CNT_UNKNOWN; |
714 |
> |
sop->ofp = NULL; /* open iff noopen==0 */ |
715 |
> |
lep->data = (char *)sop; |
716 |
> |
} |
717 |
> |
if (!noopen && sop->ofp == NULL) { /* open output stream */ |
718 |
> |
long i; |
719 |
|
if (oname[0] == '!') /* output to command */ |
720 |
< |
fp = popen(oname+1, "w"); |
721 |
< |
else |
722 |
< |
fp = fopen(oname, "w"); |
723 |
< |
if (fp == NULL) { |
720 |
> |
sop->ofp = popen(oname+1, "w"); |
721 |
> |
else if (!force_open && access(oname, F_OK) == 0) |
722 |
> |
errno = EEXIST; /* file exists */ |
723 |
> |
else /* else open it */ |
724 |
> |
sop->ofp = fopen(oname, "w"); |
725 |
> |
if (sop->ofp == NULL) { |
726 |
|
sprintf(errmsg, "cannot open '%s' for writing", oname); |
727 |
|
error(SYSTEM, errmsg); |
728 |
|
} |
729 |
|
if (outfmt != 'a') |
730 |
< |
SET_FILE_BINARY(fp); |
730 |
> |
SET_FILE_BINARY(sop->ofp); |
731 |
|
if (header) { |
732 |
|
char info[512]; |
733 |
|
char *cp = info; |
734 |
< |
if (ofl & OF_MODIFIER) { |
734 |
> |
if (ofl & OF_MODIFIER || sop->reclen == 1) { |
735 |
|
sprintf(cp, "MODIFIER=%s\n", mname); |
736 |
|
while (*cp) ++cp; |
737 |
|
} |
740 |
|
while (*cp) ++cp; |
741 |
|
} |
742 |
|
*cp = '\0'; |
743 |
< |
printheader(fp, info); |
743 |
> |
printheader(sop->ofp, info); |
744 |
|
} |
745 |
+ |
printresolu(sop->ofp); |
746 |
|
/* play catch-up */ |
747 |
< |
for (i = 0; i < lastdone; i++) { |
747 |
> |
for (i = sop->reclen > 1 ? sop->reclen*lastdone : lastdone; |
748 |
> |
i--; ) { |
749 |
|
static const DCOLOR nocontrib = BLKCOLOR; |
750 |
< |
put_contrib(nocontrib, fp); |
750 |
> |
put_contrib(nocontrib, sop->ofp); |
751 |
|
if (outfmt == 'a') |
752 |
< |
putc('\n', fp); |
752 |
> |
putc('\n', sop->ofp); |
753 |
|
} |
754 |
|
if (xres > 0) |
755 |
< |
fflush(fp); |
638 |
< |
lep->data = (char *)fp; |
755 |
> |
fflush(sop->ofp); |
756 |
|
} |
757 |
< |
return (FILE *)lep->data; /* return open file pointer */ |
757 |
> |
if (sop->reclen != CNT_PIPE) /* add to length if noopen */ |
758 |
> |
sop->reclen += noopen; |
759 |
> |
return sop; /* return open stream */ |
760 |
|
} |
761 |
|
|
762 |
|
/* read input ray into buffer */ |
763 |
|
int |
764 |
|
getinp(char *buf, FILE *fp) |
765 |
|
{ |
766 |
+ |
double dv[3]; |
767 |
+ |
float fv[3]; |
768 |
|
char *cp; |
769 |
|
int i; |
770 |
|
|
773 |
|
cp = buf; /* make sure we get 6 floats */ |
774 |
|
for (i = 0; i < 6; i++) { |
775 |
|
if (fgetword(cp, buf+127-cp, fp) == NULL) |
776 |
< |
return 0; |
776 |
> |
return -1; |
777 |
> |
if (i >= 3) |
778 |
> |
dv[i-3] = atof(cp); |
779 |
|
if ((cp = fskip(cp)) == NULL || *cp) |
780 |
< |
return 0; |
780 |
> |
return -1; |
781 |
|
*cp++ = ' '; |
782 |
|
} |
783 |
|
getc(fp); /* get/put eol */ |
784 |
|
*cp-- = '\0'; *cp = '\n'; |
785 |
+ |
if (DOT(dv,dv) <= FTINY*FTINY) |
786 |
+ |
return 0; /* dummy ray */ |
787 |
|
return strlen(buf); |
788 |
|
case 'f': |
789 |
|
if (fread(buf, sizeof(float), 6, fp) < 6) |
790 |
< |
return 0; |
790 |
> |
return -1; |
791 |
> |
memcpy(fv, buf+3*sizeof(float), 3*sizeof(float)); |
792 |
> |
if (DOT(fv,fv) <= FTINY*FTINY) |
793 |
> |
return 0; /* dummy ray */ |
794 |
|
return sizeof(float)*6; |
795 |
|
case 'd': |
796 |
|
if (fread(buf, sizeof(double), 6, fp) < 6) |
797 |
< |
return 0; |
797 |
> |
return -1; |
798 |
> |
memcpy(dv, buf+3*sizeof(double), 3*sizeof(double)); |
799 |
> |
if (DOT(dv,dv) <= FTINY*FTINY) |
800 |
> |
return 0; /* dummy ray */ |
801 |
|
return sizeof(double)*6; |
802 |
|
} |
803 |
|
error(INTERNAL, "botched input format"); |
804 |
< |
return 0; /* pro forma return */ |
804 |
> |
return -1; /* pro forma return */ |
805 |
|
} |
806 |
|
|
807 |
|
static float rparams[9]; /* traced ray parameters */ |
831 |
|
bn = (int)(evalue(mp->binv) + .5); |
832 |
|
if (bn <= 0) |
833 |
|
bn = 0; |
834 |
< |
else if (bn > mp->nbins) { /* new bin */ |
835 |
< |
mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + |
705 |
< |
bn*sizeof(DCOLOR)); |
706 |
< |
if (mp == NULL) |
707 |
< |
error(SYSTEM, "out of memory in add_contrib"); |
708 |
< |
memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins)); |
709 |
< |
mp->nbins = bn+1; |
710 |
< |
le->data = (char *)mp; |
711 |
< |
} |
834 |
> |
else if (bn >= mp->nbins) /* new bin */ |
835 |
> |
le->data = (char *)(mp = growmodifier(mp, bn+1)); |
836 |
|
addcolor(mp->cbin[bn], rparams); |
837 |
|
} |
838 |
|
|
840 |
|
static int |
841 |
|
puteol(const LUENT *e, void *p) |
842 |
|
{ |
843 |
< |
FILE *fp = (FILE *)e->data; |
843 |
> |
STREAMOUT *sop = (STREAMOUT *)e->data; |
844 |
|
|
845 |
|
if (outfmt == 'a') |
846 |
< |
putc('\n', fp); |
846 |
> |
putc('\n', sop->ofp); |
847 |
|
if (!waitflush) |
848 |
< |
fflush(fp); |
849 |
< |
if (ferror(fp)) { |
848 |
> |
fflush(sop->ofp); |
849 |
> |
if (ferror(sop->ofp)) { |
850 |
|
sprintf(errmsg, "write error on file '%s'", e->key); |
851 |
|
error(SYSTEM, errmsg); |
852 |
|
} |
886 |
|
void |
887 |
|
done_contrib(void) |
888 |
|
{ |
889 |
< |
int i, j; |
890 |
< |
MODCONT *mp; |
891 |
< |
FILE *fp; |
889 |
> |
int i, j; |
890 |
> |
MODCONT *mp; |
891 |
> |
STREAMOUT *sop; |
892 |
|
/* output modifiers in order */ |
893 |
|
for (i = 0; i < nmods; i++) { |
894 |
|
mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; |
895 |
< |
fp = getofile(mp->outspec, mp->modname, 0); |
896 |
< |
put_contrib(mp->cbin[0], fp); |
895 |
> |
sop = getostream(mp->outspec, mp->modname, 0,0); |
896 |
> |
put_contrib(mp->cbin[0], sop->ofp); |
897 |
|
if (mp->nbins > 3 && /* minor optimization */ |
898 |
< |
fp == getofile(mp->outspec, mp->modname, 1)) |
898 |
> |
sop == getostream(mp->outspec, mp->modname, 1,0)) |
899 |
|
for (j = 1; j < mp->nbins; j++) |
900 |
< |
put_contrib(mp->cbin[j], fp); |
900 |
> |
put_contrib(mp->cbin[j], sop->ofp); |
901 |
|
else |
902 |
|
for (j = 1; j < mp->nbins; j++) |
903 |
|
put_contrib(mp->cbin[j], |
904 |
< |
getofile(mp->outspec, mp->modname, j)); |
904 |
> |
getostream(mp->outspec,mp->modname,j,0)->ofp); |
905 |
|
/* clear for next ray tree */ |
906 |
|
memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); |
907 |
|
} |
975 |
|
} |
976 |
|
done_contrib(); /* sum up contributions & output */ |
977 |
|
lastdone = rtp->raynum; |
978 |
< |
free(rtp->buf); /* free up buffer space */ |
978 |
> |
if (rtp->buf != NULL) /* free up buffer space */ |
979 |
> |
free(rtp->buf); |
980 |
|
rt_unproc = rtp->next; |
981 |
|
free(rtp); /* done with this ray tree */ |
982 |
|
} |
1024 |
|
if (rt->bsiz + BUFSIZ <= treebufsiz) |
1025 |
|
rt->bsiz = treebufsiz; |
1026 |
|
else |
1027 |
< |
rt->bsiz = treebufsiz += BUFSIZ; |
1027 |
> |
treebufsiz = rt->bsiz += BUFSIZ; |
1028 |
|
rt->buf = (char *)realloc(rt->buf, rt->bsiz); |
1029 |
|
} |
1030 |
|
if (rt->buf == NULL) |
1064 |
|
int iblen; |
1065 |
|
struct rtproc *rtp; |
1066 |
|
/* loop over input */ |
1067 |
< |
while ((iblen = getinp(inpbuf, fin)) > 0) { |
1068 |
< |
if (lastray+1 < lastray) { /* counter rollover? */ |
1067 |
> |
while ((iblen = getinp(inpbuf, fin)) >= 0) { |
1068 |
> |
if (!iblen || /* need reset? */ |
1069 |
> |
queue_length() > 10*nrtprocs() || |
1070 |
> |
lastray+1 < lastray) { |
1071 |
|
while (wait_rproc() != NULL) |
1072 |
|
process_queue(); |
1073 |
< |
lastdone = lastray = 0; |
1073 |
> |
if (lastray+1 < lastray) |
1074 |
> |
lastdone = lastray = 0; |
1075 |
|
} |
1076 |
|
rtp = get_rproc(); /* get avail. rtrace process */ |
1077 |
< |
rtp->raynum = ++lastray; /* assign ray to it */ |
1078 |
< |
writebuf(rtp->pd.w, inpbuf, iblen); |
1079 |
< |
if (raysleft && !--raysleft) |
1080 |
< |
break; |
1077 |
> |
rtp->raynum = ++lastray; /* assign ray */ |
1078 |
> |
if (iblen) { /* trace ray if valid */ |
1079 |
> |
writebuf(rtp->pd.w, inpbuf, iblen); |
1080 |
> |
} else { /* else bypass dummy ray */ |
1081 |
> |
queue_raytree(rtp); /* empty tree */ |
1082 |
> |
if ((yres <= 0) | (waitflush > 1)) |
1083 |
> |
waitflush = 1; /* flush after this */ |
1084 |
> |
} |
1085 |
|
process_queue(); /* catch up with results */ |
1086 |
+ |
if (raysleft && !--raysleft) |
1087 |
+ |
break; /* preemptive EOI */ |
1088 |
|
} |
1089 |
|
while (wait_rproc() != NULL) /* process outstanding rays */ |
1090 |
|
process_queue(); |
1097 |
|
static int |
1098 |
|
myseeko(const LUENT *e, void *p) |
1099 |
|
{ |
1100 |
< |
if (fseeko((FILE *)e->data, *(off_t *)p, SEEK_CUR) < 0) { |
1100 |
> |
STREAMOUT *sop = (STREAMOUT *)e->data; |
1101 |
> |
off_t nbytes = *(off_t *)p; |
1102 |
> |
|
1103 |
> |
if (sop->reclen > 1) |
1104 |
> |
nbytes = nbytes * sop->reclen; |
1105 |
> |
if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) { |
1106 |
|
sprintf(errmsg, "seek error on file '%s'", e->key); |
1107 |
|
error(SYSTEM, errmsg); |
1108 |
|
} |
1112 |
|
void |
1113 |
|
recover_output(FILE *fin) |
1114 |
|
{ |
1115 |
< |
off_t lastout = -1; |
1116 |
< |
int outvsiz; |
1117 |
< |
char *outvfmt; |
1118 |
< |
int i, j; |
1119 |
< |
MODCONT *mp; |
1120 |
< |
int ofl; |
1121 |
< |
char oname[1024]; |
1122 |
< |
LUENT *ment, *oent; |
1123 |
< |
FILE *fp; |
1124 |
< |
off_t nvals; |
1115 |
> |
off_t lastout = -1; |
1116 |
> |
int outvsiz, recsiz; |
1117 |
> |
char *outvfmt; |
1118 |
> |
int i, j; |
1119 |
> |
MODCONT *mp; |
1120 |
> |
int ofl; |
1121 |
> |
char oname[1024]; |
1122 |
> |
LUENT *ment, *oent; |
1123 |
> |
STREAMOUT sout; |
1124 |
> |
off_t nvals; |
1125 |
> |
int xr, yr; |
1126 |
|
|
1127 |
|
switch (outfmt) { |
1128 |
|
case 'a': |
1150 |
|
mp = (MODCONT *)ment->data; |
1151 |
|
if (mp->outspec == NULL) |
1152 |
|
error(USER, "cannot recover from stdout"); |
1153 |
+ |
if (mp->outspec[0] == '!') |
1154 |
+ |
error(USER, "cannot recover from command"); |
1155 |
|
for (j = 0; ; j++) { /* check each bin's file */ |
1156 |
|
ofl = ofname(oname, mp->outspec, mp->modname, j); |
1157 |
|
if (ofl < 0) |
1158 |
|
error(USER, "bad output file specification"); |
1159 |
|
oent = lu_find(&ofiletab, oname); |
1160 |
< |
if (oent->data != NULL) { /* saw this one already */ |
1160 |
> |
if (oent->data != NULL) { |
1161 |
> |
sout = *(STREAMOUT *)oent->data; |
1162 |
> |
} else { |
1163 |
> |
sout.reclen = CNT_UNKNOWN; |
1164 |
> |
sout.ofp = NULL; |
1165 |
> |
} |
1166 |
> |
if (sout.ofp != NULL) { /* already open? */ |
1167 |
|
if (ofl & OF_BIN) |
1168 |
|
continue; |
1169 |
|
break; |
1170 |
|
} |
1023 |
– |
if (oname[0] == '!') |
1024 |
– |
error(USER, "cannot recover from command"); |
1171 |
|
/* open output */ |
1172 |
< |
fp = fopen(oname, "rb+"); |
1173 |
< |
if (fp == NULL) |
1174 |
< |
break; /* must be end of modifier */ |
1175 |
< |
nvals = lseek(fileno(fp), 0, SEEK_END); |
1172 |
> |
sout.ofp = fopen(oname, "rb+"); |
1173 |
> |
if (sout.ofp == NULL) { |
1174 |
> |
if (j) |
1175 |
> |
break; /* assume end of modifier */ |
1176 |
> |
sprintf(errmsg, "missing recover file '%s'", |
1177 |
> |
oname); |
1178 |
> |
error(USER, errmsg); |
1179 |
> |
} |
1180 |
> |
nvals = lseek(fileno(sout.ofp), 0, SEEK_END); |
1181 |
|
if (nvals <= 0) { |
1182 |
|
lastout = 0; /* empty output, quit here */ |
1183 |
< |
fclose(fp); |
1183 |
> |
fclose(sout.ofp); |
1184 |
|
break; |
1185 |
|
} |
1186 |
< |
lseek(fileno(fp), 0, SEEK_SET); |
1187 |
< |
if (header) { |
1037 |
< |
int xr, yr; |
1038 |
< |
if (checkheader(fp, outvfmt, NULL) != 1) { |
1186 |
> |
if (sout.reclen == CNT_UNKNOWN) { |
1187 |
> |
if (!(ofl & OF_BIN)) { |
1188 |
|
sprintf(errmsg, |
1189 |
< |
"format mismatch for '%s'", |
1189 |
> |
"need -bn to recover file '%s'", |
1190 |
|
oname); |
1191 |
|
error(USER, errmsg); |
1192 |
|
} |
1193 |
< |
if (xres > 0 && yres > 0 && |
1194 |
< |
(!fscnresolu(&xr, &yr, fp) || |
1195 |
< |
xr != xres || |
1196 |
< |
yr != yres)) { |
1197 |
< |
sprintf(errmsg, |
1198 |
< |
"resolution mismatch for '%s'", |
1199 |
< |
oname); |
1200 |
< |
error(USER, errmsg); |
1201 |
< |
} |
1193 |
> |
recsiz = outvsiz; |
1194 |
> |
} else |
1195 |
> |
recsiz = outvsiz * sout.reclen; |
1196 |
> |
|
1197 |
> |
lseek(fileno(sout.ofp), 0, SEEK_SET); |
1198 |
> |
if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) { |
1199 |
> |
sprintf(errmsg, "format mismatch for '%s'", |
1200 |
> |
oname); |
1201 |
> |
error(USER, errmsg); |
1202 |
|
} |
1203 |
< |
nvals = (nvals - (off_t)ftell(fp)) / outvsiz; |
1204 |
< |
if (lastout < 0 || nvals < lastout) |
1203 |
> |
if ((xres > 0) & (yres > 0) && |
1204 |
> |
(!fscnresolu(&xr, &yr, sout.ofp) || |
1205 |
> |
xr != xres || |
1206 |
> |
yr != yres)) { |
1207 |
> |
sprintf(errmsg, "resolution mismatch for '%s'", |
1208 |
> |
oname); |
1209 |
> |
error(USER, errmsg); |
1210 |
> |
} |
1211 |
> |
nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz; |
1212 |
> |
if ((lastout < 0) | (nvals < lastout)) |
1213 |
|
lastout = nvals; |
1214 |
< |
if (oent->key == NULL) /* new entry */ |
1214 |
> |
if (oent->key == NULL) /* new entry */ |
1215 |
|
oent->key = strcpy((char *) |
1216 |
|
malloc(strlen(oname)+1), oname); |
1217 |
< |
oent->data = (char *)fp; |
1217 |
> |
if (oent->data == NULL) |
1218 |
> |
oent->data = (char *)malloc(sizeof(STREAMOUT)); |
1219 |
> |
*(STREAMOUT *)oent->data = sout; |
1220 |
|
if (!(ofl & OF_BIN)) |
1221 |
|
break; /* no bin separation */ |
1222 |
|
} |
1225 |
|
lu_done(&ofiletab); /* reclose all outputs */ |
1226 |
|
return; |
1227 |
|
} |
1228 |
< |
if (j > mp->nbins) { /* preallocate modifier bins */ |
1229 |
< |
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 |
< |
} |
1228 |
> |
if (j > mp->nbins) /* reallocate modifier bins */ |
1229 |
> |
ment->data = (char *)(mp = growmodifier(mp, j)); |
1230 |
|
} |
1231 |
|
if (lastout < 0) { |
1232 |
|
error(WARNING, "no output files to recover"); |
1233 |
|
return; |
1234 |
|
} |
1235 |
+ |
if (raysleft && lastout >= raysleft) { |
1236 |
+ |
error(WARNING, "output appears to be complete"); |
1237 |
+ |
/* XXX should read & discard input? */ |
1238 |
+ |
quit(0); |
1239 |
+ |
} |
1240 |
|
/* seek on all files */ |
1241 |
|
nvals = lastout * outvsiz; |
1242 |
|
lu_doall(&ofiletab, myseeko, &nvals); |
1243 |
< |
/* discard input */ |
1243 |
> |
/* skip repeated input */ |
1244 |
|
for (nvals = 0; nvals < lastout; nvals++) |
1245 |
< |
if (getinp(oname, fin) <= 0) |
1245 |
> |
if (getinp(oname, fin) < 0) |
1246 |
|
error(USER, "unexpected EOF on input"); |
1247 |
|
lastray = lastdone = (unsigned long)lastout; |
1248 |
|
if (raysleft) |