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

Annotation of mandoc/roff.c, Revision 1.164

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

CVSweb