[BACK]Return to roff.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/roff.c, Revision 1.264

1.264   ! schwarze    1: /* $OpenBSD: roff.c,v 1.263 2022/05/31 20:21:40 schwarze Exp $ */
1.1       schwarze    2: /*
1.253     schwarze    3:  * Copyright (c) 2010-2015, 2017-2022 Ingo Schwarze <schwarze@openbsd.org>
1.139     schwarze    4:  * Copyright (c) 2008-2012, 2014 Kristaps Dzonsons <kristaps@bsd.lv>
1.1       schwarze    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
1.16      schwarze   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1       schwarze   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.16      schwarze   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1       schwarze   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.244     schwarze   17:  *
                     18:  * Implementation of the roff(7) parser for mandoc(1).
1.1       schwarze   19:  */
1.99      schwarze   20: #include <sys/types.h>
                     21:
1.1       schwarze   22: #include <assert.h>
1.3       schwarze   23: #include <ctype.h>
1.117     schwarze   24: #include <limits.h>
1.167     schwarze   25: #include <stddef.h>
                     26: #include <stdint.h>
1.51      schwarze   27: #include <stdio.h>
1.1       schwarze   28: #include <stdlib.h>
                     29: #include <string.h>
                     30:
1.74      schwarze   31: #include "mandoc_aux.h"
1.167     schwarze   32: #include "mandoc_ohash.h"
1.217     schwarze   33: #include "mandoc.h"
1.137     schwarze   34: #include "roff.h"
1.221     schwarze   35: #include "mandoc_parse.h"
1.98      daniel     36: #include "libmandoc.h"
1.138     schwarze   37: #include "roff_int.h"
1.218     schwarze   38: #include "tbl_parse.h"
1.219     schwarze   39: #include "eqn_parse.h"
1.2       schwarze   40:
1.227     schwarze   41: /*
                     42:  * ASCII_ESC is used to signal from roff_getarg() to roff_expand()
                     43:  * that an escape sequence resulted from copy-in processing and
                     44:  * needs to be checked or interpolated.  As it is used nowhere
                     45:  * else, it is defined here rather than in a header file.
                     46:  */
                     47: #define        ASCII_ESC       27
                     48:
1.43      schwarze   49: /* Maximum number of string expansions per line, to break infinite loops. */
                     50: #define        EXPAND_LIMIT    1000
                     51:
1.187     schwarze   52: /* Types of definitions of macros and strings. */
                     53: #define        ROFFDEF_USER    (1 << 1)  /* User-defined. */
                     54: #define        ROFFDEF_PRE     (1 << 2)  /* Predefined. */
                     55: #define        ROFFDEF_REN     (1 << 3)  /* Renamed standard macro. */
                     56: #define        ROFFDEF_STD     (1 << 4)  /* mdoc(7) or man(7) macro. */
                     57: #define        ROFFDEF_ANY     (ROFFDEF_USER | ROFFDEF_PRE | \
                     58:                         ROFFDEF_REN | ROFFDEF_STD)
1.197     schwarze   59: #define        ROFFDEF_UNDEF   (1 << 5)  /* Completely undefined. */
1.187     schwarze   60:
1.138     schwarze   61: /* --- data types --------------------------------------------------------- */
                     62:
1.41      schwarze   63: /*
1.42      schwarze   64:  * An incredibly-simple string buffer.
                     65:  */
1.8       schwarze   66: struct roffstr {
1.42      schwarze   67:        char            *p; /* nil-terminated buffer */
                     68:        size_t           sz; /* saved strlen(p) */
                     69: };
                     70:
                     71: /*
                     72:  * A key-value roffstr pair as part of a singly-linked list.
                     73:  */
                     74: struct roffkv {
                     75:        struct roffstr   key;
                     76:        struct roffstr   val;
                     77:        struct roffkv   *next; /* next in list */
1.8       schwarze   78: };
                     79:
1.52      schwarze   80: /*
                     81:  * A single number register as part of a singly-linked list.
                     82:  */
                     83: struct roffreg {
                     84:        struct roffstr   key;
1.53      schwarze   85:        int              val;
1.199     schwarze   86:        int              step;
1.52      schwarze   87:        struct roffreg  *next;
                     88: };
                     89:
1.167     schwarze   90: /*
                     91:  * Association of request and macro names with token IDs.
                     92:  */
                     93: struct roffreq {
                     94:        enum roff_tok    tok;
                     95:        char             name[];
                     96: };
                     97:
1.211     schwarze   98: /*
                     99:  * A macro processing context.
                    100:  * More than one is needed when macro calls are nested.
                    101:  */
                    102: struct mctx {
                    103:        char            **argv;
                    104:        int              argc;
                    105:        int              argsz;
                    106: };
                    107:
1.1       schwarze  108: struct roff {
1.168     schwarze  109:        struct roff_man *man; /* mdoc or man parser */
1.1       schwarze  110:        struct roffnode *last; /* leaf of stack */
1.211     schwarze  111:        struct mctx     *mstack; /* stack of macro contexts */
1.96      schwarze  112:        int             *rstack; /* stack of inverted `ie' values */
1.167     schwarze  113:        struct ohash    *reqtab; /* request lookup table */
1.52      schwarze  114:        struct roffreg  *regtab; /* number registers */
1.42      schwarze  115:        struct roffkv   *strtab; /* user-defined strings & macros */
1.178     schwarze  116:        struct roffkv   *rentab; /* renamed strings & macros */
1.42      schwarze  117:        struct roffkv   *xmbtab; /* multi-byte trans table (`tr') */
                    118:        struct roffstr  *xtab; /* single-byte trans table (`tr') */
1.16      schwarze  119:        const char      *current_string; /* value of last called user macro */
1.27      schwarze  120:        struct tbl_node *first_tbl; /* first table parsed */
                    121:        struct tbl_node *last_tbl; /* last table parsed */
                    122:        struct tbl_node *tbl; /* current table being parsed */
1.191     schwarze  123:        struct eqn_node *last_eqn; /* equation parser */
                    124:        struct eqn_node *eqn; /* active equation parser */
1.102     schwarze  125:        int              eqn_inline; /* current equation is inline */
1.96      schwarze  126:        int              options; /* parse options */
1.211     schwarze  127:        int              mstacksz; /* current size of mstack */
                    128:        int              mstackpos; /* position in mstack */
1.96      schwarze  129:        int              rstacksz; /* current size limit of rstack */
                    130:        int              rstackpos; /* position in rstack */
1.99      schwarze  131:        int              format; /* current file in mdoc or man format */
1.96      schwarze  132:        char             control; /* control character */
1.175     schwarze  133:        char             escape; /* escape character */
1.1       schwarze  134: };
                    135:
1.236     schwarze  136: /*
                    137:  * A macro definition, condition, or ignored block.
                    138:  */
1.1       schwarze  139: struct roffnode {
1.166     schwarze  140:        enum roff_tok    tok; /* type of node */
1.1       schwarze  141:        struct roffnode *parent; /* up one in stack */
                    142:        int              line; /* parse line */
                    143:        int              col; /* parse col */
1.16      schwarze  144:        char            *name; /* node name, e.g. macro name */
1.236     schwarze  145:        char            *end; /* custom end macro of the block */
                    146:        int              endspan; /* scope to: 1=eol 2=next line -1=\} */
                    147:        int              rule; /* content is: 1=evaluated 0=skipped */
1.1       schwarze  148: };
                    149:
                    150: #define        ROFF_ARGS        struct roff *r, /* parse ctx */ \
1.166     schwarze  151:                         enum roff_tok tok, /* tok of macro */ \
1.110     schwarze  152:                         struct buf *buf, /* input buffer */ \
1.1       schwarze  153:                         int ln, /* parse line */ \
1.2       schwarze  154:                         int ppos, /* original pos in buffer */ \
                    155:                         int pos, /* current pos in buffer */ \
                    156:                         int *offs /* reset offset of buffer data */
1.1       schwarze  157:
1.212     schwarze  158: typedef        int (*roffproc)(ROFF_ARGS);
1.1       schwarze  159:
                    160: struct roffmac {
1.2       schwarze  161:        roffproc         proc; /* process new macro */
                    162:        roffproc         text; /* process as child text of macro */
                    163:        roffproc         sub; /* process as child of macro */
                    164:        int              flags;
                    165: #define        ROFFMAC_STRUCT  (1 << 0) /* always interpret */
1.1       schwarze  166: };
                    167:
1.37      schwarze  168: struct predef {
                    169:        const char      *name; /* predefined input name */
                    170:        const char      *str; /* replacement symbol */
                    171: };
                    172:
                    173: #define        PREDEF(__name, __str) \
                    174:        { (__name), (__str) },
                    175:
1.138     schwarze  176: /* --- function prototypes ------------------------------------------------ */
                    177:
1.212     schwarze  178: static int              roffnode_cleanscope(struct roff *);
                    179: static int              roffnode_pop(struct roff *);
1.166     schwarze  180: static void             roffnode_push(struct roff *, enum roff_tok,
1.42      schwarze  181:                                const char *, int, int);
1.218     schwarze  182: static void             roff_addtbl(struct roff_man *, int, struct tbl_node *);
1.212     schwarze  183: static int              roff_als(ROFF_ARGS);
                    184: static int              roff_block(ROFF_ARGS);
                    185: static int              roff_block_text(ROFF_ARGS);
                    186: static int              roff_block_sub(ROFF_ARGS);
1.236     schwarze  187: static int              roff_break(ROFF_ARGS);
1.212     schwarze  188: static int              roff_cblock(ROFF_ARGS);
                    189: static int              roff_cc(ROFF_ARGS);
                    190: static int              roff_ccond(struct roff *, int, int);
1.213     schwarze  191: static int              roff_char(ROFF_ARGS);
1.212     schwarze  192: static int              roff_cond(ROFF_ARGS);
1.247     schwarze  193: static int              roff_cond_checkend(ROFF_ARGS);
1.212     schwarze  194: static int              roff_cond_text(ROFF_ARGS);
                    195: static int              roff_cond_sub(ROFF_ARGS);
                    196: static int              roff_ds(ROFF_ARGS);
                    197: static int              roff_ec(ROFF_ARGS);
                    198: static int              roff_eo(ROFF_ARGS);
                    199: static int              roff_eqndelim(struct roff *, struct buf *, int);
1.244     schwarze  200: static int              roff_evalcond(struct roff *, int, char *, int *);
1.106     schwarze  201: static int              roff_evalnum(struct roff *, int,
                    202:                                const char *, int *, int *, int);
                    203: static int              roff_evalpar(struct roff *, int,
1.133     schwarze  204:                                const char *, int *, int *, int);
1.71      schwarze  205: static int              roff_evalstrcond(const char *, int *);
1.227     schwarze  206: static int              roff_expand(struct roff *, struct buf *,
                    207:                                int, int, char);
1.260     schwarze  208: static void             roff_expand_patch(struct buf *, int,
                    209:                                const char *, int);
1.42      schwarze  210: static void             roff_free1(struct roff *);
1.52      schwarze  211: static void             roff_freereg(struct roffreg *);
1.42      schwarze  212: static void             roff_freestr(struct roffkv *);
1.85      schwarze  213: static size_t           roff_getname(struct roff *, char **, int, int);
1.133     schwarze  214: static int              roff_getnum(const char *, int *, int *, int);
1.56      schwarze  215: static int              roff_getop(const char *, int *, char *);
1.199     schwarze  216: static int              roff_getregn(struct roff *,
                    217:                                const char *, size_t, char);
1.145     schwarze  218: static int              roff_getregro(const struct roff *,
                    219:                                const char *name);
1.197     schwarze  220: static const char      *roff_getstrn(struct roff *,
1.187     schwarze  221:                                const char *, size_t, int *);
1.143     schwarze  222: static int              roff_hasregn(const struct roff *,
                    223:                                const char *, size_t);
1.212     schwarze  224: static int              roff_insec(ROFF_ARGS);
                    225: static int              roff_it(ROFF_ARGS);
                    226: static int              roff_line_ignore(ROFF_ARGS);
1.137     schwarze  227: static void             roff_man_alloc1(struct roff_man *);
                    228: static void             roff_man_free1(struct roff_man *);
1.212     schwarze  229: static int              roff_manyarg(ROFF_ARGS);
1.256     schwarze  230: static int              roff_mc(ROFF_ARGS);
1.230     schwarze  231: static int              roff_noarg(ROFF_ARGS);
1.212     schwarze  232: static int              roff_nop(ROFF_ARGS);
                    233: static int              roff_nr(ROFF_ARGS);
                    234: static int              roff_onearg(ROFF_ARGS);
1.166     schwarze  235: static enum roff_tok    roff_parse(struct roff *, char *, int *,
1.87      schwarze  236:                                int, int);
1.259     schwarze  237: static int              roff_parse_comment(struct roff *, struct buf *,
                    238:                                int, int, char);
1.212     schwarze  239: static int              roff_parsetext(struct roff *, struct buf *,
1.177     schwarze  240:                                int, int *);
1.212     schwarze  241: static int              roff_renamed(ROFF_ARGS);
1.258     schwarze  242: static int              roff_req_or_macro(ROFF_ARGS);
1.212     schwarze  243: static int              roff_return(ROFF_ARGS);
                    244: static int              roff_rm(ROFF_ARGS);
                    245: static int              roff_rn(ROFF_ARGS);
                    246: static int              roff_rr(ROFF_ARGS);
1.198     schwarze  247: static void             roff_setregn(struct roff *, const char *,
1.199     schwarze  248:                                size_t, int, char, int);
1.8       schwarze  249: static void             roff_setstr(struct roff *,
1.16      schwarze  250:                                const char *, const char *, int);
1.80      schwarze  251: static void             roff_setstrn(struct roffkv **, const char *,
1.42      schwarze  252:                                size_t, const char *, size_t, int);
1.212     schwarze  253: static int              roff_shift(ROFF_ARGS);
                    254: static int              roff_so(ROFF_ARGS);
                    255: static int              roff_tr(ROFF_ARGS);
                    256: static int              roff_Dd(ROFF_ARGS);
                    257: static int              roff_TE(ROFF_ARGS);
                    258: static int              roff_TS(ROFF_ARGS);
                    259: static int              roff_EQ(ROFF_ARGS);
                    260: static int              roff_EN(ROFF_ARGS);
                    261: static int              roff_T_(ROFF_ARGS);
                    262: static int              roff_unsupp(ROFF_ARGS);
                    263: static int              roff_userdef(ROFF_ARGS);
1.1       schwarze  264:
1.138     schwarze  265: /* --- constant data ------------------------------------------------------ */
                    266:
1.133     schwarze  267: #define        ROFFNUM_SCALE   (1 << 0)  /* Honour scaling in roff_getnum(). */
                    268: #define        ROFFNUM_WHITE   (1 << 1)  /* Skip whitespace in roff_evalnum(). */
                    269:
1.166     schwarze  270: const char *__roff_name[MAN_MAX + 1] = {
1.230     schwarze  271:        "br",           "ce",           "fi",           "ft",
                    272:        "ll",           "mc",           "nf",
                    273:        "po",           "rj",           "sp",
1.184     schwarze  274:        "ta",           "ti",           NULL,
1.166     schwarze  275:        "ab",           "ad",           "af",           "aln",
                    276:        "als",          "am",           "am1",          "ami",
                    277:        "ami1",         "as",           "as1",          "asciify",
                    278:        "backtrace",    "bd",           "bleedat",      "blm",
                    279:         "box",         "boxa",         "bp",           "BP",
                    280:        "break",        "breakchar",    "brnl",         "brp",
1.177     schwarze  281:        "brpnl",        "c2",           "cc",
1.166     schwarze  282:        "cf",           "cflags",       "ch",           "char",
                    283:        "chop",         "class",        "close",        "CL",
                    284:        "color",        "composite",    "continue",     "cp",
                    285:        "cropat",       "cs",           "cu",           "da",
                    286:        "dch",          "Dd",           "de",           "de1",
                    287:        "defcolor",     "dei",          "dei1",         "device",
                    288:        "devicem",      "di",           "do",           "ds",
                    289:        "ds1",          "dwh",          "dt",           "ec",
                    290:        "ecr",          "ecs",          "el",           "em",
                    291:        "EN",           "eo",           "EP",           "EQ",
                    292:        "errprint",     "ev",           "evc",          "ex",
                    293:        "fallback",     "fam",          "fc",           "fchar",
                    294:        "fcolor",       "fdeferlig",    "feature",      "fkern",
                    295:        "fl",           "flig",         "fp",           "fps",
                    296:        "fschar",       "fspacewidth",  "fspecial",     "ftr",
                    297:        "fzoom",        "gcolor",       "hc",           "hcode",
                    298:        "hidechar",     "hla",          "hlm",          "hpf",
                    299:        "hpfa",         "hpfcode",      "hw",           "hy",
                    300:        "hylang",       "hylen",        "hym",          "hypp",
                    301:        "hys",          "ie",           "if",           "ig",
                    302:        "index",        "it",           "itc",          "IX",
                    303:        "kern",         "kernafter",    "kernbefore",   "kernpair",
                    304:        "lc",           "lc_ctype",     "lds",          "length",
                    305:        "letadj",       "lf",           "lg",           "lhang",
                    306:        "linetabs",     "lnr",          "lnrf",         "lpfx",
1.176     schwarze  307:        "ls",           "lsm",          "lt",
1.166     schwarze  308:        "mediasize",    "minss",        "mk",           "mso",
                    309:        "na",           "ne",           "nh",           "nhychar",
                    310:        "nm",           "nn",           "nop",          "nr",
                    311:        "nrf",          "nroff",        "ns",           "nx",
                    312:        "open",         "opena",        "os",           "output",
                    313:        "padj",         "papersize",    "pc",           "pev",
                    314:        "pi",           "PI",           "pl",           "pm",
1.184     schwarze  315:        "pn",           "pnr",          "ps",
1.166     schwarze  316:        "psbb",         "pshape",       "pso",          "ptr",
                    317:        "pvs",          "rchar",        "rd",           "recursionlimit",
1.181     schwarze  318:        "return",       "rfschar",      "rhang",
1.166     schwarze  319:        "rm",           "rn",           "rnn",          "rr",
                    320:        "rs",           "rt",           "schar",        "sentchar",
                    321:        "shc",          "shift",        "sizes",        "so",
                    322:        "spacewidth",   "special",      "spreadwarn",   "ss",
                    323:        "sty",          "substring",    "sv",           "sy",
1.172     schwarze  324:        "T&",           "tc",           "TE",
1.173     schwarze  325:        "TH",           "tkf",          "tl",
1.166     schwarze  326:        "tm",           "tm1",          "tmc",          "tr",
                    327:        "track",        "transchar",    "trf",          "trimat",
                    328:        "trin",         "trnt",         "troff",        "TS",
                    329:        "uf",           "ul",           "unformat",     "unwatch",
                    330:        "unwatchn",     "vpt",          "vs",           "warn",
                    331:        "warnscale",    "watch",        "watchlength",  "watchn",
                    332:        "wh",           "while",        "write",        "writec",
                    333:        "writem",       "xflag",        ".",            NULL,
1.178     schwarze  334:        NULL,           "text",
1.166     schwarze  335:        "Dd",           "Dt",           "Os",           "Sh",
                    336:        "Ss",           "Pp",           "D1",           "Dl",
                    337:        "Bd",           "Ed",           "Bl",           "El",
                    338:        "It",           "Ad",           "An",           "Ap",
                    339:        "Ar",           "Cd",           "Cm",           "Dv",
                    340:        "Er",           "Ev",           "Ex",           "Fa",
                    341:        "Fd",           "Fl",           "Fn",           "Ft",
                    342:        "Ic",           "In",           "Li",           "Nd",
                    343:        "Nm",           "Op",           "Ot",           "Pa",
                    344:        "Rv",           "St",           "Va",           "Vt",
                    345:        "Xr",           "%A",           "%B",           "%D",
                    346:        "%I",           "%J",           "%N",           "%O",
                    347:        "%P",           "%R",           "%T",           "%V",
                    348:        "Ac",           "Ao",           "Aq",           "At",
                    349:        "Bc",           "Bf",           "Bo",           "Bq",
                    350:        "Bsx",          "Bx",           "Db",           "Dc",
                    351:        "Do",           "Dq",           "Ec",           "Ef",
                    352:        "Em",           "Eo",           "Fx",           "Ms",
                    353:        "No",           "Ns",           "Nx",           "Ox",
                    354:        "Pc",           "Pf",           "Po",           "Pq",
                    355:        "Qc",           "Ql",           "Qo",           "Qq",
                    356:        "Re",           "Rs",           "Sc",           "So",
                    357:        "Sq",           "Sm",           "Sx",           "Sy",
                    358:        "Tn",           "Ux",           "Xc",           "Xo",
                    359:        "Fo",           "Fc",           "Oo",           "Oc",
                    360:        "Bk",           "Ek",           "Bt",           "Hf",
                    361:        "Fr",           "Ud",           "Lb",           "Lp",
                    362:        "Lk",           "Mt",           "Brq",          "Bro",
                    363:        "Brc",          "%C",           "Es",           "En",
1.171     schwarze  364:        "Dx",           "%Q",           "%U",           "Ta",
1.241     schwarze  365:        "Tg",           NULL,
1.166     schwarze  366:        "TH",           "SH",           "SS",           "TP",
1.204     schwarze  367:        "TQ",
1.166     schwarze  368:        "LP",           "PP",           "P",            "IP",
                    369:        "HP",           "SM",           "SB",           "BI",
                    370:        "IB",           "BR",           "RB",           "R",
                    371:        "B",            "I",            "IR",           "RI",
                    372:        "RE",           "RS",           "DT",           "UC",
1.169     schwarze  373:        "PD",           "AT",           "in",
1.205     schwarze  374:        "SY",           "YS",           "OP",
                    375:        "EX",           "EE",           "UR",
1.189     bentley   376:        "UE",           "MT",           "ME",           NULL
1.166     schwarze  377: };
                    378: const  char *const *roff_name = __roff_name;
                    379:
                    380: static struct roffmac   roffs[TOKEN_NONE] = {
1.230     schwarze  381:        { roff_noarg, NULL, NULL, 0 },  /* br */
1.177     schwarze  382:        { roff_onearg, NULL, NULL, 0 },  /* ce */
1.230     schwarze  383:        { roff_noarg, NULL, NULL, 0 },  /* fi */
1.169     schwarze  384:        { roff_onearg, NULL, NULL, 0 },  /* ft */
1.170     schwarze  385:        { roff_onearg, NULL, NULL, 0 },  /* ll */
1.256     schwarze  386:        { roff_mc, NULL, NULL, 0 },  /* mc */
1.230     schwarze  387:        { roff_noarg, NULL, NULL, 0 },  /* nf */
1.184     schwarze  388:        { roff_onearg, NULL, NULL, 0 },  /* po */
1.181     schwarze  389:        { roff_onearg, NULL, NULL, 0 },  /* rj */
1.171     schwarze  390:        { roff_onearg, NULL, NULL, 0 },  /* sp */
1.172     schwarze  391:        { roff_manyarg, NULL, NULL, 0 },  /* ta */
1.173     schwarze  392:        { roff_onearg, NULL, NULL, 0 },  /* ti */
1.168     schwarze  393:        { NULL, NULL, NULL, 0 },  /* ROFF_MAX */
1.167     schwarze  394:        { roff_unsupp, NULL, NULL, 0 },  /* ab */
                    395:        { roff_line_ignore, NULL, NULL, 0 },  /* ad */
                    396:        { roff_line_ignore, NULL, NULL, 0 },  /* af */
                    397:        { roff_unsupp, NULL, NULL, 0 },  /* aln */
1.183     schwarze  398:        { roff_als, NULL, NULL, 0 },  /* als */
1.167     schwarze  399:        { roff_block, roff_block_text, roff_block_sub, 0 },  /* am */
                    400:        { roff_block, roff_block_text, roff_block_sub, 0 },  /* am1 */
                    401:        { roff_block, roff_block_text, roff_block_sub, 0 },  /* ami */
                    402:        { roff_block, roff_block_text, roff_block_sub, 0 },  /* ami1 */
                    403:        { roff_ds, NULL, NULL, 0 },  /* as */
                    404:        { roff_ds, NULL, NULL, 0 },  /* as1 */
                    405:        { roff_unsupp, NULL, NULL, 0 },  /* asciify */
                    406:        { roff_line_ignore, NULL, NULL, 0 },  /* backtrace */
                    407:        { roff_line_ignore, NULL, NULL, 0 },  /* bd */
                    408:        { roff_line_ignore, NULL, NULL, 0 },  /* bleedat */
                    409:        { roff_unsupp, NULL, NULL, 0 },  /* blm */
                    410:        { roff_unsupp, NULL, NULL, 0 },  /* box */
                    411:        { roff_unsupp, NULL, NULL, 0 },  /* boxa */
                    412:        { roff_line_ignore, NULL, NULL, 0 },  /* bp */
                    413:        { roff_unsupp, NULL, NULL, 0 },  /* BP */
1.236     schwarze  414:        { roff_break, NULL, NULL, 0 },  /* break */
1.167     schwarze  415:        { roff_line_ignore, NULL, NULL, 0 },  /* breakchar */
                    416:        { roff_line_ignore, NULL, NULL, 0 },  /* brnl */
1.230     schwarze  417:        { roff_noarg, NULL, NULL, 0 },  /* brp */
1.167     schwarze  418:        { roff_line_ignore, NULL, NULL, 0 },  /* brpnl */
                    419:        { roff_unsupp, NULL, NULL, 0 },  /* c2 */
                    420:        { roff_cc, NULL, NULL, 0 },  /* cc */
                    421:        { roff_insec, NULL, NULL, 0 },  /* cf */
                    422:        { roff_line_ignore, NULL, NULL, 0 },  /* cflags */
                    423:        { roff_line_ignore, NULL, NULL, 0 },  /* ch */
1.213     schwarze  424:        { roff_char, NULL, NULL, 0 },  /* char */
1.167     schwarze  425:        { roff_unsupp, NULL, NULL, 0 },  /* chop */
                    426:        { roff_line_ignore, NULL, NULL, 0 },  /* class */
                    427:        { roff_insec, NULL, NULL, 0 },  /* close */
                    428:        { roff_unsupp, NULL, NULL, 0 },  /* CL */
                    429:        { roff_line_ignore, NULL, NULL, 0 },  /* color */
                    430:        { roff_unsupp, NULL, NULL, 0 },  /* composite */
                    431:        { roff_unsupp, NULL, NULL, 0 },  /* continue */
                    432:        { roff_line_ignore, NULL, NULL, 0 },  /* cp */
                    433:        { roff_line_ignore, NULL, NULL, 0 },  /* cropat */
                    434:        { roff_line_ignore, NULL, NULL, 0 },  /* cs */
                    435:        { roff_line_ignore, NULL, NULL, 0 },  /* cu */
                    436:        { roff_unsupp, NULL, NULL, 0 },  /* da */
                    437:        { roff_unsupp, NULL, NULL, 0 },  /* dch */
                    438:        { roff_Dd, NULL, NULL, 0 },  /* Dd */
                    439:        { roff_block, roff_block_text, roff_block_sub, 0 },  /* de */
                    440:        { roff_block, roff_block_text, roff_block_sub, 0 },  /* de1 */
                    441:        { roff_line_ignore, NULL, NULL, 0 },  /* defcolor */
                    442:        { roff_block, roff_block_text, roff_block_sub, 0 },  /* dei */
                    443:        { roff_block, roff_block_text, roff_block_sub, 0 },  /* dei1 */
                    444:        { roff_unsupp, NULL, NULL, 0 },  /* device */
                    445:        { roff_unsupp, NULL, NULL, 0 },  /* devicem */
                    446:        { roff_unsupp, NULL, NULL, 0 },  /* di */
                    447:        { roff_unsupp, NULL, NULL, 0 },  /* do */
                    448:        { roff_ds, NULL, NULL, 0 },  /* ds */
                    449:        { roff_ds, NULL, NULL, 0 },  /* ds1 */
                    450:        { roff_unsupp, NULL, NULL, 0 },  /* dwh */
                    451:        { roff_unsupp, NULL, NULL, 0 },  /* dt */
1.175     schwarze  452:        { roff_ec, NULL, NULL, 0 },  /* ec */
1.167     schwarze  453:        { roff_unsupp, NULL, NULL, 0 },  /* ecr */
                    454:        { roff_unsupp, NULL, NULL, 0 },  /* ecs */
                    455:        { roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT },  /* el */
                    456:        { roff_unsupp, NULL, NULL, 0 },  /* em */
                    457:        { roff_EN, NULL, NULL, 0 },  /* EN */
1.175     schwarze  458:        { roff_eo, NULL, NULL, 0 },  /* eo */
1.167     schwarze  459:        { roff_unsupp, NULL, NULL, 0 },  /* EP */
                    460:        { roff_EQ, NULL, NULL, 0 },  /* EQ */
                    461:        { roff_line_ignore, NULL, NULL, 0 },  /* errprint */
                    462:        { roff_unsupp, NULL, NULL, 0 },  /* ev */
                    463:        { roff_unsupp, NULL, NULL, 0 },  /* evc */
                    464:        { roff_unsupp, NULL, NULL, 0 },  /* ex */
                    465:        { roff_line_ignore, NULL, NULL, 0 },  /* fallback */
                    466:        { roff_line_ignore, NULL, NULL, 0 },  /* fam */
                    467:        { roff_unsupp, NULL, NULL, 0 },  /* fc */
                    468:        { roff_unsupp, NULL, NULL, 0 },  /* fchar */
                    469:        { roff_line_ignore, NULL, NULL, 0 },  /* fcolor */
                    470:        { roff_line_ignore, NULL, NULL, 0 },  /* fdeferlig */
                    471:        { roff_line_ignore, NULL, NULL, 0 },  /* feature */
                    472:        { roff_line_ignore, NULL, NULL, 0 },  /* fkern */
                    473:        { roff_line_ignore, NULL, NULL, 0 },  /* fl */
                    474:        { roff_line_ignore, NULL, NULL, 0 },  /* flig */
                    475:        { roff_line_ignore, NULL, NULL, 0 },  /* fp */
                    476:        { roff_line_ignore, NULL, NULL, 0 },  /* fps */
                    477:        { roff_unsupp, NULL, NULL, 0 },  /* fschar */
                    478:        { roff_line_ignore, NULL, NULL, 0 },  /* fspacewidth */
                    479:        { roff_line_ignore, NULL, NULL, 0 },  /* fspecial */
                    480:        { roff_line_ignore, NULL, NULL, 0 },  /* ftr */
                    481:        { roff_line_ignore, NULL, NULL, 0 },  /* fzoom */
                    482:        { roff_line_ignore, NULL, NULL, 0 },  /* gcolor */
                    483:        { roff_line_ignore, NULL, NULL, 0 },  /* hc */
                    484:        { roff_line_ignore, NULL, NULL, 0 },  /* hcode */
                    485:        { roff_line_ignore, NULL, NULL, 0 },  /* hidechar */
                    486:        { roff_line_ignore, NULL, NULL, 0 },  /* hla */
                    487:        { roff_line_ignore, NULL, NULL, 0 },  /* hlm */
                    488:        { roff_line_ignore, NULL, NULL, 0 },  /* hpf */
                    489:        { roff_line_ignore, NULL, NULL, 0 },  /* hpfa */
                    490:        { roff_line_ignore, NULL, NULL, 0 },  /* hpfcode */
                    491:        { roff_line_ignore, NULL, NULL, 0 },  /* hw */
                    492:        { roff_line_ignore, NULL, NULL, 0 },  /* hy */
                    493:        { roff_line_ignore, NULL, NULL, 0 },  /* hylang */
                    494:        { roff_line_ignore, NULL, NULL, 0 },  /* hylen */
                    495:        { roff_line_ignore, NULL, NULL, 0 },  /* hym */
                    496:        { roff_line_ignore, NULL, NULL, 0 },  /* hypp */
                    497:        { roff_line_ignore, NULL, NULL, 0 },  /* hys */
                    498:        { roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT },  /* ie */
                    499:        { roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT },  /* if */
                    500:        { roff_block, roff_block_text, roff_block_sub, 0 },  /* ig */
                    501:        { roff_unsupp, NULL, NULL, 0 },  /* index */
                    502:        { roff_it, NULL, NULL, 0 },  /* it */
                    503:        { roff_unsupp, NULL, NULL, 0 },  /* itc */
                    504:        { roff_line_ignore, NULL, NULL, 0 },  /* IX */
                    505:        { roff_line_ignore, NULL, NULL, 0 },  /* kern */
                    506:        { roff_line_ignore, NULL, NULL, 0 },  /* kernafter */
                    507:        { roff_line_ignore, NULL, NULL, 0 },  /* kernbefore */
                    508:        { roff_line_ignore, NULL, NULL, 0 },  /* kernpair */
                    509:        { roff_unsupp, NULL, NULL, 0 },  /* lc */
                    510:        { roff_unsupp, NULL, NULL, 0 },  /* lc_ctype */
                    511:        { roff_unsupp, NULL, NULL, 0 },  /* lds */
                    512:        { roff_unsupp, NULL, NULL, 0 },  /* length */
                    513:        { roff_line_ignore, NULL, NULL, 0 },  /* letadj */
                    514:        { roff_insec, NULL, NULL, 0 },  /* lf */
                    515:        { roff_line_ignore, NULL, NULL, 0 },  /* lg */
                    516:        { roff_line_ignore, NULL, NULL, 0 },  /* lhang */
                    517:        { roff_unsupp, NULL, NULL, 0 },  /* linetabs */
                    518:        { roff_unsupp, NULL, NULL, 0 },  /* lnr */
                    519:        { roff_unsupp, NULL, NULL, 0 },  /* lnrf */
                    520:        { roff_unsupp, NULL, NULL, 0 },  /* lpfx */
                    521:        { roff_line_ignore, NULL, NULL, 0 },  /* ls */
                    522:        { roff_unsupp, NULL, NULL, 0 },  /* lsm */
                    523:        { roff_line_ignore, NULL, NULL, 0 },  /* lt */
                    524:        { roff_line_ignore, NULL, NULL, 0 },  /* mediasize */
                    525:        { roff_line_ignore, NULL, NULL, 0 },  /* minss */
                    526:        { roff_line_ignore, NULL, NULL, 0 },  /* mk */
                    527:        { roff_insec, NULL, NULL, 0 },  /* mso */
                    528:        { roff_line_ignore, NULL, NULL, 0 },  /* na */
                    529:        { roff_line_ignore, NULL, NULL, 0 },  /* ne */
                    530:        { roff_line_ignore, NULL, NULL, 0 },  /* nh */
                    531:        { roff_line_ignore, NULL, NULL, 0 },  /* nhychar */
                    532:        { roff_unsupp, NULL, NULL, 0 },  /* nm */
                    533:        { roff_unsupp, NULL, NULL, 0 },  /* nn */
1.202     schwarze  534:        { roff_nop, NULL, NULL, 0 },  /* nop */
1.167     schwarze  535:        { roff_nr, NULL, NULL, 0 },  /* nr */
                    536:        { roff_unsupp, NULL, NULL, 0 },  /* nrf */
                    537:        { roff_line_ignore, NULL, NULL, 0 },  /* nroff */
                    538:        { roff_line_ignore, NULL, NULL, 0 },  /* ns */
                    539:        { roff_insec, NULL, NULL, 0 },  /* nx */
                    540:        { roff_insec, NULL, NULL, 0 },  /* open */
                    541:        { roff_insec, NULL, NULL, 0 },  /* opena */
                    542:        { roff_line_ignore, NULL, NULL, 0 },  /* os */
                    543:        { roff_unsupp, NULL, NULL, 0 },  /* output */
                    544:        { roff_line_ignore, NULL, NULL, 0 },  /* padj */
                    545:        { roff_line_ignore, NULL, NULL, 0 },  /* papersize */
                    546:        { roff_line_ignore, NULL, NULL, 0 },  /* pc */
                    547:        { roff_line_ignore, NULL, NULL, 0 },  /* pev */
                    548:        { roff_insec, NULL, NULL, 0 },  /* pi */
                    549:        { roff_unsupp, NULL, NULL, 0 },  /* PI */
                    550:        { roff_line_ignore, NULL, NULL, 0 },  /* pl */
                    551:        { roff_line_ignore, NULL, NULL, 0 },  /* pm */
                    552:        { roff_line_ignore, NULL, NULL, 0 },  /* pn */
                    553:        { roff_line_ignore, NULL, NULL, 0 },  /* pnr */
                    554:        { roff_line_ignore, NULL, NULL, 0 },  /* ps */
                    555:        { roff_unsupp, NULL, NULL, 0 },  /* psbb */
                    556:        { roff_unsupp, NULL, NULL, 0 },  /* pshape */
                    557:        { roff_insec, NULL, NULL, 0 },  /* pso */
                    558:        { roff_line_ignore, NULL, NULL, 0 },  /* ptr */
                    559:        { roff_line_ignore, NULL, NULL, 0 },  /* pvs */
                    560:        { roff_unsupp, NULL, NULL, 0 },  /* rchar */
                    561:        { roff_line_ignore, NULL, NULL, 0 },  /* rd */
                    562:        { roff_line_ignore, NULL, NULL, 0 },  /* recursionlimit */
1.211     schwarze  563:        { roff_return, NULL, NULL, 0 },  /* return */
1.167     schwarze  564:        { roff_unsupp, NULL, NULL, 0 },  /* rfschar */
                    565:        { roff_line_ignore, NULL, NULL, 0 },  /* rhang */
                    566:        { roff_rm, NULL, NULL, 0 },  /* rm */
1.178     schwarze  567:        { roff_rn, NULL, NULL, 0 },  /* rn */
1.167     schwarze  568:        { roff_unsupp, NULL, NULL, 0 },  /* rnn */
                    569:        { roff_rr, NULL, NULL, 0 },  /* rr */
                    570:        { roff_line_ignore, NULL, NULL, 0 },  /* rs */
                    571:        { roff_line_ignore, NULL, NULL, 0 },  /* rt */
                    572:        { roff_unsupp, NULL, NULL, 0 },  /* schar */
                    573:        { roff_line_ignore, NULL, NULL, 0 },  /* sentchar */
                    574:        { roff_line_ignore, NULL, NULL, 0 },  /* shc */
1.211     schwarze  575:        { roff_shift, NULL, NULL, 0 },  /* shift */
1.167     schwarze  576:        { roff_line_ignore, NULL, NULL, 0 },  /* sizes */
                    577:        { roff_so, NULL, NULL, 0 },  /* so */
                    578:        { roff_line_ignore, NULL, NULL, 0 },  /* spacewidth */
                    579:        { roff_line_ignore, NULL, NULL, 0 },  /* special */
                    580:        { roff_line_ignore, NULL, NULL, 0 },  /* spreadwarn */
                    581:        { roff_line_ignore, NULL, NULL, 0 },  /* ss */
                    582:        { roff_line_ignore, NULL, NULL, 0 },  /* sty */
                    583:        { roff_unsupp, NULL, NULL, 0 },  /* substring */
                    584:        { roff_line_ignore, NULL, NULL, 0 },  /* sv */
                    585:        { roff_insec, NULL, NULL, 0 },  /* sy */
                    586:        { roff_T_, NULL, NULL, 0 },  /* T& */
                    587:        { roff_unsupp, NULL, NULL, 0 },  /* tc */
                    588:        { roff_TE, NULL, NULL, 0 },  /* TE */
1.187     schwarze  589:        { roff_Dd, NULL, NULL, 0 },  /* TH */
1.167     schwarze  590:        { roff_line_ignore, NULL, NULL, 0 },  /* tkf */
                    591:        { roff_unsupp, NULL, NULL, 0 },  /* tl */
                    592:        { roff_line_ignore, NULL, NULL, 0 },  /* tm */
                    593:        { roff_line_ignore, NULL, NULL, 0 },  /* tm1 */
                    594:        { roff_line_ignore, NULL, NULL, 0 },  /* tmc */
                    595:        { roff_tr, NULL, NULL, 0 },  /* tr */
                    596:        { roff_line_ignore, NULL, NULL, 0 },  /* track */
                    597:        { roff_line_ignore, NULL, NULL, 0 },  /* transchar */
                    598:        { roff_insec, NULL, NULL, 0 },  /* trf */
                    599:        { roff_line_ignore, NULL, NULL, 0 },  /* trimat */
                    600:        { roff_unsupp, NULL, NULL, 0 },  /* trin */
                    601:        { roff_unsupp, NULL, NULL, 0 },  /* trnt */
                    602:        { roff_line_ignore, NULL, NULL, 0 },  /* troff */
                    603:        { roff_TS, NULL, NULL, 0 },  /* TS */
                    604:        { roff_line_ignore, NULL, NULL, 0 },  /* uf */
                    605:        { roff_line_ignore, NULL, NULL, 0 },  /* ul */
                    606:        { roff_unsupp, NULL, NULL, 0 },  /* unformat */
                    607:        { roff_line_ignore, NULL, NULL, 0 },  /* unwatch */
                    608:        { roff_line_ignore, NULL, NULL, 0 },  /* unwatchn */
                    609:        { roff_line_ignore, NULL, NULL, 0 },  /* vpt */
                    610:        { roff_line_ignore, NULL, NULL, 0 },  /* vs */
                    611:        { roff_line_ignore, NULL, NULL, 0 },  /* warn */
                    612:        { roff_line_ignore, NULL, NULL, 0 },  /* warnscale */
                    613:        { roff_line_ignore, NULL, NULL, 0 },  /* watch */
                    614:        { roff_line_ignore, NULL, NULL, 0 },  /* watchlength */
                    615:        { roff_line_ignore, NULL, NULL, 0 },  /* watchn */
                    616:        { roff_unsupp, NULL, NULL, 0 },  /* wh */
1.212     schwarze  617:        { roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT }, /*while*/
1.167     schwarze  618:        { roff_insec, NULL, NULL, 0 },  /* write */
                    619:        { roff_insec, NULL, NULL, 0 },  /* writec */
                    620:        { roff_insec, NULL, NULL, 0 },  /* writem */
                    621:        { roff_line_ignore, NULL, NULL, 0 },  /* xflag */
                    622:        { roff_cblock, NULL, NULL, 0 },  /* . */
1.178     schwarze  623:        { roff_renamed, NULL, NULL, 0 },
1.167     schwarze  624:        { roff_userdef, NULL, NULL, 0 }
1.1       schwarze  625: };
                    626:
1.37      schwarze  627: /* Array of injected predefined strings. */
                    628: #define        PREDEFS_MAX      38
                    629: static const struct predef predefs[PREDEFS_MAX] = {
                    630: #include "predefs.in"
                    631: };
                    632:
1.177     schwarze  633: static int      roffce_lines;  /* number of input lines to center */
                    634: static struct roff_node *roffce_node;  /* active request */
1.51      schwarze  635: static int      roffit_lines;  /* number of lines to delay */
                    636: static char    *roffit_macro;  /* nil-terminated macro line */
                    637:
1.80      schwarze  638:
1.138     schwarze  639: /* --- request table ------------------------------------------------------ */
                    640:
1.167     schwarze  641: struct ohash *
                    642: roffhash_alloc(enum roff_tok mintok, enum roff_tok maxtok)
1.3       schwarze  643: {
1.167     schwarze  644:        struct ohash    *htab;
                    645:        struct roffreq  *req;
                    646:        enum roff_tok    tok;
                    647:        size_t           sz;
                    648:        unsigned int     slot;
1.3       schwarze  649:
1.167     schwarze  650:        htab = mandoc_malloc(sizeof(*htab));
                    651:        mandoc_ohash_init(htab, 8, offsetof(struct roffreq, name));
1.3       schwarze  652:
1.167     schwarze  653:        for (tok = mintok; tok < maxtok; tok++) {
1.168     schwarze  654:                if (roff_name[tok] == NULL)
                    655:                        continue;
1.167     schwarze  656:                sz = strlen(roff_name[tok]);
                    657:                req = mandoc_malloc(sizeof(*req) + sz + 1);
                    658:                req->tok = tok;
                    659:                memcpy(req->name, roff_name[tok], sz + 1);
                    660:                slot = ohash_qlookup(htab, req->name);
                    661:                ohash_insert(htab, slot, req);
1.3       schwarze  662:        }
1.167     schwarze  663:        return htab;
1.3       schwarze  664: }
                    665:
1.167     schwarze  666: void
                    667: roffhash_free(struct ohash *htab)
1.1       schwarze  668: {
1.167     schwarze  669:        struct roffreq  *req;
                    670:        unsigned int     slot;
1.1       schwarze  671:
1.167     schwarze  672:        if (htab == NULL)
                    673:                return;
                    674:        for (req = ohash_first(htab, &slot); req != NULL;
                    675:             req = ohash_next(htab, &slot))
                    676:                free(req);
                    677:        ohash_delete(htab);
                    678:        free(htab);
                    679: }
                    680:
                    681: enum roff_tok
                    682: roffhash_find(struct ohash *htab, const char *name, size_t sz)
                    683: {
                    684:        struct roffreq  *req;
                    685:        const char      *end;
                    686:
                    687:        if (sz) {
                    688:                end = name + sz;
                    689:                req = ohash_find(htab, ohash_qlookupi(htab, name, &end));
                    690:        } else
                    691:                req = ohash_find(htab, ohash_qlookup(htab, name));
                    692:        return req == NULL ? TOKEN_NONE : req->tok;
1.1       schwarze  693: }
                    694:
1.138     schwarze  695: /* --- stack of request blocks -------------------------------------------- */
                    696:
1.1       schwarze  697: /*
                    698:  * Pop the current node off of the stack of roff instructions currently
1.236     schwarze  699:  * pending.  Return 1 if it is a loop or 0 otherwise.
1.1       schwarze  700:  */
1.212     schwarze  701: static int
1.1       schwarze  702: roffnode_pop(struct roff *r)
                    703: {
                    704:        struct roffnode *p;
1.212     schwarze  705:        int              inloop;
1.1       schwarze  706:
1.80      schwarze  707:        p = r->last;
1.212     schwarze  708:        inloop = p->tok == ROFF_while;
                    709:        r->last = p->parent;
1.16      schwarze  710:        free(p->name);
                    711:        free(p->end);
1.1       schwarze  712:        free(p);
1.212     schwarze  713:        return inloop;
1.1       schwarze  714: }
                    715:
                    716: /*
                    717:  * Push a roff node onto the instruction stack.  This must later be
                    718:  * removed with roffnode_pop().
                    719:  */
1.11      schwarze  720: static void
1.166     schwarze  721: roffnode_push(struct roff *r, enum roff_tok tok, const char *name,
1.16      schwarze  722:                int line, int col)
1.1       schwarze  723: {
                    724:        struct roffnode *p;
                    725:
1.11      schwarze  726:        p = mandoc_calloc(1, sizeof(struct roffnode));
1.1       schwarze  727:        p->tok = tok;
1.16      schwarze  728:        if (name)
                    729:                p->name = mandoc_strdup(name);
1.1       schwarze  730:        p->parent = r->last;
                    731:        p->line = line;
                    732:        p->col = col;
1.71      schwarze  733:        p->rule = p->parent ? p->parent->rule : 0;
1.1       schwarze  734:
                    735:        r->last = p;
                    736: }
                    737:
1.138     schwarze  738: /* --- roff parser state data management ---------------------------------- */
                    739:
1.1       schwarze  740: static void
                    741: roff_free1(struct roff *r)
                    742: {
1.42      schwarze  743:        int              i;
1.27      schwarze  744:
1.218     schwarze  745:        tbl_free(r->first_tbl);
1.27      schwarze  746:        r->first_tbl = r->last_tbl = r->tbl = NULL;
1.1       schwarze  747:
1.219     schwarze  748:        eqn_free(r->last_eqn);
1.191     schwarze  749:        r->last_eqn = r->eqn = NULL;
1.32      schwarze  750:
1.211     schwarze  751:        while (r->mstackpos >= 0)
                    752:                roff_userret(r);
                    753:
1.1       schwarze  754:        while (r->last)
                    755:                roffnode_pop(r);
1.27      schwarze  756:
1.96      schwarze  757:        free (r->rstack);
                    758:        r->rstack = NULL;
                    759:        r->rstacksz = 0;
                    760:        r->rstackpos = -1;
                    761:
                    762:        roff_freereg(r->regtab);
                    763:        r->regtab = NULL;
                    764:
1.42      schwarze  765:        roff_freestr(r->strtab);
1.178     schwarze  766:        roff_freestr(r->rentab);
1.42      schwarze  767:        roff_freestr(r->xmbtab);
1.178     schwarze  768:        r->strtab = r->rentab = r->xmbtab = NULL;
1.42      schwarze  769:
                    770:        if (r->xtab)
                    771:                for (i = 0; i < 128; i++)
                    772:                        free(r->xtab[i].p);
                    773:        free(r->xtab);
                    774:        r->xtab = NULL;
1.1       schwarze  775: }
                    776:
                    777: void
                    778: roff_reset(struct roff *r)
                    779: {
                    780:        roff_free1(r);
1.239     schwarze  781:        r->options |= MPARSE_COMMENT;
1.99      schwarze  782:        r->format = r->options & (MPARSE_MDOC | MPARSE_MAN);
1.175     schwarze  783:        r->control = '\0';
                    784:        r->escape = '\\';
1.179     schwarze  785:        roffce_lines = 0;
                    786:        roffce_node = NULL;
                    787:        roffit_lines = 0;
                    788:        roffit_macro = NULL;
1.1       schwarze  789: }
                    790:
                    791: void
                    792: roff_free(struct roff *r)
                    793: {
1.238     schwarze  794:        int              i;
1.211     schwarze  795:
1.1       schwarze  796:        roff_free1(r);
1.211     schwarze  797:        for (i = 0; i < r->mstacksz; i++)
                    798:                free(r->mstack[i].argv);
                    799:        free(r->mstack);
1.167     schwarze  800:        roffhash_free(r->reqtab);
1.1       schwarze  801:        free(r);
                    802: }
                    803:
                    804: struct roff *
1.223     schwarze  805: roff_alloc(int options)
1.1       schwarze  806: {
                    807:        struct roff     *r;
                    808:
1.11      schwarze  809:        r = mandoc_calloc(1, sizeof(struct roff));
1.200     schwarze  810:        r->reqtab = roffhash_alloc(0, ROFF_RENAMED);
1.239     schwarze  811:        r->options = options | MPARSE_COMMENT;
1.99      schwarze  812:        r->format = options & (MPARSE_MDOC | MPARSE_MAN);
1.211     schwarze  813:        r->mstackpos = -1;
1.2       schwarze  814:        r->rstackpos = -1;
1.175     schwarze  815:        r->escape = '\\';
1.149     schwarze  816:        return r;
1.137     schwarze  817: }
                    818:
1.138     schwarze  819: /* --- syntax tree state data management ---------------------------------- */
                    820:
1.137     schwarze  821: static void
                    822: roff_man_free1(struct roff_man *man)
                    823: {
1.228     schwarze  824:        if (man->meta.first != NULL)
                    825:                roff_node_delete(man, man->meta.first);
1.137     schwarze  826:        free(man->meta.msec);
                    827:        free(man->meta.vol);
                    828:        free(man->meta.os);
                    829:        free(man->meta.arch);
                    830:        free(man->meta.title);
                    831:        free(man->meta.name);
                    832:        free(man->meta.date);
1.228     schwarze  833:        free(man->meta.sodest);
1.137     schwarze  834: }
                    835:
1.229     schwarze  836: void
                    837: roff_state_reset(struct roff_man *man)
                    838: {
                    839:        man->last = man->meta.first;
                    840:        man->last_es = NULL;
                    841:        man->flags = 0;
                    842:        man->lastsec = man->lastnamed = SEC_NONE;
                    843:        man->next = ROFF_NEXT_CHILD;
                    844:        roff_setreg(man->roff, "nS", 0, '=');
                    845: }
                    846:
1.137     schwarze  847: static void
                    848: roff_man_alloc1(struct roff_man *man)
                    849: {
                    850:        memset(&man->meta, 0, sizeof(man->meta));
1.228     schwarze  851:        man->meta.first = mandoc_calloc(1, sizeof(*man->meta.first));
                    852:        man->meta.first->type = ROFFT_ROOT;
                    853:        man->meta.macroset = MACROSET_NONE;
1.229     schwarze  854:        roff_state_reset(man);
1.137     schwarze  855: }
                    856:
                    857: void
                    858: roff_man_reset(struct roff_man *man)
                    859: {
                    860:        roff_man_free1(man);
                    861:        roff_man_alloc1(man);
                    862: }
                    863:
                    864: void
                    865: roff_man_free(struct roff_man *man)
                    866: {
                    867:        roff_man_free1(man);
1.252     schwarze  868:        free(man->os_r);
1.137     schwarze  869:        free(man);
                    870: }
                    871:
                    872: struct roff_man *
1.223     schwarze  873: roff_man_alloc(struct roff *roff, const char *os_s, int quick)
1.137     schwarze  874: {
                    875:        struct roff_man *man;
                    876:
                    877:        man = mandoc_calloc(1, sizeof(*man));
                    878:        man->roff = roff;
1.188     schwarze  879:        man->os_s = os_s;
1.137     schwarze  880:        man->quick = quick;
                    881:        roff_man_alloc1(man);
1.168     schwarze  882:        roff->man = man;
1.149     schwarze  883:        return man;
1.1       schwarze  884: }
                    885:
1.138     schwarze  886: /* --- syntax tree handling ----------------------------------------------- */
                    887:
                    888: struct roff_node *
                    889: roff_node_alloc(struct roff_man *man, int line, int pos,
                    890:        enum roff_type type, int tok)
                    891: {
                    892:        struct roff_node        *n;
                    893:
                    894:        n = mandoc_calloc(1, sizeof(*n));
                    895:        n->line = line;
                    896:        n->pos = pos;
                    897:        n->tok = tok;
                    898:        n->type = type;
                    899:        n->sec = man->lastsec;
                    900:
                    901:        if (man->flags & MDOC_SYNOPSIS)
1.157     schwarze  902:                n->flags |= NODE_SYNPRETTY;
1.138     schwarze  903:        else
1.157     schwarze  904:                n->flags &= ~NODE_SYNPRETTY;
1.232     schwarze  905:        if ((man->flags & (ROFF_NOFILL | ROFF_NONOFILL)) == ROFF_NOFILL)
1.231     schwarze  906:                n->flags |= NODE_NOFILL;
                    907:        else
                    908:                n->flags &= ~NODE_NOFILL;
1.138     schwarze  909:        if (man->flags & MDOC_NEWLINE)
1.157     schwarze  910:                n->flags |= NODE_LINE;
1.138     schwarze  911:        man->flags &= ~MDOC_NEWLINE;
                    912:
1.149     schwarze  913:        return n;
1.138     schwarze  914: }
                    915:
                    916: void
                    917: roff_node_append(struct roff_man *man, struct roff_node *n)
                    918: {
                    919:
                    920:        switch (man->next) {
                    921:        case ROFF_NEXT_SIBLING:
1.153     schwarze  922:                if (man->last->next != NULL) {
                    923:                        n->next = man->last->next;
                    924:                        man->last->next->prev = n;
                    925:                } else
                    926:                        man->last->parent->last = n;
1.138     schwarze  927:                man->last->next = n;
                    928:                n->prev = man->last;
                    929:                n->parent = man->last->parent;
                    930:                break;
                    931:        case ROFF_NEXT_CHILD:
1.159     schwarze  932:                if (man->last->child != NULL) {
                    933:                        n->next = man->last->child;
                    934:                        man->last->child->prev = n;
                    935:                } else
                    936:                        man->last->last = n;
1.138     schwarze  937:                man->last->child = n;
                    938:                n->parent = man->last;
                    939:                break;
                    940:        default:
                    941:                abort();
                    942:        }
1.154     schwarze  943:        man->last = n;
1.138     schwarze  944:
                    945:        switch (n->type) {
                    946:        case ROFFT_HEAD:
                    947:                n->parent->head = n;
                    948:                break;
                    949:        case ROFFT_BODY:
1.154     schwarze  950:                if (n->end != ENDBODY_NOT)
                    951:                        return;
1.138     schwarze  952:                n->parent->body = n;
                    953:                break;
                    954:        case ROFFT_TAIL:
                    955:                n->parent->tail = n;
                    956:                break;
                    957:        default:
1.154     schwarze  958:                return;
1.138     schwarze  959:        }
1.154     schwarze  960:
                    961:        /*
                    962:         * Copy over the normalised-data pointer of our parent.  Not
                    963:         * everybody has one, but copying a null pointer is fine.
                    964:         */
                    965:
                    966:        n->norm = n->parent->norm;
                    967:        assert(n->parent->type == ROFFT_BLOCK);
1.138     schwarze  968: }
                    969:
1.139     schwarze  970: void
                    971: roff_word_alloc(struct roff_man *man, int line, int pos, const char *word)
                    972: {
                    973:        struct roff_node        *n;
                    974:
                    975:        n = roff_node_alloc(man, line, pos, ROFFT_TEXT, TOKEN_NONE);
                    976:        n->string = roff_strdup(man->roff, word);
                    977:        roff_node_append(man, n);
1.158     schwarze  978:        n->flags |= NODE_VALID | NODE_ENDED;
1.139     schwarze  979:        man->next = ROFF_NEXT_SIBLING;
                    980: }
                    981:
                    982: void
                    983: roff_word_append(struct roff_man *man, const char *word)
                    984: {
                    985:        struct roff_node        *n;
                    986:        char                    *addstr, *newstr;
                    987:
                    988:        n = man->last;
                    989:        addstr = roff_strdup(man->roff, word);
                    990:        mandoc_asprintf(&newstr, "%s %s", n->string, addstr);
                    991:        free(addstr);
                    992:        free(n->string);
                    993:        n->string = newstr;
                    994:        man->next = ROFF_NEXT_SIBLING;
1.140     schwarze  995: }
                    996:
                    997: void
                    998: roff_elem_alloc(struct roff_man *man, int line, int pos, int tok)
                    999: {
                   1000:        struct roff_node        *n;
                   1001:
                   1002:        n = roff_node_alloc(man, line, pos, ROFFT_ELEM, tok);
                   1003:        roff_node_append(man, n);
                   1004:        man->next = ROFF_NEXT_CHILD;
                   1005: }
                   1006:
                   1007: struct roff_node *
                   1008: roff_block_alloc(struct roff_man *man, int line, int pos, int tok)
                   1009: {
                   1010:        struct roff_node        *n;
                   1011:
                   1012:        n = roff_node_alloc(man, line, pos, ROFFT_BLOCK, tok);
                   1013:        roff_node_append(man, n);
                   1014:        man->next = ROFF_NEXT_CHILD;
1.149     schwarze 1015:        return n;
1.139     schwarze 1016: }
                   1017:
1.138     schwarze 1018: struct roff_node *
                   1019: roff_head_alloc(struct roff_man *man, int line, int pos, int tok)
                   1020: {
                   1021:        struct roff_node        *n;
                   1022:
                   1023:        n = roff_node_alloc(man, line, pos, ROFFT_HEAD, tok);
                   1024:        roff_node_append(man, n);
                   1025:        man->next = ROFF_NEXT_CHILD;
1.149     schwarze 1026:        return n;
1.138     schwarze 1027: }
                   1028:
                   1029: struct roff_node *
                   1030: roff_body_alloc(struct roff_man *man, int line, int pos, int tok)
                   1031: {
                   1032:        struct roff_node        *n;
                   1033:
                   1034:        n = roff_node_alloc(man, line, pos, ROFFT_BODY, tok);
                   1035:        roff_node_append(man, n);
                   1036:        man->next = ROFF_NEXT_CHILD;
1.149     schwarze 1037:        return n;
1.139     schwarze 1038: }
                   1039:
1.193     schwarze 1040: static void
1.218     schwarze 1041: roff_addtbl(struct roff_man *man, int line, struct tbl_node *tbl)
1.139     schwarze 1042: {
                   1043:        struct roff_node        *n;
1.218     schwarze 1044:        struct tbl_span         *span;
1.139     schwarze 1045:
1.228     schwarze 1046:        if (man->meta.macroset == MACROSET_MAN)
1.174     schwarze 1047:                man_breakscope(man, ROFF_TS);
1.193     schwarze 1048:        while ((span = tbl_span(tbl)) != NULL) {
1.218     schwarze 1049:                n = roff_node_alloc(man, line, 0, ROFFT_TBL, TOKEN_NONE);
1.193     schwarze 1050:                n->span = span;
                   1051:                roff_node_append(man, n);
                   1052:                n->flags |= NODE_VALID | NODE_ENDED;
                   1053:                man->next = ROFF_NEXT_SIBLING;
                   1054:        }
1.138     schwarze 1055: }
                   1056:
                   1057: void
                   1058: roff_node_unlink(struct roff_man *man, struct roff_node *n)
                   1059: {
                   1060:
                   1061:        /* Adjust siblings. */
                   1062:
                   1063:        if (n->prev)
                   1064:                n->prev->next = n->next;
                   1065:        if (n->next)
                   1066:                n->next->prev = n->prev;
                   1067:
                   1068:        /* Adjust parent. */
                   1069:
                   1070:        if (n->parent != NULL) {
                   1071:                if (n->parent->child == n)
                   1072:                        n->parent->child = n->next;
                   1073:                if (n->parent->last == n)
                   1074:                        n->parent->last = n->prev;
                   1075:        }
                   1076:
                   1077:        /* Adjust parse point. */
                   1078:
                   1079:        if (man == NULL)
                   1080:                return;
                   1081:        if (man->last == n) {
                   1082:                if (n->prev == NULL) {
                   1083:                        man->last = n->parent;
                   1084:                        man->next = ROFF_NEXT_CHILD;
                   1085:                } else {
                   1086:                        man->last = n->prev;
                   1087:                        man->next = ROFF_NEXT_SIBLING;
                   1088:                }
                   1089:        }
1.228     schwarze 1090:        if (man->meta.first == n)
                   1091:                man->meta.first = NULL;
1.216     schwarze 1092: }
                   1093:
                   1094: void
                   1095: roff_node_relink(struct roff_man *man, struct roff_node *n)
                   1096: {
                   1097:        roff_node_unlink(man, n);
                   1098:        n->prev = n->next = NULL;
                   1099:        roff_node_append(man, n);
1.138     schwarze 1100: }
                   1101:
                   1102: void
                   1103: roff_node_free(struct roff_node *n)
                   1104: {
                   1105:
                   1106:        if (n->args != NULL)
                   1107:                mdoc_argv_free(n->args);
                   1108:        if (n->type == ROFFT_BLOCK || n->type == ROFFT_ELEM)
                   1109:                free(n->norm);
1.219     schwarze 1110:        eqn_box_free(n->eqn);
1.138     schwarze 1111:        free(n->string);
1.246     schwarze 1112:        free(n->tag);
1.138     schwarze 1113:        free(n);
                   1114: }
                   1115:
                   1116: void
                   1117: roff_node_delete(struct roff_man *man, struct roff_node *n)
                   1118: {
                   1119:
                   1120:        while (n->child != NULL)
                   1121:                roff_node_delete(man, n->child);
                   1122:        roff_node_unlink(man, n);
                   1123:        roff_node_free(n);
1.242     schwarze 1124: }
                   1125:
                   1126: int
                   1127: roff_node_transparent(struct roff_node *n)
                   1128: {
                   1129:        if (n == NULL)
                   1130:                return 0;
                   1131:        if (n->type == ROFFT_COMMENT || n->flags & NODE_NOPRT)
                   1132:                return 1;
1.245     schwarze 1133:        return roff_tok_transparent(n->tok);
                   1134: }
                   1135:
                   1136: int
                   1137: roff_tok_transparent(enum roff_tok tok)
                   1138: {
                   1139:        switch (tok) {
1.242     schwarze 1140:        case ROFF_ft:
                   1141:        case ROFF_ll:
                   1142:        case ROFF_mc:
                   1143:        case ROFF_po:
                   1144:        case ROFF_ta:
                   1145:        case MDOC_Db:
                   1146:        case MDOC_Es:
                   1147:        case MDOC_Sm:
                   1148:        case MDOC_Tg:
                   1149:        case MAN_DT:
                   1150:        case MAN_UC:
                   1151:        case MAN_PD:
                   1152:        case MAN_AT:
                   1153:                return 1;
                   1154:        default:
                   1155:                return 0;
                   1156:        }
                   1157: }
                   1158:
                   1159: struct roff_node *
                   1160: roff_node_child(struct roff_node *n)
                   1161: {
                   1162:        for (n = n->child; roff_node_transparent(n); n = n->next)
                   1163:                continue;
                   1164:        return n;
                   1165: }
                   1166:
                   1167: struct roff_node *
                   1168: roff_node_prev(struct roff_node *n)
                   1169: {
                   1170:        do {
                   1171:                n = n->prev;
                   1172:        } while (roff_node_transparent(n));
                   1173:        return n;
                   1174: }
                   1175:
                   1176: struct roff_node *
                   1177: roff_node_next(struct roff_node *n)
                   1178: {
                   1179:        do {
                   1180:                n = n->next;
                   1181:        } while (roff_node_transparent(n));
                   1182:        return n;
1.141     schwarze 1183: }
                   1184:
                   1185: void
                   1186: deroff(char **dest, const struct roff_node *n)
                   1187: {
                   1188:        char    *cp;
                   1189:        size_t   sz;
                   1190:
1.243     schwarze 1191:        if (n->string == NULL) {
1.141     schwarze 1192:                for (n = n->child; n != NULL; n = n->next)
                   1193:                        deroff(dest, n);
                   1194:                return;
                   1195:        }
                   1196:
1.160     schwarze 1197:        /* Skip leading whitespace. */
1.141     schwarze 1198:
1.160     schwarze 1199:        for (cp = n->string; *cp != '\0'; cp++) {
1.161     schwarze 1200:                if (cp[0] == '\\' && cp[1] != '\0' &&
                   1201:                    strchr(" %&0^|~", cp[1]) != NULL)
1.141     schwarze 1202:                        cp++;
1.160     schwarze 1203:                else if ( ! isspace((unsigned char)*cp))
1.141     schwarze 1204:                        break;
                   1205:        }
                   1206:
1.161     schwarze 1207:        /* Skip trailing backslash. */
                   1208:
                   1209:        sz = strlen(cp);
1.162     schwarze 1210:        if (sz > 0 && cp[sz - 1] == '\\')
1.161     schwarze 1211:                sz--;
                   1212:
1.141     schwarze 1213:        /* Skip trailing whitespace. */
                   1214:
1.161     schwarze 1215:        for (; sz; sz--)
1.141     schwarze 1216:                if ( ! isspace((unsigned char)cp[sz-1]))
                   1217:                        break;
                   1218:
                   1219:        /* Skip empty strings. */
                   1220:
                   1221:        if (sz == 0)
                   1222:                return;
                   1223:
                   1224:        if (*dest == NULL) {
                   1225:                *dest = mandoc_strndup(cp, sz);
                   1226:                return;
                   1227:        }
                   1228:
                   1229:        mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
                   1230:        free(*dest);
                   1231:        *dest = cp;
1.138     schwarze 1232: }
                   1233:
                   1234: /* --- main functions of the roff parser ---------------------------------- */
                   1235:
1.260     schwarze 1236: /*
                   1237:  * Save comments preceding the title macro, for example in order to
                   1238:  * preserve Copyright and license headers in HTML output,
                   1239:  * provide diagnostics about RCS ids and trailing whitespace in comments,
                   1240:  * then discard comments including preceding whitespace.
                   1241:  * This function also handles input line continuation.
                   1242:  */
1.259     schwarze 1243: static int
1.260     schwarze 1244: roff_parse_comment(struct roff *r, struct buf *buf, int ln, int pos, char ec)
1.259     schwarze 1245: {
                   1246:        struct roff_node *n;    /* used for header comments */
                   1247:        const char      *start; /* start of the string to process */
                   1248:        const char      *cp;    /* for RCS id parsing */
                   1249:        char            *stesc; /* start of an escape sequence ('\\') */
                   1250:        char            *ep;    /* end of comment string */
                   1251:        int              rcsid; /* kind of RCS id seen */
                   1252:
                   1253:        for (start = stesc = buf->buf + pos;; stesc++) {
1.260     schwarze 1254:                /*
                   1255:                 * XXX Ugly hack: Remove the newline character that
                   1256:                 * mparse_buf_r() appended to mark the end of input
                   1257:                 * if it is not preceded by an escape character.
                   1258:                 */
                   1259:                if (stesc[0] == '\n') {
                   1260:                        assert(stesc[1] == '\0');
                   1261:                        stesc[0] = '\0';
                   1262:                }
                   1263:
1.259     schwarze 1264:                /* The line ends without continuation or comment. */
                   1265:                if (stesc[0] == '\0')
                   1266:                        return ROFF_CONT;
                   1267:
                   1268:                /* Unescaped byte: skip it. */
1.260     schwarze 1269:                if (stesc[0] != ec)
1.259     schwarze 1270:                        continue;
                   1271:
1.260     schwarze 1272:                /*
                   1273:                 * XXX Ugly hack: Do not attempt to append another line
                   1274:                 * if the function mparse_buf_r() appended a newline
                   1275:                 * character to indicate the end of input.
                   1276:                 */
                   1277:                if (stesc[1] == '\n') {
                   1278:                        assert(stesc[2] == '\0');
                   1279:                        stesc[0] = '\0';
                   1280:                        return ROFF_CONT;
                   1281:                }
                   1282:
                   1283:                /*
                   1284:                 * An escape character at the end of an input line
                   1285:                 * requests line continuation.
                   1286:                 */
1.259     schwarze 1287:                if (stesc[1] == '\0') {
                   1288:                        stesc[0] = '\0';
                   1289:                        return ROFF_IGN | ROFF_APPEND;
                   1290:                }
                   1291:
                   1292:                /* Found a comment: process it. */
                   1293:                if (stesc[1] == '"' || stesc[1] == '#')
                   1294:                        break;
                   1295:
                   1296:                /* Escaped escape character: skip them both. */
1.260     schwarze 1297:                if (stesc[1] == ec)
1.259     schwarze 1298:                        stesc++;
                   1299:        }
                   1300:
                   1301:        /* Look for an RCS id in the comment. */
                   1302:
                   1303:        rcsid = 0;
                   1304:        if ((cp = strstr(stesc + 2, "$" "OpenBSD")) != NULL) {
                   1305:                rcsid = 1 << MANDOC_OS_OPENBSD;
                   1306:                cp += 8;
                   1307:        } else if ((cp = strstr(stesc + 2, "$" "NetBSD")) != NULL) {
                   1308:                rcsid = 1 << MANDOC_OS_NETBSD;
                   1309:                cp += 7;
                   1310:        }
                   1311:        if (cp != NULL && isalnum((unsigned char)*cp) == 0 &&
                   1312:            strchr(cp, '$') != NULL) {
                   1313:                if (r->man->meta.rcsids & rcsid)
                   1314:                        mandoc_msg(MANDOCERR_RCS_REP, ln,
                   1315:                            (int)(stesc - buf->buf) + 2, "%s", stesc + 1);
                   1316:                r->man->meta.rcsids |= rcsid;
                   1317:        }
                   1318:
                   1319:        /* Warn about trailing whitespace at the end of the comment. */
                   1320:
                   1321:        ep = strchr(stesc + 2, '\0') - 1;
                   1322:        if (*ep == '\n')
                   1323:                *ep-- = '\0';
                   1324:        if (*ep == ' ' || *ep == '\t')
                   1325:                mandoc_msg(MANDOCERR_SPACE_EOL,
                   1326:                    ln, (int)(ep - buf->buf), NULL);
                   1327:
                   1328:        /* Save comments preceding the title macro in the syntax tree. */
                   1329:
                   1330:        if (r->options & MPARSE_COMMENT) {
                   1331:                while (*ep == ' ' || *ep == '\t')
                   1332:                        ep--;
                   1333:                ep[1] = '\0';
                   1334:                n = roff_node_alloc(r->man, ln, stesc + 1 - buf->buf,
                   1335:                    ROFFT_COMMENT, TOKEN_NONE);
                   1336:                n->string = mandoc_strdup(stesc + 2);
                   1337:                roff_node_append(r->man, n);
                   1338:                n->flags |= NODE_VALID | NODE_ENDED;
                   1339:                r->man->next = ROFF_NEXT_SIBLING;
                   1340:        }
                   1341:
                   1342:        /* The comment requests line continuation. */
                   1343:
                   1344:        if (stesc[1] == '#') {
                   1345:                *stesc = '\0';
                   1346:                return ROFF_IGN | ROFF_APPEND;
                   1347:        }
                   1348:
                   1349:        /* Discard the comment including preceding whitespace. */
                   1350:
                   1351:        while (stesc > start && stesc[-1] == ' ' &&
                   1352:            (stesc == start + 1 || stesc[-2] != '\\'))
                   1353:                stesc--;
                   1354:        *stesc = '\0';
                   1355:        return ROFF_CONT;
                   1356: }
                   1357:
1.8       schwarze 1358: /*
1.227     schwarze 1359:  * In the current line, expand escape sequences that produce parsable
                   1360:  * input text.  Also check the syntax of the remaining escape sequences,
                   1361:  * which typically produce output glyphs or change formatter state.
1.8       schwarze 1362:  */
1.212     schwarze 1363: static int
1.260     schwarze 1364: roff_expand(struct roff *r, struct buf *buf, int ln, int pos, char ec)
1.8       schwarze 1365: {
1.260     schwarze 1366:        char             ubuf[24];      /* buffer to print a number */
                   1367:        struct mctx     *ctx;           /* current macro call context */
                   1368:        const char      *res;           /* the string to be pasted */
                   1369:        const char      *src;           /* source for copying */
                   1370:        char            *dst;           /* destination for copying */
                   1371:        int              iesc;          /* index of leading escape char */
                   1372:        int              inam;          /* index of the escape name */
                   1373:        int              iarg;          /* index beginning the argument */
                   1374:        int              iendarg;       /* index right after the argument */
                   1375:        int              iend;          /* index right after the sequence */
                   1376:        int              deftype;       /* type of definition to paste */
                   1377:        int              argi;          /* macro argument index */
                   1378:        int              quote_args;    /* true for \\$@, false for \\$* */
                   1379:        int              asz;           /* length of the replacement */
                   1380:        int              rsz;           /* length of the rest of the string */
                   1381:        int              npos;          /* position in numeric expression */
1.53      schwarze 1382:        int              expand_count;  /* to avoid infinite loops */
1.175     schwarze 1383:
1.43      schwarze 1384:        expand_count = 0;
1.260     schwarze 1385:        while (buf->buf[pos] != '\0') {
1.43      schwarze 1386:
1.260     schwarze 1387:                /*
                   1388:                 * Skip plain ASCII characters.
                   1389:                 * If we have a non-standard escape character,
                   1390:                 * escape literal backslashes because all processing in
                   1391:                 * subsequent functions uses the standard escaping rules.
                   1392:                 */
1.78      schwarze 1393:
1.260     schwarze 1394:                if (buf->buf[pos] != ec) {
                   1395:                        if (ec != ASCII_ESC && buf->buf[pos] == '\\') {
                   1396:                                roff_expand_patch(buf, pos, "\\e", pos + 1);
                   1397:                                pos++;
1.175     schwarze 1398:                        }
1.260     schwarze 1399:                        pos++;
1.78      schwarze 1400:                        continue;
1.175     schwarze 1401:                }
1.78      schwarze 1402:
1.260     schwarze 1403:                /*
                   1404:                 * Parse escape sequences,
                   1405:                 * issue diagnostic messages when appropriate,
                   1406:                 * and skip sequences that do not need expansion.
                   1407:                 * If we have a non-standard escape character, translate
                   1408:                 * it to backslashes and translate backslashes to \e.
                   1409:                 */
1.78      schwarze 1410:
1.264   ! schwarze 1411:                if (roff_escape(buf->buf, ln, pos, &iesc, &inam,
        !          1412:                    &iarg, &iendarg, &iend) != ESCAPE_EXPAND) {
1.260     schwarze 1413:                        while (pos < iend) {
                   1414:                                if (buf->buf[pos] == ec) {
                   1415:                                        buf->buf[pos] = '\\';
                   1416:                                        if (pos + 1 < iend)
                   1417:                                                pos++;
                   1418:                                } else if (buf->buf[pos] == '\\') {
                   1419:                                        roff_expand_patch(buf,
                   1420:                                            pos, "\\e", pos + 1);
                   1421:                                        pos++;
                   1422:                                        iend++;
                   1423:                                }
                   1424:                                pos++;
1.224     schwarze 1425:                        }
1.78      schwarze 1426:                        continue;
1.42      schwarze 1427:                }
1.260     schwarze 1428:
                   1429:                /* Handle expansion. */
                   1430:
                   1431:                res = NULL;
                   1432:                switch (buf->buf[inam]) {
                   1433:                case '*':
                   1434:                        if (iendarg == iarg)
1.79      schwarze 1435:                                break;
1.260     schwarze 1436:                        deftype = ROFFDEF_USER | ROFFDEF_PRE;
                   1437:                        if ((res = roff_getstrn(r, buf->buf + iarg,
                   1438:                            iendarg - iarg, &deftype)) != NULL)
1.79      schwarze 1439:                                break;
1.8       schwarze 1440:
1.260     schwarze 1441:                        /*
                   1442:                         * If not overriden,
                   1443:                         * let \*(.T through to the formatters.
                   1444:                         */
1.8       schwarze 1445:
1.260     schwarze 1446:                        if (iendarg - iarg == 2 &&
                   1447:                            buf->buf[iarg] == '.' &&
                   1448:                            buf->buf[iarg + 1] == 'T') {
                   1449:                                roff_setstrn(&r->strtab, ".T", 2, NULL, 0, 0);
                   1450:                                pos = iend;
1.125     schwarze 1451:                                continue;
                   1452:                        }
1.8       schwarze 1453:
1.260     schwarze 1454:                        mandoc_msg(MANDOCERR_STR_UNDEF, ln, iesc,
                   1455:                            "%.*s", iendarg - iarg, buf->buf + iarg);
                   1456:                        break;
1.23      schwarze 1457:
1.211     schwarze 1458:                case '$':
                   1459:                        if (r->mstackpos < 0) {
1.260     schwarze 1460:                                mandoc_msg(MANDOCERR_ARG_UNDEF, ln, iesc,
                   1461:                                    "%.*s", iend - iesc, buf->buf + iesc);
1.211     schwarze 1462:                                break;
                   1463:                        }
                   1464:                        ctx = r->mstack + r->mstackpos;
1.260     schwarze 1465:                        argi = buf->buf[iarg] - '1';
                   1466:                        if (argi >= 0 && argi <= 8) {
                   1467:                                if (argi < ctx->argc)
                   1468:                                        res = ctx->argv[argi];
1.211     schwarze 1469:                                break;
                   1470:                        }
1.260     schwarze 1471:                        if (buf->buf[iarg] == '*')
1.211     schwarze 1472:                                quote_args = 0;
1.260     schwarze 1473:                        else if (buf->buf[iarg] == '@')
1.211     schwarze 1474:                                quote_args = 1;
                   1475:                        else {
1.260     schwarze 1476:                                mandoc_msg(MANDOCERR_ARG_NONUM, ln, iesc,
                   1477:                                    "%.*s", iend - iesc, buf->buf + iesc);
1.211     schwarze 1478:                                break;
                   1479:                        }
                   1480:                        asz = 0;
1.260     schwarze 1481:                        for (argi = 0; argi < ctx->argc; argi++) {
                   1482:                                if (argi)
1.211     schwarze 1483:                                        asz++;  /* blank */
                   1484:                                if (quote_args)
                   1485:                                        asz += 2;  /* quotes */
1.260     schwarze 1486:                                asz += strlen(ctx->argv[argi]);
1.211     schwarze 1487:                        }
1.260     schwarze 1488:                        if (asz != iend - iesc) {
                   1489:                                rsz = buf->sz - iend;
                   1490:                                if (asz < iend - iesc)
                   1491:                                        memmove(buf->buf + iesc + asz,
                   1492:                                            buf->buf + iend, rsz);
                   1493:                                buf->sz = iesc + asz + rsz;
                   1494:                                buf->buf = mandoc_realloc(buf->buf, buf->sz);
                   1495:                                if (asz > iend - iesc)
                   1496:                                        memmove(buf->buf + iesc + asz,
                   1497:                                            buf->buf + iend, rsz);
1.211     schwarze 1498:                        }
1.260     schwarze 1499:                        dst = buf->buf + iesc;
                   1500:                        for (argi = 0; argi < ctx->argc; argi++) {
                   1501:                                if (argi)
                   1502:                                        *dst++ = ' ';
1.211     schwarze 1503:                                if (quote_args)
1.260     schwarze 1504:                                        *dst++ = '"';
                   1505:                                src = ctx->argv[argi];
                   1506:                                while (*src != '\0')
                   1507:                                        *dst++ = *src++;
1.211     schwarze 1508:                                if (quote_args)
1.260     schwarze 1509:                                        *dst++ = '"';
1.211     schwarze 1510:                        }
                   1511:                        continue;
1.263     schwarze 1512:                case 'A':
                   1513:                        ubuf[0] = iendarg > iarg ? '1' : '0';
                   1514:                        ubuf[1] = '\0';
                   1515:                        res = ubuf;
                   1516:                        break;
1.80      schwarze 1517:                case 'B':
1.79      schwarze 1518:                        npos = 0;
1.260     schwarze 1519:                        ubuf[0] = iendarg > iarg && iend > iendarg &&
                   1520:                            roff_evalnum(r, ln, buf->buf + iarg, &npos,
                   1521:                                         NULL, ROFFNUM_SCALE) &&
                   1522:                            npos == iendarg - iarg ? '1' : '0';
1.79      schwarze 1523:                        ubuf[1] = '\0';
1.260     schwarze 1524:                        res = ubuf;
1.79      schwarze 1525:                        break;
1.261     schwarze 1526:                case 'V':
                   1527:                        mandoc_msg(MANDOCERR_UNSUPP, ln, iesc,
                   1528:                            "%.*s", iend - iesc, buf->buf + iesc);
                   1529:                        roff_expand_patch(buf, iendarg, "}", iend);
                   1530:                        roff_expand_patch(buf, iesc, "${", iarg);
                   1531:                        continue;
1.262     schwarze 1532:                case 'g':
                   1533:                        break;
1.80      schwarze 1534:                case 'n':
1.260     schwarze 1535:                        if (iendarg > iarg)
1.91      schwarze 1536:                                (void)snprintf(ubuf, sizeof(ubuf), "%d",
1.260     schwarze 1537:                                    roff_getregn(r, buf->buf + iarg,
                   1538:                                    iendarg - iarg, buf->buf[inam + 1]));
1.91      schwarze 1539:                        else
                   1540:                                ubuf[0] = '\0';
1.260     schwarze 1541:                        res = ubuf;
1.79      schwarze 1542:                        break;
1.80      schwarze 1543:                case 'w':
1.260     schwarze 1544:                        (void)snprintf(ubuf, sizeof(ubuf),
                   1545:                            "%d", (iendarg - iarg) * 24);
                   1546:                        res = ubuf;
                   1547:                        break;
                   1548:                default:
1.79      schwarze 1549:                        break;
                   1550:                }
1.260     schwarze 1551:                if (res == NULL)
1.37      schwarze 1552:                        res = "";
1.260     schwarze 1553:                if (++expand_count > EXPAND_LIMIT ||
                   1554:                    buf->sz + strlen(res) > SHRT_MAX) {
                   1555:                        mandoc_msg(MANDOCERR_ROFFLOOP, ln, iesc, NULL);
1.149     schwarze 1556:                        return ROFF_IGN;
1.8       schwarze 1557:                }
1.260     schwarze 1558:                roff_expand_patch(buf, iesc, res, iend);
                   1559:        }
                   1560:        return ROFF_CONT;
                   1561: }
