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.48 by greg, Wed Jul 9 23:14:58 2014 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  "platform.h"
15 + #include  "paths.h"
16 + #include  "rtprocess.h" /* win_popen() */
17 + #include  "rtio.h"
18 + #include  "rtmath.h"
19   #include  "object.h"
22
20   #include  "otypes.h"
21  
22 + #ifdef getc_unlocked            /* avoid horrendous overhead of flockfile */
23 + #undef getc
24 + #define getc    getc_unlocked
25 + #endif
26 +
27   int  xac;                               /* global xform argument count */
28   char  **xav;                            /* global xform argument pointer */
29   int  xfa;                               /* start of xf arguments */
# Line 29 | Line 31 | int  xfa;                              /* start of xf arguments */
31   XF  tot;                                /* total transformation */
32   int  reverse;                           /* boolean true if scene mirrored */
33  
34 < int  invert = 0;                        /* boolean true to invert surfaces */
34 > int  invert;                            /* boolean true to invert surfaces */
35  
36 < int  expand = 0;                        /* boolean true to expand commands */
36 > int  expand;                            /* boolean true to expand commands */
37  
38 < char  *newmod = NULL;                   /* new modifier for surfaces */
38 > char  *newmod;                          /* new modifier for surfaces */
39  
40 < char  *idprefix = NULL;                 /* prefix for object identifiers */
40 > char  *idprefix;                        /* prefix for object identifiers */
41  
42 < #define  ALIAS          NUMOTYPE        /* put alias at end of array */
42 > FUN  ofun[NUMOTYPE] = INIT_OTYPE;       /* default types and actions */
43  
42 #define  NUMTYPES       (NUMOTYPE+1)    /* total number of object types */
43
44 FUN  ofun[NUMTYPES] = INIT_OTYPE;       /* default types and actions */
45
44   short  tinvers[NUMOTYPE];               /* inverse types for surfaces */
45  
46   int  nrept = 1;                         /* number of array repetitions */
47  
48 < extern char  *malloc(), *fgetword();
48 > int stdinused = 0;                      /* stdin has been used by -f option? */
49  
50 < char  *mainfn[MAXPATH];                 /* main file name */
50 > char  mainfn[PATH_MAX];                 /* main file name */
51   FILE  *mainfp = NULL;                   /* main file pointer */
52  
53   #define  progname  (xav[0])
54  
55 + static int doargf(int ac, char **av, int fi);
56 + static int doarray(int ac, char **av, int ai);
57 + static void xform(char *name, FILE *fin);
58 + static void xfcomm(char *fname, FILE *fin);
59 + static void xfobject(char *fname, FILE *fin);
60 + static int addxform(FILE *fin);
61 + static int alias(FILE *fin);
62 + void initotypes(void); /* XXX conflict with otypes.h */
63 + static void openmain(char *iname);
64  
65 < main(argc, argv)                /* get transform options and transform file */
66 < int  argc;
67 < char  *argv[];
65 >
66 > int
67 > main(           /* get transform options and transform file */
68 >        int  argc,
69 >        char  *argv[]
70 > )
71   {
72 <        char  *fname;
72 >        int  mal_prefix = 0;
73          int  a;
74 <                                        /* check for array */
74 >                                        /* check for argument list file */
75          for (a = 1; a < argc; a++)
76 +                if (!strcmp(argv[a], "-f"))
77 +                        return(doargf(argc, argv, a));
78 +                                        /* check for regular array */
79 +        for (a = 1; a < argc; a++)
80                  if (!strcmp(argv[a], "-a"))
81                          return(doarray(argc, argv, a));
82  
83 <        initotypes();
83 >        initotypes();                   /* initialize */
84 >        invert = 0;
85 >        expand = 1;
86 >        newmod = NULL;
87 >        idprefix = NULL;
88  
89          for (a = 1; a < argc; a++) {
90                  if (argv[a][0] == '-')
91                          switch (argv[a][1]) {
92                          case 'm':
93 <                                if (argv[a][2] || a+1 >= argc)
93 >                                if (argv[a][2] | (a+1 >= argc))
94                                          break;
95 <                                newmod = argv[++a];
95 >                                a++;
96 >                                if (newmod == NULL)
97 >                                        newmod = argv[a];
98                                  continue;
99                          case 'n':
100 <                                if (argv[a][2] || a+1 >= argc)
100 >                                if (argv[a][2] | (a+1 >= argc))
101                                          break;
102 <                                idprefix = argv[++a];
102 >                                a++;
103 >                                if (idprefix == NULL)
104 >                                        idprefix = argv[a];
105 >                                else {
106 >                                        char    *newp;
107 >                                        newp = (char *)malloc(strlen(idprefix)+
108 >                                                        strlen(argv[a])+2);
109 >                                        if (newp == NULL)
110 >                                                exit(2);
111 >                                        sprintf(newp, "%s.%s",
112 >                                                        idprefix, argv[a]);
113 >                                        if (mal_prefix++)
114 >                                                free(idprefix);
115 >                                        idprefix = newp;
116 >                                }
117                                  continue;
118 +                        case 'c':
119 +                                if (argv[a][2])
120 +                                        break;
121 +                                expand = 0;
122 +                                continue;
123                          case 'e':
124                                  if (argv[a][2])
125                                          break;
# Line 89 | Line 128 | char  *argv[];
128                          case 'I':
129                                  if (argv[a][2])
130                                          break;
131 <                                invert = 1;
131 >                                invert = !invert;
132                                  continue;
133                          }
134                  break;
# Line 100 | Line 139 | char  *argv[];
139  
140          a += xf(&tot, argc-a, argv+a);
141  
142 <        if (reverse = tot.sca < 0.0)
142 >        if ( (reverse = (tot.sca < 0.0)) )
143                  tot.sca = -tot.sca;
144          if (invert)
145                  reverse = !reverse;
# Line 119 | Line 158 | char  *argv[];
158          putchar('\n');
159                                          /* transform input */
160          if (xac == argc) {
161 +                if (stdinused) {
162 +                        fprintf(stderr, "%s: cannot use stdin more than once\n",
163 +                                        argv[0]);
164 +                        exit(1);
165 +                }
166                  openmain(NULL);
167                  xform(mainfn, mainfp);
168          } else
# Line 127 | Line 171 | char  *argv[];
171                          xform(mainfn, mainfp);
172                  }
173  
174 +        if (mal_prefix)
175 +                free(idprefix);
176          return(0);
177   }
178  
179  
180 < doarray(ac, av, ai)                     /* make array */
181 < char  **av;
182 < int  ac, ai;
180 > int
181 > doargf(                 /* take argument list from file */
182 >        int ac,
183 >        char  **av,
184 >        int fi
185 > )
186   {
187 +        int  inquote;
188          char  *newav[256], **avp;
189 +        char  argbuf[2048];
190 +        char  *newid, newidbuf[128];
191 +        char  *oldid;
192 +        char    *cp;
193 +        FILE    *argfp;
194 +        int  n, i, k, newac, err;
195 +        
196 +        if (fi >= ac-1 || (av[fi+1][0] == '-' && av[fi+1][1] != '\0')) {
197 +                fprintf(stderr, "%s: missing file for -f option\n", av[0]);
198 +                exit(1);
199 +        }
200 +        if (av[fi+1][0] == '-' && av[fi+1][1] == '\0') {
201 +                if (stdinused++) {
202 +                        fprintf(stderr,
203 +                                "%s: cannot use stdin more than once\n",
204 +                                        av[0]);
205 +                        exit(1);
206 +                }
207 +                argfp = stdin;
208 +                n = 100;                /* we just don't know! */
209 +        } else {
210 +                if ((argfp = fopen(av[fi+1], "r")) == NULL) {
211 +                        fprintf(stderr,
212 +                                "%s: cannot open argument file \"%s\"\n",
213 +                                        av[0], av[fi+1]);
214 +                        exit(1);
215 +                }
216 +                n = 0;                  /* count number of lines in file */
217 +                while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL)
218 +                        n += argbuf[0] && argbuf[0] != '#';
219 +                if (!n) {
220 +                        fprintf(stderr, "%s: empty argument file \"%s\"\n",
221 +                                        av[0], av[fi+1]);
222 +                        exit(1);
223 +                }
224 +                nrept *= n;
225 +                rewind(argfp);
226 +        }
227 +        err = 0; k = 0;                 /* read each arg list and call main */
228 +        while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL) {
229 +                if (!argbuf[0] || argbuf[0] == '#')
230 +                        continue;
231 +                avp = newav+2;
232 +                avp[0] = av[0];
233 +                for (i = 1; i < fi; i++)
234 +                        avp[i] = av[i];
235 +                newac = i;
236 +                cp = argbuf;            /* parse new words */
237 +                if (*cp == '!') cp++;
238 +                if (!strncmp(cp, "xform ", 6)) cp += 6;
239 +                inquote = 0;
240 +                for ( ; ; ) {
241 +                skipspaces:
242 +                        while (isspace(*cp))    /* nullify spaces */
243 +                                *cp++ = '\0';
244 +                        if ((*cp == '"') | (*cp == '\''))
245 +                                inquote = *cp++;
246 +                        if (!*cp)               /* all done? */
247 +                                break;
248 +                        if (cp[0] == '\\' && cp[1])
249 +                                if (*++cp == '\n')
250 +                                        goto skipspaces;
251 +                        avp[newac++] = cp;      /* add argument to list */
252 +                        if (inquote) {
253 +                                while (*++cp)
254 +                                        if (*cp == inquote) {
255 +                                                *cp++ = '\0';
256 +                                                break;
257 +                                        }
258 +                        } else {
259 +                                while (*++cp && !isspace(*cp))
260 +                                        ;
261 +                        }
262 +                }
263 +                for (i = fi+2; i < ac; i++)
264 +                        avp[newac++] = av[i];
265 +                avp[newac] = NULL;
266 +                newid = newidbuf;
267 +                oldid = NULL;
268 +                for (i = 2; i < newac; i++)
269 +                        if (!strcmp(avp[i-1], "-n")) {
270 +                                oldid = avp[i];
271 +                                if (strlen(oldid)+32 > sizeof(newidbuf)) {
272 +                                        newid = (char *)malloc(strlen(oldid)+32);
273 +                                        if (newid == NULL)
274 +                                                exit(2);
275 +                                }
276 +                                avp[i] = newid;
277 +                                break;
278 +                        }
279 +                if (oldid == NULL) {
280 +                        newav[0] = av[0];
281 +                        newav[1] = "-n";
282 +                        newav[2] = newid;
283 +                        avp = newav;
284 +                        newac += 2;
285 +                }
286 +                if (oldid == NULL)
287 +                        sprintf(newid, "i%d", k);
288 +                else
289 +                        sprintf(newid, "%s.%d", oldid, k);
290 +                err |= main(newac, avp);
291 +                if (newid != newidbuf)
292 +                        free(newid);
293 +                k++;
294 +        }
295 +        fclose(argfp);
296 +        return(err);
297 + }
298 +
299 +
300 + int
301 + doarray(                        /* make array */
302 +        int  ac,
303 +        char  **av,
304 +        int  ai
305 + )
306 + {
307 +        char  *newav[256], **avp;
308          char  newid[128], repts[32];
309          char  *oldid = NULL;
310          int  n, i, err;
311          
312 +        if (ai >= ac-1 || (n = atoi(av[ai+1])) <= 0) {
313 +                fprintf(stderr, "%s: missing count for -a option\n", av[0]);
314 +                exit(1);
315 +        }
316 +        nrept *= n;
317          avp = newav+2;
318          avp[0] = av[0];
319          for (i = 1; i < ac; i++)
# Line 158 | Line 332 | int  ac, ai;
332                  avp = newav;
333                  ac += 2;
334          }
161        nrept *= n = atoi(av[ai+1]);
335          err = 0;
336          for (i = 0; i < n; i++) {
337                  if (oldid == NULL)
# Line 172 | Line 345 | int  ac, ai;
345   }
346  
347  
348 < xform(name, fin)                        /* transform stream by tot.xfm */
349 < char  *name;
350 < register FILE  *fin;
348 > void
349 > xform(                  /* transform stream by tot.xfm */
350 >        char  *name,
351 >        FILE  *fin
352 > )
353   {
354          int  nobjs = 0;
355 <        register int  c;
355 >        int  c;
356  
357          while ((c = getc(fin)) != EOF) {
358                  if (isspace(c))                         /* blank */
# Line 205 | Line 380 | register FILE  *fin;
380   }
381  
382  
383 < xfcomm(fname, fin)                      /* transform a command */
384 < char  *fname;
385 < FILE  *fin;
383 > void
384 > xfcomm(                 /* transform a command */
385 >        char  *fname,
386 >        FILE  *fin
387 > )
388   {
212        extern FILE  *popen();
213        extern char  *fgetline();
389          FILE  *pin;
390 <        char  buf[512];
390 >        char  buf[2048];
391          int  i;
392  
393          fgetline(buf, sizeof(buf), fin);
# Line 227 | Line 402 | FILE  *fin;
402          } else {
403                  printf("\n%s", buf);
404                  if (xac > 1) {
405 <                        printf(" | %s -e", xav[0]);
405 >                        printf(" | %s -e", xav[0]);     /* expand next time */
406                          for (i = 1; i < xac; i++)
407 <                                printf(" %s", xav[i]);
407 >                                if (i >= xfa || strcmp(xav[i], "-c"))
408 >                                        printf(" %s", xav[i]);
409                  }
410                  putchar('\n');
411          }
412   }
413  
414  
415 < xfobject(fname, fin)                            /* transform an object */
416 < char  *fname;
417 < FILE  *fin;
415 > void
416 > xfobject(                               /* transform an object */
417 >        char  *fname,
418 >        FILE  *fin
419 > )
420   {
243        extern char  *strcpy();
421          char  typ[16], nam[MAXSTR];
422          int  fn;
423                                                  /* modifier and type */
# Line 252 | Line 429 | FILE  *fin;
429                                  progname, fname, typ);
430                  exit(1);
431          }
432 <        if (ismodifier(fn))
433 <                printf("\n%s %s ", nam, typ);
434 <        else
435 <                printf("\n%s %s ", newmod != NULL ? newmod : nam,
436 <                                invert ? ofun[tinvers[fn]].funame : typ);
432 >        putchar('\n');
433 >        if (ismodifier(fn)) {
434 >                fputword(nam, stdout);
435 >                printf(" %s ", typ);
436 >        } else {
437 >                fputword(newmod != NULL ? newmod : nam, stdout);
438 >                printf(" %s ", invert ? ofun[tinvers[fn]].funame : typ);
439 >        }
440                                                  /* object name */
441          fgetword(nam, sizeof(nam), fin);
442          if (idprefix == NULL || ismodifier(fn))
443 <                printf("%s\n", nam);
444 <        else
445 <                printf("%s.%s\n", idprefix, nam);
443 >                fputword(nam, stdout);
444 >        else {
445 >                char    nnam[MAXSTR];
446 >                sprintf(nnam, "%s.%s", idprefix, nam);
447 >                fputword(nnam, stdout);
448 >        }
449 >        putchar('\n');
450                                                  /* transform arguments */
451          if ((*ofun[fn].funp)(fin) < 0) {
452                  fprintf(stderr, "%s: (%s): bad %s \"%s\"\n",
# Line 272 | Line 456 | FILE  *fin;
456   }
457  
458  
459 < o_default(fin)                  /* pass on arguments unchanged */
460 < FILE  *fin;
459 > int
460 > o_default(                      /* pass on arguments unchanged */
461 >        FILE  *fin
462 > )
463   {
464 <        register int  i;
464 >        int  i;
465          FUNARGS  fa;
466  
467          if (readfargs(&fa, fin) != 1)
468                  return(-1);
469                                          /* string arguments */
470          printf("%d", fa.nsargs);
471 <        for (i = 0; i < fa.nsargs; i++)
472 <                printf(" %s", fa.sarg[i]);
471 >        for (i = 0; i < fa.nsargs; i++) {
472 >                fputc(' ', stdout);
473 >                fputword(fa.sarg[i], stdout);
474 >        }
475          printf("\n");
476   #ifdef  IARGS
477                                          /* integer arguments */
# Line 304 | Line 492 | FILE  *fin;
492   }
493  
494  
495 < addxform(fin)                   /* add xf arguments to strings */
496 < FILE  *fin;
495 > int
496 > addxform(                       /* add xf arguments to strings */
497 >        FILE  *fin
498 > )
499   {
500 <        register int  i;
500 >        int  i;
501          int  resetarr = 0;
502          FUNARGS  fa;
503  
# Line 317 | Line 507 | FILE  *fin;
507          if (xac > xfa && strcmp(xav[xfa], "-i"))
508                  resetarr = 2;
509          printf("%d", fa.nsargs + resetarr + xac-xfa);
510 <        for (i = 0; i < fa.nsargs; i++)
511 <                printf(" %s", fa.sarg[i]);
510 >        for (i = 0; i < fa.nsargs; i++) {
511 >                fputc(' ', stdout);
512 >                fputword(fa.sarg[i], stdout);
513 >        }
514          if (resetarr)
515                  printf(" -i 1");
516          for (i = xfa; i < xac; i++)     /* add xf arguments */
# Line 344 | Line 536 | FILE  *fin;
536  
537  
538   int
539 < otype(ofname)                   /* get object function number from its name */
540 < register char  *ofname;
539 > alias(                  /* transfer alias */
540 >        FILE  *fin
541 > )
542   {
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
360 alias(fin)                      /* transfer alias */
361 FILE  *fin;
362 {
543          char  aliasnm[MAXSTR];
544  
545          if (fgetword(aliasnm, MAXSTR, fin) == NULL)
# Line 369 | Line 549 | FILE  *fin;
549   }
550  
551  
552 < m_glow(fin)                     /* transform arguments for proximity light */
553 < FILE  *fin;
552 > int
553 > m_glow(                 /* transform arguments for proximity light */
554 >        FILE  *fin
555 > )
556   {
557          FUNARGS  fa;
558  
# Line 387 | Line 569 | FILE  *fin;
569   }
570  
571  
572 < m_spot(fin)                     /* transform arguments for spotlight */
573 < FILE  *fin;
572 > int
573 > m_spot(                 /* transform arguments for spotlight */
574 >        FILE  *fin
575 > )
576   {
577          FVECT  v;
578          FUNARGS  fa;
# Line 407 | Line 591 | FILE  *fin;
591   }
592  
593  
594 < m_mist(fin)             /* transform arguments for mist */
595 < FILE  *fin;
594 > int
595 > m_mist(         /* transform arguments for mist */
596 >        FILE  *fin
597 > )
598   {
599          FUNARGS  fa;
600          int     i;
601  
602          if (readfargs(&fa, fin) != 1)
603                  return(-1);
604 <        if (fa.nfargs > 5)
604 >        if (fa.nfargs > 7)
605                  return(-1);
606          printf("%d", fa.nsargs);
607          if (idprefix == NULL)
# Line 424 | Line 610 | FILE  *fin;
610          else
611                  for (i = 0; i < fa.nsargs; i++) {
612                          char    sname[256], *sp;
613 <                        register char   *cp1, *cp2 = sname;
613 >                        char    *cp1, *cp2 = sname;
614                                                          /* add idprefix */
615                          for (sp = fa.sarg[i]; *sp; sp = cp1) {
616                                  for (cp1 = idprefix; *cp1; )
# Line 441 | Line 627 | FILE  *fin;
627          if (fa.nfargs > 2)
628                  printf(" %12.6g %12.6g %12.6g", fa.farg[0]/tot.sca,
629                                  fa.farg[1]/tot.sca, fa.farg[2]/tot.sca);
630 <        if (fa.nfargs > 3)
631 <                printf(" %12.6g", fa.farg[3]);
446 <        if (fa.nfargs > 4)
447 <                printf(" %12.6g", fa.farg[4]);
630 >        for (i = 3; i < fa.nfargs; i++)
631 >                printf(" %12.6g", fa.farg[i]);
632          printf("\n");
633          freefargs(&fa);
634          return(0);
635   }
636  
637  
638 < m_dielectric(fin)               /* transform arguments for dielectric */
639 < FILE  *fin;
638 > int
639 > m_dielectric(           /* transform arguments for dielectric */
640 >        FILE  *fin
641 > )
642   {
643          FUNARGS  fa;
644  
# Line 471 | Line 657 | FILE  *fin;
657   }
658  
659  
660 < m_interface(fin)                /* transform arguments for interface */
661 < FILE  *fin;
660 > int
661 > m_interface(            /* transform arguments for interface */
662 >        FILE  *fin
663 > )
664   {
665          FUNARGS  fa;
666  
# Line 496 | Line 684 | FILE  *fin;
684   }
685  
686  
687 < text(fin)                       /* transform text arguments */
688 < FILE  *fin;
687 > int
688 > text(                   /* transform text arguments */
689 >        FILE  *fin
690 > )
691   {
692          int  i;
693          FVECT  v;
# Line 509 | Line 699 | FILE  *fin;
699                  return(-1);
700                                          /* string arguments */
701          printf("%d", fa.nsargs);
702 <        for (i = 0; i < fa.nsargs; i++)
703 <                printf(" %s", fa.sarg[i]);
702 >        for (i = 0; i < fa.nsargs; i++) {
703 >                fputc(' ', stdout);
704 >                fputword(fa.sarg[i], stdout);
705 >        }
706          printf("\n0\n%d\n", fa.nfargs);
707                                          /* anchor point */
708          multp3(v, fa.farg, tot.xfm);
# Line 533 | Line 725 | FILE  *fin;
725   }
726  
727  
728 < o_source(fin)                   /* transform source arguments */
729 < FILE  *fin;
728 > int
729 > o_source(                       /* transform source arguments */
730 >        FILE  *fin
731 > )
732   {
733          FVECT  dv;
734          FUNARGS  fa;
# Line 554 | Line 748 | FILE  *fin;
748   }
749  
750  
751 < o_sphere(fin)                   /* transform sphere arguments */
752 < FILE  *fin;
751 > int
752 > o_sphere(                       /* transform sphere arguments */
753 >        FILE  *fin
754 > )
755   {
756          FVECT  cent;
757          double  rad;
# Line 578 | Line 774 | FILE  *fin;
774   }
775  
776  
777 < o_face(fin)                     /* transform face arguments */
778 < FILE  *fin;
777 > int
778 > o_face(                 /* transform face arguments */
779 >        FILE  *fin
780 > )
781   {
782          FVECT  p;
783 <        register int  i;
783 >        int  i;
784          FUNARGS  fa;
785  
786          if (readfargs(&fa, fin) != 1)
# Line 604 | Line 802 | FILE  *fin;
802   }
803  
804  
805 < o_cone(fin)                     /* transform cone and cup arguments */
806 < FILE  *fin;
805 > int
806 > o_cone(                 /* transform cone and cup arguments */
807 >        FILE  *fin
808 > )
809   {
810          FVECT  p0, p1;
811          double  r0, r1;
# Line 631 | Line 831 | FILE  *fin;
831   }
832  
833  
834 < o_cylinder(fin)                 /* transform cylinder and tube arguments */
835 < FILE  *fin;
834 > int
835 > o_cylinder(                     /* transform cylinder and tube arguments */
836 >        FILE  *fin
837 > )
838   {
839          FVECT  p0, p1;
840          double  rad;
# Line 656 | Line 858 | FILE  *fin;
858   }
859  
860  
861 < o_ring(fin)                     /* transform ring arguments */
862 < FILE  *fin;
861 > int
862 > o_ring(                 /* transform ring arguments */
863 >        FILE  *fin
864 > )
865   {
866          FVECT  p0, pd;
867          double  r0, r1;
# Line 687 | Line 891 | FILE  *fin;
891   }
892  
893  
894 < initotypes()                    /* initialize ofun[] array */
894 > void
895 > initotypes(void)                        /* initialize ofun[] array */
896   {
897 <        register int  i;
897 >        int  i;
898  
899          if (ofun[OBJ_SOURCE].funp == o_source)
900                  return;                 /* done already */
696                                        /* alias is additional */
697        ofun[ALIAS].funame = ALIASID;
698        ofun[ALIAS].flags = 0;
901                                          /* functions get new transform */
902 <        for (i = 0; i < NUMTYPES; i++)
902 >        for (i = 0; i < NUMOTYPE; i++)
903                  if (hasfunc(i))
904                          ofun[i].funp = addxform;
905                                          /* special cases */
# Line 710 | Line 912 | initotypes()                   /* initialize ofun[] array */
912          ofun[OBJ_CYLINDER].funp =
913          ofun[OBJ_TUBE].funp = o_cylinder;
914          ofun[OBJ_RING].funp = o_ring;
915 <        ofun[OBJ_INSTANCE].funp = addxform;
915 >        ofun[OBJ_INSTANCE].funp =
916 >        ofun[OBJ_MESH].funp = addxform;
917          ofun[MAT_GLOW].funp = m_glow;
918          ofun[MAT_SPOT].funp = m_spot;
919          ofun[MAT_DIELECTRIC].funp = m_dielectric;
# Line 719 | Line 922 | initotypes()                   /* initialize ofun[] array */
922          ofun[PAT_CTEXT].funp =
923          ofun[PAT_BTEXT].funp =
924          ofun[MIX_TEXT].funp = text;
925 <        ofun[ALIAS].funp = alias;
925 >        ofun[MOD_ALIAS].funp = alias;
926                                          /* surface inverses */
927          tinvers[OBJ_FACE] = OBJ_FACE;
928          tinvers[OBJ_SOURCE] = OBJ_SOURCE;
# Line 731 | Line 934 | initotypes()                   /* initialize ofun[] array */
934          tinvers[OBJ_CYLINDER] = OBJ_TUBE;
935          tinvers[OBJ_TUBE] = OBJ_CYLINDER;
936          tinvers[OBJ_INSTANCE] = OBJ_INSTANCE;   /* oh, well */
937 +        tinvers[OBJ_MESH] = OBJ_MESH;           /* ditto */
938   }
939  
940  
941   #ifdef  OLDXFORM
942 < openmain(fname)
943 < char  *fname;
942 > void
943 > openmain(
944 >        char  *fname
945 > )
946   {
947          if (fname == NULL) {
948                  strcpy(mainfn, "standard input");
# Line 758 | Line 964 | char  *fname;
964          strcpy(mainfn, fname);
965   }
966   #else
967 < openmain(fname)         /* open fname for input, changing to its directory */
968 < char  *fname;
967 > void
968 > openmain(               /* open input, changing directory for file */
969 >        char  *iname
970 > )
971   {
972 <        extern FILE  *tmpfile();
973 <        extern char  *getlibpath(), *getpath();
974 <        static char  origdir[MAXPATH];
975 <        static char  curdir[MAXPATH];
768 <        char  newdir[MAXPATH], *cp;
972 >        static char  origdir[PATH_MAX];
973 >        static char  curfn[PATH_MAX];
974 >        static int  diffdir;
975 >        char  *fpath;
976  
977 <        if (fname == NULL) {                    /* standard input */
977 >        if (iname == NULL) {                    /* standard input */
978                  if (mainfp == NULL) {
979 <                        register int  c;
979 >                        int  c;
980                          strcpy(mainfn, "standard input");
981                          if (nrept <= 1) {
982                                  mainfp = stdin;
# Line 784 | Line 991 | char  *fname;
991                          }
992                          while ((c = getc(stdin)) != EOF)
993                                  putc(c, mainfp);
787                        fclose(stdin);
994                  }
995                  rewind(mainfp);                 /* rewind copy */
996                  return;
997          }
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;
998          if (mainfp == NULL) {                   /* first call, initialize */
999 <                getwd(origdir);
1000 <                strcpy(curdir, origdir);
803 <        } else if (!strcmp(cp, mainfn)) {       /* just need to rewind? */
999 >                getcwd(origdir, sizeof(origdir));
1000 >        } else if (!strcmp(iname, curfn)) {     /* just need to rewind? */
1001                  rewind(mainfp);
1002                  return;
1003 <        } else                                  /* else close old stream */
1003 >        } else {                                /* else close old stream */
1004                  fclose(mainfp);
1005 +                mainfp = NULL;
1006 +                if (diffdir) {                  /* return to our directory */
1007 +                        chdir(origdir);
1008 +                        diffdir = 0;
1009 +                }
1010 +        }
1011 +        strcpy(curfn, iname);                   /* remember input name */
1012 +                                                /* get full path for file */
1013 +        if ((fpath = getpath(iname, getrlibpath(), R_OK)) == NULL) {
1014 +                fprintf(stderr, "%s: cannot find file \"%s\"\n",
1015 +                                progname, iname);
1016 +                exit(1);
1017 +        }
1018 +        if (fpath[0] == '.' && ISDIRSEP(fpath[1]))      /* remove leading ./ */
1019 +                fpath += 2;
1020                                                  /* record path name */
1021 <        strcpy(mainfn, cp);
1022 <        if (expand) {                           /* get directory component */
1023 <                if (ISDIRSEP(cp[0]))
1024 <                        strcpy(newdir, cp);
1025 <                else
1026 <                        sprintf(newdir, "%s%c%s", origdir, DIRSEP, cp);
1027 <                for (cp = newdir+strlen(newdir); !ISDIRSEP(cp[-1]); cp--)
1028 <                        ;
1021 >        strcpy(mainfn, fpath);
1022 >        if (expand) {                           /* change to local directory */
1023 >                char  *cp = fpath + strlen(fpath);      /* get dir. */
1024 >                while (cp > fpath) {
1025 >                        cp--;
1026 >                        if (ISDIRSEP(*cp)) {
1027 >                                if (cp == fpath)
1028 >                                        cp++;   /* root special case */
1029 >                                break;
1030 >                        }
1031 >                }
1032                  *cp = '\0';
1033 <                if (strcmp(newdir, curdir)) {   /* change to new directory? */
1034 <                        if (chdir(newdir) < 0) {
1033 >                if (fpath[0]) {                 /* change to new directory? */
1034 >                        if (chdir(fpath) < 0) {
1035                                  fprintf(stderr,
1036                                  "%s: cannot change directory to \"%s\"\n",
1037 <                                                progname, newdir);
1037 >                                                progname, fpath);
1038                                  exit(1);
1039                          }
1040 <                        strcpy(curdir, newdir);
1040 >                        diffdir++;
1041                  }
1042                                                  /* get final path component */
1043 <                for (cp = fname+strlen(fname);
1044 <                                cp > fname && !ISDIRSEP(cp[-1]); cp--)
1043 >                for (fpath = iname+strlen(iname);
1044 >                                fpath > iname && !ISDIRSEP(fpath[-1]); fpath--)
1045                          ;
1046          }
1047 <                                                /* open the file */
1048 <        if ((mainfp = fopen(cp, "r")) == NULL) {
1047 >                                                /* finally, open the file */
1048 >        if ((mainfp = fopen(fpath, "r")) == NULL) {
1049                  fprintf(stderr, "%s: cannot open file \"%s\"\n",
1050                                  progname, mainfn);
1051                  exit(1);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines