--- ray/src/gen/xform.c 1989/10/04 13:09:54 1.6 +++ ray/src/gen/xform.c 1990/12/13 11:51:17 1.11 @@ -16,12 +16,8 @@ static char SCCSid[] = "$SunId$ LBL"; #include +#include "otypes.h" -typedef struct { - char *funame; /* function name */ - int (*funp)(); /* pointer to function */ -} FUN; - int xac; /* global xform argument count */ char **xav; /* global xform argument pointer */ int xfa; /* start of xf arguments */ @@ -34,60 +30,11 @@ int expand = 0; /* boolean true to expand commands char *idprefix = NULL; /* prefix for object identifiers */ -extern int o_source(); -extern int o_sphere(); -extern int o_face(); -extern int o_cone(); -extern int o_cylinder(); -extern int o_ring(); -extern int m_glow(); -extern int m_spot(); -extern int m_dielectric(); -extern int m_interface(); -extern int text(); -extern int alias(); -extern int passargs(); -extern int addxform(); +#define ALIAS NUMOTYPE /* put alias at end of array */ -FUN ofun[] = { - { "source", o_source }, - { "sphere", o_sphere }, - { "bubble", o_sphere }, - { "polygon", o_face }, - { "cone", o_cone }, - { "cup", o_cone }, - { "cylinder", o_cylinder }, - { "tube", o_cylinder }, - { "ring", o_ring }, - { "instance", addxform }, - { "alias", alias }, - { "antimatter", passargs }, - { "glow", m_glow }, - { "spotlight", m_spot }, - { "dielectric", m_dielectric }, - { "interface", m_interface }, - { "colortext", text }, - { "brighttext", text }, - { "texfunc", addxform }, - { "texdata", addxform }, - { "colorfunc", addxform }, - { "brightfunc", addxform }, - { "colorpict", addxform }, - { "colordata", addxform }, - { "brightdata", addxform }, - { "mixfunc", addxform }, - { "mixdata", addxform }, - { "mixtext", text }, - { "light", passargs }, - { "illum", passargs }, - { "plastic", passargs }, - { "metal", passargs }, - { "trans", passargs }, - { "glass", passargs }, - { 0 } /* terminator */ -}; +#define NUMTYPES (NUMOTYPE+1) /* total number of object types */ -#define issurface(t) ((t)<=9) +FUN ofun[NUMTYPES] = INIT_OTYPE; /* default types and actions */ typedef struct { short nsargs; /* # of string arguments */ @@ -113,22 +60,31 @@ char *argv[]; FILE *fopen(); FILE *fp; int a; + /* check for array */ + for (a = 1; a < argc; a++) + if (!strcmp(argv[a], "-a")) + return(doarray(argc, argv, a)); - xav = argv; + initotypes(); for (a = 1; a < argc; a++) { if (argv[a][0] == '-') switch (argv[a][1]) { case 'n': + if (argv[a][2] || a+1 >= argc) + break; idprefix = argv[++a]; continue; case 'e': + if (argv[a][2]) + break; expand = 1; continue; } break; } + xav = argv; xfa = a; totscale = 1.0; @@ -139,17 +95,23 @@ char *argv[]; if (reverse = totscale < 0.0) totscale = -totscale; - xac = a; + if (a < argc && argv[a][0] == '-') { + fprintf(stderr, "%s: command line error at '%s'\n", + argv[0], argv[a]); + exit(1); + } - putchar('#'); /* simple header */ + xac = a; + /* simple header */ + putchar('#'); for (a = 0; a < xac; a++) printf(" %s", xav[a]); putchar('\n'); - - if (a == argc) + /* transform input */ + if (xac == argc) xform("standard input", stdin); else - for ( ; a < argc; a++) { + for (a = xac; a < argc; a++) { if ((fp = fopen(argv[a], "r")) == NULL) { fprintf(stderr, "%s: cannot open \"%s\"\n", progname, argv[a]); @@ -159,10 +121,50 @@ char *argv[]; fclose(fp); } - exit(0); + return(0); } +doarray(ac, av, ai) /* make array */ +char **av; +int ac, ai; +{ + char *newav[256], **avp; + char newid[128], repts[32]; + char *oldid = NULL; + int i, err; + + avp = newav+2; + avp[0] = av[0]; + for (i = 1; i < ac; i++) + if (!strcmp(av[i-1], "-n")) { + oldid = av[i]; + avp[i] = newid; + } else + avp[i] = av[i]; + avp[ai] = "-i"; + avp[ai+1] = repts; + avp[i] = NULL; + if (oldid == NULL) { + newav[0] = av[0]; + newav[1] = "-n"; + newav[2] = newid; + avp = newav; + ac += 2; + } + err = 0; + for (i = 0; i < atoi(av[ai+1]); i++) { + if (oldid == NULL) + sprintf(newid, "a%d", i); + else + sprintf(newid, "%s.%d", oldid, i); + sprintf(repts, "%d", i); + err |= main(ac, avp); + } + return(err); +} + + xform(name, fin) /* transform stream by totxform */ char *name; register FILE *fin; @@ -181,17 +183,7 @@ register FILE *fin; } while (c != '\n'); } else if (c == '!') { /* command */ ungetc(c, fin); - if (expand) - xfcomm(name, fin); - else { - putchar('\n'); - while ((c = getc(fin)) != EOF && c != '\n') - putchar(c); - printf(" | %s -e", xav[0]); - for (c = 1; c < xac; c++) - printf(" %s", xav[c]); - putchar('\n'); - } + xfcomm(name, fin); } else { /* object */ ungetc(c, fin); xfobject(name, fin); @@ -200,24 +192,39 @@ register FILE *fin; } -xfcomm(fname, fin) /* expand a command */ +xfcomm(fname, fin) /* transform a command */ FILE *fin; { FILE *popen(); - char *fgets(); + char *fgetline(); FILE *pin; char buf[512]; + int i; - fgets(buf, sizeof(buf), fin); - if (buf[strlen(buf)-1] == '\n') - buf[strlen(buf)-1] = '\0'; - if ((pin = popen(buf+1, "r")) == NULL) { - fprintf(stderr, "%s: (%s): cannot execute \"%s\"\n", - progname, fname, buf); - exit(1); + fgetline(buf, sizeof(buf), fin); + if (expand) { + if (xac > 2) { + if ((pin = popen(buf+1, "r")) == NULL) { + fprintf(stderr, + "%s: (%s): cannot execute \"%s\"\n", + progname, fname, buf); + exit(1); + } + xform(buf, pin); + pclose(pin); + } else { + fflush(stdout); + system(buf+1); + } + } else { + printf("\n%s", buf); + if (xac > 1) { + printf(" | %s -e", xav[0]); + for (i = 1; i < xac; i++) + printf(" %s", xav[i]); + } + putchar('\n'); } - xform(buf, pin); - pclose(pin); } @@ -251,7 +258,7 @@ FILE *fin; } -passargs(fin) /* pass on arguments unchanged */ +o_default(fin) /* pass on arguments unchanged */ FILE *fin; { register int i; @@ -315,7 +322,7 @@ register char *ofname; { register int i; - for (i = 0; ofun[i].funame != NULL; i++) + for (i = 0; i < NUMTYPES; i++) if (!strcmp(ofun[i].funame, ofname)) return(i); @@ -675,4 +682,53 @@ register FUNARGS *fa; if (fa->nfargs) free(fa->farg); free(fa); +} + + +initotypes() /* initialize ofun[] array */ +{ + extern int o_source(); + extern int o_sphere(); + extern int o_face(); + extern int o_cone(); + extern int o_cylinder(); + extern int o_ring(); + extern int m_glow(); + extern int m_spot(); + extern int m_dielectric(); + extern int m_interface(); + extern int text(); + extern int alias(); + extern int passargs(); + extern int addxform(); + register int i; + + if (ofun[OBJ_SOURCE].funp == o_source) + return; /* done already */ + /* alias is additional */ + ofun[ALIAS].funame = ALIASID; + ofun[ALIAS].flags = 0; + /* functions get new transform */ + for (i = 0; i < NUMTYPES; i++) + if (hasfunc(i)) + ofun[i].funp = addxform; + /* special cases */ + ofun[OBJ_SOURCE].funp = o_source; + ofun[OBJ_SPHERE].funp = + ofun[OBJ_BUBBLE].funp = o_sphere; + ofun[OBJ_FACE].funp = o_face; + ofun[OBJ_CONE].funp = + ofun[OBJ_CUP].funp = o_cone; + ofun[OBJ_CYLINDER].funp = + ofun[OBJ_TUBE].funp = o_cylinder; + ofun[OBJ_RING].funp = o_ring; + ofun[OBJ_INSTANCE].funp = addxform; + ofun[MAT_GLOW].funp = m_glow; + ofun[MAT_SPOT].funp = m_spot; + ofun[MAT_DIELECTRIC].funp = m_dielectric; + ofun[MAT_INTERFACE].funp = m_interface; + ofun[PAT_CTEXT].funp = + ofun[PAT_BTEXT].funp = + ofun[MIX_TEXT].funp = text; + ofun[ALIAS].funp = alias; }