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

Annotation of mandoc/roff.c, Revision 1.194

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

CVSweb