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 |
|
|
18 |
< |
#define MAXFILE 16 /* maximum number of files */ |
18 |
> |
#define MAXFILE 512 /* maximum number of files */ |
19 |
|
|
20 |
< |
#define MAXLINE 512 /* maximum input line */ |
20 |
> |
#define MAXLINE 4096 /* maximum input line */ |
21 |
|
|
22 |
|
FILE *input[MAXFILE]; |
23 |
< |
int tabc[MAXFILE]; |
23 |
> |
int bytsiz[MAXFILE]; |
24 |
> |
char *tabc[MAXFILE]; |
25 |
|
int nfiles; |
26 |
|
|
27 |
|
char buf[MAXLINE]; |
31 |
|
int argc; |
32 |
|
char *argv[]; |
33 |
|
{ |
34 |
< |
register int i; |
35 |
< |
int curtab; |
36 |
< |
int running; |
34 |
> |
int unbuff = 0; |
35 |
> |
int i; |
36 |
> |
char *curtab; |
37 |
> |
int curbytes; |
38 |
> |
int puteol; |
39 |
|
|
40 |
< |
curtab = '\t'; |
40 |
> |
curtab = "\t"; |
41 |
> |
curbytes = 0; |
42 |
|
nfiles = 0; |
43 |
|
for (i = 1; i < argc; i++) { |
44 |
|
if (argv[i][0] == '-') { |
45 |
|
switch (argv[i][1]) { |
46 |
|
case 't': |
47 |
< |
curtab = argv[i][2]; |
47 |
> |
curtab = argv[i]+2; |
48 |
|
break; |
49 |
+ |
case 'u': |
50 |
+ |
unbuff = !unbuff; |
51 |
+ |
break; |
52 |
+ |
case 'i': |
53 |
+ |
switch (argv[i][2]) { |
54 |
+ |
case 'f': |
55 |
+ |
curbytes = sizeof(float); |
56 |
+ |
break; |
57 |
+ |
case 'd': |
58 |
+ |
curbytes = sizeof(double); |
59 |
+ |
break; |
60 |
+ |
case 'i': |
61 |
+ |
curbytes = sizeof(int); |
62 |
+ |
break; |
63 |
+ |
case 'w': |
64 |
+ |
curbytes = 2; |
65 |
+ |
break; |
66 |
+ |
case 'b': |
67 |
+ |
curbytes = 1; |
68 |
+ |
break; |
69 |
+ |
case 'a': |
70 |
+ |
curbytes = argv[i][3] ? -1 : 0; |
71 |
+ |
break; |
72 |
+ |
default: |
73 |
+ |
goto badopt; |
74 |
+ |
} |
75 |
+ |
if (isdigit(argv[i][3])) |
76 |
+ |
curbytes *= atoi(argv[i]+3); |
77 |
+ |
if (abs(curbytes) > MAXLINE) { |
78 |
+ |
fputs(argv[0], stderr); |
79 |
+ |
fputs(": input size too big\n", stderr); |
80 |
+ |
exit(1); |
81 |
+ |
} |
82 |
+ |
if (curbytes) |
83 |
+ |
curtab = ""; |
84 |
+ |
break; |
85 |
|
case '\0': |
86 |
|
tabc[nfiles] = curtab; |
87 |
< |
input[nfiles++] = stdin; |
87 |
> |
input[nfiles] = stdin; |
88 |
> |
if (curbytes > 0) |
89 |
> |
SET_FILE_BINARY(input[nfiles]); |
90 |
> |
else |
91 |
> |
curbytes = -curbytes; |
92 |
> |
bytsiz[nfiles++] = curbytes; |
93 |
|
break; |
94 |
+ |
badopt:; |
95 |
|
default: |
96 |
|
fputs(argv[0], stderr); |
97 |
|
fputs(": bad option\n", stderr); |
99 |
|
} |
100 |
|
} else if (argv[i][0] == '!') { |
101 |
|
tabc[nfiles] = curtab; |
102 |
< |
if ((input[nfiles++] = popen(argv[i]+1, "r")) == NULL) { |
102 |
> |
if ((input[nfiles] = popen(argv[i]+1, "r")) == NULL) { |
103 |
|
fputs(argv[i], stderr); |
104 |
|
fputs(": cannot start command\n", stderr); |
105 |
|
exit(1); |
106 |
|
} |
107 |
+ |
if (curbytes > 0) |
108 |
+ |
SET_FILE_BINARY(input[nfiles]); |
109 |
+ |
else |
110 |
+ |
curbytes = -curbytes; |
111 |
+ |
bytsiz[nfiles++] = curbytes; |
112 |
|
} else { |
113 |
|
tabc[nfiles] = curtab; |
114 |
< |
if ((input[nfiles++] = fopen(argv[i], "r")) == NULL) { |
114 |
> |
if ((input[nfiles] = fopen(argv[i], "r")) == NULL) { |
115 |
|
fputs(argv[i], stderr); |
116 |
|
fputs(": cannot open file\n", stderr); |
117 |
|
exit(1); |
118 |
|
} |
119 |
+ |
if (curbytes > 0) |
120 |
+ |
SET_FILE_BINARY(input[nfiles]); |
121 |
+ |
else |
122 |
+ |
curbytes = -curbytes; |
123 |
+ |
bytsiz[nfiles++] = curbytes; |
124 |
|
} |
125 |
|
if (nfiles >= MAXFILE) { |
126 |
|
fputs(argv[0], stderr); |
128 |
|
exit(1); |
129 |
|
} |
130 |
|
} |
131 |
< |
|
132 |
< |
do { |
133 |
< |
running = 0; |
131 |
> |
puteol = 0; /* check for ASCII output */ |
132 |
> |
for (i = nfiles; i--; ) |
133 |
> |
if (!bytsiz[i] || isprint(tabc[i][0]) || tabc[i][0] == '\t') { |
134 |
> |
puteol++; |
135 |
> |
break; |
136 |
> |
} |
137 |
> |
for ( ; ; ) { /* main loop */ |
138 |
|
for (i = 0; i < nfiles; i++) { |
139 |
< |
if (fgets(buf, MAXLINE, input[i]) != NULL) { |
139 |
> |
if (bytsiz[i]) { /* binary file */ |
140 |
> |
if (fread(buf, bytsiz[i], 1, input[i]) < 1) |
141 |
> |
break; |
142 |
|
if (i) |
143 |
< |
putchar(tabc[i]); |
143 |
> |
fputs(tabc[i], stdout); |
144 |
> |
fwrite(buf, bytsiz[i], 1, stdout); |
145 |
> |
} else { |
146 |
> |
if (fgets(buf, MAXLINE, input[i]) == NULL) |
147 |
> |
break; |
148 |
> |
if (i) |
149 |
> |
fputs(tabc[i], stdout); |
150 |
|
buf[strlen(buf)-1] = '\0'; |
151 |
|
fputs(buf, stdout); |
83 |
– |
running++; |
152 |
|
} |
153 |
|
} |
154 |
< |
if (running) |
154 |
> |
if (i < nfiles) |
155 |
> |
break; |
156 |
> |
if (puteol) |
157 |
|
putchar('\n'); |
158 |
< |
} while (running); |
159 |
< |
|
160 |
< |
exit(0); |
158 |
> |
if (unbuff) |
159 |
> |
fflush(stdout); |
160 |
> |
} |
161 |
> |
return(0); |
162 |
|
} |