| 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  "standard.h" | 
| 10 | > | #include "copyright.h" | 
| 11 |  |  | 
| 12 | < | #include  "object.h" | 
| 12 | > | #include  <ctype.h> | 
| 13 | > | #include  <string.h> | 
| 14 | > | #include  <stdio.h> | 
| 15 |  |  | 
| 16 | + | #include  "platform.h" | 
| 17 | + | #include  "paths.h" | 
| 18 | + | #include  "standard.h" | 
| 19 | + | #include  "object.h" | 
| 20 |  | #include  "otypes.h" | 
| 21 |  |  | 
| 19 | – | #include  <ctype.h> | 
| 22 |  |  | 
| 23 |  | OBJREC  *objblock[MAXOBJBLK];           /* our objects */ | 
| 24 |  | OBJECT  nobjects = 0;                   /* # of objects */ | 
| 25 |  |  | 
| 26 |  |  | 
| 27 | < | readobj(input)                  /* read in an object file or stream */ | 
| 28 | < | char  *input; | 
| 27 | > | void | 
| 28 | > | readobj(inpspec)                /* read in an object file or stream */ | 
| 29 | > | char  *inpspec; | 
| 30 |  | { | 
| 31 | < | FILE  *popen(); | 
| 29 | < | char  *fgets(), *fgetline(); | 
| 31 | > | OBJECT  lastobj; | 
| 32 |  | FILE  *infp; | 
| 33 | < | char  buf[512]; | 
| 33 | > | char  buf[2048]; | 
| 34 |  | register int  c; | 
| 35 |  |  | 
| 36 | < | if (input == NULL) { | 
| 36 | > | lastobj = nobjects; | 
| 37 | > | if (inpspec == NULL) { | 
| 38 |  | infp = stdin; | 
| 39 | < | input = "standard input"; | 
| 40 | < | } else if (input[0] == '!') { | 
| 41 | < | if ((infp = popen(input+1, "r")) == NULL) { | 
| 42 | < | sprintf(errmsg, "cannot execute \"%s\"", input); | 
| 39 | > | inpspec = "standard input"; | 
| 40 | > | } else if (inpspec[0] == '!') { | 
| 41 | > | if ((infp = popen(inpspec+1, "r")) == NULL) { | 
| 42 | > | sprintf(errmsg, "cannot execute \"%s\"", inpspec); | 
| 43 |  | error(SYSTEM, errmsg); | 
| 44 |  | } | 
| 45 | < | } else if ((infp = fopen(input, "r")) == NULL) { | 
| 46 | < | sprintf(errmsg, "cannot open scene file \"%s\"", input); | 
| 45 | > | } else if ((infp = fopen(inpspec, "r")) == NULL) { | 
| 46 | > | sprintf(errmsg, "cannot open scene file \"%s\"", inpspec); | 
| 47 |  | error(SYSTEM, errmsg); | 
| 48 |  | } | 
| 49 |  | while ((c = getc(infp)) != EOF) { | 
| 57 |  | readobj(buf); | 
| 58 |  | } else {                                /* object */ | 
| 59 |  | ungetc(c, infp); | 
| 60 | < | getobject(input, infp); | 
| 60 | > | getobject(inpspec, infp); | 
| 61 |  | } | 
| 62 |  | } | 
| 63 | < | if (input[0] == '!') | 
| 63 | > | if (inpspec[0] == '!') | 
| 64 |  | pclose(infp); | 
| 65 |  | else | 
| 66 |  | fclose(infp); | 
| 67 | + | if (nobjects == lastobj) { | 
| 68 | + | sprintf(errmsg, "(%s): empty file", inpspec); | 
| 69 | + | error(WARNING, errmsg); | 
| 70 | + | } | 
| 71 |  | } | 
| 72 |  |  | 
| 73 |  |  | 
| 74 | + | void | 
| 75 |  | getobject(name, fp)                     /* read the next object */ | 
| 76 |  | char  *name; | 
| 77 |  | FILE  *fp; | 
| 78 |  | { | 
| 79 | + | #define OALIAS  -2 | 
| 80 |  | OBJECT  obj; | 
| 81 |  | char  sbuf[MAXSTR]; | 
| 82 | + | int  rval; | 
| 83 |  | register OBJREC  *objp; | 
| 84 |  |  | 
| 85 |  | if ((obj = newobject()) == OVOID) | 
| 86 |  | error(SYSTEM, "out of object space"); | 
| 87 |  | objp = objptr(obj); | 
| 88 |  | /* get modifier */ | 
| 89 | < | fscanf(fp, "%s", sbuf); | 
| 89 | > | strcpy(sbuf, "EOF"); | 
| 90 | > | fgetword(sbuf, MAXSTR, fp); | 
| 91 | > | if (strchr(sbuf, '\t')) { | 
| 92 | > | sprintf(errmsg, "(%s): illegal tab in modifier \"%s\"", | 
| 93 | > | name, sbuf); | 
| 94 | > | error(USER, errmsg); | 
| 95 | > | } | 
| 96 |  | if (!strcmp(sbuf, VOIDID)) | 
| 97 |  | objp->omod = OVOID; | 
| 98 | + | else if (!strcmp(sbuf, ALIASMOD)) | 
| 99 | + | objp->omod = OALIAS; | 
| 100 |  | else if ((objp->omod = modifier(sbuf)) == OVOID) { | 
| 101 |  | sprintf(errmsg, "(%s): undefined modifier \"%s\"", name, sbuf); | 
| 102 |  | error(USER, errmsg); | 
| 103 |  | } | 
| 104 |  | /* get type */ | 
| 105 | < | fscanf(fp, "%s", sbuf); | 
| 106 | < | if (!strcmp(sbuf, ALIASID)) | 
| 107 | < | objp->otype = -1; | 
| 90 | < | else if ((objp->otype = otype(sbuf)) < 0) { | 
| 105 | > | strcpy(sbuf, "EOF"); | 
| 106 | > | fgetword(sbuf, MAXSTR, fp); | 
| 107 | > | if ((objp->otype = otype(sbuf)) < 0) { | 
| 108 |  | sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf); | 
| 109 |  | error(USER, errmsg); | 
| 110 |  | } | 
| 111 |  | /* get identifier */ | 
| 112 | < | fscanf(fp, "%s", sbuf); | 
| 112 | > | sbuf[0] = '\0'; | 
| 113 | > | fgetword(sbuf, MAXSTR, fp); | 
| 114 | > | if (strchr(sbuf, '\t')) { | 
| 115 | > | sprintf(errmsg, "(%s): illegal tab in identifier \"%s\"", | 
| 116 | > | name, sbuf); | 
| 117 | > | error(USER, errmsg); | 
| 118 | > | } | 
| 119 |  | objp->oname = savqstr(sbuf); | 
| 120 |  | /* get arguments */ | 
| 121 | < | if (objp->otype == -1) { | 
| 121 | > | if (objp->otype == MOD_ALIAS) { | 
| 122 |  | register OBJECT  alias; | 
| 123 | < | fscanf(fp, "%s", sbuf); | 
| 123 | > | strcpy(sbuf, "EOF"); | 
| 124 | > | fgetword(sbuf, MAXSTR, fp); | 
| 125 |  | if ((alias = modifier(sbuf)) == OVOID) { | 
| 126 | < | sprintf(errmsg, | 
| 127 | < | "(%s): bad reference \"%s\" for %s \"%s\"", | 
| 128 | < | name, sbuf, ALIASID, objp->oname); | 
| 105 | < | error(USER, errmsg); | 
| 126 | > | sprintf(errmsg, "(%s): bad reference \"%s\"", | 
| 127 | > | name, sbuf); | 
| 128 | > | objerror(objp, USER, errmsg); | 
| 129 |  | } | 
| 130 | < | objp->otype = objptr(alias)->otype; | 
| 131 | < | copystruct(&objp->oargs, &objptr(alias)->oargs); | 
| 132 | < | } else if (readfargs(&objp->oargs, fp) < 0) { | 
| 130 | > | if (objp->omod == OALIAS || | 
| 131 | > | objp->omod == objptr(alias)->omod) { | 
| 132 | > | objp->omod = alias; | 
| 133 | > | } else { | 
| 134 | > | objp->oargs.sarg = (char **)malloc(sizeof(char *)); | 
| 135 | > | if (objp->oargs.sarg == NULL) | 
| 136 | > | error(SYSTEM, "out of memory in getobject"); | 
| 137 | > | objp->oargs.nsargs = 1; | 
| 138 | > | objp->oargs.sarg[0] = savestr(sbuf); | 
| 139 | > | } | 
| 140 | > | } else if ((rval = readfargs(&objp->oargs, fp)) == 0) { | 
| 141 |  | sprintf(errmsg, "(%s): bad arguments", name); | 
| 142 |  | objerror(objp, USER, errmsg); | 
| 143 | + | } else if (rval < 0) { | 
| 144 | + | sprintf(errmsg, "(%s): error reading scene", name); | 
| 145 | + | error(SYSTEM, errmsg); | 
| 146 |  | } | 
| 147 | + | if (objp->omod == OALIAS) { | 
| 148 | + | sprintf(errmsg, "(%s): inappropriate use of '%s' modifier", | 
| 149 | + | name, ALIASMOD); | 
| 150 | + | objerror(objp, USER, errmsg); | 
| 151 | + | } | 
| 152 |  | /* initialize */ | 
| 153 |  | objp->os = NULL; | 
| 115 | – | objp->lastrno = -1; | 
| 154 |  |  | 
| 155 |  | insertobject(obj);              /* add to global structure */ | 
| 156 | + | #undef OALIAS | 
| 157 |  | } | 
| 158 |  |  | 
| 159 |  |  | 
| 160 | < | readfargs(fa, fp)               /* read function arguments from stream */ | 
| 122 | < | register FUNARGS  *fa; | 
| 123 | < | FILE  *fp; | 
| 124 | < | { | 
| 125 | < | char  sbuf[MAXSTR]; | 
| 126 | < | int  n; | 
| 127 | < | register int  i; | 
| 128 | < |  | 
| 129 | < | if (fscanf(fp, "%d", &n) != 1 || n < 0) | 
| 130 | < | return(-1); | 
| 131 | < | if (fa->nsargs = n) { | 
| 132 | < | fa->sarg = (char **)bmalloc(n*sizeof(char *)); | 
| 133 | < | if (fa->sarg == NULL) | 
| 134 | < | goto memerr; | 
| 135 | < | for (i = 0; i < fa->nsargs; i++) { | 
| 136 | < | if (fscanf(fp, "%s", sbuf) != 1) | 
| 137 | < | return(-1); | 
| 138 | < | fa->sarg[i] = savestr(sbuf); | 
| 139 | < | } | 
| 140 | < | } else | 
| 141 | < | fa->sarg = NULL; | 
| 142 | < | if (fscanf(fp, "%d", &n) != 1 || n < 0) | 
| 143 | < | return(-1); | 
| 144 | < | #ifdef  IARGS | 
| 145 | < | if (fa->niargs = n) { | 
| 146 | < | fa->iarg = (long *)bmalloc(n*sizeof(int)); | 
| 147 | < | if (fa->iarg == NULL) | 
| 148 | < | goto memerr; | 
| 149 | < | for (i = 0; i < n; i++) | 
| 150 | < | if (fscanf(fp, "%ld", &fa->iarg[i]) != 1) | 
| 151 | < | return(-1); | 
| 152 | < | } else | 
| 153 | < | fa->iarg = NULL; | 
| 154 | < | #else | 
| 155 | < | if (n != 0) | 
| 156 | < | return(-1); | 
| 157 | < | #endif | 
| 158 | < | if (fscanf(fp, "%d", &n) != 1 || n < 0) | 
| 159 | < | return(-1); | 
| 160 | < | if (fa->nfargs = n) { | 
| 161 | < | fa->farg = (double *)bmalloc(n*sizeof(double)); | 
| 162 | < | if (fa->farg == NULL) | 
| 163 | < | goto memerr; | 
| 164 | < | for (i = 0; i < n; i++) | 
| 165 | < | if (fscanf(fp, "%lf", &fa->farg[i]) != 1) | 
| 166 | < | return(-1); | 
| 167 | < | } else | 
| 168 | < | fa->farg = NULL; | 
| 169 | < | return(0); | 
| 170 | < | memerr: | 
| 171 | < | error(SYSTEM, "out of memory in readfargs"); | 
| 172 | < | } | 
| 173 | < |  | 
| 174 | < |  | 
| 175 | < | int | 
| 160 | > | OBJECT | 
| 161 |  | newobject()                             /* get a new object */ | 
| 162 |  | { | 
| 163 |  | register int  i; | 
| 164 |  |  | 
| 165 | < | if ((nobjects & 077) == 0) {            /* new block */ | 
| 165 | > | if ((nobjects & (OBJBLKSIZ-1)) == 0) {  /* new block */ | 
| 166 |  | errno = 0; | 
| 167 | < | i = nobjects >> 6; | 
| 167 | > | i = nobjects >> OBJBLKSHFT; | 
| 168 |  | if (i >= MAXOBJBLK) | 
| 169 |  | return(OVOID); | 
| 170 | < | objblock[i] = (OBJREC *)malloc(0100*sizeof(OBJREC)); | 
| 170 | > | objblock[i] = (OBJREC *)calloc(OBJBLKSIZ, sizeof(OBJREC)); | 
| 171 |  | if (objblock[i] == NULL) | 
| 172 |  | return(OVOID); | 
| 173 |  | } | 
| 174 |  | return(nobjects++); | 
| 175 | + | } | 
| 176 | + |  | 
| 177 | + | void | 
| 178 | + | freeobjects(firstobj, nobjs)            /* free a range of objects */ | 
| 179 | + | int  firstobj, nobjs; | 
| 180 | + | { | 
| 181 | + | register int  obj; | 
| 182 | + | /* check bounds */ | 
| 183 | + | if (firstobj < 0) | 
| 184 | + | return; | 
| 185 | + | if (nobjs <= 0) | 
| 186 | + | return; | 
| 187 | + | if (firstobj + nobjs > nobjects) | 
| 188 | + | return; | 
| 189 | + | /* clear objects */ | 
| 190 | + | for (obj = firstobj+nobjs; obj-- > firstobj; ) { | 
| 191 | + | register OBJREC  *o = objptr(obj); | 
| 192 | + | free_os(o);             /* free client memory */ | 
| 193 | + | freeqstr(o->oname); | 
| 194 | + | freefargs(&o->oargs); | 
| 195 | + | memset((void *)o, '\0', sizeof(OBJREC)); | 
| 196 | + | } | 
| 197 | + | clearobjndx(); | 
| 198 | + | /* free objects off end */ | 
| 199 | + | for (obj = nobjects; obj-- > 0; ) | 
| 200 | + | if (objptr(obj)->oname != NULL) | 
| 201 | + | break; | 
| 202 | + | ++obj; | 
| 203 | + | while (nobjects > obj)          /* free empty end blocks */ | 
| 204 | + | if ((--nobjects & (OBJBLKSIZ-1)) == 0) { | 
| 205 | + | int     i = nobjects >> OBJBLKSHFT; | 
| 206 | + | free((void *)objblock[i]); | 
| 207 | + | objblock[i] = NULL; | 
| 208 | + | } | 
| 209 |  | } |