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

Annotation of mandoc/roff.c, Revision 1.237

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

CVSweb