| 7 |
|
|
| 8 |
|
#include "standard.h" |
| 9 |
|
#include <ctype.h> |
| 10 |
+ |
#include <signal.h> |
| 11 |
|
#include "platform.h" |
| 12 |
|
#include "rtprocess.h" |
| 13 |
|
#include "selcall.h" |
| 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 |
> |
if (sop->reclen == CNT_PIPE) |
| 67 |
> |
pclose(sop->ofp); |
| 68 |
> |
else |
| 69 |
> |
fclose(sop->ofp); |
| 70 |
> |
free(p); |
| 71 |
> |
} |
| 72 |
|
|
| 73 |
< |
FILE *getofile(const char *ospec, const char *mname, int bn); |
| 73 |
> |
LUTAB ofiletab = LU_SINIT(free,closestream); /* output file table */ |
| 74 |
|
|
| 75 |
+ |
#define OF_MODIFIER 01 |
| 76 |
+ |
#define OF_BIN 02 |
| 77 |
+ |
|
| 78 |
+ |
STREAMOUT *getostream(const char *ospec, const char *mname, int bn, int bincnt); |
| 79 |
+ |
int ofname(char *oname, const char *ospec, const char *mname, int bn); |
| 80 |
+ |
void printheader(FILE *fout, const char *info); |
| 81 |
+ |
void printresolu(FILE *fout); |
| 82 |
+ |
|
| 83 |
|
/* |
| 84 |
|
* The rcont structure is used to manage i/o with a particular |
| 85 |
|
* rtrace child process. Input is passed unchanged from stdin, |
| 96 |
|
}; /* rtrace process buffer */ |
| 97 |
|
|
| 98 |
|
/* rtrace command and defaults */ |
| 99 |
< |
char *rtargv[256] = { "rtrace", "-dj", ".5", "-dr", "3", |
| 99 |
> |
char *rtargv[256+2*MAXMODLIST] = { "rtrace", |
| 100 |
> |
"-dj", ".5", "-dr", "3", |
| 101 |
|
"-ab", "1", "-ad", "128", }; |
| 102 |
|
int rtargc = 9; |
| 103 |
|
/* overriding rtrace options */ |
| 123 |
|
int xres = 0; /* horiz. output resolution */ |
| 124 |
|
int yres = 0; /* vert. output resolution */ |
| 125 |
|
|
| 126 |
< |
long raysleft; /* number of rays left to trace */ |
| 126 |
> |
unsigned long raysleft; /* number of rays left to trace */ |
| 127 |
|
long waitflush; /* how long until next flush */ |
| 128 |
|
|
| 129 |
|
unsigned long lastray = 0; /* last ray number sent */ |
| 134 |
|
const char *modname[MAXMODLIST]; /* ordered modifier name list */ |
| 135 |
|
int nmods = 0; /* number of modifiers */ |
| 136 |
|
|
| 137 |
< |
MODCONT *addmodifier(char *modn, char *outf, char *binv); |
| 137 |
> |
#define queue_length() (lastray - lastdone) |
| 138 |
|
|
| 139 |
+ |
MODCONT *growmodifier(MODCONT *mp, int nb); |
| 140 |
+ |
MODCONT *addmodifier(char *modn, char *outf, char *binv, int bincnt); |
| 141 |
+ |
void addmodfile(char *fname, char *outf, char *binv, int bincnt); |
| 142 |
+ |
|
| 143 |
|
void init(int np); |
| 144 |
|
int done_rprocs(struct rtproc *rtp); |
| 145 |
< |
void trace_contribs(FILE *fp); |
| 145 |
> |
void recover_output(FILE *fin); |
| 146 |
> |
void trace_contribs(FILE *fin); |
| 147 |
|
struct rtproc *wait_rproc(void); |
| 148 |
|
struct rtproc *get_rproc(void); |
| 149 |
|
void queue_raytree(struct rtproc *rtp); |
| 150 |
|
void process_queue(void); |
| 151 |
|
|
| 152 |
< |
void putcontrib(const DCOLOR cnt, FILE *fout); |
| 152 |
> |
void put_contrib(const DCOLOR cnt, FILE *fout); |
| 153 |
|
void add_contrib(const char *modn); |
| 154 |
|
void done_contrib(void); |
| 155 |
|
|
| 156 |
+ |
/* return number of open rtrace processes */ |
| 157 |
+ |
static int |
| 158 |
+ |
nrtprocs(void) |
| 159 |
+ |
{ |
| 160 |
+ |
int nrtp = 0; |
| 161 |
+ |
struct rtproc *rtp; |
| 162 |
+ |
|
| 163 |
+ |
for (rtp = &rt0; rtp != NULL; rtp = rtp->next) |
| 164 |
+ |
nrtp += rtp->pd.running; |
| 165 |
+ |
return(nrtp); |
| 166 |
+ |
} |
| 167 |
+ |
|
| 168 |
|
/* set input/output format */ |
| 169 |
|
static void |
| 170 |
|
setformat(const char *fmt) |
| 171 |
|
{ |
| 172 |
|
switch (fmt[0]) { |
| 125 |
– |
case 'a': |
| 173 |
|
case 'f': |
| 174 |
|
case 'd': |
| 175 |
+ |
SET_FILE_BINARY(stdin); |
| 176 |
+ |
/* fall through */ |
| 177 |
+ |
case 'a': |
| 178 |
|
inpfmt = fmt[0]; |
| 179 |
|
break; |
| 180 |
|
default: |
| 205 |
|
main(int argc, char *argv[]) |
| 206 |
|
{ |
| 207 |
|
int nprocs = 1; |
| 208 |
+ |
int recover = 0; |
| 209 |
|
char *curout = NULL; |
| 210 |
|
char *binval = NULL; |
| 211 |
+ |
int bincnt = 0; |
| 212 |
|
char fmt[8]; |
| 213 |
|
int i, j; |
| 214 |
+ |
/* need at least one argument */ |
| 215 |
+ |
if (argc < 2) { |
| 216 |
+ |
fprintf(stderr, |
| 217 |
+ |
"Usage: %s [-n nprocs][-r][-e expr][-f source][-o ospec][-b binv] {-m mod | -M file} [rtrace options] octree\n", |
| 218 |
+ |
argv[0]); |
| 219 |
+ |
exit(1); |
| 220 |
+ |
} |
| 221 |
|
/* global program name */ |
| 222 |
|
gargv = argv; |
| 223 |
< |
/* set up calcomp mode */ |
| 223 |
> |
/* initialize calcomp routines */ |
| 224 |
|
esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_RCONST|E_REDEFW; |
| 225 |
|
esupport &= ~(E_OUTCHAN); |
| 226 |
+ |
varset("PI", ':', PI); |
| 227 |
|
/* get our options */ |
| 228 |
|
for (i = 1; i < argc-1; i++) { |
| 229 |
|
/* expand arguments */ |
| 236 |
|
} |
| 237 |
|
if (argv[i][0] == '-') |
| 238 |
|
switch (argv[i][1]) { |
| 239 |
+ |
case 'r': /* recover output */ |
| 240 |
+ |
if (argv[i][2]) break; |
| 241 |
+ |
recover++; |
| 242 |
+ |
continue; |
| 243 |
|
case 'n': /* number of processes */ |
| 244 |
< |
if (argv[i][2] || i >= argc-1) break; |
| 244 |
> |
if (argv[i][2] || i >= argc-2) break; |
| 245 |
|
nprocs = atoi(argv[++i]); |
| 246 |
|
if (nprocs <= 0) |
| 247 |
|
error(USER, "illegal number of processes"); |
| 265 |
|
break; |
| 266 |
|
case 'f': /* file or i/o format */ |
| 267 |
|
if (!argv[i][2]) { |
| 268 |
< |
if (i >= argc-1) break; |
| 269 |
< |
fcompile(argv[++i]); |
| 268 |
> |
char *fpath; |
| 269 |
> |
if (i >= argc-2) break; |
| 270 |
> |
fpath = getpath(argv[++i], |
| 271 |
> |
getrlibpath(), R_OK); |
| 272 |
> |
if (fpath == NULL) { |
| 273 |
> |
sprintf(errmsg, |
| 274 |
> |
"cannot find file '%s'", |
| 275 |
> |
argv[i]); |
| 276 |
> |
error(USER, errmsg); |
| 277 |
> |
} |
| 278 |
> |
fcompile(fpath); |
| 279 |
|
continue; |
| 280 |
|
} |
| 281 |
|
setformat(argv[i]+2); |
| 282 |
|
continue; |
| 283 |
|
case 'e': /* expression */ |
| 284 |
< |
if (argv[i][2] || i >= argc-1) break; |
| 284 |
> |
if (argv[i][2] || i >= argc-2) break; |
| 285 |
|
scompile(argv[++i], NULL, 0); |
| 286 |
|
continue; |
| 287 |
|
case 'o': /* output file spec. */ |
| 288 |
< |
if (argv[i][2] || i >= argc-1) break; |
| 288 |
> |
if (argv[i][2] || i >= argc-2) break; |
| 289 |
|
curout = argv[++i]; |
| 290 |
|
continue; |
| 291 |
|
case 'x': /* horiz. output resolution */ |
| 292 |
< |
if (argv[i][2] || i >= argc-1) break; |
| 292 |
> |
if (argv[i][2] || i >= argc-2) break; |
| 293 |
|
xres = atoi(argv[++i]); |
| 294 |
|
continue; |
| 295 |
|
case 'y': /* vert. output resolution */ |
| 296 |
< |
if (argv[i][2] || i >= argc-1) break; |
| 296 |
> |
if (argv[i][2] || i >= argc-2) break; |
| 297 |
|
yres = atoi(argv[++i]); |
| 298 |
|
continue; |
| 299 |
< |
case 'b': /* bin expression */ |
| 300 |
< |
if (argv[i][2] || i >= argc-1) break; |
| 299 |
> |
case 'b': /* bin expression/count */ |
| 300 |
> |
if (i >= argc-2) break; |
| 301 |
> |
if (argv[i][2] == 'n') { |
| 302 |
> |
bincnt = atoi(argv[++i]); |
| 303 |
> |
continue; |
| 304 |
> |
} |
| 305 |
> |
if (argv[i][2]) break; |
| 306 |
|
binval = argv[++i]; |
| 307 |
|
continue; |
| 308 |
|
case 'm': /* modifier name */ |
| 309 |
< |
if (argv[i][2] || i >= argc-1) break; |
| 309 |
> |
if (argv[i][2] || i >= argc-2) break; |
| 310 |
|
rtargv[rtargc++] = "-ti"; |
| 311 |
|
rtargv[rtargc++] = argv[++i]; |
| 312 |
< |
addmodifier(argv[i], curout, binval); |
| 312 |
> |
addmodifier(argv[i], curout, binval, bincnt); |
| 313 |
|
continue; |
| 314 |
+ |
case 'M': /* modifier file */ |
| 315 |
+ |
if (argv[i][2] || i >= argc-2) break; |
| 316 |
+ |
rtargv[rtargc++] = "-tI"; |
| 317 |
+ |
rtargv[rtargc++] = argv[++i]; |
| 318 |
+ |
addmodfile(argv[i], curout, binval, bincnt); |
| 319 |
+ |
continue; |
| 320 |
|
} |
| 321 |
|
rtargv[rtargc++] = argv[i]; /* assume rtrace option */ |
| 322 |
|
} |
| 365 |
|
error(USER, "missing octree argument"); |
| 366 |
|
rtargv[rtargc++] = octree = argv[i]; |
| 367 |
|
rtargv[rtargc] = NULL; |
| 368 |
< |
/* start rtrace & compute contributions */ |
| 368 |
> |
/* start rtrace */ |
| 369 |
|
init(nprocs); |
| 370 |
< |
trace_contribs(stdin); |
| 370 |
> |
if (recover) /* perform recovery if requested */ |
| 371 |
> |
recover_output(stdin); |
| 372 |
> |
trace_contribs(stdin); /* compute contributions */ |
| 373 |
|
quit(0); |
| 374 |
|
} |
| 375 |
|
|
| 376 |
+ |
#ifndef SIGALRM |
| 377 |
+ |
#define SIGALRM SIGTERM |
| 378 |
+ |
#endif |
| 379 |
|
/* kill persistent rtrace process */ |
| 380 |
|
static void |
| 381 |
|
killpersist(void) |
| 382 |
|
{ |
| 383 |
|
FILE *fp = fopen(persistfn, "r"); |
| 384 |
< |
int pid; |
| 384 |
> |
RT_PID pid; |
| 385 |
|
|
| 386 |
|
if (fp == NULL) |
| 387 |
|
return; |
| 477 |
|
rtp->next = NULL; /* terminate list */ |
| 478 |
|
if (yres > 0) { |
| 479 |
|
if (xres > 0) |
| 480 |
< |
raysleft = xres*yres; |
| 480 |
> |
raysleft = (unsigned long)xres*yres; |
| 481 |
|
else |
| 482 |
|
raysleft = yres; |
| 483 |
|
} else |
| 485 |
|
waitflush = xres; |
| 486 |
|
} |
| 487 |
|
|
| 488 |
+ |
/* grow modifier to accommodate more bins */ |
| 489 |
+ |
MODCONT * |
| 490 |
+ |
growmodifier(MODCONT *mp, int nb) |
| 491 |
+ |
{ |
| 492 |
+ |
if (nb <= mp->nbins) |
| 493 |
+ |
return mp; |
| 494 |
+ |
mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + sizeof(DCOLOR)*(nb-1)); |
| 495 |
+ |
if (mp == NULL) |
| 496 |
+ |
error(SYSTEM, "out of memory in growmodifier"); |
| 497 |
+ |
memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(nb-mp->nbins)); |
| 498 |
+ |
mp->nbins = nb; |
| 499 |
+ |
return mp; |
| 500 |
+ |
} |
| 501 |
+ |
|
| 502 |
|
/* add modifier to our list to track */ |
| 503 |
|
MODCONT * |
| 504 |
< |
addmodifier(char *modn, char *outf, char *binv) |
| 504 |
> |
addmodifier(char *modn, char *outf, char *binv, int bincnt) |
| 505 |
|
{ |
| 506 |
|
LUENT *lep = lu_find(&modconttab, modn); |
| 507 |
|
MODCONT *mp; |
| 526 |
|
mp->binv = eparse("0"); |
| 527 |
|
mp->nbins = 1; |
| 528 |
|
setcolor(mp->cbin[0], 0., 0., 0.); |
| 529 |
+ |
if (mp->binv->type == NUM) /* assume one bin if constant */ |
| 530 |
+ |
bincnt = 1; |
| 531 |
+ |
else if (bincnt > 1) |
| 532 |
+ |
mp = growmodifier(mp, bincnt); |
| 533 |
+ |
if (bincnt > 0) /* allocate output stream */ |
| 534 |
+ |
getostream(mp->outspec, mp->modname, 0, bincnt); |
| 535 |
|
return mp; |
| 536 |
|
} |
| 537 |
|
|
| 538 |
+ |
/* add modifiers from a file list */ |
| 539 |
+ |
void |
| 540 |
+ |
addmodfile(char *fname, char *outf, char *binv, int bincnt) |
| 541 |
+ |
{ |
| 542 |
+ |
char *mname[MAXMODLIST]; |
| 543 |
+ |
int i; |
| 544 |
+ |
/* find the file & store strings */ |
| 545 |
+ |
if (wordfile(mname, getpath(fname, getrlibpath(), R_OK)) < 0) { |
| 546 |
+ |
sprintf(errmsg, "cannot find modifier file '%s'", fname); |
| 547 |
+ |
error(SYSTEM, errmsg); |
| 548 |
+ |
} |
| 549 |
+ |
for (i = 0; mname[i]; i++) /* add each one */ |
| 550 |
+ |
addmodifier(mname[i], outf, binv, bincnt); |
| 551 |
+ |
} |
| 552 |
+ |
|
| 553 |
|
/* put string to stderr */ |
| 554 |
|
void |
| 555 |
|
eputs(char *s) |
| 565 |
|
midline = s[strlen(s)-1] != '\n'; |
| 566 |
|
} |
| 567 |
|
|
| 568 |
+ |
/* construct output file name and return flags whether modifier/bin present */ |
| 569 |
+ |
int |
| 570 |
+ |
ofname(char *oname, const char *ospec, const char *mname, int bn) |
| 571 |
+ |
{ |
| 572 |
+ |
const char *mnp = NULL; |
| 573 |
+ |
const char *bnp = NULL; |
| 574 |
+ |
const char *cp; |
| 575 |
+ |
|
| 576 |
+ |
if (ospec == NULL) |
| 577 |
+ |
return -1; |
| 578 |
+ |
for (cp = ospec; *cp; cp++) /* check format position(s) */ |
| 579 |
+ |
if (*cp == '%') { |
| 580 |
+ |
do |
| 581 |
+ |
++cp; |
| 582 |
+ |
while (isdigit(*cp)); |
| 583 |
+ |
switch (*cp) { |
| 584 |
+ |
case '%': |
| 585 |
+ |
break; |
| 586 |
+ |
case 's': |
| 587 |
+ |
if (mnp != NULL) |
| 588 |
+ |
return -1; |
| 589 |
+ |
mnp = cp; |
| 590 |
+ |
break; |
| 591 |
+ |
case 'd': |
| 592 |
+ |
if (bnp != NULL) |
| 593 |
+ |
return -1; |
| 594 |
+ |
bnp = cp; |
| 595 |
+ |
break; |
| 596 |
+ |
default: |
| 597 |
+ |
return -1; |
| 598 |
+ |
} |
| 599 |
+ |
} |
| 600 |
+ |
if (mnp != NULL) { /* create file name */ |
| 601 |
+ |
if (bnp != NULL) { |
| 602 |
+ |
if (bnp > mnp) |
| 603 |
+ |
sprintf(oname, ospec, mname, bn); |
| 604 |
+ |
else |
| 605 |
+ |
sprintf(oname, ospec, bn, mname); |
| 606 |
+ |
return OF_MODIFIER|OF_BIN; |
| 607 |
+ |
} |
| 608 |
+ |
sprintf(oname, ospec, mname); |
| 609 |
+ |
return OF_MODIFIER; |
| 610 |
+ |
} |
| 611 |
+ |
if (bnp != NULL) { |
| 612 |
+ |
sprintf(oname, ospec, bn); |
| 613 |
+ |
return OF_BIN; |
| 614 |
+ |
} |
| 615 |
+ |
strcpy(oname, ospec); |
| 616 |
+ |
return 0; |
| 617 |
+ |
} |
| 618 |
+ |
|
| 619 |
|
/* write header to the given output stream */ |
| 620 |
|
void |
| 621 |
< |
printheader(FILE *fout) |
| 621 |
> |
printheader(FILE *fout, const char *info) |
| 622 |
|
{ |
| 623 |
|
extern char VersionID[]; |
| 624 |
|
FILE *fin = fopen(octree, "r"); |
| 630 |
|
printargs(gargc-1, gargv, fout); /* add our command */ |
| 631 |
|
fprintf(fout, "SOFTWARE= %s\n", VersionID); |
| 632 |
|
fputnow(fout); |
| 633 |
+ |
if (info != NULL) /* add extra info if given */ |
| 634 |
+ |
fputs(info, fout); |
| 635 |
|
switch (outfmt) { /* add output format */ |
| 636 |
|
case 'a': |
| 637 |
|
fputformat("ascii", fout); |
| 647 |
|
break; |
| 648 |
|
} |
| 649 |
|
fputc('\n', fout); |
| 650 |
+ |
} |
| 651 |
+ |
|
| 652 |
+ |
/* write resolution string to given output stream */ |
| 653 |
+ |
void |
| 654 |
+ |
printresolu(FILE *fout) |
| 655 |
+ |
{ |
| 656 |
|
if (xres > 0) { |
| 657 |
|
if (yres > 0) /* resolution string */ |
| 658 |
|
fprtresolu(xres, yres, fout); |
| 660 |
|
} |
| 661 |
|
} |
| 662 |
|
|
| 663 |
< |
/* Get output file pointer (open and write header if new) */ |
| 664 |
< |
FILE * |
| 665 |
< |
getofile(const char *ospec, const char *mname, int bn) |
| 663 |
> |
/* Get output stream pointer (open and write header if new and bincnt==0) */ |
| 664 |
> |
STREAMOUT * |
| 665 |
> |
getostream(const char *ospec, const char *mname, int bn, int bincnt) |
| 666 |
|
{ |
| 667 |
< |
const char *mnp = NULL; |
| 668 |
< |
const char *bnp = NULL; |
| 669 |
< |
const char *cp; |
| 670 |
< |
char ofname[1024]; |
| 671 |
< |
LUENT *lep; |
| 667 |
> |
static STREAMOUT stdos; |
| 668 |
> |
int ofl; |
| 669 |
> |
char oname[1024]; |
| 670 |
> |
LUENT *lep; |
| 671 |
> |
STREAMOUT *sop; |
| 672 |
|
|
| 673 |
|
if (ospec == NULL) { /* use stdout? */ |
| 674 |
< |
if (!using_stdout && header) |
| 675 |
< |
printheader(stdout); |
| 676 |
< |
using_stdout = 1; |
| 677 |
< |
return stdout; |
| 678 |
< |
} |
| 679 |
< |
for (cp = ospec; *cp; cp++) /* check format position(s) */ |
| 680 |
< |
if (*cp == '%') { |
| 681 |
< |
do |
| 499 |
< |
++cp; |
| 500 |
< |
while (isdigit(*cp)); |
| 501 |
< |
switch (*cp) { |
| 502 |
< |
case '%': |
| 503 |
< |
break; |
| 504 |
< |
case 's': |
| 505 |
< |
if (mnp != NULL) |
| 506 |
< |
goto badspec; |
| 507 |
< |
mnp = cp; |
| 508 |
< |
break; |
| 509 |
< |
case 'd': |
| 510 |
< |
if (bnp != NULL) |
| 511 |
< |
goto badspec; |
| 512 |
< |
bnp = cp; |
| 513 |
< |
break; |
| 514 |
< |
default: |
| 515 |
< |
goto badspec; |
| 516 |
< |
} |
| 674 |
> |
if (!bincnt && !using_stdout) { |
| 675 |
> |
stdos.reclen = 0; |
| 676 |
> |
if (outfmt != 'a') |
| 677 |
> |
SET_FILE_BINARY(stdout); |
| 678 |
> |
if (header) |
| 679 |
> |
printheader(stdout, NULL); |
| 680 |
> |
printresolu(stdout); |
| 681 |
> |
using_stdout = 1; |
| 682 |
|
} |
| 683 |
< |
if (mnp != NULL) { /* create file name */ |
| 684 |
< |
if (bnp != NULL) { |
| 685 |
< |
if (bnp > mnp) |
| 686 |
< |
sprintf(ofname, ospec, mname, bn); |
| 687 |
< |
else |
| 688 |
< |
sprintf(ofname, ospec, bn, mname); |
| 689 |
< |
} else |
| 690 |
< |
sprintf(ofname, ospec, mname); |
| 691 |
< |
} else if (bnp != NULL) |
| 692 |
< |
sprintf(ofname, ospec, bn); |
| 528 |
< |
else |
| 529 |
< |
strcpy(ofname, ospec); |
| 530 |
< |
lep = lu_find(&ofiletab, ofname); /* look it up */ |
| 683 |
> |
stdos.ofp = stdout; |
| 684 |
> |
stdos.reclen += bincnt; |
| 685 |
> |
return &stdos; |
| 686 |
> |
} |
| 687 |
> |
ofl = ofname(oname, ospec, mname, bn); /* get output name */ |
| 688 |
> |
if (ofl < 0) { |
| 689 |
> |
sprintf(errmsg, "bad output format '%s'", ospec); |
| 690 |
> |
error(USER, errmsg); |
| 691 |
> |
} |
| 692 |
> |
lep = lu_find(&ofiletab, oname); /* look it up */ |
| 693 |
|
if (lep->key == NULL) /* new entry */ |
| 694 |
< |
lep->key = strcpy((char *)malloc(strlen(ofname)+1), ofname); |
| 695 |
< |
if (lep->data == NULL) { /* open output file */ |
| 696 |
< |
FILE *fp = fopen(ofname, "w"); |
| 694 |
> |
lep->key = strcpy((char *)malloc(strlen(oname)+1), oname); |
| 695 |
> |
sop = (STREAMOUT *)lep->data; |
| 696 |
> |
if (sop == NULL) { /* allocate stream */ |
| 697 |
> |
if (ofl & OF_BIN && bincnt > 1) /* don't overcount bins */ |
| 698 |
> |
bincnt = 1; |
| 699 |
> |
sop = (STREAMOUT *)malloc(sizeof(STREAMOUT)); |
| 700 |
> |
if (sop == NULL) |
| 701 |
> |
error(SYSTEM, "out of memory in getostream"); |
| 702 |
> |
sop->reclen = oname[0] == '!' ? CNT_PIPE : 0; |
| 703 |
> |
sop->ofp = NULL; /* open iff bincnt==0 */ |
| 704 |
> |
lep->data = (char *)sop; |
| 705 |
> |
} |
| 706 |
> |
if (!bincnt && sop->ofp == NULL) { /* open output stream */ |
| 707 |
|
int i; |
| 708 |
< |
if (fp == NULL) { |
| 709 |
< |
sprintf(errmsg, "cannot open '%s' for writing", ofname); |
| 708 |
> |
if (oname[0] == '!') /* output to command */ |
| 709 |
> |
sop->ofp = popen(oname+1, "w"); |
| 710 |
> |
else |
| 711 |
> |
sop->ofp = fopen(oname, "w"); |
| 712 |
> |
if (sop->ofp == NULL) { |
| 713 |
> |
sprintf(errmsg, "cannot open '%s' for writing", oname); |
| 714 |
|
error(SYSTEM, errmsg); |
| 715 |
|
} |
| 716 |
< |
if (header) |
| 717 |
< |
printheader(fp); |
| 716 |
> |
if (outfmt != 'a') |
| 717 |
> |
SET_FILE_BINARY(sop->ofp); |
| 718 |
> |
if (header) { |
| 719 |
> |
char info[512]; |
| 720 |
> |
char *cp = info; |
| 721 |
> |
if (ofl & OF_MODIFIER) { |
| 722 |
> |
sprintf(cp, "MODIFIER=%s\n", mname); |
| 723 |
> |
while (*cp) ++cp; |
| 724 |
> |
} |
| 725 |
> |
if (ofl & OF_BIN) { |
| 726 |
> |
sprintf(cp, "BIN=%d\n", bn); |
| 727 |
> |
while (*cp) ++cp; |
| 728 |
> |
} |
| 729 |
> |
*cp = '\0'; |
| 730 |
> |
printheader(sop->ofp, info); |
| 731 |
> |
} |
| 732 |
> |
printresolu(sop->ofp); |
| 733 |
|
/* play catch-up */ |
| 734 |
|
for (i = 0; i < lastdone; i++) { |
| 735 |
|
static const DCOLOR nocontrib = BLKCOLOR; |
| 736 |
< |
putcontrib(nocontrib, fp); |
| 736 |
> |
put_contrib(nocontrib, sop->ofp); |
| 737 |
|
if (outfmt == 'a') |
| 738 |
< |
putc('\n', fp); |
| 738 |
> |
putc('\n', sop->ofp); |
| 739 |
|
} |
| 740 |
|
if (xres > 0) |
| 741 |
< |
fflush(fp); |
| 551 |
< |
lep->data = (char *)fp; |
| 741 |
> |
fflush(sop->ofp); |
| 742 |
|
} |
| 743 |
< |
return (FILE *)lep->data; /* return open file pointer */ |
| 744 |
< |
badspec: |
| 745 |
< |
sprintf(errmsg, "bad output format '%s'", ospec); |
| 556 |
< |
error(USER, errmsg); |
| 557 |
< |
return NULL; /* pro forma return */ |
| 743 |
> |
if (sop->reclen != CNT_PIPE) /* add bincnt to count */ |
| 744 |
> |
sop->reclen += bincnt; |
| 745 |
> |
return sop; /* return open stream */ |
| 746 |
|
} |
| 747 |
|
|
| 748 |
|
/* read input ray into buffer */ |
| 805 |
|
bn = (int)(evalue(mp->binv) + .5); |
| 806 |
|
if (bn <= 0) |
| 807 |
|
bn = 0; |
| 808 |
< |
else if (bn > mp->nbins) { /* new bin */ |
| 809 |
< |
mp = (MODCONT *)realloc(mp, sizeof(MODCONT) + |
| 622 |
< |
bn*sizeof(DCOLOR)); |
| 623 |
< |
if (mp == NULL) |
| 624 |
< |
error(SYSTEM, "out of memory in add_contrib"); |
| 625 |
< |
memset(mp->cbin+mp->nbins, 0, sizeof(DCOLOR)*(bn+1-mp->nbins)); |
| 626 |
< |
mp->nbins = bn+1; |
| 627 |
< |
le->data = (char *)mp; |
| 628 |
< |
} |
| 808 |
> |
else if (bn >= mp->nbins) /* new bin */ |
| 809 |
> |
le->data = (char *)(mp = growmodifier(mp, bn+1)); |
| 810 |
|
addcolor(mp->cbin[bn], rparams); |
| 811 |
|
} |
| 812 |
|
|
| 814 |
|
static int |
| 815 |
|
puteol(const LUENT *e, void *p) |
| 816 |
|
{ |
| 817 |
< |
FILE *fp = (FILE *)e->data; |
| 817 |
> |
STREAMOUT *sop = (STREAMOUT *)e->data; |
| 818 |
|
|
| 819 |
|
if (outfmt == 'a') |
| 820 |
< |
putc('\n', fp); |
| 820 |
> |
putc('\n', sop->ofp); |
| 821 |
|
if (!waitflush) |
| 822 |
< |
fflush(fp); |
| 823 |
< |
if (ferror(fp)) { |
| 822 |
> |
fflush(sop->ofp); |
| 823 |
> |
if (ferror(sop->ofp)) { |
| 824 |
|
sprintf(errmsg, "write error on file '%s'", e->key); |
| 825 |
|
error(SYSTEM, errmsg); |
| 826 |
|
} |
| 829 |
|
|
| 830 |
|
/* put out ray contribution to file */ |
| 831 |
|
void |
| 832 |
< |
putcontrib(const DCOLOR cnt, FILE *fout) |
| 832 |
> |
put_contrib(const DCOLOR cnt, FILE *fout) |
| 833 |
|
{ |
| 834 |
|
float fv[3]; |
| 835 |
|
COLR cv; |
| 860 |
|
void |
| 861 |
|
done_contrib(void) |
| 862 |
|
{ |
| 863 |
< |
int i, j; |
| 864 |
< |
MODCONT *mp; |
| 863 |
> |
int i, j; |
| 864 |
> |
MODCONT *mp; |
| 865 |
> |
STREAMOUT *sop; |
| 866 |
|
/* output modifiers in order */ |
| 867 |
|
for (i = 0; i < nmods; i++) { |
| 868 |
|
mp = (MODCONT *)lu_find(&modconttab,modname[i])->data; |
| 869 |
< |
for (j = 0; j < mp->nbins; j++) |
| 870 |
< |
putcontrib(mp->cbin[j], |
| 871 |
< |
getofile(mp->outspec, mp->modname, j)); |
| 869 |
> |
sop = getostream(mp->outspec, mp->modname, 0,0); |
| 870 |
> |
put_contrib(mp->cbin[0], sop->ofp); |
| 871 |
> |
if (mp->nbins > 3 && /* minor optimization */ |
| 872 |
> |
sop == getostream(mp->outspec, mp->modname, 1,0)) |
| 873 |
> |
for (j = 1; j < mp->nbins; j++) |
| 874 |
> |
put_contrib(mp->cbin[j], sop->ofp); |
| 875 |
> |
else |
| 876 |
> |
for (j = 1; j < mp->nbins; j++) |
| 877 |
> |
put_contrib(mp->cbin[j], |
| 878 |
> |
getostream(mp->outspec,mp->modname,j,0)->ofp); |
| 879 |
|
/* clear for next ray tree */ |
| 880 |
|
memset(mp->cbin, 0, sizeof(DCOLOR)*mp->nbins); |
| 881 |
|
} |
| 997 |
|
if (rt->bsiz + BUFSIZ <= treebufsiz) |
| 998 |
|
rt->bsiz = treebufsiz; |
| 999 |
|
else |
| 1000 |
< |
rt->bsiz = treebufsiz += BUFSIZ; |
| 1000 |
> |
treebufsiz = rt->bsiz += BUFSIZ; |
| 1001 |
|
rt->buf = (char *)realloc(rt->buf, rt->bsiz); |
| 1002 |
|
} |
| 1003 |
|
if (rt->buf == NULL) |
| 1038 |
|
struct rtproc *rtp; |
| 1039 |
|
/* loop over input */ |
| 1040 |
|
while ((iblen = getinp(inpbuf, fin)) > 0) { |
| 1041 |
< |
if (lastray+1 < lastray) { /* counter rollover? */ |
| 1041 |
> |
if (lastray+1 < lastray || /* need reset? */ |
| 1042 |
> |
queue_length() > 5*nrtprocs()) { |
| 1043 |
|
while (wait_rproc() != NULL) |
| 1044 |
|
process_queue(); |
| 1045 |
|
lastdone = lastray = 0; |
| 1047 |
|
rtp = get_rproc(); /* get avail. rtrace process */ |
| 1048 |
|
rtp->raynum = ++lastray; /* assign ray to it */ |
| 1049 |
|
writebuf(rtp->pd.w, inpbuf, iblen); |
| 1050 |
< |
if (!--raysleft) |
| 1050 |
> |
if (raysleft && !--raysleft) |
| 1051 |
|
break; |
| 1052 |
|
process_queue(); /* catch up with results */ |
| 1053 |
|
} |
| 1054 |
|
while (wait_rproc() != NULL) /* process outstanding rays */ |
| 1055 |
|
process_queue(); |
| 1056 |
< |
if (raysleft > 0) |
| 1056 |
> |
if (raysleft) |
| 1057 |
|
error(USER, "unexpected EOF on input"); |
| 1058 |
+ |
lu_done(&ofiletab); /* close output files */ |
| 1059 |
+ |
} |
| 1060 |
+ |
|
| 1061 |
+ |
/* seek on the given output file */ |
| 1062 |
+ |
static int |
| 1063 |
+ |
myseeko(const LUENT *e, void *p) |
| 1064 |
+ |
{ |
| 1065 |
+ |
STREAMOUT *sop = (STREAMOUT *)e->data; |
| 1066 |
+ |
off_t nbytes = *(off_t *)p; |
| 1067 |
+ |
|
| 1068 |
+ |
if (sop->reclen > 1) |
| 1069 |
+ |
nbytes = nbytes * sop->reclen; |
| 1070 |
+ |
if (fseeko(sop->ofp, nbytes, SEEK_CUR) < 0) { |
| 1071 |
+ |
sprintf(errmsg, "seek error on file '%s'", e->key); |
| 1072 |
+ |
error(SYSTEM, errmsg); |
| 1073 |
+ |
} |
| 1074 |
+ |
} |
| 1075 |
+ |
|
| 1076 |
+ |
/* recover output if possible */ |
| 1077 |
+ |
void |
| 1078 |
+ |
recover_output(FILE *fin) |
| 1079 |
+ |
{ |
| 1080 |
+ |
off_t lastout = -1; |
| 1081 |
+ |
int outvsiz, recsiz; |
| 1082 |
+ |
char *outvfmt; |
| 1083 |
+ |
int i, j; |
| 1084 |
+ |
MODCONT *mp; |
| 1085 |
+ |
int ofl; |
| 1086 |
+ |
char oname[1024]; |
| 1087 |
+ |
LUENT *ment, *oent; |
| 1088 |
+ |
STREAMOUT sout; |
| 1089 |
+ |
off_t nvals; |
| 1090 |
+ |
int xr, yr; |
| 1091 |
+ |
|
| 1092 |
+ |
switch (outfmt) { |
| 1093 |
+ |
case 'a': |
| 1094 |
+ |
error(USER, "cannot recover ASCII output"); |
| 1095 |
+ |
return; |
| 1096 |
+ |
case 'f': |
| 1097 |
+ |
outvsiz = sizeof(float)*3; |
| 1098 |
+ |
outvfmt = "float"; |
| 1099 |
+ |
break; |
| 1100 |
+ |
case 'd': |
| 1101 |
+ |
outvsiz = sizeof(double)*3; |
| 1102 |
+ |
outvfmt = "double"; |
| 1103 |
+ |
break; |
| 1104 |
+ |
case 'c': |
| 1105 |
+ |
outvsiz = sizeof(COLR); |
| 1106 |
+ |
outvfmt = COLRFMT; |
| 1107 |
+ |
break; |
| 1108 |
+ |
default: |
| 1109 |
+ |
error(INTERNAL, "botched output format"); |
| 1110 |
+ |
return; |
| 1111 |
+ |
} |
| 1112 |
+ |
/* check modifier outputs */ |
| 1113 |
+ |
for (i = 0; i < nmods; i++) { |
| 1114 |
+ |
ment = lu_find(&modconttab,modname[i]); |
| 1115 |
+ |
mp = (MODCONT *)ment->data; |
| 1116 |
+ |
if (mp->outspec == NULL) |
| 1117 |
+ |
error(USER, "cannot recover from stdout"); |
| 1118 |
+ |
if (mp->outspec[0] == '!') |
| 1119 |
+ |
error(USER, "cannot recover from command"); |
| 1120 |
+ |
for (j = 0; ; j++) { /* check each bin's file */ |
| 1121 |
+ |
ofl = ofname(oname, mp->outspec, mp->modname, j); |
| 1122 |
+ |
if (ofl < 0) |
| 1123 |
+ |
error(USER, "bad output file specification"); |
| 1124 |
+ |
oent = lu_find(&ofiletab, oname); |
| 1125 |
+ |
if (oent->data != NULL) { |
| 1126 |
+ |
sout = *(STREAMOUT *)oent->data; |
| 1127 |
+ |
} else { |
| 1128 |
+ |
sout.reclen = 0; |
| 1129 |
+ |
sout.ofp = NULL; |
| 1130 |
+ |
} |
| 1131 |
+ |
if (sout.ofp != NULL) { /* already open? */ |
| 1132 |
+ |
if (ofl & OF_BIN) |
| 1133 |
+ |
continue; |
| 1134 |
+ |
break; |
| 1135 |
+ |
} |
| 1136 |
+ |
/* open output */ |
| 1137 |
+ |
sout.ofp = fopen(oname, "rb+"); |
| 1138 |
+ |
if (sout.ofp == NULL) { |
| 1139 |
+ |
if (j) |
| 1140 |
+ |
break; /* assume end of modifier */ |
| 1141 |
+ |
sprintf(errmsg, "missing recover file '%s'", |
| 1142 |
+ |
oname); |
| 1143 |
+ |
error(USER, errmsg); |
| 1144 |
+ |
} |
| 1145 |
+ |
nvals = lseek(fileno(sout.ofp), 0, SEEK_END); |
| 1146 |
+ |
if (nvals <= 0) { |
| 1147 |
+ |
lastout = 0; /* empty output, quit here */ |
| 1148 |
+ |
fclose(sout.ofp); |
| 1149 |
+ |
break; |
| 1150 |
+ |
} |
| 1151 |
+ |
if (!sout.reclen) { /* unspecified record length */ |
| 1152 |
+ |
if (!(ofl & OF_BIN)) { |
| 1153 |
+ |
sprintf(errmsg, |
| 1154 |
+ |
"guessing 1 value per record in '%s'", |
| 1155 |
+ |
oname); |
| 1156 |
+ |
error(WARNING, errmsg); |
| 1157 |
+ |
} |
| 1158 |
+ |
recsiz = outvsiz; |
| 1159 |
+ |
} else |
| 1160 |
+ |
recsiz = outvsiz * sout.reclen; |
| 1161 |
+ |
|
| 1162 |
+ |
lseek(fileno(sout.ofp), 0, SEEK_SET); |
| 1163 |
+ |
if (header && checkheader(sout.ofp, outvfmt, NULL) != 1) { |
| 1164 |
+ |
sprintf(errmsg, "format mismatch for '%s'", |
| 1165 |
+ |
oname); |
| 1166 |
+ |
error(USER, errmsg); |
| 1167 |
+ |
} |
| 1168 |
+ |
if ((xres > 0) & (yres > 0) && |
| 1169 |
+ |
(!fscnresolu(&xr, &yr, sout.ofp) || |
| 1170 |
+ |
xr != xres || |
| 1171 |
+ |
yr != yres)) { |
| 1172 |
+ |
sprintf(errmsg, "resolution mismatch for '%s'", |
| 1173 |
+ |
oname); |
| 1174 |
+ |
error(USER, errmsg); |
| 1175 |
+ |
} |
| 1176 |
+ |
nvals = (nvals - (off_t)ftell(sout.ofp)) / recsiz; |
| 1177 |
+ |
if ((lastout < 0) | (nvals < lastout)) |
| 1178 |
+ |
lastout = nvals; |
| 1179 |
+ |
if (oent->key == NULL) /* new entry */ |
| 1180 |
+ |
oent->key = strcpy((char *) |
| 1181 |
+ |
malloc(strlen(oname)+1), oname); |
| 1182 |
+ |
if (oent->data == NULL) |
| 1183 |
+ |
oent->data = (char *)malloc(sizeof(STREAMOUT)); |
| 1184 |
+ |
*(STREAMOUT *)oent->data = sout; |
| 1185 |
+ |
if (!(ofl & OF_BIN)) |
| 1186 |
+ |
break; /* no bin separation */ |
| 1187 |
+ |
} |
| 1188 |
+ |
if (!lastout) { /* empty output */ |
| 1189 |
+ |
error(WARNING, "no previous data to recover"); |
| 1190 |
+ |
lu_done(&ofiletab); /* reclose all outputs */ |
| 1191 |
+ |
return; |
| 1192 |
+ |
} |
| 1193 |
+ |
if (j > mp->nbins) /* reallocate modifier bins */ |
| 1194 |
+ |
ment->data = (char *)(mp = growmodifier(mp, j)); |
| 1195 |
+ |
} |
| 1196 |
+ |
if (lastout < 0) { |
| 1197 |
+ |
error(WARNING, "no output files to recover"); |
| 1198 |
+ |
return; |
| 1199 |
+ |
} |
| 1200 |
+ |
if (raysleft && lastout >= raysleft) { |
| 1201 |
+ |
error(WARNING, "output appears to be complete"); |
| 1202 |
+ |
/* XXX should read & discard input? */ |
| 1203 |
+ |
quit(0); |
| 1204 |
+ |
} |
| 1205 |
+ |
/* seek on all files */ |
| 1206 |
+ |
nvals = lastout * outvsiz; |
| 1207 |
+ |
lu_doall(&ofiletab, myseeko, &nvals); |
| 1208 |
+ |
/* skip repeated input */ |
| 1209 |
+ |
for (nvals = 0; nvals < lastout; nvals++) |
| 1210 |
+ |
if (getinp(oname, fin) <= 0) |
| 1211 |
+ |
error(USER, "unexpected EOF on input"); |
| 1212 |
+ |
lastray = lastdone = (unsigned long)lastout; |
| 1213 |
+ |
if (raysleft) |
| 1214 |
+ |
raysleft -= lastray; |
| 1215 |
|
} |