1.8       schwarze 1562:
1.260     schwarze 1563: /*
                   1564:  * Replace the substring from the start position (inclusive)
                   1565:  * to end position (exclusive) with the repl(acement) string.
                   1566:  */
                   1567: static void
                   1568: roff_expand_patch(struct buf *buf, int start, const char *repl, int end)
                   1569: {
                   1570:        char    *nbuf;
1.23      schwarze 1571:
1.261     schwarze 1572:        buf->sz = mandoc_asprintf(&nbuf, "%.*s%s%s", start, buf->buf,
                   1573:            repl, buf->buf + end) + 1;
1.260     schwarze 1574:        free(buf->buf);
                   1575:        buf->buf = nbuf;
1.42      schwarze 1576: }
1.225     schwarze 1577:
                   1578: /*
                   1579:  * Parse a quoted or unquoted roff-style request or macro argument.
                   1580:  * Return a pointer to the parsed argument, which is either the original
                   1581:  * pointer or advanced by one byte in case the argument is quoted.
                   1582:  * NUL-terminate the argument in place.
                   1583:  * Collapse pairs of quotes inside quoted arguments.
                   1584:  * Advance the argument pointer to the next argument,
                   1585:  * or to the NUL byte terminating the argument line.
                   1586:  */
                   1587: char *
1.227     schwarze 1588: roff_getarg(struct roff *r, char **cpp, int ln, int *pos)
1.225     schwarze 1589: {
1.227     schwarze 1590:        struct buf       buf;
1.238     schwarze 1591:        char            *cp, *start;
1.227     schwarze 1592:        int              newesc, pairs, quoted, white;
1.225     schwarze 1593:
                   1594:        /* Quoting can only start with a new word. */
                   1595:        start = *cpp;
                   1596:        quoted = 0;
                   1597:        if ('"' == *start) {
                   1598:                quoted = 1;
                   1599:                start++;
                   1600:        }
                   1601:
1.227     schwarze 1602:        newesc = pairs = white = 0;
1.225     schwarze 1603:        for (cp = start; '\0' != *cp; cp++) {
                   1604:
                   1605:                /*
                   1606:                 * Move the following text left
                   1607:                 * after quoted quotes and after "\\" and "\t".
                   1608:                 */
                   1609:                if (pairs)
                   1610:                        cp[-pairs] = cp[0];
                   1611:
                   1612:                if ('\\' == cp[0]) {
                   1613:                        /*
                   1614:                         * In copy mode, translate double to single
                   1615:                         * backslashes and backslash-t to literal tabs.
                   1616:                         */
                   1617:                        switch (cp[1]) {
                   1618:                        case 'a':
                   1619:                        case 't':
1.226     schwarze 1620:                                cp[-pairs] = '\t';
1.227     schwarze 1621:                                pairs++;
                   1622:                                cp++;
                   1623:                                break;
1.225     schwarze 1624:                        case '\\':
1.227     schwarze 1625:                                newesc = 1;
                   1626:                                cp[-pairs] = ASCII_ESC;
1.225     schwarze 1627:                                pairs++;
                   1628:                                cp++;
                   1629:                                break;
                   1630:                        case ' ':
                   1631:                                /* Skip escaped blanks. */
                   1632:                                if (0 == quoted)
                   1633:                                        cp++;
                   1634:                                break;
                   1635:                        default:
                   1636:                                break;
                   1637:                        }
                   1638:                } else if (0 == quoted) {
                   1639:                        if (' ' == cp[0]) {
                   1640:                                /* Unescaped blanks end unquoted args. */
                   1641:                                white = 1;
                   1642:                                break;
                   1643:                        }
                   1644:                } else if ('"' == cp[0]) {
                   1645:                        if ('"' == cp[1]) {
                   1646:                                /* Quoted quotes collapse. */
                   1647:                                pairs++;
                   1648:                                cp++;
                   1649:                        } else {
                   1650:                                /* Unquoted quotes end quoted args. */
                   1651:                                quoted = 2;
                   1652:                                break;
                   1653:                        }
                   1654:                }
                   1655:        }
                   1656:
                   1657:        /* Quoted argument without a closing quote. */
                   1658:        if (1 == quoted)
                   1659:                mandoc_msg(MANDOCERR_ARG_QUOTE, ln, *pos, NULL);
                   1660:
                   1661:        /* NUL-terminate this argument and move to the next one. */
                   1662:        if (pairs)
                   1663:                cp[-pairs] = '\0';
                   1664:        if ('\0' != *cp) {
                   1665:                *cp++ = '\0';
                   1666:                while (' ' == *cp)
                   1667:                        cp++;
                   1668:        }
                   1669:        *pos += (int)(cp - start) + (quoted ? 1 : 0);
                   1670:        *cpp = cp;
                   1671:
                   1672:        if ('\0' == *cp && (white || ' ' == cp[-1]))
                   1673:                mandoc_msg(MANDOCERR_SPACE_EOL, ln, *pos, NULL);
                   1674:
1.227     schwarze 1675:        start = mandoc_strdup(start);
                   1676:        if (newesc == 0)
                   1677:                return start;
                   1678:
                   1679:        buf.buf = start;
                   1680:        buf.sz = strlen(start) + 1;
                   1681:        buf.next = NULL;
                   1682:        if (roff_expand(r, &buf, ln, 0, ASCII_ESC) & ROFF_IGN) {
                   1683:                free(buf.buf);
                   1684:                buf.buf = mandoc_strdup("");
                   1685:        }
                   1686:        return buf.buf;
1.225     schwarze 1687: }
                   1688:
1.42      schwarze 1689:
                   1690: /*
1.147     schwarze 1691:  * Process text streams.
1.42      schwarze 1692:  */
1.212     schwarze 1693: static int
1.177     schwarze 1694: roff_parsetext(struct roff *r, struct buf *buf, int pos, int *offs)
1.42      schwarze 1695: {
                   1696:        size_t           sz;
                   1697:        const char      *start;
1.51      schwarze 1698:        char            *p;
                   1699:        int              isz;
1.42      schwarze 1700:        enum mandoc_esc  esc;
                   1701:
1.147     schwarze 1702:        /* Spring the input line trap. */
                   1703:
                   1704:        if (roffit_lines == 1) {
                   1705:                isz = mandoc_asprintf(&p, "%s\n.%s", buf->buf, roffit_macro);
                   1706:                free(buf->buf);
                   1707:                buf->buf = p;
                   1708:                buf->sz = isz + 1;
                   1709:                *offs = 0;
                   1710:                free(roffit_macro);
                   1711:                roffit_lines = 0;
1.149     schwarze 1712:                return ROFF_REPARSE;
1.147     schwarze 1713:        } else if (roffit_lines > 1)
                   1714:                --roffit_lines;
                   1715:
1.177     schwarze 1716:        if (roffce_node != NULL && buf->buf[pos] != '\0') {
                   1717:                if (roffce_lines < 1) {
                   1718:                        r->man->last = roffce_node;
                   1719:                        r->man->next = ROFF_NEXT_SIBLING;
                   1720:                        roffce_lines = 0;
                   1721:                        roffce_node = NULL;
                   1722:                } else
                   1723:                        roffce_lines--;
                   1724:        }
                   1725:
1.147     schwarze 1726:        /* Convert all breakable hyphens into ASCII_HYPH. */
                   1727:
1.110     schwarze 1728:        start = p = buf->buf + pos;
1.42      schwarze 1729:
1.110     schwarze 1730:        while (*p != '\0') {
1.42      schwarze 1731:                sz = strcspn(p, "-\\");
                   1732:                p += sz;
                   1733:
1.110     schwarze 1734:                if (*p == '\0')
1.42      schwarze 1735:                        break;
                   1736:
1.110     schwarze 1737:                if (*p == '\\') {
1.42      schwarze 1738:                        /* Skip over escapes. */
                   1739:                        p++;
1.62      schwarze 1740:                        esc = mandoc_escape((const char **)&p, NULL, NULL);
1.110     schwarze 1741:                        if (esc == ESCAPE_ERROR)
1.42      schwarze 1742:                                break;
1.136     schwarze 1743:                        while (*p == '-')
                   1744:                                p++;
1.42      schwarze 1745:                        continue;
                   1746:                } else if (p == start) {
                   1747:                        p++;
                   1748:                        continue;
                   1749:                }
                   1750:
1.44      schwarze 1751:                if (isalpha((unsigned char)p[-1]) &&
                   1752:                    isalpha((unsigned char)p[1]))
1.42      schwarze 1753:                        *p = ASCII_HYPH;
                   1754:                p++;
1.8       schwarze 1755:        }
1.149     schwarze 1756:        return ROFF_CONT;
1.8       schwarze 1757: }
                   1758:
1.212     schwarze 1759: int
1.249     schwarze 1760: roff_parseln(struct roff *r, int ln, struct buf *buf, int *offs, size_t len)
1.1       schwarze 1761: {
1.166     schwarze 1762:        enum roff_tok    t;
1.212     schwarze 1763:        int              e;
1.110     schwarze 1764:        int              pos;   /* parse point */
1.115     schwarze 1765:        int              spos;  /* saved parse point for messages */
1.110     schwarze 1766:        int              ppos;  /* original offset in buf->buf */
                   1767:        int              ctl;   /* macro line (boolean) */
                   1768:
                   1769:        ppos = pos = *offs;
1.249     schwarze 1770:
                   1771:        if (len > 80 && r->tbl == NULL && r->eqn == NULL &&
                   1772:            (r->man->flags & ROFF_NOFILL) == 0 &&
                   1773:            strchr(" .\\", buf->buf[pos]) == NULL &&
                   1774:            buf->buf[pos] != r->control &&
                   1775:            strcspn(buf->buf, " ") < 80)
                   1776:                mandoc_msg(MANDOCERR_TEXT_LONG, ln, (int)len - 1,
                   1777:                    "%.20s...", buf->buf + pos);
1.1       schwarze 1778:
1.102     schwarze 1779:        /* Handle in-line equation delimiters. */
                   1780:
1.108     schwarze 1781:        if (r->tbl == NULL &&
                   1782:            r->last_eqn != NULL && r->last_eqn->delim &&
1.102     schwarze 1783:            (r->eqn == NULL || r->eqn_inline)) {
1.110     schwarze 1784:                e = roff_eqndelim(r, buf, pos);
1.102     schwarze 1785:                if (e == ROFF_REPARSE)
1.149     schwarze 1786:                        return e;
1.102     schwarze 1787:                assert(e == ROFF_CONT);
                   1788:        }
                   1789:
1.259     schwarze 1790:        /* Handle comments and escape sequences. */
                   1791:
                   1792:        e = roff_parse_comment(r, buf, ln, pos, r->escape);
                   1793:        if ((e & ROFF_MASK) == ROFF_IGN)
                   1794:                return e;
                   1795:        assert(e == ROFF_CONT);
1.8       schwarze 1796:
1.227     schwarze 1797:        e = roff_expand(r, buf, ln, pos, r->escape);
1.212     schwarze 1798:        if ((e & ROFF_MASK) == ROFF_IGN)
1.149     schwarze 1799:                return e;
1.110     schwarze 1800:        assert(e == ROFF_CONT);
1.8       schwarze 1801:
1.110     schwarze 1802:        ctl = roff_getcontrol(r, buf->buf, &pos);
1.35      schwarze 1803:
1.8       schwarze 1804:        /*
1.2       schwarze 1805:         * First, if a scope is open and we're not a macro, pass the
1.124     schwarze 1806:         * text through the macro's filter.
                   1807:         * Equations process all content themselves.
                   1808:         * Tables process almost all content themselves, but we want
                   1809:         * to warn about macros before passing it there.
1.2       schwarze 1810:         */
                   1811:
1.124     schwarze 1812:        if (r->last != NULL && ! ctl) {
1.2       schwarze 1813:                t = r->last->tok;
1.110     schwarze 1814:                e = (*roffs[t].text)(r, t, buf, ln, pos, pos, offs);
1.212     schwarze 1815:                if ((e & ROFF_MASK) == ROFF_IGN)
1.149     schwarze 1816:                        return e;
1.212     schwarze 1817:                e &= ~ROFF_MASK;
                   1818:        } else
                   1819:                e = ROFF_IGN;
1.191     schwarze 1820:        if (r->eqn != NULL && strncmp(buf->buf + ppos, ".EN", 3)) {
                   1821:                eqn_read(r->eqn, buf->buf + ppos);
1.212     schwarze 1822:                return e;
1.191     schwarze 1823:        }
1.193     schwarze 1824:        if (r->tbl != NULL && (ctl == 0 || buf->buf[pos] == '\0')) {
                   1825:                tbl_read(r->tbl, ln, buf->buf, ppos);
1.218     schwarze 1826:                roff_addtbl(r->man, ln, r->tbl);
1.212     schwarze 1827:                return e;
1.193     schwarze 1828:        }
1.239     schwarze 1829:        if ( ! ctl) {
                   1830:                r->options &= ~MPARSE_COMMENT;
1.212     schwarze 1831:                return roff_parsetext(r, buf, pos, offs) | e;
1.239     schwarze 1832:        }
1.100     schwarze 1833:
                   1834:        /* Skip empty request lines. */
                   1835:
1.110     schwarze 1836:        if (buf->buf[pos] == '"') {
1.222     schwarze 1837:                mandoc_msg(MANDOCERR_COMMENT_BAD, ln, pos, NULL);
1.149     schwarze 1838:                return ROFF_IGN;
1.110     schwarze 1839:        } else if (buf->buf[pos] == '\0')
1.149     schwarze 1840:                return ROFF_IGN;
1.2       schwarze 1841:
                   1842:        /*
                   1843:         * If a scope is open, go to the child handler for that macro,
                   1844:         * as it may want to preprocess before doing anything with it.
                   1845:         */
                   1846:
                   1847:        if (r->last) {
1.1       schwarze 1848:                t = r->last->tok;
1.149     schwarze 1849:                return (*roffs[t].sub)(r, t, buf, ln, ppos, pos, offs);
1.2       schwarze 1850:        }
                   1851:
1.239     schwarze 1852:        r->options &= ~MPARSE_COMMENT;
1.115     schwarze 1853:        spos = pos;
                   1854:        t = roff_parse(r, buf->buf, &pos, ln, ppos);
1.258     schwarze 1855:        return roff_req_or_macro(r, t, buf, ln, spos, pos, offs);
                   1856: }
                   1857:
                   1858: /*
                   1859:  * Handle a new request or macro.
                   1860:  * May be called outside any scope or from inside a conditional scope.
                   1861:  */
                   1862: static int
                   1863: roff_req_or_macro(ROFF_ARGS) {
1.115     schwarze 1864:
1.258     schwarze 1865:        /* For now, tables ignore most macros and some request. */
1.115     schwarze 1866:
1.258     schwarze 1867:        if (r->tbl != NULL && (tok == TOKEN_NONE || tok == ROFF_TS ||
                   1868:            tok == ROFF_br || tok == ROFF_ce || tok == ROFF_rj ||
                   1869:            tok == ROFF_sp)) {
1.222     schwarze 1870:                mandoc_msg(MANDOCERR_TBLMACRO,
1.258     schwarze 1871:                    ln, ppos, "%s", buf->buf + ppos);
                   1872:                if (tok != TOKEN_NONE)
1.149     schwarze 1873:                        return ROFF_IGN;
1.129     schwarze 1874:                while (buf->buf[pos] != '\0' && buf->buf[pos] != ' ')
                   1875:                        pos++;
1.163     schwarze 1876:                while (buf->buf[pos] == ' ')
1.129     schwarze 1877:                        pos++;
1.193     schwarze 1878:                tbl_read(r->tbl, ln, buf->buf, pos);
1.218     schwarze 1879:                roff_addtbl(r->man, ln, r->tbl);
1.193     schwarze 1880:                return ROFF_IGN;
1.115     schwarze 1881:        }
                   1882:
1.177     schwarze 1883:        /* For now, let high level macros abort .ce mode. */
                   1884:
1.258     schwarze 1885:        if (roffce_node != NULL &&
                   1886:            (tok == TOKEN_NONE || tok == ROFF_Dd || tok == ROFF_EQ ||
                   1887:             tok == ROFF_TH || tok == ROFF_TS)) {
1.177     schwarze 1888:                r->man->last = roffce_node;
                   1889:                r->man->next = ROFF_NEXT_SIBLING;
                   1890:                roffce_lines = 0;
                   1891:                roffce_node = NULL;
                   1892:        }
                   1893:
1.2       schwarze 1894:        /*
1.115     schwarze 1895:         * This is neither a roff request nor a user-defined macro.
                   1896:         * Let the standard macro set parsers handle it.
1.2       schwarze 1897:         */
                   1898:
1.258     schwarze 1899:        if (tok == TOKEN_NONE)
1.149     schwarze 1900:                return ROFF_CONT;
1.115     schwarze 1901:
1.258     schwarze 1902:        /* Execute a roff request or a user-defined macro. */
1.1       schwarze 1903:
1.258     schwarze 1904:        return (*roffs[tok].proc)(r, tok, buf, ln, ppos, pos, offs);
1.2       schwarze 1905: }
                   1906:
1.211     schwarze 1907: /*
                   1908:  * Internal interface function to tell the roff parser that execution
                   1909:  * of the current macro ended.  This is required because macro
                   1910:  * definitions usually do not end with a .return request.
                   1911:  */
                   1912: void
                   1913: roff_userret(struct roff *r)
                   1914: {
                   1915:        struct mctx     *ctx;
                   1916:        int              i;
                   1917:
                   1918:        assert(r->mstackpos >= 0);
                   1919:        ctx = r->mstack + r->mstackpos;
                   1920:        for (i = 0; i < ctx->argc; i++)
                   1921:                free(ctx->argv[i]);
                   1922:        ctx->argc = 0;
                   1923:        r->mstackpos--;
                   1924: }
                   1925:
1.27      schwarze 1926: void
1.2       schwarze 1927: roff_endparse(struct roff *r)
                   1928: {
1.193     schwarze 1929:        if (r->last != NULL)
1.222     schwarze 1930:                mandoc_msg(MANDOCERR_BLK_NOEND, r->last->line,
                   1931:                    r->last->col, "%s", roff_name[r->last->tok]);
1.27      schwarze 1932:
1.193     schwarze 1933:        if (r->eqn != NULL) {
1.222     schwarze 1934:                mandoc_msg(MANDOCERR_BLK_NOEND,
1.191     schwarze 1935:                    r->eqn->node->line, r->eqn->node->pos, "EQ");
                   1936:                eqn_parse(r->eqn);
                   1937:                r->eqn = NULL;
1.32      schwarze 1938:        }
                   1939:
1.193     schwarze 1940:        if (r->tbl != NULL) {
1.218     schwarze 1941:                tbl_end(r->tbl, 1);
1.193     schwarze 1942:                r->tbl = NULL;
1.27      schwarze 1943:        }
1.1       schwarze 1944: }
                   1945:
                   1946: /*
1.258     schwarze 1947:  * Parse the request or macro name at buf[*pos].
                   1948:  * Return ROFF_RENAMED, ROFF_USERDEF, or a ROFF_* token value.
                   1949:  * For empty, undefined, mdoc(7), and man(7) macros, return TOKEN_NONE.
                   1950:  * As a side effect, set r->current_string to the definition or to NULL.
1.1       schwarze 1951:  */
1.166     schwarze 1952: static enum roff_tok
1.87      schwarze 1953: roff_parse(struct roff *r, char *buf, int *pos, int ln, int ppos)
1.1       schwarze 1954: {
1.87      schwarze 1955:        char            *cp;
1.16      schwarze 1956:        const char      *mac;
                   1957:        size_t           maclen;
1.187     schwarze 1958:        int              deftype;
1.166     schwarze 1959:        enum roff_tok    t;
1.1       schwarze 1960:
1.87      schwarze 1961:        cp = buf + *pos;
                   1962:
                   1963:        if ('\0' == *cp || '"' == *cp || '\t' == *cp || ' ' == *cp)
1.166     schwarze 1964:                return TOKEN_NONE;
1.1       schwarze 1965:
1.87      schwarze 1966:        mac = cp;
                   1967:        maclen = roff_getname(r, &cp, ln, ppos);
1.1       schwarze 1968:
1.187     schwarze 1969:        deftype = ROFFDEF_USER | ROFFDEF_REN;
                   1970:        r->current_string = roff_getstrn(r, mac, maclen, &deftype);
                   1971:        switch (deftype) {
                   1972:        case ROFFDEF_USER:
                   1973:                t = ROFF_USERDEF;
                   1974:                break;
                   1975:        case ROFFDEF_REN:
                   1976:                t = ROFF_RENAMED;
                   1977:                break;
                   1978:        default:
                   1979:                t = roffhash_find(r->reqtab, mac, maclen);
                   1980:                break;
                   1981:        }
1.166     schwarze 1982:        if (t != TOKEN_NONE)
1.87      schwarze 1983:                *pos = cp - buf;
1.197     schwarze 1984:        else if (deftype == ROFFDEF_UNDEF) {
                   1985:                /* Using an undefined macro defines it to be empty. */
                   1986:                roff_setstrn(&r->strtab, mac, maclen, "", 0, 0);
                   1987:                roff_setstrn(&r->rentab, mac, maclen, NULL, 0, 0);
                   1988:        }
1.149     schwarze 1989:        return t;
1.1       schwarze 1990: }
                   1991:
1.138     schwarze 1992: /* --- handling of request blocks ----------------------------------------- */
                   1993:
1.247     schwarze 1994: /*
                   1995:  * Close a macro definition block or an "ignore" block.
                   1996:  */
1.212     schwarze 1997: static int
1.2       schwarze 1998: roff_cblock(ROFF_ARGS)
1.1       schwarze 1999: {
1.247     schwarze 2000:        int      rr;
1.2       schwarze 2001:
1.110     schwarze 2002:        if (r->last == NULL) {
1.222     schwarze 2003:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "..");
1.149     schwarze 2004:                return ROFF_IGN;
1.2       schwarze 2005:        }
1.1       schwarze 2006:
1.2       schwarze 2007:        switch (r->last->tok) {
1.80      schwarze 2008:        case ROFF_am:
                   2009:        case ROFF_ami:
                   2010:        case ROFF_de:
                   2011:        case ROFF_dei:
                   2012:        case ROFF_ig:
1.2       schwarze 2013:                break;
1.247     schwarze 2014:        case ROFF_am1:
                   2015:        case ROFF_de1:
                   2016:                /* Remapped in roff_block(). */
                   2017:                abort();
1.2       schwarze 2018:        default:
1.222     schwarze 2019:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "..");
1.149     schwarze 2020:                return ROFF_IGN;
1.2       schwarze 2021:        }
                   2022:
1.247     schwarze 2023:        roffnode_pop(r);
                   2024:        roffnode_cleanscope(r);
                   2025:
                   2026:        /*
                   2027:         * If a conditional block with braces is still open,
                   2028:         * check for "\}" block end markers.
                   2029:         */
                   2030:
                   2031:        if (r->last != NULL && r->last->endspan < 0) {
                   2032:                rr = 1;  /* If arguments follow "\}", warn about them. */
                   2033:                roff_cond_checkend(r, tok, buf, ln, ppos, pos, &rr);
                   2034:        }
                   2035:
1.110     schwarze 2036:        if (buf->buf[pos] != '\0')
1.222     schwarze 2037:                mandoc_msg(MANDOCERR_ARG_SKIP, ln, pos,
1.110     schwarze 2038:                    ".. %s", buf->buf + pos);
1.2       schwarze 2039:
1.149     schwarze 2040:        return ROFF_IGN;
1.2       schwarze 2041: }
1.1       schwarze 2042:
1.236     schwarze 2043: /*
                   2044:  * Pop all nodes ending at the end of the current input line.
                   2045:  * Return the number of loops ended.
                   2046:  */
