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