| 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 |
+ |
#ifndef _WIN32 |
| 14 |
+ |
#include <unistd.h> |
| 15 |
+ |
#endif |
| 16 |
|
|
| 17 |
+ |
#include "standard.h" |
| 18 |
+ |
#include "platform.h" |
| 19 |
+ |
#include "paths.h" |
| 20 |
|
#include "object.h" |
| 22 |
– |
|
| 21 |
|
#include "otypes.h" |
| 22 |
|
|
| 23 |
|
int xac; /* global xform argument count */ |
| 27 |
|
XF tot; /* total transformation */ |
| 28 |
|
int reverse; /* boolean true if scene mirrored */ |
| 29 |
|
|
| 30 |
< |
int invert = 0; /* boolean true to invert surfaces */ |
| 30 |
> |
int invert; /* boolean true to invert surfaces */ |
| 31 |
|
|
| 32 |
< |
int expand = 1; /* boolean true to expand commands */ |
| 32 |
> |
int expand; /* boolean true to expand commands */ |
| 33 |
|
|
| 34 |
< |
char *newmod = NULL; /* new modifier for surfaces */ |
| 34 |
> |
char *newmod; /* new modifier for surfaces */ |
| 35 |
|
|
| 36 |
< |
char *idprefix = NULL; /* prefix for object identifiers */ |
| 36 |
> |
char *idprefix; /* prefix for object identifiers */ |
| 37 |
|
|
| 38 |
< |
#define ALIAS NUMOTYPE /* put alias at end of array */ |
| 38 |
> |
FUN ofun[NUMOTYPE] = INIT_OTYPE; /* default types and actions */ |
| 39 |
|
|
| 42 |
– |
#define NUMTYPES (NUMOTYPE+1) /* total number of object types */ |
| 43 |
– |
|
| 44 |
– |
FUN ofun[NUMTYPES] = INIT_OTYPE; /* default types and actions */ |
| 45 |
– |
|
| 40 |
|
short tinvers[NUMOTYPE]; /* inverse types for surfaces */ |
| 41 |
|
|
| 42 |
|
int nrept = 1; /* number of array repetitions */ |
| 43 |
|
|
| 44 |
|
int stdinused = 0; /* stdin has been used by -f option? */ |
| 45 |
|
|
| 46 |
< |
extern char *malloc(), *fgets(), *fgetword(); |
| 53 |
< |
|
| 54 |
< |
char mainfn[MAXPATH]; /* main file name */ |
| 46 |
> |
char mainfn[PATH_MAX]; /* main file name */ |
| 47 |
|
FILE *mainfp = NULL; /* main file pointer */ |
| 48 |
|
|
| 49 |
|
#define progname (xav[0]) |
| 53 |
|
int argc; |
| 54 |
|
char *argv[]; |
| 55 |
|
{ |
| 56 |
+ |
int mal_prefix = 0; |
| 57 |
|
char *fname; |
| 58 |
|
int a; |
| 59 |
|
/* check for argument list file */ |
| 65 |
|
if (!strcmp(argv[a], "-a")) |
| 66 |
|
return(doarray(argc, argv, a)); |
| 67 |
|
|
| 68 |
< |
initotypes(); |
| 68 |
> |
initotypes(); /* initialize */ |
| 69 |
> |
invert = 0; |
| 70 |
> |
expand = 1; |
| 71 |
> |
newmod = NULL; |
| 72 |
> |
idprefix = NULL; |
| 73 |
|
|
| 74 |
|
for (a = 1; a < argc; a++) { |
| 75 |
|
if (argv[a][0] == '-') |
| 76 |
|
switch (argv[a][1]) { |
| 77 |
|
case 'm': |
| 78 |
< |
if (argv[a][2] || a+1 >= argc) |
| 78 |
> |
if (argv[a][2] | a+1 >= argc) |
| 79 |
|
break; |
| 80 |
< |
newmod = argv[++a]; |
| 80 |
> |
a++; |
| 81 |
> |
if (newmod == NULL) |
| 82 |
> |
newmod = argv[a]; |
| 83 |
|
continue; |
| 84 |
|
case 'n': |
| 85 |
< |
if (argv[a][2] || a+1 >= argc) |
| 85 |
> |
if (argv[a][2] | a+1 >= argc) |
| 86 |
|
break; |
| 87 |
< |
idprefix = argv[++a]; |
| 87 |
> |
a++; |
| 88 |
> |
if (idprefix == NULL) |
| 89 |
> |
idprefix = argv[a]; |
| 90 |
> |
else { |
| 91 |
> |
register char *newp; |
| 92 |
> |
newp = (char *)malloc(strlen(idprefix)+ |
| 93 |
> |
strlen(argv[a])+2); |
| 94 |
> |
if (newp == NULL) |
| 95 |
> |
exit(2); |
| 96 |
> |
sprintf(newp, "%s.%s", |
| 97 |
> |
idprefix, argv[a]); |
| 98 |
> |
if (mal_prefix++) |
| 99 |
> |
free((void *)idprefix); |
| 100 |
> |
idprefix = newp; |
| 101 |
> |
} |
| 102 |
|
continue; |
| 103 |
|
case 'c': |
| 104 |
|
if (argv[a][2]) |
| 113 |
|
case 'I': |
| 114 |
|
if (argv[a][2]) |
| 115 |
|
break; |
| 116 |
< |
invert = 1; |
| 116 |
> |
invert = !invert; |
| 117 |
|
continue; |
| 118 |
|
} |
| 119 |
|
break; |
| 156 |
|
xform(mainfn, mainfp); |
| 157 |
|
} |
| 158 |
|
|
| 159 |
+ |
if (mal_prefix) |
| 160 |
+ |
free((void *)idprefix); |
| 161 |
|
return(0); |
| 162 |
|
} |
| 163 |
|
|
| 166 |
|
char **av; |
| 167 |
|
int ac, fi; |
| 168 |
|
{ |
| 169 |
+ |
int inquote; |
| 170 |
|
char *newav[256], **avp; |
| 171 |
< |
char argbuf[2048]; |
| 171 |
> |
char argbuf[1024]; |
| 172 |
|
char newid[128]; |
| 173 |
|
char *oldid; |
| 174 |
|
register char *cp; |
| 181 |
|
} |
| 182 |
|
if (av[fi+1][0] == '-' && av[fi+1][1] == '\0') { |
| 183 |
|
if (stdinused++) { |
| 184 |
< |
fprintf(stderr, "%s: cannot use stdin more than once\n", |
| 184 |
> |
fprintf(stderr, |
| 185 |
> |
"%s: cannot use stdin more than once\n", |
| 186 |
|
av[0]); |
| 187 |
|
exit(1); |
| 188 |
|
} |
| 190 |
|
n = 100; /* we just don't know! */ |
| 191 |
|
} else { |
| 192 |
|
if ((argfp = fopen(av[fi+1], "r")) == NULL) { |
| 193 |
< |
fprintf(stderr, "%s: cannot open argument file \"%s\"\n", |
| 193 |
> |
fprintf(stderr, |
| 194 |
> |
"%s: cannot open argument file \"%s\"\n", |
| 195 |
|
av[0], av[fi+1]); |
| 196 |
|
exit(1); |
| 197 |
|
} |
| 198 |
|
n = 0; /* count number of lines in file */ |
| 199 |
< |
while (fgets(argbuf,sizeof(argbuf),argfp) != NULL) |
| 200 |
< |
n += argbuf[0] != '\n' & argbuf[0] != '#'; |
| 199 |
> |
while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL) |
| 200 |
> |
n += argbuf[0] && argbuf[0] != '#'; |
| 201 |
|
if (!n) { |
| 202 |
|
fprintf(stderr, "%s: empty argument file \"%s\"\n", |
| 203 |
|
av[0], av[fi+1]); |
| 207 |
|
rewind(argfp); |
| 208 |
|
} |
| 209 |
|
err = 0; k = 0; /* read each arg list and call main */ |
| 210 |
< |
while (fgets(argbuf,sizeof(argbuf),argfp) != NULL) { |
| 211 |
< |
if (argbuf[0] == '\n' | argbuf[0] == '#') |
| 210 |
> |
while (fgetline(argbuf,sizeof(argbuf),argfp) != NULL) { |
| 211 |
> |
if (!argbuf[0] || argbuf[0] == '#') |
| 212 |
|
continue; |
| 213 |
|
avp = newav+2; |
| 214 |
|
avp[0] = av[0]; |
| 218 |
|
cp = argbuf; /* parse new words */ |
| 219 |
|
if (*cp == '!') cp++; |
| 220 |
|
if (!strncmp(cp, "xform ", 6)) cp += 6; |
| 221 |
+ |
inquote = 0; |
| 222 |
|
for ( ; ; ) { |
| 223 |
+ |
skipspaces: |
| 224 |
|
while (isspace(*cp)) /* nullify spaces */ |
| 225 |
|
*cp++ = '\0'; |
| 226 |
+ |
if ((*cp == '"' | *cp == '\'')) |
| 227 |
+ |
inquote = *cp++; |
| 228 |
|
if (!*cp) /* all done? */ |
| 229 |
|
break; |
| 230 |
+ |
if (cp[0] == '\\' && cp[1]) |
| 231 |
+ |
if (*++cp == '\n') |
| 232 |
+ |
goto skipspaces; |
| 233 |
|
avp[newac++] = cp; /* add argument to list */ |
| 234 |
< |
while (*++cp && !isspace(*cp)) |
| 235 |
< |
; |
| 234 |
> |
if (inquote) { |
| 235 |
> |
while (*++cp) |
| 236 |
> |
if (*cp == inquote) { |
| 237 |
> |
*cp++ = '\0'; |
| 238 |
> |
break; |
| 239 |
> |
} |
| 240 |
> |
} else { |
| 241 |
> |
while (*++cp && !isspace(*cp)) |
| 242 |
> |
; |
| 243 |
> |
} |
| 244 |
|
} |
| 245 |
|
for (i = fi+2; i < ac; i++) |
| 246 |
|
avp[newac++] = av[i]; |
| 353 |
|
char *fname; |
| 354 |
|
FILE *fin; |
| 355 |
|
{ |
| 323 |
– |
extern FILE *popen(); |
| 324 |
– |
extern char *fgetline(); |
| 356 |
|
FILE *pin; |
| 357 |
|
char buf[512]; |
| 358 |
|
int i; |
| 369 |
|
} else { |
| 370 |
|
printf("\n%s", buf); |
| 371 |
|
if (xac > 1) { |
| 372 |
< |
printf(" | %s", xav[0]); |
| 372 |
> |
printf(" | %s -e", xav[0]); /* expand next time */ |
| 373 |
|
for (i = 1; i < xac; i++) |
| 374 |
< |
printf(" %s", xav[i]); |
| 374 |
> |
if (i >= xfa || strcmp(xav[i], "-c")) |
| 375 |
> |
printf(" %s", xav[i]); |
| 376 |
|
} |
| 377 |
|
putchar('\n'); |
| 378 |
|
} |
| 383 |
|
char *fname; |
| 384 |
|
FILE *fin; |
| 385 |
|
{ |
| 354 |
– |
extern char *strcpy(); |
| 386 |
|
char typ[16], nam[MAXSTR]; |
| 387 |
|
int fn; |
| 388 |
|
/* modifier and type */ |
| 424 |
|
return(-1); |
| 425 |
|
/* string arguments */ |
| 426 |
|
printf("%d", fa.nsargs); |
| 427 |
< |
for (i = 0; i < fa.nsargs; i++) |
| 428 |
< |
printf(" %s", fa.sarg[i]); |
| 427 |
> |
for (i = 0; i < fa.nsargs; i++) { |
| 428 |
> |
fputc(' ', stdout); |
| 429 |
> |
fputword(fa.sarg[i], stdout); |
| 430 |
> |
} |
| 431 |
|
printf("\n"); |
| 432 |
|
#ifdef IARGS |
| 433 |
|
/* integer arguments */ |
| 461 |
|
if (xac > xfa && strcmp(xav[xfa], "-i")) |
| 462 |
|
resetarr = 2; |
| 463 |
|
printf("%d", fa.nsargs + resetarr + xac-xfa); |
| 464 |
< |
for (i = 0; i < fa.nsargs; i++) |
| 465 |
< |
printf(" %s", fa.sarg[i]); |
| 464 |
> |
for (i = 0; i < fa.nsargs; i++) { |
| 465 |
> |
fputc(' ', stdout); |
| 466 |
> |
fputword(fa.sarg[i], stdout); |
| 467 |
> |
} |
| 468 |
|
if (resetarr) |
| 469 |
|
printf(" -i 1"); |
| 470 |
|
for (i = xfa; i < xac; i++) /* add xf arguments */ |
| 489 |
|
} |
| 490 |
|
|
| 491 |
|
|
| 457 |
– |
int |
| 458 |
– |
otype(ofname) /* get object function number from its name */ |
| 459 |
– |
register char *ofname; |
| 460 |
– |
{ |
| 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 |
– |
|
| 492 |
|
alias(fin) /* transfer alias */ |
| 493 |
|
FILE *fin; |
| 494 |
|
{ |
| 639 |
|
return(-1); |
| 640 |
|
/* string arguments */ |
| 641 |
|
printf("%d", fa.nsargs); |
| 642 |
< |
for (i = 0; i < fa.nsargs; i++) |
| 643 |
< |
printf(" %s", fa.sarg[i]); |
| 642 |
> |
for (i = 0; i < fa.nsargs; i++) { |
| 643 |
> |
fputc(' ', stdout); |
| 644 |
> |
fputword(fa.sarg[i], stdout); |
| 645 |
> |
} |
| 646 |
|
printf("\n0\n%d\n", fa.nfargs); |
| 647 |
|
/* anchor point */ |
| 648 |
|
multp3(v, fa.farg, tot.xfm); |
| 825 |
|
|
| 826 |
|
if (ofun[OBJ_SOURCE].funp == o_source) |
| 827 |
|
return; /* done already */ |
| 805 |
– |
/* alias is additional */ |
| 806 |
– |
ofun[ALIAS].funame = ALIASID; |
| 807 |
– |
ofun[ALIAS].flags = 0; |
| 828 |
|
/* functions get new transform */ |
| 829 |
< |
for (i = 0; i < NUMTYPES; i++) |
| 829 |
> |
for (i = 0; i < NUMOTYPE; i++) |
| 830 |
|
if (hasfunc(i)) |
| 831 |
|
ofun[i].funp = addxform; |
| 832 |
|
/* special cases */ |
| 848 |
|
ofun[PAT_CTEXT].funp = |
| 849 |
|
ofun[PAT_BTEXT].funp = |
| 850 |
|
ofun[MIX_TEXT].funp = text; |
| 851 |
< |
ofun[ALIAS].funp = alias; |
| 851 |
> |
ofun[MOD_ALIAS].funp = alias; |
| 852 |
|
/* surface inverses */ |
| 853 |
|
tinvers[OBJ_FACE] = OBJ_FACE; |
| 854 |
|
tinvers[OBJ_SOURCE] = OBJ_SOURCE; |
| 887 |
|
strcpy(mainfn, fname); |
| 888 |
|
} |
| 889 |
|
#else |
| 890 |
< |
openmain(fname) /* open fname for input, changing to its directory */ |
| 891 |
< |
char *fname; |
| 890 |
> |
openmain(iname) /* open input, changing directory for file */ |
| 891 |
> |
char *iname; |
| 892 |
|
{ |
| 893 |
< |
extern FILE *tmpfile(); |
| 894 |
< |
extern char *getlibpath(), *getpath(); |
| 875 |
< |
static char origdir[MAXPATH]; |
| 876 |
< |
static char curfn[MAXPATH]; |
| 893 |
> |
static char origdir[PATH_MAX]; |
| 894 |
> |
static char curfn[PATH_MAX]; |
| 895 |
|
static int diffdir; |
| 896 |
|
register char *fpath; |
| 897 |
|
|
| 898 |
< |
if (fname == NULL) { /* standard input */ |
| 898 |
> |
if (iname == NULL) { /* standard input */ |
| 899 |
|
if (mainfp == NULL) { |
| 900 |
|
register int c; |
| 901 |
|
strcpy(mainfn, "standard input"); |
| 912 |
|
} |
| 913 |
|
while ((c = getc(stdin)) != EOF) |
| 914 |
|
putc(c, mainfp); |
| 897 |
– |
fclose(stdin); |
| 915 |
|
} |
| 916 |
|
rewind(mainfp); /* rewind copy */ |
| 917 |
|
return; |
| 918 |
|
} |
| 919 |
|
if (mainfp == NULL) { /* first call, initialize */ |
| 920 |
< |
getwd(origdir); |
| 921 |
< |
} else if (!strcmp(fname, curfn)) { /* just need to rewind? */ |
| 920 |
> |
getcwd(origdir, sizeof(origdir)); |
| 921 |
> |
} else if (!strcmp(iname, curfn)) { /* just need to rewind? */ |
| 922 |
|
rewind(mainfp); |
| 923 |
|
return; |
| 924 |
|
} else { /* else close old stream */ |
| 925 |
|
fclose(mainfp); |
| 926 |
< |
if (diffdir) { |
| 926 |
> |
mainfp = NULL; |
| 927 |
> |
if (diffdir) { /* return to our directory */ |
| 928 |
|
chdir(origdir); |
| 929 |
|
diffdir = 0; |
| 930 |
|
} |
| 931 |
|
} |
| 932 |
< |
strcpy(curfn, fname); /* remember file name */ |
| 933 |
< |
/* get full path */ |
| 934 |
< |
if ((fpath = getpath(fname, getlibpath(), R_OK)) == NULL) { |
| 932 |
> |
strcpy(curfn, iname); /* remember input name */ |
| 933 |
> |
/* get full path for file */ |
| 934 |
> |
if ((fpath = getpath(iname, getrlibpath(), R_OK)) == NULL) { |
| 935 |
|
fprintf(stderr, "%s: cannot find file \"%s\"\n", |
| 936 |
< |
progname, fname); |
| 936 |
> |
progname, iname); |
| 937 |
|
exit(1); |
| 938 |
|
} |
| 939 |
|
if (fpath[0] == '.' && ISDIRSEP(fpath[1])) /* remove leading ./ */ |
| 961 |
|
diffdir++; |
| 962 |
|
} |
| 963 |
|
/* get final path component */ |
| 964 |
< |
for (fpath = fname+strlen(fname); |
| 965 |
< |
fpath > fname && !ISDIRSEP(fpath[-1]); fpath--) |
| 964 |
> |
for (fpath = iname+strlen(iname); |
| 965 |
> |
fpath > iname && !ISDIRSEP(fpath[-1]); fpath--) |
| 966 |
|
; |
| 967 |
|
} |
| 968 |
< |
/* open the file */ |
| 968 |
> |
/* finally, open the file */ |
| 969 |
|
if ((mainfp = fopen(fpath, "r")) == NULL) { |
| 970 |
|
fprintf(stderr, "%s: cannot open file \"%s\"\n", |
| 971 |
|
progname, mainfn); |