[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.50

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