ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/gen/xform.c
(Generate patch)

Comparing ray/src/gen/xform.c (file contents):
Revision 2.16 by gwlarson, Thu Jun 3 14:15:01 1999 UTC vs.
Revision 2.25 by greg, Tue May 13 17:58:32 2003 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1996 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  xform.c - program to transform object files.
6   *              Transformations must preserve aspect ratio.
# Line 29 | Line 26 | int  xfa;                              /* start of xf arguments */
26   XF  tot;                                /* total transformation */
27   int  reverse;                           /* boolean true if scene mirrored */
28  
29 < int  invert = 0;                        /* boolean true to invert surfaces */
29 > int  invert;                            /* boolean true to invert surfaces */
30  
31 < int  expand = 1;                        /* boolean true to expand commands */
31 > int  expand;                            /* boolean true to expand commands */
32  
33 < char  *newmod = NULL;                   /* new modifier for surfaces */
33 > char  *newmod;                          /* new modifier for surfaces */
34  
35 < char  *idprefix = NULL;                 /* prefix for object identifiers */
35 > char  *idprefix;                        /* prefix for object identifiers */
36  
37 < #define  ALIAS          NUMOTYPE        /* put alias at end of array */
37 > FUN  ofun[NUMOTYPE] = INIT_OTYPE;       /* default types and actions */
38  
42 #define  NUMTYPES       (NUMOTYPE+1)    /* total number of object types */
43
44 FUN  ofun[NUMTYPES] = INIT_OTYPE;       /* default types and actions */
45
39   short  tinvers[NUMOTYPE];               /* inverse types for surfaces */
40  
41   int  nrept = 1;                         /* number of array repetitions */
42  
43 < extern char  *malloc(), *fgets(), *fgetword();
43 > int stdinused = 0;                      /* stdin has been used by -f option? */
44  
45   char  mainfn[MAXPATH];                  /* main file name */
46   FILE  *mainfp = NULL;                   /* main file pointer */
# Line 59 | Line 52 | main(argc, argv)               /* get transform options and transfo
52   int  argc;
53   char  *argv[];
54   {
55 +        int  mal_prefix = 0;
56          char  *fname;
57          int  a;
58                                          /* check for argument list file */
# Line 70 | Line 64 | char  *argv[];
64                  if (!strcmp(argv[a], "-a"))
65                          return(doarray(argc, argv, a));
66  
67 <        initotypes();
67 >        initotypes();                   /* initialize */
68 >        invert = 0;
69 >        expand = 1;
70 >        newmod = NULL;
71 >        idprefix = NULL;
72  
73          for (a = 1; a < argc; a++) {
74                  if (argv[a][0] == '-')
75                          switch (argv[a][1]) {
76                          case 'm':
77 <                                if (argv[a][2] || a+1 >= argc)
77 >                                if (argv[a][2] | a+1 >= argc)
78                                          break;
79 <                                newmod = argv[++a];
79 >                                a++;
80 >                                if (newmod == NULL)
81 >                                        newmod = argv[a];
82                                  continue;
83                          case 'n':
84 <                                if (argv[a][2] || a+1 >= argc)
84 >                                if (argv[a][2] | a+1 >= argc)
85                                          break;
86 <                                idprefix = argv[++a];
86 >                                a++;
87 >                                if (idprefix == NULL)
88 >                                        idprefix = argv[a];
89 >                                else {
90 >                                        register char   *newp;
91 >                                        newp = (char *)malloc(strlen(idprefix)+
92 >                                                        strlen(argv[a])+2);
93 >                                        if (newp == NULL)
94 >                                                exit(2);
95 >                                        sprintf(newp, "%s.%s",
96 >                                                        idprefix, argv[a]);
97 >                                        if (mal_prefix++)
98 >                                                free((void *)idprefix);
99 >                                        idprefix = newp;
100 >                                }
101                                  continue;
102                          case 'c':
103                                  if (argv[a][2])
# Line 98 | Line 112 | char  *argv[];
112                          case 'I':
113                                  if (argv[a][2])
114                                          break;
115 <                                invert = 1;
115 >                                invert = !invert;
116                                  continue;
117                          }
118                  break;
# Line 128 | Line 142 | char  *argv[];
142          putchar('\n');
143                                          /* transform input */
144          if (xac == argc) {
145 +                if (stdinused) {
146 +                        fprintf(stderr, "%s: cannot use stdin more than once\n",
147 +                                        argv[0]);
148 +                        exit(1);
149 +                }
150                  openmain(NULL);
151                  xform(mainfn, mainfp);
152          } else
# Line 136 | Line 155 | char  *argv[];
155                          xform(mainfn, mainfp);
156                  }
157  
158 +        if (mal_prefix)
159 +                free((void *)idprefix);
160          return(0);
161   }
162  
# Line 144 | Line 165 | doargf(ac, av, fi)                     /* take argument list from file *
165   char  **av;
166   int  ac, fi;
167   {
168 +        int  inquote;
169          char  *newav[256], **avp;
170 <        char  argbuf[2048];
170 >        char  argbuf[1024];
171          char  newid[128];
172          char  *oldid;
173          register char   *cp;
174          FILE    *argfp;
175          int  n, i, k, newac, err;
176          
177 <        if (fi >= ac-1 || av[fi+1][0] == '-') {
177 >        if (fi >= ac-1 || (av[fi+1][0] == '-' && av[fi+1][1] != '\0')) {
178                  fprintf(stderr, "%s: missing file for -f option\n", av[0]);
179                  exit(1);
180          }
181 <        if ((argfp = fopen(av[fi+1], "r")) == NULL) {
182 <                fprintf(stderr, "%s: cannot open argument file \"%s\"\n",
183 <                                av[0], av[fi+1]);
184 <                exit(1);
181 >        if (av[fi+1][0] == '-' && av[fi+1][1] == '\0') {
182 >                if (stdinused++) {
183 >                        fprintf(stderr,
184 >                                "%s: cannot use stdin more than once\n",
185 >                                        av[0]);
186 >                        exit(1);
187 >                }
188 >                argfp = stdin;
189 >                n = 100;                /* we just don't know! */
190 >        } else {
191 >                if ((argfp = fopen(av[fi+1], "r")) == NULL) {
192 >                        fprintf(stderr,
193 >                                "%s: cannot open argument file \"%s\"\n",
194 >                                        av[0], av[fi+1]);
195 >                        exit(1);
196 >                }
197 >                n = 0;                  /* count number of lines in file */
198 >                while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL)
199 >                        n += argbuf[0] && argbuf[0] != '#';
200 >                if (!n) {
201 >                        fprintf(stderr, "%s: empty argument file \"%s\"\n",
202 >                                        av[0], av[fi+1]);
203 >                        exit(1);
204 >                }
205 >                nrept *= n;
206 >                rewind(argfp);
207          }
208 <        n = 0;                  /* count number of lines in file */
209 <        while (fgets(argbuf, sizeof(argbuf), argfp) != NULL)
210 <                n++;
211 <        if (!n) {
168 <                fprintf(stderr, "%s: empty argument file \"%s\"\n",
169 <                                av[0], av[fi+1]);
170 <                exit(1);
171 <        }
172 <        rewind(argfp);
173 <        nrept *= n;
174 <        err = 0;                        /* read each arg list and call main */
175 <        for (k = 0; k < n; k++) {
176 <                fgets(argbuf, sizeof(argbuf), argfp);
208 >        err = 0; k = 0;                 /* read each arg list and call main */
209 >        while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL) {
210 >                if (!argbuf[0] || argbuf[0] == '#')
211 >                        continue;
212                  avp = newav+2;
213                  avp[0] = av[0];
214                  for (i = 1; i < fi; i++)
215                          avp[i] = av[i];
216                  newac = i;
217 <                cp = argbuf;                    /* parse new words */
217 >                cp = argbuf;            /* parse new words */
218 >                if (*cp == '!') cp++;
219 >                if (!strncmp(cp, "xform ", 6)) cp += 6;
220 >                inquote = 0;
221                  for ( ; ; ) {
222 +                skipspaces:
223                          while (isspace(*cp))    /* nullify spaces */
224                                  *cp++ = '\0';
225 +                        if ((*cp == '"' | *cp == '\''))
226 +                                inquote = *cp++;
227                          if (!*cp)               /* all done? */
228                                  break;
229 +                        if (cp[0] == '\\' && cp[1])
230 +                                if (*++cp == '\n')
231 +                                        goto skipspaces;
232                          avp[newac++] = cp;      /* add argument to list */
233 <                        while (*++cp && !isspace(*cp))
234 <                                ;
233 >                        if (inquote) {
234 >                                while (*++cp)
235 >                                        if (*cp == inquote) {
236 >                                                *cp++ = '\0';
237 >                                                break;
238 >                                        }
239 >                        } else {
240 >                                while (*++cp && !isspace(*cp))
241 >                                        ;
242 >                        }
243                  }
244                  for (i = fi+2; i < ac; i++)
245                          avp[newac++] = av[i];
# Line 211 | Line 263 | int  ac, fi;
263                  else
264                          sprintf(newid, "%s.%d", oldid, k);
265                  err |= main(newac, avp);
266 +                k++;
267          }
268          fclose(argfp);
269          return(err);
# Line 299 | Line 352 | xfcomm(fname, fin)                     /* transform a command */
352   char  *fname;
353   FILE  *fin;
354   {
302        extern FILE  *popen();
303        extern char  *fgetline();
355          FILE  *pin;
356          char  buf[512];
357          int  i;
# Line 317 | Line 368 | FILE  *fin;
368          } else {
369                  printf("\n%s", buf);
370                  if (xac > 1) {
371 <                        printf(" | %s", xav[0]);
371 >                        printf(" | %s -e", xav[0]);     /* expand next time */
372                          for (i = 1; i < xac; i++)
373 <                                printf(" %s", xav[i]);
373 >                                if (i >= xfa || strcmp(xav[i], "-c"))
374 >                                        printf(" %s", xav[i]);
375                  }
376                  putchar('\n');
377          }
# Line 330 | Line 382 | xfobject(fname, fin)                           /* transform an object */
382   char  *fname;
383   FILE  *fin;
384   {
333        extern char  *strcpy();
385          char  typ[16], nam[MAXSTR];
386          int  fn;
387                                                  /* modifier and type */
# Line 372 | Line 423 | FILE  *fin;
423                  return(-1);
424                                          /* string arguments */
425          printf("%d", fa.nsargs);
426 <        for (i = 0; i < fa.nsargs; i++)
427 <                printf(" %s", fa.sarg[i]);
426 >        for (i = 0; i < fa.nsargs; i++) {
427 >                fputc(' ', stdout);
428 >                fputword(fa.sarg[i], stdout);
429 >        }
430          printf("\n");
431   #ifdef  IARGS
432                                          /* integer arguments */
# Line 407 | Line 460 | FILE  *fin;
460          if (xac > xfa && strcmp(xav[xfa], "-i"))
461                  resetarr = 2;
462          printf("%d", fa.nsargs + resetarr + xac-xfa);
463 <        for (i = 0; i < fa.nsargs; i++)
464 <                printf(" %s", fa.sarg[i]);
463 >        for (i = 0; i < fa.nsargs; i++) {
464 >                fputc(' ', stdout);
465 >                fputword(fa.sarg[i], stdout);
466 >        }
467          if (resetarr)
468                  printf(" -i 1");
469          for (i = xfa; i < xac; i++)     /* add xf arguments */
# Line 433 | Line 488 | FILE  *fin;
488   }
489  
490  
436 int
437 otype(ofname)                   /* get object function number from its name */
438 register char  *ofname;
439 {
440        register int  i;
441
442        for (i = 0; i < NUMTYPES; i++)
443                if (!strcmp(ofun[i].funame, ofname))
444                        return(i);
445
446        return(-1);             /* not found */
447 }
448
449
491   alias(fin)                      /* transfer alias */
492   FILE  *fin;
493   {
# Line 597 | Line 638 | FILE  *fin;
638                  return(-1);
639                                          /* string arguments */
640          printf("%d", fa.nsargs);
641 <        for (i = 0; i < fa.nsargs; i++)
642 <                printf(" %s", fa.sarg[i]);
641 >        for (i = 0; i < fa.nsargs; i++) {
642 >                fputc(' ', stdout);
643 >                fputword(fa.sarg[i], stdout);
644 >        }
645          printf("\n0\n%d\n", fa.nfargs);
646                                          /* anchor point */
647          multp3(v, fa.farg, tot.xfm);
# Line 781 | Line 824 | initotypes()                   /* initialize ofun[] array */
824  
825          if (ofun[OBJ_SOURCE].funp == o_source)
826                  return;                 /* done already */
784                                        /* alias is additional */
785        ofun[ALIAS].funame = ALIASID;
786        ofun[ALIAS].flags = 0;
827                                          /* functions get new transform */
828 <        for (i = 0; i < NUMTYPES; i++)
828 >        for (i = 0; i < NUMOTYPE; i++)
829                  if (hasfunc(i))
830                          ofun[i].funp = addxform;
831                                          /* special cases */
# Line 807 | Line 847 | initotypes()                   /* initialize ofun[] array */
847          ofun[PAT_CTEXT].funp =
848          ofun[PAT_BTEXT].funp =
849          ofun[MIX_TEXT].funp = text;
850 <        ofun[ALIAS].funp = alias;
850 >        ofun[MOD_ALIAS].funp = alias;
851                                          /* surface inverses */
852          tinvers[OBJ_FACE] = OBJ_FACE;
853          tinvers[OBJ_SOURCE] = OBJ_SOURCE;
# Line 846 | Line 886 | char  *fname;
886          strcpy(mainfn, fname);
887   }
888   #else
889 < openmain(fname)         /* open fname for input, changing to its directory */
890 < char  *fname;
889 > openmain(iname)         /* open input, changing directory for file */
890 > char  *iname;
891   {
852        extern FILE  *tmpfile();
853        extern char  *getlibpath(), *getpath();
892          static char  origdir[MAXPATH];
893          static char  curfn[MAXPATH];
894          static int  diffdir;
895          register char  *fpath;
896  
897 <        if (fname == NULL) {                    /* standard input */
897 >        if (iname == NULL) {                    /* standard input */
898                  if (mainfp == NULL) {
899                          register int  c;
900                          strcpy(mainfn, "standard input");
# Line 873 | Line 911 | char  *fname;
911                          }
912                          while ((c = getc(stdin)) != EOF)
913                                  putc(c, mainfp);
876                        fclose(stdin);
914                  }
915                  rewind(mainfp);                 /* rewind copy */
916                  return;
917          }
918          if (mainfp == NULL) {                   /* first call, initialize */
919                  getwd(origdir);
920 <        } else if (!strcmp(fname, curfn)) {     /* just need to rewind? */
920 >        } else if (!strcmp(iname, curfn)) {     /* just need to rewind? */
921                  rewind(mainfp);
922                  return;
923          } else {                                /* else close old stream */
924                  fclose(mainfp);
925 <                if (diffdir) {
925 >                mainfp = NULL;
926 >                if (diffdir) {                  /* return to our directory */
927                          chdir(origdir);
928                          diffdir = 0;
929                  }
930          }
931 <        strcpy(curfn, fname);                   /* remember file name */
932 <                                                /* get full path */
933 <        if ((fpath = getpath(fname, getlibpath(), R_OK)) == NULL) {
931 >        strcpy(curfn, iname);                   /* remember input name */
932 >                                                /* get full path for file */
933 >        if ((fpath = getpath(iname, getrlibpath(), R_OK)) == NULL) {
934                  fprintf(stderr, "%s: cannot find file \"%s\"\n",
935 <                                progname, fname);
935 >                                progname, iname);
936                  exit(1);
937          }
938          if (fpath[0] == '.' && ISDIRSEP(fpath[1]))      /* remove leading ./ */
# Line 922 | Line 960 | char  *fname;
960                          diffdir++;
961                  }
962                                                  /* get final path component */
963 <                for (fpath = fname+strlen(fname);
964 <                                fpath > fname && !ISDIRSEP(fpath[-1]); fpath--)
963 >                for (fpath = iname+strlen(iname);
964 >                                fpath > iname && !ISDIRSEP(fpath[-1]); fpath--)
965                          ;
966          }
967 <                                                /* open the file */
967 >                                                /* finally, open the file */
968          if ((mainfp = fopen(fpath, "r")) == NULL) {
969                  fprintf(stderr, "%s: cannot open file \"%s\"\n",
970                                  progname, mainfn);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines