[BACK]Return to roff.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/roff.c, Revision 1.166

1.166   ! kristaps    1: /*     $Id: roff.c,v 1.165 2011/07/28 14:53:22 kristaps Exp $ */
1.1       kristaps    2: /*
1.119     schwarze    3:  * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.66      kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.106     kristaps   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.66      kristaps   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.106     kristaps   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.66      kristaps   13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   17:  */
1.66      kristaps   18: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
                     20: #endif
1.30      kristaps   21:
1.67      kristaps   22: #include <assert.h>
1.85      kristaps   23: #include <ctype.h>
1.1       kristaps   24: #include <stdlib.h>
1.67      kristaps   25: #include <string.h>
1.1       kristaps   26:
1.67      kristaps   27: #include "mandoc.h"
1.109     kristaps   28: #include "libroff.h"
1.94      kristaps   29: #include "libmandoc.h"
1.33      kristaps   30:
1.141     kristaps   31: /* Maximum number of nested if-else conditionals. */
1.82      kristaps   32: #define        RSTACK_MAX      128
                     33:
1.67      kristaps   34: enum   rofft {
1.103     kristaps   35:        ROFF_ad,
1.80      kristaps   36:        ROFF_am,
                     37:        ROFF_ami,
                     38:        ROFF_am1,
                     39:        ROFF_de,
                     40:        ROFF_dei,
                     41:        ROFF_de1,
1.83      schwarze   42:        ROFF_ds,
1.82      kristaps   43:        ROFF_el,
1.103     kristaps   44:        ROFF_hy,
1.82      kristaps   45:        ROFF_ie,
1.75      kristaps   46:        ROFF_if,
1.76      kristaps   47:        ROFF_ig,
1.123     schwarze   48:        ROFF_it,
1.103     kristaps   49:        ROFF_ne,
                     50:        ROFF_nh,
1.104     kristaps   51:        ROFF_nr,
1.124     schwarze   52:        ROFF_ns,
                     53:        ROFF_ps,
1.83      schwarze   54:        ROFF_rm,
1.105     kristaps   55:        ROFF_so,
1.124     schwarze   56:        ROFF_ta,
1.83      schwarze   57:        ROFF_tr,
1.109     kristaps   58:        ROFF_TS,
                     59:        ROFF_TE,
1.112     kristaps   60:        ROFF_T_,
1.125     kristaps   61:        ROFF_EQ,
                     62:        ROFF_EN,
1.76      kristaps   63:        ROFF_cblock,
1.141     kristaps   64:        ROFF_ccond,
1.106     kristaps   65:        ROFF_USERDEF,
1.67      kristaps   66:        ROFF_MAX
                     67: };
                     68:
1.82      kristaps   69: enum   roffrule {
                     70:        ROFFRULE_ALLOW,
                     71:        ROFFRULE_DENY
                     72: };
                     73:
1.147     kristaps   74: /*
                     75:  * A single register entity.  If "set" is zero, the value of the
                     76:  * register should be the default one, which is per-register.
                     77:  * Registers are assumed to be unsigned ints for now.
                     78:  */
                     79: struct reg {
1.166   ! kristaps   80:        int              set; /* whether set or not */
        !            81:        unsigned int     u; /* unsigned integer */
1.147     kristaps   82: };
                     83:
1.94      kristaps   84: struct roffstr {
1.166   ! kristaps   85:        char            *p;
        !            86:        size_t           sz;
        !            87: };
        !            88:
        !            89: /*
        !            90:  * A key-value string pair with lengths.
        !            91:  */
        !            92: struct roffkv {
        !            93:        struct roffstr   key;
        !            94:        struct roffstr   val;
        !            95:        struct roffkv   *next; /* next in list */
1.94      kristaps   96: };
                     97:
1.67      kristaps   98: struct roff {
1.128     kristaps   99:        struct mparse   *parse; /* parse point */
1.67      kristaps  100:        struct roffnode *last; /* leaf of stack */
1.82      kristaps  101:        enum roffrule    rstack[RSTACK_MAX]; /* stack of !`ie' rules */
                    102:        int              rstackpos; /* position in rstack */
1.147     kristaps  103:        struct reg       regs[REG__MAX];
1.166   ! kristaps  104:        struct roffkv   *strtab; /* user-defined strings & macros */
        !           105:        struct roffkv   *chrtab; /* user-defined characters */
1.106     kristaps  106:        const char      *current_string; /* value of last called user macro */
1.118     kristaps  107:        struct tbl_node *first_tbl; /* first table parsed */
                    108:        struct tbl_node *last_tbl; /* last table parsed */
                    109:        struct tbl_node *tbl; /* current table being parsed */
1.125     kristaps  110:        struct eqn_node *last_eqn; /* last equation parsed */
                    111:        struct eqn_node *first_eqn; /* first equation parsed */
                    112:        struct eqn_node *eqn; /* current equation being parsed */
1.79      kristaps  113: };
                    114:
1.67      kristaps  115: struct roffnode {
                    116:        enum rofft       tok; /* type of node */
                    117:        struct roffnode *parent; /* up one in stack */
                    118:        int              line; /* parse line */
                    119:        int              col; /* parse col */
1.106     kristaps  120:        char            *name; /* node name, e.g. macro name */
1.79      kristaps  121:        char            *end; /* end-rules: custom token */
                    122:        int              endspan; /* end-rules: next-line or infty */
1.82      kristaps  123:        enum roffrule    rule; /* current evaluation rule */
1.67      kristaps  124: };
                    125:
                    126: #define        ROFF_ARGS        struct roff *r, /* parse ctx */ \
1.72      kristaps  127:                         enum rofft tok, /* tok of macro */ \
1.67      kristaps  128:                         char **bufp, /* input buffer */ \
                    129:                         size_t *szp, /* size of input buffer */ \
                    130:                         int ln, /* parse line */ \
1.75      kristaps  131:                         int ppos, /* original pos in buffer */ \
                    132:                         int pos, /* current pos in buffer */ \
1.74      kristaps  133:                         int *offs /* reset offset of buffer data */
1.67      kristaps  134:
                    135: typedef        enum rofferr (*roffproc)(ROFF_ARGS);
                    136:
                    137: struct roffmac {
                    138:        const char      *name; /* macro name */
1.79      kristaps  139:        roffproc         proc; /* process new macro */
                    140:        roffproc         text; /* process as child text of macro */
                    141:        roffproc         sub; /* process as child of macro */
                    142:        int              flags;
                    143: #define        ROFFMAC_STRUCT  (1 << 0) /* always interpret */
1.85      kristaps  144:        struct roffmac  *next;
1.67      kristaps  145: };
                    146:
1.141     kristaps  147: struct predef {
                    148:        const char      *name; /* predefined input name */
                    149:        const char      *str; /* replacement symbol */
                    150: };
                    151:
                    152: #define        PREDEF(__name, __str) \
                    153:        { (__name), (__str) },
                    154:
1.155     kristaps  155: static enum rofft       roffhash_find(const char *, size_t);
                    156: static void             roffhash_init(void);
                    157: static void             roffnode_cleanscope(struct roff *);
                    158: static void             roffnode_pop(struct roff *);
                    159: static void             roffnode_push(struct roff *, enum rofft,
                    160:                                const char *, int, int);
1.80      kristaps  161: static enum rofferr     roff_block(ROFF_ARGS);
                    162: static enum rofferr     roff_block_text(ROFF_ARGS);
                    163: static enum rofferr     roff_block_sub(ROFF_ARGS);
                    164: static enum rofferr     roff_cblock(ROFF_ARGS);
                    165: static enum rofferr     roff_ccond(ROFF_ARGS);
1.82      kristaps  166: static enum rofferr     roff_cond(ROFF_ARGS);
                    167: static enum rofferr     roff_cond_text(ROFF_ARGS);
                    168: static enum rofferr     roff_cond_sub(ROFF_ARGS);
1.92      schwarze  169: static enum rofferr     roff_ds(ROFF_ARGS);
1.94      kristaps  170: static enum roffrule    roff_evalcond(const char *, int *);
1.155     kristaps  171: static void             roff_free1(struct roff *);
1.166   ! kristaps  172: static void             roff_freestr(struct roffkv **);
1.121     schwarze  173: static char            *roff_getname(struct roff *, char **, int, int);
1.94      kristaps  174: static const char      *roff_getstrn(const struct roff *,
                    175:                                const char *, size_t);
1.103     kristaps  176: static enum rofferr     roff_line_ignore(ROFF_ARGS);
1.89      kristaps  177: static enum rofferr     roff_nr(ROFF_ARGS);
1.156     kristaps  178: static void             roff_openeqn(struct roff *, const char *,
                    179:                                int, int, const char *);
1.155     kristaps  180: static enum rofft       roff_parse(struct roff *, const char *, int *);
                    181: static enum rofferr     roff_parsetext(char *);
1.154     kristaps  182: static void             roff_res(struct roff *,
1.142     kristaps  183:                                char **, size_t *, int, int);
1.122     schwarze  184: static enum rofferr     roff_rm(ROFF_ARGS);
1.94      kristaps  185: static void             roff_setstr(struct roff *,
1.106     kristaps  186:                                const char *, const char *, int);
1.166   ! kristaps  187: static void             roff_setstrn(struct roffkv **, const char *,
1.164     kristaps  188:                                size_t, const char *, size_t, int);
1.105     kristaps  189: static enum rofferr     roff_so(ROFF_ARGS);
1.164     kristaps  190: static enum rofferr     roff_tr(ROFF_ARGS);
1.109     kristaps  191: static enum rofferr     roff_TE(ROFF_ARGS);
                    192: static enum rofferr     roff_TS(ROFF_ARGS);
1.125     kristaps  193: static enum rofferr     roff_EQ(ROFF_ARGS);
                    194: static enum rofferr     roff_EN(ROFF_ARGS);
1.112     kristaps  195: static enum rofferr     roff_T_(ROFF_ARGS);
1.106     kristaps  196: static enum rofferr     roff_userdef(ROFF_ARGS);
1.67      kristaps  197:
1.155     kristaps  198: /* See roffhash_find() */
1.85      kristaps  199:
                    200: #define        ASCII_HI         126
                    201: #define        ASCII_LO         33
                    202: #define        HASHWIDTH       (ASCII_HI - ASCII_LO + 1)
                    203:
                    204: static struct roffmac  *hash[HASHWIDTH];
                    205:
                    206: static struct roffmac   roffs[ROFF_MAX] = {
1.103     kristaps  207:        { "ad", roff_line_ignore, NULL, NULL, 0, NULL },
1.85      kristaps  208:        { "am", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    209:        { "ami", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    210:        { "am1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    211:        { "de", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    212:        { "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    213:        { "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.92      schwarze  214:        { "ds", roff_ds, NULL, NULL, 0, NULL },
1.85      kristaps  215:        { "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
1.103     kristaps  216:        { "hy", roff_line_ignore, NULL, NULL, 0, NULL },
1.85      kristaps  217:        { "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
                    218:        { "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
                    219:        { "ig", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.123     schwarze  220:        { "it", roff_line_ignore, NULL, NULL, 0, NULL },
1.103     kristaps  221:        { "ne", roff_line_ignore, NULL, NULL, 0, NULL },
                    222:        { "nh", roff_line_ignore, NULL, NULL, 0, NULL },
1.104     kristaps  223:        { "nr", roff_nr, NULL, NULL, 0, NULL },
1.124     schwarze  224:        { "ns", roff_line_ignore, NULL, NULL, 0, NULL },
                    225:        { "ps", roff_line_ignore, NULL, NULL, 0, NULL },
1.122     schwarze  226:        { "rm", roff_rm, NULL, NULL, 0, NULL },
1.105     kristaps  227:        { "so", roff_so, NULL, NULL, 0, NULL },
1.124     schwarze  228:        { "ta", roff_line_ignore, NULL, NULL, 0, NULL },
1.164     kristaps  229:        { "tr", roff_tr, NULL, NULL, 0, NULL },
1.109     kristaps  230:        { "TS", roff_TS, NULL, NULL, 0, NULL },
                    231:        { "TE", roff_TE, NULL, NULL, 0, NULL },
1.112     kristaps  232:        { "T&", roff_T_, NULL, NULL, 0, NULL },
1.125     kristaps  233:        { "EQ", roff_EQ, NULL, NULL, 0, NULL },
                    234:        { "EN", roff_EN, NULL, NULL, 0, NULL },
1.85      kristaps  235:        { ".", roff_cblock, NULL, NULL, 0, NULL },
                    236:        { "\\}", roff_ccond, NULL, NULL, 0, NULL },
1.106     kristaps  237:        { NULL, roff_userdef, NULL, NULL, 0, NULL },
1.67      kristaps  238: };
                    239:
1.141     kristaps  240: /* Array of injected predefined strings. */
                    241: #define        PREDEFS_MAX      38
                    242: static const struct predef predefs[PREDEFS_MAX] = {
                    243: #include "predefs.in"
                    244: };
                    245:
1.155     kristaps  246: /* See roffhash_find() */
1.85      kristaps  247: #define        ROFF_HASH(p)    (p[0] - ASCII_LO)
                    248:
                    249: static void
1.155     kristaps  250: roffhash_init(void)
1.85      kristaps  251: {
                    252:        struct roffmac   *n;
                    253:        int               buc, i;
                    254:
1.106     kristaps  255:        for (i = 0; i < (int)ROFF_USERDEF; i++) {
1.85      kristaps  256:                assert(roffs[i].name[0] >= ASCII_LO);
                    257:                assert(roffs[i].name[0] <= ASCII_HI);
                    258:
                    259:                buc = ROFF_HASH(roffs[i].name);
                    260:
                    261:                if (NULL != (n = hash[buc])) {
                    262:                        for ( ; n->next; n = n->next)
                    263:                                /* Do nothing. */ ;
                    264:                        n->next = &roffs[i];
                    265:                } else
                    266:                        hash[buc] = &roffs[i];
                    267:        }
                    268: }
                    269:
1.67      kristaps  270: /*
                    271:  * Look up a roff token by its name.  Returns ROFF_MAX if no macro by
                    272:  * the nil-terminated string name could be found.
                    273:  */
                    274: static enum rofft
1.155     kristaps  275: roffhash_find(const char *p, size_t s)
1.67      kristaps  276: {
1.85      kristaps  277:        int              buc;
                    278:        struct roffmac  *n;
1.67      kristaps  279:
1.85      kristaps  280:        /*
                    281:         * libroff has an extremely simple hashtable, for the time
                    282:         * being, which simply keys on the first character, which must
                    283:         * be printable, then walks a chain.  It works well enough until
                    284:         * optimised.
                    285:         */
                    286:
                    287:        if (p[0] < ASCII_LO || p[0] > ASCII_HI)
                    288:                return(ROFF_MAX);
                    289:
                    290:        buc = ROFF_HASH(p);
                    291:
                    292:        if (NULL == (n = hash[buc]))
                    293:                return(ROFF_MAX);
                    294:        for ( ; n; n = n->next)
1.106     kristaps  295:                if (0 == strncmp(n->name, p, s) && '\0' == n->name[(int)s])
1.85      kristaps  296:                        return((enum rofft)(n - roffs));
1.67      kristaps  297:
                    298:        return(ROFF_MAX);
                    299: }
                    300:
                    301:
                    302: /*
                    303:  * Pop the current node off of the stack of roff instructions currently
                    304:  * pending.
                    305:  */
                    306: static void
                    307: roffnode_pop(struct roff *r)
                    308: {
                    309:        struct roffnode *p;
                    310:
1.75      kristaps  311:        assert(r->last);
                    312:        p = r->last;
1.82      kristaps  313:
1.75      kristaps  314:        r->last = r->last->parent;
1.106     kristaps  315:        free(p->name);
                    316:        free(p->end);
1.67      kristaps  317:        free(p);
                    318: }
                    319:
                    320:
                    321: /*
                    322:  * Push a roff node onto the instruction stack.  This must later be
                    323:  * removed with roffnode_pop().
                    324:  */
1.98      schwarze  325: static void
1.106     kristaps  326: roffnode_push(struct roff *r, enum rofft tok, const char *name,
                    327:                int line, int col)
1.67      kristaps  328: {
                    329:        struct roffnode *p;
                    330:
1.98      schwarze  331:        p = mandoc_calloc(1, sizeof(struct roffnode));
1.67      kristaps  332:        p->tok = tok;
1.106     kristaps  333:        if (name)
                    334:                p->name = mandoc_strdup(name);
1.67      kristaps  335:        p->parent = r->last;
                    336:        p->line = line;
                    337:        p->col = col;
1.79      kristaps  338:        p->rule = p->parent ? p->parent->rule : ROFFRULE_DENY;
1.67      kristaps  339:
                    340:        r->last = p;
                    341: }
                    342:
                    343:
                    344: static void
                    345: roff_free1(struct roff *r)
                    346: {
1.118     kristaps  347:        struct tbl_node *t;
1.125     kristaps  348:        struct eqn_node *e;
1.67      kristaps  349:
1.125     kristaps  350:        while (NULL != (t = r->first_tbl)) {
1.113     kristaps  351:                r->first_tbl = t->next;
                    352:                tbl_free(t);
1.109     kristaps  353:        }
                    354:
1.113     kristaps  355:        r->first_tbl = r->last_tbl = r->tbl = NULL;
                    356:
1.125     kristaps  357:        while (NULL != (e = r->first_eqn)) {
                    358:                r->first_eqn = e->next;
                    359:                eqn_free(e);
                    360:        }
                    361:
                    362:        r->first_eqn = r->last_eqn = r->eqn = NULL;
                    363:
1.67      kristaps  364:        while (r->last)
                    365:                roffnode_pop(r);
1.109     kristaps  366:
1.164     kristaps  367:        roff_freestr(&r->strtab);
                    368:        roff_freestr(&r->chrtab);
1.67      kristaps  369: }
                    370:
                    371:
                    372: void
                    373: roff_reset(struct roff *r)
                    374: {
1.143     kristaps  375:        int              i;
1.67      kristaps  376:
                    377:        roff_free1(r);
1.143     kristaps  378:
1.147     kristaps  379:        memset(&r->regs, 0, sizeof(struct reg) * REG__MAX);
                    380:
1.143     kristaps  381:        for (i = 0; i < PREDEFS_MAX; i++)
                    382:                roff_setstr(r, predefs[i].name, predefs[i].str, 0);
1.67      kristaps  383: }
                    384:
                    385:
                    386: void
                    387: roff_free(struct roff *r)
                    388: {
                    389:
                    390:        roff_free1(r);
                    391:        free(r);
                    392: }
                    393:
                    394:
                    395: struct roff *
1.147     kristaps  396: roff_alloc(struct mparse *parse)
1.67      kristaps  397: {
                    398:        struct roff     *r;
1.141     kristaps  399:        int              i;
1.67      kristaps  400:
1.98      schwarze  401:        r = mandoc_calloc(1, sizeof(struct roff));
1.128     kristaps  402:        r->parse = parse;
1.82      kristaps  403:        r->rstackpos = -1;
1.85      kristaps  404:
1.155     kristaps  405:        roffhash_init();
1.141     kristaps  406:
                    407:        for (i = 0; i < PREDEFS_MAX; i++)
                    408:                roff_setstr(r, predefs[i].name, predefs[i].str, 0);
                    409:
1.67      kristaps  410:        return(r);
                    411: }
                    412:
1.94      kristaps  413: /*
                    414:  * Pre-filter each and every line for reserved words (one beginning with
                    415:  * `\*', e.g., `\*(ab').  These must be handled before the actual line
                    416:  * is processed.
1.153     kristaps  417:  * This also checks the syntax of regular escapes.
1.154     kristaps  418:  */
                    419: static void
1.142     kristaps  420: roff_res(struct roff *r, char **bufp, size_t *szp, int ln, int pos)
1.94      kristaps  421: {
1.152     kristaps  422:        enum mandoc_esc  esc;
1.108     schwarze  423:        const char      *stesc; /* start of an escape sequence ('\\') */
                    424:        const char      *stnam; /* start of the name, after "[(*" */
                    425:        const char      *cp;    /* end of the name, e.g. before ']' */
                    426:        const char      *res;   /* the string to be substituted */
1.94      kristaps  427:        int              i, maxl;
                    428:        size_t           nsz;
                    429:        char            *n;
                    430:
1.154     kristaps  431: again:
1.108     schwarze  432:        cp = *bufp + pos;
                    433:        while (NULL != (cp = strchr(cp, '\\'))) {
                    434:                stesc = cp++;
                    435:
                    436:                /*
                    437:                 * The second character must be an asterisk.
                    438:                 * If it isn't, skip it anyway:  It is escaped,
                    439:                 * so it can't start another escape sequence.
                    440:                 */
                    441:
                    442:                if ('\0' == *cp)
1.154     kristaps  443:                        return;
1.152     kristaps  444:
                    445:                if ('*' != *cp) {
                    446:                        res = cp;
                    447:                        esc = mandoc_escape(&cp, NULL, NULL);
                    448:                        if (ESCAPE_ERROR != esc)
                    449:                                continue;
                    450:                        cp = res;
1.153     kristaps  451:                        mandoc_msg
                    452:                                (MANDOCERR_BADESCAPE, r->parse,
                    453:                                 ln, (int)(stesc - *bufp), NULL);
1.154     kristaps  454:                        return;
1.152     kristaps  455:                }
                    456:
                    457:                cp++;
1.108     schwarze  458:
                    459:                /*
                    460:                 * The third character decides the length
                    461:                 * of the name of the string.
                    462:                 * Save a pointer to the name.
                    463:                 */
                    464:
1.94      kristaps  465:                switch (*cp) {
1.108     schwarze  466:                case ('\0'):
1.154     kristaps  467:                        return;
1.94      kristaps  468:                case ('('):
                    469:                        cp++;
                    470:                        maxl = 2;
                    471:                        break;
                    472:                case ('['):
                    473:                        cp++;
                    474:                        maxl = 0;
                    475:                        break;
                    476:                default:
                    477:                        maxl = 1;
                    478:                        break;
                    479:                }
1.108     schwarze  480:                stnam = cp;
1.94      kristaps  481:
1.108     schwarze  482:                /* Advance to the end of the name. */
1.94      kristaps  483:
                    484:                for (i = 0; 0 == maxl || i < maxl; i++, cp++) {
1.153     kristaps  485:                        if ('\0' == *cp) {
                    486:                                mandoc_msg
                    487:                                        (MANDOCERR_BADESCAPE,
                    488:                                         r->parse, ln,
                    489:                                         (int)(stesc - *bufp), NULL);
1.154     kristaps  490:                                return;
1.153     kristaps  491:                        }
1.94      kristaps  492:                        if (0 == maxl && ']' == *cp)
                    493:                                break;
                    494:                }
                    495:
1.108     schwarze  496:                /*
                    497:                 * Retrieve the replacement string; if it is
                    498:                 * undefined, resume searching for escapes.
                    499:                 */
                    500:
                    501:                res = roff_getstrn(r, stnam, (size_t)i);
1.94      kristaps  502:
                    503:                if (NULL == res) {
1.153     kristaps  504:                        mandoc_msg
                    505:                                (MANDOCERR_BADESCAPE, r->parse,
                    506:                                 ln, (int)(stesc - *bufp), NULL);
1.142     kristaps  507:                        res = "";
1.94      kristaps  508:                }
                    509:
1.108     schwarze  510:                /* Replace the escape sequence by the string. */
                    511:
1.161     kristaps  512:                pos = stesc - *bufp;
1.154     kristaps  513:
1.94      kristaps  514:                nsz = *szp + strlen(res) + 1;
                    515:                n = mandoc_malloc(nsz);
                    516:
1.108     schwarze  517:                strlcpy(n, *bufp, (size_t)(stesc - *bufp + 1));
1.94      kristaps  518:                strlcat(n, res, nsz);
                    519:                strlcat(n, cp + (maxl ? 0 : 1), nsz);
                    520:
                    521:                free(*bufp);
                    522:
                    523:                *bufp = n;
                    524:                *szp = nsz;
1.154     kristaps  525:                goto again;
                    526:        }
                    527: }
                    528:
                    529: /*
                    530:  * Process text streams: convert all breakable hyphens into ASCII_HYPH.
                    531:  */
                    532: static enum rofferr
                    533: roff_parsetext(char *p)
                    534: {
1.155     kristaps  535:        char             l, r;
1.154     kristaps  536:        size_t           sz;
                    537:        const char      *start;
                    538:        enum mandoc_esc  esc;
                    539:
                    540:        start = p;
                    541:
                    542:        while ('\0' != *p) {
                    543:                sz = strcspn(p, "-\\");
                    544:                p += sz;
                    545:
1.159     kristaps  546:                if ('\0' == *p)
                    547:                        break;
                    548:
1.154     kristaps  549:                if ('\\' == *p) {
                    550:                        /* Skip over escapes. */
                    551:                        p++;
                    552:                        esc = mandoc_escape
                    553:                                ((const char **)&p, NULL, NULL);
                    554:                        if (ESCAPE_ERROR == esc)
                    555:                                break;
1.155     kristaps  556:                        continue;
1.159     kristaps  557:                } else if (p == start) {
1.158     kristaps  558:                        p++;
1.155     kristaps  559:                        continue;
1.158     kristaps  560:                }
1.155     kristaps  561:
                    562:                l = *(p - 1);
                    563:                r = *(p + 1);
                    564:                if ('\\' != l &&
                    565:                                '\t' != r && '\t' != l &&
                    566:                                ' ' != r && ' ' != l &&
                    567:                                '-' != r && '-' != l &&
                    568:                                ! isdigit((unsigned char)l) &&
1.159     kristaps  569:                                ! isdigit((unsigned char)r))
1.155     kristaps  570:                        *p = ASCII_HYPH;
                    571:                p++;
1.94      kristaps  572:        }
                    573:
1.154     kristaps  574:        return(ROFF_CONT);
1.94      kristaps  575: }
                    576:
1.67      kristaps  577: enum rofferr
1.90      kristaps  578: roff_parseln(struct roff *r, int ln, char **bufp,
                    579:                size_t *szp, int pos, int *offs)
1.67      kristaps  580: {
                    581:        enum rofft       t;
1.109     kristaps  582:        enum rofferr     e;
1.130     kristaps  583:        int              ppos, ctl;
1.79      kristaps  584:
                    585:        /*
1.94      kristaps  586:         * Run the reserved-word filter only if we have some reserved
                    587:         * words to fill in.
                    588:         */
                    589:
1.154     kristaps  590:        roff_res(r, bufp, szp, ln, pos);
1.94      kristaps  591:
1.130     kristaps  592:        ppos = pos;
                    593:        ctl = mandoc_getcontrol(*bufp, &pos);
                    594:
1.94      kristaps  595:        /*
1.79      kristaps  596:         * First, if a scope is open and we're not a macro, pass the
                    597:         * text through the macro's filter.  If a scope isn't open and
                    598:         * we're not a macro, just let it through.
1.125     kristaps  599:         * Finally, if there's an equation scope open, divert it into it
                    600:         * no matter our state.
1.79      kristaps  601:         */
1.74      kristaps  602:
1.130     kristaps  603:        if (r->last && ! ctl) {
1.78      kristaps  604:                t = r->last->tok;
                    605:                assert(roffs[t].text);
1.109     kristaps  606:                e = (*roffs[t].text)
                    607:                        (r, t, bufp, szp, ln, pos, pos, offs);
                    608:                assert(ROFF_IGN == e || ROFF_CONT == e);
1.125     kristaps  609:                if (ROFF_CONT != e)
                    610:                        return(e);
                    611:                if (r->eqn)
1.146     kristaps  612:                        return(eqn_read(&r->eqn, ln, *bufp, pos, offs));
1.125     kristaps  613:                if (r->tbl)
1.130     kristaps  614:                        return(tbl_read(r->tbl, ln, *bufp, pos));
1.154     kristaps  615:                return(roff_parsetext(*bufp + pos));
1.130     kristaps  616:        } else if ( ! ctl) {
1.125     kristaps  617:                if (r->eqn)
1.146     kristaps  618:                        return(eqn_read(&r->eqn, ln, *bufp, pos, offs));
1.109     kristaps  619:                if (r->tbl)
1.130     kristaps  620:                        return(tbl_read(r->tbl, ln, *bufp, pos));
1.154     kristaps  621:                return(roff_parsetext(*bufp + pos));
1.125     kristaps  622:        } else if (r->eqn)
1.146     kristaps  623:                return(eqn_read(&r->eqn, ln, *bufp, ppos, offs));
1.67      kristaps  624:
1.79      kristaps  625:        /*
                    626:         * If a scope is open, go to the child handler for that macro,
                    627:         * as it may want to preprocess before doing anything with it.
1.125     kristaps  628:         * Don't do so if an equation is open.
1.79      kristaps  629:         */
1.78      kristaps  630:
1.79      kristaps  631:        if (r->last) {
                    632:                t = r->last->tok;
                    633:                assert(roffs[t].sub);
                    634:                return((*roffs[t].sub)
1.90      kristaps  635:                                (r, t, bufp, szp,
1.130     kristaps  636:                                 ln, ppos, pos, offs));
1.79      kristaps  637:        }
1.78      kristaps  638:
1.79      kristaps  639:        /*
                    640:         * Lastly, as we've no scope open, try to look up and execute
                    641:         * the new macro.  If no macro is found, simply return and let
                    642:         * the compilers handle it.
                    643:         */
1.67      kristaps  644:
1.106     kristaps  645:        if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos)))
1.79      kristaps  646:                return(ROFF_CONT);
1.67      kristaps  647:
1.75      kristaps  648:        assert(roffs[t].proc);
1.78      kristaps  649:        return((*roffs[t].proc)
1.90      kristaps  650:                        (r, t, bufp, szp,
                    651:                         ln, ppos, pos, offs));
1.74      kristaps  652: }
                    653:
                    654:
1.117     kristaps  655: void
1.74      kristaps  656: roff_endparse(struct roff *r)
                    657: {
                    658:
1.110     kristaps  659:        if (r->last)
1.128     kristaps  660:                mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
1.109     kristaps  661:                                r->last->line, r->last->col, NULL);
1.117     kristaps  662:
1.125     kristaps  663:        if (r->eqn) {
1.128     kristaps  664:                mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
1.148     kristaps  665:                                r->eqn->eqn.ln, r->eqn->eqn.pos, NULL);
1.151     kristaps  666:                eqn_end(&r->eqn);
1.125     kristaps  667:        }
                    668:
1.117     kristaps  669:        if (r->tbl) {
1.128     kristaps  670:                mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
1.117     kristaps  671:                                r->tbl->line, r->tbl->pos, NULL);
1.151     kristaps  672:                tbl_end(&r->tbl);
1.117     kristaps  673:        }
1.67      kristaps  674: }
                    675:
                    676: /*
                    677:  * Parse a roff node's type from the input buffer.  This must be in the
                    678:  * form of ".foo xxx" in the usual way.
                    679:  */
                    680: static enum rofft
1.106     kristaps  681: roff_parse(struct roff *r, const char *buf, int *pos)
1.67      kristaps  682: {
1.106     kristaps  683:        const char      *mac;
                    684:        size_t           maclen;
1.67      kristaps  685:        enum rofft       t;
                    686:
1.144     kristaps  687:        if ('\0' == buf[*pos] || '"' == buf[*pos] ||
                    688:                        '\t' == buf[*pos] || ' ' == buf[*pos])
1.67      kristaps  689:                return(ROFF_MAX);
                    690:
1.144     kristaps  691:        /*
                    692:         * We stop the macro parse at an escape, tab, space, or nil.
                    693:         * However, `\}' is also a valid macro, so make sure we don't
                    694:         * clobber it by seeing the `\' as the end of token.
                    695:         */
                    696:
1.106     kristaps  697:        mac = buf + *pos;
1.144     kristaps  698:        maclen = strcspn(mac + 1, " \\\t\0") + 1;
1.67      kristaps  699:
1.106     kristaps  700:        t = (r->current_string = roff_getstrn(r, mac, maclen))
1.155     kristaps  701:            ? ROFF_USERDEF : roffhash_find(mac, maclen);
1.67      kristaps  702:
1.127     kristaps  703:        *pos += (int)maclen;
1.130     kristaps  704:
1.67      kristaps  705:        while (buf[*pos] && ' ' == buf[*pos])
                    706:                (*pos)++;
                    707:
                    708:        return(t);
                    709: }
                    710:
                    711: /* ARGSUSED */
                    712: static enum rofferr
1.76      kristaps  713: roff_cblock(ROFF_ARGS)
1.67      kristaps  714: {
                    715:
1.79      kristaps  716:        /*
                    717:         * A block-close `..' should only be invoked as a child of an
                    718:         * ignore macro, otherwise raise a warning and just ignore it.
                    719:         */
                    720:
1.76      kristaps  721:        if (NULL == r->last) {
1.128     kristaps  722:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.76      kristaps  723:                return(ROFF_IGN);
                    724:        }
1.67      kristaps  725:
1.81      kristaps  726:        switch (r->last->tok) {
                    727:        case (ROFF_am):
                    728:                /* FALLTHROUGH */
                    729:        case (ROFF_ami):
                    730:                /* FALLTHROUGH */
                    731:        case (ROFF_am1):
                    732:                /* FALLTHROUGH */
                    733:        case (ROFF_de):
1.108     schwarze  734:                /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1.81      kristaps  735:                /* FALLTHROUGH */
                    736:        case (ROFF_dei):
                    737:                /* FALLTHROUGH */
                    738:        case (ROFF_ig):
                    739:                break;
                    740:        default:
1.128     kristaps  741:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.67      kristaps  742:                return(ROFF_IGN);
1.76      kristaps  743:        }
1.67      kristaps  744:
1.76      kristaps  745:        if ((*bufp)[pos])
1.128     kristaps  746:                mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
1.71      kristaps  747:
                    748:        roffnode_pop(r);
1.76      kristaps  749:        roffnode_cleanscope(r);
                    750:        return(ROFF_IGN);
1.71      kristaps  751:
1.67      kristaps  752: }
                    753:
                    754:
1.76      kristaps  755: static void
                    756: roffnode_cleanscope(struct roff *r)
1.67      kristaps  757: {
                    758:
1.76      kristaps  759:        while (r->last) {
                    760:                if (--r->last->endspan < 0)
                    761:                        break;
                    762:                roffnode_pop(r);
                    763:        }
1.67      kristaps  764: }
                    765:
                    766:
1.75      kristaps  767: /* ARGSUSED */
1.74      kristaps  768: static enum rofferr
1.75      kristaps  769: roff_ccond(ROFF_ARGS)
1.74      kristaps  770: {
                    771:
1.76      kristaps  772:        if (NULL == r->last) {
1.128     kristaps  773:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.76      kristaps  774:                return(ROFF_IGN);
                    775:        }
                    776:
1.82      kristaps  777:        switch (r->last->tok) {
                    778:        case (ROFF_el):
                    779:                /* FALLTHROUGH */
                    780:        case (ROFF_ie):
                    781:                /* FALLTHROUGH */
                    782:        case (ROFF_if):
                    783:                break;
                    784:        default:
1.128     kristaps  785:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.75      kristaps  786:                return(ROFF_IGN);
                    787:        }
                    788:
1.76      kristaps  789:        if (r->last->endspan > -1) {
1.128     kristaps  790:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.76      kristaps  791:                return(ROFF_IGN);
                    792:        }
                    793:
                    794:        if ((*bufp)[pos])
1.128     kristaps  795:                mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
1.76      kristaps  796:
1.75      kristaps  797:        roffnode_pop(r);
1.76      kristaps  798:        roffnode_cleanscope(r);
                    799:        return(ROFF_IGN);
                    800: }
                    801:
1.75      kristaps  802:
1.76      kristaps  803: /* ARGSUSED */
                    804: static enum rofferr
1.80      kristaps  805: roff_block(ROFF_ARGS)
1.76      kristaps  806: {
1.78      kristaps  807:        int             sv;
                    808:        size_t          sz;
1.106     kristaps  809:        char            *name;
                    810:
                    811:        name = NULL;
1.76      kristaps  812:
1.106     kristaps  813:        if (ROFF_ig != tok) {
                    814:                if ('\0' == (*bufp)[pos]) {
1.128     kristaps  815:                        mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
1.106     kristaps  816:                        return(ROFF_IGN);
                    817:                }
1.107     kristaps  818:
                    819:                /*
                    820:                 * Re-write `de1', since we don't really care about
                    821:                 * groff's strange compatibility mode, into `de'.
                    822:                 */
                    823:
1.106     kristaps  824:                if (ROFF_de1 == tok)
                    825:                        tok = ROFF_de;
                    826:                if (ROFF_de == tok)
                    827:                        name = *bufp + pos;
                    828:                else
1.128     kristaps  829:                        mandoc_msg(MANDOCERR_REQUEST, r->parse, ln, ppos,
1.106     kristaps  830:                            roffs[tok].name);
1.107     kristaps  831:
1.131     schwarze  832:                while ((*bufp)[pos] && ! isspace((unsigned char)(*bufp)[pos]))
1.80      kristaps  833:                        pos++;
1.107     kristaps  834:
1.131     schwarze  835:                while (isspace((unsigned char)(*bufp)[pos]))
1.106     kristaps  836:                        (*bufp)[pos++] = '\0';
1.80      kristaps  837:        }
                    838:
1.106     kristaps  839:        roffnode_push(r, tok, name, ln, ppos);
                    840:
                    841:        /*
                    842:         * At the beginning of a `de' macro, clear the existing string
                    843:         * with the same name, if there is one.  New content will be
                    844:         * added from roff_block_text() in multiline mode.
                    845:         */
1.107     kristaps  846:
1.106     kristaps  847:        if (ROFF_de == tok)
1.108     schwarze  848:                roff_setstr(r, name, "", 0);
1.76      kristaps  849:
1.79      kristaps  850:        if ('\0' == (*bufp)[pos])
1.78      kristaps  851:                return(ROFF_IGN);
                    852:
1.107     kristaps  853:        /* If present, process the custom end-of-line marker. */
                    854:
1.78      kristaps  855:        sv = pos;
1.131     schwarze  856:        while ((*bufp)[pos] && ! isspace((unsigned char)(*bufp)[pos]))
1.78      kristaps  857:                pos++;
                    858:
                    859:        /*
                    860:         * Note: groff does NOT like escape characters in the input.
                    861:         * Instead of detecting this, we're just going to let it fly and
                    862:         * to hell with it.
                    863:         */
                    864:
                    865:        assert(pos > sv);
                    866:        sz = (size_t)(pos - sv);
                    867:
1.79      kristaps  868:        if (1 == sz && '.' == (*bufp)[sv])
                    869:                return(ROFF_IGN);
                    870:
1.98      schwarze  871:        r->last->end = mandoc_malloc(sz + 1);
1.78      kristaps  872:
                    873:        memcpy(r->last->end, *bufp + sv, sz);
                    874:        r->last->end[(int)sz] = '\0';
                    875:
1.77      kristaps  876:        if ((*bufp)[pos])
1.128     kristaps  877:                mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
1.74      kristaps  878:
1.78      kristaps  879:        return(ROFF_IGN);
                    880: }
                    881:
                    882:
                    883: /* ARGSUSED */
                    884: static enum rofferr
1.80      kristaps  885: roff_block_sub(ROFF_ARGS)
1.79      kristaps  886: {
                    887:        enum rofft      t;
                    888:        int             i, j;
                    889:
                    890:        /*
                    891:         * First check whether a custom macro exists at this level.  If
                    892:         * it does, then check against it.  This is some of groff's
                    893:         * stranger behaviours.  If we encountered a custom end-scope
                    894:         * tag and that tag also happens to be a "real" macro, then we
                    895:         * need to try interpreting it again as a real macro.  If it's
                    896:         * not, then return ignore.  Else continue.
                    897:         */
                    898:
                    899:        if (r->last->end) {
1.130     kristaps  900:                for (i = pos, j = 0; r->last->end[j]; j++, i++)
1.79      kristaps  901:                        if ((*bufp)[i] != r->last->end[j])
                    902:                                break;
                    903:
                    904:                if ('\0' == r->last->end[j] &&
                    905:                                ('\0' == (*bufp)[i] ||
                    906:                                 ' ' == (*bufp)[i] ||
                    907:                                 '\t' == (*bufp)[i])) {
                    908:                        roffnode_pop(r);
                    909:                        roffnode_cleanscope(r);
                    910:
1.130     kristaps  911:                        while (' ' == (*bufp)[i] || '\t' == (*bufp)[i])
                    912:                                i++;
                    913:
                    914:                        pos = i;
1.106     kristaps  915:                        if (ROFF_MAX != roff_parse(r, *bufp, &pos))
1.79      kristaps  916:                                return(ROFF_RERUN);
                    917:                        return(ROFF_IGN);
                    918:                }
                    919:        }
                    920:
                    921:        /*
                    922:         * If we have no custom end-query or lookup failed, then try
                    923:         * pulling it out of the hashtable.
                    924:         */
                    925:
1.137     schwarze  926:        t = roff_parse(r, *bufp, &pos);
1.79      kristaps  927:
1.106     kristaps  928:        /*
                    929:         * Macros other than block-end are only significant
                    930:         * in `de' blocks; elsewhere, simply throw them away.
                    931:         */
                    932:        if (ROFF_cblock != t) {
                    933:                if (ROFF_de == tok)
                    934:                        roff_setstr(r, r->last->name, *bufp + ppos, 1);
1.79      kristaps  935:                return(ROFF_IGN);
1.106     kristaps  936:        }
1.79      kristaps  937:
                    938:        assert(roffs[t].proc);
1.90      kristaps  939:        return((*roffs[t].proc)(r, t, bufp, szp,
                    940:                                ln, ppos, pos, offs));
1.79      kristaps  941: }
                    942:
                    943:
                    944: /* ARGSUSED */
                    945: static enum rofferr
1.80      kristaps  946: roff_block_text(ROFF_ARGS)
1.78      kristaps  947: {
                    948:
1.106     kristaps  949:        if (ROFF_de == tok)
                    950:                roff_setstr(r, r->last->name, *bufp + pos, 1);
                    951:
1.78      kristaps  952:        return(ROFF_IGN);
                    953: }
                    954:
                    955:
                    956: /* ARGSUSED */
                    957: static enum rofferr
1.82      kristaps  958: roff_cond_sub(ROFF_ARGS)
                    959: {
                    960:        enum rofft       t;
                    961:        enum roffrule    rr;
1.139     kristaps  962:        char            *ep;
1.82      kristaps  963:
                    964:        rr = r->last->rule;
1.139     kristaps  965:        roffnode_cleanscope(r);
1.82      kristaps  966:
1.139     kristaps  967:        /*
                    968:         * If the macro is unknown, first check if it contains a closing
                    969:         * delimiter `\}'.  If it does, close out our scope and return
                    970:         * the currently-scoped rule (ignore or continue).  Else, drop
                    971:         * into the currently-scoped rule.
1.87      kristaps  972:         */
                    973:
1.106     kristaps  974:        if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos))) {
1.139     kristaps  975:                ep = &(*bufp)[pos];
                    976:                for ( ; NULL != (ep = strchr(ep, '\\')); ep++) {
                    977:                        ep++;
                    978:                        if ('}' != *ep)
                    979:                                continue;
1.144     kristaps  980:
                    981:                        /*
                    982:                         * Make the \} go away.
                    983:                         * This is a little haphazard, as it's not quite
                    984:                         * clear how nroff does this.
                    985:                         * If we're at the end of line, then just chop
                    986:                         * off the \} and resize the buffer.
                    987:                         * If we aren't, then conver it to spaces.
                    988:                         */
                    989:
                    990:                        if ('\0' == *(ep + 1)) {
                    991:                                *--ep = '\0';
                    992:                                *szp -= 2;
                    993:                        } else
                    994:                                *(ep - 1) = *ep = ' ';
                    995:
1.139     kristaps  996:                        roff_ccond(r, ROFF_ccond, bufp, szp,
                    997:                                        ln, pos, pos + 2, offs);
                    998:                        break;
                    999:                }
1.82      kristaps 1000:                return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1.100     kristaps 1001:        }
1.82      kristaps 1002:
                   1003:        /*
                   1004:         * A denied conditional must evaluate its children if and only
                   1005:         * if they're either structurally required (such as loops and
                   1006:         * conditionals) or a closing macro.
                   1007:         */
1.139     kristaps 1008:
1.82      kristaps 1009:        if (ROFFRULE_DENY == rr)
                   1010:                if ( ! (ROFFMAC_STRUCT & roffs[t].flags))
                   1011:                        if (ROFF_ccond != t)
                   1012:                                return(ROFF_IGN);
                   1013:
                   1014:        assert(roffs[t].proc);
1.90      kristaps 1015:        return((*roffs[t].proc)(r, t, bufp, szp,
                   1016:                                ln, ppos, pos, offs));
1.82      kristaps 1017: }
                   1018:
                   1019: /* ARGSUSED */
                   1020: static enum rofferr
                   1021: roff_cond_text(ROFF_ARGS)
1.78      kristaps 1022: {
1.140     kristaps 1023:        char            *ep;
1.82      kristaps 1024:        enum roffrule    rr;
                   1025:
                   1026:        rr = r->last->rule;
1.140     kristaps 1027:        roffnode_cleanscope(r);
1.82      kristaps 1028:
1.140     kristaps 1029:        ep = &(*bufp)[pos];
                   1030:        for ( ; NULL != (ep = strchr(ep, '\\')); ep++) {
                   1031:                ep++;
                   1032:                if ('}' != *ep)
                   1033:                        continue;
                   1034:                *ep = '&';
                   1035:                roff_ccond(r, ROFF_ccond, bufp, szp,
                   1036:                                ln, pos, pos + 2, offs);
1.78      kristaps 1037:        }
1.82      kristaps 1038:        return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1.74      kristaps 1039: }
                   1040:
1.88      kristaps 1041: static enum roffrule
                   1042: roff_evalcond(const char *v, int *pos)
                   1043: {
                   1044:
                   1045:        switch (v[*pos]) {
                   1046:        case ('n'):
                   1047:                (*pos)++;
                   1048:                return(ROFFRULE_ALLOW);
                   1049:        case ('e'):
                   1050:                /* FALLTHROUGH */
                   1051:        case ('o'):
                   1052:                /* FALLTHROUGH */
                   1053:        case ('t'):
                   1054:                (*pos)++;
                   1055:                return(ROFFRULE_DENY);
                   1056:        default:
                   1057:                break;
                   1058:        }
                   1059:
                   1060:        while (v[*pos] && ' ' != v[*pos])
                   1061:                (*pos)++;
                   1062:        return(ROFFRULE_DENY);
                   1063: }
                   1064:
1.75      kristaps 1065: /* ARGSUSED */
1.74      kristaps 1066: static enum rofferr
1.103     kristaps 1067: roff_line_ignore(ROFF_ARGS)
1.89      kristaps 1068: {
1.123     schwarze 1069:
                   1070:        if (ROFF_it == tok)
1.128     kristaps 1071:                mandoc_msg(MANDOCERR_REQUEST, r->parse, ln, ppos, "it");
1.89      kristaps 1072:
                   1073:        return(ROFF_IGN);
                   1074: }
                   1075:
1.104     kristaps 1076: /* ARGSUSED */
                   1077: static enum rofferr
1.82      kristaps 1078: roff_cond(ROFF_ARGS)
1.74      kristaps 1079: {
1.77      kristaps 1080:        int              sv;
1.88      kristaps 1081:        enum roffrule    rule;
1.74      kristaps 1082:
1.134     kristaps 1083:        /*
                   1084:         * An `.el' has no conditional body: it will consume the value
                   1085:         * of the current rstack entry set in prior `ie' calls or
                   1086:         * defaults to DENY.
                   1087:         *
                   1088:         * If we're not an `el', however, then evaluate the conditional.
                   1089:         */
1.133     kristaps 1090:
1.134     kristaps 1091:        rule = ROFF_el == tok ?
                   1092:                (r->rstackpos < 0 ?
                   1093:                 ROFFRULE_DENY : r->rstack[r->rstackpos--]) :
                   1094:                roff_evalcond(*bufp, &pos);
1.77      kristaps 1095:
                   1096:        sv = pos;
1.75      kristaps 1097:        while (' ' == (*bufp)[pos])
                   1098:                pos++;
1.74      kristaps 1099:
1.77      kristaps 1100:        /*
                   1101:         * Roff is weird.  If we have just white-space after the
                   1102:         * conditional, it's considered the BODY and we exit without
                   1103:         * really doing anything.  Warn about this.  It's probably
                   1104:         * wrong.
                   1105:         */
1.88      kristaps 1106:
1.77      kristaps 1107:        if ('\0' == (*bufp)[pos] && sv != pos) {
1.128     kristaps 1108:                mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
1.107     kristaps 1109:                return(ROFF_IGN);
1.77      kristaps 1110:        }
                   1111:
1.106     kristaps 1112:        roffnode_push(r, tok, NULL, ln, ppos);
1.77      kristaps 1113:
1.88      kristaps 1114:        r->last->rule = rule;
                   1115:
1.134     kristaps 1116:        /*
                   1117:         * An if-else will put the NEGATION of the current evaluated
                   1118:         * conditional into the stack of rules.
                   1119:         */
                   1120:
1.84      schwarze 1121:        if (ROFF_ie == tok) {
1.134     kristaps 1122:                if (r->rstackpos == RSTACK_MAX - 1) {
                   1123:                        mandoc_msg(MANDOCERR_MEM,
                   1124:                                r->parse, ln, ppos, NULL);
                   1125:                        return(ROFF_ERR);
                   1126:                }
                   1127:                r->rstack[++r->rstackpos] =
                   1128:                        ROFFRULE_DENY == r->last->rule ?
                   1129:                        ROFFRULE_ALLOW : ROFFRULE_DENY;
1.82      kristaps 1130:        }
1.88      kristaps 1131:
                   1132:        /* If the parent has false as its rule, then so do we. */
                   1133:
1.109     kristaps 1134:        if (r->last->parent && ROFFRULE_DENY == r->last->parent->rule)
1.84      schwarze 1135:                r->last->rule = ROFFRULE_DENY;
1.88      kristaps 1136:
                   1137:        /*
                   1138:         * Determine scope.  If we're invoked with "\{" trailing the
                   1139:         * conditional, then we're in a multiline scope.  Else our scope
                   1140:         * expires on the next line.
                   1141:         */
1.74      kristaps 1142:
1.75      kristaps 1143:        r->last->endspan = 1;
                   1144:
                   1145:        if ('\\' == (*bufp)[pos] && '{' == (*bufp)[pos + 1]) {
                   1146:                r->last->endspan = -1;
                   1147:                pos += 2;
1.109     kristaps 1148:        }
1.74      kristaps 1149:
1.77      kristaps 1150:        /*
                   1151:         * If there are no arguments on the line, the next-line scope is
                   1152:         * assumed.
                   1153:         */
                   1154:
1.75      kristaps 1155:        if ('\0' == (*bufp)[pos])
                   1156:                return(ROFF_IGN);
1.77      kristaps 1157:
                   1158:        /* Otherwise re-run the roff parser after recalculating. */
1.74      kristaps 1159:
1.75      kristaps 1160:        *offs = pos;
                   1161:        return(ROFF_RERUN);
1.83      schwarze 1162: }
                   1163:
                   1164:
                   1165: /* ARGSUSED */
                   1166: static enum rofferr
