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" |
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 |
|
|
22 |
– |
FILE *input[MAXFILE]; |
23 |
– |
int bytsiz[MAXFILE]; |
24 |
– |
char *tabc[MAXFILE]; |
24 |
|
int nfiles = 0; |
25 |
|
|
26 |
|
char buf[MAXLINE]; |
36 |
|
int puteol; |
37 |
|
int i; |
38 |
|
|
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; |
51 |
> |
continue; |
52 |
|
case 'u': |
53 |
|
unbuff = !unbuff; |
54 |
< |
break; |
54 |
> |
continue; |
55 |
|
case 'i': |
56 |
|
switch (argv[i][2]) { |
57 |
|
case 'n': |
58 |
|
incnt = atol(argv[++i]); |
59 |
< |
break; |
59 |
> |
continue; |
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': |
94 |
|
curtab = ""; |
95 |
|
++binout; |
96 |
|
} |
97 |
< |
break; |
97 |
> |
continue; |
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; |
104 |
< |
break; |
102 |
> |
SET_FILE_BINARY(rifile[nfiles].input); |
103 |
> |
rifile[nfiles++].bytsiz = curbytes; |
104 |
> |
continue; |
105 |
|
badopt:; |
106 |
|
default: |
107 |
|
fputs(argv[0], stderr); |
108 |
< |
fputs(": bad option\n", stderr); |
108 |
> |
fputs(": unknown option '", stderr); |
109 |
> |
fputs(argv[i], stderr); |
110 |
> |
fputs("'\n", stderr); |
111 |
|
return(1); |
112 |
|
} |
113 |
< |
} else if (argv[i][0] == '!') { |
114 |
< |
tabc[nfiles] = curtab; |
115 |
< |
if ((input[nfiles] = popen(argv[i]+1, "r")) == NULL) { |
113 |
> |
} |
114 |
> |
if (argv[i][0] == '!') { |
115 |
> |
rifile[nfiles].tabc = curtab; |
116 |
> |
if ((rifile[nfiles].input = popen(argv[i]+1, "r")) == NULL) { |
117 |
|
fputs(argv[i], stderr); |
118 |
|
fputs(": cannot start command\n", stderr); |
119 |
|
return(1); |
120 |
|
} |
121 |
|
if (curbytes > 0) |
122 |
< |
SET_FILE_BINARY(input[nfiles]); |
123 |
< |
bytsiz[nfiles++] = curbytes; |
122 |
> |
SET_FILE_BINARY(rifile[nfiles].input); |
123 |
> |
rifile[nfiles++].bytsiz = curbytes; |
124 |
|
} else { |
125 |
< |
tabc[nfiles] = curtab; |
126 |
< |
if ((input[nfiles] = fopen(argv[i], "r")) == NULL) { |
125 |
> |
rifile[nfiles].tabc = curtab; |
126 |
> |
if ((rifile[nfiles].input = fopen(argv[i], "r")) == NULL) { |
127 |
|
fputs(argv[i], stderr); |
128 |
|
fputs(": cannot open file\n", stderr); |
129 |
|
return(1); |
130 |
|
} |
131 |
|
if (curbytes > 0) |
132 |
< |
SET_FILE_BINARY(input[nfiles]); |
133 |
< |
bytsiz[nfiles++] = curbytes; |
132 |
> |
SET_FILE_BINARY(rifile[nfiles].input); |
133 |
> |
rifile[nfiles++].bytsiz = curbytes; |
134 |
|
} |
123 |
– |
if (nfiles >= MAXFILE) { |
124 |
– |
fputs(argv[0], stderr); |
125 |
– |
fputs(": too many input streams\n", stderr); |
126 |
– |
return(1); |
127 |
– |
} |
135 |
|
} |
136 |
|
if (!nfiles) { |
137 |
|
fputs(argv[0], stderr); |
138 |
|
fputs(": no input streams\n", stderr); |
139 |
|
return(1); |
140 |
+ |
} /* reduce array to size we need */ |
141 |
+ |
rifile = (struct instream *)realloc(rifile, nfiles*sizeof(struct instream)); |
142 |
+ |
if (!rifile) { |
143 |
+ |
fputs(argv[0], stderr); |
144 |
+ |
fputs(": realloc() failed!\n", stderr); |
145 |
+ |
return(1); |
146 |
|
} |
147 |
|
if (binout) /* binary output? */ |
148 |
|
SET_FILE_BINARY(stdout); |
149 |
|
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
150 |
|
for (i = nfiles; i--; ) |
151 |
< |
flockfile(input[i]); |
151 |
> |
flockfile(rifile[i].input); |
152 |
|
flockfile(stdout); |
153 |
|
#endif |
154 |
|
puteol = 0; /* any ASCII output at all? */ |
155 |
|
for (i = nfiles; i--; ) |
156 |
< |
puteol += (bytsiz[i] <= 0); |
156 |
> |
puteol += (rifile[i].bytsiz <= 0); |
157 |
|
do { /* main loop */ |
158 |
|
for (i = 0; i < nfiles; i++) { |
159 |
< |
if (bytsiz[i] > 0) { /* binary input */ |
160 |
< |
if (getbinary(buf, bytsiz[i], 1, input[i]) < 1) |
159 |
> |
if (rifile[i].bytsiz > 0) { /* binary input */ |
160 |
> |
if (getbinary(buf, rifile[i].bytsiz, 1, rifile[i].input) < 1) |
161 |
|
break; |
162 |
< |
if (putbinary(buf, bytsiz[i], 1, stdout) != 1) |
162 |
> |
if (putbinary(buf, rifile[i].bytsiz, 1, stdout) != 1) |
163 |
|
break; |
164 |
< |
} else if (bytsiz[i] < 0) { /* multi-line input */ |
165 |
< |
int n = -bytsiz[i]; |
164 |
> |
} else if (rifile[i].bytsiz < 0) { /* multi-line input */ |
165 |
> |
int n = -rifile[i].bytsiz; |
166 |
|
while (n--) { |
167 |
< |
if (fgets(buf, MAXLINE, input[i]) == NULL) |
167 |
> |
if (fgets(buf, MAXLINE, rifile[i].input) == NULL) |
168 |
|
break; |
169 |
< |
if ((i > 0) | (n < -bytsiz[i]-1)) |
170 |
< |
fputs(tabc[i], stdout); |
169 |
> |
if ((i > 0) | (n < -rifile[i].bytsiz-1)) |
170 |
> |
fputs(rifile[i].tabc, stdout); |
171 |
|
buf[strlen(buf)-1] = '\0'; |
172 |
|
if (fputs(buf, stdout) == EOF) |
173 |
|
break; |
175 |
|
if (n >= 0) /* fell short? */ |
176 |
|
break; |
177 |
|
} else { /* single-line input */ |
178 |
< |
if (fgets(buf, MAXLINE, input[i]) == NULL) |
178 |
> |
if (fgets(buf, MAXLINE, rifile[i].input) == NULL) |
179 |
|
break; |
180 |
|
if (i) |
181 |
< |
fputs(tabc[i], stdout); |
181 |
> |
fputs(rifile[i].tabc, stdout); |
182 |
|
buf[strlen(buf)-1] = '\0'; |
183 |
|
if (fputs(buf, stdout) == EOF) |
184 |
|
break; |