8 |
|
*/ |
9 |
|
|
10 |
|
#include <stdlib.h> |
11 |
+ |
#include <fcntl.h> |
12 |
|
#include <stdio.h> |
13 |
|
#include <string.h> |
14 |
|
#include <math.h> |
15 |
|
#include <ctype.h> |
16 |
|
|
17 |
|
#include "platform.h" |
17 |
– |
#include "calcomp.h" |
18 |
|
#include "rterror.h" |
19 |
+ |
#include "rtmisc.h" |
20 |
+ |
#include "rtio.h" |
21 |
+ |
#include "calcomp.h" |
22 |
|
|
23 |
|
#define isnum(c) (isdigit(c) || (c)=='-' || (c)=='.' \ |
24 |
|
|| (c)=='+' || (c)=='e' || (c)=='E') |
25 |
|
|
26 |
|
#define isblnk(c) (igneol ? isspace(c) : (c)==' '||(c)=='\t') |
27 |
|
|
28 |
< |
#define INBSIZ 4096 /* longest record */ |
28 |
> |
#define INBSIZ 16384 /* longest record */ |
29 |
|
#define MAXCOL 32 /* number of columns recorded */ |
30 |
|
|
31 |
|
/* field type specifications */ |
78 |
|
|
79 |
|
int blnkeq = 1; /* blanks compare equal? */ |
80 |
|
int igneol = 0; /* ignore end of line? */ |
81 |
+ |
int passive = 0; /* passive mode (transmit unmatched input) */ |
82 |
|
char sepchar = '\t'; /* input/output separator */ |
83 |
|
int noinput = 0; /* no input records? */ |
84 |
+ |
int itype = 'a'; /* input type (a/f/F/d/D) */ |
85 |
|
int nbicols = 0; /* number of binary input columns */ |
86 |
< |
int bocols = 0; /* produce binary output columns */ |
86 |
> |
int otype = 'a'; /* output format (a/f/F/d/D) */ |
87 |
|
char inpbuf[INBSIZ]; /* input buffer */ |
88 |
|
double colval[MAXCOL]; /* input column values */ |
89 |
|
unsigned long colflg = 0; /* column retrieved flags */ |
126 |
|
case 'l': |
127 |
|
igneol = !igneol; |
128 |
|
break; |
129 |
+ |
case 'p': |
130 |
+ |
passive = !passive; |
131 |
+ |
break; |
132 |
|
case 't': |
133 |
|
sepchar = argv[i][2]; |
134 |
|
break; |
147 |
|
case 'i': |
148 |
|
switch (argv[i][2]) { |
149 |
|
case '\0': |
150 |
+ |
itype = 'a'; |
151 |
|
nbicols = 0; |
152 |
|
readfmt(argv[++i], 0); |
153 |
|
break; |
154 |
|
case 'a': |
155 |
+ |
itype = 'a'; |
156 |
|
nbicols = 0; |
157 |
|
break; |
158 |
|
case 'd': |
159 |
+ |
case 'D': |
160 |
+ |
itype = argv[i][2]; |
161 |
|
if (isdigit(argv[i][3])) |
162 |
|
nbicols = atoi(argv[i]+3); |
163 |
|
else |
169 |
|
} |
170 |
|
break; |
171 |
|
case 'f': |
172 |
+ |
case 'F': |
173 |
+ |
itype = argv[i][2]; |
174 |
|
if (isdigit(argv[i][3])) |
175 |
< |
nbicols = -atoi(argv[i]+3); |
175 |
> |
nbicols = atoi(argv[i]+3); |
176 |
|
else |
177 |
< |
nbicols = -1; |
178 |
< |
if (-nbicols*sizeof(float) > INBSIZ) { |
177 |
> |
nbicols = 1; |
178 |
> |
if (nbicols*sizeof(float) > INBSIZ) { |
179 |
|
eputs(argv[0]); |
180 |
|
eputs(": too many input columns\n"); |
181 |
|
quit(1); |
188 |
|
case 'o': |
189 |
|
switch (argv[i][2]) { |
190 |
|
case '\0': |
191 |
< |
bocols = 0; |
191 |
> |
otype = 'a'; |
192 |
|
readfmt(argv[++i], 1); |
193 |
|
break; |
194 |
|
case 'a': |
195 |
< |
bocols = 0; |
195 |
> |
otype = 'a'; |
196 |
|
break; |
197 |
|
case 'd': |
198 |
< |
bocols = 1; |
185 |
< |
break; |
198 |
> |
case 'D': |
199 |
|
case 'f': |
200 |
< |
bocols = -1; |
200 |
> |
case 'F': |
201 |
> |
otype = argv[i][2]; |
202 |
|
break; |
203 |
+ |
default: |
204 |
+ |
goto userr; |
205 |
|
} |
206 |
|
break; |
207 |
|
case 'w': |
214 |
|
userr: |
215 |
|
eputs("Usage: "); |
216 |
|
eputs(argv[0]); |
217 |
< |
eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n"); |
217 |
> |
eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n"); |
218 |
|
quit(1); |
219 |
|
} |
220 |
< |
|
220 |
> |
if (otype != 'a') |
221 |
> |
SET_FILE_BINARY(stdout); |
222 |
|
if (noinput) { /* produce a single output record */ |
223 |
+ |
if (i < argc) { |
224 |
+ |
eputs(argv[0]); |
225 |
+ |
eputs(": file argument(s) incompatible with -n\n"); |
226 |
+ |
quit(1); |
227 |
+ |
} |
228 |
|
eclock++; |
229 |
|
putout(); |
230 |
|
quit(0); |
231 |
|
} |
232 |
+ |
if (itype != 'a') |
233 |
+ |
SET_FILE_BINARY(stdin); |
234 |
|
|
235 |
|
if (blnkeq) /* for efficiency */ |
236 |
|
nbsynch(); |
242 |
|
execute(argv[i]); |
243 |
|
|
244 |
|
quit(0); |
245 |
+ |
return 0; /* pro forma return */ |
246 |
|
} |
247 |
|
|
248 |
|
|
265 |
|
{ |
266 |
|
if (inpfmt != NULL) |
267 |
|
return(getrec()); |
268 |
< |
if (nbicols > 0) |
269 |
< |
return(fread(inpbuf, sizeof(double), |
270 |
< |
nbicols, fp) == nbicols); |
271 |
< |
if (nbicols < 0) |
272 |
< |
return(fread(inpbuf, sizeof(float), |
273 |
< |
-nbicols, fp) == -nbicols); |
268 |
> |
if (tolower(itype) == 'd') { |
269 |
> |
if (fread(inpbuf, sizeof(double), nbicols, fp) != nbicols) |
270 |
> |
return(0); |
271 |
> |
if (itype == 'D') |
272 |
> |
swap64(inpbuf, nbicols); |
273 |
> |
return(1); |
274 |
> |
} |
275 |
> |
if (tolower(itype) == 'f') { |
276 |
> |
if (fread(inpbuf, sizeof(float), nbicols, fp) != nbicols) |
277 |
> |
return(0); |
278 |
> |
if (itype == 'F') |
279 |
> |
swap32(inpbuf, nbicols); |
280 |
> |
return(1); |
281 |
> |
} |
282 |
|
return(fgets(inpbuf, INBSIZ, fp) != NULL); |
283 |
|
} |
284 |
|
|
323 |
|
colpos = 0; |
324 |
|
if (outfmt != NULL) |
325 |
|
putrec(); |
326 |
< |
else if (bocols) |
294 |
< |
chanout(bchanset); |
295 |
< |
else |
326 |
> |
else if (otype == 'a') |
327 |
|
chanout(chanset); |
328 |
< |
if (colpos && !bocols) |
328 |
> |
else |
329 |
> |
chanout(bchanset); |
330 |
> |
if (colpos && otype == 'a') |
331 |
|
putchar('\n'); |
332 |
|
if (unbuff) |
333 |
|
fflush(stdout); |
346 |
|
/* determine number of channels */ |
347 |
|
if (noinput || inpfmt != NULL) |
348 |
|
return(0); |
349 |
< |
if (nbicols > 0) |
349 |
> |
if (nbicols) |
350 |
|
return(nbicols); |
318 |
– |
if (nbicols < 0) |
319 |
– |
return(-nbicols); |
351 |
|
cp = inpbuf; /* need to count */ |
352 |
|
for (n = 0; *cp; ) |
353 |
|
if (blnkeq && isspace(sepchar)) { |
380 |
|
eputs("illegal channel number\n"); |
381 |
|
quit(1); |
382 |
|
} |
383 |
< |
if (nbicols > 0) { |
383 |
> |
if (nbicols) { |
384 |
|
if (n > nbicols) |
385 |
|
return(0.0); |
386 |
< |
cp = inpbuf + (n-1)*sizeof(double); |
387 |
< |
return(*(double *)cp); |
388 |
< |
} |
389 |
< |
if (nbicols < 0) { |
359 |
< |
if (n > -nbicols) |
360 |
< |
return(0.0); |
386 |
> |
if (tolower(itype) == 'd') { |
387 |
> |
cp = inpbuf + (n-1)*sizeof(double); |
388 |
> |
return(*(double *)cp); |
389 |
> |
} |
390 |
|
cp = inpbuf + (n-1)*sizeof(float); |
391 |
|
return(*(float *)cp); |
392 |
|
} |
438 |
|
) |
439 |
|
{ |
440 |
|
static char zerobuf[sizeof(double)]; |
441 |
+ |
float fval = v; |
442 |
|
|
443 |
|
while (++colpos < n) |
444 |
|
fwrite(zerobuf, |
445 |
< |
bocols>0 ? sizeof(double) : sizeof(float), |
445 |
> |
tolower(otype)=='d' ? sizeof(double) : sizeof(float), |
446 |
|
1, stdout); |
447 |
< |
if (bocols > 0) |
447 |
> |
switch (otype) { |
448 |
> |
case 'D': |
449 |
> |
swap64((char *)&v, 1); |
450 |
> |
/* fall through */ |
451 |
> |
case 'd': |
452 |
|
fwrite(&v, sizeof(double), 1, stdout); |
453 |
< |
else { |
454 |
< |
float fval = v; |
453 |
> |
break; |
454 |
> |
case 'F': |
455 |
> |
swap32((char *)&fval, 1); |
456 |
> |
/* fall through */ |
457 |
> |
case 'f': |
458 |
|
fwrite(&fval, sizeof(float), 1, stdout); |
459 |
+ |
break; |
460 |
|
} |
461 |
|
} |
462 |
|
|
484 |
|
eputs(": cannot open\n"); |
485 |
|
quit(1); |
486 |
|
} |
487 |
< |
res = read(fd, inpbuf+1, INBSIZ-1); |
487 |
> |
res = read(fd, inpbuf+2, INBSIZ-2); |
488 |
|
if (res <= 0 || res >= INBSIZ-1) { |
489 |
|
eputs(spec); |
490 |
|
if (res < 0) |
496 |
|
quit(1); |
497 |
|
} |
498 |
|
close(fd); |
499 |
< |
(inptr=inpbuf+1)[res] = '\0'; |
499 |
> |
(inptr=inpbuf+2)[res] = '\0'; |
500 |
|
} |
501 |
|
f = &fmt; /* get fields */ |
502 |
|
while ((res = readfield(&inptr)) != F_NUL) { |
658 |
|
|
659 |
|
|
660 |
|
static int |
661 |
< |
getrec(void) /* get next record from file */ |
661 |
> |
getrec(void) /* get next record from file */ |
662 |
|
{ |
663 |
|
int eatline; |
664 |
|
register struct field *f; |
665 |
< |
|
665 |
> |
|
666 |
|
while (ipb.chr != EOF) { |
667 |
< |
eatline = !igneol && ipb.chr != '\n'; |
630 |
< |
if (blnkeq) /* beware of nbsynch() */ |
667 |
> |
if (blnkeq) { /* beware of nbsynch() */ |
668 |
|
while (isblnk(ipb.chr)) |
669 |
< |
scaninp(); |
669 |
> |
resetinp(); |
670 |
> |
if (ipb.chr == EOF) |
671 |
> |
return(0); |
672 |
> |
} |
673 |
> |
eatline = (!igneol && ipb.chr != '\n'); |
674 |
|
clearrec(); /* start with fresh record */ |
675 |
|
for (f = inpfmt; f != NULL; f = f->next) |
676 |
|
if (getfield(f) == -1) |
677 |
|
break; |
678 |
|
if (f == NULL) { |
679 |
< |
advinp(); |
679 |
> |
advinp(); /* got one! */ |
680 |
|
return(1); |
681 |
|
} |
682 |
< |
resetinp(); |
682 |
> |
resetinp(); /* eat false start */ |
683 |
|
if (eatline) { /* eat rest of line */ |
684 |
|
while (ipb.chr != '\n') { |
685 |
|
if (ipb.chr == EOF) |
686 |
|
return(0); |
687 |
< |
scaninp(); |
687 |
> |
resetinp(); |
688 |
|
} |
689 |
< |
scaninp(); |
649 |
< |
advinp(); |
689 |
> |
resetinp(); |
690 |
|
} |
691 |
|
} |
692 |
|
return(0); |
883 |
|
ipb.beg = ipb.end; |
884 |
|
ipb.pos = ipb.beg; |
885 |
|
ipb.chr = *ipb.pos; |
886 |
+ |
if (passive) /* transmit unmatched character? */ |
887 |
+ |
fputc(ipb.chr, stdout); |
888 |
|
if (++ipb.beg >= &inpbuf[INBSIZ]) |
889 |
|
ipb.beg = inpbuf; |
890 |
|
scaninp(); |