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

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