8 |
|
*/ |
9 |
|
|
10 |
|
#include <stdlib.h> |
11 |
– |
#include <stdio.h> |
12 |
– |
#include <string.h> |
11 |
|
#include <math.h> |
12 |
|
#include <ctype.h> |
13 |
|
|
14 |
|
#include "platform.h" |
17 |
– |
#include "calcomp.h" |
15 |
|
#include "rterror.h" |
16 |
+ |
#include "rtmisc.h" |
17 |
+ |
#include "rtio.h" |
18 |
+ |
#include "calcomp.h" |
19 |
|
|
20 |
|
#define isnum(c) (isdigit(c) || (c)=='-' || (c)=='.' \ |
21 |
|
|| (c)=='+' || (c)=='e' || (c)=='E') |
22 |
|
|
23 |
|
#define isblnk(c) (igneol ? isspace(c) : (c)==' '||(c)=='\t') |
24 |
|
|
25 |
< |
#define INBSIZ 4096 /* longest record */ |
25 |
> |
#define INBSIZ 16384 /* longest record */ |
26 |
|
#define MAXCOL 32 /* number of columns recorded */ |
27 |
|
|
28 |
|
/* field type specifications */ |
75 |
|
|
76 |
|
int blnkeq = 1; /* blanks compare equal? */ |
77 |
|
int igneol = 0; /* ignore end of line? */ |
78 |
+ |
int passive = 0; /* passive mode (transmit unmatched input) */ |
79 |
|
char sepchar = '\t'; /* input/output separator */ |
80 |
|
int noinput = 0; /* no input records? */ |
81 |
+ |
int itype = 'a'; /* input type (a/f/F/d/D) */ |
82 |
|
int nbicols = 0; /* number of binary input columns */ |
83 |
< |
int bocols = 0; /* produce binary output columns */ |
83 |
> |
int otype = 'a'; /* output format (a/f/F/d/D) */ |
84 |
|
char inpbuf[INBSIZ]; /* input buffer */ |
85 |
|
double colval[MAXCOL]; /* input column values */ |
86 |
|
unsigned long colflg = 0; /* column retrieved flags */ |
104 |
|
char *argv[] |
105 |
|
) |
106 |
|
{ |
107 |
+ |
char *fpath; |
108 |
|
int i; |
109 |
|
|
110 |
|
esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST; |
124 |
|
case 'l': |
125 |
|
igneol = !igneol; |
126 |
|
break; |
127 |
+ |
case 'p': |
128 |
+ |
passive = !passive; |
129 |
+ |
break; |
130 |
|
case 't': |
131 |
|
sepchar = argv[i][2]; |
132 |
|
break; |
134 |
|
svpreset(argv[++i]); |
135 |
|
break; |
136 |
|
case 'f': |
137 |
< |
fcompile(argv[++i]); |
137 |
> |
fpath = getpath(argv[++i], getrlibpath(), 0); |
138 |
> |
if (fpath == NULL) { |
139 |
> |
eputs(argv[0]); |
140 |
> |
eputs(": cannot find file '"); |
141 |
> |
eputs(argv[i]); |
142 |
> |
eputs("'\n"); |
143 |
> |
quit(1); |
144 |
> |
} |
145 |
> |
fcompile(fpath); |
146 |
|
break; |
147 |
|
case 'e': |
148 |
|
scompile(argv[++i], NULL, 0); |
153 |
|
case 'i': |
154 |
|
switch (argv[i][2]) { |
155 |
|
case '\0': |
156 |
+ |
itype = 'a'; |
157 |
|
nbicols = 0; |
158 |
|
readfmt(argv[++i], 0); |
159 |
|
break; |
160 |
|
case 'a': |
161 |
+ |
itype = 'a'; |
162 |
|
nbicols = 0; |
163 |
|
break; |
164 |
|
case 'd': |
165 |
+ |
case 'D': |
166 |
+ |
itype = argv[i][2]; |
167 |
|
if (isdigit(argv[i][3])) |
168 |
|
nbicols = atoi(argv[i]+3); |
169 |
|
else |
175 |
|
} |
176 |
|
break; |
177 |
|
case 'f': |
178 |
+ |
case 'F': |
179 |
+ |
itype = argv[i][2]; |
180 |
|
if (isdigit(argv[i][3])) |
181 |
< |
nbicols = -atoi(argv[i]+3); |
181 |
> |
nbicols = atoi(argv[i]+3); |
182 |
|
else |
183 |
< |
nbicols = -1; |
184 |
< |
if (-nbicols*sizeof(float) > INBSIZ) { |
183 |
> |
nbicols = 1; |
184 |
> |
if (nbicols*sizeof(float) > INBSIZ) { |
185 |
|
eputs(argv[0]); |
186 |
|
eputs(": too many input columns\n"); |
187 |
|
quit(1); |
194 |
|
case 'o': |
195 |
|
switch (argv[i][2]) { |
196 |
|
case '\0': |
197 |
< |
bocols = 0; |
197 |
> |
otype = 'a'; |
198 |
|
readfmt(argv[++i], 1); |
199 |
|
break; |
200 |
|
case 'a': |
201 |
< |
bocols = 0; |
201 |
> |
otype = 'a'; |
202 |
|
break; |
203 |
|
case 'd': |
204 |
< |
bocols = 1; |
185 |
< |
break; |
204 |
> |
case 'D': |
205 |
|
case 'f': |
206 |
< |
bocols = -1; |
206 |
> |
case 'F': |
207 |
> |
otype = argv[i][2]; |
208 |
|
break; |
209 |
+ |
default: |
210 |
+ |
goto userr; |
211 |
|
} |
212 |
|
break; |
213 |
|
case 'w': |
220 |
|
userr: |
221 |
|
eputs("Usage: "); |
222 |
|
eputs(argv[0]); |
223 |
< |
eputs(" [-b][-l][-n][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n"); |
223 |
> |
eputs(" [-b][-l][-n][-p][-w][-u][-tS][-s svar=sval][-e expr][-f source][-i infmt][-o outfmt] [file]\n"); |
224 |
|
quit(1); |
225 |
|
} |
226 |
< |
|
226 |
> |
if (otype != 'a') |
227 |
> |
SET_FILE_BINARY(stdout); |
228 |
> |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
229 |
> |
flockfile(stdout); |
230 |
> |
#endif |
231 |
|
if (noinput) { /* produce a single output record */ |
232 |
+ |
if (i < argc) { |
233 |
+ |
eputs(argv[0]); |
234 |
+ |
eputs(": file argument(s) incompatible with -n\n"); |
235 |
+ |
quit(1); |
236 |
+ |
} |
237 |
|
eclock++; |
238 |
|
putout(); |
239 |
|
quit(0); |
240 |
|
} |
210 |
– |
|
241 |
|
if (blnkeq) /* for efficiency */ |
242 |
|
nbsynch(); |
243 |
|
|
248 |
|
execute(argv[i]); |
249 |
|
|
250 |
|
quit(0); |
251 |
+ |
return 0; /* pro forma return */ |
252 |
|
} |
253 |
|
|
254 |
|
|
271 |
|
{ |
272 |
|
if (inpfmt != NULL) |
273 |
|
return(getrec()); |
274 |
< |
if (nbicols > 0) |
275 |
< |
return(fread(inpbuf, sizeof(double), |
276 |
< |
nbicols, fp) == nbicols); |
277 |
< |
if (nbicols < 0) |
278 |
< |
return(fread(inpbuf, sizeof(float), |
279 |
< |
-nbicols, fp) == -nbicols); |
274 |
> |
if (tolower(itype) == 'd') { |
275 |
> |
if (fread(inpbuf, sizeof(double), nbicols, fp) != nbicols) |
276 |
> |
return(0); |
277 |
> |
if (itype == 'D') |
278 |
> |
swap64(inpbuf, nbicols); |
279 |
> |
return(1); |
280 |
> |
} |
281 |
> |
if (tolower(itype) == 'f') { |
282 |
> |
if (fread(inpbuf, sizeof(float), nbicols, fp) != nbicols) |
283 |
> |
return(0); |
284 |
> |
if (itype == 'F') |
285 |
> |
swap32(inpbuf, nbicols); |
286 |
> |
return(1); |
287 |
> |
} |
288 |
|
return(fgets(inpbuf, INBSIZ, fp) != NULL); |
289 |
|
} |
290 |
|
|
306 |
|
eputs(": cannot open\n"); |
307 |
|
quit(1); |
308 |
|
} |
309 |
+ |
if (itype != 'a') |
310 |
+ |
SET_FILE_BINARY(fp); |
311 |
+ |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
312 |
+ |
flockfile(fp); |
313 |
+ |
#endif |
314 |
|
if (inpfmt != NULL) |
315 |
|
initinp(fp); |
316 |
|
|
317 |
|
while (getinputrec(fp)) { |
318 |
|
varset("recno", '=', (double)++nrecs); |
319 |
+ |
varset("outno", '=', (double)(nout+1)); |
320 |
|
colflg = 0; |
321 |
|
eclock++; |
322 |
|
if (!conditional || varvalue("cond") > 0.0) { |
278 |
– |
varset("outno", '=', (double)++nout); |
323 |
|
putout(); |
324 |
+ |
++nout; |
325 |
|
} |
326 |
|
} |
327 |
|
fclose(fp); |
335 |
|
colpos = 0; |
336 |
|
if (outfmt != NULL) |
337 |
|
putrec(); |
338 |
< |
else if (bocols) |
294 |
< |
chanout(bchanset); |
295 |
< |
else |
338 |
> |
else if (otype == 'a') |
339 |
|
chanout(chanset); |
340 |
< |
if (colpos && !bocols) |
340 |
> |
else |
341 |
> |
chanout(bchanset); |
342 |
> |
if (colpos && otype == 'a') |
343 |
|
putchar('\n'); |
344 |
|
if (unbuff) |
345 |
|
fflush(stdout); |
350 |
|
l_in(char *funame) /* function call for $channel */ |
351 |
|
{ |
352 |
|
int n; |
353 |
< |
register char *cp; |
353 |
> |
char *cp; |
354 |
|
/* get argument as integer */ |
355 |
|
n = (int)(argument(1) + .5); |
356 |
|
if (n != 0) /* return channel value */ |
358 |
|
/* determine number of channels */ |
359 |
|
if (noinput || inpfmt != NULL) |
360 |
|
return(0); |
361 |
< |
if (nbicols > 0) |
361 |
> |
if (nbicols) |
362 |
|
return(nbicols); |
318 |
– |
if (nbicols < 0) |
319 |
– |
return(-nbicols); |
363 |
|
cp = inpbuf; /* need to count */ |
364 |
|
for (n = 0; *cp; ) |
365 |
|
if (blnkeq && isspace(sepchar)) { |
382 |
|
) |
383 |
|
{ |
384 |
|
int i; |
385 |
< |
register char *cp; |
385 |
> |
char *cp; |
386 |
|
|
387 |
|
if (noinput || inpfmt != NULL) { |
388 |
|
eputs("no column input\n"); |
392 |
|
eputs("illegal channel number\n"); |
393 |
|
quit(1); |
394 |
|
} |
395 |
< |
if (nbicols > 0) { |
395 |
> |
if (nbicols) { |
396 |
|
if (n > nbicols) |
397 |
|
return(0.0); |
398 |
< |
cp = inpbuf + (n-1)*sizeof(double); |
399 |
< |
return(*(double *)cp); |
400 |
< |
} |
401 |
< |
if (nbicols < 0) { |
359 |
< |
if (n > -nbicols) |
360 |
< |
return(0.0); |
398 |
> |
if (tolower(itype) == 'd') { |
399 |
> |
cp = inpbuf + (n-1)*sizeof(double); |
400 |
> |
return(*(double *)cp); |
401 |
> |
} |
402 |
|
cp = inpbuf + (n-1)*sizeof(float); |
403 |
|
return(*(float *)cp); |
404 |
|
} |
450 |
|
) |
451 |
|
{ |
452 |
|
static char zerobuf[sizeof(double)]; |
453 |
+ |
float fval = v; |
454 |
|
|
455 |
|
while (++colpos < n) |
456 |
|
fwrite(zerobuf, |
457 |
< |
bocols>0 ? sizeof(double) : sizeof(float), |
457 |
> |
tolower(otype)=='d' ? sizeof(double) : sizeof(float), |
458 |
|
1, stdout); |
459 |
< |
if (bocols > 0) |
459 |
> |
switch (otype) { |
460 |
> |
case 'D': |
461 |
> |
swap64((char *)&v, 1); |
462 |
> |
/* fall through */ |
463 |
> |
case 'd': |
464 |
|
fwrite(&v, sizeof(double), 1, stdout); |
465 |
< |
else { |
466 |
< |
float fval = v; |
465 |
> |
break; |
466 |
> |
case 'F': |
467 |
> |
swap32((char *)&fval, 1); |
468 |
> |
/* fall through */ |
469 |
> |
case 'f': |
470 |
|
fwrite(&fval, sizeof(float), 1, stdout); |
471 |
+ |
break; |
472 |
|
} |
473 |
|
} |
474 |
|
|
483 |
|
char *inptr; |
484 |
|
struct field fmt; |
485 |
|
int res; |
486 |
< |
register struct field *f; |
486 |
> |
struct field *f; |
487 |
|
/* check for inline format */ |
488 |
|
for (inptr = spec; *inptr; inptr++) |
489 |
|
if (*inptr == '$') |
496 |
|
eputs(": cannot open\n"); |
497 |
|
quit(1); |
498 |
|
} |
499 |
< |
res = read(fd, inpbuf+1, INBSIZ-1); |
499 |
> |
res = read(fd, inpbuf+2, INBSIZ-2); |
500 |
|
if (res <= 0 || res >= INBSIZ-1) { |
501 |
|
eputs(spec); |
502 |
|
if (res < 0) |
508 |
|
quit(1); |
509 |
|
} |
510 |
|
close(fd); |
511 |
< |
(inptr=inpbuf+1)[res] = '\0'; |
511 |
> |
(inptr=inpbuf+2)[res] = '\0'; |
512 |
|
} |
513 |
|
f = &fmt; /* get fields */ |
514 |
|
while ((res = readfield(&inptr)) != F_NUL) { |
543 |
|
|
544 |
|
static int |
545 |
|
readfield( /* get next field in format */ |
546 |
< |
register char **pp |
546 |
> |
char **pp |
547 |
|
) |
548 |
|
{ |
549 |
|
int type = F_NUL; |
550 |
|
int width = 0; |
551 |
< |
register char *cp; |
551 |
> |
char *cp; |
552 |
|
|
553 |
|
cp = inpbuf; |
554 |
|
while (cp < &inpbuf[INBSIZ-1] && **pp != '\0') { |
613 |
|
char *svname |
614 |
|
) |
615 |
|
{ |
616 |
< |
register struct strvar *sv; |
616 |
> |
struct strvar *sv; |
617 |
|
|
618 |
|
for (sv = svhead; sv != NULL; sv = sv->next) |
619 |
|
if (!strcmp(sv->name, svname)) |
632 |
|
char *eqn |
633 |
|
) |
634 |
|
{ |
635 |
< |
register struct strvar *sv; |
636 |
< |
register char *val; |
635 |
> |
struct strvar *sv; |
636 |
> |
char *val; |
637 |
|
|
638 |
|
for (val = eqn; *val != '='; val++) |
639 |
|
if (!*val) |
652 |
|
static void |
653 |
|
clearrec(void) /* clear input record variables */ |
654 |
|
{ |
655 |
< |
register struct field *f; |
655 |
> |
struct field *f; |
656 |
|
|
657 |
|
for (f = inpfmt; f != NULL; f = f->next) |
658 |
|
switch (f->type & F_TYP) { |
670 |
|
|
671 |
|
|
672 |
|
static int |
673 |
< |
getrec(void) /* get next record from file */ |
673 |
> |
getrec(void) /* get next record from file */ |
674 |
|
{ |
675 |
|
int eatline; |
676 |
< |
register struct field *f; |
677 |
< |
|
676 |
> |
struct field *f; |
677 |
> |
|
678 |
|
while (ipb.chr != EOF) { |
679 |
< |
eatline = !igneol && ipb.chr != '\n'; |
630 |
< |
if (blnkeq) /* beware of nbsynch() */ |
679 |
> |
if (blnkeq) { /* beware of nbsynch() */ |
680 |
|
while (isblnk(ipb.chr)) |
681 |
< |
scaninp(); |
681 |
> |
resetinp(); |
682 |
> |
if (ipb.chr == EOF) |
683 |
> |
return(0); |
684 |
> |
} |
685 |
> |
eatline = (!igneol && ipb.chr != '\n'); |
686 |
|
clearrec(); /* start with fresh record */ |
687 |
|
for (f = inpfmt; f != NULL; f = f->next) |
688 |
|
if (getfield(f) == -1) |
689 |
|
break; |
690 |
|
if (f == NULL) { |
691 |
< |
advinp(); |
691 |
> |
advinp(); /* got one! */ |
692 |
|
return(1); |
693 |
|
} |
694 |
< |
resetinp(); |
694 |
> |
resetinp(); /* eat false start */ |
695 |
|
if (eatline) { /* eat rest of line */ |
696 |
|
while (ipb.chr != '\n') { |
697 |
|
if (ipb.chr == EOF) |
698 |
|
return(0); |
699 |
< |
scaninp(); |
699 |
> |
resetinp(); |
700 |
|
} |
701 |
< |
scaninp(); |
649 |
< |
advinp(); |
701 |
> |
resetinp(); |
702 |
|
} |
703 |
|
} |
704 |
|
return(0); |
707 |
|
|
708 |
|
static int |
709 |
|
getfield( /* get next field */ |
710 |
< |
register struct field *f |
710 |
> |
struct field *f |
711 |
|
) |
712 |
|
{ |
713 |
|
static char buf[RMAXWORD+1]; /* no recursion! */ |
714 |
|
int delim, inword; |
715 |
|
double d; |
716 |
|
char *np; |
717 |
< |
register char *cp; |
717 |
> |
char *cp; |
718 |
|
|
719 |
|
switch (f->type & F_TYP) { |
720 |
|
case T_LIT: |
801 |
|
static void |
802 |
|
putrec(void) /* output a record */ |
803 |
|
{ |
804 |
< |
char fmt[32]; |
805 |
< |
register int n; |
806 |
< |
register struct field *f; |
804 |
> |
char fmt[32], typ[16]; |
805 |
> |
int n; |
806 |
> |
struct field *f; |
807 |
|
int adlast, adnext; |
808 |
+ |
double dv, av; |
809 |
|
|
810 |
|
adlast = 0; |
811 |
|
for (f = outfmt; f != NULL; f = f->next) { |
836 |
|
break; |
837 |
|
case T_NUM: |
838 |
|
n = f->type & F_WID; |
839 |
+ |
dv = evalue(f->f.ne); |
840 |
+ |
av = fabs(dv); |
841 |
+ |
if (n <= 9) |
842 |
+ |
strcpy(typ, "g"); |
843 |
+ |
else |
844 |
+ |
sprintf(typ, ".%de", n-5); |
845 |
+ |
if (av < 1L<<31) { |
846 |
+ |
long iv = (int)(av + .5); |
847 |
+ |
if (iv && fabs(av-iv) <= av*1e-14) |
848 |
+ |
strcpy(typ, ".0f"); |
849 |
+ |
} |
850 |
|
if (adlast && adnext) |
851 |
< |
strcpy(fmt, "%g"); |
851 |
> |
sprintf(fmt, "%%%s", typ); |
852 |
|
else if (adlast) |
853 |
< |
sprintf(fmt, "%%-%dg", n); |
853 |
> |
sprintf(fmt, "%%-%d%s", n, typ); |
854 |
|
else |
855 |
< |
sprintf(fmt, "%%%dg", n); |
856 |
< |
printf(fmt, evalue(f->f.ne)); |
855 |
> |
sprintf(fmt, "%%%d%s", n, typ); |
856 |
> |
printf(fmt, dv); |
857 |
|
adlast = 1; |
858 |
|
break; |
859 |
|
} |
907 |
|
ipb.beg = ipb.end; |
908 |
|
ipb.pos = ipb.beg; |
909 |
|
ipb.chr = *ipb.pos; |
910 |
+ |
if (passive) /* transmit unmatched character? */ |
911 |
+ |
fputc(ipb.chr, stdout); |
912 |
|
if (++ipb.beg >= &inpbuf[INBSIZ]) |
913 |
|
ipb.beg = inpbuf; |
914 |
|
scaninp(); |