1 |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* readobj.c - routines for reading in object descriptions. |
9 |
* |
10 |
* 7/28/85 |
11 |
*/ |
12 |
|
13 |
#include "standard.h" |
14 |
|
15 |
#include "object.h" |
16 |
|
17 |
#include "otypes.h" |
18 |
|
19 |
#include <ctype.h> |
20 |
|
21 |
OBJREC *objblock[MAXOBJBLK]; /* our objects */ |
22 |
int nobjects = 0; /* # of objects */ |
23 |
|
24 |
|
25 |
readobj(input) /* read in an object file or stream */ |
26 |
char *input; |
27 |
{ |
28 |
FILE *popen(); |
29 |
char *fgets(), *fgetline(); |
30 |
FILE *infp; |
31 |
char buf[512]; |
32 |
register int c; |
33 |
|
34 |
if (input == 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); |
40 |
error(SYSTEM, errmsg); |
41 |
} |
42 |
} else if ((infp = fopen(input, "r")) == NULL) { |
43 |
sprintf(errmsg, "cannot open scene file \"%s\"", input); |
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(input, infp); |
58 |
} |
59 |
} |
60 |
if (input[0] == '!') |
61 |
pclose(infp); |
62 |
else |
63 |
fclose(infp); |
64 |
} |
65 |
|
66 |
|
67 |
getobject(name, fp) /* read the next object */ |
68 |
char *name; |
69 |
FILE *fp; |
70 |
{ |
71 |
OBJECT obj; |
72 |
char sbuf[MAXSTR]; |
73 |
register OBJREC *objp; |
74 |
|
75 |
if ((obj = newobject()) == OVOID) |
76 |
error(SYSTEM, "out of object space"); |
77 |
objp = objptr(obj); |
78 |
/* get modifier */ |
79 |
fscanf(fp, "%s", sbuf); |
80 |
if (!strcmp(sbuf, VOIDID)) |
81 |
objp->omod = OVOID; |
82 |
else if ((objp->omod = modifier(sbuf)) == OVOID) { |
83 |
sprintf(errmsg, "(%s): undefined modifier \"%s\"", name, sbuf); |
84 |
error(USER, errmsg); |
85 |
} |
86 |
/* get type */ |
87 |
fscanf(fp, "%s", sbuf); |
88 |
if (!strcmp(sbuf, ALIASID)) |
89 |
objp->otype = -1; |
90 |
else if ((objp->otype = otype(sbuf)) < 0) { |
91 |
sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf); |
92 |
error(USER, errmsg); |
93 |
} |
94 |
/* get identifier */ |
95 |
fscanf(fp, "%s", sbuf); |
96 |
objp->oname = savqstr(sbuf); |
97 |
/* get arguments */ |
98 |
if (objp->otype == -1) { |
99 |
register OBJECT alias; |
100 |
fscanf(fp, "%s", sbuf); |
101 |
if ((alias = modifier(sbuf)) == OVOID) { |
102 |
sprintf(errmsg, |
103 |
"(%s): bad reference \"%s\" for %s \"%s\"", |
104 |
name, sbuf, ALIASID, objp->oname); |
105 |
error(USER, errmsg); |
106 |
} |
107 |
objp->otype = objptr(alias)->otype; |
108 |
bcopy(&objptr(alias)->oargs, &objp->oargs, sizeof(FUNARGS)); |
109 |
} else if (readfargs(&objp->oargs, fp) < 0) { |
110 |
sprintf(errmsg, "(%s): bad arguments", name); |
111 |
objerror(objp, USER, errmsg); |
112 |
} |
113 |
/* initialize */ |
114 |
objp->os = NULL; |
115 |
objp->lastrno = -1; |
116 |
|
117 |
insertobject(obj); /* add to global structure */ |
118 |
} |
119 |
|
120 |
|
121 |
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(int)); |
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 |
176 |
newobject() /* get a new object */ |
177 |
{ |
178 |
register int i; |
179 |
|
180 |
if ((nobjects & 077) == 0) { /* new block */ |
181 |
errno = 0; |
182 |
i = nobjects >> 6; |
183 |
if (i >= MAXOBJBLK) |
184 |
return(OVOID); |
185 |
objblock[i] = (OBJREC *)malloc(0100*sizeof(OBJREC)); |
186 |
if (objblock[i] == NULL) |
187 |
return(OVOID); |
188 |
} |
189 |
return(nobjects++); |
190 |
} |