ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/readobj.c
(Generate patch)

Comparing ray/src/common/readobj.c (file contents):
Revision 2.3 by greg, Fri Jun 4 15:08:20 1993 UTC vs.
Revision 2.27 by greg, Tue Apr 22 04:45:25 2025 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines