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

Annotation of mandoc/roff.c, Revision 1.170

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

CVSweb