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