9 |
|
*/ |
10 |
|
|
11 |
|
#include "rholo.h" |
12 |
< |
#include "paths.h" |
12 |
> |
#include "random.h" |
13 |
> |
#include <signal.h> |
14 |
|
#include <sys/types.h> |
15 |
+ |
#include <sys/stat.h> |
16 |
|
|
17 |
|
/* the following must be consistent with rholo.h */ |
18 |
|
int NVARS = NRHVARS; /* total number of variables */ |
21 |
|
|
22 |
|
char *progname; /* our program name */ |
23 |
|
char *hdkfile; /* holodeck file name */ |
24 |
< |
char froot[MAXPATH]; /* root file name */ |
24 |
> |
char froot[256]; /* root file name */ |
25 |
|
|
26 |
|
int nowarn = 0; /* turn warnings off? */ |
27 |
|
|
26 |
– |
double expval = 1.; /* global exposure value */ |
27 |
– |
|
28 |
|
int ncprocs = 0; /* desired number of compute processes */ |
29 |
|
|
30 |
|
char *outdev = NULL; /* output device name */ |
31 |
|
|
32 |
+ |
int readinp = 0; /* read commands from stdin */ |
33 |
+ |
|
34 |
+ |
int force = 0; /* allow overwrite of holodeck */ |
35 |
+ |
|
36 |
|
time_t starttime; /* time we got started */ |
37 |
|
time_t endtime; /* time we should end by */ |
38 |
|
time_t reporttime; /* time for next report */ |
39 |
|
|
40 |
+ |
long maxdisk; /* maximum file space (bytes) */ |
41 |
+ |
|
42 |
|
int rtargc = 1; /* rtrace command */ |
43 |
|
char *rtargv[128] = {"rtrace", NULL}; |
44 |
|
|
45 |
+ |
int orig_mode = -1; /* original file mode (-1 if unchanged) */ |
46 |
+ |
|
47 |
|
long nraysdone = 0L; /* number of rays done */ |
48 |
|
long npacksdone = 0L; /* number of packets done */ |
49 |
|
|
50 |
|
PACKET *freepacks; /* available packets */ |
51 |
|
|
52 |
+ |
char *sigerr[NSIG]; /* signal error messages */ |
53 |
+ |
|
54 |
|
extern time_t time(); |
55 |
|
|
56 |
|
|
58 |
|
int argc; |
59 |
|
char *argv[]; |
60 |
|
{ |
51 |
– |
HDGRID hdg; |
61 |
|
int i; |
53 |
– |
int force = 0; |
62 |
|
/* mark start time */ |
63 |
|
starttime = time(NULL); |
64 |
+ |
initurand(16384); /* initialize urand */ |
65 |
|
progname = argv[0]; /* get arguments */ |
66 |
|
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
67 |
|
switch (argv[i][1]) { |
71 |
|
case 'f': /* force overwrite */ |
72 |
|
force++; |
73 |
|
break; |
74 |
+ |
case 'i': /* read input from stdin */ |
75 |
+ |
readinp++; |
76 |
+ |
break; |
77 |
|
case 'n': /* compute processes */ |
78 |
|
if (i >= argc-2) |
79 |
|
goto userr; |
87 |
|
default: |
88 |
|
goto userr; |
89 |
|
} |
78 |
– |
/* do we have a job? */ |
79 |
– |
if (outdev == NULL && ncprocs <= 0) |
80 |
– |
goto userr; |
90 |
|
/* get root file name */ |
91 |
+ |
if (i >= argc) |
92 |
+ |
goto userr; |
93 |
|
rootname(froot, hdkfile=argv[i++]); |
94 |
< |
/* load... */ |
95 |
< |
if (i < argc) { /* variables */ |
96 |
< |
loadvars(argv[i++]); |
97 |
< |
/* cmdline settings */ |
98 |
< |
for ( ; i < argc; i++) |
99 |
< |
if (setvariable(argv[i], matchvar) < 0) { |
100 |
< |
sprintf(errmsg, "unknown variable: %s", |
101 |
< |
argv[i]); |
102 |
< |
error(USER, errmsg); |
103 |
< |
} |
104 |
< |
/* check settings */ |
105 |
< |
checkvalues(); |
106 |
< |
/* load RIF if any */ |
107 |
< |
if (vdef(RIF)) |
108 |
< |
getradfile(vval(RIF)); |
94 |
> |
/* load variables? */ |
95 |
> |
if (i < argc) |
96 |
> |
if (argv[i][0] != '-' && argv[i][0] != '+') |
97 |
> |
loadvars(argv[i]); /* load variables from file */ |
98 |
> |
|
99 |
> |
if (i >= argc || argv[i][0] == '+') |
100 |
> |
loadholo(); /* load existing holodeck */ |
101 |
> |
|
102 |
> |
while (++i < argc) /* get command line settings */ |
103 |
> |
if (setvariable(argv[i], matchvar) < 0) { |
104 |
> |
sprintf(errmsg, "unknown variable: %s", argv[i]); |
105 |
> |
error(USER, errmsg); |
106 |
> |
} |
107 |
> |
/* check settings */ |
108 |
> |
checkvalues(); |
109 |
> |
/* load RIF if any */ |
110 |
> |
getradfile(); |
111 |
> |
|
112 |
> |
if (hdlist[0] == NULL) { /* create new holodeck */ |
113 |
> |
HDGRID hdg[HDMAX]; |
114 |
|
/* set defaults */ |
115 |
< |
setdefaults(&hdg); |
115 |
> |
setdefaults(hdg); |
116 |
|
/* holodeck exists? */ |
117 |
|
if (!force && access(hdkfile, R_OK|W_OK) == 0) |
118 |
|
error(USER, |
119 |
|
"holodeck file exists -- use -f to overwrite"); |
120 |
|
/* create holodeck */ |
121 |
< |
creatholo(&hdg); |
122 |
< |
} else { /* else load holodeck */ |
123 |
< |
loadholo(); |
108 |
< |
if (vdef(RIF)) /* load RIF if any */ |
109 |
< |
getradfile(vval(RIF)); |
110 |
< |
} |
121 |
> |
creatholo(hdg); |
122 |
> |
} else /* else just set defaults */ |
123 |
> |
setdefaults(NULL); |
124 |
|
/* initialize */ |
125 |
|
initrholo(); |
126 |
< |
/* run */ |
126 |
> |
/* main loop */ |
127 |
|
while (rholo()) |
128 |
|
; |
129 |
|
/* done */ |
130 |
|
quit(0); |
131 |
|
userr: |
132 |
|
fprintf(stderr, |
133 |
< |
"Usage: %s {-n nprocs|-o disp} [-w][-f] output.hdk [control.hif [VAR=val ..]]\n", |
133 |
> |
"Usage: %s [-n nprocs][-o disp][-w][-f] output.hdk [control.hif|+|- [VAR=val ..]]\n", |
134 |
|
progname); |
135 |
|
quit(1); |
136 |
|
} |
137 |
|
|
138 |
|
|
139 |
+ |
onsig(signo) /* fatal signal */ |
140 |
+ |
int signo; |
141 |
+ |
{ |
142 |
+ |
static int gotsig = 0; |
143 |
+ |
|
144 |
+ |
if (gotsig++) /* two signals and we're gone! */ |
145 |
+ |
_exit(signo); |
146 |
+ |
|
147 |
+ |
alarm(60); /* allow 60 seconds to clean up */ |
148 |
+ |
signal(SIGALRM, SIG_DFL); /* make certain we do die */ |
149 |
+ |
eputs("signal - "); |
150 |
+ |
eputs(sigerr[signo]); |
151 |
+ |
eputs("\n"); |
152 |
+ |
quit(3); |
153 |
+ |
} |
154 |
+ |
|
155 |
+ |
|
156 |
+ |
sigdie(signo, msg) /* set fatal signal */ |
157 |
+ |
int signo; |
158 |
+ |
char *msg; |
159 |
+ |
{ |
160 |
+ |
if (signal(signo, onsig) == SIG_IGN) |
161 |
+ |
signal(signo, SIG_IGN); |
162 |
+ |
sigerr[signo] = msg; |
163 |
+ |
} |
164 |
+ |
|
165 |
+ |
|
166 |
+ |
int |
167 |
+ |
resfmode(fd, mod) /* restrict open file access mode */ |
168 |
+ |
int fd, mod; |
169 |
+ |
{ |
170 |
+ |
struct stat stbuf; |
171 |
+ |
/* get original mode */ |
172 |
+ |
if (fstat(fd, &stbuf) < 0) |
173 |
+ |
error(SYSTEM, "cannot stat open holodeck file"); |
174 |
+ |
mod &= stbuf.st_mode; /* always more restrictive */ |
175 |
+ |
if (mod == stbuf.st_mode) |
176 |
+ |
return(-1); /* already set */ |
177 |
+ |
/* else change it */ |
178 |
+ |
if (fchmod(fd, mod) < 0) { |
179 |
+ |
error(WARNING, "cannot change holodeck file access mode"); |
180 |
+ |
return(-1); |
181 |
+ |
} |
182 |
+ |
return(stbuf.st_mode); /* return original mode */ |
183 |
+ |
} |
184 |
+ |
|
185 |
+ |
|
186 |
|
initrholo() /* get our holodeck running */ |
187 |
|
{ |
188 |
|
extern int global_packet(); |
189 |
|
register int i; |
190 |
< |
/* check output device */ |
191 |
< |
if (outdev != NULL) |
192 |
< |
open_display(outdev); |
190 |
> |
/* close holodeck on exec() */ |
191 |
> |
fcntl(hdlist[0]->fd, F_SETFD, FD_CLOEXEC); |
192 |
> |
|
193 |
> |
if (outdev != NULL) /* open output device */ |
194 |
> |
disp_open(outdev); |
195 |
|
else if (ncprocs > 0) /* else use global ray feed */ |
196 |
|
init_global(); |
197 |
+ |
/* record disk space limit */ |
198 |
+ |
if (!vdef(DISKSPACE)) |
199 |
+ |
maxdisk = 0; |
200 |
+ |
else |
201 |
+ |
maxdisk = 1024.*1024.*vflt(DISKSPACE); |
202 |
|
/* record end time */ |
203 |
|
if (!vdef(TIME) || vflt(TIME) <= FTINY) |
204 |
|
endtime = 0; |
205 |
|
else |
206 |
< |
endtime = starttime + vflt(TIME)*3600.; |
206 |
> |
endtime = starttime + vflt(TIME)*3600. + .5; |
207 |
|
/* set up memory cache */ |
208 |
< |
hdcachesize = 1024.*1024.*vflt(CACHE); |
208 |
> |
if (outdev == NULL) |
209 |
> |
hdcachesize = 0; /* manual flushing */ |
210 |
> |
else if (vdef(CACHE)) |
211 |
> |
hdcachesize = 1024.*1024.*vflt(CACHE); |
212 |
|
/* open report file */ |
213 |
|
if (vdef(REPORT)) { |
214 |
|
register char *s = sskip2(vval(REPORT), 1); |
219 |
|
if (ncprocs > 0) { |
220 |
|
i = start_rtrace(); |
221 |
|
if (i < 1) |
222 |
< |
error(USER, "cannot start rtrace process"); |
222 |
> |
error(USER, "cannot start rtrace process(es)"); |
223 |
|
if (vdef(REPORT)) { /* make first report */ |
224 |
|
printargs(rtargc, rtargv, stderr); |
225 |
|
report(0); |
230 |
|
goto memerr; |
231 |
|
freepacks[--i].nr = 0; |
232 |
|
freepacks[i].next = NULL; |
233 |
< |
if (!vbool(OBSTRUCTIONS)) { |
233 |
> |
if (!vdef(OBSTRUCTIONS) || !vbool(OBSTRUCTIONS)) { |
234 |
|
freepacks[i].offset = (float *)bmalloc( |
235 |
|
RPACKSIZ*sizeof(float)*(i+1) ); |
236 |
|
if (freepacks[i].offset == NULL) |
244 |
|
freepacks[i].next = &freepacks[i+1]; |
245 |
|
} |
246 |
|
} |
247 |
+ |
/* set up signal handling */ |
248 |
+ |
sigdie(SIGINT, "Interrupt"); |
249 |
+ |
sigdie(SIGHUP, "Hangup"); |
250 |
+ |
sigdie(SIGTERM, "Terminate"); |
251 |
+ |
sigdie(SIGPIPE, "Broken pipe"); |
252 |
+ |
sigdie(SIGALRM, "Alarm clock"); |
253 |
+ |
#ifdef SIGXCPU |
254 |
+ |
sigdie(SIGXCPU, "CPU limit exceeded"); |
255 |
+ |
sigdie(SIGXFSZ, "File size exceeded"); |
256 |
+ |
#endif |
257 |
+ |
/* protect holodeck file */ |
258 |
+ |
orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444); |
259 |
|
return; |
260 |
|
memerr: |
261 |
|
error(SYSTEM, "out of memory in initrholo"); |
264 |
|
|
265 |
|
rholo() /* holodeck main loop */ |
266 |
|
{ |
267 |
< |
static int idle = 1; |
267 |
> |
static int idle = 0; |
268 |
|
PACKET *pl = NULL, *plend; |
269 |
|
register PACKET *p; |
270 |
|
time_t t; |
271 |
|
long l; |
272 |
|
/* check display */ |
273 |
< |
if (outdev != NULL && !disp_check(idle)) |
274 |
< |
return(0); |
275 |
< |
/* display only? */ |
276 |
< |
if (ncprocs <= 0) |
277 |
< |
return(1); |
273 |
> |
if (nprocs <= 0) |
274 |
> |
idle = 1; |
275 |
> |
if (outdev != NULL) { |
276 |
> |
if (!disp_check(idle)) |
277 |
> |
return(0); /* quit request */ |
278 |
> |
if (nprocs <= 0) |
279 |
> |
return(1); |
280 |
> |
} else if (idle) |
281 |
> |
return(0); /* all done */ |
282 |
|
/* check file size */ |
283 |
< |
if ((l = 1024.*1024.*vflt(DISKSPACE)) > 0 && |
284 |
< |
hdfiluse(hdlist[0]->fd, 0) + hdmemuse(0) >= l) |
285 |
< |
return(0); |
283 |
> |
if (maxdisk > 0 && hdfilen(hdlist[0]->fd) >= maxdisk) { |
284 |
> |
error(WARNING, "file limit exceeded"); |
285 |
> |
done_rtrace(); |
286 |
> |
return(1); /* comes back */ |
287 |
> |
} |
288 |
|
/* check time */ |
289 |
|
if (endtime > 0 || reporttime > 0) |
290 |
|
t = time(NULL); |
291 |
< |
if (endtime > 0 && t >= endtime) |
292 |
< |
return(0); |
291 |
> |
if (endtime > 0 && t >= endtime) { |
292 |
> |
error(WARNING, "time limit exceeded"); |
293 |
> |
done_rtrace(); |
294 |
> |
return(1); /* comes back */ |
295 |
> |
} |
296 |
|
if (reporttime > 0 && t >= reporttime) |
297 |
|
report(t); |
298 |
< |
/* get packets to process */ |
298 |
> |
idle = 0; /* get packets to process */ |
299 |
|
while (freepacks != NULL) { |
300 |
|
p = freepacks; freepacks = p->next; p->next = NULL; |
301 |
|
if (!next_packet(p)) { |
302 |
|
p->next = freepacks; freepacks = p; |
303 |
+ |
idle = 1; |
304 |
|
break; |
305 |
|
} |
306 |
|
if (pl == NULL) pl = p; |
307 |
|
else plend->next = p; |
308 |
|
plend = p; |
309 |
|
} |
310 |
< |
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 */ |
310 |
> |
/* process packets */ |
311 |
|
done_packets(do_packets(pl)); |
312 |
|
return(1); /* and continue */ |
313 |
|
} |
314 |
|
|
315 |
|
|
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 |
– |
|
316 |
|
setdefaults(gp) /* set default values */ |
317 |
|
register HDGRID *gp; |
318 |
|
{ |
319 |
|
extern char *atos(); |
320 |
|
register int i; |
321 |
+ |
int n; |
322 |
|
double len[3], maxlen, d; |
323 |
|
char buf[64]; |
324 |
|
|
326 |
|
sprintf(errmsg, "%s must be defined", vnam(SECTION)); |
327 |
|
error(USER, errmsg); |
328 |
|
} |
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); |
329 |
|
if (!vdef(OCTREE)) { |
330 |
|
if ((vval(OCTREE) = bmalloc(strlen(froot)+5)) == NULL) |
331 |
|
error(SYSTEM, "out of memory"); |
332 |
|
sprintf(vval(OCTREE), "%s.oct", froot); |
333 |
|
vdef(OCTREE)++; |
334 |
|
} |
335 |
< |
if (!vdef(DISKSPACE)) { |
336 |
< |
sprintf(errmsg, |
337 |
< |
"no %s setting, assuming 100 Mbytes available", |
292 |
< |
vnam(DISKSPACE)); |
293 |
< |
error(WARNING, errmsg); |
294 |
< |
vval(DISKSPACE) = "100"; |
295 |
< |
vdef(DISKSPACE)++; |
335 |
> |
if (!vdef(VDIST)) { |
336 |
> |
vval(VDIST) = "F"; |
337 |
> |
vdef(VDIST)++; |
338 |
|
} |
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 |
– |
} |
339 |
|
/* append rendering options */ |
340 |
|
if (vdef(RENDER)) |
341 |
|
rtargc += wordstring(rtargv+rtargc, vval(RENDER)); |
342 |
+ |
|
343 |
+ |
if (gp == NULL) /* already initialized? */ |
344 |
+ |
return; |
345 |
+ |
/* set grid parameters */ |
346 |
+ |
for (n = 0; n < vdef(SECTION); n++, gp++) { |
347 |
+ |
gp->grid[0] = gp->grid[1] = gp->grid[2] = 0; |
348 |
+ |
if (sscanf(nvalue(SECTION, n), |
349 |
+ |
"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %hd %hd %hd", |
350 |
+ |
&gp->orig[0], &gp->orig[1], &gp->orig[2], |
351 |
+ |
&gp->xv[0][0], &gp->xv[0][1], &gp->xv[0][2], |
352 |
+ |
&gp->xv[1][0], &gp->xv[1][1], &gp->xv[1][2], |
353 |
+ |
&gp->xv[2][0], &gp->xv[2][1], &gp->xv[2][2], |
354 |
+ |
&gp->grid[0], &gp->grid[1], &gp->grid[2]) < 12) |
355 |
+ |
badvalue(SECTION); |
356 |
+ |
maxlen = 0.; |
357 |
+ |
for (i = 0; i < 3; i++) |
358 |
+ |
if ((len[i] = VLEN(gp->xv[i])) > maxlen) |
359 |
+ |
maxlen = len[i]; |
360 |
+ |
if (!vdef(GRID)) |
361 |
+ |
d = 0.125*maxlen; |
362 |
+ |
else if ((d = vflt(GRID)) <= FTINY) |
363 |
+ |
badvalue(GRID); |
364 |
+ |
for (i = 0; i < 3; i++) |
365 |
+ |
if (gp->grid[i] <= 0) |
366 |
+ |
gp->grid[i] = len[i]/d + (1.-FTINY); |
367 |
+ |
} |
368 |
|
} |
369 |
|
|
370 |
|
|
371 |
|
creatholo(gp) /* create a holodeck output file */ |
372 |
|
HDGRID *gp; |
373 |
|
{ |
374 |
< |
long endloc = 0; |
374 |
> |
extern char VersionID[]; |
375 |
> |
int4 lastloc, nextloc; |
376 |
> |
int n; |
377 |
> |
int fd; |
378 |
|
FILE *fp; |
379 |
|
/* open & truncate file */ |
380 |
|
if ((fp = fopen(hdkfile, "w+")) == NULL) { |
383 |
|
} |
384 |
|
/* write information header */ |
385 |
|
newheader("RADIANCE", fp); |
386 |
+ |
fprintf(fp, "SOFTWARE= %s\n", VersionID); |
387 |
|
printvars(fp); |
388 |
|
fputformat(HOLOFMT, fp); |
389 |
|
fputc('\n', fp); |
390 |
< |
putw(HOLOMAGIC, fp); /* put magic number & terminus */ |
391 |
< |
fwrite(&endloc, sizeof(long), 1, fp); |
392 |
< |
fflush(fp); /* flush buffer */ |
393 |
< |
hdinit(fileno(fp), gp); /* allocate and initialize index */ |
394 |
< |
/* we're dropping fp here.... */ |
390 |
> |
putw(HOLOMAGIC, fp); /* put magic number */ |
391 |
> |
fd = dup(fileno(fp)); |
392 |
> |
fclose(fp); /* flush and close stdio stream */ |
393 |
> |
lastloc = lseek(fd, 0L, 2); |
394 |
> |
for (n = vdef(SECTION); n--; gp++) { /* initialize each section */ |
395 |
> |
nextloc = 0L; |
396 |
> |
write(fd, (char *)&nextloc, sizeof(nextloc)); |
397 |
> |
hdinit(fd, gp); /* writes beam index */ |
398 |
> |
if (!n) |
399 |
> |
break; |
400 |
> |
nextloc = hdfilen(fd); /* write section pointer */ |
401 |
> |
if (lseek(fd, (long)lastloc, 0) < 0) |
402 |
> |
error(SYSTEM, |
403 |
> |
"cannot seek on holodeck file in creatholo"); |
404 |
> |
write(fd, (char *)&nextloc, sizeof(nextloc)); |
405 |
> |
lseek(fd, (long)(lastloc=nextloc), 0); |
406 |
> |
} |
407 |
|
} |
408 |
|
|
409 |
|
|
433 |
|
|
434 |
|
loadholo() /* start loading a holodeck from fname */ |
435 |
|
{ |
436 |
+ |
extern long ftell(); |
437 |
|
FILE *fp; |
438 |
< |
long endloc; |
439 |
< |
/* open input file */ |
440 |
< |
if ((fp = fopen(hdkfile, "r+")) == NULL) { |
441 |
< |
sprintf(errmsg, "cannot open \"%s\" for appending", hdkfile); |
438 |
> |
int fd; |
439 |
> |
int n; |
440 |
> |
int4 nextloc; |
441 |
> |
/* open holodeck file */ |
442 |
> |
if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) { |
443 |
> |
sprintf(errmsg, "cannot open \"%s\" for %s", hdkfile, |
444 |
> |
ncprocs>0 ? "appending" : "reading"); |
445 |
|
error(SYSTEM, errmsg); |
446 |
|
} |
447 |
|
/* load variables from header */ |
452 |
|
hdkfile); |
453 |
|
error(USER, errmsg); |
454 |
|
} |
455 |
< |
fread(&endloc, sizeof(long), 1, fp); |
456 |
< |
if (endloc != 0) |
457 |
< |
error(WARNING, "ignoring multiple sections in holodeck file"); |
458 |
< |
fseek(fp, 0L, 1); /* align system file pointer */ |
459 |
< |
hdinit(fileno(fp), NULL); /* allocate and load index */ |
460 |
< |
/* we're dropping fp here.... */ |
455 |
> |
nextloc = ftell(fp); /* get stdio position */ |
456 |
> |
fd = dup(fileno(fp)); |
457 |
> |
fclose(fp); /* done with stdio */ |
458 |
> |
for (n = 0; nextloc > 0L; n++) { /* initialize each section */ |
459 |
> |
lseek(fd, (long)nextloc, 0); |
460 |
> |
read(fd, (char *)&nextloc, sizeof(nextloc)); |
461 |
> |
hdinit(fd, NULL); |
462 |
> |
} |
463 |
> |
if (n != vdef(SECTION)) { |
464 |
> |
sprintf(errmsg, "number of sections does not match %s setting", |
465 |
> |
vnam(SECTION)); |
466 |
> |
error(WARNING, errmsg); |
467 |
> |
} |
468 |
|
} |
469 |
|
|
470 |
|
|
471 |
|
done_packets(pl) /* handle finished packets */ |
472 |
|
PACKET *pl; |
473 |
|
{ |
474 |
+ |
static int n2flush = 0; |
475 |
|
register PACKET *p; |
476 |
|
|
477 |
|
while (pl != NULL) { |
481 |
|
(char *)hdnewrays(hdlist[p->hd],p->bi,p->nr), |
482 |
|
p->nr*sizeof(RAYVAL)); |
483 |
|
if (outdev != NULL) /* display it */ |
484 |
< |
disp_packet(p); |
484 |
> |
disp_packet((PACKHEAD *)p); |
485 |
> |
if (hdcachesize <= 0) /* manual flushing */ |
486 |
> |
n2flush += p->nr; |
487 |
> |
nraysdone += p->nr; |
488 |
> |
npacksdone++; |
489 |
|
} |
406 |
– |
nraysdone += p->nr; |
407 |
– |
npacksdone++; |
490 |
|
p->nr = 0; /* push onto free list */ |
491 |
|
p->next = freepacks; |
492 |
|
freepacks = p; |
493 |
|
} |
494 |
< |
} |
495 |
< |
|
496 |
< |
|
415 |
< |
getradfile(rfargs) /* run rad and get needed variables */ |
416 |
< |
char *rfargs; |
417 |
< |
{ |
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); |
494 |
> |
if (n2flush > 1024*RPACKSIZ*nprocs) { |
495 |
> |
hdflush(NULL); /* flush holodeck buffers */ |
496 |
> |
n2flush = 0; |
497 |
|
} |
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 */ |
498 |
|
} |
499 |
|
|
500 |
|
|
504 |
|
char *tp, *dp; |
505 |
|
|
506 |
|
for (tp = NULL, dp = rn; *rn = *fn++; rn++) |
507 |
< |
if (ISDIRSEP(*rn)) |
507 |
> |
if (*rn == '/') |
508 |
|
dp = rn; |
509 |
|
else if (*rn == '.') |
510 |
|
tp = rn; |
553 |
|
{ |
554 |
|
int status = 0; |
555 |
|
|
556 |
< |
if (hdlist[0] != NULL) { /* flush holodeck */ |
557 |
< |
if (ncprocs > 0) { |
558 |
< |
done_packets(flush_queue()); |
559 |
< |
status = end_rtrace(); /* close rtrace */ |
560 |
< |
hdflush(NULL); |
561 |
< |
if (vdef(REPORT)) { |
562 |
< |
long fsiz, fuse; |
563 |
< |
report(0); |
525 |
< |
fsiz = lseek(hdlist[0]->fd, 0L, 2); |
526 |
< |
fuse = hdfiluse(hdlist[0]->fd, 1); |
527 |
< |
fprintf(stderr, |
556 |
> |
if (hdlist[0] != NULL) { /* close holodeck */ |
557 |
> |
if (nprocs > 0) |
558 |
> |
status = done_rtrace(); /* calls hdsync() */ |
559 |
> |
if (ncprocs > 0 && vdef(REPORT)) { |
560 |
> |
long fsiz, fuse; |
561 |
> |
fsiz = hdfilen(hdlist[0]->fd); |
562 |
> |
fuse = hdfiluse(hdlist[0]->fd, 1); |
563 |
> |
fprintf(stderr, |
564 |
|
"%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n", |
565 |
< |
hdkfile, fsiz/(1024.*1024.), |
566 |
< |
100.*(fsiz-fuse)/fsiz); |
567 |
< |
} |
532 |
< |
} else |
533 |
< |
hdflush(NULL); |
565 |
> |
hdkfile, fsiz/(1024.*1024.), |
566 |
> |
100.*(fsiz-fuse)/fsiz); |
567 |
> |
} |
568 |
|
} |
569 |
+ |
if (orig_mode >= 0) /* reset holodeck access mode */ |
570 |
+ |
fchmod(hdlist[0]->fd, orig_mode); |
571 |
+ |
if (outdev != NULL) /* close display */ |
572 |
+ |
disp_close(); |
573 |
|
exit(ec ? ec : status); /* exit */ |
574 |
|
} |