1.92      schwarze 1167: roff_ds(ROFF_ARGS)
                   1168: {
1.96      kristaps 1169:        char            *name, *string;
                   1170:
                   1171:        /*
                   1172:         * A symbol is named by the first word following the macro
                   1173:         * invocation up to a space.  Its value is anything after the
                   1174:         * name's trailing whitespace and optional double-quote.  Thus,
                   1175:         *
                   1176:         *  [.ds foo "bar  "     ]
                   1177:         *
                   1178:         * will have `bar  "     ' as its value.
                   1179:         */
1.92      schwarze 1180:
1.121     schwarze 1181:        string = *bufp + pos;
                   1182:        name = roff_getname(r, &string, ln, pos);
1.92      schwarze 1183:        if ('\0' == *name)
                   1184:                return(ROFF_IGN);
                   1185:
1.121     schwarze 1186:        /* Read past initial double-quote. */
                   1187:        if ('"' == *string)
1.92      schwarze 1188:                string++;
                   1189:
1.96      kristaps 1190:        /* The rest is the value. */
1.106     kristaps 1191:        roff_setstr(r, name, string, 0);
1.92      schwarze 1192:        return(ROFF_IGN);
                   1193: }
                   1194:
1.147     kristaps 1195: int
                   1196: roff_regisset(const struct roff *r, enum regs reg)
                   1197: {
                   1198:
                   1199:        return(r->regs[(int)reg].set);
                   1200: }
                   1201:
                   1202: unsigned int
                   1203: roff_regget(const struct roff *r, enum regs reg)
                   1204: {
                   1205:
                   1206:        return(r->regs[(int)reg].u);
                   1207: }
                   1208:
                   1209: void
                   1210: roff_regunset(struct roff *r, enum regs reg)
                   1211: {
                   1212:
                   1213:        r->regs[(int)reg].set = 0;
                   1214: }
1.92      schwarze 1215:
                   1216: /* ARGSUSED */
                   1217: static enum rofferr
1.89      kristaps 1218: roff_nr(ROFF_ARGS)
1.83      schwarze 1219: {
1.121     schwarze 1220:        const char      *key;
                   1221:        char            *val;
1.138     kristaps 1222:        int              iv;
1.89      kristaps 1223:
1.121     schwarze 1224:        val = *bufp + pos;
                   1225:        key = roff_getname(r, &val, ln, pos);
1.89      kristaps 1226:
                   1227:        if (0 == strcmp(key, "nS")) {
1.147     kristaps 1228:                r->regs[(int)REG_nS].set = 1;
1.149     kristaps 1229:                if ((iv = mandoc_strntoi(val, strlen(val), 10)) >= 0)
1.147     kristaps 1230:                        r->regs[(int)REG_nS].u = (unsigned)iv;
1.138     kristaps 1231:                else
1.147     kristaps 1232:                        r->regs[(int)REG_nS].u = 0u;
1.109     kristaps 1233:        }
                   1234:
1.122     schwarze 1235:        return(ROFF_IGN);
                   1236: }
                   1237:
                   1238: /* ARGSUSED */
                   1239: static enum rofferr
                   1240: roff_rm(ROFF_ARGS)
                   1241: {
                   1242:        const char       *name;
                   1243:        char             *cp;
                   1244:
                   1245:        cp = *bufp + pos;
                   1246:        while ('\0' != *cp) {
1.127     kristaps 1247:                name = roff_getname(r, &cp, ln, (int)(cp - *bufp));
1.122     schwarze 1248:                if ('\0' != *name)
                   1249:                        roff_setstr(r, name, NULL, 0);
                   1250:        }
1.109     kristaps 1251:        return(ROFF_IGN);
                   1252: }
                   1253:
                   1254: /* ARGSUSED */
                   1255: static enum rofferr
                   1256: roff_TE(ROFF_ARGS)
                   1257: {
                   1258:
                   1259:        if (NULL == r->tbl)
1.128     kristaps 1260:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.115     kristaps 1261:        else
1.151     kristaps 1262:                tbl_end(&r->tbl);
1.109     kristaps 1263:
1.112     kristaps 1264:        return(ROFF_IGN);
                   1265: }
                   1266:
                   1267: /* ARGSUSED */
                   1268: static enum rofferr
                   1269: roff_T_(ROFF_ARGS)
                   1270: {
                   1271:
                   1272:        if (NULL == r->tbl)
1.128     kristaps 1273:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.112     kristaps 1274:        else
1.116     kristaps 1275:                tbl_restart(ppos, ln, r->tbl);
1.112     kristaps 1276:
1.109     kristaps 1277:        return(ROFF_IGN);
                   1278: }
                   1279:
1.156     kristaps 1280: #if 0
                   1281: static int
1.151     kristaps 1282: roff_closeeqn(struct roff *r)
                   1283: {
                   1284:
                   1285:        return(r->eqn && ROFF_EQN == eqn_end(&r->eqn) ? 1 : 0);
                   1286: }
1.156     kristaps 1287: #endif
1.151     kristaps 1288:
1.156     kristaps 1289: static void
1.151     kristaps 1290: roff_openeqn(struct roff *r, const char *name, int line,
                   1291:                int offs, const char *buf)
