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

Annotation of src/usr.bin/m4/gnum4.c, Revision 1.23

1.23    ! espie       1: /* $OpenBSD: gnum4.c,v 1.22 2003/06/30 21:42:50 espie Exp $ */
1.1       espie       2:
                      3: /*
                      4:  * Copyright (c) 1999 Marc Espie
                      5:  *
                      6:  * Redistribution and use in source and binary forms, with or without
                      7:  * modification, are permitted provided that the following conditions
                      8:  * are met:
                      9:  * 1. Redistributions of source code must retain the above copyright
                     10:  *    notice, this list of conditions and the following disclaimer.
                     11:  * 2. Redistributions in binary form must reproduce the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer in the
                     13:  *    documentation and/or other materials provided with the distribution.
                     14:  *
                     15:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     16:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     17:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     18:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     19:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     20:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     21:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     22:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     23:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     24:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     25:  * SUCH DAMAGE.
                     26:  */
                     27:
                     28: /*
                     29:  * functions needed to support gnu-m4 extensions, including a fake freezing
                     30:  */
                     31:
                     32: #include <sys/param.h>
1.5       espie      33: #include <sys/types.h>
1.8       espie      34: #include <sys/wait.h>
1.5       espie      35: #include <ctype.h>
1.8       espie      36: #include <paths.h>
1.5       espie      37: #include <regex.h>
1.1       espie      38: #include <stddef.h>
                     39: #include <stdlib.h>
                     40: #include <stdio.h>
                     41: #include <string.h>
                     42: #include <err.h>
1.8       espie      43: #include <errno.h>
                     44: #include <unistd.h>
1.1       espie      45: #include "mdef.h"
                     46: #include "stdd.h"
                     47: #include "extern.h"
                     48:
1.5       espie      49:
                     50: int mimic_gnu = 0;
                     51:
1.1       espie      52: /*
                     53:  * Support for include path search
                     54:  * First search in the the current directory.
                     55:  * If not found, and the path is not absolute, include path kicks in.
                     56:  * First, -I options, in the order found on the command line.
                     57:  * Then M4PATH env variable
                     58:  */
                     59:
                     60: struct path_entry {
                     61:        char *name;
                     62:        struct path_entry *next;
                     63: } *first, *last;
                     64:
1.16      millert    65: static struct path_entry *new_path_entry(const char *);
                     66: static void ensure_m4path(void);
                     67: static struct input_file *dopath(struct input_file *, const char *);
1.3       espie      68:
1.1       espie      69: static struct path_entry *
1.18      espie      70: new_path_entry(const char *dirname)
1.1       espie      71: {
                     72:        struct path_entry *n;
                     73:
                     74:        n = malloc(sizeof(struct path_entry));
                     75:        if (!n)
                     76:                errx(1, "out of memory");
                     77:        n->name = strdup(dirname);
                     78:        if (!n->name)
                     79:                errx(1, "out of memory");
                     80:        n->next = 0;
                     81:        return n;
                     82: }
                     83:
                     84: void
1.18      espie      85: addtoincludepath(const char *dirname)
1.1       espie      86: {
                     87:        struct path_entry *n;
                     88:
                     89:        n = new_path_entry(dirname);
                     90:
                     91:        if (last) {
                     92:                last->next = n;
                     93:                last = n;
                     94:        }
                     95:        else
                     96:                last = first = n;
                     97: }
                     98:
                     99: static void
                    100: ensure_m4path()
                    101: {
                    102:        static int envpathdone = 0;
                    103:        char *envpath;
                    104:        char *sweep;
                    105:        char *path;
                    106:
                    107:        if (envpathdone)
                    108:                return;
                    109:        envpathdone = TRUE;
                    110:        envpath = getenv("M4PATH");
                    111:        if (!envpath)
                    112:                return;
                    113:        /* for portability: getenv result is read-only */
                    114:        envpath = strdup(envpath);
                    115:        if (!envpath)
                    116:                errx(1, "out of memory");
                    117:        for (sweep = envpath;
                    118:            (path = strsep(&sweep, ":")) != NULL;)
                    119:            addtoincludepath(path);
                    120:        free(envpath);
                    121: }
                    122:
                    123: static
1.4       espie     124: struct input_file *
1.18      espie     125: dopath(struct input_file *i, const char *filename)
1.1       espie     126: {
                    127:        char path[MAXPATHLEN];
                    128:        struct path_entry *pe;
1.4       espie     129:        FILE *f;
1.1       espie     130:
                    131:        for (pe = first; pe; pe = pe->next) {
                    132:                snprintf(path, sizeof(path), "%s/%s", pe->name, filename);
1.4       espie     133:                if ((f = fopen(path, "r")) != 0) {
                    134:                        set_input(i, f, path);
                    135:                        return i;
                    136:                }
1.1       espie     137:        }
                    138:        return NULL;
                    139: }
                    140:
1.4       espie     141: struct input_file *
1.18      espie     142: fopen_trypath(struct input_file *i, const char *filename)
1.1       espie     143: {
                    144:        FILE *f;
                    145:
                    146:        f = fopen(filename, "r");
1.4       espie     147:        if (f != NULL) {
                    148:                set_input(i, f, filename);
                    149:                return i;
                    150:        }
1.1       espie     151:        if (filename[0] == '/')
                    152:                return NULL;
                    153:
                    154:        ensure_m4path();
                    155:
1.4       espie     156:        return dopath(i, filename);
1.1       espie     157: }
                    158:
1.5       espie     159: void
1.18      espie     160: doindir(const char *argv[], int argc)
1.5       espie     161: {
1.22      espie     162:        struct macro_definition *p;
1.5       espie     163:
1.22      espie     164:        p = lookup_macro_definition(argv[2]);
1.5       espie     165:        if (p == NULL)
                    166:                errx(1, "undefined macro %s", argv[2]);
                    167:        argv[1] = p->defn;
1.23    ! espie     168:
        !           169:        eval(argv+1, argc-1, p->type, traced_macros && is_traced(argv[2]));
1.5       espie     170: }
                    171:
                    172: void
1.18      espie     173: dobuiltin(const char *argv[], int argc)
1.5       espie     174: {
                    175:        int n;
                    176:        argv[1] = NULL;
                    177:        n = builtin_type(argv[2]);
                    178:        if (n != -1)
1.23    ! espie     179:                eval(argv+1, argc-1, n, traced_macros && is_traced(argv[2]));
1.5       espie     180:        else
                    181:                errx(1, "unknown builtin %s", argv[2]);
                    182: }
                    183:
                    184:
                    185: /* We need some temporary buffer space, as pb pushes BACK and substitution
                    186:  * proceeds forward... */
                    187: static char *buffer;
                    188: static size_t bufsize = 0;
                    189: static size_t current = 0;
                    190:
1.16      millert   191: static void addchars(const char *, size_t);
1.17      espie     192: static void addchar(int);
1.16      millert   193: static char *twiddle(const char *);
                    194: static char *getstring(void);
                    195: static void exit_regerror(int, regex_t *);
                    196: static void do_subst(const char *, regex_t *, const char *, regmatch_t *);
                    197: static void do_regexpindex(const char *, regex_t *, regmatch_t *);
                    198: static void do_regexp(const char *, regex_t *, const char *, regmatch_t *);
                    199: static void add_sub(int, const char *, regex_t *, regmatch_t *);
                    200: static void add_replace(const char *, regex_t *, const char *, regmatch_t *);
1.14      espie     201: #define addconstantstring(s) addchars((s), sizeof(s)-1)
1.5       espie     202:
                    203: static void
1.18      espie     204: addchars(const char *c, size_t n)
1.5       espie     205: {
                    206:        if (n == 0)
                    207:                return;
1.15      espie     208:        while (current + n > bufsize) {
1.5       espie     209:                if (bufsize == 0)
                    210:                        bufsize = 1024;
                    211:                else
                    212:                        bufsize *= 2;
                    213:                buffer = realloc(buffer, bufsize);
                    214:                if (buffer == NULL)
                    215:                        errx(1, "out of memory");
                    216:        }
                    217:        memcpy(buffer+current, c, n);
                    218:        current += n;
                    219: }
                    220:
                    221: static void
