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. |
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 |
|
int xac; /* global xform argument count */ |
34 |
|
|
35 |
|
char *idprefix; /* prefix for object identifiers */ |
36 |
|
|
37 |
< |
#define ALIAS NUMOTYPE /* put alias at end of array */ |
37 |
> |
FUN ofun[NUMOTYPE] = INIT_OTYPE; /* default types and actions */ |
38 |
|
|
42 |
– |
#define NUMTYPES (NUMOTYPE+1) /* total number of object types */ |
43 |
– |
|
44 |
– |
FUN ofun[NUMTYPES] = INIT_OTYPE; /* default types and actions */ |
45 |
– |
|
39 |
|
short tinvers[NUMOTYPE]; /* inverse types for surfaces */ |
40 |
|
|
41 |
|
int nrept = 1; /* number of array repetitions */ |
42 |
|
|
43 |
|
int stdinused = 0; /* stdin has been used by -f option? */ |
44 |
|
|
45 |
< |
extern char *malloc(), *fgets(), *fgetword(); |
53 |
< |
|
54 |
< |
char mainfn[MAXPATH]; /* main file name */ |
45 |
> |
char mainfn[PATH_MAX]; /* main file name */ |
46 |
|
FILE *mainfp = NULL; /* main file pointer */ |
47 |
|
|
48 |
|
#define progname (xav[0]) |
49 |
|
|
50 |
+ |
static int doargf(int ac, char **av, int fi); |
51 |
+ |
static int doarray(int ac, char **av, int ai); |
52 |
+ |
static void xform(char *name, FILE *fin); |
53 |
+ |
static void xfcomm(char *fname, FILE *fin); |
54 |
+ |
static void xfobject(char *fname, FILE *fin); |
55 |
+ |
static int addxform(FILE *fin); |
56 |
+ |
static int alias(FILE *fin); |
57 |
+ |
void initotypes(void); /* XXX conflict with otypes.h */ |
58 |
+ |
static void openmain(char *iname); |
59 |
|
|
60 |
< |
main(argc, argv) /* get transform options and transform file */ |
61 |
< |
int argc; |
62 |
< |
char *argv[]; |
60 |
> |
|
61 |
> |
int |
62 |
> |
main( /* get transform options and transform file */ |
63 |
> |
int argc, |
64 |
> |
char *argv[] |
65 |
> |
) |
66 |
|
{ |
67 |
|
int mal_prefix = 0; |
65 |
– |
char *fname; |
68 |
|
int a; |
69 |
|
/* check for argument list file */ |
70 |
|
for (a = 1; a < argc; a++) |
85 |
|
if (argv[a][0] == '-') |
86 |
|
switch (argv[a][1]) { |
87 |
|
case 'm': |
88 |
< |
if (argv[a][2] | a+1 >= argc) |
88 |
> |
if (argv[a][2] | (a+1 >= argc)) |
89 |
|
break; |
90 |
|
a++; |
91 |
|
if (newmod == NULL) |
92 |
|
newmod = argv[a]; |
93 |
|
continue; |
94 |
|
case 'n': |
95 |
< |
if (argv[a][2] | a+1 >= argc) |
95 |
> |
if (argv[a][2] | (a+1 >= argc)) |
96 |
|
break; |
97 |
|
a++; |
98 |
|
if (idprefix == NULL) |
106 |
|
sprintf(newp, "%s.%s", |
107 |
|
idprefix, argv[a]); |
108 |
|
if (mal_prefix++) |
109 |
< |
free((char *)idprefix); |
109 |
> |
free((void *)idprefix); |
110 |
|
idprefix = newp; |
111 |
|
} |
112 |
|
continue; |
134 |
|
|
135 |
|
a += xf(&tot, argc-a, argv+a); |
136 |
|
|
137 |
< |
if (reverse = tot.sca < 0.0) |
137 |
> |
if ( (reverse = tot.sca < 0.0) ) |
138 |
|
tot.sca = -tot.sca; |
139 |
|
if (invert) |
140 |
|
reverse = !reverse; |
167 |
|
} |
168 |
|
|
169 |
|
if (mal_prefix) |
170 |
< |
free((char *)idprefix); |
170 |
> |
free((void *)idprefix); |
171 |
|
return(0); |
172 |
|
} |
173 |
|
|
174 |
|
|
175 |
< |
doargf(ac, av, fi) /* take argument list from file */ |
176 |
< |
char **av; |
177 |
< |
int ac, fi; |
175 |
> |
int |
176 |
> |
doargf( /* take argument list from file */ |
177 |
> |
int ac, |
178 |
> |
char **av, |
179 |
> |
int fi |
180 |
> |
) |
181 |
|
{ |
182 |
+ |
int inquote; |
183 |
|
char *newav[256], **avp; |
184 |
< |
char argbuf[1024]; |
184 |
> |
char argbuf[2048]; |
185 |
|
char newid[128]; |
186 |
|
char *oldid; |
187 |
|
register char *cp; |
194 |
|
} |
195 |
|
if (av[fi+1][0] == '-' && av[fi+1][1] == '\0') { |
196 |
|
if (stdinused++) { |
197 |
< |
fprintf(stderr, "%s: cannot use stdin more than once\n", |
197 |
> |
fprintf(stderr, |
198 |
> |
"%s: cannot use stdin more than once\n", |
199 |
|
av[0]); |
200 |
|
exit(1); |
201 |
|
} |
203 |
|
n = 100; /* we just don't know! */ |
204 |
|
} else { |
205 |
|
if ((argfp = fopen(av[fi+1], "r")) == NULL) { |
206 |
< |
fprintf(stderr, "%s: cannot open argument file \"%s\"\n", |
206 |
> |
fprintf(stderr, |
207 |
> |
"%s: cannot open argument file \"%s\"\n", |
208 |
|
av[0], av[fi+1]); |
209 |
|
exit(1); |
210 |
|
} |
211 |
|
n = 0; /* count number of lines in file */ |
212 |
< |
while (fgets(argbuf,sizeof(argbuf),argfp) != NULL) |
213 |
< |
n += argbuf[0] != '\n' & argbuf[0] != '#'; |
212 |
> |
while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL) |
213 |
> |
n += argbuf[0] && argbuf[0] != '#'; |
214 |
|
if (!n) { |
215 |
|
fprintf(stderr, "%s: empty argument file \"%s\"\n", |
216 |
|
av[0], av[fi+1]); |
220 |
|
rewind(argfp); |
221 |
|
} |
222 |
|
err = 0; k = 0; /* read each arg list and call main */ |
223 |
< |
while (fgets(argbuf,sizeof(argbuf),argfp) != NULL) { |
224 |
< |
if (argbuf[0] == '\n' | argbuf[0] == '#') |
223 |
> |
while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL) { |
224 |
> |
if (!argbuf[0] || argbuf[0] == '#') |
225 |
|
continue; |
226 |
|
avp = newav+2; |
227 |
|
avp[0] = av[0]; |
231 |
|
cp = argbuf; /* parse new words */ |
232 |
|
if (*cp == '!') cp++; |
233 |
|
if (!strncmp(cp, "xform ", 6)) cp += 6; |
234 |
+ |
inquote = 0; |
235 |
|
for ( ; ; ) { |
236 |
+ |
skipspaces: |
237 |
|
while (isspace(*cp)) /* nullify spaces */ |
238 |
|
*cp++ = '\0'; |
239 |
+ |
if ((*cp == '"') | (*cp == '\'')) |
240 |
+ |
inquote = *cp++; |
241 |
|
if (!*cp) /* all done? */ |
242 |
|
break; |
243 |
+ |
if (cp[0] == '\\' && cp[1]) |
244 |
+ |
if (*++cp == '\n') |
245 |
+ |
goto skipspaces; |
246 |
|
avp[newac++] = cp; /* add argument to list */ |
247 |
< |
while (*++cp && !isspace(*cp)) |
248 |
< |
; |
247 |
> |
if (inquote) { |
248 |
> |
while (*++cp) |
249 |
> |
if (*cp == inquote) { |
250 |
> |
*cp++ = '\0'; |
251 |
> |
break; |
252 |
> |
} |
253 |
> |
} else { |
254 |
> |
while (*++cp && !isspace(*cp)) |
255 |
> |
; |
256 |
> |
} |
257 |
|
} |
258 |
|
for (i = fi+2; i < ac; i++) |
259 |
|
avp[newac++] = av[i]; |
284 |
|
} |
285 |
|
|
286 |
|
|
287 |
< |
doarray(ac, av, ai) /* make array */ |
288 |
< |
char **av; |
289 |
< |
int ac, ai; |
287 |
> |
int |
288 |
> |
doarray( /* make array */ |
289 |
> |
int ac, |
290 |
> |
char **av, |
291 |
> |
int ai |
292 |
> |
) |
293 |
|
{ |
294 |
|
char *newav[256], **avp; |
295 |
|
char newid[128], repts[32]; |
332 |
|
} |
333 |
|
|
334 |
|
|
335 |
< |
xform(name, fin) /* transform stream by tot.xfm */ |
336 |
< |
char *name; |
337 |
< |
register FILE *fin; |
335 |
> |
void |
336 |
> |
xform( /* transform stream by tot.xfm */ |
337 |
> |
char *name, |
338 |
> |
register FILE *fin |
339 |
> |
) |
340 |
|
{ |
341 |
|
int nobjs = 0; |
342 |
|
register int c; |
367 |
|
} |
368 |
|
|
369 |
|
|
370 |
< |
xfcomm(fname, fin) /* transform a command */ |
371 |
< |
char *fname; |
372 |
< |
FILE *fin; |
370 |
> |
void |
371 |
> |
xfcomm( /* transform a command */ |
372 |
> |
char *fname, |
373 |
> |
FILE *fin |
374 |
> |
) |
375 |
|
{ |
346 |
– |
extern FILE *popen(); |
347 |
– |
extern char *fgetline(); |
376 |
|
FILE *pin; |
377 |
< |
char buf[512]; |
377 |
> |
char buf[2048]; |
378 |
|
int i; |
379 |
|
|
380 |
|
fgetline(buf, sizeof(buf), fin); |
399 |
|
} |
400 |
|
|
401 |
|
|
402 |
< |
xfobject(fname, fin) /* transform an object */ |
403 |
< |
char *fname; |
404 |
< |
FILE *fin; |
402 |
> |
void |
403 |
> |
xfobject( /* transform an object */ |
404 |
> |
char *fname, |
405 |
> |
FILE *fin |
406 |
> |
) |
407 |
|
{ |
378 |
– |
extern char *strcpy(); |
408 |
|
char typ[16], nam[MAXSTR]; |
409 |
|
int fn; |
410 |
|
/* modifier and type */ |
416 |
|
progname, fname, typ); |
417 |
|
exit(1); |
418 |
|
} |
419 |
< |
if (ismodifier(fn)) |
420 |
< |
printf("\n%s %s ", nam, typ); |
421 |
< |
else |
422 |
< |
printf("\n%s %s ", newmod != NULL ? newmod : nam, |
423 |
< |
invert ? ofun[tinvers[fn]].funame : typ); |
419 |
> |
putchar('\n'); |
420 |
> |
if (ismodifier(fn)) { |
421 |
> |
fputword(nam, stdout); |
422 |
> |
printf(" %s ", typ); |
423 |
> |
} else { |
424 |
> |
fputword(newmod != NULL ? newmod : nam, stdout); |
425 |
> |
printf(" %s ", invert ? ofun[tinvers[fn]].funame : typ); |
426 |
> |
} |
427 |
|
/* object name */ |
428 |
|
fgetword(nam, sizeof(nam), fin); |
429 |
|
if (idprefix == NULL || ismodifier(fn)) |
430 |
< |
printf("%s\n", nam); |
431 |
< |
else |
432 |
< |
printf("%s.%s\n", idprefix, nam); |
430 |
> |
fputword(nam, stdout); |
431 |
> |
else { |
432 |
> |
char nnam[MAXSTR]; |
433 |
> |
sprintf(nnam, "%s.%s", idprefix, nam); |
434 |
> |
fputword(nnam, stdout); |
435 |
> |
} |
436 |
> |
putchar('\n'); |
437 |
|
/* transform arguments */ |
438 |
|
if ((*ofun[fn].funp)(fin) < 0) { |
439 |
|
fprintf(stderr, "%s: (%s): bad %s \"%s\"\n", |
443 |
|
} |
444 |
|
|
445 |
|
|
446 |
< |
o_default(fin) /* pass on arguments unchanged */ |
447 |
< |
FILE *fin; |
446 |
> |
int |
447 |
> |
o_default( /* pass on arguments unchanged */ |
448 |
> |
FILE *fin |
449 |
> |
) |
450 |
|
{ |
451 |
|
register int i; |
452 |
|
FUNARGS fa; |
455 |
|
return(-1); |
456 |
|
/* string arguments */ |
457 |
|
printf("%d", fa.nsargs); |
458 |
< |
for (i = 0; i < fa.nsargs; i++) |
459 |
< |
printf(" %s", fa.sarg[i]); |
458 |
> |
for (i = 0; i < fa.nsargs; i++) { |
459 |
> |
fputc(' ', stdout); |
460 |
> |
fputword(fa.sarg[i], stdout); |
461 |
> |
} |
462 |
|
printf("\n"); |
463 |
|
#ifdef IARGS |
464 |
|
/* integer arguments */ |
479 |
|
} |
480 |
|
|
481 |
|
|
482 |
< |
addxform(fin) /* add xf arguments to strings */ |
483 |
< |
FILE *fin; |
482 |
> |
int |
483 |
> |
addxform( /* add xf arguments to strings */ |
484 |
> |
FILE *fin |
485 |
> |
) |
486 |
|
{ |
487 |
|
register int i; |
488 |
|
int resetarr = 0; |
494 |
|
if (xac > xfa && strcmp(xav[xfa], "-i")) |
495 |
|
resetarr = 2; |
496 |
|
printf("%d", fa.nsargs + resetarr + xac-xfa); |
497 |
< |
for (i = 0; i < fa.nsargs; i++) |
498 |
< |
printf(" %s", fa.sarg[i]); |
497 |
> |
for (i = 0; i < fa.nsargs; i++) { |
498 |
> |
fputc(' ', stdout); |
499 |
> |
fputword(fa.sarg[i], stdout); |
500 |
> |
} |
501 |
|
if (resetarr) |
502 |
|
printf(" -i 1"); |
503 |
|
for (i = xfa; i < xac; i++) /* add xf arguments */ |
523 |
|
|
524 |
|
|
525 |
|
int |
526 |
< |
otype(ofname) /* get object function number from its name */ |
527 |
< |
register char *ofname; |
526 |
> |
alias( /* transfer alias */ |
527 |
> |
FILE *fin |
528 |
> |
) |
529 |
|
{ |
485 |
– |
register int i; |
486 |
– |
|
487 |
– |
for (i = 0; i < NUMTYPES; i++) |
488 |
– |
if (!strcmp(ofun[i].funame, ofname)) |
489 |
– |
return(i); |
490 |
– |
|
491 |
– |
return(-1); /* not found */ |
492 |
– |
} |
493 |
– |
|
494 |
– |
|
495 |
– |
alias(fin) /* transfer alias */ |
496 |
– |
FILE *fin; |
497 |
– |
{ |
530 |
|
char aliasnm[MAXSTR]; |
531 |
|
|
532 |
|
if (fgetword(aliasnm, MAXSTR, fin) == NULL) |
536 |
|
} |
537 |
|
|
538 |
|
|
539 |
< |
m_glow(fin) /* transform arguments for proximity light */ |
540 |
< |
FILE *fin; |
539 |
> |
int |
540 |
> |
m_glow( /* transform arguments for proximity light */ |
541 |
> |
FILE *fin |
542 |
> |
) |
543 |
|
{ |
544 |
|
FUNARGS fa; |
545 |
|
|
556 |
|
} |
557 |
|
|
558 |
|
|
559 |
< |
m_spot(fin) /* transform arguments for spotlight */ |
560 |
< |
FILE *fin; |
559 |
> |
int |
560 |
> |
m_spot( /* transform arguments for spotlight */ |
561 |
> |
FILE *fin |
562 |
> |
) |
563 |
|
{ |
564 |
|
FVECT v; |
565 |
|
FUNARGS fa; |
578 |
|
} |
579 |
|
|
580 |
|
|
581 |
< |
m_mist(fin) /* transform arguments for mist */ |
582 |
< |
FILE *fin; |
581 |
> |
int |
582 |
> |
m_mist( /* transform arguments for mist */ |
583 |
> |
FILE *fin |
584 |
> |
) |
585 |
|
{ |
586 |
|
FUNARGS fa; |
587 |
|
int i; |
622 |
|
} |
623 |
|
|
624 |
|
|
625 |
< |
m_dielectric(fin) /* transform arguments for dielectric */ |
626 |
< |
FILE *fin; |
625 |
> |
int |
626 |
> |
m_dielectric( /* transform arguments for dielectric */ |
627 |
> |
FILE *fin |
628 |
> |
) |
629 |
|
{ |
630 |
|
FUNARGS fa; |
631 |
|
|
644 |
|
} |
645 |
|
|
646 |
|
|
647 |
< |
m_interface(fin) /* transform arguments for interface */ |
648 |
< |
FILE *fin; |
647 |
> |
int |
648 |
> |
m_interface( /* transform arguments for interface */ |
649 |
> |
FILE *fin |
650 |
> |
) |
651 |
|
{ |
652 |
|
FUNARGS fa; |
653 |
|
|
671 |
|
} |
672 |
|
|
673 |
|
|
674 |
< |
text(fin) /* transform text arguments */ |
675 |
< |
FILE *fin; |
674 |
> |
int |
675 |
> |
text( /* transform text arguments */ |
676 |
> |
FILE *fin |
677 |
> |
) |
678 |
|
{ |
679 |
|
int i; |
680 |
|
FVECT v; |
686 |
|
return(-1); |
687 |
|
/* string arguments */ |
688 |
|
printf("%d", fa.nsargs); |
689 |
< |
for (i = 0; i < fa.nsargs; i++) |
690 |
< |
printf(" %s", fa.sarg[i]); |
689 |
> |
for (i = 0; i < fa.nsargs; i++) { |
690 |
> |
fputc(' ', stdout); |
691 |
> |
fputword(fa.sarg[i], stdout); |
692 |
> |
} |
693 |
|
printf("\n0\n%d\n", fa.nfargs); |
694 |
|
/* anchor point */ |
695 |
|
multp3(v, fa.farg, tot.xfm); |
712 |
|
} |
713 |
|
|
714 |
|
|
715 |
< |
o_source(fin) /* transform source arguments */ |
716 |
< |
FILE *fin; |
715 |
> |
int |
716 |
> |
o_source( /* transform source arguments */ |
717 |
> |
FILE *fin |
718 |
> |
) |
719 |
|
{ |
720 |
|
FVECT dv; |
721 |
|
FUNARGS fa; |
735 |
|
} |
736 |
|
|
737 |
|
|
738 |
< |
o_sphere(fin) /* transform sphere arguments */ |
739 |
< |
FILE *fin; |
738 |
> |
int |
739 |
> |
o_sphere( /* transform sphere arguments */ |
740 |
> |
FILE *fin |
741 |
> |
) |
742 |
|
{ |
743 |
|
FVECT cent; |
744 |
|
double rad; |
761 |
|
} |
762 |
|
|
763 |
|
|
764 |
< |
o_face(fin) /* transform face arguments */ |
765 |
< |
FILE *fin; |
764 |
> |
int |
765 |
> |
o_face( /* transform face arguments */ |
766 |
> |
FILE *fin |
767 |
> |
) |
768 |
|
{ |
769 |
|
FVECT p; |
770 |
|
register int i; |
789 |
|
} |
790 |
|
|
791 |
|
|
792 |
< |
o_cone(fin) /* transform cone and cup arguments */ |
793 |
< |
FILE *fin; |
792 |
> |
int |
793 |
> |
o_cone( /* transform cone and cup arguments */ |
794 |
> |
FILE *fin |
795 |
> |
) |
796 |
|
{ |
797 |
|
FVECT p0, p1; |
798 |
|
double r0, r1; |
818 |
|
} |
819 |
|
|
820 |
|
|
821 |
< |
o_cylinder(fin) /* transform cylinder and tube arguments */ |
822 |
< |
FILE *fin; |
821 |
> |
int |
822 |
> |
o_cylinder( /* transform cylinder and tube arguments */ |
823 |
> |
FILE *fin |
824 |
> |
) |
825 |
|
{ |
826 |
|
FVECT p0, p1; |
827 |
|
double rad; |
845 |
|
} |
846 |
|
|
847 |
|
|
848 |
< |
o_ring(fin) /* transform ring arguments */ |
849 |
< |
FILE *fin; |
848 |
> |
int |
849 |
> |
o_ring( /* transform ring arguments */ |
850 |
> |
FILE *fin |
851 |
> |
) |
852 |
|
{ |
853 |
|
FVECT p0, pd; |
854 |
|
double r0, r1; |
878 |
|
} |
879 |
|
|
880 |
|
|
881 |
< |
initotypes() /* initialize ofun[] array */ |
881 |
> |
void |
882 |
> |
initotypes(void) /* initialize ofun[] array */ |
883 |
|
{ |
884 |
|
register int i; |
885 |
|
|
886 |
|
if (ofun[OBJ_SOURCE].funp == o_source) |
887 |
|
return; /* done already */ |
829 |
– |
/* alias is additional */ |
830 |
– |
ofun[ALIAS].funame = ALIASID; |
831 |
– |
ofun[ALIAS].flags = 0; |
888 |
|
/* functions get new transform */ |
889 |
< |
for (i = 0; i < NUMTYPES; i++) |
889 |
> |
for (i = 0; i < NUMOTYPE; i++) |
890 |
|
if (hasfunc(i)) |
891 |
|
ofun[i].funp = addxform; |
892 |
|
/* special cases */ |
899 |
|
ofun[OBJ_CYLINDER].funp = |
900 |
|
ofun[OBJ_TUBE].funp = o_cylinder; |
901 |
|
ofun[OBJ_RING].funp = o_ring; |
902 |
< |
ofun[OBJ_INSTANCE].funp = addxform; |
902 |
> |
ofun[OBJ_INSTANCE].funp = |
903 |
> |
ofun[OBJ_MESH].funp = addxform; |
904 |
|
ofun[MAT_GLOW].funp = m_glow; |
905 |
|
ofun[MAT_SPOT].funp = m_spot; |
906 |
|
ofun[MAT_DIELECTRIC].funp = m_dielectric; |
909 |
|
ofun[PAT_CTEXT].funp = |
910 |
|
ofun[PAT_BTEXT].funp = |
911 |
|
ofun[MIX_TEXT].funp = text; |
912 |
< |
ofun[ALIAS].funp = alias; |
912 |
> |
ofun[MOD_ALIAS].funp = alias; |
913 |
|
/* surface inverses */ |
914 |
|
tinvers[OBJ_FACE] = OBJ_FACE; |
915 |
|
tinvers[OBJ_SOURCE] = OBJ_SOURCE; |
925 |
|
|
926 |
|
|
927 |
|
#ifdef OLDXFORM |
928 |
< |
openmain(fname) |
929 |
< |
char *fname; |
928 |
> |
void |
929 |
> |
openmain( |
930 |
> |
char *fname |
931 |
> |
) |
932 |
|
{ |
933 |
|
if (fname == NULL) { |
934 |
|
strcpy(mainfn, "standard input"); |
950 |
|
strcpy(mainfn, fname); |
951 |
|
} |
952 |
|
#else |
953 |
< |
openmain(fname) /* open fname for input, changing to its directory */ |
954 |
< |
char *fname; |
953 |
> |
void |
954 |
> |
openmain( /* open input, changing directory for file */ |
955 |
> |
char *iname |
956 |
> |
) |
957 |
|
{ |
958 |
< |
extern FILE *tmpfile(); |
959 |
< |
extern char *getlibpath(), *getpath(); |
899 |
< |
static char origdir[MAXPATH]; |
900 |
< |
static char curfn[MAXPATH]; |
958 |
> |
static char origdir[PATH_MAX]; |
959 |
> |
static char curfn[PATH_MAX]; |
960 |
|
static int diffdir; |
961 |
|
register char *fpath; |
962 |
|
|
963 |
< |
if (fname == NULL) { /* standard input */ |
963 |
> |
if (iname == NULL) { /* standard input */ |
964 |
|
if (mainfp == NULL) { |
965 |
|
register int c; |
966 |
|
strcpy(mainfn, "standard input"); |
977 |
|
} |
978 |
|
while ((c = getc(stdin)) != EOF) |
979 |
|
putc(c, mainfp); |
921 |
– |
fclose(stdin); |
980 |
|
} |
981 |
|
rewind(mainfp); /* rewind copy */ |
982 |
|
return; |
983 |
|
} |
984 |
|
if (mainfp == NULL) { /* first call, initialize */ |
985 |
< |
getwd(origdir); |
986 |
< |
} else if (!strcmp(fname, curfn)) { /* just need to rewind? */ |
985 |
> |
getcwd(origdir, sizeof(origdir)); |
986 |
> |
} else if (!strcmp(iname, curfn)) { /* just need to rewind? */ |
987 |
|
rewind(mainfp); |
988 |
|
return; |
989 |
|
} else { /* else close old stream */ |
990 |
|
fclose(mainfp); |
991 |
< |
if (diffdir) { |
991 |
> |
mainfp = NULL; |
992 |
> |
if (diffdir) { /* return to our directory */ |
993 |
|
chdir(origdir); |
994 |
|
diffdir = 0; |
995 |
|
} |
996 |
|
} |
997 |
< |
strcpy(curfn, fname); /* remember file name */ |
998 |
< |
/* get full path */ |
999 |
< |
if ((fpath = getpath(fname, getlibpath(), R_OK)) == NULL) { |
997 |
> |
strcpy(curfn, iname); /* remember input name */ |
998 |
> |
/* get full path for file */ |
999 |
> |
if ((fpath = getpath(iname, getrlibpath(), R_OK)) == NULL) { |
1000 |
|
fprintf(stderr, "%s: cannot find file \"%s\"\n", |
1001 |
< |
progname, fname); |
1001 |
> |
progname, iname); |
1002 |
|
exit(1); |
1003 |
|
} |
1004 |
|
if (fpath[0] == '.' && ISDIRSEP(fpath[1])) /* remove leading ./ */ |
1026 |
|
diffdir++; |
1027 |
|
} |
1028 |
|
/* get final path component */ |
1029 |
< |
for (fpath = fname+strlen(fname); |
1030 |
< |
fpath > fname && !ISDIRSEP(fpath[-1]); fpath--) |
1029 |
> |
for (fpath = iname+strlen(iname); |
1030 |
> |
fpath > iname && !ISDIRSEP(fpath[-1]); fpath--) |
1031 |
|
; |
1032 |
|
} |
1033 |
< |
/* open the file */ |
1033 |
> |
/* finally, open the file */ |
1034 |
|
if ((mainfp = fopen(fpath, "r")) == NULL) { |
1035 |
|
fprintf(stderr, "%s: cannot open file \"%s\"\n", |
1036 |
|
progname, mainfn); |