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

Annotation of mandoc/roff.c, Revision 1.183

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

CVSweb