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 "rtmath.h" |
16 |
+ |
#include "rtprocess.h" /* win_popen() */ |
17 |
+ |
#include "paths.h" |
18 |
|
#include "object.h" |
22 |
– |
|
19 |
|
#include "otypes.h" |
20 |
|
|
21 |
|
int xac; /* global xform argument count */ |
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]) |
51 |
|
int argc; |
52 |
|
char *argv[]; |
53 |
|
{ |
54 |
+ |
int mal_prefix = 0; |
55 |
|
char *fname; |
56 |
|
int a; |
57 |
|
/* check for argument list file */ |
63 |
|
if (!strcmp(argv[a], "-a")) |
64 |
|
return(doarray(argc, argv, a)); |
65 |
|
|
66 |
< |
initotypes(); |
66 |
> |
initotypes(); /* initialize */ |
67 |
> |
invert = 0; |
68 |
> |
expand = 1; |
69 |
> |
newmod = NULL; |
70 |
> |
idprefix = NULL; |
71 |
|
|
72 |
|
for (a = 1; a < argc; a++) { |
73 |
|
if (argv[a][0] == '-') |
74 |
|
switch (argv[a][1]) { |
75 |
|
case 'm': |
76 |
< |
if (argv[a][2] || a+1 >= argc) |
76 |
> |
if (argv[a][2] | (a+1 >= argc)) |
77 |
|
break; |
78 |
< |
newmod = argv[++a]; |
78 |
> |
a++; |
79 |
> |
if (newmod == NULL) |
80 |
> |
newmod = argv[a]; |
81 |
|
continue; |
82 |
|
case 'n': |
83 |
< |
if (argv[a][2] || a+1 >= argc) |
83 |
> |
if (argv[a][2] | (a+1 >= argc)) |
84 |
|
break; |
85 |
< |
idprefix = argv[++a]; |
85 |
> |
a++; |
86 |
> |
if (idprefix == NULL) |
87 |
> |
idprefix = argv[a]; |
88 |
> |
else { |
89 |
> |
register char *newp; |
90 |
> |
newp = (char *)malloc(strlen(idprefix)+ |
91 |
> |
strlen(argv[a])+2); |
92 |
> |
if (newp == NULL) |
93 |
> |
exit(2); |
94 |
> |
sprintf(newp, "%s.%s", |
95 |
> |
idprefix, argv[a]); |
96 |
> |
if (mal_prefix++) |
97 |
> |
free((void *)idprefix); |
98 |
> |
idprefix = newp; |
99 |
> |
} |
100 |
|
continue; |
101 |
|
case 'c': |
102 |
|
if (argv[a][2]) |
111 |
|
case 'I': |
112 |
|
if (argv[a][2]) |
113 |
|
break; |
114 |
< |
invert = 1; |
114 |
> |
invert = !invert; |
115 |
|
continue; |
116 |
|
} |
117 |
|
break; |
122 |
|
|
123 |
|
a += xf(&tot, argc-a, argv+a); |
124 |
|
|
125 |
< |
if (reverse = tot.sca < 0.0) |
125 |
> |
if ( (reverse = tot.sca < 0.0) ) |
126 |
|
tot.sca = -tot.sca; |
127 |
|
if (invert) |
128 |
|
reverse = !reverse; |
154 |
|
xform(mainfn, mainfp); |
155 |
|
} |
156 |
|
|
157 |
+ |
if (mal_prefix) |
158 |
+ |
free((void *)idprefix); |
159 |
|
return(0); |
160 |
|
} |
161 |
|
|
164 |
|
char **av; |
165 |
|
int ac, fi; |
166 |
|
{ |
167 |
+ |
int inquote; |
168 |
|
char *newav[256], **avp; |
169 |
< |
char argbuf[2048]; |
169 |
> |
char argbuf[1024]; |
170 |
|
char newid[128]; |
171 |
|
char *oldid; |
172 |
|
register char *cp; |
179 |
|
} |
180 |
|
if (av[fi+1][0] == '-' && av[fi+1][1] == '\0') { |
181 |
|
if (stdinused++) { |
182 |
< |
fprintf(stderr, "%s: cannot use stdin more than once\n", |
182 |
> |
fprintf(stderr, |
183 |
> |
"%s: cannot use stdin more than once\n", |
184 |
|
av[0]); |
185 |
|
exit(1); |
186 |
|
} |
188 |
|
n = 100; /* we just don't know! */ |
189 |
|
} else { |
190 |
|
if ((argfp = fopen(av[fi+1], "r")) == NULL) { |
191 |
< |
fprintf(stderr, "%s: cannot open argument file \"%s\"\n", |
191 |
> |
fprintf(stderr, |
192 |
> |
"%s: cannot open argument file \"%s\"\n", |
193 |
|
av[0], av[fi+1]); |
194 |
|
exit(1); |
195 |
|
} |
196 |
|
n = 0; /* count number of lines in file */ |
197 |
< |
while (fgets(argbuf,sizeof(argbuf),argfp) != NULL) |
198 |
< |
n++; |
197 |
> |
while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL) |
198 |
> |
n += argbuf[0] && argbuf[0] != '#'; |
199 |
|
if (!n) { |
200 |
|
fprintf(stderr, "%s: empty argument file \"%s\"\n", |
201 |
|
av[0], av[fi+1]); |
204 |
|
nrept *= n; |
205 |
|
rewind(argfp); |
206 |
|
} |
207 |
< |
err = 0; /* read each arg list and call main */ |
208 |
< |
for (k = 0; fgets(argbuf,sizeof(argbuf),argfp) != NULL; k++) { |
207 |
> |
err = 0; k = 0; /* read each arg list and call main */ |
208 |
> |
while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL) { |
209 |
> |
if (!argbuf[0] || argbuf[0] == '#') |
210 |
> |
continue; |
211 |
|
avp = newav+2; |
212 |
|
avp[0] = av[0]; |
213 |
|
for (i = 1; i < fi; i++) |
214 |
|
avp[i] = av[i]; |
215 |
|
newac = i; |
216 |
< |
cp = argbuf; /* parse new words */ |
216 |
> |
cp = argbuf; /* parse new words */ |
217 |
> |
if (*cp == '!') cp++; |
218 |
> |
if (!strncmp(cp, "xform ", 6)) cp += 6; |
219 |
> |
inquote = 0; |
220 |
|
for ( ; ; ) { |
221 |
+ |
skipspaces: |
222 |
|
while (isspace(*cp)) /* nullify spaces */ |
223 |
|
*cp++ = '\0'; |
224 |
+ |
if ((*cp == '"') | (*cp == '\'')) |
225 |
+ |
inquote = *cp++; |
226 |
|
if (!*cp) /* all done? */ |
227 |
|
break; |
228 |
+ |
if (cp[0] == '\\' && cp[1]) |
229 |
+ |
if (*++cp == '\n') |
230 |
+ |
goto skipspaces; |
231 |
|
avp[newac++] = cp; /* add argument to list */ |
232 |
< |
while (*++cp && !isspace(*cp)) |
233 |
< |
; |
232 |
> |
if (inquote) { |
233 |
> |
while (*++cp) |
234 |
> |
if (*cp == inquote) { |
235 |
> |
*cp++ = '\0'; |
236 |
> |
break; |
237 |
> |
} |
238 |
> |
} else { |
239 |
> |
while (*++cp && !isspace(*cp)) |
240 |
> |
; |
241 |
> |
} |
242 |
|
} |
243 |
|
for (i = fi+2; i < ac; i++) |
244 |
|
avp[newac++] = av[i]; |
262 |
|
else |
263 |
|
sprintf(newid, "%s.%d", oldid, k); |
264 |
|
err |= main(newac, avp); |
265 |
+ |
k++; |
266 |
|
} |
267 |
|
fclose(argfp); |
268 |
|
return(err); |
351 |
|
char *fname; |
352 |
|
FILE *fin; |
353 |
|
{ |
318 |
– |
extern FILE *popen(); |
319 |
– |
extern char *fgetline(); |
354 |
|
FILE *pin; |
355 |
|
char buf[512]; |
356 |
|
int i; |
367 |
|
} else { |
368 |
|
printf("\n%s", buf); |
369 |
|
if (xac > 1) { |
370 |
< |
printf(" | %s", xav[0]); |
370 |
> |
printf(" | %s -e", xav[0]); /* expand next time */ |
371 |
|
for (i = 1; i < xac; i++) |
372 |
< |
printf(" %s", xav[i]); |
372 |
> |
if (i >= xfa || strcmp(xav[i], "-c")) |
373 |
> |
printf(" %s", xav[i]); |
374 |
|
} |
375 |
|
putchar('\n'); |
376 |
|
} |
381 |
|
char *fname; |
382 |
|
FILE *fin; |
383 |
|
{ |
349 |
– |
extern char *strcpy(); |
384 |
|
char typ[16], nam[MAXSTR]; |
385 |
|
int fn; |
386 |
|
/* modifier and type */ |
422 |
|
return(-1); |
423 |
|
/* string arguments */ |
424 |
|
printf("%d", fa.nsargs); |
425 |
< |
for (i = 0; i < fa.nsargs; i++) |
426 |
< |
printf(" %s", fa.sarg[i]); |
425 |
> |
for (i = 0; i < fa.nsargs; i++) { |
426 |
> |
fputc(' ', stdout); |
427 |
> |
fputword(fa.sarg[i], stdout); |
428 |
> |
} |
429 |
|
printf("\n"); |
430 |
|
#ifdef IARGS |
431 |
|
/* integer arguments */ |
459 |
|
if (xac > xfa && strcmp(xav[xfa], "-i")) |
460 |
|
resetarr = 2; |
461 |
|
printf("%d", fa.nsargs + resetarr + xac-xfa); |
462 |
< |
for (i = 0; i < fa.nsargs; i++) |
463 |
< |
printf(" %s", fa.sarg[i]); |
462 |
> |
for (i = 0; i < fa.nsargs; i++) { |
463 |
> |
fputc(' ', stdout); |
464 |
> |
fputword(fa.sarg[i], stdout); |
465 |
> |
} |
466 |
|
if (resetarr) |
467 |
|
printf(" -i 1"); |
468 |
|
for (i = xfa; i < xac; i++) /* add xf arguments */ |
487 |
|
} |
488 |
|
|
489 |
|
|
452 |
– |
int |
453 |
– |
otype(ofname) /* get object function number from its name */ |
454 |
– |
register char *ofname; |
455 |
– |
{ |
456 |
– |
register int i; |
457 |
– |
|
458 |
– |
for (i = 0; i < NUMTYPES; i++) |
459 |
– |
if (!strcmp(ofun[i].funame, ofname)) |
460 |
– |
return(i); |
461 |
– |
|
462 |
– |
return(-1); /* not found */ |
463 |
– |
} |
464 |
– |
|
465 |
– |
|
490 |
|
alias(fin) /* transfer alias */ |
491 |
|
FILE *fin; |
492 |
|
{ |
637 |
|
return(-1); |
638 |
|
/* string arguments */ |
639 |
|
printf("%d", fa.nsargs); |
640 |
< |
for (i = 0; i < fa.nsargs; i++) |
641 |
< |
printf(" %s", fa.sarg[i]); |
640 |
> |
for (i = 0; i < fa.nsargs; i++) { |
641 |
> |
fputc(' ', stdout); |
642 |
> |
fputword(fa.sarg[i], stdout); |
643 |
> |
} |
644 |
|
printf("\n0\n%d\n", fa.nfargs); |
645 |
|
/* anchor point */ |
646 |
|
multp3(v, fa.farg, tot.xfm); |
823 |
|
|
824 |
|
if (ofun[OBJ_SOURCE].funp == o_source) |
825 |
|
return; /* done already */ |
800 |
– |
/* alias is additional */ |
801 |
– |
ofun[ALIAS].funame = ALIASID; |
802 |
– |
ofun[ALIAS].flags = 0; |
826 |
|
/* functions get new transform */ |
827 |
< |
for (i = 0; i < NUMTYPES; i++) |
827 |
> |
for (i = 0; i < NUMOTYPE; i++) |
828 |
|
if (hasfunc(i)) |
829 |
|
ofun[i].funp = addxform; |
830 |
|
/* special cases */ |
837 |
|
ofun[OBJ_CYLINDER].funp = |
838 |
|
ofun[OBJ_TUBE].funp = o_cylinder; |
839 |
|
ofun[OBJ_RING].funp = o_ring; |
840 |
< |
ofun[OBJ_INSTANCE].funp = addxform; |
840 |
> |
ofun[OBJ_INSTANCE].funp = |
841 |
> |
ofun[OBJ_MESH].funp = addxform; |
842 |
|
ofun[MAT_GLOW].funp = m_glow; |
843 |
|
ofun[MAT_SPOT].funp = m_spot; |
844 |
|
ofun[MAT_DIELECTRIC].funp = m_dielectric; |
847 |
|
ofun[PAT_CTEXT].funp = |
848 |
|
ofun[PAT_BTEXT].funp = |
849 |
|
ofun[MIX_TEXT].funp = text; |
850 |
< |
ofun[ALIAS].funp = alias; |
850 |
> |
ofun[MOD_ALIAS].funp = alias; |
851 |
|
/* surface inverses */ |
852 |
|
tinvers[OBJ_FACE] = OBJ_FACE; |
853 |
|
tinvers[OBJ_SOURCE] = OBJ_SOURCE; |
886 |
|
strcpy(mainfn, fname); |
887 |
|
} |
888 |
|
#else |
889 |
< |
openmain(fname) /* open fname for input, changing to its directory */ |
890 |
< |
char *fname; |
889 |
> |
openmain(iname) /* open input, changing directory for file */ |
890 |
> |
char *iname; |
891 |
|
{ |
892 |
< |
extern FILE *tmpfile(); |
893 |
< |
extern char *getlibpath(), *getpath(); |
870 |
< |
static char origdir[MAXPATH]; |
871 |
< |
static char curfn[MAXPATH]; |
892 |
> |
static char origdir[PATH_MAX]; |
893 |
> |
static char curfn[PATH_MAX]; |
894 |
|
static int diffdir; |
895 |
|
register char *fpath; |
896 |
|
|
897 |
< |
if (fname == NULL) { /* standard input */ |
897 |
> |
if (iname == NULL) { /* standard input */ |
898 |
|
if (mainfp == NULL) { |
899 |
|
register int c; |
900 |
|
strcpy(mainfn, "standard input"); |
911 |
|
} |
912 |
|
while ((c = getc(stdin)) != EOF) |
913 |
|
putc(c, mainfp); |
892 |
– |
fclose(stdin); |
914 |
|
} |
915 |
|
rewind(mainfp); /* rewind copy */ |
916 |
|
return; |
917 |
|
} |
918 |
|
if (mainfp == NULL) { /* first call, initialize */ |
919 |
< |
getwd(origdir); |
920 |
< |
} else if (!strcmp(fname, curfn)) { /* just need to rewind? */ |
919 |
> |
getcwd(origdir, sizeof(origdir)); |
920 |
> |
} else if (!strcmp(iname, curfn)) { /* just need to rewind? */ |
921 |
|
rewind(mainfp); |
922 |
|
return; |
923 |
|
} else { /* else close old stream */ |
924 |
|
fclose(mainfp); |
925 |
< |
if (diffdir) { |
925 |
> |
mainfp = NULL; |
926 |
> |
if (diffdir) { /* return to our directory */ |
927 |
|
chdir(origdir); |
928 |
|
diffdir = 0; |
929 |
|
} |
930 |
|
} |
931 |
< |
strcpy(curfn, fname); /* remember file name */ |
932 |
< |
/* get full path */ |
933 |
< |
if ((fpath = getpath(fname, getlibpath(), R_OK)) == NULL) { |
931 |
> |
strcpy(curfn, iname); /* remember input name */ |
932 |
> |
/* get full path for file */ |
933 |
> |
if ((fpath = getpath(iname, getrlibpath(), R_OK)) == NULL) { |
934 |
|
fprintf(stderr, "%s: cannot find file \"%s\"\n", |
935 |
< |
progname, fname); |
935 |
> |
progname, iname); |
936 |
|
exit(1); |
937 |
|
} |
938 |
|
if (fpath[0] == '.' && ISDIRSEP(fpath[1])) /* remove leading ./ */ |
960 |
|
diffdir++; |
961 |
|
} |
962 |
|
/* get final path component */ |
963 |
< |
for (fpath = fname+strlen(fname); |
964 |
< |
fpath > fname && !ISDIRSEP(fpath[-1]); fpath--) |
963 |
> |
for (fpath = iname+strlen(iname); |
964 |
> |
fpath > iname && !ISDIRSEP(fpath[-1]); fpath--) |
965 |
|
; |
966 |
|
} |
967 |
< |
/* open the file */ |
967 |
> |
/* finally, open the file */ |
968 |
|
if ((mainfp = fopen(fpath, "r")) == NULL) { |
969 |
|
fprintf(stderr, "%s: cannot open file \"%s\"\n", |
970 |
|
progname, mainfn); |