1.125     kristaps 1292: {
1.151     kristaps 1293:        struct eqn_node *e;
                   1294:        int              poff;
1.125     kristaps 1295:
                   1296:        assert(NULL == r->eqn);
1.151     kristaps 1297:        e = eqn_alloc(name, offs, line, r->parse);
1.125     kristaps 1298:
                   1299:        if (r->last_eqn)
                   1300:                r->last_eqn->next = e;
                   1301:        else
                   1302:                r->first_eqn = r->last_eqn = e;
                   1303:
                   1304:        r->eqn = r->last_eqn = e;
1.151     kristaps 1305:
                   1306:        if (buf) {
                   1307:                poff = 0;
                   1308:                eqn_read(&r->eqn, line, buf, offs, &poff);
                   1309:        }
                   1310: }
                   1311:
                   1312: /* ARGSUSED */
                   1313: static enum rofferr
                   1314: roff_EQ(ROFF_ARGS)
                   1315: {
                   1316:
                   1317:        roff_openeqn(r, *bufp + pos, ln, ppos, NULL);
1.125     kristaps 1318:        return(ROFF_IGN);
                   1319: }
                   1320:
                   1321: /* ARGSUSED */
                   1322: static enum rofferr
                   1323: roff_EN(ROFF_ARGS)
                   1324: {
                   1325:
1.128     kristaps 1326:        mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.125     kristaps 1327:        return(ROFF_IGN);
                   1328: }
                   1329:
                   1330: /* ARGSUSED */
                   1331: static enum rofferr
1.109     kristaps 1332: roff_TS(ROFF_ARGS)
                   1333: {
1.118     kristaps 1334:        struct tbl_node *t;
1.89      kristaps 1335:
1.115     kristaps 1336:        if (r->tbl) {
1.128     kristaps 1337:                mandoc_msg(MANDOCERR_SCOPEBROKEN, r->parse, ln, ppos, NULL);
1.151     kristaps 1338:                tbl_end(&r->tbl);
1.115     kristaps 1339:        }
1.83      schwarze 1340:
1.128     kristaps 1341:        t = tbl_alloc(ppos, ln, r->parse);
1.113     kristaps 1342:
                   1343:        if (r->last_tbl)
                   1344:                r->last_tbl->next = t;
                   1345:        else
                   1346:                r->first_tbl = r->last_tbl = t;
                   1347:
                   1348:        r->tbl = r->last_tbl = t;
1.83      schwarze 1349:        return(ROFF_IGN);
1.92      schwarze 1350: }
                   1351:
1.105     kristaps 1352: /* ARGSUSED */
                   1353: static enum rofferr
1.164     kristaps 1354: roff_tr(ROFF_ARGS)
                   1355: {
                   1356:        const char      *p, *first, *second;
                   1357:        size_t           fsz, ssz;
                   1358:        enum mandoc_esc  esc;
                   1359:
                   1360:        p = *bufp + pos;
                   1361:
                   1362:        if ('\0' == *p) {
                   1363:                mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
                   1364:                return(ROFF_IGN);
                   1365:        }
                   1366:
                   1367:        while ('\0' != *p) {
                   1368:                fsz = ssz = 1;
                   1369:
                   1370:                first = p++;
                   1371:                if ('\\' == *first) {
                   1372:                        esc = mandoc_escape(&p, NULL, NULL);
                   1373:                        if (ESCAPE_ERROR == esc) {
                   1374:                                mandoc_msg
                   1375:                                        (MANDOCERR_BADESCAPE, r->parse,
                   1376:                                         ln, (int)(p - *bufp), NULL);
                   1377:                                return(ROFF_IGN);
                   1378:                        }
                   1379:                        fsz = (size_t)(p - first);
                   1380:                }
                   1381:
                   1382:                second = p++;
                   1383:                if ('\\' == *second) {
                   1384:                        esc = mandoc_escape(&p, NULL, NULL);
                   1385:                        if (ESCAPE_ERROR == esc) {
                   1386:                                mandoc_msg
                   1387:                                        (MANDOCERR_BADESCAPE, r->parse,
                   1388:                                         ln, (int)(p - *bufp), NULL);
                   1389:                                return(ROFF_IGN);
                   1390:                        }
                   1391:                        ssz = (size_t)(p - second);
1.165     kristaps 1392:                } else if ('\0' == *second) {
1.164     kristaps 1393:                        mandoc_msg(MANDOCERR_ARGCOUNT, r->parse,
                   1394:                                        ln, (int)(p - *bufp), NULL);
                   1395:                        second = " ";
1.165     kristaps 1396:                        p--;
1.164     kristaps 1397:                }
                   1398:
                   1399:                roff_setstrn(&r->chrtab, first, fsz, second, ssz, 0);
                   1400:        }
                   1401:
                   1402:        return(ROFF_IGN);
                   1403: }
                   1404:
                   1405: /* ARGSUSED */
                   1406: static enum rofferr
1.105     kristaps 1407: roff_so(ROFF_ARGS)
                   1408: {
                   1409:        char *name;
                   1410:
1.128     kristaps 1411:        mandoc_msg(MANDOCERR_SO, r->parse, ln, ppos, NULL);
1.105     kristaps 1412:
                   1413:        /*
                   1414:         * Handle `so'.  Be EXTREMELY careful, as we shouldn't be
                   1415:         * opening anything that's not in our cwd or anything beneath
                   1416:         * it.  Thus, explicitly disallow traversing up the file-system
                   1417:         * or using absolute paths.
                   1418:         */
                   1419:
                   1420:        name = *bufp + pos;
                   1421:        if ('/' == *name || strstr(name, "../") || strstr(name, "/..")) {
1.128     kristaps 1422:                mandoc_msg(MANDOCERR_SOPATH, r->parse, ln, pos, NULL);
1.105     kristaps 1423:                return(ROFF_ERR);
                   1424:        }
                   1425:
                   1426:        *offs = pos;
                   1427:        return(ROFF_SO);
                   1428: }
1.92      schwarze 1429:
1.106     kristaps 1430: /* ARGSUSED */
                   1431: static enum rofferr
                   1432: roff_userdef(ROFF_ARGS)
1.99      kristaps 1433: {
1.106     kristaps 1434:        const char       *arg[9];
                   1435:        char             *cp, *n1, *n2;
1.119     schwarze 1436:        int               i;
1.106     kristaps 1437:
                   1438:        /*
                   1439:         * Collect pointers to macro argument strings
                   1440:         * and null-terminate them.
                   1441:         */
                   1442:        cp = *bufp + pos;
1.119     schwarze 1443:        for (i = 0; i < 9; i++)
1.120     schwarze 1444:                arg[i] = '\0' == *cp ? "" :
1.136     kristaps 1445:                    mandoc_getarg(r->parse, &cp, ln, &pos);
1.99      kristaps 1446:
1.106     kristaps 1447:        /*
                   1448:         * Expand macro arguments.
1.99      kristaps 1449:         */
1.106     kristaps 1450:        *szp = 0;
                   1451:        n1 = cp = mandoc_strdup(r->current_string);
                   1452:        while (NULL != (cp = strstr(cp, "\\$"))) {
                   1453:                i = cp[2] - '1';
                   1454:                if (0 > i || 8 < i) {
                   1455:                        /* Not an argument invocation. */
                   1456:                        cp += 2;
                   1457:                        continue;
                   1458:                }
                   1459:
                   1460:                *szp = strlen(n1) - 3 + strlen(arg[i]) + 1;
                   1461:                n2 = mandoc_malloc(*szp);
                   1462:
                   1463:                strlcpy(n2, n1, (size_t)(cp - n1 + 1));
                   1464:                strlcat(n2, arg[i], *szp);
                   1465:                strlcat(n2, cp + 3, *szp);
                   1466:
                   1467:                cp = n2 + (cp - n1);
                   1468:                free(n1);
                   1469:                n1 = n2;
1.99      kristaps 1470:        }
                   1471:
1.106     kristaps 1472:        /*
                   1473:         * Replace the macro invocation
                   1474:         * by the expanded macro.
                   1475:         */
                   1476:        free(*bufp);
                   1477:        *bufp = n1;
                   1478:        if (0 == *szp)
                   1479:                *szp = strlen(*bufp) + 1;
                   1480:
                   1481:        return(*szp > 1 && '\n' == (*bufp)[(int)*szp - 2] ?
                   1482:           ROFF_REPARSE : ROFF_APPEND);
1.99      kristaps 1483: }
1.121     schwarze 1484:
                   1485: static char *
                   1486: roff_getname(struct roff *r, char **cpp, int ln, int pos)
                   1487: {
                   1488:        char     *name, *cp;
                   1489:
                   1490:        name = *cpp;
                   1491:        if ('\0' == *name)
                   1492:                return(name);
                   1493:
                   1494:        /* Read until end of name. */
                   1495:        for (cp = name; '\0' != *cp && ' ' != *cp; cp++) {
                   1496:                if ('\\' != *cp)
                   1497:                        continue;
                   1498:                cp++;
                   1499:                if ('\\' == *cp)
                   1500:                        continue;
1.128     kristaps 1501:                mandoc_msg(MANDOCERR_NAMESC, r->parse, ln, pos, NULL);
1.121     schwarze 1502:                *cp = '\0';
                   1503:                name = cp;
                   1504:        }
                   1505:
                   1506:        /* Nil-terminate name. */
                   1507:        if ('\0' != *cp)
                   1508:                *(cp++) = '\0';
                   1509:
                   1510:        /* Read past spaces. */
                   1511:        while (' ' == *cp)
                   1512:                cp++;
                   1513:
                   1514:        *cpp = cp;
                   1515:        return(name);
                   1516: }
                   1517:
