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 |
OBJECT obj; |
77 |
char sbuf[MAXSTR]; |
78 |
int rval; |
79 |
register OBJREC *objp; |
80 |
|
81 |
if ((obj = newobject()) == OVOID) |
82 |
error(SYSTEM, "out of object space"); |
83 |
objp = objptr(obj); |
84 |
/* get modifier */ |
85 |
strcpy(sbuf, "EOF"); |
86 |
fgetword(sbuf, MAXSTR, fp); |
87 |
if (!strcmp(sbuf, VOIDID)) |
88 |
objp->omod = OVOID; |
89 |
else if ((objp->omod = modifier(sbuf)) == OVOID) { |
90 |
sprintf(errmsg, "(%s): undefined modifier \"%s\"", name, sbuf); |
91 |
error(USER, errmsg); |
92 |
} |
93 |
/* get type */ |
94 |
strcpy(sbuf, "EOF"); |
95 |
fgetword(sbuf, MAXSTR, fp); |
96 |
if (!strcmp(sbuf, ALIASID)) |
97 |
objp->otype = -1; |
98 |
else if ((objp->otype = otype(sbuf)) < 0) { |
99 |
sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf); |
100 |
error(USER, errmsg); |
101 |
} |
102 |
/* get identifier */ |
103 |
sbuf[0] = '\0'; |
104 |
fgetword(sbuf, MAXSTR, fp); |
105 |
objp->oname = savqstr(sbuf); |
106 |
/* get arguments */ |
107 |
if (objp->otype == -1) { |
108 |
register OBJECT alias; |
109 |
strcpy(sbuf, "EOF"); |
110 |
fgetword(sbuf, MAXSTR, fp); |
111 |
if ((alias = modifier(sbuf)) == OVOID) { |
112 |
sprintf(errmsg, |
113 |
"(%s): bad reference \"%s\" for %s \"%s\"", |
114 |
name, sbuf, ALIASID, objp->oname); |
115 |
error(USER, errmsg); |
116 |
} |
117 |
objp->otype = objptr(alias)->otype; |
118 |
copystruct(&objp->oargs, &objptr(alias)->oargs); |
119 |
} else if ((rval = readfargs(&objp->oargs, fp)) == 0) { |
120 |
sprintf(errmsg, "(%s): bad arguments", name); |
121 |
objerror(objp, USER, errmsg); |
122 |
} else if (rval < 0) { |
123 |
sprintf(errmsg, "(%s): error reading scene", name); |
124 |
error(SYSTEM, errmsg); |
125 |
} |
126 |
/* initialize */ |
127 |
objp->os = NULL; |
128 |
|
129 |
insertobject(obj); /* add to global structure */ |
130 |
} |
131 |
|
132 |
|
133 |
OBJECT |
134 |
newobject() /* get a new object */ |
135 |
{ |
136 |
register int i; |
137 |
|
138 |
if ((nobjects & (OBJBLKSIZ-1)) == 0) { /* new block */ |
139 |
errno = 0; |
140 |
i = nobjects >> OBJBLKSHFT; |
141 |
if (i >= MAXOBJBLK) |
142 |
return(OVOID); |
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 |
} |