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

1.35    ! millert     1: /*     $OpenBSD: lib.c,v 1.34 2020/06/10 21:05:50 millert Exp $        */
1.1       tholo       2: /****************************************************************
1.4       kstailey    3: Copyright (C) Lucent Technologies 1997
1.1       tholo       4: All Rights Reserved
                      5:
                      6: Permission to use, copy, modify, and distribute this software and
                      7: its documentation for any purpose and without fee is hereby
                      8: granted, provided that the above copyright notice appear in all
                      9: copies and that both that the copyright notice and this
                     10: permission notice and warranty disclaimer appear in supporting
1.4       kstailey   11: documentation, and that the name Lucent Technologies or any of
                     12: its entities not be used in advertising or publicity pertaining
                     13: to distribution of the software without specific, written prior
                     14: permission.
                     15:
                     16: LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
                     17: INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
                     18: IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY
                     19: SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     20: WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
                     21: IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
                     22: ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
                     23: THIS SOFTWARE.
1.1       tholo      24: ****************************************************************/
                     25:
                     26: #define DEBUG
                     27: #include <stdio.h>
                     28: #include <string.h>
                     29: #include <ctype.h>
                     30: #include <errno.h>
                     31: #include <stdlib.h>
1.7       millert    32: #include <stdarg.h>
1.33      millert    33: #include <limits.h>
1.1       tholo      34: #include "awk.h"
1.4       kstailey   35: #include "ytab.h"
1.1       tholo      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.31      millert    65:        if ( (record = malloc(n)) == NULL
                     66:          || (fields = malloc(n+1)) == NULL
                     67:          || (fldtab = calloc(nfields+2, sizeof(*fldtab))) == NULL
                     68:          || (fldtab[0] = 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.31      millert    83:                fldtab[i] = 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.33      millert   125:        size_t len;
                    126:        if ((len = strlen(getsval(fsloc))) < len_inputFS) {
                    127:                strlcpy(inputFS, *FS, sizeof(inputFS)); /* for subsequent field splitting */
                    128:                return;
                    129:        }
                    130:
                    131:        len_inputFS = len + 1;
                    132:        inputFS = realloc(inputFS, len_inputFS);
                    133:        if (inputFS == NULL)
1.29      millert   134:                FATAL("field separator %.10s... is too long", *FS);
1.33      millert   135:        memcpy(inputFS, *FS, len_inputFS);
1.29      millert   136: }
                    137:
1.32      millert   138: static bool firsttime = true;
1.15      millert   139:
1.32      millert   140: int getrec(char **pbuf, int *pbufsize, bool isrecord)  /* get next input record */
1.4       kstailey  141: {                      /* note: cares whether buf == record */
1.1       tholo     142:        int c;
1.4       kstailey  143:        char *buf = *pbuf;
1.18      millert   144:        uschar saveb0;
                    145:        int bufsize = *pbufsize, savebufsize = bufsize;
1.1       tholo     146:
                    147:        if (firsttime) {
1.32      millert   148:                firsttime = false;
1.1       tholo     149:                initgetrec();
                    150:        }
1.24      deraadt   151:           DPRINTF( ("RS=<%s>, FS=<%s>, ARGC=%g, FILENAME=%s\n",
1.1       tholo     152:                *RS, *FS, *ARGC, *FILENAME) );
1.5       millert   153:        if (isrecord) {
1.32      millert   154:                donefld = false;
                    155:                donerec = true;
1.29      millert   156:                savefs();
1.5       millert   157:        }
1.18      millert   158:        saveb0 = buf[0];
1.1       tholo     159:        buf[0] = 0;
                    160:        while (argno < *ARGC || infile == stdin) {
1.24      deraadt   161:                   DPRINTF( ("argno=%d, file=|%s|\n", argno, file) );
1.1       tholo     162:                if (infile == NULL) {   /* have to open a new file */
                    163:                        file = getargv(argno);
1.20      millert   164:                        if (file == NULL || *file == '\0') {    /* deleted or zapped */
1.1       tholo     165:                                argno++;
                    166:                                continue;
                    167:                        }
                    168:                        if (isclvar(file)) {    /* a var=value arg */
                    169:                                setclvar(file);
                    170:                                argno++;
                    171:                                continue;
                    172:                        }
                    173:                        *FILENAME = file;
1.24      deraadt   174:                           DPRINTF( ("opening file %s\n", file) );
1.1       tholo     175:                        if (*file == '-' && *(file+1) == '\0')
                    176:                                infile = stdin;
1.4       kstailey  177:                        else if ((infile = fopen(file, "r")) == NULL)
1.7       millert   178:                                FATAL("can't open file %s", file);
1.1       tholo     179:                        setfval(fnrloc, 0.0);
                    180:                }
1.34      millert   181:                c = readrec(&buf, &bufsize, infile, innew);
                    182:                if (innew)
                    183:                        innew = false;
1.1       tholo     184:                if (c != 0 || buf[0] != '\0') { /* normal record */
1.4       kstailey  185:                        if (isrecord) {
                    186:                                if (freeable(fldtab[0]))
                    187:                                        xfree(fldtab[0]->sval);
                    188:                                fldtab[0]->sval = buf;  /* buf == record */
                    189:                                fldtab[0]->tval = REC | STR | DONTFREE;
1.5       millert   190:                                if (is_number(fldtab[0]->sval)) {
1.4       kstailey  191:                                        fldtab[0]->fval = atof(fldtab[0]->sval);
                    192:                                        fldtab[0]->tval |= NUM;
1.1       tholo     193:                                }
                    194:                        }
                    195:                        setfval(nrloc, nrloc->fval+1);
                    196:                        setfval(fnrloc, fnrloc->fval+1);
1.4       kstailey  197:                        *pbuf = buf;
                    198:                        *pbufsize = bufsize;
1.1       tholo     199:                        return 1;
                    200:                }
                    201:                /* EOF arrived on this file; set up next */
                    202:                if (infile != stdin)
                    203:                        fclose(infile);
                    204:                infile = NULL;
                    205:                argno++;
                    206:        }
1.18      millert   207:        buf[0] = saveb0;
1.4       kstailey  208:        *pbuf = buf;
1.18      millert   209:        *pbufsize = savebufsize;
1.1       tholo     210:        return 0;       /* true end of file */
                    211: }
                    212:
                    213: void nextfile(void)
                    214: {
1.18      millert   215:        if (infile != NULL && infile != stdin)
1.1       tholo     216:                fclose(infile);
                    217:        infile = NULL;
                    218:        argno++;
                    219: }
                    220:
1.34      millert   221: int readrec(char **pbuf, int *pbufsize, FILE *inf, bool newflag)       /* read one record into buf */
1.1       tholo     222: {
1.30      millert   223:        int sep, c, isrec;
1.4       kstailey  224:        char *rr, *buf = *pbuf;
                    225:        int bufsize = *pbufsize;
1.27      millert   226:        char *rs = getsval(rsloc);
1.1       tholo     227:
1.30      millert   228:        if (*rs && rs[1]) {
1.32      millert   229:                bool found;
1.30      millert   230:
                    231:                fa *pfa = makedfa(rs, 1);
1.34      millert   232:                if (newflag)
                    233:                        found = fnematch(pfa, inf, &buf, &bufsize, recsize);
                    234:                else {
                    235:                        int tempstat = pfa->initstat;
                    236:                        pfa->initstat = 2;
                    237:                        found = fnematch(pfa, inf, &buf, &bufsize, recsize);
                    238:                        pfa->initstat = tempstat;
                    239:                }
1.30      millert   240:                if (found)
1.31      millert   241:                        setptr(patbeg, '\0');
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.1       tholo     271:        }
1.4       kstailey  272:        *pbuf = buf;
                    273:        *pbufsize = bufsize;
1.30      millert   274:        isrec = *buf || !feof(inf);
                    275:        DPRINTF( ("readrec saw <%s>, returns %d\n", buf, isrec) );
                    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.24      deraadt   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: {
                    296:        char *p;
                    297:        Cell *q;
                    298:
                    299:        for (p=s; *p != '='; p++)
                    300:                ;
                    301:        *p++ = 0;
                    302:        p = qstring(p, '\0');
                    303:        q = setsymtab(s, p, 0.0, STR, symtab);
                    304:        setsval(q, p);
1.5       millert   305:        if (is_number(q->sval)) {
1.1       tholo     306:                q->fval = atof(q->sval);
                    307:                q->tval |= NUM;
                    308:        }
1.24      deraadt   309:           DPRINTF( ("command line set %s to |%s|\n", s, p) );
1.1       tholo     310: }
                    311:
                    312:
                    313: void fldbld(void)      /* create fields from current record */
                    314: {
1.4       kstailey  315:        /* this relies on having fields[] the same length as $0 */
                    316:        /* the fields are all stored in this one array with \0's */
1.20      millert   317:        /* possibly with a final trailing \0 not associated with any field */
1.1       tholo     318:        char *r, *fr, sep;
                    319:        Cell *p;
1.4       kstailey  320:        int i, j, n;
1.1       tholo     321:
                    322:        if (donefld)
                    323:                return;
1.4       kstailey  324:        if (!isstr(fldtab[0]))
                    325:                getsval(fldtab[0]);
                    326:        r = fldtab[0]->sval;
                    327:        n = strlen(r);
                    328:        if (n > fieldssize) {
                    329:                xfree(fields);
1.31      millert   330:                if ((fields = malloc(n+2)) == NULL) /* possibly 2 final \0s */
1.7       millert   331:                        FATAL("out of space for fields in fldbld %d", n);
1.4       kstailey  332:                fieldssize = n;
                    333:        }
1.1       tholo     334:        fr = fields;
                    335:        i = 0;  /* number of fields accumulated here */
1.35    ! millert   336:        if (inputFS == NULL)    /* make sure we have a copy of FS */
        !           337:                savefs();
1.2       millert   338:        if (strlen(inputFS) > 1) {      /* it's a regular expression */
                    339:                i = refldbld(r, inputFS);
                    340:        } else if ((sep = *inputFS) == ' ') {   /* default whitespace */
1.1       tholo     341:                for (i = 0; ; ) {
                    342:                        while (*r == ' ' || *r == '\t' || *r == '\n')
                    343:                                r++;
                    344:                        if (*r == 0)
                    345:                                break;
                    346:                        i++;
1.4       kstailey  347:                        if (i > nfields)
                    348:                                growfldtab(i);
                    349:                        if (freeable(fldtab[i]))
                    350:                                xfree(fldtab[i]->sval);
                    351:                        fldtab[i]->sval = fr;
                    352:                        fldtab[i]->tval = FLD | STR | DONTFREE;
1.1       tholo     353:                        do
                    354:                                *fr++ = *r++;
                    355:                        while (*r != ' ' && *r != '\t' && *r != '\n' && *r != '\0');
                    356:                        *fr++ = 0;
                    357:                }
                    358:                *fr = 0;
1.2       millert   359:        } else if ((sep = *inputFS) == 0) {             /* new: FS="" => 1 char/field */
1.32      millert   360:                for (i = 0; *r != '\0'; r += n) {
1.33      millert   361:                        char buf[MB_LEN_MAX + 1];
1.32      millert   362:
1.1       tholo     363:                        i++;
1.4       kstailey  364:                        if (i > nfields)
                    365:                                growfldtab(i);
                    366:                        if (freeable(fldtab[i]))
                    367:                                xfree(fldtab[i]->sval);
1.33      millert   368:                        n = mblen(r, MB_LEN_MAX);
1.32      millert   369:                        if (n < 0)
                    370:                                n = 1;
                    371:                        memcpy(buf, r, n);
                    372:                        buf[n] = '\0';
1.4       kstailey  373:                        fldtab[i]->sval = tostring(buf);
                    374:                        fldtab[i]->tval = FLD | STR;
1.1       tholo     375:                }
                    376:                *fr = 0;
                    377:        } else if (*r != 0) {   /* if 0, it's a null field */
1.15      millert   378:                /* subtlecase : if length(FS) == 1 && length(RS > 0)
                    379:                 * \n is NOT a field separator (cf awk book 61,84).
                    380:                 * this variable is tested in the inner while loop.
                    381:                 */
                    382:                int rtest = '\n';  /* normal case */
                    383:                if (strlen(*RS) > 0)
                    384:                        rtest = '\0';
1.1       tholo     385:                for (;;) {
                    386:                        i++;
1.4       kstailey  387:                        if (i > nfields)
                    388:                                growfldtab(i);
                    389:                        if (freeable(fldtab[i]))
                    390:                                xfree(fldtab[i]->sval);
                    391:                        fldtab[i]->sval = fr;
                    392:                        fldtab[i]->tval = FLD | STR | DONTFREE;
1.15      millert   393:                        while (*r != sep && *r != rtest && *r != '\0')  /* \n is always a separator */
1.1       tholo     394:                                *fr++ = *r++;
                    395:                        *fr++ = 0;
                    396:                        if (*r++ == 0)
                    397:                                break;
                    398:                }
                    399:                *fr = 0;
                    400:        }
1.4       kstailey  401:        if (i > nfields)
1.7       millert   402:                FATAL("record `%.30s...' has too many fields; can't happen", r);
1.4       kstailey  403:        cleanfld(i+1, lastfld); /* clean out junk from previous record */
                    404:        lastfld = i;
1.32      millert   405:        donefld = true;
1.4       kstailey  406:        for (j = 1; j <= lastfld; j++) {
                    407:                p = fldtab[j];
1.5       millert   408:                if(is_number(p->sval)) {
1.1       tholo     409:                        p->fval = atof(p->sval);
                    410:                        p->tval |= NUM;
                    411:                }
                    412:        }
1.4       kstailey  413:        setfval(nfloc, (Awkfloat) lastfld);
1.32      millert   414:        donerec = true; /* restore */
1.4       kstailey  415:        if (dbg) {
                    416:                for (j = 0; j <= lastfld; j++) {
                    417:                        p = fldtab[j];
                    418:                        printf("field %d (%s): |%s|\n", j, p->nval, p->sval);
                    419:                }
                    420:        }
1.1       tholo     421: }
                    422:
1.4       kstailey  423: void cleanfld(int n1, int n2)  /* clean out fields n1 .. n2 inclusive */
                    424: {                              /* nvals remain intact */
                    425:        Cell *p;
                    426:        int i;
1.1       tholo     427:
1.4       kstailey  428:        for (i = n1; i <= n2; i++) {
                    429:                p = fldtab[i];
                    430:                if (freeable(p))
1.1       tholo     431:                        xfree(p->sval);
1.33      millert   432:                p->sval = EMPTY,
1.1       tholo     433:                p->tval = FLD | STR | DONTFREE;
                    434:        }
                    435: }
                    436:
1.4       kstailey  437: void newfld(int n)     /* add field n after end of existing lastfld */
1.1       tholo     438: {
1.4       kstailey  439:        if (n > nfields)
                    440:                growfldtab(n);
                    441:        cleanfld(lastfld+1, n);
                    442:        lastfld = n;
1.1       tholo     443:        setfval(nfloc, (Awkfloat) n);
1.26      millert   444: }
                    445:
                    446: void setlastfld(int n) /* set lastfld cleaning fldtab cells if necessary */
                    447: {
1.27      millert   448:        if (n < 0)
                    449:                FATAL("cannot set NF to a negative value");
1.26      millert   450:        if (n > nfields)
                    451:                growfldtab(n);
                    452:
                    453:        if (lastfld < n)
                    454:            cleanfld(lastfld+1, n);
                    455:        else
                    456:            cleanfld(n+1, lastfld);
                    457:
                    458:        lastfld = n;
1.1       tholo     459: }
                    460:
1.4       kstailey  461: Cell *fieldadr(int n)  /* get nth field */
                    462: {
                    463:        if (n < 0)
1.15      millert   464:                FATAL("trying to access out of range field %d", n);
1.4       kstailey  465:        if (n > nfields)        /* fields after NF are empty */
                    466:                growfldtab(n);  /* but does not increase NF */
                    467:        return(fldtab[n]);
                    468: }
                    469:
                    470: void growfldtab(int n) /* make new fields up to at least $n */
                    471: {
                    472:        int nf = 2 * nfields;
1.15      millert   473:        size_t s;
1.4       kstailey  474:
                    475:        if (n > nf)
                    476:                nf = n;
1.15      millert   477:        s = (nf+1) * (sizeof (struct Cell *));  /* freebsd: how much do we need? */
1.34      millert   478:        if (s / sizeof(struct Cell *) - 1 == (size_t)nf) /* didn't overflow */
1.31      millert   479:                fldtab = realloc(fldtab, s);
1.15      millert   480:        else                                    /* overflow sizeof int */
                    481:                xfree(fldtab);  /* make it null */
1.4       kstailey  482:        if (fldtab == NULL)
1.7       millert   483:                FATAL("out of space creating %d fields", nf);
1.4       kstailey  484:        makefields(nfields+1, nf);
                    485:        nfields = nf;
                    486: }
                    487:
1.11      millert   488: int refldbld(const char *rec, const char *fs)  /* build fields from reg expr in FS */
1.1       tholo     489: {
1.4       kstailey  490:        /* this relies on having fields[] the same length as $0 */
                    491:        /* the fields are all stored in this one array with \0's */
1.1       tholo     492:        char *fr;
1.4       kstailey  493:        int i, tempstat, n;
1.1       tholo     494:        fa *pfa;
                    495:
1.4       kstailey  496:        n = strlen(rec);
                    497:        if (n > fieldssize) {
                    498:                xfree(fields);
1.31      millert   499:                if ((fields = malloc(n+1)) == NULL)
1.7       millert   500:                        FATAL("out of space for fields in refldbld %d", n);
1.4       kstailey  501:                fieldssize = n;
                    502:        }
1.1       tholo     503:        fr = fields;
                    504:        *fr = '\0';
                    505:        if (*rec == '\0')
                    506:                return 0;
                    507:        pfa = makedfa(fs, 1);
1.24      deraadt   508:           DPRINTF( ("into refldbld, rec = <%s>, pat = <%s>\n", rec, fs) );
1.1       tholo     509:        tempstat = pfa->initstat;
1.4       kstailey  510:        for (i = 1; ; i++) {
                    511:                if (i > nfields)
                    512:                        growfldtab(i);
                    513:                if (freeable(fldtab[i]))
                    514:                        xfree(fldtab[i]->sval);
                    515:                fldtab[i]->tval = FLD | STR | DONTFREE;
                    516:                fldtab[i]->sval = fr;
1.24      deraadt   517:                   DPRINTF( ("refldbld: i=%d\n", i) );
1.1       tholo     518:                if (nematch(pfa, rec)) {
1.4       kstailey  519:                        pfa->initstat = 2;      /* horrible coupling to b.c */
1.24      deraadt   520:                           DPRINTF( ("match %s (%d chars)\n", patbeg, patlen) );
1.1       tholo     521:                        strncpy(fr, rec, patbeg-rec);
                    522:                        fr += patbeg - rec + 1;
                    523:                        *(fr-1) = '\0';
                    524:                        rec = patbeg + patlen;
                    525:                } else {
1.24      deraadt   526:                           DPRINTF( ("no match %s\n", rec) );
1.13      tedu      527:                        strlcpy(fr, rec, fields + fieldssize - fr);
1.1       tholo     528:                        pfa->initstat = tempstat;
                    529:                        break;
                    530:                }
                    531:        }
1.29      millert   532:        return i;
1.1       tholo     533: }
                    534:
                    535: void recbld(void)      /* create $0 from $1..$NF if necessary */
                    536: {
                    537:        int i;
                    538:        char *r, *p;
1.27      millert   539:        char *sep = getsval(ofsloc);
1.1       tholo     540:
1.32      millert   541:        if (donerec)
1.1       tholo     542:                return;
1.5       millert   543:        r = record;
1.1       tholo     544:        for (i = 1; i <= *NF; i++) {
1.4       kstailey  545:                p = getsval(fldtab[i]);
1.5       millert   546:                if (!adjbuf(&record, &recsize, 1+strlen(p)+r-record, recsize, &r, "recbld 1"))
1.7       millert   547:                        FATAL("created $0 `%.30s...' too long", record);
1.4       kstailey  548:                while ((*r = *p++) != 0)
1.1       tholo     549:                        r++;
1.4       kstailey  550:                if (i < *NF) {
1.27      millert   551:                        if (!adjbuf(&record, &recsize, 2+strlen(sep)+r-record, recsize, &r, "recbld 2"))
1.7       millert   552:                                FATAL("created $0 `%.30s...' too long", record);
1.27      millert   553:                        for (p = sep; (*r = *p++) != 0; )
1.1       tholo     554:                                r++;
1.4       kstailey  555:                }
1.1       tholo     556:        }
1.5       millert   557:        if (!adjbuf(&record, &recsize, 2+r-record, recsize, &r, "recbld 3"))
1.7       millert   558:                FATAL("built giant record `%.30s...'", record);
1.1       tholo     559:        *r = '\0';
1.24      deraadt   560:           DPRINTF( ("in recbld inputFS=%s, fldtab[0]=%p\n", inputFS, (void*)fldtab[0]) );
1.4       kstailey  561:
                    562:        if (freeable(fldtab[0]))
                    563:                xfree(fldtab[0]->sval);
                    564:        fldtab[0]->tval = REC | STR | DONTFREE;
                    565:        fldtab[0]->sval = record;
                    566:
1.24      deraadt   567:           DPRINTF( ("in recbld inputFS=%s, fldtab[0]=%p\n", inputFS, (void*)fldtab[0]) );
                    568:           DPRINTF( ("recbld = |%s|\n", record) );
1.32      millert   569:        donerec = true;
1.1       tholo     570: }
                    571:
                    572: int    errorflag       = 0;
                    573:
1.11      millert   574: void yyerror(const char *s)
1.1       tholo     575: {
1.14      grange    576:        SYNTAX("%s", s);
1.7       millert   577: }
                    578:
1.11      millert   579: void SYNTAX(const char *fmt, ...)
1.7       millert   580: {
1.1       tholo     581:        extern char *cmdname, *curfname;
                    582:        static int been_here = 0;
1.7       millert   583:        va_list varg;
1.1       tholo     584:
                    585:        if (been_here++ > 2)
                    586:                return;
1.7       millert   587:        fprintf(stderr, "%s: ", cmdname);
                    588:        va_start(varg, fmt);
                    589:        vfprintf(stderr, fmt, varg);
                    590:        va_end(varg);
1.1       tholo     591:        fprintf(stderr, " at source line %d", lineno);
                    592:        if (curfname != NULL)
                    593:                fprintf(stderr, " in function %s", curfname);
1.32      millert   594:        if (compile_time == COMPILING && cursource() != NULL)
1.6       millert   595:                fprintf(stderr, " source file %s", cursource());
1.1       tholo     596:        fprintf(stderr, "\n");
                    597:        errorflag = 2;
                    598:        eprint();
                    599: }
                    600:
                    601: extern int bracecnt, brackcnt, parencnt;
                    602:
                    603: void bracecheck(void)
                    604: {
                    605:        int c;
                    606:        static int beenhere = 0;
                    607:
                    608:        if (beenhere++)
                    609:                return;
1.3       millert   610:        while ((c = input()) != EOF && c != '\0')
1.1       tholo     611:                bclass(c);
                    612:        bcheck2(bracecnt, '{', '}');
                    613:        bcheck2(brackcnt, '[', ']');
                    614:        bcheck2(parencnt, '(', ')');
                    615: }
                    616:
                    617: void bcheck2(int n, int c1, int c2)
                    618: {
                    619:        if (n == 1)
                    620:                fprintf(stderr, "\tmissing %c\n", c2);
                    621:        else if (n > 1)
                    622:                fprintf(stderr, "\t%d missing %c's\n", n, c2);
                    623:        else if (n == -1)
                    624:                fprintf(stderr, "\textra %c\n", c2);
                    625:        else if (n < -1)
                    626:                fprintf(stderr, "\t%d extra %c's\n", -n, c2);
                    627: }
                    628:
1.35    ! millert   629: void FATAL(const char *fmt, ...)
1.7       millert   630: {
                    631:        extern char *cmdname;
                    632:        va_list varg;
                    633:
                    634:        fflush(stdout);
                    635:        fprintf(stderr, "%s: ", cmdname);
                    636:        va_start(varg, fmt);
                    637:        vfprintf(stderr, fmt, varg);
                    638:        va_end(varg);
                    639:        error();
                    640:        if (dbg > 1)            /* core dump if serious debugging on */
                    641:                abort();
                    642:        exit(2);
                    643: }
                    644:
1.11      millert   645: void WARNING(const char *fmt, ...)
1.1       tholo     646: {
                    647:        extern char *cmdname;
1.7       millert   648:        va_list varg;
1.1       tholo     649:
                    650:        fflush(stdout);
                    651:        fprintf(stderr, "%s: ", cmdname);
1.7       millert   652:        va_start(varg, fmt);
                    653:        vfprintf(stderr, fmt, varg);
                    654:        va_end(varg);
                    655:        error();
                    656: }
                    657:
                    658: void error()
                    659: {
                    660:        extern Node *curnode;
                    661:
1.1       tholo     662:        fprintf(stderr, "\n");
1.32      millert   663:        if (compile_time != ERROR_PRINTING) {
                    664:                if (NR && *NR > 0) {
                    665:                        fprintf(stderr, " input record number %d", (int) (*FNR));
                    666:                        if (strcmp(*FILENAME, "-") != 0)
                    667:                                fprintf(stderr, ", file %s", *FILENAME);
                    668:                        fprintf(stderr, "\n");
                    669:                }
                    670:                if (curnode)
                    671:                        fprintf(stderr, " source line number %d", curnode->lineno);
                    672:                else if (lineno)
                    673:                        fprintf(stderr, " source line number %d", lineno);
                    674:        }
                    675:
                    676:        if (compile_time == COMPILING && cursource() != NULL)
1.6       millert   677:                fprintf(stderr, " source file %s", cursource());
                    678:        fprintf(stderr, "\n");
1.1       tholo     679:        eprint();
                    680: }
                    681:
                    682: void eprint(void)      /* try to print context around error */
                    683: {
                    684:        char *p, *q;
                    685:        int c;
                    686:        static int been_here = 0;
                    687:        extern char ebuf[], *ep;
                    688:
1.32      millert   689:        if (compile_time != COMPILING || been_here++ > 0 || ebuf == ep)
1.1       tholo     690:                return;
                    691:        p = ep - 1;
                    692:        if (p > ebuf && *p == '\n')
                    693:                p--;
                    694:        for ( ; p > ebuf && *p != '\n' && *p != '\0'; p--)
                    695:                ;
                    696:        while (*p == '\n')
                    697:                p++;
                    698:        fprintf(stderr, " context is\n\t");
                    699:        for (q=ep-1; q>=p && *q!=' ' && *q!='\t' && *q!='\n'; q--)
                    700:                ;
                    701:        for ( ; p < q; p++)
                    702:                if (*p)
                    703:                        putc(*p, stderr);
                    704:        fprintf(stderr, " >>> ");
                    705:        for ( ; p < ep; p++)
                    706:                if (*p)
                    707:                        putc(*p, stderr);
                    708:        fprintf(stderr, " <<< ");
                    709:        if (*ep)
                    710:                while ((c = input()) != '\n' && c != '\0' && c != EOF) {
                    711:                        putc(c, stderr);
                    712:                        bclass(c);
                    713:                }
                    714:        putc('\n', stderr);
                    715:        ep = ebuf;
                    716: }
                    717:
                    718: void bclass(int c)
                    719: {
                    720:        switch (c) {
                    721:        case '{': bracecnt++; break;
                    722:        case '}': bracecnt--; break;
                    723:        case '[': brackcnt++; break;
                    724:        case ']': brackcnt--; break;
                    725:        case '(': parencnt++; break;
                    726:        case ')': parencnt--; break;
                    727:        }
                    728: }
                    729:
1.11      millert   730: double errcheck(double x, const char *s)
1.1       tholo     731: {
                    732:
                    733:        if (errno == EDOM) {
                    734:                errno = 0;
1.7       millert   735:                WARNING("%s argument out of domain", s);
1.1       tholo     736:                x = 1;
                    737:        } else if (errno == ERANGE) {
                    738:                errno = 0;
1.7       millert   739:                WARNING("%s result out of range", s);
1.1       tholo     740:                x = 1;
                    741:        }
                    742:        return x;
                    743: }
                    744:
1.11      millert   745: int isclvar(const char *s)     /* is s of form var=something ? */
1.1       tholo     746: {
1.11      millert   747:        const char *os = s;
1.1       tholo     748:
1.8       millert   749:        if (!isalpha((uschar) *s) && *s != '_')
1.1       tholo     750:                return 0;
                    751:        for ( ; *s; s++)
1.8       millert   752:                if (!(isalnum((uschar) *s) || *s == '_'))
1.1       tholo     753:                        break;
1.28      millert   754:        return *s == '=' && s > os;
1.1       tholo     755: }
                    756:
1.4       kstailey  757: /* strtod is supposed to be a proper test of what's a valid number */
1.8       millert   758: /* appears to be broken in gcc on linux: thinks 0x123 is a valid FP number */
                    759: /* wrong: violates 4.10.1.4 of ansi C standard */
1.1       tholo     760:
1.4       kstailey  761: #include <math.h>
1.11      millert   762: int is_number(const char *s)
1.1       tholo     763: {
1.4       kstailey  764:        double r;
                    765:        char *ep;
                    766:        errno = 0;
                    767:        r = strtod(s, &ep);
                    768:        if (ep == s || r == HUGE_VAL || errno == ERANGE)
                    769:                return 0;
                    770:        while (*ep == ' ' || *ep == '\t' || *ep == '\n')
                    771:                ep++;
                    772:        if (*ep == '\0')
                    773:                return 1;
1.1       tholo     774:        else
1.4       kstailey  775:                return 0;
1.1       tholo     776: }