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

Annotation of mandoc/roff.c, Revision 1.186

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

CVSweb