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

Comparing ray/src/cv/mgflib/parser.c (file contents):
Revision 1.15 by greg, Thu Apr 13 12:11:30 1995 UTC vs.
Revision 1.24 by greg, Tue Mar 18 11:17:24 1997 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1994 Regents of the University of California */
1 > /* Copyright (c) 1996 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 27 | Line 27 | char   mg_ename[MG_NENTITIES][MG_MAXELEN] = MG_NAMELIST;
27  
28   int     (*mg_ehand[MG_NENTITIES])();
29  
30 +                        /* Handler routine for unknown entities */
31 +
32 + int     (*mg_uhand)() = mg_defuhand;
33 +
34 + unsigned        mg_nunknown;    /* count of unknown entities */
35 +
36                          /* error messages */
37  
38   char    *mg_err[MG_NERRS] = MG_ERRLIST;
# Line 57 | Line 63 | int    mg_nqcdivs = MG_NQCD;   /* number of divisions per q
63  
64   static int      e_any_toss(),           /* discard unneeded entity */
65                  e_ies(),                /* IES luminaire file */
60                e_include(),            /* include file */
61                e_sph(),                /* sphere */
66                  e_cct(),                /* color temperature */
67                  e_cmix(),               /* color mixtures */
68 <                e_cspec(),              /* color spectra */
65 <                e_cyl(),                /* cylinder */
66 <                e_cone(),               /* cone */
67 <                e_prism(),              /* prism */
68 <                e_ring(),               /* ring */
69 <                e_torus();              /* torus */
68 >                e_cspec();              /* color spectra */
69  
70                                  /* alternate handler support functions */
71  
# Line 89 | Line 88 | mg_init()                      /* initialize alternate entity handlers */
88                  mg_ehand[MG_E_INCLUDE] = e_include;
89          if (mg_ehand[MG_E_SPH] == NULL) {
90                  mg_ehand[MG_E_SPH] = e_sph;
91 <                ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX;
91 >                ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX;
92          } else
93 <                uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
93 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
94          if (mg_ehand[MG_E_CYL] == NULL) {
95                  mg_ehand[MG_E_CYL] = e_cyl;
96 <                ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX;
96 >                ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX;
97          } else
98 <                uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
98 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
99          if (mg_ehand[MG_E_CONE] == NULL) {
100                  mg_ehand[MG_E_CONE] = e_cone;
101 <                ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX;
101 >                ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX;
102          } else
103 <                uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
103 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
104          if (mg_ehand[MG_E_RING] == NULL) {
105                  mg_ehand[MG_E_RING] = e_ring;
106 <                ineed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX;
106 >                ineed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX;
107          } else
108 <                uneed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX|1<<MG_E_XF;
108 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX|1L<<MG_E_XF;
109          if (mg_ehand[MG_E_PRISM] == NULL) {
110                  mg_ehand[MG_E_PRISM] = e_prism;
111 <                ineed |= 1<<MG_E_POINT|1<<MG_E_VERTEX;
111 >                ineed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX;
112          } else
113 <                uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
113 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
114          if (mg_ehand[MG_E_TORUS] == NULL) {
115                  mg_ehand[MG_E_TORUS] = e_torus;
116 <                ineed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX;
116 >                ineed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX;
117          } else
118 <                uneed |= 1<<MG_E_POINT|1<<MG_E_NORMAL|1<<MG_E_VERTEX|1<<MG_E_XF;
118 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX|1L<<MG_E_XF;
119 >        if (mg_ehand[MG_E_FACE] == NULL)
120 >                mg_ehand[MG_E_FACE] = mg_ehand[MG_E_FACEH];
121 >        else if (mg_ehand[MG_E_FACEH] == NULL)
122 >                mg_ehand[MG_E_FACEH] = e_faceh;
123          if (mg_ehand[MG_E_COLOR] != NULL) {
124                  if (mg_ehand[MG_E_CMIX] == NULL) {
125                          mg_ehand[MG_E_CMIX] = e_cmix;
126 <                        ineed |= 1<<MG_E_COLOR|1<<MG_E_CXY|1<<MG_E_CSPEC|1<<MG_E_CMIX|1<<MG_E_CCT;
126 >                        ineed |= 1L<<MG_E_COLOR|1L<<MG_E_CXY|1L<<MG_E_CSPEC|1L<<MG_E_CMIX|1L<<MG_E_CCT;
127                  }
128                  if (mg_ehand[MG_E_CSPEC] == NULL) {
129                          mg_ehand[MG_E_CSPEC] = e_cspec;
130 <                        ineed |= 1<<MG_E_COLOR|1<<MG_E_CXY|1<<MG_E_CSPEC|1<<MG_E_CMIX|1<<MG_E_CCT;
130 >                        ineed |= 1L<<MG_E_COLOR|1L<<MG_E_CXY|1L<<MG_E_CSPEC|1L<<MG_E_CMIX|1L<<MG_E_CCT;
131                  }
132                  if (mg_ehand[MG_E_CCT] == NULL) {
133                          mg_ehand[MG_E_CCT] = e_cct;
134 <                        ineed |= 1<<MG_E_COLOR|1<<MG_E_CXY|1<<MG_E_CSPEC|1<<MG_E_CMIX|1<<MG_E_CCT;
134 >                        ineed |= 1L<<MG_E_COLOR|1L<<MG_E_CXY|1L<<MG_E_CSPEC|1L<<MG_E_CMIX|1L<<MG_E_CCT;
135                  }
136          }
137                                          /* check for consistency */
138          if (mg_ehand[MG_E_FACE] != NULL)
139 <                uneed |= 1<<MG_E_POINT|1<<MG_E_VERTEX|1<<MG_E_XF;
139 >                uneed |= 1L<<MG_E_POINT|1L<<MG_E_VERTEX|1L<<MG_E_XF;
140          if (mg_ehand[MG_E_CXY] != NULL || mg_ehand[MG_E_CSPEC] != NULL ||
141                          mg_ehand[MG_E_CMIX] != NULL)
142 <                uneed |= 1<<MG_E_COLOR;
142 >                uneed |= 1L<<MG_E_COLOR;
143          if (mg_ehand[MG_E_RD] != NULL || mg_ehand[MG_E_TD] != NULL ||
144                          mg_ehand[MG_E_IR] != NULL ||
145                          mg_ehand[MG_E_ED] != NULL ||
146                          mg_ehand[MG_E_RS] != NULL ||
147                          mg_ehand[MG_E_TS] != NULL ||
148                          mg_ehand[MG_E_SIDES] != NULL)
149 <                uneed |= 1<<MG_E_MATERIAL;
149 >                uneed |= 1L<<MG_E_MATERIAL;
150          for (i = 0; i < MG_NENTITIES; i++)
151 <                if (uneed & 1<<i && mg_ehand[i] == NULL) {
151 >                if (uneed & 1L<<i && mg_ehand[i] == NULL) {
152                          fprintf(stderr, "Missing support for \"%s\" entity\n",
153                                          mg_ename[i]);
154                          exit(1);
155                  }
156                                          /* add support as needed */
157 <        if (ineed & 1<<MG_E_VERTEX && mg_ehand[MG_E_VERTEX] != c_hvertex)
157 >        if (ineed & 1L<<MG_E_VERTEX && mg_ehand[MG_E_VERTEX] != c_hvertex)
158                  e_supp[MG_E_VERTEX] = c_hvertex;
159 <        if (ineed & 1<<MG_E_POINT && mg_ehand[MG_E_POINT] != c_hvertex)
159 >        if (ineed & 1L<<MG_E_POINT && mg_ehand[MG_E_POINT] != c_hvertex)
160                  e_supp[MG_E_POINT] = c_hvertex;
161 <        if (ineed & 1<<MG_E_NORMAL && mg_ehand[MG_E_NORMAL] != c_hvertex)
161 >        if (ineed & 1L<<MG_E_NORMAL && mg_ehand[MG_E_NORMAL] != c_hvertex)
162                  e_supp[MG_E_NORMAL] = c_hvertex;
163 <        if (ineed & 1<<MG_E_COLOR && mg_ehand[MG_E_COLOR] != c_hcolor)
163 >        if (ineed & 1L<<MG_E_COLOR && mg_ehand[MG_E_COLOR] != c_hcolor)
164                  e_supp[MG_E_COLOR] = c_hcolor;
165 <        if (ineed & 1<<MG_E_CXY && mg_ehand[MG_E_CXY] != c_hcolor)
165 >        if (ineed & 1L<<MG_E_CXY && mg_ehand[MG_E_CXY] != c_hcolor)
166                  e_supp[MG_E_CXY] = c_hcolor;
167 <        if (ineed & 1<<MG_E_CSPEC && mg_ehand[MG_E_CSPEC] != c_hcolor)
167 >        if (ineed & 1L<<MG_E_CSPEC && mg_ehand[MG_E_CSPEC] != c_hcolor)
168                  e_supp[MG_E_CSPEC] = c_hcolor;
169 <        if (ineed & 1<<MG_E_CMIX && mg_ehand[MG_E_CMIX] != c_hcolor)
169 >        if (ineed & 1L<<MG_E_CMIX && mg_ehand[MG_E_CMIX] != c_hcolor)
170                  e_supp[MG_E_CMIX] = c_hcolor;
171 <        if (ineed & 1<<MG_E_CCT && mg_ehand[MG_E_CCT] != c_hcolor)
171 >        if (ineed & 1L<<MG_E_CCT && mg_ehand[MG_E_CCT] != c_hcolor)
172                  e_supp[MG_E_CCT] = c_hcolor;
173                                          /* discard remaining entities */
174          for (i = 0; i < MG_NENTITIES; i++)
# Line 203 | Line 206 | char   **av;
206   {
207          int     rv;
208  
209 <        if (en < 0 && (en = mg_entity(av[0])) < 0)
209 >        if (en < 0 && (en = mg_entity(av[0])) < 0) {    /* unknown entity */
210 >                if (mg_uhand != NULL)
211 >                        return((*mg_uhand)(ac, av));
212                  return(MG_EUNK);
213 <        if (e_supp[en] != NULL) {
213 >        }
214 >        if (e_supp[en] != NULL) {                       /* support handler */
215                  if ((rv = (*e_supp[en])(ac, av)) != MG_OK)
216                          return(rv);
217          }
218 <        return((*mg_ehand[en])(ac, av));
218 >        return((*mg_ehand[en])(ac, av));                /* assigned handler */
219   }
220  
221  
# Line 219 | Line 225 | register MG_FCTXT      *ctx;
225   char    *fn;
226   {
227          static int      nfids;
222        int     olen;
228          register char   *cp;
229  
230          ctx->fid = ++nfids;
# Line 232 | Line 237 | char   *fn;
237                  return(MG_OK);
238          }
239                                          /* get name relative to this context */
240 <        if (mg_file != NULL &&
236 <                        (cp = strrchr(mg_file->fname, '/')) != NULL)
237 <                olen = cp - mg_file->fname + 1;
238 <        else
239 <                olen = 0;
240 <        if (olen)
240 >        if (mg_file != NULL && (cp = strrchr(mg_file->fname, '/')) != NULL) {
241                  strcpy(ctx->fname, mg_file->fname);
242 <        strcpy(ctx->fname+olen, fn);
242 >                strcpy(ctx->fname+(cp-mg_file->fname+1), fn);
243 >        } else
244 >                strcpy(ctx->fname, fn);
245          ctx->fp = fopen(ctx->fname, "r");
246          if (ctx->fp == NULL)
247                  return(MG_ENOFILE);
# Line 255 | Line 257 | mg_close()                     /* close input file */
257          register MG_FCTXT       *ctx = mg_file;
258  
259          mg_file = ctx->prev;            /* restore enclosing context */
260 <        if (ctx->fp == stdin)
261 <                return;                 /* don't close standard input */
260 <        fclose(ctx->fp);
260 >        if (ctx->fp != stdin)           /* close file if it's a file */
261 >                fclose(ctx->fp);
262   }
263  
264  
# Line 299 | Line 300 | mg_read()                      /* read next line from file */
300                  if (fgets(mg_file->inpline+len,
301                                  MG_MAXLINE-len, mg_file->fp) == NULL)
302                          return(len);
302                mg_file->lineno++;
303                  len += strlen(mg_file->inpline+len);
304 <                if (len > 1 && mg_file->inpline[len-2] == '\\')
305 <                        mg_file->inpline[--len-1] = ' ';
306 <        } while (mg_file->inpline[len]);
304 >                if (len >= MG_MAXLINE-1)
305 >                        return(len);
306 >                mg_file->lineno++;
307 >        } while (len > 1 && mg_file->inpline[len-2] == '\\');
308  
309          return(len);
310   }
# Line 315 | Line 316 | mg_parse()                     /* parse current input line */
316          char    abuf[MG_MAXLINE];
317          char    *argv[MG_MAXARGC];
318          int     en;
319 <        register char   *cp, **ap;
320 <
321 <        strcpy(cp=abuf, mg_file->inpline);
322 <        ap = argv;                      /* break into words */
319 >        register char   *cp, *cp2, **ap;
320 >                                        /* copy line, removing escape chars */
321 >        cp = abuf; cp2 = mg_file->inpline;
322 >        while ((*cp++ = *cp2++))
323 >                if (cp2[0] == '\n' && cp2[-1] == '\\')
324 >                        cp--;
325 >        cp = abuf; ap = argv;           /* break into words */
326          for ( ; ; ) {
327                  while (isspace(*cp))
328                          *cp++ = '\0';
# Line 344 | Line 348 | char   *fn;
348   {
349          MG_FCTXT        cntxt;
350          int     rval;
351 +        register int    nbr;
352  
353          if ((rval = mg_open(&cntxt, fn)) != MG_OK) {
354                  fprintf(stderr, "%s: %s\n", fn, mg_err[rval]);
355                  return(rval);
356          }
357 <        while (mg_read())               /* parse each line */
357 >        while ((nbr = mg_read()) > 0) { /* parse each line */
358 >                if (nbr >= MG_MAXLINE-1) {
359 >                        fprintf(stderr, "%s: %d: %s\n", cntxt.fname,
360 >                                        cntxt.lineno, mg_err[rval=MG_ELINE]);
361 >                        break;
362 >                }
363                  if ((rval = mg_parse()) != MG_OK) {
364                          fprintf(stderr, "%s: %d: %s:\n%s", cntxt.fname,
365                                          cntxt.lineno, mg_err[rval],
366                                          cntxt.inpline);
367                          break;
368                  }
369 +        }
370          mg_close();
371          return(rval);
372   }
373  
374  
375 + int
376 + mg_defuhand(ac, av)             /* default handler for unknown entities */
377 + int     ac;
378 + char    **av;
379 + {
380 +        if (mg_nunknown++ == 0)         /* report first incident */
381 +                fprintf(stderr, "%s: %d: %s: %s\n", mg_file->fname,
382 +                                mg_file->lineno, mg_err[MG_EUNK], av[0]);
383 +        return(MG_OK);
384 + }
385 +
386 +
387   void
388   mg_clear()                      /* clear parser history */
389   {
390          c_clearall();                   /* clear context tables */
391 <        mg_file = NULL;                 /* reset our context */
391 >        while (mg_file != NULL)         /* reset our file context */
392 >                mg_close();
393   }
394  
395  
# Line 383 | Line 407 | char   **av;
407   }
408  
409  
410 < static int
410 > int
411   e_include(ac, av)               /* include file */
412   int     ac;
413   char    **av;
414   {
415          char    *xfarg[MG_MAXARGC];
416          MG_FCTXT        ictx;
417 <        int     rv;
417 >        XF_SPEC *xf_orig = xf_context;
418 >        register int    rv;
419  
420          if (ac < 2)
421                  return(MG_EARGC);
# Line 403 | Line 428 | char   **av;
428                  for (i = 1; i < ac-1; i++)
429                          xfarg[i] = av[i+1];
430                  xfarg[ac-1] = NULL;
431 <                if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK)
431 >                if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK) {
432 >                        mg_close();
433                          return(rv);
434 +                }
435          }
436 <        while (!feof(mg_file->fp)) {
437 <                while (mg_read())
436 >        do {
437 >                while ((rv = mg_read()) > 0) {
438 >                        if (rv >= MG_MAXLINE-1) {
439 >                                fprintf(stderr, "%s: %d: %s\n", ictx.fname,
440 >                                                ictx.lineno, mg_err[MG_ELINE]);
441 >                                mg_close();
442 >                                return(MG_EINCL);
443 >                        }
444                          if ((rv = mg_parse()) != MG_OK) {
445                                  fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname,
446                                                  ictx.lineno, mg_err[rv],
# Line 415 | Line 448 | char   **av;
448                                  mg_close();
449                                  return(MG_EINCL);
450                          }
451 +                }
452                  if (ac > 2)
453 <                        if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK)
453 >                        if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK) {
454 >                                mg_close();
455                                  return(rv);
456 <        }
456 >                        }
457 >        } while (xf_context != xf_orig);
458          mg_close();
459          return(MG_OK);
460   }
461  
462  
463 + int
464 + e_faceh(ac, av)                 /* replace face+holes with single contour */
465 + int     ac;
466 + char    **av;
467 + {
468 +        char    *newav[MG_MAXARGC];
469 +        int     lastp = 0;
470 +        register int    i, j;
471 +
472 +        newav[0] = mg_ename[MG_E_FACE];
473 +        for (i = 1; i < ac; i++)
474 +                if (av[i][0] == '-') {
475 +                        if (i < 4)
476 +                                return(MG_EARGC);
477 +                        if (i >= ac-1)
478 +                                break;
479 +                        if (!lastp)
480 +                                lastp = i-1;
481 +                        for (j = i+1; j < ac-1 && av[j+1][0] != '-'; j++)
482 +                                ;
483 +                        if (j - i < 3)
484 +                                return(MG_EARGC);
485 +                        newav[i] = av[j];       /* connect hole loop */
486 +                } else
487 +                        newav[i] = av[i];       /* hole or perimeter vertex */
488 +        if (lastp)
489 +                newav[i++] = av[lastp];         /* finish seam to outside */
490 +        newav[i] = NULL;
491 +        return(mg_handle(MG_E_FACE, i, newav));
492 + }
493 +
494 +
495   static void
496   make_axes(u, v, w)              /* compute u and v given w (normalized) */
497   FVECT   u, v, w;
# Line 441 | Line 509 | FVECT  u, v, w;
509   }
510  
511  
512 < static int
512 > int
513   e_sph(ac, av)                   /* expand a sphere into cones */
514   int     ac;
515   char    **av;
# Line 493 | Line 561 | char   **av;
561   }
562  
563  
564 < static int
564 > int
565   e_torus(ac, av)                 /* expand a torus into cones */
566   int     ac;
567   char    **av;
# Line 585 | Line 653 | char   **av;
653   }
654  
655  
656 < static int
656 > int
657   e_cyl(ac, av)                   /* replace a cylinder with equivalent cone */
658   int     ac;
659   char    **av;
# Line 602 | Line 670 | char   **av;
670   }
671  
672  
673 < static int
673 > int
674   e_ring(ac, av)                  /* turn a ring into polygons */
675   int     ac;
676   char    **av;
# Line 700 | Line 768 | char   **av;
768   }
769  
770  
771 < static int
771 > int
772   e_cone(ac, av)                  /* turn a cone into polygons */
773   int     ac;
774   char    **av;
# Line 715 | Line 783 | char   **av;
783          static char     *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]};
784          static char     *n4ent[5] = {mg_ename[MG_E_NORMAL],n4[0],n4[1],n4[2]};
785          static char     *fent[6] = {mg_ename[MG_E_FACE],"_cv1","_cv2","_cv3","_cv4"};
786 +        char    *v1n;
787          register C_VERTEX       *cv1, *cv2;
788          register int    i, j;
789          FVECT   u, v, w;
# Line 730 | Line 799 | char   **av;
799          if ((cv1 = c_getvert(av[1])) == NULL ||
800                          (cv2 = c_getvert(av[3])) == NULL)
801                  return(MG_EUNDEF);
802 +        v1n = av[1];
803          if (!isflt(av[2]) || !isflt(av[4]))
804                  return(MG_ETYPE);
805          rad1 = atof(av[2]);
# Line 748 | Line 818 | char   **av;
818                  cv = cv1;
819                  cv1 = cv2;
820                  cv2 = cv;
821 +                v1n = av[3];
822                  d = rad1;
823                  rad1 = rad2;
824                  rad2 = d;
# Line 781 | Line 852 | char   **av;
852          if ((rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK)
853                  return(rv);
854          if (rad1 == 0.) {               /* triangles */
855 <                v1ent[3] = av[1];
855 >                v1ent[3] = v1n;
856                  if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK)
857                          return(rv);
858                  for (j = 0; j < 3; j++)
# Line 867 | Line 938 | char   **av;
938   }
939  
940  
941 < static int
941 > int
942   e_prism(ac, av)                 /* turn a prism into polygons */
943   int     ac;
944   char    **av;
# Line 979 | Line 1050 | put_cxy()                      /* put out current xy chromaticities */
1050   {
1051          static char     xbuf[24], ybuf[24];
1052          static char     *ccom[4] = {mg_ename[MG_E_CXY], xbuf, ybuf};
982        int     rv;
1053  
1054          sprintf(xbuf, "%.4f", c_ccolor->cx);
1055          sprintf(ybuf, "%.4f", c_ccolor->cy);
1056 <        if ((rv = mg_handle(MG_E_CXY, 3, ccom)) != MG_OK)
987 <                return(rv);
988 <        return(MG_OK);
1056 >        return(mg_handle(MG_E_CXY, 3, ccom));
1057   }
1058  
1059  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines