1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id$"; |
3 |
#endif |
4 |
/* |
5 |
* readobj2.c - routines for reading in object descriptions. |
6 |
*/ |
7 |
|
8 |
#include "standard.h" |
9 |
|
10 |
#include "object.h" |
11 |
|
12 |
#include "otypes.h" |
13 |
|
14 |
#include <ctype.h> |
15 |
|
16 |
extern char *fgetword(); |
17 |
|
18 |
readobj2(input, callback) /* read in an object file or stream */ |
19 |
char *input; |
20 |
int (*callback)(); |
21 |
{ |
22 |
FILE *popen(); |
23 |
char *fgetline(); |
24 |
FILE *infp; |
25 |
char buf[512]; |
26 |
register int c; |
27 |
|
28 |
if (input == NULL) { |
29 |
infp = stdin; |
30 |
input = "standard input"; |
31 |
} else if (input[0] == '!') { |
32 |
if ((infp = popen(input+1, "r")) == NULL) { |
33 |
sprintf(errmsg, "cannot execute \"%s\"", input); |
34 |
error(SYSTEM, errmsg); |
35 |
} |
36 |
} else if ((infp = fopen(input, "r")) == NULL) { |
37 |
sprintf(errmsg, "cannot open scene file \"%s\"", input); |
38 |
error(SYSTEM, errmsg); |
39 |
} |
40 |
while ((c = getc(infp)) != EOF) { |
41 |
if (isspace(c)) |
42 |
continue; |
43 |
if (c == '#') { /* comment */ |
44 |
fgets(buf, sizeof(buf), infp); |
45 |
} else if (c == '!') { /* command */ |
46 |
ungetc(c, infp); |
47 |
fgetline(buf, sizeof(buf), infp); |
48 |
readobj2(buf, callback); |
49 |
} else { /* object */ |
50 |
ungetc(c, infp); |
51 |
getobject2(input, infp, callback); |
52 |
} |
53 |
} |
54 |
if (input[0] == '!') |
55 |
pclose(infp); |
56 |
else |
57 |
fclose(infp); |
58 |
} |
59 |
|
60 |
|
61 |
getobject2(name, fp, f) /* read the next object */ |
62 |
char *name; |
63 |
FILE *fp; |
64 |
int (*f)(); |
65 |
{ |
66 |
char sbuf[MAXSTR]; |
67 |
OBJREC thisobj; |
68 |
/* get modifier */ |
69 |
fgetword(sbuf, MAXSTR, fp); |
70 |
thisobj.omod = OVOID; |
71 |
/* get type */ |
72 |
fgetword(sbuf, MAXSTR, fp); |
73 |
if (!strcmp(sbuf, ALIASID)) |
74 |
thisobj.otype = -1; |
75 |
else if ((thisobj.otype = otype(sbuf)) < 0) { |
76 |
sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf); |
77 |
error(USER, errmsg); |
78 |
} |
79 |
/* get identifier */ |
80 |
fgetword(sbuf, MAXSTR, fp); |
81 |
thisobj.oname = sbuf; |
82 |
/* get arguments */ |
83 |
if (thisobj.otype == -1) { |
84 |
fscanf(fp, "%*s"); |
85 |
return; |
86 |
} |
87 |
if (readfargs(&thisobj.oargs, fp) <= 0) { |
88 |
sprintf(errmsg, "(%s): bad arguments", name); |
89 |
objerror(&thisobj, USER, errmsg); |
90 |
} |
91 |
thisobj.os = NULL; |
92 |
/* call function */ |
93 |
(*f)(&thisobj); |
94 |
/* free memory */ |
95 |
freefargs(&thisobj.oargs); |
96 |
free_os(&thisobj); |
97 |
} |