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

Annotation of mandoc/roff.c, Revision 1.373

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

CVSweb