1 |
< |
/* Copyright (c) 1995 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"; |
10 |
|
|
11 |
|
#include <stdio.h> |
12 |
|
#include <math.h> |
13 |
+ |
#include <stdlib.h> |
14 |
|
#include <ctype.h> |
15 |
|
#include <string.h> |
16 |
|
#include "parser.h" |
28 |
|
|
29 |
|
int (*mg_ehand[MG_NENTITIES])(); |
30 |
|
|
31 |
+ |
/* Handler routine for unknown entities */ |
32 |
+ |
|
33 |
+ |
int (*mg_uhand)() = mg_defuhand; |
34 |
+ |
|
35 |
+ |
unsigned mg_nunknown; /* count of unknown entities */ |
36 |
+ |
|
37 |
|
/* error messages */ |
38 |
|
|
39 |
|
char *mg_err[MG_NERRS] = MG_ERRLIST; |
64 |
|
|
65 |
|
static int e_any_toss(), /* discard unneeded entity */ |
66 |
|
e_ies(), /* IES luminaire file */ |
60 |
– |
e_include(), /* include file */ |
61 |
– |
e_sph(), /* sphere */ |
67 |
|
e_cct(), /* color temperature */ |
68 |
|
e_cmix(), /* color mixtures */ |
69 |
< |
e_cspec(), /* color spectra */ |
65 |
< |
e_cyl(), /* cylinder */ |
66 |
< |
e_cone(), /* cone */ |
67 |
< |
e_prism(), /* prism */ |
68 |
< |
e_ring(), /* ring */ |
69 |
< |
e_torus(); /* torus */ |
69 |
> |
e_cspec(); /* color spectra */ |
70 |
|
|
71 |
|
/* alternate handler support functions */ |
72 |
|
|
117 |
|
ineed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX; |
118 |
|
} else |
119 |
|
uneed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX|1L<<MG_E_XF; |
120 |
+ |
if (mg_ehand[MG_E_FACE] == NULL) |
121 |
+ |
mg_ehand[MG_E_FACE] = mg_ehand[MG_E_FACEH]; |
122 |
+ |
else if (mg_ehand[MG_E_FACEH] == NULL) |
123 |
+ |
mg_ehand[MG_E_FACEH] = e_faceh; |
124 |
|
if (mg_ehand[MG_E_COLOR] != NULL) { |
125 |
|
if (mg_ehand[MG_E_CMIX] == NULL) { |
126 |
|
mg_ehand[MG_E_CMIX] = e_cmix; |
207 |
|
{ |
208 |
|
int rv; |
209 |
|
|
210 |
< |
if (en < 0 && (en = mg_entity(av[0])) < 0) |
210 |
> |
if (en < 0 && (en = mg_entity(av[0])) < 0) { /* unknown entity */ |
211 |
> |
if (mg_uhand != NULL) |
212 |
> |
return((*mg_uhand)(ac, av)); |
213 |
|
return(MG_EUNK); |
214 |
< |
if (e_supp[en] != NULL) { |
214 |
> |
} |
215 |
> |
if (e_supp[en] != NULL) { /* support handler */ |
216 |
|
if ((rv = (*e_supp[en])(ac, av)) != MG_OK) |
217 |
|
return(rv); |
218 |
|
} |
219 |
< |
return((*mg_ehand[en])(ac, av)); |
219 |
> |
return((*mg_ehand[en])(ac, av)); /* assigned handler */ |
220 |
|
} |
221 |
|
|
222 |
|
|
258 |
|
register MG_FCTXT *ctx = mg_file; |
259 |
|
|
260 |
|
mg_file = ctx->prev; /* restore enclosing context */ |
261 |
< |
if (ctx->fp == stdin) |
262 |
< |
return; /* don't close standard input */ |
256 |
< |
fclose(ctx->fp); |
261 |
> |
if (ctx->fp != stdin) /* close file if it's a file */ |
262 |
> |
fclose(ctx->fp); |
263 |
|
} |
264 |
|
|
265 |
|
|
267 |
|
mg_fgetpos(pos) /* get current position in input file */ |
268 |
|
register MG_FPOS *pos; |
269 |
|
{ |
264 |
– |
extern long ftell(); |
265 |
– |
|
270 |
|
pos->fid = mg_file->fid; |
271 |
|
pos->lineno = mg_file->lineno; |
272 |
|
pos->offset = ftell(mg_file->fp); |
299 |
|
if (fgets(mg_file->inpline+len, |
300 |
|
MG_MAXLINE-len, mg_file->fp) == NULL) |
301 |
|
return(len); |
298 |
– |
mg_file->lineno++; |
302 |
|
len += strlen(mg_file->inpline+len); |
303 |
< |
if (len > 1 && mg_file->inpline[len-2] == '\\') |
304 |
< |
mg_file->inpline[--len-1] = ' '; |
305 |
< |
} while (mg_file->inpline[len]); |
303 |
> |
if (len >= MG_MAXLINE-1) |
304 |
> |
return(len); |
305 |
> |
mg_file->lineno++; |
306 |
> |
} while (len > 1 && mg_file->inpline[len-2] == '\\'); |
307 |
|
|
308 |
|
return(len); |
309 |
|
} |
314 |
|
{ |
315 |
|
char abuf[MG_MAXLINE]; |
316 |
|
char *argv[MG_MAXARGC]; |
317 |
< |
int en; |
318 |
< |
register char *cp, **ap; |
319 |
< |
|
320 |
< |
strcpy(cp=abuf, mg_file->inpline); |
321 |
< |
ap = argv; /* break into words */ |
317 |
> |
register char *cp, *cp2, **ap; |
318 |
> |
/* copy line, removing escape chars */ |
319 |
> |
cp = abuf; cp2 = mg_file->inpline; |
320 |
> |
while ((*cp++ = *cp2++)) |
321 |
> |
if (cp2[0] == '\n' && cp2[-1] == '\\') |
322 |
> |
cp--; |
323 |
> |
cp = abuf; ap = argv; /* break into words */ |
324 |
|
for ( ; ; ) { |
325 |
|
while (isspace(*cp)) |
326 |
|
*cp++ = '\0'; |
346 |
|
{ |
347 |
|
MG_FCTXT cntxt; |
348 |
|
int rval; |
349 |
+ |
register int nbr; |
350 |
|
|
351 |
|
if ((rval = mg_open(&cntxt, fn)) != MG_OK) { |
352 |
|
fprintf(stderr, "%s: %s\n", fn, mg_err[rval]); |
353 |
|
return(rval); |
354 |
|
} |
355 |
< |
while (mg_read()) /* parse each line */ |
355 |
> |
while ((nbr = mg_read()) > 0) { /* parse each line */ |
356 |
> |
if (nbr >= MG_MAXLINE-1) { |
357 |
> |
fprintf(stderr, "%s: %d: %s\n", cntxt.fname, |
358 |
> |
cntxt.lineno, mg_err[rval=MG_ELINE]); |
359 |
> |
break; |
360 |
> |
} |
361 |
|
if ((rval = mg_parse()) != MG_OK) { |
362 |
|
fprintf(stderr, "%s: %d: %s:\n%s", cntxt.fname, |
363 |
|
cntxt.lineno, mg_err[rval], |
364 |
|
cntxt.inpline); |
365 |
|
break; |
366 |
|
} |
367 |
+ |
} |
368 |
|
mg_close(); |
369 |
|
return(rval); |
370 |
|
} |
371 |
|
|
372 |
|
|
373 |
+ |
int |
374 |
+ |
mg_defuhand(ac, av) /* default handler for unknown entities */ |
375 |
+ |
int ac; |
376 |
+ |
char **av; |
377 |
+ |
{ |
378 |
+ |
if (mg_nunknown++ == 0) /* report first incident */ |
379 |
+ |
fprintf(stderr, "%s: %d: %s: %s\n", mg_file->fname, |
380 |
+ |
mg_file->lineno, mg_err[MG_EUNK], av[0]); |
381 |
+ |
return(MG_OK); |
382 |
+ |
} |
383 |
+ |
|
384 |
+ |
|
385 |
|
void |
386 |
|
mg_clear() /* clear parser history */ |
387 |
|
{ |
388 |
|
c_clearall(); /* clear context tables */ |
389 |
< |
mg_file = NULL; /* reset our context */ |
389 |
> |
while (mg_file != NULL) /* reset our file context */ |
390 |
> |
mg_close(); |
391 |
|
} |
392 |
|
|
393 |
|
|
405 |
|
} |
406 |
|
|
407 |
|
|
408 |
< |
static int |
408 |
> |
int |
409 |
|
e_include(ac, av) /* include file */ |
410 |
|
int ac; |
411 |
|
char **av; |
413 |
|
char *xfarg[MG_MAXARGC]; |
414 |
|
MG_FCTXT ictx; |
415 |
|
XF_SPEC *xf_orig = xf_context; |
416 |
< |
int rv; |
416 |
> |
register int rv; |
417 |
|
|
418 |
|
if (ac < 2) |
419 |
|
return(MG_EARGC); |
426 |
|
for (i = 1; i < ac-1; i++) |
427 |
|
xfarg[i] = av[i+1]; |
428 |
|
xfarg[ac-1] = NULL; |
429 |
< |
if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK) |
429 |
> |
if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK) { |
430 |
> |
mg_close(); |
431 |
|
return(rv); |
432 |
+ |
} |
433 |
|
} |
434 |
|
do { |
435 |
< |
while (mg_read()) |
435 |
> |
while ((rv = mg_read()) > 0) { |
436 |
> |
if (rv >= MG_MAXLINE-1) { |
437 |
> |
fprintf(stderr, "%s: %d: %s\n", ictx.fname, |
438 |
> |
ictx.lineno, mg_err[MG_ELINE]); |
439 |
> |
mg_close(); |
440 |
> |
return(MG_EINCL); |
441 |
> |
} |
442 |
|
if ((rv = mg_parse()) != MG_OK) { |
443 |
|
fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname, |
444 |
|
ictx.lineno, mg_err[rv], |
446 |
|
mg_close(); |
447 |
|
return(MG_EINCL); |
448 |
|
} |
449 |
+ |
} |
450 |
|
if (ac > 2) |
451 |
< |
if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK) |
451 |
> |
if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK) { |
452 |
> |
mg_close(); |
453 |
|
return(rv); |
454 |
+ |
} |
455 |
|
} while (xf_context != xf_orig); |
456 |
|
mg_close(); |
457 |
|
return(MG_OK); |
458 |
|
} |
459 |
|
|
460 |
|
|
461 |
+ |
int |
462 |
+ |
e_faceh(ac, av) /* replace face+holes with single contour */ |
463 |
+ |
int ac; |
464 |
+ |
char **av; |
465 |
+ |
{ |
466 |
+ |
char *newav[MG_MAXARGC]; |
467 |
+ |
int lastp = 0; |
468 |
+ |
register int i, j; |
469 |
+ |
|
470 |
+ |
newav[0] = mg_ename[MG_E_FACE]; |
471 |
+ |
for (i = 1; i < ac; i++) |
472 |
+ |
if (av[i][0] == '-') { |
473 |
+ |
if (i < 4) |
474 |
+ |
return(MG_EARGC); |
475 |
+ |
if (i >= ac-1) |
476 |
+ |
break; |
477 |
+ |
if (!lastp) |
478 |
+ |
lastp = i-1; |
479 |
+ |
for (j = i+1; j < ac-1 && av[j+1][0] != '-'; j++) |
480 |
+ |
; |
481 |
+ |
if (j - i < 3) |
482 |
+ |
return(MG_EARGC); |
483 |
+ |
newav[i] = av[j]; /* connect hole loop */ |
484 |
+ |
} else |
485 |
+ |
newav[i] = av[i]; /* hole or perimeter vertex */ |
486 |
+ |
if (lastp) |
487 |
+ |
newav[i++] = av[lastp]; /* finish seam to outside */ |
488 |
+ |
newav[i] = NULL; |
489 |
+ |
return(mg_handle(MG_E_FACE, i, newav)); |
490 |
+ |
} |
491 |
+ |
|
492 |
+ |
|
493 |
|
static void |
494 |
|
make_axes(u, v, w) /* compute u and v given w (normalized) */ |
495 |
|
FVECT u, v, w; |
507 |
|
} |
508 |
|
|
509 |
|
|
510 |
< |
static int |
510 |
> |
int |
511 |
|
e_sph(ac, av) /* expand a sphere into cones */ |
512 |
|
int ac; |
513 |
|
char **av; |
559 |
|
} |
560 |
|
|
561 |
|
|
562 |
< |
static int |
562 |
> |
int |
563 |
|
e_torus(ac, av) /* expand a torus into cones */ |
564 |
|
int ac; |
565 |
|
char **av; |
651 |
|
} |
652 |
|
|
653 |
|
|
654 |
< |
static int |
654 |
> |
int |
655 |
|
e_cyl(ac, av) /* replace a cylinder with equivalent cone */ |
656 |
|
int ac; |
657 |
|
char **av; |
668 |
|
} |
669 |
|
|
670 |
|
|
671 |
< |
static int |
671 |
> |
int |
672 |
|
e_ring(ac, av) /* turn a ring into polygons */ |
673 |
|
int ac; |
674 |
|
char **av; |
766 |
|
} |
767 |
|
|
768 |
|
|
769 |
< |
static int |
769 |
> |
int |
770 |
|
e_cone(ac, av) /* turn a cone into polygons */ |
771 |
|
int ac; |
772 |
|
char **av; |
781 |
|
static char *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]}; |
782 |
|
static char *n4ent[5] = {mg_ename[MG_E_NORMAL],n4[0],n4[1],n4[2]}; |
783 |
|
static char *fent[6] = {mg_ename[MG_E_FACE],"_cv1","_cv2","_cv3","_cv4"}; |
784 |
+ |
char *v1n; |
785 |
|
register C_VERTEX *cv1, *cv2; |
786 |
|
register int i, j; |
787 |
|
FVECT u, v, w; |
797 |
|
if ((cv1 = c_getvert(av[1])) == NULL || |
798 |
|
(cv2 = c_getvert(av[3])) == NULL) |
799 |
|
return(MG_EUNDEF); |
800 |
+ |
v1n = av[1]; |
801 |
|
if (!isflt(av[2]) || !isflt(av[4])) |
802 |
|
return(MG_ETYPE); |
803 |
|
rad1 = atof(av[2]); |
808 |
|
if (rad2 == 0.) |
809 |
|
return(MG_EILL); |
810 |
|
} else if (rad2 != 0.) { |
811 |
< |
if (rad1 < 0. ^ rad2 < 0.) |
811 |
> |
if ((rad1 < 0.) ^ (rad2 < 0.)) |
812 |
|
return(MG_EILL); |
813 |
|
} else { /* swap */ |
814 |
|
C_VERTEX *cv; |
816 |
|
cv = cv1; |
817 |
|
cv1 = cv2; |
818 |
|
cv2 = cv; |
819 |
+ |
v1n = av[3]; |
820 |
|
d = rad1; |
821 |
|
rad1 = rad2; |
822 |
|
rad2 = d; |
850 |
|
if ((rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK) |
851 |
|
return(rv); |
852 |
|
if (rad1 == 0.) { /* triangles */ |
853 |
< |
v1ent[3] = av[1]; |
853 |
> |
v1ent[3] = v1n; |
854 |
|
if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
855 |
|
return(rv); |
856 |
|
for (j = 0; j < 3; j++) |
936 |
|
} |
937 |
|
|
938 |
|
|
939 |
< |
static int |
939 |
> |
int |
940 |
|
e_prism(ac, av) /* turn a prism into polygons */ |
941 |
|
int ac; |
942 |
|
char **av; |
1048 |
|
{ |
1049 |
|
static char xbuf[24], ybuf[24]; |
1050 |
|
static char *ccom[4] = {mg_ename[MG_E_CXY], xbuf, ybuf}; |
979 |
– |
int rv; |
1051 |
|
|
1052 |
|
sprintf(xbuf, "%.4f", c_ccolor->cx); |
1053 |
|
sprintf(ybuf, "%.4f", c_ccolor->cy); |
1054 |
< |
if ((rv = mg_handle(MG_E_CXY, 3, ccom)) != MG_OK) |
984 |
< |
return(rv); |
985 |
< |
return(MG_OK); |
1054 |
> |
return(mg_handle(MG_E_CXY, 3, ccom)); |
1055 |
|
} |
1056 |
|
|
1057 |
|
|