1 |
gregl |
3.1 |
#ifndef lint |
2 |
greg |
3.61 |
static const char RCSid[] = "$Id: rholo.c,v 3.60 2003/07/27 22:12:02 schorsch Exp $"; |
3 |
gregl |
3.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Radiance holodeck generation controller |
6 |
|
|
*/ |
7 |
|
|
|
8 |
schorsch |
3.59 |
#include <signal.h> |
9 |
|
|
#include <sys/stat.h> |
10 |
|
|
#include <string.h> |
11 |
|
|
|
12 |
gregl |
3.1 |
#include "rholo.h" |
13 |
greg |
3.61 |
#include "platform.h" |
14 |
gregl |
3.14 |
#include "random.h" |
15 |
gregl |
3.1 |
|
16 |
gwlarson |
3.44 |
#ifndef FRAGWARN |
17 |
|
|
#define FRAGWARN 20 /* fragmentation for warning (%) */ |
18 |
|
|
#endif |
19 |
gregl |
3.33 |
#ifndef MAXQTIME |
20 |
|
|
#define MAXQTIME 5 /* target maximum seconds in queue */ |
21 |
|
|
#endif |
22 |
gwlarson |
3.49 |
/* 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 |
gregl |
3.1 |
/* the following must be consistent with rholo.h */ |
31 |
|
|
int NVARS = NRHVARS; /* total number of variables */ |
32 |
|
|
|
33 |
|
|
VARIABLE vv[] = RHVINIT; /* variable-value pairs */ |
34 |
|
|
|
35 |
|
|
char *progname; /* our program name */ |
36 |
|
|
char *hdkfile; /* holodeck file name */ |
37 |
gregl |
3.22 |
char froot[256]; /* root file name */ |
38 |
gregl |
3.1 |
|
39 |
|
|
int ncprocs = 0; /* desired number of compute processes */ |
40 |
|
|
|
41 |
|
|
char *outdev = NULL; /* output device name */ |
42 |
|
|
|
43 |
gregl |
3.17 |
int readinp = 0; /* read commands from stdin */ |
44 |
|
|
|
45 |
gwlarson |
3.51 |
int force = 0; /* allow overwrite of holodeck (-1 == read-only) */ |
46 |
gregl |
3.20 |
|
47 |
gregl |
3.1 |
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 |
greg |
3.54 |
off_t maxdisk; /* maximum file space (bytes) */ |
52 |
gregl |
3.8 |
|
53 |
gregl |
3.1 |
int rtargc = 1; /* rtrace command */ |
54 |
|
|
char *rtargv[128] = {"rtrace", NULL}; |
55 |
|
|
|
56 |
gregl |
3.12 |
int orig_mode = -1; /* original file mode (-1 if unchanged) */ |
57 |
|
|
|
58 |
gregl |
3.1 |
long nraysdone = 0L; /* number of rays done */ |
59 |
|
|
long npacksdone = 0L; /* number of packets done */ |
60 |
|
|
|
61 |
|
|
PACKET *freepacks; /* available packets */ |
62 |
gwlarson |
3.49 |
int totqlen; /* maximum queue length (number of packets) */ |
63 |
gregl |
3.1 |
|
64 |
gregl |
3.12 |
char *sigerr[NSIG]; /* signal error messages */ |
65 |
|
|
|
66 |
gwlarson |
3.40 |
extern int nowarn; /* turn warnings off? */ |
67 |
|
|
|
68 |
gregl |
3.1 |
extern time_t time(); |
69 |
|
|
|
70 |
|
|
|
71 |
|
|
main(argc, argv) |
72 |
|
|
int argc; |
73 |
|
|
char *argv[]; |
74 |
|
|
{ |
75 |
|
|
int i; |
76 |
gregl |
3.33 |
|
77 |
gregl |
3.18 |
initurand(16384); /* initialize urand */ |
78 |
gregl |
3.1 |
progname = argv[0]; /* get arguments */ |
79 |
|
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
80 |
|
|
switch (argv[i][1]) { |
81 |
|
|
case 'w': /* turn off warnings */ |
82 |
|
|
nowarn++; |
83 |
|
|
break; |
84 |
|
|
case 'f': /* force overwrite */ |
85 |
gwlarson |
3.51 |
force = 1; |
86 |
gregl |
3.1 |
break; |
87 |
gwlarson |
3.51 |
case 'r': /* read-only mode */ |
88 |
|
|
force = -1; |
89 |
|
|
break; |
90 |
gregl |
3.17 |
case 'i': /* read input from stdin */ |
91 |
|
|
readinp++; |
92 |
|
|
break; |
93 |
gregl |
3.1 |
case 'n': /* compute processes */ |
94 |
|
|
if (i >= argc-2) |
95 |
|
|
goto userr; |
96 |
|
|
ncprocs = atoi(argv[++i]); |
97 |
|
|
break; |
98 |
|
|
case 'o': /* output display */ |
99 |
|
|
if (i >= argc-2) |
100 |
|
|
goto userr; |
101 |
|
|
outdev = argv[++i]; |
102 |
|
|
break; |
103 |
|
|
default: |
104 |
|
|
goto userr; |
105 |
|
|
} |
106 |
|
|
/* get root file name */ |
107 |
gregl |
3.32 |
if (i >= argc) |
108 |
|
|
goto userr; |
109 |
gregl |
3.1 |
rootname(froot, hdkfile=argv[i++]); |
110 |
gregl |
3.27 |
/* 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 |
gregl |
3.36 |
/* load rad input file */ |
126 |
|
|
getradfile(); |
127 |
gregl |
3.27 |
|
128 |
gregl |
3.28 |
if (hdlist[0] == NULL) { /* create new holodeck */ |
129 |
gregl |
3.29 |
HDGRID hdg[HDMAX]; |
130 |
gregl |
3.1 |
/* set defaults */ |
131 |
gregl |
3.29 |
setdefaults(hdg); |
132 |
gwlarson |
3.51 |
/* check read-only */ |
133 |
|
|
if (force < 0) |
134 |
|
|
error(USER, "cannot create read-only holodeck"); |
135 |
gregl |
3.1 |
/* 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 |
gregl |
3.29 |
creatholo(hdg); |
141 |
gregl |
3.27 |
} else /* else just set defaults */ |
142 |
gregl |
3.15 |
setdefaults(NULL); |
143 |
gregl |
3.1 |
/* initialize */ |
144 |
|
|
initrholo(); |
145 |
gregl |
3.15 |
/* main loop */ |
146 |
gregl |
3.1 |
while (rholo()) |
147 |
|
|
; |
148 |
|
|
/* done */ |
149 |
|
|
quit(0); |
150 |
|
|
userr: |
151 |
|
|
fprintf(stderr, |
152 |
gwlarson |
3.51 |
"Usage: %s [-n nprocs][-o disp][-w][-r|-f] output.hdk [control.hif|+|- [VAR=val ..]]\n", |
153 |
gregl |
3.1 |
progname); |
154 |
|
|
quit(1); |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
|
158 |
schorsch |
3.55 |
void |
159 |
gregl |
3.12 |
onsig(signo) /* fatal signal */ |
160 |
|
|
int signo; |
161 |
|
|
{ |
162 |
|
|
static int gotsig = 0; |
163 |
|
|
|
164 |
gwlarson |
3.47 |
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 |
gregl |
3.12 |
_exit(signo); |
169 |
gwlarson |
3.47 |
} |
170 |
|
|
alarm(300); /* allow 5 minutes to clean up */ |
171 |
gregl |
3.12 |
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 |
gregl |
3.35 |
if (mod == (stbuf.st_mode & 0777)) |
198 |
gregl |
3.12 |
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 |
gregl |
3.1 |
initrholo() /* get our holodeck running */ |
209 |
|
|
{ |
210 |
|
|
extern int global_packet(); |
211 |
|
|
register int i; |
212 |
gregl |
3.17 |
/* close holodeck on exec() */ |
213 |
|
|
fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC); |
214 |
gregl |
3.4 |
|
215 |
|
|
if (outdev != NULL) /* open output device */ |
216 |
|
|
disp_open(outdev); |
217 |
gregl |
3.1 |
else if (ncprocs > 0) /* else use global ray feed */ |
218 |
|
|
init_global(); |
219 |
gregl |
3.8 |
/* record disk space limit */ |
220 |
|
|
if (!vdef(DISKSPACE)) |
221 |
greg |
3.58 |
maxdisk = ((off_t)1<<(sizeof(off_t)*8-2)) - 1024; |
222 |
gregl |
3.8 |
else |
223 |
gregl |
3.9 |
maxdisk = 1024.*1024.*vflt(DISKSPACE); |
224 |
gregl |
3.1 |
/* set up memory cache */ |
225 |
gregl |
3.5 |
if (outdev == NULL) |
226 |
gwlarson |
3.50 |
hdcachesize = 0; /* manual flushing */ |
227 |
gregl |
3.5 |
else if (vdef(CACHE)) |
228 |
|
|
hdcachesize = 1024.*1024.*vflt(CACHE); |
229 |
gregl |
3.1 |
/* 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 |
gregl |
3.33 |
/* 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 |
gregl |
3.1 |
/* start rtrace */ |
243 |
|
|
if (ncprocs > 0) { |
244 |
gwlarson |
3.38 |
totqlen = i = start_rtrace(); |
245 |
gregl |
3.1 |
if (i < 1) |
246 |
gregl |
3.20 |
error(USER, "cannot start rtrace process(es)"); |
247 |
gregl |
3.1 |
if (vdef(REPORT)) { /* make first report */ |
248 |
|
|
printargs(rtargc, rtargv, stderr); |
249 |
|
|
report(0); |
250 |
|
|
} |
251 |
|
|
/* allocate packets */ |
252 |
|
|
freepacks = (PACKET *)bmalloc(i*sizeof(PACKET)); |
253 |
|
|
if (freepacks == NULL) |
254 |
|
|
goto memerr; |
255 |
|
|
freepacks[--i].nr = 0; |
256 |
|
|
freepacks[i].next = NULL; |
257 |
gregl |
3.25 |
if (!vdef(OBSTRUCTIONS) || !vbool(OBSTRUCTIONS)) { |
258 |
gregl |
3.1 |
freepacks[i].offset = (float *)bmalloc( |
259 |
|
|
RPACKSIZ*sizeof(float)*(i+1) ); |
260 |
|
|
if (freepacks[i].offset == NULL) |
261 |
|
|
goto memerr; |
262 |
|
|
} else |
263 |
|
|
freepacks[i].offset = NULL; |
264 |
|
|
while (i--) { |
265 |
|
|
freepacks[i].nr = 0; |
266 |
|
|
freepacks[i].offset = freepacks[i+1].offset == NULL ? |
267 |
|
|
NULL : freepacks[i+1].offset+RPACKSIZ ; |
268 |
|
|
freepacks[i].next = &freepacks[i+1]; |
269 |
|
|
} |
270 |
|
|
} |
271 |
gregl |
3.12 |
/* 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 |
gwlarson |
3.50 |
/* protect holodeck file */ |
282 |
schorsch |
3.60 |
orig_mode = resfmode(hdlist[0]->fd, (ncprocs>0) & (force>=0) ? 0 : 0444); |
283 |
gregl |
3.1 |
return; |
284 |
|
|
memerr: |
285 |
|
|
error(SYSTEM, "out of memory in initrholo"); |
286 |
|
|
} |
287 |
|
|
|
288 |
|
|
|
289 |
|
|
rholo() /* holodeck main loop */ |
290 |
|
|
{ |
291 |
gwlarson |
3.44 |
static long nextfragwarn = 100*(1L<<20); |
292 |
gregl |
3.30 |
static int idle = 0; |
293 |
gregl |
3.1 |
PACKET *pl = NULL, *plend; |
294 |
greg |
3.54 |
off_t fsiz; |
295 |
gregl |
3.33 |
int pksiz; |
296 |
gregl |
3.1 |
register PACKET *p; |
297 |
|
|
time_t t; |
298 |
gregl |
3.30 |
/* check display */ |
299 |
|
|
if (nprocs <= 0) |
300 |
|
|
idle = 1; |
301 |
|
|
if (outdev != NULL) { |
302 |
gregl |
3.4 |
if (!disp_check(idle)) |
303 |
gregl |
3.30 |
return(0); /* quit request */ |
304 |
|
|
if (nprocs <= 0) |
305 |
|
|
return(1); |
306 |
|
|
} else if (idle) |
307 |
|
|
return(0); /* all done */ |
308 |
gwlarson |
3.44 |
fsiz = hdfilen(hdlist[0]->fd); /* check file size */ |
309 |
|
|
if (maxdisk > 0 && fsiz >= maxdisk) { |
310 |
gregl |
3.8 |
error(WARNING, "file limit exceeded"); |
311 |
gregl |
3.21 |
done_rtrace(); |
312 |
|
|
return(1); /* comes back */ |
313 |
gregl |
3.8 |
} |
314 |
gwlarson |
3.44 |
#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 |
gregl |
3.33 |
t = time(NULL); /* check time */ |
324 |
gregl |
3.8 |
if (endtime > 0 && t >= endtime) { |
325 |
|
|
error(WARNING, "time limit exceeded"); |
326 |
gregl |
3.21 |
done_rtrace(); |
327 |
|
|
return(1); /* comes back */ |
328 |
gregl |
3.8 |
} |
329 |
gregl |
3.3 |
if (reporttime > 0 && t >= reporttime) |
330 |
gregl |
3.1 |
report(t); |
331 |
gregl |
3.33 |
/* figure out good packet size */ |
332 |
gwlarson |
3.48 |
pksiz = RPACKSIZ; |
333 |
gregl |
3.33 |
#if MAXQTIME |
334 |
gwlarson |
3.48 |
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 |
gregl |
3.33 |
#endif |
340 |
gregl |
3.28 |
idle = 0; /* get packets to process */ |
341 |
gregl |
3.1 |
while (freepacks != NULL) { |
342 |
|
|
p = freepacks; freepacks = p->next; p->next = NULL; |
343 |
gregl |
3.33 |
if (!next_packet(p, pksiz)) { |
344 |
gregl |
3.1 |
p->next = freepacks; freepacks = p; |
345 |
gregl |
3.21 |
idle = 1; |
346 |
gregl |
3.1 |
break; |
347 |
|
|
} |
348 |
|
|
if (pl == NULL) pl = p; |
349 |
|
|
else plend->next = p; |
350 |
|
|
plend = p; |
351 |
|
|
} |
352 |
gregl |
3.21 |
/* process packets */ |
353 |
gregl |
3.1 |
done_packets(do_packets(pl)); |
354 |
|
|
return(1); /* and continue */ |
355 |
|
|
} |
356 |
|
|
|
357 |
|
|
|
358 |
|
|
setdefaults(gp) /* set default values */ |
359 |
|
|
register HDGRID *gp; |
360 |
|
|
{ |
361 |
|
|
extern char *atos(); |
362 |
|
|
register int i; |
363 |
gregl |
3.29 |
int n; |
364 |
gregl |
3.37 |
double len[3], d; |
365 |
gregl |
3.1 |
char buf[64]; |
366 |
|
|
|
367 |
|
|
if (!vdef(SECTION)) { |
368 |
|
|
sprintf(errmsg, "%s must be defined", vnam(SECTION)); |
369 |
|
|
error(USER, errmsg); |
370 |
|
|
} |
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 |
gregl |
3.14 |
} |
377 |
|
|
if (!vdef(VDIST)) { |
378 |
|
|
vval(VDIST) = "F"; |
379 |
|
|
vdef(VDIST)++; |
380 |
gregl |
3.1 |
} |
381 |
|
|
/* append rendering options */ |
382 |
|
|
if (vdef(RENDER)) |
383 |
|
|
rtargc += wordstring(rtargv+rtargc, vval(RENDER)); |
384 |
gregl |
3.15 |
|
385 |
|
|
if (gp == NULL) /* already initialized? */ |
386 |
|
|
return; |
387 |
|
|
/* set grid parameters */ |
388 |
gregl |
3.29 |
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 |
gregl |
3.16 |
"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd", |
392 |
gregl |
3.29 |
&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 |
gregl |
3.37 |
len[i] = VLEN(gp->xv[i]); |
400 |
|
|
if (!vdef(GRID)) { |
401 |
gwlarson |
3.43 |
d = 2/2e5*( len[0]*len[0]*(len[1]*len[1] + |
402 |
gregl |
3.37 |
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 |
gregl |
3.29 |
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 |
gregl |
3.15 |
} |
412 |
gregl |
3.1 |
} |
413 |
|
|
|
414 |
|
|
|
415 |
|
|
creatholo(gp) /* create a holodeck output file */ |
416 |
|
|
HDGRID *gp; |
417 |
|
|
{ |
418 |
gregl |
3.12 |
extern char VersionID[]; |
419 |
greg |
3.57 |
int32 lastloc, nextloc; |
420 |
gregl |
3.29 |
int n; |
421 |
gregl |
3.10 |
int fd; |
422 |
gregl |
3.1 |
FILE *fp; |
423 |
|
|
/* open & truncate file */ |
424 |
|
|
if ((fp = fopen(hdkfile, "w+")) == NULL) { |
425 |
|
|
sprintf(errmsg, "cannot open \"%s\" for writing", hdkfile); |
426 |
|
|
error(SYSTEM, errmsg); |
427 |
|
|
} |
428 |
|
|
/* write information header */ |
429 |
|
|
newheader("RADIANCE", fp); |
430 |
gregl |
3.12 |
fprintf(fp, "SOFTWARE= %s\n", VersionID); |
431 |
gregl |
3.1 |
printvars(fp); |
432 |
|
|
fputformat(HOLOFMT, fp); |
433 |
|
|
fputc('\n', fp); |
434 |
gregl |
3.29 |
putw(HOLOMAGIC, fp); /* put magic number */ |
435 |
gregl |
3.10 |
fd = dup(fileno(fp)); |
436 |
|
|
fclose(fp); /* flush and close stdio stream */ |
437 |
greg |
3.61 |
lastloc = lseek(fd, (off_t)0, SEEK_END); |
438 |
gregl |
3.29 |
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 |
greg |
3.61 |
if (lseek(fd, (off_t)lastloc, SEEK_SET) < 0) |
446 |
gregl |
3.29 |
error(SYSTEM, |
447 |
|
|
"cannot seek on holodeck file in creatholo"); |
448 |
|
|
write(fd, (char *)&nextloc, sizeof(nextloc)); |
449 |
greg |
3.61 |
lseek(fd, (off_t)(lastloc=nextloc), SEEK_SET); |
450 |
gregl |
3.29 |
} |
451 |
gregl |
3.1 |
} |
452 |
|
|
|
453 |
|
|
|
454 |
gwlarson |
3.42 |
int |
455 |
gregl |
3.1 |
headline(s) /* process information header line */ |
456 |
|
|
char *s; |
457 |
|
|
{ |
458 |
|
|
extern char FMTSTR[]; |
459 |
|
|
register char *cp; |
460 |
|
|
char fmt[32]; |
461 |
|
|
|
462 |
gregl |
3.2 |
if (formatval(fmt, s)) { |
463 |
gregl |
3.1 |
if (strcmp(fmt, HOLOFMT)) { |
464 |
|
|
sprintf(errmsg, "%s file \"%s\" has %s%s", |
465 |
|
|
HOLOFMT, hdkfile, FMTSTR, fmt); |
466 |
|
|
error(USER, errmsg); |
467 |
|
|
} |
468 |
gwlarson |
3.42 |
return(0); |
469 |
gregl |
3.1 |
} |
470 |
|
|
for (cp = s; *cp; cp++) /* take off any comments */ |
471 |
|
|
if (*cp == '#') { |
472 |
|
|
*cp = '\0'; |
473 |
|
|
break; |
474 |
|
|
} |
475 |
|
|
setvariable(s, matchvar); /* don't flag errors */ |
476 |
gwlarson |
3.42 |
return(0); |
477 |
gregl |
3.1 |
} |
478 |
|
|
|
479 |
|
|
|
480 |
|
|
loadholo() /* start loading a holodeck from fname */ |
481 |
|
|
{ |
482 |
|
|
FILE *fp; |
483 |
gregl |
3.10 |
int fd; |
484 |
gregl |
3.29 |
int n; |
485 |
greg |
3.57 |
int32 nextloc; |
486 |
gwlarson |
3.51 |
|
487 |
schorsch |
3.60 |
if ((ncprocs > 0) & (force >= 0)) |
488 |
gwlarson |
3.51 |
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 |
gregl |
3.1 |
} |
504 |
|
|
/* load variables from header */ |
505 |
|
|
getheader(fp, headline, NULL); |
506 |
|
|
/* check magic number */ |
507 |
|
|
if (getw(fp) != HOLOMAGIC) { |
508 |
|
|
sprintf(errmsg, "bad magic number in holodeck file \"%s\"", |
509 |
|
|
hdkfile); |
510 |
|
|
error(USER, errmsg); |
511 |
|
|
} |
512 |
gregl |
3.29 |
nextloc = ftell(fp); /* get stdio position */ |
513 |
gregl |
3.10 |
fd = dup(fileno(fp)); |
514 |
|
|
fclose(fp); /* done with stdio */ |
515 |
gregl |
3.29 |
for (n = 0; nextloc > 0L; n++) { /* initialize each section */ |
516 |
greg |
3.61 |
lseek(fd, (off_t)nextloc, SEEK_SET); |
517 |
gregl |
3.29 |
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 |
gregl |
3.1 |
} |
526 |
|
|
|
527 |
|
|
|
528 |
|
|
done_packets(pl) /* handle finished packets */ |
529 |
|
|
PACKET *pl; |
530 |
|
|
{ |
531 |
gregl |
3.13 |
static int n2flush = 0; |
532 |
gregl |
3.1 |
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 |
schorsch |
3.59 |
memcpy( (void *)hdnewrays(hdlist[p->hd],p->bi,p->nr), |
538 |
|
|
(void *)p->ra, |
539 |
gregl |
3.1 |
p->nr*sizeof(RAYVAL)); |
540 |
|
|
if (outdev != NULL) /* display it */ |
541 |
gregl |
3.7 |
disp_packet((PACKHEAD *)p); |
542 |
gwlarson |
3.49 |
if (hdcachesize <= 0) |
543 |
gregl |
3.33 |
n2flush++; |
544 |
gregl |
3.6 |
nraysdone += p->nr; |
545 |
|
|
npacksdone++; |
546 |
gregl |
3.33 |
p->nr = 0; |
547 |
gregl |
3.1 |
} |
548 |
gregl |
3.33 |
p->next = freepacks; /* push onto free list */ |
549 |
gregl |
3.1 |
freepacks = p; |
550 |
gregl |
3.5 |
} |
551 |
gwlarson |
3.50 |
if (n2flush >= RTFLUSH) { |
552 |
gwlarson |
3.46 |
if (outdev != NULL) |
553 |
|
|
hdsync(NULL, 1); |
554 |
gwlarson |
3.41 |
else |
555 |
gwlarson |
3.46 |
hdflush(NULL); |
556 |
gregl |
3.13 |
n2flush = 0; |
557 |
gregl |
3.1 |
} |
558 |
|
|
} |
559 |
|
|
|
560 |
|
|
|
561 |
|
|
rootname(rn, fn) /* remove tail from end of fn */ |
562 |
|
|
register char *rn, *fn; |
563 |
|
|
{ |
564 |
|
|
char *tp, *dp; |
565 |
|
|
|
566 |
schorsch |
3.60 |
for (tp = NULL, dp = rn; (*rn = *fn++); rn++) { |
567 |
gregl |
3.22 |
if (*rn == '/') |
568 |
gregl |
3.1 |
dp = rn; |
569 |
|
|
else if (*rn == '.') |
570 |
|
|
tp = rn; |
571 |
schorsch |
3.60 |
} |
572 |
gregl |
3.1 |
if (tp != NULL && tp > dp) |
573 |
|
|
*tp = '\0'; |
574 |
|
|
} |
575 |
|
|
|
576 |
|
|
|
577 |
|
|
badvalue(vc) /* report bad variable value and exit */ |
578 |
|
|
int vc; |
579 |
|
|
{ |
580 |
|
|
sprintf(errmsg, "bad value for variable '%s'", vnam(vc)); |
581 |
|
|
error(USER, errmsg); |
582 |
|
|
} |
583 |
|
|
|
584 |
|
|
|
585 |
greg |
3.52 |
void |
586 |
gregl |
3.1 |
eputs(s) /* put error message to stderr */ |
587 |
|
|
register char *s; |
588 |
|
|
{ |
589 |
|
|
static int midline = 0; |
590 |
|
|
|
591 |
|
|
if (!*s) |
592 |
|
|
return; |
593 |
|
|
if (!midline++) { /* prepend line with program name */ |
594 |
|
|
fputs(progname, stderr); |
595 |
|
|
fputs(": ", stderr); |
596 |
|
|
} |
597 |
|
|
fputs(s, stderr); |
598 |
|
|
if (s[strlen(s)-1] == '\n') { |
599 |
|
|
fflush(stderr); |
600 |
|
|
midline = 0; |
601 |
|
|
} |
602 |
|
|
} |
603 |
|
|
|
604 |
|
|
|
605 |
greg |
3.52 |
void |
606 |
gregl |
3.1 |
quit(ec) /* exit program gracefully */ |
607 |
|
|
int ec; |
608 |
|
|
{ |
609 |
|
|
int status = 0; |
610 |
|
|
|
611 |
gregl |
3.24 |
if (hdlist[0] != NULL) { /* close holodeck */ |
612 |
gregl |
3.21 |
if (nprocs > 0) |
613 |
gregl |
3.24 |
status = done_rtrace(); /* calls hdsync() */ |
614 |
schorsch |
3.60 |
if ((ncprocs > 0) & (force >= 0) && vdef(REPORT)) { |
615 |
greg |
3.54 |
off_t fsiz, fuse; |
616 |
gregl |
3.19 |
fsiz = hdfilen(hdlist[0]->fd); |
617 |
|
|
fuse = hdfiluse(hdlist[0]->fd, 1); |
618 |
|
|
fprintf(stderr, |
619 |
gregl |
3.3 |
"%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n", |
620 |
gregl |
3.19 |
hdkfile, fsiz/(1024.*1024.), |
621 |
|
|
100.*(fsiz-fuse)/fsiz); |
622 |
|
|
} |
623 |
gregl |
3.1 |
} |
624 |
gregl |
3.12 |
if (orig_mode >= 0) /* reset holodeck access mode */ |
625 |
|
|
fchmod(hdlist[0]->fd, orig_mode); |
626 |
gregl |
3.11 |
if (outdev != NULL) /* close display */ |
627 |
|
|
disp_close(); |
628 |
gregl |
3.1 |
exit(ec ? ec : status); /* exit */ |
629 |
|
|
} |