ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
(Generate patch)

Comparing ray/src/hd/rholo.c (file contents):
Revision 3.8 by gregl, Mon Nov 10 18:05:24 1997 UTC vs.
Revision 3.61 by greg, Mon Oct 20 16:01:55 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1997 Silicon Graphics, Inc. */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ SGI";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   * Radiance holodeck generation controller
6   */
7  
8 + #include <signal.h>
9 + #include <sys/stat.h>
10 + #include <string.h>
11 +
12   #include "rholo.h"
13 < #include "paths.h"
14 < #include <sys/types.h>
13 > #include "platform.h"
14 > #include "random.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  
# Line 19 | Line 34 | VARIABLE       vv[] = RHVINIT;         /* variable-value pairs */
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  
24 int     nowarn = 0;             /* turn warnings off? */
25
26 double  expval = 1.;            /* global exposure value */
27
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 */
50  
51 < long    maxdisk;                /* maximum file space (bytes) */
51 > off_t   maxdisk;                /* maximum file space (bytes) */
52  
53   int     rtargc = 1;             /* rtrace command */
54   char    *rtargv[128] = {"rtrace", NULL};
55  
56 + int     orig_mode = -1;         /* original file mode (-1 if unchanged) */
57 +
58   long    nraysdone = 0L;         /* number of rays done */
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  
# Line 50 | Line 72 | main(argc, argv)
72   int     argc;
73   char    *argv[];
74   {
53        HDGRID  hdg;
75          int     i;
76 <        int     force = 0;
77 <                                                /* mark start time */
57 <        starttime = time(NULL);
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]) {
# Line 62 | Line 82 | char   *argv[];
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;
# Line 77 | Line 103 | char   *argv[];
103                  default:
104                          goto userr;
105                  }
80                                                /* do we have a job? */
81        if (outdev == NULL && ncprocs <= 0)
82                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();
110 <                if (vdef(RIF))                          /* load RIF if any */
111 <                        getradfile(vval(RIF));
112 <        }
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   }
156  
157  
158 + void
159 + onsig(signo)                            /* fatal signal */
160 + int  signo;
161 + {
162 +        static int  gotsig = 0;
163 +
164 +        if (gotsig > 1)                 /* we're going as fast as we can! */
165 +                return;
166 +        if (gotsig++) {                 /* two signals and we split */
167 +                hdsync(NULL, 0);        /* don't leave w/o saying goodbye */
168 +                _exit(signo);
169 +        }
170 +        alarm(300);                     /* allow 5 minutes to clean up */
171 +        eputs("signal - ");
172 +        eputs(sigerr[signo]);
173 +        eputs("\n");
174 +        quit(3);
175 + }
176 +
177 +
178 + sigdie(signo, msg)                      /* set fatal signal */
179 + int  signo;
180 + char  *msg;
181 + {
182 +        if (signal(signo, onsig) == SIG_IGN)
183 +                signal(signo, SIG_IGN);
184 +        sigerr[signo] = msg;
185 + }
186 +
187 +
188 + int
189 + resfmode(fd, mod)               /* restrict open file access mode */
190 + int     fd, mod;
191 + {
192 +        struct stat     stbuf;
193 +                                        /* get original mode */
194 +        if (fstat(fd, &stbuf) < 0)
195 +                error(SYSTEM, "cannot stat open holodeck file");
196 +        mod &= stbuf.st_mode;           /* always more restrictive */
197 +        if (mod == (stbuf.st_mode & 0777))
198 +                return(-1);             /* already set */
199 +                                        /* else change it */
200 +        if (fchmod(fd, mod) < 0) {
201 +                error(WARNING, "cannot change holodeck file access mode");
202 +                return(-1);
203 +        }
204 +        return(stbuf.st_mode);          /* return original mode */
205 + }
206 +
207 +
208   initrholo()                     /* get our holodeck running */
209   {
210          extern int      global_packet();
211          register int    i;
212 +                                                /* close holodeck on exec() */
213 +        fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC);
214  
215          if (outdev != NULL)                     /* open output device */
216                  disp_open(outdev);
# Line 136 | Line 218 | initrholo()                    /* get our holodeck running */
218                  init_global();
219                                                  /* record disk space limit */
220          if (!vdef(DISKSPACE))
221 <                maxdisk = 0;
221 >                maxdisk = ((off_t)1<<(sizeof(off_t)*8-2)) - 1024;
222          else
223 <                maxdisk = 1024.*1024.*vflt(DISKSPACE));
142 <                                                /* record end time */
143 <        if (!vdef(TIME) || vflt(TIME) <= FTINY)
144 <                endtime = 0;
145 <        else
146 <                endtime = starttime + vflt(TIME)*3600.;
223 >                maxdisk = 1024.*1024.*vflt(DISKSPACE);
224                                                  /* set up memory cache */
225          if (outdev == NULL)
226 <                hdcachesize = 0;        /* manual flushing */
226 >                hdcachesize = 0;                /* manual flushing */
227          else if (vdef(CACHE))
228                  hdcachesize = 1024.*1024.*vflt(CACHE);
229                                                  /* open report file */
# Line 155 | Line 232 | initrholo()                    /* get our holodeck running */
232                  if (*s && freopen(s, "a", stderr) == NULL)
233                          quit(2);
234          }
235 +                                                /* mark the starting time */
236 +        starttime = time(NULL);
237 +                                                /* compute end time */
238 +        if (!vdef(TIME) || vflt(TIME) <= FTINY)
239 +                endtime = 0;
240 +        else
241 +                endtime = starttime + vflt(TIME)*3600. + .5;
242                                                  /* start rtrace */
243          if (ncprocs > 0) {
244 <                i = start_rtrace();
244 >                totqlen = i = start_rtrace();
245                  if (i < 1)
246 <                        error(USER, "cannot start rtrace process");
246 >                        error(USER, "cannot start rtrace process(es)");
247                  if (vdef(REPORT)) {             /* make first report */
248                          printargs(rtargc, rtargv, stderr);
249                          report(0);
# Line 170 | Line 254 | initrholo()                    /* get our holodeck running */
254                          goto memerr;
255                  freepacks[--i].nr = 0;
256                  freepacks[i].next = NULL;
257 <                if (!vbool(OBSTRUCTIONS)) {
257 >                if (!vdef(OBSTRUCTIONS) || !vbool(OBSTRUCTIONS)) {
258                          freepacks[i].offset = (float *)bmalloc(
259                                          RPACKSIZ*sizeof(float)*(i+1) );
260                          if (freepacks[i].offset == NULL)
# Line 184 | Line 268 | initrholo()                    /* get our holodeck running */
268                          freepacks[i].next = &freepacks[i+1];
269                  }
270          }
271 +                                        /* set up signal handling */
272 +        sigdie(SIGINT, "Interrupt");
273 +        sigdie(SIGHUP, "Hangup");
274 +        sigdie(SIGTERM, "Terminate");
275 +        sigdie(SIGPIPE, "Broken pipe");
276 +        sigdie(SIGALRM, "Alarm clock");
277 + #ifdef  SIGXCPU
278 +        sigdie(SIGXCPU, "CPU limit exceeded");
279 +        sigdie(SIGXFSZ, "File size exceeded");
280 + #endif
281 +                                        /* protect holodeck file */
282 +        orig_mode = resfmode(hdlist[0]->fd, (ncprocs>0) & (force>=0) ? 0 : 0444);
283          return;
284   memerr:
285          error(SYSTEM, "out of memory in initrholo");
# Line 192 | Line 288 | memerr:
288  
289   rholo()                         /* holodeck main loop */
290   {
291 <        static int      idle = 1;
291 >        static long     nextfragwarn = 100*(1L<<20);
292 >        static int      idle = 0;
293          PACKET  *pl = NULL, *plend;
294 +        off_t   fsiz;
295 +        int     pksiz;
296          register PACKET *p;
297          time_t  t;
298 <        long    l;
299 <
300 <        if (outdev != NULL)             /* check display */
298 >                                        /* check display */
299 >        if (nprocs <= 0)
300 >                idle = 1;
301 >        if (outdev != NULL) {
302                  if (!disp_check(idle))
303 <                        return(0);
304 <                                        /* display only? */
305 <        if (ncprocs <= 0)
306 <                return(1);
307 <                                        /* check file size */
308 <        if (maxdisk > 0 && hdfiluse(hdlist[0]->fd,0)+hdmemuse(0) >= maxdisk) {
303 >                        return(0);      /* quit request */
304 >                if (nprocs <= 0)
305 >                        return(1);
306 >        } else if (idle)
307 >                return(0);              /* all done */
308 >        fsiz = hdfilen(hdlist[0]->fd);  /* check file size */
309 >        if (maxdisk > 0 && fsiz >= maxdisk) {
310                  error(WARNING, "file limit exceeded");
311 <                return(0);
311 >                done_rtrace();
312 >                return(1);      /* comes back */
313          }
314 <                                        /* check time */
315 <        if (endtime > 0 || reporttime > 0)
316 <                t = time(NULL);
314 > #if FRAGWARN
315 >        if (fsiz >= nextfragwarn &&
316 >                (fsiz-hdfiluse(hdlist[0]->fd,0))/(fsiz/100) > FRAGWARN) {
317 >                sprintf(errmsg, "holodeck file fragmentation is %.0f%%",
318 >                                100.*(fsiz-hdfiluse(hdlist[0]->fd,1))/fsiz);
319 >                error(WARNING, errmsg);
320 >                nextfragwarn = fsiz + (fsiz>>2);        /* decent interval */
321 >        }
322 > #endif
323 >        t = time(NULL);                 /* check time */
324          if (endtime > 0 && t >= endtime) {
325                  error(WARNING, "time limit exceeded");
326 <                return(0);
326 >                done_rtrace();
327 >                return(1);      /* comes back */
328          }
329          if (reporttime > 0 && t >= reporttime)
330                  report(t);
331 <                                        /* get packets to process */
331 >                                        /* figure out good packet size */
332 >        pksiz = RPACKSIZ;
333 > #if MAXQTIME
334 >        if (!chunkycmp) {
335 >                pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L));
336 >                if (pksiz < 1) pksiz = 1;
337 >                else if (pksiz > RPACKSIZ) pksiz = RPACKSIZ;
338 >        }
339 > #endif
340 >        idle = 0;                       /* get packets to process */
341          while (freepacks != NULL) {
342                  p = freepacks; freepacks = p->next; p->next = NULL;
343 <                if (!next_packet(p)) {
343 >                if (!next_packet(p, pksiz)) {
344                          p->next = freepacks; freepacks = p;
345 +                        idle = 1;
346                          break;
347                  }
348                  if (pl == NULL) pl = p;
349                  else plend->next = p;
350                  plend = p;
351          }
352 <        idle = pl == NULL && freepacks != NULL;
233 <                                        /* are we out of stuff to do? */
234 <        if (idle && outdev == NULL)
235 <                return(0);
236 <                                        /* else process packets */
352 >                                        /* process packets */
353          done_packets(do_packets(pl));
354          return(1);                      /* and continue */
355   }
356  
357  
242 report(t)                       /* report progress so far */
243 time_t  t;
244 {
245        if (t == 0)
246                t = time(NULL);
247        fprintf(stderr, "%s: %ld packets (%ld rays) done after %.2f hours\n",
248                        progname, npacksdone, nraysdone, (t-starttime)/3600.);
249        fflush(stderr);
250        if (vdef(REPORT))
251                reporttime = t + (time_t)(vflt(REPORT)*60.+.5);
252 }
253
254
358   setdefaults(gp)                 /* set default values */
359   register HDGRID *gp;
360   {
361          extern char     *atos();
362          register int    i;
363 <        double  len[3], maxlen, d;
363 >        int     n;
364 >        double  len[3], d;
365          char    buf[64];
366  
367          if (!vdef(SECTION)) {
368                  sprintf(errmsg, "%s must be defined", vnam(SECTION));
369                  error(USER, errmsg);
370          }
267        if (vdef(SECTION) > 1) {
268                sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
269                error(WARNING, errmsg);
270        }
271        if (sscanf(vval(SECTION),
272                        "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
273                        &gp->orig[0], &gp->orig[1], &gp->orig[2],
274                        &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
275                        &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
276                        &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2]) != 12)
277                badvalue(SECTION);
278        maxlen = 0.;
279        for (i = 0; i < 3; i++)
280                if ((len[i] = VLEN(gp->xv[i])) > maxlen)
281                        maxlen = len[i];
282        if (!vdef(GRID)) {
283                sprintf(buf, "%.4f", maxlen/8.);
284                vval(GRID) = savqstr(buf);
285                vdef(GRID)++;
286        }
287        if ((d = vflt(GRID)) <= FTINY)
288                badvalue(GRID);
289        for (i = 0; i < 3; i++)
290                gp->grid[i] = len[i]/d + (1.-FTINY);
291        if (!vdef(EXPOSURE)) {
292                sprintf(errmsg, "%s must be defined", vnam(EXPOSURE));
293                error(USER, errmsg);
294        }
295        expval = vval(EXPOSURE)[0] == '-' || vval(EXPOSURE)[0] == '+' ?
296                        pow(2., vflt(EXPOSURE)) : vflt(EXPOSURE);
371          if (!vdef(OCTREE)) {
372                  if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
373                          error(SYSTEM, "out of memory");
374                  sprintf(vval(OCTREE), "%s.oct", froot);
375                  vdef(OCTREE)++;
376          }
377 <        if (!vdef(OBSTRUCTIONS)) {
378 <                vval(OBSTRUCTIONS) = "T";
379 <                vdef(OBSTRUCTIONS)++;
377 >        if (!vdef(VDIST)) {
378 >                vval(VDIST) = "F";
379 >                vdef(VDIST)++;
380          }
307        if (!vdef(OCCUPANCY)) {
308                vval(OCCUPANCY) = "U";
309                vdef(OCCUPANCY)++;
310        }
381                                  /* append rendering options */
382          if (vdef(RENDER))
383                  rtargc += wordstring(rtargv+rtargc, vval(RENDER));
384 +        
385 +        if (gp == NULL)         /* already initialized? */
386 +                return;
387 +                                /* set grid parameters */
388 +        for (n = 0; n < vdef(SECTION); n++, gp++) {
389 +                gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
390 +                if (sscanf(nvalue(SECTION, n),
391 +                "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd",
392 +                                &gp->orig[0], &gp->orig[1], &gp->orig[2],
393 +                                &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
394 +                                &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
395 +                                &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
396 +                                &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
397 +                        badvalue(SECTION);
398 +                for (i = 0; i < 3; i++)
399 +                        len[i] = VLEN(gp->xv[i]);
400 +                if (!vdef(GRID)) {
401 +                        d = 2/2e5*( len[0]*len[0]*(len[1]*len[1] +
402 +                                        len[2]*len[2] + 4*len[1]*len[2])
403 +                                + len[1]*len[1]*len[2]*(len[2] + 4*len[0])
404 +                                + 4*len[0]*len[1]*len[2]*len[2] );
405 +                        d = sqrt(sqrt(d));
406 +                } else if ((d = vflt(GRID)) <= FTINY)
407 +                        badvalue(GRID);
408 +                for (i = 0; i < 3; i++)
409 +                        if (gp->grid[i] <= 0)
410 +                                gp->grid[i] = len[i]/d + (1.-FTINY);
411 +        }
412   }
413  
414  
415   creatholo(gp)                   /* create a holodeck output file */
416   HDGRID  *gp;
417   {
418 <        long    endloc = 0;
418 >        extern char     VersionID[];
419 >        int32   lastloc, nextloc;
420 >        int     n;
421 >        int     fd;
422          FILE    *fp;
423                                          /* open & truncate file */
424          if ((fp = fopen(hdkfile, "w+")) == NULL) {
# Line 326 | Line 427 | HDGRID *gp;
427          }
428                                          /* write information header */
429          newheader("RADIANCE", fp);
430 +        fprintf(fp, "SOFTWARE= %s\n", VersionID);
431          printvars(fp);
432          fputformat(HOLOFMT, fp);
433          fputc('\n', fp);
434 <        putw(HOLOMAGIC, fp);            /* put magic number & terminus */
435 <        fwrite(&endloc, sizeof(long), 1, fp);
436 <        fflush(fp);                     /* flush buffer */
437 <        hdinit(fileno(fp), gp);         /* allocate and initialize index */
438 <                        /* we're dropping fp here.... */
434 >        putw(HOLOMAGIC, fp);            /* put magic number */
435 >        fd = dup(fileno(fp));
436 >        fclose(fp);                     /* flush and close stdio stream */
437 >        lastloc = lseek(fd, (off_t)0, SEEK_END);
438 >        for (n = vdef(SECTION); n--; gp++) {    /* initialize each section */
439 >                nextloc = 0L;
440 >                write(fd, (char *)&nextloc, sizeof(nextloc));
441 >                hdinit(fd, gp);                 /* writes beam index */
442 >                if (!n)
443 >                        break;
444 >                nextloc = hdfilen(fd);          /* write section pointer */
445 >                if (lseek(fd, (off_t)lastloc, SEEK_SET) < 0)
446 >                        error(SYSTEM,
447 >                                "cannot seek on holodeck file in creatholo");
448 >                write(fd, (char *)&nextloc, sizeof(nextloc));
449 >                lseek(fd, (off_t)(lastloc=nextloc), SEEK_SET);
450 >        }
451   }
452  
453  
454 + int
455   headline(s)                     /* process information header line */
456   char    *s;
457   {
# Line 350 | Line 465 | char   *s;
465                                          HOLOFMT, hdkfile, FMTSTR, fmt);
466                          error(USER, errmsg);
467                  }
468 <                return;
468 >                return(0);
469          }
470          for (cp = s; *cp; cp++)         /* take off any comments */
471                  if (*cp == '#') {
# Line 358 | Line 473 | char   *s;
473                          break;
474                  }
475          setvariable(s, matchvar);       /* don't flag errors */
476 +        return(0);
477   }
478  
479  
480   loadholo()                      /* start loading a holodeck from fname */
481   {
482          FILE    *fp;
483 <        long    endloc;
484 <                                        /* open holodeck file */
485 <        if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) {
486 <                sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile,
487 <                                ncprocs>0 ? "appending" : "reading");
488 <                error(SYSTEM, errmsg);
483 >        int     fd;
484 >        int     n;
485 >        int32   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);
# Line 379 | Line 509 | loadholo()                     /* start loading a holodeck from fname */
509                                  hdkfile);
510                  error(USER, errmsg);
511          }
512 <        fread(&endloc, sizeof(long), 1, fp);
513 <        if (endloc != 0)
514 <                error(WARNING, "ignoring multiple sections in holodeck file");
515 <        fseek(fp, 0L, 1);                       /* align system file pointer */
516 <        hdinit(fileno(fp), NULL);               /* allocate and load index */
517 <                        /* we're dropping fp here.... */
512 >        nextloc = ftell(fp);                    /* get stdio position */
513 >        fd = dup(fileno(fp));
514 >        fclose(fp);                             /* done with stdio */
515 >        for (n = 0; nextloc > 0L; n++) {        /* initialize each section */
516 >                lseek(fd, (off_t)nextloc, SEEK_SET);
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  
528   done_packets(pl)                /* handle finished packets */
529   PACKET  *pl;
530   {
531 <        static int      nunflushed = 0;
531 >        static int      n2flush = 0;
532          register PACKET *p;
533  
534          while (pl != NULL) {
535                  p = pl; pl = p->next; p->next = NULL;
536                  if (p->nr > 0) {                /* add to holodeck */
537 <                        bcopy((char *)p->ra,
538 <                                (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
537 >                        memcpy( (void *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
538 >                                (void *)p->ra,
539                                  p->nr*sizeof(RAYVAL));
540                          if (outdev != NULL)     /* display it */
541                                  disp_packet((PACKHEAD *)p);
542 <                        else
543 <                                nunflushed += 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 */
411 <                p->next = freepacks;
548 >                p->next = freepacks;            /* push onto free list */
549                  freepacks = p;
550          }
551 <        if (nunflushed >= 256*RPACKSIZ) {
552 <                hdflush(NULL);                  /* flush holodeck buffers */
553 <                nunflushed = 0;
551 >        if (n2flush >= RTFLUSH) {
552 >                if (outdev != NULL)
553 >                        hdsync(NULL, 1);
554 >                else
555 >                        hdflush(NULL);
556 >                n2flush = 0;
557          }
558   }
559  
560  
421 getradfile(rfargs)              /* run rad and get needed variables */
422 char    *rfargs;
423 {
424        static short    mvar[] = {VIEW,OCTREE,EXPOSURE,-1};
425        static char     tf1[] = TEMPLATE;
426        char    tf2[64];
427        char    combuf[256];
428        char    *pippt;
429        register int    i;
430        register char   *cp;
431                                        /* create rad command */
432        mktemp(tf1);
433        sprintf(tf2, "%s.rif", tf1);
434        sprintf(combuf,
435                "rad -v 0 -s -e -w %s OPTFILE=%s | egrep '^[ \t]*(NOMATCH",
436                        rfargs, tf1);
437        cp = combuf;
438        while (*cp){
439                if (*cp == '|') pippt = cp;
440                cp++;
441        }                               /* match unset variables */
442        for (i = 0; mvar[i] >= 0; i++)
443                if (!vdef(mvar[i])) {
444                        *cp++ = '|';
445                        strcpy(cp, vnam(mvar[i]));
446                        while (*cp) cp++;
447                        pippt = NULL;
448                }
449        if (pippt != NULL)
450                strcpy(pippt, "> /dev/null");   /* nothing to match */
451        else
452                sprintf(cp, ")[ \t]*=' > %s", tf2);
453        if (system(combuf)) {
454                error(SYSTEM, "cannot execute rad command");
455                unlink(tf2);                    /* clean up */
456                unlink(tf1);
457                quit(1);
458        }
459        if (pippt == NULL) {
460                loadvars(tf2);                  /* load variables */
461                unlink(tf2);
462        }
463        rtargc += wordfile(rtargv+rtargc, tf1); /* get rtrace options */
464        unlink(tf1);                    /* clean up */
465 }
466
467
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))
566 >        for (tp = NULL, dp = rn; (*rn = *fn++); rn++) {
567 >                if (*rn == '/')
568                          dp = rn;
569                  else if (*rn == '.')
570                          tp = rn;
571 +        }
572          if (tp != NULL && tp > dp)
573                  *tp = '\0';
574   }
# Line 488 | Line 582 | int    vc;
582   }
583  
584  
585 + void
586   eputs(s)                        /* put error message to stderr */
587   register char  *s;
588   {
# Line 507 | Line 602 | register char  *s;
602   }
603  
604  
605 < wputs(s)                        /* put warning string to stderr */
511 < char    *s;
512 < {
513 <        if (!nowarn)
514 <                eputs(s);
515 < }
516 <
517 <
605 > void
606   quit(ec)                        /* exit program gracefully */
607   int     ec;
608   {
609          int     status = 0;
610  
611 <        if (outdev != NULL)             /* close display */
612 <                disp_close();
613 <        if (hdlist[0] != NULL) {        /* flush holodeck */
614 <                if (ncprocs > 0) {
615 <                        done_packets(flush_queue());
616 <                        status = end_rtrace();  /* close rtrace */
617 <                        hdflush(NULL);
618 <                        if (vdef(REPORT)) {
531 <                                long    fsiz, fuse;
532 <                                report(0);
533 <                                fsiz = lseek(hdlist[0]->fd, 0L, 2);
534 <                                fuse = hdfiluse(hdlist[0]->fd, 1);
535 <                                fprintf(stderr,
611 >        if (hdlist[0] != NULL) {        /* close holodeck */
612 >                if (nprocs > 0)
613 >                        status = done_rtrace();         /* calls hdsync() */
614 >                if ((ncprocs > 0) & (force >= 0) && vdef(REPORT)) {
615 >                        off_t   fsiz, fuse;
616 >                        fsiz = hdfilen(hdlist[0]->fd);
617 >                        fuse = hdfiluse(hdlist[0]->fd, 1);
618 >                        fprintf(stderr,
619                          "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
620 <                                                hdkfile, fsiz/(1024.*1024.),
621 <                                                100.*(fsiz-fuse)/fsiz);
622 <                        }
540 <                } else
541 <                        hdflush(NULL);
620 >                                        hdkfile, fsiz/(1024.*1024.),
621 >                                        100.*(fsiz-fuse)/fsiz);
622 >                }
623          }
624 +        if (orig_mode >= 0)             /* reset holodeck access mode */
625 +                fchmod(hdlist[0]->fd, orig_mode);
626 +        if (outdev != NULL)             /* close display */
627 +                disp_close();
628          exit(ec ? ec : status);         /* exit */
629   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines