| 1 | < | /* Copyright (c) 1997 Silicon Graphics, Inc. */ | 
| 1 | > | /* Copyright (c) 1999 Silicon Graphics, Inc. */ | 
| 2 |  |  | 
| 3 |  | #ifndef lint | 
| 4 |  | static char SCCSid[] = "$SunId$ SGI"; | 
| 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 | + | #ifndef RTFLUSH | 
| 18 | + | #define RTFLUSH         0               /* flush one at a time in batch */ | 
| 19 | + | #endif | 
| 20 | + | #ifndef FRAGWARN | 
| 21 | + | #define FRAGWARN        20              /* fragmentation for warning (%) */ | 
| 22 | + | #endif | 
| 23 | + | #ifndef MAXQTIME | 
| 24 | + | #define MAXQTIME        5               /* target maximum seconds in queue */ | 
| 25 | + | #endif | 
| 26 |  | /* the following must be consistent with rholo.h */ | 
| 27 |  | int     NVARS = NRHVARS;                /* total number of variables */ | 
| 28 |  |  | 
| 30 |  |  | 
| 31 |  | char    *progname;              /* our program name */ | 
| 32 |  | char    *hdkfile;               /* holodeck file name */ | 
| 33 | < | char    froot[MAXPATH];         /* root file name */ | 
| 33 | > | char    froot[256];             /* root file name */ | 
| 34 |  |  | 
| 26 | – | int     nowarn = 0;             /* turn warnings off? */ | 
| 27 | – |  | 
| 28 | – | double  expval = 1.;            /* global exposure value */ | 
| 29 | – |  | 
| 35 |  | int     ncprocs = 0;            /* desired number of compute processes */ | 
| 36 |  |  | 
| 37 |  | char    *outdev = NULL;         /* output device name */ | 
| 38 |  |  | 
| 39 | + | int     readinp = 0;            /* read commands from stdin */ | 
| 40 | + |  | 
| 41 | + | int     force = 0;              /* allow overwrite of holodeck */ | 
| 42 | + |  | 
| 43 |  | time_t  starttime;              /* time we got started */ | 
| 44 |  | time_t  endtime;                /* time we should end by */ | 
| 45 |  | time_t  reporttime;             /* time for next report */ | 
| 55 |  | long    npacksdone = 0L;        /* number of packets done */ | 
| 56 |  |  | 
| 57 |  | PACKET  *freepacks;             /* available packets */ | 
| 58 | + | int     totqlen;                /* maximum queue length when full */ | 
| 59 |  |  | 
| 60 |  | char  *sigerr[NSIG];            /* signal error messages */ | 
| 61 |  |  | 
| 62 | + | extern int      nowarn;         /* turn warnings off? */ | 
| 63 | + |  | 
| 64 |  | extern time_t   time(); | 
| 65 |  |  | 
| 66 |  |  | 
| 68 |  | int     argc; | 
| 69 |  | char    *argv[]; | 
| 70 |  | { | 
| 59 | – | HDGRID  hdg; | 
| 71 |  | int     i; | 
| 72 | < | int     force = 0; | 
| 73 | < | /* mark start time */ | 
| 63 | < | starttime = time(NULL); | 
| 72 | > |  | 
| 73 | > | initurand(16384);                       /* initialize urand */ | 
| 74 |  | progname = argv[0];                     /* get arguments */ | 
| 75 |  | for (i = 1; i < argc && argv[i][0] == '-'; i++) | 
| 76 |  | switch (argv[i][1]) { | 
| 80 |  | case 'f':                       /* force overwrite */ | 
| 81 |  | force++; | 
| 82 |  | break; | 
| 83 | + | case 'i':                       /* read input from stdin */ | 
| 84 | + | readinp++; | 
| 85 | + | break; | 
| 86 |  | case 'n':                       /* compute processes */ | 
| 87 |  | if (i >= argc-2) | 
| 88 |  | goto userr; | 
| 96 |  | default: | 
| 97 |  | goto userr; | 
| 98 |  | } | 
| 86 | – | /* do we have a job? */ | 
| 87 | – | if (outdev == NULL && ncprocs <= 0) | 
| 88 | – | goto userr; | 
| 99 |  | /* get root file name */ | 
| 100 | + | if (i >= argc) | 
| 101 | + | goto userr; | 
| 102 |  | rootname(froot, hdkfile=argv[i++]); | 
| 103 | < | /* load... */ | 
| 104 | < | if (i < argc) {                         /* variables */ | 
| 105 | < | loadvars(argv[i++]); | 
| 106 | < | /* cmdline settings */ | 
| 107 | < | for ( ; i < argc; i++) | 
| 108 | < | if (setvariable(argv[i], matchvar) < 0) { | 
| 109 | < | sprintf(errmsg, "unknown variable: %s", | 
| 110 | < | argv[i]); | 
| 111 | < | error(USER, errmsg); | 
| 112 | < | } | 
| 113 | < | /* check settings */ | 
| 114 | < | checkvalues(); | 
| 115 | < | /* load RIF if any */ | 
| 116 | < | if (vdef(RIF)) | 
| 117 | < | getradfile(vval(RIF)); | 
| 103 | > | /* load variables? */ | 
| 104 | > | if (i < argc) | 
| 105 | > | if (argv[i][0] != '-' && argv[i][0] != '+') | 
| 106 | > | loadvars(argv[i]);      /* load variables from file */ | 
| 107 | > |  | 
| 108 | > | if (i >= argc || argv[i][0] == '+') | 
| 109 | > | loadholo();                     /* load existing holodeck */ | 
| 110 | > |  | 
| 111 | > | while (++i < argc)                      /* get command line settings */ | 
| 112 | > | if (setvariable(argv[i], matchvar) < 0) { | 
| 113 | > | sprintf(errmsg, "unknown variable: %s", argv[i]); | 
| 114 | > | error(USER, errmsg); | 
| 115 | > | } | 
| 116 | > | /* check settings */ | 
| 117 | > | checkvalues(); | 
| 118 | > | /* load rad input file */ | 
| 119 | > | getradfile(); | 
| 120 | > |  | 
| 121 | > | if (hdlist[0] == NULL) {                /* create new holodeck */ | 
| 122 | > | HDGRID  hdg[HDMAX]; | 
| 123 |  | /* set defaults */ | 
| 124 | < | setdefaults(&hdg); | 
| 124 | > | setdefaults(hdg); | 
| 125 |  | /* holodeck exists? */ | 
| 126 |  | if (!force && access(hdkfile, R_OK|W_OK) == 0) | 
| 127 |  | error(USER, | 
| 128 |  | "holodeck file exists -- use -f to overwrite"); | 
| 129 |  | /* create holodeck */ | 
| 130 | < | creatholo(&hdg); | 
| 131 | < | } else {                                /* else load holodeck */ | 
| 132 | < | loadholo(); | 
| 116 | < | if (vdef(RIF))                          /* load RIF if any */ | 
| 117 | < | getradfile(vval(RIF)); | 
| 118 | < | } | 
| 130 | > | creatholo(hdg); | 
| 131 | > | } else                                  /* else just set defaults */ | 
| 132 | > | setdefaults(NULL); | 
| 133 |  | /* initialize */ | 
| 134 |  | initrholo(); | 
| 135 | < | /* run */ | 
| 135 | > | /* main loop */ | 
| 136 |  | while (rholo()) | 
| 137 |  | ; | 
| 138 |  | /* done */ | 
| 139 |  | quit(0); | 
| 140 |  | userr: | 
| 141 |  | fprintf(stderr, | 
| 142 | < | "Usage: %s {-n nprocs|-o disp} [-w][-f] output.hdk [control.hif [VAR=val ..]]\n", | 
| 142 | > | "Usage: %s [-n nprocs][-o disp][-w][-f] output.hdk [control.hif|+|- [VAR=val ..]]\n", | 
| 143 |  | progname); | 
| 144 |  | quit(1); | 
| 145 |  | } | 
| 150 |  | { | 
| 151 |  | static int  gotsig = 0; | 
| 152 |  |  | 
| 153 | < | if (gotsig++)                   /* two signals and we're gone! */ | 
| 153 | > | if (gotsig > 1)                 /* we're going as fast as we can! */ | 
| 154 | > | return; | 
| 155 | > | if (gotsig++) {                 /* two signals and we split */ | 
| 156 | > | hdsync(NULL, 0);        /* don't leave w/o saying goodbye */ | 
| 157 |  | _exit(signo); | 
| 158 | < |  | 
| 159 | < | alarm(30);                      /* allow 30 seconds to clean up */ | 
| 143 | < | signal(SIGALRM, SIG_DFL);       /* make certain we do die */ | 
| 158 | > | } | 
| 159 | > | alarm(300);                     /* allow 5 minutes to clean up */ | 
| 160 |  | eputs("signal - "); | 
| 161 |  | eputs(sigerr[signo]); | 
| 162 |  | eputs("\n"); | 
| 183 |  | if (fstat(fd, &stbuf) < 0) | 
| 184 |  | error(SYSTEM, "cannot stat open holodeck file"); | 
| 185 |  | mod &= stbuf.st_mode;           /* always more restrictive */ | 
| 186 | < | if (mod == stbuf.st_mode) | 
| 186 | > | if (mod == (stbuf.st_mode & 0777)) | 
| 187 |  | return(-1);             /* already set */ | 
| 188 |  | /* else change it */ | 
| 189 |  | if (fchmod(fd, mod) < 0) { | 
| 198 |  | { | 
| 199 |  | extern int      global_packet(); | 
| 200 |  | register int    i; | 
| 201 | + | /* close holodeck on exec() */ | 
| 202 | + | fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC); | 
| 203 |  |  | 
| 204 |  | if (outdev != NULL)                     /* open output device */ | 
| 205 |  | disp_open(outdev); | 
| 210 |  | maxdisk = 0; | 
| 211 |  | else | 
| 212 |  | maxdisk = 1024.*1024.*vflt(DISKSPACE); | 
| 195 | – | /* record end time */ | 
| 196 | – | if (!vdef(TIME) || vflt(TIME) <= FTINY) | 
| 197 | – | endtime = 0; | 
| 198 | – | else | 
| 199 | – | endtime = starttime + vflt(TIME)*3600.; | 
| 213 |  | /* set up memory cache */ | 
| 214 |  | if (outdev == NULL) | 
| 215 |  | hdcachesize = 0;        /* manual flushing */ | 
| 221 |  | if (*s && freopen(s, "a", stderr) == NULL) | 
| 222 |  | quit(2); | 
| 223 |  | } | 
| 224 | + | /* mark the starting time */ | 
| 225 | + | starttime = time(NULL); | 
| 226 | + | /* compute end time */ | 
| 227 | + | if (!vdef(TIME) || vflt(TIME) <= FTINY) | 
| 228 | + | endtime = 0; | 
| 229 | + | else | 
| 230 | + | endtime = starttime + vflt(TIME)*3600. + .5; | 
| 231 |  | /* start rtrace */ | 
| 232 |  | if (ncprocs > 0) { | 
| 233 | < | i = start_rtrace(); | 
| 233 | > | totqlen = i = start_rtrace(); | 
| 234 |  | if (i < 1) | 
| 235 | < | error(USER, "cannot start rtrace process"); | 
| 235 | > | error(USER, "cannot start rtrace process(es)"); | 
| 236 |  | if (vdef(REPORT)) {             /* make first report */ | 
| 237 |  | printargs(rtargc, rtargv, stderr); | 
| 238 |  | report(0); | 
| 243 |  | goto memerr; | 
| 244 |  | freepacks[--i].nr = 0; | 
| 245 |  | freepacks[i].next = NULL; | 
| 246 | < | if (!vbool(OBSTRUCTIONS)) { | 
| 246 | > | if (!vdef(OBSTRUCTIONS) || !vbool(OBSTRUCTIONS)) { | 
| 247 |  | freepacks[i].offset = (float *)bmalloc( | 
| 248 |  | RPACKSIZ*sizeof(float)*(i+1) ); | 
| 249 |  | if (freepacks[i].offset == NULL) | 
| 277 |  |  | 
| 278 |  | rholo()                         /* holodeck main loop */ | 
| 279 |  | { | 
| 280 | < | static int      idle = 1; | 
| 280 | > | static long     nextfragwarn = 100*(1L<<20); | 
| 281 | > | static int      idle = 0; | 
| 282 |  | PACKET  *pl = NULL, *plend; | 
| 283 | + | long    fsiz; | 
| 284 | + | int     pksiz; | 
| 285 |  | register PACKET *p; | 
| 286 |  | time_t  t; | 
| 287 | < | long    l; | 
| 288 | < |  | 
| 289 | < | if (outdev != NULL)             /* check display */ | 
| 287 | > | /* check display */ | 
| 288 | > | if (nprocs <= 0) | 
| 289 | > | idle = 1; | 
| 290 | > | if (outdev != NULL) { | 
| 291 |  | if (!disp_check(idle)) | 
| 292 | < | return(0); | 
| 293 | < | /* display only? */ | 
| 294 | < | if (ncprocs <= 0) | 
| 295 | < | return(1); | 
| 296 | < | /* check file size */ | 
| 297 | < | if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) { | 
| 292 | > | return(0);      /* quit request */ | 
| 293 | > | if (nprocs <= 0) | 
| 294 | > | return(1); | 
| 295 | > | } else if (idle) | 
| 296 | > | return(0);              /* all done */ | 
| 297 | > | fsiz = hdfilen(hdlist[0]->fd);  /* check file size */ | 
| 298 | > | if (maxdisk > 0 && fsiz >= maxdisk) { | 
| 299 |  | error(WARNING, "file limit exceeded"); | 
| 300 | < | return(0); | 
| 300 | > | done_rtrace(); | 
| 301 | > | return(1);      /* comes back */ | 
| 302 |  | } | 
| 303 | < | /* check time */ | 
| 304 | < | if (endtime > 0 || reporttime > 0) | 
| 305 | < | t = time(NULL); | 
| 303 | > | #if FRAGWARN | 
| 304 | > | if (fsiz >= nextfragwarn && | 
| 305 | > | (fsiz-hdfiluse(hdlist[0]->fd,0))/(fsiz/100) > FRAGWARN) { | 
| 306 | > | sprintf(errmsg, "holodeck file fragmentation is %.0f%%", | 
| 307 | > | 100.*(fsiz-hdfiluse(hdlist[0]->fd,1))/fsiz); | 
| 308 | > | error(WARNING, errmsg); | 
| 309 | > | nextfragwarn = fsiz + (fsiz>>2);        /* decent interval */ | 
| 310 | > | } | 
| 311 | > | #endif | 
| 312 | > | t = time(NULL);                 /* check time */ | 
| 313 |  | if (endtime > 0 && t >= endtime) { | 
| 314 |  | error(WARNING, "time limit exceeded"); | 
| 315 | < | return(0); | 
| 315 | > | done_rtrace(); | 
| 316 | > | return(1);      /* comes back */ | 
| 317 |  | } | 
| 318 |  | if (reporttime > 0 && t >= reporttime) | 
| 319 |  | report(t); | 
| 320 | < | /* get packets to process */ | 
| 320 | > | /* figure out good packet size */ | 
| 321 | > | #if MAXQTIME | 
| 322 | > | pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L)); | 
| 323 | > | if (pksiz < 1) | 
| 324 | > | pksiz = 1; | 
| 325 | > | else if (pksiz > RPACKSIZ) | 
| 326 | > | #endif | 
| 327 | > | pksiz = RPACKSIZ; | 
| 328 | > | idle = 0;                       /* get packets to process */ | 
| 329 |  | while (freepacks != NULL) { | 
| 330 |  | p = freepacks; freepacks = p->next; p->next = NULL; | 
| 331 | < | if (!next_packet(p)) { | 
| 331 | > | if (!next_packet(p, pksiz)) { | 
| 332 |  | p->next = freepacks; freepacks = p; | 
| 333 | + | idle = 1; | 
| 334 |  | break; | 
| 335 |  | } | 
| 336 |  | if (pl == NULL) pl = p; | 
| 337 |  | else plend->next = p; | 
| 338 |  | plend = p; | 
| 339 |  | } | 
| 340 | < | idle = pl == NULL && freepacks != NULL; | 
| 298 | < | /* are we out of stuff to do? */ | 
| 299 | < | if (idle && outdev == NULL) | 
| 300 | < | return(0); | 
| 301 | < | /* else process packets */ | 
| 340 | > | /* process packets */ | 
| 341 |  | done_packets(do_packets(pl)); | 
| 342 |  | return(1);                      /* and continue */ | 
| 343 |  | } | 
| 344 |  |  | 
| 345 |  |  | 
| 307 | – | report(t)                       /* report progress so far */ | 
| 308 | – | time_t  t; | 
| 309 | – | { | 
| 310 | – | if (t == 0) | 
| 311 | – | t = time(NULL); | 
| 312 | – | fprintf(stderr, "%s: %ld packets (%ld rays) done after %.2f hours\n", | 
| 313 | – | progname, npacksdone, nraysdone, (t-starttime)/3600.); | 
| 314 | – | fflush(stderr); | 
| 315 | – | if (vdef(REPORT)) | 
| 316 | – | reporttime = t + (time_t)(vflt(REPORT)*60.+.5); | 
| 317 | – | } | 
| 318 | – |  | 
| 319 | – |  | 
| 346 |  | setdefaults(gp)                 /* set default values */ | 
| 347 |  | register HDGRID *gp; | 
| 348 |  | { | 
| 349 |  | extern char     *atos(); | 
| 350 |  | register int    i; | 
| 351 | < | double  len[3], maxlen, d; | 
| 351 | > | int     n; | 
| 352 | > | double  len[3], d; | 
| 353 |  | char    buf[64]; | 
| 354 |  |  | 
| 355 |  | if (!vdef(SECTION)) { | 
| 356 |  | sprintf(errmsg, "%s must be defined", vnam(SECTION)); | 
| 357 |  | error(USER, errmsg); | 
| 358 |  | } | 
| 332 | – | if (vdef(SECTION) > 1) { | 
| 333 | – | sprintf(errmsg, "ignoring all but first %s", vnam(SECTION)); | 
| 334 | – | error(WARNING, errmsg); | 
| 335 | – | } | 
| 336 | – | if (sscanf(vval(SECTION), | 
| 337 | – | "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf", | 
| 338 | – | &gp->orig[0], &gp->orig[1], &gp->orig[2], | 
| 339 | – | &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2], | 
| 340 | – | &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2], | 
| 341 | – | &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2]) != 12) | 
| 342 | – | badvalue(SECTION); | 
| 343 | – | maxlen = 0.; | 
| 344 | – | for (i = 0; i < 3; i++) | 
| 345 | – | if ((len[i] = VLEN(gp->xv[i])) > maxlen) | 
| 346 | – | maxlen = len[i]; | 
| 347 | – | if (!vdef(GRID)) { | 
| 348 | – | sprintf(buf, "%.4f", maxlen/8.); | 
| 349 | – | vval(GRID) = savqstr(buf); | 
| 350 | – | vdef(GRID)++; | 
| 351 | – | } | 
| 352 | – | if ((d = vflt(GRID)) <= FTINY) | 
| 353 | – | badvalue(GRID); | 
| 354 | – | for (i = 0; i < 3; i++) | 
| 355 | – | gp->grid[i] = len[i]/d + (1.-FTINY); | 
| 356 | – | if (!vdef(EXPOSURE)) { | 
| 357 | – | sprintf(errmsg, "%s must be defined", vnam(EXPOSURE)); | 
| 358 | – | error(USER, errmsg); | 
| 359 | – | } | 
| 360 | – | expval = vval(EXPOSURE)[0] == '-' || vval(EXPOSURE)[0] == '+' ? | 
| 361 | – | pow(2., vflt(EXPOSURE)) : vflt(EXPOSURE); | 
| 359 |  | if (!vdef(OCTREE)) { | 
| 360 |  | if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL) | 
| 361 |  | error(SYSTEM, "out of memory"); | 
| 362 |  | sprintf(vval(OCTREE), "%s.oct", froot); | 
| 363 |  | vdef(OCTREE)++; | 
| 364 |  | } | 
| 365 | < | if (!vdef(OBSTRUCTIONS)) { | 
| 366 | < | vval(OBSTRUCTIONS) = "T"; | 
| 367 | < | vdef(OBSTRUCTIONS)++; | 
| 365 | > | if (!vdef(VDIST)) { | 
| 366 | > | vval(VDIST) = "F"; | 
| 367 | > | vdef(VDIST)++; | 
| 368 |  | } | 
| 372 | – | if (!vdef(OCCUPANCY)) { | 
| 373 | – | vval(OCCUPANCY) = "U"; | 
| 374 | – | vdef(OCCUPANCY)++; | 
| 375 | – | } | 
| 369 |  | /* append rendering options */ | 
| 370 |  | if (vdef(RENDER)) | 
| 371 |  | rtargc += wordstring(rtargv+rtargc, vval(RENDER)); | 
| 372 | + |  | 
| 373 | + | if (gp == NULL)         /* already initialized? */ | 
| 374 | + | return; | 
| 375 | + | /* set grid parameters */ | 
| 376 | + | for (n = 0; n < vdef(SECTION); n++, gp++) { | 
| 377 | + | gp->grid[0] = gp->grid[1] = gp->grid[2] = 0; | 
| 378 | + | if (sscanf(nvalue(SECTION, n), | 
| 379 | + | "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd", | 
| 380 | + | &gp->orig[0], &gp->orig[1], &gp->orig[2], | 
| 381 | + | &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2], | 
| 382 | + | &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2], | 
| 383 | + | &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2], | 
| 384 | + | &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12) | 
| 385 | + | badvalue(SECTION); | 
| 386 | + | for (i = 0; i < 3; i++) | 
| 387 | + | len[i] = VLEN(gp->xv[i]); | 
| 388 | + | if (!vdef(GRID)) { | 
| 389 | + | d = 2/2e5*( len[0]*len[0]*(len[1]*len[1] + | 
| 390 | + | len[2]*len[2] + 4*len[1]*len[2]) | 
| 391 | + | + len[1]*len[1]*len[2]*(len[2] + 4*len[0]) | 
| 392 | + | + 4*len[0]*len[1]*len[2]*len[2] ); | 
| 393 | + | d = sqrt(sqrt(d)); | 
| 394 | + | } else if ((d = vflt(GRID)) <= FTINY) | 
| 395 | + | badvalue(GRID); | 
| 396 | + | for (i = 0; i < 3; i++) | 
| 397 | + | if (gp->grid[i] <= 0) | 
| 398 | + | gp->grid[i] = len[i]/d + (1.-FTINY); | 
| 399 | + | } | 
| 400 |  | } | 
| 401 |  |  | 
| 402 |  |  | 
| 404 |  | HDGRID  *gp; | 
| 405 |  | { | 
| 406 |  | extern char     VersionID[]; | 
| 407 | < | long    endloc = 0; | 
| 407 | > | int4    lastloc, nextloc; | 
| 408 | > | int     n; | 
| 409 |  | int     fd; | 
| 410 |  | FILE    *fp; | 
| 411 |  | /* open & truncate file */ | 
| 419 |  | printvars(fp); | 
| 420 |  | fputformat(HOLOFMT, fp); | 
| 421 |  | fputc('\n', fp); | 
| 422 | < | putw(HOLOMAGIC, fp);            /* put magic number & terminus */ | 
| 401 | < | fwrite(&endloc, sizeof(long), 1, fp); | 
| 422 | > | putw(HOLOMAGIC, fp);            /* put magic number */ | 
| 423 |  | fd = dup(fileno(fp)); | 
| 424 |  | fclose(fp);                     /* flush and close stdio stream */ | 
| 425 | < | hdinit(fd, gp);                 /* allocate and initialize index */ | 
| 425 | > | lastloc = lseek(fd, 0L, 2); | 
| 426 | > | for (n = vdef(SECTION); n--; gp++) {    /* initialize each section */ | 
| 427 | > | nextloc = 0L; | 
| 428 | > | write(fd, (char *)&nextloc, sizeof(nextloc)); | 
| 429 | > | hdinit(fd, gp);                 /* writes beam index */ | 
| 430 | > | if (!n) | 
| 431 | > | break; | 
| 432 | > | nextloc = hdfilen(fd);          /* write section pointer */ | 
| 433 | > | if (lseek(fd, (long)lastloc, 0) < 0) | 
| 434 | > | error(SYSTEM, | 
| 435 | > | "cannot seek on holodeck file in creatholo"); | 
| 436 | > | write(fd, (char *)&nextloc, sizeof(nextloc)); | 
| 437 | > | lseek(fd, (long)(lastloc=nextloc), 0); | 
| 438 | > | } | 
| 439 |  | } | 
| 440 |  |  | 
| 441 |  |  | 
| 442 | + | int | 
| 443 |  | headline(s)                     /* process information header line */ | 
| 444 |  | char    *s; | 
| 445 |  | { | 
| 453 |  | HOLOFMT, hdkfile, FMTSTR, fmt); | 
| 454 |  | error(USER, errmsg); | 
| 455 |  | } | 
| 456 | < | return; | 
| 456 | > | return(0); | 
| 457 |  | } | 
| 458 |  | for (cp = s; *cp; cp++)         /* take off any comments */ | 
| 459 |  | if (*cp == '#') { | 
| 461 |  | break; | 
| 462 |  | } | 
| 463 |  | setvariable(s, matchvar);       /* don't flag errors */ | 
| 464 | + | return(0); | 
| 465 |  | } | 
| 466 |  |  | 
| 467 |  |  | 
| 470 |  | extern long     ftell(); | 
| 471 |  | FILE    *fp; | 
| 472 |  | int     fd; | 
| 473 | < | long    fpos; | 
| 473 | > | int     n; | 
| 474 | > | int4    nextloc; | 
| 475 |  | /* open holodeck file */ | 
| 476 |  | if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) { | 
| 477 | < | sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile, | 
| 478 | < | ncprocs>0 ? "appending" : "reading"); | 
| 477 | > | sprintf(errmsg, "cannot %s \"%s\"", | 
| 478 | > | ncprocs>0 ? "append" : "read", hdkfile); | 
| 479 |  | error(SYSTEM, errmsg); | 
| 480 |  | } | 
| 481 |  | /* load variables from header */ | 
| 486 |  | hdkfile); | 
| 487 |  | error(USER, errmsg); | 
| 488 |  | } | 
| 489 | < | fread(&fpos, sizeof(long), 1, fp); | 
| 453 | < | if (fpos != 0) | 
| 454 | < | error(WARNING, "ignoring multiple sections in holodeck file"); | 
| 455 | < | fpos = ftell(fp);                       /* get stdio position */ | 
| 489 | > | nextloc = ftell(fp);                    /* get stdio position */ | 
| 490 |  | fd = dup(fileno(fp)); | 
| 491 |  | fclose(fp);                             /* done with stdio */ | 
| 492 | < | lseek(fd, fpos, 0);                     /* align system file pointer */ | 
| 493 | < | hdinit(fd, NULL);                       /* allocate and load index */ | 
| 492 | > | for (n = 0; nextloc > 0L; n++) {        /* initialize each section */ | 
| 493 | > | lseek(fd, (long)nextloc, 0); | 
| 494 | > | read(fd, (char *)&nextloc, sizeof(nextloc)); | 
| 495 | > | hdinit(fd, NULL); | 
| 496 | > | } | 
| 497 | > | if (n != vdef(SECTION)) { | 
| 498 | > | sprintf(errmsg, "number of sections does not match %s setting", | 
| 499 | > | vnam(SECTION)); | 
| 500 | > | error(WARNING, errmsg); | 
| 501 | > | } | 
| 502 |  | } | 
| 503 |  |  | 
| 504 |  |  | 
| 505 |  | done_packets(pl)                /* handle finished packets */ | 
| 506 |  | PACKET  *pl; | 
| 507 |  | { | 
| 508 | < | static int      nunflushed = 0; | 
| 508 | > | static int      n2flush = 0; | 
| 509 |  | register PACKET *p; | 
| 510 |  |  | 
| 511 |  | while (pl != NULL) { | 
| 516 |  | p->nr*sizeof(RAYVAL)); | 
| 517 |  | if (outdev != NULL)     /* display it */ | 
| 518 |  | disp_packet((PACKHEAD *)p); | 
| 519 | < | else | 
| 520 | < | nunflushed += p->nr; | 
| 519 | > | if (hdcachesize <= 0) { | 
| 520 | > | #if RTFLUSH | 
| 521 | > | if (outdev == NULL) | 
| 522 | > | hdfreebeam(hdlist[p->hd], p->bi); | 
| 523 | > | #endif | 
| 524 | > | n2flush++; | 
| 525 | > | } | 
| 526 |  | nraysdone += p->nr; | 
| 527 |  | npacksdone++; | 
| 528 | + | p->nr = 0; | 
| 529 |  | } | 
| 530 | < | p->nr = 0;                      /* push onto free list */ | 
| 483 | < | p->next = freepacks; | 
| 530 | > | p->next = freepacks;            /* push onto free list */ | 
| 531 |  | freepacks = p; | 
| 532 |  | } | 
| 533 | < | if (nunflushed >= 256*RPACKSIZ) { | 
| 534 | < | hdflush(NULL);                  /* flush holodeck buffers */ | 
| 535 | < | nunflushed = 0; | 
| 533 | > | #if MAXQTIME | 
| 534 | > | if (n2flush > 300/MAXQTIME*totqlen) { | 
| 535 | > | #else | 
| 536 | > | if (n2flush > 50*totqlen) { | 
| 537 | > | #endif | 
| 538 | > | #if RTFLUSH | 
| 539 | > | hdsync(NULL, outdev!=NULL);     /* sync beams & directories */ | 
| 540 | > | #else | 
| 541 | > | if (outdev != NULL) | 
| 542 | > | hdsync(NULL, 1); | 
| 543 | > | else | 
| 544 | > | hdflush(NULL); | 
| 545 | > | #endif | 
| 546 | > | n2flush = 0; | 
| 547 |  | } | 
| 548 |  | } | 
| 549 |  |  | 
| 550 |  |  | 
| 493 | – | getradfile(rfargs)              /* run rad and get needed variables */ | 
| 494 | – | char    *rfargs; | 
| 495 | – | { | 
| 496 | – | static short    mvar[] = {OCTREE,EXPOSURE,-1}; | 
| 497 | – | static char     tf1[] = TEMPLATE; | 
| 498 | – | char    tf2[64]; | 
| 499 | – | char    combuf[256]; | 
| 500 | – | char    *pippt; | 
| 501 | – | register int    i; | 
| 502 | – | register char   *cp; | 
| 503 | – | /* create rad command */ | 
| 504 | – | mktemp(tf1); | 
| 505 | – | sprintf(tf2, "%s.rif", tf1); | 
| 506 | – | sprintf(combuf, | 
| 507 | – | "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH", | 
| 508 | – | rfargs, tf1); | 
| 509 | – | cp = combuf; | 
| 510 | – | while (*cp){ | 
| 511 | – | if (*cp == '|') pippt = cp; | 
| 512 | – | cp++; | 
| 513 | – | }                               /* match unset variables */ | 
| 514 | – | for (i = 0; mvar[i] >= 0; i++) | 
| 515 | – | if (!vdef(mvar[i])) { | 
| 516 | – | *cp++ = '|'; | 
| 517 | – | strcpy(cp, vnam(mvar[i])); | 
| 518 | – | while (*cp) cp++; | 
| 519 | – | pippt = NULL; | 
| 520 | – | } | 
| 521 | – | if (pippt != NULL) | 
| 522 | – | strcpy(pippt, "> /dev/null");   /* nothing to match */ | 
| 523 | – | else | 
| 524 | – | sprintf(cp, ")[ \t]*=' > %s", tf2); | 
| 525 | – | if (system(combuf)) { | 
| 526 | – | error(SYSTEM, "cannot execute rad command"); | 
| 527 | – | unlink(tf2);                    /* clean up */ | 
| 528 | – | unlink(tf1); | 
| 529 | – | quit(1); | 
| 530 | – | } | 
| 531 | – | if (pippt == NULL) { | 
| 532 | – | loadvars(tf2);                  /* load variables */ | 
| 533 | – | unlink(tf2); | 
| 534 | – | } | 
| 535 | – | rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */ | 
| 536 | – | unlink(tf1);                    /* clean up */ | 
| 537 | – | } | 
| 538 | – |  | 
| 539 | – |  | 
| 551 |  | rootname(rn, fn)                /* remove tail from end of fn */ | 
| 552 |  | register char   *rn, *fn; | 
| 553 |  | { | 
| 554 |  | char    *tp, *dp; | 
| 555 |  |  | 
| 556 |  | for (tp = NULL, dp = rn; *rn = *fn++; rn++) | 
| 557 | < | if (ISDIRSEP(*rn)) | 
| 557 | > | if (*rn == '/') | 
| 558 |  | dp = rn; | 
| 559 |  | else if (*rn == '.') | 
| 560 |  | tp = rn; | 
| 590 |  | } | 
| 591 |  |  | 
| 592 |  |  | 
| 582 | – | wputs(s)                        /* put warning string to stderr */ | 
| 583 | – | char    *s; | 
| 584 | – | { | 
| 585 | – | if (!nowarn) | 
| 586 | – | eputs(s); | 
| 587 | – | } | 
| 588 | – |  | 
| 589 | – |  | 
| 593 |  | quit(ec)                        /* exit program gracefully */ | 
| 594 |  | int     ec; | 
| 595 |  | { | 
| 596 |  | int     status = 0; | 
| 597 |  |  | 
| 598 | < | if (hdlist[0] != NULL) {        /* flush holodeck */ | 
| 599 | < | if (ncprocs > 0) { | 
| 600 | < | done_packets(flush_queue()); | 
| 601 | < | status = end_rtrace();  /* close rtrace */ | 
| 602 | < | hdflush(NULL); | 
| 603 | < | if (vdef(REPORT)) { | 
| 604 | < | long    fsiz, fuse; | 
| 605 | < | report(0); | 
| 603 | < | fsiz = hdfilen(hdlist[0]->fd); | 
| 604 | < | fuse = hdfiluse(hdlist[0]->fd, 1); | 
| 605 | < | fprintf(stderr, | 
| 598 | > | if (hdlist[0] != NULL) {        /* close holodeck */ | 
| 599 | > | if (nprocs > 0) | 
| 600 | > | status = done_rtrace();         /* calls hdsync() */ | 
| 601 | > | if (ncprocs > 0 && vdef(REPORT)) { | 
| 602 | > | long    fsiz, fuse; | 
| 603 | > | fsiz = hdfilen(hdlist[0]->fd); | 
| 604 | > | fuse = hdfiluse(hdlist[0]->fd, 1); | 
| 605 | > | fprintf(stderr, | 
| 606 |  | "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n", | 
| 607 | < | hdkfile, fsiz/(1024.*1024.), | 
| 608 | < | 100.*(fsiz-fuse)/fsiz); | 
| 609 | < | } | 
| 610 | < | } else | 
| 611 | < | hdflush(NULL); | 
| 607 | > | hdkfile, fsiz/(1024.*1024.), | 
| 608 | > | 100.*(fsiz-fuse)/fsiz); | 
| 609 | > | } | 
| 610 |  | } | 
| 611 |  | if (orig_mode >= 0)             /* reset holodeck access mode */ | 
| 612 |  | fchmod(hdlist[0]->fd, orig_mode); |