| 9 |  | */ | 
| 10 |  |  | 
| 11 |  | #include "rholo.h" | 
| 12 | < | #include "paths.h" | 
| 12 | > | #include "random.h" | 
| 13 | > | #include <signal.h> | 
| 14 |  | #include <sys/types.h> | 
| 15 | + | #include <sys/stat.h> | 
| 16 |  |  | 
| 17 |  | /* the following must be consistent with rholo.h */ | 
| 18 |  | int     NVARS = NRHVARS;                /* total number of variables */ | 
| 21 |  |  | 
| 22 |  | char    *progname;              /* our program name */ | 
| 23 |  | char    *hdkfile;               /* holodeck file name */ | 
| 24 | < | char    froot[MAXPATH];         /* root file name */ | 
| 24 | > | char    froot[256];             /* root file name */ | 
| 25 |  |  | 
| 26 |  | int     nowarn = 0;             /* turn warnings off? */ | 
| 27 |  |  | 
| 26 | – | double  expval = 1.;            /* global exposure value */ | 
| 27 | – |  | 
| 28 |  | int     ncprocs = 0;            /* desired number of compute processes */ | 
| 29 |  |  | 
| 30 |  | char    *outdev = NULL;         /* output device name */ | 
| 31 |  |  | 
| 32 | + | int     readinp = 0;            /* read commands from stdin */ | 
| 33 | + |  | 
| 34 | + | int     force = 0;              /* allow overwrite of holodeck */ | 
| 35 | + |  | 
| 36 |  | time_t  starttime;              /* time we got started */ | 
| 37 |  | time_t  endtime;                /* time we should end by */ | 
| 38 |  | time_t  reporttime;             /* time for next report */ | 
| 39 |  |  | 
| 40 | + | long    maxdisk;                /* maximum file space (bytes) */ | 
| 41 | + |  | 
| 42 |  | int     rtargc = 1;             /* rtrace command */ | 
| 43 |  | char    *rtargv[128] = {"rtrace", NULL}; | 
| 44 |  |  | 
| 45 | + | int     orig_mode = -1;         /* original file mode (-1 if unchanged) */ | 
| 46 | + |  | 
| 47 |  | long    nraysdone = 0L;         /* number of rays done */ | 
| 48 |  | long    npacksdone = 0L;        /* number of packets done */ | 
| 49 |  |  | 
| 50 |  | PACKET  *freepacks;             /* available packets */ | 
| 51 |  |  | 
| 52 | + | char  *sigerr[NSIG];            /* signal error messages */ | 
| 53 | + |  | 
| 54 |  | extern time_t   time(); | 
| 55 |  |  | 
| 56 |  |  | 
| 60 |  | { | 
| 61 |  | HDGRID  hdg; | 
| 62 |  | int     i; | 
| 53 | – | int     force = 0; | 
| 63 |  | /* mark start time */ | 
| 64 |  | starttime = time(NULL); | 
| 65 | + | initurand(16384);                       /* initialize urand */ | 
| 66 |  | progname = argv[0];                     /* get arguments */ | 
| 67 |  | for (i = 1; i < argc && argv[i][0] == '-'; i++) | 
| 68 |  | switch (argv[i][1]) { | 
| 72 |  | case 'f':                       /* force overwrite */ | 
| 73 |  | force++; | 
| 74 |  | break; | 
| 75 | + | case 'i':                       /* read input from stdin */ | 
| 76 | + | readinp++; | 
| 77 | + | break; | 
| 78 |  | case 'n':                       /* compute processes */ | 
| 79 |  | if (i >= argc-2) | 
| 80 |  | goto userr; | 
| 88 |  | default: | 
| 89 |  | goto userr; | 
| 90 |  | } | 
| 78 | – | /* do we have a job? */ | 
| 79 | – | if (outdev == NULL && ncprocs <= 0) | 
| 80 | – | goto userr; | 
| 91 |  | /* get root file name */ | 
| 92 |  | rootname(froot, hdkfile=argv[i++]); | 
| 93 |  | /* load... */ | 
| 103 |  | /* check settings */ | 
| 104 |  | checkvalues(); | 
| 105 |  | /* load RIF if any */ | 
| 106 | < | if (vdef(RIF)) | 
| 97 | < | getradfile(vval(RIF)); | 
| 106 | > | getradfile(); | 
| 107 |  | /* set defaults */ | 
| 108 |  | setdefaults(&hdg); | 
| 109 |  | /* holodeck exists? */ | 
| 114 |  | creatholo(&hdg); | 
| 115 |  | } else {                                /* else load holodeck */ | 
| 116 |  | loadholo(); | 
| 117 | < | if (vdef(RIF))                          /* load RIF if any */ | 
| 118 | < | getradfile(vval(RIF)); | 
| 117 | > | /* check settings */ | 
| 118 | > | checkvalues(); | 
| 119 | > | /* load RIF if any */ | 
| 120 | > | getradfile(); | 
| 121 | > | /* set defaults */ | 
| 122 | > | setdefaults(NULL); | 
| 123 |  | } | 
| 124 |  | /* initialize */ | 
| 125 |  | initrholo(); | 
| 126 | < | /* run */ | 
| 126 | > | /* main loop */ | 
| 127 |  | while (rholo()) | 
| 128 |  | ; | 
| 129 |  | /* done */ | 
| 130 |  | quit(0); | 
| 131 |  | userr: | 
| 132 |  | fprintf(stderr, | 
| 133 | < | "Usage: %s {-n nprocs|-o disp} [-w][-f] output.hdk [control.hif [VAR=val ..]]\n", | 
| 133 | > | "Usage: %s [-n nprocs][-o disp][-w][-f] output.hdk [control.hif [VAR=val ..]]\n", | 
| 134 |  | progname); | 
| 135 |  | quit(1); | 
| 136 |  | } | 
| 137 |  |  | 
| 138 |  |  | 
| 139 | + | onsig(signo)                            /* fatal signal */ | 
| 140 | + | int  signo; | 
| 141 | + | { | 
| 142 | + | static int  gotsig = 0; | 
| 143 | + |  | 
| 144 | + | if (gotsig++)                   /* two signals and we're gone! */ | 
| 145 | + | _exit(signo); | 
| 146 | + |  | 
| 147 | + | alarm(30);                      /* allow 30 seconds to clean up */ | 
| 148 | + | signal(SIGALRM, SIG_DFL);       /* make certain we do die */ | 
| 149 | + | eputs("signal - "); | 
| 150 | + | eputs(sigerr[signo]); | 
| 151 | + | eputs("\n"); | 
| 152 | + | quit(3); | 
| 153 | + | } | 
| 154 | + |  | 
| 155 | + |  | 
| 156 | + | sigdie(signo, msg)                      /* set fatal signal */ | 
| 157 | + | int  signo; | 
| 158 | + | char  *msg; | 
| 159 | + | { | 
| 160 | + | if (signal(signo, onsig) == SIG_IGN) | 
| 161 | + | signal(signo, SIG_IGN); | 
| 162 | + | sigerr[signo] = msg; | 
| 163 | + | } | 
| 164 | + |  | 
| 165 | + |  | 
| 166 | + | int | 
| 167 | + | resfmode(fd, mod)               /* restrict open file access mode */ | 
| 168 | + | int     fd, mod; | 
| 169 | + | { | 
| 170 | + | struct stat     stbuf; | 
| 171 | + | /* get original mode */ | 
| 172 | + | if (fstat(fd, &stbuf) < 0) | 
| 173 | + | error(SYSTEM, "cannot stat open holodeck file"); | 
| 174 | + | mod &= stbuf.st_mode;           /* always more restrictive */ | 
| 175 | + | if (mod == stbuf.st_mode) | 
| 176 | + | return(-1);             /* already set */ | 
| 177 | + | /* else change it */ | 
| 178 | + | if (fchmod(fd, mod) < 0) { | 
| 179 | + | error(WARNING, "cannot change holodeck file access mode"); | 
| 180 | + | return(-1); | 
| 181 | + | } | 
| 182 | + | return(stbuf.st_mode);          /* return original mode */ | 
| 183 | + | } | 
| 184 | + |  | 
| 185 | + |  | 
| 186 |  | initrholo()                     /* get our holodeck running */ | 
| 187 |  | { | 
| 188 |  | extern int      global_packet(); | 
| 189 |  | register int    i; | 
| 190 | < | /* check output device */ | 
| 191 | < | if (outdev != NULL) | 
| 192 | < | open_display(outdev); | 
| 190 | > | /* close holodeck on exec() */ | 
| 191 | > | fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC); | 
| 192 | > |  | 
| 193 | > | if (outdev != NULL)                     /* open output device */ | 
| 194 | > | disp_open(outdev); | 
| 195 |  | else if (ncprocs > 0)                   /* else use global ray feed */ | 
| 196 |  | init_global(); | 
| 197 | + | /* record disk space limit */ | 
| 198 | + | if (!vdef(DISKSPACE)) | 
| 199 | + | maxdisk = 0; | 
| 200 | + | else | 
| 201 | + | maxdisk = 1024.*1024.*vflt(DISKSPACE); | 
| 202 |  | /* record end time */ | 
| 203 |  | if (!vdef(TIME) || vflt(TIME) <= FTINY) | 
| 204 |  | endtime = 0; | 
| 205 |  | else | 
| 206 | < | endtime = starttime + vflt(TIME)*3600.; | 
| 206 | > | endtime = starttime + vflt(TIME)*3600. + .5; | 
| 207 |  | /* set up memory cache */ | 
| 208 | < | hdcachesize = 1024.*1024.*vflt(CACHE); | 
| 208 | > | if (outdev == NULL) | 
| 209 | > | hdcachesize = 0;        /* manual flushing */ | 
| 210 | > | else if (vdef(CACHE)) | 
| 211 | > | hdcachesize = 1024.*1024.*vflt(CACHE); | 
| 212 |  | /* open report file */ | 
| 213 |  | if (vdef(REPORT)) { | 
| 214 |  | register char   *s = sskip2(vval(REPORT), 1); | 
| 219 |  | if (ncprocs > 0) { | 
| 220 |  | i = start_rtrace(); | 
| 221 |  | if (i < 1) | 
| 222 | < | error(USER, "cannot start rtrace process"); | 
| 222 | > | error(USER, "cannot start rtrace process(es)"); | 
| 223 |  | if (vdef(REPORT)) {             /* make first report */ | 
| 224 |  | printargs(rtargc, rtargv, stderr); | 
| 225 |  | report(0); | 
| 230 |  | goto memerr; | 
| 231 |  | freepacks[--i].nr = 0; | 
| 232 |  | freepacks[i].next = NULL; | 
| 233 | < | if (!vbool(OBSTRUCTIONS)) { | 
| 233 | > | if (!vdef(OBSTRUCTIONS) || !vbool(OBSTRUCTIONS)) { | 
| 234 |  | freepacks[i].offset = (float *)bmalloc( | 
| 235 |  | RPACKSIZ*sizeof(float)*(i+1) ); | 
| 236 |  | if (freepacks[i].offset == NULL) | 
| 244 |  | freepacks[i].next = &freepacks[i+1]; | 
| 245 |  | } | 
| 246 |  | } | 
| 247 | + | /* set up signal handling */ | 
| 248 | + | sigdie(SIGINT, "Interrupt"); | 
| 249 | + | sigdie(SIGHUP, "Hangup"); | 
| 250 | + | sigdie(SIGTERM, "Terminate"); | 
| 251 | + | sigdie(SIGPIPE, "Broken pipe"); | 
| 252 | + | sigdie(SIGALRM, "Alarm clock"); | 
| 253 | + | #ifdef  SIGXCPU | 
| 254 | + | sigdie(SIGXCPU, "CPU limit exceeded"); | 
| 255 | + | sigdie(SIGXFSZ, "File size exceeded"); | 
| 256 | + | #endif | 
| 257 | + | /* protect holodeck file */ | 
| 258 | + | orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444); | 
| 259 |  | return; | 
| 260 |  | memerr: | 
| 261 |  | error(SYSTEM, "out of memory in initrholo"); | 
| 269 |  | register PACKET *p; | 
| 270 |  | time_t  t; | 
| 271 |  | long    l; | 
| 272 | < | /* check display */ | 
| 273 | < | if (outdev != NULL && !disp_check(idle)) | 
| 274 | < | return(0); | 
| 272 | > |  | 
| 273 | > | if (outdev != NULL)             /* check display */ | 
| 274 | > | if (!disp_check(idle)) | 
| 275 | > | return(0); | 
| 276 |  | /* display only? */ | 
| 277 | < | if (ncprocs <= 0) | 
| 278 | < | return(1); | 
| 277 | > | if (nprocs <= 0) | 
| 278 | > | return(outdev != NULL); | 
| 279 |  | /* check file size */ | 
| 280 | < | if ((l = 1024.*1024.*vflt(DISKSPACE)) > 0 && | 
| 281 | < | hdfiluse(hdlist[0]->fd, 0) + hdmemuse(0) >= l) | 
| 282 | < | return(0); | 
| 280 | > | if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) { | 
| 281 | > | error(WARNING, "file limit exceeded"); | 
| 282 | > | done_rtrace(); | 
| 283 | > | idle = 1; | 
| 284 | > | return(1);      /* comes back */ | 
| 285 | > | } | 
| 286 |  | /* check time */ | 
| 287 |  | if (endtime > 0 || reporttime > 0) | 
| 288 |  | t = time(NULL); | 
| 289 | < | if (endtime > 0 && t >= endtime) | 
| 290 | < | return(0); | 
| 289 | > | if (endtime > 0 && t >= endtime) { | 
| 290 | > | error(WARNING, "time limit exceeded"); | 
| 291 | > | done_rtrace(); | 
| 292 | > | idle = 1; | 
| 293 | > | return(1);      /* comes back */ | 
| 294 | > | } | 
| 295 |  | if (reporttime > 0 && t >= reporttime) | 
| 296 |  | report(t); | 
| 297 |  | /* get packets to process */ | 
| 299 |  | p = freepacks; freepacks = p->next; p->next = NULL; | 
| 300 |  | if (!next_packet(p)) { | 
| 301 |  | p->next = freepacks; freepacks = p; | 
| 302 | + | idle = 1; | 
| 303 |  | break; | 
| 304 |  | } | 
| 305 |  | if (pl == NULL) pl = p; | 
| 306 |  | else plend->next = p; | 
| 307 |  | plend = p; | 
| 308 |  | } | 
| 309 | < | idle = pl == NULL && freepacks != NULL; | 
| 219 | < | /* are we out of stuff to do? */ | 
| 220 | < | if (idle && outdev == NULL) | 
| 221 | < | return(0); | 
| 222 | < | /* else process packets */ | 
| 309 | > | /* process packets */ | 
| 310 |  | done_packets(do_packets(pl)); | 
| 311 |  | return(1);                      /* and continue */ | 
| 312 |  | } | 
| 313 |  |  | 
| 314 |  |  | 
| 228 | – | report(t)                       /* report progress so far */ | 
| 229 | – | time_t  t; | 
| 230 | – | { | 
| 231 | – | if (t == 0) | 
| 232 | – | t = time(NULL); | 
| 233 | – | fprintf(stderr, "%s: %ld packets (%ld rays) done after %.2f hours\n", | 
| 234 | – | progname, npacksdone, nraysdone, (t-starttime)/3600.); | 
| 235 | – | fflush(stderr); | 
| 236 | – | if (vdef(REPORT)) | 
| 237 | – | reporttime = t + (time_t)(vflt(REPORT)*60.+.5); | 
| 238 | – | } | 
| 239 | – |  | 
| 240 | – |  | 
| 315 |  | setdefaults(gp)                 /* set default values */ | 
| 316 |  | register HDGRID *gp; | 
| 317 |  | { | 
| 328 |  | sprintf(errmsg, "ignoring all but first %s", vnam(SECTION)); | 
| 329 |  | error(WARNING, errmsg); | 
| 330 |  | } | 
| 331 | + | if (!vdef(OCTREE)) { | 
| 332 | + | if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL) | 
| 333 | + | error(SYSTEM, "out of memory"); | 
| 334 | + | sprintf(vval(OCTREE), "%s.oct", froot); | 
| 335 | + | vdef(OCTREE)++; | 
| 336 | + | } | 
| 337 | + | if (!vdef(VDIST)) { | 
| 338 | + | vval(VDIST) = "F"; | 
| 339 | + | vdef(VDIST)++; | 
| 340 | + | } | 
| 341 | + | if (!vdef(OCCUPANCY)) { | 
| 342 | + | vval(OCCUPANCY) = "U"; | 
| 343 | + | vdef(OCCUPANCY)++; | 
| 344 | + | } | 
| 345 | + | /* append rendering options */ | 
| 346 | + | if (vdef(RENDER)) | 
| 347 | + | rtargc += wordstring(rtargv+rtargc, vval(RENDER)); | 
| 348 | + |  | 
| 349 | + | if (gp == NULL)         /* already initialized? */ | 
| 350 | + | return; | 
| 351 | + | /* set grid parameters */ | 
| 352 | + | gp->grid[0] = gp->grid[1] = gp->grid[2] = 0; | 
| 353 |  | if (sscanf(vval(SECTION), | 
| 354 | < | "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf", | 
| 354 | > | "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd", | 
| 355 |  | &gp->orig[0], &gp->orig[1], &gp->orig[2], | 
| 356 |  | &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2], | 
| 357 |  | &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2], | 
| 358 | < | &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2]) != 12) | 
| 358 | > | &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2], | 
| 359 | > | &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12) | 
| 360 |  | badvalue(SECTION); | 
| 361 |  | maxlen = 0.; | 
| 362 |  | for (i = 0; i < 3; i++) | 
| 370 |  | if ((d = vflt(GRID)) <= FTINY) | 
| 371 |  | badvalue(GRID); | 
| 372 |  | for (i = 0; i < 3; i++) | 
| 373 | < | gp->grid[i] = len[i]/d + (1.-FTINY); | 
| 374 | < | if (!vdef(EXPOSURE)) { | 
| 278 | < | sprintf(errmsg, "%s must be defined", vnam(EXPOSURE)); | 
| 279 | < | error(USER, errmsg); | 
| 280 | < | } | 
| 281 | < | expval = vval(EXPOSURE)[0] == '-' || vval(EXPOSURE)[0] == '+' ? | 
| 282 | < | pow(2., vflt(EXPOSURE)) : vflt(EXPOSURE); | 
| 283 | < | if (!vdef(OCTREE)) { | 
| 284 | < | if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL) | 
| 285 | < | error(SYSTEM, "out of memory"); | 
| 286 | < | sprintf(vval(OCTREE), "%s.oct", froot); | 
| 287 | < | vdef(OCTREE)++; | 
| 288 | < | } | 
| 289 | < | if (!vdef(DISKSPACE)) { | 
| 290 | < | sprintf(errmsg, | 
| 291 | < | "no %s setting, assuming 100 Mbytes available", | 
| 292 | < | vnam(DISKSPACE)); | 
| 293 | < | error(WARNING, errmsg); | 
| 294 | < | vval(DISKSPACE) = "100"; | 
| 295 | < | vdef(DISKSPACE)++; | 
| 296 | < | } | 
| 297 | < | if (!vdef(CACHE)) { | 
| 298 | < | sprintf(errmsg, | 
| 299 | < | "no %s setting, assuming 10 Mbytes available", | 
| 300 | < | vnam(CACHE)); | 
| 301 | < | error(WARNING, errmsg); | 
| 302 | < | vval(CACHE) = "10"; | 
| 303 | < | vdef(CACHE)++; | 
| 304 | < | } | 
| 305 | < | if (!vdef(OBSTRUCTIONS)) { | 
| 306 | < | vval(OBSTRUCTIONS) = "T"; | 
| 307 | < | vdef(OBSTRUCTIONS)++; | 
| 308 | < | } | 
| 309 | < | if (!vdef(OCCUPANCY)) { | 
| 310 | < | vval(OCCUPANCY) = "U"; | 
| 311 | < | vdef(OCCUPANCY)++; | 
| 312 | < | } | 
| 313 | < | /* append rendering options */ | 
| 314 | < | if (vdef(RENDER)) | 
| 315 | < | rtargc += wordstring(rtargv+rtargc, vval(RENDER)); | 
| 373 | > | if (gp->grid[i] <= 0) | 
| 374 | > | gp->grid[i] = len[i]/d + (1.-FTINY); | 
| 375 |  | } | 
| 376 |  |  | 
| 377 |  |  | 
| 378 |  | creatholo(gp)                   /* create a holodeck output file */ | 
| 379 |  | HDGRID  *gp; | 
| 380 |  | { | 
| 381 | + | extern char     VersionID[]; | 
| 382 |  | long    endloc = 0; | 
| 383 | + | int     fd; | 
| 384 |  | FILE    *fp; | 
| 385 |  | /* open & truncate file */ | 
| 386 |  | if ((fp = fopen(hdkfile, "w+")) == NULL) { | 
| 389 |  | } | 
| 390 |  | /* write information header */ | 
| 391 |  | newheader("RADIANCE", fp); | 
| 392 | + | fprintf(fp, "SOFTWARE= %s\n", VersionID); | 
| 393 |  | printvars(fp); | 
| 394 |  | fputformat(HOLOFMT, fp); | 
| 395 |  | fputc('\n', fp); | 
| 396 |  | putw(HOLOMAGIC, fp);            /* put magic number & terminus */ | 
| 397 |  | fwrite(&endloc, sizeof(long), 1, fp); | 
| 398 | < | fflush(fp);                     /* flush buffer */ | 
| 399 | < | hdinit(fileno(fp), gp);         /* allocate and initialize index */ | 
| 400 | < | /* we're dropping fp here.... */ | 
| 398 | > | fd = dup(fileno(fp)); | 
| 399 | > | fclose(fp);                     /* flush and close stdio stream */ | 
| 400 | > | hdinit(fd, gp);                 /* allocate and initialize index */ | 
| 401 |  | } | 
| 402 |  |  | 
| 403 |  |  | 
| 427 |  |  | 
| 428 |  | loadholo()                      /* start loading a holodeck from fname */ | 
| 429 |  | { | 
| 430 | + | extern long     ftell(); | 
| 431 |  | FILE    *fp; | 
| 432 | < | long    endloc; | 
| 433 | < | /* open input file */ | 
| 434 | < | if ((fp = fopen(hdkfile, "r+")) == NULL) { | 
| 435 | < | sprintf(errmsg, "cannot open \"%s\" for appending", hdkfile); | 
| 432 | > | int     fd; | 
| 433 | > | long    fpos; | 
| 434 | > | /* open holodeck file */ | 
| 435 | > | if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) { | 
| 436 | > | sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile, | 
| 437 | > | ncprocs>0 ? "appending" : "reading"); | 
| 438 |  | error(SYSTEM, errmsg); | 
| 439 |  | } | 
| 440 |  | /* load variables from header */ | 
| 445 |  | hdkfile); | 
| 446 |  | error(USER, errmsg); | 
| 447 |  | } | 
| 448 | < | fread(&endloc, sizeof(long), 1, fp); | 
| 449 | < | if (endloc != 0) | 
| 448 | > | fread(&fpos, sizeof(long), 1, fp); | 
| 449 | > | if (fpos != 0) | 
| 450 |  | error(WARNING, "ignoring multiple sections in holodeck file"); | 
| 451 | < | fseek(fp, 0L, 1);                       /* align system file pointer */ | 
| 452 | < | hdinit(fileno(fp), NULL);               /* allocate and load index */ | 
| 453 | < | /* we're dropping fp here.... */ | 
| 451 | > | fpos = ftell(fp);                       /* get stdio position */ | 
| 452 | > | fd = dup(fileno(fp)); | 
| 453 | > | fclose(fp);                             /* done with stdio */ | 
| 454 | > | lseek(fd, fpos, 0);                     /* align system file pointer */ | 
| 455 | > | hdinit(fd, NULL);                       /* allocate and load index */ | 
| 456 |  | } | 
| 457 |  |  | 
| 458 |  |  | 
| 459 |  | done_packets(pl)                /* handle finished packets */ | 
| 460 |  | PACKET  *pl; | 
| 461 |  | { | 
| 462 | + | static int      n2flush = 0; | 
| 463 |  | register PACKET *p; | 
| 464 |  |  | 
| 465 |  | while (pl != NULL) { | 
| 469 |  | (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr), | 
| 470 |  | p->nr*sizeof(RAYVAL)); | 
| 471 |  | if (outdev != NULL)     /* display it */ | 
| 472 | < | disp_packet(p); | 
| 472 | > | disp_packet((PACKHEAD *)p); | 
| 473 | > | if (hdcachesize <= 0)   /* manual flushing */ | 
| 474 | > | n2flush += p->nr; | 
| 475 | > | nraysdone += p->nr; | 
| 476 | > | npacksdone++; | 
| 477 |  | } | 
| 406 | – | nraysdone += p->nr; | 
| 407 | – | npacksdone++; | 
| 478 |  | p->nr = 0;                      /* push onto free list */ | 
| 479 |  | p->next = freepacks; | 
| 480 |  | freepacks = p; | 
| 481 |  | } | 
| 482 | < | } | 
| 483 | < |  | 
| 484 | < |  | 
| 415 | < | getradfile(rfargs)              /* run rad and get needed variables */ | 
| 416 | < | char    *rfargs; | 
| 417 | < | { | 
| 418 | < | static short    mvar[] = {VIEW,OCTREE,EXPOSURE,-1}; | 
| 419 | < | static char     tf1[] = TEMPLATE; | 
| 420 | < | char    tf2[64]; | 
| 421 | < | char    combuf[256]; | 
| 422 | < | char    *pippt; | 
| 423 | < | register int    i; | 
| 424 | < | register char   *cp; | 
| 425 | < | /* create rad command */ | 
| 426 | < | mktemp(tf1); | 
| 427 | < | sprintf(tf2, "%s.rif", tf1); | 
| 428 | < | sprintf(combuf, | 
| 429 | < | "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH", | 
| 430 | < | rfargs, tf1); | 
| 431 | < | cp = combuf; | 
| 432 | < | while (*cp){ | 
| 433 | < | if (*cp == '|') pippt = cp; | 
| 434 | < | cp++; | 
| 435 | < | }                               /* match unset variables */ | 
| 436 | < | for (i = 0; mvar[i] >= 0; i++) | 
| 437 | < | if (!vdef(mvar[i])) { | 
| 438 | < | *cp++ = '|'; | 
| 439 | < | strcpy(cp, vnam(mvar[i])); | 
| 440 | < | while (*cp) cp++; | 
| 441 | < | pippt = NULL; | 
| 442 | < | } | 
| 443 | < | if (pippt != NULL) | 
| 444 | < | strcpy(pippt, "> /dev/null");   /* nothing to match */ | 
| 445 | < | else | 
| 446 | < | sprintf(cp, ")[ \t]*=' > %s", tf2); | 
| 447 | < | if (system(combuf)) { | 
| 448 | < | error(SYSTEM, "cannot execute rad command"); | 
| 449 | < | unlink(tf2);                    /* clean up */ | 
| 450 | < | unlink(tf1); | 
| 451 | < | quit(1); | 
| 482 | > | if (n2flush > 512*RPACKSIZ*nprocs) { | 
| 483 | > | hdflush(NULL);                  /* flush holodeck buffers */ | 
| 484 | > | n2flush = 0; | 
| 485 |  | } | 
| 453 | – | if (pippt == NULL) { | 
| 454 | – | loadvars(tf2);                  /* load variables */ | 
| 455 | – | unlink(tf2); | 
| 456 | – | } | 
| 457 | – | rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */ | 
| 458 | – | unlink(tf1);                    /* clean up */ | 
| 486 |  | } | 
| 487 |  |  | 
| 488 |  |  | 
| 492 |  | char    *tp, *dp; | 
| 493 |  |  | 
| 494 |  | for (tp = NULL, dp = rn; *rn = *fn++; rn++) | 
| 495 | < | if (ISDIRSEP(*rn)) | 
| 495 | > | if (*rn == '/') | 
| 496 |  | dp = rn; | 
| 497 |  | else if (*rn == '.') | 
| 498 |  | tp = rn; | 
| 541 |  | { | 
| 542 |  | int     status = 0; | 
| 543 |  |  | 
| 544 | < | if (hdlist[0] != NULL) {        /* flush holodeck */ | 
| 545 | < | if (ncprocs > 0) { | 
| 546 | < | done_packets(flush_queue()); | 
| 547 | < | status = end_rtrace();  /* close rtrace */ | 
| 548 | < | hdflush(NULL); | 
| 549 | < | if (vdef(REPORT)) { | 
| 550 | < | long    fsiz, fuse; | 
| 551 | < | report(0); | 
| 525 | < | fsiz = lseek(hdlist[0]->fd, 0L, 2); | 
| 526 | < | fuse = hdfiluse(hdlist[0]->fd, 1); | 
| 527 | < | fprintf(stderr, | 
| 544 | > | if (hdlist[0] != NULL) {        /* close holodeck */ | 
| 545 | > | if (nprocs > 0) | 
| 546 | > | status = done_rtrace();         /* calls hdsync() */ | 
| 547 | > | if (ncprocs > 0 && vdef(REPORT)) { | 
| 548 | > | long    fsiz, fuse; | 
| 549 | > | fsiz = hdfilen(hdlist[0]->fd); | 
| 550 | > | fuse = hdfiluse(hdlist[0]->fd, 1); | 
| 551 | > | fprintf(stderr, | 
| 552 |  | "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n", | 
| 553 | < | hdkfile, fsiz/(1024.*1024.), | 
| 554 | < | 100.*(fsiz-fuse)/fsiz); | 
| 555 | < | } | 
| 532 | < | } else | 
| 533 | < | hdflush(NULL); | 
| 553 | > | hdkfile, fsiz/(1024.*1024.), | 
| 554 | > | 100.*(fsiz-fuse)/fsiz); | 
| 555 | > | } | 
| 556 |  | } | 
| 557 | + | if (orig_mode >= 0)             /* reset holodeck access mode */ | 
| 558 | + | fchmod(hdlist[0]->fd, orig_mode); | 
| 559 | + | if (outdev != NULL)             /* close display */ | 
| 560 | + | disp_close(); | 
| 561 |  | exit(ec ? ec : status);         /* exit */ | 
| 562 |  | } |