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

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