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

Annotation of src/usr.bin/sed/compile.c, Revision 1.27

1.27    ! millert     1: /*     $OpenBSD: compile.c,v 1.26 2008/10/09 16:40:56 millert Exp $    */
1.2       deraadt     2:
1.1       deraadt     3: /*-
                      4:  * Copyright (c) 1992 Diomidis Spinellis.
                      5:  * Copyright (c) 1992, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Diomidis Spinellis of Imperial College, University of London.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
1.14      millert    19:  * 3. Neither the name of the University nor the names of its contributors
1.1       deraadt    20:  *    may be used to endorse or promote products derived from this software
                     21:  *    without specific prior written permission.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     24:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     25:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     27:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #ifndef lint
1.9       millert    37: /* from: static char sccsid[] = "@(#)compile.c 8.2 (Berkeley) 4/28/95"; */
1.27    ! millert    38: static const char rcsid[] = "$OpenBSD: compile.c,v 1.26 2008/10/09 16:40:56 millert Exp $";
1.1       deraadt    39: #endif /* not lint */
                     40:
                     41: #include <sys/types.h>
                     42: #include <sys/stat.h>
                     43:
                     44: #include <ctype.h>
                     45: #include <errno.h>
                     46: #include <fcntl.h>
                     47: #include <limits.h>
                     48: #include <regex.h>
                     49: #include <stdio.h>
                     50: #include <stdlib.h>
                     51: #include <string.h>
                     52:
                     53: #include "defs.h"
                     54: #include "extern.h"
                     55:
                     56: #define LHSZ   128
                     57: #define        LHMASK  (LHSZ - 1)
                     58: static struct labhash {
                     59:        struct  labhash *lh_next;
                     60:        u_int   lh_hash;
                     61:        struct  s_command *lh_cmd;
                     62:        int     lh_ref;
                     63: } *labels[LHSZ];
                     64:
1.11      millert    65: static char     *compile_addr(char *, struct s_addr *);
                     66: static char     *compile_ccl(char **, char *);
                     67: static char     *compile_delimited(char *, char *);
                     68: static char     *compile_flags(char *, struct s_subst *);
                     69: static char     *compile_re(char *, regex_t **);
                     70: static char     *compile_subst(char *, struct s_subst *);
                     71: static char     *compile_text(void);
                     72: static char     *compile_tr(char *, char **);
1.1       deraadt    73: static struct s_command
1.11      millert    74:                **compile_stream(struct s_command **);
1.16      deraadt    75: static char     *duptoeol(char *, char *, char **);
1.11      millert    76: static void      enterlabel(struct s_command *);
1.1       deraadt    77: static struct s_command
1.11      millert    78:                 *findlabel(char *);
                     79: static void      fixuplabel(struct s_command *, struct s_command *);
                     80: static void      uselabel(void);
1.1       deraadt    81:
                     82: /*
                     83:  * Command specification.  This is used to drive the command parser.
                     84:  */
                     85: struct s_format {
                     86:        char code;                              /* Command code */
                     87:        int naddr;                              /* Number of address args */
                     88:        enum e_args args;                       /* Argument type */
                     89: };
                     90:
                     91: static struct s_format cmd_fmts[] = {
                     92:        {'{', 2, GROUP},
                     93:        {'}', 0, ENDGROUP},
                     94:        {'a', 1, TEXT},
                     95:        {'b', 2, BRANCH},
                     96:        {'c', 2, TEXT},
                     97:        {'d', 2, EMPTY},
                     98:        {'D', 2, EMPTY},
                     99:        {'g', 2, EMPTY},
                    100:        {'G', 2, EMPTY},
                    101:        {'h', 2, EMPTY},
                    102:        {'H', 2, EMPTY},
                    103:        {'i', 1, TEXT},
                    104:        {'l', 2, EMPTY},
                    105:        {'n', 2, EMPTY},
                    106:        {'N', 2, EMPTY},
                    107:        {'p', 2, EMPTY},
                    108:        {'P', 2, EMPTY},
                    109:        {'q', 1, EMPTY},
                    110:        {'r', 1, RFILE},
                    111:        {'s', 2, SUBST},
                    112:        {'t', 2, BRANCH},
                    113:        {'w', 2, WFILE},
                    114:        {'x', 2, EMPTY},
                    115:        {'y', 2, TR},
                    116:        {'!', 2, NONSEL},
                    117:        {':', 0, LABEL},
                    118:        {'#', 0, COMMENT},
                    119:        {'=', 1, EMPTY},
                    120:        {'\0', 0, COMMENT},
                    121: };
                    122:
                    123: /* The compiled program. */
                    124: struct s_command *prog;
                    125:
                    126: /*
                    127:  * Compile the program into prog.
                    128:  * Initialise appends.
                    129:  */
                    130: void
1.15      deraadt   131: compile(void)
1.1       deraadt   132: {
                    133:        *compile_stream(&prog) = NULL;
                    134:        fixuplabel(prog, NULL);
                    135:        uselabel();
                    136:        appends = xmalloc(sizeof(struct s_appends) * appendnum);
                    137:        match = xmalloc((maxnsub + 1) * sizeof(regmatch_t));
                    138: }
                    139:
                    140: #define EATSPACE() do {                                                        \
                    141:        if (p)                                                          \
1.24      tedu      142:                while (isascii(*p) && isspace(*p))                      \
1.1       deraadt   143:                        p++;                                            \
                    144:        } while (0)
                    145:
                    146: static struct s_command **
1.15      deraadt   147: compile_stream(struct s_command **link)
1.1       deraadt   148: {
1.10      mpech     149:        char *p;
1.25      millert   150:        static char *lbuf;      /* To avoid excessive malloc calls */
                    151:        static size_t len = _POSIX2_LINE_MAX;
1.1       deraadt   152:        struct s_command *cmd, *cmd2, *stack;
                    153:        struct s_format *fp;
                    154:        int naddr;                              /* Number of addresses */
                    155:
                    156:        stack = 0;
1.25      millert   157:        if (!lbuf)
                    158:                lbuf = xmalloc(len);
1.1       deraadt   159:        for (;;) {
1.25      millert   160:                if ((p = cu_fgets(&lbuf, &len)) == NULL) {
1.1       deraadt   161:                        if (stack != 0)
                    162:                                err(COMPILE, "unexpected EOF (pending }'s)");
                    163:                        return (link);
                    164:                }
                    165:
                    166: semicolon:     EATSPACE();
1.23      otto      167:                if (*p == '#' || *p == '\0')
1.1       deraadt   168:                        continue;
1.23      otto      169:                if (*p == ';') {
                    170:                        p++;
                    171:                        goto semicolon;
                    172:                }
1.1       deraadt   173:                *link = cmd = xmalloc(sizeof(struct s_command));
                    174:                link = &cmd->next;
                    175:                cmd->nonsel = cmd->inrange = 0;
                    176:                /* First parse the addresses */
                    177:                naddr = 0;
                    178:
                    179: /* Valid characters to start an address */
                    180: #define        addrchar(c)     (strchr("0123456789/\\$", (c)))
                    181:                if (addrchar(*p)) {
                    182:                        naddr++;
                    183:                        cmd->a1 = xmalloc(sizeof(struct s_addr));
                    184:                        p = compile_addr(p, cmd->a1);
                    185:                        EATSPACE();                             /* EXTENSION */
                    186:                        if (*p == ',') {
                    187:                                p++;
                    188:                                EATSPACE();                     /* EXTENSION */
                    189:                                naddr++;
                    190:                                cmd->a2 = xmalloc(sizeof(struct s_addr));
                    191:                                p = compile_addr(p, cmd->a2);
                    192:                                EATSPACE();
1.22      tedu      193:                        } else {
1.1       deraadt   194:                                cmd->a2 = 0;
1.22      tedu      195:                        }
                    196:                } else {
1.1       deraadt   197:                        cmd->a1 = cmd->a2 = 0;
1.22      tedu      198:                }
1.1       deraadt   199:
                    200: nonsel:                /* Now parse the command */
                    201:                if (!*p)
                    202:                        err(COMPILE, "command expected");
                    203:                cmd->code = *p;
                    204:                for (fp = cmd_fmts; fp->code; fp++)
                    205:                        if (fp->code == *p)
                    206:                                break;
                    207:                if (!fp->code)
                    208:                        err(COMPILE, "invalid command code %c", *p);
                    209:                if (naddr > fp->naddr)
                    210:                        err(COMPILE,
1.22      tedu      211:                            "command %c expects up to %d address(es), found %d",
                    212:                            *p, fp->naddr, naddr);
1.1       deraadt   213:                switch (fp->args) {
                    214:                case NONSEL:                    /* ! */
                    215:                        p++;
                    216:                        EATSPACE();
                    217:                        cmd->nonsel = ! cmd->nonsel;
                    218:                        goto nonsel;
                    219:                case GROUP:                     /* { */
                    220:                        p++;
                    221:                        EATSPACE();
                    222:                        cmd->next = stack;
                    223:                        stack = cmd;
                    224:                        link = &cmd->u.c;
                    225:                        if (*p)
                    226:                                goto semicolon;
                    227:                        break;
                    228:                case ENDGROUP:
                    229:                        /*
                    230:                         * Short-circuit command processing, since end of
                    231:                         * group is really just a noop.
                    232:                         */
                    233:                        cmd->nonsel = 1;
                    234:                        if (stack == 0)
                    235:                                err(COMPILE, "unexpected }");
                    236:                        cmd2 = stack;
                    237:                        stack = cmd2->next;
                    238:                        cmd2->next = cmd;
                    239:                        /*FALLTHROUGH*/
                    240:                case EMPTY:             /* d D g G h H l n N p P q x = \0 */
                    241:                        p++;
                    242:                        EATSPACE();
                    243:                        if (*p == ';') {
                    244:                                p++;
                    245:                                link = &cmd->next;
                    246:                                goto semicolon;
                    247:                        }
                    248:                        if (*p)
                    249:                                err(COMPILE,
                    250: "extra characters at the end of %c command", cmd->code);
                    251:                        break;
                    252:                case TEXT:                      /* a c i */
                    253:                        p++;
                    254:                        EATSPACE();
                    255:                        if (*p != '\\')
1.22      tedu      256:                                err(COMPILE, "command %c expects \\ followed by"
                    257:                                    " text", cmd->code);
1.1       deraadt   258:                        p++;
                    259:                        EATSPACE();
                    260:                        if (*p)
1.22      tedu      261:                                err(COMPILE, "extra characters after \\ at the"
                    262:                                    " end of %c command", cmd->code);
1.1       deraadt   263:                        cmd->t = compile_text();
                    264:                        break;
                    265:                case COMMENT:                   /* \0 # */
                    266:                        break;
                    267:                case WFILE:                     /* w */
                    268:                        p++;
                    269:                        EATSPACE();
                    270:                        if (*p == '\0')
                    271:                                err(COMPILE, "filename expected");
1.16      deraadt   272:                        cmd->t = duptoeol(p, "w command", NULL);
1.1       deraadt   273:                        if (aflag)
                    274:                                cmd->u.fd = -1;
1.21      deraadt   275:                        else if ((cmd->u.fd = open(p,
1.1       deraadt   276:                            O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
                    277:                            DEFFILEMODE)) == -1)
1.12      jsyn      278:                                err(FATAL, "%s: %s", p, strerror(errno));
1.1       deraadt   279:                        break;
                    280:                case RFILE:                     /* r */
                    281:                        p++;
                    282:                        EATSPACE();
1.16      deraadt   283:                        cmd->t = duptoeol(p, "read command", NULL);
1.1       deraadt   284:                        break;
                    285:                case BRANCH:                    /* b t */
                    286:                        p++;
                    287:                        EATSPACE();
                    288:                        if (*p == '\0')
                    289:                                cmd->t = NULL;
                    290:                        else
1.16      deraadt   291:                                cmd->t = duptoeol(p, "branch", &p);
                    292:                        if (*p == ';') {
                    293:                                p++;
                    294:                                goto semicolon;
                    295:                        }
1.1       deraadt   296:                        break;
                    297:                case LABEL:                     /* : */
                    298:                        p++;
                    299:                        EATSPACE();
1.16      deraadt   300:                        cmd->t = duptoeol(p, "label", &p);
                    301:                        if (strlen(cmd->t) == 0)
1.1       deraadt   302:                                err(COMPILE, "empty label");
                    303:                        enterlabel(cmd);
1.16      deraadt   304:                        if (*p == ';') {
                    305:                                p++;
                    306:                                goto semicolon;
                    307:                        }
1.1       deraadt   308:                        break;
                    309:                case SUBST:                     /* s */
                    310:                        p++;
                    311:                        if (*p == '\0' || *p == '\\')
1.22      tedu      312:                                err(COMPILE, "substitute pattern can not be"
                    313:                                    " delimited by newline or backslash");
1.1       deraadt   314:                        cmd->u.s = xmalloc(sizeof(struct s_subst));
                    315:                        p = compile_re(p, &cmd->u.s->re);
                    316:                        if (p == NULL)
                    317:                                err(COMPILE, "unterminated substitute pattern");
                    318:                        --p;
                    319:                        p = compile_subst(p, cmd->u.s);
                    320:                        p = compile_flags(p, cmd->u.s);
                    321:                        EATSPACE();
                    322:                        if (*p == ';') {
                    323:                                p++;
                    324:                                link = &cmd->next;
                    325:                                goto semicolon;
                    326:                        }
                    327:                        break;
                    328:                case TR:                        /* y */
                    329:                        p++;
                    330:                        p = compile_tr(p, (char **)&cmd->u.y);
                    331:                        EATSPACE();
                    332:                        if (*p == ';') {
                    333:                                p++;
                    334:                                link = &cmd->next;
                    335:                                goto semicolon;
                    336:                        }
                    337:                        if (*p)
1.22      tedu      338:                                err(COMPILE, "extra text at the end of a"
                    339:                                    " transform command");
1.1       deraadt   340:                        break;
                    341:                }
                    342:        }
                    343: }
                    344:
                    345: /*
                    346:  * Get a delimited string.  P points to the delimeter of the string; d points
                    347:  * to a buffer area.  Newline and delimiter escapes are processed; other
                    348:  * escapes are ignored.
                    349:  *
                    350:  * Returns a pointer to the first character after the final delimiter or NULL
                    351:  * in the case of a non-terminated string.  The character array d is filled
                    352:  * with the processed string.
                    353:  */
                    354: static char *
1.15      deraadt   355: compile_delimited(char *p, char *d)
1.1       deraadt   356: {
                    357:        char c;
                    358:
                    359:        c = *p++;
                    360:        if (c == '\0')
                    361:                return (NULL);
                    362:        else if (c == '\\')
                    363:                err(COMPILE, "\\ can not be used as a string delimiter");
                    364:        else if (c == '\n')
                    365:                err(COMPILE, "newline can not be used as a string delimiter");
                    366:        while (*p) {
                    367:                if (*p == '[') {
                    368:                        if ((d = compile_ccl(&p, d)) == NULL)
                    369:                                err(COMPILE, "unbalanced brackets ([])");
                    370:                        continue;
                    371:                } else if (*p == '\\' && p[1] == '[') {
                    372:                        *d++ = *p++;
1.22      tedu      373:                } else if (*p == '\\' && p[1] == c) {
1.1       deraadt   374:                        p++;
1.22      tedu      375:                } else if (*p == '\\' && p[1] == 'n') {
1.1       deraadt   376:                        *d++ = '\n';
                    377:                        p += 2;
                    378:                        continue;
1.22      tedu      379:                } else if (*p == '\\' && p[1] == '\\') {
1.1       deraadt   380:                        *d++ = *p++;
1.22      tedu      381:                } else if (*p == c) {
1.1       deraadt   382:                        *d = '\0';
                    383:                        return (p + 1);
                    384:                }
                    385:                *d++ = *p++;
                    386:        }
                    387:        return (NULL);
                    388: }
                    389:
                    390:
                    391: /* compile_ccl: expand a POSIX character class */
                    392: static char *
