7 |
|
* 1/3/86 |
8 |
|
*/ |
9 |
|
|
10 |
< |
#include <stdio.h> |
11 |
< |
|
10 |
> |
#include "rtio.h" |
11 |
|
#include "platform.h" |
12 |
+ |
#include "rtprocess.h" |
13 |
+ |
#include "resolu.h" |
14 |
|
|
15 |
+ |
#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 |
23 |
|
|
24 |
+ |
static gethfunc tabstr; |
25 |
+ |
static void getdim(FILE *fp); |
26 |
+ |
static void copycat(void); |
27 |
|
|
28 |
< |
int |
29 |
< |
tabstr(s) /* put out line followed by tab */ |
30 |
< |
register char *s; |
28 |
> |
|
29 |
> |
static int |
30 |
> |
tabstr( /* put out line followed by tab */ |
31 |
> |
char *s, |
32 |
> |
void *p |
33 |
> |
) |
34 |
|
{ |
35 |
|
while (*s) { |
36 |
|
putchar(*s); |
42 |
|
} |
43 |
|
|
44 |
|
|
45 |
< |
main(argc, argv) |
46 |
< |
int argc; |
47 |
< |
char **argv; |
45 |
> |
int |
46 |
> |
main( |
47 |
> |
int argc, |
48 |
> |
char **argv |
49 |
> |
) |
50 |
|
{ |
51 |
|
int dim = 0; |
52 |
+ |
char fmt[MAXFMTLEN]; |
53 |
|
FILE *fp; |
54 |
|
int i; |
55 |
|
|
56 |
< |
if (argc > 1 && !strcmp(argv[1], "-d")) { |
56 |
> |
if (argc > 1 && (argv[1][0] == '-') | (argv[1][0] == '+') && |
57 |
> |
argv[1][1] == 'd') { |
58 |
> |
dim = 1 - 2*(argv[1][0] == '-'); |
59 |
|
argc--; argv++; |
60 |
< |
dim = 1; |
61 |
< |
SET_DEFAULT_BINARY(); /* for output file */ |
62 |
< |
SET_FILE_BINARY(stdin); |
60 |
> |
} |
61 |
> |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
62 |
> |
flockfile(stdin); |
63 |
> |
#endif |
64 |
> |
SET_FILE_BINARY(stdin); |
65 |
> |
fmt[0] = '*'; fmt[1] = '\0'; |
66 |
> |
if (argc > 2 && !strcmp(argv[1], "-c")) { |
67 |
> |
SET_FILE_BINARY(stdout); |
68 |
> |
setvbuf(stdin, NULL, _IONBF, 2); |
69 |
> |
if (checkheader(stdin, fmt, stdout) < 0) { |
70 |
> |
fputs("Bad header!\n", stderr); |
71 |
> |
return 1; |
72 |
> |
} |
73 |
> |
printargs(argc-2, argv+2, stdout); |
74 |
> |
if (fmt[0] != '*') /* better be the same! */ |
75 |
> |
fputformat(fmt, stdout); |
76 |
> |
fputc('\n', stdout); |
77 |
> |
if (dim) { /* copy resolution string? */ |
78 |
> |
RESOLU rs; |
79 |
> |
if (!fgetsresolu(&rs, stdin)) { |
80 |
> |
fputs("No resolution string!\n", stderr); |
81 |
> |
return 1; |
82 |
> |
} |
83 |
> |
if (dim > 0) |
84 |
> |
fputsresolu(&rs, stdout); |
85 |
> |
} |
86 |
> |
fflush(stdout); |
87 |
> |
execvp(argv[2], argv+2); |
88 |
> |
perror(argv[2]); |
89 |
> |
return 1; |
90 |
> |
} else if (argc > 2 && !strcmp(argv[1], "-a")) { |
91 |
> |
SET_FILE_BINARY(stdout); |
92 |
> |
if (checkheader(stdin, fmt, stdout) < 0) { |
93 |
> |
fputs("Bad header!\n", stderr); |
94 |
> |
return 1; |
95 |
> |
} |
96 |
> |
for (i = 2; i < argc; i++) { |
97 |
> |
int len = strlen(argv[i]); |
98 |
> |
if (!len) continue; |
99 |
> |
fputs(argv[i], stdout); |
100 |
> |
if (argv[i][len-1] != '\n') |
101 |
> |
fputc('\n', stdout); |
102 |
> |
} |
103 |
> |
if (fmt[0] != '*') |
104 |
> |
fputformat(fmt, stdout); |
105 |
> |
fputc('\n', stdout); |
106 |
> |
copycat(); |
107 |
> |
return 0; |
108 |
|
} else if (argc == 2 && !strcmp(argv[1], "-")) { |
44 |
– |
SET_FILE_BINARY(stdin); |
109 |
|
SET_FILE_BINARY(stdout); |
110 |
< |
getheader(stdin, NULL, NULL); |
110 |
> |
if (getheader(stdin, NULL, NULL) < 0) { |
111 |
> |
fputs("Bad header!\n", stderr); |
112 |
> |
return 1; |
113 |
> |
} |
114 |
> |
if (dim < 0) { /* skip resolution string? */ |
115 |
> |
RESOLU rs; |
116 |
> |
if (!fgetsresolu(&rs, stdin)) { |
117 |
> |
fputs("No resolution string!\n", stderr); |
118 |
> |
return 1; |
119 |
> |
} |
120 |
> |
} |
121 |
|
copycat(); |
122 |
< |
exit(0); |
122 |
> |
return 0; |
123 |
|
} |
124 |
|
for (i = 1; i < argc; i++) { |
125 |
|
fputs(argv[i], stdout); |
126 |
|
if ((fp = fopen(argv[i], "r")) == NULL) |
127 |
|
fputs(": cannot open\n", stdout); |
128 |
|
else { |
129 |
< |
if (dim) { |
129 |
> |
if (dim < 0) { /* dimensions only */ |
130 |
> |
if (getheader(fp, NULL, NULL) < 0) { |
131 |
> |
fputs("bad header!\n", stdout); |
132 |
> |
continue; |
133 |
> |
} |
134 |
|
fputs(": ", stdout); |
135 |
|
getdim(fp); |
136 |
|
} else { |
137 |
< |
tabstr(":\n"); |
138 |
< |
getheader(fp, tabstr, NULL); |
139 |
< |
putchar('\n'); |
137 |
> |
tabstr(":\n", NULL); |
138 |
> |
if (getheader(fp, tabstr, NULL) < 0) { |
139 |
> |
fputs(argv[i], stderr); |
140 |
> |
fputs(": bad header!\n", stderr); |
141 |
> |
return 1; |
142 |
> |
} |
143 |
> |
fputc('\n', stdout); |
144 |
> |
if (dim > 0) { |
145 |
> |
fputc('\t', stdout); |
146 |
> |
getdim(fp); |
147 |
> |
} |
148 |
|
} |
149 |
|
fclose(fp); |
150 |
|
} |
151 |
|
} |
152 |
|
if (argc == 1) { |
153 |
< |
if (dim) { |
153 |
> |
if (dim < 0) { |
154 |
> |
if (getheader(stdin, NULL, NULL) < 0) { |
155 |
> |
fputs("Bad header!\n", stderr); |
156 |
> |
return 1; |
157 |
> |
} |
158 |
|
getdim(stdin); |
159 |
|
} else { |
160 |
< |
getheader(stdin, fputs, stdout); |
161 |
< |
putchar('\n'); |
160 |
> |
if (getheader(stdin, (gethfunc *)fputs, stdout) < 0) { |
161 |
> |
fputs("Bad header!\n", stderr); |
162 |
> |
return 1; |
163 |
> |
} |
164 |
> |
fputc('\n', stdout); |
165 |
> |
if (dim > 0) |
166 |
> |
getdim(stdin); |
167 |
|
} |
168 |
|
} |
169 |
< |
exit(0); |
169 |
> |
return 0; |
170 |
|
} |
171 |
|
|
172 |
|
|
173 |
< |
getdim(fp) /* get dimensions from file */ |
174 |
< |
register FILE *fp; |
173 |
> |
static void |
174 |
> |
getdim( /* get dimensions from file */ |
175 |
> |
FILE *fp |
176 |
> |
) |
177 |
|
{ |
178 |
|
int j; |
179 |
< |
register int c; |
179 |
> |
int c; |
180 |
|
|
84 |
– |
getheader(fp, NULL, NULL); /* skip header */ |
85 |
– |
|
181 |
|
switch (c = getc(fp)) { |
182 |
|
case '+': /* picture */ |
183 |
|
case '-': |
189 |
|
getc(fp); |
190 |
|
j = 0; |
191 |
|
while ((c = getc(fp)) != EOF) |
192 |
< |
if (c == 0) |
192 |
> |
if (c == 0) { |
193 |
|
if (++j >= 4) |
194 |
|
break; |
195 |
< |
else |
196 |
< |
putchar(' '); |
102 |
< |
else |
195 |
> |
putchar(' '); |
196 |
> |
} else { |
197 |
|
putchar(c); |
198 |
+ |
} |
199 |
|
putchar('\n'); |
200 |
|
break; |
201 |
|
default: /* ??? */ |
205 |
|
} |
206 |
|
|
207 |
|
|
208 |
< |
copycat() /* copy input to output */ |
208 |
> |
static void |
209 |
> |
copycat(void) /* copy input to output */ |
210 |
|
{ |
211 |
< |
register int c; |
211 |
> |
char buf[8192]; |
212 |
> |
int n; |
213 |
|
|
214 |
< |
while ((c = getchar()) != EOF) |
215 |
< |
putchar(c); |
214 |
> |
fflush(stdout); |
215 |
> |
while ((n = fread(buf, 1, sizeof(buf), stdin)) > 0) |
216 |
> |
if (writebuf(fileno(stdout), buf, n) != n) |
217 |
> |
break; |
218 |
|
} |