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