8 |
|
*/ |
9 |
|
|
10 |
|
#include <stdlib.h> |
11 |
– |
#include <fcntl.h> |
12 |
– |
#include <stdio.h> |
13 |
– |
#include <string.h> |
11 |
|
#include <math.h> |
12 |
|
#include <ctype.h> |
13 |
|
|
73 |
|
struct field *outfmt = NULL; /* output record structure */ |
74 |
|
struct strvar *svhead = NULL; /* string variables */ |
75 |
|
|
76 |
+ |
long incnt = 0; /* limit number of input records? */ |
77 |
+ |
long outcnt = 0; /* limit number of output records? */ |
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) */ |
107 |
|
char *argv[] |
108 |
|
) |
109 |
|
{ |
110 |
+ |
char *fpath; |
111 |
|
int i; |
112 |
|
|
113 |
|
esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST; |
137 |
|
svpreset(argv[++i]); |
138 |
|
break; |
139 |
|
case 'f': |
140 |
< |
fcompile(argv[++i]); |
140 |
> |
fpath = getpath(argv[++i], getrlibpath(), 0); |
141 |
> |
if (fpath == NULL) { |
142 |
> |
eputs(argv[0]); |
143 |
> |
eputs(": cannot find file '"); |
144 |
> |
eputs(argv[i]); |
145 |
> |
eputs("'\n"); |
146 |
> |
quit(1); |
147 |
> |
} |
148 |
> |
fcompile(fpath); |
149 |
|
break; |
150 |
|
case 'e': |
151 |
|
scompile(argv[++i], NULL, 0); |
160 |
|
nbicols = 0; |
161 |
|
readfmt(argv[++i], 0); |
162 |
|
break; |
163 |
+ |
case 'n': |
164 |
+ |
incnt = atol(argv[++i]); |
165 |
+ |
break; |
166 |
|
case 'a': |
167 |
|
itype = 'a'; |
168 |
|
nbicols = 0; |
203 |
|
otype = 'a'; |
204 |
|
readfmt(argv[++i], 1); |
205 |
|
break; |
206 |
+ |
case 'n': |
207 |
+ |
outcnt = atol(argv[++i]); |
208 |
+ |
break; |
209 |
|
case 'a': |
210 |
|
otype = 'a'; |
211 |
|
break; |
234 |
|
} |
235 |
|
if (otype != 'a') |
236 |
|
SET_FILE_BINARY(stdout); |
237 |
+ |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
238 |
+ |
flockfile(stdout); |
239 |
+ |
#endif |
240 |
|
if (noinput) { /* produce a single output record */ |
241 |
|
if (i < argc) { |
242 |
|
eputs(argv[0]); |
247 |
|
putout(); |
248 |
|
quit(0); |
249 |
|
} |
232 |
– |
if (itype != 'a') |
233 |
– |
SET_FILE_BINARY(stdin); |
234 |
– |
|
250 |
|
if (blnkeq) /* for efficiency */ |
251 |
|
nbsynch(); |
252 |
|
|
281 |
|
if (inpfmt != NULL) |
282 |
|
return(getrec()); |
283 |
|
if (tolower(itype) == 'd') { |
284 |
< |
if (fread(inpbuf, sizeof(double), nbicols, fp) != nbicols) |
284 |
> |
if (getbinary(inpbuf, sizeof(double), nbicols, fp) != nbicols) |
285 |
|
return(0); |
286 |
|
if (itype == 'D') |
287 |
|
swap64(inpbuf, nbicols); |
288 |
|
return(1); |
289 |
|
} |
290 |
|
if (tolower(itype) == 'f') { |
291 |
< |
if (fread(inpbuf, sizeof(float), nbicols, fp) != nbicols) |
291 |
> |
if (getbinary(inpbuf, sizeof(float), nbicols, fp) != nbicols) |
292 |
|
return(0); |
293 |
|
if (itype == 'F') |
294 |
|
swap32(inpbuf, nbicols); |
303 |
|
char *file |
304 |
|
) |
305 |
|
{ |
306 |
< |
int conditional = vardefined("cond"); |
306 |
> |
const int conditional = vardefined("cond"); |
307 |
> |
const int set_recno = (varlookup("recno") != NULL); |
308 |
> |
const int set_outno = (varlookup("outno") != NULL); |
309 |
|
long nrecs = 0; |
310 |
|
long nout = 0; |
311 |
|
FILE *fp; |
317 |
|
eputs(": cannot open\n"); |
318 |
|
quit(1); |
319 |
|
} |
320 |
+ |
if (itype != 'a') |
321 |
+ |
SET_FILE_BINARY(fp); |
322 |
+ |
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
323 |
+ |
flockfile(fp); |
324 |
+ |
#endif |
325 |
|
if (inpfmt != NULL) |
326 |
|
initinp(fp); |
327 |
|
|
328 |
|
while (getinputrec(fp)) { |
329 |
< |
varset("recno", '=', (double)++nrecs); |
329 |
> |
++nrecs; |
330 |
> |
if (set_recno) |
331 |
> |
varset("recno", '=', (double)nrecs); |
332 |
> |
if (set_outno) |
333 |
> |
varset("outno", '=', (double)(nout+1)); |
334 |
|
colflg = 0; |
335 |
|
eclock++; |
336 |
|
if (!conditional || varvalue("cond") > 0.0) { |
311 |
– |
varset("outno", '=', (double)++nout); |
337 |
|
putout(); |
338 |
+ |
++nout; |
339 |
|
} |
340 |
+ |
if (incnt && nrecs >= incnt) |
341 |
+ |
break; |
342 |
+ |
if (outcnt && nout >= outcnt) |
343 |
+ |
break; |
344 |
|
} |
345 |
|
fclose(fp); |
346 |
|
} |
368 |
|
l_in(char *funame) /* function call for $channel */ |
369 |
|
{ |
370 |
|
int n; |
371 |
< |
register char *cp; |
371 |
> |
char *cp; |
372 |
|
/* get argument as integer */ |
373 |
|
n = (int)(argument(1) + .5); |
374 |
|
if (n != 0) /* return channel value */ |
400 |
|
) |
401 |
|
{ |
402 |
|
int i; |
403 |
< |
register char *cp; |
403 |
> |
char *cp; |
404 |
|
|
405 |
|
if (noinput || inpfmt != NULL) { |
406 |
|
eputs("no column input\n"); |
471 |
|
float fval = v; |
472 |
|
|
473 |
|
while (++colpos < n) |
474 |
< |
fwrite(zerobuf, |
474 |
> |
putbinary(zerobuf, |
475 |
|
tolower(otype)=='d' ? sizeof(double) : sizeof(float), |
476 |
|
1, stdout); |
477 |
|
switch (otype) { |
479 |
|
swap64((char *)&v, 1); |
480 |
|
/* fall through */ |
481 |
|
case 'd': |
482 |
< |
fwrite(&v, sizeof(double), 1, stdout); |
482 |
> |
putbinary(&v, sizeof(double), 1, stdout); |
483 |
|
break; |
484 |
|
case 'F': |
485 |
|
swap32((char *)&fval, 1); |
486 |
|
/* fall through */ |
487 |
|
case 'f': |
488 |
< |
fwrite(&fval, sizeof(float), 1, stdout); |
488 |
> |
putbinary(&fval, sizeof(float), 1, stdout); |
489 |
|
break; |
490 |
|
} |
491 |
|
} |
501 |
|
char *inptr; |
502 |
|
struct field fmt; |
503 |
|
int res; |
504 |
< |
register struct field *f; |
504 |
> |
struct field *f; |
505 |
|
/* check for inline format */ |
506 |
|
for (inptr = spec; *inptr; inptr++) |
507 |
|
if (*inptr == '$') |
561 |
|
|
562 |
|
static int |
563 |
|
readfield( /* get next field in format */ |
564 |
< |
register char **pp |
564 |
> |
char **pp |
565 |
|
) |
566 |
|
{ |
567 |
|
int type = F_NUL; |
568 |
|
int width = 0; |
569 |
< |
register char *cp; |
569 |
> |
char *cp; |
570 |
|
|
571 |
|
cp = inpbuf; |
572 |
|
while (cp < &inpbuf[INBSIZ-1] && **pp != '\0') { |
631 |
|
char *svname |
632 |
|
) |
633 |
|
{ |
634 |
< |
register struct strvar *sv; |
634 |
> |
struct strvar *sv; |
635 |
|
|
636 |
|
for (sv = svhead; sv != NULL; sv = sv->next) |
637 |
|
if (!strcmp(sv->name, svname)) |
650 |
|
char *eqn |
651 |
|
) |
652 |
|
{ |
653 |
< |
register struct strvar *sv; |
654 |
< |
register char *val; |
653 |
> |
struct strvar *sv; |
654 |
> |
char *val; |
655 |
|
|
656 |
|
for (val = eqn; *val != '='; val++) |
657 |
|
if (!*val) |
670 |
|
static void |
671 |
|
clearrec(void) /* clear input record variables */ |
672 |
|
{ |
673 |
< |
register struct field *f; |
673 |
> |
struct field *f; |
674 |
|
|
675 |
|
for (f = inpfmt; f != NULL; f = f->next) |
676 |
|
switch (f->type & F_TYP) { |
691 |
|
getrec(void) /* get next record from file */ |
692 |
|
{ |
693 |
|
int eatline; |
694 |
< |
register struct field *f; |
694 |
> |
struct field *f; |
695 |
|
|
696 |
|
while (ipb.chr != EOF) { |
697 |
|
if (blnkeq) { /* beware of nbsynch() */ |
725 |
|
|
726 |
|
static int |
727 |
|
getfield( /* get next field */ |
728 |
< |
register struct field *f |
728 |
> |
struct field *f |
729 |
|
) |
730 |
|
{ |
731 |
|
static char buf[RMAXWORD+1]; /* no recursion! */ |
732 |
|
int delim, inword; |
733 |
|
double d; |
734 |
|
char *np; |
735 |
< |
register char *cp; |
735 |
> |
char *cp; |
736 |
|
|
737 |
|
switch (f->type & F_TYP) { |
738 |
|
case T_LIT: |
819 |
|
static void |
820 |
|
putrec(void) /* output a record */ |
821 |
|
{ |
822 |
< |
char fmt[32]; |
823 |
< |
register int n; |
824 |
< |
register struct field *f; |
822 |
> |
char fmt[32], typ[16]; |
823 |
> |
int n; |
824 |
> |
struct field *f; |
825 |
|
int adlast, adnext; |
826 |
+ |
double dv, av; |
827 |
|
|
828 |
|
adlast = 0; |
829 |
|
for (f = outfmt; f != NULL; f = f->next) { |
854 |
|
break; |
855 |
|
case T_NUM: |
856 |
|
n = f->type & F_WID; |
857 |
+ |
dv = evalue(f->f.ne); |
858 |
+ |
av = fabs(dv); |
859 |
+ |
if (n <= 9) |
860 |
+ |
strcpy(typ, "g"); |
861 |
+ |
else |
862 |
+ |
sprintf(typ, ".%de", n-5); |
863 |
+ |
if (av < 1L<<31) { |
864 |
+ |
long iv = (int)(av + .5); |
865 |
+ |
if (iv && fabs(av-iv) <= av*1e-14) |
866 |
+ |
strcpy(typ, ".0f"); |
867 |
+ |
} |
868 |
|
if (adlast && adnext) |
869 |
< |
strcpy(fmt, "%g"); |
869 |
> |
sprintf(fmt, "%%%s", typ); |
870 |
|
else if (adlast) |
871 |
< |
sprintf(fmt, "%%-%dg", n); |
871 |
> |
sprintf(fmt, "%%-%d%s", n, typ); |
872 |
|
else |
873 |
< |
sprintf(fmt, "%%%dg", n); |
874 |
< |
printf(fmt, evalue(f->f.ne)); |
873 |
> |
sprintf(fmt, "%%%d%s", n, typ); |
874 |
> |
printf(fmt, dv); |
875 |
|
adlast = 1; |
876 |
|
break; |
877 |
|
} |