1.15      deraadt   393: compile_ccl(char **sp, char *t)
1.1       deraadt   394: {
                    395:        int c, d;
                    396:        char *s = *sp;
                    397:
                    398:        *t++ = *s++;
                    399:        if (*s == '^')
                    400:                *t++ = *s++;
                    401:        if (*s == ']')
                    402:                *t++ = *s++;
                    403:        for (; *s && (*t = *s) != ']'; s++, t++)
                    404:                if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) {
                    405:                        *++t = *++s, t++, s++;
                    406:                        for (c = *s; (*t = *s) != ']' || c != d; s++, t++)
                    407:                                if ((c = *s) == '\0')
                    408:                                        return NULL;
1.22      tedu      409:                } else if (*s == '\\' && s[1] == 'n') {
                    410:                        *t = '\n';
                    411:                        s++;
                    412:                }
                    413:        if (*s == ']') {
                    414:                *sp = ++s;
                    415:                return (++t);
                    416:        } else {
                    417:                return (NULL);
                    418:        }
1.1       deraadt   419: }
                    420:
                    421: /*
                    422:  * Get a regular expression.  P points to the delimiter of the regular
                    423:  * expression; repp points to the address of a regexp pointer.  Newline
                    424:  * and delimiter escapes are processed; other escapes are ignored.
                    425:  * Returns a pointer to the first character after the final delimiter
                    426:  * or NULL in the case of a non terminated regular expression.  The regexp
                    427:  * pointer is set to the compiled regular expression.
                    428:  * Cflags are passed to regcomp.
                    429:  */
                    430: static char *
1.15      deraadt   431: compile_re(char *p, regex_t **repp)
1.1       deraadt   432: {
                    433:        int eval;
1.25      millert   434:        char *re;
1.1       deraadt   435:
1.25      millert   436:        re = xmalloc(strlen(p) + 1); /* strlen(re) <= strlen(p) */
1.1       deraadt   437:        p = compile_delimited(p, re);
                    438:        if (p && strlen(re) == 0) {
                    439:                *repp = NULL;
1.25      millert   440:                free(re);
1.1       deraadt   441:                return (p);
                    442:        }
                    443:        *repp = xmalloc(sizeof(regex_t));
1.6       millert   444:        if (p && (eval = regcomp(*repp, re, 0)) != 0)
1.1       deraadt   445:                err(COMPILE, "RE error: %s", strregerror(eval, *repp));
                    446:        if (maxnsub < (*repp)->re_nsub)
                    447:                maxnsub = (*repp)->re_nsub;
1.25      millert   448:        free(re);
1.1       deraadt   449:        return (p);
                    450: }
                    451:
                    452: /*
                    453:  * Compile the substitution string of a regular expression and set res to
                    454:  * point to a saved copy of it.  Nsub is the number of parenthesized regular
                    455:  * expressions.
                    456:  */
                    457: static char *
1.15      deraadt   458: compile_subst(char *p, struct s_subst *s)
1.1       deraadt   459: {
1.25      millert   460:        static char *lbuf;
1.26      millert   461:        static size_t len;
1.1       deraadt   462:        int asize, ref, size;
                    463:        char c, *text, *op, *sp;
1.19      otto      464:        int sawesc = 0;
1.1       deraadt   465:
                    466:        c = *p++;                       /* Terminator character */
                    467:        if (c == '\0')
                    468:                return (NULL);
                    469:
1.26      millert   470:        len = strlen(p);
                    471:        if (len < _POSIX2_LINE_MAX)
                    472:                len = _POSIX2_LINE_MAX;
1.27    ! millert   473:        if (!lbuf)
        !           474:                lbuf = xmalloc(len);
        !           475:
1.1       deraadt   476:        s->maxbref = 0;
                    477:        s->linenum = linenum;
1.25      millert   478:        asize = 2 * len + 1;
1.1       deraadt   479:        text = xmalloc(asize);
                    480:        size = 0;
                    481:        do {
1.25      millert   482:                if (asize - size < len + 1) {
                    483:                        do {
                    484:                                asize *= 2;
                    485:                        } while (asize - size < len + 1);
                    486:                        text = xrealloc(text, asize);
                    487:                }
1.1       deraadt   488:                op = sp = text + size;
                    489:                for (; *p; p++) {
1.19      otto      490:                        if (*p == '\\' || sawesc) {
                    491:                                /*
                    492:                                 * If this is a continuation from the last
                    493:                                 * buffer, we won't have a character to
                    494:                                 * skip over.
                    495:                                 */
                    496:                                if (sawesc)
                    497:                                        sawesc = 0;
                    498:                                else
                    499:                                        p++;
                    500:
                    501:                                if (*p == '\0') {
                    502:                                        /*
                    503:                                         * This escaped character is continued
                    504:                                         * in the next part of the line.  Note
                    505:                                         * this fact, then cause the loop to
                    506:                                         * exit w/ normal EOL case and reenter
                    507:                                         * above with the new buffer.
                    508:                                         */
                    509:                                        sawesc = 1;
                    510:                                        p--;
                    511:                                        continue;
                    512:                                } else if (strchr("123456789", *p) != NULL) {
1.1       deraadt   513:                                        *sp++ = '\\';
                    514:                                        ref = *p - '0';
                    515:                                        if (s->re != NULL &&
                    516:                                            ref > s->re->re_nsub)
                    517:                                                err(COMPILE,
                    518: "\\%c not defined in the RE", *p);
                    519:                                        if (s->maxbref < ref)
                    520:                                                s->maxbref = ref;
                    521:                                } else if (*p == '&' || *p == '\\')
                    522:                                        *sp++ = '\\';
                    523:                        } else if (*p == c) {
                    524:                                p++;
                    525:                                *sp++ = '\0';
                    526:                                size += sp - op;
                    527:                                s->new = xrealloc(text, size);
                    528:                                return (p);
                    529:                        } else if (*p == '\n') {
                    530:                                err(COMPILE,
                    531: "unescaped newline inside substitute pattern");
                    532:                                /* NOTREACHED */
                    533:                        }
                    534:                        *sp++ = *p;
                    535:                }
                    536:                size += sp - op;
1.25      millert   537:        } while ((p = cu_fgets(&lbuf, &len)));
1.1       deraadt   538:        err(COMPILE, "unterminated substitute in regular expression");
                    539:        /* NOTREACHED */
                    540: }
                    541:
                    542: /*
                    543:  * Compile the flags of the s command
                    544:  */
                    545: static char *
