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.12 by greg, Wed Jun 12 11:55:27 1996 UTC vs.
Revision 2.32 by greg, Fri Aug 29 21:44:17 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 12 | Line 9 | static char SCCSid[] = "$SunId$ LBL";
9   *     11/6/86          Finally added error checking!
10   */
11  
15 #include  "standard.h"
16
17 #include  "paths.h"
18
12   #include  <ctype.h>
13  
14 + #include  "standard.h"
15 + #include  "platform.h"
16 + #include  "paths.h"
17   #include  "object.h"
22
18   #include  "otypes.h"
19  
20   int  xac;                               /* global xform argument count */
# Line 29 | Line 24 | int  xfa;                              /* start of xf arguments */
24   XF  tot;                                /* total transformation */
25   int  reverse;                           /* boolean true if scene mirrored */
26  
27 < int  invert = 0;                        /* boolean true to invert surfaces */
27 > int  invert;                            /* boolean true to invert surfaces */
28  
29 < int  expand = 0;                        /* boolean true to expand commands */
29 > int  expand;                            /* boolean true to expand commands */
30  
31 < char  *newmod = NULL;                   /* new modifier for surfaces */
31 > char  *newmod;                          /* new modifier for surfaces */
32  
33 < char  *idprefix = NULL;                 /* prefix for object identifiers */
33 > char  *idprefix;                        /* prefix for object identifiers */
34  
35 < #define  ALIAS          NUMOTYPE        /* put alias at end of array */
35 > FUN  ofun[NUMOTYPE] = INIT_OTYPE;       /* default types and actions */
36  
42 #define  NUMTYPES       (NUMOTYPE+1)    /* total number of object types */
43
44 FUN  ofun[NUMTYPES] = INIT_OTYPE;       /* default types and actions */
45
37   short  tinvers[NUMOTYPE];               /* inverse types for surfaces */
38  
39   int  nrept = 1;                         /* number of array repetitions */
40  
41 < extern char  *malloc(), *fgetword();
41 > int stdinused = 0;                      /* stdin has been used by -f option? */
42  
43 < char  *mainfn[MAXPATH];                 /* main file name */
43 > char  mainfn[PATH_MAX];                 /* main file name */
44   FILE  *mainfp = NULL;                   /* main file pointer */
45  
46   #define  progname  (xav[0])
# Line 59 | Line 50 | main(argc, argv)               /* get transform options and transfo
50   int  argc;
51   char  *argv[];
52   {
53 +        int  mal_prefix = 0;
54          char  *fname;
55          int  a;
56 <                                        /* check for array */
56 >                                        /* check for argument list file */
57          for (a = 1; a < argc; a++)
58 +                if (!strcmp(argv[a], "-f"))
59 +                        return(doargf(argc, argv, a));
60 +                                        /* check for regular array */
61 +        for (a = 1; a < argc; a++)
62                  if (!strcmp(argv[a], "-a"))
63                          return(doarray(argc, argv, a));
64  
65 <        initotypes();
65 >        initotypes();                   /* initialize */
66 >        invert = 0;
67 >        expand = 1;
68 >        newmod = NULL;
69 >        idprefix = NULL;
70  
71          for (a = 1; a < argc; a++) {
72                  if (argv[a][0] == '-')
73                          switch (argv[a][1]) {
74                          case 'm':
75 <                                if (argv[a][2] || a+1 >= argc)
75 >                                if (argv[a][2] | (a+1 >= argc))
76                                          break;
77 <                                newmod = argv[++a];
77 >                                a++;
78 >                                if (newmod == NULL)
79 >                                        newmod = argv[a];
80                                  continue;
81                          case 'n':
82 <                                if (argv[a][2] || a+1 >= argc)
82 >                                if (argv[a][2] | (a+1 >= argc))
83                                          break;
84 <                                idprefix = argv[++a];
84 >                                a++;
85 >                                if (idprefix == NULL)
86 >                                        idprefix = argv[a];
87 >                                else {
88 >                                        register char   *newp;
89 >                                        newp = (char *)malloc(strlen(idprefix)+
90 >                                                        strlen(argv[a])+2);
91 >                                        if (newp == NULL)
92 >                                                exit(2);
93 >                                        sprintf(newp, "%s.%s",
94 >                                                        idprefix, argv[a]);
95 >                                        if (mal_prefix++)
96 >                                                free((void *)idprefix);
97 >                                        idprefix = newp;
98 >                                }
99                                  continue;
100 +                        case 'c':
101 +                                if (argv[a][2])
102 +                                        break;
103 +                                expand = 0;
104 +                                continue;
105                          case 'e':
106                                  if (argv[a][2])
107                                          break;
# Line 89 | Line 110 | char  *argv[];
110                          case 'I':
111                                  if (argv[a][2])
112                                          break;
113 <                                invert = 1;
113 >                                invert = !invert;
114                                  continue;
115                          }
116                  break;
# Line 100 | Line 121 | char  *argv[];
121  
122          a += xf(&tot, argc-a, argv+a);
123  
124 <        if (reverse = tot.sca < 0.0)
124 >        if ( (reverse = tot.sca < 0.0) )
125                  tot.sca = -tot.sca;
126          if (invert)
127                  reverse = !reverse;
# Line 119 | Line 140 | char  *argv[];
140          putchar('\n');
141                                          /* transform input */
142          if (xac == argc) {
143 +                if (stdinused) {
144 +                        fprintf(stderr, "%s: cannot use stdin more than once\n",
145 +                                        argv[0]);
146 +                        exit(1);
147 +                }
148                  openmain(NULL);
149                  xform(mainfn, mainfp);
150          } else
# Line 127 | Line 153 | char  *argv[];
153                          xform(mainfn, mainfp);
154                  }
155  
156 +        if (mal_prefix)
157 +                free((void *)idprefix);
158          return(0);
159   }
160  
161  
162 + doargf(ac, av, fi)                      /* take argument list from file */
163 + char  **av;
164 + int  ac, fi;
165 + {
166 +        int  inquote;
167 +        char  *newav[256], **avp;
168 +        char  argbuf[1024];
169 +        char  newid[128];
170 +        char  *oldid;
171 +        register char   *cp;
172 +        FILE    *argfp;
173 +        int  n, i, k, newac, err;
174 +        
175 +        if (fi >= ac-1 || (av[fi+1][0] == '-' && av[fi+1][1] != '\0')) {
176 +                fprintf(stderr, "%s: missing file for -f option\n", av[0]);
177 +                exit(1);
178 +        }
179 +        if (av[fi+1][0] == '-' && av[fi+1][1] == '\0') {
180 +                if (stdinused++) {
181 +                        fprintf(stderr,
182 +                                "%s: cannot use stdin more than once\n",
183 +                                        av[0]);
184 +                        exit(1);
185 +                }
186 +                argfp = stdin;
187 +                n = 100;                /* we just don't know! */
188 +        } else {
189 +                if ((argfp = fopen(av[fi+1], "r")) == NULL) {
190 +                        fprintf(stderr,
191 +                                "%s: cannot open argument file \"%s\"\n",
192 +                                        av[0], av[fi+1]);
193 +                        exit(1);
194 +                }
195 +                n = 0;                  /* count number of lines in file */
196 +                while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL)
197 +                        n += argbuf[0] && argbuf[0] != '#';
198 +                if (!n) {
199 +                        fprintf(stderr, "%s: empty argument file \"%s\"\n",
200 +                                        av[0], av[fi+1]);
201 +                        exit(1);
202 +                }
203 +                nrept *= n;
204 +                rewind(argfp);
205 +        }
206 +        err = 0; k = 0;                 /* read each arg list and call main */
207 +        while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL) {
208 +                if (!argbuf[0] || argbuf[0] == '#')
209 +                        continue;
210 +                avp = newav+2;
211 +                avp[0] = av[0];
212 +                for (i = 1; i < fi; i++)
213 +                        avp[i] = av[i];
214 +                newac = i;
215 +                cp = argbuf;            /* parse new words */
216 +                if (*cp == '!') cp++;
217 +                if (!strncmp(cp, "xform ", 6)) cp += 6;
218 +                inquote = 0;
219 +                for ( ; ; ) {
220 +                skipspaces:
221 +                        while (isspace(*cp))    /* nullify spaces */
222 +                                *cp++ = '\0';
223 +                        if ((*cp == '"') | (*cp == '\''))
224 +                                inquote = *cp++;
225 +                        if (!*cp)               /* all done? */
226 +                                break;
227 +                        if (cp[0] == '\\' && cp[1])
228 +                                if (*++cp == '\n')
229 +                                        goto skipspaces;
230 +                        avp[newac++] = cp;      /* add argument to list */
231 +                        if (inquote) {
232 +                                while (*++cp)
233 +                                        if (*cp == inquote) {
234 +                                                *cp++ = '\0';
235 +                                                break;
236 +                                        }
237 +                        } else {
238 +                                while (*++cp && !isspace(*cp))
239 +                                        ;
240 +                        }
241 +                }
242 +                for (i = fi+2; i < ac; i++)
243 +                        avp[newac++] = av[i];
244 +                avp[newac] = NULL;
245 +                oldid = NULL;
246 +                for (i = 2; i < newac; i++)
247 +                        if (!strcmp(avp[i-1], "-n")) {
248 +                                oldid = avp[i];
249 +                                avp[i] = newid;
250 +                                break;
251 +                        }
252 +                if (oldid == NULL) {
253 +                        newav[0] = av[0];
254 +                        newav[1] = "-n";
255 +                        newav[2] = newid;
256 +                        avp = newav;
257 +                        newac += 2;
258 +                }
259 +                if (oldid == NULL)
260 +                        sprintf(newid, "i%d", k);
261 +                else
262 +                        sprintf(newid, "%s.%d", oldid, k);
263 +                err |= main(newac, avp);
264 +                k++;
265 +        }
266 +        fclose(argfp);
267 +        return(err);
268 + }
269 +
270 +
271   doarray(ac, av, ai)                     /* make array */
272   char  **av;
273   int  ac, ai;
# Line 140 | Line 277 | int  ac, ai;
277          char  *oldid = NULL;
278          int  n, i, err;
279          
280 +        if (ai >= ac-1 || (n = atoi(av[ai+1])) <= 0) {
281 +                fprintf(stderr, "%s: missing count for -a option\n", av[0]);
282 +                exit(1);
283 +        }
284 +        nrept *= n;
285          avp = newav+2;
286          avp[0] = av[0];
287          for (i = 1; i < ac; i++)
# Line 158 | Line 300 | int  ac, ai;
300                  avp = newav;
301                  ac += 2;
302          }
161        nrept *= n = atoi(av[ai+1]);
303          err = 0;
304          for (i = 0; i < n; i++) {
305                  if (oldid == NULL)
# Line 209 | Line 350 | xfcomm(fname, fin)                     /* transform a command */
350   char  *fname;
351   FILE  *fin;
352   {
212        extern FILE  *popen();
213        extern char  *fgetline();
353          FILE  *pin;
354          char  buf[512];
355          int  i;
# Line 227 | Line 366 | FILE  *fin;
366          } else {
367                  printf("\n%s", buf);
368                  if (xac > 1) {
369 <                        printf(" | %s -e", xav[0]);
369 >                        printf(" | %s -e", xav[0]);     /* expand next time */
370                          for (i = 1; i < xac; i++)
371 <                                printf(" %s", xav[i]);
371 >                                if (i >= xfa || strcmp(xav[i], "-c"))
372 >                                        printf(" %s", xav[i]);
373                  }
374                  putchar('\n');
375          }
# Line 240 | Line 380 | xfobject(fname, fin)                           /* transform an object */
380   char  *fname;
381   FILE  *fin;
382   {
243        extern char  *strcpy();
383          char  typ[16], nam[MAXSTR];
384          int  fn;
385                                                  /* modifier and type */
# Line 282 | Line 421 | FILE  *fin;
421                  return(-1);
422                                          /* string arguments */
423          printf("%d", fa.nsargs);
424 <        for (i = 0; i < fa.nsargs; i++)
425 <                printf(" %s", fa.sarg[i]);
424 >        for (i = 0; i < fa.nsargs; i++) {
425 >                fputc(' ', stdout);
426 >                fputword(fa.sarg[i], stdout);
427 >        }
428          printf("\n");
429   #ifdef  IARGS
430                                          /* integer arguments */
# Line 317 | Line 458 | FILE  *fin;
458          if (xac > xfa && strcmp(xav[xfa], "-i"))
459                  resetarr = 2;
460          printf("%d", fa.nsargs + resetarr + xac-xfa);
461 <        for (i = 0; i < fa.nsargs; i++)
462 <                printf(" %s", fa.sarg[i]);
461 >        for (i = 0; i < fa.nsargs; i++) {
462 >                fputc(' ', stdout);
463 >                fputword(fa.sarg[i], stdout);
464 >        }
465          if (resetarr)
466                  printf(" -i 1");
467          for (i = xfa; i < xac; i++)     /* add xf arguments */
# Line 343 | Line 486 | FILE  *fin;
486   }
487  
488  
346 int
347 otype(ofname)                   /* get object function number from its name */
348 register char  *ofname;
349 {
350        register int  i;
351
352        for (i = 0; i < NUMTYPES; i++)
353                if (!strcmp(ofun[i].funame, ofname))
354                        return(i);
355
356        return(-1);             /* not found */
357 }
358
359
489   alias(fin)                      /* transfer alias */
490   FILE  *fin;
491   {
# Line 415 | Line 544 | FILE  *fin;
544  
545          if (readfargs(&fa, fin) != 1)
546                  return(-1);
547 <        if (fa.nfargs > 5)
547 >        if (fa.nfargs > 7)
548                  return(-1);
549          printf("%d", fa.nsargs);
550          if (idprefix == NULL)
# Line 441 | Line 570 | FILE  *fin;
570          if (fa.nfargs > 2)
571                  printf(" %12.6g %12.6g %12.6g", fa.farg[0]/tot.sca,
572                                  fa.farg[1]/tot.sca, fa.farg[2]/tot.sca);
573 <        if (fa.nfargs > 3)
574 <                printf(" %12.6g", fa.farg[3]);
446 <        if (fa.nfargs > 4)
447 <                printf(" %12.6g", fa.farg[4]);
573 >        for (i = 3; i < fa.nfargs; i++)
574 >                printf(" %12.6g", fa.farg[i]);
575          printf("\n");
576          freefargs(&fa);
577          return(0);
# Line 509 | Line 636 | FILE  *fin;
636                  return(-1);
637                                          /* string arguments */
638          printf("%d", fa.nsargs);
639 <        for (i = 0; i < fa.nsargs; i++)
640 <                printf(" %s", fa.sarg[i]);
639 >        for (i = 0; i < fa.nsargs; i++) {
640 >                fputc(' ', stdout);
641 >                fputword(fa.sarg[i], stdout);
642 >        }
643          printf("\n0\n%d\n", fa.nfargs);
644                                          /* anchor point */
645          multp3(v, fa.farg, tot.xfm);
# Line 693 | Line 822 | initotypes()                   /* initialize ofun[] array */
822  
823          if (ofun[OBJ_SOURCE].funp == o_source)
824                  return;                 /* done already */
696                                        /* alias is additional */
697        ofun[ALIAS].funame = ALIASID;
698        ofun[ALIAS].flags = 0;
825                                          /* functions get new transform */
826 <        for (i = 0; i < NUMTYPES; i++)
826 >        for (i = 0; i < NUMOTYPE; i++)
827                  if (hasfunc(i))
828                          ofun[i].funp = addxform;
829                                          /* special cases */
# Line 710 | Line 836 | initotypes()                   /* initialize ofun[] array */
836          ofun[OBJ_CYLINDER].funp =
837          ofun[OBJ_TUBE].funp = o_cylinder;
838          ofun[OBJ_RING].funp = o_ring;
839 <        ofun[OBJ_INSTANCE].funp = addxform;
839 >        ofun[OBJ_INSTANCE].funp =
840 >        ofun[OBJ_MESH].funp = addxform;
841          ofun[MAT_GLOW].funp = m_glow;
842          ofun[MAT_SPOT].funp = m_spot;
843          ofun[MAT_DIELECTRIC].funp = m_dielectric;
# Line 719 | Line 846 | initotypes()                   /* initialize ofun[] array */
846          ofun[PAT_CTEXT].funp =
847          ofun[PAT_BTEXT].funp =
848          ofun[MIX_TEXT].funp = text;
849 <        ofun[ALIAS].funp = alias;
849 >        ofun[MOD_ALIAS].funp = alias;
850                                          /* surface inverses */
851          tinvers[OBJ_FACE] = OBJ_FACE;
852          tinvers[OBJ_SOURCE] = OBJ_SOURCE;
# Line 758 | Line 885 | char  *fname;
885          strcpy(mainfn, fname);
886   }
887   #else
888 < openmain(fname)         /* open fname for input, changing to its directory */
889 < char  *fname;
888 > openmain(iname)         /* open input, changing directory for file */
889 > char  *iname;
890   {
891 <        extern FILE  *tmpfile();
892 <        extern char  *getlibpath(), *getpath();
893 <        static char  origdir[MAXPATH];
894 <        static char  curdir[MAXPATH];
768 <        char  newdir[MAXPATH], *cp;
891 >        static char  origdir[PATH_MAX];
892 >        static char  curfn[PATH_MAX];
893 >        static int  diffdir;
894 >        register char  *fpath;
895  
896 <        if (fname == NULL) {                    /* standard input */
896 >        if (iname == NULL) {                    /* standard input */
897                  if (mainfp == NULL) {
898                          register int  c;
899                          strcpy(mainfn, "standard input");
# Line 784 | Line 910 | char  *fname;
910                          }
911                          while ((c = getc(stdin)) != EOF)
912                                  putc(c, mainfp);
787                        fclose(stdin);
913                  }
914                  rewind(mainfp);                 /* rewind copy */
915                  return;
916          }
792                                                /* get full path */
793        if ((cp = getpath(fname, getlibpath(), R_OK)) == NULL) {
794                fprintf(stderr, "%s: cannot find file \"%s\"\n",
795                                progname, fname);
796                exit(1);
797        }
798        if (cp[0] == '.' && ISDIRSEP(cp[1]))    /* remove leading ./ */
799                cp += 2;
917          if (mainfp == NULL) {                   /* first call, initialize */
918 <                getwd(origdir);
919 <                strcpy(curdir, origdir);
803 <        } else if (!strcmp(cp, mainfn)) {       /* just need to rewind? */
918 >                getcwd(origdir, sizeof(origdir));
919 >        } else if (!strcmp(iname, curfn)) {     /* just need to rewind? */
920                  rewind(mainfp);
921                  return;
922 <        } else                                  /* else close old stream */
922 >        } else {                                /* else close old stream */
923                  fclose(mainfp);
924 +                mainfp = NULL;
925 +                if (diffdir) {                  /* return to our directory */
926 +                        chdir(origdir);
927 +                        diffdir = 0;
928 +                }
929 +        }
930 +        strcpy(curfn, iname);                   /* remember input name */
931 +                                                /* get full path for file */
932 +        if ((fpath = getpath(iname, getrlibpath(), R_OK)) == NULL) {
933 +                fprintf(stderr, "%s: cannot find file \"%s\"\n",
934 +                                progname, iname);
935 +                exit(1);
936 +        }
937 +        if (fpath[0] == '.' && ISDIRSEP(fpath[1]))      /* remove leading ./ */
938 +                fpath += 2;
939                                                  /* record path name */
940 <        strcpy(mainfn, cp);
941 <        if (expand) {                           /* get directory component */
942 <                if (ISDIRSEP(cp[0]))
943 <                        strcpy(newdir, cp);
944 <                else
945 <                        sprintf(newdir, "%s%c%s", origdir, DIRSEP, cp);
946 <                for (cp = newdir+strlen(newdir); !ISDIRSEP(cp[-1]); cp--)
947 <                        ;
940 >        strcpy(mainfn, fpath);
941 >        if (expand) {                           /* change to local directory */
942 >                register char  *cp = fpath + strlen(fpath);     /* get dir. */
943 >                while (cp > fpath) {
944 >                        cp--;
945 >                        if (ISDIRSEP(*cp)) {
946 >                                if (cp == fpath)
947 >                                        cp++;   /* root special case */
948 >                                break;
949 >                        }
950 >                }
951                  *cp = '\0';
952 <                if (strcmp(newdir, curdir)) {   /* change to new directory? */
953 <                        if (chdir(newdir) < 0) {
952 >                if (fpath[0]) {                 /* change to new directory? */
953 >                        if (chdir(fpath) < 0) {
954                                  fprintf(stderr,
955                                  "%s: cannot change directory to \"%s\"\n",
956 <                                                progname, newdir);
956 >                                                progname, fpath);
957                                  exit(1);
958                          }
959 <                        strcpy(curdir, newdir);
959 >                        diffdir++;
960                  }
961                                                  /* get final path component */
962 <                for (cp = fname+strlen(fname);
963 <                                cp > fname && !ISDIRSEP(cp[-1]); cp--)
962 >                for (fpath = iname+strlen(iname);
963 >                                fpath > iname && !ISDIRSEP(fpath[-1]); fpath--)
964                          ;
965          }
966 <                                                /* open the file */
967 <        if ((mainfp = fopen(cp, "r")) == NULL) {
966 >                                                /* finally, open the file */
967 >        if ((mainfp = fopen(fpath, "r")) == NULL) {
968                  fprintf(stderr, "%s: cannot open file \"%s\"\n",
969                                  progname, mainfn);
970                  exit(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines