| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: readobj.c,v 2.17 2003/10/22 02:06:34 greg Exp $";
|
| 3 |
#endif
|
| 4 |
/*
|
| 5 |
* readobj.c - routines for reading in object descriptions.
|
| 6 |
*
|
| 7 |
* External symbols declared in object.h
|
| 8 |
*/
|
| 9 |
|
| 10 |
#include "copyright.h"
|
| 11 |
|
| 12 |
#include <ctype.h>
|
| 13 |
#include <string.h>
|
| 14 |
#include <stdio.h>
|
| 15 |
|
| 16 |
#include "platform.h"
|
| 17 |
#include "rtprocess.h"
|
| 18 |
#include "standard.h"
|
| 19 |
#include "object.h"
|
| 20 |
#include "otypes.h"
|
| 21 |
|
| 22 |
|
| 23 |
OBJREC *objblock[MAXOBJBLK]; /* our objects */
|
| 24 |
OBJECT nobjects = 0; /* # of objects */
|
| 25 |
|
| 26 |
|
| 27 |
void
|
| 28 |
readobj(inpspec) /* read in an object file or stream */
|
| 29 |
char *inpspec;
|
| 30 |
{
|
| 31 |
OBJECT lastobj;
|
| 32 |
FILE *infp;
|
| 33 |
char buf[1024];
|
| 34 |
register int c;
|
| 35 |
|
| 36 |
lastobj = nobjects;
|
| 37 |
if (inpspec == NULL) {
|
| 38 |
infp = stdin;
|
| 39 |
inpspec = "standard input";
|
| 40 |
} else if (inpspec[0] == '!') {
|
| 41 |
if ((infp = popen(inpspec+1, "r")) == NULL) {
|
| 42 |
sprintf(errmsg, "cannot execute \"%s\"", inpspec);
|
| 43 |
error(SYSTEM, errmsg);
|
| 44 |
}
|
| 45 |
} else if ((infp = fopen(inpspec, "r")) == NULL) {
|
| 46 |
sprintf(errmsg, "cannot open scene file \"%s\"", inpspec);
|
| 47 |
error(SYSTEM, errmsg);
|
| 48 |
}
|
| 49 |
while ((c = getc(infp)) != EOF) {
|
| 50 |
if (isspace(c))
|
| 51 |
continue;
|
| 52 |
if (c == '#') { /* comment */
|
| 53 |
fgets(buf, sizeof(buf), infp);
|
| 54 |
} else if (c == '!') { /* command */
|
| 55 |
ungetc(c, infp);
|
| 56 |
fgetline(buf, sizeof(buf), infp);
|
| 57 |
readobj(buf);
|
| 58 |
} else { /* object */
|
| 59 |
ungetc(c, infp);
|
| 60 |
getobject(inpspec, infp);
|
| 61 |
}
|
| 62 |
}
|
| 63 |
if (inpspec[0] == '!')
|
| 64 |
pclose(infp);
|
| 65 |
else
|
| 66 |
fclose(infp);
|
| 67 |
if (nobjects == lastobj) {
|
| 68 |
sprintf(errmsg, "(%s): empty file", inpspec);
|
| 69 |
error(WARNING, errmsg);
|
| 70 |
}
|
| 71 |
}
|
| 72 |
|
| 73 |
|
| 74 |
void
|
| 75 |
getobject(name, fp) /* read the next object */
|
| 76 |
char *name;
|
| 77 |
FILE *fp;
|
| 78 |
{
|
| 79 |
#define OALIAS -2
|
| 80 |
OBJECT obj;
|
| 81 |
char sbuf[MAXSTR];
|
| 82 |
int rval;
|
| 83 |
register OBJREC *objp;
|
| 84 |
|
| 85 |
if ((obj = newobject()) == OVOID)
|
| 86 |
error(SYSTEM, "out of object space");
|
| 87 |
objp = objptr(obj);
|
| 88 |
/* get modifier */
|
| 89 |
strcpy(sbuf, "EOF");
|
| 90 |
fgetword(sbuf, MAXSTR, fp);
|
| 91 |
if (!strcmp(sbuf, VOIDID))
|
| 92 |
objp->omod = OVOID;
|
| 93 |
else if (!strcmp(sbuf, ALIASMOD))
|
| 94 |
objp->omod = OALIAS;
|
| 95 |
else if ((objp->omod = modifier(sbuf)) == OVOID) {
|
| 96 |
sprintf(errmsg, "(%s): undefined modifier \"%s\"", name, sbuf);
|
| 97 |
error(USER, errmsg);
|
| 98 |
}
|
| 99 |
/* get type */
|
| 100 |
strcpy(sbuf, "EOF");
|
| 101 |
fgetword(sbuf, MAXSTR, fp);
|
| 102 |
if ((objp->otype = otype(sbuf)) < 0) {
|
| 103 |
sprintf(errmsg, "(%s): unknown type \"%s\"", name, sbuf);
|
| 104 |
error(USER, errmsg);
|
| 105 |
}
|
| 106 |
/* get identifier */
|
| 107 |
sbuf[0] = '\0';
|
| 108 |
fgetword(sbuf, MAXSTR, fp);
|
| 109 |
objp->oname = savqstr(sbuf);
|
| 110 |
/* get arguments */
|
| 111 |
if (objp->otype == MOD_ALIAS) {
|
| 112 |
register OBJECT alias;
|
| 113 |
strcpy(sbuf, "EOF");
|
| 114 |
fgetword(sbuf, MAXSTR, fp);
|
| 115 |
if ((alias = modifier(sbuf)) == OVOID) {
|
| 116 |
sprintf(errmsg, "(%s): bad reference \"%s\"",
|
| 117 |
name, sbuf);
|
| 118 |
objerror(objp, USER, errmsg);
|
| 119 |
}
|
| 120 |
if (objp->omod == OALIAS ||
|
| 121 |
objp->omod == objptr(alias)->omod) {
|
| 122 |
objp->omod = alias;
|
| 123 |
} else {
|
| 124 |
objp->oargs.sarg = (char **)malloc(sizeof(char *));
|
| 125 |
if (objp->oargs.sarg == NULL)
|
| 126 |
error(SYSTEM, "out of memory in getobject");
|
| 127 |
objp->oargs.nsargs = 1;
|
| 128 |
objp->oargs.sarg[0] = savestr(sbuf);
|
| 129 |
}
|
| 130 |
} else if ((rval = readfargs(&objp->oargs, fp)) == 0) {
|
| 131 |
sprintf(errmsg, "(%s): bad arguments", name);
|
| 132 |
objerror(objp, USER, errmsg);
|
| 133 |
} else if (rval < 0) {
|
| 134 |
sprintf(errmsg, "(%s): error reading scene", name);
|
| 135 |
error(SYSTEM, errmsg);
|
| 136 |
}
|
| 137 |
if (objp->omod == OALIAS) {
|
| 138 |
sprintf(errmsg, "(%s): inappropriate use of '%s' modifier",
|
| 139 |
name, ALIASMOD);
|
| 140 |
objerror(objp, USER, errmsg);
|
| 141 |
}
|
| 142 |
/* initialize */
|
| 143 |
objp->os = NULL;
|
| 144 |
|
| 145 |
insertobject(obj); /* add to global structure */
|
| 146 |
#undef OALIAS
|
| 147 |
}
|
| 148 |
|
| 149 |
|
| 150 |
OBJECT
|
| 151 |
newobject() /* get a new object */
|
| 152 |
{
|
| 153 |
register int i;
|
| 154 |
|
| 155 |
if ((nobjects & (OBJBLKSIZ-1)) == 0) { /* new block */
|
| 156 |
errno = 0;
|
| 157 |
i = nobjects >> OBJBLKSHFT;
|
| 158 |
if (i >= MAXOBJBLK)
|
| 159 |
return(OVOID);
|
| 160 |
objblock[i] = (OBJREC *)calloc(OBJBLKSIZ, sizeof(OBJREC));
|
| 161 |
if (objblock[i] == NULL)
|
| 162 |
return(OVOID);
|
| 163 |
}
|
| 164 |
return(nobjects++);
|
| 165 |
}
|
| 166 |
|
| 167 |
void
|
| 168 |
freeobjects(firstobj, nobjs) /* free a range of objects */
|
| 169 |
int firstobj, nobjs;
|
| 170 |
{
|
| 171 |
register int obj;
|
| 172 |
/* check bounds */
|
| 173 |
if (firstobj < 0)
|
| 174 |
return;
|
| 175 |
if (nobjs <= 0)
|
| 176 |
return;
|
| 177 |
if (firstobj + nobjs > nobjects)
|
| 178 |
return;
|
| 179 |
/* clear objects */
|
| 180 |
for (obj = firstobj+nobjs; obj-- > firstobj; ) {
|
| 181 |
register OBJREC *o = objptr(obj);
|
| 182 |
free_os(o); /* free client memory */
|
| 183 |
freeqstr(o->oname);
|
| 184 |
freefargs(&o->oargs);
|
| 185 |
memset((void *)o, '\0', sizeof(OBJREC));
|
| 186 |
}
|
| 187 |
clearobjndx();
|
| 188 |
/* free objects off end */
|
| 189 |
for (obj = nobjects; obj-- > 0; )
|
| 190 |
if (objptr(obj)->oname != NULL)
|
| 191 |
break;
|
| 192 |
++obj;
|
| 193 |
while (nobjects > obj) /* free empty end blocks */
|
| 194 |
if ((--nobjects & (OBJBLKSIZ-1)) == 0) {
|
| 195 |
int i = nobjects >> OBJBLKSHFT;
|
| 196 |
free((void *)objblock[i]);
|
| 197 |
objblock[i] = NULL;
|
| 198 |
}
|
| 199 |
}
|