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"; |
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; |
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 |
|
|
116 |
|
ineed |= 1L<<MG_E_POINT|1L<<MG_E_NORMAL|1L<<MG_E_VERTEX; |
117 |
|
} else |
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; |
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 |
|
|
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 */ |
256 |
< |
fclose(ctx->fp); |
260 |
> |
if (ctx->fp != stdin) /* close file if it's a file */ |
261 |
> |
fclose(ctx->fp); |
262 |
|
} |
263 |
|
|
264 |
|
|
300 |
|
if (fgets(mg_file->inpline+len, |
301 |
|
MG_MAXLINE-len, mg_file->fp) == NULL) |
302 |
|
return(len); |
298 |
– |
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 |
|
} |
315 |
|
{ |
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 */ |
318 |
> |
register char *cp, *cp2, **ap; |
319 |
> |
/* copy line, removing escape chars */ |
320 |
> |
cp = abuf; cp2 = mg_file->inpline; |
321 |
> |
while ((*cp++ = *cp2++)) |
322 |
> |
if (cp2[0] == '\n' && cp2[-1] == '\\') |
323 |
> |
cp--; |
324 |
> |
cp = abuf; ap = argv; /* break into words */ |
325 |
|
for ( ; ; ) { |
326 |
|
while (isspace(*cp)) |
327 |
|
*cp++ = '\0'; |
347 |
|
{ |
348 |
|
MG_FCTXT cntxt; |
349 |
|
int rval; |
350 |
+ |
register int nbr; |
351 |
|
|
352 |
|
if ((rval = mg_open(&cntxt, fn)) != MG_OK) { |
353 |
|
fprintf(stderr, "%s: %s\n", fn, mg_err[rval]); |
354 |
|
return(rval); |
355 |
|
} |
356 |
< |
while (mg_read()) /* parse each line */ |
356 |
> |
while ((nbr = mg_read()) > 0) { /* parse each line */ |
357 |
> |
if (nbr >= MG_MAXLINE-1) { |
358 |
> |
fprintf(stderr, "%s: %d: %s\n", cntxt.fname, |
359 |
> |
cntxt.lineno, mg_err[rval=MG_ELINE]); |
360 |
> |
break; |
361 |
> |
} |
362 |
|
if ((rval = mg_parse()) != MG_OK) { |
363 |
|
fprintf(stderr, "%s: %d: %s:\n%s", cntxt.fname, |
364 |
|
cntxt.lineno, mg_err[rval], |
365 |
|
cntxt.inpline); |
366 |
|
break; |
367 |
|
} |
368 |
+ |
} |
369 |
|
mg_close(); |
370 |
|
return(rval); |
371 |
|
} |
372 |
|
|
373 |
|
|
374 |
+ |
int |
375 |
+ |
mg_defuhand(ac, av) /* default handler for unknown entities */ |
376 |
+ |
int ac; |
377 |
+ |
char **av; |
378 |
+ |
{ |
379 |
+ |
if (mg_nunknown++ == 0) /* report first incident */ |
380 |
+ |
fprintf(stderr, "%s: %d: %s: %s\n", mg_file->fname, |
381 |
+ |
mg_file->lineno, mg_err[MG_EUNK], av[0]); |
382 |
+ |
return(MG_OK); |
383 |
+ |
} |
384 |
+ |
|
385 |
+ |
|
386 |
|
void |
387 |
|
mg_clear() /* clear parser history */ |
388 |
|
{ |
389 |
|
c_clearall(); /* clear context tables */ |
390 |
< |
mg_file = NULL; /* reset our context */ |
390 |
> |
while (mg_file != NULL) /* reset our file context */ |
391 |
> |
mg_close(); |
392 |
|
} |
393 |
|
|
394 |
|
|
406 |
|
} |
407 |
|
|
408 |
|
|
409 |
< |
static int |
409 |
> |
int |
410 |
|
e_include(ac, av) /* include file */ |
411 |
|
int ac; |
412 |
|
char **av; |
414 |
|
char *xfarg[MG_MAXARGC]; |
415 |
|
MG_FCTXT ictx; |
416 |
|
XF_SPEC *xf_orig = xf_context; |
417 |
< |
int rv; |
417 |
> |
register int rv; |
418 |
|
|
419 |
|
if (ac < 2) |
420 |
|
return(MG_EARGC); |
427 |
|
for (i = 1; i < ac-1; i++) |
428 |
|
xfarg[i] = av[i+1]; |
429 |
|
xfarg[ac-1] = NULL; |
430 |
< |
if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK) |
430 |
> |
if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK) { |
431 |
> |
mg_close(); |
432 |
|
return(rv); |
433 |
+ |
} |
434 |
|
} |
435 |
|
do { |
436 |
< |
while (mg_read()) |
436 |
> |
while ((rv = mg_read()) > 0) { |
437 |
> |
if (rv >= MG_MAXLINE-1) { |
438 |
> |
fprintf(stderr, "%s: %d: %s\n", ictx.fname, |
439 |
> |
ictx.lineno, mg_err[MG_ELINE]); |
440 |
> |
mg_close(); |
441 |
> |
return(MG_EINCL); |
442 |
> |
} |
443 |
|
if ((rv = mg_parse()) != MG_OK) { |
444 |
|
fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname, |
445 |
|
ictx.lineno, mg_err[rv], |
447 |
|
mg_close(); |
448 |
|
return(MG_EINCL); |
449 |
|
} |
450 |
+ |
} |
451 |
|
if (ac > 2) |
452 |
< |
if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK) |
452 |
> |
if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK) { |
453 |
> |
mg_close(); |
454 |
|
return(rv); |
455 |
+ |
} |
456 |
|
} while (xf_context != xf_orig); |
457 |
|
mg_close(); |
458 |
|
return(MG_OK); |
459 |
|
} |
460 |
|
|
461 |
|
|
462 |
+ |
int |
463 |
+ |
e_faceh(ac, av) /* replace face+holes with single contour */ |
464 |
+ |
int ac; |
465 |
+ |
char **av; |
466 |
+ |
{ |
467 |
+ |
char *newav[MG_MAXARGC]; |
468 |
+ |
int lastp = 0; |
469 |
+ |
register int i, j; |
470 |
+ |
|
471 |
+ |
newav[0] = mg_ename[MG_E_FACE]; |
472 |
+ |
for (i = 1; i < ac; i++) |
473 |
+ |
if (av[i][0] == '-') { |
474 |
+ |
if (i < 4) |
475 |
+ |
return(MG_EARGC); |
476 |
+ |
if (i >= ac-1) |
477 |
+ |
break; |
478 |
+ |
if (!lastp) |
479 |
+ |
lastp = i-1; |
480 |
+ |
for (j = i+1; j < ac-1 && av[j+1][0] != '-'; j++) |
481 |
+ |
; |
482 |
+ |
if (j - i < 3) |
483 |
+ |
return(MG_EARGC); |
484 |
+ |
newav[i] = av[j]; /* connect hole loop */ |
485 |
+ |
} else |
486 |
+ |
newav[i] = av[i]; /* hole or perimeter vertex */ |
487 |
+ |
if (lastp) |
488 |
+ |
newav[i++] = av[lastp]; /* finish seam to outside */ |
489 |
+ |
newav[i] = NULL; |
490 |
+ |
return(mg_handle(MG_E_FACE, i, newav)); |
491 |
+ |
} |
492 |
+ |
|
493 |
+ |
|
494 |
|
static void |
495 |
|
make_axes(u, v, w) /* compute u and v given w (normalized) */ |
496 |
|
FVECT u, v, w; |
508 |
|
} |
509 |
|
|
510 |
|
|
511 |
< |
static int |
511 |
> |
int |
512 |
|
e_sph(ac, av) /* expand a sphere into cones */ |
513 |
|
int ac; |
514 |
|
char **av; |
560 |
|
} |
561 |
|
|
562 |
|
|
563 |
< |
static int |
563 |
> |
int |
564 |
|
e_torus(ac, av) /* expand a torus into cones */ |
565 |
|
int ac; |
566 |
|
char **av; |
652 |
|
} |
653 |
|
|
654 |
|
|
655 |
< |
static int |
655 |
> |
int |
656 |
|
e_cyl(ac, av) /* replace a cylinder with equivalent cone */ |
657 |
|
int ac; |
658 |
|
char **av; |
669 |
|
} |
670 |
|
|
671 |
|
|
672 |
< |
static int |
672 |
> |
int |
673 |
|
e_ring(ac, av) /* turn a ring into polygons */ |
674 |
|
int ac; |
675 |
|
char **av; |
767 |
|
} |
768 |
|
|
769 |
|
|
770 |
< |
static int |
770 |
> |
int |
771 |
|
e_cone(ac, av) /* turn a cone into polygons */ |
772 |
|
int ac; |
773 |
|
char **av; |
782 |
|
static char *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]}; |
783 |
|
static char *n4ent[5] = {mg_ename[MG_E_NORMAL],n4[0],n4[1],n4[2]}; |
784 |
|
static char *fent[6] = {mg_ename[MG_E_FACE],"_cv1","_cv2","_cv3","_cv4"}; |
785 |
+ |
char *v1n; |
786 |
|
register C_VERTEX *cv1, *cv2; |
787 |
|
register int i, j; |
788 |
|
FVECT u, v, w; |
798 |
|
if ((cv1 = c_getvert(av[1])) == NULL || |
799 |
|
(cv2 = c_getvert(av[3])) == NULL) |
800 |
|
return(MG_EUNDEF); |
801 |
+ |
v1n = av[1]; |
802 |
|
if (!isflt(av[2]) || !isflt(av[4])) |
803 |
|
return(MG_ETYPE); |
804 |
|
rad1 = atof(av[2]); |
809 |
|
if (rad2 == 0.) |
810 |
|
return(MG_EILL); |
811 |
|
} else if (rad2 != 0.) { |
812 |
< |
if (rad1 < 0. ^ rad2 < 0.) |
812 |
> |
if ((rad1 < 0.) ^ (rad2 < 0.)) |
813 |
|
return(MG_EILL); |
814 |
|
} else { /* swap */ |
815 |
|
C_VERTEX *cv; |
817 |
|
cv = cv1; |
818 |
|
cv1 = cv2; |
819 |
|
cv2 = cv; |
820 |
+ |
v1n = av[3]; |
821 |
|
d = rad1; |
822 |
|
rad1 = rad2; |
823 |
|
rad2 = d; |
851 |
|
if ((rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK) |
852 |
|
return(rv); |
853 |
|
if (rad1 == 0.) { /* triangles */ |
854 |
< |
v1ent[3] = av[1]; |
854 |
> |
v1ent[3] = v1n; |
855 |
|
if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
856 |
|
return(rv); |
857 |
|
for (j = 0; j < 3; j++) |
937 |
|
} |
938 |
|
|
939 |
|
|
940 |
< |
static int |
940 |
> |
int |
941 |
|
e_prism(ac, av) /* turn a prism into polygons */ |
942 |
|
int ac; |
943 |
|
char **av; |
1049 |
|
{ |
1050 |
|
static char xbuf[24], ybuf[24]; |
1051 |
|
static char *ccom[4] = {mg_ename[MG_E_CXY], xbuf, ybuf}; |
979 |
– |
int rv; |
1052 |
|
|
1053 |
|
sprintf(xbuf, "%.4f", c_ccolor->cx); |
1054 |
|
sprintf(ybuf, "%.4f", c_ccolor->cy); |
1055 |
< |
if ((rv = mg_handle(MG_E_CXY, 3, ccom)) != MG_OK) |
984 |
< |
return(rv); |
985 |
< |
return(MG_OK); |
1055 |
> |
return(mg_handle(MG_E_CXY, 3, ccom)); |
1056 |
|
} |
1057 |
|
|
1058 |
|
|