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 "rtprocess.h" |
18 |
+ |
#include "standard.h" |
19 |
+ |
#include "object.h" |
20 |
|
#include "otypes.h" |
21 |
|
|
19 |
– |
#include <ctype.h> |
22 |
|
|
21 |
– |
extern char *fgetword(), *strcpy(); |
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(); |
31 |
< |
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; |
90 |
|
fgetword(sbuf, MAXSTR, fp); |
91 |
|
if (!strcmp(sbuf, VOIDID)) |
92 |
|
objp->omod = OVOID; |
93 |
+ |
else if (!strcmp(sbuf, ALIASMOD)) |
94 |
+ |
objp->omod = OALIAS; |
95 |
|
else if ((objp->omod = modifier(sbuf)) == OVOID) { |
96 |
|
sprintf(errmsg, "(%s): undefined modifier \"%s\"", name, sbuf); |
97 |
|
error(USER, errmsg); |
99 |
|
/* get type */ |
100 |
|
strcpy(sbuf, "EOF"); |
101 |
|
fgetword(sbuf, MAXSTR, fp); |
102 |
< |
if (!strcmp(sbuf, ALIASID)) |
94 |
< |
objp->otype = -1; |
95 |
< |
else if ((objp->otype = otype(sbuf)) < 0) { |
102 |
> |
if ((objp->otype = otype(sbuf)) < 0) { |
103 |
|
sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf); |
104 |
|
error(USER, errmsg); |
105 |
|
} |
108 |
|
fgetword(sbuf, MAXSTR, fp); |
109 |
|
objp->oname = savqstr(sbuf); |
110 |
|
/* get arguments */ |
111 |
< |
if (objp->otype == -1) { |
111 |
> |
if (objp->otype == MOD_ALIAS) { |
112 |
|
register OBJECT alias; |
113 |
|
strcpy(sbuf, "EOF"); |
114 |
|
fgetword(sbuf, MAXSTR, fp); |
115 |
|
if ((alias = modifier(sbuf)) == OVOID) { |
116 |
< |
sprintf(errmsg, |
117 |
< |
"(%s): bad reference \"%s\" for %s \"%s\"", |
118 |
< |
name, sbuf, ALIASID, objp->oname); |
112 |
< |
error(USER, errmsg); |
116 |
> |
sprintf(errmsg, "(%s): bad reference \"%s\"", |
117 |
> |
name, sbuf); |
118 |
> |
objerror(objp, USER, errmsg); |
119 |
|
} |
120 |
< |
objp->otype = objptr(alias)->otype; |
121 |
< |
copystruct(&objp->oargs, &objptr(alias)->oargs); |
120 |
> |
if (objp->omod == OALIAS || |
121 |
> |
objp->omod == objptr(alias)->omod) { |
122 |
> |
objp->omod = alias; |
123 |
> |
} else { |
124 |
> |
objp->oargs.sarg = (char **)malloc(sizeof(char *)); |
125 |
> |
if (objp->oargs.sarg == NULL) |
126 |
> |
error(SYSTEM, "out of memory in getobject"); |
127 |
> |
objp->oargs.nsargs = 1; |
128 |
> |
objp->oargs.sarg[0] = savestr(sbuf); |
129 |
> |
} |
130 |
|
} else if ((rval = readfargs(&objp->oargs, fp)) == 0) { |
131 |
|
sprintf(errmsg, "(%s): bad arguments", name); |
132 |
|
objerror(objp, USER, errmsg); |
134 |
|
sprintf(errmsg, "(%s): error reading scene", name); |
135 |
|
error(SYSTEM, errmsg); |
136 |
|
} |
137 |
+ |
if (objp->omod == OALIAS) { |
138 |
+ |
sprintf(errmsg, "(%s): inappropriate use of '%s' modifier", |
139 |
+ |
name, ALIASMOD); |
140 |
+ |
objerror(objp, USER, errmsg); |
141 |
+ |
} |
142 |
|
/* initialize */ |
143 |
|
objp->os = NULL; |
125 |
– |
objp->lastrno = -1; |
144 |
|
|
145 |
|
insertobject(obj); /* add to global structure */ |
146 |
+ |
#undef OALIAS |
147 |
|
} |
148 |
|
|
149 |
|
|
150 |
< |
int |
150 |
> |
OBJECT |
151 |
|
newobject() /* get a new object */ |
152 |
|
{ |
153 |
|
register int i; |
154 |
|
|
155 |
< |
if ((nobjects & 077) == 0) { /* new block */ |
155 |
> |
if ((nobjects & (OBJBLKSIZ-1)) == 0) { /* new block */ |
156 |
|
errno = 0; |
157 |
< |
i = nobjects >> 6; |
157 |
> |
i = nobjects >> OBJBLKSHFT; |
158 |
|
if (i >= MAXOBJBLK) |
159 |
|
return(OVOID); |
160 |
< |
objblock[i] = (OBJREC *)bmalloc(0100*sizeof(OBJREC)); |
160 |
> |
objblock[i] = (OBJREC *)calloc(OBJBLKSIZ, sizeof(OBJREC)); |
161 |
|
if (objblock[i] == NULL) |
162 |
|
return(OVOID); |
163 |
|
} |
164 |
|
return(nobjects++); |
165 |
+ |
} |
166 |
+ |
|
167 |
+ |
void |
168 |
+ |
freeobjects(firstobj, nobjs) /* free a range of objects */ |
169 |
+ |
int firstobj, nobjs; |
170 |
+ |
{ |
171 |
+ |
register int obj; |
172 |
+ |
/* check bounds */ |
173 |
+ |
if (firstobj < 0) |
174 |
+ |
return; |
175 |
+ |
if (nobjs <= 0) |
176 |
+ |
return; |
177 |
+ |
if (firstobj + nobjs > nobjects) |
178 |
+ |
return; |
179 |
+ |
/* clear objects */ |
180 |
+ |
for (obj = firstobj+nobjs; obj-- > firstobj; ) { |
181 |
+ |
register OBJREC *o = objptr(obj); |
182 |
+ |
free_os(o); /* free client memory */ |
183 |
+ |
freeqstr(o->oname); |
184 |
+ |
freefargs(&o->oargs); |
185 |
+ |
memset((void *)o, '\0', sizeof(OBJREC)); |
186 |
+ |
} |
187 |
+ |
clearobjndx(); |
188 |
+ |
/* free objects off end */ |
189 |
+ |
for (obj = nobjects; obj-- > 0; ) |
190 |
+ |
if (objptr(obj)->oname != NULL) |
191 |
+ |
break; |
192 |
+ |
++obj; |
193 |
+ |
while (nobjects > obj) /* free empty end blocks */ |
194 |
+ |
if ((--nobjects & (OBJBLKSIZ-1)) == 0) { |
195 |
+ |
int i = nobjects >> OBJBLKSHFT; |
196 |
+ |
free((void *)objblock[i]); |
197 |
+ |
objblock[i] = NULL; |
198 |
+ |
} |
199 |
|
} |