10 |
|
#include <stdlib.h> |
11 |
|
#include <string.h> |
12 |
|
#include <stdio.h> |
13 |
+ |
#include <ctype.h> |
14 |
|
|
15 |
|
#include "platform.h" |
16 |
|
#include "rtprocess.h" |
17 |
+ |
#include "rtio.h" |
18 |
|
|
19 |
< |
#define MAXFILE 32 /* maximum number of files */ |
19 |
> |
#define MAXFILE 512 /* maximum number of files */ |
20 |
|
|
21 |
< |
#define MAXLINE 512 /* maximum input line */ |
21 |
> |
#define MAXLINE 65536 /* maximum input line */ |
22 |
|
|
23 |
|
FILE *input[MAXFILE]; |
24 |
+ |
int bytsiz[MAXFILE]; |
25 |
|
char *tabc[MAXFILE]; |
26 |
|
int nfiles; |
27 |
|
|
32 |
|
int argc; |
33 |
|
char *argv[]; |
34 |
|
{ |
35 |
< |
register int i; |
36 |
< |
char *curtab; |
37 |
< |
int running; |
35 |
> |
long incnt = 0; |
36 |
> |
int unbuff = 0; |
37 |
> |
int binout = 0; |
38 |
> |
char *curtab = "\t"; |
39 |
> |
int curbytes = 0; |
40 |
> |
int puteol; |
41 |
> |
int i; |
42 |
|
|
36 |
– |
curtab = "\t"; |
43 |
|
nfiles = 0; |
44 |
|
for (i = 1; i < argc; i++) { |
45 |
|
if (argv[i][0] == '-') { |
47 |
|
case 't': |
48 |
|
curtab = argv[i]+2; |
49 |
|
break; |
50 |
+ |
case 'u': |
51 |
+ |
unbuff = !unbuff; |
52 |
+ |
break; |
53 |
+ |
case 'i': |
54 |
+ |
switch (argv[i][2]) { |
55 |
+ |
case 'n': |
56 |
+ |
incnt = atol(argv[++i]); |
57 |
+ |
break; |
58 |
+ |
case 'f': |
59 |
+ |
curbytes = sizeof(float); |
60 |
+ |
break; |
61 |
+ |
case 'd': |
62 |
+ |
curbytes = sizeof(double); |
63 |
+ |
break; |
64 |
+ |
case 'i': |
65 |
+ |
curbytes = sizeof(int); |
66 |
+ |
break; |
67 |
+ |
case 'w': |
68 |
+ |
curbytes = 2; |
69 |
+ |
break; |
70 |
+ |
case 'b': |
71 |
+ |
curbytes = 1; |
72 |
+ |
break; |
73 |
+ |
case 'a': |
74 |
+ |
curbytes = argv[i][3] ? -1 : 0; |
75 |
+ |
break; |
76 |
+ |
default: |
77 |
+ |
goto badopt; |
78 |
+ |
} |
79 |
+ |
if (isdigit(argv[i][3])) |
80 |
+ |
curbytes *= atoi(argv[i]+3); |
81 |
+ |
if (abs(curbytes) > MAXLINE) { |
82 |
+ |
fputs(argv[0], stderr); |
83 |
+ |
fputs(": input size too big\n", stderr); |
84 |
+ |
exit(1); |
85 |
+ |
} |
86 |
+ |
if (curbytes) { |
87 |
+ |
curtab = ""; |
88 |
+ |
binout += (curbytes > 0); |
89 |
+ |
} |
90 |
+ |
break; |
91 |
|
case '\0': |
92 |
|
tabc[nfiles] = curtab; |
93 |
< |
input[nfiles++] = stdin; |
93 |
> |
input[nfiles] = stdin; |
94 |
> |
if (curbytes > 0) |
95 |
> |
SET_FILE_BINARY(input[nfiles]); |
96 |
> |
else |
97 |
> |
curbytes = -curbytes; |
98 |
> |
bytsiz[nfiles++] = curbytes; |
99 |
|
break; |
100 |
+ |
badopt:; |
101 |
|
default: |
102 |
|
fputs(argv[0], stderr); |
103 |
|
fputs(": bad option\n", stderr); |
105 |
|
} |
106 |
|
} else if (argv[i][0] == '!') { |
107 |
|
tabc[nfiles] = curtab; |
108 |
< |
if ((input[nfiles++] = popen(argv[i]+1, "r")) == NULL) { |
108 |
> |
if ((input[nfiles] = popen(argv[i]+1, "r")) == NULL) { |
109 |
|
fputs(argv[i], stderr); |
110 |
|
fputs(": cannot start command\n", stderr); |
111 |
|
exit(1); |
112 |
|
} |
113 |
+ |
if (curbytes > 0) |
114 |
+ |
SET_FILE_BINARY(input[nfiles]); |
115 |
+ |
else |
116 |
+ |
curbytes = -curbytes; |
117 |
+ |
bytsiz[nfiles++] = curbytes; |
118 |
|
} else { |
119 |
|
tabc[nfiles] = curtab; |
120 |
< |
if ((input[nfiles++] = fopen(argv[i], "r")) == NULL) { |
120 |
> |
if ((input[nfiles] = fopen(argv[i], "r")) == NULL) { |
121 |
|
fputs(argv[i], stderr); |
122 |
|
fputs(": cannot open file\n", stderr); |
123 |
|
exit(1); |
124 |
|
} |
125 |
+ |
if (curbytes > 0) |
126 |
+ |
SET_FILE_BINARY(input[nfiles]); |
127 |
+ |
else |
128 |
+ |
curbytes = -curbytes; |
129 |
+ |
bytsiz[nfiles++] = curbytes; |
130 |
|
} |
131 |
|
if (nfiles >= MAXFILE) { |
132 |
|
fputs(argv[0], stderr); |
134 |
|
exit(1); |
135 |
|
} |
136 |
|
} |
137 |
< |
|
138 |
< |
do { |
139 |
< |
running = 0; |
137 |
> |
if (binout) /* binary output? */ |
138 |
> |
SET_FILE_BINARY(stdout); |
139 |
> |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
140 |
> |
for (i = nfiles; i--; ) |
141 |
> |
flockfile(input[i]); |
142 |
> |
flockfile(stdout); |
143 |
> |
#endif |
144 |
> |
puteol = 0; /* any ASCII output at all? */ |
145 |
> |
for (i = nfiles; i--; ) |
146 |
> |
if (!bytsiz[i] || isprint(tabc[i][0]) || tabc[i][0] == '\t') { |
147 |
> |
puteol++; |
148 |
> |
break; |
149 |
> |
} |
150 |
> |
do { /* main loop */ |
151 |
|
for (i = 0; i < nfiles; i++) { |
152 |
< |
if (fgets(buf, MAXLINE, input[i]) != NULL) { |
152 |
> |
if (bytsiz[i]) { /* binary/fixed width */ |
153 |
> |
if (getbinary(buf, bytsiz[i], 1, input[i]) < 1) |
154 |
> |
break; |
155 |
|
if (i) |
156 |
|
fputs(tabc[i], stdout); |
157 |
+ |
putbinary(buf, bytsiz[i], 1, stdout); |
158 |
+ |
} else { |
159 |
+ |
if (fgets(buf, MAXLINE, input[i]) == NULL) |
160 |
+ |
break; |
161 |
+ |
if (i) |
162 |
+ |
fputs(tabc[i], stdout); |
163 |
|
buf[strlen(buf)-1] = '\0'; |
164 |
|
fputs(buf, stdout); |
83 |
– |
running++; |
165 |
|
} |
166 |
|
} |
167 |
< |
if (running) |
167 |
> |
if (i < nfiles) |
168 |
> |
break; |
169 |
> |
if (puteol) |
170 |
|
putchar('\n'); |
171 |
< |
} while (running); |
172 |
< |
|
173 |
< |
exit(0); |
171 |
> |
if (unbuff) |
172 |
> |
fflush(stdout); |
173 |
> |
} while (--incnt); |
174 |
> |
return(0); |
175 |
|
} |