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

Annotation of mandoc/roff.c, Revision 1.193

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

CVSweb