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