1.212     schwarze 2047: static int
1.2       schwarze 2048: roffnode_cleanscope(struct roff *r)
                   2049: {
1.212     schwarze 2050:        int inloop;
1.1       schwarze 2051:
1.212     schwarze 2052:        inloop = 0;
1.247     schwarze 2053:        while (r->last != NULL && r->last->endspan > 0) {
1.46      schwarze 2054:                if (--r->last->endspan != 0)
1.2       schwarze 2055:                        break;
1.212     schwarze 2056:                inloop += roffnode_pop(r);
1.2       schwarze 2057:        }
1.212     schwarze 2058:        return inloop;
1.2       schwarze 2059: }
1.1       schwarze 2060:
1.236     schwarze 2061: /*
1.247     schwarze 2062:  * Handle the closing "\}" of a conditional block.
1.236     schwarze 2063:  * Apart from generating warnings, this only pops nodes.
                   2064:  * Return the number of loops ended.
                   2065:  */
1.212     schwarze 2066: static int
1.68      schwarze 2067: roff_ccond(struct roff *r, int ln, int ppos)
1.2       schwarze 2068: {
                   2069:        if (NULL == r->last) {
1.222     schwarze 2070:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "\\}");
1.212     schwarze 2071:                return 0;
1.2       schwarze 2072:        }
1.1       schwarze 2073:
1.2       schwarze 2074:        switch (r->last->tok) {
1.80      schwarze 2075:        case ROFF_el:
                   2076:        case ROFF_ie:
                   2077:        case ROFF_if:
1.212     schwarze 2078:        case ROFF_while:
1.2       schwarze 2079:                break;
                   2080:        default:
1.222     schwarze 2081:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "\\}");
1.212     schwarze 2082:                return 0;
1.2       schwarze 2083:        }
1.1       schwarze 2084:
1.2       schwarze 2085:        if (r->last->endspan > -1) {
1.222     schwarze 2086:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "\\}");
1.212     schwarze 2087:                return 0;
1.2       schwarze 2088:        }
                   2089:
1.212     schwarze 2090:        return roffnode_pop(r) + roffnode_cleanscope(r);
1.1       schwarze 2091: }
                   2092:
1.212     schwarze 2093: static int
1.2       schwarze 2094: roff_block(ROFF_ARGS)
1.1       schwarze 2095: {
1.187     schwarze 2096:        const char      *name, *value;
                   2097:        char            *call, *cp, *iname, *rname;
                   2098:        size_t           csz, namesz, rsz;
                   2099:        int              deftype;
1.16      schwarze 2100:
1.93      schwarze 2101:        /* Ignore groff compatibility mode for now. */
1.2       schwarze 2102:
1.110     schwarze 2103:        if (tok == ROFF_de1)
1.93      schwarze 2104:                tok = ROFF_de;
1.123     schwarze 2105:        else if (tok == ROFF_dei1)
                   2106:                tok = ROFF_dei;
1.110     schwarze 2107:        else if (tok == ROFF_am1)
1.93      schwarze 2108:                tok = ROFF_am;
1.123     schwarze 2109:        else if (tok == ROFF_ami1)
                   2110:                tok = ROFF_ami;
1.93      schwarze 2111:
                   2112:        /* Parse the macro name argument. */
                   2113:
1.110     schwarze 2114:        cp = buf->buf + pos;
                   2115:        if (tok == ROFF_ig) {
1.93      schwarze 2116:                iname = NULL;
                   2117:                namesz = 0;
                   2118:        } else {
                   2119:                iname = cp;
                   2120:                namesz = roff_getname(r, &cp, ln, ppos);
                   2121:                iname[namesz] = '\0';
                   2122:        }
1.22      schwarze 2123:
1.93      schwarze 2124:        /* Resolve the macro name argument if it is indirect. */
1.22      schwarze 2125:
1.110     schwarze 2126:        if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
1.187     schwarze 2127:                deftype = ROFFDEF_USER;
                   2128:                name = roff_getstrn(r, iname, namesz, &deftype);
                   2129:                if (name == NULL) {
1.222     schwarze 2130:                        mandoc_msg(MANDOCERR_STR_UNDEF,
                   2131:                            ln, (int)(iname - buf->buf),
1.93      schwarze 2132:                            "%.*s", (int)namesz, iname);
                   2133:                        namesz = 0;
                   2134:                } else
                   2135:                        namesz = strlen(name);
                   2136:        } else
                   2137:                name = iname;
1.22      schwarze 2138:
1.110     schwarze 2139:        if (namesz == 0 && tok != ROFF_ig) {
1.222     schwarze 2140:                mandoc_msg(MANDOCERR_REQ_EMPTY,
                   2141:                    ln, ppos, "%s", roff_name[tok]);
1.149     schwarze 2142:                return ROFF_IGN;
1.93      schwarze 2143:        }
1.2       schwarze 2144:
1.16      schwarze 2145:        roffnode_push(r, tok, name, ln, ppos);
                   2146:
                   2147:        /*
                   2148:         * At the beginning of a `de' macro, clear the existing string
                   2149:         * with the same name, if there is one.  New content will be
1.66      schwarze 2150:         * appended from roff_block_text() in multiline mode.
1.16      schwarze 2151:         */
1.22      schwarze 2152:
1.183     schwarze 2153:        if (tok == ROFF_de || tok == ROFF_dei) {
1.86      schwarze 2154:                roff_setstrn(&r->strtab, name, namesz, "", 0, 0);
1.183     schwarze 2155:                roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
1.187     schwarze 2156:        } else if (tok == ROFF_am || tok == ROFF_ami) {
                   2157:                deftype = ROFFDEF_ANY;
                   2158:                value = roff_getstrn(r, iname, namesz, &deftype);
                   2159:                switch (deftype) {  /* Before appending, ... */
                   2160:                case ROFFDEF_PRE: /* copy predefined to user-defined. */
                   2161:                        roff_setstrn(&r->strtab, name, namesz,
                   2162:                            value, strlen(value), 0);
                   2163:                        break;
                   2164:                case ROFFDEF_REN: /* call original standard macro. */
                   2165:                        csz = mandoc_asprintf(&call, ".%.*s \\$* \\\"\n",
                   2166:                            (int)strlen(value), value);
                   2167:                        roff_setstrn(&r->strtab, name, namesz, call, csz, 0);
                   2168:                        roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
                   2169:                        free(call);
                   2170:                        break;
                   2171:                case ROFFDEF_STD:  /* rename and call standard macro. */
                   2172:                        rsz = mandoc_asprintf(&rname, "__%s_renamed", name);
                   2173:                        roff_setstrn(&r->rentab, rname, rsz, name, namesz, 0);
                   2174:                        csz = mandoc_asprintf(&call, ".%.*s \\$* \\\"\n",
                   2175:                            (int)rsz, rname);
                   2176:                        roff_setstrn(&r->strtab, name, namesz, call, csz, 0);
                   2177:                        free(call);
                   2178:                        free(rname);
                   2179:                        break;
                   2180:                default:
                   2181:                        break;
                   2182:                }
1.183     schwarze 2183:        }
1.2       schwarze 2184:
1.110     schwarze 2185:        if (*cp == '\0')
1.149     schwarze 2186:                return ROFF_IGN;
1.1       schwarze 2187:
1.93      schwarze 2188:        /* Get the custom end marker. */
1.22      schwarze 2189:
1.93      schwarze 2190:        iname = cp;
1.86      schwarze 2191:        namesz = roff_getname(r, &cp, ln, ppos);
1.93      schwarze 2192:
                   2193:        /* Resolve the end marker if it is indirect. */
                   2194:
1.110     schwarze 2195:        if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
1.187     schwarze 2196:                deftype = ROFFDEF_USER;
                   2197:                name = roff_getstrn(r, iname, namesz, &deftype);
                   2198:                if (name == NULL) {
1.222     schwarze 2199:                        mandoc_msg(MANDOCERR_STR_UNDEF,
                   2200:                            ln, (int)(iname - buf->buf),
1.93      schwarze 2201:                            "%.*s", (int)namesz, iname);
                   2202:                        namesz = 0;
                   2203:                } else
                   2204:                        namesz = strlen(name);
                   2205:        } else
                   2206:                name = iname;
                   2207:
1.86      schwarze 2208:        if (namesz)
                   2209:                r->last->end = mandoc_strndup(name, namesz);
1.2       schwarze 2210:
1.110     schwarze 2211:        if (*cp != '\0')
1.222     schwarze 2212:                mandoc_msg(MANDOCERR_ARG_EXCESS,
1.167     schwarze 2213:                    ln, pos, ".%s ... %s", roff_name[tok], cp);
1.1       schwarze 2214:
1.149     schwarze 2215:        return ROFF_IGN;
1.1       schwarze 2216: }
                   2217:
1.212     schwarze 2218: static int
1.2       schwarze 2219: roff_block_sub(ROFF_ARGS)
1.1       schwarze 2220: {
1.166     schwarze 2221:        enum roff_tok   t;
1.2       schwarze 2222:        int             i, j;
                   2223:
                   2224:        /*
1.257     schwarze 2225:         * If a custom end marker is a user-defined or predefined macro
                   2226:         * or a request, interpret it.
1.2       schwarze 2227:         */
                   2228:
                   2229:        if (r->last->end) {
1.35      schwarze 2230:                for (i = pos, j = 0; r->last->end[j]; j++, i++)
1.110     schwarze 2231:                        if (buf->buf[i] != r->last->end[j])
1.2       schwarze 2232:                                break;
1.1       schwarze 2233:
1.110     schwarze 2234:                if (r->last->end[j] == '\0' &&
                   2235:                    (buf->buf[i] == '\0' ||
                   2236:                     buf->buf[i] == ' ' ||
                   2237:                     buf->buf[i] == '\t')) {
1.2       schwarze 2238:                        roffnode_pop(r);
                   2239:                        roffnode_cleanscope(r);
1.1       schwarze 2240:
1.110     schwarze 2241:                        while (buf->buf[i] == ' ' || buf->buf[i] == '\t')
1.35      schwarze 2242:                                i++;
                   2243:
                   2244:                        pos = i;
1.110     schwarze 2245:                        if (roff_parse(r, buf->buf, &pos, ln, ppos) !=
1.166     schwarze 2246:                            TOKEN_NONE)
1.149     schwarze 2247:                                return ROFF_RERUN;
                   2248:                        return ROFF_IGN;
1.2       schwarze 2249:                }
1.1       schwarze 2250:        }
                   2251:
1.257     schwarze 2252:        /* Handle the standard end marker. */
1.1       schwarze 2253:
1.110     schwarze 2254:        t = roff_parse(r, buf->buf, &pos, ln, ppos);
1.257     schwarze 2255:        if (t == ROFF_cblock)
                   2256:                return roff_cblock(r, t, buf, ln, ppos, pos, offs);
1.1       schwarze 2257:
1.257     schwarze 2258:        /* Not an end marker, so append the line to the block. */
1.1       schwarze 2259:
1.257     schwarze 2260:        if (tok != ROFF_ig)
                   2261:                roff_setstr(r, r->last->name, buf->buf + ppos, 2);
                   2262:        return ROFF_IGN;
1.2       schwarze 2263: }
                   2264:
1.212     schwarze 2265: static int
1.2       schwarze 2266: roff_block_text(ROFF_ARGS)
                   2267: {
                   2268:
1.110     schwarze 2269:        if (tok != ROFF_ig)
                   2270:                roff_setstr(r, r->last->name, buf->buf + pos, 2);
1.16      schwarze 2271:
1.149     schwarze 2272:        return ROFF_IGN;
1.2       schwarze 2273: }
                   2274:
1.247     schwarze 2275: /*
                   2276:  * Check for a closing "\}" and handle it.
                   2277:  * In this function, the final "int *offs" argument is used for
                   2278:  * different purposes than elsewhere:
                   2279:  * Input: *offs == 0: caller wants to discard arguments following \}
                   2280:  *        *offs == 1: caller wants to preserve text following \}
                   2281:  * Output: *offs = 0: tell caller to discard input line
                   2282:  *         *offs = 1: tell caller to use input line
                   2283:  */
1.212     schwarze 2284: static int
1.247     schwarze 2285: roff_cond_checkend(ROFF_ARGS)
1.2       schwarze 2286: {
1.212     schwarze 2287:        char            *ep;
                   2288:        int              endloop, irc, rr;
1.2       schwarze 2289:
1.212     schwarze 2290:        irc = ROFF_IGN;
1.2       schwarze 2291:        rr = r->last->rule;
1.212     schwarze 2292:        endloop = tok != ROFF_while ? ROFF_IGN :
                   2293:            rr ? ROFF_LOOPCONT : ROFF_LOOPEXIT;
                   2294:        if (roffnode_cleanscope(r))
                   2295:                irc |= endloop;
1.39      schwarze 2296:
1.69      schwarze 2297:        /*
1.247     schwarze 2298:         * If "\}" occurs on a macro line without a preceding macro or
                   2299:         * a text line contains nothing else, drop the line completely.
1.69      schwarze 2300:         */
                   2301:
1.110     schwarze 2302:        ep = buf->buf + pos;
1.247     schwarze 2303:        if (ep[0] == '\\' && ep[1] == '}' && (ep[2] == '\0' || *offs == 0))
1.71      schwarze 2304:                rr = 0;
1.69      schwarze 2305:
1.215     schwarze 2306:        /*
1.247     schwarze 2307:         * The closing delimiter "\}" rewinds the conditional scope
1.215     schwarze 2308:         * but is otherwise ignored when interpreting the line.
                   2309:         */
1.39      schwarze 2310:
1.110     schwarze 2311:        while ((ep = strchr(ep, '\\')) != NULL) {
1.190     schwarze 2312:                switch (ep[1]) {
                   2313:                case '}':
1.248     schwarze 2314:                        if (ep[2] == '\0')
                   2315:                                ep[0] = '\0';
                   2316:                        else if (rr)
1.247     schwarze 2317:                                ep[1] = '&';
                   2318:                        else
                   2319:                                memmove(ep, ep + 2, strlen(ep + 2) + 1);
1.212     schwarze 2320:                        if (roff_ccond(r, ln, ep - buf->buf))
                   2321:                                irc |= endloop;
1.190     schwarze 2322:                        break;
                   2323:                case '\0':
                   2324:                        ++ep;
                   2325:                        break;
                   2326:                default:
                   2327:                        ep += 2;
                   2328:                        break;
1.70      schwarze 2329:                }
1.50      schwarze 2330:        }
1.247     schwarze 2331:        *offs = rr;
                   2332:        return irc;
                   2333: }
1.190     schwarze 2334:
1.247     schwarze 2335: /*
                   2336:  * Parse and process a request or macro line in conditional scope.
                   2337:  */
                   2338: static int
                   2339: roff_cond_sub(ROFF_ARGS)
                   2340: {
                   2341:        struct roffnode *bl;
1.258     schwarze 2342:        int              irc, rr, spos;
1.247     schwarze 2343:        enum roff_tok    t;
                   2344:
                   2345:        rr = 0;  /* If arguments follow "\}", skip them. */
                   2346:        irc = roff_cond_checkend(r, tok, buf, ln, ppos, pos, &rr);
1.258     schwarze 2347:        spos = pos;
1.240     schwarze 2348:        t = roff_parse(r, buf->buf, &pos, ln, ppos);
                   2349:
1.190     schwarze 2350:        /*
1.258     schwarze 2351:         * Handle requests and macros if the conditional evaluated
                   2352:         * to true or if they are structurally required.
                   2353:         * The .break request is always handled specially.
1.190     schwarze 2354:         */
                   2355:
1.236     schwarze 2356:        if (t == ROFF_break) {
                   2357:                if (irc & ROFF_LOOPMASK)
                   2358:                        irc = ROFF_IGN | ROFF_LOOPEXIT;
                   2359:                else if (rr) {
                   2360:                        for (bl = r->last; bl != NULL; bl = bl->parent) {
                   2361:                                bl->rule = 0;
                   2362:                                if (bl->tok == ROFF_while)
                   2363:                                        break;
                   2364:                        }
                   2365:                }
1.258     schwarze 2366:        } else if (rr || (t < TOKEN_NONE && roffs[t].flags & ROFFMAC_STRUCT)) {
                   2367:                irc |= roff_req_or_macro(r, t, buf, ln, spos, pos, offs);
1.255     schwarze 2368:                if (irc & ROFF_WHILE)
                   2369:                        irc &= ~(ROFF_LOOPCONT | ROFF_LOOPEXIT);
1.258     schwarze 2370:        }
1.212     schwarze 2371:        return irc;
1.2       schwarze 2372: }
                   2373:
1.247     schwarze 2374: /*
                   2375:  * Parse and process a text line in conditional scope.
                   2376:  */
1.212     schwarze 2377: static int
1.2       schwarze 2378: roff_cond_text(ROFF_ARGS)
                   2379: {
1.247     schwarze 2380:        int      irc, rr;
1.215     schwarze 2381:
1.247     schwarze 2382:        rr = 1;  /* If arguments follow "\}", preserve them. */
                   2383:        irc = roff_cond_checkend(r, tok, buf, ln, ppos, pos, &rr);
1.212     schwarze 2384:        if (rr)
                   2385:                irc |= ROFF_CONT;
                   2386:        return irc;
1.2       schwarze 2387: }
                   2388:
1.138     schwarze 2389: /* --- handling of numeric and conditional expressions -------------------- */
                   2390:
1.77      schwarze 2391: /*
                   2392:  * Parse a single signed integer number.  Stop at the first non-digit.
                   2393:  * If there is at least one digit, return success and advance the
                   2394:  * parse point, else return failure and let the parse point unchanged.
                   2395:  * Ignore overflows, treat them just like the C language.
                   2396:  */
1.56      schwarze 2397: static int
1.133     schwarze 2398: roff_getnum(const char *v, int *pos, int *res, int flags)
1.56      schwarze 2399: {
1.133     schwarze 2400:        int      myres, scaled, n, p;
1.79      schwarze 2401:
                   2402:        if (NULL == res)
                   2403:                res = &myres;
1.56      schwarze 2404:
                   2405:        p = *pos;
                   2406:        n = v[p] == '-';
1.133     schwarze 2407:        if (n || v[p] == '+')
1.56      schwarze 2408:                p++;
                   2409:
1.133     schwarze 2410:        if (flags & ROFFNUM_WHITE)
                   2411:                while (isspace((unsigned char)v[p]))
                   2412:                        p++;
                   2413:
1.56      schwarze 2414:        for (*res = 0; isdigit((unsigned char)v[p]); p++)
1.77      schwarze 2415:                *res = 10 * *res + v[p] - '0';
1.56      schwarze 2416:        if (p == *pos + n)
                   2417:                return 0;
                   2418:
                   2419:        if (n)
                   2420:                *res = -*res;
                   2421:
1.126     schwarze 2422:        /* Each number may be followed by one optional scaling unit. */
                   2423:
                   2424:        switch (v[p]) {
                   2425:        case 'f':
1.133     schwarze 2426:                scaled = *res * 65536;
1.126     schwarze 2427:                break;
                   2428:        case 'i':
1.133     schwarze 2429:                scaled = *res * 240;
1.126     schwarze 2430:                break;
                   2431:        case 'c':
1.133     schwarze 2432:                scaled = *res * 240 / 2.54;
1.126     schwarze 2433:                break;
                   2434:        case 'v':
                   2435:        case 'P':
1.133     schwarze 2436:                scaled = *res * 40;
1.126     schwarze 2437:                break;
                   2438:        case 'm':
                   2439:        case 'n':
1.133     schwarze 2440:                scaled = *res * 24;
1.126     schwarze 2441:                break;
                   2442:        case 'p':
1.133     schwarze 2443:                scaled = *res * 10 / 3;
1.126     schwarze 2444:                break;
                   2445:        case 'u':
1.133     schwarze 2446:                scaled = *res;
1.126     schwarze 2447:                break;
                   2448:        case 'M':
1.133     schwarze 2449:                scaled = *res * 6 / 25;
1.126     schwarze 2450:                break;
                   2451:        default:
1.133     schwarze 2452:                scaled = *res;
1.126     schwarze 2453:                p--;
                   2454:                break;
                   2455:        }
1.133     schwarze 2456:        if (flags & ROFFNUM_SCALE)
                   2457:                *res = scaled;
1.126     schwarze 2458:
                   2459:        *pos = p + 1;
1.149     schwarze 2460:        return 1;
1.56      schwarze 2461: }
                   2462:
1.71      schwarze 2463: /*
                   2464:  * Evaluate a string comparison condition.
                   2465:  * The first character is the delimiter.
                   2466:  * Succeed if the string up to its second occurrence
                   2467:  * matches the string up to its third occurence.
                   2468:  * Advance the cursor after the third occurrence
                   2469:  * or lacking that, to the end of the line.
                   2470:  */
                   2471: static int
                   2472: roff_evalstrcond(const char *v, int *pos)
                   2473: {
                   2474:        const char      *s1, *s2, *s3;
                   2475:        int              match;
                   2476:
                   2477:        match = 0;
                   2478:        s1 = v + *pos;          /* initial delimiter */
                   2479:        s2 = s1 + 1;            /* for scanning the first string */
                   2480:        s3 = strchr(s2, *s1);   /* for scanning the second string */
                   2481:
                   2482:        if (NULL == s3)         /* found no middle delimiter */
                   2483:                goto out;
                   2484:
                   2485:        while ('\0' != *++s3) {
                   2486:                if (*s2 != *s3) {  /* mismatch */
                   2487:                        s3 = strchr(s3, *s1);
                   2488:                        break;
                   2489:                }
                   2490:                if (*s3 == *s1) {  /* found the final delimiter */
                   2491:                        match = 1;
                   2492:                        break;
                   2493:                }
                   2494:                s2++;
                   2495:        }
                   2496:
                   2497: out:
                   2498:        if (NULL == s3)
                   2499:                s3 = strchr(s2, '\0');
1.114     schwarze 2500:        else if (*s3 != '\0')
1.71      schwarze 2501:                s3++;
                   2502:        *pos = s3 - v;
1.149     schwarze 2503:        return match;
1.71      schwarze 2504: }
                   2505:
1.77      schwarze 2506: /*
                   2507:  * Evaluate an optionally negated single character, numerical,
                   2508:  * or string condition.
                   2509:  */
1.71      schwarze 2510: static int
1.143     schwarze 2511: roff_evalcond(struct roff *r, int ln, char *v, int *pos)
1.5       schwarze 2512: {
1.208     schwarze 2513:        const char      *start, *end;
                   2514:        char            *cp, *name;
                   2515:        size_t           sz;
                   2516:        int              deftype, len, number, savepos, istrue, wanttrue;
1.5       schwarze 2517:
1.71      schwarze 2518:        if ('!' == v[*pos]) {
                   2519:                wanttrue = 0;
                   2520:                (*pos)++;
                   2521:        } else
                   2522:                wanttrue = 1;
                   2523:
1.5       schwarze 2524:        switch (v[*pos]) {
1.112     schwarze 2525:        case '\0':
1.149     schwarze 2526:                return 0;
1.80      schwarze 2527:        case 'n':
                   2528:        case 'o':
1.5       schwarze 2529:                (*pos)++;
1.149     schwarze 2530:                return wanttrue;
1.80      schwarze 2531:        case 'e':
                   2532:        case 't':
1.111     schwarze 2533:        case 'v':
1.5       schwarze 2534:                (*pos)++;
1.149     schwarze 2535:                return !wanttrue;
1.208     schwarze 2536:        case 'c':
                   2537:                do {
                   2538:                        (*pos)++;
                   2539:                } while (v[*pos] == ' ');
                   2540:
                   2541:                /*
                   2542:                 * Quirk for groff compatibility:
                   2543:                 * The horizontal tab is neither available nor unavailable.
                   2544:                 */
                   2545:
                   2546:                if (v[*pos] == '\t') {
                   2547:                        (*pos)++;
                   2548:                        return 0;
                   2549:                }
                   2550:
                   2551:                /* Printable ASCII characters are available. */
                   2552:
                   2553:                if (v[*pos] != '\\') {
                   2554:                        (*pos)++;
                   2555:                        return wanttrue;
                   2556:                }
                   2557:
                   2558:                end = v + ++*pos;
                   2559:                switch (mandoc_escape(&end, &start, &len)) {
                   2560:                case ESCAPE_SPECIAL:
                   2561:                        istrue = mchars_spec2cp(start, len) != -1;
                   2562:                        break;
                   2563:                case ESCAPE_UNICODE:
                   2564:                        istrue = 1;
                   2565:                        break;
                   2566:                case ESCAPE_NUMBERED:
                   2567:                        istrue = mchars_num2char(start, len) != -1;
                   2568:                        break;
                   2569:                default:
                   2570:                        istrue = !wanttrue;
                   2571:                        break;
                   2572:                }
                   2573:                *pos = end - v;
                   2574:                return istrue == wanttrue;
1.182     schwarze 2575:        case 'd':
1.143     schwarze 2576:        case 'r':
1.182     schwarze 2577:                cp = v + *pos + 1;
                   2578:                while (*cp == ' ')
                   2579:                        cp++;
                   2580:                name = cp;
                   2581:                sz = roff_getname(r, &cp, ln, cp - v);
1.187     schwarze 2582:                if (sz == 0)
                   2583:                        istrue = 0;
                   2584:                else if (v[*pos] == 'r')
                   2585:                        istrue = roff_hasregn(r, name, sz);
                   2586:                else {
                   2587:                        deftype = ROFFDEF_ANY;
                   2588:                        roff_getstrn(r, name, sz, &deftype);
                   2589:                        istrue = !!deftype;
                   2590:                }
1.235     schwarze 2591:                *pos = (name + sz) - v;
1.182     schwarze 2592:                return istrue == wanttrue;
1.5       schwarze 2593:        default:
                   2594:                break;
                   2595:        }
                   2596:
1.113     schwarze 2597:        savepos = *pos;
1.133     schwarze 2598:        if (roff_evalnum(r, ln, v, pos, &number, ROFFNUM_SCALE))
1.149     schwarze 2599:                return (number > 0) == wanttrue;
1.113     schwarze 2600:        else if (*pos == savepos)
1.149     schwarze 2601:                return roff_evalstrcond(v, pos) == wanttrue;
1.77      schwarze 2602:        else
1.149     schwarze 2603:                return 0;
1.5       schwarze 2604: }
                   2605:
1.212     schwarze 2606: static int
1.21      schwarze 2607: roff_line_ignore(ROFF_ARGS)
1.6       schwarze 2608: {
1.30      schwarze 2609:
1.149     schwarze 2610:        return ROFF_IGN;
1.21      schwarze 2611: }
                   2612:
1.212     schwarze 2613: static int
1.123     schwarze 2614: roff_insec(ROFF_ARGS)
                   2615: {
                   2616:
1.222     schwarze 2617:        mandoc_msg(MANDOCERR_REQ_INSEC, ln, ppos, "%s", roff_name[tok]);
1.149     schwarze 2618:        return ROFF_IGN;
1.123     schwarze 2619: }
                   2620:
1.212     schwarze 2621: static int
1.123     schwarze 2622: roff_unsupp(ROFF_ARGS)
                   2623: {
                   2624:
1.222     schwarze 2625:        mandoc_msg(MANDOCERR_REQ_UNSUPP, ln, ppos, "%s", roff_name[tok]);
1.149     schwarze 2626:        return ROFF_IGN;
1.123     schwarze 2627: }
                   2628:
