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