1 |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* readobj2.c - routines for reading in object descriptions. |
9 |
*/ |
10 |
|
11 |
#include "standard.h" |
12 |
|
13 |
#include "object.h" |
14 |
|
15 |
#include "otypes.h" |
16 |
|
17 |
#include <ctype.h> |
18 |
|
19 |
extern char *fgetword(); |
20 |
|
21 |
OBJREC *objblock[MAXOBJBLK]; /* our objects */ |
22 |
OBJECT nobjects = 0; /* # of objects */ |
23 |
int newobject() {return(0);} |
24 |
|
25 |
|
26 |
readobj(input, callback) /* read in an object file or stream */ |
27 |
char *input; |
28 |
int (*callback)(); |
29 |
{ |
30 |
FILE *popen(); |
31 |
char *fgets(), *fgetline(); |
32 |
FILE *infp; |
33 |
char buf[512]; |
34 |
register int c; |
35 |
|
36 |
if (input == NULL) { |
37 |
infp = stdin; |
38 |
input = "standard input"; |
39 |
} else if (input[0] == '!') { |
40 |
if ((infp = popen(input+1, "r")) == NULL) { |
41 |
sprintf(errmsg, "cannot execute \"%s\"", input); |
42 |
error(SYSTEM, errmsg); |
43 |
} |
44 |
} else if ((infp = fopen(input, "r")) == NULL) { |
45 |
sprintf(errmsg, "cannot open scene file \"%s\"", input); |
46 |
error(SYSTEM, errmsg); |
47 |
} |
48 |
while ((c = getc(infp)) != EOF) { |
49 |
if (isspace(c)) |
50 |
continue; |
51 |
if (c == '#') { /* comment */ |
52 |
fgets(buf, sizeof(buf), infp); |
53 |
} else if (c == '!') { /* command */ |
54 |
ungetc(c, infp); |
55 |
fgetline(buf, sizeof(buf), infp); |
56 |
readobj(buf, callback); |
57 |
} else { /* object */ |
58 |
ungetc(c, infp); |
59 |
getobject(input, infp, callback); |
60 |
} |
61 |
} |
62 |
if (input[0] == '!') |
63 |
pclose(infp); |
64 |
else |
65 |
fclose(infp); |
66 |
} |
67 |
|
68 |
|
69 |
getobject(name, fp, f) /* read the next object */ |
70 |
char *name; |
71 |
FILE *fp; |
72 |
int (*f)(); |
73 |
{ |
74 |
char sbuf[MAXSTR]; |
75 |
OBJREC thisobj; |
76 |
/* get modifier */ |
77 |
fgetword(sbuf, MAXSTR, fp); |
78 |
thisobj.omod = OVOID; |
79 |
/* get type */ |
80 |
fgetword(sbuf, MAXSTR, fp); |
81 |
if (!strcmp(sbuf, ALIASID)) |
82 |
thisobj.otype = -1; |
83 |
else if ((thisobj.otype = otype(sbuf)) < 0) { |
84 |
sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf); |
85 |
error(USER, errmsg); |
86 |
} |
87 |
/* get identifier */ |
88 |
fgetword(sbuf, MAXSTR, fp); |
89 |
thisobj.oname = sbuf; |
90 |
/* get arguments */ |
91 |
if (thisobj.otype == -1) { |
92 |
fscanf(fp, "%*s"); |
93 |
return; |
94 |
} |
95 |
if (readfargs(&thisobj.oargs, fp) <= 0) { |
96 |
sprintf(errmsg, "(%s): bad arguments", name); |
97 |
objerror(&thisobj, USER, errmsg); |
98 |
} |
99 |
thisobj.os = NULL; |
100 |
/* call function */ |
101 |
(*f)(&thisobj); |
102 |
/* free memory */ |
103 |
if (thisobj.os != NULL) |
104 |
switch (thisobj.otype) { |
105 |
case OBJ_CONE: |
106 |
case OBJ_CUP: |
107 |
case OBJ_CYLINDER: |
108 |
case OBJ_TUBE: |
109 |
case OBJ_RING: |
110 |
freecone(&thisobj); |
111 |
break; |
112 |
case OBJ_FACE: |
113 |
freeface(&thisobj); |
114 |
break; |
115 |
case OBJ_INSTANCE: |
116 |
freeinstance(&thisobj); |
117 |
break; |
118 |
} |
119 |
freefargs(&thisobj.oargs); |
120 |
} |