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

Annotation of mandoc/roff.c, Revision 1.202

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

CVSweb