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.19 by gwlarson, Fri Jun 4 16:00:27 1999 UTC vs.
Revision 2.50 by greg, Fri Mar 4 00:21:21 2016 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  "rtio.h"
17 + #include  "rtmath.h"
18   #include  "object.h"
22
19   #include  "otypes.h"
20  
21   int  xac;                               /* global xform argument count */
# Line 29 | Line 25 | int  xfa;                              /* start of xf arguments */
25   XF  tot;                                /* total transformation */
26   int  reverse;                           /* boolean true if scene mirrored */
27  
28 < int  invert = 0;                        /* boolean true to invert surfaces */
28 > int  invert;                            /* boolean true to invert surfaces */
29  
30 < int  expand = 1;                        /* boolean true to expand commands */
30 > int  expand;                            /* boolean true to expand commands */
31  
32 < char  *newmod = NULL;                   /* new modifier for surfaces */
32 > char  *newmod;                          /* new modifier for surfaces */
33  
34 < char  *idprefix = NULL;                 /* prefix for object identifiers */
34 > char  *idprefix;                        /* prefix for object identifiers */
35  
36 < #define  ALIAS          NUMOTYPE        /* put alias at end of array */
36 > FUN  ofun[NUMOTYPE] = INIT_OTYPE;       /* default types and actions */
37  
42 #define  NUMTYPES       (NUMOTYPE+1)    /* total number of object types */
43
44 FUN  ofun[NUMTYPES] = INIT_OTYPE;       /* default types and actions */
45
38   short  tinvers[NUMOTYPE];               /* inverse types for surfaces */
39  
40   int  nrept = 1;                         /* number of array repetitions */
41  
42   int stdinused = 0;                      /* stdin has been used by -f option? */
43  
44 < extern char  *malloc(), *fgets(), *fgetword();
53 <
54 < char  mainfn[MAXPATH];                  /* main file name */
44 > char  mainfn[PATH_MAX];                 /* main file name */
45   FILE  *mainfp = NULL;                   /* main file pointer */
46  
47   #define  progname  (xav[0])
48  
49 + static int doargf(int ac, char **av, int fi);
50 + static int doarray(int ac, char **av, int ai);
51 + static void xform(char *name, FILE *fin);
52 + static void xfcomm(char *fname, FILE *fin);
53 + static void xfobject(char *fname, FILE *fin);
54 + static int addxform(FILE *fin);
55 + static int alias(FILE *fin);
56 + void initotypes(void); /* XXX conflict with otypes.h */
57 + static void openmain(char *iname);
58  
59 < main(argc, argv)                /* get transform options and transform file */
60 < int  argc;
61 < char  *argv[];
59 >
60 > int
61 > main(           /* get transform options and transform file */
62 >        int  argc,
63 >        char  *argv[]
64 > )
65   {
66 <        char  *fname;
66 >        int  mal_prefix = 0;
67          int  a;
68                                          /* check for argument list file */
69          for (a = 1; a < argc; a++)
# Line 72 | Line 74 | char  *argv[];
74                  if (!strcmp(argv[a], "-a"))
75                          return(doarray(argc, argv, a));
76  
77 <        initotypes();
77 >        initotypes();                   /* initialize */
78 >        invert = 0;
79 >        expand = 1;
80 >        newmod = NULL;
81 >        idprefix = NULL;
82  
83          for (a = 1; a < argc; a++) {
84                  if (argv[a][0] == '-')
85                          switch (argv[a][1]) {
86                          case 'm':
87 <                                if (argv[a][2] || a+1 >= argc)
87 >                                if (argv[a][2] | (a+1 >= argc))
88                                          break;
89 <                                newmod = argv[++a];
89 >                                a++;
90 >                                if (newmod == NULL)
91 >                                        newmod = argv[a];
92                                  continue;
93                          case 'n':
94 <                                if (argv[a][2] || a+1 >= argc)
94 >                                if (argv[a][2] | (a+1 >= argc))
95                                          break;
96 <                                idprefix = argv[++a];
96 >                                a++;
97 >                                if (idprefix == NULL)
98 >                                        idprefix = argv[a];
99 >                                else {
100 >                                        char    *newp;
101 >                                        newp = (char *)malloc(strlen(idprefix)+
102 >                                                        strlen(argv[a])+2);
103 >                                        if (newp == NULL)
104 >                                                exit(2);
105 >                                        sprintf(newp, "%s.%s",
106 >                                                        idprefix, argv[a]);
107 >                                        if (mal_prefix++)
108 >                                                free(idprefix);
109 >                                        idprefix = newp;
110 >                                }
111                                  continue;
112                          case 'c':
113                                  if (argv[a][2])
# Line 100 | Line 122 | char  *argv[];
122                          case 'I':
123                                  if (argv[a][2])
124                                          break;
125 <                                invert = 1;
125 >                                invert = !invert;
126                                  continue;
127                          }
128                  break;
# Line 111 | Line 133 | char  *argv[];
133  
134          a += xf(&tot, argc-a, argv+a);
135  
136 <        if (reverse = tot.sca < 0.0)
136 >        if ( (reverse = (tot.sca < 0.0)) )
137                  tot.sca = -tot.sca;
138          if (invert)
139                  reverse = !reverse;
# Line 143 | Line 165 | char  *argv[];
165                          xform(mainfn, mainfp);
166                  }
167  
168 +        if (mal_prefix)
169 +                free(idprefix);
170          return(0);
171   }
172  
173  
174 < doargf(ac, av, fi)                      /* take argument list from file */
175 < char  **av;
176 < int  ac, fi;
174 > int
175 > doargf(                 /* take argument list from file */
176 >        int ac,
177 >        char  **av,
178 >        int fi
179 > )
180   {
181 +        int  inquote;
182          char  *newav[256], **avp;
183          char  argbuf[2048];
184 <        char  newid[128];
184 >        char  *newid, newidbuf[128];
185          char  *oldid;
186 <        register char   *cp;
186 >        char    *cp;
187          FILE    *argfp;
188          int  n, i, k, newac, err;
189          
# Line 165 | Line 193 | int  ac, fi;
193          }
194          if (av[fi+1][0] == '-' && av[fi+1][1] == '\0') {
195                  if (stdinused++) {
196 <                        fprintf(stderr, "%s: cannot use stdin more than once\n",
196 >                        fprintf(stderr,
197 >                                "%s: cannot use stdin more than once\n",
198                                          av[0]);
199                          exit(1);
200                  }
# Line 173 | Line 202 | int  ac, fi;
202                  n = 100;                /* we just don't know! */
203          } else {
204                  if ((argfp = fopen(av[fi+1], "r")) == NULL) {
205 <                        fprintf(stderr, "%s: cannot open argument file \"%s\"\n",
205 >                        fprintf(stderr,
206 >                                "%s: cannot open argument file \"%s\"\n",
207                                          av[0], av[fi+1]);
208                          exit(1);
209                  }
210                  n = 0;                  /* count number of lines in file */
211 <                while (fgets(argbuf,sizeof(argbuf),argfp) != NULL)
212 <                        n += argbuf[0] != '\n' & argbuf[0] != '#';
211 >                while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL)
212 >                        n += argbuf[0] && argbuf[0] != '#';
213                  if (!n) {
214                          fprintf(stderr, "%s: empty argument file \"%s\"\n",
215                                          av[0], av[fi+1]);
# Line 189 | Line 219 | int  ac, fi;
219                  rewind(argfp);
220          }
221          err = 0; k = 0;                 /* read each arg list and call main */
222 <        while (fgets(argbuf,sizeof(argbuf),argfp) != NULL) {
223 <                if (argbuf[0] == '\n' | argbuf[0] == '#')
222 >        while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL) {
223 >                if (!argbuf[0] || argbuf[0] == '#')
224                          continue;
225                  avp = newav+2;
226                  avp[0] = av[0];
# Line 200 | Line 230 | int  ac, fi;
230                  cp = argbuf;            /* parse new words */
231                  if (*cp == '!') cp++;
232                  if (!strncmp(cp, "xform ", 6)) cp += 6;
233 +                inquote = 0;
234                  for ( ; ; ) {
235 +                skipspaces:
236                          while (isspace(*cp))    /* nullify spaces */
237                                  *cp++ = '\0';
238 +                        if ((*cp == '"') | (*cp == '\''))
239 +                                inquote = *cp++;
240                          if (!*cp)               /* all done? */
241                                  break;
242 +                        if (cp[0] == '\\' && cp[1])
243 +                                if (*++cp == '\n')
244 +                                        goto skipspaces;
245                          avp[newac++] = cp;      /* add argument to list */
246 <                        while (*++cp && !isspace(*cp))
247 <                                ;
246 >                        if (inquote) {
247 >                                while (*++cp)
248 >                                        if (*cp == inquote) {
249 >                                                *cp++ = '\0';
250 >                                                break;
251 >                                        }
252 >                        } else {
253 >                                while (*++cp && !isspace(*cp))
254 >                                        ;
255 >                        }
256                  }
257                  for (i = fi+2; i < ac; i++)
258                          avp[newac++] = av[i];
259                  avp[newac] = NULL;
260 +                newid = newidbuf;
261                  oldid = NULL;
262                  for (i = 2; i < newac; i++)
263                          if (!strcmp(avp[i-1], "-n")) {
264                                  oldid = avp[i];
265 +                                if (strlen(oldid)+32 > sizeof(newidbuf)) {
266 +                                        newid = (char *)malloc(strlen(oldid)+32);
267 +                                        if (newid == NULL)
268 +                                                exit(2);
269 +                                }
270                                  avp[i] = newid;
271                                  break;
272                          }
# Line 231 | Line 282 | int  ac, fi;
282                  else
283                          sprintf(newid, "%s.%d", oldid, k);
284                  err |= main(newac, avp);
285 +                if (newid != newidbuf)
286 +                        free(newid);
287                  k++;
288          }
289          fclose(argfp);
# Line 238 | Line 291 | int  ac, fi;
291   }
292  
293  
294 < doarray(ac, av, ai)                     /* make array */
295 < char  **av;
296 < int  ac, ai;
294 > int
295 > doarray(                        /* make array */
296 >        int  ac,
297 >        char  **av,
298 >        int  ai
299 > )
300   {
301          char  *newav[256], **avp;
302          char  newid[128], repts[32];
# Line 283 | Line 339 | int  ac, ai;
339   }
340  
341  
342 < xform(name, fin)                        /* transform stream by tot.xfm */
343 < char  *name;
344 < register FILE  *fin;
342 > void
343 > xform(                  /* transform stream by tot.xfm */
344 >        char  *name,
345 >        FILE  *fin
346 > )
347   {
348          int  nobjs = 0;
349 <        register int  c;
349 >        int  c;
350  
351          while ((c = getc(fin)) != EOF) {
352                  if (isspace(c))                         /* blank */
# Line 316 | Line 374 | register FILE  *fin;
374   }
375  
376  
377 < xfcomm(fname, fin)                      /* transform a command */
378 < char  *fname;
379 < FILE  *fin;
377 > void
378 > xfcomm(                 /* transform a command */
379 >        char  *fname,
380 >        FILE  *fin
381 > )
382   {
323        extern FILE  *popen();
324        extern char  *fgetline();
383          FILE  *pin;
384 <        char  buf[512];
384 >        char  buf[2048];
385          int  i;
386  
387          fgetline(buf, sizeof(buf), fin);
# Line 338 | Line 396 | FILE  *fin;
396          } else {
397                  printf("\n%s", buf);
398                  if (xac > 1) {
399 <                        printf(" | %s", xav[0]);
399 >                        printf(" | %s -e", xav[0]);     /* expand next time */
400                          for (i = 1; i < xac; i++)
401 <                                printf(" %s", xav[i]);
401 >                                if (i >= xfa || strcmp(xav[i], "-c"))
402 >                                        printf(" %s", xav[i]);
403                  }
404                  putchar('\n');
405          }
406   }
407  
408  
409 < xfobject(fname, fin)                            /* transform an object */
410 < char  *fname;
411 < FILE  *fin;
409 > void
410 > xfobject(                               /* transform an object */
411 >        char  *fname,
412 >        FILE  *fin
413 > )
414   {
354        extern char  *strcpy();
415          char  typ[16], nam[MAXSTR];
416          int  fn;
417                                                  /* modifier and type */
# Line 363 | Line 423 | FILE  *fin;
423                                  progname, fname, typ);
424                  exit(1);
425          }
426 <        if (ismodifier(fn))
427 <                printf("\n%s %s ", nam, typ);
428 <        else
429 <                printf("\n%s %s ", newmod != NULL ? newmod : nam,
430 <                                invert ? ofun[tinvers[fn]].funame : typ);
426 >        putchar('\n');
427 >        if (ismodifier(fn)) {
428 >                fputword(nam, stdout);
429 >                printf(" %s ", typ);
430 >        } else {
431 >                fputword(newmod != NULL ? newmod : nam, stdout);
432 >                printf(" %s ", invert ? ofun[tinvers[fn]].funame : typ);
433 >        }
434                                                  /* object name */
435          fgetword(nam, sizeof(nam), fin);
436          if (idprefix == NULL || ismodifier(fn))
437 <                printf("%s\n", nam);
438 <        else
439 <                printf("%s.%s\n", idprefix, nam);
437 >                fputword(nam, stdout);
438 >        else {
439 >                char    nnam[MAXSTR];
440 >                sprintf(nnam, "%s.%s", idprefix, nam);
441 >                fputword(nnam, stdout);
442 >        }
443 >        putchar('\n');
444                                                  /* transform arguments */
445          if ((*ofun[fn].funp)(fin) < 0) {
446                  fprintf(stderr, "%s: (%s): bad %s \"%s\"\n",
# Line 383 | Line 450 | FILE  *fin;
450   }
451  
452  
453 < o_default(fin)                  /* pass on arguments unchanged */
454 < FILE  *fin;
453 > int
454 > o_default(                      /* pass on arguments unchanged */
455 >        FILE  *fin
456 > )
457   {
458 <        register int  i;
458 >        int  i;
459          FUNARGS  fa;
460  
461          if (readfargs(&fa, fin) != 1)
462                  return(-1);
463                                          /* string arguments */
464          printf("%d", fa.nsargs);
465 <        for (i = 0; i < fa.nsargs; i++)
466 <                printf(" %s", fa.sarg[i]);
465 >        for (i = 0; i < fa.nsargs; i++) {
466 >                fputc(' ', stdout);
467 >                fputword(fa.sarg[i], stdout);
468 >        }
469          printf("\n");
470   #ifdef  IARGS
471                                          /* integer arguments */
# Line 415 | Line 486 | FILE  *fin;
486   }
487  
488  
489 < addxform(fin)                   /* add xf arguments to strings */
490 < FILE  *fin;
489 > int
490 > addxform(                       /* add xf arguments to strings */
491 >        FILE  *fin
492 > )
493   {
494 <        register int  i;
494 >        int  i;
495          int  resetarr = 0;
496          FUNARGS  fa;
497  
# Line 428 | Line 501 | FILE  *fin;
501          if (xac > xfa && strcmp(xav[xfa], "-i"))
502                  resetarr = 2;
503          printf("%d", fa.nsargs + resetarr + xac-xfa);
504 <        for (i = 0; i < fa.nsargs; i++)
505 <                printf(" %s", fa.sarg[i]);
504 >        for (i = 0; i < fa.nsargs; i++) {
505 >                fputc(' ', stdout);
506 >                fputword(fa.sarg[i], stdout);
507 >        }
508          if (resetarr)
509                  printf(" -i 1");
510          for (i = xfa; i < xac; i++)     /* add xf arguments */
# Line 455 | Line 530 | FILE  *fin;
530  
531  
532   int
533 < otype(ofname)                   /* get object function number from its name */
534 < register char  *ofname;
533 > alias(                  /* transfer alias */
534 >        FILE  *fin
535 > )
536   {
461        register int  i;
462
463        for (i = 0; i < NUMTYPES; i++)
464                if (!strcmp(ofun[i].funame, ofname))
465                        return(i);
466
467        return(-1);             /* not found */
468 }
469
470
471 alias(fin)                      /* transfer alias */
472 FILE  *fin;
473 {
537          char  aliasnm[MAXSTR];
538  
539          if (fgetword(aliasnm, MAXSTR, fin) == NULL)
# Line 480 | Line 543 | FILE  *fin;
543   }
544  
545  
546 < m_glow(fin)                     /* transform arguments for proximity light */
547 < FILE  *fin;
546 > int
547 > m_glow(                 /* transform arguments for proximity light */
548 >        FILE  *fin
549 > )
550   {
551          FUNARGS  fa;
552  
# Line 498 | Line 563 | FILE  *fin;
563   }
564  
565  
566 < m_spot(fin)                     /* transform arguments for spotlight */
567 < FILE  *fin;
566 > int
567 > m_spot(                 /* transform arguments for spotlight */
568 >        FILE  *fin
569 > )
570   {
571          FVECT  v;
572          FUNARGS  fa;
# Line 518 | Line 585 | FILE  *fin;
585   }
586  
587  
588 < m_mist(fin)             /* transform arguments for mist */
589 < FILE  *fin;
588 > int
589 > m_mist(         /* transform arguments for mist */
590 >        FILE  *fin
591 > )
592   {
593          FUNARGS  fa;
594          int     i;
# Line 535 | Line 604 | FILE  *fin;
604          else
605                  for (i = 0; i < fa.nsargs; i++) {
606                          char    sname[256], *sp;
607 <                        register char   *cp1, *cp2 = sname;
607 >                        char    *cp1, *cp2 = sname;
608                                                          /* add idprefix */
609                          for (sp = fa.sarg[i]; *sp; sp = cp1) {
610                                  for (cp1 = idprefix; *cp1; )
# Line 560 | Line 629 | FILE  *fin;
629   }
630  
631  
632 < m_dielectric(fin)               /* transform arguments for dielectric */
633 < FILE  *fin;
632 > int
633 > m_dielectric(           /* transform arguments for dielectric */
634 >        FILE  *fin
635 > )
636   {
637          FUNARGS  fa;
638  
# Line 580 | Line 651 | FILE  *fin;
651   }
652  
653  
654 < m_interface(fin)                /* transform arguments for interface */
655 < FILE  *fin;
654 > int
655 > m_interface(            /* transform arguments for interface */
656 >        FILE  *fin
657 > )
658   {
659          FUNARGS  fa;
660  
# Line 605 | Line 678 | FILE  *fin;
678   }
679  
680  
681 < text(fin)                       /* transform text arguments */
682 < FILE  *fin;
681 > int
682 > text(                   /* transform text arguments */
683 >        FILE  *fin
684 > )
685   {
686          int  i;
687          FVECT  v;
# Line 618 | Line 693 | FILE  *fin;
693                  return(-1);
694                                          /* string arguments */
695          printf("%d", fa.nsargs);
696 <        for (i = 0; i < fa.nsargs; i++)
697 <                printf(" %s", fa.sarg[i]);
696 >        for (i = 0; i < fa.nsargs; i++) {
697 >                fputc(' ', stdout);
698 >                fputword(fa.sarg[i], stdout);
699 >        }
700          printf("\n0\n%d\n", fa.nfargs);
701                                          /* anchor point */
702          multp3(v, fa.farg, tot.xfm);
# Line 642 | Line 719 | FILE  *fin;
719   }
720  
721  
722 < o_source(fin)                   /* transform source arguments */
723 < FILE  *fin;
722 > int
723 > o_source(                       /* transform source arguments */
724 >        FILE  *fin
725 > )
726   {
727          FVECT  dv;
728          FUNARGS  fa;
# Line 663 | Line 742 | FILE  *fin;
742   }
743  
744  
745 < o_sphere(fin)                   /* transform sphere arguments */
746 < FILE  *fin;
745 > int
746 > o_sphere(                       /* transform sphere arguments */
747 >        FILE  *fin
748 > )
749   {
750          FVECT  cent;
751          double  rad;
# Line 687 | Line 768 | FILE  *fin;
768   }
769  
770  
771 < o_face(fin)                     /* transform face arguments */
772 < FILE  *fin;
771 > int
772 > o_face(                 /* transform face arguments */
773 >        FILE  *fin
774 > )
775   {
776          FVECT  p;
777 <        register int  i;
777 >        int  i;
778          FUNARGS  fa;
779  
780          if (readfargs(&fa, fin) != 1)
# Line 713 | Line 796 | FILE  *fin;
796   }
797  
798  
799 < o_cone(fin)                     /* transform cone and cup arguments */
800 < FILE  *fin;
799 > int
800 > o_cone(                 /* transform cone and cup arguments */
801 >        FILE  *fin
802 > )
803   {
804          FVECT  p0, p1;
805          double  r0, r1;
# Line 740 | Line 825 | FILE  *fin;
825   }
826  
827  
828 < o_cylinder(fin)                 /* transform cylinder and tube arguments */
829 < FILE  *fin;
828 > int
829 > o_cylinder(                     /* transform cylinder and tube arguments */
830 >        FILE  *fin
831 > )
832   {
833          FVECT  p0, p1;
834          double  rad;
# Line 765 | Line 852 | FILE  *fin;
852   }
853  
854  
855 < o_ring(fin)                     /* transform ring arguments */
856 < FILE  *fin;
855 > int
856 > o_ring(                 /* transform ring arguments */
857 >        FILE  *fin
858 > )
859   {
860          FVECT  p0, pd;
861          double  r0, r1;
# Line 796 | Line 885 | FILE  *fin;
885   }
886  
887  
888 < initotypes()                    /* initialize ofun[] array */
888 > void
889 > initotypes(void)                        /* initialize ofun[] array */
890   {
891 <        register int  i;
891 >        int  i;
892  
893          if (ofun[OBJ_SOURCE].funp == o_source)
894                  return;                 /* done already */
805                                        /* alias is additional */
806        ofun[ALIAS].funame = ALIASID;
807        ofun[ALIAS].flags = 0;
895                                          /* functions get new transform */
896 <        for (i = 0; i < NUMTYPES; i++)
896 >        for (i = 0; i < NUMOTYPE; i++)
897                  if (hasfunc(i))
898                          ofun[i].funp = addxform;
899                                          /* special cases */
# Line 819 | Line 906 | initotypes()                   /* initialize ofun[] array */
906          ofun[OBJ_CYLINDER].funp =
907          ofun[OBJ_TUBE].funp = o_cylinder;
908          ofun[OBJ_RING].funp = o_ring;
909 <        ofun[OBJ_INSTANCE].funp = addxform;
909 >        ofun[OBJ_INSTANCE].funp =
910 >        ofun[OBJ_MESH].funp = addxform;
911          ofun[MAT_GLOW].funp = m_glow;
912          ofun[MAT_SPOT].funp = m_spot;
913          ofun[MAT_DIELECTRIC].funp = m_dielectric;
# Line 828 | Line 916 | initotypes()                   /* initialize ofun[] array */
916          ofun[PAT_CTEXT].funp =
917          ofun[PAT_BTEXT].funp =
918          ofun[MIX_TEXT].funp = text;
919 <        ofun[ALIAS].funp = alias;
919 >        ofun[MOD_ALIAS].funp = alias;
920                                          /* surface inverses */
921          tinvers[OBJ_FACE] = OBJ_FACE;
922          tinvers[OBJ_SOURCE] = OBJ_SOURCE;
# Line 840 | Line 928 | initotypes()                   /* initialize ofun[] array */
928          tinvers[OBJ_CYLINDER] = OBJ_TUBE;
929          tinvers[OBJ_TUBE] = OBJ_CYLINDER;
930          tinvers[OBJ_INSTANCE] = OBJ_INSTANCE;   /* oh, well */
931 +        tinvers[OBJ_MESH] = OBJ_MESH;           /* ditto */
932   }
933  
934  
935   #ifdef  OLDXFORM
936 < openmain(fname)
937 < char  *fname;
936 > void
937 > openmain(
938 >        char  *fname
939 > )
940   {
941          if (fname == NULL) {
942                  strcpy(mainfn, "standard input");
# Line 867 | Line 958 | char  *fname;
958          strcpy(mainfn, fname);
959   }
960   #else
961 < openmain(fname)         /* open fname for input, changing to its directory */
962 < char  *fname;
961 > void
962 > openmain(               /* open input, changing directory for file */
963 >        char  *iname
964 > )
965   {
966 <        extern FILE  *tmpfile();
967 <        extern char  *getlibpath(), *getpath();
875 <        static char  origdir[MAXPATH];
876 <        static char  curfn[MAXPATH];
966 >        static char  origdir[PATH_MAX];
967 >        static char  curfn[PATH_MAX];
968          static int  diffdir;
969 <        register char  *fpath;
969 >        char  *fpath;
970  
971 <        if (fname == NULL) {                    /* standard input */
971 >        if (iname == NULL) {                    /* standard input */
972                  if (mainfp == NULL) {
973 <                        register int  c;
973 >                        int  c;
974                          strcpy(mainfn, "standard input");
975                          if (nrept <= 1) {
976                                  mainfp = stdin;
# Line 894 | Line 985 | char  *fname;
985                          }
986                          while ((c = getc(stdin)) != EOF)
987                                  putc(c, mainfp);
897                        fclose(stdin);
988                  }
989                  rewind(mainfp);                 /* rewind copy */
990                  return;
991          }
992          if (mainfp == NULL) {                   /* first call, initialize */
993 <                getwd(origdir);
994 <        } else if (!strcmp(fname, curfn)) {     /* just need to rewind? */
993 >                getcwd(origdir, sizeof(origdir));
994 >        } else if (!strcmp(iname, curfn)) {     /* just need to rewind? */
995                  rewind(mainfp);
996                  return;
997          } else {                                /* else close old stream */
998                  fclose(mainfp);
999 <                if (diffdir) {
999 >                mainfp = NULL;
1000 >                if (diffdir) {                  /* return to our directory */
1001                          chdir(origdir);
1002                          diffdir = 0;
1003                  }
1004          }
1005 <        strcpy(curfn, fname);                   /* remember file name */
1006 <                                                /* get full path */
1007 <        if ((fpath = getpath(fname, getlibpath(), R_OK)) == NULL) {
1005 >        strcpy(curfn, iname);                   /* remember input name */
1006 >                                                /* get full path for file */
1007 >        if ((fpath = getpath(iname, getrlibpath(), R_OK)) == NULL) {
1008                  fprintf(stderr, "%s: cannot find file \"%s\"\n",
1009 <                                progname, fname);
1009 >                                progname, iname);
1010                  exit(1);
1011          }
1012          if (fpath[0] == '.' && ISDIRSEP(fpath[1]))      /* remove leading ./ */
# Line 923 | Line 1014 | char  *fname;
1014                                                  /* record path name */
1015          strcpy(mainfn, fpath);
1016          if (expand) {                           /* change to local directory */
1017 <                register char  *cp = fpath + strlen(fpath);     /* get dir. */
1017 >                char  *cp = fpath + strlen(fpath);      /* get dir. */
1018                  while (cp > fpath) {
1019                          cp--;
1020                          if (ISDIRSEP(*cp)) {
# Line 943 | Line 1034 | char  *fname;
1034                          diffdir++;
1035                  }
1036                                                  /* get final path component */
1037 <                for (fpath = fname+strlen(fname);
1038 <                                fpath > fname && !ISDIRSEP(fpath[-1]); fpath--)
1037 >                for (fpath = iname+strlen(iname);
1038 >                                fpath > iname && !ISDIRSEP(fpath[-1]); fpath--)
1039                          ;
1040          }
1041 <                                                /* open the file */
1041 >                                                /* finally, open the file */
1042          if ((mainfp = fopen(fpath, "r")) == NULL) {
1043                  fprintf(stderr, "%s: cannot open file \"%s\"\n",
1044                                  progname, mainfn);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines