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

1.34    ! millert     1: /*     $OpenBSD: lib.c,v 1.33 2020/06/10 21:05:02 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.2       millert   336:        if (strlen(inputFS) > 1) {      /* it's a regular expression */
                    337:                i = refldbld(r, inputFS);
                    338:        } else if ((sep = *inputFS) == ' ') {   /* default whitespace */
1.1       tholo     339:                for (i = 0; ; ) {
                    340:                        while (*r == ' ' || *r == '\t' || *r == '\n')
                    341:                                r++;
                    342:                        if (*r == 0)
                    343:                                break;
                    344:                        i++;
1.4       kstailey  345:                        if (i > nfields)
                    346:                                growfldtab(i);
                    347:                        if (freeable(fldtab[i]))
                    348:                                xfree(fldtab[i]->sval);
                    349:                        fldtab[i]->sval = fr;
                    350:                        fldtab[i]->tval = FLD | STR | DONTFREE;
1.1       tholo     351:                        do
                    352:                                *fr++ = *r++;
                    353:                        while (*r != ' ' && *r != '\t' && *r != '\n' && *r != '\0');
                    354:                        *fr++ = 0;
                    355:                }
                    356:                *fr = 0;
1.2       millert   357:        } else if ((sep = *inputFS) == 0) {             /* new: FS="" => 1 char/field */
1.32      millert   358:                for (i = 0; *r != '\0'; r += n) {
1.33      millert   359:                        char buf[MB_LEN_MAX + 1];
1.32      millert   360:
1.1       tholo     361:                        i++;
1.4       kstailey  362:                        if (i > nfields)
                    363:                                growfldtab(i);
                    364:                        if (freeable(fldtab[i]))
                    365:                                xfree(fldtab[i]->sval);
1.33      millert   366:                        n = mblen(r, MB_LEN_MAX);
1.32      millert   367:                        if (n < 0)
                    368:                                n = 1;
                    369:                        memcpy(buf, r, n);
                    370:                        buf[n] = '\0';
1.4       kstailey  371:                        fldtab[i]->sval = tostring(buf);
                    372:                        fldtab[i]->tval = FLD | STR;
1.1       tholo     373:                }
                    374:                *fr = 0;
                    375:        } else if (*r != 0) {   /* if 0, it's a null field */
1.15      millert   376:                /* subtlecase : if length(FS) == 1 && length(RS > 0)
                    377:                 * \n is NOT a field separator (cf awk book 61,84).
                    378:                 * this variable is tested in the inner while loop.
                    379:                 */
                    380:                int rtest = '\n';  /* normal case */
                    381:                if (strlen(*RS) > 0)
                    382:                        rtest = '\0';
1.1       tholo     383:                for (;;) {
                    384:                        i++;
1.4       kstailey  385:                        if (i > nfields)
                    386:                                growfldtab(i);
                    387:                        if (freeable(fldtab[i]))
                    388:                                xfree(fldtab[i]->sval);
                    389:                        fldtab[i]->sval = fr;
                    390:                        fldtab[i]->tval = FLD | STR | DONTFREE;
1.15      millert   391:                        while (*r != sep && *r != rtest && *r != '\0')  /* \n is always a separator */
1.1       tholo     392:                                *fr++ = *r++;
                    393:                        *fr++ = 0;
                    394:                        if (*r++ == 0)
                    395:                                break;
                    396:                }
                    397:                *fr = 0;
                    398:        }
1.4       kstailey  399:        if (i > nfields)
1.7       millert   400:                FATAL("record `%.30s...' has too many fields; can't happen", r);
1.4       kstailey  401:        cleanfld(i+1, lastfld); /* clean out junk from previous record */
                    402:        lastfld = i;
1.32      millert   403:        donefld = true;
1.4       kstailey  404:        for (j = 1; j <= lastfld; j++) {
                    405:                p = fldtab[j];
1.5       millert   406:                if(is_number(p->sval)) {
1.1       tholo     407:                        p->fval = atof(p->sval);
                    408:                        p->tval |= NUM;
                    409:                }
                    410:        }
1.4       kstailey  411:        setfval(nfloc, (Awkfloat) lastfld);
1.32      millert   412:        donerec = true; /* restore */
1.4       kstailey  413:        if (dbg) {
                    414:                for (j = 0; j <= lastfld; j++) {
                    415:                        p = fldtab[j];
                    416:                        printf("field %d (%s): |%s|\n", j, p->nval, p->sval);
                    417:                }
                    418:        }
1.1       tholo     419: }
                    420:
1.4       kstailey  421: void cleanfld(int n1, int n2)  /* clean out fields n1 .. n2 inclusive */
                    422: {                              /* nvals remain intact */
                    423:        Cell *p;
                    424:        int i;
1.1       tholo     425:
1.4       kstailey  426:        for (i = n1; i <= n2; i++) {
                    427:                p = fldtab[i];
                    428:                if (freeable(p))
1.1       tholo     429:                        xfree(p->sval);
1.33      millert   430:                p->sval = EMPTY,
1.1       tholo     431:                p->tval = FLD | STR | DONTFREE;
                    432:        }
                    433: }
                    434:
1.4       kstailey  435: void newfld(int n)     /* add field n after end of existing lastfld */
1.1       tholo     436: {
1.4       kstailey  437:        if (n > nfields)
                    438:                growfldtab(n);
                    439:        cleanfld(lastfld+1, n);
                    440:        lastfld = n;
1.1       tholo     441:        setfval(nfloc, (Awkfloat) n);
1.26      millert   442: }
                    443:
                    444: void setlastfld(int n) /* set lastfld cleaning fldtab cells if necessary */
                    445: {
1.27      millert   446:        if (n < 0)
                    447:                FATAL("cannot set NF to a negative value");
1.26      millert   448:        if (n > nfields)
                    449:                growfldtab(n);
                    450:
                    451:        if (lastfld < n)
                    452:            cleanfld(lastfld+1, n);
                    453:        else
                    454:            cleanfld(n+1, lastfld);
                    455:
                    456:        lastfld = n;
1.1       tholo     457: }
                    458:
1.4       kstailey  459: Cell *fieldadr(int n)  /* get nth field */
                    460: {
                    461:        if (n < 0)
1.15      millert   462:                FATAL("trying to access out of range field %d", n);
1.4       kstailey  463:        if (n > nfields)        /* fields after NF are empty */
                    464:                growfldtab(n);  /* but does not increase NF */
                    465:        return(fldtab[n]);
                    466: }
                    467:
                    468: void growfldtab(int n) /* make new fields up to at least $n */
                    469: {
                    470:        int nf = 2 * nfields;
1.15      millert   471:        size_t s;
1.4       kstailey  472:
                    473:        if (n > nf)
                    474:                nf = n;
1.15      millert   475:        s = (nf+1) * (sizeof (struct Cell *));  /* freebsd: how much do we need? */
1.34    ! millert   476:        if (s / sizeof(struct Cell *) - 1 == (size_t)nf) /* didn't overflow */
1.31      millert   477:                fldtab = realloc(fldtab, s);
1.15      millert   478:        else                                    /* overflow sizeof int */
                    479:                xfree(fldtab);  /* make it null */
1.4       kstailey  480:        if (fldtab == NULL)
1.7       millert   481:                FATAL("out of space creating %d fields", nf);
1.4       kstailey  482:        makefields(nfields+1, nf);
                    483:        nfields = nf;
                    484: }
                    485:
1.11      millert   486: int refldbld(const char *rec, const char *fs)  /* build fields from reg expr in FS */
1.1       tholo     487: {
1.4       kstailey  488:        /* this relies on having fields[] the same length as $0 */
                    489:        /* the fields are all stored in this one array with \0's */
1.1       tholo     490:        char *fr;
1.4       kstailey  491:        int i, tempstat, n;
1.1       tholo     492:        fa *pfa;
                    493:
1.4       kstailey  494:        n = strlen(rec);
                    495:        if (n > fieldssize) {
                    496:                xfree(fields);
1.31      millert   497:                if ((fields = malloc(n+1)) == NULL)
1.7       millert   498:                        FATAL("out of space for fields in refldbld %d", n);
1.4       kstailey  499:                fieldssize = n;
                    500:        }
1.1       tholo     501:        fr = fields;
                    502:        *fr = '\0';
                    503:        if (*rec == '\0')
                    504:                return 0;
                    505:        pfa = makedfa(fs, 1);
1.24      deraadt   506:           DPRINTF( ("into refldbld, rec = <%s>, pat = <%s>\n", rec, fs) );
1.1       tholo     507:        tempstat = pfa->initstat;
1.4       kstailey  508:        for (i = 1; ; i++) {
                    509:                if (i > nfields)
                    510:                        growfldtab(i);
                    511:                if (freeable(fldtab[i]))
                    512:                        xfree(fldtab[i]->sval);
                    513:                fldtab[i]->tval = FLD | STR | DONTFREE;
                    514:                fldtab[i]->sval = fr;
1.24      deraadt   515:                   DPRINTF( ("refldbld: i=%d\n", i) );
1.1       tholo     516:                if (nematch(pfa, rec)) {
1.4       kstailey  517:                        pfa->initstat = 2;      /* horrible coupling to b.c */
1.24      deraadt   518:                           DPRINTF( ("match %s (%d chars)\n", patbeg, patlen) );
1.1       tholo     519:                        strncpy(fr, rec, patbeg-rec);
                    520:                        fr += patbeg - rec + 1;
                    521:                        *(fr-1) = '\0';
                    522:                        rec = patbeg + patlen;
                    523:                } else {
1.24      deraadt   524:                           DPRINTF( ("no match %s\n", rec) );
1.13      tedu      525:                        strlcpy(fr, rec, fields + fieldssize - fr);
1.1       tholo     526:                        pfa->initstat = tempstat;
                    527:                        break;
                    528:                }
                    529:        }
1.29      millert   530:        return i;
1.1       tholo     531: }
                    532:
                    533: void recbld(void)      /* create $0 from $1..$NF if necessary */
                    534: {
                    535:        int i;
                    536:        char *r, *p;
1.27      millert   537:        char *sep = getsval(ofsloc);
1.1       tholo     538:
1.32      millert   539:        if (donerec)
1.1       tholo     540:                return;
1.5       millert   541:        r = record;
1.1       tholo     542:        for (i = 1; i <= *NF; i++) {
1.4       kstailey  543:                p = getsval(fldtab[i]);
1.5       millert   544:                if (!adjbuf(&record, &recsize, 1+strlen(p)+r-record, recsize, &r, "recbld 1"))
1.7       millert   545:                        FATAL("created $0 `%.30s...' too long", record);
1.4       kstailey  546:                while ((*r = *p++) != 0)
1.1       tholo     547:                        r++;
1.4       kstailey  548:                if (i < *NF) {
1.27      millert   549:                        if (!adjbuf(&record, &recsize, 2+strlen(sep)+r-record, recsize, &r, "recbld 2"))
1.7       millert   550:                                FATAL("created $0 `%.30s...' too long", record);
1.27      millert   551:                        for (p = sep; (*r = *p++) != 0; )
1.1       tholo     552:                                r++;
1.4       kstailey  553:                }
1.1       tholo     554:        }
1.5       millert   555:        if (!adjbuf(&record, &recsize, 2+r-record, recsize, &r, "recbld 3"))
1.7       millert   556:                FATAL("built giant record `%.30s...'", record);
1.1       tholo     557:        *r = '\0';
1.24      deraadt   558:           DPRINTF( ("in recbld inputFS=%s, fldtab[0]=%p\n", inputFS, (void*)fldtab[0]) );
1.4       kstailey  559:
                    560:        if (freeable(fldtab[0]))
                    561:                xfree(fldtab[0]->sval);
                    562:        fldtab[0]->tval = REC | STR | DONTFREE;
                    563:        fldtab[0]->sval = record;
                    564:
1.24      deraadt   565:           DPRINTF( ("in recbld inputFS=%s, fldtab[0]=%p\n", inputFS, (void*)fldtab[0]) );
                    566:           DPRINTF( ("recbld = |%s|\n", record) );
1.32      millert   567:        donerec = true;
1.1       tholo     568: }
                    569:
                    570: int    errorflag       = 0;
                    571:
1.11      millert   572: void yyerror(const char *s)
1.1       tholo     573: {
1.14      grange    574:        SYNTAX("%s", s);
1.7       millert   575: }
                    576:
