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

Annotation of mandoc/roff.c, Revision 1.314

1.314   ! schwarze    1: /*     $Id: roff.c,v 1.313 2017/06/17 22:43:14 schwarze Exp $ */
1.1       kristaps    2: /*
1.267     schwarze    3:  * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.287     schwarze    4:  * Copyright (c) 2010-2015, 2017 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.245     schwarze   24: #include <limits.h>
1.295     schwarze   25: #include <stddef.h>
                     26: #include <stdint.h>
1.178     schwarze   27: #include <stdio.h>
1.1       kristaps   28: #include <stdlib.h>
1.67      kristaps   29: #include <string.h>
1.1       kristaps   30:
1.67      kristaps   31: #include "mandoc.h"
1.201     schwarze   32: #include "mandoc_aux.h"
1.295     schwarze   33: #include "mandoc_ohash.h"
1.265     schwarze   34: #include "roff.h"
1.226     schwarze   35: #include "libmandoc.h"
1.266     schwarze   36: #include "roff_int.h"
1.109     kristaps   37: #include "libroff.h"
1.82      kristaps   38:
1.170     schwarze   39: /* Maximum number of string expansions per line, to break infinite loops. */
                     40: #define        EXPAND_LIMIT    1000
                     41:
1.266     schwarze   42: /* --- data types --------------------------------------------------------- */
                     43:
1.147     kristaps   44: /*
1.167     kristaps   45:  * An incredibly-simple string buffer.
                     46:  */
1.94      kristaps   47: struct roffstr {
1.167     kristaps   48:        char            *p; /* nil-terminated buffer */
                     49:        size_t           sz; /* saved strlen(p) */
1.166     kristaps   50: };
                     51:
                     52: /*
1.167     kristaps   53:  * A key-value roffstr pair as part of a singly-linked list.
1.166     kristaps   54:  */
                     55: struct roffkv {
                     56:        struct roffstr   key;
                     57:        struct roffstr   val;
                     58:        struct roffkv   *next; /* next in list */
1.94      kristaps   59: };
                     60:
1.180     schwarze   61: /*
                     62:  * A single number register as part of a singly-linked list.
                     63:  */
                     64: struct roffreg {
                     65:        struct roffstr   key;
1.181     schwarze   66:        int              val;
1.180     schwarze   67:        struct roffreg  *next;
                     68: };
                     69:
1.295     schwarze   70: /*
                     71:  * Association of request and macro names with token IDs.
                     72:  */
                     73: struct roffreq {
                     74:        enum roff_tok    tok;
                     75:        char             name[];
                     76: };
                     77:
1.67      kristaps   78: struct roff {
1.128     kristaps   79:        struct mparse   *parse; /* parse point */
1.296     schwarze   80:        struct roff_man *man; /* mdoc or man parser */
1.67      kristaps   81:        struct roffnode *last; /* leaf of stack */
1.223     schwarze   82:        int             *rstack; /* stack of inverted `ie' values */
1.295     schwarze   83:        struct ohash    *reqtab; /* request lookup table */
1.180     schwarze   84:        struct roffreg  *regtab; /* number registers */
1.166     kristaps   85:        struct roffkv   *strtab; /* user-defined strings & macros */
1.306     schwarze   86:        struct roffkv   *rentab; /* renamed strings & macros */
1.167     kristaps   87:        struct roffkv   *xmbtab; /* multi-byte trans table (`tr') */
                     88:        struct roffstr  *xtab; /* single-byte trans table (`tr') */
1.106     kristaps   89:        const char      *current_string; /* value of last called user macro */
1.118     kristaps   90:        struct tbl_node *first_tbl; /* first table parsed */
                     91:        struct tbl_node *last_tbl; /* last table parsed */
                     92:        struct tbl_node *tbl; /* current table being parsed */
1.125     kristaps   93:        struct eqn_node *last_eqn; /* last equation parsed */
                     94:        struct eqn_node *first_eqn; /* first equation parsed */
                     95:        struct eqn_node *eqn; /* current equation being parsed */
1.230     schwarze   96:        int              eqn_inline; /* current equation is inline */
1.223     schwarze   97:        int              options; /* parse options */
                     98:        int              rstacksz; /* current size limit of rstack */
                     99:        int              rstackpos; /* position in rstack */
1.227     schwarze  100:        int              format; /* current file in mdoc or man format */
1.273     schwarze  101:        int              argc; /* number of args of the last macro */
1.223     schwarze  102:        char             control; /* control character */
1.303     schwarze  103:        char             escape; /* escape character */
1.79      kristaps  104: };
                    105:
1.67      kristaps  106: struct roffnode {
1.294     schwarze  107:        enum roff_tok    tok; /* type of node */
1.67      kristaps  108:        struct roffnode *parent; /* up one in stack */
                    109:        int              line; /* parse line */
                    110:        int              col; /* parse col */
1.106     kristaps  111:        char            *name; /* node name, e.g. macro name */
1.79      kristaps  112:        char            *end; /* end-rules: custom token */
                    113:        int              endspan; /* end-rules: next-line or infty */
1.198     schwarze  114:        int              rule; /* current evaluation rule */
1.67      kristaps  115: };
                    116:
                    117: #define        ROFF_ARGS        struct roff *r, /* parse ctx */ \
1.294     schwarze  118:                         enum roff_tok tok, /* tok of macro */ \
1.238     schwarze  119:                         struct buf *buf, /* input buffer */ \
1.67      kristaps  120:                         int ln, /* parse line */ \
1.75      kristaps  121:                         int ppos, /* original pos in buffer */ \
                    122:                         int pos, /* current pos in buffer */ \
1.74      kristaps  123:                         int *offs /* reset offset of buffer data */
1.67      kristaps  124:
                    125: typedef        enum rofferr (*roffproc)(ROFF_ARGS);
                    126:
                    127: struct roffmac {
1.79      kristaps  128:        roffproc         proc; /* process new macro */
                    129:        roffproc         text; /* process as child text of macro */
                    130:        roffproc         sub; /* process as child of macro */
                    131:        int              flags;
                    132: #define        ROFFMAC_STRUCT  (1 << 0) /* always interpret */
1.67      kristaps  133: };
                    134:
1.141     kristaps  135: struct predef {
                    136:        const char      *name; /* predefined input name */
                    137:        const char      *str; /* replacement symbol */
                    138: };
                    139:
                    140: #define        PREDEF(__name, __str) \
                    141:        { (__name), (__str) },
                    142:
1.266     schwarze  143: /* --- function prototypes ------------------------------------------------ */
                    144:
1.155     kristaps  145: static void             roffnode_cleanscope(struct roff *);
                    146: static void             roffnode_pop(struct roff *);
1.294     schwarze  147: static void             roffnode_push(struct roff *, enum roff_tok,
1.155     kristaps  148:                                const char *, int, int);
1.311     schwarze  149: static enum rofferr     roff_als(ROFF_ARGS);
1.80      kristaps  150: static enum rofferr     roff_block(ROFF_ARGS);
                    151: static enum rofferr     roff_block_text(ROFF_ARGS);
                    152: static enum rofferr     roff_block_sub(ROFF_ARGS);
1.296     schwarze  153: static enum rofferr     roff_br(ROFF_ARGS);
1.80      kristaps  154: static enum rofferr     roff_cblock(ROFF_ARGS);
1.174     kristaps  155: static enum rofferr     roff_cc(ROFF_ARGS);
1.195     schwarze  156: static void             roff_ccond(struct roff *, int, int);
1.82      kristaps  157: static enum rofferr     roff_cond(ROFF_ARGS);
                    158: static enum rofferr     roff_cond_text(ROFF_ARGS);
                    159: static enum rofferr     roff_cond_sub(ROFF_ARGS);
1.92      schwarze  160: static enum rofferr     roff_ds(ROFF_ARGS);
1.303     schwarze  161: static enum rofferr     roff_ec(ROFF_ARGS);
                    162: static enum rofferr     roff_eo(ROFF_ARGS);
1.238     schwarze  163: static enum rofferr     roff_eqndelim(struct roff *, struct buf *, int);
1.271     schwarze  164: static int              roff_evalcond(struct roff *r, int, char *, int *);
1.234     kristaps  165: static int              roff_evalnum(struct roff *, int,
                    166:                                const char *, int *, int *, int);
                    167: static int              roff_evalpar(struct roff *, int,
1.261     schwarze  168:                                const char *, int *, int *, int);
1.198     schwarze  169: static int              roff_evalstrcond(const char *, int *);
1.155     kristaps  170: static void             roff_free1(struct roff *);
1.180     schwarze  171: static void             roff_freereg(struct roffreg *);
1.167     kristaps  172: static void             roff_freestr(struct roffkv *);
1.212     schwarze  173: static size_t           roff_getname(struct roff *, char **, int, int);
1.261     schwarze  174: static int              roff_getnum(const char *, int *, int *, int);
1.184     schwarze  175: static int              roff_getop(const char *, int *, char *);
1.181     schwarze  176: static int              roff_getregn(const struct roff *,
                    177:                                const char *, size_t);
1.273     schwarze  178: static int              roff_getregro(const struct roff *,
                    179:                                const char *name);
1.306     schwarze  180: static const char      *roff_getrenn(const struct roff *,
                    181:                                const char *, size_t);
1.207     schwarze  182: static const char      *roff_getstrn(const struct roff *,
1.94      kristaps  183:                                const char *, size_t);
1.271     schwarze  184: static int              roff_hasregn(const struct roff *,
                    185:                                const char *, size_t);
1.251     schwarze  186: static enum rofferr     roff_insec(ROFF_ARGS);
1.178     schwarze  187: static enum rofferr     roff_it(ROFF_ARGS);
1.103     kristaps  188: static enum rofferr     roff_line_ignore(ROFF_ARGS);
1.265     schwarze  189: static void             roff_man_alloc1(struct roff_man *);
                    190: static void             roff_man_free1(struct roff_man *);
1.300     schwarze  191: static enum rofferr     roff_manyarg(ROFF_ARGS);
1.89      kristaps  192: static enum rofferr     roff_nr(ROFF_ARGS);
1.297     schwarze  193: static enum rofferr     roff_onearg(ROFF_ARGS);
1.294     schwarze  194: static enum roff_tok    roff_parse(struct roff *, char *, int *,
1.214     schwarze  195:                                int, int);
1.305     schwarze  196: static enum rofferr     roff_parsetext(struct roff *, struct buf *,
                    197:                                int, int *);
1.306     schwarze  198: static enum rofferr     roff_renamed(ROFF_ARGS);
1.238     schwarze  199: static enum rofferr     roff_res(struct roff *, struct buf *, int, int);
1.122     schwarze  200: static enum rofferr     roff_rm(ROFF_ARGS);
1.306     schwarze  201: static enum rofferr     roff_rn(ROFF_ARGS);
1.203     schwarze  202: static enum rofferr     roff_rr(ROFF_ARGS);
1.94      kristaps  203: static void             roff_setstr(struct roff *,
1.106     kristaps  204:                                const char *, const char *, int);
1.207     schwarze  205: static void             roff_setstrn(struct roffkv **, const char *,
1.164     kristaps  206:                                size_t, const char *, size_t, int);
1.105     kristaps  207: static enum rofferr     roff_so(ROFF_ARGS);
1.164     kristaps  208: static enum rofferr     roff_tr(ROFF_ARGS);
1.175     schwarze  209: static enum rofferr     roff_Dd(ROFF_ARGS);
                    210: static enum rofferr     roff_TH(ROFF_ARGS);
1.109     kristaps  211: static enum rofferr     roff_TE(ROFF_ARGS);
                    212: static enum rofferr     roff_TS(ROFF_ARGS);
1.125     kristaps  213: static enum rofferr     roff_EQ(ROFF_ARGS);
                    214: static enum rofferr     roff_EN(ROFF_ARGS);
1.112     kristaps  215: static enum rofferr     roff_T_(ROFF_ARGS);
1.251     schwarze  216: static enum rofferr     roff_unsupp(ROFF_ARGS);
1.106     kristaps  217: static enum rofferr     roff_userdef(ROFF_ARGS);
1.67      kristaps  218:
1.266     schwarze  219: /* --- constant data ------------------------------------------------------ */
                    220:
1.261     schwarze  221: #define        ROFFNUM_SCALE   (1 << 0)  /* Honour scaling in roff_getnum(). */
                    222: #define        ROFFNUM_WHITE   (1 << 1)  /* Skip whitespace in roff_evalnum(). */
                    223:
1.294     schwarze  224: const char *__roff_name[MAN_MAX + 1] = {
1.305     schwarze  225:        "br",           "ce",           "ft",           "ll",
1.312     schwarze  226:        "mc",           "po",           "rj",           "sp",
                    227:        "ta",           "ti",           NULL,
1.294     schwarze  228:        "ab",           "ad",           "af",           "aln",
                    229:        "als",          "am",           "am1",          "ami",
                    230:        "ami1",         "as",           "as1",          "asciify",
                    231:        "backtrace",    "bd",           "bleedat",      "blm",
                    232:         "box",         "boxa",         "bp",           "BP",
                    233:        "break",        "breakchar",    "brnl",         "brp",
1.305     schwarze  234:        "brpnl",        "c2",           "cc",
1.294     schwarze  235:        "cf",           "cflags",       "ch",           "char",
                    236:        "chop",         "class",        "close",        "CL",
                    237:        "color",        "composite",    "continue",     "cp",
                    238:        "cropat",       "cs",           "cu",           "da",
                    239:        "dch",          "Dd",           "de",           "de1",
                    240:        "defcolor",     "dei",          "dei1",         "device",
                    241:        "devicem",      "di",           "do",           "ds",
                    242:        "ds1",          "dwh",          "dt",           "ec",
                    243:        "ecr",          "ecs",          "el",           "em",
                    244:        "EN",           "eo",           "EP",           "EQ",
                    245:        "errprint",     "ev",           "evc",          "ex",
                    246:        "fallback",     "fam",          "fc",           "fchar",
                    247:        "fcolor",       "fdeferlig",    "feature",      "fkern",
                    248:        "fl",           "flig",         "fp",           "fps",
                    249:        "fschar",       "fspacewidth",  "fspecial",     "ftr",
                    250:        "fzoom",        "gcolor",       "hc",           "hcode",
                    251:        "hidechar",     "hla",          "hlm",          "hpf",
                    252:        "hpfa",         "hpfcode",      "hw",           "hy",
                    253:        "hylang",       "hylen",        "hym",          "hypp",
                    254:        "hys",          "ie",           "if",           "ig",
                    255:        "index",        "it",           "itc",          "IX",
                    256:        "kern",         "kernafter",    "kernbefore",   "kernpair",
                    257:        "lc",           "lc_ctype",     "lds",          "length",
                    258:        "letadj",       "lf",           "lg",           "lhang",
                    259:        "linetabs",     "lnr",          "lnrf",         "lpfx",
1.304     schwarze  260:        "ls",           "lsm",          "lt",
1.294     schwarze  261:        "mediasize",    "minss",        "mk",           "mso",
                    262:        "na",           "ne",           "nh",           "nhychar",
                    263:        "nm",           "nn",           "nop",          "nr",
                    264:        "nrf",          "nroff",        "ns",           "nx",
                    265:        "open",         "opena",        "os",           "output",
                    266:        "padj",         "papersize",    "pc",           "pev",
                    267:        "pi",           "PI",           "pl",           "pm",
1.312     schwarze  268:        "pn",           "pnr",          "ps",
1.294     schwarze  269:        "psbb",         "pshape",       "pso",          "ptr",
                    270:        "pvs",          "rchar",        "rd",           "recursionlimit",
1.309     schwarze  271:        "return",       "rfschar",      "rhang",
1.294     schwarze  272:        "rm",           "rn",           "rnn",          "rr",
                    273:        "rs",           "rt",           "schar",        "sentchar",
                    274:        "shc",          "shift",        "sizes",        "so",
                    275:        "spacewidth",   "special",      "spreadwarn",   "ss",
                    276:        "sty",          "substring",    "sv",           "sy",
1.300     schwarze  277:        "T&",           "tc",           "TE",
1.301     schwarze  278:        "TH",           "tkf",          "tl",
1.294     schwarze  279:        "tm",           "tm1",          "tmc",          "tr",
                    280:        "track",        "transchar",    "trf",          "trimat",
                    281:        "trin",         "trnt",         "troff",        "TS",
                    282:        "uf",           "ul",           "unformat",     "unwatch",
                    283:        "unwatchn",     "vpt",          "vs",           "warn",
                    284:        "warnscale",    "watch",        "watchlength",  "watchn",
                    285:        "wh",           "while",        "write",        "writec",
                    286:        "writem",       "xflag",        ".",            NULL,
1.306     schwarze  287:        NULL,           "text",
1.294     schwarze  288:        "Dd",           "Dt",           "Os",           "Sh",
                    289:        "Ss",           "Pp",           "D1",           "Dl",
                    290:        "Bd",           "Ed",           "Bl",           "El",
                    291:        "It",           "Ad",           "An",           "Ap",
                    292:        "Ar",           "Cd",           "Cm",           "Dv",
                    293:        "Er",           "Ev",           "Ex",           "Fa",
                    294:        "Fd",           "Fl",           "Fn",           "Ft",
                    295:        "Ic",           "In",           "Li",           "Nd",
                    296:        "Nm",           "Op",           "Ot",           "Pa",
                    297:        "Rv",           "St",           "Va",           "Vt",
                    298:        "Xr",           "%A",           "%B",           "%D",
                    299:        "%I",           "%J",           "%N",           "%O",
                    300:        "%P",           "%R",           "%T",           "%V",
                    301:        "Ac",           "Ao",           "Aq",           "At",
                    302:        "Bc",           "Bf",           "Bo",           "Bq",
                    303:        "Bsx",          "Bx",           "Db",           "Dc",
                    304:        "Do",           "Dq",           "Ec",           "Ef",
                    305:        "Em",           "Eo",           "Fx",           "Ms",
                    306:        "No",           "Ns",           "Nx",           "Ox",
                    307:        "Pc",           "Pf",           "Po",           "Pq",
                    308:        "Qc",           "Ql",           "Qo",           "Qq",
                    309:        "Re",           "Rs",           "Sc",           "So",
                    310:        "Sq",           "Sm",           "Sx",           "Sy",
                    311:        "Tn",           "Ux",           "Xc",           "Xo",
                    312:        "Fo",           "Fc",           "Oo",           "Oc",
                    313:        "Bk",           "Ek",           "Bt",           "Hf",
                    314:        "Fr",           "Ud",           "Lb",           "Lp",
                    315:        "Lk",           "Mt",           "Brq",          "Bro",
                    316:        "Brc",          "%C",           "Es",           "En",
1.299     schwarze  317:        "Dx",           "%Q",           "%U",           "Ta",
                    318:        NULL,
1.294     schwarze  319:        "TH",           "SH",           "SS",           "TP",
                    320:        "LP",           "PP",           "P",            "IP",
                    321:        "HP",           "SM",           "SB",           "BI",
                    322:        "IB",           "BR",           "RB",           "R",
                    323:        "B",            "I",            "IR",           "RI",
1.299     schwarze  324:        "nf",           "fi",
1.294     schwarze  325:        "RE",           "RS",           "DT",           "UC",
1.297     schwarze  326:        "PD",           "AT",           "in",
1.294     schwarze  327:        "OP",           "EX",           "EE",           "UR",
1.298     schwarze  328:        "UE",           NULL
1.294     schwarze  329: };
                    330: const  char *const *roff_name = __roff_name;
                    331:
                    332: static struct roffmac   roffs[TOKEN_NONE] = {
1.296     schwarze  333:        { roff_br, NULL, NULL, 0 },  /* br */
1.305     schwarze  334:        { roff_onearg, NULL, NULL, 0 },  /* ce */
1.297     schwarze  335:        { roff_onearg, NULL, NULL, 0 },  /* ft */
1.298     schwarze  336:        { roff_onearg, NULL, NULL, 0 },  /* ll */
1.304     schwarze  337:        { roff_onearg, NULL, NULL, 0 },  /* mc */
1.312     schwarze  338:        { roff_onearg, NULL, NULL, 0 },  /* po */
1.309     schwarze  339:        { roff_onearg, NULL, NULL, 0 },  /* rj */
1.299     schwarze  340:        { roff_onearg, NULL, NULL, 0 },  /* sp */
1.300     schwarze  341:        { roff_manyarg, NULL, NULL, 0 },  /* ta */
1.301     schwarze  342:        { roff_onearg, NULL, NULL, 0 },  /* ti */
1.296     schwarze  343:        { NULL, NULL, NULL, 0 },  /* ROFF_MAX */
1.295     schwarze  344:        { roff_unsupp, NULL, NULL, 0 },  /* ab */
                    345:        { roff_line_ignore, NULL, NULL, 0 },  /* ad */
                    346:        { roff_line_ignore, NULL, NULL, 0 },  /* af */
                    347:        { roff_unsupp, NULL, NULL, 0 },  /* aln */
1.311     schwarze  348:        { roff_als, NULL, NULL, 0 },  /* als */
1.295     schwarze  349:        { roff_block, roff_block_text, roff_block_sub, 0 },  /* am */
                    350:        { roff_block, roff_block_text, roff_block_sub, 0 },  /* am1 */
                    351:        { roff_block, roff_block_text, roff_block_sub, 0 },  /* ami */
                    352:        { roff_block, roff_block_text, roff_block_sub, 0 },  /* ami1 */
                    353:        { roff_ds, NULL, NULL, 0 },  /* as */
                    354:        { roff_ds, NULL, NULL, 0 },  /* as1 */
                    355:        { roff_unsupp, NULL, NULL, 0 },  /* asciify */
                    356:        { roff_line_ignore, NULL, NULL, 0 },  /* backtrace */
                    357:        { roff_line_ignore, NULL, NULL, 0 },  /* bd */
                    358:        { roff_line_ignore, NULL, NULL, 0 },  /* bleedat */
                    359:        { roff_unsupp, NULL, NULL, 0 },  /* blm */
                    360:        { roff_unsupp, NULL, NULL, 0 },  /* box */
                    361:        { roff_unsupp, NULL, NULL, 0 },  /* boxa */
                    362:        { roff_line_ignore, NULL, NULL, 0 },  /* bp */
                    363:        { roff_unsupp, NULL, NULL, 0 },  /* BP */
                    364:        { roff_unsupp, NULL, NULL, 0 },  /* break */
                    365:        { roff_line_ignore, NULL, NULL, 0 },  /* breakchar */
                    366:        { roff_line_ignore, NULL, NULL, 0 },  /* brnl */
1.296     schwarze  367:        { roff_br, NULL, NULL, 0 },  /* brp */
1.295     schwarze  368:        { roff_line_ignore, NULL, NULL, 0 },  /* brpnl */
                    369:        { roff_unsupp, NULL, NULL, 0 },  /* c2 */
                    370:        { roff_cc, NULL, NULL, 0 },  /* cc */
                    371:        { roff_insec, NULL, NULL, 0 },  /* cf */
                    372:        { roff_line_ignore, NULL, NULL, 0 },  /* cflags */
                    373:        { roff_line_ignore, NULL, NULL, 0 },  /* ch */
                    374:        { roff_unsupp, NULL, NULL, 0 },  /* char */
                    375:        { roff_unsupp, NULL, NULL, 0 },  /* chop */
                    376:        { roff_line_ignore, NULL, NULL, 0 },  /* class */
                    377:        { roff_insec, NULL, NULL, 0 },  /* close */
                    378:        { roff_unsupp, NULL, NULL, 0 },  /* CL */
                    379:        { roff_line_ignore, NULL, NULL, 0 },  /* color */
                    380:        { roff_unsupp, NULL, NULL, 0 },  /* composite */
                    381:        { roff_unsupp, NULL, NULL, 0 },  /* continue */
                    382:        { roff_line_ignore, NULL, NULL, 0 },  /* cp */
                    383:        { roff_line_ignore, NULL, NULL, 0 },  /* cropat */
                    384:        { roff_line_ignore, NULL, NULL, 0 },  /* cs */
                    385:        { roff_line_ignore, NULL, NULL, 0 },  /* cu */
                    386:        { roff_unsupp, NULL, NULL, 0 },  /* da */
                    387:        { roff_unsupp, NULL, NULL, 0 },  /* dch */
                    388:        { roff_Dd, NULL, NULL, 0 },  /* Dd */
                    389:        { roff_block, roff_block_text, roff_block_sub, 0 },  /* de */
                    390:        { roff_block, roff_block_text, roff_block_sub, 0 },  /* de1 */
                    391:        { roff_line_ignore, NULL, NULL, 0 },  /* defcolor */
                    392:        { roff_block, roff_block_text, roff_block_sub, 0 },  /* dei */
                    393:        { roff_block, roff_block_text, roff_block_sub, 0 },  /* dei1 */
                    394:        { roff_unsupp, NULL, NULL, 0 },  /* device */
                    395:        { roff_unsupp, NULL, NULL, 0 },  /* devicem */
                    396:        { roff_unsupp, NULL, NULL, 0 },  /* di */
                    397:        { roff_unsupp, NULL, NULL, 0 },  /* do */
                    398:        { roff_ds, NULL, NULL, 0 },  /* ds */
                    399:        { roff_ds, NULL, NULL, 0 },  /* ds1 */
                    400:        { roff_unsupp, NULL, NULL, 0 },  /* dwh */
                    401:        { roff_unsupp, NULL, NULL, 0 },  /* dt */
1.303     schwarze  402:        { roff_ec, NULL, NULL, 0 },  /* ec */
1.295     schwarze  403:        { roff_unsupp, NULL, NULL, 0 },  /* ecr */
                    404:        { roff_unsupp, NULL, NULL, 0 },  /* ecs */
                    405:        { roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT },  /* el */
                    406:        { roff_unsupp, NULL, NULL, 0 },  /* em */
                    407:        { roff_EN, NULL, NULL, 0 },  /* EN */
1.303     schwarze  408:        { roff_eo, NULL, NULL, 0 },  /* eo */
1.295     schwarze  409:        { roff_unsupp, NULL, NULL, 0 },  /* EP */
                    410:        { roff_EQ, NULL, NULL, 0 },  /* EQ */
                    411:        { roff_line_ignore, NULL, NULL, 0 },  /* errprint */
                    412:        { roff_unsupp, NULL, NULL, 0 },  /* ev */
                    413:        { roff_unsupp, NULL, NULL, 0 },  /* evc */
                    414:        { roff_unsupp, NULL, NULL, 0 },  /* ex */
                    415:        { roff_line_ignore, NULL, NULL, 0 },  /* fallback */
                    416:        { roff_line_ignore, NULL, NULL, 0 },  /* fam */
                    417:        { roff_unsupp, NULL, NULL, 0 },  /* fc */
                    418:        { roff_unsupp, NULL, NULL, 0 },  /* fchar */
                    419:        { roff_line_ignore, NULL, NULL, 0 },  /* fcolor */
                    420:        { roff_line_ignore, NULL, NULL, 0 },  /* fdeferlig */
                    421:        { roff_line_ignore, NULL, NULL, 0 },  /* feature */
                    422:        { roff_line_ignore, NULL, NULL, 0 },  /* fkern */
                    423:        { roff_line_ignore, NULL, NULL, 0 },  /* fl */
                    424:        { roff_line_ignore, NULL, NULL, 0 },  /* flig */
                    425:        { roff_line_ignore, NULL, NULL, 0 },  /* fp */
                    426:        { roff_line_ignore, NULL, NULL, 0 },  /* fps */
                    427:        { roff_unsupp, NULL, NULL, 0 },  /* fschar */
                    428:        { roff_line_ignore, NULL, NULL, 0 },  /* fspacewidth */
                    429:        { roff_line_ignore, NULL, NULL, 0 },  /* fspecial */
                    430:        { roff_line_ignore, NULL, NULL, 0 },  /* ftr */
                    431:        { roff_line_ignore, NULL, NULL, 0 },  /* fzoom */
                    432:        { roff_line_ignore, NULL, NULL, 0 },  /* gcolor */
                    433:        { roff_line_ignore, NULL, NULL, 0 },  /* hc */
                    434:        { roff_line_ignore, NULL, NULL, 0 },  /* hcode */
                    435:        { roff_line_ignore, NULL, NULL, 0 },  /* hidechar */
                    436:        { roff_line_ignore, NULL, NULL, 0 },  /* hla */
                    437:        { roff_line_ignore, NULL, NULL, 0 },  /* hlm */
                    438:        { roff_line_ignore, NULL, NULL, 0 },  /* hpf */
                    439:        { roff_line_ignore, NULL, NULL, 0 },  /* hpfa */
                    440:        { roff_line_ignore, NULL, NULL, 0 },  /* hpfcode */
                    441:        { roff_line_ignore, NULL, NULL, 0 },  /* hw */
                    442:        { roff_line_ignore, NULL, NULL, 0 },  /* hy */
                    443:        { roff_line_ignore, NULL, NULL, 0 },  /* hylang */
                    444:        { roff_line_ignore, NULL, NULL, 0 },  /* hylen */
                    445:        { roff_line_ignore, NULL, NULL, 0 },  /* hym */
                    446:        { roff_line_ignore, NULL, NULL, 0 },  /* hypp */
                    447:        { roff_line_ignore, NULL, NULL, 0 },  /* hys */
                    448:        { roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT },  /* ie */
                    449:        { roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT },  /* if */
                    450:        { roff_block, roff_block_text, roff_block_sub, 0 },  /* ig */
                    451:        { roff_unsupp, NULL, NULL, 0 },  /* index */
                    452:        { roff_it, NULL, NULL, 0 },  /* it */
                    453:        { roff_unsupp, NULL, NULL, 0 },  /* itc */
                    454:        { roff_line_ignore, NULL, NULL, 0 },  /* IX */
                    455:        { roff_line_ignore, NULL, NULL, 0 },  /* kern */
                    456:        { roff_line_ignore, NULL, NULL, 0 },  /* kernafter */
                    457:        { roff_line_ignore, NULL, NULL, 0 },  /* kernbefore */
                    458:        { roff_line_ignore, NULL, NULL, 0 },  /* kernpair */
                    459:        { roff_unsupp, NULL, NULL, 0 },  /* lc */
                    460:        { roff_unsupp, NULL, NULL, 0 },  /* lc_ctype */
                    461:        { roff_unsupp, NULL, NULL, 0 },  /* lds */
                    462:        { roff_unsupp, NULL, NULL, 0 },  /* length */
                    463:        { roff_line_ignore, NULL, NULL, 0 },  /* letadj */
                    464:        { roff_insec, NULL, NULL, 0 },  /* lf */
                    465:        { roff_line_ignore, NULL, NULL, 0 },  /* lg */
                    466:        { roff_line_ignore, NULL, NULL, 0 },  /* lhang */
                    467:        { roff_unsupp, NULL, NULL, 0 },  /* linetabs */
                    468:        { roff_unsupp, NULL, NULL, 0 },  /* lnr */
                    469:        { roff_unsupp, NULL, NULL, 0 },  /* lnrf */
                    470:        { roff_unsupp, NULL, NULL, 0 },  /* lpfx */
                    471:        { roff_line_ignore, NULL, NULL, 0 },  /* ls */
                    472:        { roff_unsupp, NULL, NULL, 0 },  /* lsm */
                    473:        { roff_line_ignore, NULL, NULL, 0 },  /* lt */
                    474:        { roff_line_ignore, NULL, NULL, 0 },  /* mediasize */
                    475:        { roff_line_ignore, NULL, NULL, 0 },  /* minss */
                    476:        { roff_line_ignore, NULL, NULL, 0 },  /* mk */
                    477:        { roff_insec, NULL, NULL, 0 },  /* mso */
                    478:        { roff_line_ignore, NULL, NULL, 0 },  /* na */
                    479:        { roff_line_ignore, NULL, NULL, 0 },  /* ne */
                    480:        { roff_line_ignore, NULL, NULL, 0 },  /* nh */
                    481:        { roff_line_ignore, NULL, NULL, 0 },  /* nhychar */
                    482:        { roff_unsupp, NULL, NULL, 0 },  /* nm */
                    483:        { roff_unsupp, NULL, NULL, 0 },  /* nn */
                    484:        { roff_unsupp, NULL, NULL, 0 },  /* nop */
                    485:        { roff_nr, NULL, NULL, 0 },  /* nr */
                    486:        { roff_unsupp, NULL, NULL, 0 },  /* nrf */
                    487:        { roff_line_ignore, NULL, NULL, 0 },  /* nroff */
                    488:        { roff_line_ignore, NULL, NULL, 0 },  /* ns */
                    489:        { roff_insec, NULL, NULL, 0 },  /* nx */
                    490:        { roff_insec, NULL, NULL, 0 },  /* open */
                    491:        { roff_insec, NULL, NULL, 0 },  /* opena */
                    492:        { roff_line_ignore, NULL, NULL, 0 },  /* os */
                    493:        { roff_unsupp, NULL, NULL, 0 },  /* output */
                    494:        { roff_line_ignore, NULL, NULL, 0 },  /* padj */
                    495:        { roff_line_ignore, NULL, NULL, 0 },  /* papersize */
                    496:        { roff_line_ignore, NULL, NULL, 0 },  /* pc */
                    497:        { roff_line_ignore, NULL, NULL, 0 },  /* pev */
                    498:        { roff_insec, NULL, NULL, 0 },  /* pi */
                    499:        { roff_unsupp, NULL, NULL, 0 },  /* PI */
                    500:        { roff_line_ignore, NULL, NULL, 0 },  /* pl */
                    501:        { roff_line_ignore, NULL, NULL, 0 },  /* pm */
                    502:        { roff_line_ignore, NULL, NULL, 0 },  /* pn */
                    503:        { roff_line_ignore, NULL, NULL, 0 },  /* pnr */
                    504:        { roff_line_ignore, NULL, NULL, 0 },  /* ps */
                    505:        { roff_unsupp, NULL, NULL, 0 },  /* psbb */
                    506:        { roff_unsupp, NULL, NULL, 0 },  /* pshape */
                    507:        { roff_insec, NULL, NULL, 0 },  /* pso */
                    508:        { roff_line_ignore, NULL, NULL, 0 },  /* ptr */
                    509:        { roff_line_ignore, NULL, NULL, 0 },  /* pvs */
                    510:        { roff_unsupp, NULL, NULL, 0 },  /* rchar */
                    511:        { roff_line_ignore, NULL, NULL, 0 },  /* rd */
                    512:        { roff_line_ignore, NULL, NULL, 0 },  /* recursionlimit */
                    513:        { roff_unsupp, NULL, NULL, 0 },  /* return */
                    514:        { roff_unsupp, NULL, NULL, 0 },  /* rfschar */
                    515:        { roff_line_ignore, NULL, NULL, 0 },  /* rhang */
                    516:        { roff_rm, NULL, NULL, 0 },  /* rm */
1.306     schwarze  517:        { roff_rn, NULL, NULL, 0 },  /* rn */
1.295     schwarze  518:        { roff_unsupp, NULL, NULL, 0 },  /* rnn */
                    519:        { roff_rr, NULL, NULL, 0 },  /* rr */
                    520:        { roff_line_ignore, NULL, NULL, 0 },  /* rs */
                    521:        { roff_line_ignore, NULL, NULL, 0 },  /* rt */
                    522:        { roff_unsupp, NULL, NULL, 0 },  /* schar */
                    523:        { roff_line_ignore, NULL, NULL, 0 },  /* sentchar */
                    524:        { roff_line_ignore, NULL, NULL, 0 },  /* shc */
                    525:        { roff_unsupp, NULL, NULL, 0 },  /* shift */
                    526:        { roff_line_ignore, NULL, NULL, 0 },  /* sizes */
                    527:        { roff_so, NULL, NULL, 0 },  /* so */
                    528:        { roff_line_ignore, NULL, NULL, 0 },  /* spacewidth */
                    529:        { roff_line_ignore, NULL, NULL, 0 },  /* special */
                    530:        { roff_line_ignore, NULL, NULL, 0 },  /* spreadwarn */
                    531:        { roff_line_ignore, NULL, NULL, 0 },  /* ss */
                    532:        { roff_line_ignore, NULL, NULL, 0 },  /* sty */
                    533:        { roff_unsupp, NULL, NULL, 0 },  /* substring */
                    534:        { roff_line_ignore, NULL, NULL, 0 },  /* sv */
                    535:        { roff_insec, NULL, NULL, 0 },  /* sy */
                    536:        { roff_T_, NULL, NULL, 0 },  /* T& */
                    537:        { roff_unsupp, NULL, NULL, 0 },  /* tc */
                    538:        { roff_TE, NULL, NULL, 0 },  /* TE */
                    539:        { roff_TH, NULL, NULL, 0 },  /* TH */
                    540:        { roff_line_ignore, NULL, NULL, 0 },  /* tkf */
                    541:        { roff_unsupp, NULL, NULL, 0 },  /* tl */
                    542:        { roff_line_ignore, NULL, NULL, 0 },  /* tm */
                    543:        { roff_line_ignore, NULL, NULL, 0 },  /* tm1 */
                    544:        { roff_line_ignore, NULL, NULL, 0 },  /* tmc */
                    545:        { roff_tr, NULL, NULL, 0 },  /* tr */
                    546:        { roff_line_ignore, NULL, NULL, 0 },  /* track */
                    547:        { roff_line_ignore, NULL, NULL, 0 },  /* transchar */
                    548:        { roff_insec, NULL, NULL, 0 },  /* trf */
                    549:        { roff_line_ignore, NULL, NULL, 0 },  /* trimat */
                    550:        { roff_unsupp, NULL, NULL, 0 },  /* trin */
                    551:        { roff_unsupp, NULL, NULL, 0 },  /* trnt */
                    552:        { roff_line_ignore, NULL, NULL, 0 },  /* troff */
                    553:        { roff_TS, NULL, NULL, 0 },  /* TS */
                    554:        { roff_line_ignore, NULL, NULL, 0 },  /* uf */
                    555:        { roff_line_ignore, NULL, NULL, 0 },  /* ul */
                    556:        { roff_unsupp, NULL, NULL, 0 },  /* unformat */
                    557:        { roff_line_ignore, NULL, NULL, 0 },  /* unwatch */
                    558:        { roff_line_ignore, NULL, NULL, 0 },  /* unwatchn */
                    559:        { roff_line_ignore, NULL, NULL, 0 },  /* vpt */
                    560:        { roff_line_ignore, NULL, NULL, 0 },  /* vs */
                    561:        { roff_line_ignore, NULL, NULL, 0 },  /* warn */
                    562:        { roff_line_ignore, NULL, NULL, 0 },  /* warnscale */
                    563:        { roff_line_ignore, NULL, NULL, 0 },  /* watch */
                    564:        { roff_line_ignore, NULL, NULL, 0 },  /* watchlength */
                    565:        { roff_line_ignore, NULL, NULL, 0 },  /* watchn */
                    566:        { roff_unsupp, NULL, NULL, 0 },  /* wh */
                    567:        { roff_unsupp, NULL, NULL, 0 },  /* while */
                    568:        { roff_insec, NULL, NULL, 0 },  /* write */
                    569:        { roff_insec, NULL, NULL, 0 },  /* writec */
                    570:        { roff_insec, NULL, NULL, 0 },  /* writem */
                    571:        { roff_line_ignore, NULL, NULL, 0 },  /* xflag */
                    572:        { roff_cblock, NULL, NULL, 0 },  /* . */
1.306     schwarze  573:        { roff_renamed, NULL, NULL, 0 },
1.295     schwarze  574:        { roff_userdef, NULL, NULL, 0 }
1.67      kristaps  575: };
                    576:
1.200     schwarze  577: /* not currently implemented: Ds em Eq LP Me PP pp Or Rd Sf SH */
1.175     schwarze  578: const  char *const __mdoc_reserved[] = {
                    579:        "Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
                    580:        "Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
                    581:        "Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
                    582:        "Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
1.200     schwarze  583:        "Dt", "Dv", "Dx", "D1",
                    584:        "Ec", "Ed", "Ef", "Ek", "El", "Em",
                    585:        "En", "Eo", "Er", "Es", "Ev", "Ex",
1.175     schwarze  586:        "Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
1.200     schwarze  587:        "Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp",
                    588:        "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
1.175     schwarze  589:        "Oc", "Oo", "Op", "Os", "Ot", "Ox",
1.200     schwarze  590:        "Pa", "Pc", "Pf", "Po", "Pp", "Pq",
                    591:        "Qc", "Ql", "Qo", "Qq", "Re", "Rs", "Rv",
                    592:        "Sc", "Sh", "Sm", "So", "Sq",
1.175     schwarze  593:        "Ss", "St", "Sx", "Sy",
                    594:        "Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
1.200     schwarze  595:        "%A", "%B", "%C", "%D", "%I", "%J", "%N", "%O",
1.175     schwarze  596:        "%P", "%Q", "%R", "%T", "%U", "%V",
                    597:        NULL
                    598: };
                    599:
1.200     schwarze  600: /* not currently implemented: BT DE DS ME MT PT SY TQ YS */
1.175     schwarze  601: const  char *const __man_reserved[] = {
1.200     schwarze  602:        "AT", "B", "BI", "BR", "DT",
                    603:        "EE", "EN", "EQ", "EX", "HP", "I", "IB", "IP", "IR",
                    604:        "LP", "OP", "P", "PD", "PP",
                    605:        "R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS",
                    606:        "TE", "TH", "TP", "TS", "T&", "UC", "UE", "UR",
1.175     schwarze  607:        NULL
                    608: };
                    609:
1.141     kristaps  610: /* Array of injected predefined strings. */
                    611: #define        PREDEFS_MAX      38
                    612: static const struct predef predefs[PREDEFS_MAX] = {
                    613: #include "predefs.in"
                    614: };
                    615:
1.305     schwarze  616: static int      roffce_lines;  /* number of input lines to center */
                    617: static struct roff_node *roffce_node;  /* active request */
1.178     schwarze  618: static int      roffit_lines;  /* number of lines to delay */
                    619: static char    *roffit_macro;  /* nil-terminated macro line */
                    620:
1.207     schwarze  621:
1.266     schwarze  622: /* --- request table ------------------------------------------------------ */
                    623:
1.295     schwarze  624: struct ohash *
                    625: roffhash_alloc(enum roff_tok mintok, enum roff_tok maxtok)
1.85      kristaps  626: {
1.295     schwarze  627:        struct ohash    *htab;
                    628:        struct roffreq  *req;
                    629:        enum roff_tok    tok;
                    630:        size_t           sz;
                    631:        unsigned int     slot;
1.85      kristaps  632:
1.295     schwarze  633:        htab = mandoc_malloc(sizeof(*htab));
                    634:        mandoc_ohash_init(htab, 8, offsetof(struct roffreq, name));
1.85      kristaps  635:
1.295     schwarze  636:        for (tok = mintok; tok < maxtok; tok++) {
1.296     schwarze  637:                if (roff_name[tok] == NULL)
                    638:                        continue;
1.295     schwarze  639:                sz = strlen(roff_name[tok]);
                    640:                req = mandoc_malloc(sizeof(*req) + sz + 1);
                    641:                req->tok = tok;
                    642:                memcpy(req->name, roff_name[tok], sz + 1);
                    643:                slot = ohash_qlookup(htab, req->name);
                    644:                ohash_insert(htab, slot, req);
1.85      kristaps  645:        }
1.295     schwarze  646:        return htab;
1.85      kristaps  647: }
                    648:
1.295     schwarze  649: void
                    650: roffhash_free(struct ohash *htab)
1.67      kristaps  651: {
1.295     schwarze  652:        struct roffreq  *req;
                    653:        unsigned int     slot;
1.67      kristaps  654:
1.295     schwarze  655:        if (htab == NULL)
                    656:                return;
                    657:        for (req = ohash_first(htab, &slot); req != NULL;
                    658:             req = ohash_next(htab, &slot))
                    659:                free(req);
                    660:        ohash_delete(htab);
                    661:        free(htab);
                    662: }
                    663:
                    664: enum roff_tok
                    665: roffhash_find(struct ohash *htab, const char *name, size_t sz)
                    666: {
                    667:        struct roffreq  *req;
                    668:        const char      *end;
                    669:
                    670:        if (sz) {
                    671:                end = name + sz;
                    672:                req = ohash_find(htab, ohash_qlookupi(htab, name, &end));
                    673:        } else
                    674:                req = ohash_find(htab, ohash_qlookup(htab, name));
                    675:        return req == NULL ? TOKEN_NONE : req->tok;
1.67      kristaps  676: }
                    677:
1.266     schwarze  678: /* --- stack of request blocks -------------------------------------------- */
                    679:
1.67      kristaps  680: /*
                    681:  * Pop the current node off of the stack of roff instructions currently
                    682:  * pending.
                    683:  */
                    684: static void
                    685: roffnode_pop(struct roff *r)
                    686: {
                    687:        struct roffnode *p;
                    688:
1.75      kristaps  689:        assert(r->last);
1.207     schwarze  690:        p = r->last;
1.82      kristaps  691:
1.75      kristaps  692:        r->last = r->last->parent;
1.106     kristaps  693:        free(p->name);
                    694:        free(p->end);
1.67      kristaps  695:        free(p);
                    696: }
                    697:
                    698: /*
                    699:  * Push a roff node onto the instruction stack.  This must later be
                    700:  * removed with roffnode_pop().
                    701:  */
1.98      schwarze  702: static void
1.294     schwarze  703: roffnode_push(struct roff *r, enum roff_tok tok, const char *name,
1.106     kristaps  704:                int line, int col)
1.67      kristaps  705: {
                    706:        struct roffnode *p;
                    707:
1.98      schwarze  708:        p = mandoc_calloc(1, sizeof(struct roffnode));
1.67      kristaps  709:        p->tok = tok;
1.106     kristaps  710:        if (name)
                    711:                p->name = mandoc_strdup(name);
1.67      kristaps  712:        p->parent = r->last;
                    713:        p->line = line;
                    714:        p->col = col;
1.198     schwarze  715:        p->rule = p->parent ? p->parent->rule : 0;
1.67      kristaps  716:
                    717:        r->last = p;
                    718: }
                    719:
1.266     schwarze  720: /* --- roff parser state data management ---------------------------------- */
                    721:
1.67      kristaps  722: static void
                    723: roff_free1(struct roff *r)
                    724: {
1.176     schwarze  725:        struct tbl_node *tbl;
1.125     kristaps  726:        struct eqn_node *e;
1.167     kristaps  727:        int              i;
1.67      kristaps  728:
1.176     schwarze  729:        while (NULL != (tbl = r->first_tbl)) {
                    730:                r->first_tbl = tbl->next;
                    731:                tbl_free(tbl);
1.109     kristaps  732:        }
1.113     kristaps  733:        r->first_tbl = r->last_tbl = r->tbl = NULL;
                    734:
1.125     kristaps  735:        while (NULL != (e = r->first_eqn)) {
                    736:                r->first_eqn = e->next;
                    737:                eqn_free(e);
                    738:        }
                    739:        r->first_eqn = r->last_eqn = r->eqn = NULL;
                    740:
1.67      kristaps  741:        while (r->last)
                    742:                roffnode_pop(r);
1.109     kristaps  743:
1.223     schwarze  744:        free (r->rstack);
                    745:        r->rstack = NULL;
                    746:        r->rstacksz = 0;
                    747:        r->rstackpos = -1;
                    748:
                    749:        roff_freereg(r->regtab);
                    750:        r->regtab = NULL;
                    751:
1.167     kristaps  752:        roff_freestr(r->strtab);
1.306     schwarze  753:        roff_freestr(r->rentab);
1.167     kristaps  754:        roff_freestr(r->xmbtab);
1.306     schwarze  755:        r->strtab = r->rentab = r->xmbtab = NULL;
1.167     kristaps  756:
                    757:        if (r->xtab)
                    758:                for (i = 0; i < 128; i++)
                    759:                        free(r->xtab[i].p);
                    760:        free(r->xtab);
                    761:        r->xtab = NULL;
1.67      kristaps  762: }
                    763:
                    764: void
                    765: roff_reset(struct roff *r)
                    766: {
                    767:        roff_free1(r);
1.227     schwarze  768:        r->format = r->options & (MPARSE_MDOC | MPARSE_MAN);
1.303     schwarze  769:        r->control = '\0';
                    770:        r->escape = '\\';
1.307     schwarze  771:        roffce_lines = 0;
                    772:        roffce_node = NULL;
                    773:        roffit_lines = 0;
                    774:        roffit_macro = NULL;
1.67      kristaps  775: }
                    776:
                    777: void
                    778: roff_free(struct roff *r)
                    779: {
                    780:        roff_free1(r);
1.295     schwarze  781:        roffhash_free(r->reqtab);
1.67      kristaps  782:        free(r);
                    783: }
                    784:
                    785: struct roff *
1.279     schwarze  786: roff_alloc(struct mparse *parse, int options)
1.67      kristaps  787: {
                    788:        struct roff     *r;
                    789:
1.98      schwarze  790:        r = mandoc_calloc(1, sizeof(struct roff));
1.128     kristaps  791:        r->parse = parse;
1.295     schwarze  792:        r->reqtab = roffhash_alloc(0, ROFF_USERDEF);
1.199     schwarze  793:        r->options = options;
1.227     schwarze  794:        r->format = options & (MPARSE_MDOC | MPARSE_MAN);
1.82      kristaps  795:        r->rstackpos = -1;
1.303     schwarze  796:        r->escape = '\\';
1.277     schwarze  797:        return r;
1.265     schwarze  798: }
                    799:
1.266     schwarze  800: /* --- syntax tree state data management ---------------------------------- */
                    801:
1.265     schwarze  802: static void
                    803: roff_man_free1(struct roff_man *man)
                    804: {
                    805:
1.266     schwarze  806:        if (man->first != NULL)
                    807:                roff_node_delete(man, man->first);
1.265     schwarze  808:        free(man->meta.msec);
                    809:        free(man->meta.vol);
                    810:        free(man->meta.os);
                    811:        free(man->meta.arch);
                    812:        free(man->meta.title);
                    813:        free(man->meta.name);
                    814:        free(man->meta.date);
                    815: }
                    816:
                    817: static void
                    818: roff_man_alloc1(struct roff_man *man)
                    819: {
                    820:
                    821:        memset(&man->meta, 0, sizeof(man->meta));
                    822:        man->first = mandoc_calloc(1, sizeof(*man->first));
                    823:        man->first->type = ROFFT_ROOT;
                    824:        man->last = man->first;
                    825:        man->last_es = NULL;
                    826:        man->flags = 0;
                    827:        man->macroset = MACROSET_NONE;
                    828:        man->lastsec = man->lastnamed = SEC_NONE;
                    829:        man->next = ROFF_NEXT_CHILD;
                    830: }
                    831:
                    832: void
                    833: roff_man_reset(struct roff_man *man)
                    834: {
                    835:
                    836:        roff_man_free1(man);
                    837:        roff_man_alloc1(man);
                    838: }
                    839:
                    840: void
                    841: roff_man_free(struct roff_man *man)
                    842: {
                    843:
                    844:        roff_man_free1(man);
                    845:        free(man);
                    846: }
                    847:
                    848: struct roff_man *
                    849: roff_man_alloc(struct roff *roff, struct mparse *parse,
                    850:        const char *defos, int quick)
                    851: {
                    852:        struct roff_man *man;
                    853:
                    854:        man = mandoc_calloc(1, sizeof(*man));
                    855:        man->parse = parse;
                    856:        man->roff = roff;
                    857:        man->defos = defos;
                    858:        man->quick = quick;
                    859:        roff_man_alloc1(man);
1.296     schwarze  860:        roff->man = man;
1.277     schwarze  861:        return man;
1.67      kristaps  862: }
                    863:
1.266     schwarze  864: /* --- syntax tree handling ----------------------------------------------- */
                    865:
                    866: struct roff_node *
                    867: roff_node_alloc(struct roff_man *man, int line, int pos,
                    868:        enum roff_type type, int tok)
                    869: {
                    870:        struct roff_node        *n;
                    871:
                    872:        n = mandoc_calloc(1, sizeof(*n));
                    873:        n->line = line;
                    874:        n->pos = pos;
                    875:        n->tok = tok;
                    876:        n->type = type;
                    877:        n->sec = man->lastsec;
                    878:
                    879:        if (man->flags & MDOC_SYNOPSIS)
1.285     schwarze  880:                n->flags |= NODE_SYNPRETTY;
1.266     schwarze  881:        else
1.285     schwarze  882:                n->flags &= ~NODE_SYNPRETTY;
1.266     schwarze  883:        if (man->flags & MDOC_NEWLINE)
1.285     schwarze  884:                n->flags |= NODE_LINE;
1.266     schwarze  885:        man->flags &= ~MDOC_NEWLINE;
                    886:
1.277     schwarze  887:        return n;
1.266     schwarze  888: }
                    889:
                    890: void
                    891: roff_node_append(struct roff_man *man, struct roff_node *n)
                    892: {
                    893:
                    894:        switch (man->next) {
                    895:        case ROFF_NEXT_SIBLING:
1.281     schwarze  896:                if (man->last->next != NULL) {
                    897:                        n->next = man->last->next;
                    898:                        man->last->next->prev = n;
                    899:                } else
                    900:                        man->last->parent->last = n;
1.266     schwarze  901:                man->last->next = n;
                    902:                n->prev = man->last;
                    903:                n->parent = man->last->parent;
                    904:                break;
                    905:        case ROFF_NEXT_CHILD:
1.287     schwarze  906:                if (man->last->child != NULL) {
                    907:                        n->next = man->last->child;
                    908:                        man->last->child->prev = n;
                    909:                } else
                    910:                        man->last->last = n;
1.266     schwarze  911:                man->last->child = n;
                    912:                n->parent = man->last;
                    913:                break;
                    914:        default:
                    915:                abort();
                    916:        }
1.282     schwarze  917:        man->last = n;
1.266     schwarze  918:
                    919:        switch (n->type) {
                    920:        case ROFFT_HEAD:
                    921:                n->parent->head = n;
                    922:                break;
                    923:        case ROFFT_BODY:
1.282     schwarze  924:                if (n->end != ENDBODY_NOT)
                    925:                        return;
1.266     schwarze  926:                n->parent->body = n;
                    927:                break;
                    928:        case ROFFT_TAIL:
                    929:                n->parent->tail = n;
                    930:                break;
                    931:        default:
1.282     schwarze  932:                return;
1.266     schwarze  933:        }
1.282     schwarze  934:
                    935:        /*
                    936:         * Copy over the normalised-data pointer of our parent.  Not
                    937:         * everybody has one, but copying a null pointer is fine.
                    938:         */
                    939:
                    940:        n->norm = n->parent->norm;
                    941:        assert(n->parent->type == ROFFT_BLOCK);
1.266     schwarze  942: }
                    943:
1.267     schwarze  944: void
                    945: roff_word_alloc(struct roff_man *man, int line, int pos, const char *word)
                    946: {
                    947:        struct roff_node        *n;
                    948:
                    949:        n = roff_node_alloc(man, line, pos, ROFFT_TEXT, TOKEN_NONE);
                    950:        n->string = roff_strdup(man->roff, word);
                    951:        roff_node_append(man, n);
1.286     schwarze  952:        n->flags |= NODE_VALID | NODE_ENDED;
1.267     schwarze  953:        man->next = ROFF_NEXT_SIBLING;
                    954: }
                    955:
                    956: void
                    957: roff_word_append(struct roff_man *man, const char *word)
                    958: {
                    959:        struct roff_node        *n;
                    960:        char                    *addstr, *newstr;
                    961:
                    962:        n = man->last;
                    963:        addstr = roff_strdup(man->roff, word);
                    964:        mandoc_asprintf(&newstr, "%s %s", n->string, addstr);
                    965:        free(addstr);
                    966:        free(n->string);
                    967:        n->string = newstr;
                    968:        man->next = ROFF_NEXT_SIBLING;
1.268     schwarze  969: }
                    970:
                    971: void
                    972: roff_elem_alloc(struct roff_man *man, int line, int pos, int tok)
                    973: {
                    974:        struct roff_node        *n;
                    975:
                    976:        n = roff_node_alloc(man, line, pos, ROFFT_ELEM, tok);
                    977:        roff_node_append(man, n);
                    978:        man->next = ROFF_NEXT_CHILD;
                    979: }
                    980:
                    981: struct roff_node *
                    982: roff_block_alloc(struct roff_man *man, int line, int pos, int tok)
                    983: {
                    984:        struct roff_node        *n;
                    985:
                    986:        n = roff_node_alloc(man, line, pos, ROFFT_BLOCK, tok);
                    987:        roff_node_append(man, n);
                    988:        man->next = ROFF_NEXT_CHILD;
1.277     schwarze  989:        return n;
1.267     schwarze  990: }
                    991:
1.266     schwarze  992: struct roff_node *
                    993: roff_head_alloc(struct roff_man *man, int line, int pos, int tok)
                    994: {
                    995:        struct roff_node        *n;
                    996:
                    997:        n = roff_node_alloc(man, line, pos, ROFFT_HEAD, tok);
                    998:        roff_node_append(man, n);
                    999:        man->next = ROFF_NEXT_CHILD;
1.277     schwarze 1000:        return n;
1.266     schwarze 1001: }
                   1002:
                   1003: struct roff_node *
                   1004: roff_body_alloc(struct roff_man *man, int line, int pos, int tok)
                   1005: {
                   1006:        struct roff_node        *n;
                   1007:
                   1008:        n = roff_node_alloc(man, line, pos, ROFFT_BODY, tok);
                   1009:        roff_node_append(man, n);
                   1010:        man->next = ROFF_NEXT_CHILD;
1.277     schwarze 1011:        return n;
1.267     schwarze 1012: }
                   1013:
                   1014: void
                   1015: roff_addeqn(struct roff_man *man, const struct eqn *eqn)
                   1016: {
                   1017:        struct roff_node        *n;
                   1018:
                   1019:        n = roff_node_alloc(man, eqn->ln, eqn->pos, ROFFT_EQN, TOKEN_NONE);
                   1020:        n->eqn = eqn;
                   1021:        if (eqn->ln > man->last->line)
1.285     schwarze 1022:                n->flags |= NODE_LINE;
1.267     schwarze 1023:        roff_node_append(man, n);
                   1024:        man->next = ROFF_NEXT_SIBLING;
                   1025: }
                   1026:
                   1027: void
                   1028: roff_addtbl(struct roff_man *man, const struct tbl_span *tbl)
                   1029: {
                   1030:        struct roff_node        *n;
                   1031:
                   1032:        if (man->macroset == MACROSET_MAN)
1.302     schwarze 1033:                man_breakscope(man, ROFF_TS);
1.267     schwarze 1034:        n = roff_node_alloc(man, tbl->line, 0, ROFFT_TBL, TOKEN_NONE);
                   1035:        n->span = tbl;
                   1036:        roff_node_append(man, n);
1.286     schwarze 1037:        n->flags |= NODE_VALID | NODE_ENDED;
1.267     schwarze 1038:        man->next = ROFF_NEXT_SIBLING;
1.266     schwarze 1039: }
                   1040:
                   1041: void
                   1042: roff_node_unlink(struct roff_man *man, struct roff_node *n)
                   1043: {
                   1044:
                   1045:        /* Adjust siblings. */
                   1046:
                   1047:        if (n->prev)
                   1048:                n->prev->next = n->next;
                   1049:        if (n->next)
                   1050:                n->next->prev = n->prev;
                   1051:
                   1052:        /* Adjust parent. */
                   1053:
                   1054:        if (n->parent != NULL) {
                   1055:                if (n->parent->child == n)
                   1056:                        n->parent->child = n->next;
                   1057:                if (n->parent->last == n)
                   1058:                        n->parent->last = n->prev;
                   1059:        }
                   1060:
                   1061:        /* Adjust parse point. */
                   1062:
                   1063:        if (man == NULL)
                   1064:                return;
                   1065:        if (man->last == n) {
                   1066:                if (n->prev == NULL) {
                   1067:                        man->last = n->parent;
                   1068:                        man->next = ROFF_NEXT_CHILD;
                   1069:                } else {
                   1070:                        man->last = n->prev;
                   1071:                        man->next = ROFF_NEXT_SIBLING;
                   1072:                }
                   1073:        }
                   1074:        if (man->first == n)
                   1075:                man->first = NULL;
                   1076: }
                   1077:
                   1078: void
                   1079: roff_node_free(struct roff_node *n)
                   1080: {
                   1081:
                   1082:        if (n->args != NULL)
                   1083:                mdoc_argv_free(n->args);
                   1084:        if (n->type == ROFFT_BLOCK || n->type == ROFFT_ELEM)
                   1085:                free(n->norm);
                   1086:        free(n->string);
                   1087:        free(n);
                   1088: }
                   1089:
                   1090: void
                   1091: roff_node_delete(struct roff_man *man, struct roff_node *n)
                   1092: {
                   1093:
                   1094:        while (n->child != NULL)
                   1095:                roff_node_delete(man, n->child);
                   1096:        roff_node_unlink(man, n);
                   1097:        roff_node_free(n);
1.269     schwarze 1098: }
                   1099:
                   1100: void
                   1101: deroff(char **dest, const struct roff_node *n)
                   1102: {
                   1103:        char    *cp;
                   1104:        size_t   sz;
                   1105:
                   1106:        if (n->type != ROFFT_TEXT) {
                   1107:                for (n = n->child; n != NULL; n = n->next)
                   1108:                        deroff(dest, n);
                   1109:                return;
                   1110:        }
                   1111:
1.288     schwarze 1112:        /* Skip leading whitespace. */
1.269     schwarze 1113:
1.288     schwarze 1114:        for (cp = n->string; *cp != '\0'; cp++) {
1.289     schwarze 1115:                if (cp[0] == '\\' && cp[1] != '\0' &&
                   1116:                    strchr(" %&0^|~", cp[1]) != NULL)
1.269     schwarze 1117:                        cp++;
1.288     schwarze 1118:                else if ( ! isspace((unsigned char)*cp))
1.269     schwarze 1119:                        break;
                   1120:        }
                   1121:
1.289     schwarze 1122:        /* Skip trailing backslash. */
                   1123:
                   1124:        sz = strlen(cp);
1.290     schwarze 1125:        if (sz > 0 && cp[sz - 1] == '\\')
1.289     schwarze 1126:                sz--;
                   1127:
1.269     schwarze 1128:        /* Skip trailing whitespace. */
                   1129:
1.289     schwarze 1130:        for (; sz; sz--)
1.269     schwarze 1131:                if ( ! isspace((unsigned char)cp[sz-1]))
                   1132:                        break;
                   1133:
                   1134:        /* Skip empty strings. */
                   1135:
                   1136:        if (sz == 0)
                   1137:                return;
                   1138:
                   1139:        if (*dest == NULL) {
                   1140:                *dest = mandoc_strndup(cp, sz);
                   1141:                return;
                   1142:        }
                   1143:
                   1144:        mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
                   1145:        free(*dest);
                   1146:        *dest = cp;
1.266     schwarze 1147: }
                   1148:
                   1149: /* --- main functions of the roff parser ---------------------------------- */
                   1150:
1.94      kristaps 1151: /*
1.206     schwarze 1152:  * In the current line, expand escape sequences that tend to get
                   1153:  * used in numerical expressions and conditional requests.
                   1154:  * Also check the syntax of the remaining escape sequences.
1.154     kristaps 1155:  */
1.172     schwarze 1156: static enum rofferr
1.238     schwarze 1157: roff_res(struct roff *r, struct buf *buf, int ln, int pos)
1.94      kristaps 1158: {
1.208     schwarze 1159:        char             ubuf[24]; /* buffer to print the number */
1.205     schwarze 1160:        const char      *start; /* start of the string to process */
1.209     schwarze 1161:        char            *stesc; /* start of an escape sequence ('\\') */
1.108     schwarze 1162:        const char      *stnam; /* start of the name, after "[(*" */
                   1163:        const char      *cp;    /* end of the name, e.g. before ']' */
                   1164:        const char      *res;   /* the string to be substituted */
1.238     schwarze 1165:        char            *nbuf;  /* new buffer to copy buf->buf to */
1.181     schwarze 1166:        size_t           maxl;  /* expected length of the escape name */
                   1167:        size_t           naml;  /* actual length of the escape name */
1.237     schwarze 1168:        enum mandoc_esc  esc;   /* type of the escape sequence */
1.313     schwarze 1169:        enum mdoc_os     os_e;  /* kind of RCS id seen */
1.237     schwarze 1170:        int              inaml; /* length returned from mandoc_escape() */
1.181     schwarze 1171:        int              expand_count;  /* to avoid infinite loops */
1.206     schwarze 1172:        int              npos;  /* position in numeric expression */
1.218     schwarze 1173:        int              arg_complete; /* argument not interrupted by eol */
1.303     schwarze 1174:        int              done;  /* no more input available */
1.206     schwarze 1175:        char             term;  /* character terminating the escape */
1.94      kristaps 1176:
1.303     schwarze 1177:        /* Search forward for comments. */
                   1178:
                   1179:        done = 0;
                   1180:        start = buf->buf + pos;
                   1181:        for (stesc = buf->buf + pos; *stesc != '\0'; stesc++) {
                   1182:                if (stesc[0] != r->escape || stesc[1] == '\0')
                   1183:                        continue;
                   1184:                stesc++;
                   1185:                if (*stesc != '"' && *stesc != '#')
                   1186:                        continue;
1.313     schwarze 1187:
                   1188:                /* Comment found, look for RCS id. */
                   1189:
                   1190:                if ((cp = strstr(stesc, "$" "OpenBSD")) != NULL) {
                   1191:                        os_e = MDOC_OS_OPENBSD;
                   1192:                        cp += 8;
                   1193:                } else if ((cp = strstr(stesc, "$" "NetBSD")) != NULL) {
                   1194:                        os_e = MDOC_OS_NETBSD;
                   1195:                        cp += 7;
                   1196:                }
                   1197:                if (cp != NULL &&
                   1198:                    isalnum((unsigned char)*cp) == 0 &&
1.314   ! schwarze 1199:                    strchr(cp, '$') != NULL) {
        !          1200:                        if (r->man->meta.rcsids & (1 << os_e))
        !          1201:                                mandoc_msg(MANDOCERR_RCS_REP, r->parse,
        !          1202:                                    ln, stesc + 1 - buf->buf, stesc + 1);
1.313     schwarze 1203:                        r->man->meta.rcsids |= 1 << os_e;
1.314   ! schwarze 1204:                }
1.313     schwarze 1205:
                   1206:                /* Handle trailing whitespace. */
                   1207:
1.303     schwarze 1208:                cp = strchr(stesc--, '\0') - 1;
                   1209:                if (*cp == '\n') {
                   1210:                        done = 1;
                   1211:                        cp--;
                   1212:                }
                   1213:                if (*cp == ' ' || *cp == '\t')
                   1214:                        mandoc_msg(MANDOCERR_SPACE_EOL, r->parse,
                   1215:                            ln, cp - buf->buf, NULL);
                   1216:                while (stesc > start && stesc[-1] == ' ')
                   1217:                        stesc--;
                   1218:                *stesc = '\0';
                   1219:                break;
                   1220:        }
                   1221:        if (stesc == start)
                   1222:                return ROFF_CONT;
                   1223:        stesc--;
                   1224:
                   1225:        /* Notice the end of the input. */
                   1226:
                   1227:        if (*stesc == '\n') {
                   1228:                *stesc-- = '\0';
                   1229:                done = 1;
                   1230:        }
                   1231:
1.170     schwarze 1232:        expand_count = 0;
1.303     schwarze 1233:        while (stesc >= start) {
1.170     schwarze 1234:
1.205     schwarze 1235:                /* Search backwards for the next backslash. */
                   1236:
1.303     schwarze 1237:                if (*stesc != r->escape) {
                   1238:                        if (*stesc == '\\') {
                   1239:                                *stesc = '\0';
                   1240:                                buf->sz = mandoc_asprintf(&nbuf, "%s\\e%s",
                   1241:                                    buf->buf, stesc + 1) + 1;
                   1242:                                start = nbuf + pos;
                   1243:                                stesc = nbuf + (stesc - buf->buf);
                   1244:                                free(buf->buf);
                   1245:                                buf->buf = nbuf;
                   1246:                        }
                   1247:                        stesc--;
1.205     schwarze 1248:                        continue;
1.303     schwarze 1249:                }
1.205     schwarze 1250:
                   1251:                /* If it is escaped, skip it. */
                   1252:
                   1253:                for (cp = stesc - 1; cp >= start; cp--)
1.303     schwarze 1254:                        if (*cp != r->escape)
1.205     schwarze 1255:                                break;
                   1256:
1.238     schwarze 1257:                if ((stesc - cp) % 2 == 0) {
1.303     schwarze 1258:                        while (stesc > cp)
                   1259:                                *stesc-- = '\\';
1.205     schwarze 1260:                        continue;
1.303     schwarze 1261:                } else if (stesc[1] != '\0') {
                   1262:                        *stesc = '\\';
                   1263:                } else {
                   1264:                        *stesc-- = '\0';
                   1265:                        if (done)
                   1266:                                continue;
                   1267:                        else
                   1268:                                return ROFF_APPEND;
1.205     schwarze 1269:                }
1.108     schwarze 1270:
1.206     schwarze 1271:                /* Decide whether to expand or to check only. */
1.108     schwarze 1272:
1.206     schwarze 1273:                term = '\0';
1.205     schwarze 1274:                cp = stesc + 1;
1.181     schwarze 1275:                switch (*cp) {
1.207     schwarze 1276:                case '*':
1.181     schwarze 1277:                        res = NULL;
                   1278:                        break;
1.207     schwarze 1279:                case 'B':
                   1280:                case 'w':
1.206     schwarze 1281:                        term = cp[1];
                   1282:                        /* FALLTHROUGH */
1.207     schwarze 1283:                case 'n':
1.181     schwarze 1284:                        res = ubuf;
                   1285:                        break;
                   1286:                default:
1.237     schwarze 1287:                        esc = mandoc_escape(&cp, &stnam, &inaml);
                   1288:                        if (esc == ESCAPE_ERROR ||
                   1289:                            (esc == ESCAPE_SPECIAL &&
1.279     schwarze 1290:                             mchars_spec2cp(stnam, inaml) < 0))
1.219     schwarze 1291:                                mandoc_vmsg(MANDOCERR_ESC_BAD,
1.238     schwarze 1292:                                    r->parse, ln, (int)(stesc - buf->buf),
1.219     schwarze 1293:                                    "%.*s", (int)(cp - stesc), stesc);
1.303     schwarze 1294:                        stesc--;
1.205     schwarze 1295:                        continue;
1.152     kristaps 1296:                }
                   1297:
1.205     schwarze 1298:                if (EXPAND_LIMIT < ++expand_count) {
                   1299:                        mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
1.238     schwarze 1300:                            ln, (int)(stesc - buf->buf), NULL);
1.277     schwarze 1301:                        return ROFF_IGN;
1.205     schwarze 1302:                }
1.108     schwarze 1303:
                   1304:                /*
                   1305:                 * The third character decides the length
1.181     schwarze 1306:                 * of the name of the string or register.
1.108     schwarze 1307:                 * Save a pointer to the name.
                   1308:                 */
                   1309:
1.238     schwarze 1310:                if (term == '\0') {
1.206     schwarze 1311:                        switch (*++cp) {
1.207     schwarze 1312:                        case '\0':
1.206     schwarze 1313:                                maxl = 0;
                   1314:                                break;
1.207     schwarze 1315:                        case '(':
1.206     schwarze 1316:                                cp++;
                   1317:                                maxl = 2;
                   1318:                                break;
1.207     schwarze 1319:                        case '[':
1.206     schwarze 1320:                                cp++;
                   1321:                                term = ']';
                   1322:                                maxl = 0;
                   1323:                                break;
                   1324:                        default:
                   1325:                                maxl = 1;
                   1326:                                break;
                   1327:                        }
                   1328:                } else {
                   1329:                        cp += 2;
1.94      kristaps 1330:                        maxl = 0;
                   1331:                }
1.108     schwarze 1332:                stnam = cp;
1.94      kristaps 1333:
1.108     schwarze 1334:                /* Advance to the end of the name. */
1.94      kristaps 1335:
1.253     schwarze 1336:                naml = 0;
1.218     schwarze 1337:                arg_complete = 1;
1.253     schwarze 1338:                while (maxl == 0 || naml < maxl) {
1.238     schwarze 1339:                        if (*cp == '\0') {
1.219     schwarze 1340:                                mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1.238     schwarze 1341:                                    ln, (int)(stesc - buf->buf), stesc);
1.218     schwarze 1342:                                arg_complete = 0;
1.206     schwarze 1343:                                break;
1.153     kristaps 1344:                        }
1.238     schwarze 1345:                        if (maxl == 0 && *cp == term) {
1.206     schwarze 1346:                                cp++;
1.253     schwarze 1347:                                break;
                   1348:                        }
                   1349:                        if (*cp++ != '\\' || stesc[1] != 'w') {
                   1350:                                naml++;
                   1351:                                continue;
                   1352:                        }
                   1353:                        switch (mandoc_escape(&cp, NULL, NULL)) {
                   1354:                        case ESCAPE_SPECIAL:
                   1355:                        case ESCAPE_UNICODE:
                   1356:                        case ESCAPE_NUMBERED:
                   1357:                        case ESCAPE_OVERSTRIKE:
                   1358:                                naml++;
                   1359:                                break;
                   1360:                        default:
1.94      kristaps 1361:                                break;
1.206     schwarze 1362:                        }
1.94      kristaps 1363:                }
                   1364:
1.108     schwarze 1365:                /*
                   1366:                 * Retrieve the replacement string; if it is
                   1367:                 * undefined, resume searching for escapes.
                   1368:                 */
                   1369:
1.206     schwarze 1370:                switch (stesc[1]) {
1.207     schwarze 1371:                case '*':
1.218     schwarze 1372:                        if (arg_complete)
                   1373:                                res = roff_getstrn(r, stnam, naml);
1.206     schwarze 1374:                        break;
1.207     schwarze 1375:                case 'B':
1.206     schwarze 1376:                        npos = 0;
1.218     schwarze 1377:                        ubuf[0] = arg_complete &&
1.261     schwarze 1378:                            roff_evalnum(r, ln, stnam, &npos,
                   1379:                              NULL, ROFFNUM_SCALE) &&
1.218     schwarze 1380:                            stnam + npos + 1 == cp ? '1' : '0';
1.206     schwarze 1381:                        ubuf[1] = '\0';
                   1382:                        break;
1.207     schwarze 1383:                case 'n':
1.218     schwarze 1384:                        if (arg_complete)
                   1385:                                (void)snprintf(ubuf, sizeof(ubuf), "%d",
                   1386:                                    roff_getregn(r, stnam, naml));
                   1387:                        else
                   1388:                                ubuf[0] = '\0';
1.206     schwarze 1389:                        break;
1.207     schwarze 1390:                case 'w':
1.218     schwarze 1391:                        /* use even incomplete args */
1.208     schwarze 1392:                        (void)snprintf(ubuf, sizeof(ubuf), "%d",
1.206     schwarze 1393:                            24 * (int)naml);
                   1394:                        break;
                   1395:                }
1.94      kristaps 1396:
1.238     schwarze 1397:                if (res == NULL) {
1.219     schwarze 1398:                        mandoc_vmsg(MANDOCERR_STR_UNDEF,
1.238     schwarze 1399:                            r->parse, ln, (int)(stesc - buf->buf),
1.219     schwarze 1400:                            "%.*s", (int)naml, stnam);
1.142     kristaps 1401:                        res = "";
1.246     schwarze 1402:                } else if (buf->sz + strlen(res) > SHRT_MAX) {
                   1403:                        mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
                   1404:                            ln, (int)(stesc - buf->buf), NULL);
1.277     schwarze 1405:                        return ROFF_IGN;
1.94      kristaps 1406:                }
                   1407:
1.108     schwarze 1408:                /* Replace the escape sequence by the string. */
                   1409:
1.209     schwarze 1410:                *stesc = '\0';
1.238     schwarze 1411:                buf->sz = mandoc_asprintf(&nbuf, "%s%s%s",
                   1412:                    buf->buf, res, cp) + 1;
1.94      kristaps 1413:
1.205     schwarze 1414:                /* Prepare for the next replacement. */
1.94      kristaps 1415:
1.205     schwarze 1416:                start = nbuf + pos;
1.238     schwarze 1417:                stesc = nbuf + (stesc - buf->buf) + strlen(res);
                   1418:                free(buf->buf);
                   1419:                buf->buf = nbuf;
1.154     kristaps 1420:        }
1.277     schwarze 1421:        return ROFF_CONT;
1.154     kristaps 1422: }
                   1423:
                   1424: /*
1.275     schwarze 1425:  * Process text streams.
1.154     kristaps 1426:  */
                   1427: static enum rofferr
1.305     schwarze 1428: roff_parsetext(struct roff *r, struct buf *buf, int pos, int *offs)
1.154     kristaps 1429: {
                   1430:        size_t           sz;
                   1431:        const char      *start;
1.178     schwarze 1432:        char            *p;
                   1433:        int              isz;
1.154     kristaps 1434:        enum mandoc_esc  esc;
                   1435:
1.275     schwarze 1436:        /* Spring the input line trap. */
                   1437:
                   1438:        if (roffit_lines == 1) {
                   1439:                isz = mandoc_asprintf(&p, "%s\n.%s", buf->buf, roffit_macro);
                   1440:                free(buf->buf);
                   1441:                buf->buf = p;
                   1442:                buf->sz = isz + 1;
                   1443:                *offs = 0;
                   1444:                free(roffit_macro);
                   1445:                roffit_lines = 0;
1.277     schwarze 1446:                return ROFF_REPARSE;
1.275     schwarze 1447:        } else if (roffit_lines > 1)
                   1448:                --roffit_lines;
                   1449:
1.305     schwarze 1450:        if (roffce_node != NULL && buf->buf[pos] != '\0') {
                   1451:                if (roffce_lines < 1) {
                   1452:                        r->man->last = roffce_node;
                   1453:                        r->man->next = ROFF_NEXT_SIBLING;
                   1454:                        roffce_lines = 0;
                   1455:                        roffce_node = NULL;
                   1456:                } else
                   1457:                        roffce_lines--;
                   1458:        }
                   1459:
1.275     schwarze 1460:        /* Convert all breakable hyphens into ASCII_HYPH. */
                   1461:
1.238     schwarze 1462:        start = p = buf->buf + pos;
1.154     kristaps 1463:
1.238     schwarze 1464:        while (*p != '\0') {
1.154     kristaps 1465:                sz = strcspn(p, "-\\");
                   1466:                p += sz;
                   1467:
1.238     schwarze 1468:                if (*p == '\0')
1.159     kristaps 1469:                        break;
                   1470:
1.238     schwarze 1471:                if (*p == '\\') {
1.154     kristaps 1472:                        /* Skip over escapes. */
                   1473:                        p++;
1.189     schwarze 1474:                        esc = mandoc_escape((const char **)&p, NULL, NULL);
1.238     schwarze 1475:                        if (esc == ESCAPE_ERROR)
1.154     kristaps 1476:                                break;
1.264     schwarze 1477:                        while (*p == '-')
                   1478:                                p++;
1.155     kristaps 1479:                        continue;
1.159     kristaps 1480:                } else if (p == start) {
1.158     kristaps 1481:                        p++;
1.155     kristaps 1482:                        continue;
1.158     kristaps 1483:                }
1.155     kristaps 1484:
1.171     schwarze 1485:                if (isalpha((unsigned char)p[-1]) &&
                   1486:                    isalpha((unsigned char)p[1]))
1.155     kristaps 1487:                        *p = ASCII_HYPH;
                   1488:                p++;
1.94      kristaps 1489:        }
1.277     schwarze 1490:        return ROFF_CONT;
1.94      kristaps 1491: }
                   1492:
1.67      kristaps 1493: enum rofferr
1.238     schwarze 1494: roff_parseln(struct roff *r, int ln, struct buf *buf, int *offs)
1.67      kristaps 1495: {
1.294     schwarze 1496:        enum roff_tok    t;
1.109     kristaps 1497:        enum rofferr     e;
1.238     schwarze 1498:        int              pos;   /* parse point */
1.243     schwarze 1499:        int              spos;  /* saved parse point for messages */
1.238     schwarze 1500:        int              ppos;  /* original offset in buf->buf */
                   1501:        int              ctl;   /* macro line (boolean) */
                   1502:
                   1503:        ppos = pos = *offs;
1.79      kristaps 1504:
1.230     schwarze 1505:        /* Handle in-line equation delimiters. */
                   1506:
1.236     schwarze 1507:        if (r->tbl == NULL &&
                   1508:            r->last_eqn != NULL && r->last_eqn->delim &&
1.230     schwarze 1509:            (r->eqn == NULL || r->eqn_inline)) {
1.238     schwarze 1510:                e = roff_eqndelim(r, buf, pos);
1.230     schwarze 1511:                if (e == ROFF_REPARSE)
1.277     schwarze 1512:                        return e;
1.230     schwarze 1513:                assert(e == ROFF_CONT);
                   1514:        }
                   1515:
                   1516:        /* Expand some escape sequences. */
1.94      kristaps 1517:
1.238     schwarze 1518:        e = roff_res(r, buf, ln, pos);
1.303     schwarze 1519:        if (e == ROFF_IGN || e == ROFF_APPEND)
1.277     schwarze 1520:                return e;
1.238     schwarze 1521:        assert(e == ROFF_CONT);
1.94      kristaps 1522:
1.238     schwarze 1523:        ctl = roff_getcontrol(r, buf->buf, &pos);
1.130     kristaps 1524:
1.94      kristaps 1525:        /*
1.79      kristaps 1526:         * First, if a scope is open and we're not a macro, pass the
1.252     schwarze 1527:         * text through the macro's filter.
                   1528:         * Equations process all content themselves.
                   1529:         * Tables process almost all content themselves, but we want
                   1530:         * to warn about macros before passing it there.
1.79      kristaps 1531:         */
1.74      kristaps 1532:
1.252     schwarze 1533:        if (r->last != NULL && ! ctl) {
1.78      kristaps 1534:                t = r->last->tok;
1.238     schwarze 1535:                e = (*roffs[t].text)(r, t, buf, ln, pos, pos, offs);
1.294     schwarze 1536:                if (e == ROFF_IGN)
1.277     schwarze 1537:                        return e;
1.294     schwarze 1538:                assert(e == ROFF_CONT);
1.182     schwarze 1539:        }
1.252     schwarze 1540:        if (r->eqn != NULL)
1.277     schwarze 1541:                return eqn_read(&r->eqn, ln, buf->buf, ppos, offs);
1.252     schwarze 1542:        if (r->tbl != NULL && ( ! ctl || buf->buf[pos] == '\0'))
1.277     schwarze 1543:                return tbl_read(r->tbl, ln, buf->buf, ppos);
1.252     schwarze 1544:        if ( ! ctl)
1.305     schwarze 1545:                return roff_parsetext(r, buf, pos, offs);
1.228     schwarze 1546:
                   1547:        /* Skip empty request lines. */
                   1548:
1.238     schwarze 1549:        if (buf->buf[pos] == '"') {
1.228     schwarze 1550:                mandoc_msg(MANDOCERR_COMMENT_BAD, r->parse,
                   1551:                    ln, pos, NULL);
1.277     schwarze 1552:                return ROFF_IGN;
1.238     schwarze 1553:        } else if (buf->buf[pos] == '\0')
1.277     schwarze 1554:                return ROFF_IGN;
1.67      kristaps 1555:
1.79      kristaps 1556:        /*
                   1557:         * If a scope is open, go to the child handler for that macro,
                   1558:         * as it may want to preprocess before doing anything with it.
1.125     kristaps 1559:         * Don't do so if an equation is open.
1.79      kristaps 1560:         */
1.78      kristaps 1561:
1.79      kristaps 1562:        if (r->last) {
                   1563:                t = r->last->tok;
1.277     schwarze 1564:                return (*roffs[t].sub)(r, t, buf, ln, ppos, pos, offs);
1.79      kristaps 1565:        }
1.78      kristaps 1566:
1.243     schwarze 1567:        /* No scope is open.  This is a new request or macro. */
                   1568:
                   1569:        spos = pos;
                   1570:        t = roff_parse(r, buf->buf, &pos, ln, ppos);
                   1571:
                   1572:        /* Tables ignore most macros. */
                   1573:
1.308     schwarze 1574:        if (r->tbl != NULL && (t == TOKEN_NONE || t == ROFF_TS ||
1.309     schwarze 1575:            t == ROFF_br || t == ROFF_ce || t == ROFF_rj || t == ROFF_sp)) {
1.243     schwarze 1576:                mandoc_msg(MANDOCERR_TBLMACRO, r->parse,
                   1577:                    ln, pos, buf->buf + spos);
1.308     schwarze 1578:                if (t != TOKEN_NONE)
1.277     schwarze 1579:                        return ROFF_IGN;
1.257     schwarze 1580:                while (buf->buf[pos] != '\0' && buf->buf[pos] != ' ')
                   1581:                        pos++;
1.291     schwarze 1582:                while (buf->buf[pos] == ' ')
1.257     schwarze 1583:                        pos++;
1.277     schwarze 1584:                return tbl_read(r->tbl, ln, buf->buf, pos);
1.243     schwarze 1585:        }
                   1586:
1.305     schwarze 1587:        /* For now, let high level macros abort .ce mode. */
                   1588:
                   1589:        if (ctl && roffce_node != NULL &&
                   1590:            (t == TOKEN_NONE || t == ROFF_EQ || t == ROFF_TS)) {
                   1591:                r->man->last = roffce_node;
                   1592:                r->man->next = ROFF_NEXT_SIBLING;
                   1593:                roffce_lines = 0;
                   1594:                roffce_node = NULL;
                   1595:        }
                   1596:
1.79      kristaps 1597:        /*
1.243     schwarze 1598:         * This is neither a roff request nor a user-defined macro.
                   1599:         * Let the standard macro set parsers handle it.
1.79      kristaps 1600:         */
1.67      kristaps 1601:
1.294     schwarze 1602:        if (t == TOKEN_NONE)
1.277     schwarze 1603:                return ROFF_CONT;
1.243     schwarze 1604:
                   1605:        /* Execute a roff request or a user defined macro. */
1.67      kristaps 1606:
1.296     schwarze 1607:        return (*roffs[t].proc)(r, t, buf, ln, spos, pos, offs);
1.74      kristaps 1608: }
                   1609:
1.117     kristaps 1610: void
1.74      kristaps 1611: roff_endparse(struct roff *r)
                   1612: {
                   1613:
1.110     kristaps 1614:        if (r->last)
1.221     schwarze 1615:                mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
                   1616:                    r->last->line, r->last->col,
1.295     schwarze 1617:                    roff_name[r->last->tok]);
1.117     kristaps 1618:
1.125     kristaps 1619:        if (r->eqn) {
1.221     schwarze 1620:                mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
                   1621:                    r->eqn->eqn.ln, r->eqn->eqn.pos, "EQ");
1.151     kristaps 1622:                eqn_end(&r->eqn);
1.125     kristaps 1623:        }
                   1624:
1.117     kristaps 1625:        if (r->tbl) {
1.221     schwarze 1626:                mandoc_msg(MANDOCERR_BLK_NOEND, r->parse,
                   1627:                    r->tbl->line, r->tbl->pos, "TS");
1.151     kristaps 1628:                tbl_end(&r->tbl);
1.117     kristaps 1629:        }
1.67      kristaps 1630: }
                   1631:
                   1632: /*
                   1633:  * Parse a roff node's type from the input buffer.  This must be in the
                   1634:  * form of ".foo xxx" in the usual way.
                   1635:  */
1.294     schwarze 1636: static enum roff_tok
1.214     schwarze 1637: roff_parse(struct roff *r, char *buf, int *pos, int ln, int ppos)
1.67      kristaps 1638: {
1.214     schwarze 1639:        char            *cp;
1.106     kristaps 1640:        const char      *mac;
                   1641:        size_t           maclen;
1.294     schwarze 1642:        enum roff_tok    t;
1.67      kristaps 1643:
1.214     schwarze 1644:        cp = buf + *pos;
                   1645:
                   1646:        if ('\0' == *cp || '"' == *cp || '\t' == *cp || ' ' == *cp)
1.294     schwarze 1647:                return TOKEN_NONE;
1.67      kristaps 1648:
1.214     schwarze 1649:        mac = cp;
                   1650:        maclen = roff_getname(r, &cp, ln, ppos);
1.67      kristaps 1651:
1.306     schwarze 1652:        t = (r->current_string = roff_getstrn(r, mac, maclen)) ?
                   1653:            ROFF_USERDEF :
                   1654:            (r->current_string = roff_getrenn(r, mac, maclen)) ?
                   1655:            ROFF_RENAMED : roffhash_find(r->reqtab, mac, maclen);
1.67      kristaps 1656:
1.294     schwarze 1657:        if (t != TOKEN_NONE)
1.214     schwarze 1658:                *pos = cp - buf;
1.67      kristaps 1659:
1.277     schwarze 1660:        return t;
1.67      kristaps 1661: }
                   1662:
1.266     schwarze 1663: /* --- handling of request blocks ----------------------------------------- */
                   1664:
1.67      kristaps 1665: static enum rofferr
1.76      kristaps 1666: roff_cblock(ROFF_ARGS)
1.67      kristaps 1667: {
                   1668:
1.79      kristaps 1669:        /*
                   1670:         * A block-close `..' should only be invoked as a child of an
                   1671:         * ignore macro, otherwise raise a warning and just ignore it.
                   1672:         */
                   1673:
1.238     schwarze 1674:        if (r->last == NULL) {
1.221     schwarze 1675:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   1676:                    ln, ppos, "..");
1.277     schwarze 1677:                return ROFF_IGN;
1.76      kristaps 1678:        }
1.67      kristaps 1679:
1.81      kristaps 1680:        switch (r->last->tok) {
1.207     schwarze 1681:        case ROFF_am:
1.220     schwarze 1682:                /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1.207     schwarze 1683:        case ROFF_ami:
                   1684:        case ROFF_de:
1.108     schwarze 1685:                /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1.207     schwarze 1686:        case ROFF_dei:
                   1687:        case ROFF_ig:
1.81      kristaps 1688:                break;
                   1689:        default:
1.221     schwarze 1690:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   1691:                    ln, ppos, "..");
1.277     schwarze 1692:                return ROFF_IGN;
1.76      kristaps 1693:        }
1.67      kristaps 1694:
1.238     schwarze 1695:        if (buf->buf[pos] != '\0')
1.217     schwarze 1696:                mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
1.238     schwarze 1697:                    ".. %s", buf->buf + pos);
1.71      kristaps 1698:
                   1699:        roffnode_pop(r);
1.76      kristaps 1700:        roffnode_cleanscope(r);
1.277     schwarze 1701:        return ROFF_IGN;
1.71      kristaps 1702:
1.67      kristaps 1703: }
                   1704:
1.76      kristaps 1705: static void
                   1706: roffnode_cleanscope(struct roff *r)
1.67      kristaps 1707: {
                   1708:
1.76      kristaps 1709:        while (r->last) {
1.173     schwarze 1710:                if (--r->last->endspan != 0)
1.76      kristaps 1711:                        break;
                   1712:                roffnode_pop(r);
                   1713:        }
1.67      kristaps 1714: }
                   1715:
1.195     schwarze 1716: static void
                   1717: roff_ccond(struct roff *r, int ln, int ppos)
1.74      kristaps 1718: {
                   1719:
1.76      kristaps 1720:        if (NULL == r->last) {
1.221     schwarze 1721:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   1722:                    ln, ppos, "\\}");
1.195     schwarze 1723:                return;
1.76      kristaps 1724:        }
                   1725:
1.82      kristaps 1726:        switch (r->last->tok) {
1.207     schwarze 1727:        case ROFF_el:
                   1728:        case ROFF_ie:
                   1729:        case ROFF_if:
1.82      kristaps 1730:                break;
                   1731:        default:
1.221     schwarze 1732:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   1733:                    ln, ppos, "\\}");
1.195     schwarze 1734:                return;
1.75      kristaps 1735:        }
                   1736:
1.76      kristaps 1737:        if (r->last->endspan > -1) {
1.221     schwarze 1738:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   1739:                    ln, ppos, "\\}");
1.195     schwarze 1740:                return;
1.76      kristaps 1741:        }
                   1742:
1.75      kristaps 1743:        roffnode_pop(r);
1.76      kristaps 1744:        roffnode_cleanscope(r);
1.195     schwarze 1745:        return;
1.76      kristaps 1746: }
                   1747:
                   1748: static enum rofferr
1.80      kristaps 1749: roff_block(ROFF_ARGS)
1.76      kristaps 1750: {
1.220     schwarze 1751:        const char      *name;
                   1752:        char            *iname, *cp;
1.213     schwarze 1753:        size_t           namesz;
1.106     kristaps 1754:
1.220     schwarze 1755:        /* Ignore groff compatibility mode for now. */
1.76      kristaps 1756:
1.238     schwarze 1757:        if (tok == ROFF_de1)
1.220     schwarze 1758:                tok = ROFF_de;
1.251     schwarze 1759:        else if (tok == ROFF_dei1)
                   1760:                tok = ROFF_dei;
1.238     schwarze 1761:        else if (tok == ROFF_am1)
1.220     schwarze 1762:                tok = ROFF_am;
1.251     schwarze 1763:        else if (tok == ROFF_ami1)
                   1764:                tok = ROFF_ami;
1.220     schwarze 1765:
                   1766:        /* Parse the macro name argument. */
                   1767:
1.238     schwarze 1768:        cp = buf->buf + pos;
                   1769:        if (tok == ROFF_ig) {
1.220     schwarze 1770:                iname = NULL;
                   1771:                namesz = 0;
                   1772:        } else {
                   1773:                iname = cp;
                   1774:                namesz = roff_getname(r, &cp, ln, ppos);
                   1775:                iname[namesz] = '\0';
                   1776:        }
1.107     kristaps 1777:
1.220     schwarze 1778:        /* Resolve the macro name argument if it is indirect. */
1.107     kristaps 1779:
1.238     schwarze 1780:        if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
                   1781:                if ((name = roff_getstrn(r, iname, namesz)) == NULL) {
1.220     schwarze 1782:                        mandoc_vmsg(MANDOCERR_STR_UNDEF,
1.238     schwarze 1783:                            r->parse, ln, (int)(iname - buf->buf),
1.220     schwarze 1784:                            "%.*s", (int)namesz, iname);
                   1785:                        namesz = 0;
                   1786:                } else
                   1787:                        namesz = strlen(name);
                   1788:        } else
                   1789:                name = iname;
1.107     kristaps 1790:
1.238     schwarze 1791:        if (namesz == 0 && tok != ROFF_ig) {
1.220     schwarze 1792:                mandoc_msg(MANDOCERR_REQ_EMPTY, r->parse,
1.295     schwarze 1793:                    ln, ppos, roff_name[tok]);
1.277     schwarze 1794:                return ROFF_IGN;
1.220     schwarze 1795:        }
1.80      kristaps 1796:
1.106     kristaps 1797:        roffnode_push(r, tok, name, ln, ppos);
                   1798:
                   1799:        /*
                   1800:         * At the beginning of a `de' macro, clear the existing string
                   1801:         * with the same name, if there is one.  New content will be
1.193     schwarze 1802:         * appended from roff_block_text() in multiline mode.
1.106     kristaps 1803:         */
1.107     kristaps 1804:
1.311     schwarze 1805:        if (tok == ROFF_de || tok == ROFF_dei) {
1.213     schwarze 1806:                roff_setstrn(&r->strtab, name, namesz, "", 0, 0);
1.311     schwarze 1807:                roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
                   1808:        }
1.76      kristaps 1809:
1.238     schwarze 1810:        if (*cp == '\0')
1.277     schwarze 1811:                return ROFF_IGN;
1.78      kristaps 1812:
1.220     schwarze 1813:        /* Get the custom end marker. */
1.107     kristaps 1814:
1.220     schwarze 1815:        iname = cp;
1.213     schwarze 1816:        namesz = roff_getname(r, &cp, ln, ppos);
1.220     schwarze 1817:
                   1818:        /* Resolve the end marker if it is indirect. */
                   1819:
1.238     schwarze 1820:        if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
                   1821:                if ((name = roff_getstrn(r, iname, namesz)) == NULL) {
1.220     schwarze 1822:                        mandoc_vmsg(MANDOCERR_STR_UNDEF,
1.238     schwarze 1823:                            r->parse, ln, (int)(iname - buf->buf),
1.220     schwarze 1824:                            "%.*s", (int)namesz, iname);
                   1825:                        namesz = 0;
                   1826:                } else
                   1827:                        namesz = strlen(name);
                   1828:        } else
                   1829:                name = iname;
                   1830:
1.213     schwarze 1831:        if (namesz)
                   1832:                r->last->end = mandoc_strndup(name, namesz);
1.78      kristaps 1833:
1.238     schwarze 1834:        if (*cp != '\0')
1.217     schwarze 1835:                mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
1.295     schwarze 1836:                    ln, pos, ".%s ... %s", roff_name[tok], cp);
1.74      kristaps 1837:
1.277     schwarze 1838:        return ROFF_IGN;
1.78      kristaps 1839: }
                   1840:
                   1841: static enum rofferr
1.80      kristaps 1842: roff_block_sub(ROFF_ARGS)
1.79      kristaps 1843: {
1.294     schwarze 1844:        enum roff_tok   t;
1.79      kristaps 1845:        int             i, j;
                   1846:
                   1847:        /*
                   1848:         * First check whether a custom macro exists at this level.  If
                   1849:         * it does, then check against it.  This is some of groff's
                   1850:         * stranger behaviours.  If we encountered a custom end-scope
                   1851:         * tag and that tag also happens to be a "real" macro, then we
                   1852:         * need to try interpreting it again as a real macro.  If it's
                   1853:         * not, then return ignore.  Else continue.
                   1854:         */
                   1855:
                   1856:        if (r->last->end) {
1.130     kristaps 1857:                for (i = pos, j = 0; r->last->end[j]; j++, i++)
1.238     schwarze 1858:                        if (buf->buf[i] != r->last->end[j])
1.79      kristaps 1859:                                break;
                   1860:
1.238     schwarze 1861:                if (r->last->end[j] == '\0' &&
                   1862:                    (buf->buf[i] == '\0' ||
                   1863:                     buf->buf[i] == ' ' ||
                   1864:                     buf->buf[i] == '\t')) {
1.79      kristaps 1865:                        roffnode_pop(r);
                   1866:                        roffnode_cleanscope(r);
                   1867:
1.238     schwarze 1868:                        while (buf->buf[i] == ' ' || buf->buf[i] == '\t')
1.130     kristaps 1869:                                i++;
                   1870:
                   1871:                        pos = i;
1.238     schwarze 1872:                        if (roff_parse(r, buf->buf, &pos, ln, ppos) !=
1.294     schwarze 1873:                            TOKEN_NONE)
1.277     schwarze 1874:                                return ROFF_RERUN;
                   1875:                        return ROFF_IGN;
1.79      kristaps 1876:                }
                   1877:        }
                   1878:
                   1879:        /*
                   1880:         * If we have no custom end-query or lookup failed, then try
                   1881:         * pulling it out of the hashtable.
                   1882:         */
                   1883:
1.238     schwarze 1884:        t = roff_parse(r, buf->buf, &pos, ln, ppos);
1.79      kristaps 1885:
1.238     schwarze 1886:        if (t != ROFF_cblock) {
                   1887:                if (tok != ROFF_ig)
                   1888:                        roff_setstr(r, r->last->name, buf->buf + ppos, 2);
1.277     schwarze 1889:                return ROFF_IGN;
1.106     kristaps 1890:        }
1.79      kristaps 1891:
1.277     schwarze 1892:        return (*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs);
1.79      kristaps 1893: }
                   1894:
                   1895: static enum rofferr
1.80      kristaps 1896: roff_block_text(ROFF_ARGS)
1.78      kristaps 1897: {
                   1898:
1.238     schwarze 1899:        if (tok != ROFF_ig)
                   1900:                roff_setstr(r, r->last->name, buf->buf + pos, 2);
1.106     kristaps 1901:
1.277     schwarze 1902:        return ROFF_IGN;
1.78      kristaps 1903: }
                   1904:
                   1905: static enum rofferr
1.82      kristaps 1906: roff_cond_sub(ROFF_ARGS)
                   1907: {
1.294     schwarze 1908:        enum roff_tok    t;
1.139     kristaps 1909:        char            *ep;
1.198     schwarze 1910:        int              rr;
1.82      kristaps 1911:
                   1912:        rr = r->last->rule;
1.139     kristaps 1913:        roffnode_cleanscope(r);
1.238     schwarze 1914:        t = roff_parse(r, buf->buf, &pos, ln, ppos);
1.82      kristaps 1915:
1.139     kristaps 1916:        /*
1.177     schwarze 1917:         * Fully handle known macros when they are structurally
                   1918:         * required or when the conditional evaluated to true.
1.87      kristaps 1919:         */
                   1920:
1.294     schwarze 1921:        if (t != TOKEN_NONE && (rr || roffs[t].flags & ROFFMAC_STRUCT))
1.277     schwarze 1922:                return (*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs);
1.144     kristaps 1923:
1.196     schwarze 1924:        /*
                   1925:         * If `\}' occurs on a macro line without a preceding macro,
                   1926:         * drop the line completely.
                   1927:         */
                   1928:
1.238     schwarze 1929:        ep = buf->buf + pos;
                   1930:        if (ep[0] == '\\' && ep[1] == '}')
1.198     schwarze 1931:                rr = 0;
1.196     schwarze 1932:
1.177     schwarze 1933:        /* Always check for the closing delimiter `\}'. */
1.144     kristaps 1934:
1.238     schwarze 1935:        while ((ep = strchr(ep, '\\')) != NULL) {
                   1936:                if (*(++ep) == '}') {
1.197     schwarze 1937:                        *ep = '&';
1.238     schwarze 1938:                        roff_ccond(r, ln, ep - buf->buf - 1);
1.197     schwarze 1939:                }
1.247     schwarze 1940:                if (*ep != '\0')
                   1941:                        ++ep;
1.177     schwarze 1942:        }
1.277     schwarze 1943:        return rr ? ROFF_CONT : ROFF_IGN;
1.82      kristaps 1944: }
                   1945:
                   1946: static enum rofferr
                   1947: roff_cond_text(ROFF_ARGS)
1.78      kristaps 1948: {
1.140     kristaps 1949:        char            *ep;
1.198     schwarze 1950:        int              rr;
1.82      kristaps 1951:
                   1952:        rr = r->last->rule;
1.140     kristaps 1953:        roffnode_cleanscope(r);
1.82      kristaps 1954:
1.238     schwarze 1955:        ep = buf->buf + pos;
                   1956:        while ((ep = strchr(ep, '\\')) != NULL) {
                   1957:                if (*(++ep) == '}') {
1.197     schwarze 1958:                        *ep = '&';
1.238     schwarze 1959:                        roff_ccond(r, ln, ep - buf->buf - 1);
1.197     schwarze 1960:                }
1.247     schwarze 1961:                if (*ep != '\0')
                   1962:                        ++ep;
1.78      kristaps 1963:        }
1.277     schwarze 1964:        return rr ? ROFF_CONT : ROFF_IGN;
1.74      kristaps 1965: }
                   1966:
1.266     schwarze 1967: /* --- handling of numeric and conditional expressions -------------------- */
                   1968:
1.204     schwarze 1969: /*
                   1970:  * Parse a single signed integer number.  Stop at the first non-digit.
                   1971:  * If there is at least one digit, return success and advance the
                   1972:  * parse point, else return failure and let the parse point unchanged.
                   1973:  * Ignore overflows, treat them just like the C language.
                   1974:  */
1.184     schwarze 1975: static int
1.261     schwarze 1976: roff_getnum(const char *v, int *pos, int *res, int flags)
1.184     schwarze 1977: {
1.261     schwarze 1978:        int      myres, scaled, n, p;
1.206     schwarze 1979:
                   1980:        if (NULL == res)
                   1981:                res = &myres;
1.184     schwarze 1982:
                   1983:        p = *pos;
                   1984:        n = v[p] == '-';
1.261     schwarze 1985:        if (n || v[p] == '+')
1.184     schwarze 1986:                p++;
                   1987:
1.261     schwarze 1988:        if (flags & ROFFNUM_WHITE)
                   1989:                while (isspace((unsigned char)v[p]))
                   1990:                        p++;
                   1991:
1.184     schwarze 1992:        for (*res = 0; isdigit((unsigned char)v[p]); p++)
1.204     schwarze 1993:                *res = 10 * *res + v[p] - '0';
1.184     schwarze 1994:        if (p == *pos + n)
                   1995:                return 0;
                   1996:
                   1997:        if (n)
                   1998:                *res = -*res;
                   1999:
1.254     schwarze 2000:        /* Each number may be followed by one optional scaling unit. */
                   2001:
                   2002:        switch (v[p]) {
                   2003:        case 'f':
1.261     schwarze 2004:                scaled = *res * 65536;
1.254     schwarze 2005:                break;
                   2006:        case 'i':
1.261     schwarze 2007:                scaled = *res * 240;
1.254     schwarze 2008:                break;
                   2009:        case 'c':
1.261     schwarze 2010:                scaled = *res * 240 / 2.54;
1.254     schwarze 2011:                break;
                   2012:        case 'v':
                   2013:        case 'P':
1.261     schwarze 2014:                scaled = *res * 40;
1.254     schwarze 2015:                break;
                   2016:        case 'm':
                   2017:        case 'n':
1.261     schwarze 2018:                scaled = *res * 24;
1.254     schwarze 2019:                break;
                   2020:        case 'p':
1.261     schwarze 2021:                scaled = *res * 10 / 3;
1.254     schwarze 2022:                break;
                   2023:        case 'u':
1.261     schwarze 2024:                scaled = *res;
1.254     schwarze 2025:                break;
                   2026:        case 'M':
1.261     schwarze 2027:                scaled = *res * 6 / 25;
1.254     schwarze 2028:                break;
                   2029:        default:
1.261     schwarze 2030:                scaled = *res;
1.254     schwarze 2031:                p--;
                   2032:                break;
                   2033:        }
1.261     schwarze 2034:        if (flags & ROFFNUM_SCALE)
                   2035:                *res = scaled;
1.254     schwarze 2036:
                   2037:        *pos = p + 1;
1.277     schwarze 2038:        return 1;
1.184     schwarze 2039: }
                   2040:
1.198     schwarze 2041: /*
                   2042:  * Evaluate a string comparison condition.
                   2043:  * The first character is the delimiter.
                   2044:  * Succeed if the string up to its second occurrence
                   2045:  * matches the string up to its third occurence.
                   2046:  * Advance the cursor after the third occurrence
                   2047:  * or lacking that, to the end of the line.
                   2048:  */
                   2049: static int
                   2050: roff_evalstrcond(const char *v, int *pos)
                   2051: {
                   2052:        const char      *s1, *s2, *s3;
                   2053:        int              match;
                   2054:
                   2055:        match = 0;
                   2056:        s1 = v + *pos;          /* initial delimiter */
                   2057:        s2 = s1 + 1;            /* for scanning the first string */
                   2058:        s3 = strchr(s2, *s1);   /* for scanning the second string */
                   2059:
                   2060:        if (NULL == s3)         /* found no middle delimiter */
                   2061:                goto out;
                   2062:
                   2063:        while ('\0' != *++s3) {
                   2064:                if (*s2 != *s3) {  /* mismatch */
                   2065:                        s3 = strchr(s3, *s1);
                   2066:                        break;
                   2067:                }
                   2068:                if (*s3 == *s1) {  /* found the final delimiter */
                   2069:                        match = 1;
                   2070:                        break;
                   2071:                }
                   2072:                s2++;
                   2073:        }
                   2074:
                   2075: out:
                   2076:        if (NULL == s3)
                   2077:                s3 = strchr(s2, '\0');
1.242     schwarze 2078:        else if (*s3 != '\0')
1.198     schwarze 2079:                s3++;
                   2080:        *pos = s3 - v;
1.277     schwarze 2081:        return match;
1.198     schwarze 2082: }
                   2083:
1.204     schwarze 2084: /*
                   2085:  * Evaluate an optionally negated single character, numerical,
                   2086:  * or string condition.
                   2087:  */
1.198     schwarze 2088: static int
1.271     schwarze 2089: roff_evalcond(struct roff *r, int ln, char *v, int *pos)
1.88      kristaps 2090: {
1.271     schwarze 2091:        char    *cp, *name;
                   2092:        size_t   sz;
1.310     schwarze 2093:        int      number, savepos, istrue, wanttrue;
1.88      kristaps 2094:
1.198     schwarze 2095:        if ('!' == v[*pos]) {
                   2096:                wanttrue = 0;
                   2097:                (*pos)++;
                   2098:        } else
                   2099:                wanttrue = 1;
                   2100:
1.88      kristaps 2101:        switch (v[*pos]) {
1.240     schwarze 2102:        case '\0':
1.277     schwarze 2103:                return 0;
1.207     schwarze 2104:        case 'n':
                   2105:        case 'o':
1.88      kristaps 2106:                (*pos)++;
1.277     schwarze 2107:                return wanttrue;
1.207     schwarze 2108:        case 'c':
                   2109:        case 'e':
                   2110:        case 't':
1.239     schwarze 2111:        case 'v':
1.88      kristaps 2112:                (*pos)++;
1.277     schwarze 2113:                return !wanttrue;
1.310     schwarze 2114:        case 'd':
1.271     schwarze 2115:        case 'r':
1.310     schwarze 2116:                cp = v + *pos + 1;
                   2117:                while (*cp == ' ')
                   2118:                        cp++;
                   2119:                name = cp;
                   2120:                sz = roff_getname(r, &cp, ln, cp - v);
                   2121:                istrue = sz && (v[*pos] == 'r' ? roff_hasregn(r, name, sz) :
                   2122:                    (roff_getstrn(r, name, sz) != NULL ||
                   2123:                     roff_getrenn(r, name, sz) != NULL));
1.271     schwarze 2124:                *pos = cp - v;
1.310     schwarze 2125:                return istrue == wanttrue;
1.88      kristaps 2126:        default:
                   2127:                break;
                   2128:        }
                   2129:
1.241     schwarze 2130:        savepos = *pos;
1.261     schwarze 2131:        if (roff_evalnum(r, ln, v, pos, &number, ROFFNUM_SCALE))
1.277     schwarze 2132:                return (number > 0) == wanttrue;
1.241     schwarze 2133:        else if (*pos == savepos)
1.277     schwarze 2134:                return roff_evalstrcond(v, pos) == wanttrue;
1.204     schwarze 2135:        else
1.277     schwarze 2136:                return 0;
1.88      kristaps 2137: }
                   2138:
1.74      kristaps 2139: static enum rofferr
1.103     kristaps 2140: roff_line_ignore(ROFF_ARGS)
1.89      kristaps 2141: {
1.123     schwarze 2142:
1.277     schwarze 2143:        return ROFF_IGN;
1.89      kristaps 2144: }
                   2145:
1.104     kristaps 2146: static enum rofferr
1.251     schwarze 2147: roff_insec(ROFF_ARGS)
                   2148: {
                   2149:
                   2150:        mandoc_msg(MANDOCERR_REQ_INSEC, r->parse,
1.295     schwarze 2151:            ln, ppos, roff_name[tok]);
1.277     schwarze 2152:        return ROFF_IGN;
1.251     schwarze 2153: }
                   2154:
                   2155: static enum rofferr
                   2156: roff_unsupp(ROFF_ARGS)
                   2157: {
                   2158:
                   2159:        mandoc_msg(MANDOCERR_REQ_UNSUPP, r->parse,
1.295     schwarze 2160:            ln, ppos, roff_name[tok]);
1.277     schwarze 2161:        return ROFF_IGN;
1.251     schwarze 2162: }
                   2163:
                   2164: static enum rofferr
1.82      kristaps 2165: roff_cond(ROFF_ARGS)
1.74      kristaps 2166: {
1.173     schwarze 2167:
                   2168:        roffnode_push(r, tok, NULL, ln, ppos);
1.74      kristaps 2169:
1.207     schwarze 2170:        /*
1.134     kristaps 2171:         * An `.el' has no conditional body: it will consume the value
                   2172:         * of the current rstack entry set in prior `ie' calls or
1.207     schwarze 2173:         * defaults to DENY.
1.134     kristaps 2174:         *
                   2175:         * If we're not an `el', however, then evaluate the conditional.
                   2176:         */
1.133     kristaps 2177:
1.238     schwarze 2178:        r->last->rule = tok == ROFF_el ?
1.207     schwarze 2179:            (r->rstackpos < 0 ? 0 : r->rstack[r->rstackpos--]) :
1.238     schwarze 2180:            roff_evalcond(r, ln, buf->buf, &pos);
1.77      kristaps 2181:
1.134     kristaps 2182:        /*
                   2183:         * An if-else will put the NEGATION of the current evaluated
                   2184:         * conditional into the stack of rules.
                   2185:         */
                   2186:
1.238     schwarze 2187:        if (tok == ROFF_ie) {
1.223     schwarze 2188:                if (r->rstackpos + 1 == r->rstacksz) {
                   2189:                        r->rstacksz += 16;
                   2190:                        r->rstack = mandoc_reallocarray(r->rstack,
                   2191:                            r->rstacksz, sizeof(int));
1.134     kristaps 2192:                }
1.198     schwarze 2193:                r->rstack[++r->rstackpos] = !r->last->rule;
1.82      kristaps 2194:        }
1.88      kristaps 2195:
                   2196:        /* If the parent has false as its rule, then so do we. */
                   2197:
1.198     schwarze 2198:        if (r->last->parent && !r->last->parent->rule)
                   2199:                r->last->rule = 0;
1.88      kristaps 2200:
                   2201:        /*
1.173     schwarze 2202:         * Determine scope.
                   2203:         * If there is nothing on the line after the conditional,
                   2204:         * not even whitespace, use next-line scope.
1.88      kristaps 2205:         */
1.74      kristaps 2206:
1.238     schwarze 2207:        if (buf->buf[pos] == '\0') {
1.173     schwarze 2208:                r->last->endspan = 2;
                   2209:                goto out;
                   2210:        }
                   2211:
1.238     schwarze 2212:        while (buf->buf[pos] == ' ')
1.173     schwarze 2213:                pos++;
                   2214:
                   2215:        /* An opening brace requests multiline scope. */
1.75      kristaps 2216:
1.238     schwarze 2217:        if (buf->buf[pos] == '\\' && buf->buf[pos + 1] == '{') {
1.75      kristaps 2218:                r->last->endspan = -1;
                   2219:                pos += 2;
1.272     schwarze 2220:                while (buf->buf[pos] == ' ')
                   2221:                        pos++;
1.173     schwarze 2222:                goto out;
1.207     schwarze 2223:        }
1.74      kristaps 2224:
1.77      kristaps 2225:        /*
1.173     schwarze 2226:         * Anything else following the conditional causes
                   2227:         * single-line scope.  Warn if the scope contains
                   2228:         * nothing but trailing whitespace.
1.77      kristaps 2229:         */
                   2230:
1.238     schwarze 2231:        if (buf->buf[pos] == '\0')
1.216     schwarze 2232:                mandoc_msg(MANDOCERR_COND_EMPTY, r->parse,
1.295     schwarze 2233:                    ln, ppos, roff_name[tok]);
1.77      kristaps 2234:
1.173     schwarze 2235:        r->last->endspan = 1;
1.74      kristaps 2236:
1.173     schwarze 2237: out:
1.75      kristaps 2238:        *offs = pos;
1.277     schwarze 2239:        return ROFF_RERUN;
1.83      schwarze 2240: }
                   2241:
                   2242: static enum rofferr
1.92      schwarze 2243: roff_ds(ROFF_ARGS)
                   2244: {
1.212     schwarze 2245:        char            *string;
                   2246:        const char      *name;
                   2247:        size_t           namesz;
1.96      kristaps 2248:
1.251     schwarze 2249:        /* Ignore groff compatibility mode for now. */
                   2250:
                   2251:        if (tok == ROFF_ds1)
                   2252:                tok = ROFF_ds;
                   2253:        else if (tok == ROFF_as1)
                   2254:                tok = ROFF_as;
                   2255:
1.96      kristaps 2256:        /*
1.212     schwarze 2257:         * The first word is the name of the string.
                   2258:         * If it is empty or terminated by an escape sequence,
                   2259:         * abort the `ds' request without defining anything.
1.96      kristaps 2260:         */
1.92      schwarze 2261:
1.238     schwarze 2262:        name = string = buf->buf + pos;
                   2263:        if (*name == '\0')
1.277     schwarze 2264:                return ROFF_IGN;
1.92      schwarze 2265:
1.212     schwarze 2266:        namesz = roff_getname(r, &string, ln, pos);
1.238     schwarze 2267:        if (name[namesz] == '\\')
1.277     schwarze 2268:                return ROFF_IGN;
1.212     schwarze 2269:
                   2270:        /* Read past the initial double-quote, if any. */
1.238     schwarze 2271:        if (*string == '"')
1.92      schwarze 2272:                string++;
                   2273:
1.96      kristaps 2274:        /* The rest is the value. */
1.212     schwarze 2275:        roff_setstrn(&r->strtab, name, namesz, string, strlen(string),
                   2276:            ROFF_as == tok);
1.311     schwarze 2277:        roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
1.277     schwarze 2278:        return ROFF_IGN;
1.92      schwarze 2279: }
                   2280:
1.204     schwarze 2281: /*
                   2282:  * Parse a single operator, one or two characters long.
                   2283:  * If the operator is recognized, return success and advance the
                   2284:  * parse point, else return failure and let the parse point unchanged.
                   2285:  */
                   2286: static int
                   2287: roff_getop(const char *v, int *pos, char *res)
                   2288: {
                   2289:
                   2290:        *res = v[*pos];
                   2291:
                   2292:        switch (*res) {
1.207     schwarze 2293:        case '+':
                   2294:        case '-':
                   2295:        case '*':
                   2296:        case '/':
                   2297:        case '%':
                   2298:        case '&':
                   2299:        case ':':
1.204     schwarze 2300:                break;
                   2301:        case '<':
                   2302:                switch (v[*pos + 1]) {
1.207     schwarze 2303:                case '=':
1.204     schwarze 2304:                        *res = 'l';
                   2305:                        (*pos)++;
                   2306:                        break;
1.207     schwarze 2307:                case '>':
1.204     schwarze 2308:                        *res = '!';
                   2309:                        (*pos)++;
                   2310:                        break;
1.207     schwarze 2311:                case '?':
1.204     schwarze 2312:                        *res = 'i';
                   2313:                        (*pos)++;
                   2314:                        break;
                   2315:                default:
                   2316:                        break;
                   2317:                }
                   2318:                break;
                   2319:        case '>':
                   2320:                switch (v[*pos + 1]) {
1.207     schwarze 2321:                case '=':
1.204     schwarze 2322:                        *res = 'g';
                   2323:                        (*pos)++;
                   2324:                        break;
1.207     schwarze 2325:                case '?':
1.204     schwarze 2326:                        *res = 'a';
                   2327:                        (*pos)++;
                   2328:                        break;
                   2329:                default:
                   2330:                        break;
                   2331:                }
                   2332:                break;
                   2333:        case '=':
                   2334:                if ('=' == v[*pos + 1])
                   2335:                        (*pos)++;
                   2336:                break;
                   2337:        default:
1.277     schwarze 2338:                return 0;
1.204     schwarze 2339:        }
                   2340:        (*pos)++;
                   2341:
1.277     schwarze 2342:        return *res;
1.204     schwarze 2343: }
                   2344:
                   2345: /*
                   2346:  * Evaluate either a parenthesized numeric expression
                   2347:  * or a single signed integer number.
                   2348:  */
                   2349: static int
1.235     schwarze 2350: roff_evalpar(struct roff *r, int ln,
1.261     schwarze 2351:        const char *v, int *pos, int *res, int flags)
1.204     schwarze 2352: {
                   2353:
                   2354:        if ('(' != v[*pos])
1.277     schwarze 2355:                return roff_getnum(v, pos, res, flags);
1.204     schwarze 2356:
                   2357:        (*pos)++;
1.261     schwarze 2358:        if ( ! roff_evalnum(r, ln, v, pos, res, flags | ROFFNUM_WHITE))
1.277     schwarze 2359:                return 0;
1.204     schwarze 2360:
1.206     schwarze 2361:        /*
                   2362:         * Omission of the closing parenthesis
                   2363:         * is an error in validation mode,
                   2364:         * but ignored in evaluation mode.
                   2365:         */
                   2366:
1.204     schwarze 2367:        if (')' == v[*pos])
                   2368:                (*pos)++;
1.206     schwarze 2369:        else if (NULL == res)
1.277     schwarze 2370:                return 0;
1.204     schwarze 2371:
1.277     schwarze 2372:        return 1;
1.204     schwarze 2373: }
                   2374:
                   2375: /*
                   2376:  * Evaluate a complete numeric expression.
                   2377:  * Proceed left to right, there is no concept of precedence.
                   2378:  */
                   2379: static int
1.235     schwarze 2380: roff_evalnum(struct roff *r, int ln, const char *v,
1.261     schwarze 2381:        int *pos, int *res, int flags)
1.204     schwarze 2382: {
                   2383:        int              mypos, operand2;
                   2384:        char             operator;
                   2385:
                   2386:        if (NULL == pos) {
                   2387:                mypos = 0;
                   2388:                pos = &mypos;
                   2389:        }
                   2390:
1.261     schwarze 2391:        if (flags & ROFFNUM_WHITE)
1.204     schwarze 2392:                while (isspace((unsigned char)v[*pos]))
                   2393:                        (*pos)++;
                   2394:
1.261     schwarze 2395:        if ( ! roff_evalpar(r, ln, v, pos, res, flags))
1.277     schwarze 2396:                return 0;
1.204     schwarze 2397:
                   2398:        while (1) {
1.261     schwarze 2399:                if (flags & ROFFNUM_WHITE)
1.204     schwarze 2400:                        while (isspace((unsigned char)v[*pos]))
                   2401:                                (*pos)++;
                   2402:
                   2403:                if ( ! roff_getop(v, pos, &operator))
                   2404:                        break;
                   2405:
1.261     schwarze 2406:                if (flags & ROFFNUM_WHITE)
1.204     schwarze 2407:                        while (isspace((unsigned char)v[*pos]))
                   2408:                                (*pos)++;
                   2409:
1.261     schwarze 2410:                if ( ! roff_evalpar(r, ln, v, pos, &operand2, flags))
1.277     schwarze 2411:                        return 0;
1.204     schwarze 2412:
1.261     schwarze 2413:                if (flags & ROFFNUM_WHITE)
1.204     schwarze 2414:                        while (isspace((unsigned char)v[*pos]))
                   2415:                                (*pos)++;
1.206     schwarze 2416:
                   2417:                if (NULL == res)
                   2418:                        continue;
1.204     schwarze 2419:
                   2420:                switch (operator) {
1.207     schwarze 2421:                case '+':
1.204     schwarze 2422:                        *res += operand2;
                   2423:                        break;
1.207     schwarze 2424:                case '-':
1.204     schwarze 2425:                        *res -= operand2;
                   2426:                        break;
1.207     schwarze 2427:                case '*':
1.204     schwarze 2428:                        *res *= operand2;
                   2429:                        break;
1.207     schwarze 2430:                case '/':
1.244     schwarze 2431:                        if (operand2 == 0) {
1.235     schwarze 2432:                                mandoc_msg(MANDOCERR_DIVZERO,
1.234     kristaps 2433:                                        r->parse, ln, *pos, v);
                   2434:                                *res = 0;
                   2435:                                break;
                   2436:                        }
1.204     schwarze 2437:                        *res /= operand2;
                   2438:                        break;
1.207     schwarze 2439:                case '%':
1.244     schwarze 2440:                        if (operand2 == 0) {
                   2441:                                mandoc_msg(MANDOCERR_DIVZERO,
                   2442:                                        r->parse, ln, *pos, v);
                   2443:                                *res = 0;
                   2444:                                break;
                   2445:                        }
1.204     schwarze 2446:                        *res %= operand2;
                   2447:                        break;
1.207     schwarze 2448:                case '<':
1.204     schwarze 2449:                        *res = *res < operand2;
                   2450:                        break;
1.207     schwarze 2451:                case '>':
1.204     schwarze 2452:                        *res = *res > operand2;
                   2453:                        break;
1.207     schwarze 2454:                case 'l':
1.204     schwarze 2455:                        *res = *res <= operand2;
                   2456:                        break;
1.207     schwarze 2457:                case 'g':
1.204     schwarze 2458:                        *res = *res >= operand2;
                   2459:                        break;
1.207     schwarze 2460:                case '=':
1.204     schwarze 2461:                        *res = *res == operand2;
                   2462:                        break;
1.207     schwarze 2463:                case '!':
1.204     schwarze 2464:                        *res = *res != operand2;
                   2465:                        break;
1.207     schwarze 2466:                case '&':
1.204     schwarze 2467:                        *res = *res && operand2;
                   2468:                        break;
1.207     schwarze 2469:                case ':':
1.204     schwarze 2470:                        *res = *res || operand2;
                   2471:                        break;
1.207     schwarze 2472:                case 'i':
1.204     schwarze 2473:                        if (operand2 < *res)
                   2474:                                *res = operand2;
                   2475:                        break;
1.207     schwarze 2476:                case 'a':
1.204     schwarze 2477:                        if (operand2 > *res)
                   2478:                                *res = operand2;
                   2479:                        break;
                   2480:                default:
                   2481:                        abort();
                   2482:                }
                   2483:        }
1.277     schwarze 2484:        return 1;
1.204     schwarze 2485: }
                   2486:
1.266     schwarze 2487: /* --- register management ------------------------------------------------ */
                   2488:
1.180     schwarze 2489: void
1.187     schwarze 2490: roff_setreg(struct roff *r, const char *name, int val, char sign)
1.147     kristaps 2491: {
1.180     schwarze 2492:        struct roffreg  *reg;
                   2493:
                   2494:        /* Search for an existing register with the same name. */
                   2495:        reg = r->regtab;
                   2496:
                   2497:        while (reg && strcmp(name, reg->key.p))
                   2498:                reg = reg->next;
1.147     kristaps 2499:
1.180     schwarze 2500:        if (NULL == reg) {
                   2501:                /* Create a new register. */
                   2502:                reg = mandoc_malloc(sizeof(struct roffreg));
                   2503:                reg->key.p = mandoc_strdup(name);
                   2504:                reg->key.sz = strlen(name);
1.187     schwarze 2505:                reg->val = 0;
1.180     schwarze 2506:                reg->next = r->regtab;
                   2507:                r->regtab = reg;
                   2508:        }
                   2509:
1.187     schwarze 2510:        if ('+' == sign)
                   2511:                reg->val += val;
                   2512:        else if ('-' == sign)
                   2513:                reg->val -= val;
                   2514:        else
                   2515:                reg->val = val;
1.147     kristaps 2516: }
                   2517:
1.192     schwarze 2518: /*
                   2519:  * Handle some predefined read-only number registers.
                   2520:  * For now, return -1 if the requested register is not predefined;
                   2521:  * in case a predefined read-only register having the value -1
                   2522:  * were to turn up, another special value would have to be chosen.
                   2523:  */
                   2524: static int
1.273     schwarze 2525: roff_getregro(const struct roff *r, const char *name)
1.192     schwarze 2526: {
                   2527:
                   2528:        switch (*name) {
1.273     schwarze 2529:        case '$':  /* Number of arguments of the last macro evaluated. */
1.277     schwarze 2530:                return r->argc;
1.207     schwarze 2531:        case 'A':  /* ASCII approximation mode is always off. */
1.277     schwarze 2532:                return 0;
1.207     schwarze 2533:        case 'g':  /* Groff compatibility mode is always on. */
1.277     schwarze 2534:                return 1;
1.207     schwarze 2535:        case 'H':  /* Fixed horizontal resolution. */
1.277     schwarze 2536:                return 24;
1.207     schwarze 2537:        case 'j':  /* Always adjust left margin only. */
1.277     schwarze 2538:                return 0;
1.207     schwarze 2539:        case 'T':  /* Some output device is always defined. */
1.277     schwarze 2540:                return 1;
1.207     schwarze 2541:        case 'V':  /* Fixed vertical resolution. */
1.277     schwarze 2542:                return 40;
1.192     schwarze 2543:        default:
1.277     schwarze 2544:                return -1;
1.192     schwarze 2545:        }
                   2546: }
                   2547:
1.181     schwarze 2548: int
1.180     schwarze 2549: roff_getreg(const struct roff *r, const char *name)
1.147     kristaps 2550: {
1.180     schwarze 2551:        struct roffreg  *reg;
1.192     schwarze 2552:        int              val;
                   2553:
                   2554:        if ('.' == name[0] && '\0' != name[1] && '\0' == name[2]) {
1.273     schwarze 2555:                val = roff_getregro(r, name + 1);
1.192     schwarze 2556:                if (-1 != val)
1.277     schwarze 2557:                        return val;
1.192     schwarze 2558:        }
1.180     schwarze 2559:
                   2560:        for (reg = r->regtab; reg; reg = reg->next)
                   2561:                if (0 == strcmp(name, reg->key.p))
1.277     schwarze 2562:                        return reg->val;
1.181     schwarze 2563:
1.277     schwarze 2564:        return 0;
1.181     schwarze 2565: }
                   2566:
                   2567: static int
                   2568: roff_getregn(const struct roff *r, const char *name, size_t len)
                   2569: {
                   2570:        struct roffreg  *reg;
1.192     schwarze 2571:        int              val;
                   2572:
                   2573:        if ('.' == name[0] && 2 == len) {
1.273     schwarze 2574:                val = roff_getregro(r, name + 1);
1.192     schwarze 2575:                if (-1 != val)
1.277     schwarze 2576:                        return val;
1.192     schwarze 2577:        }
1.181     schwarze 2578:
                   2579:        for (reg = r->regtab; reg; reg = reg->next)
                   2580:                if (len == reg->key.sz &&
                   2581:                    0 == strncmp(name, reg->key.p, len))
1.277     schwarze 2582:                        return reg->val;
1.271     schwarze 2583:
1.277     schwarze 2584:        return 0;
1.271     schwarze 2585: }
                   2586:
                   2587: static int
                   2588: roff_hasregn(const struct roff *r, const char *name, size_t len)
                   2589: {
                   2590:        struct roffreg  *reg;
                   2591:        int              val;
                   2592:
                   2593:        if ('.' == name[0] && 2 == len) {
1.273     schwarze 2594:                val = roff_getregro(r, name + 1);
1.271     schwarze 2595:                if (-1 != val)
1.277     schwarze 2596:                        return 1;
1.271     schwarze 2597:        }
                   2598:
                   2599:        for (reg = r->regtab; reg; reg = reg->next)
                   2600:                if (len == reg->key.sz &&
                   2601:                    0 == strncmp(name, reg->key.p, len))
1.277     schwarze 2602:                        return 1;
1.147     kristaps 2603:
1.277     schwarze 2604:        return 0;
1.147     kristaps 2605: }
                   2606:
1.180     schwarze 2607: static void
                   2608: roff_freereg(struct roffreg *reg)
1.147     kristaps 2609: {
1.180     schwarze 2610:        struct roffreg  *old_reg;
1.147     kristaps 2611:
1.180     schwarze 2612:        while (NULL != reg) {
                   2613:                free(reg->key.p);
                   2614:                old_reg = reg;
                   2615:                reg = reg->next;
                   2616:                free(old_reg);
                   2617:        }
1.147     kristaps 2618: }
1.92      schwarze 2619:
                   2620: static enum rofferr
1.89      kristaps 2621: roff_nr(ROFF_ARGS)
1.83      schwarze 2622: {
1.212     schwarze 2623:        char            *key, *val;
                   2624:        size_t           keysz;
1.138     kristaps 2625:        int              iv;
1.187     schwarze 2626:        char             sign;
1.89      kristaps 2627:
1.238     schwarze 2628:        key = val = buf->buf + pos;
                   2629:        if (*key == '\0')
1.277     schwarze 2630:                return ROFF_IGN;
1.212     schwarze 2631:
                   2632:        keysz = roff_getname(r, &val, ln, pos);
1.238     schwarze 2633:        if (key[keysz] == '\\')
1.277     schwarze 2634:                return ROFF_IGN;
1.212     schwarze 2635:        key[keysz] = '\0';
1.89      kristaps 2636:
1.187     schwarze 2637:        sign = *val;
1.238     schwarze 2638:        if (sign == '+' || sign == '-')
1.187     schwarze 2639:                val++;
                   2640:
1.261     schwarze 2641:        if (roff_evalnum(r, ln, val, NULL, &iv, ROFFNUM_SCALE))
1.204     schwarze 2642:                roff_setreg(r, key, iv, sign);
1.109     kristaps 2643:
1.277     schwarze 2644:        return ROFF_IGN;
1.203     schwarze 2645: }
                   2646:
                   2647: static enum rofferr
                   2648: roff_rr(ROFF_ARGS)
                   2649: {
                   2650:        struct roffreg  *reg, **prev;
1.212     schwarze 2651:        char            *name, *cp;
                   2652:        size_t           namesz;
1.203     schwarze 2653:
1.238     schwarze 2654:        name = cp = buf->buf + pos;
                   2655:        if (*name == '\0')
1.277     schwarze 2656:                return ROFF_IGN;
1.212     schwarze 2657:        namesz = roff_getname(r, &cp, ln, pos);
                   2658:        name[namesz] = '\0';
1.203     schwarze 2659:
                   2660:        prev = &r->regtab;
                   2661:        while (1) {
                   2662:                reg = *prev;
1.238     schwarze 2663:                if (reg == NULL || !strcmp(name, reg->key.p))
1.203     schwarze 2664:                        break;
                   2665:                prev = &reg->next;
                   2666:        }
1.238     schwarze 2667:        if (reg != NULL) {
1.203     schwarze 2668:                *prev = reg->next;
                   2669:                free(reg->key.p);
                   2670:                free(reg);
                   2671:        }
1.277     schwarze 2672:        return ROFF_IGN;
1.122     schwarze 2673: }
                   2674:
1.266     schwarze 2675: /* --- handler functions for roff requests -------------------------------- */
                   2676:
1.122     schwarze 2677: static enum rofferr
                   2678: roff_rm(ROFF_ARGS)
                   2679: {
                   2680:        const char       *name;
                   2681:        char             *cp;
1.212     schwarze 2682:        size_t            namesz;
1.122     schwarze 2683:
1.238     schwarze 2684:        cp = buf->buf + pos;
                   2685:        while (*cp != '\0') {
1.212     schwarze 2686:                name = cp;
1.238     schwarze 2687:                namesz = roff_getname(r, &cp, ln, (int)(cp - buf->buf));
1.212     schwarze 2688:                roff_setstrn(&r->strtab, name, namesz, NULL, 0, 0);
1.311     schwarze 2689:                roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
1.238     schwarze 2690:                if (name[namesz] == '\\')
1.212     schwarze 2691:                        break;
1.122     schwarze 2692:        }
1.277     schwarze 2693:        return ROFF_IGN;
1.178     schwarze 2694: }
                   2695:
                   2696: static enum rofferr
                   2697: roff_it(ROFF_ARGS)
                   2698: {
                   2699:        int              iv;
                   2700:
                   2701:        /* Parse the number of lines. */
1.261     schwarze 2702:
                   2703:        if ( ! roff_evalnum(r, ln, buf->buf, &pos, &iv, 0)) {
1.222     schwarze 2704:                mandoc_msg(MANDOCERR_IT_NONUM, r->parse,
1.238     schwarze 2705:                    ln, ppos, buf->buf + 1);
1.277     schwarze 2706:                return ROFF_IGN;
1.178     schwarze 2707:        }
                   2708:
1.262     schwarze 2709:        while (isspace((unsigned char)buf->buf[pos]))
                   2710:                pos++;
                   2711:
                   2712:        /*
                   2713:         * Arm the input line trap.
                   2714:         * Special-casing "an-trap" is an ugly workaround to cope
                   2715:         * with DocBook stupidly fiddling with man(7) internals.
                   2716:         */
1.261     schwarze 2717:
1.178     schwarze 2718:        roffit_lines = iv;
1.262     schwarze 2719:        roffit_macro = mandoc_strdup(iv != 1 ||
                   2720:            strcmp(buf->buf + pos, "an-trap") ?
                   2721:            buf->buf + pos : "br");
1.277     schwarze 2722:        return ROFF_IGN;
1.175     schwarze 2723: }
                   2724:
                   2725: static enum rofferr
                   2726: roff_Dd(ROFF_ARGS)
                   2727: {
                   2728:        const char *const       *cp;
                   2729:
1.227     schwarze 2730:        if ((r->options & (MPARSE_MDOC | MPARSE_QUICK)) == 0)
1.175     schwarze 2731:                for (cp = __mdoc_reserved; *cp; cp++)
                   2732:                        roff_setstr(r, *cp, NULL, 0);
                   2733:
1.227     schwarze 2734:        if (r->format == 0)
                   2735:                r->format = MPARSE_MDOC;
                   2736:
1.277     schwarze 2737:        return ROFF_CONT;
1.175     schwarze 2738: }
                   2739:
                   2740: static enum rofferr
                   2741: roff_TH(ROFF_ARGS)
                   2742: {
                   2743:        const char *const       *cp;
                   2744:
1.227     schwarze 2745:        if ((r->options & MPARSE_QUICK) == 0)
1.175     schwarze 2746:                for (cp = __man_reserved; *cp; cp++)
                   2747:                        roff_setstr(r, *cp, NULL, 0);
                   2748:
1.227     schwarze 2749:        if (r->format == 0)
                   2750:                r->format = MPARSE_MAN;
                   2751:
1.277     schwarze 2752:        return ROFF_CONT;
1.109     kristaps 2753: }
                   2754:
                   2755: static enum rofferr
                   2756: roff_TE(ROFF_ARGS)
                   2757: {
                   2758:
                   2759:        if (NULL == r->tbl)
1.221     schwarze 2760:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   2761:                    ln, ppos, "TE");
1.258     schwarze 2762:        else if ( ! tbl_end(&r->tbl)) {
                   2763:                free(buf->buf);
                   2764:                buf->buf = mandoc_strdup(".sp");
                   2765:                buf->sz = 4;
1.277     schwarze 2766:                return ROFF_REPARSE;
1.258     schwarze 2767:        }
1.277     schwarze 2768:        return ROFF_IGN;
1.112     kristaps 2769: }
                   2770:
                   2771: static enum rofferr
                   2772: roff_T_(ROFF_ARGS)
                   2773: {
                   2774:
                   2775:        if (NULL == r->tbl)
1.221     schwarze 2776:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse,
                   2777:                    ln, ppos, "T&");
1.112     kristaps 2778:        else
1.296     schwarze 2779:                tbl_restart(ln, ppos, r->tbl);
1.112     kristaps 2780:
1.277     schwarze 2781:        return ROFF_IGN;
1.109     kristaps 2782: }
                   2783:
1.230     schwarze 2784: /*
                   2785:  * Handle in-line equation delimiters.
                   2786:  */
                   2787: static enum rofferr
1.238     schwarze 2788: roff_eqndelim(struct roff *r, struct buf *buf, int pos)
1.151     kristaps 2789: {
1.233     schwarze 2790:        char            *cp1, *cp2;
                   2791:        const char      *bef_pr, *bef_nl, *mac, *aft_nl, *aft_pr;
1.151     kristaps 2792:
1.230     schwarze 2793:        /*
                   2794:         * Outside equations, look for an opening delimiter.
                   2795:         * If we are inside an equation, we already know it is
                   2796:         * in-line, or this function wouldn't have been called;
                   2797:         * so look for a closing delimiter.
                   2798:         */
                   2799:
1.238     schwarze 2800:        cp1 = buf->buf + pos;
1.230     schwarze 2801:        cp2 = strchr(cp1, r->eqn == NULL ?
                   2802:            r->last_eqn->odelim : r->last_eqn->cdelim);
                   2803:        if (cp2 == NULL)
1.277     schwarze 2804:                return ROFF_CONT;
1.230     schwarze 2805:
1.233     schwarze 2806:        *cp2++ = '\0';
                   2807:        bef_pr = bef_nl = aft_nl = aft_pr = "";
                   2808:
                   2809:        /* Handle preceding text, protecting whitespace. */
                   2810:
1.238     schwarze 2811:        if (*buf->buf != '\0') {
1.233     schwarze 2812:                if (r->eqn == NULL)
                   2813:                        bef_pr = "\\&";
                   2814:                bef_nl = "\n";
                   2815:        }
                   2816:
                   2817:        /*
                   2818:         * Prepare replacing the delimiter with an equation macro
                   2819:         * and drop leading white space from the equation.
                   2820:         */
1.230     schwarze 2821:
1.233     schwarze 2822:        if (r->eqn == NULL) {
                   2823:                while (*cp2 == ' ')
                   2824:                        cp2++;
                   2825:                mac = ".EQ";
                   2826:        } else
                   2827:                mac = ".EN";
                   2828:
                   2829:        /* Handle following text, protecting whitespace. */
                   2830:
                   2831:        if (*cp2 != '\0') {
                   2832:                aft_nl = "\n";
                   2833:                if (r->eqn != NULL)
                   2834:                        aft_pr = "\\&";
                   2835:        }
                   2836:
                   2837:        /* Do the actual replacement. */
                   2838:
1.238     schwarze 2839:        buf->sz = mandoc_asprintf(&cp1, "%s%s%s%s%s%s%s", buf->buf,
1.233     schwarze 2840:            bef_pr, bef_nl, mac, aft_nl, aft_pr, cp2) + 1;
1.238     schwarze 2841:        free(buf->buf);
                   2842:        buf->buf = cp1;
1.230     schwarze 2843:
                   2844:        /* Toggle the in-line state of the eqn subsystem. */
                   2845:
                   2846:        r->eqn_inline = r->eqn == NULL;
1.277     schwarze 2847:        return ROFF_REPARSE;
1.151     kristaps 2848: }
                   2849:
1.235     schwarze 2850: static enum rofferr
                   2851: roff_EQ(ROFF_ARGS)
1.125     kristaps 2852: {
1.151     kristaps 2853:        struct eqn_node *e;
1.125     kristaps 2854:
1.238     schwarze 2855:        assert(r->eqn == NULL);
1.235     schwarze 2856:        e = eqn_alloc(ppos, ln, r->parse);
1.125     kristaps 2857:
1.230     schwarze 2858:        if (r->last_eqn) {
1.125     kristaps 2859:                r->last_eqn->next = e;
1.230     schwarze 2860:                e->delim = r->last_eqn->delim;
                   2861:                e->odelim = r->last_eqn->odelim;
                   2862:                e->cdelim = r->last_eqn->cdelim;
                   2863:        } else
1.125     kristaps 2864:                r->first_eqn = r->last_eqn = e;
                   2865:
                   2866:        r->eqn = r->last_eqn = e;
1.151     kristaps 2867:
1.238     schwarze 2868:        if (buf->buf[pos] != '\0')
1.235     schwarze 2869:                mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
1.238     schwarze 2870:                    ".EQ %s", buf->buf + pos);
1.151     kristaps 2871:
1.277     schwarze 2872:        return ROFF_IGN;
1.125     kristaps 2873: }
                   2874:
                   2875: static enum rofferr
                   2876: roff_EN(ROFF_ARGS)
                   2877: {
                   2878:
1.221     schwarze 2879:        mandoc_msg(MANDOCERR_BLK_NOTOPEN, r->parse, ln, ppos, "EN");
1.277     schwarze 2880:        return ROFF_IGN;
1.125     kristaps 2881: }
                   2882:
                   2883: static enum rofferr
1.109     kristaps 2884: roff_TS(ROFF_ARGS)
                   2885: {
1.176     schwarze 2886:        struct tbl_node *tbl;
1.89      kristaps 2887:
1.115     kristaps 2888:        if (r->tbl) {
1.221     schwarze 2889:                mandoc_msg(MANDOCERR_BLK_BROKEN, r->parse,
                   2890:                    ln, ppos, "TS breaks TS");
1.151     kristaps 2891:                tbl_end(&r->tbl);
1.115     kristaps 2892:        }
1.83      schwarze 2893:
1.176     schwarze 2894:        tbl = tbl_alloc(ppos, ln, r->parse);
1.113     kristaps 2895:
                   2896:        if (r->last_tbl)
1.176     schwarze 2897:                r->last_tbl->next = tbl;
1.113     kristaps 2898:        else
1.176     schwarze 2899:                r->first_tbl = r->last_tbl = tbl;
1.113     kristaps 2900:
1.176     schwarze 2901:        r->tbl = r->last_tbl = tbl;
1.297     schwarze 2902:        return ROFF_IGN;
                   2903: }
                   2904:
                   2905: static enum rofferr
                   2906: roff_onearg(ROFF_ARGS)
                   2907: {
                   2908:        struct roff_node        *n;
                   2909:        char                    *cp;
1.305     schwarze 2910:        int                      npos;
1.297     schwarze 2911:
1.302     schwarze 2912:        if (r->man->flags & (MAN_BLINE | MAN_ELINE) &&
                   2913:            (tok == ROFF_sp || tok == ROFF_ti))
                   2914:                man_breakscope(r->man, tok);
                   2915:
1.309     schwarze 2916:        if (roffce_node != NULL && (tok == ROFF_ce || tok == ROFF_rj)) {
1.305     schwarze 2917:                r->man->last = roffce_node;
                   2918:                r->man->next = ROFF_NEXT_SIBLING;
                   2919:        }
                   2920:
1.297     schwarze 2921:        roff_elem_alloc(r->man, ln, ppos, tok);
                   2922:        n = r->man->last;
                   2923:
                   2924:        cp = buf->buf + pos;
                   2925:        if (*cp != '\0') {
                   2926:                while (*cp != '\0' && *cp != ' ')
                   2927:                        cp++;
                   2928:                while (*cp == ' ')
                   2929:                        *cp++ = '\0';
                   2930:                if (*cp != '\0')
                   2931:                        mandoc_vmsg(MANDOCERR_ARG_EXCESS,
                   2932:                            r->parse, ln, cp - buf->buf,
                   2933:                            "%s ... %s", roff_name[tok], cp);
                   2934:                roff_word_alloc(r->man, ln, pos, buf->buf + pos);
1.300     schwarze 2935:        }
                   2936:
1.309     schwarze 2937:        if (tok == ROFF_ce || tok == ROFF_rj) {
                   2938:                if (r->man->last->type == ROFFT_ELEM) {
1.305     schwarze 2939:                        roff_word_alloc(r->man, ln, pos, "1");
                   2940:                        r->man->last->flags |= NODE_NOSRC;
                   2941:                }
                   2942:                npos = 0;
                   2943:                if (roff_evalnum(r, ln, r->man->last->string, &npos,
                   2944:                    &roffce_lines, 0) == 0) {
                   2945:                        mandoc_vmsg(MANDOCERR_CE_NONUM,
                   2946:                            r->parse, ln, pos, "ce %s", buf->buf + pos);
                   2947:                        roffce_lines = 1;
                   2948:                }
                   2949:                if (roffce_lines < 1) {
                   2950:                        r->man->last = r->man->last->parent;
                   2951:                        roffce_node = NULL;
                   2952:                        roffce_lines = 0;
                   2953:                } else
                   2954:                        roffce_node = r->man->last->parent;
                   2955:        } else {
                   2956:                n->flags |= NODE_VALID | NODE_ENDED;
                   2957:                r->man->last = n;
                   2958:        }
                   2959:        n->flags |= NODE_LINE;
1.300     schwarze 2960:        r->man->next = ROFF_NEXT_SIBLING;
                   2961:        return ROFF_IGN;
                   2962: }
                   2963:
                   2964: static enum rofferr
                   2965: roff_manyarg(ROFF_ARGS)
                   2966: {
                   2967:        struct roff_node        *n;
                   2968:        char                    *sp, *ep;
                   2969:
                   2970:        roff_elem_alloc(r->man, ln, ppos, tok);
                   2971:        n = r->man->last;
                   2972:
                   2973:        for (sp = ep = buf->buf + pos; *sp != '\0'; sp = ep) {
                   2974:                while (*ep != '\0' && *ep != ' ')
                   2975:                        ep++;
                   2976:                while (*ep == ' ')
                   2977:                        *ep++ = '\0';
                   2978:                roff_word_alloc(r->man, ln, sp - buf->buf, sp);
1.297     schwarze 2979:        }
                   2980:
                   2981:        n->flags |= NODE_LINE | NODE_VALID | NODE_ENDED;
                   2982:        r->man->last = n;
                   2983:        r->man->next = ROFF_NEXT_SIBLING;
1.277     schwarze 2984:        return ROFF_IGN;
1.251     schwarze 2985: }
                   2986:
                   2987: static enum rofferr
1.311     schwarze 2988: roff_als(ROFF_ARGS)
                   2989: {
                   2990:        char            *oldn, *newn, *end, *value;
                   2991:        size_t           oldsz, newsz, valsz;
                   2992:
                   2993:        newn = oldn = buf->buf + pos;
                   2994:        if (*newn == '\0')
                   2995:                return ROFF_IGN;
                   2996:
                   2997:        newsz = roff_getname(r, &oldn, ln, pos);
                   2998:        if (newn[newsz] == '\\' || *oldn == '\0')
                   2999:                return ROFF_IGN;
                   3000:
                   3001:        end = oldn;
                   3002:        oldsz = roff_getname(r, &end, ln, oldn - buf->buf);
                   3003:        if (oldsz == 0)
                   3004:                return ROFF_IGN;
                   3005:
                   3006:        valsz = mandoc_asprintf(&value, ".%.*s \\$*\n", (int)oldsz, oldn);
                   3007:        roff_setstrn(&r->strtab, newn, newsz, value, valsz, 0);
                   3008:        roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
                   3009:        free(value);
                   3010:        return ROFF_IGN;
                   3011: }
                   3012:
                   3013: static enum rofferr
1.296     schwarze 3014: roff_br(ROFF_ARGS)
1.251     schwarze 3015: {
1.302     schwarze 3016:        if (r->man->flags & (MAN_BLINE | MAN_ELINE))
                   3017:                man_breakscope(r->man, ROFF_br);
1.296     schwarze 3018:        roff_elem_alloc(r->man, ln, ppos, ROFF_br);
                   3019:        if (buf->buf[pos] != '\0')
                   3020:                mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
                   3021:                    "%s %s", roff_name[tok], buf->buf + pos);
                   3022:        r->man->last->flags |= NODE_LINE | NODE_VALID | NODE_ENDED;
                   3023:        r->man->next = ROFF_NEXT_SIBLING;
                   3024:        return ROFF_IGN;
1.92      schwarze 3025: }
                   3026:
1.105     kristaps 3027: static enum rofferr
1.174     kristaps 3028: roff_cc(ROFF_ARGS)
                   3029: {
                   3030:        const char      *p;
                   3031:
1.238     schwarze 3032:        p = buf->buf + pos;
1.174     kristaps 3033:
1.238     schwarze 3034:        if (*p == '\0' || (r->control = *p++) == '.')
1.303     schwarze 3035:                r->control = '\0';
1.174     kristaps 3036:
1.238     schwarze 3037:        if (*p != '\0')
1.260     schwarze 3038:                mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
                   3039:                    ln, p - buf->buf, "cc ... %s", p);
1.174     kristaps 3040:
1.277     schwarze 3041:        return ROFF_IGN;
1.174     kristaps 3042: }
                   3043:
                   3044: static enum rofferr
1.303     schwarze 3045: roff_ec(ROFF_ARGS)
                   3046: {
                   3047:        const char      *p;
                   3048:
                   3049:        p = buf->buf + pos;
                   3050:        if (*p == '\0')
                   3051:                r->escape = '\\';
                   3052:        else {
                   3053:                r->escape = *p;
                   3054:                if (*++p != '\0')
                   3055:                        mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
                   3056:                            ln, p - buf->buf, "ec ... %s", p);
                   3057:        }
                   3058:        return ROFF_IGN;
                   3059: }
                   3060:
                   3061: static enum rofferr
                   3062: roff_eo(ROFF_ARGS)
                   3063: {
                   3064:        r->escape = '\0';
                   3065:        if (buf->buf[pos] != '\0')
                   3066:                mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse,
                   3067:                    ln, pos, "eo %s", buf->buf + pos);
                   3068:        return ROFF_IGN;
                   3069: }
                   3070:
                   3071: static enum rofferr
1.164     kristaps 3072: roff_tr(ROFF_ARGS)
                   3073: {
                   3074:        const char      *p, *first, *second;
                   3075:        size_t           fsz, ssz;
                   3076:        enum mandoc_esc  esc;
                   3077:
1.238     schwarze 3078:        p = buf->buf + pos;
1.164     kristaps 3079:
1.238     schwarze 3080:        if (*p == '\0') {
1.260     schwarze 3081:                mandoc_msg(MANDOCERR_REQ_EMPTY, r->parse, ln, ppos, "tr");
1.277     schwarze 3082:                return ROFF_IGN;
1.164     kristaps 3083:        }
                   3084:
1.238     schwarze 3085:        while (*p != '\0') {
1.164     kristaps 3086:                fsz = ssz = 1;
                   3087:
                   3088:                first = p++;
1.238     schwarze 3089:                if (*first == '\\') {
1.164     kristaps 3090:                        esc = mandoc_escape(&p, NULL, NULL);
1.238     schwarze 3091:                        if (esc == ESCAPE_ERROR) {
1.219     schwarze 3092:                                mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1.238     schwarze 3093:                                    ln, (int)(p - buf->buf), first);
1.277     schwarze 3094:                                return ROFF_IGN;
1.164     kristaps 3095:                        }
                   3096:                        fsz = (size_t)(p - first);
                   3097:                }
                   3098:
                   3099:                second = p++;
1.238     schwarze 3100:                if (*second == '\\') {
1.164     kristaps 3101:                        esc = mandoc_escape(&p, NULL, NULL);
1.238     schwarze 3102:                        if (esc == ESCAPE_ERROR) {
1.219     schwarze 3103:                                mandoc_msg(MANDOCERR_ESC_BAD, r->parse,
1.238     schwarze 3104:                                    ln, (int)(p - buf->buf), second);
1.277     schwarze 3105:                                return ROFF_IGN;
1.164     kristaps 3106:                        }
                   3107:                        ssz = (size_t)(p - second);
1.238     schwarze 3108:                } else if (*second == '\0') {
1.260     schwarze 3109:                        mandoc_vmsg(MANDOCERR_TR_ODD, r->parse,
                   3110:                            ln, first - buf->buf, "tr %s", first);
1.164     kristaps 3111:                        second = " ";
1.165     kristaps 3112:                        p--;
1.164     kristaps 3113:                }
                   3114:
1.167     kristaps 3115:                if (fsz > 1) {
1.207     schwarze 3116:                        roff_setstrn(&r->xmbtab, first, fsz,
                   3117:                            second, ssz, 0);
1.167     kristaps 3118:                        continue;
                   3119:                }
                   3120:
1.238     schwarze 3121:                if (r->xtab == NULL)
1.207     schwarze 3122:                        r->xtab = mandoc_calloc(128,
                   3123:                            sizeof(struct roffstr));
1.167     kristaps 3124:
                   3125:                free(r->xtab[(int)*first].p);
                   3126:                r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
                   3127:                r->xtab[(int)*first].sz = ssz;
1.164     kristaps 3128:        }
                   3129:
1.277     schwarze 3130:        return ROFF_IGN;
1.164     kristaps 3131: }
                   3132:
                   3133: static enum rofferr
1.306     schwarze 3134: roff_rn(ROFF_ARGS)
                   3135: {
                   3136:        const char      *value;
                   3137:        char            *oldn, *newn, *end;
                   3138:        size_t           oldsz, newsz;
                   3139:
                   3140:        oldn = newn = buf->buf + pos;
                   3141:        if (*oldn == '\0')
                   3142:                return ROFF_IGN;
                   3143:
                   3144:        oldsz = roff_getname(r, &newn, ln, pos);
                   3145:        if (oldn[oldsz] == '\\' || *newn == '\0')
                   3146:                return ROFF_IGN;
                   3147:
                   3148:        end = newn;
                   3149:        newsz = roff_getname(r, &end, ln, newn - buf->buf);
                   3150:        if (newsz == 0)
                   3151:                return ROFF_IGN;
                   3152:
                   3153:        /*
                   3154:         * Rename a user-defined macro bearing the old name,
                   3155:         * overriding an existing renamed high-level macro
                   3156:         * bearing the new name, if that exists.
                   3157:         */
                   3158:
                   3159:        if ((value = roff_getstrn(r, oldn, oldsz)) != NULL) {
                   3160:                roff_setstrn(&r->strtab, newn, newsz, value, strlen(value), 0);
                   3161:                roff_setstrn(&r->strtab, oldn, oldsz, NULL, 0, 0);
                   3162:                roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
                   3163:                return ROFF_IGN;
                   3164:        }
                   3165:
                   3166:        /*
                   3167:         * Rename a high-level macro bearing the old name,
                   3168:         * either renaming it a second time if it was already
                   3169:         * renamed before, or renaming it for the first time.
                   3170:         * In both cases, override an existing user-defined
                   3171:         * macro bearing the new name, if that exists.
                   3172:         */
                   3173:
                   3174:        if ((value = roff_getrenn(r, oldn, oldsz)) != NULL) {
                   3175:                roff_setstrn(&r->rentab, newn, newsz, value, strlen(value), 0);
                   3176:                roff_setstrn(&r->rentab, oldn, oldsz, NULL, 0, 0);
                   3177:        } else
                   3178:                roff_setstrn(&r->rentab, newn, newsz, oldn, oldsz, 0);
                   3179:        roff_setstrn(&r->strtab, newn, newsz, NULL, 0, 0);
                   3180:        return ROFF_IGN;
                   3181: }
                   3182:
                   3183: static enum rofferr
