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

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