| 1 | – | /* Copyright (c) 1986 Regents of the University of California */ | 
| 2 | – |  | 
| 1 |  | #ifndef lint | 
| 2 | < | static char SCCSid[] = "$SunId$ LBL"; | 
| 2 | > | static const char       RCSid[] = "$Id$"; | 
| 3 |  | #endif | 
| 6 | – |  | 
| 4 |  | /* | 
| 5 |  | *  readobj.c - routines for reading in object descriptions. | 
| 6 |  | * | 
| 7 | < | *     7/28/85 | 
| 7 | > | *  External symbols declared in object.h | 
| 8 |  | */ | 
| 9 |  |  | 
| 10 | + | #include "copyright.h" | 
| 11 | + |  | 
| 12 |  | #include  "standard.h" | 
| 13 |  |  | 
| 14 |  | #include  "object.h" | 
| 17 |  |  | 
| 18 |  | #include  <ctype.h> | 
| 19 |  |  | 
| 21 | – | extern char  *fgetword(), *strcpy(); | 
| 22 | – |  | 
| 20 |  | OBJREC  *objblock[MAXOBJBLK];           /* our objects */ | 
| 21 |  | OBJECT  nobjects = 0;                   /* # of objects */ | 
| 22 |  |  | 
| 23 |  |  | 
| 24 | < | readobj(input)                  /* read in an object file or stream */ | 
| 25 | < | char  *input; | 
| 24 | > | void | 
| 25 | > | readobj(inpspec)                /* read in an object file or stream */ | 
| 26 | > | char  *inpspec; | 
| 27 |  | { | 
| 28 | < | FILE  *popen(); | 
| 31 | < | char  *fgets(), *fgetline(); | 
| 28 | > | OBJECT  lastobj; | 
| 29 |  | FILE  *infp; | 
| 30 | < | char  buf[512]; | 
| 30 | > | char  buf[1024]; | 
| 31 |  | register int  c; | 
| 32 |  |  | 
| 33 | < | if (input == NULL) { | 
| 33 | > | lastobj = nobjects; | 
| 34 | > | if (inpspec == NULL) { | 
| 35 |  | infp = stdin; | 
| 36 | < | input = "standard input"; | 
| 37 | < | } else if (input[0] == '!') { | 
| 38 | < | if ((infp = popen(input+1, "r")) == NULL) { | 
| 39 | < | sprintf(errmsg, "cannot execute \"%s\"", input); | 
| 36 | > | inpspec = "standard input"; | 
| 37 | > | } else if (inpspec[0] == '!') { | 
| 38 | > | if ((infp = popen(inpspec+1, "r")) == NULL) { | 
| 39 | > | sprintf(errmsg, "cannot execute \"%s\"", inpspec); | 
| 40 |  | error(SYSTEM, errmsg); | 
| 41 |  | } | 
| 42 | < | } else if ((infp = fopen(input, "r")) == NULL) { | 
| 43 | < | sprintf(errmsg, "cannot open scene file \"%s\"", input); | 
| 42 | > | } else if ((infp = fopen(inpspec, "r")) == NULL) { | 
| 43 | > | sprintf(errmsg, "cannot open scene file \"%s\"", inpspec); | 
| 44 |  | error(SYSTEM, errmsg); | 
| 45 |  | } | 
| 46 |  | while ((c = getc(infp)) != EOF) { | 
| 54 |  | readobj(buf); | 
| 55 |  | } else {                                /* object */ | 
| 56 |  | ungetc(c, infp); | 
| 57 | < | getobject(input, infp); | 
| 57 | > | getobject(inpspec, infp); | 
| 58 |  | } | 
| 59 |  | } | 
| 60 | < | if (input[0] == '!') | 
| 60 | > | if (inpspec[0] == '!') | 
| 61 |  | pclose(infp); | 
| 62 |  | else | 
| 63 |  | fclose(infp); | 
| 64 | + | if (nobjects == lastobj) { | 
| 65 | + | sprintf(errmsg, "(%s): empty file", inpspec); | 
| 66 | + | error(WARNING, errmsg); | 
| 67 | + | } | 
| 68 |  | } | 
| 69 |  |  | 
| 70 |  |  | 
| 71 | + | void | 
| 72 |  | getobject(name, fp)                     /* read the next object */ | 
| 73 |  | char  *name; | 
| 74 |  | FILE  *fp; | 
| 125 |  | } | 
| 126 |  | /* initialize */ | 
| 127 |  | objp->os = NULL; | 
| 125 | – | objp->lastrno = -1; | 
| 128 |  |  | 
| 129 |  | insertobject(obj);              /* add to global structure */ | 
| 130 |  | } | 
| 135 |  | { | 
| 136 |  | register int  i; | 
| 137 |  |  | 
| 138 | < | if ((nobjects & 077) == 0) {            /* new block */ | 
| 138 | > | if ((nobjects & (OBJBLKSIZ-1)) == 0) {  /* new block */ | 
| 139 |  | errno = 0; | 
| 140 | < | i = nobjects >> 6; | 
| 140 | > | i = nobjects >> OBJBLKSHFT; | 
| 141 |  | if (i >= MAXOBJBLK) | 
| 142 |  | return(OVOID); | 
| 143 | < | objblock[i] = (OBJREC *)bmalloc(0100*sizeof(OBJREC)); | 
| 143 | > | objblock[i] = (OBJREC *)calloc(OBJBLKSIZ, sizeof(OBJREC)); | 
| 144 |  | if (objblock[i] == NULL) | 
| 145 |  | return(OVOID); | 
| 146 |  | } | 
| 147 |  | return(nobjects++); | 
| 148 | + | } | 
| 149 | + |  | 
| 150 | + | void | 
| 151 | + | freeobjects(firstobj, nobjs)            /* free a range of objects */ | 
| 152 | + | OBJECT  firstobj, nobjs; | 
| 153 | + | { | 
| 154 | + | register int  obj; | 
| 155 | + | /* check bounds */ | 
| 156 | + | if (firstobj < 0) | 
| 157 | + | return; | 
| 158 | + | if (nobjs <= 0) | 
| 159 | + | return; | 
| 160 | + | if (firstobj + nobjs > nobjects) | 
| 161 | + | return; | 
| 162 | + | /* clear objects */ | 
| 163 | + | for (obj = firstobj+nobjs; obj-- > firstobj; ) { | 
| 164 | + | register OBJREC  *o = objptr(obj); | 
| 165 | + | free_os(o);             /* free client memory */ | 
| 166 | + | freeqstr(o->oname); | 
| 167 | + | freefargs(&o->oargs); | 
| 168 | + | bzero(o, sizeof(OBJREC)); | 
| 169 | + | } | 
| 170 | + | clearobjndx(); | 
| 171 | + | /* free objects off end */ | 
| 172 | + | for (obj = nobjects; obj-- > 0; ) | 
| 173 | + | if (objptr(obj)->oname != NULL) | 
| 174 | + | break; | 
| 175 | + | ++obj; | 
| 176 | + | while (nobjects > obj)          /* free empty end blocks */ | 
| 177 | + | if ((--nobjects & (OBJBLKSIZ-1)) == 0) { | 
| 178 | + | int     i = nobjects >> OBJBLKSHFT; | 
| 179 | + | free((void *)objblock[i]); | 
| 180 | + | objblock[i] = NULL; | 
| 181 | + | } | 
| 182 |  | } |