--- ray/src/common/readobj.c 2003/03/10 17:13:29 2.10 +++ ray/src/common/readobj.c 2023/02/07 20:28:16 2.25 @@ -1,5 +1,5 @@ #ifndef lint -static const char RCSid[] = "$Id: readobj.c,v 2.10 2003/03/10 17:13:29 greg Exp $"; +static const char RCSid[] = "$Id: readobj.c,v 2.25 2023/02/07 20:28:16 greg Exp $"; #endif /* * readobj.c - routines for reading in object descriptions. @@ -9,26 +9,30 @@ static const char RCSid[] = "$Id: readobj.c,v 2.10 200 #include "copyright.h" -#include "standard.h" +#include +#include +#include +#include "platform.h" +#include "paths.h" +#include "standard.h" #include "object.h" - #include "otypes.h" -#include OBJREC *objblock[MAXOBJBLK]; /* our objects */ OBJECT nobjects = 0; /* # of objects */ void -readobj(inpspec) /* read in an object file or stream */ -char *inpspec; +readobj( /* read in an object file or stream */ + char *inpspec +) { OBJECT lastobj; FILE *infp; - char buf[1024]; - register int c; + char buf[2048]; + int c; lastobj = nobjects; if (inpspec == NULL) { @@ -43,6 +47,9 @@ char *inpspec; sprintf(errmsg, "cannot open scene file \"%s\"", inpspec); error(SYSTEM, errmsg); } +#ifdef getc_unlocked /* avoid stupid semaphores */ + flockfile(infp); +#endif while ((c = getc(infp)) != EOF) { if (isspace(c)) continue; @@ -59,8 +66,12 @@ char *inpspec; } if (inpspec[0] == '!') pclose(infp); - else + else if (infp != stdin) fclose(infp); +#ifdef getc_unlocked + else + funlockfile(infp); +#endif if (nobjects == lastobj) { sprintf(errmsg, "(%s): empty file", inpspec); error(WARNING, errmsg); @@ -69,14 +80,16 @@ char *inpspec; void -getobject(name, fp) /* read the next object */ -char *name; -FILE *fp; +getobject( /* read the next object */ + char *name, + FILE *fp +) { +#define OALIAS -2 OBJECT obj; char sbuf[MAXSTR]; int rval; - register OBJREC *objp; + OBJREC *objp; if ((obj = newobject()) == OVOID) error(SYSTEM, "out of object space"); @@ -84,8 +97,15 @@ FILE *fp; /* get modifier */ strcpy(sbuf, "EOF"); fgetword(sbuf, MAXSTR, fp); + if (strchr(sbuf, '\t')) { + sprintf(errmsg, "(%s): illegal tab in modifier \"%s\"", + name, sbuf); + error(USER, errmsg); + } if (!strcmp(sbuf, VOIDID)) objp->omod = OVOID; + else if (!strcmp(sbuf, ALIASMOD)) + objp->omod = OALIAS; else if ((objp->omod = modifier(sbuf)) == OVOID) { sprintf(errmsg, "(%s): undefined modifier \"%s\"", name, sbuf); error(USER, errmsg); @@ -93,29 +113,39 @@ FILE *fp; /* get type */ strcpy(sbuf, "EOF"); fgetword(sbuf, MAXSTR, fp); - if (!strcmp(sbuf, ALIASID)) - objp->otype = -1; - else if ((objp->otype = otype(sbuf)) < 0) { + if ((objp->otype = otype(sbuf)) < 0) { sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf); error(USER, errmsg); } /* get identifier */ sbuf[0] = '\0'; fgetword(sbuf, MAXSTR, fp); + if (strchr(sbuf, '\t')) { + sprintf(errmsg, "(%s): illegal tab in identifier \"%s\"", + name, sbuf); + error(USER, errmsg); + } objp->oname = savqstr(sbuf); /* get arguments */ - if (objp->otype == -1) { - register OBJECT alias; + if (objp->otype == MOD_ALIAS) { + OBJECT alias; strcpy(sbuf, "EOF"); fgetword(sbuf, MAXSTR, fp); if ((alias = modifier(sbuf)) == OVOID) { - sprintf(errmsg, - "(%s): bad reference \"%s\" for %s \"%s\"", - name, sbuf, ALIASID, objp->oname); - error(USER, errmsg); + sprintf(errmsg, "(%s): bad reference \"%s\"", + name, sbuf); + objerror(objp, USER, errmsg); } - objp->otype = objptr(alias)->otype; - copystruct(&objp->oargs, &objptr(alias)->oargs); + if (objp->omod == OALIAS || + objp->omod == objptr(alias)->omod) { + objp->omod = alias; + } else { + objp->oargs.sarg = (char **)malloc(sizeof(char *)); + if (objp->oargs.sarg == NULL) + error(SYSTEM, "out of memory in getobject"); + objp->oargs.nsargs = 1; + objp->oargs.sarg[0] = savestr(sbuf); + } } else if ((rval = readfargs(&objp->oargs, fp)) == 0) { sprintf(errmsg, "(%s): bad arguments", name); objerror(objp, USER, errmsg); @@ -123,17 +153,23 @@ FILE *fp; sprintf(errmsg, "(%s): error reading scene", name); error(SYSTEM, errmsg); } + if (objp->omod == OALIAS) { + sprintf(errmsg, "(%s): inappropriate use of '%s' modifier", + name, ALIASMOD); + objerror(objp, USER, errmsg); + } /* initialize */ objp->os = NULL; insertobject(obj); /* add to global structure */ +#undef OALIAS } OBJECT -newobject() /* get a new object */ +newobject(void) /* get a new object */ { - register int i; + int i; if ((nobjects & (OBJBLKSIZ-1)) == 0) { /* new block */ errno = 0; @@ -148,10 +184,12 @@ newobject() /* get a new object */ } void -freeobjects(firstobj, nobjs) /* free a range of objects */ -OBJECT firstobj, nobjs; +freeobjects( /* free a range of objects */ + int firstobj, + int nobjs +) { - register int obj; + int obj; /* check bounds */ if (firstobj < 0) return; @@ -161,22 +199,23 @@ OBJECT firstobj, nobjs; return; /* clear objects */ for (obj = firstobj+nobjs; obj-- > firstobj; ) { - register OBJREC *o = objptr(obj); + OBJREC *o = objptr(obj); free_os(o); /* free client memory */ freeqstr(o->oname); freefargs(&o->oargs); - bzero(o, sizeof(OBJREC)); + memset((void *)o, '\0', sizeof(OBJREC)); } - clearobjndx(); /* free objects off end */ for (obj = nobjects; obj-- > 0; ) if (objptr(obj)->oname != NULL) break; - ++obj; + if (++obj >= nobjects) + return; while (nobjects > obj) /* free empty end blocks */ if ((--nobjects & (OBJBLKSIZ-1)) == 0) { int i = nobjects >> OBJBLKSHFT; free((void *)objblock[i]); objblock[i] = NULL; } + truncobjndx(); /* truncate modifier look-up */ }