1 |
– |
/* Copyright (c) 1991 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 |
|
* readobj2.c - routines for reading in object descriptions. |
6 |
|
*/ |
7 |
|
|
8 |
< |
#include "standard.h" |
8 |
> |
#include <ctype.h> |
9 |
> |
#include <stdio.h> |
10 |
|
|
11 |
+ |
#include "platform.h" |
12 |
+ |
#include "paths.h" |
13 |
+ |
#include "rtmath.h" |
14 |
+ |
#include "rtio.h" |
15 |
+ |
#include "rterror.h" |
16 |
|
#include "object.h" |
14 |
– |
|
17 |
|
#include "otypes.h" |
18 |
+ |
#include "oconv.h" |
19 |
|
|
17 |
– |
#include <ctype.h> |
20 |
|
|
21 |
< |
OBJREC *objblock[MAXOBJBLK]; /* our objects */ |
20 |
< |
OBJECT nobjects = 0; /* # of objects */ |
21 |
< |
int newobject() {return(0);} |
21 |
> |
static void getobject2(char *name, FILE *fp, ro_cbfunc f); |
22 |
|
|
23 |
|
|
24 |
< |
readobj(input, callback) /* read in an object file or stream */ |
25 |
< |
char *input; |
26 |
< |
int (*callback)(); |
24 |
> |
void |
25 |
> |
readobj2( /* read in an object file or stream */ |
26 |
> |
char *input, |
27 |
> |
ro_cbfunc callback |
28 |
> |
) |
29 |
|
{ |
28 |
– |
FILE *popen(); |
29 |
– |
char *fgets(), *fgetline(); |
30 |
|
FILE *infp; |
31 |
< |
char buf[512]; |
31 |
> |
char buf[2048]; |
32 |
|
register int c; |
33 |
|
|
34 |
|
if (input == NULL) { |
51 |
|
} else if (c == '!') { /* command */ |
52 |
|
ungetc(c, infp); |
53 |
|
fgetline(buf, sizeof(buf), infp); |
54 |
< |
readobj(buf); |
54 |
> |
readobj2(buf, callback); |
55 |
|
} else { /* object */ |
56 |
|
ungetc(c, infp); |
57 |
< |
getobject(input, infp, callback); |
57 |
> |
getobject2(input, infp, callback); |
58 |
|
} |
59 |
|
} |
60 |
< |
if (input[0] == '!') |
61 |
< |
pclose(infp); |
62 |
< |
else |
60 |
> |
if (input[0] == '!') { |
61 |
> |
if (pclose(infp) != 0) { |
62 |
> |
sprintf(errmsg, "bad status from \"%s\"", input); |
63 |
> |
error(SYSTEM, errmsg); |
64 |
> |
} |
65 |
> |
} else |
66 |
|
fclose(infp); |
67 |
|
} |
68 |
|
|
69 |
|
|
70 |
< |
getobject(name, fp, f) /* read the next object */ |
71 |
< |
char *name; |
72 |
< |
FILE *fp; |
73 |
< |
int (*f)(); |
70 |
> |
static void |
71 |
> |
getobject2( /* read the next object */ |
72 |
> |
char *name, |
73 |
> |
FILE *fp, |
74 |
> |
ro_cbfunc f |
75 |
> |
) |
76 |
|
{ |
77 |
|
char sbuf[MAXSTR]; |
78 |
|
OBJREC thisobj; |
79 |
|
/* get modifier */ |
80 |
< |
fscanf(fp, "%s", sbuf); |
81 |
< |
thisobj.omod = modifier(sbuf); |
80 |
> |
fgetword(sbuf, MAXSTR, fp); |
81 |
> |
thisobj.omod = OVOID; |
82 |
|
/* get type */ |
83 |
< |
fscanf(fp, "%s", sbuf); |
84 |
< |
if (!strcmp(sbuf, ALIASID)) |
80 |
< |
thisobj.otype = -1; |
81 |
< |
else if ((thisobj.otype = otype(sbuf)) < 0) { |
83 |
> |
fgetword(sbuf, MAXSTR, fp); |
84 |
> |
if ((thisobj.otype = otype(sbuf)) < 0) { |
85 |
|
sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf); |
86 |
|
error(USER, errmsg); |
87 |
|
} |
88 |
|
/* get identifier */ |
89 |
< |
fscanf(fp, "%s", sbuf); |
89 |
> |
fgetword(sbuf, MAXSTR, fp); |
90 |
|
thisobj.oname = sbuf; |
91 |
|
/* get arguments */ |
92 |
< |
if (thisobj.otype == -1) { |
92 |
> |
if (thisobj.otype == MOD_ALIAS) { |
93 |
|
fscanf(fp, "%*s"); |
94 |
|
return; |
95 |
|
} |
96 |
< |
if (readfargs(&thisobj.oargs, fp) < 0) { |
96 |
> |
if (readfargs(&thisobj.oargs, fp) <= 0) { |
97 |
|
sprintf(errmsg, "(%s): bad arguments", name); |
98 |
|
objerror(&thisobj, USER, errmsg); |
99 |
|
} |
100 |
|
thisobj.os = NULL; |
98 |
– |
thisobj.lastrno = -1; |
101 |
|
/* call function */ |
102 |
|
(*f)(&thisobj); |
103 |
|
/* free memory */ |
102 |
– |
if (thisobj.os != NULL) |
103 |
– |
switch (thisobj.otype) { |
104 |
– |
case OBJ_CONE: |
105 |
– |
case OBJ_CUP: |
106 |
– |
case OBJ_CYLINDER: |
107 |
– |
case OBJ_TUBE: |
108 |
– |
case OBJ_RING: |
109 |
– |
freecone(&thisobj); |
110 |
– |
break; |
111 |
– |
case OBJ_FACE: |
112 |
– |
freeface(&thisobj); |
113 |
– |
break; |
114 |
– |
case OBJ_INSTANCE: |
115 |
– |
freeinstance(&thisobj); |
116 |
– |
break; |
117 |
– |
} |
104 |
|
freefargs(&thisobj.oargs); |
105 |
< |
} |
120 |
< |
|
121 |
< |
|
122 |
< |
readfargs(fa, fp) /* read function arguments from stream */ |
123 |
< |
register FUNARGS *fa; |
124 |
< |
FILE *fp; |
125 |
< |
{ |
126 |
< |
char sbuf[MAXSTR]; |
127 |
< |
int n; |
128 |
< |
register int i; |
129 |
< |
|
130 |
< |
if (fscanf(fp, "%d", &n) != 1 || n < 0) |
131 |
< |
return(-1); |
132 |
< |
if (fa->nsargs = n) { |
133 |
< |
fa->sarg = (char **)malloc(n*sizeof(char *)); |
134 |
< |
if (fa->sarg == NULL) |
135 |
< |
goto memerr; |
136 |
< |
for (i = 0; i < fa->nsargs; i++) { |
137 |
< |
if (fscanf(fp, "%s", sbuf) != 1) |
138 |
< |
return(-1); |
139 |
< |
fa->sarg[i] = savestr(sbuf); |
140 |
< |
} |
141 |
< |
} else |
142 |
< |
fa->sarg = NULL; |
143 |
< |
if (fscanf(fp, "%d", &n) != 1 || n < 0) |
144 |
< |
return(-1); |
145 |
< |
#ifdef IARGS |
146 |
< |
if (fa->niargs = n) { |
147 |
< |
fa->iarg = (long *)malloc(n*sizeof(long)); |
148 |
< |
if (fa->iarg == NULL) |
149 |
< |
goto memerr; |
150 |
< |
for (i = 0; i < n; i++) |
151 |
< |
if (fscanf(fp, "%ld", &fa->iarg[i]) != 1) |
152 |
< |
return(-1); |
153 |
< |
} else |
154 |
< |
fa->iarg = NULL; |
155 |
< |
#else |
156 |
< |
if (n != 0) |
157 |
< |
return(-1); |
158 |
< |
#endif |
159 |
< |
if (fscanf(fp, "%d", &n) != 1 || n < 0) |
160 |
< |
return(-1); |
161 |
< |
if (fa->nfargs = n) { |
162 |
< |
fa->farg = (double *)malloc(n*sizeof(double)); |
163 |
< |
if (fa->farg == NULL) |
164 |
< |
goto memerr; |
165 |
< |
for (i = 0; i < n; i++) |
166 |
< |
if (fscanf(fp, "%lf", &fa->farg[i]) != 1) |
167 |
< |
return(-1); |
168 |
< |
} else |
169 |
< |
fa->farg = NULL; |
170 |
< |
return(0); |
171 |
< |
memerr: |
172 |
< |
error(SYSTEM, "out of memory in readfargs"); |
173 |
< |
} |
174 |
< |
|
175 |
< |
|
176 |
< |
freefargs(fa) /* free arguments */ |
177 |
< |
register FUNARGS *fa; |
178 |
< |
{ |
179 |
< |
register int i; |
180 |
< |
|
181 |
< |
if (fa->nsargs) { |
182 |
< |
for (i = 0; i < fa->nsargs; i++) |
183 |
< |
free(fa->sarg[i]); |
184 |
< |
free((char *)fa->sarg); |
185 |
< |
} |
186 |
< |
#ifdef IARGS |
187 |
< |
if (fa->niargs) |
188 |
< |
free((char *)fa->iarg); |
189 |
< |
#endif |
190 |
< |
if (fa->nfargs) |
191 |
< |
free((char *)fa->farg); |
105 |
> |
free_os(&thisobj); |
106 |
|
} |