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 */ |
68 |
> |
e_cspec(); /* color spectra */ |
69 |
> |
|
70 |
> |
int e_include(), /* include file */ |
71 |
> |
e_sph(), /* sphere */ |
72 |
|
e_cyl(), /* cylinder */ |
73 |
|
e_cone(), /* cone */ |
74 |
|
e_prism(), /* prism */ |
210 |
|
{ |
211 |
|
int rv; |
212 |
|
|
213 |
< |
if (en < 0 && (en = mg_entity(av[0])) < 0) |
213 |
> |
if (en < 0 && (en = mg_entity(av[0])) < 0) { /* unknown entity */ |
214 |
> |
if (mg_uhand != NULL) |
215 |
> |
return((*mg_uhand)(ac, av)); |
216 |
|
return(MG_EUNK); |
217 |
< |
if (e_supp[en] != NULL) { |
217 |
> |
} |
218 |
> |
if (e_supp[en] != NULL) { /* support handler */ |
219 |
|
if ((rv = (*e_supp[en])(ac, av)) != MG_OK) |
220 |
|
return(rv); |
221 |
|
} |
222 |
< |
return((*mg_ehand[en])(ac, av)); |
222 |
> |
return((*mg_ehand[en])(ac, av)); /* assigned handler */ |
223 |
|
} |
224 |
|
|
225 |
|
|
261 |
|
register MG_FCTXT *ctx = mg_file; |
262 |
|
|
263 |
|
mg_file = ctx->prev; /* restore enclosing context */ |
264 |
< |
if (ctx->fp == stdin) |
265 |
< |
return; /* don't close standard input */ |
256 |
< |
fclose(ctx->fp); |
264 |
> |
if (ctx->fp != stdin) /* close file if it's a file */ |
265 |
> |
fclose(ctx->fp); |
266 |
|
} |
267 |
|
|
268 |
|
|
304 |
|
if (fgets(mg_file->inpline+len, |
305 |
|
MG_MAXLINE-len, mg_file->fp) == NULL) |
306 |
|
return(len); |
298 |
– |
mg_file->lineno++; |
307 |
|
len += strlen(mg_file->inpline+len); |
308 |
< |
if (len > 1 && mg_file->inpline[len-2] == '\\') |
309 |
< |
mg_file->inpline[--len-1] = ' '; |
310 |
< |
} while (mg_file->inpline[len]); |
308 |
> |
if (len >= MG_MAXLINE-1) |
309 |
> |
return(len); |
310 |
> |
mg_file->lineno++; |
311 |
> |
} while (len > 1 && mg_file->inpline[len-2] == '\\'); |
312 |
|
|
313 |
|
return(len); |
314 |
|
} |
320 |
|
char abuf[MG_MAXLINE]; |
321 |
|
char *argv[MG_MAXARGC]; |
322 |
|
int en; |
323 |
< |
register char *cp, **ap; |
324 |
< |
|
325 |
< |
strcpy(cp=abuf, mg_file->inpline); |
326 |
< |
ap = argv; /* break into words */ |
323 |
> |
register char *cp, *cp2, **ap; |
324 |
> |
/* copy line, removing escape chars */ |
325 |
> |
cp = abuf; cp2 = mg_file->inpline; |
326 |
> |
while ((*cp++ = *cp2++)) |
327 |
> |
if (cp2[0] == '\n' && cp2[-1] == '\\') |
328 |
> |
cp--; |
329 |
> |
cp = abuf; ap = argv; /* break into words */ |
330 |
|
for ( ; ; ) { |
331 |
|
while (isspace(*cp)) |
332 |
|
*cp++ = '\0'; |
352 |
|
{ |
353 |
|
MG_FCTXT cntxt; |
354 |
|
int rval; |
355 |
+ |
register int nbr; |
356 |
|
|
357 |
|
if ((rval = mg_open(&cntxt, fn)) != MG_OK) { |
358 |
|
fprintf(stderr, "%s: %s\n", fn, mg_err[rval]); |
359 |
|
return(rval); |
360 |
|
} |
361 |
< |
while (mg_read()) /* parse each line */ |
361 |
> |
while ((nbr = mg_read()) > 0) { /* parse each line */ |
362 |
> |
if (nbr >= MG_MAXLINE-1) { |
363 |
> |
fprintf(stderr, "%s: %d: %s\n", cntxt.fname, |
364 |
> |
cntxt.lineno, mg_err[rval=MG_ELINE]); |
365 |
> |
break; |
366 |
> |
} |
367 |
|
if ((rval = mg_parse()) != MG_OK) { |
368 |
|
fprintf(stderr, "%s: %d: %s:\n%s", cntxt.fname, |
369 |
|
cntxt.lineno, mg_err[rval], |
370 |
|
cntxt.inpline); |
371 |
|
break; |
372 |
|
} |
373 |
+ |
} |
374 |
|
mg_close(); |
375 |
|
return(rval); |
376 |
|
} |
377 |
|
|
378 |
|
|
379 |
+ |
int |
380 |
+ |
mg_defuhand(ac, av) /* default handler for unknown entities */ |
381 |
+ |
int ac; |
382 |
+ |
char **av; |
383 |
+ |
{ |
384 |
+ |
if (mg_nunknown++ == 0) /* report first incident */ |
385 |
+ |
fprintf(stderr, "%s: %d: %s: %s\n", mg_file->fname, |
386 |
+ |
mg_file->lineno, mg_err[MG_EUNK], av[0]); |
387 |
+ |
return(MG_OK); |
388 |
+ |
} |
389 |
+ |
|
390 |
+ |
|
391 |
|
void |
392 |
|
mg_clear() /* clear parser history */ |
393 |
|
{ |
394 |
|
c_clearall(); /* clear context tables */ |
395 |
< |
mg_file = NULL; /* reset our context */ |
395 |
> |
while (mg_file != NULL) /* reset our file context */ |
396 |
> |
mg_close(); |
397 |
|
} |
398 |
|
|
399 |
|
|
411 |
|
} |
412 |
|
|
413 |
|
|
414 |
< |
static int |
414 |
> |
int |
415 |
|
e_include(ac, av) /* include file */ |
416 |
|
int ac; |
417 |
|
char **av; |
419 |
|
char *xfarg[MG_MAXARGC]; |
420 |
|
MG_FCTXT ictx; |
421 |
|
XF_SPEC *xf_orig = xf_context; |
422 |
< |
int rv; |
422 |
> |
register int rv; |
423 |
|
|
424 |
|
if (ac < 2) |
425 |
|
return(MG_EARGC); |
432 |
|
for (i = 1; i < ac-1; i++) |
433 |
|
xfarg[i] = av[i+1]; |
434 |
|
xfarg[ac-1] = NULL; |
435 |
< |
if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK) |
435 |
> |
if ((rv = mg_handle(MG_E_XF, ac-1, xfarg)) != MG_OK) { |
436 |
> |
mg_close(); |
437 |
|
return(rv); |
438 |
+ |
} |
439 |
|
} |
440 |
|
do { |
441 |
< |
while (mg_read()) |
441 |
> |
while ((rv = mg_read()) > 0) { |
442 |
> |
if (rv >= MG_MAXLINE-1) { |
443 |
> |
fprintf(stderr, "%s: %d: %s\n", ictx.fname, |
444 |
> |
ictx.lineno, mg_err[MG_ELINE]); |
445 |
> |
mg_close(); |
446 |
> |
return(MG_EINCL); |
447 |
> |
} |
448 |
|
if ((rv = mg_parse()) != MG_OK) { |
449 |
|
fprintf(stderr, "%s: %d: %s:\n%s", ictx.fname, |
450 |
|
ictx.lineno, mg_err[rv], |
452 |
|
mg_close(); |
453 |
|
return(MG_EINCL); |
454 |
|
} |
455 |
+ |
} |
456 |
|
if (ac > 2) |
457 |
< |
if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK) |
457 |
> |
if ((rv = mg_handle(MG_E_XF, 1, xfarg)) != MG_OK) { |
458 |
> |
mg_close(); |
459 |
|
return(rv); |
460 |
+ |
} |
461 |
|
} while (xf_context != xf_orig); |
462 |
|
mg_close(); |
463 |
|
return(MG_OK); |
481 |
|
} |
482 |
|
|
483 |
|
|
484 |
< |
static int |
484 |
> |
int |
485 |
|
e_sph(ac, av) /* expand a sphere into cones */ |
486 |
|
int ac; |
487 |
|
char **av; |
533 |
|
} |
534 |
|
|
535 |
|
|
536 |
< |
static int |
536 |
> |
int |
537 |
|
e_torus(ac, av) /* expand a torus into cones */ |
538 |
|
int ac; |
539 |
|
char **av; |
625 |
|
} |
626 |
|
|
627 |
|
|
628 |
< |
static int |
628 |
> |
int |
629 |
|
e_cyl(ac, av) /* replace a cylinder with equivalent cone */ |
630 |
|
int ac; |
631 |
|
char **av; |
642 |
|
} |
643 |
|
|
644 |
|
|
645 |
< |
static int |
645 |
> |
int |
646 |
|
e_ring(ac, av) /* turn a ring into polygons */ |
647 |
|
int ac; |
648 |
|
char **av; |
740 |
|
} |
741 |
|
|
742 |
|
|
743 |
< |
static int |
743 |
> |
int |
744 |
|
e_cone(ac, av) /* turn a cone into polygons */ |
745 |
|
int ac; |
746 |
|
char **av; |
755 |
|
static char *p4ent[5] = {mg_ename[MG_E_POINT],p4[0],p4[1],p4[2]}; |
756 |
|
static char *n4ent[5] = {mg_ename[MG_E_NORMAL],n4[0],n4[1],n4[2]}; |
757 |
|
static char *fent[6] = {mg_ename[MG_E_FACE],"_cv1","_cv2","_cv3","_cv4"}; |
758 |
+ |
char *v1n; |
759 |
|
register C_VERTEX *cv1, *cv2; |
760 |
|
register int i, j; |
761 |
|
FVECT u, v, w; |
771 |
|
if ((cv1 = c_getvert(av[1])) == NULL || |
772 |
|
(cv2 = c_getvert(av[3])) == NULL) |
773 |
|
return(MG_EUNDEF); |
774 |
+ |
v1n = av[1]; |
775 |
|
if (!isflt(av[2]) || !isflt(av[4])) |
776 |
|
return(MG_ETYPE); |
777 |
|
rad1 = atof(av[2]); |
790 |
|
cv = cv1; |
791 |
|
cv1 = cv2; |
792 |
|
cv2 = cv; |
793 |
+ |
v1n = av[3]; |
794 |
|
d = rad1; |
795 |
|
rad1 = rad2; |
796 |
|
rad2 = d; |
824 |
|
if ((rv = mg_handle(MG_E_NORMAL, 4, n3ent)) != MG_OK) |
825 |
|
return(rv); |
826 |
|
if (rad1 == 0.) { /* triangles */ |
827 |
< |
v1ent[3] = av[1]; |
827 |
> |
v1ent[3] = v1n; |
828 |
|
if ((rv = mg_handle(MG_E_VERTEX, 4, v1ent)) != MG_OK) |
829 |
|
return(rv); |
830 |
|
for (j = 0; j < 3; j++) |
910 |
|
} |
911 |
|
|
912 |
|
|
913 |
< |
static int |
913 |
> |
int |
914 |
|
e_prism(ac, av) /* turn a prism into polygons */ |
915 |
|
int ac; |
916 |
|
char **av; |
1022 |
|
{ |
1023 |
|
static char xbuf[24], ybuf[24]; |
1024 |
|
static char *ccom[4] = {mg_ename[MG_E_CXY], xbuf, ybuf}; |
979 |
– |
int rv; |
1025 |
|
|
1026 |
|
sprintf(xbuf, "%.4f", c_ccolor->cx); |
1027 |
|
sprintf(ybuf, "%.4f", c_ccolor->cy); |
1028 |
< |
if ((rv = mg_handle(MG_E_CXY, 3, ccom)) != MG_OK) |
984 |
< |
return(rv); |
985 |
< |
return(MG_OK); |
1028 |
> |
return(mg_handle(MG_E_CXY, 3, ccom)); |
1029 |
|
} |
1030 |
|
|
1031 |
|
|