1.212     schwarze 2629: static int
1.2       schwarze 2630: roff_cond(ROFF_ARGS)
                   2631: {
1.212     schwarze 2632:        int      irc;
1.46      schwarze 2633:
                   2634:        roffnode_push(r, tok, NULL, ln, ppos);
1.2       schwarze 2635:
1.80      schwarze 2636:        /*
1.35      schwarze 2637:         * An `.el' has no conditional body: it will consume the value
                   2638:         * of the current rstack entry set in prior `ie' calls or
1.80      schwarze 2639:         * defaults to DENY.
1.35      schwarze 2640:         *
                   2641:         * If we're not an `el', however, then evaluate the conditional.
                   2642:         */
1.1       schwarze 2643:
1.110     schwarze 2644:        r->last->rule = tok == ROFF_el ?
1.80      schwarze 2645:            (r->rstackpos < 0 ? 0 : r->rstack[r->rstackpos--]) :
1.110     schwarze 2646:            roff_evalcond(r, ln, buf->buf, &pos);
1.2       schwarze 2647:
1.35      schwarze 2648:        /*
                   2649:         * An if-else will put the NEGATION of the current evaluated
                   2650:         * conditional into the stack of rules.
                   2651:         */
                   2652:
1.110     schwarze 2653:        if (tok == ROFF_ie) {
1.96      schwarze 2654:                if (r->rstackpos + 1 == r->rstacksz) {
                   2655:                        r->rstacksz += 16;
                   2656:                        r->rstack = mandoc_reallocarray(r->rstack,
                   2657:                            r->rstacksz, sizeof(int));
1.35      schwarze 2658:                }
1.71      schwarze 2659:                r->rstack[++r->rstackpos] = !r->last->rule;
1.2       schwarze 2660:        }
1.5       schwarze 2661:
                   2662:        /* If the parent has false as its rule, then so do we. */
                   2663:
1.71      schwarze 2664:        if (r->last->parent && !r->last->parent->rule)
                   2665:                r->last->rule = 0;
1.5       schwarze 2666:
                   2667:        /*
1.46      schwarze 2668:         * Determine scope.
                   2669:         * If there is nothing on the line after the conditional,
                   2670:         * not even whitespace, use next-line scope.
1.212     schwarze 2671:         * Except that .while does not support next-line scope.
1.5       schwarze 2672:         */
1.2       schwarze 2673:
1.212     schwarze 2674:        if (buf->buf[pos] == '\0' && tok != ROFF_while) {
1.46      schwarze 2675:                r->last->endspan = 2;
                   2676:                goto out;
                   2677:        }
                   2678:
1.110     schwarze 2679:        while (buf->buf[pos] == ' ')
1.46      schwarze 2680:                pos++;
                   2681:
                   2682:        /* An opening brace requests multiline scope. */
1.2       schwarze 2683:
1.110     schwarze 2684:        if (buf->buf[pos] == '\\' && buf->buf[pos + 1] == '{') {
1.2       schwarze 2685:                r->last->endspan = -1;
                   2686:                pos += 2;
1.144     schwarze 2687:                while (buf->buf[pos] == ' ')
                   2688:                        pos++;
1.46      schwarze 2689:                goto out;
1.80      schwarze 2690:        }
1.2       schwarze 2691:
                   2692:        /*
1.46      schwarze 2693:         * Anything else following the conditional causes
                   2694:         * single-line scope.  Warn if the scope contains
                   2695:         * nothing but trailing whitespace.
1.2       schwarze 2696:         */
                   2697:
1.110     schwarze 2698:        if (buf->buf[pos] == '\0')
1.222     schwarze 2699:                mandoc_msg(MANDOCERR_COND_EMPTY,
                   2700:                    ln, ppos, "%s", roff_name[tok]);
1.2       schwarze 2701:
1.46      schwarze 2702:        r->last->endspan = 1;
1.1       schwarze 2703:
1.46      schwarze 2704: out:
1.2       schwarze 2705:        *offs = pos;
1.212     schwarze 2706:        irc = ROFF_RERUN;
                   2707:        if (tok == ROFF_while)
                   2708:                irc |= ROFF_WHILE;
                   2709:        return irc;
1.1       schwarze 2710: }
                   2711:
1.212     schwarze 2712: static int
1.7       schwarze 2713: roff_ds(ROFF_ARGS)
                   2714: {
1.85      schwarze 2715:        char            *string;
                   2716:        const char      *name;
                   2717:        size_t           namesz;
1.10      schwarze 2718:
1.123     schwarze 2719:        /* Ignore groff compatibility mode for now. */
                   2720:
                   2721:        if (tok == ROFF_ds1)
                   2722:                tok = ROFF_ds;
                   2723:        else if (tok == ROFF_as1)
                   2724:                tok = ROFF_as;
                   2725:
1.10      schwarze 2726:        /*
1.85      schwarze 2727:         * The first word is the name of the string.
                   2728:         * If it is empty or terminated by an escape sequence,
                   2729:         * abort the `ds' request without defining anything.
1.10      schwarze 2730:         */
1.7       schwarze 2731:
1.110     schwarze 2732:        name = string = buf->buf + pos;
                   2733:        if (*name == '\0')
1.149     schwarze 2734:                return ROFF_IGN;
1.7       schwarze 2735:
1.85      schwarze 2736:        namesz = roff_getname(r, &string, ln, pos);
1.235     schwarze 2737:        switch (name[namesz]) {
                   2738:        case '\\':
1.149     schwarze 2739:                return ROFF_IGN;
1.235     schwarze 2740:        case '\t':
                   2741:                string = buf->buf + pos + namesz;
                   2742:                break;
                   2743:        default:
                   2744:                break;
                   2745:        }
1.85      schwarze 2746:
                   2747:        /* Read past the initial double-quote, if any. */
1.110     schwarze 2748:        if (*string == '"')
1.7       schwarze 2749:                string++;
                   2750:
1.10      schwarze 2751:        /* The rest is the value. */
1.85      schwarze 2752:        roff_setstrn(&r->strtab, name, namesz, string, strlen(string),
                   2753:            ROFF_as == tok);
1.183     schwarze 2754:        roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
1.149     schwarze 2755:        return ROFF_IGN;
1.7       schwarze 2756: }
                   2757:
1.77      schwarze 2758: /*
                   2759:  * Parse a single operator, one or two characters long.
                   2760:  * If the operator is recognized, return success and advance the
                   2761:  * parse point, else return failure and let the parse point unchanged.
                   2762:  */
                   2763: static int
                   2764: roff_getop(const char *v, int *pos, char *res)
                   2765: {
                   2766:
                   2767:        *res = v[*pos];
                   2768:
                   2769:        switch (*res) {
1.80      schwarze 2770:        case '+':
                   2771:        case '-':
                   2772:        case '*':
                   2773:        case '/':
                   2774:        case '%':
                   2775:        case '&':
                   2776:        case ':':
1.77      schwarze 2777:                break;
                   2778:        case '<':
                   2779:                switch (v[*pos + 1]) {
1.80      schwarze 2780:                case '=':
1.77      schwarze 2781:                        *res = 'l';
                   2782:                        (*pos)++;
                   2783:                        break;
1.80      schwarze 2784:                case '>':
1.77      schwarze 2785:                        *res = '!';
                   2786:                        (*pos)++;
                   2787:                        break;
1.80      schwarze 2788:                case '?':
1.77      schwarze 2789:                        *res = 'i';
                   2790:                        (*pos)++;
                   2791:                        break;
                   2792:                default:
                   2793:                        break;
                   2794:                }
                   2795:                break;
                   2796:        case '>':
                   2797:                switch (v[*pos + 1]) {
1.80      schwarze 2798:                case '=':
1.77      schwarze 2799:                        *res = 'g';
                   2800:                        (*pos)++;
                   2801:                        break;
1.80      schwarze 2802:                case '?':
1.77      schwarze 2803:                        *res = 'a';
                   2804:                        (*pos)++;
                   2805:                        break;
                   2806:                default:
                   2807:                        break;
                   2808:                }
                   2809:                break;
                   2810:        case '=':
                   2811:                if ('=' == v[*pos + 1])
                   2812:                        (*pos)++;
                   2813:                break;
                   2814:        default:
1.149     schwarze 2815:                return 0;
1.77      schwarze 2816:        }
                   2817:        (*pos)++;
                   2818:
1.149     schwarze 2819:        return *res;
1.77      schwarze 2820: }
                   2821:
                   2822: /*
                   2823:  * Evaluate either a parenthesized numeric expression
                   2824:  * or a single signed integer number.
                   2825:  */
                   2826: static int
1.106     schwarze 2827: roff_evalpar(struct roff *r, int ln,
1.133     schwarze 2828:        const char *v, int *pos, int *res, int flags)
1.77      schwarze 2829: {
                   2830:
                   2831:        if ('(' != v[*pos])
1.149     schwarze 2832:                return roff_getnum(v, pos, res, flags);
1.77      schwarze 2833:
                   2834:        (*pos)++;
1.133     schwarze 2835:        if ( ! roff_evalnum(r, ln, v, pos, res, flags | ROFFNUM_WHITE))
1.149     schwarze 2836:                return 0;
1.77      schwarze 2837:
1.79      schwarze 2838:        /*
                   2839:         * Omission of the closing parenthesis
                   2840:         * is an error in validation mode,
                   2841:         * but ignored in evaluation mode.
                   2842:         */
                   2843:
1.77      schwarze 2844:        if (')' == v[*pos])
                   2845:                (*pos)++;
1.79      schwarze 2846:        else if (NULL == res)
1.149     schwarze 2847:                return 0;
1.77      schwarze 2848:
1.149     schwarze 2849:        return 1;
1.77      schwarze 2850: }
                   2851:
                   2852: /*
                   2853:  * Evaluate a complete numeric expression.
                   2854:  * Proceed left to right, there is no concept of precedence.
                   2855:  */
                   2856: static int
1.106     schwarze 2857: roff_evalnum(struct roff *r, int ln, const char *v,
1.133     schwarze 2858:        int *pos, int *res, int flags)
1.77      schwarze 2859: {
                   2860:        int              mypos, operand2;
                   2861:        char             operator;
                   2862:
                   2863:        if (NULL == pos) {
                   2864:                mypos = 0;
                   2865:                pos = &mypos;
                   2866:        }
                   2867:
1.133     schwarze 2868:        if (flags & ROFFNUM_WHITE)
1.77      schwarze 2869:                while (isspace((unsigned char)v[*pos]))
                   2870:                        (*pos)++;
                   2871:
1.133     schwarze 2872:        if ( ! roff_evalpar(r, ln, v, pos, res, flags))
1.149     schwarze 2873:                return 0;
1.77      schwarze 2874:
                   2875:        while (1) {
1.133     schwarze 2876:                if (flags & ROFFNUM_WHITE)
1.77      schwarze 2877:                        while (isspace((unsigned char)v[*pos]))
                   2878:                                (*pos)++;
                   2879:
                   2880:                if ( ! roff_getop(v, pos, &operator))
                   2881:                        break;
                   2882:
1.133     schwarze 2883:                if (flags & ROFFNUM_WHITE)
1.77      schwarze 2884:                        while (isspace((unsigned char)v[*pos]))
                   2885:                                (*pos)++;
                   2886:
1.133     schwarze 2887:                if ( ! roff_evalpar(r, ln, v, pos, &operand2, flags))
1.149     schwarze 2888:                        return 0;
1.77      schwarze 2889:
1.133     schwarze 2890:                if (flags & ROFFNUM_WHITE)
1.77      schwarze 2891:                        while (isspace((unsigned char)v[*pos]))
                   2892:                                (*pos)++;
1.79      schwarze 2893:
                   2894:                if (NULL == res)
                   2895:                        continue;
1.77      schwarze 2896:
                   2897:                switch (operator) {
1.80      schwarze 2898:                case '+':
1.77      schwarze 2899:                        *res += operand2;
                   2900:                        break;
1.80      schwarze 2901:                case '-':
1.77      schwarze 2902:                        *res -= operand2;
                   2903:                        break;
1.80      schwarze 2904:                case '*':
1.77      schwarze 2905:                        *res *= operand2;
                   2906:                        break;
1.80      schwarze 2907:                case '/':
1.116     schwarze 2908:                        if (operand2 == 0) {
1.106     schwarze 2909:                                mandoc_msg(MANDOCERR_DIVZERO,
1.222     schwarze 2910:                                        ln, *pos, "%s", v);
1.106     schwarze 2911:                                *res = 0;
                   2912:                                break;
                   2913:                        }
1.77      schwarze 2914:                        *res /= operand2;
                   2915:                        break;
1.80      schwarze 2916:                case '%':
1.116     schwarze 2917:                        if (operand2 == 0) {
                   2918:                                mandoc_msg(MANDOCERR_DIVZERO,
1.222     schwarze 2919:                                        ln, *pos, "%s", v);
1.116     schwarze 2920:                                *res = 0;
                   2921:                                break;
                   2922:                        }
1.77      schwarze 2923:                        *res %= operand2;
                   2924:                        break;
1.80      schwarze 2925:                case '<':
1.77      schwarze 2926:                        *res = *res < operand2;
                   2927:                        break;
1.80      schwarze 2928:                case '>':
1.77      schwarze 2929:                        *res = *res > operand2;
                   2930:                        break;
1.80      schwarze 2931:                case 'l':
1.77      schwarze 2932:                        *res = *res <= operand2;
                   2933:                        break;
1.80      schwarze 2934:                case 'g':
1.77      schwarze 2935:                        *res = *res >= operand2;
                   2936:                        break;
1.80      schwarze 2937:                case '=':
1.77      schwarze 2938:                        *res = *res == operand2;
                   2939:                        break;
1.80      schwarze 2940:                case '!':
1.77      schwarze 2941:                        *res = *res != operand2;
                   2942:                        break;
1.80      schwarze 2943:                case '&':
1.77      schwarze 2944:                        *res = *res && operand2;
                   2945:                        break;
1.80      schwarze 2946:                case ':':
1.77      schwarze 2947:                        *res = *res || operand2;
                   2948:                        break;
1.80      schwarze 2949:                case 'i':
1.77      schwarze 2950:                        if (operand2 < *res)
                   2951:                                *res = operand2;
                   2952:                        break;
1.80      schwarze 2953:                case 'a':
1.77      schwarze 2954:                        if (operand2 > *res)
                   2955:                                *res = operand2;
                   2956:                        break;
                   2957:                default:
                   2958:                        abort();
                   2959:                }
                   2960:        }
1.149     schwarze 2961:        return 1;
1.77      schwarze 2962: }
                   2963:
1.138     schwarze 2964: /* --- register management ------------------------------------------------ */
                   2965:
1.52      schwarze 2966: void
1.60      schwarze 2967: roff_setreg(struct roff *r, const char *name, int val, char sign)
1.41      schwarze 2968: {
1.199     schwarze 2969:        roff_setregn(r, name, strlen(name), val, sign, INT_MIN);
1.198     schwarze 2970: }
                   2971:
                   2972: static void
                   2973: roff_setregn(struct roff *r, const char *name, size_t len,
1.199     schwarze 2974:     int val, char sign, int step)
1.198     schwarze 2975: {
1.52      schwarze 2976:        struct roffreg  *reg;
                   2977:
                   2978:        /* Search for an existing register with the same name. */
                   2979:        reg = r->regtab;
                   2980:
1.198     schwarze 2981:        while (reg != NULL && (reg->key.sz != len ||
                   2982:            strncmp(reg->key.p, name, len) != 0))
1.52      schwarze 2983:                reg = reg->next;
1.41      schwarze 2984:
1.52      schwarze 2985:        if (NULL == reg) {
                   2986:                /* Create a new register. */
                   2987:                reg = mandoc_malloc(sizeof(struct roffreg));
1.198     schwarze 2988:                reg->key.p = mandoc_strndup(name, len);
                   2989:                reg->key.sz = len;
1.60      schwarze 2990:                reg->val = 0;
1.199     schwarze 2991:                reg->step = 0;
1.52      schwarze 2992:                reg->next = r->regtab;
                   2993:                r->regtab = reg;
                   2994:        }
                   2995:
1.60      schwarze 2996:        if ('+' == sign)
                   2997:                reg->val += val;
                   2998:        else if ('-' == sign)
                   2999:                reg->val -= val;
                   3000:        else
                   3001:                reg->val = val;
1.199     schwarze 3002:        if (step != INT_MIN)
                   3003:                reg->step = step;
1.41      schwarze 3004: }
                   3005:
1.65      schwarze 3006: /*
                   3007:  * Handle some predefined read-only number registers.
                   3008:  * For now, return -1 if the requested register is not predefined;
                   3009:  * in case a predefined read-only register having the value -1
                   3010:  * were to turn up, another special value would have to be chosen.
                   3011:  */
                   3012: static int
1.145     schwarze 3013: roff_getregro(const struct roff *r, const char *name)
1.65      schwarze 3014: {
                   3015:
                   3016:        switch (*name) {
1.145     schwarze 3017:        case '$':  /* Number of arguments of the last macro evaluated. */
1.211     schwarze 3018:                return r->mstackpos < 0 ? 0 : r->mstack[r->mstackpos].argc;
1.80      schwarze 3019:        case 'A':  /* ASCII approximation mode is always off. */
1.149     schwarze 3020:                return 0;
1.80      schwarze 3021:        case 'g':  /* Groff compatibility mode is always on. */
1.149     schwarze 3022:                return 1;
1.80      schwarze 3023:        case 'H':  /* Fixed horizontal resolution. */
1.149     schwarze 3024:                return 24;
1.80      schwarze 3025:        case 'j':  /* Always adjust left margin only. */
1.149     schwarze 3026:                return 0;
1.80      schwarze 3027:        case 'T':  /* Some output device is always defined. */
1.149     schwarze 3028:                return 1;
1.80      schwarze 3029:        case 'V':  /* Fixed vertical resolution. */
1.149     schwarze 3030:                return 40;
1.65      schwarze 3031:        default:
1.149     schwarze 3032:                return -1;
1.65      schwarze 3033:        }
                   3034: }
                   3035:
1.53      schwarze 3036: int
1.198     schwarze 3037: roff_getreg(struct roff *r, const char *name)
1.41      schwarze 3038: {
1.199     schwarze 3039:        return roff_getregn(r, name, strlen(name), '\0');
1.53      schwarze 3040: }
                   3041:
                   3042: static int
1.199     schwarze 3043: roff_getregn(struct roff *r, const char *name, size_t len, char sign)
1.53      schwarze 3044: {
                   3045:        struct roffreg  *reg;
1.65      schwarze 3046:        int              val;
                   3047:
                   3048:        if ('.' == name[0] && 2 == len) {
1.145     schwarze 3049:                val = roff_getregro(r, name + 1);
1.65      schwarze 3050:                if (-1 != val)
1.149     schwarze 3051:                        return val;
1.65      schwarze 3052:        }
1.53      schwarze 3053:
1.199     schwarze 3054:        for (reg = r->regtab; reg; reg = reg->next) {
1.53      schwarze 3055:                if (len == reg->key.sz &&
1.199     schwarze 3056:                    0 == strncmp(name, reg->key.p, len)) {
                   3057:                        switch (sign) {
                   3058:                        case '+':
                   3059:                                reg->val += reg->step;
                   3060:                                break;
                   3061:                        case '-':
                   3062:                                reg->val -= reg->step;
                   3063:                                break;
                   3064:                        default:
                   3065:                                break;
                   3066:                        }
1.149     schwarze 3067:                        return reg->val;
1.199     schwarze 3068:                }
                   3069:        }
1.143     schwarze 3070:
1.199     schwarze 3071:        roff_setregn(r, name, len, 0, '\0', INT_MIN);
1.149     schwarze 3072:        return 0;
1.143     schwarze 3073: }
                   3074:
                   3075: static int
                   3076: roff_hasregn(const struct roff *r, const char *name, size_t len)
                   3077: {
                   3078:        struct roffreg  *reg;
                   3079:        int              val;
                   3080:
                   3081:        if ('.' == name[0] && 2 == len) {
1.145     schwarze 3082:                val = roff_getregro(r, name + 1);
1.143     schwarze 3083:                if (-1 != val)
1.149     schwarze 3084:                        return 1;
1.143     schwarze 3085:        }
                   3086:
                   3087:        for (reg = r->regtab; reg; reg = reg->next)
                   3088:                if (len == reg->key.sz &&
                   3089:                    0 == strncmp(name, reg->key.p, len))
1.149     schwarze 3090:                        return 1;
1.41      schwarze 3091:
1.149     schwarze 3092:        return 0;
1.41      schwarze 3093: }
                   3094:
1.52      schwarze 3095: static void
                   3096: roff_freereg(struct roffreg *reg)
1.41      schwarze 3097: {
1.52      schwarze 3098:        struct roffreg  *old_reg;
1.41      schwarze 3099:
1.52      schwarze 3100:        while (NULL != reg) {
                   3101:                free(reg->key.p);
                   3102:                old_reg = reg;
                   3103:                reg = reg->next;
                   3104:                free(old_reg);
                   3105:        }
1.41      schwarze 3106: }
1.7       schwarze 3107:
1.212     schwarze 3108: static int
1.6       schwarze 3109: roff_nr(ROFF_ARGS)
1.1       schwarze 3110: {
1.199     schwarze 3111:        char            *key, *val, *step;
1.85      schwarze 3112:        size_t           keysz;
1.199     schwarze 3113:        int              iv, is, len;
1.60      schwarze 3114:        char             sign;
1.6       schwarze 3115:
1.110     schwarze 3116:        key = val = buf->buf + pos;
                   3117:        if (*key == '\0')
1.149     schwarze 3118:                return ROFF_IGN;
1.85      schwarze 3119:
                   3120:        keysz = roff_getname(r, &val, ln, pos);
1.235     schwarze 3121:        if (key[keysz] == '\\' || key[keysz] == '\t')
1.149     schwarze 3122:                return ROFF_IGN;
1.6       schwarze 3123:
1.60      schwarze 3124:        sign = *val;
1.110     schwarze 3125:        if (sign == '+' || sign == '-')
1.60      schwarze 3126:                val++;
                   3127:
1.199     schwarze 3128:        len = 0;
                   3129:        if (roff_evalnum(r, ln, val, &len, &iv, ROFFNUM_SCALE) == 0)
                   3130:                return ROFF_IGN;
                   3131:
                   3132:        step = val + len;
                   3133:        while (isspace((unsigned char)*step))
                   3134:                step++;
                   3135:        if (roff_evalnum(r, ln, step, NULL, &is, 0) == 0)
                   3136:                is = INT_MIN;
1.1       schwarze 3137:
1.199     schwarze 3138:        roff_setregn(r, key, keysz, iv, sign, is);
1.149     schwarze 3139:        return ROFF_IGN;
1.76      schwarze 3140: }
                   3141:
1.212     schwarze 3142: static int
1.76      schwarze 3143: roff_rr(ROFF_ARGS)
                   3144: {
                   3145:        struct roffreg  *reg, **prev;
1.85      schwarze 3146:        char            *name, *cp;
                   3147:        size_t           namesz;
1.76      schwarze 3148:
1.110     schwarze 3149:        name = cp = buf->buf + pos;
                   3150:        if (*name == '\0')
1.149     schwarze 3151:                return ROFF_IGN;
1.85      schwarze 3152:        namesz = roff_getname(r, &cp, ln, pos);
                   3153:        name[namesz] = '\0';
1.76      schwarze 3154:
                   3155:        prev = &r->regtab;
                   3156:        while (1) {
                   3157:                reg = *prev;
1.110     schwarze 3158:                if (reg == NULL || !strcmp(name, reg->key.p))
1.76      schwarze 3159:                        break;
                   3160:                prev = &reg->next;
                   3161:        }
1.110     schwarze 3162:        if (reg != NULL) {
1.76      schwarze 3163:                *prev = reg->next;
                   3164:                free(reg->key.p);
                   3165:                free(reg);
                   3166:        }
1.149     schwarze 3167:        return ROFF_IGN;
1.29      schwarze 3168: }
                   3169:
1.138     schwarze 3170: /* --- handler functions for roff requests -------------------------------- */
                   3171:
1.212     schwarze 3172: static int
1.29      schwarze 3173: roff_rm(ROFF_ARGS)
                   3174: {
                   3175:        const char       *name;
                   3176:        char             *cp;
1.85      schwarze 3177:        size_t            namesz;
1.29      schwarze 3178:
1.110     schwarze 3179:        cp = buf->buf + pos;
                   3180:        while (*cp != '\0') {
1.85      schwarze 3181:                name = cp;
1.110     schwarze 3182:                namesz = roff_getname(r, &cp, ln, (int)(cp - buf->buf));
1.85      schwarze 3183:                roff_setstrn(&r->strtab, name, namesz, NULL, 0, 0);
1.183     schwarze 3184:                roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
1.235     schwarze 3185:                if (name[namesz] == '\\' || name[namesz] == '\t')
1.85      schwarze 3186:                        break;
1.29      schwarze 3187:        }
1.149     schwarze 3188:        return ROFF_IGN;
1.51      schwarze 3189: }
                   3190:
1.212     schwarze 3191: static int
1.51      schwarze 3192: roff_it(ROFF_ARGS)
                   3193: {
                   3194:        int              iv;
                   3195:
                   3196:        /* Parse the number of lines. */
1.133     schwarze 3197:
                   3198:        if ( ! roff_evalnum(r, ln, buf->buf, &pos, &iv, 0)) {
1.222     schwarze 3199:                mandoc_msg(MANDOCERR_IT_NONUM,
                   3200:                    ln, ppos, "%s", buf->buf + 1);
1.149     schwarze 3201:                return ROFF_IGN;
1.51      schwarze 3202:        }
                   3203:
1.134     schwarze 3204:        while (isspace((unsigned char)buf->buf[pos]))
                   3205:                pos++;
                   3206:
                   3207:        /*
                   3208:         * Arm the input line trap.
                   3209:         * Special-casing "an-trap" is an ugly workaround to cope
                   3210:         * with DocBook stupidly fiddling with man(7) internals.
                   3211:         */
1.133     schwarze 3212:
1.51      schwarze 3213:        roffit_lines = iv;
1.134     schwarze 3214:        roffit_macro = mandoc_strdup(iv != 1 ||
                   3215:            strcmp(buf->buf + pos, "an-trap") ?
                   3216:            buf->buf + pos : "br");
1.149     schwarze 3217:        return ROFF_IGN;
1.47      schwarze 3218: }
                   3219:
1.212     schwarze 3220: static int
1.47      schwarze 3221: roff_Dd(ROFF_ARGS)
                   3222: {
1.187     schwarze 3223:        int              mask;
                   3224:        enum roff_tok    t, te;
1.99      schwarze 3225:
1.187     schwarze 3226:        switch (tok) {
                   3227:        case ROFF_Dd:
                   3228:                tok = MDOC_Dd;
                   3229:                te = MDOC_MAX;
                   3230:                if (r->format == 0)
                   3231:                        r->format = MPARSE_MDOC;
                   3232:                mask = MPARSE_MDOC | MPARSE_QUICK;
                   3233:                break;
                   3234:        case ROFF_TH:
                   3235:                tok = MAN_TH;
                   3236:                te = MAN_MAX;
                   3237:                if (r->format == 0)
                   3238:                        r->format = MPARSE_MAN;
                   3239:                mask = MPARSE_QUICK;
                   3240:                break;
                   3241:        default:
                   3242:                abort();
                   3243:        }
                   3244:        if ((r->options & mask) == 0)
                   3245:                for (t = tok; t < te; t++)
                   3246:                        roff_setstr(r, roff_name[t], NULL, 0);
1.149     schwarze 3247:        return ROFF_CONT;
1.14      schwarze 3248: }
                   3249:
1.212     schwarze 3250: static int
1.27      schwarze 3251: roff_TE(ROFF_ARGS)
                   3252: {
1.233     schwarze 3253:        r->man->flags &= ~ROFF_NONOFILL;
1.193     schwarze 3254:        if (r->tbl == NULL) {
1.222     schwarze 3255:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "TE");
1.193     schwarze 3256:                return ROFF_IGN;
                   3257:        }
1.218     schwarze 3258:        if (tbl_end(r->tbl, 0) == 0) {
1.193     schwarze 3259:                r->tbl = NULL;
1.130     schwarze 3260:                free(buf->buf);
                   3261:                buf->buf = mandoc_strdup(".sp");
                   3262:                buf->sz = 4;
1.201     schwarze 3263:                *offs = 0;
1.149     schwarze 3264:                return ROFF_REPARSE;
1.130     schwarze 3265:        }
1.193     schwarze 3266:        r->tbl = NULL;
1.149     schwarze 3267:        return ROFF_IGN;
1.27      schwarze 3268: }
                   3269:
1.212     schwarze 3270: static int
1.27      schwarze 3271: roff_T_(ROFF_ARGS)
                   3272: {
                   3273:
                   3274:        if (NULL == r->tbl)
1.222     schwarze 3275:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "T&");
1.27      schwarze 3276:        else
1.168     schwarze 3277:                tbl_restart(ln, ppos, r->tbl);
1.27      schwarze 3278:
1.149     schwarze 3279:        return ROFF_IGN;
1.27      schwarze 3280: }
                   3281:
1.102     schwarze 3282: /*
                   3283:  * Handle in-line equation delimiters.
                   3284:  */
1.212     schwarze 3285: static int
1.110     schwarze 3286: roff_eqndelim(struct roff *r, struct buf *buf, int pos)
1.41      schwarze 3287: {
1.105     schwarze 3288:        char            *cp1, *cp2;
                   3289:        const char      *bef_pr, *bef_nl, *mac, *aft_nl, *aft_pr;
1.41      schwarze 3290:
1.102     schwarze 3291:        /*
                   3292:         * Outside equations, look for an opening delimiter.
                   3293:         * If we are inside an equation, we already know it is
                   3294:         * in-line, or this function wouldn't have been called;
                   3295:         * so look for a closing delimiter.
                   3296:         */
                   3297:
1.110     schwarze 3298:        cp1 = buf->buf + pos;
1.102     schwarze 3299:        cp2 = strchr(cp1, r->eqn == NULL ?
                   3300:            r->last_eqn->odelim : r->last_eqn->cdelim);
                   3301:        if (cp2 == NULL)
1.149     schwarze 3302:                return ROFF_CONT;
1.102     schwarze 3303:
1.105     schwarze 3304:        *cp2++ = '\0';
                   3305:        bef_pr = bef_nl = aft_nl = aft_pr = "";
                   3306:
                   3307:        /* Handle preceding text, protecting whitespace. */
                   3308:
1.110     schwarze 3309:        if (*buf->buf != '\0') {
1.105     schwarze 3310:                if (r->eqn == NULL)
                   3311:                        bef_pr = "\\&";
                   3312:                bef_nl = "\n";
                   3313:        }
                   3314:
                   3315:        /*
                   3316:         * Prepare replacing the delimiter with an equation macro
                   3317:         * and drop leading white space from the equation.
                   3318:         */
1.102     schwarze 3319:
1.105     schwarze 3320:        if (r->eqn == NULL) {
                   3321:                while (*cp2 == ' ')
                   3322:                        cp2++;
                   3323:                mac = ".EQ";
                   3324:        } else
                   3325:                mac = ".EN";
                   3326:
                   3327:        /* Handle following text, protecting whitespace. */
                   3328:
                   3329:        if (*cp2 != '\0') {
                   3330:                aft_nl = "\n";
                   3331:                if (r->eqn != NULL)
                   3332:                        aft_pr = "\\&";
                   3333:        }
                   3334:
                   3335:        /* Do the actual replacement. */
                   3336:
1.110     schwarze 3337:        buf->sz = mandoc_asprintf(&cp1, "%s%s%s%s%s%s%s", buf->buf,
1.105     schwarze 3338:            bef_pr, bef_nl, mac, aft_nl, aft_pr, cp2) + 1;
1.110     schwarze 3339:        free(buf->buf);
                   3340:        buf->buf = cp1;
1.102     schwarze 3341:
                   3342:        /* Toggle the in-line state of the eqn subsystem. */
                   3343:
                   3344:        r->eqn_inline = r->eqn == NULL;
1.149     schwarze 3345:        return ROFF_REPARSE;
1.41      schwarze 3346: }
                   3347:
1.212     schwarze 3348: static int
1.107     schwarze 3349: roff_EQ(ROFF_ARGS)
1.32      schwarze 3350: {
1.191     schwarze 3351:        struct roff_node        *n;
                   3352:
1.228     schwarze 3353:        if (r->man->meta.macroset == MACROSET_MAN)
1.194     schwarze 3354:                man_breakscope(r->man, ROFF_EQ);
1.191     schwarze 3355:        n = roff_node_alloc(r->man, ln, ppos, ROFFT_EQN, TOKEN_NONE);
                   3356:        if (ln > r->man->last->line)
                   3357:                n->flags |= NODE_LINE;
1.220     schwarze 3358:        n->eqn = eqn_box_new();
1.191     schwarze 3359:        roff_node_append(r->man, n);
                   3360:        r->man->next = ROFF_NEXT_SIBLING;
1.32      schwarze 3361:
1.110     schwarze 3362:        assert(r->eqn == NULL);
1.191     schwarze 3363:        if (r->last_eqn == NULL)
1.223     schwarze 3364:                r->last_eqn = eqn_alloc();
1.191     schwarze 3365:        else
                   3366:                eqn_reset(r->last_eqn);
                   3367:        r->eqn = r->last_eqn;
                   3368:        r->eqn->node = n;
1.41      schwarze 3369:
1.110     schwarze 3370:        if (buf->buf[pos] != '\0')
1.222     schwarze 3371:                mandoc_msg(MANDOCERR_ARG_SKIP, ln, pos,
1.110     schwarze 3372:                    ".EQ %s", buf->buf + pos);
1.41      schwarze 3373:
1.149     schwarze 3374:        return ROFF_IGN;
1.32      schwarze 3375: }
                   3376:
1.212     schwarze 3377: static int
1.32      schwarze 3378: roff_EN(ROFF_ARGS)
                   3379: {
1.191     schwarze 3380:        if (r->eqn != NULL) {
                   3381:                eqn_parse(r->eqn);
                   3382:                r->eqn = NULL;
                   3383:        } else
1.222     schwarze 3384:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "EN");
1.191     schwarze 3385:        if (buf->buf[pos] != '\0')
1.222     schwarze 3386:                mandoc_msg(MANDOCERR_ARG_SKIP, ln, pos,
1.191     schwarze 3387:                    "EN %s", buf->buf + pos);
1.149     schwarze 3388:        return ROFF_IGN;
1.32      schwarze 3389: }
                   3390:
1.212     schwarze 3391: static int
1.27      schwarze 3392: roff_TS(ROFF_ARGS)
                   3393: {
1.193     schwarze 3394:        if (r->tbl != NULL) {
1.222     schwarze 3395:                mandoc_msg(MANDOCERR_BLK_BROKEN, ln, ppos, "TS breaks TS");
1.218     schwarze 3396:                tbl_end(r->tbl, 0);
1.27      schwarze 3397:        }
1.233     schwarze 3398:        r->man->flags |= ROFF_NONOFILL;
1.223     schwarze 3399:        r->tbl = tbl_alloc(ppos, ln, r->last_tbl);
1.218     schwarze 3400:        if (r->last_tbl == NULL)
1.193     schwarze 3401:                r->first_tbl = r->tbl;
                   3402:        r->last_tbl = r->tbl;
1.169     schwarze 3403:        return ROFF_IGN;
                   3404: }
                   3405:
1.212     schwarze 3406: static int
1.230     schwarze 3407: roff_noarg(ROFF_ARGS)
                   3408: {
                   3409:        if (r->man->flags & (MAN_BLINE | MAN_ELINE))
                   3410:                man_breakscope(r->man, tok);
                   3411:        if (tok == ROFF_brp)
                   3412:                tok = ROFF_br;
                   3413:        roff_elem_alloc(r->man, ln, ppos, tok);
                   3414:        if (buf->buf[pos] != '\0')
                   3415:                mandoc_msg(MANDOCERR_ARG_SKIP, ln, pos,
                   3416:                   "%s %s", roff_name[tok], buf->buf + pos);
                   3417:        if (tok == ROFF_nf)
                   3418:                r->man->flags |= ROFF_NOFILL;
                   3419:        else if (tok == ROFF_fi)
                   3420:                r->man->flags &= ~ROFF_NOFILL;
                   3421:        r->man->last->flags |= NODE_LINE | NODE_VALID | NODE_ENDED;
                   3422:        r->man->next = ROFF_NEXT_SIBLING;
                   3423:        return ROFF_IGN;
                   3424: }
                   3425:
                   3426: static int
1.169     schwarze 3427: roff_onearg(ROFF_ARGS)
                   3428: {
                   3429:        struct roff_node        *n;
                   3430:        char                    *cp;
1.177     schwarze 3431:        int                      npos;
1.169     schwarze 3432:
1.174     schwarze 3433:        if (r->man->flags & (MAN_BLINE | MAN_ELINE) &&
1.192     schwarze 3434:            (tok == ROFF_ce || tok == ROFF_rj || tok == ROFF_sp ||
                   3435:             tok == ROFF_ti))
1.174     schwarze 3436:                man_breakscope(r->man, tok);
                   3437:
1.181     schwarze 3438:        if (roffce_node != NULL && (tok == ROFF_ce || tok == ROFF_rj)) {
1.177     schwarze 3439:                r->man->last = roffce_node;
                   3440:                r->man->next = ROFF_NEXT_SIBLING;
                   3441:        }
                   3442:
1.169     schwarze 3443:        roff_elem_alloc(r->man, ln, ppos, tok);
                   3444:        n = r->man->last;
                   3445:
                   3446:        cp = buf->buf + pos;
                   3447:        if (*cp != '\0') {
                   3448:                while (*cp != '\0' && *cp != ' ')
                   3449:                        cp++;
                   3450:                while (*cp == ' ')
                   3451:                        *cp++ = '\0';
                   3452:                if (*cp != '\0')
1.222     schwarze 3453:                        mandoc_msg(MANDOCERR_ARG_EXCESS,
                   3454:                            ln, (int)(cp - buf->buf),
1.169     schwarze 3455:                            "%s ... %s", roff_name[tok], cp);
                   3456:                roff_word_alloc(r->man, ln, pos, buf->buf + pos);
1.172     schwarze 3457:        }
                   3458:
1.181     schwarze 3459:        if (tok == ROFF_ce || tok == ROFF_rj) {
                   3460:                if (r->man->last->type == ROFFT_ELEM) {
1.177     schwarze 3461:                        roff_word_alloc(r->man, ln, pos, "1");
                   3462:                        r->man->last->flags |= NODE_NOSRC;
                   3463:                }
                   3464:                npos = 0;
                   3465:                if (roff_evalnum(r, ln, r->man->last->string, &npos,
                   3466:                    &roffce_lines, 0) == 0) {
1.222     schwarze 3467:                        mandoc_msg(MANDOCERR_CE_NONUM,
                   3468:                            ln, pos, "ce %s", buf->buf + pos);
1.177     schwarze 3469:                        roffce_lines = 1;
                   3470:                }
                   3471:                if (roffce_lines < 1) {
                   3472:                        r->man->last = r->man->last->parent;
                   3473:                        roffce_node = NULL;
                   3474:                        roffce_lines = 0;
                   3475:                } else
                   3476:                        roffce_node = r->man->last->parent;
                   3477:        } else {
                   3478:                n->flags |= NODE_VALID | NODE_ENDED;
                   3479:                r->man->last = n;
                   3480:        }
                   3481:        n->flags |= NODE_LINE;
1.172     schwarze 3482:        r->man->next = ROFF_NEXT_SIBLING;
                   3483:        return ROFF_IGN;
                   3484: }
                   3485:
1.212     schwarze 3486: static int
1.172     schwarze 3487: roff_manyarg(ROFF_ARGS)
                   3488: {
                   3489:        struct roff_node        *n;
                   3490:        char                    *sp, *ep;
                   3491:
                   3492:        roff_elem_alloc(r->man, ln, ppos, tok);
                   3493:        n = r->man->last;
                   3494:
                   3495:        for (sp = ep = buf->buf + pos; *sp != '\0'; sp = ep) {
                   3496:                while (*ep != '\0' && *ep != ' ')
                   3497:                        ep++;
                   3498:                while (*ep == ' ')
                   3499:                        *ep++ = '\0';
                   3500:                roff_word_alloc(r->man, ln, sp - buf->buf, sp);
1.169     schwarze 3501:        }
                   3502:
                   3503:        n->flags |= NODE_LINE | NODE_VALID | NODE_ENDED;
                   3504:        r->man->last = n;
                   3505:        r->man->next = ROFF_NEXT_SIBLING;
1.149     schwarze 3506:        return ROFF_IGN;
1.123     schwarze 3507: }
                   3508:
1.212     schwarze 3509: static int
1.183     schwarze 3510: roff_als(ROFF_ARGS)
                   3511: {
                   3512:        char            *oldn, *newn, *end, *value;
                   3513:        size_t           oldsz, newsz, valsz;
                   3514:
                   3515:        newn = oldn = buf->buf + pos;
                   3516:        if (*newn == '\0')
                   3517:                return ROFF_IGN;
                   3518:
                   3519:        newsz = roff_getname(r, &oldn, ln, pos);
1.235     schwarze 3520:        if (newn[newsz] == '\\' || newn[newsz] == '\t' || *oldn == '\0')
1.183     schwarze 3521:                return ROFF_IGN;
                   3522:
                   3523:        end = oldn;
                   3524:        oldsz = roff_getname(r, &end, ln, oldn - buf->buf);
                   3525:        if (oldsz == 0)
                   3526:                return ROFF_IGN;
                   3527:
1.210     schwarze 3528:        valsz = mandoc_asprintf(&value, ".%.*s \\$@\\\"\n",
1.187     schwarze 3529:            (int)oldsz, oldn);
1.183     schwarze 3530:        roff_setstrn(&r->strtab, newn, newsz, value, valsz, 0);
                   3531:        roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
                   3532:        free(value);
1.236     schwarze 3533:        return ROFF_IGN;
                   3534: }
                   3535:
                   3536: /*
                   3537:  * The .break request only makes sense inside conditionals,
                   3538:  * and that case is already handled in roff_cond_sub().
                   3539:  */
                   3540: static int
                   3541: roff_break(ROFF_ARGS)
                   3542: {
                   3543:        mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, pos, "break");
1.168     schwarze 3544:        return ROFF_IGN;
1.27      schwarze 3545: }
                   3546:
1.212     schwarze 3547: static int
1.48      schwarze 3548: roff_cc(ROFF_ARGS)
                   3549: {
                   3550:        const char      *p;
                   3551:
1.110     schwarze 3552:        p = buf->buf + pos;
1.48      schwarze 3553:
1.110     schwarze 3554:        if (*p == '\0' || (r->control = *p++) == '.')
1.175     schwarze 3555:                r->control = '\0';
1.48      schwarze 3556:
1.110     schwarze 3557:        if (*p != '\0')
1.222     schwarze 3558:                mandoc_msg(MANDOCERR_ARG_EXCESS,
1.132     schwarze 3559:                    ln, p - buf->buf, "cc ... %s", p);
1.48      schwarze 3560:
1.213     schwarze 3561:        return ROFF_IGN;
                   3562: }
                   3563:
                   3564: static int
                   3565: roff_char(ROFF_ARGS)
                   3566: {
                   3567:        const char      *p, *kp, *vp;
                   3568:        size_t           ksz, vsz;
                   3569:        int              font;
                   3570:
                   3571:        /* Parse the character to be replaced. */
                   3572:
                   3573:        kp = buf->buf + pos;
                   3574:        p = kp + 1;
                   3575:        if (*kp == '\0' || (*kp == '\\' &&
                   3576:             mandoc_escape(&p, NULL, NULL) != ESCAPE_SPECIAL) ||
                   3577:            (*p != ' ' && *p != '\0')) {
1.222     schwarze 3578:                mandoc_msg(MANDOCERR_CHAR_ARG, ln, pos, "char %s", kp);
1.213     schwarze 3579:                return ROFF_IGN;
                   3580:        }
                   3581:        ksz = p - kp;
                   3582:        while (*p == ' ')
                   3583:                p++;
                   3584:
                   3585:        /*
                   3586:         * If the replacement string contains a font escape sequence,
                   3587:         * we have to restore the font at the end.
                   3588:         */
                   3589:
                   3590:        vp = p;
                   3591:        vsz = strlen(p);
                   3592:        font = 0;
                   3593:        while (*p != '\0') {
                   3594:                if (*p++ != '\\')
                   3595:                        continue;
                   3596:                switch (mandoc_escape(&p, NULL, NULL)) {
                   3597:                case ESCAPE_FONT:
                   3598:                case ESCAPE_FONTROMAN:
                   3599:                case ESCAPE_FONTITALIC:
                   3600:                case ESCAPE_FONTBOLD:
                   3601:                case ESCAPE_FONTBI:
1.250     schwarze 3602:                case ESCAPE_FONTCR:
                   3603:                case ESCAPE_FONTCB:
                   3604:                case ESCAPE_FONTCI:
1.213     schwarze 3605:                case ESCAPE_FONTPREV:
                   3606:                        font++;
                   3607:                        break;
                   3608:                default:
                   3609:                        break;
                   3610:                }
                   3611:        }
                   3612:        if (font > 1)
1.222     schwarze 3613:                mandoc_msg(MANDOCERR_CHAR_FONT,
                   3614:                    ln, (int)(vp - buf->buf), "%s", vp);
1.213     schwarze 3615:
                   3616:        /*
                   3617:         * Approximate the effect of .char using the .tr tables.
                   3618:         * XXX In groff, .char and .tr interact differently.
                   3619:         */
                   3620:
                   3621:        if (ksz == 1) {
                   3622:                if (r->xtab == NULL)
                   3623:                        r->xtab = mandoc_calloc(128, sizeof(*r->xtab));
                   3624:                assert((unsigned int)*kp < 128);
                   3625:                free(r->xtab[(int)*kp].p);
                   3626:                r->xtab[(int)*kp].sz = mandoc_asprintf(&r->xtab[(int)*kp].p,
                   3627:                    "%s%s", vp, font ? "\fP" : "");
                   3628:        } else {
                   3629:                roff_setstrn(&r->xmbtab, kp, ksz, vp, vsz, 0);
                   3630:                if (font)
                   3631:                        roff_setstrn(&r->xmbtab, kp, ksz, "\\fP", 3, 1);
                   3632:        }
1.149     schwarze 3633:        return ROFF_IGN;
1.48      schwarze 3634: }
                   3635:
1.212     schwarze 3636: static int
1.175     schwarze 3637: roff_ec(ROFF_ARGS)
                   3638: {
                   3639:        const char      *p;
                   3640:
                   3641:        p = buf->buf + pos;
                   3642:        if (*p == '\0')
                   3643:                r->escape = '\\';
                   3644:        else {
                   3645:                r->escape = *p;
                   3646:                if (*++p != '\0')
1.222     schwarze 3647:                        mandoc_msg(MANDOCERR_ARG_EXCESS, ln,
                   3648:                            (int)(p - buf->buf), "ec ... %s", p);
1.175     schwarze 3649:        }
                   3650:        return ROFF_IGN;
                   3651: }
                   3652:
1.212     schwarze 3653: static int
1.175     schwarze 3654: roff_eo(ROFF_ARGS)
                   3655: {
                   3656:        r->escape = '\0';
                   3657:        if (buf->buf[pos] != '\0')
1.222     schwarze 3658:                mandoc_msg(MANDOCERR_ARG_SKIP,
1.175     schwarze 3659:                    ln, pos, "eo %s", buf->buf + pos);
1.256     schwarze 3660:        return ROFF_IGN;
                   3661: }
                   3662:
                   3663: static int
                   3664: roff_mc(ROFF_ARGS)
                   3665: {
                   3666:        struct roff_node        *n;
                   3667:        char                    *cp;
                   3668:
                   3669:        /* Parse the first argument. */
                   3670:
                   3671:        cp = buf->buf + pos;
                   3672:        if (*cp != '\0')
                   3673:                cp++;
                   3674:        if (buf->buf[pos] == '\\') {
                   3675:                switch (mandoc_escape((const char **)&cp, NULL, NULL)) {
                   3676:                case ESCAPE_SPECIAL:
                   3677:                case ESCAPE_UNICODE:
                   3678:                case ESCAPE_NUMBERED:
                   3679:                        break;
                   3680:                default:
                   3681:                        *cp = '\0';
                   3682:                        mandoc_msg(MANDOCERR_MC_ESC, ln, pos,
                   3683:                            "mc %s", buf->buf + pos);
                   3684:                        buf->buf[pos] = '\0';
                   3685:                        break;
                   3686:                }
                   3687:        }
                   3688:
                   3689:        /* Ignore additional arguments. */
                   3690:
                   3691:        while (*cp == ' ')
                   3692:                *cp++ = '\0';
                   3693:        if (*cp != '\0') {
                   3694:                mandoc_msg(MANDOCERR_MC_DIST, ln, (int)(cp - buf->buf),
                   3695:                    "mc ... %s", cp);
                   3696:                *cp = '\0';
                   3697:        }
                   3698:
                   3699:        /* Create the .mc node. */
                   3700:
                   3701:        roff_elem_alloc(r->man, ln, ppos, tok);
                   3702:        n = r->man->last;
                   3703:        if (buf->buf[pos] != '\0')
                   3704:                roff_word_alloc(r->man, ln, pos, buf->buf + pos);
                   3705:        n->flags |= NODE_LINE | NODE_VALID | NODE_ENDED;
                   3706:        r->man->last = n;
                   3707:        r->man->next = ROFF_NEXT_SIBLING;
1.175     schwarze 3708:        return ROFF_IGN;
1.202     schwarze 3709: }
                   3710:
1.212     schwarze 3711: static int
1.202     schwarze 3712: roff_nop(ROFF_ARGS)
                   3713: {
                   3714:        while (buf->buf[pos] == ' ')
                   3715:                pos++;
                   3716:        *offs = pos;
                   3717:        return ROFF_RERUN;
1.175     schwarze 3718: }
                   3719:
1.212     schwarze 3720: static int
1.42      schwarze 3721: roff_tr(ROFF_ARGS)
                   3722: {
                   3723:        const char      *p, *first, *second;
                   3724:        size_t           fsz, ssz;
                   3725:        enum mandoc_esc  esc;
                   3726:
1.110     schwarze 3727:        p = buf->buf + pos;
1.42      schwarze 3728:
1.110     schwarze 3729:        if (*p == '\0') {
1.222     schwarze 3730:                mandoc_msg(MANDOCERR_REQ_EMPTY, ln, ppos, "tr");
1.149     schwarze 3731:                return ROFF_IGN;
1.42      schwarze 3732:        }
                   3733:
1.110     schwarze 3734:        while (*p != '\0') {
1.42      schwarze 3735:                fsz = ssz = 1;
                   3736:
                   3737:                first = p++;
1.110     schwarze 3738:                if (*first == '\\') {
1.42      schwarze 3739:                        esc = mandoc_escape(&p, NULL, NULL);
1.110     schwarze 3740:                        if (esc == ESCAPE_ERROR) {
1.222     schwarze 3741:                                mandoc_msg(MANDOCERR_ESC_BAD, ln,
                   3742:                                    (int)(p - buf->buf), "%s", first);
1.149     schwarze 3743:                                return ROFF_IGN;
1.42      schwarze 3744:                        }
                   3745:                        fsz = (size_t)(p - first);
                   3746:                }
                   3747:
                   3748:                second = p++;
1.110     schwarze 3749:                if (*second == '\\') {
1.42      schwarze 3750:                        esc = mandoc_escape(&p, NULL, NULL);
1.110     schwarze 3751:                        if (esc == ESCAPE_ERROR) {
1.222     schwarze 3752:                                mandoc_msg(MANDOCERR_ESC_BAD, ln,
                   3753:                                    (int)(p - buf->buf), "%s", second);
1.149     schwarze 3754:                                return ROFF_IGN;
1.42      schwarze 3755:                        }
                   3756:                        ssz = (size_t)(p - second);
1.110     schwarze 3757:                } else if (*second == '\0') {
1.222     schwarze 3758:                        mandoc_msg(MANDOCERR_TR_ODD, ln,
                   3759:                            (int)(first - buf->buf), "tr %s", first);
1.42      schwarze 3760:                        second = " ";
                   3761:                        p--;
                   3762:                }
                   3763:
                   3764:                if (fsz > 1) {
1.80      schwarze 3765:                        roff_setstrn(&r->xmbtab, first, fsz,
                   3766:                            second, ssz, 0);
1.42      schwarze 3767:                        continue;
                   3768:                }
                   3769:
1.110     schwarze 3770:                if (r->xtab == NULL)
1.80      schwarze 3771:                        r->xtab = mandoc_calloc(128,
                   3772:                            sizeof(struct roffstr));
1.42      schwarze 3773:
                   3774:                free(r->xtab[(int)*first].p);
                   3775:                r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
                   3776:                r->xtab[(int)*first].sz = ssz;
                   3777:        }
                   3778:
1.149     schwarze 3779:        return ROFF_IGN;
1.42      schwarze 3780: }
                   3781:
1.211     schwarze 3782: /*
                   3783:  * Implementation of the .return request.
                   3784:  * There is no need to call roff_userret() from here.
                   3785:  * The read module will call that after rewinding the reader stack
                   3786:  * to the place from where the current macro was called.
                   3787:  */
1.212     schwarze 3788: static int
1.211     schwarze 3789: roff_return(ROFF_ARGS)
                   3790: {
                   3791:        if (r->mstackpos >= 0)
1.212     schwarze 3792:                return ROFF_IGN | ROFF_USERRET;
1.211     schwarze 3793:
1.222     schwarze 3794:        mandoc_msg(MANDOCERR_REQ_NOMAC, ln, ppos, "return");
1.211     schwarze 3795:        return ROFF_IGN;
                   3796: }
                   3797:
