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.27 by gregl, Sun Dec 14 09:46:52 1997 UTC vs.
Revision 3.86 by greg, Thu Jun 5 19:10:31 2025 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 "rholo.h"
9 < #include "random.h"
8 > #include <stdio.h>
9 > #include <time.h>
10   #include <signal.h>
14 #include <sys/types.h>
11   #include <sys/stat.h>
12 + #include <string.h>
13  
14 + #include "platform.h"
15 + #include "rterror.h"
16 + #include "resolu.h"
17 + #include "rholo.h"
18 + #include "random.h"
19 +
20 + #ifndef FRAGWARN
21 + #define FRAGWARN        20              /* fragmentation for warning (%) */
22 + #endif
23 + #ifndef MAXQTIME
24 + #define MAXQTIME        3               /* target maximum seconds in queue */
25 + #endif
26 +                                        /* manual cache flushing frequency */
27 + #ifndef RTFLUSH
28 + #if MAXQTIME
29 + #define RTFLUSH         (300/MAXQTIME*totqlen)  /* <= 5 minutes */
30 + #else
31 + #define RTFLUSH         (50*totqlen)            /* just guess */
32 + #endif
33 + #endif
34                          /* the following must be consistent with rholo.h */
35   int     NVARS = NRHVARS;                /* total number of variables */
36  
37   VARIABLE        vv[] = RHVINIT;         /* variable-value pairs */
38  
22 char    *progname;              /* our program name */
39   char    *hdkfile;               /* holodeck file name */
40   char    froot[256];             /* root file name */
41  
26 int     nowarn = 0;             /* turn warnings off? */
27
42   int     ncprocs = 0;            /* desired number of compute processes */
43  
44   char    *outdev = NULL;         /* output device name */
45  
46   int     readinp = 0;            /* read commands from stdin */
47  
48 < int     force = 0;              /* allow overwrite of holodeck */
48 > int     force = 0;              /* allow overwrite of holodeck (-1 == read-only) */
49  
50   time_t  starttime;              /* time we got started */
51   time_t  endtime;                /* time we should end by */
52   time_t  reporttime;             /* time for next report */
53  
54 < long    maxdisk;                /* maximum file space (bytes) */
54 > off_t   maxdisk;                /* maximum file space (bytes) */
55  
56   int     rtargc = 1;             /* rtrace command */
57 < char    *rtargv[128] = {"rtrace", NULL};
57 > char    *rtargv[MAXRTARGC] = {"rtrace", NULL};
58  
59   int     orig_mode = -1;         /* original file mode (-1 if unchanged) */
60  
# Line 48 | Line 62 | long   nraysdone = 0L;         /* number of rays done */
62   long    npacksdone = 0L;        /* number of packets done */
63  
64   PACKET  *freepacks;             /* available packets */
65 + int     totqlen;                /* maximum queue length (number of packets) */
66  
67   char  *sigerr[NSIG];            /* signal error messages */
68  
69 < extern time_t   time();
69 > extern int      nowarn;         /* turn warnings off? */
70  
71 + static void onsig(int  signo);
72 + static void sigdie(int  signo, char  *msg);
73 + static int resfmode(int fd, int mod);
74 + static void initrholo(void);
75 + static int rholo(void);
76 + static void setdefaults(HDGRID  *gp);
77 + static void creatholo(HDGRID    *gp);
78 + static gethfunc headline;
79 + static void loadholo(void);
80 + static void rootname(char       *rn, char       *fn);
81 + static void badvalue(int        vc);
82  
83 < main(argc, argv)
84 < int     argc;
85 < char    *argv[];
83 >
84 > int
85 > main(
86 >        int     argc,
87 >        char    *argv[]
88 > )
89   {
61        HDGRID  hdg;
90          int     i;
91 <                                                /* mark start time */
92 <        starttime = time(NULL);
65 <        initurand(16384);                       /* initialize urand */
66 <        progname = argv[0];                     /* get arguments */
91 >
92 >        fixargv0(argv[0]);                      /* get arguments */
93          for (i = 1; i < argc && argv[i][0] == '-'; i++)
94                  switch (argv[i][1]) {
95                  case 'w':                       /* turn off warnings */
96                          nowarn++;
97                          break;
98                  case 'f':                       /* force overwrite */
99 <                        force++;
99 >                        force = 1;
100                          break;
101 +                case 'r':                       /* read-only mode */
102 +                        force = -1;
103 +                        break;
104                  case 'i':                       /* read input from stdin */
105                          readinp++;
106                          break;
# Line 89 | Line 118 | char   *argv[];
118                          goto userr;
119                  }
120                                                  /* get root file name */
121 +        if (i >= argc)
122 +                goto userr;
123          rootname(froot, hdkfile=argv[i++]);
124                                                  /* load variables? */
125          if (i < argc)
# Line 105 | Line 136 | char   *argv[];
136                  }
137                                                  /* check settings */
138          checkvalues();
139 <                                                /* load RIF if any */
139 >                                                /* load rad input file */
140          getradfile();
141  
142 <        if (hdlist[i] == NULL) {                /* create new holodeck */
142 >        if (hdlist[0] == NULL) {                /* create new holodeck */
143 >                HDGRID  hdg[HDMAX];
144                                                          /* set defaults */
145 <                setdefaults(&hdg);
145 >                setdefaults(hdg);
146 >                                                        /* check read-only */
147 >                if (force < 0)
148 >                        error(USER, "cannot create read-only holodeck");
149                                                          /* holodeck exists? */
150                  if (!force && access(hdkfile, R_OK|W_OK) == 0)
151                          error(USER,
152                                  "holodeck file exists -- use -f to overwrite");
153                                                          /* create holodeck */
154 <                creatholo(&hdg);
154 >                creatholo(hdg);
155          } else                                  /* else just set defaults */
156                  setdefaults(NULL);
157                                                  /* initialize */
# Line 128 | Line 163 | char   *argv[];
163          quit(0);
164   userr:
165          fprintf(stderr,
166 < "Usage: %s [-n nprocs][-o disp][-w][-f] output.hdk [control.hif|+|- [VAR=val ..]]\n",
166 > "Usage: %s [-n nprocs][-o disp][-i][-w][-r|-f] output.hdk [control.hif|+|- [VAR=val ..]]\n",
167                          progname);
168          quit(1);
169 +        return 1; /* pro forma return */
170   }
171  
172  
173 < onsig(signo)                            /* fatal signal */
174 < int  signo;
173 > static void
174 > onsig(                          /* fatal signal */
175 >        int  signo
176 > )
177   {
178          static int  gotsig = 0;
179  
180 <        if (gotsig++)                   /* two signals and we're gone! */
180 >        if (gotsig > 1)                 /* we're going as fast as we can! */
181 >                return;
182 >        if (gotsig++) {                 /* two signals and we split */
183 >                hdsync(NULL, 0);        /* don't leave w/o saying goodbye */
184                  _exit(signo);
185 <
186 <        alarm(30);                      /* allow 30 seconds to clean up */
146 <        signal(SIGALRM, SIG_DFL);       /* make certain we do die */
185 >        }
186 >        alarm(300);                     /* allow 10 minutes to clean up */
187          eputs("signal - ");
188          eputs(sigerr[signo]);
189          eputs("\n");
# Line 151 | Line 191 | int  signo;
191   }
192  
193  
194 < sigdie(signo, msg)                      /* set fatal signal */
195 < int  signo;
196 < char  *msg;
194 > static void
195 > sigdie(                 /* set fatal signal */
196 >        int  signo,
197 >        char  *msg
198 > )
199   {
200          if (signal(signo, onsig) == SIG_IGN)
201                  signal(signo, SIG_IGN);
# Line 161 | Line 203 | char  *msg;
203   }
204  
205  
206 < int
207 < resfmode(fd, mod)               /* restrict open file access mode */
208 < int     fd, mod;
206 > static int
207 > resfmode(               /* restrict open file access mode */
208 >        int     fd,
209 >        int     mod
210 > )
211   {
212          struct stat     stbuf;
213                                          /* get original mode */
214          if (fstat(fd, &stbuf) < 0)
215                  error(SYSTEM, "cannot stat open holodeck file");
216          mod &= stbuf.st_mode;           /* always more restrictive */
217 <        if (mod == stbuf.st_mode)
217 >        if (mod == (stbuf.st_mode & 0777))
218                  return(-1);             /* already set */
219                                          /* else change it */
220          if (fchmod(fd, mod) < 0) {
# Line 181 | Line 225 | int    fd, mod;
225   }
226  
227  
228 < initrholo()                     /* get our holodeck running */
228 > static void
229 > initrholo(void)                 /* get our holodeck running */
230   {
231 <        extern int      global_packet();
187 <        register int    i;
231 >        int     i;
232                                                  /* close holodeck on exec() */
233          fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC);
234  
# Line 194 | Line 238 | initrholo()                    /* get our holodeck running */
238                  init_global();
239                                                  /* record disk space limit */
240          if (!vdef(DISKSPACE))
241 <                maxdisk = 0;
241 >                maxdisk = ((off_t)1<<(sizeof(off_t)*8-2)) - 1024;
242          else
243                  maxdisk = 1024.*1024.*vflt(DISKSPACE);
200                                                /* record end time */
201        if (!vdef(TIME) || vflt(TIME) <= FTINY)
202                endtime = 0;
203        else
204                endtime = starttime + vflt(TIME)*3600. + .5;
244                                                  /* set up memory cache */
245          if (outdev == NULL)
246 <                hdcachesize = 0;        /* manual flushing */
246 >                hdcachesize = 0;                /* manual flushing */
247          else if (vdef(CACHE))
248                  hdcachesize = 1024.*1024.*vflt(CACHE);
249                                                  /* open report file */
250          if (vdef(REPORT)) {
251 <                register char   *s = sskip2(vval(REPORT), 1);
251 >                char    *s = sskip2(vval(REPORT), 1);
252                  if (*s && freopen(s, "a", stderr) == NULL)
253                          quit(2);
254          }
255 +                                                /* mark the starting time */
256 +        starttime = time(NULL);
257 +                                                /* compute end time */
258 +        if (!vdef(TIME) || vflt(TIME) <= FTINY)
259 +                endtime = 0;
260 +        else
261 +                endtime = starttime + vflt(TIME)*3600. + .5;
262                                                  /* start rtrace */
263          if (ncprocs > 0) {
264 <                i = start_rtrace();
264 >                totqlen = i = start_rtrace();
265                  if (i < 1)
266                          error(USER, "cannot start rtrace process(es)");
267                  if (vdef(REPORT)) {             /* make first report */
# Line 244 | Line 290 | initrholo()                    /* get our holodeck running */
290          }
291                                          /* set up signal handling */
292          sigdie(SIGINT, "Interrupt");
247        sigdie(SIGHUP, "Hangup");
293          sigdie(SIGTERM, "Terminate");
294 + #ifdef SIGHUP
295 +        sigdie(SIGHUP, "Hangup");
296 + #endif
297 + #ifdef SIGPIPE
298          sigdie(SIGPIPE, "Broken pipe");
299 + #endif
300 + #ifdef SIGALRM
301          sigdie(SIGALRM, "Alarm clock");
302 + #endif
303   #ifdef  SIGXCPU
304          sigdie(SIGXCPU, "CPU limit exceeded");
305 + #endif
306 + #ifdef  SIGXFSZ
307          sigdie(SIGXFSZ, "File size exceeded");
308   #endif
309 <                                                /* protect holodeck file */
310 <        orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444);
309 >                                        /* protect holodeck file */
310 >        orig_mode = resfmode(hdlist[0]->fd, (ncprocs>0) & (force>=0) ? 0 : 0444);
311          return;
312   memerr:
313          error(SYSTEM, "out of memory in initrholo");
314   }
315  
316  
317 < rholo()                         /* holodeck main loop */
317 > static int
318 > rholo(void)                             /* holodeck main loop */
319   {
320 <        static int      idle = 1;
320 >        static off_t    nextfragwarn = 100L<<20;
321 >        static int      idle = 0;
322          PACKET  *pl = NULL, *plend;
323 <        register PACKET *p;
323 >        off_t   fsiz;
324 >        int     pksiz;
325 >        PACKET  *p;
326          time_t  t;
327 <        long    l;
270 <
271 <        if (outdev != NULL)             /* check display */
272 <                if (!disp_check(idle))
273 <                        return(0);
274 <                                        /* display only? */
327 >                                        /* check display */
328          if (nprocs <= 0)
276                return(outdev != NULL);
277                                        /* check file size */
278        if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) {
279                error(WARNING, "file limit exceeded");
280                done_rtrace();
329                  idle = 1;
330 +        if (outdev != NULL) {
331 +                if (!disp_check(idle))
332 +                        return(0);      /* quit request */
333 +                if (nprocs <= 0)
334 +                        return(1);
335 +        } else if (idle)
336 +                return(0);              /* all done */
337 +        fsiz = hdfilen(hdlist[0]->fd);  /* check file size */
338 +        if (maxdisk > 0 && fsiz >= maxdisk) {
339 +                error(USER, "file limit exceeded");
340 +                done_rtrace();
341                  return(1);      /* comes back */
342          }
343 <                                        /* check time */
344 <        if (endtime > 0 || reporttime > 0)
345 <                t = time(NULL);
343 > #if FRAGWARN
344 >        if (fsiz >= nextfragwarn) {
345 >                double  pctfrag = 100.*(fsiz-hdfiluse(hdlist[0]->fd))/fsiz;
346 >                if (pctfrag >= (double)FRAGWARN) {
347 >                        sprintf(errmsg, "holodeck file fragmentation is %.0f%%",
348 >                                        pctfrag);
349 >                        error(WARNING, errmsg);
350 >                        nextfragwarn = fsiz + (fsiz>>2);
351 >                } else
352 >                        nextfragwarn = fsiz + (10L<<20);
353 >        }
354 > #endif
355 >        t = time(NULL);                 /* check time */
356          if (endtime > 0 && t >= endtime) {
357 <                error(WARNING, "time limit exceeded");
357 >                error(USER, "time limit exceeded");
358                  done_rtrace();
290                idle = 1;
359                  return(1);      /* comes back */
360          }
361          if (reporttime > 0 && t >= reporttime)
362                  report(t);
363 <                                        /* get packets to process */
363 >                                        /* figure out good packet size */
364 >        pksiz = RPACKSIZ;
365 > #if MAXQTIME
366 >        if (!chunkycmp) {
367 >                pksiz = nraysdone*MAXQTIME/(totqlen*(t - starttime + 1L));
368 >                if (pksiz < 1) pksiz = 1;
369 >                else if (pksiz > RPACKSIZ) pksiz = RPACKSIZ;
370 >        }
371 > #endif
372 >        idle = 0;                       /* get packets to process */
373          while (freepacks != NULL) {
374                  p = freepacks; freepacks = p->next; p->next = NULL;
375 <                if (!next_packet(p)) {
375 >                if (!next_packet(p, pksiz)) {
376                          p->next = freepacks; freepacks = p;
377                          idle = 1;
378                          break;
# Line 310 | Line 387 | rholo()                                /* holodeck main loop */
387   }
388  
389  
390 < setdefaults(gp)                 /* set default values */
391 < register HDGRID *gp;
390 > static void
391 > setdefaults(                    /* set default values */
392 >        HDGRID  *gp
393 > )
394   {
395 <        extern char     *atos();
396 <        register int    i;
397 <        double  len[3], maxlen, d;
319 <        char    buf[64];
395 >        int     i;
396 >        int     n;
397 >        double  len[3], d;
398  
399          if (!vdef(SECTION)) {
400                  sprintf(errmsg, "%s must be defined", vnam(SECTION));
401                  error(USER, errmsg);
402          }
325        if (vdef(SECTION) > 1) {
326                sprintf(errmsg, "ignoring all but first %s", vnam(SECTION));
327                error(WARNING, errmsg);
328        }
403          if (!vdef(OCTREE)) {
404                  if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
405                          error(SYSTEM, "out of memory");
# Line 336 | Line 410 | register HDGRID        *gp;
410                  vval(VDIST) = "F";
411                  vdef(VDIST)++;
412          }
339        if (!vdef(OCCUPANCY)) {
340                vval(OCCUPANCY) = "U";
341                vdef(OCCUPANCY)++;
342        }
413                                  /* append rendering options */
414          if (vdef(RENDER))
415 <                rtargc += wordstring(rtargv+rtargc, vval(RENDER));
415 >                rtargc += wordstring(rtargv+rtargc, MAXRTARGC-rtargc, vval(RENDER));
416          
417          if (gp == NULL)         /* already initialized? */
418                  return;
419                                  /* set grid parameters */
420 <        gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
421 <        if (sscanf(vval(SECTION),
420 >        for (n = 0; n < vdef(SECTION); n++, gp++) {
421 >                gp->grid[0] = gp->grid[1] = gp->grid[2] = 0;
422 >                if (sscanf(nvalue(SECTION, n),
423                  "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd",
424 <                        &gp->orig[0], &gp->orig[1], &gp->orig[2],
425 <                        &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
426 <                        &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
427 <                        &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
428 <                        &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
429 <                badvalue(SECTION);
430 <        maxlen = 0.;
431 <        for (i = 0; i < 3; i++)
432 <                if ((len[i] = VLEN(gp->xv[i])) > maxlen)
433 <                        maxlen = len[i];
434 <        if (!vdef(GRID)) {
435 <                sprintf(buf, "%.4f", maxlen/8.);
436 <                vval(GRID) = savqstr(buf);
437 <                vdef(GRID)++;
424 >                                &gp->orig[0], &gp->orig[1], &gp->orig[2],
425 >                                &gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2],
426 >                                &gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2],
427 >                                &gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2],
428 >                                &gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12)
429 >                        badvalue(SECTION);
430 >                for (i = 0; i < 3; i++)
431 >                        len[i] = VLEN(gp->xv[i]);
432 >                if (!vdef(GRID)) {
433 >                        d = 2/2e5*( len[0]*len[0]*(len[1]*len[1] +
434 >                                        len[2]*len[2] + 4*len[1]*len[2])
435 >                                + len[1]*len[1]*len[2]*(len[2] + 4*len[0])
436 >                                + 4*len[0]*len[1]*len[2]*len[2] );
437 >                        d = sqrt(sqrt(d));
438 >                } else if ((d = vflt(GRID)) <= FTINY)
439 >                        badvalue(GRID);
440 >                for (i = 0; i < 3; i++)
441 >                        if (gp->grid[i] <= 0)
442 >                                gp->grid[i] = len[i]/d + (1.-FTINY);
443          }
368        if ((d = vflt(GRID)) <= FTINY)
369                badvalue(GRID);
370        for (i = 0; i < 3; i++)
371                if (gp->grid[i] <= 0)
372                        gp->grid[i] = len[i]/d + (1.-FTINY);
444   }
445  
446  
447 < creatholo(gp)                   /* create a holodeck output file */
448 < HDGRID  *gp;
447 > static void
448 > creatholo(                      /* create a holodeck output file */
449 >        HDGRID  *gp
450 > )
451   {
452          extern char     VersionID[];
453 <        long    endloc = 0;
453 >        off_t   lastloc, nextloc;
454 >        int     n;
455          int     fd;
456          FILE    *fp;
457                                          /* open & truncate file */
458 <        if ((fp = fopen(hdkfile, "w+")) == NULL) {
458 >        if ((fp = fopen(hdkfile, "wb+")) == NULL) {
459                  sprintf(errmsg, "cannot open \"%s\" for writing", hdkfile);
460                  error(SYSTEM, errmsg);
461          }
462                                          /* write information header */
463          newheader("RADIANCE", fp);
464          fprintf(fp, "SOFTWARE= %s\n", VersionID);
465 +        fputnow(fp);
466          printvars(fp);
467 +        fputendian(fp);
468          fputformat(HOLOFMT, fp);
469          fputc('\n', fp);
470 <        putw(HOLOMAGIC, fp);            /* put magic number & terminus */
395 <        fwrite(&endloc, sizeof(long), 1, fp);
470 >        putw(HOLOMAGIC, fp);            /* put magic number */
471          fd = dup(fileno(fp));
472          fclose(fp);                     /* flush and close stdio stream */
473 <        hdinit(fd, gp);                 /* allocate and initialize index */
473 >        lastloc = lseek(fd, (off_t)0, SEEK_END);
474 >        for (n = vdef(SECTION); n--; gp++) {    /* initialize each section */
475 >                nextloc = 0L;
476 >                write(fd, (char *)&nextloc, sizeof(nextloc));
477 >                hdinit(fd, gp);                 /* writes beam index */
478 >                if (!n)
479 >                        break;
480 >                nextloc = hdfilen(fd);          /* write section pointer */
481 >                if (lseek(fd, lastloc, SEEK_SET) < 0)
482 >                        error(SYSTEM,
483 >                                "cannot seek on holodeck file in creatholo");
484 >                write(fd, (char *)&nextloc, sizeof(nextloc));
485 >                lseek(fd, (lastloc=nextloc), SEEK_SET);
486 >        }
487   }
488  
489  
490 < headline(s)                     /* process information header line */
491 < char    *s;
490 > static int
491 > headline(                       /* process information header line */
492 >        char    *s,
493 >        void    *p
494 > )
495   {
496          extern char     FMTSTR[];
497 <        register char   *cp;
498 <        char    fmt[32];
497 >        char    *cp;
498 >        char    fmt[MAXFMTLEN];
499  
500          if (formatval(fmt, s)) {
501                  if (strcmp(fmt, HOLOFMT)) {
# Line 412 | Line 503 | char   *s;
503                                          HOLOFMT, hdkfile, FMTSTR, fmt);
504                          error(USER, errmsg);
505                  }
506 <                return;
506 >                return(0);
507          }
508          for (cp = s; *cp; cp++)         /* take off any comments */
509                  if (*cp == '#') {
# Line 420 | Line 511 | char   *s;
511                          break;
512                  }
513          setvariable(s, matchvar);       /* don't flag errors */
514 +        return(0);
515   }
516  
517  
518 < loadholo()                      /* start loading a holodeck from fname */
518 > static void
519 > loadholo(void)                  /* start loading a holodeck from fname */
520   {
428        extern long     ftell();
521          FILE    *fp;
522          int     fd;
523 <        long    fpos;
524 <                                        /* open holodeck file */
525 <        if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) {
526 <                sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile,
527 <                                ncprocs>0 ? "appending" : "reading");
528 <                error(SYSTEM, errmsg);
523 >        int     n;
524 >        off_t   nextloc;
525 >        
526 >        if ((ncprocs > 0) & (force >= 0))
527 >                fp = fopen(hdkfile, "rb+");
528 >        else
529 >                fp = NULL;
530 >        if (fp == NULL) {
531 >                if ((fp = fopen(hdkfile, "rb")) == NULL) {
532 >                        sprintf(errmsg, "cannot open \"%s\"", hdkfile);
533 >                        error(SYSTEM, errmsg);
534 >                }
535 >                if (ncprocs > 0) {
536 >                        sprintf(errmsg, "\"%s\" is read-only", hdkfile);
537 >                        if (outdev == NULL)
538 >                                error(USER, errmsg);
539 >                        strcat(errmsg, "; new rays will be discarded");
540 >                        error(WARNING, errmsg);
541 >                        force = -1;
542 >                }
543          }
544                                          /* load variables from header */
545          getheader(fp, headline, NULL);
# Line 443 | Line 549 | loadholo()                     /* start loading a holodeck from fname */
549                                  hdkfile);
550                  error(USER, errmsg);
551          }
552 <        fread(&fpos, sizeof(long), 1, fp);
447 <        if (fpos != 0)
448 <                error(WARNING, "ignoring multiple sections in holodeck file");
449 <        fpos = ftell(fp);                       /* get stdio position */
552 >        nextloc = ftell(fp);                    /* get stdio position */
553          fd = dup(fileno(fp));
554          fclose(fp);                             /* done with stdio */
555 <        lseek(fd, fpos, 0);                     /* align system file pointer */
556 <        hdinit(fd, NULL);                       /* allocate and load index */
555 >        for (n = 0; nextloc > 0L; n++) {        /* initialize each section */
556 >                lseek(fd, nextloc, SEEK_SET);
557 >                read(fd, (char *)&nextloc, sizeof(nextloc));
558 >                hdinit(fd, NULL);
559 >        }
560 >        if (n != vdef(SECTION)) {
561 >                sprintf(errmsg, "number of sections does not match %s setting",
562 >                                vnam(SECTION));
563 >                error(WARNING, errmsg);
564 >        }
565   }
566  
567  
568 < done_packets(pl)                /* handle finished packets */
569 < PACKET  *pl;
568 > void
569 > done_packets(           /* handle finished packets */
570 >        PACKET  *pl
571 > )
572   {
573          static int      n2flush = 0;
574 <        register PACKET *p;
574 >        PACKET  *p;
575  
576          while (pl != NULL) {
577                  p = pl; pl = p->next; p->next = NULL;
578                  if (p->nr > 0) {                /* add to holodeck */
579 <                        bcopy((char *)p->ra,
580 <                                (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
579 >                        memcpy( (void *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
580 >                                (void *)p->ra,
581                                  p->nr*sizeof(RAYVAL));
582                          if (outdev != NULL)     /* display it */
583                                  disp_packet((PACKHEAD *)p);
584 <                        if (hdcachesize <= 0)   /* manual flushing */
585 <                                n2flush += p->nr;
584 >                        if (hdcachesize <= 0)
585 >                                n2flush++;
586                          nraysdone += p->nr;
587                          npacksdone++;
588 +                        p->nr = 0;
589                  }
590 <                p->nr = 0;                      /* push onto free list */
477 <                p->next = freepacks;
590 >                p->next = freepacks;            /* push onto free list */
591                  freepacks = p;
592          }
593 <        if (n2flush > 512*RPACKSIZ*nprocs) {
594 <                hdflush(NULL);                  /* flush holodeck buffers */
593 >        if (n2flush >= RTFLUSH) {
594 >                if (outdev != NULL)
595 >                        hdsync(NULL, 1);
596 >                else
597 >                        hdflush(NULL);
598                  n2flush = 0;
599          }
600   }
601  
602  
603 < rootname(rn, fn)                /* remove tail from end of fn */
604 < register char   *rn, *fn;
603 > static void
604 > rootname(               /* remove tail from end of fn */
605 >        char    *rn,
606 >        char    *fn
607 > )
608   {
609          char    *tp, *dp;
610  
611 <        for (tp = NULL, dp = rn; *rn = *fn++; rn++)
611 >        for (tp = NULL, dp = rn; (*rn = *fn++); rn++) {
612                  if (*rn == '/')
613                          dp = rn;
614                  else if (*rn == '.')
615                          tp = rn;
616 +        }
617          if (tp != NULL && tp > dp)
618                  *tp = '\0';
619   }
620  
621  
622 < badvalue(vc)                    /* report bad variable value and exit */
623 < int     vc;
622 > static void
623 > badvalue(                       /* report bad variable value and exit */
624 >        int     vc
625 > )
626   {
627          sprintf(errmsg, "bad value for variable '%s'", vnam(vc));
628          error(USER, errmsg);
629   }
630  
631  
632 < eputs(s)                        /* put error message to stderr */
633 < register char  *s;
632 > void
633 > eputs(const char *s)            /* put error message to stderr */
634   {
635          static int  midline = 0;
636  
# Line 526 | Line 648 | register char  *s;
648   }
649  
650  
651 < wputs(s)                        /* put warning string to stderr */
652 < char    *s;
651 > void
652 > quit(int ec)                    /* exit program gracefully */
653   {
532        if (!nowarn)
533                eputs(s);
534 }
535
536
537 quit(ec)                        /* exit program gracefully */
538 int     ec;
539 {
654          int     status = 0;
655  
656          if (hdlist[0] != NULL) {        /* close holodeck */
657                  if (nprocs > 0)
658                          status = done_rtrace();         /* calls hdsync() */
659 <                if (ncprocs > 0 && vdef(REPORT)) {
660 <                        long    fsiz, fuse;
659 >                if ((ncprocs > 0) & (force >= 0) && vdef(REPORT)) {
660 >                        off_t   fsiz, fuse;
661                          fsiz = hdfilen(hdlist[0]->fd);
662 <                        fuse = hdfiluse(hdlist[0]->fd, 1);
662 >                        fuse = hdfiluse(hdlist[0]->fd);
663                          fprintf(stderr,
664                          "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
665                                          hdkfile, fsiz/(1024.*1024.),

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines