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

Annotation of mandoc/roff.c, Revision 1.371

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

CVSweb