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

Annotation of mandoc/roff.c, Revision 1.155

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

CVSweb