[BACK]Return to lib.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / awk

Annotation of src/usr.bin/awk/lib.c, Revision 1.48

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