1.212     schwarze 3798: static int
1.178     schwarze 3799: roff_rn(ROFF_ARGS)
                   3800: {
                   3801:        const char      *value;
                   3802:        char            *oldn, *newn, *end;
                   3803:        size_t           oldsz, newsz;
1.187     schwarze 3804:        int              deftype;
1.178     schwarze 3805:
                   3806:        oldn = newn = buf->buf + pos;
                   3807:        if (*oldn == '\0')
                   3808:                return ROFF_IGN;
                   3809:
                   3810:        oldsz = roff_getname(r, &newn, ln, pos);
1.235     schwarze 3811:        if (oldn[oldsz] == '\\' || oldn[oldsz] == '\t' || *newn == '\0')
1.178     schwarze 3812:                return ROFF_IGN;
                   3813:
                   3814:        end = newn;
                   3815:        newsz = roff_getname(r, &end, ln, newn - buf->buf);
                   3816:        if (newsz == 0)
                   3817:                return ROFF_IGN;
                   3818:
1.187     schwarze 3819:        deftype = ROFFDEF_ANY;
                   3820:        value = roff_getstrn(r, oldn, oldsz, &deftype);
                   3821:        switch (deftype) {
                   3822:        case ROFFDEF_USER:
1.178     schwarze 3823:                roff_setstrn(&r->strtab, newn, newsz, value, strlen(value), 0);
                   3824:                roff_setstrn(&r->strtab, oldn, oldsz, NULL, 0, 0);
                   3825:                roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
1.187     schwarze 3826:                break;
                   3827:        case ROFFDEF_PRE:
                   3828:                roff_setstrn(&r->strtab, newn, newsz, value, strlen(value), 0);
                   3829:                roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
                   3830:                break;
                   3831:        case ROFFDEF_REN:
1.178     schwarze 3832:                roff_setstrn(&r->rentab, newn, newsz, value, strlen(value), 0);
                   3833:                roff_setstrn(&r->rentab, oldn, oldsz, NULL, 0, 0);
1.187     schwarze 3834:                roff_setstrn(&r->strtab, newn, newsz, NULL, 0, 0);
                   3835:                break;
                   3836:        case ROFFDEF_STD:
1.178     schwarze 3837:                roff_setstrn(&r->rentab, newn, newsz, oldn, oldsz, 0);
1.187     schwarze 3838:                roff_setstrn(&r->strtab, newn, newsz, NULL, 0, 0);
                   3839:                break;
                   3840:        default:
                   3841:                roff_setstrn(&r->strtab, newn, newsz, NULL, 0, 0);
                   3842:                roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
                   3843:                break;
                   3844:        }
1.178     schwarze 3845:        return ROFF_IGN;
                   3846: }
                   3847:
1.212     schwarze 3848: static int
1.211     schwarze 3849: roff_shift(ROFF_ARGS)
                   3850: {
                   3851:        struct mctx     *ctx;
1.254     schwarze 3852:        int              argpos, levels, i;
1.211     schwarze 3853:
1.254     schwarze 3854:        argpos = pos;
1.211     schwarze 3855:        levels = 1;
                   3856:        if (buf->buf[pos] != '\0' &&
                   3857:            roff_evalnum(r, ln, buf->buf, &pos, &levels, 0) == 0) {
1.222     schwarze 3858:                mandoc_msg(MANDOCERR_CE_NONUM,
1.211     schwarze 3859:                    ln, pos, "shift %s", buf->buf + pos);
                   3860:                levels = 1;
                   3861:        }
                   3862:        if (r->mstackpos < 0) {
1.222     schwarze 3863:                mandoc_msg(MANDOCERR_REQ_NOMAC, ln, ppos, "shift");
1.211     schwarze 3864:                return ROFF_IGN;
                   3865:        }
                   3866:        ctx = r->mstack + r->mstackpos;
                   3867:        if (levels > ctx->argc) {
1.222     schwarze 3868:                mandoc_msg(MANDOCERR_SHIFT,
1.254     schwarze 3869:                    ln, argpos, "%d, but max is %d", levels, ctx->argc);
1.211     schwarze 3870:                levels = ctx->argc;
1.254     schwarze 3871:        }
                   3872:        if (levels < 0) {
                   3873:                mandoc_msg(MANDOCERR_ARG_NEG, ln, argpos, "shift %d", levels);
                   3874:                levels = 0;
1.211     schwarze 3875:        }
                   3876:        if (levels == 0)
                   3877:                return ROFF_IGN;
                   3878:        for (i = 0; i < levels; i++)
                   3879:                free(ctx->argv[i]);
                   3880:        ctx->argc -= levels;
                   3881:        for (i = 0; i < ctx->argc; i++)
                   3882:                ctx->argv[i] = ctx->argv[i + levels];
                   3883:        return ROFF_IGN;
                   3884: }
                   3885:
1.212     schwarze 3886: static int
1.14      schwarze 3887: roff_so(ROFF_ARGS)
                   3888: {
1.121     schwarze 3889:        char *name, *cp;
1.15      schwarze 3890:
1.110     schwarze 3891:        name = buf->buf + pos;
1.222     schwarze 3892:        mandoc_msg(MANDOCERR_SO, ln, ppos, "so %s", name);
1.14      schwarze 3893:
1.22      schwarze 3894:        /*
                   3895:         * Handle `so'.  Be EXTREMELY careful, as we shouldn't be
                   3896:         * opening anything that's not in our cwd or anything beneath
                   3897:         * it.  Thus, explicitly disallow traversing up the file-system
                   3898:         * or using absolute paths.
                   3899:         */
                   3900:
1.110     schwarze 3901:        if (*name == '/' || strstr(name, "../") || strstr(name, "/..")) {
1.222     schwarze 3902:                mandoc_msg(MANDOCERR_SO_PATH, ln, ppos, ".so %s", name);
1.121     schwarze 3903:                buf->sz = mandoc_asprintf(&cp,
                   3904:                    ".sp\nSee the file %s.\n.sp", name) + 1;
                   3905:                free(buf->buf);
                   3906:                buf->buf = cp;
                   3907:                *offs = 0;
1.149     schwarze 3908:                return ROFF_REPARSE;
1.14      schwarze 3909:        }
                   3910:
                   3911:        *offs = pos;
1.149     schwarze 3912:        return ROFF_SO;
1.7       schwarze 3913: }
                   3914:
1.138     schwarze 3915: /* --- user defined strings and macros ------------------------------------ */
                   3916:
1.212     schwarze 3917: static int
1.16      schwarze 3918: roff_userdef(ROFF_ARGS)
1.12      schwarze 3919: {
1.211     schwarze 3920:        struct mctx      *ctx;
                   3921:        char             *arg, *ap, *dst, *src;
                   3922:        size_t            sz;
                   3923:
1.237     schwarze 3924:        /* If the macro is empty, ignore it altogether. */
                   3925:
                   3926:        if (*r->current_string == '\0')
                   3927:                return ROFF_IGN;
                   3928:
1.211     schwarze 3929:        /* Initialize a new macro stack context. */
                   3930:
                   3931:        if (++r->mstackpos == r->mstacksz) {
                   3932:                r->mstack = mandoc_recallocarray(r->mstack,
                   3933:                    r->mstacksz, r->mstacksz + 8, sizeof(*r->mstack));
                   3934:                r->mstacksz += 8;
                   3935:        }
                   3936:        ctx = r->mstack + r->mstackpos;
                   3937:        ctx->argc = 0;
                   3938:
                   3939:        /*
                   3940:         * Collect pointers to macro argument strings,
                   3941:         * NUL-terminating them and escaping quotes.
                   3942:         */
                   3943:
                   3944:        src = buf->buf + pos;
                   3945:        while (*src != '\0') {
                   3946:                if (ctx->argc == ctx->argsz) {
                   3947:                        ctx->argsz += 8;
                   3948:                        ctx->argv = mandoc_reallocarray(ctx->argv,
                   3949:                            ctx->argsz, sizeof(*ctx->argv));
                   3950:                }
1.227     schwarze 3951:                arg = roff_getarg(r, &src, ln, &pos);
1.211     schwarze 3952:                sz = 1;  /* For the terminating NUL. */
                   3953:                for (ap = arg; *ap != '\0'; ap++)
                   3954:                        sz += *ap == '"' ? 4 : 1;
                   3955:                ctx->argv[ctx->argc++] = dst = mandoc_malloc(sz);
                   3956:                for (ap = arg; *ap != '\0'; ap++) {
                   3957:                        if (*ap == '"') {
                   3958:                                memcpy(dst, "\\(dq", 4);
                   3959:                                dst += 4;
                   3960:                        } else
                   3961:                                *dst++ = *ap;
1.16      schwarze 3962:                }
1.211     schwarze 3963:                *dst = '\0';
1.227     schwarze 3964:                free(arg);
1.209     schwarze 3965:        }
                   3966:
1.211     schwarze 3967:        /* Replace the macro invocation by the macro definition. */
1.135     schwarze 3968:
1.110     schwarze 3969:        free(buf->buf);
1.211     schwarze 3970:        buf->buf = mandoc_strdup(r->current_string);
                   3971:        buf->sz = strlen(buf->buf) + 1;
1.120     schwarze 3972:        *offs = 0;
1.16      schwarze 3973:
1.237     schwarze 3974:        return buf->buf[buf->sz - 2] == '\n' ?
1.212     schwarze 3975:            ROFF_REPARSE | ROFF_USERCALL : ROFF_IGN | ROFF_APPEND;
1.12      schwarze 3976: }
1.28      schwarze 3977:
1.178     schwarze 3978: /*
                   3979:  * Calling a high-level macro that was renamed with .rn.
                   3980:  * r->current_string has already been set up by roff_parse().
                   3981:  */
1.212     schwarze 3982: static int
1.178     schwarze 3983: roff_renamed(ROFF_ARGS)
                   3984: {
                   3985:        char    *nbuf;
                   3986:
1.187     schwarze 3987:        buf->sz = mandoc_asprintf(&nbuf, ".%s%s%s", r->current_string,
                   3988:            buf->buf[pos] == '\0' ? "" : " ", buf->buf + pos) + 1;
1.178     schwarze 3989:        free(buf->buf);
                   3990:        buf->buf = nbuf;
1.201     schwarze 3991:        *offs = 0;
1.178     schwarze 3992:        return ROFF_CONT;
                   3993: }
                   3994:
1.234     schwarze 3995: /*
                   3996:  * Measure the length in bytes of the roff identifier at *cpp
                   3997:  * and advance the pointer to the next word.
                   3998:  */
1.85      schwarze 3999: static size_t
1.28      schwarze 4000: roff_getname(struct roff *r, char **cpp, int ln, int pos)
                   4001: {
                   4002:        char     *name, *cp;
1.85      schwarze 4003:        size_t    namesz;
1.28      schwarze 4004:
                   4005:        name = *cpp;
1.234     schwarze 4006:        if (*name == '\0')
1.149     schwarze 4007:                return 0;
1.28      schwarze 4008:
1.234     schwarze 4009:        /* Advance cp to the byte after the end of the name. */
                   4010:
1.85      schwarze 4011:        for (cp = name; 1; cp++) {
1.234     schwarze 4012:                namesz = cp - name;
1.235     schwarze 4013:                if (*cp == '\0')
1.85      schwarze 4014:                        break;
1.235     schwarze 4015:                if (*cp == ' ' || *cp == '\t') {
                   4016:                        cp++;
                   4017:                        break;
                   4018:                }
1.234     schwarze 4019:                if (*cp != '\\')
1.28      schwarze 4020:                        continue;
1.234     schwarze 4021:                if (cp[1] == '{' || cp[1] == '}')
1.88      schwarze 4022:                        break;
1.234     schwarze 4023:                if (*++cp == '\\')
1.28      schwarze 4024:                        continue;
1.222     schwarze 4025:                mandoc_msg(MANDOCERR_NAMESC, ln, pos,
1.97      schwarze 4026:                    "%.*s", (int)(cp - name + 1), name);
1.85      schwarze 4027:                mandoc_escape((const char **)&cp, NULL, NULL);
                   4028:                break;
1.28      schwarze 4029:        }
                   4030:
                   4031:        /* Read past spaces. */
1.234     schwarze 4032:
                   4033:        while (*cp == ' ')
1.28      schwarze 4034:                cp++;
                   4035:
                   4036:        *cpp = cp;
1.149     schwarze 4037:        return namesz;
1.28      schwarze 4038: }
                   4039:
1.16      schwarze 4040: /*
                   4041:  * Store *string into the user-defined string called *name.
                   4042:  * To clear an existing entry, call with (*r, *name, NULL, 0).
1.66      schwarze 4043:  * append == 0: replace mode
                   4044:  * append == 1: single-line append mode
                   4045:  * append == 2: multiline append mode, append '\n' after each call
1.16      schwarze 4046:  */
1.8       schwarze 4047: static void
1.16      schwarze 4048: roff_setstr(struct roff *r, const char *name, const char *string,
1.66      schwarze 4049:        int append)
1.7       schwarze 4050: {
1.187     schwarze 4051:        size_t   namesz;
1.42      schwarze 4052:
1.187     schwarze 4053:        namesz = strlen(name);
                   4054:        roff_setstrn(&r->strtab, name, namesz, string,
1.80      schwarze 4055:            string ? strlen(string) : 0, append);
1.187     schwarze 4056:        roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
1.42      schwarze 4057: }
                   4058:
                   4059: static void
                   4060: roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
1.66      schwarze 4061:                const char *string, size_t stringsz, int append)
1.42      schwarze 4062: {
                   4063:        struct roffkv   *n;
                   4064:        char            *c;
                   4065:        int              i;
                   4066:        size_t           oldch, newch;
1.7       schwarze 4067:
1.16      schwarze 4068:        /* Search for an existing string with the same name. */
1.42      schwarze 4069:        n = *r;
                   4070:
1.84      schwarze 4071:        while (n && (namesz != n->key.sz ||
                   4072:                        strncmp(n->key.p, name, namesz)))
1.7       schwarze 4073:                n = n->next;
1.8       schwarze 4074:
                   4075:        if (NULL == n) {
1.16      schwarze 4076:                /* Create a new string table entry. */
1.42      schwarze 4077:                n = mandoc_malloc(sizeof(struct roffkv));
                   4078:                n->key.p = mandoc_strndup(name, namesz);
                   4079:                n->key.sz = namesz;
                   4080:                n->val.p = NULL;
                   4081:                n->val.sz = 0;
                   4082:                n->next = *r;
                   4083:                *r = n;
1.66      schwarze 4084:        } else if (0 == append) {
1.42      schwarze 4085:                free(n->val.p);
                   4086:                n->val.p = NULL;
                   4087:                n->val.sz = 0;
1.16      schwarze 4088:        }
                   4089:
                   4090:        if (NULL == string)
                   4091:                return;
                   4092:
                   4093:        /*
                   4094:         * One additional byte for the '\n' in multiline mode,
                   4095:         * and one for the terminating '\0'.
                   4096:         */
1.66      schwarze 4097:        newch = stringsz + (1 < append ? 2u : 1u);
1.42      schwarze 4098:
                   4099:        if (NULL == n->val.p) {
                   4100:                n->val.p = mandoc_malloc(newch);
                   4101:                *n->val.p = '\0';
1.16      schwarze 4102:                oldch = 0;
                   4103:        } else {
1.42      schwarze 4104:                oldch = n->val.sz;
                   4105:                n->val.p = mandoc_realloc(n->val.p, oldch + newch);
1.16      schwarze 4106:        }
                   4107:
                   4108:        /* Skip existing content in the destination buffer. */
1.42      schwarze 4109:        c = n->val.p + (int)oldch;
1.16      schwarze 4110:
                   4111:        /* Append new content to the destination buffer. */
1.42      schwarze 4112:        i = 0;
                   4113:        while (i < (int)stringsz) {
1.16      schwarze 4114:                /*
                   4115:                 * Rudimentary roff copy mode:
                   4116:                 * Handle escaped backslashes.
                   4117:                 */
1.42      schwarze 4118:                if ('\\' == string[i] && '\\' == string[i + 1])
                   4119:                        i++;
                   4120:                *c++ = string[i++];
1.16      schwarze 4121:        }
1.8       schwarze 4122:
1.16      schwarze 4123:        /* Append terminating bytes. */
1.66      schwarze 4124:        if (1 < append)
1.16      schwarze 4125:                *c++ = '\n';
1.42      schwarze 4126:
1.16      schwarze 4127:        *c = '\0';
1.42      schwarze 4128:        n->val.sz = (int)(c - n->val.p);
1.7       schwarze 4129: }
                   4130:
1.8       schwarze 4131: static const char *
1.197     schwarze 4132: roff_getstrn(struct roff *r, const char *name, size_t len,
1.187     schwarze 4133:     int *deftype)
1.7       schwarze 4134: {
1.187     schwarze 4135:        const struct roffkv     *n;
1.197     schwarze 4136:        int                      found, i;
1.187     schwarze 4137:        enum roff_tok            tok;
                   4138:
1.197     schwarze 4139:        found = 0;
                   4140:        for (n = r->strtab; n != NULL; n = n->next) {
                   4141:                if (strncmp(name, n->key.p, len) != 0 ||
                   4142:                    n->key.p[len] != '\0' || n->val.p == NULL)
                   4143:                        continue;
                   4144:                if (*deftype & ROFFDEF_USER) {
                   4145:                        *deftype = ROFFDEF_USER;
                   4146:                        return n->val.p;
                   4147:                } else {
                   4148:                        found = 1;
                   4149:                        break;
                   4150:                }
                   4151:        }
                   4152:        for (n = r->rentab; n != NULL; n = n->next) {
                   4153:                if (strncmp(name, n->key.p, len) != 0 ||
                   4154:                    n->key.p[len] != '\0' || n->val.p == NULL)
                   4155:                        continue;
                   4156:                if (*deftype & ROFFDEF_REN) {
                   4157:                        *deftype = ROFFDEF_REN;
                   4158:                        return n->val.p;
                   4159:                } else {
                   4160:                        found = 1;
                   4161:                        break;
1.187     schwarze 4162:                }
                   4163:        }
1.197     schwarze 4164:        for (i = 0; i < PREDEFS_MAX; i++) {
                   4165:                if (strncmp(name, predefs[i].name, len) != 0 ||
                   4166:                    predefs[i].name[len] != '\0')
                   4167:                        continue;
                   4168:                if (*deftype & ROFFDEF_PRE) {
                   4169:                        *deftype = ROFFDEF_PRE;
                   4170:                        return predefs[i].str;
                   4171:                } else {
                   4172:                        found = 1;
                   4173:                        break;
1.187     schwarze 4174:                }
                   4175:        }
1.228     schwarze 4176:        if (r->man->meta.macroset != MACROSET_MAN) {
1.197     schwarze 4177:                for (tok = MDOC_Dd; tok < MDOC_MAX; tok++) {
                   4178:                        if (strncmp(name, roff_name[tok], len) != 0 ||
                   4179:                            roff_name[tok][len] != '\0')
                   4180:                                continue;
                   4181:                        if (*deftype & ROFFDEF_STD) {
                   4182:                                *deftype = ROFFDEF_STD;
                   4183:                                return NULL;
                   4184:                        } else {
                   4185:                                found = 1;
                   4186:                                break;
1.187     schwarze 4187:                        }
                   4188:                }
                   4189:        }
1.228     schwarze 4190:        if (r->man->meta.macroset != MACROSET_MDOC) {
1.197     schwarze 4191:                for (tok = MAN_TH; tok < MAN_MAX; tok++) {
                   4192:                        if (strncmp(name, roff_name[tok], len) != 0 ||
                   4193:                            roff_name[tok][len] != '\0')
                   4194:                                continue;
                   4195:                        if (*deftype & ROFFDEF_STD) {
                   4196:                                *deftype = ROFFDEF_STD;
                   4197:                                return NULL;
                   4198:                        } else {
                   4199:                                found = 1;
                   4200:                                break;
1.187     schwarze 4201:                        }
                   4202:                }
1.197     schwarze 4203:        }
                   4204:
                   4205:        if (found == 0 && *deftype != ROFFDEF_ANY) {
                   4206:                if (*deftype & ROFFDEF_REN) {
                   4207:                        /*
                   4208:                         * This might still be a request,
                   4209:                         * so do not treat it as undefined yet.
                   4210:                         */
                   4211:                        *deftype = ROFFDEF_UNDEF;
                   4212:                        return NULL;
1.187     schwarze 4213:                }
1.197     schwarze 4214:
                   4215:                /* Using an undefined string defines it to be empty. */
                   4216:
                   4217:                roff_setstrn(&r->strtab, name, len, "", 0, 0);
                   4218:                roff_setstrn(&r->rentab, name, len, NULL, 0, 0);
1.187     schwarze 4219:        }
1.197     schwarze 4220:
1.187     schwarze 4221:        *deftype = 0;
1.149     schwarze 4222:        return NULL;
1.7       schwarze 4223: }
                   4224:
1.8       schwarze 4225: static void
1.42      schwarze 4226: roff_freestr(struct roffkv *r)
1.7       schwarze 4227: {
1.42      schwarze 4228:        struct roffkv    *n, *nn;
1.7       schwarze 4229:
1.42      schwarze 4230:        for (n = r; n; n = nn) {
                   4231:                free(n->key.p);
                   4232:                free(n->val.p);
1.7       schwarze 4233:                nn = n->next;
                   4234:                free(n);
                   4235:        }
1.27      schwarze 4236: }
1.138     schwarze 4237:
                   4238: /* --- accessors and utility functions ------------------------------------ */
1.42      schwarze 4239:
                   4240: /*
                   4241:  * Duplicate an input string, making the appropriate character
                   4242:  * conversations (as stipulated by `tr') along the way.
                   4243:  * Returns a heap-allocated string with all the replacements made.
                   4244:  */
                   4245: char *
                   4246: roff_strdup(const struct roff *r, const char *p)
                   4247: {
                   4248:        const struct roffkv *cp;
                   4249:        char            *res;
                   4250:        const char      *pp;
                   4251:        size_t           ssz, sz;
                   4252:        enum mandoc_esc  esc;
                   4253:
                   4254:        if (NULL == r->xmbtab && NULL == r->xtab)
1.149     schwarze 4255:                return mandoc_strdup(p);
1.42      schwarze 4256:        else if ('\0' == *p)
1.149     schwarze 4257:                return mandoc_strdup("");
1.42      schwarze 4258:
                   4259:        /*
                   4260:         * Step through each character looking for term matches
                   4261:         * (remember that a `tr' can be invoked with an escape, which is
                   4262:         * a glyph but the escape is multi-character).
                   4263:         * We only do this if the character hash has been initialised
                   4264:         * and the string is >0 length.
                   4265:         */
                   4266:
                   4267:        res = NULL;
                   4268:        ssz = 0;
                   4269:
                   4270:        while ('\0' != *p) {
1.161     schwarze 4271:                assert((unsigned int)*p < 128);
                   4272:                if ('\\' != *p && r->xtab && r->xtab[(unsigned int)*p].p) {
1.42      schwarze 4273:                        sz = r->xtab[(int)*p].sz;
                   4274:                        res = mandoc_realloc(res, ssz + sz + 1);
                   4275:                        memcpy(res + ssz, r->xtab[(int)*p].p, sz);
                   4276:                        ssz += sz;
                   4277:                        p++;
                   4278:                        continue;
                   4279:                } else if ('\\' != *p) {
                   4280:                        res = mandoc_realloc(res, ssz + 2);
                   4281:                        res[ssz++] = *p++;
                   4282:                        continue;
                   4283:                }
                   4284:
                   4285:                /* Search for term matches. */
                   4286:                for (cp = r->xmbtab; cp; cp = cp->next)
                   4287:                        if (0 == strncmp(p, cp->key.p, cp->key.sz))
                   4288:                                break;
                   4289:
                   4290:                if (NULL != cp) {
                   4291:                        /*
                   4292:                         * A match has been found.
                   4293:                         * Append the match to the array and move
                   4294:                         * forward by its keysize.
                   4295:                         */
1.80      schwarze 4296:                        res = mandoc_realloc(res,
                   4297:                            ssz + cp->val.sz + 1);
1.42      schwarze 4298:                        memcpy(res + ssz, cp->val.p, cp->val.sz);
                   4299:                        ssz += cp->val.sz;
                   4300:                        p += (int)cp->key.sz;
                   4301:                        continue;
                   4302:                }
                   4303:
                   4304:                /*
                   4305:                 * Handle escapes carefully: we need to copy
                   4306:                 * over just the escape itself, or else we might
                   4307:                 * do replacements within the escape itself.
                   4308:                 * Make sure to pass along the bogus string.
                   4309:                 */
                   4310:                pp = p++;
                   4311:                esc = mandoc_escape(&p, NULL, NULL);
                   4312:                if (ESCAPE_ERROR == esc) {
                   4313:                        sz = strlen(pp);
                   4314:                        res = mandoc_realloc(res, ssz + sz + 1);
                   4315:                        memcpy(res + ssz, pp, sz);
                   4316:                        break;
                   4317:                }
1.80      schwarze 4318:                /*
                   4319:                 * We bail out on bad escapes.
1.42      schwarze 4320:                 * No need to warn: we already did so when
1.227     schwarze 4321:                 * roff_expand() was called.
1.42      schwarze 4322:                 */
                   4323:                sz = (int)(p - pp);
                   4324:                res = mandoc_realloc(res, ssz + sz + 1);
                   4325:                memcpy(res + ssz, pp, sz);
                   4326:                ssz += sz;
                   4327:        }
                   4328:
                   4329:        res[(int)ssz] = '\0';
1.149     schwarze 4330:        return res;
1.99      schwarze 4331: }
                   4332:
                   4333: int
                   4334: roff_getformat(const struct roff *r)
                   4335: {
                   4336:
1.149     schwarze 4337:        return r->format;
1.48      schwarze 4338: }
                   4339:
                   4340: /*
1.80      schwarze 4341:  * Find out whether a line is a macro line or not.
1.48      schwarze 4342:  * If it is, adjust the current position and return one; if it isn't,
                   4343:  * return zero and don't change the current position.
                   4344:  * If the control character has been set with `.cc', then let that grain
                   4345:  * precedence.
                   4346:  * This is slighly contrary to groff, where using the non-breaking
                   4347:  * control character when `cc' has been invoked will cause the
                   4348:  * non-breaking macro contents to be printed verbatim.
                   4349:  */
                   4350: int
                   4351: roff_getcontrol(const struct roff *r, const char *cp, int *ppos)
                   4352: {
                   4353:        int             pos;
                   4354:
                   4355:        pos = *ppos;
                   4356:
1.175     schwarze 4357:        if (r->control != '\0' && cp[pos] == r->control)
1.48      schwarze 4358:                pos++;
1.175     schwarze 4359:        else if (r->control != '\0')
1.149     schwarze 4360:                return 0;
1.48      schwarze 4361:        else if ('\\' == cp[pos] && '.' == cp[pos + 1])
                   4362:                pos += 2;
                   4363:        else if ('.' == cp[pos] || '\'' == cp[pos])
                   4364:                pos++;
                   4365:        else
1.149     schwarze 4366:                return 0;
1.48      schwarze 4367:
                   4368:        while (' ' == cp[pos] || '\t' == cp[pos])
                   4369:                pos++;
                   4370:
                   4371:        *ppos = pos;
1.149     schwarze 4372:        return 1;
1.1       schwarze 4373: }