1.106     kristaps 1518: /*
                   1519:  * Store *string into the user-defined string called *name.
                   1520:  * In multiline mode, append to an existing entry and append '\n';
                   1521:  * else replace the existing entry, if there is one.
                   1522:  * To clear an existing entry, call with (*r, *name, NULL, 0).
                   1523:  */
1.94      kristaps 1524: static void
1.106     kristaps 1525: roff_setstr(struct roff *r, const char *name, const char *string,
                   1526:        int multiline)
1.92      schwarze 1527: {
1.164     kristaps 1528:
                   1529:        roff_setstrn(&r->strtab, name, strlen(name), string,
                   1530:                        string ? strlen(string) : 0, multiline);
                   1531: }
                   1532:
                   1533: static void
1.166   ! kristaps 1534: roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
1.164     kristaps 1535:                const char *string, size_t stringsz, int multiline)
                   1536: {
1.166   ! kristaps 1537:        struct roffkv   *n;
1.164     kristaps 1538:        char            *c;
                   1539:        int              i;
                   1540:        size_t           oldch, newch;
1.92      schwarze 1541:
1.106     kristaps 1542:        /* Search for an existing string with the same name. */
1.164     kristaps 1543:        n = *r;
                   1544:
1.166   ! kristaps 1545:        while (n && strcmp(name, n->key.p))
1.92      schwarze 1546:                n = n->next;
1.94      kristaps 1547:
                   1548:        if (NULL == n) {
1.106     kristaps 1549:                /* Create a new string table entry. */
1.166   ! kristaps 1550:                n = mandoc_malloc(sizeof(struct roffkv));
        !          1551:                n->key.p = mandoc_strndup(name, namesz);
        !          1552:                n->key.sz = namesz;
        !          1553:                n->val.p = NULL;
        !          1554:                n->val.sz = 0;
1.164     kristaps 1555:                n->next = *r;
                   1556:                *r = n;
1.106     kristaps 1557:        } else if (0 == multiline) {
                   1558:                /* In multiline mode, append; else replace. */
1.166   ! kristaps 1559:                free(n->val.p);
        !          1560:                n->val.p = NULL;
        !          1561:                n->val.sz = 0;
1.106     kristaps 1562:        }
                   1563:
                   1564:        if (NULL == string)
                   1565:                return;
                   1566:
                   1567:        /*
                   1568:         * One additional byte for the '\n' in multiline mode,
                   1569:         * and one for the terminating '\0'.
                   1570:         */
1.164     kristaps 1571:        newch = stringsz + (multiline ? 2u : 1u);
                   1572:
1.166   ! kristaps 1573:        if (NULL == n->val.p) {
        !          1574:                n->val.p = mandoc_malloc(newch);
        !          1575:                *n->val.p = '\0';
1.106     kristaps 1576:                oldch = 0;
                   1577:        } else {
1.166   ! kristaps 1578:                oldch = n->val.sz;
        !          1579:                n->val.p = mandoc_realloc(n->val.p, oldch + newch);
1.106     kristaps 1580:        }
                   1581:
                   1582:        /* Skip existing content in the destination buffer. */
1.166   ! kristaps 1583:        c = n->val.p + (int)oldch;
1.106     kristaps 1584:
                   1585:        /* Append new content to the destination buffer. */
1.164     kristaps 1586:        i = 0;
                   1587:        while (i < (int)stringsz) {
1.106     kristaps 1588:                /*
                   1589:                 * Rudimentary roff copy mode:
                   1590:                 * Handle escaped backslashes.
                   1591:                 */
1.164     kristaps 1592:                if ('\\' == string[i] && '\\' == string[i + 1])
                   1593:                        i++;
                   1594:                *c++ = string[i++];
1.106     kristaps 1595:        }
1.94      kristaps 1596:
1.106     kristaps 1597:        /* Append terminating bytes. */
                   1598:        if (multiline)
                   1599:                *c++ = '\n';
1.163     kristaps 1600:
1.106     kristaps 1601:        *c = '\0';
1.166   ! kristaps 1602:        n->val.sz = (int)(c - n->val.p);
1.92      schwarze 1603: }
                   1604:
1.94      kristaps 1605: static const char *
                   1606: roff_getstrn(const struct roff *r, const char *name, size_t len)
1.92      schwarze 1607: {
1.166   ! kristaps 1608:        const struct roffkv *n;
1.92      schwarze 1609:
1.164     kristaps 1610:        for (n = r->strtab; n; n = n->next)
1.166   ! kristaps 1611:                if (0 == strncmp(name, n->key.p, len) &&
        !          1612:                                '\0' == n->key.p[(int)len])
        !          1613:                        return(n->val.p);
1.94      kristaps 1614:
1.157     kristaps 1615:        return(NULL);
1.92      schwarze 1616: }
                   1617:
1.94      kristaps 1618: static void
1.166   ! kristaps 1619: roff_freestr(struct roffkv **r)
1.92      schwarze 1620: {
1.166   ! kristaps 1621:        struct roffkv    *n, *nn;
1.92      schwarze 1622:
1.164     kristaps 1623:        for (n = *r; n; n = nn) {
1.166   ! kristaps 1624:                free(n->key.p);
        !          1625:                free(n->val.p);
1.92      schwarze 1626:                nn = n->next;
                   1627:                free(n);
                   1628:        }
1.94      kristaps 1629:
1.164     kristaps 1630:        *r = NULL;
1.114     kristaps 1631: }
                   1632:
                   1633: const struct tbl_span *
                   1634: roff_span(const struct roff *r)
                   1635: {
                   1636:
                   1637:        return(r->tbl ? tbl_span(r->tbl) : NULL);
1.125     kristaps 1638: }
                   1639:
                   1640: const struct eqn *
                   1641: roff_eqn(const struct roff *r)
                   1642: {
                   1643:
                   1644:        return(r->last_eqn ? &r->last_eqn->eqn : NULL);
1.151     kristaps 1645: }
                   1646:
                   1647: char
                   1648: roff_eqndelim(const struct roff *r)
                   1649: {
                   1650:
                   1651:        return('\0');
1.164     kristaps 1652: }
                   1653:
                   1654: /*
                   1655:  * Duplicate an input string, making the appropriate character
                   1656:  * conversations (as stipulated by `tr') along the way.
                   1657:  * Returns a heap-allocated string with all the replacements made.
                   1658:  */
                   1659: char *
                   1660: roff_strdup(const struct roff *r, const char *p)
                   1661: {
1.166   ! kristaps 1662:        const struct roffkv *cp;
1.164     kristaps 1663:        char            *res;
                   1664:        const char      *pp;
                   1665:        size_t           ssz, sz;
                   1666:        enum mandoc_esc  esc;
                   1667:
                   1668:        if (NULL == r->chrtab)
                   1669:                return(mandoc_strdup(p));
                   1670:        else if ('\0' == *p)
                   1671:                return(mandoc_strdup(""));
                   1672:
                   1673:        /*
                   1674:         * Step through each character looking for term matches
                   1675:         * (remember that a `tr' can be invoked with an escape, which is
                   1676:         * a glyph but the escape is multi-character).
                   1677:         * We only do this if the character hash has been initialised
                   1678:         * and the string is >0 length.
                   1679:         */
                   1680:
                   1681:        res = NULL;
                   1682:        ssz = 0;
                   1683:
                   1684:        while ('\0' != *p) {
                   1685:                /* Search for term matches. */
                   1686:                for (cp = r->chrtab; cp; cp = cp->next)
1.166   ! kristaps 1687:                        if (0 == strncmp(p, cp->key.p, cp->key.sz))
1.164     kristaps 1688:                                break;
                   1689:
                   1690:                if (NULL != cp) {
                   1691:                        /*
                   1692:                         * A match has been found.
                   1693:                         * Append the match to the array and move
                   1694:                         * forward by its keysize.
                   1695:                         */
1.166   ! kristaps 1696:                        res = mandoc_realloc
        !          1697:                                (res, ssz + cp->val.sz + 1);
        !          1698:                        memcpy(res + ssz, cp->val.p, cp->val.sz);
        !          1699:                        ssz += cp->val.sz;
        !          1700:                        p += (int)cp->key.sz;
1.164     kristaps 1701:                        continue;
                   1702:                }
                   1703:
                   1704:                if ('\\' == *p) {
                   1705:                        /*
                   1706:                         * Handle escapes carefully: we need to copy
                   1707:                         * over just the escape itself, or else we might
                   1708:                         * do replacements within the escape itself.
                   1709:                         * Make sure to pass along the bogus string.
                   1710:                         */
                   1711:                        pp = p++;
                   1712:                        esc = mandoc_escape(&p, NULL, NULL);
                   1713:                        if (ESCAPE_ERROR == esc) {
                   1714:                                sz = strlen(pp);
                   1715:                                res = mandoc_realloc(res, ssz + sz + 1);
                   1716:                                memcpy(res + ssz, pp, sz);
                   1717:                                break;
                   1718:                        }
                   1719:                        /*
                   1720:                         * We bail out on bad escapes.
                   1721:                         * No need to warn: we already did so when
                   1722:                         * roff_res() was called.
                   1723:                         */
                   1724:                        sz = (int)(p - pp);
                   1725:                        res = mandoc_realloc(res, ssz + sz + 1);
                   1726:                        memcpy(res + ssz, pp, sz);
                   1727:                        ssz += sz;
                   1728:                        continue;
                   1729:                }
                   1730:
                   1731:                /* Just append the charater. */
                   1732:                res = mandoc_realloc(res, ssz + 2);
                   1733:                res[ssz++] = *p++;
                   1734:        }
                   1735:
                   1736:        res[(int)ssz] = '\0';
                   1737:        return(res);
1.74      kristaps 1738: }

CVSweb