| 1 |
greg |
1.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 |
|
|
OBJREC *objblock[MAXOBJBLK]; /* our objects */ |
| 20 |
|
|
OBJECT nobjects = 0; /* # of objects */ |
| 21 |
|
|
int newobject() {return(0);} |
| 22 |
|
|
|
| 23 |
|
|
|
| 24 |
|
|
readobj(input, callback) /* read in an object file or stream */ |
| 25 |
|
|
char *input; |
| 26 |
|
|
int (*callback)(); |
| 27 |
|
|
{ |
| 28 |
|
|
FILE *popen(); |
| 29 |
|
|
char *fgets(), *fgetline(); |
| 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 == '!') { /* command */ |
| 52 |
|
|
ungetc(c, infp); |
| 53 |
|
|
fgetline(buf, sizeof(buf), infp); |
| 54 |
greg |
1.2 |
readobj(buf, callback); |
| 55 |
greg |
1.1 |
} else { /* object */ |
| 56 |
|
|
ungetc(c, infp); |
| 57 |
|
|
getobject(input, infp, callback); |
| 58 |
|
|
} |
| 59 |
|
|
} |
| 60 |
|
|
if (input[0] == '!') |
| 61 |
|
|
pclose(infp); |
| 62 |
|
|
else |
| 63 |
|
|
fclose(infp); |
| 64 |
|
|
} |
| 65 |
|
|
|
| 66 |
|
|
|
| 67 |
|
|
getobject(name, fp, f) /* read the next object */ |
| 68 |
|
|
char *name; |
| 69 |
|
|
FILE *fp; |
| 70 |
|
|
int (*f)(); |
| 71 |
|
|
{ |
| 72 |
|
|
char sbuf[MAXSTR]; |
| 73 |
|
|
OBJREC thisobj; |
| 74 |
|
|
/* get modifier */ |
| 75 |
greg |
1.2 |
fscanf(fp, "%*s"); |
| 76 |
|
|
thisobj.omod = OVOID; |
| 77 |
greg |
1.1 |
/* get type */ |
| 78 |
|
|
fscanf(fp, "%s", sbuf); |
| 79 |
|
|
if (!strcmp(sbuf, ALIASID)) |
| 80 |
|
|
thisobj.otype = -1; |
| 81 |
|
|
else if ((thisobj.otype = otype(sbuf)) < 0) { |
| 82 |
|
|
sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf); |
| 83 |
|
|
error(USER, errmsg); |
| 84 |
|
|
} |
| 85 |
|
|
/* get identifier */ |
| 86 |
|
|
fscanf(fp, "%s", sbuf); |
| 87 |
|
|
thisobj.oname = sbuf; |
| 88 |
|
|
/* get arguments */ |
| 89 |
|
|
if (thisobj.otype == -1) { |
| 90 |
|
|
fscanf(fp, "%*s"); |
| 91 |
|
|
return; |
| 92 |
|
|
} |
| 93 |
|
|
if (readfargs(&thisobj.oargs, fp) < 0) { |
| 94 |
|
|
sprintf(errmsg, "(%s): bad arguments", name); |
| 95 |
|
|
objerror(&thisobj, USER, errmsg); |
| 96 |
|
|
} |
| 97 |
|
|
thisobj.os = NULL; |
| 98 |
|
|
thisobj.lastrno = -1; |
| 99 |
|
|
/* call function */ |
| 100 |
|
|
(*f)(&thisobj); |
| 101 |
|
|
/* 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 |
|
|
} |
| 118 |
|
|
freefargs(&thisobj.oargs); |
| 119 |
|
|
} |
| 120 |
|
|
|
| 121 |
|
|
|
| 122 |
|
|
readfargs(fa, fp) /* read function arguments from stream */ |
| 123 |
|
|
register FUNARGS *fa; |
| 124 |
|
|
FILE *fp; |
| 125 |
|
|
{ |
| 126 |
greg |
1.3 |
extern char *strcpy(); |
| 127 |
greg |
1.1 |
char sbuf[MAXSTR]; |
| 128 |
|
|
int n; |
| 129 |
|
|
register int i; |
| 130 |
|
|
|
| 131 |
|
|
if (fscanf(fp, "%d", &n) != 1 || n < 0) |
| 132 |
|
|
return(-1); |
| 133 |
|
|
if (fa->nsargs = n) { |
| 134 |
|
|
fa->sarg = (char **)malloc(n*sizeof(char *)); |
| 135 |
|
|
if (fa->sarg == NULL) |
| 136 |
|
|
goto memerr; |
| 137 |
|
|
for (i = 0; i < fa->nsargs; i++) { |
| 138 |
|
|
if (fscanf(fp, "%s", sbuf) != 1) |
| 139 |
|
|
return(-1); |
| 140 |
greg |
1.3 |
fa->sarg[i] = malloc(strlen(sbuf)+1); |
| 141 |
|
|
if (fa->sarg[i] == NULL) |
| 142 |
|
|
goto memerr; |
| 143 |
|
|
(void)strcpy(fa->sarg[i], sbuf); |
| 144 |
greg |
1.1 |
} |
| 145 |
|
|
} else |
| 146 |
|
|
fa->sarg = NULL; |
| 147 |
|
|
if (fscanf(fp, "%d", &n) != 1 || n < 0) |
| 148 |
|
|
return(-1); |
| 149 |
|
|
#ifdef IARGS |
| 150 |
|
|
if (fa->niargs = n) { |
| 151 |
|
|
fa->iarg = (long *)malloc(n*sizeof(long)); |
| 152 |
|
|
if (fa->iarg == NULL) |
| 153 |
|
|
goto memerr; |
| 154 |
|
|
for (i = 0; i < n; i++) |
| 155 |
|
|
if (fscanf(fp, "%ld", &fa->iarg[i]) != 1) |
| 156 |
|
|
return(-1); |
| 157 |
|
|
} else |
| 158 |
|
|
fa->iarg = NULL; |
| 159 |
|
|
#else |
| 160 |
|
|
if (n != 0) |
| 161 |
|
|
return(-1); |
| 162 |
|
|
#endif |
| 163 |
|
|
if (fscanf(fp, "%d", &n) != 1 || n < 0) |
| 164 |
|
|
return(-1); |
| 165 |
|
|
if (fa->nfargs = n) { |
| 166 |
|
|
fa->farg = (double *)malloc(n*sizeof(double)); |
| 167 |
|
|
if (fa->farg == NULL) |
| 168 |
|
|
goto memerr; |
| 169 |
|
|
for (i = 0; i < n; i++) |
| 170 |
|
|
if (fscanf(fp, "%lf", &fa->farg[i]) != 1) |
| 171 |
|
|
return(-1); |
| 172 |
|
|
} else |
| 173 |
|
|
fa->farg = NULL; |
| 174 |
|
|
return(0); |
| 175 |
|
|
memerr: |
| 176 |
|
|
error(SYSTEM, "out of memory in readfargs"); |
| 177 |
|
|
} |
| 178 |
|
|
|
| 179 |
|
|
|
| 180 |
|
|
freefargs(fa) /* free arguments */ |
| 181 |
|
|
register FUNARGS *fa; |
| 182 |
|
|
{ |
| 183 |
|
|
register int i; |
| 184 |
|
|
|
| 185 |
|
|
if (fa->nsargs) { |
| 186 |
|
|
for (i = 0; i < fa->nsargs; i++) |
| 187 |
|
|
free(fa->sarg[i]); |
| 188 |
|
|
free((char *)fa->sarg); |
| 189 |
|
|
} |
| 190 |
|
|
#ifdef IARGS |
| 191 |
|
|
if (fa->niargs) |
| 192 |
|
|
free((char *)fa->iarg); |
| 193 |
|
|
#endif |
| 194 |
|
|
if (fa->nfargs) |
| 195 |
|
|
free((char *)fa->farg); |
| 196 |
|
|
} |