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