1.18      espie     222: addchar(int c)
1.5       espie     223: {
                    224:        if (current +1 > bufsize) {
                    225:                if (bufsize == 0)
                    226:                        bufsize = 1024;
                    227:                else
                    228:                        bufsize *= 2;
                    229:                buffer = realloc(buffer, bufsize);
                    230:                if (buffer == NULL)
                    231:                        errx(1, "out of memory");
                    232:        }
                    233:        buffer[current++] = c;
                    234: }
                    235:
                    236: static char *
                    237: getstring()
                    238: {
                    239:        addchar('\0');
                    240:        current = 0;
                    241:        return buffer;
                    242: }
                    243:
                    244:
                    245: static void
1.18      espie     246: exit_regerror(int er, regex_t *re)
1.5       espie     247: {
                    248:        size_t  errlen;
                    249:        char    *errbuf;
                    250:
                    251:        errlen = regerror(er, re, NULL, 0);
                    252:        errbuf = xalloc(errlen);
                    253:        regerror(er, re, errbuf, errlen);
1.19      espie     254:        errx(1, "%s at line %lu: regular expression error: %s",
                    255:            CURRENT_NAME, CURRENT_LINE, errbuf);
1.5       espie     256: }
                    257:
                    258: static void
1.18      espie     259: add_sub(int n, const char *string, regex_t *re, regmatch_t *pm)
1.5       espie     260: {
                    261:        if (n > re->re_nsub)
                    262:                warnx("No subexpression %d", n);
                    263:        /* Subexpressions that did not match are
                    264:         * not an error.  */
                    265:        else if (pm[n].rm_so != -1 &&
                    266:            pm[n].rm_eo != -1) {
                    267:                addchars(string + pm[n].rm_so,
                    268:                        pm[n].rm_eo - pm[n].rm_so);
                    269:        }
                    270: }
                    271:
                    272: /* Add replacement string to the output buffer, recognizing special
                    273:  * constructs and replacing them with substrings of the original string.
                    274:  */
                    275: static void
1.18      espie     276: add_replace(const char *string, regex_t *re, const char *replace, regmatch_t *pm)
1.5       espie     277: {
                    278:        const char *p;
                    279:
                    280:        for (p = replace; *p != '\0'; p++) {
                    281:                if (*p == '&' && !mimic_gnu) {
                    282:                        add_sub(0, string, re, pm);
                    283:                        continue;
                    284:                }
                    285:                if (*p == '\\') {
                    286:                        if (p[1] == '\\') {
                    287:                                addchar(p[1]);
1.10      espie     288:                                p++;
1.5       espie     289:                                continue;
                    290:                        }
                    291:                        if (p[1] == '&') {
                    292:                                if (mimic_gnu)
                    293:                                        add_sub(0, string, re, pm);
                    294:                                else
                    295:                                        addchar(p[1]);
                    296:                                p++;
                    297:                                continue;
                    298:                        }
                    299:                        if (isdigit(p[1])) {
                    300:                                add_sub(*(++p) - '0', string, re, pm);
                    301:                                continue;
                    302:                        }
                    303:                }
                    304:                addchar(*p);
                    305:        }
                    306: }
                    307:
                    308: static void
