1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: rcalc.c,v 1.19 2005/06/14 01:25:02 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* rcalc.c - record calculator program. |
6 |
* |
7 |
* 9/11/87 |
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" |
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 16384 /* longest record */ |
29 |
#define MAXCOL 32 /* number of columns recorded */ |
30 |
|
31 |
/* field type specifications */ |
32 |
#define F_NUL 0 /* empty */ |
33 |
#define F_TYP 0x7000 /* mask for type */ |
34 |
#define F_WID 0x0fff /* mask for width */ |
35 |
#define T_LIT 0x1000 /* string literal */ |
36 |
#define T_STR 0x2000 /* string variable */ |
37 |
#define T_NUM 0x3000 /* numeric value */ |
38 |
|
39 |
struct strvar { /* string variable */ |
40 |
char *name; |
41 |
char *val; |
42 |
char *preset; |
43 |
struct strvar *next; |
44 |
}; |
45 |
|
46 |
struct field { /* record format structure */ |
47 |
int type; /* type of field (& width) */ |
48 |
union { |
49 |
char *sl; /* string literal */ |
50 |
struct strvar *sv; /* string variable */ |
51 |
char *nv; /* numeric variable */ |
52 |
EPNODE *ne; /* numeric expression */ |
53 |
} f; /* field contents */ |
54 |
struct field *next; /* next field in record */ |
55 |
}; |
56 |
|
57 |
#define savqstr(s) strcpy(emalloc(strlen(s)+1),s) |
58 |
#define freqstr(s) efree(s) |
59 |
|
60 |
static int getinputrec(FILE *fp); |
61 |
static void scaninp(void), advinp(void), resetinp(void); |
62 |
static void putrec(void), putout(void), nbsynch(void); |
63 |
static int getrec(void); |
64 |
static void execute(char *file); |
65 |
static void initinp(FILE *fp); |
66 |
static void svpreset(char *eqn); |
67 |
static void readfmt(char *spec, int output); |
68 |
static int readfield(char **pp); |
69 |
static int getfield(struct field *f); |
70 |
static void chanset(int n, double v); |
71 |
static void bchanset(int n, double v); |
72 |
static struct strvar* getsvar(char *svname); |
73 |
static double l_in(char *); |
74 |
|
75 |
struct field *inpfmt = NULL; /* input record format */ |
76 |
struct field *outfmt = NULL; /* output record structure */ |
77 |
struct strvar *svhead = NULL; /* string variables */ |
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 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 */ |
102 |
|
103 |
|
104 |
int |
105 |
main( |
106 |
int argc, |
107 |
char *argv[] |
108 |
) |
109 |
{ |
110 |
int i; |
111 |
|
112 |
esupport |= E_VARIABLE|E_FUNCTION|E_INCHAN|E_OUTCHAN|E_RCONST; |
113 |
esupport &= ~(E_REDEFW); |
114 |
|
115 |
#ifdef BIGGERLIB |
116 |
biggerlib(); |
117 |
#endif |
118 |
varset("PI", ':', 3.14159265358979323846); |
119 |
funset("in", 1, '=', &l_in); |
120 |
|
121 |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
122 |
switch (argv[i][1]) { |
123 |
case 'b': |
124 |
blnkeq = !blnkeq; |
125 |
break; |
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; |
135 |
case 's': |
136 |
svpreset(argv[++i]); |
137 |
break; |
138 |
case 'f': |
139 |
fcompile(argv[++i]); |
140 |
break; |
141 |
case 'e': |
142 |
scompile(argv[++i], NULL, 0); |
143 |
break; |
144 |
case 'n': |
145 |
noinput = 1; |
146 |
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 |
164 |
nbicols = 1; |
165 |
if (nbicols*sizeof(double) > INBSIZ) { |
166 |
eputs(argv[0]); |
167 |
eputs(": too many input columns\n"); |
168 |
quit(1); |
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); |
176 |
else |
177 |
nbicols = 1; |
178 |
if (nbicols*sizeof(float) > INBSIZ) { |
179 |
eputs(argv[0]); |
180 |
eputs(": too many input columns\n"); |
181 |
quit(1); |
182 |
} |
183 |
break; |
184 |
default: |
185 |
goto userr; |
186 |
} |
187 |
break; |
188 |
case 'o': |
189 |
switch (argv[i][2]) { |
190 |
case '\0': |
191 |
otype = 'a'; |
192 |
readfmt(argv[++i], 1); |
193 |
break; |
194 |
case 'a': |
195 |
otype = 'a'; |
196 |
break; |
197 |
case 'd': |
198 |
case 'D': |
199 |
case 'f': |
200 |
case 'F': |
201 |
otype = argv[i][2]; |
202 |
break; |
203 |
default: |
204 |
goto userr; |
205 |
} |
206 |
break; |
207 |
case 'w': |
208 |
nowarn = !nowarn; |
209 |
break; |
210 |
case 'u': |
211 |
unbuff = !unbuff; |
212 |
break; |
213 |
default:; |
214 |
userr: |
215 |
eputs("Usage: "); |
216 |
eputs(argv[0]); |
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 |
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(); |
237 |
|
238 |
if (i == argc) /* from stdin */ |
239 |
execute(NULL); |
240 |
else /* from one or more files */ |
241 |
for ( ; i < argc; i++) |
242 |
execute(argv[i]); |
243 |
|
244 |
quit(0); |
245 |
return 0; /* pro forma return */ |
246 |
} |
247 |
|
248 |
|
249 |
static void |
250 |
nbsynch(void) /* non-blank starting synch character */ |
251 |
{ |
252 |
if (inpfmt == NULL || (inpfmt->type & F_TYP) != T_LIT) |
253 |
return; |
254 |
while (isblnk(*inpfmt->f.sl)) |
255 |
inpfmt->f.sl++; |
256 |
if (!*inpfmt->f.sl) |
257 |
inpfmt = inpfmt->next; |
258 |
} |
259 |
|
260 |
|
261 |
static int |
262 |
getinputrec( /* get next input record */ |
263 |
FILE *fp |
264 |
) |
265 |
{ |
266 |
if (inpfmt != NULL) |
267 |
return(getrec()); |
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 |
|
285 |
|
286 |
static void |
287 |
execute( /* process a file */ |
288 |
char *file |
289 |
) |
290 |
{ |
291 |
int conditional = vardefined("cond"); |
292 |
long nrecs = 0; |
293 |
long nout = 0; |
294 |
FILE *fp; |
295 |
|
296 |
if (file == NULL) |
297 |
fp = stdin; |
298 |
else if ((fp = fopen(file, "r")) == NULL) { |
299 |
eputs(file); |
300 |
eputs(": cannot open\n"); |
301 |
quit(1); |
302 |
} |
303 |
if (inpfmt != NULL) |
304 |
initinp(fp); |
305 |
|
306 |
while (getinputrec(fp)) { |
307 |
varset("recno", '=', (double)++nrecs); |
308 |
colflg = 0; |
309 |
eclock++; |
310 |
if (!conditional || varvalue("cond") > 0.0) { |
311 |
varset("outno", '=', (double)++nout); |
312 |
putout(); |
313 |
} |
314 |
} |
315 |
fclose(fp); |
316 |
} |
317 |
|
318 |
|
319 |
static void |
320 |
putout(void) /* produce an output record */ |
321 |
{ |
322 |
|
323 |
colpos = 0; |
324 |
if (outfmt != NULL) |
325 |
putrec(); |
326 |
else if (otype == 'a') |
327 |
chanout(chanset); |
328 |
else |
329 |
chanout(bchanset); |
330 |
if (colpos && otype == 'a') |
331 |
putchar('\n'); |
332 |
if (unbuff) |
333 |
fflush(stdout); |
334 |
} |
335 |
|
336 |
|
337 |
static double |
338 |
l_in(char *funame) /* function call for $channel */ |
339 |
{ |
340 |
int n; |
341 |
register char *cp; |
342 |
/* get argument as integer */ |
343 |
n = (int)(argument(1) + .5); |
344 |
if (n != 0) /* return channel value */ |
345 |
return(chanvalue(n)); |
346 |
/* determine number of channels */ |
347 |
if (noinput || inpfmt != NULL) |
348 |
return(0); |
349 |
if (nbicols) |
350 |
return(nbicols); |
351 |
cp = inpbuf; /* need to count */ |
352 |
for (n = 0; *cp; ) |
353 |
if (blnkeq && isspace(sepchar)) { |
354 |
while (isspace(*cp)) |
355 |
cp++; |
356 |
n += *cp != '\0'; |
357 |
while (*cp && !isspace(*cp)) |
358 |
cp++; |
359 |
} else { |
360 |
n += *cp != '\n'; |
361 |
while (*cp && *cp++ != sepchar) |
362 |
; |
363 |
} |
364 |
return(n); |
365 |
} |
366 |
|
367 |
double |
368 |
chanvalue( /* return value for column n */ |
369 |
int n |
370 |
) |
371 |
{ |
372 |
int i; |
373 |
register char *cp; |
374 |
|
375 |
if (noinput || inpfmt != NULL) { |
376 |
eputs("no column input\n"); |
377 |
quit(1); |
378 |
} |
379 |
if (n < 1) { |
380 |
eputs("illegal channel number\n"); |
381 |
quit(1); |
382 |
} |
383 |
if (nbicols) { |
384 |
if (n > nbicols) |
385 |
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 |
} |
393 |
if (n <= MAXCOL && colflg & 1L<<(n-1)) |
394 |
return(colval[n-1]); |
395 |
|
396 |
cp = inpbuf; |
397 |
for (i = 1; i < n; i++) |
398 |
if (blnkeq && isspace(sepchar)) { |
399 |
while (isspace(*cp)) |
400 |
cp++; |
401 |
while (*cp && !isspace(*cp)) |
402 |
cp++; |
403 |
} else |
404 |
while (*cp && *cp++ != sepchar) |
405 |
; |
406 |
|
407 |
while (isspace(*cp)) /* some atof()'s don't like tabs */ |
408 |
cp++; |
409 |
|
410 |
if (n <= MAXCOL) { |
411 |
colflg |= 1L<<(n-1); |
412 |
return(colval[n-1] = atof(cp)); |
413 |
} else |
414 |
return(atof(cp)); |
415 |
} |
416 |
|
417 |
|
418 |
void |
419 |
chanset( /* output column n */ |
420 |
int n, |
421 |
double v |
422 |
) |
423 |
{ |
424 |
if (colpos == 0) /* no leading separator */ |
425 |
colpos = 1; |
426 |
while (colpos < n) { |
427 |
putchar(sepchar); |
428 |
colpos++; |
429 |
} |
430 |
printf("%.9g", v); |
431 |
} |
432 |
|
433 |
|
434 |
void |
435 |
bchanset( /* output binary channel n */ |
436 |
int n, |
437 |
double v |
438 |
) |
439 |
{ |
440 |
static char zerobuf[sizeof(double)]; |
441 |
float fval = v; |
442 |
|
443 |
while (++colpos < n) |
444 |
fwrite(zerobuf, |
445 |
tolower(otype)=='d' ? sizeof(double) : sizeof(float), |
446 |
1, stdout); |
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 |
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 |
|
463 |
|
464 |
static void |
465 |
readfmt( /* read record format */ |
466 |
char *spec, |
467 |
int output |
468 |
) |
469 |
{ |
470 |
int fd; |
471 |
char *inptr; |
472 |
struct field fmt; |
473 |
int res; |
474 |
register struct field *f; |
475 |
/* check for inline format */ |
476 |
for (inptr = spec; *inptr; inptr++) |
477 |
if (*inptr == '$') |
478 |
break; |
479 |
if (*inptr) /* inline */ |
480 |
inptr = spec; |
481 |
else { /* from file */ |
482 |
if ((fd = open(spec, 0)) == -1) { |
483 |
eputs(spec); |
484 |
eputs(": cannot open\n"); |
485 |
quit(1); |
486 |
} |
487 |
res = read(fd, inpbuf+2, INBSIZ-2); |
488 |
if (res <= 0 || res >= INBSIZ-1) { |
489 |
eputs(spec); |
490 |
if (res < 0) |
491 |
eputs(": read error\n"); |
492 |
else if (res == 0) |
493 |
eputs(": empty file\n"); |
494 |
else if (res >= INBSIZ-1) |
495 |
eputs(": format too long\n"); |
496 |
quit(1); |
497 |
} |
498 |
close(fd); |
499 |
(inptr=inpbuf+2)[res] = '\0'; |
500 |
} |
501 |
f = &fmt; /* get fields */ |
502 |
while ((res = readfield(&inptr)) != F_NUL) { |
503 |
f->next = (struct field *)emalloc(sizeof(struct field)); |
504 |
f = f->next; |
505 |
f->type = res; |
506 |
switch (res & F_TYP) { |
507 |
case T_LIT: |
508 |
f->f.sl = savqstr(inpbuf); |
509 |
break; |
510 |
case T_STR: |
511 |
f->f.sv = getsvar(inpbuf); |
512 |
break; |
513 |
case T_NUM: |
514 |
if (output) |
515 |
f->f.ne = eparse(inpbuf); |
516 |
else |
517 |
f->f.nv = savestr(inpbuf); |
518 |
break; |
519 |
} |
520 |
/* add final newline if necessary */ |
521 |
if (!igneol && *inptr == '\0' && inptr[-1] != '\n') |
522 |
inptr = "\n"; |
523 |
} |
524 |
f->next = NULL; |
525 |
if (output) |
526 |
outfmt = fmt.next; |
527 |
else |
528 |
inpfmt = fmt.next; |
529 |
} |
530 |
|
531 |
|
532 |
static int |
533 |
readfield( /* get next field in format */ |
534 |
register char **pp |
535 |
) |
536 |
{ |
537 |
int type = F_NUL; |
538 |
int width = 0; |
539 |
register char *cp; |
540 |
|
541 |
cp = inpbuf; |
542 |
while (cp < &inpbuf[INBSIZ-1] && **pp != '\0') { |
543 |
width++; |
544 |
switch (type) { |
545 |
case F_NUL: |
546 |
if (**pp == '$') { |
547 |
(*pp)++; |
548 |
width++; |
549 |
if (**pp == '{') { |
550 |
type = T_NUM; |
551 |
(*pp)++; |
552 |
continue; |
553 |
} else if (**pp == '(') { |
554 |
type = T_STR; |
555 |
(*pp)++; |
556 |
continue; |
557 |
} else if (**pp != '$') { |
558 |
eputs("format error\n"); |
559 |
quit(1); |
560 |
} |
561 |
width--; |
562 |
} |
563 |
type = T_LIT; |
564 |
*cp++ = *(*pp)++; |
565 |
continue; |
566 |
case T_LIT: |
567 |
if (**pp == '$') { |
568 |
width--; |
569 |
break; |
570 |
} |
571 |
*cp++ = *(*pp)++; |
572 |
continue; |
573 |
case T_NUM: |
574 |
if (**pp == '}') { |
575 |
(*pp)++; |
576 |
break; |
577 |
} |
578 |
if (!isspace(**pp)) |
579 |
*cp++ = **pp; |
580 |
(*pp)++; |
581 |
continue; |
582 |
case T_STR: |
583 |
if (**pp == ')') { |
584 |
(*pp)++; |
585 |
break; |
586 |
} |
587 |
if (!isspace(**pp)) |
588 |
*cp++ = **pp; |
589 |
(*pp)++; |
590 |
continue; |
591 |
} |
592 |
break; |
593 |
} |
594 |
*cp = '\0'; |
595 |
return(type | width); |
596 |
} |
597 |
|
598 |
|
599 |
struct strvar * |
600 |
getsvar( /* get string variable */ |
601 |
char *svname |
602 |
) |
603 |
{ |
604 |
register struct strvar *sv; |
605 |
|
606 |
for (sv = svhead; sv != NULL; sv = sv->next) |
607 |
if (!strcmp(sv->name, svname)) |
608 |
return(sv); |
609 |
sv = (struct strvar *)emalloc(sizeof(struct strvar)); |
610 |
sv->name = savqstr(svname); |
611 |
sv->val = sv->preset = NULL; |
612 |
sv->next = svhead; |
613 |
svhead = sv; |
614 |
return(sv); |
615 |
} |
616 |
|
617 |
|
618 |
static void |
619 |
svpreset( /* preset a string variable */ |
620 |
char *eqn |
621 |
) |
622 |
{ |
623 |
register struct strvar *sv; |
624 |
register char *val; |
625 |
|
626 |
for (val = eqn; *val != '='; val++) |
627 |
if (!*val) |
628 |
return; |
629 |
*val++ = '\0'; |
630 |
sv = getsvar(eqn); |
631 |
if (sv->preset != NULL) |
632 |
freqstr(sv->preset); |
633 |
if (sv->val != NULL) |
634 |
freqstr(sv->val); |
635 |
sv->val = sv->preset = savqstr(val); |
636 |
*--val = '='; |
637 |
} |
638 |
|
639 |
|
640 |
static void |
641 |
clearrec(void) /* clear input record variables */ |
642 |
{ |
643 |
register struct field *f; |
644 |
|
645 |
for (f = inpfmt; f != NULL; f = f->next) |
646 |
switch (f->type & F_TYP) { |
647 |
case T_NUM: |
648 |
dremove(f->f.nv); |
649 |
break; |
650 |
case T_STR: |
651 |
if (f->f.sv->val != f->f.sv->preset) { |
652 |
freqstr(f->f.sv->val); |
653 |
f->f.sv->val = f->f.sv->preset; |
654 |
} |
655 |
break; |
656 |
} |
657 |
} |
658 |
|
659 |
|
660 |
static int |
661 |
getrec(void) /* get next record from file */ |
662 |
{ |
663 |
int eatline; |
664 |
register struct field *f; |
665 |
|
666 |
while (ipb.chr != EOF) { |
667 |
if (blnkeq) { /* beware of nbsynch() */ |
668 |
while (isblnk(ipb.chr)) |
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(); /* got one! */ |
680 |
return(1); |
681 |
} |
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 |
resetinp(); |
688 |
} |
689 |
resetinp(); |
690 |
} |
691 |
} |
692 |
return(0); |
693 |
} |
694 |
|
695 |
|
696 |
static int |
697 |
getfield( /* get next field */ |
698 |
register struct field *f |
699 |
) |
700 |
{ |
701 |
static char buf[RMAXWORD+1]; /* no recursion! */ |
702 |
int delim, inword; |
703 |
double d; |
704 |
char *np; |
705 |
register char *cp; |
706 |
|
707 |
switch (f->type & F_TYP) { |
708 |
case T_LIT: |
709 |
cp = f->f.sl; |
710 |
do { |
711 |
if (blnkeq && isblnk(*cp)) { |
712 |
if (!isblnk(ipb.chr)) |
713 |
return(-1); |
714 |
do |
715 |
cp++; |
716 |
while (isblnk(*cp)); |
717 |
do |
718 |
scaninp(); |
719 |
while (isblnk(ipb.chr)); |
720 |
} else if (*cp == ipb.chr) { |
721 |
cp++; |
722 |
scaninp(); |
723 |
} else |
724 |
return(-1); |
725 |
} while (*cp); |
726 |
return(0); |
727 |
case T_STR: |
728 |
if (f->next == NULL || (f->next->type & F_TYP) != T_LIT) |
729 |
delim = EOF; |
730 |
else |
731 |
delim = f->next->f.sl[0]; |
732 |
cp = buf; |
733 |
do { |
734 |
if (ipb.chr == EOF || ipb.chr == '\n') |
735 |
inword = 0; |
736 |
else if (blnkeq && delim != EOF) |
737 |
inword = isblnk(delim) ? |
738 |
!isblnk(ipb.chr) |
739 |
: ipb.chr != delim; |
740 |
else |
741 |
inword = cp-buf < (f->type & F_WID); |
742 |
if (inword) { |
743 |
*cp++ = ipb.chr; |
744 |
scaninp(); |
745 |
} |
746 |
} while (inword && cp < &buf[RMAXWORD]); |
747 |
*cp = '\0'; |
748 |
if (f->f.sv->val == NULL) |
749 |
f->f.sv->val = savqstr(buf); /* first setting */ |
750 |
else if (strcmp(f->f.sv->val, buf)) |
751 |
return(-1); /* doesn't match! */ |
752 |
return(0); |
753 |
case T_NUM: |
754 |
if (f->next == NULL || (f->next->type & F_TYP) != T_LIT) |
755 |
delim = EOF; |
756 |
else |
757 |
delim = f->next->f.sl[0]; |
758 |
np = NULL; |
759 |
cp = buf; |
760 |
do { |
761 |
if (!((np==NULL&&isblnk(ipb.chr)) || isnum(ipb.chr))) |
762 |
inword = 0; |
763 |
else if (blnkeq && delim != EOF) |
764 |
inword = isblnk(delim) ? |
765 |
!isblnk(ipb.chr) |
766 |
: ipb.chr != delim; |
767 |
else |
768 |
inword = cp-buf < (f->type & F_WID); |
769 |
if (inword) { |
770 |
if (np==NULL && !isblnk(ipb.chr)) |
771 |
np = cp; |
772 |
*cp++ = ipb.chr; |
773 |
scaninp(); |
774 |
} |
775 |
} while (inword && cp < &buf[RMAXWORD]); |
776 |
*cp = '\0'; |
777 |
d = np==NULL ? 0. : atof(np); |
778 |
if (!vardefined(f->f.nv)) |
779 |
varset(f->f.nv, '=', d); /* first setting */ |
780 |
else if ((d = (varvalue(f->f.nv)-d)/(d==0.?1.:d)) > .001 |
781 |
|| d < -.001) |
782 |
return(-1); /* doesn't match! */ |
783 |
return(0); |
784 |
} |
785 |
return -1; /* pro forma return */ |
786 |
} |
787 |
|
788 |
|
789 |
static void |
790 |
putrec(void) /* output a record */ |
791 |
{ |
792 |
char fmt[32]; |
793 |
register int n; |
794 |
register struct field *f; |
795 |
int adlast, adnext; |
796 |
|
797 |
adlast = 0; |
798 |
for (f = outfmt; f != NULL; f = f->next) { |
799 |
adnext = blnkeq && |
800 |
f->next != NULL && |
801 |
!( (f->next->type&F_TYP) == T_LIT && |
802 |
f->next->f.sl[0] == ' ' ); |
803 |
switch (f->type & F_TYP) { |
804 |
case T_LIT: |
805 |
fputs(f->f.sl, stdout); |
806 |
adlast = f->f.sl[(f->type&F_WID)-1] != ' '; |
807 |
break; |
808 |
case T_STR: |
809 |
if (f->f.sv->val == NULL) { |
810 |
eputs(f->f.sv->name); |
811 |
eputs(": undefined string\n"); |
812 |
quit(1); |
813 |
} |
814 |
n = (int)(f->type & F_WID) - strlen(f->f.sv->val); |
815 |
if (adlast) |
816 |
fputs(f->f.sv->val, stdout); |
817 |
if (!(adlast && adnext)) |
818 |
while (n-- > 0) |
819 |
putchar(' '); |
820 |
if (!adlast) |
821 |
fputs(f->f.sv->val, stdout); |
822 |
adlast = 1; |
823 |
break; |
824 |
case T_NUM: |
825 |
n = f->type & F_WID; |
826 |
if (adlast && adnext) |
827 |
strcpy(fmt, "%g"); |
828 |
else if (adlast) |
829 |
sprintf(fmt, "%%-%dg", n); |
830 |
else |
831 |
sprintf(fmt, "%%%dg", n); |
832 |
printf(fmt, evalue(f->f.ne)); |
833 |
adlast = 1; |
834 |
break; |
835 |
} |
836 |
} |
837 |
} |
838 |
|
839 |
|
840 |
static void |
841 |
initinp(FILE *fp) /* prepare lookahead buffer */ |
842 |
|
843 |
{ |
844 |
ipb.fin = fp; |
845 |
ipb.beg = ipb.end = inpbuf; |
846 |
ipb.pos = inpbuf-1; /* position before beginning */ |
847 |
ipb.chr = '\0'; |
848 |
scaninp(); |
849 |
} |
850 |
|
851 |
|
852 |
static void |
853 |
scaninp(void) /* scan next character */ |
854 |
{ |
855 |
if (ipb.chr == EOF) |
856 |
return; |
857 |
if (++ipb.pos >= &inpbuf[INBSIZ]) |
858 |
ipb.pos = inpbuf; |
859 |
if (ipb.pos == ipb.end) { /* new character */ |
860 |
if ((ipb.chr = getc(ipb.fin)) != EOF) { |
861 |
*ipb.end = ipb.chr; |
862 |
if (++ipb.end >= &inpbuf[INBSIZ]) |
863 |
ipb.end = inpbuf; |
864 |
if (ipb.end == ipb.beg) |
865 |
ipb.beg = NULL; |
866 |
} |
867 |
} else |
868 |
ipb.chr = *ipb.pos; |
869 |
} |
870 |
|
871 |
|
872 |
static void |
873 |
advinp(void) /* move home to current position */ |
874 |
{ |
875 |
ipb.beg = ipb.pos; |
876 |
} |
877 |
|
878 |
|
879 |
static void |
880 |
resetinp(void) /* rewind position and advance 1 */ |
881 |
{ |
882 |
if (ipb.beg == NULL) /* full */ |
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(); |
891 |
} |
892 |
|
893 |
|
894 |
void |
895 |
eputs(char *msg) |
896 |
{ |
897 |
fputs(msg, stderr); |
898 |
} |
899 |
|
900 |
|
901 |
void |
902 |
wputs(char *msg) |
903 |
{ |
904 |
if (!nowarn) |
905 |
eputs(msg); |
906 |
} |
907 |
|
908 |
|
909 |
void |
910 |
quit(int code) |
911 |
{ |
912 |
exit(code); |
913 |
} |