--- ray/src/gen/xform.c 1996/06/12 11:55:27 2.12 +++ ray/src/gen/xform.c 2003/07/03 22:41:44 2.29 @@ -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.29 2003/07/03 22:41:44 schorsch Exp $"; #endif - /* * xform.c - program to transform object files. * Transformations must preserve aspect ratio. @@ -12,14 +9,15 @@ static char SCCSid[] = "$SunId$ LBL"; * 11/6/86 Finally added error checking! */ -#include "standard.h" - -#include "paths.h" - #include +#ifndef _WIN32 + #include +#endif +#include "standard.h" +#include "platform.h" +#include "paths.h" #include "object.h" - #include "otypes.h" int xac; /* global xform argument count */ @@ -29,27 +27,23 @@ 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 = 0; /* 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 */ -extern char *malloc(), *fgetword(); +int stdinused = 0; /* stdin has been used by -f option? */ -char *mainfn[MAXPATH]; /* main file name */ +char mainfn[PATH_MAX]; /* main file name */ FILE *mainfp = NULL; /* main file pointer */ #define progname (xav[0]) @@ -59,28 +53,58 @@ main(argc, argv) /* get transform options and transfo int argc; char *argv[]; { + int mal_prefix = 0; char *fname; int a; - /* check for array */ + /* check for argument list file */ for (a = 1; a < argc; a++) + if (!strcmp(argv[a], "-f")) + return(doargf(argc, argv, a)); + /* check for regular array */ + for (a = 1; a < argc; a++) 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]) + break; + expand = 0; + continue; case 'e': if (argv[a][2]) break; @@ -89,7 +113,7 @@ char *argv[]; case 'I': if (argv[a][2]) break; - invert = 1; + invert = !invert; continue; } break; @@ -119,6 +143,11 @@ char *argv[]; putchar('\n'); /* transform input */ if (xac == argc) { + if (stdinused) { + fprintf(stderr, "%s: cannot use stdin more than once\n", + argv[0]); + exit(1); + } openmain(NULL); xform(mainfn, mainfp); } else @@ -127,10 +156,121 @@ char *argv[]; xform(mainfn, mainfp); } + if (mal_prefix) + free((void *)idprefix); return(0); } +doargf(ac, av, fi) /* take argument list from file */ +char **av; +int ac, fi; +{ + int inquote; + char *newav[256], **avp; + char argbuf[1024]; + char newid[128]; + char *oldid; + register char *cp; + FILE *argfp; + int n, i, k, newac, err; + + if (fi >= ac-1 || (av[fi+1][0] == '-' && av[fi+1][1] != '\0')) { + fprintf(stderr, "%s: missing file for -f option\n", av[0]); + exit(1); + } + if (av[fi+1][0] == '-' && av[fi+1][1] == '\0') { + if (stdinused++) { + fprintf(stderr, + "%s: cannot use stdin more than once\n", + av[0]); + exit(1); + } + argfp = stdin; + 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", + av[0], av[fi+1]); + exit(1); + } + n = 0; /* count number of lines in file */ + 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]); + exit(1); + } + nrept *= n; + rewind(argfp); + } + err = 0; k = 0; /* read each arg list and call main */ + while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL) { + if (!argbuf[0] || argbuf[0] == '#') + continue; + avp = newav+2; + avp[0] = av[0]; + for (i = 1; i < fi; i++) + avp[i] = av[i]; + newac = i; + 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 */ + 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]; + avp[newac] = NULL; + oldid = NULL; + for (i = 2; i < newac; i++) + if (!strcmp(avp[i-1], "-n")) { + oldid = avp[i]; + avp[i] = newid; + break; + } + if (oldid == NULL) { + newav[0] = av[0]; + newav[1] = "-n"; + newav[2] = newid; + avp = newav; + newac += 2; + } + if (oldid == NULL) + sprintf(newid, "i%d", k); + else + sprintf(newid, "%s.%d", oldid, k); + err |= main(newac, avp); + k++; + } + fclose(argfp); + return(err); +} + + doarray(ac, av, ai) /* make array */ char **av; int ac, ai; @@ -140,6 +280,11 @@ int ac, ai; char *oldid = NULL; int n, i, err; + if (ai >= ac-1 || (n = atoi(av[ai+1])) <= 0) { + fprintf(stderr, "%s: missing count for -a option\n", av[0]); + exit(1); + } + nrept *= n; avp = newav+2; avp[0] = av[0]; for (i = 1; i < ac; i++) @@ -158,7 +303,6 @@ int ac, ai; avp = newav; ac += 2; } - nrept *= n = atoi(av[ai+1]); err = 0; for (i = 0; i < n; i++) { if (oldid == NULL) @@ -209,8 +353,6 @@ xfcomm(fname, fin) /* transform a command */ char *fname; FILE *fin; { - extern FILE *popen(); - extern char *fgetline(); FILE *pin; char buf[512]; int i; @@ -227,9 +369,10 @@ FILE *fin; } else { printf("\n%s", buf); if (xac > 1) { - printf(" | %s -e", 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'); } @@ -240,7 +383,6 @@ xfobject(fname, fin) /* transform an object */ char *fname; FILE *fin; { - extern char *strcpy(); char typ[16], nam[MAXSTR]; int fn; /* modifier and type */ @@ -282,8 +424,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 */ @@ -317,8 +461,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 */ @@ -343,20 +489,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; { @@ -415,7 +547,7 @@ FILE *fin; if (readfargs(&fa, fin) != 1) return(-1); - if (fa.nfargs > 5) + if (fa.nfargs > 7) return(-1); printf("%d", fa.nsargs); if (idprefix == NULL) @@ -441,10 +573,8 @@ FILE *fin; if (fa.nfargs > 2) printf(" %12.6g %12.6g %12.6g", fa.farg[0]/tot.sca, fa.farg[1]/tot.sca, fa.farg[2]/tot.sca); - if (fa.nfargs > 3) - printf(" %12.6g", fa.farg[3]); - if (fa.nfargs > 4) - printf(" %12.6g", fa.farg[4]); + for (i = 3; i < fa.nfargs; i++) + printf(" %12.6g", fa.farg[i]); printf("\n"); freefargs(&fa); return(0); @@ -509,8 +639,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); @@ -693,11 +825,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 */ @@ -719,7 +848,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; @@ -758,16 +887,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 curdir[MAXPATH]; - char newdir[MAXPATH], *cp; + static char origdir[PATH_MAX]; + static char curfn[PATH_MAX]; + 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"); @@ -784,53 +912,61 @@ char *fname; } while ((c = getc(stdin)) != EOF) putc(c, mainfp); - fclose(stdin); } rewind(mainfp); /* rewind copy */ return; } - /* get full path */ - if ((cp = getpath(fname, getlibpath(), R_OK)) == NULL) { - fprintf(stderr, "%s: cannot find file \"%s\"\n", - progname, fname); - exit(1); - } - if (cp[0] == '.' && ISDIRSEP(cp[1])) /* remove leading ./ */ - cp += 2; if (mainfp == NULL) { /* first call, initialize */ - getwd(origdir); - strcpy(curdir, origdir); - } else if (!strcmp(cp, mainfn)) { /* just need to rewind? */ + getcwd(origdir, sizeof(origdir)); + } else if (!strcmp(iname, curfn)) { /* just need to rewind? */ rewind(mainfp); return; - } else /* else close old stream */ + } else { /* else close old stream */ fclose(mainfp); + mainfp = NULL; + if (diffdir) { /* return to our directory */ + chdir(origdir); + diffdir = 0; + } + } + 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, iname); + exit(1); + } + if (fpath[0] == '.' && ISDIRSEP(fpath[1])) /* remove leading ./ */ + fpath += 2; /* record path name */ - strcpy(mainfn, cp); - if (expand) { /* get directory component */ - if (ISDIRSEP(cp[0])) - strcpy(newdir, cp); - else - sprintf(newdir, "%s%c%s", origdir, DIRSEP, cp); - for (cp = newdir+strlen(newdir); !ISDIRSEP(cp[-1]); cp--) - ; + strcpy(mainfn, fpath); + if (expand) { /* change to local directory */ + register char *cp = fpath + strlen(fpath); /* get dir. */ + while (cp > fpath) { + cp--; + if (ISDIRSEP(*cp)) { + if (cp == fpath) + cp++; /* root special case */ + break; + } + } *cp = '\0'; - if (strcmp(newdir, curdir)) { /* change to new directory? */ - if (chdir(newdir) < 0) { + if (fpath[0]) { /* change to new directory? */ + if (chdir(fpath) < 0) { fprintf(stderr, "%s: cannot change directory to \"%s\"\n", - progname, newdir); + progname, fpath); exit(1); } - strcpy(curdir, newdir); + diffdir++; } /* get final path component */ - for (cp = fname+strlen(fname); - cp > fname && !ISDIRSEP(cp[-1]); cp--) + for (fpath = iname+strlen(iname); + fpath > iname && !ISDIRSEP(fpath[-1]); fpath--) ; } - /* open the file */ - if ((mainfp = fopen(cp, "r")) == NULL) { + /* finally, open the file */ + if ((mainfp = fopen(fpath, "r")) == NULL) { fprintf(stderr, "%s: cannot open file \"%s\"\n", progname, mainfn); exit(1);