7 |
|
* 7/14/88 Greg Ward |
8 |
|
*/ |
9 |
|
|
10 |
– |
#include <stdlib.h> |
11 |
– |
#include <string.h> |
12 |
– |
#include <stdio.h> |
10 |
|
#include <ctype.h> |
11 |
|
|
15 |
– |
#include "platform.h" |
16 |
– |
#include "rtprocess.h" |
12 |
|
#include "rtio.h" |
13 |
+ |
#include "platform.h" |
14 |
+ |
#include "paths.h" |
15 |
|
|
16 |
< |
#define MAXFILE 512 /* maximum number of files */ |
16 |
> |
#define MAXLINE 262144 /* maximum input line */ |
17 |
|
|
18 |
< |
#define MAXLINE 65536 /* maximum input line */ |
18 |
> |
struct instream { /* structure to hold input stream info */ |
19 |
> |
FILE *input; /* input stream */ |
20 |
> |
int bytsiz; /* bytes/component if binary */ |
21 |
> |
char *tabc; /* data separation string */ |
22 |
> |
} *rifile = NULL; |
23 |
|
|
24 |
< |
FILE *input[MAXFILE]; |
24 |
< |
int bytsiz[MAXFILE]; |
25 |
< |
char *tabc[MAXFILE]; |
26 |
< |
int nfiles; |
24 |
> |
int nfiles = 0; |
25 |
|
|
26 |
|
char buf[MAXLINE]; |
27 |
|
|
28 |
|
int |
29 |
< |
main(argc, argv) |
32 |
< |
int argc; |
33 |
< |
char *argv[]; |
29 |
> |
main(int argc, char *argv[]) |
30 |
|
{ |
31 |
|
long incnt = 0; |
32 |
|
int unbuff = 0; |
36 |
|
int puteol; |
37 |
|
int i; |
38 |
|
|
39 |
< |
nfiles = 0; |
39 |
> |
rifile = (struct instream *)calloc(argc-1, sizeof(struct instream)); |
40 |
> |
if (!rifile) { |
41 |
> |
fputs(argv[0], stderr); |
42 |
> |
fputs(": not enough memory\n", stderr); |
43 |
> |
return(1); |
44 |
> |
} |
45 |
|
for (i = 1; i < argc; i++) { |
46 |
|
if (argv[i][0] == '-') { |
47 |
|
switch (argv[i][1]) { |
48 |
|
case 't': |
49 |
|
curtab = argv[i]+2; |
50 |
+ |
if (!*curtab) curtab = "\n"; |
51 |
|
break; |
52 |
|
case 'u': |
53 |
|
unbuff = !unbuff; |
58 |
|
incnt = atol(argv[++i]); |
59 |
|
break; |
60 |
|
case 'f': |
61 |
+ |
case 'F': |
62 |
|
curbytes = sizeof(float); |
63 |
|
break; |
64 |
|
case 'd': |
65 |
+ |
case 'D': |
66 |
|
curbytes = sizeof(double); |
67 |
|
break; |
68 |
|
case 'i': |
69 |
+ |
case 'I': |
70 |
|
curbytes = sizeof(int); |
71 |
|
break; |
72 |
|
case 'w': |
73 |
+ |
case 'W': |
74 |
|
curbytes = 2; |
75 |
|
break; |
76 |
|
case 'b': |
96 |
|
} |
97 |
|
break; |
98 |
|
case '\0': |
99 |
< |
tabc[nfiles] = curtab; |
100 |
< |
input[nfiles] = stdin; |
99 |
> |
rifile[nfiles].tabc = curtab; |
100 |
> |
rifile[nfiles].input = stdin; |
101 |
|
if (curbytes > 0) |
102 |
< |
SET_FILE_BINARY(input[nfiles]); |
103 |
< |
bytsiz[nfiles++] = curbytes; |
102 |
> |
SET_FILE_BINARY(rifile[nfiles].input); |
103 |
> |
rifile[nfiles++].bytsiz = curbytes; |
104 |
|
break; |
105 |
|
badopt:; |
106 |
|
default: |
109 |
|
return(1); |
110 |
|
} |
111 |
|
} else if (argv[i][0] == '!') { |
112 |
< |
tabc[nfiles] = curtab; |
113 |
< |
if ((input[nfiles] = popen(argv[i]+1, "r")) == NULL) { |
112 |
> |
rifile[nfiles].tabc = curtab; |
113 |
> |
if ((rifile[nfiles].input = popen(argv[i]+1, "r")) == NULL) { |
114 |
|
fputs(argv[i], stderr); |
115 |
|
fputs(": cannot start command\n", stderr); |
116 |
|
return(1); |
117 |
|
} |
118 |
|
if (curbytes > 0) |
119 |
< |
SET_FILE_BINARY(input[nfiles]); |
120 |
< |
bytsiz[nfiles++] = curbytes; |
119 |
> |
SET_FILE_BINARY(rifile[nfiles].input); |
120 |
> |
rifile[nfiles++].bytsiz = curbytes; |
121 |
|
} else { |
122 |
< |
tabc[nfiles] = curtab; |
123 |
< |
if ((input[nfiles] = fopen(argv[i], "r")) == NULL) { |
122 |
> |
rifile[nfiles].tabc = curtab; |
123 |
> |
if ((rifile[nfiles].input = fopen(argv[i], "r")) == NULL) { |
124 |
|
fputs(argv[i], stderr); |
125 |
|
fputs(": cannot open file\n", stderr); |
126 |
|
return(1); |
127 |
|
} |
128 |
|
if (curbytes > 0) |
129 |
< |
SET_FILE_BINARY(input[nfiles]); |
130 |
< |
bytsiz[nfiles++] = curbytes; |
129 |
> |
SET_FILE_BINARY(rifile[nfiles].input); |
130 |
> |
rifile[nfiles++].bytsiz = curbytes; |
131 |
|
} |
126 |
– |
if (nfiles >= MAXFILE) { |
127 |
– |
fputs(argv[0], stderr); |
128 |
– |
fputs(": too many input streams\n", stderr); |
129 |
– |
return(1); |
130 |
– |
} |
132 |
|
} |
133 |
|
if (!nfiles) { |
134 |
|
fputs(argv[0], stderr); |
135 |
|
fputs(": no input streams\n", stderr); |
136 |
|
return(1); |
137 |
+ |
} /* reduce array to size we need */ |
138 |
+ |
rifile = (struct instream *)realloc(rifile, nfiles*sizeof(struct instream)); |
139 |
+ |
if (!rifile) { |
140 |
+ |
fputs(argv[0], stderr); |
141 |
+ |
fputs(": realloc() failed!\n", stderr); |
142 |
+ |
return(1); |
143 |
|
} |
144 |
|
if (binout) /* binary output? */ |
145 |
|
SET_FILE_BINARY(stdout); |
146 |
|
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
147 |
|
for (i = nfiles; i--; ) |
148 |
< |
flockfile(input[i]); |
148 |
> |
flockfile(rifile[i].input); |
149 |
|
flockfile(stdout); |
150 |
|
#endif |
151 |
|
puteol = 0; /* any ASCII output at all? */ |
152 |
|
for (i = nfiles; i--; ) |
153 |
< |
puteol += (bytsiz[i] <= 0); |
153 |
> |
puteol += (rifile[i].bytsiz <= 0); |
154 |
|
do { /* main loop */ |
155 |
|
for (i = 0; i < nfiles; i++) { |
156 |
< |
if (bytsiz[i] > 0) { /* binary input */ |
157 |
< |
if (getbinary(buf, bytsiz[i], 1, input[i]) < 1) |
156 |
> |
if (rifile[i].bytsiz > 0) { /* binary input */ |
157 |
> |
if (getbinary(buf, rifile[i].bytsiz, 1, rifile[i].input) < 1) |
158 |
|
break; |
159 |
< |
if (i) |
160 |
< |
fputs(tabc[i], stdout); |
161 |
< |
putbinary(buf, bytsiz[i], 1, stdout); |
162 |
< |
} else if (bytsiz[i] < 0) { /* multi-line input */ |
156 |
< |
int n = -bytsiz[i]; |
159 |
> |
if (putbinary(buf, rifile[i].bytsiz, 1, stdout) != 1) |
160 |
> |
break; |
161 |
> |
} else if (rifile[i].bytsiz < 0) { /* multi-line input */ |
162 |
> |
int n = -rifile[i].bytsiz; |
163 |
|
while (n--) { |
164 |
< |
if (fgets(buf, MAXLINE, input[i]) == NULL) |
164 |
> |
if (fgets(buf, MAXLINE, rifile[i].input) == NULL) |
165 |
|
break; |
166 |
< |
if ((i > 0) | (n < -bytsiz[i]-1)) |
167 |
< |
fputs(tabc[i], stdout); |
166 |
> |
if ((i > 0) | (n < -rifile[i].bytsiz-1)) |
167 |
> |
fputs(rifile[i].tabc, stdout); |
168 |
|
buf[strlen(buf)-1] = '\0'; |
169 |
< |
fputs(buf, stdout); |
169 |
> |
if (fputs(buf, stdout) == EOF) |
170 |
> |
break; |
171 |
|
} |
172 |
|
if (n >= 0) /* fell short? */ |
173 |
|
break; |
174 |
|
} else { /* single-line input */ |
175 |
< |
if (fgets(buf, MAXLINE, input[i]) == NULL) |
175 |
> |
if (fgets(buf, MAXLINE, rifile[i].input) == NULL) |
176 |
|
break; |
177 |
|
if (i) |
178 |
< |
fputs(tabc[i], stdout); |
178 |
> |
fputs(rifile[i].tabc, stdout); |
179 |
|
buf[strlen(buf)-1] = '\0'; |
180 |
< |
fputs(buf, stdout); |
180 |
> |
if (fputs(buf, stdout) == EOF) |
181 |
> |
break; |
182 |
|
} |
183 |
|
} |
184 |
|
if (i < nfiles) |
188 |
|
if (unbuff) |
189 |
|
fflush(stdout); |
190 |
|
} while (--incnt); |
191 |
+ |
/* check ending */ |
192 |
+ |
if (fflush(stdout) == EOF) { |
193 |
+ |
fputs(argv[0], stderr); |
194 |
+ |
fputs(": write error on standard output\n", stderr); |
195 |
+ |
return(1); |
196 |
+ |
} |
197 |
+ |
if (incnt > 0) { |
198 |
+ |
fputs(argv[0], stderr); |
199 |
+ |
fputs(": warning: premature EOD\n", stderr); |
200 |
+ |
} |
201 |
|
return(0); |
202 |
|
} |