1.18      espie     309: do_subst(const char *string, regex_t *re, const char *replace, regmatch_t *pm)
1.5       espie     310: {
                    311:        int error;
1.11      espie     312:        int flags = 0;
                    313:        const char *last_match = NULL;
1.5       espie     314:
1.11      espie     315:        while ((error = regexec(re, string, re->re_nsub+1, pm, flags)) == 0) {
1.13      espie     316:                if (pm[0].rm_eo != 0) {
                    317:                        if (string[pm[0].rm_eo-1] == '\n')
                    318:                                flags = 0;
                    319:                        else
                    320:                                flags = REG_NOTBOL;
                    321:                }
1.5       espie     322:
                    323:                /* NULL length matches are special... We use the `vi-mode'
                    324:                 * rule: don't allow a NULL-match at the last match
                    325:                 * position.
                    326:                 */
1.11      espie     327:                if (pm[0].rm_so == pm[0].rm_eo &&
                    328:                    string + pm[0].rm_so == last_match) {
1.5       espie     329:                        if (*string == '\0')
                    330:                                return;
                    331:                        addchar(*string);
1.13      espie     332:                        if (*string++ == '\n')
                    333:                                flags = 0;
                    334:                        else
                    335:                                flags = REG_NOTBOL;
1.5       espie     336:                        continue;
                    337:                }
1.11      espie     338:                last_match = string + pm[0].rm_so;
                    339:                addchars(string, pm[0].rm_so);
1.5       espie     340:                add_replace(string, re, replace, pm);
                    341:                string += pm[0].rm_eo;
                    342:        }
                    343:        if (error != REG_NOMATCH)
                    344:                exit_regerror(error, re);
                    345:        pbstr(string);
                    346: }
                    347:
                    348: static void
1.18      espie     349: do_regexp(const char *string, regex_t *re, const char *replace, regmatch_t *pm)
1.5       espie     350: {
                    351:        int error;
                    352:
                    353:        switch(error = regexec(re, string, re->re_nsub+1, pm, 0)) {
                    354:        case 0:
                    355:                add_replace(string, re, replace, pm);
                    356:                pbstr(getstring());
                    357:                break;
                    358:        case REG_NOMATCH:
                    359:                break;
                    360:        default:
                    361:                exit_regerror(error, re);
                    362:        }
                    363: }
                    364:
                    365: static void
1.18      espie     366: do_regexpindex(const char *string, regex_t *re, regmatch_t *pm)
1.5       espie     367: {
                    368:        int error;
                    369:
                    370:        switch(error = regexec(re, string, re->re_nsub+1, pm, 0)) {
                    371:        case 0:
                    372:                pbunsigned(pm[0].rm_so);
                    373:                break;
                    374:        case REG_NOMATCH:
                    375:                pbnum(-1);
                    376:                break;
                    377:        default:
                    378:                exit_regerror(error, re);
                    379:        }
                    380: }
                    381:
                    382: /* In Gnu m4 mode, parentheses for backmatch don't work like POSIX 1003.2
                    383:  * says. So we twiddle with the regexp before passing it to regcomp.
                    384:  */
                    385: static char *
1.18      espie     386: twiddle(const char *p)
1.5       espie     387: {
                    388:        /* This could use strcspn for speed... */
                    389:        while (*p != '\0') {
1.14      espie     390:                if (*p == '\\') {
                    391:                        switch(p[1]) {
                    392:                        case '(':
                    393:                        case ')':
                    394:                        case '|':
                    395:                                addchar(p[1]);
                    396:                                break;
                    397:                        case 'w':
                    398:                                addconstantstring("[_a-zA-Z0-9]");
                    399:                                break;
                    400:                        case 'W':
                    401:                                addconstantstring("[^_a-zA-Z0-9]");
                    402:                                break;
                    403:                        case '<':
                    404:                                addconstantstring("[[:<:]]");
                    405:                                break;
                    406:                        case '>':
                    407:                                addconstantstring("[[:>:]]");
                    408:                                break;
                    409:                        default:
                    410:                                addchars(p, 2);
                    411:                                break;
                    412:                        }
1.5       espie     413:                        p+=2;
                    414:                        continue;
                    415:                }
1.14      espie     416:                if (*p == '(' || *p == ')' || *p == '|')
1.5       espie     417:                        addchar('\\');
                    418:
                    419:                addchar(*p);
                    420:                p++;
                    421:        }
                    422:        return getstring();
                    423: }
                    424:
                    425: /* patsubst(string, regexp, opt replacement) */
                    426: /* argv[2]: string
                    427:  * argv[3]: regexp
                    428:  * argv[4]: opt rep
                    429:  */
                    430: void
