--- ray/src/hd/rholo.c 1999/02/10 16:24:07 3.49 +++ ray/src/hd/rholo.c 2005/12/15 16:47:16 3.70 @@ -1,19 +1,22 @@ -/* Copyright (c) 1999 Silicon Graphics, Inc. */ - #ifndef lint -static char SCCSid[] = "$SunId$ SGI"; +static const char RCSid[] = "$Id: rholo.c,v 3.70 2005/12/15 16:47:16 greg Exp $"; #endif - /* * Radiance holodeck generation controller */ -#include "rholo.h" -#include "random.h" +#include +#include #include -#include #include +#include +#include "platform.h" +#include "rterror.h" +#include "resolu.h" +#include "rholo.h" +#include "random.h" + #ifndef FRAGWARN #define FRAGWARN 20 /* fragmentation for warning (%) */ #endif @@ -43,13 +46,13 @@ char *outdev = NULL; /* output device name */ int readinp = 0; /* read commands from stdin */ -int force = 0; /* allow overwrite of holodeck */ +int force = 0; /* allow overwrite of holodeck (-1 == read-only) */ time_t starttime; /* time we got started */ time_t endtime; /* time we should end by */ time_t reporttime; /* time for next report */ -long maxdisk; /* maximum file space (bytes) */ +off_t maxdisk; /* maximum file space (bytes) */ int rtargc = 1; /* rtrace command */ char *rtargv[128] = {"rtrace", NULL}; @@ -66,16 +69,27 @@ char *sigerr[NSIG]; /* signal error messages */ extern int nowarn; /* turn warnings off? */ -extern time_t time(); +static void onsig(int signo); +static void sigdie(int signo, char *msg); +static int resfmode(int fd, int mod); +static void initrholo(void); +static int rholo(void); +static void setdefaults(HDGRID *gp); +static void creatholo(HDGRID *gp); +static gethfunc headline; +static void loadholo(void); +static void rootname(char *rn, char *fn); +static void badvalue(int vc); -main(argc, argv) -int argc; -char *argv[]; +int +main( + int argc, + char *argv[] +) { int i; - initurand(16384); /* initialize urand */ progname = argv[0]; /* get arguments */ for (i = 1; i < argc && argv[i][0] == '-'; i++) switch (argv[i][1]) { @@ -83,8 +97,11 @@ char *argv[]; nowarn++; break; case 'f': /* force overwrite */ - force++; + force = 1; break; + case 'r': /* read-only mode */ + force = -1; + break; case 'i': /* read input from stdin */ readinp++; break; @@ -127,6 +144,9 @@ char *argv[]; HDGRID hdg[HDMAX]; /* set defaults */ setdefaults(hdg); + /* check read-only */ + if (force < 0) + error(USER, "cannot create read-only holodeck"); /* holodeck exists? */ if (!force && access(hdkfile, R_OK|W_OK) == 0) error(USER, @@ -144,14 +164,17 @@ char *argv[]; quit(0); userr: fprintf(stderr, -"Usage: %s [-n nprocs][-o disp][-w][-f] output.hdk [control.hif|+|- [VAR=val ..]]\n", +"Usage: %s [-n nprocs][-o disp][-w][-r|-f] output.hdk [control.hif|+|- [VAR=val ..]]\n", progname); quit(1); + return 1; /* pro forma return */ } -onsig(signo) /* fatal signal */ -int signo; +static void +onsig( /* fatal signal */ + int signo +) { static int gotsig = 0; @@ -169,9 +192,11 @@ int signo; } -sigdie(signo, msg) /* set fatal signal */ -int signo; -char *msg; +static void +sigdie( /* set fatal signal */ + int signo, + char *msg +) { if (signal(signo, onsig) == SIG_IGN) signal(signo, SIG_IGN); @@ -179,9 +204,11 @@ char *msg; } -int -resfmode(fd, mod) /* restrict open file access mode */ -int fd, mod; +static int +resfmode( /* restrict open file access mode */ + int fd, + int mod +) { struct stat stbuf; /* get original mode */ @@ -199,7 +226,8 @@ int fd, mod; } -initrholo() /* get our holodeck running */ +static void +initrholo(void) /* get our holodeck running */ { extern int global_packet(); register int i; @@ -212,12 +240,12 @@ initrholo() /* get our holodeck running */ init_global(); /* record disk space limit */ if (!vdef(DISKSPACE)) - maxdisk = 0; + maxdisk = ((off_t)1<<(sizeof(off_t)*8-2)) - 1024; else maxdisk = 1024.*1024.*vflt(DISKSPACE); /* set up memory cache */ if (outdev == NULL) - hdcachesize = 0; /* manual flushing */ + hdcachesize = 0; /* manual flushing */ else if (vdef(CACHE)) hdcachesize = 1024.*1024.*vflt(CACHE); /* open report file */ @@ -264,28 +292,37 @@ initrholo() /* get our holodeck running */ } /* set up signal handling */ sigdie(SIGINT, "Interrupt"); - sigdie(SIGHUP, "Hangup"); sigdie(SIGTERM, "Terminate"); +#ifdef SIGHUP + sigdie(SIGHUP, "Hangup"); +#endif +#ifdef SIGPIPE sigdie(SIGPIPE, "Broken pipe"); +#endif +#ifdef SIGALRM sigdie(SIGALRM, "Alarm clock"); +#endif #ifdef SIGXCPU sigdie(SIGXCPU, "CPU limit exceeded"); +#endif +#ifdef SIGXFSZ sigdie(SIGXFSZ, "File size exceeded"); #endif - /* protect holodeck file */ - orig_mode = resfmode(hdlist[0]->fd, ncprocs>0 ? 0 : 0444); + /* protect holodeck file */ + orig_mode = resfmode(hdlist[0]->fd, (ncprocs>0) & (force>=0) ? 0 : 0444); return; memerr: error(SYSTEM, "out of memory in initrholo"); } -rholo() /* holodeck main loop */ +static int +rholo(void) /* holodeck main loop */ { - static long nextfragwarn = 100*(1L<<20); + static long nextfragwarn = 100L<<20; static int idle = 0; PACKET *pl = NULL, *plend; - long fsiz; + off_t fsiz; int pksiz; register PACKET *p; time_t t; @@ -301,22 +338,25 @@ rholo() /* holodeck main loop */ return(0); /* all done */ fsiz = hdfilen(hdlist[0]->fd); /* check file size */ if (maxdisk > 0 && fsiz >= maxdisk) { - error(WARNING, "file limit exceeded"); + error(USER, "file limit exceeded"); done_rtrace(); return(1); /* comes back */ } #if FRAGWARN - if (fsiz >= nextfragwarn && - (fsiz-hdfiluse(hdlist[0]->fd,0))/(fsiz/100) > FRAGWARN) { - sprintf(errmsg, "holodeck file fragmentation is %.0f%%", - 100.*(fsiz-hdfiluse(hdlist[0]->fd,1))/fsiz); - error(WARNING, errmsg); - nextfragwarn = fsiz + (fsiz>>2); /* decent interval */ + if (fsiz >= nextfragwarn) { + double pctfrag = 100.*(fsiz-hdfiluse(hdlist[0]->fd))/fsiz; + if (pctfrag >= (double)FRAGWARN) { + sprintf(errmsg, "holodeck file fragmentation is %.0f%%", + pctfrag); + error(WARNING, errmsg); + nextfragwarn = fsiz + (fsiz>>2); + } else + nextfragwarn = fsiz + (10L<<20); } #endif t = time(NULL); /* check time */ if (endtime > 0 && t >= endtime) { - error(WARNING, "time limit exceeded"); + error(USER, "time limit exceeded"); done_rtrace(); return(1); /* comes back */ } @@ -349,14 +389,15 @@ rholo() /* holodeck main loop */ } -setdefaults(gp) /* set default values */ -register HDGRID *gp; +static void +setdefaults( /* set default values */ + register HDGRID *gp +) { extern char *atos(); register int i; int n; double len[3], d; - char buf[64]; if (!vdef(SECTION)) { sprintf(errmsg, "%s must be defined", vnam(SECTION)); @@ -406,11 +447,13 @@ register HDGRID *gp; } -creatholo(gp) /* create a holodeck output file */ -HDGRID *gp; +static void +creatholo( /* create a holodeck output file */ + HDGRID *gp +) { extern char VersionID[]; - int4 lastloc, nextloc; + int32 lastloc, nextloc; int n; int fd; FILE *fp; @@ -428,7 +471,7 @@ HDGRID *gp; putw(HOLOMAGIC, fp); /* put magic number */ fd = dup(fileno(fp)); fclose(fp); /* flush and close stdio stream */ - lastloc = lseek(fd, 0L, 2); + lastloc = lseek(fd, (off_t)0, SEEK_END); for (n = vdef(SECTION); n--; gp++) { /* initialize each section */ nextloc = 0L; write(fd, (char *)&nextloc, sizeof(nextloc)); @@ -436,18 +479,20 @@ HDGRID *gp; if (!n) break; nextloc = hdfilen(fd); /* write section pointer */ - if (lseek(fd, (long)lastloc, 0) < 0) + if (lseek(fd, (off_t)lastloc, SEEK_SET) < 0) error(SYSTEM, "cannot seek on holodeck file in creatholo"); write(fd, (char *)&nextloc, sizeof(nextloc)); - lseek(fd, (long)(lastloc=nextloc), 0); + lseek(fd, (off_t)(lastloc=nextloc), SEEK_SET); } } -int -headline(s) /* process information header line */ -char *s; +static int +headline( /* process information header line */ + char *s, + void *p +) { extern char FMTSTR[]; register char *cp; @@ -471,18 +516,30 @@ char *s; } -loadholo() /* start loading a holodeck from fname */ +static void +loadholo(void) /* start loading a holodeck from fname */ { - extern long ftell(); FILE *fp; int fd; int n; - int4 nextloc; - /* open holodeck file */ - if ((fp = fopen(hdkfile, ncprocs>0 ? "r+" : "r")) == NULL) { - sprintf(errmsg, "cannot %s \"%s\"", - ncprocs>0 ? "append" : "read", hdkfile); - error(SYSTEM, errmsg); + int32 nextloc; + + if ((ncprocs > 0) & (force >= 0)) + fp = fopen(hdkfile, "r+"); + else + fp = NULL; + if (fp == NULL) { + if ((fp = fopen(hdkfile, "r")) == NULL) { + sprintf(errmsg, "cannot open \"%s\"", hdkfile); + error(SYSTEM, errmsg); + } + if (ncprocs > 0) { + sprintf(errmsg, + "\"%s\" opened read-only; new rays will be discarded", + hdkfile); + error(WARNING, errmsg); + force = -1; + } } /* load variables from header */ getheader(fp, headline, NULL); @@ -496,7 +553,7 @@ loadholo() /* start loading a holodeck from fname */ fd = dup(fileno(fp)); fclose(fp); /* done with stdio */ for (n = 0; nextloc > 0L; n++) { /* initialize each section */ - lseek(fd, (long)nextloc, 0); + lseek(fd, (off_t)nextloc, SEEK_SET); read(fd, (char *)&nextloc, sizeof(nextloc)); hdinit(fd, NULL); } @@ -508,8 +565,10 @@ loadholo() /* start loading a holodeck from fname */ } -done_packets(pl) /* handle finished packets */ -PACKET *pl; +extern void +done_packets( /* handle finished packets */ + PACKET *pl +) { static int n2flush = 0; register PACKET *p; @@ -517,8 +576,8 @@ PACKET *pl; while (pl != NULL) { p = pl; pl = p->next; p->next = NULL; if (p->nr > 0) { /* add to holodeck */ - bcopy((char *)p->ra, - (char *)hdnewrays(hdlist[p->hd],p->bi,p->nr), + memcpy( (void *)hdnewrays(hdlist[p->hd],p->bi,p->nr), + (void *)p->ra, p->nr*sizeof(RAYVAL)); if (outdev != NULL) /* display it */ disp_packet((PACKHEAD *)p); @@ -531,7 +590,7 @@ PACKET *pl; p->next = freepacks; /* push onto free list */ freepacks = p; } - if (n2flush > RTFLUSH) { + if (n2flush >= RTFLUSH) { if (outdev != NULL) hdsync(NULL, 1); else @@ -541,29 +600,36 @@ PACKET *pl; } -rootname(rn, fn) /* remove tail from end of fn */ -register char *rn, *fn; +static void +rootname( /* remove tail from end of fn */ + register char *rn, + register char *fn +) { char *tp, *dp; - for (tp = NULL, dp = rn; *rn = *fn++; rn++) + for (tp = NULL, dp = rn; (*rn = *fn++); rn++) { if (*rn == '/') dp = rn; else if (*rn == '.') tp = rn; + } if (tp != NULL && tp > dp) *tp = '\0'; } -badvalue(vc) /* report bad variable value and exit */ -int vc; +static void +badvalue( /* report bad variable value and exit */ + int vc +) { sprintf(errmsg, "bad value for variable '%s'", vnam(vc)); error(USER, errmsg); } +void eputs(s) /* put error message to stderr */ register char *s; { @@ -583,6 +649,7 @@ register char *s; } +void quit(ec) /* exit program gracefully */ int ec; { @@ -591,10 +658,10 @@ int ec; if (hdlist[0] != NULL) { /* close holodeck */ if (nprocs > 0) status = done_rtrace(); /* calls hdsync() */ - if (ncprocs > 0 && vdef(REPORT)) { - long fsiz, fuse; + if ((ncprocs > 0) & (force >= 0) && vdef(REPORT)) { + off_t fsiz, fuse; fsiz = hdfilen(hdlist[0]->fd); - fuse = hdfiluse(hdlist[0]->fd, 1); + fuse = hdfiluse(hdlist[0]->fd); fprintf(stderr, "%s: %.1f Mbyte holodeck file, %.1f%% fragmentation\n", hdkfile, fsiz/(1024.*1024.),