2 |
|
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
4 |
|
/* |
5 |
< |
* rcalc.c - record calculator program. |
5 |
> |
* rcalc.c - record calculator program. |
6 |
|
* |
7 |
< |
* 9/11/87 |
7 |
> |
* 9/11/87 |
8 |
|
*/ |
9 |
|
|
10 |
< |
#include <stdlib.h> |
11 |
< |
#include <math.h> |
12 |
< |
#include <ctype.h> |
10 |
> |
#include <stdlib.h> |
11 |
> |
#include <math.h> |
12 |
> |
#include <ctype.h> |
13 |
|
|
14 |
< |
#include "platform.h" |
15 |
< |
#include "rterror.h" |
16 |
< |
#include "rtmisc.h" |
17 |
< |
#include "rtio.h" |
18 |
< |
#include "calcomp.h" |
14 |
> |
#include "platform.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') |
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') |
23 |
> |
#define isblnk(c) (igneol ? isspace(c) : ((c)==' ')|((c)=='\t')) |
24 |
|
|
25 |
< |
#define INBSIZ 16384 /* longest record */ |
26 |
< |
#define MAXCOL 32 /* number of columns recorded */ |
25 |
> |
#define INBSIZ 16384 /* longest record */ |
26 |
> |
#define MAXCOL 32 /* number of columns recorded */ |
27 |
|
|
28 |
|
/* field type specifications */ |
29 |
< |
#define F_NUL 0 /* empty */ |
30 |
< |
#define F_TYP 0x7000 /* mask for type */ |
31 |
< |
#define F_WID 0x0fff /* mask for width */ |
32 |
< |
#define T_LIT 0x1000 /* string literal */ |
33 |
< |
#define T_STR 0x2000 /* string variable */ |
34 |
< |
#define T_NUM 0x3000 /* numeric value */ |
29 |
> |
#define F_NUL 0 /* empty */ |
30 |
> |
#define F_TYP 0x7000 /* mask for type */ |
31 |
> |
#define F_WID 0x0fff /* mask for width */ |
32 |
> |
#define T_LIT 0x1000 /* string literal */ |
33 |
> |
#define T_STR 0x2000 /* string variable */ |
34 |
> |
#define T_NUM 0x3000 /* numeric value */ |
35 |
|
|
36 |
< |
struct strvar { /* string variable */ |
37 |
< |
char *name; |
38 |
< |
char *val; |
39 |
< |
char *preset; |
40 |
< |
struct strvar *next; |
36 |
> |
struct strvar { /* string variable */ |
37 |
> |
char *name; |
38 |
> |
char *val; |
39 |
> |
char *preset; |
40 |
> |
struct strvar *next; |
41 |
|
}; |
42 |
|
|
43 |
< |
struct field { /* record format structure */ |
44 |
< |
int type; /* type of field (& width) */ |
43 |
> |
struct field { /* record format structure */ |
44 |
> |
int type; /* type of field (& width) */ |
45 |
|
union { |
46 |
< |
char *sl; /* string literal */ |
47 |
< |
struct strvar *sv; /* string variable */ |
48 |
< |
char *nv; /* numeric variable */ |
49 |
< |
EPNODE *ne; /* numeric expression */ |
50 |
< |
} f; /* field contents */ |
51 |
< |
struct field *next; /* next field in record */ |
46 |
> |
char *sl; /* string literal */ |
47 |
> |
struct strvar *sv; /* string variable */ |
48 |
> |
char *nv; /* numeric variable */ |
49 |
> |
EPNODE *ne; /* numeric expression */ |
50 |
> |
} f; /* field contents */ |
51 |
> |
struct field *next; /* next field in record */ |
52 |
|
}; |
53 |
|
|
54 |
< |
#define savqstr(s) strcpy(emalloc(strlen(s)+1),s) |
55 |
< |
#define freqstr(s) efree(s) |
54 |
> |
#define savqstr(s) strcpy(emalloc(strlen(s)+1),s) |
55 |
> |
#define freqstr(s) efree(s) |
56 |
|
|
57 |
|
static int getinputrec(FILE *fp); |
58 |
< |
static void scaninp(void), advinp(void), resetinp(void); |
58 |
> |
static void scaninp(void), advinp(void), skipinp(void); |
59 |
|
static void putrec(void), putout(void), nbsynch(void); |
60 |
|
static int getrec(void); |
61 |
|
static void execute(char *file); |
69 |
|
static struct strvar* getsvar(char *svname); |
70 |
|
static double l_in(char *); |
71 |
|
|
72 |
< |
struct field *inpfmt = NULL; /* input record format */ |
73 |
< |
struct field *outfmt = NULL; /* output record structure */ |
74 |
< |
struct strvar *svhead = NULL; /* string variables */ |
72 |
> |
struct field *inpfmt = NULL; /* input record format */ |
73 |
> |
struct field *outfmt = NULL; /* output record structure */ |
74 |
> |
struct strvar *svhead = NULL; /* string variables */ |
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 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 */ |
87 |
< |
int colpos; /* output column position */ |
76 |
> |
long incnt = 0; /* limit number of input records? */ |
77 |
> |
long outcnt = 0; /* limit number of output records? */ |
78 |
|
|
79 |
< |
int nowarn = 0; /* non-fatal diagnostic output */ |
80 |
< |
int unbuff = 0; /* unbuffered output (flush each record) */ |
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 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 */ |
90 |
> |
int colpos; /* output column position */ |
91 |
|
|
92 |
+ |
int nowarn = 0; /* non-fatal diagnostic output */ |
93 |
+ |
int unbuff = 0; /* unbuffered output (flush each record) */ |
94 |
+ |
|
95 |
|
struct { |
96 |
< |
FILE *fin; /* input file */ |
97 |
< |
int chr; /* next character */ |
98 |
< |
char *beg; /* home position */ |
99 |
< |
char *pos; /* scan position */ |
100 |
< |
char *end; /* read position */ |
101 |
< |
} ipb; /* circular lookahead buffer */ |
96 |
> |
FILE *fin; /* input file */ |
97 |
> |
int chr; /* next character */ |
98 |
> |
char *beg; /* home position */ |
99 |
> |
char *pos; /* scan position */ |
100 |
> |
char *end; /* read position */ |
101 |
> |
} ipb; /* circular lookahead buffer */ |
102 |
|
|
103 |
|
|
104 |
|
int |
105 |
|
main( |
106 |
< |
int argc, |
107 |
< |
char *argv[] |
106 |
> |
int argc, |
107 |
> |
char *argv[] |
108 |
|
) |
109 |
|
{ |
110 |
< |
char *fpath; |
111 |
< |
int i; |
110 |
> |
char *fpath; |
111 |
> |
int i; |
112 |
|
|
113 |
|
esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST; |
114 |
|
esupport &= ~(E_REDEFW); |
115 |
|
|
116 |
< |
#ifdef BIGGERLIB |
116 |
> |
#ifdef BIGGERLIB |
117 |
|
biggerlib(); |
118 |
|
#endif |
119 |
|
varset("PI", ':', 3.14159265358979323846); |
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; |
237 |
|
#ifdef getc_unlocked /* avoid lock/unlock overhead */ |
238 |
|
flockfile(stdout); |
239 |
|
#endif |
240 |
< |
if (noinput) { /* produce a single output record */ |
240 |
> |
if (noinput) { /* produce a single output record */ |
241 |
|
if (i < argc) { |
242 |
|
eputs(argv[0]); |
243 |
|
eputs(": file argument(s) incompatible with -n\n"); |
247 |
|
putout(); |
248 |
|
quit(0); |
249 |
|
} |
250 |
< |
if (blnkeq) /* for efficiency */ |
250 |
> |
if (blnkeq) /* for efficiency */ |
251 |
|
nbsynch(); |
252 |
|
|
253 |
< |
if (i == argc) /* from stdin */ |
253 |
> |
if (i == argc) /* from stdin */ |
254 |
|
execute(NULL); |
255 |
< |
else /* from one or more files */ |
255 |
> |
else /* from one or more files */ |
256 |
|
for ( ; i < argc; i++) |
257 |
|
execute(argv[i]); |
258 |
|
|
262 |
|
|
263 |
|
|
264 |
|
static void |
265 |
< |
nbsynch(void) /* non-blank starting synch character */ |
265 |
> |
nbsynch(void) /* non-blank starting synch character */ |
266 |
|
{ |
267 |
|
if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT) |
268 |
|
return; |
275 |
|
|
276 |
|
static int |
277 |
|
getinputrec( /* get next input record */ |
278 |
< |
FILE *fp |
278 |
> |
FILE *fp |
279 |
|
) |
280 |
|
{ |
281 |
|
if (inpfmt != NULL) |
282 |
|
return(getrec()); |
283 |
< |
if (tolower(itype) == 'd') { |
284 |
< |
if (fread(inpbuf, sizeof(double), nbicols, fp) != nbicols) |
283 |
> |
if ((itype == 'd') | (itype == 'D')) { |
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) |
290 |
> |
if ((itype == 'f') | (itype == 'F')) { |
291 |
> |
if (getbinary(inpbuf, sizeof(float), nbicols, fp) != nbicols) |
292 |
|
return(0); |
293 |
|
if (itype == 'F') |
294 |
|
swap32(inpbuf, nbicols); |
299 |
|
|
300 |
|
|
301 |
|
static void |
302 |
< |
execute( /* process a file */ |
303 |
< |
char *file |
302 |
> |
execute( /* process a file */ |
303 |
> |
char *file |
304 |
|
) |
305 |
|
{ |
306 |
< |
int conditional = vardefined("cond"); |
307 |
< |
long nrecs = 0; |
308 |
< |
long nout = 0; |
309 |
< |
FILE *fp; |
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; |
312 |
|
|
313 |
|
if (file == NULL) |
314 |
|
fp = stdin; |
326 |
|
initinp(fp); |
327 |
|
|
328 |
|
while (getinputrec(fp)) { |
329 |
< |
varset("recno", '=', (double)++nrecs); |
330 |
< |
varset("outno", '=', (double)(nout+1)); |
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) { |
337 |
|
putout(); |
338 |
|
++nout; |
339 |
|
} |
340 |
+ |
if (incnt && nrecs >= incnt) |
341 |
+ |
break; |
342 |
+ |
if (outcnt && nout >= outcnt) |
343 |
+ |
break; |
344 |
|
} |
345 |
|
fclose(fp); |
346 |
|
} |
347 |
|
|
348 |
|
|
349 |
|
static void |
350 |
< |
putout(void) /* produce an output record */ |
350 |
> |
putout(void) /* produce an output record */ |
351 |
|
{ |
352 |
|
|
353 |
|
colpos = 0; |
367 |
|
static double |
368 |
|
l_in(char *funame) /* function call for $channel */ |
369 |
|
{ |
370 |
< |
int n; |
371 |
< |
char *cp; |
370 |
> |
int n; |
371 |
> |
char *cp; |
372 |
|
/* get argument as integer */ |
373 |
|
n = (int)(argument(1) + .5); |
374 |
|
if (n != 0) /* return channel value */ |
395 |
|
} |
396 |
|
|
397 |
|
double |
398 |
< |
chanvalue( /* return value for column n */ |
399 |
< |
int n |
398 |
> |
chanvalue( /* return value for column n */ |
399 |
> |
int n |
400 |
|
) |
401 |
|
{ |
402 |
< |
int i; |
403 |
< |
char *cp; |
402 |
> |
int i; |
403 |
> |
char *cp; |
404 |
|
|
405 |
|
if (noinput || inpfmt != NULL) { |
406 |
|
eputs("no column input\n"); |
413 |
|
if (nbicols) { |
414 |
|
if (n > nbicols) |
415 |
|
return(0.0); |
416 |
< |
if (tolower(itype) == 'd') { |
416 |
> |
if ((itype == 'd') | (itype == 'D')) { |
417 |
|
cp = inpbuf + (n-1)*sizeof(double); |
418 |
|
return(*(double *)cp); |
419 |
|
} |
434 |
|
while (*cp && *cp++ != sepchar) |
435 |
|
; |
436 |
|
|
437 |
< |
while (isspace(*cp)) /* some atof()'s don't like tabs */ |
437 |
> |
while (isspace(*cp)) /* some atof()'s don't like tabs */ |
438 |
|
cp++; |
439 |
|
|
440 |
|
if (n <= MAXCOL) { |
446 |
|
|
447 |
|
|
448 |
|
void |
449 |
< |
chanset( /* output column n */ |
450 |
< |
int n, |
451 |
< |
double v |
449 |
> |
chanset( /* output column n */ |
450 |
> |
int n, |
451 |
> |
double v |
452 |
|
) |
453 |
|
{ |
454 |
< |
if (colpos == 0) /* no leading separator */ |
454 |
> |
if (colpos == 0) /* no leading separator */ |
455 |
|
colpos = 1; |
456 |
|
while (colpos < n) { |
457 |
|
putchar(sepchar); |
462 |
|
|
463 |
|
|
464 |
|
void |
465 |
< |
bchanset( /* output binary channel n */ |
466 |
< |
int n, |
467 |
< |
double v |
465 |
> |
bchanset( /* output binary channel n */ |
466 |
> |
int n, |
467 |
> |
double v |
468 |
|
) |
469 |
|
{ |
470 |
|
static char zerobuf[sizeof(double)]; |
471 |
+ |
const int otlen = ((otype == 'd') | (otype == 'D')) ? |
472 |
+ |
sizeof(double) : sizeof(float); |
473 |
|
float fval = v; |
474 |
|
|
475 |
|
while (++colpos < n) |
476 |
< |
fwrite(zerobuf, |
457 |
< |
tolower(otype)=='d' ? sizeof(double) : sizeof(float), |
458 |
< |
1, stdout); |
476 |
> |
putbinary(zerobuf, otlen, 1, stdout); |
477 |
|
switch (otype) { |
478 |
|
case 'D': |
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 |
|
} |
492 |
|
|
493 |
|
|
494 |
|
static void |
495 |
< |
readfmt( /* read record format */ |
496 |
< |
char *spec, |
497 |
< |
int output |
495 |
> |
readfmt( /* read record format */ |
496 |
> |
char *spec, |
497 |
> |
int output |
498 |
|
) |
499 |
|
{ |
500 |
< |
int fd; |
501 |
< |
char *inptr; |
502 |
< |
struct field fmt; |
503 |
< |
int res; |
504 |
< |
struct field *f; |
500 |
> |
int fd; |
501 |
> |
char *inptr; |
502 |
> |
struct field fmt; |
503 |
> |
int res; |
504 |
> |
struct field *f; |
505 |
|
/* check for inline format */ |
506 |
|
for (inptr = spec; *inptr; inptr++) |
507 |
|
if (*inptr == '$') |
508 |
|
break; |
509 |
< |
if (*inptr) /* inline */ |
509 |
> |
if (*inptr) /* inline */ |
510 |
|
inptr = spec; |
511 |
< |
else { /* from file */ |
511 |
> |
else { /* from file */ |
512 |
|
if ((fd = open(spec, 0)) == -1) { |
513 |
|
eputs(spec); |
514 |
|
eputs(": cannot open\n"); |
528 |
|
close(fd); |
529 |
|
(inptr=inpbuf+2)[res] = '\0'; |
530 |
|
} |
531 |
< |
f = &fmt; /* get fields */ |
531 |
> |
f = &fmt; /* get fields */ |
532 |
|
while ((res = readfield(&inptr)) != F_NUL) { |
533 |
|
f->next = (struct field *)emalloc(sizeof(struct field)); |
534 |
|
f = f->next; |
560 |
|
|
561 |
|
|
562 |
|
static int |
563 |
< |
readfield( /* get next field in format */ |
564 |
< |
char **pp |
563 |
> |
readfield( /* get next field in format */ |
564 |
> |
char **pp |
565 |
|
) |
566 |
|
{ |
567 |
< |
int type = F_NUL; |
568 |
< |
int width = 0; |
569 |
< |
char *cp; |
567 |
> |
int type = F_NUL; |
568 |
> |
int width = 0; |
569 |
> |
char *cp; |
570 |
|
|
571 |
|
cp = inpbuf; |
572 |
|
while (cp < &inpbuf[INBSIZ-1] && **pp != '\0') { |
627 |
|
|
628 |
|
|
629 |
|
struct strvar * |
630 |
< |
getsvar( /* get string variable */ |
631 |
< |
char *svname |
630 |
> |
getsvar( /* get string variable */ |
631 |
> |
char *svname |
632 |
|
) |
633 |
|
{ |
634 |
< |
struct strvar *sv; |
634 |
> |
struct strvar *sv; |
635 |
|
|
636 |
|
for (sv = svhead; sv != NULL; sv = sv->next) |
637 |
|
if (!strcmp(sv->name, svname)) |
646 |
|
|
647 |
|
|
648 |
|
static void |
649 |
< |
svpreset( /* preset a string variable */ |
650 |
< |
char *eqn |
649 |
> |
svpreset( /* preset a string variable */ |
650 |
> |
char *eqn |
651 |
|
) |
652 |
|
{ |
653 |
< |
struct strvar *sv; |
654 |
< |
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 |
< |
struct field *f; |
673 |
> |
struct field *f; |
674 |
|
|
675 |
|
for (f = inpfmt; f != NULL; f = f->next) |
676 |
|
switch (f->type & F_TYP) { |
690 |
|
static int |
691 |
|
getrec(void) /* get next record from file */ |
692 |
|
{ |
693 |
< |
int eatline; |
694 |
< |
struct field *f; |
693 |
> |
int eatline; |
694 |
> |
struct field *f; |
695 |
|
|
696 |
|
while (ipb.chr != EOF) { |
697 |
|
if (blnkeq) { /* beware of nbsynch() */ |
698 |
|
while (isblnk(ipb.chr)) |
699 |
< |
resetinp(); |
699 |
> |
skipinp(); |
700 |
|
if (ipb.chr == EOF) |
701 |
|
return(0); |
702 |
|
} |
703 |
< |
eatline = (!igneol && ipb.chr != '\n'); |
703 |
> |
eatline = !igneol & (ipb.chr != '\n'); |
704 |
|
clearrec(); /* start with fresh record */ |
705 |
|
for (f = inpfmt; f != NULL; f = f->next) |
706 |
< |
if (getfield(f) == -1) |
706 |
> |
if (!getfield(f)) |
707 |
|
break; |
708 |
|
if (f == NULL) { |
709 |
< |
advinp(); /* got one! */ |
709 |
> |
advinp(); /* got one! */ |
710 |
|
return(1); |
711 |
|
} |
712 |
< |
resetinp(); /* eat false start */ |
713 |
< |
if (eatline) { /* eat rest of line */ |
712 |
> |
skipinp(); /* eat false start */ |
713 |
> |
if (eatline) { /* eat rest of line */ |
714 |
|
while (ipb.chr != '\n') { |
715 |
|
if (ipb.chr == EOF) |
716 |
|
return(0); |
717 |
< |
resetinp(); |
717 |
> |
skipinp(); |
718 |
|
} |
719 |
< |
resetinp(); |
719 |
> |
skipinp(); |
720 |
|
} |
721 |
|
} |
722 |
|
return(0); |
724 |
|
|
725 |
|
|
726 |
|
static int |
727 |
< |
getfield( /* get next field */ |
728 |
< |
struct field *f |
727 |
> |
getfield( /* get next field */ |
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 |
< |
char *cp; |
731 |
> |
static char buf[RMAXWORD+1]; /* no recursion! */ |
732 |
> |
int delim, inword; |
733 |
> |
double d; |
734 |
> |
char *np; |
735 |
> |
char *cp; |
736 |
|
|
737 |
|
switch (f->type & F_TYP) { |
738 |
|
case T_LIT: |
740 |
|
do { |
741 |
|
if (blnkeq && isblnk(*cp)) { |
742 |
|
if (!isblnk(ipb.chr)) |
743 |
< |
return(-1); |
743 |
> |
return(0); |
744 |
|
do |
745 |
|
cp++; |
746 |
|
while (isblnk(*cp)); |
751 |
|
cp++; |
752 |
|
scaninp(); |
753 |
|
} else |
754 |
< |
return(-1); |
754 |
> |
return(0); |
755 |
|
} while (*cp); |
756 |
< |
return(0); |
756 |
> |
break; |
757 |
|
case T_STR: |
758 |
|
if (f->next == NULL || (f->next->type & F_TYP) != T_LIT) |
759 |
|
delim = EOF; |
761 |
|
delim = f->next->f.sl[0]; |
762 |
|
cp = buf; |
763 |
|
do { |
764 |
< |
if (ipb.chr == EOF || ipb.chr == '\n') |
764 |
> |
if ((ipb.chr == EOF) | (ipb.chr == '\n')) |
765 |
|
inword = 0; |
766 |
|
else if (blnkeq && delim != EOF) |
767 |
|
inword = isblnk(delim) ? |
778 |
|
if (f->f.sv->val == NULL) |
779 |
|
f->f.sv->val = savqstr(buf); /* first setting */ |
780 |
|
else if (strcmp(f->f.sv->val, buf)) |
781 |
< |
return(-1); /* doesn't match! */ |
782 |
< |
return(0); |
781 |
> |
return(0); /* doesn't match! */ |
782 |
> |
break; |
783 |
|
case T_NUM: |
784 |
|
if (f->next == NULL || (f->next->type & F_TYP) != T_LIT) |
785 |
|
delim = EOF; |
809 |
|
varset(f->f.nv, '=', d); /* first setting */ |
810 |
|
else if ((d = (varvalue(f->f.nv)-d)/(d==0.?1.:d)) > .001 |
811 |
|
|| d < -.001) |
812 |
< |
return(-1); /* doesn't match! */ |
813 |
< |
return(0); |
812 |
> |
return(0); /* doesn't match! */ |
813 |
> |
break; |
814 |
|
} |
815 |
< |
return -1; /* pro forma return */ |
815 |
> |
return(1); /* success! */ |
816 |
|
} |
817 |
|
|
818 |
|
|
819 |
|
static void |
820 |
< |
putrec(void) /* output a record */ |
820 |
> |
putrec(void) /* output a record */ |
821 |
|
{ |
822 |
< |
char fmt[32], typ[32]; |
823 |
< |
int n; |
824 |
< |
struct field *f; |
825 |
< |
int adlast, adnext; |
826 |
< |
double dv, av; |
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) { |
830 |
< |
adnext = blnkeq && |
830 |
> |
adnext = blnkeq && |
831 |
|
f->next != NULL && |
832 |
|
!( (f->next->type&F_TYP) == T_LIT && |
833 |
|
f->next->f.sl[0] == ' ' ); |
854 |
|
break; |
855 |
|
case T_NUM: |
856 |
|
n = f->type & F_WID; |
839 |
– |
typ[0] = (n <= 6) ? 'g' : 'e'; |
840 |
– |
typ[1] = '\0'; |
857 |
|
dv = evalue(f->f.ne); |
858 |
< |
if ((av = fabs(dv)) < 1L<<31) { |
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"); |
880 |
|
|
881 |
|
|
882 |
|
static void |
883 |
< |
initinp(FILE *fp) /* prepare lookahead buffer */ |
883 |
> |
initinp(FILE *fp) /* prepare lookahead buffer */ |
884 |
|
|
885 |
|
{ |
886 |
|
ipb.fin = fp; |
887 |
|
ipb.beg = ipb.end = inpbuf; |
888 |
< |
ipb.pos = inpbuf-1; /* position before beginning */ |
888 |
> |
ipb.pos = inpbuf-1; /* position before beginning */ |
889 |
|
ipb.chr = '\0'; |
890 |
|
scaninp(); |
891 |
|
} |
892 |
|
|
893 |
|
|
894 |
|
static void |
895 |
< |
scaninp(void) /* scan next character */ |
895 |
> |
scaninp(void) /* scan next character */ |
896 |
|
{ |
897 |
|
if (ipb.chr == EOF) |
898 |
|
return; |
899 |
|
if (++ipb.pos >= &inpbuf[INBSIZ]) |
900 |
|
ipb.pos = inpbuf; |
901 |
< |
if (ipb.pos == ipb.end) { /* new character */ |
901 |
> |
if (ipb.pos == ipb.end) { /* new character */ |
902 |
|
if ((ipb.chr = getc(ipb.fin)) != EOF) { |
903 |
|
*ipb.end = ipb.chr; |
904 |
|
if (++ipb.end >= &inpbuf[INBSIZ]) |
912 |
|
|
913 |
|
|
914 |
|
static void |
915 |
< |
advinp(void) /* move home to current position */ |
915 |
> |
advinp(void) /* move home to current position */ |
916 |
|
{ |
917 |
|
ipb.beg = ipb.pos; |
918 |
|
} |
919 |
|
|
920 |
|
|
921 |
|
static void |
922 |
< |
resetinp(void) /* rewind position and advance 1 */ |
922 |
> |
skipinp(void) /* rewind position and advance 1 */ |
923 |
|
{ |
924 |
< |
if (ipb.beg == NULL) /* full */ |
924 |
> |
if (ipb.beg == NULL) /* full */ |
925 |
|
ipb.beg = ipb.end; |
926 |
|
ipb.pos = ipb.beg; |
927 |
|
ipb.chr = *ipb.pos; |
928 |
|
if (passive) /* transmit unmatched character? */ |
929 |
< |
fputc(ipb.chr, stdout); |
929 |
> |
putchar(ipb.chr); |
930 |
|
if (++ipb.beg >= &inpbuf[INBSIZ]) |
931 |
|
ipb.beg = inpbuf; |
932 |
|
scaninp(); |
934 |
|
|
935 |
|
|
936 |
|
void |
937 |
< |
eputs(char *msg) |
937 |
> |
eputs(char *msg) |
938 |
|
{ |
939 |
|
fputs(msg, stderr); |
940 |
|
} |
941 |
|
|
942 |
|
|
943 |
|
void |
944 |
< |
wputs(char *msg) |
944 |
> |
wputs(char *msg) |
945 |
|
{ |
946 |
|
if (!nowarn) |
947 |
|
eputs(msg); |
949 |
|
|
950 |
|
|
951 |
|
void |
952 |
< |
quit(int code) |
952 |
> |
quit(int code) |
953 |
|
{ |
954 |
|
exit(code); |
955 |
|
} |