7 |
|
* 1/3/86 |
8 |
|
*/ |
9 |
|
|
10 |
< |
#include <stdio.h> |
11 |
< |
#include <string.h> |
12 |
< |
|
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 */ |
17 |
< |
#undef getchar |
18 |
< |
#undef putchar |
19 |
< |
#define getchar getchar_unlocked |
20 |
< |
#define putchar putchar_unlocked |
21 |
< |
#endif |
16 |
> |
static char fmt[MAXFMTLEN] = "*"; |
17 |
|
|
23 |
– |
#if defined(_WIN32) || defined(_WIN64) |
24 |
– |
#include <process.h> |
25 |
– |
#define execvp _execvp |
26 |
– |
#endif |
18 |
|
|
28 |
– |
static gethfunc tabstr; |
29 |
– |
static void getdim(FILE *fp); |
30 |
– |
static void copycat(void); |
31 |
– |
|
32 |
– |
|
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 |
< |
int |
36 |
< |
main( |
37 |
< |
int argc, |
38 |
< |
char **argv |
35 |
> |
static int |
36 |
> |
adjheadline( /* check for lines to remove */ |
37 |
> |
char *s, |
38 |
> |
void *p |
39 |
|
) |
40 |
|
{ |
41 |
< |
int dim = 0; |
56 |
< |
FILE *fp; |
57 |
< |
int i; |
41 |
> |
char **av; |
42 |
|
|
43 |
< |
if (argc > 1 && !strcmp(argv[1], "-d")) { |
44 |
< |
argc--; argv++; |
45 |
< |
dim = 1; |
46 |
< |
SET_FILE_BINARY(stdin); |
47 |
< |
} else if (argc > 2 && !strcmp(argv[1], "-c")) { |
48 |
< |
SET_FILE_BINARY(stdin); |
49 |
< |
SET_FILE_BINARY(stdout); |
50 |
< |
setvbuf(stdin, NULL, _IONBF, 2); |
51 |
< |
getheader(stdin, (gethfunc *)fputs, stdout); |
52 |
< |
printargs(argc-2, argv+2, stdout); |
53 |
< |
fputc('\n', stdout); |
54 |
< |
fflush(stdout); |
71 |
< |
execvp(argv[2], argv+2); |
72 |
< |
perror(argv[2]); |
73 |
< |
return 1; |
74 |
< |
} else if (argc == 2 && !strcmp(argv[1], "-")) { |
75 |
< |
SET_FILE_BINARY(stdin); |
76 |
< |
SET_FILE_BINARY(stdout); |
77 |
< |
getheader(stdin, NULL, NULL); |
78 |
< |
copycat(); |
79 |
< |
return 0; |
80 |
< |
} |
81 |
< |
for (i = 1; i < argc; i++) { |
82 |
< |
fputs(argv[i], stdout); |
83 |
< |
if ((fp = fopen(argv[i], "r")) == NULL) |
84 |
< |
fputs(": cannot open\n", stdout); |
85 |
< |
else { |
86 |
< |
if (dim) { |
87 |
< |
fputs(": ", stdout); |
88 |
< |
getdim(fp); |
89 |
< |
} else { |
90 |
< |
tabstr(":\n", NULL); |
91 |
< |
getheader(fp, tabstr, NULL); |
92 |
< |
fputc('\n', stdout); |
93 |
< |
} |
94 |
< |
fclose(fp); |
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 |
< |
} |
57 |
< |
if (argc == 1) { |
58 |
< |
if (dim) { |
59 |
< |
getdim(stdin); |
60 |
< |
} else { |
61 |
< |
getheader(stdin, (gethfunc *)fputs, stdout); |
102 |
< |
fputc('\n', stdout); |
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 |
< |
return 0; |
68 |
> |
fputs(s, stdout); /* copy if no match */ |
69 |
> |
return(0); |
70 |
|
} |
71 |
|
|
72 |
|
|
78 |
|
int j; |
79 |
|
int c; |
80 |
|
|
117 |
– |
getheader(fp, NULL, NULL); /* skip header */ |
118 |
– |
|
81 |
|
switch (c = getc(fp)) { |
82 |
|
case '+': /* picture */ |
83 |
|
case '-': |
92 |
|
if (c == 0) { |
93 |
|
if (++j >= 4) |
94 |
|
break; |
95 |
< |
putchar(' '); |
95 |
> |
fputc(' ', stdout); |
96 |
|
} else { |
97 |
|
putchar(c); |
98 |
|
} |
99 |
< |
putchar('\n'); |
99 |
> |
fputc('\n', stdout); |
100 |
|
break; |
101 |
|
default: /* ??? */ |
102 |
|
fputs("unknown file type\n", stdout); |
113 |
|
|
114 |
|
fflush(stdout); |
115 |
|
while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0) |
116 |
< |
if (write(fileno(stdout), buf, n) != n) |
116 |
> |
if (writebuf(fileno(stdout), buf, n) != n) |
117 |
|
break; |
118 |
+ |
} |
119 |
+ |
|
120 |
+ |
|
121 |
+ |
int |
122 |
+ |
main( |
123 |
+ |
int argc, |
124 |
+ |
char **argv |
125 |
+ |
) |
126 |
+ |
{ |
127 |
+ |
int dim = 0; |
128 |
+ |
FILE *fp; |
129 |
+ |
int i; |
130 |
+ |
|
131 |
+ |
if (argc > 1 && (argv[1][0] == '-') | (argv[1][0] == '+') && |
132 |
+ |
argv[1][1] == 'd') { |
133 |
+ |
dim = 1 - 2*(argv[1][0] == '-'); |
134 |
+ |
argc--; argv++; |
135 |
+ |
} |
136 |
+ |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
137 |
+ |
flockfile(stdin); |
138 |
+ |
flockfile(stdout); |
139 |
+ |
#endif |
140 |
+ |
SET_FILE_BINARY(stdin); |
141 |
+ |
if (argc > 2 && !strcmp(argv[1], "-c")) { |
142 |
+ |
SET_FILE_BINARY(stdout); |
143 |
+ |
setvbuf(stdin, NULL, _IONBF, 2); |
144 |
+ |
if (checkheader(stdin, fmt, stdout) < 0) { |
145 |
+ |
fputs("Bad header!\n", stderr); |
146 |
+ |
return 1; |
147 |
+ |
} |
148 |
+ |
printargs(argc-2, argv+2, stdout); |
149 |
+ |
if (fmt[0] != '*') /* better be the same! */ |
150 |
+ |
fputformat(fmt, stdout); |
151 |
+ |
fputc('\n', stdout); |
152 |
+ |
if (dim) { /* copy resolution string? */ |
153 |
+ |
RESOLU rs; |
154 |
+ |
if (!fgetsresolu(&rs, stdin)) { |
155 |
+ |
fputs("No resolution string!\n", stderr); |
156 |
+ |
return 1; |
157 |
+ |
} |
158 |
+ |
if (dim > 0) |
159 |
+ |
fputsresolu(&rs, stdout); |
160 |
+ |
} |
161 |
+ |
fflush(stdout); |
162 |
+ |
execvp(argv[2], argv+2); |
163 |
+ |
perror(argv[2]); |
164 |
+ |
return 1; |
165 |
+ |
} |
166 |
+ |
if (argc > 2 && !strcmp(argv[1], "-a")) { |
167 |
+ |
SET_FILE_BINARY(stdout); |
168 |
+ |
if (checkheader(stdin, fmt, stdout) < 0) { |
169 |
+ |
fputs("Bad header!\n", stderr); |
170 |
+ |
return 1; |
171 |
+ |
} |
172 |
+ |
for (i = 2; i < argc; i++) { |
173 |
+ |
int len = strlen(argv[i]); |
174 |
+ |
if (!len) continue; |
175 |
+ |
fputs(argv[i], stdout); |
176 |
+ |
if (argv[i][len-1] != '\n') |
177 |
+ |
fputc('\n', stdout); |
178 |
+ |
} |
179 |
+ |
if (fmt[0] != '*') |
180 |
+ |
fputformat(fmt, stdout); |
181 |
+ |
fputc('\n', stdout); |
182 |
+ |
copycat(); |
183 |
+ |
return 0; |
184 |
+ |
} |
185 |
+ |
if (argc > 2 && !strcmp(argv[1], "-r")) { |
186 |
+ |
SET_FILE_BINARY(stdout); |
187 |
+ |
if (getheader(stdin, adjheadline, argv+2) < 0) { |
188 |
+ |
fputs("Bad header!\n", stderr); |
189 |
+ |
return 1; |
190 |
+ |
} |
191 |
+ |
for (i = 2; i < argc; i++) { /* add lines w/ var[= ]value */ |
192 |
+ |
int len = strlen(argv[i]); |
193 |
+ |
int strt = 0, j; |
194 |
+ |
while(isspace(argv[i][strt])) strt++; |
195 |
+ |
if (strt == len) continue; |
196 |
+ |
while (isspace(argv[i][len-1])) len--; |
197 |
+ |
for (j = strt+1; j < len-1; j++) |
198 |
+ |
if (argv[i][j] == '=' || isspace(argv[i][j])) { |
199 |
+ |
for (j = 0; j < len; j++) |
200 |
+ |
putchar(argv[i][j]); |
201 |
+ |
fputc('\n', stdout); |
202 |
+ |
break; |
203 |
+ |
} |
204 |
+ |
} |
205 |
+ |
if (fmt[0] != '*') |
206 |
+ |
fputformat(fmt, stdout); |
207 |
+ |
fputc('\n', stdout); |
208 |
+ |
copycat(); |
209 |
+ |
return 0; |
210 |
+ |
} |
211 |
+ |
if (argc == 2 && !strcmp(argv[1], "-")) { |
212 |
+ |
SET_FILE_BINARY(stdout); |
213 |
+ |
if (getheader(stdin, NULL, NULL) < 0) { |
214 |
+ |
fputs("Bad header!\n", stderr); |
215 |
+ |
return 1; |
216 |
+ |
} |
217 |
+ |
if (dim < 0) { /* skip resolution string? */ |
218 |
+ |
RESOLU rs; |
219 |
+ |
if (!fgetsresolu(&rs, stdin)) { |
220 |
+ |
fputs("No resolution string!\n", stderr); |
221 |
+ |
return 1; |
222 |
+ |
} |
223 |
+ |
} |
224 |
+ |
copycat(); |
225 |
+ |
return 0; |
226 |
+ |
} |
227 |
+ |
for (i = 1; i < argc; i++) { |
228 |
+ |
fputs(argv[i], stdout); |
229 |
+ |
if ((fp = fopen(argv[i], "r")) == NULL) |
230 |
+ |
fputs(": cannot open\n", stdout); |
231 |
+ |
else { |
232 |
+ |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
233 |
+ |
flockfile(fp); |
234 |
+ |
#endif |
235 |
+ |
if (dim < 0) { /* dimensions only */ |
236 |
+ |
if (getheader(fp, NULL, NULL) < 0) { |
237 |
+ |
fputs("bad header!\n", stdout); |
238 |
+ |
continue; |
239 |
+ |
} |
240 |
+ |
fputs(": ", stdout); |
241 |
+ |
getdim(fp); |
242 |
+ |
} else { |
243 |
+ |
tabstr(":\n", NULL); |
244 |
+ |
if (getheader(fp, tabstr, NULL) < 0) { |
245 |
+ |
fputs(argv[i], stderr); |
246 |
+ |
fputs(": bad header!\n", stderr); |
247 |
+ |
return 1; |
248 |
+ |
} |
249 |
+ |
fputc('\n', stdout); |
250 |
+ |
if (dim > 0) { |
251 |
+ |
fputc('\t', stdout); |
252 |
+ |
getdim(fp); |
253 |
+ |
} |
254 |
+ |
} |
255 |
+ |
fclose(fp); |
256 |
+ |
} |
257 |
+ |
} |
258 |
+ |
if (argc == 1) { |
259 |
+ |
if (dim < 0) { |
260 |
+ |
if (getheader(stdin, NULL, NULL) < 0) { |
261 |
+ |
fputs("Bad header!\n", stderr); |
262 |
+ |
return 1; |
263 |
+ |
} |
264 |
+ |
getdim(stdin); |
265 |
+ |
} else { |
266 |
+ |
if (getheader(stdin, (gethfunc *)fputs, stdout) < 0) { |
267 |
+ |
fputs("Bad header!\n", stderr); |
268 |
+ |
return 1; |
269 |
+ |
} |
270 |
+ |
fputc('\n', stdout); |
271 |
+ |
if (dim > 0) |
272 |
+ |
getdim(stdin); |
273 |
+ |
} |
274 |
+ |
} |
275 |
+ |
return 0; |
276 |
|
} |