1.105     kristaps 3184: roff_so(ROFF_ARGS)
                   3185: {
1.249     schwarze 3186:        char *name, *cp;
1.105     kristaps 3187:
1.238     schwarze 3188:        name = buf->buf + pos;
1.224     schwarze 3189:        mandoc_vmsg(MANDOCERR_SO, r->parse, ln, ppos, "so %s", name);
1.105     kristaps 3190:
                   3191:        /*
                   3192:         * Handle `so'.  Be EXTREMELY careful, as we shouldn't be
                   3193:         * opening anything that's not in our cwd or anything beneath
                   3194:         * it.  Thus, explicitly disallow traversing up the file-system
                   3195:         * or using absolute paths.
                   3196:         */
                   3197:
1.238     schwarze 3198:        if (*name == '/' || strstr(name, "../") || strstr(name, "/..")) {
1.210     schwarze 3199:                mandoc_vmsg(MANDOCERR_SO_PATH, r->parse, ln, ppos,
                   3200:                    ".so %s", name);
1.249     schwarze 3201:                buf->sz = mandoc_asprintf(&cp,
                   3202:                    ".sp\nSee the file %s.\n.sp", name) + 1;
                   3203:                free(buf->buf);
                   3204:                buf->buf = cp;
                   3205:                *offs = 0;
1.277     schwarze 3206:                return ROFF_REPARSE;
1.105     kristaps 3207:        }
                   3208:
                   3209:        *offs = pos;
1.277     schwarze 3210:        return ROFF_SO;
1.105     kristaps 3211: }
1.92      schwarze 3212:
1.266     schwarze 3213: /* --- user defined strings and macros ------------------------------------ */
                   3214:
1.106     kristaps 3215: static enum rofferr
                   3216: roff_userdef(ROFF_ARGS)
1.99      kristaps 3217: {
1.311     schwarze 3218:        const char       *arg[16], *ap;
1.106     kristaps 3219:        char             *cp, *n1, *n2;
1.292     schwarze 3220:        int               expand_count, i, ib, ie;
1.263     schwarze 3221:        size_t            asz, rsz;
1.106     kristaps 3222:
                   3223:        /*
                   3224:         * Collect pointers to macro argument strings
1.188     schwarze 3225:         * and NUL-terminate them.
1.106     kristaps 3226:         */
1.263     schwarze 3227:
1.273     schwarze 3228:        r->argc = 0;
1.238     schwarze 3229:        cp = buf->buf + pos;
1.311     schwarze 3230:        for (i = 0; i < 16; i++) {
1.273     schwarze 3231:                if (*cp == '\0')
                   3232:                        arg[i] = "";
                   3233:                else {
                   3234:                        arg[i] = mandoc_getarg(r->parse, &cp, ln, &pos);
                   3235:                        r->argc = i + 1;
                   3236:                }
                   3237:        }
1.99      kristaps 3238:
1.106     kristaps 3239:        /*
                   3240:         * Expand macro arguments.
1.99      kristaps 3241:         */
1.263     schwarze 3242:
                   3243:        buf->sz = strlen(r->current_string) + 1;
1.292     schwarze 3244:        n1 = n2 = cp = mandoc_malloc(buf->sz);
1.263     schwarze 3245:        memcpy(n1, r->current_string, buf->sz);
1.292     schwarze 3246:        expand_count = 0;
1.263     schwarze 3247:        while (*cp != '\0') {
                   3248:
                   3249:                /* Scan ahead for the next argument invocation. */
                   3250:
                   3251:                if (*cp++ != '\\')
                   3252:                        continue;
                   3253:                if (*cp++ != '$')
                   3254:                        continue;
1.274     schwarze 3255:                if (*cp == '*') {  /* \\$* inserts all arguments */
                   3256:                        ib = 0;
                   3257:                        ie = r->argc - 1;
                   3258:                } else {  /* \\$1 .. \\$9 insert one argument */
                   3259:                        ib = ie = *cp - '1';
                   3260:                        if (ib < 0 || ib > 8)
                   3261:                                continue;
                   3262:                }
1.263     schwarze 3263:                cp -= 2;
1.292     schwarze 3264:
                   3265:                /*
                   3266:                 * Prevent infinite recursion.
                   3267:                 */
                   3268:
                   3269:                if (cp >= n2)
                   3270:                        expand_count = 1;
                   3271:                else if (++expand_count > EXPAND_LIMIT) {
                   3272:                        mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
                   3273:                            ln, (int)(cp - n1), NULL);
1.293     schwarze 3274:                        free(buf->buf);
                   3275:                        buf->buf = n1;
1.292     schwarze 3276:                        return ROFF_IGN;
                   3277:                }
1.263     schwarze 3278:
                   3279:                /*
                   3280:                 * Determine the size of the expanded argument,
                   3281:                 * taking escaping of quotes into account.
                   3282:                 */
                   3283:
1.274     schwarze 3284:                asz = ie > ib ? ie - ib : 0;  /* for blanks */
                   3285:                for (i = ib; i <= ie; i++) {
                   3286:                        for (ap = arg[i]; *ap != '\0'; ap++) {
                   3287:                                asz++;
                   3288:                                if (*ap == '"')
                   3289:                                        asz += 3;
                   3290:                        }
1.263     schwarze 3291:                }
                   3292:                if (asz != 3) {
                   3293:
                   3294:                        /*
                   3295:                         * Determine the size of the rest of the
                   3296:                         * unexpanded macro, including the NUL.
                   3297:                         */
                   3298:
                   3299:                        rsz = buf->sz - (cp - n1) - 3;
                   3300:
                   3301:                        /*
                   3302:                         * When shrinking, move before
                   3303:                         * releasing the storage.
                   3304:                         */
                   3305:
                   3306:                        if (asz < 3)
                   3307:                                memmove(cp + asz, cp + 3, rsz);
                   3308:
                   3309:                        /*
                   3310:                         * Resize the storage for the macro
                   3311:                         * and readjust the parse pointer.
                   3312:                         */
                   3313:
                   3314:                        buf->sz += asz - 3;
                   3315:                        n2 = mandoc_realloc(n1, buf->sz);
                   3316:                        cp = n2 + (cp - n1);
                   3317:                        n1 = n2;
                   3318:
                   3319:                        /*
                   3320:                         * When growing, make room
                   3321:                         * for the expanded argument.
                   3322:                         */
                   3323:
                   3324:                        if (asz > 3)
                   3325:                                memmove(cp + asz, cp + 3, rsz);
                   3326:                }
                   3327:
                   3328:                /* Copy the expanded argument, escaping quotes. */
                   3329:
                   3330:                n2 = cp;
1.274     schwarze 3331:                for (i = ib; i <= ie; i++) {
                   3332:                        for (ap = arg[i]; *ap != '\0'; ap++) {
                   3333:                                if (*ap == '"') {
                   3334:                                        memcpy(n2, "\\(dq", 4);
                   3335:                                        n2 += 4;
                   3336:                                } else
                   3337:                                        *n2++ = *ap;
                   3338:                        }
                   3339:                        if (i < ie)
                   3340:                                *n2++ = ' ';
1.106     kristaps 3341:                }
1.99      kristaps 3342:        }
                   3343:
1.106     kristaps 3344:        /*
                   3345:         * Replace the macro invocation
                   3346:         * by the expanded macro.
                   3347:         */
1.263     schwarze 3348:
1.238     schwarze 3349:        free(buf->buf);
                   3350:        buf->buf = n1;
1.248     schwarze 3351:        *offs = 0;
1.106     kristaps 3352:
1.277     schwarze 3353:        return buf->sz > 1 && buf->buf[buf->sz - 2] == '\n' ?
                   3354:           ROFF_REPARSE : ROFF_APPEND;
1.99      kristaps 3355: }
1.121     schwarze 3356:
1.306     schwarze 3357: /*
                   3358:  * Calling a high-level macro that was renamed with .rn.
                   3359:  * r->current_string has already been set up by roff_parse().
                   3360:  */
                   3361: static enum rofferr
                   3362: roff_renamed(ROFF_ARGS)
                   3363: {
                   3364:        char    *nbuf;
                   3365:
                   3366:        buf->sz = mandoc_asprintf(&nbuf, ".%s %s", r->current_string,
                   3367:            buf->buf + pos) + 1;
                   3368:        free(buf->buf);
                   3369:        buf->buf = nbuf;
                   3370:        return ROFF_CONT;
                   3371: }
                   3372:
1.212     schwarze 3373: static size_t
1.121     schwarze 3374: roff_getname(struct roff *r, char **cpp, int ln, int pos)
                   3375: {
                   3376:        char     *name, *cp;
1.212     schwarze 3377:        size_t    namesz;
1.121     schwarze 3378:
                   3379:        name = *cpp;
                   3380:        if ('\0' == *name)
1.277     schwarze 3381:                return 0;
1.121     schwarze 3382:
1.212     schwarze 3383:        /* Read until end of name and terminate it with NUL. */
                   3384:        for (cp = name; 1; cp++) {
                   3385:                if ('\0' == *cp || ' ' == *cp) {
                   3386:                        namesz = cp - name;
                   3387:                        break;
                   3388:                }
1.121     schwarze 3389:                if ('\\' != *cp)
                   3390:                        continue;
1.215     schwarze 3391:                namesz = cp - name;
                   3392:                if ('{' == cp[1] || '}' == cp[1])
                   3393:                        break;
1.121     schwarze 3394:                cp++;
                   3395:                if ('\\' == *cp)
                   3396:                        continue;
1.224     schwarze 3397:                mandoc_vmsg(MANDOCERR_NAMESC, r->parse, ln, pos,
                   3398:                    "%.*s", (int)(cp - name + 1), name);
1.212     schwarze 3399:                mandoc_escape((const char **)&cp, NULL, NULL);
                   3400:                break;
1.121     schwarze 3401:        }
                   3402:
                   3403:        /* Read past spaces. */
                   3404:        while (' ' == *cp)
                   3405:                cp++;
                   3406:
                   3407:        *cpp = cp;
1.277     schwarze 3408:        return namesz;
1.121     schwarze 3409: }
                   3410:
1.106     kristaps 3411: /*
                   3412:  * Store *string into the user-defined string called *name.
                   3413:  * To clear an existing entry, call with (*r, *name, NULL, 0).
1.193     schwarze 3414:  * append == 0: replace mode
                   3415:  * append == 1: single-line append mode
                   3416:  * append == 2: multiline append mode, append '\n' after each call
1.106     kristaps 3417:  */
1.94      kristaps 3418: static void
1.106     kristaps 3419: roff_setstr(struct roff *r, const char *name, const char *string,
1.193     schwarze 3420:        int append)
1.92      schwarze 3421: {
1.164     kristaps 3422:
                   3423:        roff_setstrn(&r->strtab, name, strlen(name), string,
1.207     schwarze 3424:            string ? strlen(string) : 0, append);
1.164     kristaps 3425: }
                   3426:
                   3427: static void
1.166     kristaps 3428: roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
1.193     schwarze 3429:                const char *string, size_t stringsz, int append)
1.164     kristaps 3430: {
1.166     kristaps 3431:        struct roffkv   *n;
1.164     kristaps 3432:        char            *c;
                   3433:        int              i;
                   3434:        size_t           oldch, newch;
1.92      schwarze 3435:
1.106     kristaps 3436:        /* Search for an existing string with the same name. */
1.164     kristaps 3437:        n = *r;
                   3438:
1.211     schwarze 3439:        while (n && (namesz != n->key.sz ||
                   3440:                        strncmp(n->key.p, name, namesz)))
1.92      schwarze 3441:                n = n->next;
1.94      kristaps 3442:
                   3443:        if (NULL == n) {
1.106     kristaps 3444:                /* Create a new string table entry. */
1.166     kristaps 3445:                n = mandoc_malloc(sizeof(struct roffkv));
                   3446:                n->key.p = mandoc_strndup(name, namesz);
                   3447:                n->key.sz = namesz;
                   3448:                n->val.p = NULL;
                   3449:                n->val.sz = 0;
1.164     kristaps 3450:                n->next = *r;
                   3451:                *r = n;
1.193     schwarze 3452:        } else if (0 == append) {
1.166     kristaps 3453:                free(n->val.p);
                   3454:                n->val.p = NULL;
                   3455:                n->val.sz = 0;
1.106     kristaps 3456:        }
                   3457:
                   3458:        if (NULL == string)
                   3459:                return;
                   3460:
                   3461:        /*
                   3462:         * One additional byte for the '\n' in multiline mode,
                   3463:         * and one for the terminating '\0'.
                   3464:         */
1.193     schwarze 3465:        newch = stringsz + (1 < append ? 2u : 1u);
1.164     kristaps 3466:
1.166     kristaps 3467:        if (NULL == n->val.p) {
                   3468:                n->val.p = mandoc_malloc(newch);
                   3469:                *n->val.p = '\0';
1.106     kristaps 3470:                oldch = 0;
                   3471:        } else {
1.166     kristaps 3472:                oldch = n->val.sz;
                   3473:                n->val.p = mandoc_realloc(n->val.p, oldch + newch);
1.106     kristaps 3474:        }
                   3475:
                   3476:        /* Skip existing content in the destination buffer. */
1.166     kristaps 3477:        c = n->val.p + (int)oldch;
1.106     kristaps 3478:
                   3479:        /* Append new content to the destination buffer. */
1.164     kristaps 3480:        i = 0;
                   3481:        while (i < (int)stringsz) {
1.106     kristaps 3482:                /*
                   3483:                 * Rudimentary roff copy mode:
                   3484:                 * Handle escaped backslashes.
                   3485:                 */
1.164     kristaps 3486:                if ('\\' == string[i] && '\\' == string[i + 1])
                   3487:                        i++;
                   3488:                *c++ = string[i++];
1.106     kristaps 3489:        }
1.94      kristaps 3490:
1.106     kristaps 3491:        /* Append terminating bytes. */
1.193     schwarze 3492:        if (1 < append)
1.106     kristaps 3493:                *c++ = '\n';
1.163     kristaps 3494:
1.106     kristaps 3495:        *c = '\0';
1.166     kristaps 3496:        n->val.sz = (int)(c - n->val.p);
1.92      schwarze 3497: }
                   3498:
1.94      kristaps 3499: static const char *
                   3500: roff_getstrn(const struct roff *r, const char *name, size_t len)
1.92      schwarze 3501: {
1.166     kristaps 3502:        const struct roffkv *n;
1.191     schwarze 3503:        int i;
1.92      schwarze 3504:
1.164     kristaps 3505:        for (n = r->strtab; n; n = n->next)
1.207     schwarze 3506:                if (0 == strncmp(name, n->key.p, len) &&
                   3507:                    '\0' == n->key.p[(int)len])
1.277     schwarze 3508:                        return n->val.p;
1.191     schwarze 3509:
                   3510:        for (i = 0; i < PREDEFS_MAX; i++)
                   3511:                if (0 == strncmp(name, predefs[i].name, len) &&
                   3512:                                '\0' == predefs[i].name[(int)len])
1.277     schwarze 3513:                        return predefs[i].str;
1.306     schwarze 3514:
                   3515:        return NULL;
                   3516: }
                   3517:
                   3518: /*
                   3519:  * Check whether *name is the renamed name of a high-level macro.
                   3520:  * Return the standard name, or NULL if it is not.
                   3521:  */
                   3522: static const char *
                   3523: roff_getrenn(const struct roff *r, const char *name, size_t len)
                   3524: {
                   3525:        const struct roffkv *n;
                   3526:
                   3527:        for (n = r->rentab; n; n = n->next)
                   3528:                if (0 == strncmp(name, n->key.p, len) &&
                   3529:                    '\0' == n->key.p[(int)len])
                   3530:                        return n->val.p;
1.94      kristaps 3531:
1.277     schwarze 3532:        return NULL;
1.92      schwarze 3533: }
                   3534:
1.94      kristaps 3535: static void
1.167     kristaps 3536: roff_freestr(struct roffkv *r)
1.92      schwarze 3537: {
1.166     kristaps 3538:        struct roffkv    *n, *nn;
1.92      schwarze 3539:
1.167     kristaps 3540:        for (n = r; n; n = nn) {
1.166     kristaps 3541:                free(n->key.p);
                   3542:                free(n->val.p);
1.92      schwarze 3543:                nn = n->next;
                   3544:                free(n);
                   3545:        }
1.114     kristaps 3546: }
1.266     schwarze 3547:
                   3548: /* --- accessors and utility functions ------------------------------------ */
1.114     kristaps 3549:
                   3550: const struct tbl_span *
                   3551: roff_span(const struct roff *r)
                   3552: {
1.207     schwarze 3553:
1.277     schwarze 3554:        return r->tbl ? tbl_span(r->tbl) : NULL;
1.125     kristaps 3555: }
                   3556:
                   3557: const struct eqn *
                   3558: roff_eqn(const struct roff *r)
                   3559: {
1.207     schwarze 3560:
1.277     schwarze 3561:        return r->last_eqn ? &r->last_eqn->eqn : NULL;
1.164     kristaps 3562: }
                   3563:
                   3564: /*
                   3565:  * Duplicate an input string, making the appropriate character
                   3566:  * conversations (as stipulated by `tr') along the way.
                   3567:  * Returns a heap-allocated string with all the replacements made.
                   3568:  */
                   3569: char *
                   3570: roff_strdup(const struct roff *r, const char *p)
                   3571: {
1.166     kristaps 3572:        const struct roffkv *cp;
1.164     kristaps 3573:        char            *res;
                   3574:        const char      *pp;
                   3575:        size_t           ssz, sz;
                   3576:        enum mandoc_esc  esc;
                   3577:
1.167     kristaps 3578:        if (NULL == r->xmbtab && NULL == r->xtab)
1.277     schwarze 3579:                return mandoc_strdup(p);
1.164     kristaps 3580:        else if ('\0' == *p)
1.277     schwarze 3581:                return mandoc_strdup("");
1.164     kristaps 3582:
                   3583:        /*
                   3584:         * Step through each character looking for term matches
                   3585:         * (remember that a `tr' can be invoked with an escape, which is
                   3586:         * a glyph but the escape is multi-character).
                   3587:         * We only do this if the character hash has been initialised
                   3588:         * and the string is >0 length.
                   3589:         */
                   3590:
                   3591:        res = NULL;
                   3592:        ssz = 0;
                   3593:
                   3594:        while ('\0' != *p) {
1.289     schwarze 3595:                assert((unsigned int)*p < 128);
                   3596:                if ('\\' != *p && r->xtab && r->xtab[(unsigned int)*p].p) {
1.167     kristaps 3597:                        sz = r->xtab[(int)*p].sz;
                   3598:                        res = mandoc_realloc(res, ssz + sz + 1);
                   3599:                        memcpy(res + ssz, r->xtab[(int)*p].p, sz);
                   3600:                        ssz += sz;
                   3601:                        p++;
                   3602:                        continue;
                   3603:                } else if ('\\' != *p) {
                   3604:                        res = mandoc_realloc(res, ssz + 2);
                   3605:                        res[ssz++] = *p++;
                   3606:                        continue;
                   3607:                }
                   3608:
1.164     kristaps 3609:                /* Search for term matches. */
1.167     kristaps 3610:                for (cp = r->xmbtab; cp; cp = cp->next)
1.166     kristaps 3611:                        if (0 == strncmp(p, cp->key.p, cp->key.sz))
1.164     kristaps 3612:                                break;
                   3613:
                   3614:                if (NULL != cp) {
                   3615:                        /*
                   3616:                         * A match has been found.
                   3617:                         * Append the match to the array and move
                   3618:                         * forward by its keysize.
                   3619:                         */
1.207     schwarze 3620:                        res = mandoc_realloc(res,
                   3621:                            ssz + cp->val.sz + 1);
1.166     kristaps 3622:                        memcpy(res + ssz, cp->val.p, cp->val.sz);
                   3623:                        ssz += cp->val.sz;
                   3624:                        p += (int)cp->key.sz;
1.164     kristaps 3625:                        continue;
                   3626:                }
                   3627:
1.167     kristaps 3628:                /*
                   3629:                 * Handle escapes carefully: we need to copy
                   3630:                 * over just the escape itself, or else we might
                   3631:                 * do replacements within the escape itself.
                   3632:                 * Make sure to pass along the bogus string.
                   3633:                 */
                   3634:                pp = p++;
                   3635:                esc = mandoc_escape(&p, NULL, NULL);
                   3636:                if (ESCAPE_ERROR == esc) {
                   3637:                        sz = strlen(pp);
1.164     kristaps 3638:                        res = mandoc_realloc(res, ssz + sz + 1);
                   3639:                        memcpy(res + ssz, pp, sz);
1.167     kristaps 3640:                        break;
1.164     kristaps 3641:                }
1.207     schwarze 3642:                /*
                   3643:                 * We bail out on bad escapes.
1.167     kristaps 3644:                 * No need to warn: we already did so when
                   3645:                 * roff_res() was called.
                   3646:                 */
                   3647:                sz = (int)(p - pp);
                   3648:                res = mandoc_realloc(res, ssz + sz + 1);
                   3649:                memcpy(res + ssz, pp, sz);
                   3650:                ssz += sz;
1.164     kristaps 3651:        }
                   3652:
                   3653:        res[(int)ssz] = '\0';
1.277     schwarze 3654:        return res;
1.227     schwarze 3655: }
                   3656:
                   3657: int
                   3658: roff_getformat(const struct roff *r)
                   3659: {
                   3660:
1.277     schwarze 3661:        return r->format;
1.174     kristaps 3662: }
                   3663:
                   3664: /*
1.207     schwarze 3665:  * Find out whether a line is a macro line or not.
1.174     kristaps 3666:  * If it is, adjust the current position and return one; if it isn't,
                   3667:  * return zero and don't change the current position.
                   3668:  * If the control character has been set with `.cc', then let that grain
                   3669:  * precedence.
                   3670:  * This is slighly contrary to groff, where using the non-breaking
                   3671:  * control character when `cc' has been invoked will cause the
                   3672:  * non-breaking macro contents to be printed verbatim.
                   3673:  */
                   3674: int
                   3675: roff_getcontrol(const struct roff *r, const char *cp, int *ppos)
                   3676: {
                   3677:        int             pos;
                   3678:
                   3679:        pos = *ppos;
                   3680:
1.303     schwarze 3681:        if (r->control != '\0' && cp[pos] == r->control)
1.174     kristaps 3682:                pos++;
1.303     schwarze 3683:        else if (r->control != '\0')
1.277     schwarze 3684:                return 0;
1.174     kristaps 3685:        else if ('\\' == cp[pos] && '.' == cp[pos + 1])
                   3686:                pos += 2;
                   3687:        else if ('.' == cp[pos] || '\'' == cp[pos])
                   3688:                pos++;
                   3689:        else
1.277     schwarze 3690:                return 0;
1.174     kristaps 3691:
                   3692:        while (' ' == cp[pos] || '\t' == cp[pos])
                   3693:                pos++;
                   3694:
                   3695:        *ppos = pos;
1.277     schwarze 3696:        return 1;
1.74      kristaps 3697: }

CVSweb