1.15      deraadt   546: compile_flags(char *p, struct s_subst *s)
1.1       deraadt   547: {
                    548:        int gn;                 /* True if we have seen g or n */
1.17      otto      549:        long l;
1.25      millert   550:        char wfile[PATH_MAX], *q;
1.1       deraadt   551:
                    552:        s->n = 1;                               /* Default */
                    553:        s->p = 0;
                    554:        s->wfile = NULL;
                    555:        s->wfd = -1;
                    556:        for (gn = 0;;) {
                    557:                EATSPACE();                     /* EXTENSION */
                    558:                switch (*p) {
                    559:                case 'g':
                    560:                        if (gn)
1.22      tedu      561:                                err(COMPILE, "more than one number or 'g' in"
                    562:                                    " substitute flags");
1.1       deraadt   563:                        gn = 1;
                    564:                        s->n = 0;
                    565:                        break;
                    566:                case '\0':
                    567:                case '\n':
                    568:                case ';':
                    569:                        return (p);
                    570:                case 'p':
                    571:                        s->p = 1;
                    572:                        break;
                    573:                case '1': case '2': case '3':
                    574:                case '4': case '5': case '6':
                    575:                case '7': case '8': case '9':
                    576:                        if (gn)
1.22      tedu      577:                                err(COMPILE, "more than one number or 'g' in"
                    578:                                    " substitute flags");
1.1       deraadt   579:                        gn = 1;
1.17      otto      580:                        l = strtol(p, &p, 10);
                    581:                        if (l <= 0 || l >= INT_MAX)
                    582:                                err(COMPILE,
                    583:                                    "number in substitute flags out of range");
                    584:                        s->n = (int)l;
                    585:                        continue;
1.1       deraadt   586:                case 'w':
                    587:                        p++;
                    588: #ifdef HISTORIC_PRACTICE
                    589:                        if (*p != ' ') {
                    590:                                err(WARNING, "space missing before w wfile");
                    591:                                return (p);
                    592:                        }
                    593: #endif
                    594:                        EATSPACE();
                    595:                        q = wfile;
                    596:                        while (*p) {
                    597:                                if (*p == '\n')
                    598:                                        break;
                    599:                                *q++ = *p++;
                    600:                        }
                    601:                        *q = '\0';
                    602:                        if (q == wfile)
                    603:                                err(COMPILE, "no wfile specified");
                    604:                        s->wfile = strdup(wfile);
                    605:                        if (!aflag && (s->wfd = open(wfile,
                    606:                            O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
                    607:                            DEFFILEMODE)) == -1)
1.12      jsyn      608:                                err(FATAL, "%s: %s", wfile, strerror(errno));
1.1       deraadt   609:                        return (p);
                    610:                default:
                    611:                        err(COMPILE,
                    612:                            "bad flag in substitute command: '%c'", *p);
                    613:                        break;
                    614:                }
                    615:                p++;
                    616:        }
                    617: }
                    618:
                    619: /*
                    620:  * Compile a translation set of strings into a lookup table.
                    621:  */
                    622: static char *
1.15      deraadt   623: compile_tr(char *p, char **transtab)
1.1       deraadt   624: {
                    625:        int i;
                    626:        char *lt, *op, *np;
1.25      millert   627:        char *old = NULL, *new = NULL;
1.1       deraadt   628:
                    629:        if (*p == '\0' || *p == '\\')
                    630:                err(COMPILE,
                    631: "transform pattern can not be delimited by newline or backslash");
1.25      millert   632:        old = xmalloc(strlen(p) + 1);
1.1       deraadt   633:        p = compile_delimited(p, old);
                    634:        if (p == NULL) {
                    635:                err(COMPILE, "unterminated transform source string");
1.25      millert   636:                goto bad;
1.1       deraadt   637:        }
1.25      millert   638:        new = xmalloc(strlen(p) + 1);
1.1       deraadt   639:        p = compile_delimited(--p, new);
                    640:        if (p == NULL) {
                    641:                err(COMPILE, "unterminated transform target string");
1.25      millert   642:                goto bad;
1.1       deraadt   643:        }
                    644:        EATSPACE();
                    645:        if (strlen(new) != strlen(old)) {
                    646:                err(COMPILE, "transform strings are not the same length");
1.25      millert   647:                goto bad;
1.1       deraadt   648:        }
                    649:        /* We assume characters are 8 bits */
1.18      deraadt   650:        lt = xmalloc(UCHAR_MAX + 1);
1.1       deraadt   651:        for (i = 0; i <= UCHAR_MAX; i++)
                    652:                lt[i] = (char)i;
                    653:        for (op = old, np = new; *op; op++, np++)
                    654:                lt[(u_char)*op] = *np;
                    655:        *transtab = lt;
1.25      millert   656:        free(old);
                    657:        free(new);
1.1       deraadt   658:        return (p);
1.25      millert   659: bad:
                    660:        free(old);
                    661:        free(new);
                    662:        return (NULL);
1.1       deraadt   663: }
                    664:
                    665: /*
1.7       deraadt   666:  * Compile the text following an a, c, or i command.
1.1       deraadt   667:  */
                    668: static char *
1.15      deraadt   669: compile_text(void)
1.1       deraadt   670: {
1.4       deraadt   671:        int asize, esc_nl, size;
1.25      millert   672:        char *lbuf, *text, *p, *op, *s;
                    673:        size_t len = _POSIX2_LINE_MAX;
1.1       deraadt   674:
1.25      millert   675:        lbuf = xmalloc(len);
                    676:        asize = 2 * len + 1;
1.1       deraadt   677:        text = xmalloc(asize);
                    678:        size = 0;
1.25      millert   679:        while (cu_fgets(&lbuf, &len)) {
                    680:                if (asize - size < len + 1) {
                    681:                        do {
                    682:                                asize *= 2;
                    683:                        } while (asize - size < len + 1);
                    684:                        text = xrealloc(text, asize);
                    685:                }
1.1       deraadt   686:                op = s = text + size;
                    687:                p = lbuf;
                    688:                EATSPACE();
1.4       deraadt   689:                for (esc_nl = 0; *p != '\0'; p++) {
                    690:                        if (*p == '\\' && p[1] != '\0' && *++p == '\n')
                    691:                                esc_nl = 1;
1.1       deraadt   692:                        *s++ = *p;
                    693:                }
                    694:                size += s - op;
1.4       deraadt   695:                if (!esc_nl) {
1.1       deraadt   696:                        *s = '\0';
                    697:                        break;
                    698:                }
                    699:        }
1.25      millert   700:        free(lbuf);
1.8       brian     701:        text[size] = '\0';
1.1       deraadt   702:        return (xrealloc(text, size + 1));
                    703: }
                    704:
                    705: /*
                    706:  * Get an address and return a pointer to the first character after
                    707:  * it.  Fill the structure pointed to according to the address.
                    708:  */
                    709: static char *
1.15      deraadt   710: compile_addr(char *p, struct s_addr *a)
1.1       deraadt   711: {
                    712:        char *end;
                    713:
                    714:        switch (*p) {
                    715:        case '\\':                              /* Context address */
                    716:                ++p;
                    717:                /* FALLTHROUGH */
                    718:        case '/':                               /* Context address */
                    719:                p = compile_re(p, &a->u.r);
                    720:                if (p == NULL)
                    721:                        err(COMPILE, "unterminated regular expression");
                    722:                a->type = AT_RE;
                    723:                return (p);
                    724:
                    725:        case '$':                               /* Last line */
                    726:                a->type = AT_LAST;
                    727:                return (p + 1);
                    728:                                                /* Line number */
1.21      deraadt   729:        case '0': case '1': case '2': case '3': case '4':
1.1       deraadt   730:        case '5': case '6': case '7': case '8': case '9':
                    731:                a->type = AT_LINE;
1.3       millert   732:                a->u.l = strtoul(p, &end, 10);
1.1       deraadt   733:                return (end);
                    734:        default:
                    735:                err(COMPILE, "expected context address");
                    736:                return (NULL);
                    737:        }
                    738: }
                    739:
                    740: /*
                    741:  * duptoeol --
                    742:  *     Return a copy of all the characters up to \n or \0.
                    743:  */
                    744: static char *
