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 "copyright.h" |
11 |
+ |
|
12 |
|
#include "standard.h" |
13 |
|
|
14 |
|
#include "object.h" |
18 |
|
#include <ctype.h> |
19 |
|
|
20 |
|
OBJREC *objblock[MAXOBJBLK]; /* our objects */ |
21 |
< |
int nobjects = 0; /* # of objects */ |
21 |
> |
OBJECT nobjects = 0; /* # of objects */ |
22 |
|
|
23 |
|
|
24 |
< |
readobj(input) /* read in an object file or stream */ |
25 |
< |
char *input; |
24 |
> |
void |
25 |
> |
readobj(inpspec) /* read in an object file or stream */ |
26 |
> |
char *inpspec; |
27 |
|
{ |
28 |
< |
FILE *popen(); |
29 |
< |
char *fgets(); |
28 |
> |
OBJECT lastobj; |
29 |
|
FILE *infp; |
30 |
< |
char buf[512]; |
30 |
> |
char buf[1024]; |
31 |
|
register int c; |
32 |
|
|
33 |
< |
if (input == NULL) { |
33 |
> |
lastobj = nobjects; |
34 |
> |
if (inpspec == NULL) { |
35 |
|
infp = stdin; |
36 |
< |
input = "standard input"; |
37 |
< |
} else if (input[0] == '!') { |
38 |
< |
if ((infp = popen(input+1, "r")) == NULL) { |
39 |
< |
sprintf(errmsg, "cannot execute \"%s\"", input); |
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(input, "r")) == NULL) { |
43 |
< |
sprintf(errmsg, "cannot open scene file \"%s\"", input); |
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) { |
48 |
|
continue; |
49 |
|
if (c == '#') { /* comment */ |
50 |
|
fgets(buf, sizeof(buf), infp); |
51 |
< |
} else if (c == '!') { /* pipe */ |
51 |
> |
} else if (c == '!') { /* command */ |
52 |
|
ungetc(c, infp); |
53 |
< |
fgets(buf, sizeof(buf), infp); |
54 |
< |
c = strlen(buf); |
55 |
< |
if (buf[c-1] == '\n') |
56 |
< |
buf[c-1] = '\0'; |
53 |
> |
fgetline(buf, sizeof(buf), infp); |
54 |
|
readobj(buf); |
55 |
|
} else { /* object */ |
56 |
|
ungetc(c, infp); |
57 |
< |
getobject(input, infp); |
57 |
> |
getobject(inpspec, infp); |
58 |
|
} |
59 |
|
} |
60 |
< |
if (input[0] == '!') |
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 |
< |
fscanf(fp, "%s", sbuf); |
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) { |
91 |
|
error(USER, errmsg); |
92 |
|
} |
93 |
|
/* get type */ |
94 |
< |
fscanf(fp, "%s", sbuf); |
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) { |
100 |
|
error(USER, errmsg); |
101 |
|
} |
102 |
|
/* get identifier */ |
103 |
< |
fscanf(fp, "%s", sbuf); |
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 |
< |
fscanf(fp, "%s", sbuf); |
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\"", |
115 |
|
error(USER, errmsg); |
116 |
|
} |
117 |
|
objp->otype = objptr(alias)->otype; |
118 |
< |
bcopy(&objptr(alias)->oargs, &objp->oargs, sizeof(FUNARGS)); |
119 |
< |
} else if (readfargs(&objp->oargs, fp) < 0) { |
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; |
118 |
– |
objp->lastrno = -1; |
128 |
|
|
129 |
|
insertobject(obj); /* add to global structure */ |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
< |
readfargs(fa, fp) /* read function arguments from stream */ |
125 |
< |
register FUNARGS *fa; |
126 |
< |
FILE *fp; |
127 |
< |
{ |
128 |
< |
char sbuf[MAXSTR]; |
129 |
< |
int n; |
130 |
< |
register int i; |
131 |
< |
|
132 |
< |
if (fscanf(fp, "%d", &n) != 1 || n < 0) |
133 |
< |
return(-1); |
134 |
< |
if (fa->nsargs = n) { |
135 |
< |
fa->sarg = (char **)bmalloc(n*sizeof(char *)); |
136 |
< |
if (fa->sarg == NULL) |
137 |
< |
goto memerr; |
138 |
< |
for (i = 0; i < fa->nsargs; i++) { |
139 |
< |
if (fscanf(fp, "%s", sbuf) != 1) |
140 |
< |
return(-1); |
141 |
< |
fa->sarg[i] = savestr(sbuf); |
142 |
< |
} |
143 |
< |
} else |
144 |
< |
fa->sarg = NULL; |
145 |
< |
if (fscanf(fp, "%d", &n) != 1 || n < 0) |
146 |
< |
return(-1); |
147 |
< |
#ifdef IARGS |
148 |
< |
if (fa->niargs = n) { |
149 |
< |
fa->iarg = (long *)bmalloc(n*sizeof(int)); |
150 |
< |
if (fa->iarg == NULL) |
151 |
< |
goto memerr; |
152 |
< |
for (i = 0; i < n; i++) |
153 |
< |
if (fscanf(fp, "%ld", &fa->iarg[i]) != 1) |
154 |
< |
return(-1); |
155 |
< |
} else |
156 |
< |
fa->iarg = NULL; |
157 |
< |
#else |
158 |
< |
if (n != 0) |
159 |
< |
return(-1); |
160 |
< |
#endif |
161 |
< |
if (fscanf(fp, "%d", &n) != 1 || n < 0) |
162 |
< |
return(-1); |
163 |
< |
if (fa->nfargs = n) { |
164 |
< |
fa->farg = (double *)bmalloc(n*sizeof(double)); |
165 |
< |
if (fa->farg == NULL) |
166 |
< |
goto memerr; |
167 |
< |
for (i = 0; i < n; i++) |
168 |
< |
if (fscanf(fp, "%lf", &fa->farg[i]) != 1) |
169 |
< |
return(-1); |
170 |
< |
} else |
171 |
< |
fa->farg = NULL; |
172 |
< |
return(0); |
173 |
< |
memerr: |
174 |
< |
error(SYSTEM, "out of memory in readfargs"); |
175 |
< |
} |
176 |
< |
|
177 |
< |
|
178 |
< |
int |
133 |
> |
OBJECT |
134 |
|
newobject() /* get a new object */ |
135 |
|
{ |
136 |
|
register int i; |
137 |
|
|
138 |
< |
if ((nobjects & 077) == 0) { /* new block */ |
138 |
> |
if ((nobjects & (OBJBLKSIZ-1)) == 0) { /* new block */ |
139 |
|
errno = 0; |
140 |
< |
i = nobjects >> 6; |
140 |
> |
i = nobjects >> OBJBLKSHFT; |
141 |
|
if (i >= MAXOBJBLK) |
142 |
|
return(OVOID); |
143 |
< |
objblock[i] = (OBJREC *)malloc(0100*sizeof(OBJREC)); |
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 |
|
} |