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.3 by gregl, Fri Oct 31 15:49:23 1997 UTC vs.
Revision 3.62 by schorsch, Tue Oct 21 19:19:28 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 + 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 48 | Line 72 | main(argc, argv)
72   int     argc;
73   char    *argv[];
74   {
51        HDGRID  hdg;
75          int     i;
76 <        int     force = 0;
77 <                                                /* mark start time */
55 <        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 60 | 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 75 | Line 103 | char   *argv[];
103                  default:
104                          goto userr;
105                  }
78                                                /* do we have a job? */
79        if (outdev == NULL && ncprocs <= 0)
80                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();
108 <                if (vdef(RIF))                          /* load RIF if any */
109 <                        getradfile(vval(RIF));
110 <        }
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 <                                                /* check output device */
213 <        if (outdev != NULL)
214 <                open_display(outdev);
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);
217          else if (ncprocs > 0)                   /* else use global ray feed */
218                  init_global();
219 <                                                /* record end time */
220 <        if (!vdef(TIME) || vflt(TIME) <= FTINY)
221 <                endtime = 0;
219 >                                                /* record disk space limit */
220 >        if (!vdef(DISKSPACE))
221 >                maxdisk = ((off_t)1<<(sizeof(off_t)*8-2)) - 1024;
222          else
223 <                endtime = starttime + vflt(TIME)*3600.;
223 >                maxdisk = 1024.*1024.*vflt(DISKSPACE);
224                                                  /* set up memory cache */
225 <        hdcachesize = 1024.*1024.*vflt(CACHE);
225 >        if (outdev == NULL)
226 >                hdcachesize = 0;                /* manual flushing */
227 >        else if (vdef(CACHE))
228 >                hdcachesize = 1024.*1024.*vflt(CACHE);
229                                                  /* open report file */
230          if (vdef(REPORT)) {
231                  register char   *s = sskip2(vval(REPORT), 1);
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 160 | 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 174 | 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(SIGTERM, "Terminate");
274 + #ifdef SIGHUP
275 +        sigdie(SIGHUP, "Hangup");
276 + #endif
277 + #ifdef SIGPIPE
278 +        sigdie(SIGPIPE, "Broken pipe");
279 + #endif
280 + #ifdef SIGALRM
281 +        sigdie(SIGALRM, "Alarm clock");
282 + #endif
283 + #ifdef  SIGXCPU
284 +        sigdie(SIGXCPU, "CPU limit exceeded");
285 + #endif
286 + #ifdef  SIGXFSZ
287 +        sigdie(SIGXFSZ, "File size exceeded");
288 + #endif
289 +                                        /* protect holodeck file */
290 +        orig_mode = resfmode(hdlist[0]->fd, (ncprocs>0) & (force>=0) ? 0 : 0444);
291          return;
292   memerr:
293          error(SYSTEM, "out of memory in initrholo");
# Line 182 | Line 296 | memerr:
296  
297   rholo()                         /* holodeck main loop */
298   {
299 <        static int      idle = 1;
299 >        static long     nextfragwarn = 100*(1L<<20);
300 >        static int      idle = 0;
301          PACKET  *pl = NULL, *plend;
302 +        off_t   fsiz;
303 +        int     pksiz;
304          register PACKET *p;
305          time_t  t;
189        long    l;
306                                          /* check display */
307 <        if (outdev != NULL && !disp_check(idle))
308 <                return(0);
309 <                                        /* display only? */
310 <        if (ncprocs <= 0)
311 <                return(1);
312 <                                        /* check file size */
313 <        if ((l = 1024.*1024.*vflt(DISKSPACE)) > 0 &&
314 <                        hdfiluse(hdlist[0]->fd, 0) + hdmemuse(0) >= l)
315 <                return(0);
316 <                                        /* check time */
317 <        if (endtime > 0 || reporttime > 0)
318 <                t = time(NULL);
319 <        if (endtime > 0 && t >= endtime)
320 <                return(0);
307 >        if (nprocs <= 0)
308 >                idle = 1;
309 >        if (outdev != NULL) {
310 >                if (!disp_check(idle))
311 >                        return(0);      /* quit request */
312 >                if (nprocs <= 0)
313 >                        return(1);
314 >        } else if (idle)
315 >                return(0);              /* all done */
316 >        fsiz = hdfilen(hdlist[0]->fd);  /* check file size */
317 >        if (maxdisk > 0 && fsiz >= maxdisk) {
318 >                error(WARNING, "file limit exceeded");
319 >                done_rtrace();
320 >                return(1);      /* comes back */
321 >        }
322 > #if FRAGWARN
323 >        if (fsiz >= nextfragwarn &&
324 >                (fsiz-hdfiluse(hdlist[0]->fd,0))/(fsiz/100) > FRAGWARN) {
325 >                sprintf(errmsg, "holodeck file fragmentation is %.0f%%",
326 >                                100.*(fsiz-hdfiluse(hdlist[0]->fd,1))/fsiz);
327 >                error(WARNING, errmsg);
328 >                nextfragwarn = fsiz + (fsiz>>2);        /* decent interval */
329 >        }
330 > #endif
331 >        t = time(NULL);                 /* check time */
332 >        if (endtime > 0 && t >= endtime) {
333 >                error(WARNING, "time limit exceeded");
334 >                done_rtrace();
335 >                return(1);      /* comes back */
336 >        }
337          if (reporttime > 0 && t >= reporttime)
338                  report(t);
339 <                                        /* get packets to process */
339 >                                        /* figure out good packet size */
340 >        pksiz = RPACKSIZ;
341 > #if MAXQTIME
342 >        if (!chunkycmp) {
343 >                pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L));
344 >                if (pksiz < 1) pksiz = 1;
345 >                else if (pksiz > RPACKSIZ) pksiz = RPACKSIZ;
346 >        }
347 > #endif
348 >        idle = 0;                       /* get packets to process */
349          while (freepacks != NULL) {
350                  p = freepacks; freepacks = p->next; p->next = NULL;
351 <                if (!next_packet(p)) {
351 >                if (!next_packet(p, pksiz)) {
352                          p->next = freepacks; freepacks = p;
353 +                        idle = 1;
354                          break;
355                  }
356                  if (pl == NULL) pl = p;
357                  else plend->next = p;
358                  plend = p;
359          }
360 <        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 */
360 >                                        /* process packets */
361          done_packets(do_packets(pl));
362          return(1);                      /* and continue */
363   }
364  
365  
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
366   setdefaults(gp)                 /* set default values */
367   register HDGRID *gp;
368   {
369          extern char     *atos();
370          register int    i;
371 <        double  len[3], maxlen, d;
371 >        int     n;
372 >        double  len[3], d;
373          char    buf[64];
374  
375          if (!vdef(SECTION)) {
376                  sprintf(errmsg, "%s must be defined", vnam(SECTION));
377                  error(USER, errmsg);
378          }
253        if (vdef(SECTION) > 1) {
254                sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
255                error(WARNING, errmsg);
256        }
257        if (sscanf(vval(SECTION),
258                        "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
259                        &gp->orig[0], &gp->orig[1], &gp->orig[2],
260                        &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
261                        &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
262                        &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2]) != 12)
263                badvalue(SECTION);
264        maxlen = 0.;
265        for (i = 0; i < 3; i++)
266                if ((len[i] = VLEN(gp->xv[i])) > maxlen)
267                        maxlen = len[i];
268        if (!vdef(GRID)) {
269                sprintf(buf, "%.4f", maxlen/8.);
270                vval(GRID) = savqstr(buf);
271                vdef(GRID)++;
272        }
273        if ((d = vflt(GRID)) <= FTINY)
274                badvalue(GRID);
275        for (i = 0; i < 3; i++)
276                gp->grid[i] = len[i]/d + (1.-FTINY);
277        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);
379          if (!vdef(OCTREE)) {
380                  if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
381                          error(SYSTEM, "out of memory");
382                  sprintf(vval(OCTREE), "%s.oct", froot);
383                  vdef(OCTREE)++;
384          }
385 <        if (!vdef(DISKSPACE)) {
386 <                sprintf(errmsg,
387 <                        "no %s setting, assuming 100 Mbytes available",
292 <                                vnam(DISKSPACE));
293 <                error(WARNING, errmsg);
294 <                vval(DISKSPACE) = "100";
295 <                vdef(DISKSPACE)++;
385 >        if (!vdef(VDIST)) {
386 >                vval(VDIST) = "F";
387 >                vdef(VDIST)++;
388          }
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        }
389                                  /* append rendering options */
390          if (vdef(RENDER))
391                  rtargc += wordstring(rtargv+rtargc, vval(RENDER));
392 +        
393 +        if (gp == NULL)         /* already initialized? */
394 +                return;
395 +                                /* set grid parameters */
396 +        for (n = 0; n < vdef(SECTION); n++, gp++) {
397 +                gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
398 +                if (sscanf(nvalue(SECTION, n),
399 +                "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd",
400 +                                &gp->orig[0], &gp->orig[1], &gp->orig[2],
401 +                                &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
402 +                                &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
403 +                                &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
404 +                                &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
405 +                        badvalue(SECTION);
406 +                for (i = 0; i < 3; i++)
407 +                        len[i] = VLEN(gp->xv[i]);
408 +                if (!vdef(GRID)) {
409 +                        d = 2/2e5*( len[0]*len[0]*(len[1]*len[1] +
410 +                                        len[2]*len[2] + 4*len[1]*len[2])
411 +                                + len[1]*len[1]*len[2]*(len[2] + 4*len[0])
412 +                                + 4*len[0]*len[1]*len[2]*len[2] );
413 +                        d = sqrt(sqrt(d));
414 +                } else if ((d = vflt(GRID)) <= FTINY)
415 +                        badvalue(GRID);
416 +                for (i = 0; i < 3; i++)
417 +                        if (gp->grid[i] <= 0)
418 +                                gp->grid[i] = len[i]/d + (1.-FTINY);
419 +        }
420   }
421  
422  
423   creatholo(gp)                   /* create a holodeck output file */
424   HDGRID  *gp;
425   {
426 <        long    endloc = 0;
426 >        extern char     VersionID[];
427 >        int32   lastloc, nextloc;
428 >        int     n;
429 >        int     fd;
430          FILE    *fp;
431                                          /* open & truncate file */
432          if ((fp = fopen(hdkfile, "w+")) == NULL) {
# Line 328 | Line 435 | HDGRID *gp;
435          }
436                                          /* write information header */
437          newheader("RADIANCE", fp);
438 +        fprintf(fp, "SOFTWARE= %s\n", VersionID);
439          printvars(fp);
440          fputformat(HOLOFMT, fp);
441          fputc('\n', fp);
442 <        putw(HOLOMAGIC, fp);            /* put magic number & terminus */
443 <        fwrite(&endloc, sizeof(long), 1, fp);
444 <        fflush(fp);                     /* flush buffer */
445 <        hdinit(fileno(fp), gp);         /* allocate and initialize index */
446 <                        /* we're dropping fp here.... */
442 >        putw(HOLOMAGIC, fp);            /* put magic number */
443 >        fd = dup(fileno(fp));
444 >        fclose(fp);                     /* flush and close stdio stream */
445 >        lastloc = lseek(fd, (off_t)0, SEEK_END);
446 >        for (n = vdef(SECTION); n--; gp++) {    /* initialize each section */
447 >                nextloc = 0L;
448 >                write(fd, (char *)&nextloc, sizeof(nextloc));
449 >                hdinit(fd, gp);                 /* writes beam index */
450 >                if (!n)
451 >                        break;
452 >                nextloc = hdfilen(fd);          /* write section pointer */
453 >                if (lseek(fd, (off_t)lastloc, SEEK_SET) < 0)
454 >                        error(SYSTEM,
455 >                                "cannot seek on holodeck file in creatholo");
456 >                write(fd, (char *)&nextloc, sizeof(nextloc));
457 >                lseek(fd, (off_t)(lastloc=nextloc), SEEK_SET);
458 >        }
459   }
460  
461  
462 + int
463   headline(s)                     /* process information header line */
464   char    *s;
465   {
# Line 352 | Line 473 | char   *s;
473                                          HOLOFMT, hdkfile, FMTSTR, fmt);
474                          error(USER, errmsg);
475                  }
476 <                return;
476 >                return(0);
477          }
478          for (cp = s; *cp; cp++)         /* take off any comments */
479                  if (*cp == '#') {
# Line 360 | Line 481 | char   *s;
481                          break;
482                  }
483          setvariable(s, matchvar);       /* don't flag errors */
484 +        return(0);
485   }
486  
487  
488   loadholo()                      /* start loading a holodeck from fname */
489   {
490          FILE    *fp;
491 <        long    endloc;
492 <                                        /* open input file */
493 <        if ((fp = fopen(hdkfile, "r+")) == NULL) {
494 <                sprintf(errmsg, "cannot open \"%s\" for appending", hdkfile);
495 <                error(SYSTEM, errmsg);
491 >        int     fd;
492 >        int     n;
493 >        int32   nextloc;
494 >        
495 >        if ((ncprocs > 0) & (force >= 0))
496 >                fp = fopen(hdkfile, "r+");
497 >        else
498 >                fp = NULL;
499 >        if (fp == NULL) {
500 >                if ((fp = fopen(hdkfile, "r")) == NULL) {
501 >                        sprintf(errmsg, "cannot open \"%s\"", hdkfile);
502 >                        error(SYSTEM, errmsg);
503 >                }
504 >                if (ncprocs > 0) {
505 >                        sprintf(errmsg,
506 >                        "\"%s\" opened read-only; new rays will be discarded",
507 >                                        hdkfile);
508 >                        error(WARNING, errmsg);
509 >                        force = -1;
510 >                }
511          }
512                                          /* load variables from header */
513          getheader(fp, headline, NULL);
# Line 380 | Line 517 | loadholo()                     /* start loading a holodeck from fname */
517                                  hdkfile);
518                  error(USER, errmsg);
519          }
520 <        fread(&endloc, sizeof(long), 1, fp);
521 <        if (endloc != 0)
522 <                error(WARNING, "ignoring multiple sections in holodeck file");
523 <        fseek(fp, 0L, 1);                       /* align system file pointer */
524 <        hdinit(fileno(fp), NULL);               /* allocate and load index */
525 <                        /* we're dropping fp here.... */
520 >        nextloc = ftell(fp);                    /* get stdio position */
521 >        fd = dup(fileno(fp));
522 >        fclose(fp);                             /* done with stdio */
523 >        for (n = 0; nextloc > 0L; n++) {        /* initialize each section */
524 >                lseek(fd, (off_t)nextloc, SEEK_SET);
525 >                read(fd, (char *)&nextloc, sizeof(nextloc));
526 >                hdinit(fd, NULL);
527 >        }
528 >        if (n != vdef(SECTION)) {
529 >                sprintf(errmsg, "number of sections does not match %s setting",
530 >                                vnam(SECTION));
531 >                error(WARNING, errmsg);
532 >        }
533   }
534  
535  
536   done_packets(pl)                /* handle finished packets */
537   PACKET  *pl;
538   {
539 +        static int      n2flush = 0;
540          register PACKET *p;
541  
542          while (pl != NULL) {
543                  p = pl; pl = p->next; p->next = NULL;
544                  if (p->nr > 0) {                /* add to holodeck */
545 <                        bcopy((char *)p->ra,
546 <                                (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
545 >                        memcpy( (void *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
546 >                                (void *)p->ra,
547                                  p->nr*sizeof(RAYVAL));
548                          if (outdev != NULL)     /* display it */
549 <                                disp_packet(p);
549 >                                disp_packet((PACKHEAD *)p);
550 >                        if (hdcachesize <= 0)
551 >                                n2flush++;
552 >                        nraysdone += p->nr;
553 >                        npacksdone++;
554 >                        p->nr = 0;
555                  }
556 <                nraysdone += p->nr;
407 <                npacksdone++;
408 <                p->nr = 0;                      /* push onto free list */
409 <                p->next = freepacks;
556 >                p->next = freepacks;            /* push onto free list */
557                  freepacks = p;
558          }
559 < }
560 <
561 <
562 < getradfile(rfargs)              /* run rad and get needed variables */
563 < char    *rfargs;
564 < {
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);
559 >        if (n2flush >= RTFLUSH) {
560 >                if (outdev != NULL)
561 >                        hdsync(NULL, 1);
562 >                else
563 >                        hdflush(NULL);
564 >                n2flush = 0;
565          }
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 */
566   }
567  
568  
# Line 464 | Line 571 | register char  *rn, *fn;
571   {
572          char    *tp, *dp;
573  
574 <        for (tp = NULL, dp = rn; *rn = *fn++; rn++)
575 <                if (ISDIRSEP(*rn))
574 >        for (tp = NULL, dp = rn; (*rn = *fn++); rn++) {
575 >                if (*rn == '/')
576                          dp = rn;
577                  else if (*rn == '.')
578                          tp = rn;
579 +        }
580          if (tp != NULL && tp > dp)
581                  *tp = '\0';
582   }
# Line 482 | Line 590 | int    vc;
590   }
591  
592  
593 + void
594   eputs(s)                        /* put error message to stderr */
595   register char  *s;
596   {
# Line 501 | Line 610 | register char  *s;
610   }
611  
612  
613 < wputs(s)                        /* put warning string to stderr */
505 < char    *s;
506 < {
507 <        if (!nowarn)
508 <                eputs(s);
509 < }
510 <
511 <
613 > void
614   quit(ec)                        /* exit program gracefully */
615   int     ec;
616   {
617          int     status = 0;
618  
619 <        if (hdlist[0] != NULL) {        /* flush holodeck */
620 <                if (ncprocs > 0) {
621 <                        done_packets(flush_queue());
622 <                        status = end_rtrace();  /* close rtrace */
623 <                        hdflush(NULL);
624 <                        if (vdef(REPORT)) {
625 <                                long    fsiz, fuse;
626 <                                report(0);
525 <                                fsiz = lseek(hdlist[0]->fd, 0L, 2);
526 <                                fuse = hdfiluse(hdlist[0]->fd, 1);
527 <                                fprintf(stderr,
619 >        if (hdlist[0] != NULL) {        /* close holodeck */
620 >                if (nprocs > 0)
621 >                        status = done_rtrace();         /* calls hdsync() */
622 >                if ((ncprocs > 0) & (force >= 0) && vdef(REPORT)) {
623 >                        off_t   fsiz, fuse;
624 >                        fsiz = hdfilen(hdlist[0]->fd);
625 >                        fuse = hdfiluse(hdlist[0]->fd, 1);
626 >                        fprintf(stderr,
627                          "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
628 <                                                hdkfile, fsiz/(1024.*1024.),
629 <                                                100.*(fsiz-fuse)/fsiz);
630 <                        }
532 <                } else
533 <                        hdflush(NULL);
628 >                                        hdkfile, fsiz/(1024.*1024.),
629 >                                        100.*(fsiz-fuse)/fsiz);
630 >                }
631          }
632 +        if (orig_mode >= 0)             /* reset holodeck access mode */
633 +                fchmod(hdlist[0]->fd, orig_mode);
634 +        if (outdev != NULL)             /* close display */
635 +                disp_close();
636          exit(ec ? ec : status);         /* exit */
637   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines