--- ray/src/gen/xform.c 1999/06/04 16:00:27 2.19 +++ ray/src/gen/xform.c 2003/06/27 22:27:45 2.27 @@ -1,9 +1,6 @@ -/* Copyright (c) 1996 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: xform.c,v 2.27 2003/06/27 22:27:45 greg Exp $"; #endif - /* * xform.c - program to transform object files. * Transformations must preserve aspect ratio. @@ -12,14 +9,12 @@ static char SCCSid[] = "$SunId$ LBL"; * 11/6/86 Finally added error checking! */ -#include "standard.h" - -#include "paths.h" - #include +#include "standard.h" +#include "platform.h" +#include "paths.h" #include "object.h" - #include "otypes.h" int xac; /* global xform argument count */ @@ -29,28 +24,22 @@ int xfa; /* start of xf arguments */ XF tot; /* total transformation */ int reverse; /* boolean true if scene mirrored */ -int invert = 0; /* boolean true to invert surfaces */ +int invert; /* boolean true to invert surfaces */ -int expand = 1; /* boolean true to expand commands */ +int expand; /* boolean true to expand commands */ -char *newmod = NULL; /* new modifier for surfaces */ +char *newmod; /* new modifier for surfaces */ -char *idprefix = NULL; /* prefix for object identifiers */ +char *idprefix; /* prefix for object identifiers */ -#define ALIAS NUMOTYPE /* put alias at end of array */ +FUN ofun[NUMOTYPE] = INIT_OTYPE; /* default types and actions */ -#define NUMTYPES (NUMOTYPE+1) /* total number of object types */ - -FUN ofun[NUMTYPES] = INIT_OTYPE; /* default types and actions */ - short tinvers[NUMOTYPE]; /* inverse types for surfaces */ int nrept = 1; /* number of array repetitions */ int stdinused = 0; /* stdin has been used by -f option? */ -extern char *malloc(), *fgets(), *fgetword(); - char mainfn[MAXPATH]; /* main file name */ FILE *mainfp = NULL; /* main file pointer */ @@ -61,6 +50,7 @@ main(argc, argv) /* get transform options and transfo int argc; char *argv[]; { + int mal_prefix = 0; char *fname; int a; /* check for argument list file */ @@ -72,20 +62,40 @@ char *argv[]; if (!strcmp(argv[a], "-a")) return(doarray(argc, argv, a)); - initotypes(); + initotypes(); /* initialize */ + invert = 0; + expand = 1; + newmod = NULL; + idprefix = NULL; for (a = 1; a < argc; a++) { if (argv[a][0] == '-') switch (argv[a][1]) { case 'm': - if (argv[a][2] || a+1 >= argc) + if (argv[a][2] | a+1 >= argc) break; - newmod = argv[++a]; + a++; + if (newmod == NULL) + newmod = argv[a]; continue; case 'n': - if (argv[a][2] || a+1 >= argc) + if (argv[a][2] | a+1 >= argc) break; - idprefix = argv[++a]; + a++; + if (idprefix == NULL) + idprefix = argv[a]; + else { + register char *newp; + newp = (char *)malloc(strlen(idprefix)+ + strlen(argv[a])+2); + if (newp == NULL) + exit(2); + sprintf(newp, "%s.%s", + idprefix, argv[a]); + if (mal_prefix++) + free((void *)idprefix); + idprefix = newp; + } continue; case 'c': if (argv[a][2]) @@ -100,7 +110,7 @@ char *argv[]; case 'I': if (argv[a][2]) break; - invert = 1; + invert = !invert; continue; } break; @@ -143,6 +153,8 @@ char *argv[]; xform(mainfn, mainfp); } + if (mal_prefix) + free((void *)idprefix); return(0); } @@ -151,8 +163,9 @@ doargf(ac, av, fi) /* take argument list from file * char **av; int ac, fi; { + int inquote; char *newav[256], **avp; - char argbuf[2048]; + char argbuf[1024]; char newid[128]; char *oldid; register char *cp; @@ -165,7 +178,8 @@ int ac, fi; } if (av[fi+1][0] == '-' && av[fi+1][1] == '\0') { if (stdinused++) { - fprintf(stderr, "%s: cannot use stdin more than once\n", + fprintf(stderr, + "%s: cannot use stdin more than once\n", av[0]); exit(1); } @@ -173,13 +187,14 @@ int ac, fi; n = 100; /* we just don't know! */ } else { if ((argfp = fopen(av[fi+1], "r")) == NULL) { - fprintf(stderr, "%s: cannot open argument file \"%s\"\n", + fprintf(stderr, + "%s: cannot open argument file \"%s\"\n", av[0], av[fi+1]); exit(1); } n = 0; /* count number of lines in file */ - while (fgets(argbuf,sizeof(argbuf),argfp) != NULL) - n += argbuf[0] != '\n' & argbuf[0] != '#'; + while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL) + n += argbuf[0] && argbuf[0] != '#'; if (!n) { fprintf(stderr, "%s: empty argument file \"%s\"\n", av[0], av[fi+1]); @@ -189,8 +204,8 @@ int ac, fi; rewind(argfp); } err = 0; k = 0; /* read each arg list and call main */ - while (fgets(argbuf,sizeof(argbuf),argfp) != NULL) { - if (argbuf[0] == '\n' | argbuf[0] == '#') + while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL) { + if (!argbuf[0] || argbuf[0] == '#') continue; avp = newav+2; avp[0] = av[0]; @@ -200,14 +215,29 @@ int ac, fi; cp = argbuf; /* parse new words */ if (*cp == '!') cp++; if (!strncmp(cp, "xform ", 6)) cp += 6; + inquote = 0; for ( ; ; ) { + skipspaces: while (isspace(*cp)) /* nullify spaces */ *cp++ = '\0'; + if ((*cp == '"' | *cp == '\'')) + inquote = *cp++; if (!*cp) /* all done? */ break; + if (cp[0] == '\\' && cp[1]) + if (*++cp == '\n') + goto skipspaces; avp[newac++] = cp; /* add argument to list */ - while (*++cp && !isspace(*cp)) - ; + if (inquote) { + while (*++cp) + if (*cp == inquote) { + *cp++ = '\0'; + break; + } + } else { + while (*++cp && !isspace(*cp)) + ; + } } for (i = fi+2; i < ac; i++) avp[newac++] = av[i]; @@ -320,8 +350,6 @@ xfcomm(fname, fin) /* transform a command */ char *fname; FILE *fin; { - extern FILE *popen(); - extern char *fgetline(); FILE *pin; char buf[512]; int i; @@ -338,9 +366,10 @@ FILE *fin; } else { printf("\n%s", buf); if (xac > 1) { - printf(" | %s", xav[0]); + printf(" | %s -e", xav[0]); /* expand next time */ for (i = 1; i < xac; i++) - printf(" %s", xav[i]); + if (i >= xfa || strcmp(xav[i], "-c")) + printf(" %s", xav[i]); } putchar('\n'); } @@ -351,7 +380,6 @@ xfobject(fname, fin) /* transform an object */ char *fname; FILE *fin; { - extern char *strcpy(); char typ[16], nam[MAXSTR]; int fn; /* modifier and type */ @@ -393,8 +421,10 @@ FILE *fin; return(-1); /* string arguments */ printf("%d", fa.nsargs); - for (i = 0; i < fa.nsargs; i++) - printf(" %s", fa.sarg[i]); + for (i = 0; i < fa.nsargs; i++) { + fputc(' ', stdout); + fputword(fa.sarg[i], stdout); + } printf("\n"); #ifdef IARGS /* integer arguments */ @@ -428,8 +458,10 @@ FILE *fin; if (xac > xfa && strcmp(xav[xfa], "-i")) resetarr = 2; printf("%d", fa.nsargs + resetarr + xac-xfa); - for (i = 0; i < fa.nsargs; i++) - printf(" %s", fa.sarg[i]); + for (i = 0; i < fa.nsargs; i++) { + fputc(' ', stdout); + fputword(fa.sarg[i], stdout); + } if (resetarr) printf(" -i 1"); for (i = xfa; i < xac; i++) /* add xf arguments */ @@ -454,20 +486,6 @@ FILE *fin; } -int -otype(ofname) /* get object function number from its name */ -register char *ofname; -{ - register int i; - - for (i = 0; i < NUMTYPES; i++) - if (!strcmp(ofun[i].funame, ofname)) - return(i); - - return(-1); /* not found */ -} - - alias(fin) /* transfer alias */ FILE *fin; { @@ -618,8 +636,10 @@ FILE *fin; return(-1); /* string arguments */ printf("%d", fa.nsargs); - for (i = 0; i < fa.nsargs; i++) - printf(" %s", fa.sarg[i]); + for (i = 0; i < fa.nsargs; i++) { + fputc(' ', stdout); + fputword(fa.sarg[i], stdout); + } printf("\n0\n%d\n", fa.nfargs); /* anchor point */ multp3(v, fa.farg, tot.xfm); @@ -802,11 +822,8 @@ initotypes() /* initialize ofun[] array */ 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++) + for (i = 0; i < NUMOTYPE; i++) if (hasfunc(i)) ofun[i].funp = addxform; /* special cases */ @@ -828,7 +845,7 @@ initotypes() /* initialize ofun[] array */ ofun[PAT_CTEXT].funp = ofun[PAT_BTEXT].funp = ofun[MIX_TEXT].funp = text; - ofun[ALIAS].funp = alias; + ofun[MOD_ALIAS].funp = alias; /* surface inverses */ tinvers[OBJ_FACE] = OBJ_FACE; tinvers[OBJ_SOURCE] = OBJ_SOURCE; @@ -867,17 +884,15 @@ char *fname; strcpy(mainfn, fname); } #else -openmain(fname) /* open fname for input, changing to its directory */ -char *fname; +openmain(iname) /* open input, changing directory for file */ +char *iname; { - extern FILE *tmpfile(); - extern char *getlibpath(), *getpath(); static char origdir[MAXPATH]; static char curfn[MAXPATH]; static int diffdir; register char *fpath; - if (fname == NULL) { /* standard input */ + if (iname == NULL) { /* standard input */ if (mainfp == NULL) { register int c; strcpy(mainfn, "standard input"); @@ -894,28 +909,28 @@ char *fname; } while ((c = getc(stdin)) != EOF) putc(c, mainfp); - fclose(stdin); } rewind(mainfp); /* rewind copy */ return; } if (mainfp == NULL) { /* first call, initialize */ getwd(origdir); - } else if (!strcmp(fname, curfn)) { /* just need to rewind? */ + } else if (!strcmp(iname, curfn)) { /* just need to rewind? */ rewind(mainfp); return; } else { /* else close old stream */ fclose(mainfp); - if (diffdir) { + mainfp = NULL; + if (diffdir) { /* return to our directory */ chdir(origdir); diffdir = 0; } } - strcpy(curfn, fname); /* remember file name */ - /* get full path */ - if ((fpath = getpath(fname, getlibpath(), R_OK)) == NULL) { + strcpy(curfn, iname); /* remember input name */ + /* get full path for file */ + if ((fpath = getpath(iname, getrlibpath(), R_OK)) == NULL) { fprintf(stderr, "%s: cannot find file \"%s\"\n", - progname, fname); + progname, iname); exit(1); } if (fpath[0] == '.' && ISDIRSEP(fpath[1])) /* remove leading ./ */ @@ -943,11 +958,11 @@ char *fname; diffdir++; } /* get final path component */ - for (fpath = fname+strlen(fname); - fpath > fname && !ISDIRSEP(fpath[-1]); fpath--) + for (fpath = iname+strlen(iname); + fpath > iname && !ISDIRSEP(fpath[-1]); fpath--) ; } - /* open the file */ + /* finally, open the file */ if ((mainfp = fopen(fpath, "r")) == NULL) { fprintf(stderr, "%s: cannot open file \"%s\"\n", progname, mainfn);