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

Annotation of mandoc/roff.c, Revision 1.295

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

CVSweb