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.13 by greg, Tue Jun 18 21:12:51 1996 UTC vs.
Revision 2.24 by greg, Tue Mar 11 19:29:05 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(), *fgetword();
43 > int stdinused = 0;                      /* stdin has been used by -f option? */
44  
45 < char  *mainfn[MAXPATH];                 /* main file name */
45 > char  mainfn[MAXPATH];                  /* main file name */
46   FILE  *mainfp = NULL;                   /* main file pointer */
47  
48   #define  progname  (xav[0])
# 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 array */
58 >                                        /* check for argument list file */
59          for (a = 1; a < argc; a++)
60 +                if (!strcmp(argv[a], "-f"))
61 +                        return(doargf(argc, argv, a));
62 +                                        /* check for regular array */
63 +        for (a = 1; a < argc; a++)
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 94 | 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 124 | 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 132 | Line 155 | char  *argv[];
155                          xform(mainfn, mainfp);
156                  }
157  
158 +        if (mal_prefix)
159 +                free((void *)idprefix);
160          return(0);
161   }
162  
163  
164 + 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[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] == '-' && av[fi+1][1] != '\0')) {
178 +                fprintf(stderr, "%s: missing file for -f option\n", av[0]);
179 +                exit(1);
180 +        }
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 +        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 */
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 +                        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];
246 +                avp[newac] = NULL;
247 +                oldid = NULL;
248 +                for (i = 2; i < newac; i++)
249 +                        if (!strcmp(avp[i-1], "-n")) {
250 +                                oldid = avp[i];
251 +                                avp[i] = newid;
252 +                                break;
253 +                        }
254 +                if (oldid == NULL) {
255 +                        newav[0] = av[0];
256 +                        newav[1] = "-n";
257 +                        newav[2] = newid;
258 +                        avp = newav;
259 +                        newac += 2;
260 +                }
261 +                if (oldid == NULL)
262 +                        sprintf(newid, "i%d", k);
263 +                else
264 +                        sprintf(newid, "%s.%d", oldid, k);
265 +                err |= main(newac, avp);
266 +                k++;
267 +        }
268 +        fclose(argfp);
269 +        return(err);
270 + }
271 +
272 +
273   doarray(ac, av, ai)                     /* make array */
274   char  **av;
275   int  ac, ai;
# Line 145 | Line 279 | int  ac, ai;
279          char  *oldid = NULL;
280          int  n, i, err;
281          
282 +        if (ai >= ac-1 || (n = atoi(av[ai+1])) <= 0) {
283 +                fprintf(stderr, "%s: missing count for -a option\n", av[0]);
284 +                exit(1);
285 +        }
286 +        nrept *= n;
287          avp = newav+2;
288          avp[0] = av[0];
289          for (i = 1; i < ac; i++)
# Line 163 | Line 302 | int  ac, ai;
302                  avp = newav;
303                  ac += 2;
304          }
166        nrept *= n = atoi(av[ai+1]);
305          err = 0;
306          for (i = 0; i < n; i++) {
307                  if (oldid == NULL)
# Line 214 | Line 352 | xfcomm(fname, fin)                     /* transform a command */
352   char  *fname;
353   FILE  *fin;
354   {
217        extern FILE  *popen();
218        extern char  *fgetline();
355          FILE  *pin;
356          char  buf[512];
357          int  i;
# Line 232 | 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 245 | Line 382 | xfobject(fname, fin)                           /* transform an object */
382   char  *fname;
383   FILE  *fin;
384   {
248        extern char  *strcpy();
385          char  typ[16], nam[MAXSTR];
386          int  fn;
387                                                  /* modifier and type */
# Line 287 | 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 322 | 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 348 | Line 488 | FILE  *fin;
488   }
489  
490  
351 int
352 otype(ofname)                   /* get object function number from its name */
353 register char  *ofname;
354 {
355        register int  i;
356
357        for (i = 0; i < NUMTYPES; i++)
358                if (!strcmp(ofun[i].funame, ofname))
359                        return(i);
360
361        return(-1);             /* not found */
362 }
363
364
491   alias(fin)                      /* transfer alias */
492   FILE  *fin;
493   {
# Line 420 | Line 546 | FILE  *fin;
546  
547          if (readfargs(&fa, fin) != 1)
548                  return(-1);
549 <        if (fa.nfargs > 5)
549 >        if (fa.nfargs > 7)
550                  return(-1);
551          printf("%d", fa.nsargs);
552          if (idprefix == NULL)
# Line 446 | Line 572 | FILE  *fin;
572          if (fa.nfargs > 2)
573                  printf(" %12.6g %12.6g %12.6g", fa.farg[0]/tot.sca,
574                                  fa.farg[1]/tot.sca, fa.farg[2]/tot.sca);
575 <        if (fa.nfargs > 3)
576 <                printf(" %12.6g", fa.farg[3]);
451 <        if (fa.nfargs > 4)
452 <                printf(" %12.6g", fa.farg[4]);
575 >        for (i = 3; i < fa.nfargs; i++)
576 >                printf(" %12.6g", fa.farg[i]);
577          printf("\n");
578          freefargs(&fa);
579          return(0);
# Line 514 | 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 698 | Line 824 | initotypes()                   /* initialize ofun[] array */
824  
825          if (ofun[OBJ_SOURCE].funp == o_source)
826                  return;                 /* done already */
701                                        /* alias is additional */
702        ofun[ALIAS].funame = ALIASID;
703        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 724 | 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 763 | 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   {
769        extern FILE  *tmpfile();
770        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 790 | Line 911 | char  *fname;
911                          }
912                          while ((c = getc(stdin)) != EOF)
913                                  putc(c, mainfp);
793                        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, getlibpath(), 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 839 | 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