ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/hd/rholo.c
Revision: 3.86
Committed: Thu Jun 5 19:10:31 2025 UTC (40 hours, 6 minutes ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 3.85: +2 -3 lines
Log Message:
refactor: Made progname declarations more consistent

File Contents

# User Rev Content
1 gregl 3.1 #ifndef lint
2 greg 3.86 static const char RCSid[] = "$Id: rholo.c,v 3.85 2025/02/06 21:45:00 greg Exp $";
3 gregl 3.1 #endif
4     /*
5     * Radiance holodeck generation controller
6     */
7    
8 schorsch 3.64 #include <stdio.h>
9     #include <time.h>
10 schorsch 3.59 #include <signal.h>
11     #include <sys/stat.h>
12     #include <string.h>
13    
14 greg 3.63 #include "platform.h"
15 schorsch 3.64 #include "rterror.h"
16     #include "resolu.h"
17 gregl 3.1 #include "rholo.h"
18 gregl 3.14 #include "random.h"
19 gregl 3.1
20 gwlarson 3.44 #ifndef FRAGWARN
21     #define FRAGWARN 20 /* fragmentation for warning (%) */
22     #endif
23 gregl 3.33 #ifndef MAXQTIME
24 greg 3.73 #define MAXQTIME 3 /* target maximum seconds in queue */
25 gregl 3.33 #endif
26 gwlarson 3.49 /* 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 gregl 3.1 /* 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    
39     char *hdkfile; /* holodeck file name */
40 gregl 3.22 char froot[256]; /* root file name */
41 gregl 3.1
42     int ncprocs = 0; /* desired number of compute processes */
43    
44     char *outdev = NULL; /* output device name */
45    
46 gregl 3.17 int readinp = 0; /* read commands from stdin */
47    
48 gwlarson 3.51 int force = 0; /* allow overwrite of holodeck (-1 == read-only) */
49 gregl 3.20
50 gregl 3.1 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 greg 3.54 off_t maxdisk; /* maximum file space (bytes) */
55 gregl 3.8
56 gregl 3.1 int rtargc = 1; /* rtrace command */
57 greg 3.78 char *rtargv[MAXRTARGC] = {"rtrace", NULL};
58 gregl 3.1
59 gregl 3.12 int orig_mode = -1; /* original file mode (-1 if unchanged) */
60    
61 gregl 3.1 long nraysdone = 0L; /* number of rays done */
62     long npacksdone = 0L; /* number of packets done */
63    
64     PACKET *freepacks; /* available packets */
65 gwlarson 3.49 int totqlen; /* maximum queue length (number of packets) */
66 gregl 3.1
67 gregl 3.12 char *sigerr[NSIG]; /* signal error messages */
68    
69 gwlarson 3.40 extern int nowarn; /* turn warnings off? */
70    
71 schorsch 3.64 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 schorsch 3.65 static gethfunc headline;
79 schorsch 3.64 static void loadholo(void);
80     static void rootname(char *rn, char *fn);
81     static void badvalue(int vc);
82 gregl 3.1
83    
84 schorsch 3.64 int
85     main(
86     int argc,
87     char *argv[]
88     )
89 gregl 3.1 {
90     int i;
91 gregl 3.33
92 greg 3.86 fixargv0(argv[0]); /* get arguments */
93 gregl 3.1 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 gwlarson 3.51 force = 1;
100 gregl 3.1 break;
101 gwlarson 3.51 case 'r': /* read-only mode */
102     force = -1;
103     break;
104 gregl 3.17 case 'i': /* read input from stdin */
105     readinp++;
106     break;
107 gregl 3.1 case 'n': /* compute processes */
108     if (i >= argc-2)
109     goto userr;
110     ncprocs = atoi(argv[++i]);
111     break;
112     case 'o': /* output display */
113     if (i >= argc-2)
114     goto userr;
115     outdev = argv[++i];
116     break;
117     default:
118     goto userr;
119     }
120     /* get root file name */
121 gregl 3.32 if (i >= argc)
122     goto userr;
123 gregl 3.1 rootname(froot, hdkfile=argv[i++]);
124 gregl 3.27 /* load variables? */
125     if (i < argc)
126     if (argv[i][0] != '-' && argv[i][0] != '+')
127     loadvars(argv[i]); /* load variables from file */
128    
129     if (i >= argc || argv[i][0] == '+')
130     loadholo(); /* load existing holodeck */
131    
132     while (++i < argc) /* get command line settings */
133     if (setvariable(argv[i], matchvar) < 0) {
134     sprintf(errmsg, "unknown variable: %s", argv[i]);
135     error(USER, errmsg);
136     }
137     /* check settings */
138     checkvalues();
139 gregl 3.36 /* load rad input file */
140     getradfile();
141 gregl 3.27
142 gregl 3.28 if (hdlist[0] == NULL) { /* create new holodeck */
143 gregl 3.29 HDGRID hdg[HDMAX];
144 gregl 3.1 /* set defaults */
145 gregl 3.29 setdefaults(hdg);
146 gwlarson 3.51 /* check read-only */
147     if (force < 0)
148     error(USER, "cannot create read-only holodeck");
149 gregl 3.1 /* 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 gregl 3.29 creatholo(hdg);
155 gregl 3.27 } else /* else just set defaults */
156 gregl 3.15 setdefaults(NULL);
157 gregl 3.1 /* initialize */
158     initrholo();
159 gregl 3.15 /* main loop */
160 gregl 3.1 while (rholo())
161     ;
162     /* done */
163     quit(0);
164     userr:
165     fprintf(stderr,
166 greg 3.74 "Usage: %s [-n nprocs][-o disp][-i][-w][-r|-f] output.hdk [control.hif|+|- [VAR=val ..]]\n",
167 gregl 3.1 progname);
168     quit(1);
169 schorsch 3.64 return 1; /* pro forma return */
170 gregl 3.1 }
171    
172    
173 schorsch 3.64 static void
174     onsig( /* fatal signal */
175     int signo
176     )
177 gregl 3.12 {
178     static int gotsig = 0;
179    
180 gwlarson 3.47 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 gregl 3.12 _exit(signo);
185 gwlarson 3.47 }
186 greg 3.77 alarm(300); /* allow 10 minutes to clean up */
187 gregl 3.12 eputs("signal - ");
188     eputs(sigerr[signo]);
189     eputs("\n");
190     quit(3);
191     }
192    
193    
194 schorsch 3.64 static void
195     sigdie( /* set fatal signal */
196     int signo,
197     char *msg
198     )
199 gregl 3.12 {
200     if (signal(signo, onsig) == SIG_IGN)
201     signal(signo, SIG_IGN);
202     sigerr[signo] = msg;
203     }
204    
205    
206 schorsch 3.64 static int
207     resfmode( /* restrict open file access mode */
208     int fd,
209     int mod
210     )
211 gregl 3.12 {
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 gregl 3.35 if (mod == (stbuf.st_mode & 0777))
218 gregl 3.12 return(-1); /* already set */
219     /* else change it */
220     if (fchmod(fd, mod) < 0) {
221     error(WARNING, "cannot change holodeck file access mode");
222     return(-1);
223     }
224     return(stbuf.st_mode); /* return original mode */
225     }
226    
227    
228 schorsch 3.64 static void
229     initrholo(void) /* get our holodeck running */
230 gregl 3.1 {
231 greg 3.80 int i;
232 gregl 3.17 /* close holodeck on exec() */
233     fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC);
234 gregl 3.4
235     if (outdev != NULL) /* open output device */
236     disp_open(outdev);
237 gregl 3.1 else if (ncprocs > 0) /* else use global ray feed */
238     init_global();
239 gregl 3.8 /* record disk space limit */
240     if (!vdef(DISKSPACE))
241 greg 3.58 maxdisk = ((off_t)1<<(sizeof(off_t)*8-2)) - 1024;
242 gregl 3.8 else
243 gregl 3.9 maxdisk = 1024.*1024.*vflt(DISKSPACE);
244 gregl 3.1 /* set up memory cache */
245 gregl 3.5 if (outdev == NULL)
246 gwlarson 3.50 hdcachesize = 0; /* manual flushing */
247 gregl 3.5 else if (vdef(CACHE))
248     hdcachesize = 1024.*1024.*vflt(CACHE);
249 gregl 3.1 /* open report file */
250     if (vdef(REPORT)) {
251 greg 3.80 char *s = sskip2(vval(REPORT), 1);
252 gregl 3.1 if (*s && freopen(s, "a", stderr) == NULL)
253     quit(2);
254     }
255 gregl 3.33 /* 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 gregl 3.1 /* start rtrace */
263     if (ncprocs > 0) {
264 gwlarson 3.38 totqlen = i = start_rtrace();
265 gregl 3.1 if (i < 1)
266 gregl 3.20 error(USER, "cannot start rtrace process(es)");
267 gregl 3.1 if (vdef(REPORT)) { /* make first report */
268     printargs(rtargc, rtargv, stderr);
269     report(0);
270     }
271     /* allocate packets */
272     freepacks = (PACKET *)bmalloc(i*sizeof(PACKET));
273     if (freepacks == NULL)
274     goto memerr;
275     freepacks[--i].nr = 0;
276     freepacks[i].next = NULL;
277 gregl 3.25 if (!vdef(OBSTRUCTIONS) || !vbool(OBSTRUCTIONS)) {
278 gregl 3.1 freepacks[i].offset = (float *)bmalloc(
279     RPACKSIZ*sizeof(float)*(i+1) );
280     if (freepacks[i].offset == NULL)
281     goto memerr;
282     } else
283     freepacks[i].offset = NULL;
284     while (i--) {
285     freepacks[i].nr = 0;
286     freepacks[i].offset = freepacks[i+1].offset == NULL ?
287     NULL : freepacks[i+1].offset+RPACKSIZ ;
288     freepacks[i].next = &freepacks[i+1];
289     }
290     }
291 gregl 3.12 /* set up signal handling */
292     sigdie(SIGINT, "Interrupt");
293 schorsch 3.62 sigdie(SIGTERM, "Terminate");
294     #ifdef SIGHUP
295 gregl 3.12 sigdie(SIGHUP, "Hangup");
296 schorsch 3.62 #endif
297     #ifdef SIGPIPE
298 gregl 3.12 sigdie(SIGPIPE, "Broken pipe");
299 schorsch 3.62 #endif
300     #ifdef SIGALRM
301 gregl 3.12 sigdie(SIGALRM, "Alarm clock");
302 schorsch 3.62 #endif
303 gregl 3.12 #ifdef SIGXCPU
304     sigdie(SIGXCPU, "CPU limit exceeded");
305 schorsch 3.62 #endif
306     #ifdef SIGXFSZ
307 gregl 3.12 sigdie(SIGXFSZ, "File size exceeded");
308     #endif
309 gwlarson 3.50 /* protect holodeck file */
310 schorsch 3.60 orig_mode = resfmode(hdlist[0]->fd, (ncprocs>0) & (force>=0) ? 0 : 0444);
311 gregl 3.1 return;
312     memerr:
313     error(SYSTEM, "out of memory in initrholo");
314     }
315    
316    
317 schorsch 3.64 static int
318     rholo(void) /* holodeck main loop */
319 gregl 3.1 {
320 greg 3.71 static off_t nextfragwarn = 100L<<20;
321 gregl 3.30 static int idle = 0;
322 gregl 3.1 PACKET *pl = NULL, *plend;
323 greg 3.68 off_t fsiz;
324 gregl 3.33 int pksiz;
325 greg 3.80 PACKET *p;
326 gregl 3.1 time_t t;
327 gregl 3.30 /* check display */
328     if (nprocs <= 0)
329     idle = 1;
330     if (outdev != NULL) {
331 gregl 3.4 if (!disp_check(idle))
332 gregl 3.30 return(0); /* quit request */
333     if (nprocs <= 0)
334     return(1);
335     } else if (idle)
336     return(0); /* all done */
337 gwlarson 3.44 fsiz = hdfilen(hdlist[0]->fd); /* check file size */
338     if (maxdisk > 0 && fsiz >= maxdisk) {
339 greg 3.70 error(USER, "file limit exceeded");
340 gregl 3.21 done_rtrace();
341     return(1); /* comes back */
342 gregl 3.8 }
343 gwlarson 3.44 #if FRAGWARN
344 greg 3.68 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 gwlarson 3.44 }
354     #endif
355 gregl 3.33 t = time(NULL); /* check time */
356 gregl 3.8 if (endtime > 0 && t >= endtime) {
357 greg 3.70 error(USER, "time limit exceeded");
358 gregl 3.21 done_rtrace();
359     return(1); /* comes back */
360 gregl 3.8 }
361 gregl 3.3 if (reporttime > 0 && t >= reporttime)
362 gregl 3.1 report(t);
363 gregl 3.33 /* figure out good packet size */
364 gwlarson 3.48 pksiz = RPACKSIZ;
365 gregl 3.33 #if MAXQTIME
366 gwlarson 3.48 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 gregl 3.33 #endif
372 gregl 3.28 idle = 0; /* get packets to process */
373 gregl 3.1 while (freepacks != NULL) {
374     p = freepacks; freepacks = p->next; p->next = NULL;
375 gregl 3.33 if (!next_packet(p, pksiz)) {
376 gregl 3.1 p->next = freepacks; freepacks = p;
377 gregl 3.21 idle = 1;
378 gregl 3.1 break;
379     }
380     if (pl == NULL) pl = p;
381     else plend->next = p;
382     plend = p;
383     }
384 gregl 3.21 /* process packets */
385 gregl 3.1 done_packets(do_packets(pl));
386     return(1); /* and continue */
387     }
388    
389    
390 schorsch 3.64 static void
391     setdefaults( /* set default values */
392 greg 3.80 HDGRID *gp
393 schorsch 3.64 )
394 gregl 3.1 {
395 greg 3.80 int i;
396 gregl 3.29 int n;
397 gregl 3.37 double len[3], d;
398 gregl 3.1
399     if (!vdef(SECTION)) {
400     sprintf(errmsg, "%s must be defined", vnam(SECTION));
401     error(USER, errmsg);
402     }
403     if (!vdef(OCTREE)) {
404     if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL)
405     error(SYSTEM, "out of memory");
406     sprintf(vval(OCTREE), "%s.oct", froot);
407     vdef(OCTREE)++;
408 gregl 3.14 }
409     if (!vdef(VDIST)) {
410     vval(VDIST) = "F";
411     vdef(VDIST)++;
412 gregl 3.1 }
413     /* append rendering options */
414     if (vdef(RENDER))
415 greg 3.78 rtargc += wordstring(rtargv+rtargc, MAXRTARGC-rtargc, vval(RENDER));
416 gregl 3.15
417     if (gp == NULL) /* already initialized? */
418     return;
419     /* set grid parameters */
420 gregl 3.29 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 gregl 3.16 "%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd",
424 gregl 3.29 &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 gregl 3.37 len[i] = VLEN(gp->xv[i]);
432     if (!vdef(GRID)) {
433 gwlarson 3.43 d = 2/2e5*( len[0]*len[0]*(len[1]*len[1] +
434 gregl 3.37 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 gregl 3.29 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 gregl 3.15 }
444 gregl 3.1 }
445    
446    
447 schorsch 3.64 static void
448     creatholo( /* create a holodeck output file */
449     HDGRID *gp
450     )
451 gregl 3.1 {
452 gregl 3.12 extern char VersionID[];
453 greg 3.75 off_t lastloc, nextloc;
454 gregl 3.29 int n;
455 gregl 3.10 int fd;
456 gregl 3.1 FILE *fp;
457     /* open & truncate file */
458 greg 3.81 if ((fp = fopen(hdkfile, "wb+")) == NULL) {
459 gregl 3.1 sprintf(errmsg, "cannot open \"%s\" for writing", hdkfile);
460     error(SYSTEM, errmsg);
461     }
462     /* write information header */
463     newheader("RADIANCE", fp);
464 gregl 3.12 fprintf(fp, "SOFTWARE= %s\n", VersionID);
465 greg 3.85 fputnow(fp);
466 gregl 3.1 printvars(fp);
467 greg 3.82 fputendian(fp);
468 gregl 3.1 fputformat(HOLOFMT, fp);
469     fputc('\n', fp);
470 gregl 3.29 putw(HOLOMAGIC, fp); /* put magic number */
471 gregl 3.10 fd = dup(fileno(fp));
472     fclose(fp); /* flush and close stdio stream */
473 greg 3.61 lastloc = lseek(fd, (off_t)0, SEEK_END);
474 gregl 3.29 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 greg 3.75 if (lseek(fd, lastloc, SEEK_SET) < 0)
482 gregl 3.29 error(SYSTEM,
483     "cannot seek on holodeck file in creatholo");
484     write(fd, (char *)&nextloc, sizeof(nextloc));
485 greg 3.75 lseek(fd, (lastloc=nextloc), SEEK_SET);
486 gregl 3.29 }
487 gregl 3.1 }
488    
489    
490 schorsch 3.64 static int
491     headline( /* process information header line */
492 schorsch 3.65 char *s,
493     void *p
494 schorsch 3.64 )
495 gregl 3.1 {
496     extern char FMTSTR[];
497 greg 3.80 char *cp;
498 greg 3.79 char fmt[MAXFMTLEN];
499 gregl 3.1
500 gregl 3.2 if (formatval(fmt, s)) {
501 gregl 3.1 if (strcmp(fmt, HOLOFMT)) {
502     sprintf(errmsg, "%s file \"%s\" has %s%s",
503     HOLOFMT, hdkfile, FMTSTR, fmt);
504     error(USER, errmsg);
505     }
506 gwlarson 3.42 return(0);
507 gregl 3.1 }
508     for (cp = s; *cp; cp++) /* take off any comments */
509     if (*cp == '#') {
510     *cp = '\0';
511     break;
512     }
513     setvariable(s, matchvar); /* don't flag errors */
514 gwlarson 3.42 return(0);
515 gregl 3.1 }
516    
517    
518 schorsch 3.64 static void
519     loadholo(void) /* start loading a holodeck from fname */
520 gregl 3.1 {
521     FILE *fp;
522 gregl 3.10 int fd;
523 gregl 3.29 int n;
524 greg 3.75 off_t nextloc;
525 gwlarson 3.51
526 schorsch 3.60 if ((ncprocs > 0) & (force >= 0))
527 greg 3.81 fp = fopen(hdkfile, "rb+");
528 gwlarson 3.51 else
529     fp = NULL;
530     if (fp == NULL) {
531 greg 3.81 if ((fp = fopen(hdkfile, "rb")) == NULL) {
532 gwlarson 3.51 sprintf(errmsg, "cannot open \"%s\"", hdkfile);
533     error(SYSTEM, errmsg);
534     }
535     if (ncprocs > 0) {
536 greg 3.72 sprintf(errmsg, "\"%s\" is read-only", hdkfile);
537     if (outdev == NULL)
538     error(USER, errmsg);
539     strcat(errmsg, "; new rays will be discarded");
540 gwlarson 3.51 error(WARNING, errmsg);
541     force = -1;
542     }
543 gregl 3.1 }
544     /* load variables from header */
545     getheader(fp, headline, NULL);
546     /* check magic number */
547     if (getw(fp) != HOLOMAGIC) {
548     sprintf(errmsg, "bad magic number in holodeck file \"%s\"",
549     hdkfile);
550     error(USER, errmsg);
551     }
552 gregl 3.29 nextloc = ftell(fp); /* get stdio position */
553 gregl 3.10 fd = dup(fileno(fp));
554     fclose(fp); /* done with stdio */
555 gregl 3.29 for (n = 0; nextloc > 0L; n++) { /* initialize each section */
556 greg 3.75 lseek(fd, nextloc, SEEK_SET);
557 gregl 3.29 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 gregl 3.1 }
566    
567    
568 greg 3.80 void
569 schorsch 3.64 done_packets( /* handle finished packets */
570     PACKET *pl
571     )
572 gregl 3.1 {
573 gregl 3.13 static int n2flush = 0;
574 greg 3.80 PACKET *p;
575 gregl 3.1
576     while (pl != NULL) {
577     p = pl; pl = p->next; p->next = NULL;
578     if (p->nr > 0) { /* add to holodeck */
579 schorsch 3.59 memcpy( (void *)hdnewrays(hdlist[p->hd],p->bi,p->nr),
580     (void *)p->ra,
581 gregl 3.1 p->nr*sizeof(RAYVAL));
582     if (outdev != NULL) /* display it */
583 gregl 3.7 disp_packet((PACKHEAD *)p);
584 gwlarson 3.49 if (hdcachesize <= 0)
585 gregl 3.33 n2flush++;
586 gregl 3.6 nraysdone += p->nr;
587     npacksdone++;
588 gregl 3.33 p->nr = 0;
589 gregl 3.1 }
590 gregl 3.33 p->next = freepacks; /* push onto free list */
591 gregl 3.1 freepacks = p;
592 gregl 3.5 }
593 gwlarson 3.50 if (n2flush >= RTFLUSH) {
594 gwlarson 3.46 if (outdev != NULL)
595     hdsync(NULL, 1);
596 gwlarson 3.41 else
597 gwlarson 3.46 hdflush(NULL);
598 gregl 3.13 n2flush = 0;
599 gregl 3.1 }
600     }
601    
602    
603 schorsch 3.64 static void
604     rootname( /* remove tail from end of fn */
605 greg 3.80 char *rn,
606     char *fn
607 schorsch 3.64 )
608 gregl 3.1 {
609     char *tp, *dp;
610    
611 schorsch 3.60 for (tp = NULL, dp = rn; (*rn = *fn++); rn++) {
612 gregl 3.22 if (*rn == '/')
613 gregl 3.1 dp = rn;
614     else if (*rn == '.')
615     tp = rn;
616 schorsch 3.60 }
617 gregl 3.1 if (tp != NULL && tp > dp)
618     *tp = '\0';
619     }
620    
621    
622 schorsch 3.64 static void
623     badvalue( /* report bad variable value and exit */
624     int vc
625     )
626 gregl 3.1 {
627     sprintf(errmsg, "bad value for variable '%s'", vnam(vc));
628     error(USER, errmsg);
629     }
630    
631    
632 greg 3.52 void
633 greg 3.83 eputs(const char *s) /* put error message to stderr */
634 gregl 3.1 {
635     static int midline = 0;
636    
637     if (!*s)
638     return;
639     if (!midline++) { /* prepend line with program name */
640     fputs(progname, stderr);
641     fputs(": ", stderr);
642     }
643     fputs(s, stderr);
644     if (s[strlen(s)-1] == '\n') {
645     fflush(stderr);
646     midline = 0;
647     }
648     }
649    
650    
651 greg 3.52 void
652 greg 3.83 quit(int ec) /* exit program gracefully */
653 gregl 3.1 {
654     int status = 0;
655    
656 gregl 3.24 if (hdlist[0] != NULL) { /* close holodeck */
657 gregl 3.21 if (nprocs > 0)
658 gregl 3.24 status = done_rtrace(); /* calls hdsync() */
659 schorsch 3.60 if ((ncprocs > 0) & (force >= 0) && vdef(REPORT)) {
660 greg 3.54 off_t fsiz, fuse;
661 gregl 3.19 fsiz = hdfilen(hdlist[0]->fd);
662 greg 3.67 fuse = hdfiluse(hdlist[0]->fd);
663 gregl 3.19 fprintf(stderr,
664 gregl 3.3 "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n",
665 gregl 3.19 hdkfile, fsiz/(1024.*1024.),
666     100.*(fsiz-fuse)/fsiz);
667     }
668 gregl 3.1 }
669 gregl 3.12 if (orig_mode >= 0) /* reset holodeck access mode */
670     fchmod(hdlist[0]->fd, orig_mode);
671 gregl 3.11 if (outdev != NULL) /* close display */
672     disp_close();
673 gregl 3.1 exit(ec ? ec : status); /* exit */
674     }