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