1.11      millert   577: void SYNTAX(const char *fmt, ...)
1.7       millert   578: {
1.1       tholo     579:        extern char *cmdname, *curfname;
                    580:        static int been_here = 0;
1.7       millert   581:        va_list varg;
1.1       tholo     582:
                    583:        if (been_here++ > 2)
                    584:                return;
1.7       millert   585:        fprintf(stderr, "%s: ", cmdname);
                    586:        va_start(varg, fmt);
                    587:        vfprintf(stderr, fmt, varg);
                    588:        va_end(varg);
1.1       tholo     589:        fprintf(stderr, " at source line %d", lineno);
                    590:        if (curfname != NULL)
                    591:                fprintf(stderr, " in function %s", curfname);
1.32      millert   592:        if (compile_time == COMPILING && cursource() != NULL)
1.6       millert   593:                fprintf(stderr, " source file %s", cursource());
1.1       tholo     594:        fprintf(stderr, "\n");
                    595:        errorflag = 2;
                    596:        eprint();
                    597: }
                    598:
                    599: extern int bracecnt, brackcnt, parencnt;
                    600:
                    601: void bracecheck(void)
                    602: {
                    603:        int c;
                    604:        static int beenhere = 0;
                    605:
                    606:        if (beenhere++)
                    607:                return;
1.3       millert   608:        while ((c = input()) != EOF && c != '\0')
1.1       tholo     609:                bclass(c);
                    610:        bcheck2(bracecnt, '{', '}');
                    611:        bcheck2(brackcnt, '[', ']');
                    612:        bcheck2(parencnt, '(', ')');
                    613: }
                    614:
                    615: void bcheck2(int n, int c1, int c2)
                    616: {
                    617:        if (n == 1)
                    618:                fprintf(stderr, "\tmissing %c\n", c2);
                    619:        else if (n > 1)
                    620:                fprintf(stderr, "\t%d missing %c's\n", n, c2);
                    621:        else if (n == -1)
                    622:                fprintf(stderr, "\textra %c\n", c2);
                    623:        else if (n < -1)
                    624:                fprintf(stderr, "\t%d extra %c's\n", -n, c2);
                    625: }
                    626:
1.23      krw       627: __dead void FATAL(const char *fmt, ...)
1.7       millert   628: {
                    629:        extern char *cmdname;
                    630:        va_list varg;
                    631:
                    632:        fflush(stdout);
                    633:        fprintf(stderr, "%s: ", cmdname);
                    634:        va_start(varg, fmt);
                    635:        vfprintf(stderr, fmt, varg);
                    636:        va_end(varg);
                    637:        error();
                    638:        if (dbg > 1)            /* core dump if serious debugging on */
                    639:                abort();
                    640:        exit(2);
                    641: }
                    642:
1.11      millert   643: void WARNING(const char *fmt, ...)
1.1       tholo     644: {
                    645:        extern char *cmdname;
1.7       millert   646:        va_list varg;
1.1       tholo     647:
                    648:        fflush(stdout);
                    649:        fprintf(stderr, "%s: ", cmdname);
1.7       millert   650:        va_start(varg, fmt);
                    651:        vfprintf(stderr, fmt, varg);
                    652:        va_end(varg);
                    653:        error();
                    654: }
                    655:
                    656: void error()
                    657: {
                    658:        extern Node *curnode;
                    659:
1.1       tholo     660:        fprintf(stderr, "\n");
1.32      millert   661:        if (compile_time != ERROR_PRINTING) {
                    662:                if (NR && *NR > 0) {
                    663:                        fprintf(stderr, " input record number %d", (int) (*FNR));
                    664:                        if (strcmp(*FILENAME, "-") != 0)
                    665:                                fprintf(stderr, ", file %s", *FILENAME);
                    666:                        fprintf(stderr, "\n");
                    667:                }
                    668:                if (curnode)
                    669:                        fprintf(stderr, " source line number %d", curnode->lineno);
                    670:                else if (lineno)
                    671:                        fprintf(stderr, " source line number %d", lineno);
                    672:        }
                    673:
                    674:        if (compile_time == COMPILING && cursource() != NULL)
1.6       millert   675:                fprintf(stderr, " source file %s", cursource());
                    676:        fprintf(stderr, "\n");
1.1       tholo     677:        eprint();
                    678: }
                    679:
                    680: void eprint(void)      /* try to print context around error */
                    681: {
                    682:        char *p, *q;
                    683:        int c;
                    684:        static int been_here = 0;
                    685:        extern char ebuf[], *ep;
                    686:
1.32      millert   687:        if (compile_time != COMPILING || been_here++ > 0 || ebuf == ep)
1.1       tholo     688:                return;
                    689:        p = ep - 1;
                    690:        if (p > ebuf && *p == '\n')
                    691:                p--;
                    692:        for ( ; p > ebuf && *p != '\n' && *p != '\0'; p--)
                    693:                ;
                    694:        while (*p == '\n')
                    695:                p++;
                    696:        fprintf(stderr, " context is\n\t");
                    697:        for (q=ep-1; q>=p && *q!=' ' && *q!='\t' && *q!='\n'; q--)
                    698:                ;
                    699:        for ( ; p < q; p++)
                    700:                if (*p)
                    701:                        putc(*p, stderr);
                    702:        fprintf(stderr, " >>> ");
                    703:        for ( ; p < ep; p++)
                    704:                if (*p)
                    705:                        putc(*p, stderr);
                    706:        fprintf(stderr, " <<< ");
                    707:        if (*ep)
                    708:                while ((c = input()) != '\n' && c != '\0' && c != EOF) {
                    709:                        putc(c, stderr);
                    710:                        bclass(c);
                    711:                }
                    712:        putc('\n', stderr);
                    713:        ep = ebuf;
                    714: }
                    715:
                    716: void bclass(int c)
                    717: {
                    718:        switch (c) {
                    719:        case '{': bracecnt++; break;
                    720:        case '}': bracecnt--; break;
                    721:        case '[': brackcnt++; break;
                    722:        case ']': brackcnt--; break;
                    723:        case '(': parencnt++; break;
                    724:        case ')': parencnt--; break;
                    725:        }
                    726: }
                    727:
1.11      millert   728: double errcheck(double x, const char *s)
1.1       tholo     729: {
                    730:
                    731:        if (errno == EDOM) {
                    732:                errno = 0;
1.7       millert   733:                WARNING("%s argument out of domain", s);
1.1       tholo     734:                x = 1;
                    735:        } else if (errno == ERANGE) {
                    736:                errno = 0;
1.7       millert   737:                WARNING("%s result out of range", s);
1.1       tholo     738:                x = 1;
                    739:        }
                    740:        return x;
                    741: }
                    742:
1.11      millert   743: int isclvar(const char *s)     /* is s of form var=something ? */
1.1       tholo     744: {
1.11      millert   745:        const char *os = s;
1.1       tholo     746:
1.8       millert   747:        if (!isalpha((uschar) *s) && *s != '_')
1.1       tholo     748:                return 0;
                    749:        for ( ; *s; s++)
1.8       millert   750:                if (!(isalnum((uschar) *s) || *s == '_'))
1.1       tholo     751:                        break;
1.28      millert   752:        return *s == '=' && s > os;
1.1       tholo     753: }
                    754:
1.4       kstailey  755: /* strtod is supposed to be a proper test of what's a valid number */
1.8       millert   756: /* appears to be broken in gcc on linux: thinks 0x123 is a valid FP number */
                    757: /* wrong: violates 4.10.1.4 of ansi C standard */
1.1       tholo     758:
1.4       kstailey  759: #include <math.h>
1.11      millert   760: int is_number(const char *s)
1.1       tholo     761: {
1.4       kstailey  762:        double r;
                    763:        char *ep;
                    764:        errno = 0;
                    765:        r = strtod(s, &ep);
                    766:        if (ep == s || r == HUGE_VAL || errno == ERANGE)
                    767:                return 0;
                    768:        while (*ep == ' ' || *ep == '\t' || *ep == '\n')
                    769:                ep++;
                    770:        if (*ep == '\0')
                    771:                return 1;
1.1       tholo     772:        else
1.4       kstailey  773:                return 0;
1.1       tholo     774: }