1.18      espie     431: dopatsubst(const char *argv[], int argc)
1.5       espie     432: {
                    433:        if (argc <= 3) {
                    434:                warnx("Too few arguments to patsubst");
                    435:                return;
                    436:        }
1.20      espie     437:        /* special case: empty regexp */
                    438:        if (argv[3][0] == '\0') {
                    439:                const char *s;
1.21      espie     440:                size_t len;
                    441:                if (argv[4] && argc > 4)
                    442:                        len = strlen(argv[4]);
                    443:                else
                    444:                        len = 0;
1.20      espie     445:                for (s = argv[2]; *s != '\0'; s++) {
                    446:                        addchars(argv[4], len);
                    447:                        addchar(*s);
                    448:                }
                    449:        } else {
                    450:                int error;
                    451:                regex_t re;
                    452:                regmatch_t *pmatch;
                    453:
                    454:                error = regcomp(&re, mimic_gnu ? twiddle(argv[3]) : argv[3],
                    455:                    REG_NEWLINE | REG_EXTENDED);
                    456:                if (error != 0)
                    457:                        exit_regerror(error, &re);
                    458:
                    459:                pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1));
                    460:                do_subst(argv[2], &re,
1.21      espie     461:                    argc > 4 && argv[4] != NULL ? argv[4] : "", pmatch);
1.20      espie     462:                free(pmatch);
                    463:                regfree(&re);
                    464:        }
1.5       espie     465:        pbstr(getstring());
                    466: }
                    467:
                    468: void
1.18      espie     469: doregexp(const char *argv[], int argc)
1.5       espie     470: {
                    471:        int error;
                    472:        regex_t re;
                    473:        regmatch_t *pmatch;
                    474:
                    475:        if (argc <= 3) {
1.7       espie     476:                warnx("Too few arguments to regexp");
1.5       espie     477:                return;
                    478:        }
                    479:        error = regcomp(&re, mimic_gnu ? twiddle(argv[3]) : argv[3],
                    480:            REG_EXTENDED);
                    481:        if (error != 0)
                    482:                exit_regerror(error, &re);
                    483:
                    484:        pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1));
1.7       espie     485:        if (argv[4] == NULL || argc == 4)
1.5       espie     486:                do_regexpindex(argv[2], &re, pmatch);
                    487:        else
                    488:                do_regexp(argv[2], &re, argv[4], pmatch);
                    489:        free(pmatch);
                    490:        regfree(&re);
1.8       espie     491: }
                    492:
                    493: void
1.18      espie     494: doesyscmd(const char *cmd)
1.8       espie     495: {
                    496:        int p[2];
                    497:        pid_t pid, cpid;
                    498:        char *argv[4];
                    499:        int cc;
                    500:        int status;
                    501:
                    502:        /* Follow gnu m4 documentation: first flush buffers. */
                    503:        fflush(NULL);
                    504:
                    505:        argv[0] = "sh";
                    506:        argv[1] = "-c";
                    507:        argv[2] = (char *)cmd;
                    508:        argv[3] = NULL;
                    509:
                    510:        /* Just set up standard output, share stderr and stdin with m4 */
                    511:        if (pipe(p) == -1)
                    512:                err(1, "bad pipe");
                    513:        switch(cpid = fork()) {
                    514:        case -1:
                    515:                err(1, "bad fork");
                    516:                /* NOTREACHED */
                    517:        case 0:
                    518:                (void) close(p[0]);
                    519:                (void) dup2(p[1], 1);
                    520:                (void) close(p[1]);
                    521:                execv(_PATH_BSHELL, argv);
                    522:                exit(1);
                    523:        default:
                    524:                /* Read result in two stages, since m4's buffer is
                    525:                 * pushback-only. */
                    526:                (void) close(p[1]);
                    527:                do {
                    528:                        char result[BUFSIZE];
                    529:                        cc = read(p[0], result, sizeof result);
                    530:                        if (cc > 0)
                    531:                                addchars(result, cc);
                    532:                } while (cc > 0 || (cc == -1 && errno == EINTR));
                    533:
                    534:                (void) close(p[0]);
                    535:                while ((pid = wait(&status)) != cpid && pid >= 0)
                    536:                        continue;
                    537:                pbstr(getstring());
                    538:        }
1.5       espie     539: }