1.16      deraadt   745: duptoeol(char *s, char *ctype, char **semi)
1.1       deraadt   746: {
                    747:        size_t len;
                    748:        int ws;
                    749:        char *start;
                    750:
                    751:        ws = 0;
1.16      deraadt   752:        if (semi) {
                    753:                for (start = s; *s != '\0' && *s != '\n' && *s != ';'; ++s)
                    754:                        ws = isspace(*s);
                    755:        } else {
                    756:                for (start = s; *s != '\0' && *s != '\n'; ++s)
                    757:                        ws = isspace(*s);
                    758:                *s = '\0';
                    759:        }
1.1       deraadt   760:        if (ws)
                    761:                err(WARNING, "whitespace after %s", ctype);
                    762:        len = s - start + 1;
1.16      deraadt   763:        if (semi)
                    764:                *semi = s;
                    765:        s = xmalloc(len);
                    766:        strlcpy(s, start, len);
                    767:        return (s);
1.1       deraadt   768: }
                    769:
                    770: /*
                    771:  * Convert goto label names to addresses, and count a and r commands, in
                    772:  * the given subset of the script.  Free the memory used by labels in b
                    773:  * and t commands (but not by :).
                    774:  *
                    775:  * TODO: Remove } nodes
                    776:  */
                    777: static void
1.15      deraadt   778: fixuplabel(struct s_command *cp, struct s_command *end)
1.1       deraadt   779: {
                    780:
                    781:        for (; cp != end; cp = cp->next)
                    782:                switch (cp->code) {
                    783:                case 'a':
                    784:                case 'r':
                    785:                        appendnum++;
                    786:                        break;
                    787:                case 'b':
                    788:                case 't':
                    789:                        /* Resolve branch target. */
                    790:                        if (cp->t == NULL) {
                    791:                                cp->u.c = NULL;
                    792:                                break;
                    793:                        }
                    794:                        if ((cp->u.c = findlabel(cp->t)) == NULL)
                    795:                                err(COMPILE2, "undefined label '%s'", cp->t);
                    796:                        free(cp->t);
                    797:                        break;
                    798:                case '{':
                    799:                        /* Do interior commands. */
                    800:                        fixuplabel(cp->u.c, cp->next);
                    801:                        break;
                    802:                }
                    803: }
                    804:
                    805: /*
                    806:  * Associate the given command label for later lookup.
                    807:  */
                    808: static void
1.15      deraadt   809: enterlabel(struct s_command *cp)
1.1       deraadt   810: {
1.10      mpech     811:        struct labhash **lhp, *lh;
                    812:        u_char *p;
                    813:        u_int h, c;
1.1       deraadt   814:
                    815:        for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++)
                    816:                h = (h << 5) + h + c;
                    817:        lhp = &labels[h & LHMASK];
                    818:        for (lh = *lhp; lh != NULL; lh = lh->lh_next)
                    819:                if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
                    820:                        err(COMPILE2, "duplicate label '%s'", cp->t);
                    821:        lh = xmalloc(sizeof *lh);
                    822:        lh->lh_next = *lhp;
                    823:        lh->lh_hash = h;
                    824:        lh->lh_cmd = cp;
                    825:        lh->lh_ref = 0;
                    826:        *lhp = lh;
                    827: }
                    828:
                    829: /*
                    830:  * Find the label contained in the command l in the command linked
                    831:  * list cp.  L is excluded from the search.  Return NULL if not found.
                    832:  */
                    833: static struct s_command *
1.15      deraadt   834: findlabel(char *name)
1.1       deraadt   835: {
1.10      mpech     836:        struct labhash *lh;
                    837:        u_char *p;
                    838:        u_int h, c;
1.1       deraadt   839:
                    840:        for (h = 0, p = (u_char *)name; (c = *p) != 0; p++)
                    841:                h = (h << 5) + h + c;
                    842:        for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) {
                    843:                if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) {
                    844:                        lh->lh_ref = 1;
                    845:                        return (lh->lh_cmd);
                    846:                }
                    847:        }
                    848:        return (NULL);
                    849: }
                    850:
1.21      deraadt   851: /*
1.1       deraadt   852:  * Warn about any unused labels.  As a side effect, release the label hash
                    853:  * table space.
                    854:  */
                    855: static void
1.15      deraadt   856: uselabel(void)
1.1       deraadt   857: {
1.10      mpech     858:        struct labhash *lh, *next;
                    859:        int i;
1.1       deraadt   860:
                    861:        for (i = 0; i < LHSZ; i++) {
                    862:                for (lh = labels[i]; lh != NULL; lh = next) {
                    863:                        next = lh->lh_next;
                    864:                        if (!lh->lh_ref)
                    865:                                err(WARNING, "unused label '%s'",
                    866:                                    lh->lh_cmd->t);
                    867:                        free(lh);
                    868:                }
                    869:        }
                    870: }