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.29 by schorsch, Thu Jul 3 22:41:44 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 + #ifndef _WIN32
14 +  #include  <unistd.h>
15 + #endif
16  
17 + #include  "standard.h"
18 + #include  "platform.h"
19 + #include  "paths.h"
20   #include  "object.h"
22
21   #include  "otypes.h"
22  
23   int  xac;                               /* global xform argument count */
# Line 29 | Line 27 | int  xfa;                              /* start of xf arguments */
27   XF  tot;                                /* total transformation */
28   int  reverse;                           /* boolean true if scene mirrored */
29  
30 < int  invert = 0;                        /* boolean true to invert surfaces */
30 > int  invert;                            /* boolean true to invert surfaces */
31  
32 < int  expand = 0;                        /* boolean true to expand commands */
32 > int  expand;                            /* boolean true to expand commands */
33  
34 < char  *newmod = NULL;                   /* new modifier for surfaces */
34 > char  *newmod;                          /* new modifier for surfaces */
35  
36 < char  *idprefix = NULL;                 /* prefix for object identifiers */
36 > char  *idprefix;                        /* prefix for object identifiers */
37  
38 < #define  ALIAS          NUMOTYPE        /* put alias at end of array */
38 > FUN  ofun[NUMOTYPE] = INIT_OTYPE;       /* default types and actions */
39  
42 #define  NUMTYPES       (NUMOTYPE+1)    /* total number of object types */
43
44 FUN  ofun[NUMTYPES] = INIT_OTYPE;       /* default types and actions */
45
40   short  tinvers[NUMOTYPE];               /* inverse types for surfaces */
41  
42   int  nrept = 1;                         /* number of array repetitions */
43  
44 < extern char  *malloc(), *fgetword();
44 > int stdinused = 0;                      /* stdin has been used by -f option? */
45  
46 < char  *mainfn[MAXPATH];                 /* main file name */
46 > char  mainfn[PATH_MAX];                 /* main file name */
47   FILE  *mainfp = NULL;                   /* main file pointer */
48  
49   #define  progname  (xav[0])
# Line 59 | Line 53 | main(argc, argv)               /* get transform options and transfo
53   int  argc;
54   char  *argv[];
55   {
56 +        int  mal_prefix = 0;
57          char  *fname;
58          int  a;
59 <                                        /* check for array */
59 >                                        /* check for argument list file */
60          for (a = 1; a < argc; a++)
61 +                if (!strcmp(argv[a], "-f"))
62 +                        return(doargf(argc, argv, a));
63 +                                        /* check for regular array */
64 +        for (a = 1; a < argc; a++)
65                  if (!strcmp(argv[a], "-a"))
66                          return(doarray(argc, argv, a));
67  
68 <        initotypes();
68 >        initotypes();                   /* initialize */
69 >        invert = 0;
70 >        expand = 1;
71 >        newmod = NULL;
72 >        idprefix = NULL;
73  
74          for (a = 1; a < argc; a++) {
75                  if (argv[a][0] == '-')
76                          switch (argv[a][1]) {
77                          case 'm':
78 <                                if (argv[a][2] || a+1 >= argc)
78 >                                if (argv[a][2] | a+1 >= argc)
79                                          break;
80 <                                newmod = argv[++a];
80 >                                a++;
81 >                                if (newmod == NULL)
82 >                                        newmod = argv[a];
83                                  continue;
84                          case 'n':
85 <                                if (argv[a][2] || a+1 >= argc)
85 >                                if (argv[a][2] | a+1 >= argc)
86                                          break;
87 <                                idprefix = argv[++a];
87 >                                a++;
88 >                                if (idprefix == NULL)
89 >                                        idprefix = argv[a];
90 >                                else {
91 >                                        register char   *newp;
92 >                                        newp = (char *)malloc(strlen(idprefix)+
93 >                                                        strlen(argv[a])+2);
94 >                                        if (newp == NULL)
95 >                                                exit(2);
96 >                                        sprintf(newp, "%s.%s",
97 >                                                        idprefix, argv[a]);
98 >                                        if (mal_prefix++)
99 >                                                free((void *)idprefix);
100 >                                        idprefix = newp;
101 >                                }
102                                  continue;
103 +                        case 'c':
104 +                                if (argv[a][2])
105 +                                        break;
106 +                                expand = 0;
107 +                                continue;
108                          case 'e':
109                                  if (argv[a][2])
110                                          break;
# Line 89 | Line 113 | char  *argv[];
113                          case 'I':
114                                  if (argv[a][2])
115                                          break;
116 <                                invert = 1;
116 >                                invert = !invert;
117                                  continue;
118                          }
119                  break;
# Line 119 | Line 143 | char  *argv[];
143          putchar('\n');
144                                          /* transform input */
145          if (xac == argc) {
146 +                if (stdinused) {
147 +                        fprintf(stderr, "%s: cannot use stdin more than once\n",
148 +                                        argv[0]);
149 +                        exit(1);
150 +                }
151                  openmain(NULL);
152                  xform(mainfn, mainfp);
153          } else
# Line 127 | Line 156 | char  *argv[];
156                          xform(mainfn, mainfp);
157                  }
158  
159 +        if (mal_prefix)
160 +                free((void *)idprefix);
161          return(0);
162   }
163  
164  
165 + doargf(ac, av, fi)                      /* take argument list from file */
166 + char  **av;
167 + int  ac, fi;
168 + {
169 +        int  inquote;
170 +        char  *newav[256], **avp;
171 +        char  argbuf[1024];
172 +        char  newid[128];
173 +        char  *oldid;
174 +        register char   *cp;
175 +        FILE    *argfp;
176 +        int  n, i, k, newac, err;
177 +        
178 +        if (fi >= ac-1 || (av[fi+1][0] == '-' && av[fi+1][1] != '\0')) {
179 +                fprintf(stderr, "%s: missing file for -f option\n", av[0]);
180 +                exit(1);
181 +        }
182 +        if (av[fi+1][0] == '-' && av[fi+1][1] == '\0') {
183 +                if (stdinused++) {
184 +                        fprintf(stderr,
185 +                                "%s: cannot use stdin more than once\n",
186 +                                        av[0]);
187 +                        exit(1);
188 +                }
189 +                argfp = stdin;
190 +                n = 100;                /* we just don't know! */
191 +        } else {
192 +                if ((argfp = fopen(av[fi+1], "r")) == NULL) {
193 +                        fprintf(stderr,
194 +                                "%s: cannot open argument file \"%s\"\n",
195 +                                        av[0], av[fi+1]);
196 +                        exit(1);
197 +                }
198 +                n = 0;                  /* count number of lines in file */
199 +                while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL)
200 +                        n += argbuf[0] && argbuf[0] != '#';
201 +                if (!n) {
202 +                        fprintf(stderr, "%s: empty argument file \"%s\"\n",
203 +                                        av[0], av[fi+1]);
204 +                        exit(1);
205 +                }
206 +                nrept *= n;
207 +                rewind(argfp);
208 +        }
209 +        err = 0; k = 0;                 /* read each arg list and call main */
210 +        while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL) {
211 +                if (!argbuf[0] || argbuf[0] == '#')
212 +                        continue;
213 +                avp = newav+2;
214 +                avp[0] = av[0];
215 +                for (i = 1; i < fi; i++)
216 +                        avp[i] = av[i];
217 +                newac = i;
218 +                cp = argbuf;            /* parse new words */
219 +                if (*cp == '!') cp++;
220 +                if (!strncmp(cp, "xform ", 6)) cp += 6;
221 +                inquote = 0;
222 +                for ( ; ; ) {
223 +                skipspaces:
224 +                        while (isspace(*cp))    /* nullify spaces */
225 +                                *cp++ = '\0';
226 +                        if ((*cp == '"' | *cp == '\''))
227 +                                inquote = *cp++;
228 +                        if (!*cp)               /* all done? */
229 +                                break;
230 +                        if (cp[0] == '\\' && cp[1])
231 +                                if (*++cp == '\n')
232 +                                        goto skipspaces;
233 +                        avp[newac++] = cp;      /* add argument to list */
234 +                        if (inquote) {
235 +                                while (*++cp)
236 +                                        if (*cp == inquote) {
237 +                                                *cp++ = '\0';
238 +                                                break;
239 +                                        }
240 +                        } else {
241 +                                while (*++cp && !isspace(*cp))
242 +                                        ;
243 +                        }
244 +                }
245 +                for (i = fi+2; i < ac; i++)
246 +                        avp[newac++] = av[i];
247 +                avp[newac] = NULL;
248 +                oldid = NULL;
249 +                for (i = 2; i < newac; i++)
250 +                        if (!strcmp(avp[i-1], "-n")) {
251 +                                oldid = avp[i];
252 +                                avp[i] = newid;
253 +                                break;
254 +                        }
255 +                if (oldid == NULL) {
256 +                        newav[0] = av[0];
257 +                        newav[1] = "-n";
258 +                        newav[2] = newid;
259 +                        avp = newav;
260 +                        newac += 2;
261 +                }
262 +                if (oldid == NULL)
263 +                        sprintf(newid, "i%d", k);
264 +                else
265 +                        sprintf(newid, "%s.%d", oldid, k);
266 +                err |= main(newac, avp);
267 +                k++;
268 +        }
269 +        fclose(argfp);
270 +        return(err);
271 + }
272 +
273 +
274   doarray(ac, av, ai)                     /* make array */
275   char  **av;
276   int  ac, ai;
# Line 140 | Line 280 | int  ac, ai;
280          char  *oldid = NULL;
281          int  n, i, err;
282          
283 +        if (ai >= ac-1 || (n = atoi(av[ai+1])) <= 0) {
284 +                fprintf(stderr, "%s: missing count for -a option\n", av[0]);
285 +                exit(1);
286 +        }
287 +        nrept *= n;
288          avp = newav+2;
289          avp[0] = av[0];
290          for (i = 1; i < ac; i++)
# Line 158 | Line 303 | int  ac, ai;
303                  avp = newav;
304                  ac += 2;
305          }
161        nrept *= n = atoi(av[ai+1]);
306          err = 0;
307          for (i = 0; i < n; i++) {
308                  if (oldid == NULL)
# Line 209 | Line 353 | xfcomm(fname, fin)                     /* transform a command */
353   char  *fname;
354   FILE  *fin;
355   {
212        extern FILE  *popen();
213        extern char  *fgetline();
356          FILE  *pin;
357          char  buf[512];
358          int  i;
# Line 227 | Line 369 | FILE  *fin;
369          } else {
370                  printf("\n%s", buf);
371                  if (xac > 1) {
372 <                        printf(" | %s -e", xav[0]);
372 >                        printf(" | %s -e", xav[0]);     /* expand next time */
373                          for (i = 1; i < xac; i++)
374 <                                printf(" %s", xav[i]);
374 >                                if (i >= xfa || strcmp(xav[i], "-c"))
375 >                                        printf(" %s", xav[i]);
376                  }
377                  putchar('\n');
378          }
# Line 240 | Line 383 | xfobject(fname, fin)                           /* transform an object */
383   char  *fname;
384   FILE  *fin;
385   {
243        extern char  *strcpy();
386          char  typ[16], nam[MAXSTR];
387          int  fn;
388                                                  /* modifier and type */
# Line 282 | Line 424 | FILE  *fin;
424                  return(-1);
425                                          /* string arguments */
426          printf("%d", fa.nsargs);
427 <        for (i = 0; i < fa.nsargs; i++)
428 <                printf(" %s", fa.sarg[i]);
427 >        for (i = 0; i < fa.nsargs; i++) {
428 >                fputc(' ', stdout);
429 >                fputword(fa.sarg[i], stdout);
430 >        }
431          printf("\n");
432   #ifdef  IARGS
433                                          /* integer arguments */
# Line 317 | Line 461 | FILE  *fin;
461          if (xac > xfa && strcmp(xav[xfa], "-i"))
462                  resetarr = 2;
463          printf("%d", fa.nsargs + resetarr + xac-xfa);
464 <        for (i = 0; i < fa.nsargs; i++)
465 <                printf(" %s", fa.sarg[i]);
464 >        for (i = 0; i < fa.nsargs; i++) {
465 >                fputc(' ', stdout);
466 >                fputword(fa.sarg[i], stdout);
467 >        }
468          if (resetarr)
469                  printf(" -i 1");
470          for (i = xfa; i < xac; i++)     /* add xf arguments */
# Line 343 | Line 489 | FILE  *fin;
489   }
490  
491  
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
492   alias(fin)                      /* transfer alias */
493   FILE  *fin;
494   {
# Line 415 | Line 547 | FILE  *fin;
547  
548          if (readfargs(&fa, fin) != 1)
549                  return(-1);
550 <        if (fa.nfargs > 5)
550 >        if (fa.nfargs > 7)
551                  return(-1);
552          printf("%d", fa.nsargs);
553          if (idprefix == NULL)
# Line 441 | Line 573 | FILE  *fin;
573          if (fa.nfargs > 2)
574                  printf(" %12.6g %12.6g %12.6g", fa.farg[0]/tot.sca,
575                                  fa.farg[1]/tot.sca, fa.farg[2]/tot.sca);
576 <        if (fa.nfargs > 3)
577 <                printf(" %12.6g", fa.farg[3]);
446 <        if (fa.nfargs > 4)
447 <                printf(" %12.6g", fa.farg[4]);
576 >        for (i = 3; i < fa.nfargs; i++)
577 >                printf(" %12.6g", fa.farg[i]);
578          printf("\n");
579          freefargs(&fa);
580          return(0);
# Line 509 | Line 639 | FILE  *fin;
639                  return(-1);
640                                          /* string arguments */
641          printf("%d", fa.nsargs);
642 <        for (i = 0; i < fa.nsargs; i++)
643 <                printf(" %s", fa.sarg[i]);
642 >        for (i = 0; i < fa.nsargs; i++) {
643 >                fputc(' ', stdout);
644 >                fputword(fa.sarg[i], stdout);
645 >        }
646          printf("\n0\n%d\n", fa.nfargs);
647                                          /* anchor point */
648          multp3(v, fa.farg, tot.xfm);
# Line 693 | Line 825 | initotypes()                   /* initialize ofun[] array */
825  
826          if (ofun[OBJ_SOURCE].funp == o_source)
827                  return;                 /* done already */
696                                        /* alias is additional */
697        ofun[ALIAS].funame = ALIASID;
698        ofun[ALIAS].flags = 0;
828                                          /* functions get new transform */
829 <        for (i = 0; i < NUMTYPES; i++)
829 >        for (i = 0; i < NUMOTYPE; i++)
830                  if (hasfunc(i))
831                          ofun[i].funp = addxform;
832                                          /* special cases */
# Line 719 | Line 848 | initotypes()                   /* initialize ofun[] array */
848          ofun[PAT_CTEXT].funp =
849          ofun[PAT_BTEXT].funp =
850          ofun[MIX_TEXT].funp = text;
851 <        ofun[ALIAS].funp = alias;
851 >        ofun[MOD_ALIAS].funp = alias;
852                                          /* surface inverses */
853          tinvers[OBJ_FACE] = OBJ_FACE;
854          tinvers[OBJ_SOURCE] = OBJ_SOURCE;
# Line 758 | Line 887 | char  *fname;
887          strcpy(mainfn, fname);
888   }
889   #else
890 < openmain(fname)         /* open fname for input, changing to its directory */
891 < char  *fname;
890 > openmain(iname)         /* open input, changing directory for file */
891 > char  *iname;
892   {
893 <        extern FILE  *tmpfile();
894 <        extern char  *getlibpath(), *getpath();
895 <        static char  origdir[MAXPATH];
896 <        static char  curdir[MAXPATH];
768 <        char  newdir[MAXPATH], *cp;
893 >        static char  origdir[PATH_MAX];
894 >        static char  curfn[PATH_MAX];
895 >        static int  diffdir;
896 >        register char  *fpath;
897  
898 <        if (fname == NULL) {                    /* standard input */
898 >        if (iname == NULL) {                    /* standard input */
899                  if (mainfp == NULL) {
900                          register int  c;
901                          strcpy(mainfn, "standard input");
# Line 784 | Line 912 | char  *fname;
912                          }
913                          while ((c = getc(stdin)) != EOF)
914                                  putc(c, mainfp);
787                        fclose(stdin);
915                  }
916                  rewind(mainfp);                 /* rewind copy */
917                  return;
918          }
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;
919          if (mainfp == NULL) {                   /* first call, initialize */
920 <                getwd(origdir);
921 <                strcpy(curdir, origdir);
803 <        } else if (!strcmp(cp, mainfn)) {       /* just need to rewind? */
920 >                getcwd(origdir, sizeof(origdir));
921 >        } else if (!strcmp(iname, curfn)) {     /* just need to rewind? */
922                  rewind(mainfp);
923                  return;
924 <        } else                                  /* else close old stream */
924 >        } else {                                /* else close old stream */
925                  fclose(mainfp);
926 +                mainfp = NULL;
927 +                if (diffdir) {                  /* return to our directory */
928 +                        chdir(origdir);
929 +                        diffdir = 0;
930 +                }
931 +        }
932 +        strcpy(curfn, iname);                   /* remember input name */
933 +                                                /* get full path for file */
934 +        if ((fpath = getpath(iname, getrlibpath(), R_OK)) == NULL) {
935 +                fprintf(stderr, "%s: cannot find file \"%s\"\n",
936 +                                progname, iname);
937 +                exit(1);
938 +        }
939 +        if (fpath[0] == '.' && ISDIRSEP(fpath[1]))      /* remove leading ./ */
940 +                fpath += 2;
941                                                  /* record path name */
942 <        strcpy(mainfn, cp);
943 <        if (expand) {                           /* get directory component */
944 <                if (ISDIRSEP(cp[0]))
945 <                        strcpy(newdir, cp);
946 <                else
947 <                        sprintf(newdir, "%s%c%s", origdir, DIRSEP, cp);
948 <                for (cp = newdir+strlen(newdir); !ISDIRSEP(cp[-1]); cp--)
949 <                        ;
942 >        strcpy(mainfn, fpath);
943 >        if (expand) {                           /* change to local directory */
944 >                register char  *cp = fpath + strlen(fpath);     /* get dir. */
945 >                while (cp > fpath) {
946 >                        cp--;
947 >                        if (ISDIRSEP(*cp)) {
948 >                                if (cp == fpath)
949 >                                        cp++;   /* root special case */
950 >                                break;
951 >                        }
952 >                }
953                  *cp = '\0';
954 <                if (strcmp(newdir, curdir)) {   /* change to new directory? */
955 <                        if (chdir(newdir) < 0) {
954 >                if (fpath[0]) {                 /* change to new directory? */
955 >                        if (chdir(fpath) < 0) {
956                                  fprintf(stderr,
957                                  "%s: cannot change directory to \"%s\"\n",
958 <                                                progname, newdir);
958 >                                                progname, fpath);
959                                  exit(1);
960                          }
961 <                        strcpy(curdir, newdir);
961 >                        diffdir++;
962                  }
963                                                  /* get final path component */
964 <                for (cp = fname+strlen(fname);
965 <                                cp > fname && !ISDIRSEP(cp[-1]); cp--)
964 >                for (fpath = iname+strlen(iname);
965 >                                fpath > iname && !ISDIRSEP(fpath[-1]); fpath--)
966                          ;
967          }
968 <                                                /* open the file */
969 <        if ((mainfp = fopen(cp, "r")) == NULL) {
968 >                                                /* finally, open the file */
969 >        if ((mainfp = fopen(fpath, "r")) == NULL) {
970                  fprintf(stderr, "%s: cannot open file \"%s\"\n",
971                                  progname, mainfn);
972                  exit(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines