7 |
|
* 1/3/86 |
8 |
|
*/ |
9 |
|
|
10 |
+ |
#include <ctype.h> |
11 |
|
#include "rtio.h" |
12 |
|
#include "platform.h" |
13 |
|
#include "rtprocess.h" |
14 |
|
#include "resolu.h" |
15 |
|
|
16 |
< |
#ifdef getc_unlocked /* avoid nasty file-locking overhead */ |
16 |
< |
#undef getc |
17 |
< |
#undef getchar |
18 |
< |
#undef putchar |
19 |
< |
#define getc getc_unlocked |
20 |
< |
#define getchar getchar_unlocked |
21 |
< |
#define putchar putchar_unlocked |
22 |
< |
#endif |
16 |
> |
static char fmt[MAXFMTLEN] = "*"; |
17 |
|
|
24 |
– |
static gethfunc tabstr; |
25 |
– |
static void getdim(FILE *fp); |
26 |
– |
static void copycat(void); |
18 |
|
|
28 |
– |
|
19 |
|
static int |
20 |
|
tabstr( /* put out line followed by tab */ |
21 |
|
char *s, |
27 |
|
s++; |
28 |
|
} |
29 |
|
if (*--s == '\n') |
30 |
< |
putchar('\t'); |
30 |
> |
fputc('\t', stdout); |
31 |
|
return(0); |
32 |
|
} |
33 |
|
|
34 |
|
|
35 |
+ |
static int |
36 |
+ |
adjheadline( /* check for lines to remove */ |
37 |
+ |
char *s, |
38 |
+ |
void *p |
39 |
+ |
) |
40 |
+ |
{ |
41 |
+ |
char **av; |
42 |
+ |
|
43 |
+ |
if (formatval(fmt, s)) |
44 |
+ |
return(0); /* don't echo format */ |
45 |
+ |
|
46 |
+ |
/* check for match to skip */ |
47 |
+ |
for (av = (char **)p; *av; av++) { |
48 |
+ |
char *s1 = s; |
49 |
+ |
char *s2 = *av; |
50 |
+ |
if (isspace(*s2)) { |
51 |
+ |
if (!isspace(*s1)) |
52 |
+ |
continue; |
53 |
+ |
while (isspace(*++s1)) ; |
54 |
+ |
while (isspace(*++s2)) ; |
55 |
+ |
} |
56 |
+ |
while (*s2 && *s1 == *s2) { |
57 |
+ |
if ((*s1 == '=') & (s1 > s)) |
58 |
+ |
return(0); |
59 |
+ |
if (isspace(*s2)) |
60 |
+ |
break; |
61 |
+ |
s1++; s2++; |
62 |
+ |
} |
63 |
+ |
if (s1 == s) |
64 |
+ |
continue; |
65 |
+ |
if (isspace(*s1) && !*s2 | isspace(*s2)) |
66 |
+ |
return(0); /* skip */ |
67 |
+ |
} |
68 |
+ |
fputs(s, stdout); /* copy if no match */ |
69 |
+ |
return(0); |
70 |
+ |
} |
71 |
+ |
|
72 |
+ |
|
73 |
+ |
static void |
74 |
+ |
getdim( /* get dimensions from file */ |
75 |
+ |
FILE *fp |
76 |
+ |
) |
77 |
+ |
{ |
78 |
+ |
int j; |
79 |
+ |
int c; |
80 |
+ |
|
81 |
+ |
switch (c = getc(fp)) { |
82 |
+ |
case '+': /* picture */ |
83 |
+ |
case '-': |
84 |
+ |
do |
85 |
+ |
putchar(c); |
86 |
+ |
while (c != '\n' && (c = getc(fp)) != EOF); |
87 |
+ |
break; |
88 |
+ |
case 1: /* octree */ |
89 |
+ |
getc(fp); |
90 |
+ |
j = 0; |
91 |
+ |
while ((c = getc(fp)) != EOF) |
92 |
+ |
if (c == 0) { |
93 |
+ |
if (++j >= 4) |
94 |
+ |
break; |
95 |
+ |
fputc(' ', stdout); |
96 |
+ |
} else { |
97 |
+ |
putchar(c); |
98 |
+ |
} |
99 |
+ |
fputc('\n', stdout); |
100 |
+ |
break; |
101 |
+ |
default: /* ??? */ |
102 |
+ |
fputs("unknown file type\n", stdout); |
103 |
+ |
break; |
104 |
+ |
} |
105 |
+ |
} |
106 |
+ |
|
107 |
+ |
|
108 |
+ |
static void |
109 |
+ |
copycat(void) /* copy input to output */ |
110 |
+ |
{ |
111 |
+ |
char buf[8192]; |
112 |
+ |
int n; |
113 |
+ |
|
114 |
+ |
fflush(stdout); |
115 |
+ |
while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0) |
116 |
+ |
if (writebuf(fileno(stdout), buf, n) != n) |
117 |
+ |
break; |
118 |
+ |
} |
119 |
+ |
|
120 |
+ |
|
121 |
|
int |
122 |
|
main( |
123 |
|
int argc, |
125 |
|
) |
126 |
|
{ |
127 |
|
int dim = 0; |
52 |
– |
char fmt[MAXFMTLEN]; |
128 |
|
FILE *fp; |
129 |
|
int i; |
130 |
|
|
137 |
|
flockfile(stdin); |
138 |
|
#endif |
139 |
|
SET_FILE_BINARY(stdin); |
65 |
– |
fmt[0] = '*'; fmt[1] = '\0'; |
140 |
|
if (argc > 2 && !strcmp(argv[1], "-c")) { |
141 |
|
SET_FILE_BINARY(stdout); |
142 |
|
setvbuf(stdin, NULL, _IONBF, 2); |
161 |
|
execvp(argv[2], argv+2); |
162 |
|
perror(argv[2]); |
163 |
|
return 1; |
164 |
< |
} else if (argc > 2 && !strcmp(argv[1], "-a")) { |
164 |
> |
} |
165 |
> |
if (argc > 2 && !strcmp(argv[1], "-a")) { |
166 |
|
SET_FILE_BINARY(stdout); |
167 |
|
if (checkheader(stdin, fmt, stdout) < 0) { |
168 |
|
fputs("Bad header!\n", stderr); |
180 |
|
fputc('\n', stdout); |
181 |
|
copycat(); |
182 |
|
return 0; |
183 |
< |
} else if (argc == 2 && !strcmp(argv[1], "-")) { |
183 |
> |
} |
184 |
> |
if (argc > 2 && !strcmp(argv[1], "-r")) { |
185 |
|
SET_FILE_BINARY(stdout); |
186 |
+ |
if (getheader(stdin, adjheadline, argv+2) < 0) { |
187 |
+ |
fputs("Bad header!\n", stderr); |
188 |
+ |
return 1; |
189 |
+ |
} |
190 |
+ |
for (i = 2; i < argc; i++) { /* add lines w/ var[= ]value */ |
191 |
+ |
int len = strlen(argv[i]); |
192 |
+ |
int strt = 0, j; |
193 |
+ |
while(isspace(argv[i][strt])) strt++; |
194 |
+ |
if (strt == len) continue; |
195 |
+ |
while (isspace(argv[i][len-1])) len--; |
196 |
+ |
for (j = strt+1; j < len-1; j++) |
197 |
+ |
if (argv[i][j] == '=' || isspace(argv[i][j])) { |
198 |
+ |
for (j = 0; j < len; j++) |
199 |
+ |
putchar(argv[i][j]); |
200 |
+ |
fputc('\n', stdout); |
201 |
+ |
break; |
202 |
+ |
} |
203 |
+ |
} |
204 |
+ |
if (fmt[0] != '*') |
205 |
+ |
fputformat(fmt, stdout); |
206 |
+ |
fputc('\n', stdout); |
207 |
+ |
copycat(); |
208 |
+ |
return 0; |
209 |
+ |
} |
210 |
+ |
if (argc == 2 && !strcmp(argv[1], "-")) { |
211 |
+ |
SET_FILE_BINARY(stdout); |
212 |
|
if (getheader(stdin, NULL, NULL) < 0) { |
213 |
|
fputs("Bad header!\n", stderr); |
214 |
|
return 1; |
269 |
|
} |
270 |
|
} |
271 |
|
return 0; |
170 |
– |
} |
171 |
– |
|
172 |
– |
|
173 |
– |
static void |
174 |
– |
getdim( /* get dimensions from file */ |
175 |
– |
FILE *fp |
176 |
– |
) |
177 |
– |
{ |
178 |
– |
int j; |
179 |
– |
int c; |
180 |
– |
|
181 |
– |
switch (c = getc(fp)) { |
182 |
– |
case '+': /* picture */ |
183 |
– |
case '-': |
184 |
– |
do |
185 |
– |
putchar(c); |
186 |
– |
while (c != '\n' && (c = getc(fp)) != EOF); |
187 |
– |
break; |
188 |
– |
case 1: /* octree */ |
189 |
– |
getc(fp); |
190 |
– |
j = 0; |
191 |
– |
while ((c = getc(fp)) != EOF) |
192 |
– |
if (c == 0) { |
193 |
– |
if (++j >= 4) |
194 |
– |
break; |
195 |
– |
putchar(' '); |
196 |
– |
} else { |
197 |
– |
putchar(c); |
198 |
– |
} |
199 |
– |
putchar('\n'); |
200 |
– |
break; |
201 |
– |
default: /* ??? */ |
202 |
– |
fputs("unknown file type\n", stdout); |
203 |
– |
break; |
204 |
– |
} |
205 |
– |
} |
206 |
– |
|
207 |
– |
|
208 |
– |
static void |
209 |
– |
copycat(void) /* copy input to output */ |
210 |
– |
{ |
211 |
– |
char buf[8192]; |
212 |
– |
int n; |
213 |
– |
|
214 |
– |
fflush(stdout); |
215 |
– |
while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0) |
216 |
– |
if (writebuf(fileno(stdout), buf, n) != n) |
217 |
– |
break; |
272 |
|
} |