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(); |
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 == '!') { /* pipe */ |
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'; |
57 |
readobj(buf); |
58 |
} else { /* object */ |
59 |
ungetc(c, infp); |
60 |
getobject(input, infp); |
61 |
} |
62 |
} |
63 |
if (input[0] == '!') |
64 |
pclose(infp); |
65 |
else |
66 |
fclose(infp); |
67 |
} |
68 |
|
69 |
|
70 |
getobject(name, fp) /* read the next object */ |
71 |
char *name; |
72 |
FILE *fp; |
73 |
{ |
74 |
OBJECT obj; |
75 |
char sbuf[MAXSTR]; |
76 |
register OBJREC *objp; |
77 |
|
78 |
if ((obj = newobject()) == OVOID) |
79 |
error(SYSTEM, "out of object space"); |
80 |
objp = objptr(obj); |
81 |
/* get modifier */ |
82 |
fscanf(fp, "%s", sbuf); |
83 |
if (!strcmp(sbuf, VOIDID)) |
84 |
objp->omod = OVOID; |
85 |
else if ((objp->omod = modifier(sbuf)) == OVOID) { |
86 |
sprintf(errmsg, "(%s): undefined modifier \"%s\"", name, sbuf); |
87 |
error(USER, errmsg); |
88 |
} |
89 |
/* get type */ |
90 |
fscanf(fp, "%s", sbuf); |
91 |
if (!strcmp(sbuf, ALIASID)) |
92 |
objp->otype = -1; |
93 |
else if ((objp->otype = otype(sbuf)) < 0) { |
94 |
sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf); |
95 |
error(USER, errmsg); |
96 |
} |
97 |
/* get identifier */ |
98 |
fscanf(fp, "%s", sbuf); |
99 |
objp->oname = savqstr(sbuf); |
100 |
/* get arguments */ |
101 |
if (objp->otype == -1) { |
102 |
register OBJECT alias; |
103 |
fscanf(fp, "%s", sbuf); |
104 |
if ((alias = modifier(sbuf)) == OVOID) { |
105 |
sprintf(errmsg, |
106 |
"(%s): bad reference \"%s\" for %s \"%s\"", |
107 |
name, sbuf, ALIASID, objp->oname); |
108 |
error(USER, errmsg); |
109 |
} |
110 |
objp->otype = objptr(alias)->otype; |
111 |
bcopy(&objptr(alias)->oargs, &objp->oargs, sizeof(FUNARGS)); |
112 |
} else if (readfargs(&objp->oargs, fp) < 0) { |
113 |
sprintf(errmsg, "(%s): bad arguments", name); |
114 |
objerror(objp, USER, errmsg); |
115 |
} |
116 |
/* initialize */ |
117 |
objp->os = NULL; |
118 |
objp->lastrno = -1; |
119 |
|
120 |
insertobject(obj); /* add to global structure */ |
121 |
} |
122 |
|
123 |
|
124 |
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 |
179 |
newobject() /* get a new object */ |
180 |
{ |
181 |
register int i; |
182 |
|
183 |
if ((nobjects & 077) == 0) { /* new block */ |
184 |
errno = 0; |
185 |
i = nobjects >> 6; |
186 |
if (i >= MAXOBJBLK) |
187 |
return(OVOID); |
188 |
objblock[i] = (OBJREC *)malloc(0100*sizeof(OBJREC)); |
189 |
if (objblock[i] == NULL) |
190 |
return(OVOID); |
191 |
} |
192 |
return(nobjects++); |
193 |
} |