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

Annotation of mandoc/roff.c, Revision 1.199

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

CVSweb