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

Annotation of mandoc/roff.c, Revision 1.180

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

CVSweb