Annotation of src/usr.bin/awk/lib.c, Revision 1.43
1.43 ! millert 1: /* $OpenBSD: lib.c,v 1.42 2020/12/09 20:00:11 millert Exp $ */
1.1 tholo 2: /****************************************************************
1.4 kstailey 3: Copyright (C) Lucent Technologies 1997
1.1 tholo 4: All Rights Reserved
5:
6: Permission to use, copy, modify, and distribute this software and
7: its documentation for any purpose and without fee is hereby
8: granted, provided that the above copyright notice appear in all
9: copies and that both that the copyright notice and this
10: permission notice and warranty disclaimer appear in supporting
1.4 kstailey 11: documentation, and that the name Lucent Technologies or any of
12: its entities not be used in advertising or publicity pertaining
13: to distribution of the software without specific, written prior
14: permission.
15:
16: LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17: INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
18: IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
19: SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20: WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
21: IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
22: ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
23: THIS SOFTWARE.
1.1 tholo 24: ****************************************************************/
25:
26: #define DEBUG
27: #include <stdio.h>
28: #include <string.h>
29: #include <ctype.h>
30: #include <errno.h>
31: #include <stdlib.h>
1.7 millert 32: #include <stdarg.h>
1.33 millert 33: #include <limits.h>
1.1 tholo 34: #include "awk.h"
35:
1.33 millert 36: char EMPTY[] = { '\0' };
1.1 tholo 37: FILE *infile = NULL;
1.34 millert 38: bool innew; /* true = infile has not been read by readrec */
1.33 millert 39: char *file = EMPTY;
1.4 kstailey 40: char *record;
1.1 tholo 41: int recsize = RECSIZE;
42: char *fields;
1.4 kstailey 43: int fieldssize = RECSIZE;
44:
45: Cell **fldtab; /* pointers to Cells */
1.33 millert 46: static size_t len_inputFS = 0;
47: static char *inputFS = NULL; /* FS at time of input, for field splitting */
1.1 tholo 48:
1.18 millert 49: #define MAXFLD 2
1.4 kstailey 50: int nfields = MAXFLD; /* last allocated slot for $i */
1.1 tholo 51:
1.32 millert 52: bool donefld; /* true = implies rec broken into fields */
53: bool donerec; /* true = record is valid (no flds have changed) */
1.1 tholo 54:
1.4 kstailey 55: int lastfld = 0; /* last used field */
1.1 tholo 56: int argno = 1; /* current input argument number */
57: extern Awkfloat *ARGC;
58:
1.34 millert 59: static Cell dollar0 = { OCELL, CFLD, NULL, EMPTY, 0.0, REC|STR|DONTFREE, NULL, NULL };
60: static Cell dollar1 = { OCELL, CFLD, NULL, EMPTY, 0.0, FLD|STR|DONTFREE, NULL, NULL };
1.4 kstailey 61:
1.1 tholo 62: void recinit(unsigned int n)
63: {
1.42 millert 64: if ( (record = (char *) malloc(n)) == NULL
65: || (fields = (char *) malloc(n+1)) == NULL
66: || (fldtab = (Cell **) calloc(nfields+2, sizeof(*fldtab))) == NULL
67: || (fldtab[0] = (Cell *) malloc(sizeof(**fldtab))) == NULL)
1.7 millert 68: FATAL("out of space for $0 and fields");
1.22 millert 69: *record = '\0';
1.4 kstailey 70: *fldtab[0] = dollar0;
71: fldtab[0]->sval = record;
72: fldtab[0]->nval = tostring("0");
73: makefields(1, nfields);
74: }
75:
76: void makefields(int n1, int n2) /* create $n1..$n2 inclusive */
77: {
78: char temp[50];
1.1 tholo 79: int i;
80:
1.4 kstailey 81: for (i = n1; i <= n2; i++) {
1.42 millert 82: fldtab[i] = (Cell *) malloc(sizeof(**fldtab));
1.4 kstailey 83: if (fldtab[i] == NULL)
1.7 millert 84: FATAL("out of space in makefields %d", i);
1.4 kstailey 85: *fldtab[i] = dollar1;
1.31 millert 86: snprintf(temp, sizeof(temp), "%d", i);
1.4 kstailey 87: fldtab[i]->nval = tostring(temp);
88: }
1.1 tholo 89: }
90:
91: void initgetrec(void)
92: {
93: int i;
94: char *p;
95:
96: for (i = 1; i < *ARGC; i++) {
1.20 millert 97: p = getargv(i); /* find 1st real filename */
98: if (p == NULL || *p == '\0') { /* deleted or zapped */
99: argno++;
100: continue;
101: }
102: if (!isclvar(p)) {
103: setsval(lookup("FILENAME", symtab), p);
1.1 tholo 104: return;
105: }
106: setclvar(p); /* a commandline assignment before filename */
107: argno++;
108: }
109: infile = stdin; /* no filenames, so use stdin */
1.34 millert 110: innew = true;
1.1 tholo 111: }
112:
1.29 millert 113: /*
114: * POSIX specifies that fields are supposed to be evaluated as if they were
115: * split using the value of FS at the time that the record's value ($0) was
116: * read.
117: *
118: * Since field-splitting is done lazily, we save the current value of FS
119: * whenever a new record is read in (implicitly or via getline), or when
120: * a new value is assigned to $0.
121: */
122: void savefs(void)
123: {
1.37 millert 124: size_t len = strlen(getsval(fsloc));
125: if (len >= len_inputFS) {
126: len_inputFS = len + 1;
1.42 millert 127: inputFS = (char *) realloc(inputFS, len_inputFS);
1.37 millert 128: if (inputFS == NULL)
129: FATAL("field separator %.10s... is too long", *FS);
1.33 millert 130: }
1.37 millert 131: if (strlcpy(inputFS, *FS, len_inputFS) >= len_inputFS)
1.29 millert 132: FATAL("field separator %.10s... is too long", *FS);
133: }
134:
1.32 millert 135: static bool firsttime = true;
1.15 millert 136:
1.32 millert 137: int getrec(char **pbuf, int *pbufsize, bool isrecord) /* get next input record */
1.4 kstailey 138: { /* note: cares whether buf == record */
1.1 tholo 139: int c;
1.4 kstailey 140: char *buf = *pbuf;
1.18 millert 141: uschar saveb0;
142: int bufsize = *pbufsize, savebufsize = bufsize;
1.1 tholo 143:
144: if (firsttime) {
1.32 millert 145: firsttime = false;
1.1 tholo 146: initgetrec();
147: }
1.39 millert 148: DPRINTF("RS=<%s>, FS=<%s>, ARGC=%g, FILENAME=%s\n",
149: *RS, *FS, *ARGC, *FILENAME);
1.5 millert 150: if (isrecord) {
1.32 millert 151: donefld = false;
152: donerec = true;
1.29 millert 153: savefs();
1.5 millert 154: }
1.18 millert 155: saveb0 = buf[0];
1.1 tholo 156: buf[0] = 0;
157: while (argno < *ARGC || infile == stdin) {
1.39 millert 158: DPRINTF("argno=%d, file=|%s|\n", argno, file);
1.1 tholo 159: if (infile == NULL) { /* have to open a new file */
160: file = getargv(argno);
1.20 millert 161: if (file == NULL || *file == '\0') { /* deleted or zapped */
1.1 tholo 162: argno++;
163: continue;
164: }
165: if (isclvar(file)) { /* a var=value arg */
166: setclvar(file);
167: argno++;
168: continue;
169: }
170: *FILENAME = file;
1.39 millert 171: DPRINTF("opening file %s\n", file);
1.1 tholo 172: if (*file == '-' && *(file+1) == '\0')
173: infile = stdin;
1.4 kstailey 174: else if ((infile = fopen(file, "r")) == NULL)
1.7 millert 175: FATAL("can't open file %s", file);
1.1 tholo 176: setfval(fnrloc, 0.0);
177: }
1.34 millert 178: c = readrec(&buf, &bufsize, infile, innew);
179: if (innew)
180: innew = false;
1.1 tholo 181: if (c != 0 || buf[0] != '\0') { /* normal record */
1.4 kstailey 182: if (isrecord) {
1.42 millert 183: double result;
184:
1.4 kstailey 185: if (freeable(fldtab[0]))
186: xfree(fldtab[0]->sval);
187: fldtab[0]->sval = buf; /* buf == record */
188: fldtab[0]->tval = REC | STR | DONTFREE;
1.42 millert 189: if (is_number(fldtab[0]->sval, & result)) {
190: fldtab[0]->fval = result;
1.4 kstailey 191: fldtab[0]->tval |= NUM;
1.1 tholo 192: }
193: }
194: setfval(nrloc, nrloc->fval+1);
195: setfval(fnrloc, fnrloc->fval+1);
1.4 kstailey 196: *pbuf = buf;
197: *pbufsize = bufsize;
1.1 tholo 198: return 1;
199: }
200: /* EOF arrived on this file; set up next */
201: if (infile != stdin)
202: fclose(infile);
203: infile = NULL;
204: argno++;
205: }
1.18 millert 206: buf[0] = saveb0;
1.4 kstailey 207: *pbuf = buf;
1.18 millert 208: *pbufsize = savebufsize;
1.1 tholo 209: return 0; /* true end of file */
210: }
211:
212: void nextfile(void)
213: {
1.18 millert 214: if (infile != NULL && infile != stdin)
1.1 tholo 215: fclose(infile);
216: infile = NULL;
217: argno++;
218: }
219:
1.34 millert 220: int readrec(char **pbuf, int *pbufsize, FILE *inf, bool newflag) /* read one record into buf */
1.1 tholo 221: {
1.30 millert 222: int sep, c, isrec;
1.4 kstailey 223: char *rr, *buf = *pbuf;
224: int bufsize = *pbufsize;
1.27 millert 225: char *rs = getsval(rsloc);
1.1 tholo 226:
1.30 millert 227: if (*rs && rs[1]) {
1.32 millert 228: bool found;
1.30 millert 229:
230: fa *pfa = makedfa(rs, 1);
1.34 millert 231: if (newflag)
232: found = fnematch(pfa, inf, &buf, &bufsize, recsize);
233: else {
234: int tempstat = pfa->initstat;
235: pfa->initstat = 2;
236: found = fnematch(pfa, inf, &buf, &bufsize, recsize);
237: pfa->initstat = tempstat;
238: }
1.30 millert 239: if (found)
1.31 millert 240: setptr(patbeg, '\0');
1.30 millert 241: } else {
242: if ((sep = *rs) == 0) {
243: sep = '\n';
244: while ((c=getc(inf)) == '\n' && c != EOF) /* skip leading \n's */
245: ;
246: if (c != EOF)
247: ungetc(c, inf);
248: }
249: for (rr = buf; ; ) {
250: for (; (c=getc(inf)) != sep && c != EOF; ) {
251: if (rr-buf+1 > bufsize)
252: if (!adjbuf(&buf, &bufsize, 1+rr-buf,
253: recsize, &rr, "readrec 1"))
254: FATAL("input record `%.30s...' too long", buf);
255: *rr++ = c;
256: }
257: if (*rs == sep || c == EOF)
258: break;
259: if ((c = getc(inf)) == '\n' || c == EOF) /* 2 in a row */
260: break;
261: if (!adjbuf(&buf, &bufsize, 2+rr-buf, recsize, &rr,
262: "readrec 2"))
263: FATAL("input record `%.30s...' too long", buf);
264: *rr++ = '\n';
1.4 kstailey 265: *rr++ = c;
266: }
1.30 millert 267: if (!adjbuf(&buf, &bufsize, 1+rr-buf, recsize, &rr, "readrec 3"))
1.7 millert 268: FATAL("input record `%.30s...' too long", buf);
1.30 millert 269: *rr = 0;
1.1 tholo 270: }
1.4 kstailey 271: *pbuf = buf;
272: *pbufsize = bufsize;
1.30 millert 273: isrec = *buf || !feof(inf);
1.39 millert 274: DPRINTF("readrec saw <%s>, returns %d\n", buf, isrec);
1.30 millert 275: return isrec;
1.1 tholo 276: }
277:
278: char *getargv(int n) /* get ARGV[n] */
279: {
280: Cell *x;
1.4 kstailey 281: char *s, temp[50];
1.1 tholo 282: extern Array *ARGVtab;
283:
1.31 millert 284: snprintf(temp, sizeof(temp), "%d", n);
1.20 millert 285: if (lookup(temp, ARGVtab) == NULL)
286: return NULL;
1.1 tholo 287: x = setsymtab(temp, "", 0.0, STR, ARGVtab);
288: s = getsval(x);
1.39 millert 289: DPRINTF("getargv(%d) returns |%s|\n", n, s);
1.1 tholo 290: return s;
291: }
292:
293: void setclvar(char *s) /* set var=value from s */
294: {
295: char *p;
296: Cell *q;
1.42 millert 297: double result;
1.1 tholo 298:
299: for (p=s; *p != '='; p++)
300: ;
301: *p++ = 0;
302: p = qstring(p, '\0');
303: q = setsymtab(s, p, 0.0, STR, symtab);
304: setsval(q, p);
1.42 millert 305: if (is_number(q->sval, & result)) {
306: q->fval = result;
1.1 tholo 307: q->tval |= NUM;
308: }
1.39 millert 309: DPRINTF("command line set %s to |%s|\n", s, p);
1.1 tholo 310: }
311:
312:
313: void fldbld(void) /* create fields from current record */
314: {
1.4 kstailey 315: /* this relies on having fields[] the same length as $0 */
316: /* the fields are all stored in this one array with \0's */
1.20 millert 317: /* possibly with a final trailing \0 not associated with any field */
1.1 tholo 318: char *r, *fr, sep;
319: Cell *p;
1.4 kstailey 320: int i, j, n;
1.1 tholo 321:
322: if (donefld)
323: return;
1.4 kstailey 324: if (!isstr(fldtab[0]))
325: getsval(fldtab[0]);
326: r = fldtab[0]->sval;
327: n = strlen(r);
328: if (n > fieldssize) {
329: xfree(fields);
1.42 millert 330: if ((fields = (char *) malloc(n+2)) == NULL) /* possibly 2 final \0s */
1.7 millert 331: FATAL("out of space for fields in fldbld %d", n);
1.4 kstailey 332: fieldssize = n;
333: }
1.1 tholo 334: fr = fields;
335: i = 0; /* number of fields accumulated here */
1.35 millert 336: if (inputFS == NULL) /* make sure we have a copy of FS */
337: savefs();
1.2 millert 338: if (strlen(inputFS) > 1) { /* it's a regular expression */
339: i = refldbld(r, inputFS);
340: } else if ((sep = *inputFS) == ' ') { /* default whitespace */
1.1 tholo 341: for (i = 0; ; ) {
342: while (*r == ' ' || *r == '\t' || *r == '\n')
343: r++;
344: if (*r == 0)
345: break;
346: i++;
1.4 kstailey 347: if (i > nfields)
348: growfldtab(i);
349: if (freeable(fldtab[i]))
350: xfree(fldtab[i]->sval);
351: fldtab[i]->sval = fr;
352: fldtab[i]->tval = FLD | STR | DONTFREE;
1.1 tholo 353: do
354: *fr++ = *r++;
355: while (*r != ' ' && *r != '\t' && *r != '\n' && *r != '\0');
356: *fr++ = 0;
357: }
358: *fr = 0;
1.2 millert 359: } else if ((sep = *inputFS) == 0) { /* new: FS="" => 1 char/field */
1.32 millert 360: for (i = 0; *r != '\0'; r += n) {
1.33 millert 361: char buf[MB_LEN_MAX + 1];
1.32 millert 362:
1.1 tholo 363: i++;
1.4 kstailey 364: if (i > nfields)
365: growfldtab(i);
366: if (freeable(fldtab[i]))
367: xfree(fldtab[i]->sval);
1.33 millert 368: n = mblen(r, MB_LEN_MAX);
1.32 millert 369: if (n < 0)
370: n = 1;
371: memcpy(buf, r, n);
372: buf[n] = '\0';
1.4 kstailey 373: fldtab[i]->sval = tostring(buf);
374: fldtab[i]->tval = FLD | STR;
1.1 tholo 375: }
376: *fr = 0;
377: } else if (*r != 0) { /* if 0, it's a null field */
1.15 millert 378: /* subtlecase : if length(FS) == 1 && length(RS > 0)
379: * \n is NOT a field separator (cf awk book 61,84).
380: * this variable is tested in the inner while loop.
381: */
382: int rtest = '\n'; /* normal case */
383: if (strlen(*RS) > 0)
384: rtest = '\0';
1.1 tholo 385: for (;;) {
386: i++;
1.4 kstailey 387: if (i > nfields)
388: growfldtab(i);
389: if (freeable(fldtab[i]))
390: xfree(fldtab[i]->sval);
391: fldtab[i]->sval = fr;
392: fldtab[i]->tval = FLD | STR | DONTFREE;
1.15 millert 393: while (*r != sep && *r != rtest && *r != '\0') /* \n is always a separator */
1.1 tholo 394: *fr++ = *r++;
395: *fr++ = 0;
396: if (*r++ == 0)
397: break;
398: }
399: *fr = 0;
400: }
1.4 kstailey 401: if (i > nfields)
1.7 millert 402: FATAL("record `%.30s...' has too many fields; can't happen", r);
1.4 kstailey 403: cleanfld(i+1, lastfld); /* clean out junk from previous record */
404: lastfld = i;
1.32 millert 405: donefld = true;
1.4 kstailey 406: for (j = 1; j <= lastfld; j++) {
1.42 millert 407: double result;
408:
1.4 kstailey 409: p = fldtab[j];
1.42 millert 410: if(is_number(p->sval, & result)) {
411: p->fval = result;
1.1 tholo 412: p->tval |= NUM;
413: }
414: }
1.4 kstailey 415: setfval(nfloc, (Awkfloat) lastfld);
1.32 millert 416: donerec = true; /* restore */
1.4 kstailey 417: if (dbg) {
418: for (j = 0; j <= lastfld; j++) {
419: p = fldtab[j];
420: printf("field %d (%s): |%s|\n", j, p->nval, p->sval);
421: }
422: }
1.1 tholo 423: }
424:
1.4 kstailey 425: void cleanfld(int n1, int n2) /* clean out fields n1 .. n2 inclusive */
426: { /* nvals remain intact */
427: Cell *p;
428: int i;
1.1 tholo 429:
1.4 kstailey 430: for (i = n1; i <= n2; i++) {
431: p = fldtab[i];
432: if (freeable(p))
1.1 tholo 433: xfree(p->sval);
1.33 millert 434: p->sval = EMPTY,
1.1 tholo 435: p->tval = FLD | STR | DONTFREE;
436: }
437: }
438:
1.4 kstailey 439: void newfld(int n) /* add field n after end of existing lastfld */
1.1 tholo 440: {
1.4 kstailey 441: if (n > nfields)
442: growfldtab(n);
443: cleanfld(lastfld+1, n);
444: lastfld = n;
1.1 tholo 445: setfval(nfloc, (Awkfloat) n);
1.26 millert 446: }
447:
448: void setlastfld(int n) /* set lastfld cleaning fldtab cells if necessary */
449: {
1.27 millert 450: if (n < 0)
451: FATAL("cannot set NF to a negative value");
1.26 millert 452: if (n > nfields)
453: growfldtab(n);
454:
455: if (lastfld < n)
456: cleanfld(lastfld+1, n);
457: else
458: cleanfld(n+1, lastfld);
459:
460: lastfld = n;
1.1 tholo 461: }
462:
1.4 kstailey 463: Cell *fieldadr(int n) /* get nth field */
464: {
465: if (n < 0)
1.15 millert 466: FATAL("trying to access out of range field %d", n);
1.4 kstailey 467: if (n > nfields) /* fields after NF are empty */
468: growfldtab(n); /* but does not increase NF */
469: return(fldtab[n]);
470: }
471:
472: void growfldtab(int n) /* make new fields up to at least $n */
473: {
474: int nf = 2 * nfields;
1.15 millert 475: size_t s;
1.4 kstailey 476:
477: if (n > nf)
478: nf = n;
1.15 millert 479: s = (nf+1) * (sizeof (struct Cell *)); /* freebsd: how much do we need? */
1.34 millert 480: if (s / sizeof(struct Cell *) - 1 == (size_t)nf) /* didn't overflow */
1.42 millert 481: fldtab = (Cell **) realloc(fldtab, s);
1.15 millert 482: else /* overflow sizeof int */
483: xfree(fldtab); /* make it null */
1.4 kstailey 484: if (fldtab == NULL)
1.7 millert 485: FATAL("out of space creating %d fields", nf);
1.4 kstailey 486: makefields(nfields+1, nf);
487: nfields = nf;
488: }
489:
1.11 millert 490: int refldbld(const char *rec, const char *fs) /* build fields from reg expr in FS */
1.1 tholo 491: {
1.4 kstailey 492: /* this relies on having fields[] the same length as $0 */
493: /* the fields are all stored in this one array with \0's */
1.1 tholo 494: char *fr;
1.4 kstailey 495: int i, tempstat, n;
1.1 tholo 496: fa *pfa;
497:
1.4 kstailey 498: n = strlen(rec);
499: if (n > fieldssize) {
500: xfree(fields);
1.42 millert 501: if ((fields = (char *) malloc(n+1)) == NULL)
1.7 millert 502: FATAL("out of space for fields in refldbld %d", n);
1.4 kstailey 503: fieldssize = n;
504: }
1.1 tholo 505: fr = fields;
506: *fr = '\0';
507: if (*rec == '\0')
508: return 0;
509: pfa = makedfa(fs, 1);
1.39 millert 510: DPRINTF("into refldbld, rec = <%s>, pat = <%s>\n", rec, fs);
1.1 tholo 511: tempstat = pfa->initstat;
1.4 kstailey 512: for (i = 1; ; i++) {
1.37 millert 513: const size_t fss_rem = fields + fieldssize + 1 - fr;
1.4 kstailey 514: if (i > nfields)
515: growfldtab(i);
516: if (freeable(fldtab[i]))
517: xfree(fldtab[i]->sval);
518: fldtab[i]->tval = FLD | STR | DONTFREE;
519: fldtab[i]->sval = fr;
1.39 millert 520: DPRINTF("refldbld: i=%d\n", i);
1.1 tholo 521: if (nematch(pfa, rec)) {
1.37 millert 522: const size_t reclen = patbeg - rec;
1.4 kstailey 523: pfa->initstat = 2; /* horrible coupling to b.c */
1.39 millert 524: DPRINTF("match %s (%d chars)\n", patbeg, patlen);
1.37 millert 525: if (reclen >= fss_rem)
526: FATAL("out of space for fields in refldbld");
527: memcpy(fr, rec, reclen);
528: fr += reclen;
529: *fr++ = '\0';
1.1 tholo 530: rec = patbeg + patlen;
531: } else {
1.39 millert 532: DPRINTF("no match %s\n", rec);
1.37 millert 533: if (strlcpy(fr, rec, fss_rem) >= fss_rem)
534: FATAL("out of space for fields in refldbld");
1.1 tholo 535: pfa->initstat = tempstat;
536: break;
537: }
538: }
1.29 millert 539: return i;
1.1 tholo 540: }
541:
542: void recbld(void) /* create $0 from $1..$NF if necessary */
543: {
544: int i;
545: char *r, *p;
1.27 millert 546: char *sep = getsval(ofsloc);
1.1 tholo 547:
1.32 millert 548: if (donerec)
1.1 tholo 549: return;
1.5 millert 550: r = record;
1.1 tholo 551: for (i = 1; i <= *NF; i++) {
1.4 kstailey 552: p = getsval(fldtab[i]);
1.5 millert 553: if (!adjbuf(&record, &recsize, 1+strlen(p)+r-record, recsize, &r, "recbld 1"))
1.7 millert 554: FATAL("created $0 `%.30s...' too long", record);
1.4 kstailey 555: while ((*r = *p++) != 0)
1.1 tholo 556: r++;
1.4 kstailey 557: if (i < *NF) {
1.27 millert 558: if (!adjbuf(&record, &recsize, 2+strlen(sep)+r-record, recsize, &r, "recbld 2"))
1.7 millert 559: FATAL("created $0 `%.30s...' too long", record);
1.27 millert 560: for (p = sep; (*r = *p++) != 0; )
1.1 tholo 561: r++;
1.4 kstailey 562: }
1.1 tholo 563: }
1.5 millert 564: if (!adjbuf(&record, &recsize, 2+r-record, recsize, &r, "recbld 3"))
1.7 millert 565: FATAL("built giant record `%.30s...'", record);
1.1 tholo 566: *r = '\0';
1.39 millert 567: DPRINTF("in recbld inputFS=%s, fldtab[0]=%p\n", inputFS, (void*)fldtab[0]);
1.4 kstailey 568:
569: if (freeable(fldtab[0]))
570: xfree(fldtab[0]->sval);
571: fldtab[0]->tval = REC | STR | DONTFREE;
572: fldtab[0]->sval = record;
573:
1.39 millert 574: DPRINTF("in recbld inputFS=%s, fldtab[0]=%p\n", inputFS, (void*)fldtab[0]);
575: DPRINTF("recbld = |%s|\n", record);
1.32 millert 576: donerec = true;
1.1 tholo 577: }
578:
579: int errorflag = 0;
580:
1.11 millert 581: void yyerror(const char *s)
1.1 tholo 582: {
1.14 grange 583: SYNTAX("%s", s);
1.7 millert 584: }
585:
1.11 millert 586: void SYNTAX(const char *fmt, ...)
1.7 millert 587: {
1.1 tholo 588: extern char *cmdname, *curfname;
589: static int been_here = 0;
1.7 millert 590: va_list varg;
1.1 tholo 591:
592: if (been_here++ > 2)
593: return;
1.7 millert 594: fprintf(stderr, "%s: ", cmdname);
595: va_start(varg, fmt);
596: vfprintf(stderr, fmt, varg);
597: va_end(varg);
1.1 tholo 598: fprintf(stderr, " at source line %d", lineno);
599: if (curfname != NULL)
600: fprintf(stderr, " in function %s", curfname);
1.32 millert 601: if (compile_time == COMPILING && cursource() != NULL)
1.6 millert 602: fprintf(stderr, " source file %s", cursource());
1.1 tholo 603: fprintf(stderr, "\n");
604: errorflag = 2;
605: eprint();
606: }
607:
608: extern int bracecnt, brackcnt, parencnt;
609:
610: void bracecheck(void)
611: {
612: int c;
613: static int beenhere = 0;
614:
615: if (beenhere++)
616: return;
1.3 millert 617: while ((c = input()) != EOF && c != '\0')
1.1 tholo 618: bclass(c);
619: bcheck2(bracecnt, '{', '}');
620: bcheck2(brackcnt, '[', ']');
621: bcheck2(parencnt, '(', ')');
622: }
623:
624: void bcheck2(int n, int c1, int c2)
625: {
626: if (n == 1)
627: fprintf(stderr, "\tmissing %c\n", c2);
628: else if (n > 1)
629: fprintf(stderr, "\t%d missing %c's\n", n, c2);
630: else if (n == -1)
631: fprintf(stderr, "\textra %c\n", c2);
632: else if (n < -1)
633: fprintf(stderr, "\t%d extra %c's\n", -n, c2);
634: }
635:
1.35 millert 636: void FATAL(const char *fmt, ...)
1.7 millert 637: {
638: extern char *cmdname;
639: va_list varg;
640:
641: fflush(stdout);
642: fprintf(stderr, "%s: ", cmdname);
643: va_start(varg, fmt);
644: vfprintf(stderr, fmt, varg);
645: va_end(varg);
646: error();
647: if (dbg > 1) /* core dump if serious debugging on */
648: abort();
649: exit(2);
650: }
651:
1.11 millert 652: void WARNING(const char *fmt, ...)
1.1 tholo 653: {
654: extern char *cmdname;
1.7 millert 655: va_list varg;
1.1 tholo 656:
657: fflush(stdout);
658: fprintf(stderr, "%s: ", cmdname);
1.7 millert 659: va_start(varg, fmt);
660: vfprintf(stderr, fmt, varg);
661: va_end(varg);
662: error();
663: }
664:
665: void error()
666: {
667: extern Node *curnode;
668:
1.1 tholo 669: fprintf(stderr, "\n");
1.32 millert 670: if (compile_time != ERROR_PRINTING) {
671: if (NR && *NR > 0) {
672: fprintf(stderr, " input record number %d", (int) (*FNR));
673: if (strcmp(*FILENAME, "-") != 0)
674: fprintf(stderr, ", file %s", *FILENAME);
675: fprintf(stderr, "\n");
676: }
677: if (curnode)
678: fprintf(stderr, " source line number %d", curnode->lineno);
679: else if (lineno)
680: fprintf(stderr, " source line number %d", lineno);
1.41 millert 681: if (compile_time == COMPILING && cursource() != NULL)
682: fprintf(stderr, " source file %s", cursource());
683: fprintf(stderr, "\n");
684: eprint();
1.32 millert 685: }
1.1 tholo 686: }
687:
688: void eprint(void) /* try to print context around error */
689: {
690: char *p, *q;
691: int c;
692: static int been_here = 0;
693: extern char ebuf[], *ep;
694:
1.32 millert 695: if (compile_time != COMPILING || been_here++ > 0 || ebuf == ep)
1.1 tholo 696: return;
697: p = ep - 1;
698: if (p > ebuf && *p == '\n')
699: p--;
700: for ( ; p > ebuf && *p != '\n' && *p != '\0'; p--)
701: ;
702: while (*p == '\n')
703: p++;
704: fprintf(stderr, " context is\n\t");
705: for (q=ep-1; q>=p && *q!=' ' && *q!='\t' && *q!='\n'; q--)
706: ;
707: for ( ; p < q; p++)
708: if (*p)
709: putc(*p, stderr);
710: fprintf(stderr, " >>> ");
711: for ( ; p < ep; p++)
712: if (*p)
713: putc(*p, stderr);
714: fprintf(stderr, " <<< ");
715: if (*ep)
716: while ((c = input()) != '\n' && c != '\0' && c != EOF) {
717: putc(c, stderr);
718: bclass(c);
719: }
720: putc('\n', stderr);
721: ep = ebuf;
722: }
723:
724: void bclass(int c)
725: {
726: switch (c) {
727: case '{': bracecnt++; break;
728: case '}': bracecnt--; break;
729: case '[': brackcnt++; break;
730: case ']': brackcnt--; break;
731: case '(': parencnt++; break;
732: case ')': parencnt--; break;
733: }
734: }
735:
1.11 millert 736: double errcheck(double x, const char *s)
1.1 tholo 737: {
738:
739: if (errno == EDOM) {
740: errno = 0;
1.7 millert 741: WARNING("%s argument out of domain", s);
1.1 tholo 742: x = 1;
743: } else if (errno == ERANGE) {
744: errno = 0;
1.7 millert 745: WARNING("%s result out of range", s);
1.1 tholo 746: x = 1;
747: }
748: return x;
749: }
750:
1.11 millert 751: int isclvar(const char *s) /* is s of form var=something ? */
1.1 tholo 752: {
1.11 millert 753: const char *os = s;
1.1 tholo 754:
1.8 millert 755: if (!isalpha((uschar) *s) && *s != '_')
1.1 tholo 756: return 0;
757: for ( ; *s; s++)
1.8 millert 758: if (!(isalnum((uschar) *s) || *s == '_'))
1.1 tholo 759: break;
1.28 millert 760: return *s == '=' && s > os;
1.1 tholo 761: }
762:
1.4 kstailey 763: /* strtod is supposed to be a proper test of what's a valid number */
1.8 millert 764: /* appears to be broken in gcc on linux: thinks 0x123 is a valid FP number */
765: /* wrong: violates 4.10.1.4 of ansi C standard */
1.42 millert 766:
1.38 millert 767: /* well, not quite. As of C99, hex floating point is allowed. so this is
1.42 millert 768: * a bit of a mess. We work around the mess by checking for a hexadecimal
769: * value and disallowing it. Similarly, we now follow gawk and allow only
770: * +nan, -nan, +inf, and -inf for NaN and infinity values.
1.38 millert 771: */
1.1 tholo 772:
1.42 millert 773: /*
774: * This routine now has a more complicated interface, the main point
775: * being to avoid the double conversion of a string to double, and
776: * also to convey out, if requested, the information that the numeric
777: * value was a leading string or is all of the string. The latter bit
778: * is used in getfval().
779: */
780:
781: bool is_valid_number(const char *s, bool trailing_stuff_ok,
782: bool *no_trailing, double *result)
1.1 tholo 783: {
1.4 kstailey 784: double r;
785: char *ep;
1.42 millert 786: bool retval = false;
787:
788: if (no_trailing)
789: *no_trailing = false;
790:
1.43 ! millert 791: while (isspace((uschar)*s))
1.42 millert 792: s++;
793:
1.43 ! millert 794: // no hex floating point, sorry
! 795: if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
1.42 millert 796: return false;
797:
798: // allow +nan, -nan, +inf, -inf, any other letter, no
799: if (s[0] == '+' || s[0] == '-') {
1.43 ! millert 800: if ((strncasecmp(s+1, "nan", 3) == 0 ||
! 801: strncasecmp(s+1, "inf", 3) == 0)) {
! 802: trailing_stuff_ok = false;
! 803: ep = (char *)(long)s + 4;
! 804: if (isspace((uschar)*ep)) {
! 805: trailing_stuff_ok = true;
! 806: do {
! 807: ep++;
! 808: } while (isspace((uschar)*ep));
! 809: }
! 810: if (no_trailing)
! 811: *no_trailing = (*ep == '\0');
! 812: if (*ep != '\0' && !trailing_stuff_ok)
! 813: return false;
! 814: } else if (! isdigit((uschar)s[1]) && s[1] != '.')
1.42 millert 815: return false;
1.43 ! millert 816: } else if (! isdigit((uschar)s[0]) && s[0] != '.')
1.42 millert 817: return false;
818:
1.4 kstailey 819: errno = 0;
820: r = strtod(s, &ep);
1.43 ! millert 821: if (ep == s || errno == ERANGE)
1.42 millert 822: return false;
823:
824: if (result != NULL)
825: *result = r;
826:
1.43 ! millert 827: // check for trailing stuff
! 828: while (isspace((uschar)*ep))
1.4 kstailey 829: ep++;
1.42 millert 830:
831: if (no_trailing)
832: *no_trailing = (*ep == '\0');
833:
834: // return true if found the end, or trailing stuff is allowed
835: retval = (*ep == '\0') || trailing_stuff_ok;
836:
837: return retval;
1.1 tholo 838: }