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

Annotation of mandoc/roff.c, Revision 1.372

1.372   ! schwarze    1: /* $Id: roff.c,v 1.371 2020/02/27 21:43:44 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;
                   1126:        switch (n->tok) {
                   1127:        case ROFF_ft:
                   1128:        case ROFF_ll:
                   1129:        case ROFF_mc:
                   1130:        case ROFF_po:
                   1131:        case ROFF_ta:
                   1132:        case MDOC_Db:
                   1133:        case MDOC_Es:
                   1134:        case MDOC_Sm:
                   1135:        case MDOC_Tg:
                   1136:        case MAN_DT:
                   1137:        case MAN_UC:
                   1138:        case MAN_PD:
                   1139:        case MAN_AT:
                   1140:                return 1;
                   1141:        default:
                   1142:                return 0;
                   1143:        }
                   1144: }
                   1145:
                   1146: struct roff_node *
                   1147: roff_node_child(struct roff_node *n)
                   1148: {
                   1149:        for (n = n->child; roff_node_transparent(n); n = n->next)
                   1150:                continue;
                   1151:        return n;
                   1152: }
                   1153:
                   1154: struct roff_node *
                   1155: roff_node_prev(struct roff_node *n)
                   1156: {
                   1157:        do {
                   1158:                n = n->prev;
                   1159:        } while (roff_node_transparent(n));
                   1160:        return n;
                   1161: }
                   1162:
                   1163: struct roff_node *
                   1164: roff_node_next(struct roff_node *n)
                   1165: {
                   1166:        do {
                   1167:                n = n->next;
                   1168:        } while (roff_node_transparent(n));
                   1169:        return n;
1.269     schwarze 1170: }
                   1171:
                   1172: void
                   1173: deroff(char **dest, const struct roff_node *n)
                   1174: {
                   1175:        char    *cp;
                   1176:        size_t   sz;
                   1177:
1.371     schwarze 1178:        if (n->string == NULL) {
1.269     schwarze 1179:                for (n = n->child; n != NULL; n = n->next)
                   1180:                        deroff(dest, n);
                   1181:                return;
                   1182:        }
                   1183:
1.288     schwarze 1184:        /* Skip leading whitespace. */
1.269     schwarze 1185:
1.288     schwarze 1186:        for (cp = n->string; *cp != '\0'; cp++) {
1.289     schwarze 1187:                if (cp[0] == '\\' && cp[1] != '\0' &&
                   1188:                    strchr(" %&0^|~", cp[1]) != NULL)
1.269     schwarze 1189:                        cp++;
1.288     schwarze 1190:                else if ( ! isspace((unsigned char)*cp))
1.269     schwarze 1191:                        break;
                   1192:        }
                   1193:
1.289     schwarze 1194:        /* Skip trailing backslash. */
                   1195:
                   1196:        sz = strlen(cp);
1.290     schwarze 1197:        if (sz > 0 && cp[sz - 1] == '\\')
1.289     schwarze 1198:                sz--;
                   1199:
1.269     schwarze 1200:        /* Skip trailing whitespace. */
                   1201:
1.289     schwarze 1202:        for (; sz; sz--)
1.269     schwarze 1203:                if ( ! isspace((unsigned char)cp[sz-1]))
                   1204:                        break;
                   1205:
                   1206:        /* Skip empty strings. */
                   1207:
                   1208:        if (sz == 0)
                   1209:                return;
                   1210:
                   1211:        if (*dest == NULL) {
                   1212:                *dest = mandoc_strndup(cp, sz);
                   1213:                return;
                   1214:        }
                   1215:
                   1216:        mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
                   1217:        free(*dest);
                   1218:        *dest = cp;
1.266     schwarze 1219: }
                   1220:
                   1221: /* --- main functions of the roff parser ---------------------------------- */
                   1222:
1.94      kristaps 1223: /*
1.355     schwarze 1224:  * In the current line, expand escape sequences that produce parsable
                   1225:  * input text.  Also check the syntax of the remaining escape sequences,
                   1226:  * which typically produce output glyphs or change formatter state.
1.154     kristaps 1227:  */
1.340     schwarze 1228: static int
1.355     schwarze 1229: roff_expand(struct roff *r, struct buf *buf, int ln, int pos, char newesc)
1.94      kristaps 1230: {
1.339     schwarze 1231:        struct mctx     *ctx;   /* current macro call context */
1.208     schwarze 1232:        char             ubuf[24]; /* buffer to print the number */
1.328     schwarze 1233:        struct roff_node *n;    /* used for header comments */
1.205     schwarze 1234:        const char      *start; /* start of the string to process */
1.209     schwarze 1235:        char            *stesc; /* start of an escape sequence ('\\') */
1.352     schwarze 1236:        const char      *esct;  /* type of esccape sequence */
1.328     schwarze 1237:        char            *ep;    /* end of comment string */
1.108     schwarze 1238:        const char      *stnam; /* start of the name, after "[(*" */
                   1239:        const char      *cp;    /* end of the name, e.g. before ']' */
                   1240:        const char      *res;   /* the string to be substituted */
1.238     schwarze 1241:        char            *nbuf;  /* new buffer to copy buf->buf to */
1.181     schwarze 1242:        size_t           maxl;  /* expected length of the escape name */
                   1243:        size_t           naml;  /* actual length of the escape name */
1.339     schwarze 1244:        size_t           asz;   /* length of the replacement */
                   1245:        size_t           rsz;   /* length of the rest of the string */
1.237     schwarze 1246:        int              inaml; /* length returned from mandoc_escape() */
1.181     schwarze 1247:        int              expand_count;  /* to avoid infinite loops */
1.206     schwarze 1248:        int              npos;  /* position in numeric expression */
1.218     schwarze 1249:        int              arg_complete; /* argument not interrupted by eol */
1.339     schwarze 1250:        int              quote_args; /* true for \\$@, false for \\$* */
1.303     schwarze 1251:        int              done;  /* no more input available */
1.315     schwarze 1252:        int              deftype; /* type of definition to paste */
1.323     schwarze 1253:        int              rcsid; /* kind of RCS id seen */
1.352     schwarze 1254:        enum mandocerr   err;   /* for escape sequence problems */
1.327     schwarze 1255:        char             sign;  /* increment number register */
1.206     schwarze 1256:        char             term;  /* character terminating the escape */
1.94      kristaps 1257:
1.303     schwarze 1258:        /* Search forward for comments. */
                   1259:
                   1260:        done = 0;
                   1261:        start = buf->buf + pos;
                   1262:        for (stesc = buf->buf + pos; *stesc != '\0'; stesc++) {
1.355     schwarze 1263:                if (stesc[0] != newesc || stesc[1] == '\0')
1.303     schwarze 1264:                        continue;
                   1265:                stesc++;
                   1266:                if (*stesc != '"' && *stesc != '#')
                   1267:                        continue;
1.313     schwarze 1268:
                   1269:                /* Comment found, look for RCS id. */
                   1270:
1.323     schwarze 1271:                rcsid = 0;
1.313     schwarze 1272:                if ((cp = strstr(stesc, "$" "OpenBSD")) != NULL) {
1.323     schwarze 1273:                        rcsid = 1 << MANDOC_OS_OPENBSD;
1.313     schwarze 1274:                        cp += 8;
                   1275:                } else if ((cp = strstr(stesc, "$" "NetBSD")) != NULL) {
1.323     schwarze 1276:                        rcsid = 1 << MANDOC_OS_NETBSD;
1.313     schwarze 1277:                        cp += 7;
                   1278:                }
                   1279:                if (cp != NULL &&
                   1280:                    isalnum((unsigned char)*cp) == 0 &&
1.314     schwarze 1281:                    strchr(cp, '$') != NULL) {
1.323     schwarze 1282:                        if (r->man->meta.rcsids & rcsid)
1.350     schwarze 1283:                                mandoc_msg(MANDOCERR_RCS_REP, ln,
                   1284:                                    (int)(stesc - buf->buf) + 1,
                   1285:                                    "%s", stesc + 1);
1.323     schwarze 1286:                        r->man->meta.rcsids |= rcsid;
1.314     schwarze 1287:                }
1.313     schwarze 1288:
                   1289:                /* Handle trailing whitespace. */
                   1290:
1.328     schwarze 1291:                ep = strchr(stesc--, '\0') - 1;
                   1292:                if (*ep == '\n') {
1.303     schwarze 1293:                        done = 1;
1.328     schwarze 1294:                        ep--;
1.303     schwarze 1295:                }
1.328     schwarze 1296:                if (*ep == ' ' || *ep == '\t')
1.350     schwarze 1297:                        mandoc_msg(MANDOCERR_SPACE_EOL,
                   1298:                            ln, (int)(ep - buf->buf), NULL);
1.328     schwarze 1299:
                   1300:                /*
                   1301:                 * Save comments preceding the title macro
                   1302:                 * in the syntax tree.
                   1303:                 */
                   1304:
1.367     schwarze 1305:                if (newesc != ASCII_ESC && r->options & MPARSE_COMMENT) {
1.328     schwarze 1306:                        while (*ep == ' ' || *ep == '\t')
                   1307:                                ep--;
                   1308:                        ep[1] = '\0';
                   1309:                        n = roff_node_alloc(r->man,
                   1310:                            ln, stesc + 1 - buf->buf,
                   1311:                            ROFFT_COMMENT, TOKEN_NONE);
                   1312:                        n->string = mandoc_strdup(stesc + 2);
                   1313:                        roff_node_append(r->man, n);
                   1314:                        n->flags |= NODE_VALID | NODE_ENDED;
                   1315:                        r->man->next = ROFF_NEXT_SIBLING;
                   1316:                }
                   1317:
1.334     schwarze 1318:                /* Line continuation with comment. */
                   1319:
                   1320:                if (stesc[1] == '#') {
                   1321:                        *stesc = '\0';
1.340     schwarze 1322:                        return ROFF_IGN | ROFF_APPEND;
1.334     schwarze 1323:                }
                   1324:
                   1325:                /* Discard normal comments. */
1.328     schwarze 1326:
1.335     schwarze 1327:                while (stesc > start && stesc[-1] == ' ' &&
                   1328:                    (stesc == start + 1 || stesc[-2] != '\\'))
1.303     schwarze 1329:                        stesc--;
                   1330:                *stesc = '\0';
                   1331:                break;
                   1332:        }
                   1333:        if (stesc == start)
                   1334:                return ROFF_CONT;
                   1335:        stesc--;
                   1336:
                   1337:        /* Notice the end of the input. */
                   1338:
                   1339:        if (*stesc == '\n') {
                   1340:                *stesc-- = '\0';
                   1341:                done = 1;
                   1342:        }
                   1343:
1.170     schwarze 1344:        expand_count = 0;
1.303     schwarze 1345:        while (stesc >= start) {
1.355     schwarze 1346:                if (*stesc != newesc) {
1.170     schwarze 1347:
1.355     schwarze 1348:                        /*
                   1349:                         * If we have a non-standard escape character,
                   1350:                         * escape literal backslashes because all
                   1351:                         * processing in subsequent functions uses
                   1352:                         * the standard escaping rules.
                   1353:                         */
1.205     schwarze 1354:
1.355     schwarze 1355:                        if (newesc != ASCII_ESC && *stesc == '\\') {
1.303     schwarze 1356:                                *stesc = '\0';
                   1357:                                buf->sz = mandoc_asprintf(&nbuf, "%s\\e%s",
                   1358:                                    buf->buf, stesc + 1) + 1;
                   1359:                                start = nbuf + pos;
                   1360:                                stesc = nbuf + (stesc - buf->buf);
                   1361:                                free(buf->buf);
                   1362:                                buf->buf = nbuf;
                   1363:                        }
1.355     schwarze 1364:
                   1365:                        /* Search backwards for the next escape. */
                   1366:
1.303     schwarze 1367:                        stesc--;
1.205     schwarze 1368:                        continue;
1.303     schwarze 1369:                }
1.205     schwarze 1370:
                   1371:                /* If it is escaped, skip it. */
                   1372:
                   1373:                for (cp = stesc - 1; cp >= start; cp--)
1.303     schwarze 1374:                        if (*cp != r->escape)
1.205     schwarze 1375:                                break;
                   1376:
1.238     schwarze 1377:                if ((stesc - cp) % 2 == 0) {
1.303     schwarze 1378:                        while (stesc > cp)
                   1379:                                *stesc-- = '\\';
1.205     schwarze 1380:                        continue;
1.303     schwarze 1381:                } else if (stesc[1] != '\0') {
                   1382:                        *stesc = '\\';
                   1383:                } else {
                   1384:                        *stesc-- = '\0';
                   1385:                        if (done)
                   1386:                                continue;
                   1387:                        else
1.340     schwarze 1388:                                return ROFF_IGN | ROFF_APPEND;
1.205     schwarze 1389:                }
1.108     schwarze 1390:
1.206     schwarze 1391:                /* Decide whether to expand or to check only. */
1.108     schwarze 1392:
1.206     schwarze 1393:                term = '\0';
1.205     schwarze 1394:                cp = stesc + 1;
1.352     schwarze 1395:                if (*cp == 'E')
                   1396:                        cp++;
                   1397:                esct = cp;
                   1398:                switch (*esct) {
1.207     schwarze 1399:                case '*':
1.339     schwarze 1400:                case '$':
1.181     schwarze 1401:                        res = NULL;
                   1402:                        break;
1.207     schwarze 1403:                case 'B':
                   1404:                case 'w':
1.206     schwarze 1405:                        term = cp[1];
                   1406:                        /* FALLTHROUGH */
1.207     schwarze 1407:                case 'n':
1.327     schwarze 1408:                        sign = cp[1];
                   1409:                        if (sign == '+' || sign == '-')
                   1410:                                cp++;
1.181     schwarze 1411:                        res = ubuf;
                   1412:                        break;
                   1413:                default:
1.352     schwarze 1414:                        err = MANDOCERR_OK;
                   1415:                        switch(mandoc_escape(&cp, &stnam, &inaml)) {
                   1416:                        case ESCAPE_SPECIAL:
                   1417:                                if (mchars_spec2cp(stnam, inaml) >= 0)
                   1418:                                        break;
                   1419:                                /* FALLTHROUGH */
                   1420:                        case ESCAPE_ERROR:
                   1421:                                err = MANDOCERR_ESC_BAD;
                   1422:                                break;
                   1423:                        case ESCAPE_UNDEF:
                   1424:                                err = MANDOCERR_ESC_UNDEF;
                   1425:                                break;
                   1426:                        case ESCAPE_UNSUPP:
                   1427:                                err = MANDOCERR_ESC_UNSUPP;
                   1428:                                break;
                   1429:                        default:
                   1430:                                break;
                   1431:                        }
                   1432:                        if (err != MANDOCERR_OK)
                   1433:                                mandoc_msg(err, ln, (int)(stesc - buf->buf),
1.219     schwarze 1434:                                    "%.*s", (int)(cp - stesc), stesc);
1.303     schwarze 1435:                        stesc--;
1.205     schwarze 1436:                        continue;
1.152     kristaps 1437:                }
                   1438:
1.205     schwarze 1439:                if (EXPAND_LIMIT < ++expand_count) {
1.350     schwarze 1440:                        mandoc_msg(MANDOCERR_ROFFLOOP,
1.238     schwarze 1441:                            ln, (int)(stesc - buf->buf), NULL);
1.277     schwarze 1442:                        return ROFF_IGN;
1.205     schwarze 1443:                }
1.108     schwarze 1444:
                   1445:                /*
                   1446:                 * The third character decides the length
1.181     schwarze 1447:                 * of the name of the string or register.
1.108     schwarze 1448:                 * Save a pointer to the name.
                   1449:                 */
                   1450:
1.238     schwarze 1451:                if (term == '\0') {
1.206     schwarze 1452:                        switch (*++cp) {
1.207     schwarze 1453:                        case '\0':
1.206     schwarze 1454:                                maxl = 0;
                   1455:                                break;
1.207     schwarze 1456:                        case '(':
1.206     schwarze 1457:                                cp++;
                   1458:                                maxl = 2;
                   1459:                                break;
1.207     schwarze 1460:                        case '[':
1.206     schwarze 1461:                                cp++;
                   1462:                                term = ']';
                   1463:                                maxl = 0;
                   1464:                                break;
                   1465:                        default:
                   1466:                                maxl = 1;
                   1467:                                break;
                   1468:                        }
                   1469:                } else {
                   1470:                        cp += 2;
1.94      kristaps 1471:                        maxl = 0;
                   1472:                }
1.108     schwarze 1473:                stnam = cp;
1.94      kristaps 1474:
1.108     schwarze 1475:                /* Advance to the end of the name. */
1.94      kristaps 1476:
1.253     schwarze 1477:                naml = 0;
1.218     schwarze 1478:                arg_complete = 1;
1.253     schwarze 1479:                while (maxl == 0 || naml < maxl) {
1.238     schwarze 1480:                        if (*cp == '\0') {
1.350     schwarze 1481:                                mandoc_msg(MANDOCERR_ESC_BAD, ln,
                   1482:                                    (int)(stesc - buf->buf), "%s", stesc);
1.218     schwarze 1483:                                arg_complete = 0;
1.206     schwarze 1484:                                break;
1.153     kristaps 1485:                        }
1.238     schwarze 1486:                        if (maxl == 0 && *cp == term) {
1.206     schwarze 1487:                                cp++;
1.253     schwarze 1488:                                break;
                   1489:                        }
1.352     schwarze 1490:                        if (*cp++ != '\\' || *esct != 'w') {
1.253     schwarze 1491:                                naml++;
                   1492:                                continue;
                   1493:                        }
                   1494:                        switch (mandoc_escape(&cp, NULL, NULL)) {
                   1495:                        case ESCAPE_SPECIAL:
                   1496:                        case ESCAPE_UNICODE:
                   1497:                        case ESCAPE_NUMBERED:
1.352     schwarze 1498:                        case ESCAPE_UNDEF:
1.253     schwarze 1499:                        case ESCAPE_OVERSTRIKE:
                   1500:                                naml++;
                   1501:                                break;
                   1502:                        default:
1.94      kristaps 1503:                                break;
1.206     schwarze 1504:                        }
1.94      kristaps 1505:                }
                   1506:
1.108     schwarze 1507:                /*
                   1508:                 * Retrieve the replacement string; if it is
                   1509:                 * undefined, resume searching for escapes.
                   1510:                 */
                   1511:
1.352     schwarze 1512:                switch (*esct) {
1.207     schwarze 1513:                case '*':
1.315     schwarze 1514:                        if (arg_complete) {
                   1515:                                deftype = ROFFDEF_USER | ROFFDEF_PRE;
                   1516:                                res = roff_getstrn(r, stnam, naml, &deftype);
1.331     schwarze 1517:
                   1518:                                /*
                   1519:                                 * If not overriden, let \*(.T
                   1520:                                 * through to the formatters.
                   1521:                                 */
                   1522:
                   1523:                                if (res == NULL && naml == 2 &&
                   1524:                                    stnam[0] == '.' && stnam[1] == 'T') {
                   1525:                                        roff_setstrn(&r->strtab,
                   1526:                                            ".T", 2, NULL, 0, 0);
                   1527:                                        stesc--;
                   1528:                                        continue;
                   1529:                                }
1.315     schwarze 1530:                        }
1.206     schwarze 1531:                        break;
1.339     schwarze 1532:                case '$':
                   1533:                        if (r->mstackpos < 0) {
1.350     schwarze 1534:                                mandoc_msg(MANDOCERR_ARG_UNDEF, ln,
                   1535:                                    (int)(stesc - buf->buf), "%.3s", stesc);
1.339     schwarze 1536:                                break;
                   1537:                        }
                   1538:                        ctx = r->mstack + r->mstackpos;
1.352     schwarze 1539:                        npos = esct[1] - '1';
1.339     schwarze 1540:                        if (npos >= 0 && npos <= 8) {
                   1541:                                res = npos < ctx->argc ?
                   1542:                                    ctx->argv[npos] : "";
                   1543:                                break;
                   1544:                        }
1.352     schwarze 1545:                        if (esct[1] == '*')
1.339     schwarze 1546:                                quote_args = 0;
1.352     schwarze 1547:                        else if (esct[1] == '@')
1.339     schwarze 1548:                                quote_args = 1;
                   1549:                        else {
1.350     schwarze 1550:                                mandoc_msg(MANDOCERR_ARG_NONUM, ln,
                   1551:                                    (int)(stesc - buf->buf), "%.3s", stesc);
1.339     schwarze 1552:                                break;
                   1553:                        }
                   1554:                        asz = 0;
                   1555:                        for (npos = 0; npos < ctx->argc; npos++) {
                   1556:                                if (npos)
                   1557:                                        asz++;  /* blank */
                   1558:                                if (quote_args)
                   1559:                                        asz += 2;  /* quotes */
                   1560:                                asz += strlen(ctx->argv[npos]);
                   1561:                        }
                   1562:                        if (asz != 3) {
                   1563:                                rsz = buf->sz - (stesc - buf->buf) - 3;
                   1564:                                if (asz < 3)
                   1565:                                        memmove(stesc + asz, stesc + 3, rsz);
                   1566:                                buf->sz += asz - 3;
                   1567:                                nbuf = mandoc_realloc(buf->buf, buf->sz);
                   1568:                                start = nbuf + pos;
                   1569:                                stesc = nbuf + (stesc - buf->buf);
                   1570:                                buf->buf = nbuf;
                   1571:                                if (asz > 3)
                   1572:                                        memmove(stesc + asz, stesc + 3, rsz);
                   1573:                        }
                   1574:                        for (npos = 0; npos < ctx->argc; npos++) {
                   1575:                                if (npos)
                   1576:                                        *stesc++ = ' ';
                   1577:                                if (quote_args)
                   1578:                                        *stesc++ = '"';
                   1579:                                cp = ctx->argv[npos];
                   1580:                                while (*cp != '\0')
                   1581:                                        *stesc++ = *cp++;
                   1582:                                if (quote_args)
                   1583:                                        *stesc++ = '"';
                   1584:                        }
                   1585:                        continue;
1.207     schwarze 1586:                case 'B':
1.206     schwarze 1587:                        npos = 0;
1.218     schwarze 1588:                        ubuf[0] = arg_complete &&
1.261     schwarze 1589:                            roff_evalnum(r, ln, stnam, &npos,
                   1590:                              NULL, ROFFNUM_SCALE) &&
1.218     schwarze 1591:                            stnam + npos + 1 == cp ? '1' : '0';
1.206     schwarze 1592:                        ubuf[1] = '\0';
                   1593:                        break;
1.207     schwarze 1594:                case 'n':
1.218     schwarze 1595:                        if (arg_complete)
                   1596:                                (void)snprintf(ubuf, sizeof(ubuf), "%d",
1.327     schwarze 1597:                                    roff_getregn(r, stnam, naml, sign));
1.218     schwarze 1598:                        else
                   1599:                                ubuf[0] = '\0';
1.206     schwarze 1600:                        break;
1.207     schwarze 1601:                case 'w':
1.218     schwarze 1602:                        /* use even incomplete args */
1.208     schwarze 1603:                        (void)snprintf(ubuf, sizeof(ubuf), "%d",
1.206     schwarze 1604:                            24 * (int)naml);
                   1605:                        break;
                   1606:                }
1.94      kristaps 1607:
1.238     schwarze 1608:                if (res == NULL) {
1.352     schwarze 1609:                        if (*esct == '*')
1.350     schwarze 1610:                                mandoc_msg(MANDOCERR_STR_UNDEF,
                   1611:                                    ln, (int)(stesc - buf->buf),
1.339     schwarze 1612:                                    "%.*s", (int)naml, stnam);
1.142     kristaps 1613:                        res = "";
1.246     schwarze 1614:                } else if (buf->sz + strlen(res) > SHRT_MAX) {
1.350     schwarze 1615:                        mandoc_msg(MANDOCERR_ROFFLOOP,
1.246     schwarze 1616:                            ln, (int)(stesc - buf->buf), NULL);
1.277     schwarze 1617:                        return ROFF_IGN;
1.94      kristaps 1618:                }
                   1619:
1.108     schwarze 1620:                /* Replace the escape sequence by the string. */
                   1621:
1.209     schwarze 1622:                *stesc = '\0';
1.238     schwarze 1623:                buf->sz = mandoc_asprintf(&nbuf, "%s%s%s",
                   1624:                    buf->buf, res, cp) + 1;
1.94      kristaps 1625:
1.205     schwarze 1626:                /* Prepare for the next replacement. */
1.94      kristaps 1627:
1.205     schwarze 1628:                start = nbuf + pos;
1.238     schwarze 1629:                stesc = nbuf + (stesc - buf->buf) + strlen(res);
                   1630:                free(buf->buf);
                   1631:                buf->buf = nbuf;
1.154     kristaps 1632:        }
1.277     schwarze 1633:        return ROFF_CONT;
1.154     kristaps 1634: }
1.353     schwarze 1635:
                   1636: /*
                   1637:  * Parse a quoted or unquoted roff-style request or macro argument.
                   1638:  * Return a pointer to the parsed argument, which is either the original
                   1639:  * pointer or advanced by one byte in case the argument is quoted.
                   1640:  * NUL-terminate the argument in place.
                   1641:  * Collapse pairs of quotes inside quoted arguments.
                   1642:  * Advance the argument pointer to the next argument,
                   1643:  * or to the NUL byte terminating the argument line.
                   1644:  */
                   1645: char *
1.355     schwarze 1646: roff_getarg(struct roff *r, char **cpp, int ln, int *pos)
1.353     schwarze 1647: {
1.355     schwarze 1648:        struct buf       buf;
1.366     schwarze 1649:        char            *cp, *start;
1.355     schwarze 1650:        int              newesc, pairs, quoted, white;
1.353     schwarze 1651:
                   1652:        /* Quoting can only start with a new word. */
                   1653:        start = *cpp;
                   1654:        quoted = 0;
                   1655:        if ('"' == *start) {
                   1656:                quoted = 1;
                   1657:                start++;
                   1658:        }
                   1659:
1.355     schwarze 1660:        newesc = pairs = white = 0;
1.353     schwarze 1661:        for (cp = start; '\0' != *cp; cp++) {
                   1662:
                   1663:                /*
                   1664:                 * Move the following text left
                   1665:                 * after quoted quotes and after "\\" and "\t".
                   1666:                 */
                   1667:                if (pairs)
                   1668:                        cp[-pairs] = cp[0];
                   1669:
                   1670:                if ('\\' == cp[0]) {
                   1671:                        /*
                   1672:                         * In copy mode, translate double to single
                   1673:                         * backslashes and backslash-t to literal tabs.
                   1674:                         */
                   1675:                        switch (cp[1]) {
                   1676:                        case 'a':
                   1677:                        case 't':
1.354     schwarze 1678:                                cp[-pairs] = '\t';
1.355     schwarze 1679:                                pairs++;
                   1680:                                cp++;
                   1681:                                break;
1.353     schwarze 1682:                        case '\\':
1.355     schwarze 1683:                                newesc = 1;
                   1684:                                cp[-pairs] = ASCII_ESC;
1.353     schwarze 1685:                                pairs++;
                   1686:                                cp++;
                   1687:                                break;
                   1688:                        case ' ':
                   1689:                                /* Skip escaped blanks. */
                   1690:                                if (0 == quoted)
                   1691:                                        cp++;
                   1692:                                break;
                   1693:                        default:
                   1694:                                break;
                   1695:                        }
                   1696:                } else if (0 == quoted) {
                   1697:                        if (' ' == cp[0]) {
                   1698:                                /* Unescaped blanks end unquoted args. */
                   1699:                                white = 1;
                   1700:                                break;
                   1701:                        }
                   1702:                } else if ('"' == cp[0]) {
                   1703:                        if ('"' == cp[1]) {
                   1704:                                /* Quoted quotes collapse. */
                   1705:                                pairs++;
                   1706:                                cp++;
                   1707:                        } else {
                   1708:                                /* Unquoted quotes end quoted args. */
                   1709:                                quoted = 2;
                   1710:                                break;
                   1711:                        }
                   1712:                }
                   1713:        }
                   1714:
                   1715:        /* Quoted argument without a closing quote. */
                   1716:        if (1 == quoted)
                   1717:                mandoc_msg(MANDOCERR_ARG_QUOTE, ln, *pos, NULL);
                   1718:
                   1719:        /* NUL-terminate this argument and move to the next one. */
                   1720:        if (pairs)
                   1721:                cp[-pairs] = '\0';
                   1722:        if ('\0' != *cp) {
                   1723:                *cp++ = '\0';
                   1724:                while (' ' == *cp)
                   1725:                        cp++;
                   1726:        }
                   1727:        *pos += (int)(cp - start) + (quoted ? 1 : 0);
                   1728:        *cpp = cp;
                   1729:
                   1730:        if ('\0' == *cp && (white || ' ' == cp[-1]))
                   1731:                mandoc_msg(MANDOCERR_SPACE_EOL, ln, *pos, NULL);
                   1732:
1.355     schwarze 1733:        start = mandoc_strdup(start);
                   1734:        if (newesc == 0)
                   1735:                return start;
                   1736:
                   1737:        buf.buf = start;
                   1738:        buf.sz = strlen(start) + 1;
                   1739:        buf.next = NULL;
                   1740:        if (roff_expand(r, &buf, ln, 0, ASCII_ESC) & ROFF_IGN) {
                   1741:                free(buf.buf);
                   1742:                buf.buf = mandoc_strdup("");
                   1743:        }
                   1744:        return buf.buf;
1.353     schwarze 1745: }
                   1746:
1.154     kristaps 1747:
                   1748: /*
1.275     schwarze 1749:  * Process text streams.
1.154     kristaps 1750:  */
1.340     schwarze 1751: static int
1.305     schwarze 1752: roff_parsetext(struct roff *r, struct buf *buf, int pos, int *offs)
1.154     kristaps 1753: {
                   1754:        size_t           sz;
                   1755:        const char      *start;
1.178     schwarze 1756:        char            *p;
                   1757:        int              isz;
1.154     kristaps 1758:        enum mandoc_esc  esc;
                   1759:
1.275     schwarze 1760:        /* Spring the input line trap. */
                   1761:
                   1762:        if (roffit_lines == 1) {
                   1763:                isz = mandoc_asprintf(&p, "%s\n.%s", buf->buf, roffit_macro);
                   1764:                free(buf->buf);
                   1765:                buf->buf = p;
                   1766:                buf->sz = isz + 1;
                   1767:                *offs = 0;
                   1768:                free(roffit_macro);
                   1769:                roffit_lines = 0;
1.277     schwarze 1770:                return ROFF_REPARSE;
1.275     schwarze 1771:        } else if (roffit_lines > 1)
                   1772:                --roffit_lines;
                   1773:
1.305     schwarze 1774:        if (roffce_node != NULL && buf->buf[pos] != '\0') {
                   1775:                if (roffce_lines < 1) {
                   1776:                        r->man->last = roffce_node;
                   1777:                        r->man->next = ROFF_NEXT_SIBLING;
                   1778:                        roffce_lines = 0;
                   1779:                        roffce_node = NULL;
                   1780:                } else
                   1781:                        roffce_lines--;
                   1782:        }
                   1783:
1.275     schwarze 1784:        /* Convert all breakable hyphens into ASCII_HYPH. */
                   1785:
1.238     schwarze 1786:        start = p = buf->buf + pos;
1.154     kristaps 1787:
1.238     schwarze 1788:        while (*p != '\0') {
1.154     kristaps 1789:                sz = strcspn(p, "-\\");
                   1790:                p += sz;
                   1791:
1.238     schwarze 1792:                if (*p == '\0')
1.159     kristaps 1793:                        break;
                   1794:
1.238     schwarze 1795:                if (*p == '\\') {
1.154     kristaps 1796:                        /* Skip over escapes. */
                   1797:                        p++;
1.189     schwarze 1798:                        esc = mandoc_escape((const char **)&p, NULL, NULL);
1.238     schwarze 1799:                        if (esc == ESCAPE_ERROR)
1.154     kristaps 1800:                                break;
1.264     schwarze 1801:                        while (*p == '-')
                   1802:                                p++;
1.155     kristaps 1803:                        continue;
1.159     kristaps 1804:                } else if (p == start) {
1.158     kristaps 1805:                        p++;
1.155     kristaps 1806:                        continue;
1.158     kristaps 1807:                }
1.155     kristaps 1808:
1.171     schwarze 1809:                if (isalpha((unsigned char)p[-1]) &&
                   1810:                    isalpha((unsigned char)p[1]))
1.155     kristaps 1811:                        *p = ASCII_HYPH;
                   1812:                p++;
1.94      kristaps 1813:        }
1.277     schwarze 1814:        return ROFF_CONT;
1.94      kristaps 1815: }
                   1816:
1.340     schwarze 1817: int
1.238     schwarze 1818: roff_parseln(struct roff *r, int ln, struct buf *buf, int *offs)
1.67      kristaps 1819: {
1.294     schwarze 1820:        enum roff_tok    t;
1.340     schwarze 1821:        int              e;
1.238     schwarze 1822:        int              pos;   /* parse point */
1.243     schwarze 1823:        int              spos;  /* saved parse point for messages */
1.238     schwarze 1824:        int              ppos;  /* original offset in buf->buf */
                   1825:        int              ctl;   /* macro line (boolean) */
                   1826:
                   1827:        ppos = pos = *offs;
1.79      kristaps 1828:
1.230     schwarze 1829:        /* Handle in-line equation delimiters. */
                   1830:
1.236     schwarze 1831:        if (r->tbl == NULL &&
                   1832:            r->last_eqn != NULL && r->last_eqn->delim &&
1.230     schwarze 1833:            (r->eqn == NULL || r->eqn_inline)) {
1.238     schwarze 1834:                e = roff_eqndelim(r, buf, pos);
1.230     schwarze 1835:                if (e == ROFF_REPARSE)
1.277     schwarze 1836:                        return e;
1.230     schwarze 1837:                assert(e == ROFF_CONT);
                   1838:        }
                   1839:
                   1840:        /* Expand some escape sequences. */
1.94      kristaps 1841:
1.355     schwarze 1842:        e = roff_expand(r, buf, ln, pos, r->escape);
1.340     schwarze 1843:        if ((e & ROFF_MASK) == ROFF_IGN)
1.277     schwarze 1844:                return e;
1.238     schwarze 1845:        assert(e == ROFF_CONT);
1.94      kristaps 1846:
1.238     schwarze 1847:        ctl = roff_getcontrol(r, buf->buf, &pos);
1.130     kristaps 1848:
1.94      kristaps 1849:        /*
1.79      kristaps 1850:         * First, if a scope is open and we're not a macro, pass the
1.252     schwarze 1851:         * text through the macro's filter.
                   1852:         * Equations process all content themselves.
                   1853:         * Tables process almost all content themselves, but we want
                   1854:         * to warn about macros before passing it there.
1.79      kristaps 1855:         */
1.74      kristaps 1856:
1.252     schwarze 1857:        if (r->last != NULL && ! ctl) {
1.78      kristaps 1858:                t = r->last->tok;
1.238     schwarze 1859:                e = (*roffs[t].text)(r, t, buf, ln, pos, pos, offs);
1.340     schwarze 1860:                if ((e & ROFF_MASK) == ROFF_IGN)
1.277     schwarze 1861:                        return e;
1.340     schwarze 1862:                e &= ~ROFF_MASK;
                   1863:        } else
                   1864:                e = ROFF_IGN;
1.319     schwarze 1865:        if (r->eqn != NULL && strncmp(buf->buf + ppos, ".EN", 3)) {
                   1866:                eqn_read(r->eqn, buf->buf + ppos);
1.340     schwarze 1867:                return e;
1.319     schwarze 1868:        }
1.321     schwarze 1869:        if (r->tbl != NULL && (ctl == 0 || buf->buf[pos] == '\0')) {
                   1870:                tbl_read(r->tbl, ln, buf->buf, ppos);
1.346     schwarze 1871:                roff_addtbl(r->man, ln, r->tbl);
1.340     schwarze 1872:                return e;
1.321     schwarze 1873:        }
1.367     schwarze 1874:        if ( ! ctl) {
                   1875:                r->options &= ~MPARSE_COMMENT;
1.340     schwarze 1876:                return roff_parsetext(r, buf, pos, offs) | e;
1.367     schwarze 1877:        }
1.228     schwarze 1878:
                   1879:        /* Skip empty request lines. */
                   1880:
1.238     schwarze 1881:        if (buf->buf[pos] == '"') {
1.350     schwarze 1882:                mandoc_msg(MANDOCERR_COMMENT_BAD, ln, pos, NULL);
1.277     schwarze 1883:                return ROFF_IGN;
1.238     schwarze 1884:        } else if (buf->buf[pos] == '\0')
1.277     schwarze 1885:                return ROFF_IGN;
1.67      kristaps 1886:
1.79      kristaps 1887:        /*
                   1888:         * If a scope is open, go to the child handler for that macro,
                   1889:         * as it may want to preprocess before doing anything with it.
1.125     kristaps 1890:         * Don't do so if an equation is open.
1.79      kristaps 1891:         */
1.78      kristaps 1892:
1.79      kristaps 1893:        if (r->last) {
                   1894:                t = r->last->tok;
1.277     schwarze 1895:                return (*roffs[t].sub)(r, t, buf, ln, ppos, pos, offs);
1.79      kristaps 1896:        }
1.78      kristaps 1897:
1.243     schwarze 1898:        /* No scope is open.  This is a new request or macro. */
                   1899:
1.367     schwarze 1900:        r->options &= ~MPARSE_COMMENT;
1.243     schwarze 1901:        spos = pos;
                   1902:        t = roff_parse(r, buf->buf, &pos, ln, ppos);
                   1903:
                   1904:        /* Tables ignore most macros. */
                   1905:
1.308     schwarze 1906:        if (r->tbl != NULL && (t == TOKEN_NONE || t == ROFF_TS ||
1.309     schwarze 1907:            t == ROFF_br || t == ROFF_ce || t == ROFF_rj || t == ROFF_sp)) {
1.350     schwarze 1908:                mandoc_msg(MANDOCERR_TBLMACRO,
                   1909:                    ln, pos, "%s", buf->buf + spos);
1.308     schwarze 1910:                if (t != TOKEN_NONE)
1.277     schwarze 1911:                        return ROFF_IGN;
1.257     schwarze 1912:                while (buf->buf[pos] != '\0' && buf->buf[pos] != ' ')
                   1913:                        pos++;
1.291     schwarze 1914:                while (buf->buf[pos] == ' ')
1.257     schwarze 1915:                        pos++;
1.321     schwarze 1916:                tbl_read(r->tbl, ln, buf->buf, pos);
1.346     schwarze 1917:                roff_addtbl(r->man, ln, r->tbl);
1.321     schwarze 1918:                return ROFF_IGN;
1.243     schwarze 1919:        }
                   1920:
1.305     schwarze 1921:        /* For now, let high level macros abort .ce mode. */
                   1922:
                   1923:        if (ctl && roffce_node != NULL &&
1.324     schwarze 1924:            (t == TOKEN_NONE || t == ROFF_Dd || t == ROFF_EQ ||
                   1925:             t == ROFF_TH || t == ROFF_TS)) {
1.305     schwarze 1926:                r->man->last = roffce_node;
                   1927:                r->man->next = ROFF_NEXT_SIBLING;
                   1928:                roffce_lines = 0;
                   1929:                roffce_node = NULL;
                   1930:        }
                   1931:
1.79      kristaps 1932:        /*
1.243     schwarze 1933:         * This is neither a roff request nor a user-defined macro.
                   1934:         * Let the standard macro set parsers handle it.
1.79      kristaps 1935:         */
1.67      kristaps 1936:
1.294     schwarze 1937:        if (t == TOKEN_NONE)
1.277     schwarze 1938:                return ROFF_CONT;
1.243     schwarze 1939:
                   1940:        /* Execute a roff request or a user defined macro. */
1.67      kristaps 1941:
1.296     schwarze 1942:        return (*roffs[t].proc)(r, t, buf, ln, spos, pos, offs);
1.74      kristaps 1943: }
                   1944:
1.339     schwarze 1945: /*
                   1946:  * Internal interface function to tell the roff parser that execution
                   1947:  * of the current macro ended.  This is required because macro
                   1948:  * definitions usually do not end with a .return request.
                   1949:  */
                   1950: void
                   1951: roff_userret(struct roff *r)
                   1952: {
                   1953:        struct mctx     *ctx;
                   1954:        int              i;
                   1955:
                   1956:        assert(r->mstackpos >= 0);
                   1957:        ctx = r->mstack + r->mstackpos;
                   1958:        for (i = 0; i < ctx->argc; i++)
                   1959:                free(ctx->argv[i]);
                   1960:        ctx->argc = 0;
                   1961:        r->mstackpos--;
                   1962: }
                   1963:
1.117     kristaps 1964: void
1.74      kristaps 1965: roff_endparse(struct roff *r)
                   1966: {
1.321     schwarze 1967:        if (r->last != NULL)
1.350     schwarze 1968:                mandoc_msg(MANDOCERR_BLK_NOEND, r->last->line,
                   1969:                    r->last->col, "%s", roff_name[r->last->tok]);
1.117     kristaps 1970:
1.321     schwarze 1971:        if (r->eqn != NULL) {
1.350     schwarze 1972:                mandoc_msg(MANDOCERR_BLK_NOEND,
1.319     schwarze 1973:                    r->eqn->node->line, r->eqn->node->pos, "EQ");
                   1974:                eqn_parse(r->eqn);
                   1975:                r->eqn = NULL;
1.125     kristaps 1976:        }
                   1977:
1.321     schwarze 1978:        if (r->tbl != NULL) {
1.346     schwarze 1979:                tbl_end(r->tbl, 1);
1.321     schwarze 1980:                r->tbl = NULL;
1.117     kristaps 1981:        }
1.67      kristaps 1982: }
                   1983:
                   1984: /*
                   1985:  * Parse a roff node's type from the input buffer.  This must be in the
                   1986:  * form of ".foo xxx" in the usual way.
                   1987:  */
1.294     schwarze 1988: static enum roff_tok
1.214     schwarze 1989: roff_parse(struct roff *r, char *buf, int *pos, int ln, int ppos)
1.67      kristaps 1990: {
1.214     schwarze 1991:        char            *cp;
1.106     kristaps 1992:        const char      *mac;
                   1993:        size_t           maclen;
1.315     schwarze 1994:        int              deftype;
1.294     schwarze 1995:        enum roff_tok    t;
1.67      kristaps 1996:
1.214     schwarze 1997:        cp = buf + *pos;
                   1998:
                   1999:        if ('\0' == *cp || '"' == *cp || '\t' == *cp || ' ' == *cp)
1.294     schwarze 2000:                return TOKEN_NONE;
1.67      kristaps 2001:
1.214     schwarze 2002:        mac = cp;
                   2003:        maclen = roff_getname(r, &cp, ln, ppos);
1.67      kristaps 2004:
1.315     schwarze 2005:        deftype = ROFFDEF_USER | ROFFDEF_REN;
                   2006:        r->current_string = roff_getstrn(r, mac, maclen, &deftype);
                   2007:        switch (deftype) {
                   2008:        case ROFFDEF_USER:
                   2009:                t = ROFF_USERDEF;
                   2010:                break;
                   2011:        case ROFFDEF_REN:
                   2012:                t = ROFF_RENAMED;
                   2013:                break;
                   2014:        default:
                   2015:                t = roffhash_find(r->reqtab, mac, maclen);
                   2016:                break;
                   2017:        }
1.294     schwarze 2018:        if (t != TOKEN_NONE)
1.214     schwarze 2019:                *pos = cp - buf;
1.325     schwarze 2020:        else if (deftype == ROFFDEF_UNDEF) {
                   2021:                /* Using an undefined macro defines it to be empty. */
                   2022:                roff_setstrn(&r->strtab, mac, maclen, "", 0, 0);
                   2023:                roff_setstrn(&r->rentab, mac, maclen, NULL, 0, 0);
                   2024:        }
1.277     schwarze 2025:        return t;
1.67      kristaps 2026: }
                   2027:
1.266     schwarze 2028: /* --- handling of request blocks ----------------------------------------- */
                   2029:
1.340     schwarze 2030: static int
1.76      kristaps 2031: roff_cblock(ROFF_ARGS)
1.67      kristaps 2032: {
                   2033:
1.79      kristaps 2034:        /*
                   2035:         * A block-close `..' should only be invoked as a child of an
                   2036:         * ignore macro, otherwise raise a warning and just ignore it.
                   2037:         */
                   2038:
1.238     schwarze 2039:        if (r->last == NULL) {
1.350     schwarze 2040:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "..");
1.277     schwarze 2041:                return ROFF_IGN;
1.76      kristaps 2042:        }
1.67      kristaps 2043:
1.81      kristaps 2044:        switch (r->last->tok) {
1.207     schwarze 2045:        case ROFF_am:
1.220     schwarze 2046:                /* ROFF_am1 is remapped to ROFF_am in roff_block(). */
1.207     schwarze 2047:        case ROFF_ami:
                   2048:        case ROFF_de:
1.108     schwarze 2049:                /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1.207     schwarze 2050:        case ROFF_dei:
                   2051:        case ROFF_ig:
1.81      kristaps 2052:                break;
                   2053:        default:
1.350     schwarze 2054:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "..");
1.277     schwarze 2055:                return ROFF_IGN;
1.76      kristaps 2056:        }
1.67      kristaps 2057:
1.238     schwarze 2058:        if (buf->buf[pos] != '\0')
1.350     schwarze 2059:                mandoc_msg(MANDOCERR_ARG_SKIP, ln, pos,
1.238     schwarze 2060:                    ".. %s", buf->buf + pos);
1.71      kristaps 2061:
                   2062:        roffnode_pop(r);
1.76      kristaps 2063:        roffnode_cleanscope(r);
1.277     schwarze 2064:        return ROFF_IGN;
1.71      kristaps 2065:
1.67      kristaps 2066: }
                   2067:
1.364     schwarze 2068: /*
                   2069:  * Pop all nodes ending at the end of the current input line.
                   2070:  * Return the number of loops ended.
                   2071:  */
1.340     schwarze 2072: static int
1.76      kristaps 2073: roffnode_cleanscope(struct roff *r)
1.67      kristaps 2074: {
1.340     schwarze 2075:        int inloop;
1.67      kristaps 2076:
1.340     schwarze 2077:        inloop = 0;
                   2078:        while (r->last != NULL) {
1.173     schwarze 2079:                if (--r->last->endspan != 0)
1.76      kristaps 2080:                        break;
1.340     schwarze 2081:                inloop += roffnode_pop(r);
1.76      kristaps 2082:        }
1.340     schwarze 2083:        return inloop;
1.67      kristaps 2084: }
                   2085:
1.364     schwarze 2086: /*
                   2087:  * Handle the closing \} of a conditional block.
                   2088:  * Apart from generating warnings, this only pops nodes.
                   2089:  * Return the number of loops ended.
                   2090:  */
1.340     schwarze 2091: static int
1.195     schwarze 2092: roff_ccond(struct roff *r, int ln, int ppos)
1.74      kristaps 2093: {
1.76      kristaps 2094:        if (NULL == r->last) {
1.350     schwarze 2095:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "\\}");
1.340     schwarze 2096:                return 0;
1.76      kristaps 2097:        }
                   2098:
1.82      kristaps 2099:        switch (r->last->tok) {
1.207     schwarze 2100:        case ROFF_el:
                   2101:        case ROFF_ie:
                   2102:        case ROFF_if:
1.340     schwarze 2103:        case ROFF_while:
1.82      kristaps 2104:                break;
                   2105:        default:
1.350     schwarze 2106:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "\\}");
1.340     schwarze 2107:                return 0;
1.75      kristaps 2108:        }
                   2109:
1.76      kristaps 2110:        if (r->last->endspan > -1) {
1.350     schwarze 2111:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "\\}");
1.340     schwarze 2112:                return 0;
1.76      kristaps 2113:        }
                   2114:
1.340     schwarze 2115:        return roffnode_pop(r) + roffnode_cleanscope(r);
1.76      kristaps 2116: }
                   2117:
1.340     schwarze 2118: static int
1.80      kristaps 2119: roff_block(ROFF_ARGS)
1.76      kristaps 2120: {
1.315     schwarze 2121:        const char      *name, *value;
                   2122:        char            *call, *cp, *iname, *rname;
                   2123:        size_t           csz, namesz, rsz;
                   2124:        int              deftype;
1.106     kristaps 2125:
1.220     schwarze 2126:        /* Ignore groff compatibility mode for now. */
1.76      kristaps 2127:
1.238     schwarze 2128:        if (tok == ROFF_de1)
1.220     schwarze 2129:                tok = ROFF_de;
1.251     schwarze 2130:        else if (tok == ROFF_dei1)
                   2131:                tok = ROFF_dei;
1.238     schwarze 2132:        else if (tok == ROFF_am1)
1.220     schwarze 2133:                tok = ROFF_am;
1.251     schwarze 2134:        else if (tok == ROFF_ami1)
                   2135:                tok = ROFF_ami;
1.220     schwarze 2136:
                   2137:        /* Parse the macro name argument. */
                   2138:
1.238     schwarze 2139:        cp = buf->buf + pos;
                   2140:        if (tok == ROFF_ig) {
1.220     schwarze 2141:                iname = NULL;
                   2142:                namesz = 0;
                   2143:        } else {
                   2144:                iname = cp;
                   2145:                namesz = roff_getname(r, &cp, ln, ppos);
                   2146:                iname[namesz] = '\0';
                   2147:        }
1.107     kristaps 2148:
1.220     schwarze 2149:        /* Resolve the macro name argument if it is indirect. */
1.107     kristaps 2150:
1.238     schwarze 2151:        if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
1.315     schwarze 2152:                deftype = ROFFDEF_USER;
                   2153:                name = roff_getstrn(r, iname, namesz, &deftype);
                   2154:                if (name == NULL) {
1.350     schwarze 2155:                        mandoc_msg(MANDOCERR_STR_UNDEF,
                   2156:                            ln, (int)(iname - buf->buf),
1.220     schwarze 2157:                            "%.*s", (int)namesz, iname);
                   2158:                        namesz = 0;
                   2159:                } else
                   2160:                        namesz = strlen(name);
                   2161:        } else
                   2162:                name = iname;
1.107     kristaps 2163:
1.238     schwarze 2164:        if (namesz == 0 && tok != ROFF_ig) {
1.350     schwarze 2165:                mandoc_msg(MANDOCERR_REQ_EMPTY,
                   2166:                    ln, ppos, "%s", roff_name[tok]);
1.277     schwarze 2167:                return ROFF_IGN;
1.220     schwarze 2168:        }
1.80      kristaps 2169:
1.106     kristaps 2170:        roffnode_push(r, tok, name, ln, ppos);
                   2171:
                   2172:        /*
                   2173:         * At the beginning of a `de' macro, clear the existing string
                   2174:         * with the same name, if there is one.  New content will be
1.193     schwarze 2175:         * appended from roff_block_text() in multiline mode.
1.106     kristaps 2176:         */
1.107     kristaps 2177:
1.311     schwarze 2178:        if (tok == ROFF_de || tok == ROFF_dei) {
1.213     schwarze 2179:                roff_setstrn(&r->strtab, name, namesz, "", 0, 0);
1.311     schwarze 2180:                roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
1.315     schwarze 2181:        } else if (tok == ROFF_am || tok == ROFF_ami) {
                   2182:                deftype = ROFFDEF_ANY;
                   2183:                value = roff_getstrn(r, iname, namesz, &deftype);
                   2184:                switch (deftype) {  /* Before appending, ... */
                   2185:                case ROFFDEF_PRE: /* copy predefined to user-defined. */
                   2186:                        roff_setstrn(&r->strtab, name, namesz,
                   2187:                            value, strlen(value), 0);
                   2188:                        break;
                   2189:                case ROFFDEF_REN: /* call original standard macro. */
                   2190:                        csz = mandoc_asprintf(&call, ".%.*s \\$* \\\"\n",
                   2191:                            (int)strlen(value), value);
                   2192:                        roff_setstrn(&r->strtab, name, namesz, call, csz, 0);
                   2193:                        roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
                   2194:                        free(call);
                   2195:                        break;
                   2196:                case ROFFDEF_STD:  /* rename and call standard macro. */
                   2197:                        rsz = mandoc_asprintf(&rname, "__%s_renamed", name);
                   2198:                        roff_setstrn(&r->rentab, rname, rsz, name, namesz, 0);
                   2199:                        csz = mandoc_asprintf(&call, ".%.*s \\$* \\\"\n",
                   2200:                            (int)rsz, rname);
                   2201:                        roff_setstrn(&r->strtab, name, namesz, call, csz, 0);
                   2202:                        free(call);
                   2203:                        free(rname);
                   2204:                        break;
                   2205:                default:
                   2206:                        break;
                   2207:                }
1.311     schwarze 2208:        }
1.76      kristaps 2209:
1.238     schwarze 2210:        if (*cp == '\0')
1.277     schwarze 2211:                return ROFF_IGN;
1.78      kristaps 2212:
1.220     schwarze 2213:        /* Get the custom end marker. */
1.107     kristaps 2214:
1.220     schwarze 2215:        iname = cp;
1.213     schwarze 2216:        namesz = roff_getname(r, &cp, ln, ppos);
1.220     schwarze 2217:
                   2218:        /* Resolve the end marker if it is indirect. */
                   2219:
1.238     schwarze 2220:        if (namesz && (tok == ROFF_dei || tok == ROFF_ami)) {
1.315     schwarze 2221:                deftype = ROFFDEF_USER;
                   2222:                name = roff_getstrn(r, iname, namesz, &deftype);
                   2223:                if (name == NULL) {
1.350     schwarze 2224:                        mandoc_msg(MANDOCERR_STR_UNDEF,
                   2225:                            ln, (int)(iname - buf->buf),
1.220     schwarze 2226:                            "%.*s", (int)namesz, iname);
                   2227:                        namesz = 0;
                   2228:                } else
                   2229:                        namesz = strlen(name);
                   2230:        } else
                   2231:                name = iname;
                   2232:
1.213     schwarze 2233:        if (namesz)
                   2234:                r->last->end = mandoc_strndup(name, namesz);
1.78      kristaps 2235:
1.238     schwarze 2236:        if (*cp != '\0')
1.350     schwarze 2237:                mandoc_msg(MANDOCERR_ARG_EXCESS,
1.295     schwarze 2238:                    ln, pos, ".%s ... %s", roff_name[tok], cp);
1.74      kristaps 2239:
1.277     schwarze 2240:        return ROFF_IGN;
1.78      kristaps 2241: }
                   2242:
1.340     schwarze 2243: static int
1.80      kristaps 2244: roff_block_sub(ROFF_ARGS)
1.79      kristaps 2245: {
1.294     schwarze 2246:        enum roff_tok   t;
1.79      kristaps 2247:        int             i, j;
                   2248:
                   2249:        /*
                   2250:         * First check whether a custom macro exists at this level.  If
                   2251:         * it does, then check against it.  This is some of groff's
                   2252:         * stranger behaviours.  If we encountered a custom end-scope
                   2253:         * tag and that tag also happens to be a "real" macro, then we
                   2254:         * need to try interpreting it again as a real macro.  If it's
                   2255:         * not, then return ignore.  Else continue.
                   2256:         */
                   2257:
                   2258:        if (r->last->end) {
1.130     kristaps 2259:                for (i = pos, j = 0; r->last->end[j]; j++, i++)
1.238     schwarze 2260:                        if (buf->buf[i] != r->last->end[j])
1.79      kristaps 2261:                                break;
                   2262:
1.238     schwarze 2263:                if (r->last->end[j] == '\0' &&
                   2264:                    (buf->buf[i] == '\0' ||
                   2265:                     buf->buf[i] == ' ' ||
                   2266:                     buf->buf[i] == '\t')) {
1.79      kristaps 2267:                        roffnode_pop(r);
                   2268:                        roffnode_cleanscope(r);
                   2269:
1.238     schwarze 2270:                        while (buf->buf[i] == ' ' || buf->buf[i] == '\t')
1.130     kristaps 2271:                                i++;
                   2272:
                   2273:                        pos = i;
1.238     schwarze 2274:                        if (roff_parse(r, buf->buf, &pos, ln, ppos) !=
1.294     schwarze 2275:                            TOKEN_NONE)
1.277     schwarze 2276:                                return ROFF_RERUN;
                   2277:                        return ROFF_IGN;
1.79      kristaps 2278:                }
                   2279:        }
                   2280:
                   2281:        /*
                   2282:         * If we have no custom end-query or lookup failed, then try
                   2283:         * pulling it out of the hashtable.
                   2284:         */
                   2285:
1.238     schwarze 2286:        t = roff_parse(r, buf->buf, &pos, ln, ppos);
1.79      kristaps 2287:
1.238     schwarze 2288:        if (t != ROFF_cblock) {
                   2289:                if (tok != ROFF_ig)
                   2290:                        roff_setstr(r, r->last->name, buf->buf + ppos, 2);
1.277     schwarze 2291:                return ROFF_IGN;
1.106     kristaps 2292:        }
1.79      kristaps 2293:
1.277     schwarze 2294:        return (*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs);
1.79      kristaps 2295: }
                   2296:
1.340     schwarze 2297: static int
1.80      kristaps 2298: roff_block_text(ROFF_ARGS)
1.78      kristaps 2299: {
                   2300:
1.238     schwarze 2301:        if (tok != ROFF_ig)
                   2302:                roff_setstr(r, r->last->name, buf->buf + pos, 2);
1.106     kristaps 2303:
1.277     schwarze 2304:        return ROFF_IGN;
1.78      kristaps 2305: }
                   2306:
1.340     schwarze 2307: static int
1.82      kristaps 2308: roff_cond_sub(ROFF_ARGS)
                   2309: {
1.364     schwarze 2310:        struct roffnode *bl;
1.340     schwarze 2311:        char            *ep;
                   2312:        int              endloop, irc, rr;
1.294     schwarze 2313:        enum roff_tok    t;
1.82      kristaps 2314:
1.340     schwarze 2315:        irc = ROFF_IGN;
1.82      kristaps 2316:        rr = r->last->rule;
1.340     schwarze 2317:        endloop = tok != ROFF_while ? ROFF_IGN :
                   2318:            rr ? ROFF_LOOPCONT : ROFF_LOOPEXIT;
                   2319:        if (roffnode_cleanscope(r))
                   2320:                irc |= endloop;
1.144     kristaps 2321:
1.196     schwarze 2322:        /*
                   2323:         * If `\}' occurs on a macro line without a preceding macro,
                   2324:         * drop the line completely.
                   2325:         */
                   2326:
1.238     schwarze 2327:        ep = buf->buf + pos;
                   2328:        if (ep[0] == '\\' && ep[1] == '}')
1.198     schwarze 2329:                rr = 0;
1.196     schwarze 2330:
1.343     schwarze 2331:        /*
                   2332:         * The closing delimiter `\}' rewinds the conditional scope
                   2333:         * but is otherwise ignored when interpreting the line.
                   2334:         */
1.144     kristaps 2335:
1.238     schwarze 2336:        while ((ep = strchr(ep, '\\')) != NULL) {
1.318     schwarze 2337:                switch (ep[1]) {
                   2338:                case '}':
                   2339:                        memmove(ep, ep + 2, strlen(ep + 2) + 1);
1.340     schwarze 2340:                        if (roff_ccond(r, ln, ep - buf->buf))
                   2341:                                irc |= endloop;
1.318     schwarze 2342:                        break;
                   2343:                case '\0':
                   2344:                        ++ep;
                   2345:                        break;
                   2346:                default:
                   2347:                        ep += 2;
                   2348:                        break;
1.197     schwarze 2349:                }
1.177     schwarze 2350:        }
1.318     schwarze 2351:
1.368     schwarze 2352:        t = roff_parse(r, buf->buf, &pos, ln, ppos);
                   2353:
                   2354:        /* For now, let high level macros abort .ce mode. */
                   2355:
                   2356:        if (roffce_node != NULL &&
                   2357:            (t == TOKEN_NONE || t == ROFF_Dd || t == ROFF_EQ ||
                   2358:              t == ROFF_TH || t == ROFF_TS)) {
                   2359:                r->man->last = roffce_node;
                   2360:                r->man->next = ROFF_NEXT_SIBLING;
                   2361:                roffce_lines = 0;
                   2362:                roffce_node = NULL;
                   2363:        }
                   2364:
1.318     schwarze 2365:        /*
                   2366:         * Fully handle known macros when they are structurally
                   2367:         * required or when the conditional evaluated to true.
                   2368:         */
                   2369:
1.364     schwarze 2370:        if (t == ROFF_break) {
                   2371:                if (irc & ROFF_LOOPMASK)
                   2372:                        irc = ROFF_IGN | ROFF_LOOPEXIT;
                   2373:                else if (rr) {
                   2374:                        for (bl = r->last; bl != NULL; bl = bl->parent) {
                   2375:                                bl->rule = 0;
                   2376:                                if (bl->tok == ROFF_while)
                   2377:                                        break;
                   2378:                        }
                   2379:                }
                   2380:        } else if (t != TOKEN_NONE &&
                   2381:            (rr || roffs[t].flags & ROFFMAC_STRUCT))
                   2382:                irc |= (*roffs[t].proc)(r, t, buf, ln, ppos, pos, offs);
                   2383:        else
                   2384:                irc |= rr ? ROFF_CONT : ROFF_IGN;
1.340     schwarze 2385:        return irc;
1.82      kristaps 2386: }
                   2387:
1.340     schwarze 2388: static int
1.82      kristaps 2389: roff_cond_text(ROFF_ARGS)
1.78      kristaps 2390: {
1.140     kristaps 2391:        char            *ep;
1.340     schwarze 2392:        int              endloop, irc, rr;
1.82      kristaps 2393:
1.340     schwarze 2394:        irc = ROFF_IGN;
1.82      kristaps 2395:        rr = r->last->rule;
1.340     schwarze 2396:        endloop = tok != ROFF_while ? ROFF_IGN :
                   2397:            rr ? ROFF_LOOPCONT : ROFF_LOOPEXIT;
                   2398:        if (roffnode_cleanscope(r))
                   2399:                irc |= endloop;
1.82      kristaps 2400:
1.343     schwarze 2401:        /*
                   2402:         * If `\}' occurs on a text line with neither preceding
                   2403:         * nor following characters, drop the line completely.
                   2404:         */
                   2405:
1.238     schwarze 2406:        ep = buf->buf + pos;
1.343     schwarze 2407:        if (strcmp(ep, "\\}") == 0)
                   2408:                rr = 0;
                   2409:
                   2410:        /*
                   2411:         * The closing delimiter `\}' rewinds the conditional scope
                   2412:         * but is otherwise ignored when interpreting the line.
                   2413:         */
                   2414:
1.238     schwarze 2415:        while ((ep = strchr(ep, '\\')) != NULL) {
1.343     schwarze 2416:                switch (ep[1]) {
                   2417:                case '}':
                   2418:                        memmove(ep, ep + 2, strlen(ep + 2) + 1);
                   2419:                        if (roff_ccond(r, ln, ep - buf->buf))
1.340     schwarze 2420:                                irc |= endloop;
1.343     schwarze 2421:                        break;
                   2422:                case '\0':
                   2423:                        ++ep;
                   2424:                        break;
                   2425:                default:
                   2426:                        ep += 2;
                   2427:                        break;
1.197     schwarze 2428:                }
1.78      kristaps 2429:        }
1.340     schwarze 2430:        if (rr)
                   2431:                irc |= ROFF_CONT;
                   2432:        return irc;
1.74      kristaps 2433: }
                   2434:
1.266     schwarze 2435: /* --- handling of numeric and conditional expressions -------------------- */
                   2436:
1.204     schwarze 2437: /*
                   2438:  * Parse a single signed integer number.  Stop at the first non-digit.
                   2439:  * If there is at least one digit, return success and advance the
                   2440:  * parse point, else return failure and let the parse point unchanged.
                   2441:  * Ignore overflows, treat them just like the C language.
                   2442:  */
1.184     schwarze 2443: static int
1.261     schwarze 2444: roff_getnum(const char *v, int *pos, int *res, int flags)
1.184     schwarze 2445: {
1.261     schwarze 2446:        int      myres, scaled, n, p;
1.206     schwarze 2447:
                   2448:        if (NULL == res)
                   2449:                res = &myres;
1.184     schwarze 2450:
                   2451:        p = *pos;
                   2452:        n = v[p] == '-';
1.261     schwarze 2453:        if (n || v[p] == '+')
1.184     schwarze 2454:                p++;
                   2455:
1.261     schwarze 2456:        if (flags & ROFFNUM_WHITE)
                   2457:                while (isspace((unsigned char)v[p]))
                   2458:                        p++;
                   2459:
1.184     schwarze 2460:        for (*res = 0; isdigit((unsigned char)v[p]); p++)
1.204     schwarze 2461:                *res = 10 * *res + v[p] - '0';
1.184     schwarze 2462:        if (p == *pos + n)
                   2463:                return 0;
                   2464:
                   2465:        if (n)
                   2466:                *res = -*res;
                   2467:
1.254     schwarze 2468:        /* Each number may be followed by one optional scaling unit. */
                   2469:
                   2470:        switch (v[p]) {
                   2471:        case 'f':
1.261     schwarze 2472:                scaled = *res * 65536;
1.254     schwarze 2473:                break;
                   2474:        case 'i':
1.261     schwarze 2475:                scaled = *res * 240;
1.254     schwarze 2476:                break;
                   2477:        case 'c':
1.261     schwarze 2478:                scaled = *res * 240 / 2.54;
1.254     schwarze 2479:                break;
                   2480:        case 'v':
                   2481:        case 'P':
1.261     schwarze 2482:                scaled = *res * 40;
1.254     schwarze 2483:                break;
                   2484:        case 'm':
                   2485:        case 'n':
1.261     schwarze 2486:                scaled = *res * 24;
1.254     schwarze 2487:                break;
                   2488:        case 'p':
1.261     schwarze 2489:                scaled = *res * 10 / 3;
1.254     schwarze 2490:                break;
                   2491:        case 'u':
1.261     schwarze 2492:                scaled = *res;
1.254     schwarze 2493:                break;
                   2494:        case 'M':
1.261     schwarze 2495:                scaled = *res * 6 / 25;
1.254     schwarze 2496:                break;
                   2497:        default:
1.261     schwarze 2498:                scaled = *res;
1.254     schwarze 2499:                p--;
                   2500:                break;
                   2501:        }
1.261     schwarze 2502:        if (flags & ROFFNUM_SCALE)
                   2503:                *res = scaled;
1.254     schwarze 2504:
                   2505:        *pos = p + 1;
1.277     schwarze 2506:        return 1;
1.184     schwarze 2507: }
                   2508:
1.198     schwarze 2509: /*
                   2510:  * Evaluate a string comparison condition.
                   2511:  * The first character is the delimiter.
                   2512:  * Succeed if the string up to its second occurrence
                   2513:  * matches the string up to its third occurence.
                   2514:  * Advance the cursor after the third occurrence
                   2515:  * or lacking that, to the end of the line.
                   2516:  */
                   2517: static int
                   2518: roff_evalstrcond(const char *v, int *pos)
                   2519: {
                   2520:        const char      *s1, *s2, *s3;
                   2521:        int              match;
                   2522:
                   2523:        match = 0;
                   2524:        s1 = v + *pos;          /* initial delimiter */
                   2525:        s2 = s1 + 1;            /* for scanning the first string */
                   2526:        s3 = strchr(s2, *s1);   /* for scanning the second string */
                   2527:
                   2528:        if (NULL == s3)         /* found no middle delimiter */
                   2529:                goto out;
                   2530:
                   2531:        while ('\0' != *++s3) {
                   2532:                if (*s2 != *s3) {  /* mismatch */
                   2533:                        s3 = strchr(s3, *s1);
                   2534:                        break;
                   2535:                }
                   2536:                if (*s3 == *s1) {  /* found the final delimiter */
                   2537:                        match = 1;
                   2538:                        break;
                   2539:                }
                   2540:                s2++;
                   2541:        }
                   2542:
                   2543: out:
                   2544:        if (NULL == s3)
                   2545:                s3 = strchr(s2, '\0');
1.242     schwarze 2546:        else if (*s3 != '\0')
1.198     schwarze 2547:                s3++;
                   2548:        *pos = s3 - v;
1.277     schwarze 2549:        return match;
1.198     schwarze 2550: }
                   2551:
1.204     schwarze 2552: /*
                   2553:  * Evaluate an optionally negated single character, numerical,
                   2554:  * or string condition.
                   2555:  */
1.198     schwarze 2556: static int
1.271     schwarze 2557: roff_evalcond(struct roff *r, int ln, char *v, int *pos)
1.88      kristaps 2558: {
1.336     schwarze 2559:        const char      *start, *end;
                   2560:        char            *cp, *name;
                   2561:        size_t           sz;
                   2562:        int              deftype, len, number, savepos, istrue, wanttrue;
1.88      kristaps 2563:
1.198     schwarze 2564:        if ('!' == v[*pos]) {
                   2565:                wanttrue = 0;
                   2566:                (*pos)++;
                   2567:        } else
                   2568:                wanttrue = 1;
                   2569:
1.88      kristaps 2570:        switch (v[*pos]) {
1.240     schwarze 2571:        case '\0':
1.277     schwarze 2572:                return 0;
1.207     schwarze 2573:        case 'n':
                   2574:        case 'o':
1.88      kristaps 2575:                (*pos)++;
1.277     schwarze 2576:                return wanttrue;
1.207     schwarze 2577:        case 'e':
                   2578:        case 't':
1.239     schwarze 2579:        case 'v':
1.88      kristaps 2580:                (*pos)++;
1.277     schwarze 2581:                return !wanttrue;
1.336     schwarze 2582:        case 'c':
                   2583:                do {
                   2584:                        (*pos)++;
                   2585:                } while (v[*pos] == ' ');
                   2586:
                   2587:                /*
                   2588:                 * Quirk for groff compatibility:
                   2589:                 * The horizontal tab is neither available nor unavailable.
                   2590:                 */
                   2591:
                   2592:                if (v[*pos] == '\t') {
                   2593:                        (*pos)++;
                   2594:                        return 0;
                   2595:                }
                   2596:
                   2597:                /* Printable ASCII characters are available. */
                   2598:
                   2599:                if (v[*pos] != '\\') {
                   2600:                        (*pos)++;
                   2601:                        return wanttrue;
                   2602:                }
                   2603:
                   2604:                end = v + ++*pos;
                   2605:                switch (mandoc_escape(&end, &start, &len)) {
                   2606:                case ESCAPE_SPECIAL:
                   2607:                        istrue = mchars_spec2cp(start, len) != -1;
                   2608:                        break;
                   2609:                case ESCAPE_UNICODE:
                   2610:                        istrue = 1;
                   2611:                        break;
                   2612:                case ESCAPE_NUMBERED:
                   2613:                        istrue = mchars_num2char(start, len) != -1;
                   2614:                        break;
                   2615:                default:
                   2616:                        istrue = !wanttrue;
                   2617:                        break;
                   2618:                }
                   2619:                *pos = end - v;
                   2620:                return istrue == wanttrue;
1.310     schwarze 2621:        case 'd':
1.271     schwarze 2622:        case 'r':
1.310     schwarze 2623:                cp = v + *pos + 1;
                   2624:                while (*cp == ' ')
                   2625:                        cp++;
                   2626:                name = cp;
                   2627:                sz = roff_getname(r, &cp, ln, cp - v);
1.315     schwarze 2628:                if (sz == 0)
                   2629:                        istrue = 0;
                   2630:                else if (v[*pos] == 'r')
                   2631:                        istrue = roff_hasregn(r, name, sz);
                   2632:                else {
                   2633:                        deftype = ROFFDEF_ANY;
                   2634:                        roff_getstrn(r, name, sz, &deftype);
                   2635:                        istrue = !!deftype;
                   2636:                }
1.363     schwarze 2637:                *pos = (name + sz) - v;
1.310     schwarze 2638:                return istrue == wanttrue;
1.88      kristaps 2639:        default:
                   2640:                break;
                   2641:        }
                   2642:
1.241     schwarze 2643:        savepos = *pos;
1.261     schwarze 2644:        if (roff_evalnum(r, ln, v, pos, &number, ROFFNUM_SCALE))
1.277     schwarze 2645:                return (number > 0) == wanttrue;
1.241     schwarze 2646:        else if (*pos == savepos)
1.277     schwarze 2647:                return roff_evalstrcond(v, pos) == wanttrue;
1.204     schwarze 2648:        else
1.277     schwarze 2649:                return 0;
1.88      kristaps 2650: }
                   2651:
1.340     schwarze 2652: static int
1.103     kristaps 2653: roff_line_ignore(ROFF_ARGS)
1.89      kristaps 2654: {
1.123     schwarze 2655:
1.277     schwarze 2656:        return ROFF_IGN;
1.89      kristaps 2657: }
                   2658:
1.340     schwarze 2659: static int
1.251     schwarze 2660: roff_insec(ROFF_ARGS)
                   2661: {
                   2662:
1.350     schwarze 2663:        mandoc_msg(MANDOCERR_REQ_INSEC, ln, ppos, "%s", roff_name[tok]);
1.277     schwarze 2664:        return ROFF_IGN;
1.251     schwarze 2665: }
                   2666:
1.340     schwarze 2667: static int
1.251     schwarze 2668: roff_unsupp(ROFF_ARGS)
                   2669: {
                   2670:
1.350     schwarze 2671:        mandoc_msg(MANDOCERR_REQ_UNSUPP, ln, ppos, "%s", roff_name[tok]);
1.277     schwarze 2672:        return ROFF_IGN;
1.251     schwarze 2673: }
                   2674:
1.340     schwarze 2675: static int
1.82      kristaps 2676: roff_cond(ROFF_ARGS)
1.74      kristaps 2677: {
1.340     schwarze 2678:        int      irc;
1.173     schwarze 2679:
                   2680:        roffnode_push(r, tok, NULL, ln, ppos);
1.74      kristaps 2681:
1.207     schwarze 2682:        /*
1.134     kristaps 2683:         * An `.el' has no conditional body: it will consume the value
                   2684:         * of the current rstack entry set in prior `ie' calls or
1.207     schwarze 2685:         * defaults to DENY.
1.134     kristaps 2686:         *
                   2687:         * If we're not an `el', however, then evaluate the conditional.
                   2688:         */
1.133     kristaps 2689:
1.238     schwarze 2690:        r->last->rule = tok == ROFF_el ?
1.207     schwarze 2691:            (r->rstackpos < 0 ? 0 : r->rstack[r->rstackpos--]) :
1.238     schwarze 2692:            roff_evalcond(r, ln, buf->buf, &pos);
1.77      kristaps 2693:
1.134     kristaps 2694:        /*
                   2695:         * An if-else will put the NEGATION of the current evaluated
                   2696:         * conditional into the stack of rules.
                   2697:         */
                   2698:
1.238     schwarze 2699:        if (tok == ROFF_ie) {
1.223     schwarze 2700:                if (r->rstackpos + 1 == r->rstacksz) {
                   2701:                        r->rstacksz += 16;
                   2702:                        r->rstack = mandoc_reallocarray(r->rstack,
                   2703:                            r->rstacksz, sizeof(int));
1.134     kristaps 2704:                }
1.198     schwarze 2705:                r->rstack[++r->rstackpos] = !r->last->rule;
1.82      kristaps 2706:        }
1.88      kristaps 2707:
                   2708:        /* If the parent has false as its rule, then so do we. */
                   2709:
1.198     schwarze 2710:        if (r->last->parent && !r->last->parent->rule)
                   2711:                r->last->rule = 0;
1.88      kristaps 2712:
                   2713:        /*
1.173     schwarze 2714:         * Determine scope.
                   2715:         * If there is nothing on the line after the conditional,
                   2716:         * not even whitespace, use next-line scope.
1.340     schwarze 2717:         * Except that .while does not support next-line scope.
1.88      kristaps 2718:         */
1.74      kristaps 2719:
1.340     schwarze 2720:        if (buf->buf[pos] == '\0' && tok != ROFF_while) {
1.173     schwarze 2721:                r->last->endspan = 2;
                   2722:                goto out;
                   2723:        }
                   2724:
1.238     schwarze 2725:        while (buf->buf[pos] == ' ')
1.173     schwarze 2726:                pos++;
                   2727:
                   2728:        /* An opening brace requests multiline scope. */
1.75      kristaps 2729:
1.238     schwarze 2730:        if (buf->buf[pos] == '\\' && buf->buf[pos + 1] == '{') {
1.75      kristaps 2731:                r->last->endspan = -1;
                   2732:                pos += 2;
1.272     schwarze 2733:                while (buf->buf[pos] == ' ')
                   2734:                        pos++;
1.173     schwarze 2735:                goto out;
1.207     schwarze 2736:        }
1.74      kristaps 2737:
1.77      kristaps 2738:        /*
1.173     schwarze 2739:         * Anything else following the conditional causes
                   2740:         * single-line scope.  Warn if the scope contains
                   2741:         * nothing but trailing whitespace.
1.77      kristaps 2742:         */
                   2743:
1.238     schwarze 2744:        if (buf->buf[pos] == '\0')
1.350     schwarze 2745:                mandoc_msg(MANDOCERR_COND_EMPTY,
                   2746:                    ln, ppos, "%s", roff_name[tok]);
1.77      kristaps 2747:
1.173     schwarze 2748:        r->last->endspan = 1;
1.74      kristaps 2749:
1.173     schwarze 2750: out:
1.75      kristaps 2751:        *offs = pos;
1.340     schwarze 2752:        irc = ROFF_RERUN;
                   2753:        if (tok == ROFF_while)
                   2754:                irc |= ROFF_WHILE;
                   2755:        return irc;
1.83      schwarze 2756: }
                   2757:
1.340     schwarze 2758: static int
1.92      schwarze 2759: roff_ds(ROFF_ARGS)
                   2760: {
1.212     schwarze 2761:        char            *string;
                   2762:        const char      *name;
                   2763:        size_t           namesz;
1.96      kristaps 2764:
1.251     schwarze 2765:        /* Ignore groff compatibility mode for now. */
                   2766:
                   2767:        if (tok == ROFF_ds1)
                   2768:                tok = ROFF_ds;
                   2769:        else if (tok == ROFF_as1)
                   2770:                tok = ROFF_as;
                   2771:
1.96      kristaps 2772:        /*
1.212     schwarze 2773:         * The first word is the name of the string.
                   2774:         * If it is empty or terminated by an escape sequence,
                   2775:         * abort the `ds' request without defining anything.
1.96      kristaps 2776:         */
1.92      schwarze 2777:
1.238     schwarze 2778:        name = string = buf->buf + pos;
                   2779:        if (*name == '\0')
1.277     schwarze 2780:                return ROFF_IGN;
1.92      schwarze 2781:
1.212     schwarze 2782:        namesz = roff_getname(r, &string, ln, pos);
1.363     schwarze 2783:        switch (name[namesz]) {
                   2784:        case '\\':
1.277     schwarze 2785:                return ROFF_IGN;
1.363     schwarze 2786:        case '\t':
                   2787:                string = buf->buf + pos + namesz;
                   2788:                break;
                   2789:        default:
                   2790:                break;
                   2791:        }
1.212     schwarze 2792:
                   2793:        /* Read past the initial double-quote, if any. */
1.238     schwarze 2794:        if (*string == '"')
1.92      schwarze 2795:                string++;
                   2796:
1.96      kristaps 2797:        /* The rest is the value. */
1.212     schwarze 2798:        roff_setstrn(&r->strtab, name, namesz, string, strlen(string),
                   2799:            ROFF_as == tok);
1.311     schwarze 2800:        roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
1.277     schwarze 2801:        return ROFF_IGN;
1.92      schwarze 2802: }
                   2803:
1.204     schwarze 2804: /*
                   2805:  * Parse a single operator, one or two characters long.
                   2806:  * If the operator is recognized, return success and advance the
                   2807:  * parse point, else return failure and let the parse point unchanged.
                   2808:  */
                   2809: static int
                   2810: roff_getop(const char *v, int *pos, char *res)
                   2811: {
                   2812:
                   2813:        *res = v[*pos];
                   2814:
                   2815:        switch (*res) {
1.207     schwarze 2816:        case '+':
                   2817:        case '-':
                   2818:        case '*':
                   2819:        case '/':
                   2820:        case '%':
                   2821:        case '&':
                   2822:        case ':':
1.204     schwarze 2823:                break;
                   2824:        case '<':
                   2825:                switch (v[*pos + 1]) {
1.207     schwarze 2826:                case '=':
1.204     schwarze 2827:                        *res = 'l';
                   2828:                        (*pos)++;
                   2829:                        break;
1.207     schwarze 2830:                case '>':
1.204     schwarze 2831:                        *res = '!';
                   2832:                        (*pos)++;
                   2833:                        break;
1.207     schwarze 2834:                case '?':
1.204     schwarze 2835:                        *res = 'i';
                   2836:                        (*pos)++;
                   2837:                        break;
                   2838:                default:
                   2839:                        break;
                   2840:                }
                   2841:                break;
                   2842:        case '>':
                   2843:                switch (v[*pos + 1]) {
1.207     schwarze 2844:                case '=':
1.204     schwarze 2845:                        *res = 'g';
                   2846:                        (*pos)++;
                   2847:                        break;
1.207     schwarze 2848:                case '?':
1.204     schwarze 2849:                        *res = 'a';
                   2850:                        (*pos)++;
                   2851:                        break;
                   2852:                default:
                   2853:                        break;
                   2854:                }
                   2855:                break;
                   2856:        case '=':
                   2857:                if ('=' == v[*pos + 1])
                   2858:                        (*pos)++;
                   2859:                break;
                   2860:        default:
1.277     schwarze 2861:                return 0;
1.204     schwarze 2862:        }
                   2863:        (*pos)++;
                   2864:
1.277     schwarze 2865:        return *res;
1.204     schwarze 2866: }
                   2867:
                   2868: /*
                   2869:  * Evaluate either a parenthesized numeric expression
                   2870:  * or a single signed integer number.
                   2871:  */
                   2872: static int
1.235     schwarze 2873: roff_evalpar(struct roff *r, int ln,
1.261     schwarze 2874:        const char *v, int *pos, int *res, int flags)
1.204     schwarze 2875: {
                   2876:
                   2877:        if ('(' != v[*pos])
1.277     schwarze 2878:                return roff_getnum(v, pos, res, flags);
1.204     schwarze 2879:
                   2880:        (*pos)++;
1.261     schwarze 2881:        if ( ! roff_evalnum(r, ln, v, pos, res, flags | ROFFNUM_WHITE))
1.277     schwarze 2882:                return 0;
1.204     schwarze 2883:
1.206     schwarze 2884:        /*
                   2885:         * Omission of the closing parenthesis
                   2886:         * is an error in validation mode,
                   2887:         * but ignored in evaluation mode.
                   2888:         */
                   2889:
1.204     schwarze 2890:        if (')' == v[*pos])
                   2891:                (*pos)++;
1.206     schwarze 2892:        else if (NULL == res)
1.277     schwarze 2893:                return 0;
1.204     schwarze 2894:
1.277     schwarze 2895:        return 1;
1.204     schwarze 2896: }
                   2897:
                   2898: /*
                   2899:  * Evaluate a complete numeric expression.
                   2900:  * Proceed left to right, there is no concept of precedence.
                   2901:  */
                   2902: static int
1.235     schwarze 2903: roff_evalnum(struct roff *r, int ln, const char *v,
1.261     schwarze 2904:        int *pos, int *res, int flags)
1.204     schwarze 2905: {
                   2906:        int              mypos, operand2;
                   2907:        char             operator;
                   2908:
                   2909:        if (NULL == pos) {
                   2910:                mypos = 0;
                   2911:                pos = &mypos;
                   2912:        }
                   2913:
1.261     schwarze 2914:        if (flags & ROFFNUM_WHITE)
1.204     schwarze 2915:                while (isspace((unsigned char)v[*pos]))
                   2916:                        (*pos)++;
                   2917:
1.261     schwarze 2918:        if ( ! roff_evalpar(r, ln, v, pos, res, flags))
1.277     schwarze 2919:                return 0;
1.204     schwarze 2920:
                   2921:        while (1) {
1.261     schwarze 2922:                if (flags & ROFFNUM_WHITE)
1.204     schwarze 2923:                        while (isspace((unsigned char)v[*pos]))
                   2924:                                (*pos)++;
                   2925:
                   2926:                if ( ! roff_getop(v, pos, &operator))
                   2927:                        break;
                   2928:
1.261     schwarze 2929:                if (flags & ROFFNUM_WHITE)
1.204     schwarze 2930:                        while (isspace((unsigned char)v[*pos]))
                   2931:                                (*pos)++;
                   2932:
1.261     schwarze 2933:                if ( ! roff_evalpar(r, ln, v, pos, &operand2, flags))
1.277     schwarze 2934:                        return 0;
1.204     schwarze 2935:
1.261     schwarze 2936:                if (flags & ROFFNUM_WHITE)
1.204     schwarze 2937:                        while (isspace((unsigned char)v[*pos]))
                   2938:                                (*pos)++;
1.206     schwarze 2939:
                   2940:                if (NULL == res)
                   2941:                        continue;
1.204     schwarze 2942:
                   2943:                switch (operator) {
1.207     schwarze 2944:                case '+':
1.204     schwarze 2945:                        *res += operand2;
                   2946:                        break;
1.207     schwarze 2947:                case '-':
1.204     schwarze 2948:                        *res -= operand2;
                   2949:                        break;
1.207     schwarze 2950:                case '*':
1.204     schwarze 2951:                        *res *= operand2;
                   2952:                        break;
1.207     schwarze 2953:                case '/':
1.244     schwarze 2954:                        if (operand2 == 0) {
1.235     schwarze 2955:                                mandoc_msg(MANDOCERR_DIVZERO,
1.350     schwarze 2956:                                        ln, *pos, "%s", v);
1.234     kristaps 2957:                                *res = 0;
                   2958:                                break;
                   2959:                        }
1.204     schwarze 2960:                        *res /= operand2;
                   2961:                        break;
1.207     schwarze 2962:                case '%':
1.244     schwarze 2963:                        if (operand2 == 0) {
                   2964:                                mandoc_msg(MANDOCERR_DIVZERO,
1.350     schwarze 2965:                                        ln, *pos, "%s", v);
1.244     schwarze 2966:                                *res = 0;
                   2967:                                break;
                   2968:                        }
1.204     schwarze 2969:                        *res %= operand2;
                   2970:                        break;
1.207     schwarze 2971:                case '<':
1.204     schwarze 2972:                        *res = *res < operand2;
                   2973:                        break;
1.207     schwarze 2974:                case '>':
1.204     schwarze 2975:                        *res = *res > operand2;
                   2976:                        break;
1.207     schwarze 2977:                case 'l':
1.204     schwarze 2978:                        *res = *res <= operand2;
                   2979:                        break;
1.207     schwarze 2980:                case 'g':
1.204     schwarze 2981:                        *res = *res >= operand2;
                   2982:                        break;
1.207     schwarze 2983:                case '=':
1.204     schwarze 2984:                        *res = *res == operand2;
                   2985:                        break;
1.207     schwarze 2986:                case '!':
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 'i':
1.204     schwarze 2996:                        if (operand2 < *res)
                   2997:                                *res = operand2;
                   2998:                        break;
1.207     schwarze 2999:                case 'a':
1.204     schwarze 3000:                        if (operand2 > *res)
                   3001:                                *res = operand2;
                   3002:                        break;
                   3003:                default:
                   3004:                        abort();
                   3005:                }
                   3006:        }
1.277     schwarze 3007:        return 1;
1.204     schwarze 3008: }
                   3009:
1.266     schwarze 3010: /* --- register management ------------------------------------------------ */
                   3011:
1.180     schwarze 3012: void
1.187     schwarze 3013: roff_setreg(struct roff *r, const char *name, int val, char sign)
1.147     kristaps 3014: {
1.327     schwarze 3015:        roff_setregn(r, name, strlen(name), val, sign, INT_MIN);
1.326     schwarze 3016: }
                   3017:
                   3018: static void
                   3019: roff_setregn(struct roff *r, const char *name, size_t len,
1.327     schwarze 3020:     int val, char sign, int step)
1.326     schwarze 3021: {
1.180     schwarze 3022:        struct roffreg  *reg;
                   3023:
                   3024:        /* Search for an existing register with the same name. */
                   3025:        reg = r->regtab;
                   3026:
1.326     schwarze 3027:        while (reg != NULL && (reg->key.sz != len ||
                   3028:            strncmp(reg->key.p, name, len) != 0))
1.180     schwarze 3029:                reg = reg->next;
1.147     kristaps 3030:
1.180     schwarze 3031:        if (NULL == reg) {
                   3032:                /* Create a new register. */
                   3033:                reg = mandoc_malloc(sizeof(struct roffreg));
1.326     schwarze 3034:                reg->key.p = mandoc_strndup(name, len);
                   3035:                reg->key.sz = len;
1.187     schwarze 3036:                reg->val = 0;
1.327     schwarze 3037:                reg->step = 0;
1.180     schwarze 3038:                reg->next = r->regtab;
                   3039:                r->regtab = reg;
                   3040:        }
                   3041:
1.187     schwarze 3042:        if ('+' == sign)
                   3043:                reg->val += val;
                   3044:        else if ('-' == sign)
                   3045:                reg->val -= val;
                   3046:        else
                   3047:                reg->val = val;
1.327     schwarze 3048:        if (step != INT_MIN)
                   3049:                reg->step = step;
1.147     kristaps 3050: }
                   3051:
1.192     schwarze 3052: /*
                   3053:  * Handle some predefined read-only number registers.
                   3054:  * For now, return -1 if the requested register is not predefined;
                   3055:  * in case a predefined read-only register having the value -1
                   3056:  * were to turn up, another special value would have to be chosen.
                   3057:  */
                   3058: static int
1.273     schwarze 3059: roff_getregro(const struct roff *r, const char *name)
1.192     schwarze 3060: {
                   3061:
                   3062:        switch (*name) {
1.273     schwarze 3063:        case '$':  /* Number of arguments of the last macro evaluated. */
1.339     schwarze 3064:                return r->mstackpos < 0 ? 0 : r->mstack[r->mstackpos].argc;
1.207     schwarze 3065:        case 'A':  /* ASCII approximation mode is always off. */
1.277     schwarze 3066:                return 0;
1.207     schwarze 3067:        case 'g':  /* Groff compatibility mode is always on. */
1.277     schwarze 3068:                return 1;
1.207     schwarze 3069:        case 'H':  /* Fixed horizontal resolution. */
1.277     schwarze 3070:                return 24;
1.207     schwarze 3071:        case 'j':  /* Always adjust left margin only. */
1.277     schwarze 3072:                return 0;
1.207     schwarze 3073:        case 'T':  /* Some output device is always defined. */
1.277     schwarze 3074:                return 1;
1.207     schwarze 3075:        case 'V':  /* Fixed vertical resolution. */
1.277     schwarze 3076:                return 40;
1.192     schwarze 3077:        default:
1.277     schwarze 3078:                return -1;
1.192     schwarze 3079:        }
                   3080: }
                   3081:
1.181     schwarze 3082: int
1.326     schwarze 3083: roff_getreg(struct roff *r, const char *name)
1.147     kristaps 3084: {
1.327     schwarze 3085:        return roff_getregn(r, name, strlen(name), '\0');
1.181     schwarze 3086: }
                   3087:
                   3088: static int
1.327     schwarze 3089: roff_getregn(struct roff *r, const char *name, size_t len, char sign)
1.181     schwarze 3090: {
                   3091:        struct roffreg  *reg;
1.192     schwarze 3092:        int              val;
                   3093:
                   3094:        if ('.' == name[0] && 2 == len) {
1.273     schwarze 3095:                val = roff_getregro(r, name + 1);
1.192     schwarze 3096:                if (-1 != val)
1.277     schwarze 3097:                        return val;
1.192     schwarze 3098:        }
1.181     schwarze 3099:
1.327     schwarze 3100:        for (reg = r->regtab; reg; reg = reg->next) {
1.181     schwarze 3101:                if (len == reg->key.sz &&
1.327     schwarze 3102:                    0 == strncmp(name, reg->key.p, len)) {
                   3103:                        switch (sign) {
                   3104:                        case '+':
                   3105:                                reg->val += reg->step;
                   3106:                                break;
                   3107:                        case '-':
                   3108:                                reg->val -= reg->step;
                   3109:                                break;
                   3110:                        default:
                   3111:                                break;
                   3112:                        }
1.277     schwarze 3113:                        return reg->val;
1.327     schwarze 3114:                }
                   3115:        }
1.271     schwarze 3116:
1.327     schwarze 3117:        roff_setregn(r, name, len, 0, '\0', INT_MIN);
1.277     schwarze 3118:        return 0;
1.271     schwarze 3119: }
                   3120:
                   3121: static int
                   3122: roff_hasregn(const struct roff *r, const char *name, size_t len)
                   3123: {
                   3124:        struct roffreg  *reg;
                   3125:        int              val;
                   3126:
                   3127:        if ('.' == name[0] && 2 == len) {
1.273     schwarze 3128:                val = roff_getregro(r, name + 1);
1.271     schwarze 3129:                if (-1 != val)
1.277     schwarze 3130:                        return 1;
1.271     schwarze 3131:        }
                   3132:
                   3133:        for (reg = r->regtab; reg; reg = reg->next)
                   3134:                if (len == reg->key.sz &&
                   3135:                    0 == strncmp(name, reg->key.p, len))
1.277     schwarze 3136:                        return 1;
1.147     kristaps 3137:
1.277     schwarze 3138:        return 0;
1.147     kristaps 3139: }
                   3140:
1.180     schwarze 3141: static void
                   3142: roff_freereg(struct roffreg *reg)
1.147     kristaps 3143: {
1.180     schwarze 3144:        struct roffreg  *old_reg;
1.147     kristaps 3145:
1.180     schwarze 3146:        while (NULL != reg) {
                   3147:                free(reg->key.p);
                   3148:                old_reg = reg;
                   3149:                reg = reg->next;
                   3150:                free(old_reg);
                   3151:        }
1.147     kristaps 3152: }
1.92      schwarze 3153:
1.340     schwarze 3154: static int
1.89      kristaps 3155: roff_nr(ROFF_ARGS)
1.83      schwarze 3156: {
1.327     schwarze 3157:        char            *key, *val, *step;
1.212     schwarze 3158:        size_t           keysz;
1.327     schwarze 3159:        int              iv, is, len;
1.187     schwarze 3160:        char             sign;
1.89      kristaps 3161:
1.238     schwarze 3162:        key = val = buf->buf + pos;
                   3163:        if (*key == '\0')
1.277     schwarze 3164:                return ROFF_IGN;
1.212     schwarze 3165:
                   3166:        keysz = roff_getname(r, &val, ln, pos);
1.363     schwarze 3167:        if (key[keysz] == '\\' || key[keysz] == '\t')
1.277     schwarze 3168:                return ROFF_IGN;
1.89      kristaps 3169:
1.187     schwarze 3170:        sign = *val;
1.238     schwarze 3171:        if (sign == '+' || sign == '-')
1.187     schwarze 3172:                val++;
                   3173:
1.327     schwarze 3174:        len = 0;
                   3175:        if (roff_evalnum(r, ln, val, &len, &iv, ROFFNUM_SCALE) == 0)
                   3176:                return ROFF_IGN;
                   3177:
                   3178:        step = val + len;
                   3179:        while (isspace((unsigned char)*step))
                   3180:                step++;
                   3181:        if (roff_evalnum(r, ln, step, NULL, &is, 0) == 0)
                   3182:                is = INT_MIN;
1.109     kristaps 3183:
1.327     schwarze 3184:        roff_setregn(r, key, keysz, iv, sign, is);
1.277     schwarze 3185:        return ROFF_IGN;
1.203     schwarze 3186: }
                   3187:
1.340     schwarze 3188: static int
1.203     schwarze 3189: roff_rr(ROFF_ARGS)
                   3190: {
                   3191:        struct roffreg  *reg, **prev;
1.212     schwarze 3192:        char            *name, *cp;
                   3193:        size_t           namesz;
1.203     schwarze 3194:
1.238     schwarze 3195:        name = cp = buf->buf + pos;
                   3196:        if (*name == '\0')
1.277     schwarze 3197:                return ROFF_IGN;
1.212     schwarze 3198:        namesz = roff_getname(r, &cp, ln, pos);
                   3199:        name[namesz] = '\0';
1.203     schwarze 3200:
                   3201:        prev = &r->regtab;
                   3202:        while (1) {
                   3203:                reg = *prev;
1.238     schwarze 3204:                if (reg == NULL || !strcmp(name, reg->key.p))
1.203     schwarze 3205:                        break;
                   3206:                prev = &reg->next;
                   3207:        }
1.238     schwarze 3208:        if (reg != NULL) {
1.203     schwarze 3209:                *prev = reg->next;
                   3210:                free(reg->key.p);
                   3211:                free(reg);
                   3212:        }
1.277     schwarze 3213:        return ROFF_IGN;
1.122     schwarze 3214: }
                   3215:
1.266     schwarze 3216: /* --- handler functions for roff requests -------------------------------- */
                   3217:
1.340     schwarze 3218: static int
1.122     schwarze 3219: roff_rm(ROFF_ARGS)
                   3220: {
                   3221:        const char       *name;
                   3222:        char             *cp;
1.212     schwarze 3223:        size_t            namesz;
1.122     schwarze 3224:
1.238     schwarze 3225:        cp = buf->buf + pos;
                   3226:        while (*cp != '\0') {
1.212     schwarze 3227:                name = cp;
1.238     schwarze 3228:                namesz = roff_getname(r, &cp, ln, (int)(cp - buf->buf));
1.212     schwarze 3229:                roff_setstrn(&r->strtab, name, namesz, NULL, 0, 0);
1.311     schwarze 3230:                roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
1.363     schwarze 3231:                if (name[namesz] == '\\' || name[namesz] == '\t')
1.212     schwarze 3232:                        break;
1.122     schwarze 3233:        }
1.277     schwarze 3234:        return ROFF_IGN;
1.178     schwarze 3235: }
                   3236:
1.340     schwarze 3237: static int
1.178     schwarze 3238: roff_it(ROFF_ARGS)
                   3239: {
                   3240:        int              iv;
                   3241:
                   3242:        /* Parse the number of lines. */
1.261     schwarze 3243:
                   3244:        if ( ! roff_evalnum(r, ln, buf->buf, &pos, &iv, 0)) {
1.350     schwarze 3245:                mandoc_msg(MANDOCERR_IT_NONUM,
                   3246:                    ln, ppos, "%s", buf->buf + 1);
1.277     schwarze 3247:                return ROFF_IGN;
1.178     schwarze 3248:        }
                   3249:
1.262     schwarze 3250:        while (isspace((unsigned char)buf->buf[pos]))
                   3251:                pos++;
                   3252:
                   3253:        /*
                   3254:         * Arm the input line trap.
                   3255:         * Special-casing "an-trap" is an ugly workaround to cope
                   3256:         * with DocBook stupidly fiddling with man(7) internals.
                   3257:         */
1.261     schwarze 3258:
1.178     schwarze 3259:        roffit_lines = iv;
1.262     schwarze 3260:        roffit_macro = mandoc_strdup(iv != 1 ||
                   3261:            strcmp(buf->buf + pos, "an-trap") ?
                   3262:            buf->buf + pos : "br");
1.277     schwarze 3263:        return ROFF_IGN;
1.175     schwarze 3264: }
                   3265:
1.340     schwarze 3266: static int
1.175     schwarze 3267: roff_Dd(ROFF_ARGS)
                   3268: {
1.315     schwarze 3269:        int              mask;
                   3270:        enum roff_tok    t, te;
1.227     schwarze 3271:
1.315     schwarze 3272:        switch (tok) {
                   3273:        case ROFF_Dd:
                   3274:                tok = MDOC_Dd;
                   3275:                te = MDOC_MAX;
                   3276:                if (r->format == 0)
                   3277:                        r->format = MPARSE_MDOC;
                   3278:                mask = MPARSE_MDOC | MPARSE_QUICK;
                   3279:                break;
                   3280:        case ROFF_TH:
                   3281:                tok = MAN_TH;
                   3282:                te = MAN_MAX;
                   3283:                if (r->format == 0)
                   3284:                        r->format = MPARSE_MAN;
                   3285:                mask = MPARSE_QUICK;
                   3286:                break;
                   3287:        default:
                   3288:                abort();
                   3289:        }
                   3290:        if ((r->options & mask) == 0)
                   3291:                for (t = tok; t < te; t++)
                   3292:                        roff_setstr(r, roff_name[t], NULL, 0);
1.277     schwarze 3293:        return ROFF_CONT;
1.109     kristaps 3294: }
                   3295:
1.340     schwarze 3296: static int
1.109     kristaps 3297: roff_TE(ROFF_ARGS)
                   3298: {
1.361     schwarze 3299:        r->man->flags &= ~ROFF_NONOFILL;
1.321     schwarze 3300:        if (r->tbl == NULL) {
1.350     schwarze 3301:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "TE");
1.321     schwarze 3302:                return ROFF_IGN;
                   3303:        }
1.346     schwarze 3304:        if (tbl_end(r->tbl, 0) == 0) {
1.321     schwarze 3305:                r->tbl = NULL;
1.258     schwarze 3306:                free(buf->buf);
                   3307:                buf->buf = mandoc_strdup(".sp");
                   3308:                buf->sz = 4;
1.329     schwarze 3309:                *offs = 0;
1.277     schwarze 3310:                return ROFF_REPARSE;
1.258     schwarze 3311:        }
1.321     schwarze 3312:        r->tbl = NULL;
1.277     schwarze 3313:        return ROFF_IGN;
1.112     kristaps 3314: }
                   3315:
1.340     schwarze 3316: static int
1.112     kristaps 3317: roff_T_(ROFF_ARGS)
                   3318: {
                   3319:
                   3320:        if (NULL == r->tbl)
1.350     schwarze 3321:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "T&");
1.112     kristaps 3322:        else
1.296     schwarze 3323:                tbl_restart(ln, ppos, r->tbl);
1.112     kristaps 3324:
1.277     schwarze 3325:        return ROFF_IGN;
1.109     kristaps 3326: }
                   3327:
1.230     schwarze 3328: /*
                   3329:  * Handle in-line equation delimiters.
                   3330:  */
1.340     schwarze 3331: static int
1.238     schwarze 3332: roff_eqndelim(struct roff *r, struct buf *buf, int pos)
1.151     kristaps 3333: {
1.233     schwarze 3334:        char            *cp1, *cp2;
                   3335:        const char      *bef_pr, *bef_nl, *mac, *aft_nl, *aft_pr;
1.151     kristaps 3336:
1.230     schwarze 3337:        /*
                   3338:         * Outside equations, look for an opening delimiter.
                   3339:         * If we are inside an equation, we already know it is
                   3340:         * in-line, or this function wouldn't have been called;
                   3341:         * so look for a closing delimiter.
                   3342:         */
                   3343:
1.238     schwarze 3344:        cp1 = buf->buf + pos;
1.230     schwarze 3345:        cp2 = strchr(cp1, r->eqn == NULL ?
                   3346:            r->last_eqn->odelim : r->last_eqn->cdelim);
                   3347:        if (cp2 == NULL)
1.277     schwarze 3348:                return ROFF_CONT;
1.230     schwarze 3349:
1.233     schwarze 3350:        *cp2++ = '\0';
                   3351:        bef_pr = bef_nl = aft_nl = aft_pr = "";
                   3352:
                   3353:        /* Handle preceding text, protecting whitespace. */
                   3354:
1.238     schwarze 3355:        if (*buf->buf != '\0') {
1.233     schwarze 3356:                if (r->eqn == NULL)
                   3357:                        bef_pr = "\\&";
                   3358:                bef_nl = "\n";
                   3359:        }
                   3360:
                   3361:        /*
                   3362:         * Prepare replacing the delimiter with an equation macro
                   3363:         * and drop leading white space from the equation.
                   3364:         */
1.230     schwarze 3365:
1.233     schwarze 3366:        if (r->eqn == NULL) {
                   3367:                while (*cp2 == ' ')
                   3368:                        cp2++;
                   3369:                mac = ".EQ";
                   3370:        } else
                   3371:                mac = ".EN";
                   3372:
                   3373:        /* Handle following text, protecting whitespace. */
                   3374:
                   3375:        if (*cp2 != '\0') {
                   3376:                aft_nl = "\n";
                   3377:                if (r->eqn != NULL)
                   3378:                        aft_pr = "\\&";
                   3379:        }
                   3380:
                   3381:        /* Do the actual replacement. */
                   3382:
1.238     schwarze 3383:        buf->sz = mandoc_asprintf(&cp1, "%s%s%s%s%s%s%s", buf->buf,
1.233     schwarze 3384:            bef_pr, bef_nl, mac, aft_nl, aft_pr, cp2) + 1;
1.238     schwarze 3385:        free(buf->buf);
                   3386:        buf->buf = cp1;
1.230     schwarze 3387:
                   3388:        /* Toggle the in-line state of the eqn subsystem. */
                   3389:
                   3390:        r->eqn_inline = r->eqn == NULL;
1.277     schwarze 3391:        return ROFF_REPARSE;
1.151     kristaps 3392: }
                   3393:
1.340     schwarze 3394: static int
1.235     schwarze 3395: roff_EQ(ROFF_ARGS)
1.125     kristaps 3396: {
1.319     schwarze 3397:        struct roff_node        *n;
                   3398:
1.356     schwarze 3399:        if (r->man->meta.macroset == MACROSET_MAN)
1.322     schwarze 3400:                man_breakscope(r->man, ROFF_EQ);
1.319     schwarze 3401:        n = roff_node_alloc(r->man, ln, ppos, ROFFT_EQN, TOKEN_NONE);
                   3402:        if (ln > r->man->last->line)
                   3403:                n->flags |= NODE_LINE;
1.348     schwarze 3404:        n->eqn = eqn_box_new();
1.319     schwarze 3405:        roff_node_append(r->man, n);
                   3406:        r->man->next = ROFF_NEXT_SIBLING;
1.125     kristaps 3407:
1.238     schwarze 3408:        assert(r->eqn == NULL);
1.319     schwarze 3409:        if (r->last_eqn == NULL)
1.351     schwarze 3410:                r->last_eqn = eqn_alloc();
1.319     schwarze 3411:        else
                   3412:                eqn_reset(r->last_eqn);
                   3413:        r->eqn = r->last_eqn;
                   3414:        r->eqn->node = n;
1.151     kristaps 3415:
1.238     schwarze 3416:        if (buf->buf[pos] != '\0')
1.350     schwarze 3417:                mandoc_msg(MANDOCERR_ARG_SKIP, ln, pos,
1.238     schwarze 3418:                    ".EQ %s", buf->buf + pos);
1.151     kristaps 3419:
1.277     schwarze 3420:        return ROFF_IGN;
1.125     kristaps 3421: }
                   3422:
1.340     schwarze 3423: static int
1.125     kristaps 3424: roff_EN(ROFF_ARGS)
                   3425: {
1.319     schwarze 3426:        if (r->eqn != NULL) {
                   3427:                eqn_parse(r->eqn);
                   3428:                r->eqn = NULL;
                   3429:        } else
1.350     schwarze 3430:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, ppos, "EN");
1.319     schwarze 3431:        if (buf->buf[pos] != '\0')
1.350     schwarze 3432:                mandoc_msg(MANDOCERR_ARG_SKIP, ln, pos,
1.319     schwarze 3433:                    "EN %s", buf->buf + pos);
1.277     schwarze 3434:        return ROFF_IGN;
1.125     kristaps 3435: }
                   3436:
1.340     schwarze 3437: static int
1.109     kristaps 3438: roff_TS(ROFF_ARGS)
                   3439: {
1.321     schwarze 3440:        if (r->tbl != NULL) {
1.350     schwarze 3441:                mandoc_msg(MANDOCERR_BLK_BROKEN, ln, ppos, "TS breaks TS");
1.346     schwarze 3442:                tbl_end(r->tbl, 0);
1.115     kristaps 3443:        }
1.361     schwarze 3444:        r->man->flags |= ROFF_NONOFILL;
1.351     schwarze 3445:        r->tbl = tbl_alloc(ppos, ln, r->last_tbl);
1.346     schwarze 3446:        if (r->last_tbl == NULL)
1.321     schwarze 3447:                r->first_tbl = r->tbl;
                   3448:        r->last_tbl = r->tbl;
1.297     schwarze 3449:        return ROFF_IGN;
                   3450: }
                   3451:
1.340     schwarze 3452: static int
1.358     schwarze 3453: roff_noarg(ROFF_ARGS)
                   3454: {
                   3455:        if (r->man->flags & (MAN_BLINE | MAN_ELINE))
                   3456:                man_breakscope(r->man, tok);
                   3457:        if (tok == ROFF_brp)
                   3458:                tok = ROFF_br;
                   3459:        roff_elem_alloc(r->man, ln, ppos, tok);
                   3460:        if (buf->buf[pos] != '\0')
                   3461:                mandoc_msg(MANDOCERR_ARG_SKIP, ln, pos,
                   3462:                   "%s %s", roff_name[tok], buf->buf + pos);
                   3463:        if (tok == ROFF_nf)
                   3464:                r->man->flags |= ROFF_NOFILL;
                   3465:        else if (tok == ROFF_fi)
                   3466:                r->man->flags &= ~ROFF_NOFILL;
                   3467:        r->man->last->flags |= NODE_LINE | NODE_VALID | NODE_ENDED;
                   3468:        r->man->next = ROFF_NEXT_SIBLING;
                   3469:        return ROFF_IGN;
                   3470: }
                   3471:
                   3472: static int
1.297     schwarze 3473: roff_onearg(ROFF_ARGS)
                   3474: {
                   3475:        struct roff_node        *n;
                   3476:        char                    *cp;
1.305     schwarze 3477:        int                      npos;
1.297     schwarze 3478:
1.302     schwarze 3479:        if (r->man->flags & (MAN_BLINE | MAN_ELINE) &&
1.320     schwarze 3480:            (tok == ROFF_ce || tok == ROFF_rj || tok == ROFF_sp ||
                   3481:             tok == ROFF_ti))
1.302     schwarze 3482:                man_breakscope(r->man, tok);
                   3483:
1.309     schwarze 3484:        if (roffce_node != NULL && (tok == ROFF_ce || tok == ROFF_rj)) {
1.305     schwarze 3485:                r->man->last = roffce_node;
                   3486:                r->man->next = ROFF_NEXT_SIBLING;
                   3487:        }
                   3488:
1.297     schwarze 3489:        roff_elem_alloc(r->man, ln, ppos, tok);
                   3490:        n = r->man->last;
                   3491:
                   3492:        cp = buf->buf + pos;
                   3493:        if (*cp != '\0') {
                   3494:                while (*cp != '\0' && *cp != ' ')
                   3495:                        cp++;
                   3496:                while (*cp == ' ')
                   3497:                        *cp++ = '\0';
                   3498:                if (*cp != '\0')
1.350     schwarze 3499:                        mandoc_msg(MANDOCERR_ARG_EXCESS,
                   3500:                            ln, (int)(cp - buf->buf),
1.297     schwarze 3501:                            "%s ... %s", roff_name[tok], cp);
                   3502:                roff_word_alloc(r->man, ln, pos, buf->buf + pos);
1.300     schwarze 3503:        }
                   3504:
1.309     schwarze 3505:        if (tok == ROFF_ce || tok == ROFF_rj) {
                   3506:                if (r->man->last->type == ROFFT_ELEM) {
1.305     schwarze 3507:                        roff_word_alloc(r->man, ln, pos, "1");
                   3508:                        r->man->last->flags |= NODE_NOSRC;
                   3509:                }
                   3510:                npos = 0;
                   3511:                if (roff_evalnum(r, ln, r->man->last->string, &npos,
                   3512:                    &roffce_lines, 0) == 0) {
1.350     schwarze 3513:                        mandoc_msg(MANDOCERR_CE_NONUM,
                   3514:                            ln, pos, "ce %s", buf->buf + pos);
1.305     schwarze 3515:                        roffce_lines = 1;
                   3516:                }
                   3517:                if (roffce_lines < 1) {
                   3518:                        r->man->last = r->man->last->parent;
                   3519:                        roffce_node = NULL;
                   3520:                        roffce_lines = 0;
                   3521:                } else
                   3522:                        roffce_node = r->man->last->parent;
                   3523:        } else {
                   3524:                n->flags |= NODE_VALID | NODE_ENDED;
                   3525:                r->man->last = n;
                   3526:        }
                   3527:        n->flags |= NODE_LINE;
1.300     schwarze 3528:        r->man->next = ROFF_NEXT_SIBLING;
                   3529:        return ROFF_IGN;
                   3530: }
                   3531:
1.340     schwarze 3532: static int
1.300     schwarze 3533: roff_manyarg(ROFF_ARGS)
                   3534: {
                   3535:        struct roff_node        *n;
                   3536:        char                    *sp, *ep;
                   3537:
                   3538:        roff_elem_alloc(r->man, ln, ppos, tok);
                   3539:        n = r->man->last;
                   3540:
                   3541:        for (sp = ep = buf->buf + pos; *sp != '\0'; sp = ep) {
                   3542:                while (*ep != '\0' && *ep != ' ')
                   3543:                        ep++;
                   3544:                while (*ep == ' ')
                   3545:                        *ep++ = '\0';
                   3546:                roff_word_alloc(r->man, ln, sp - buf->buf, sp);
1.297     schwarze 3547:        }
                   3548:
                   3549:        n->flags |= NODE_LINE | NODE_VALID | NODE_ENDED;
                   3550:        r->man->last = n;
                   3551:        r->man->next = ROFF_NEXT_SIBLING;
1.277     schwarze 3552:        return ROFF_IGN;
1.251     schwarze 3553: }
                   3554:
1.340     schwarze 3555: static int
1.311     schwarze 3556: roff_als(ROFF_ARGS)
                   3557: {
                   3558:        char            *oldn, *newn, *end, *value;
                   3559:        size_t           oldsz, newsz, valsz;
                   3560:
                   3561:        newn = oldn = buf->buf + pos;
                   3562:        if (*newn == '\0')
                   3563:                return ROFF_IGN;
                   3564:
                   3565:        newsz = roff_getname(r, &oldn, ln, pos);
1.363     schwarze 3566:        if (newn[newsz] == '\\' || newn[newsz] == '\t' || *oldn == '\0')
1.311     schwarze 3567:                return ROFF_IGN;
                   3568:
                   3569:        end = oldn;
                   3570:        oldsz = roff_getname(r, &end, ln, oldn - buf->buf);
                   3571:        if (oldsz == 0)
                   3572:                return ROFF_IGN;
                   3573:
1.338     schwarze 3574:        valsz = mandoc_asprintf(&value, ".%.*s \\$@\\\"\n",
1.315     schwarze 3575:            (int)oldsz, oldn);
1.311     schwarze 3576:        roff_setstrn(&r->strtab, newn, newsz, value, valsz, 0);
                   3577:        roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
                   3578:        free(value);
1.364     schwarze 3579:        return ROFF_IGN;
                   3580: }
                   3581:
                   3582: /*
                   3583:  * The .break request only makes sense inside conditionals,
                   3584:  * and that case is already handled in roff_cond_sub().
                   3585:  */
                   3586: static int
                   3587: roff_break(ROFF_ARGS)
                   3588: {
                   3589:        mandoc_msg(MANDOCERR_BLK_NOTOPEN, ln, pos, "break");
1.296     schwarze 3590:        return ROFF_IGN;
1.92      schwarze 3591: }
                   3592:
1.340     schwarze 3593: static int
1.174     kristaps 3594: roff_cc(ROFF_ARGS)
                   3595: {
                   3596:        const char      *p;
                   3597:
1.238     schwarze 3598:        p = buf->buf + pos;
1.174     kristaps 3599:
1.238     schwarze 3600:        if (*p == '\0' || (r->control = *p++) == '.')
1.303     schwarze 3601:                r->control = '\0';
1.174     kristaps 3602:
1.238     schwarze 3603:        if (*p != '\0')
1.350     schwarze 3604:                mandoc_msg(MANDOCERR_ARG_EXCESS,
1.260     schwarze 3605:                    ln, p - buf->buf, "cc ... %s", p);
1.174     kristaps 3606:
1.341     schwarze 3607:        return ROFF_IGN;
                   3608: }
                   3609:
                   3610: static int
                   3611: roff_char(ROFF_ARGS)
                   3612: {
                   3613:        const char      *p, *kp, *vp;
                   3614:        size_t           ksz, vsz;
                   3615:        int              font;
                   3616:
                   3617:        /* Parse the character to be replaced. */
                   3618:
                   3619:        kp = buf->buf + pos;
                   3620:        p = kp + 1;
                   3621:        if (*kp == '\0' || (*kp == '\\' &&
                   3622:             mandoc_escape(&p, NULL, NULL) != ESCAPE_SPECIAL) ||
                   3623:            (*p != ' ' && *p != '\0')) {
1.350     schwarze 3624:                mandoc_msg(MANDOCERR_CHAR_ARG, ln, pos, "char %s", kp);
1.341     schwarze 3625:                return ROFF_IGN;
                   3626:        }
                   3627:        ksz = p - kp;
                   3628:        while (*p == ' ')
                   3629:                p++;
                   3630:
                   3631:        /*
                   3632:         * If the replacement string contains a font escape sequence,
                   3633:         * we have to restore the font at the end.
                   3634:         */
                   3635:
                   3636:        vp = p;
                   3637:        vsz = strlen(p);
                   3638:        font = 0;
                   3639:        while (*p != '\0') {
                   3640:                if (*p++ != '\\')
                   3641:                        continue;
                   3642:                switch (mandoc_escape(&p, NULL, NULL)) {
                   3643:                case ESCAPE_FONT:
                   3644:                case ESCAPE_FONTROMAN:
                   3645:                case ESCAPE_FONTITALIC:
                   3646:                case ESCAPE_FONTBOLD:
                   3647:                case ESCAPE_FONTBI:
1.342     schwarze 3648:                case ESCAPE_FONTCW:
1.341     schwarze 3649:                case ESCAPE_FONTPREV:
                   3650:                        font++;
                   3651:                        break;
                   3652:                default:
                   3653:                        break;
                   3654:                }
                   3655:        }
                   3656:        if (font > 1)
1.350     schwarze 3657:                mandoc_msg(MANDOCERR_CHAR_FONT,
                   3658:                    ln, (int)(vp - buf->buf), "%s", vp);
1.341     schwarze 3659:
                   3660:        /*
                   3661:         * Approximate the effect of .char using the .tr tables.
                   3662:         * XXX In groff, .char and .tr interact differently.
                   3663:         */
                   3664:
                   3665:        if (ksz == 1) {
                   3666:                if (r->xtab == NULL)
                   3667:                        r->xtab = mandoc_calloc(128, sizeof(*r->xtab));
                   3668:                assert((unsigned int)*kp < 128);
                   3669:                free(r->xtab[(int)*kp].p);
                   3670:                r->xtab[(int)*kp].sz = mandoc_asprintf(&r->xtab[(int)*kp].p,
                   3671:                    "%s%s", vp, font ? "\fP" : "");
                   3672:        } else {
                   3673:                roff_setstrn(&r->xmbtab, kp, ksz, vp, vsz, 0);
                   3674:                if (font)
                   3675:                        roff_setstrn(&r->xmbtab, kp, ksz, "\\fP", 3, 1);
                   3676:        }
1.277     schwarze 3677:        return ROFF_IGN;
1.174     kristaps 3678: }
                   3679:
1.340     schwarze 3680: static int
1.303     schwarze 3681: roff_ec(ROFF_ARGS)
                   3682: {
                   3683:        const char      *p;
                   3684:
                   3685:        p = buf->buf + pos;
                   3686:        if (*p == '\0')
                   3687:                r->escape = '\\';
                   3688:        else {
                   3689:                r->escape = *p;
                   3690:                if (*++p != '\0')
1.350     schwarze 3691:                        mandoc_msg(MANDOCERR_ARG_EXCESS, ln,
                   3692:                            (int)(p - buf->buf), "ec ... %s", p);
1.303     schwarze 3693:        }
                   3694:        return ROFF_IGN;
                   3695: }
                   3696:
1.340     schwarze 3697: static int
1.303     schwarze 3698: roff_eo(ROFF_ARGS)
                   3699: {
                   3700:        r->escape = '\0';
                   3701:        if (buf->buf[pos] != '\0')
1.350     schwarze 3702:                mandoc_msg(MANDOCERR_ARG_SKIP,
1.303     schwarze 3703:                    ln, pos, "eo %s", buf->buf + pos);
                   3704:        return ROFF_IGN;
1.330     schwarze 3705: }
                   3706:
1.340     schwarze 3707: static int
1.330     schwarze 3708: roff_nop(ROFF_ARGS)
                   3709: {
                   3710:        while (buf->buf[pos] == ' ')
                   3711:                pos++;
                   3712:        *offs = pos;
                   3713:        return ROFF_RERUN;
1.303     schwarze 3714: }
                   3715:
1.340     schwarze 3716: static int
1.164     kristaps 3717: roff_tr(ROFF_ARGS)
                   3718: {
                   3719:        const char      *p, *first, *second;
                   3720:        size_t           fsz, ssz;
                   3721:        enum mandoc_esc  esc;
                   3722:
1.238     schwarze 3723:        p = buf->buf + pos;
1.164     kristaps 3724:
1.238     schwarze 3725:        if (*p == '\0') {
1.350     schwarze 3726:                mandoc_msg(MANDOCERR_REQ_EMPTY, ln, ppos, "tr");
1.277     schwarze 3727:                return ROFF_IGN;
1.164     kristaps 3728:        }
                   3729:
1.238     schwarze 3730:        while (*p != '\0') {
1.164     kristaps 3731:                fsz = ssz = 1;
                   3732:
                   3733:                first = p++;
1.238     schwarze 3734:                if (*first == '\\') {
1.164     kristaps 3735:                        esc = mandoc_escape(&p, NULL, NULL);
1.238     schwarze 3736:                        if (esc == ESCAPE_ERROR) {
1.350     schwarze 3737:                                mandoc_msg(MANDOCERR_ESC_BAD, ln,
                   3738:                                    (int)(p - buf->buf), "%s", first);
1.277     schwarze 3739:                                return ROFF_IGN;
1.164     kristaps 3740:                        }
                   3741:                        fsz = (size_t)(p - first);
                   3742:                }
                   3743:
                   3744:                second = p++;
1.238     schwarze 3745:                if (*second == '\\') {
1.164     kristaps 3746:                        esc = mandoc_escape(&p, NULL, NULL);
1.238     schwarze 3747:                        if (esc == ESCAPE_ERROR) {
1.350     schwarze 3748:                                mandoc_msg(MANDOCERR_ESC_BAD, ln,
                   3749:                                    (int)(p - buf->buf), "%s", second);
1.277     schwarze 3750:                                return ROFF_IGN;
1.164     kristaps 3751:                        }
                   3752:                        ssz = (size_t)(p - second);
1.238     schwarze 3753:                } else if (*second == '\0') {
1.350     schwarze 3754:                        mandoc_msg(MANDOCERR_TR_ODD, ln,
                   3755:                            (int)(first - buf->buf), "tr %s", first);
1.164     kristaps 3756:                        second = " ";
1.165     kristaps 3757:                        p--;
1.164     kristaps 3758:                }
                   3759:
1.167     kristaps 3760:                if (fsz > 1) {
1.207     schwarze 3761:                        roff_setstrn(&r->xmbtab, first, fsz,
                   3762:                            second, ssz, 0);
1.167     kristaps 3763:                        continue;
                   3764:                }
                   3765:
1.238     schwarze 3766:                if (r->xtab == NULL)
1.207     schwarze 3767:                        r->xtab = mandoc_calloc(128,
                   3768:                            sizeof(struct roffstr));
1.167     kristaps 3769:
                   3770:                free(r->xtab[(int)*first].p);
                   3771:                r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
                   3772:                r->xtab[(int)*first].sz = ssz;
1.164     kristaps 3773:        }
                   3774:
1.277     schwarze 3775:        return ROFF_IGN;
1.164     kristaps 3776: }
                   3777:
1.339     schwarze 3778: /*
                   3779:  * Implementation of the .return request.
                   3780:  * There is no need to call roff_userret() from here.
                   3781:  * The read module will call that after rewinding the reader stack
                   3782:  * to the place from where the current macro was called.
                   3783:  */
1.340     schwarze 3784: static int
1.339     schwarze 3785: roff_return(ROFF_ARGS)
                   3786: {
                   3787:        if (r->mstackpos >= 0)
1.340     schwarze 3788:                return ROFF_IGN | ROFF_USERRET;
1.339     schwarze 3789:
1.350     schwarze 3790:        mandoc_msg(MANDOCERR_REQ_NOMAC, ln, ppos, "return");
1.339     schwarze 3791:        return ROFF_IGN;
                   3792: }
                   3793:
1.340     schwarze 3794: static int
1.306     schwarze 3795: roff_rn(ROFF_ARGS)
                   3796: {
                   3797:        const char      *value;
                   3798:        char            *oldn, *newn, *end;
                   3799:        size_t           oldsz, newsz;
1.315     schwarze 3800:        int              deftype;
1.306     schwarze 3801:
                   3802:        oldn = newn = buf->buf + pos;
                   3803:        if (*oldn == '\0')
                   3804:                return ROFF_IGN;
                   3805:
                   3806:        oldsz = roff_getname(r, &newn, ln, pos);
1.363     schwarze 3807:        if (oldn[oldsz] == '\\' || oldn[oldsz] == '\t' || *newn == '\0')
1.306     schwarze 3808:                return ROFF_IGN;
                   3809:
                   3810:        end = newn;
                   3811:        newsz = roff_getname(r, &end, ln, newn - buf->buf);
                   3812:        if (newsz == 0)
                   3813:                return ROFF_IGN;
                   3814:
1.315     schwarze 3815:        deftype = ROFFDEF_ANY;
                   3816:        value = roff_getstrn(r, oldn, oldsz, &deftype);
                   3817:        switch (deftype) {
                   3818:        case ROFFDEF_USER:
1.306     schwarze 3819:                roff_setstrn(&r->strtab, newn, newsz, value, strlen(value), 0);
                   3820:                roff_setstrn(&r->strtab, oldn, oldsz, NULL, 0, 0);
                   3821:                roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
1.315     schwarze 3822:                break;
                   3823:        case ROFFDEF_PRE:
                   3824:                roff_setstrn(&r->strtab, newn, newsz, value, strlen(value), 0);
                   3825:                roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
                   3826:                break;
                   3827:        case ROFFDEF_REN:
1.306     schwarze 3828:                roff_setstrn(&r->rentab, newn, newsz, value, strlen(value), 0);
                   3829:                roff_setstrn(&r->rentab, oldn, oldsz, NULL, 0, 0);
1.315     schwarze 3830:                roff_setstrn(&r->strtab, newn, newsz, NULL, 0, 0);
                   3831:                break;
                   3832:        case ROFFDEF_STD:
1.306     schwarze 3833:                roff_setstrn(&r->rentab, newn, newsz, oldn, oldsz, 0);
1.315     schwarze 3834:                roff_setstrn(&r->strtab, newn, newsz, NULL, 0, 0);
                   3835:                break;
                   3836:        default:
                   3837:                roff_setstrn(&r->strtab, newn, newsz, NULL, 0, 0);
                   3838:                roff_setstrn(&r->rentab, newn, newsz, NULL, 0, 0);
                   3839:                break;
                   3840:        }
1.306     schwarze 3841:        return ROFF_IGN;
                   3842: }
                   3843:
1.340     schwarze 3844: static int
1.339     schwarze 3845: roff_shift(ROFF_ARGS)
                   3846: {
                   3847:        struct mctx     *ctx;
                   3848:        int              levels, i;
                   3849:
                   3850:        levels = 1;
                   3851:        if (buf->buf[pos] != '\0' &&
                   3852:            roff_evalnum(r, ln, buf->buf, &pos, &levels, 0) == 0) {
1.350     schwarze 3853:                mandoc_msg(MANDOCERR_CE_NONUM,
1.339     schwarze 3854:                    ln, pos, "shift %s", buf->buf + pos);
                   3855:                levels = 1;
                   3856:        }
                   3857:        if (r->mstackpos < 0) {
1.350     schwarze 3858:                mandoc_msg(MANDOCERR_REQ_NOMAC, ln, ppos, "shift");
1.339     schwarze 3859:                return ROFF_IGN;
                   3860:        }
                   3861:        ctx = r->mstack + r->mstackpos;
                   3862:        if (levels > ctx->argc) {
1.350     schwarze 3863:                mandoc_msg(MANDOCERR_SHIFT,
1.339     schwarze 3864:                    ln, pos, "%d, but max is %d", levels, ctx->argc);
                   3865:                levels = ctx->argc;
                   3866:        }
                   3867:        if (levels == 0)
                   3868:                return ROFF_IGN;
                   3869:        for (i = 0; i < levels; i++)
                   3870:                free(ctx->argv[i]);
                   3871:        ctx->argc -= levels;
                   3872:        for (i = 0; i < ctx->argc; i++)
                   3873:                ctx->argv[i] = ctx->argv[i + levels];
                   3874:        return ROFF_IGN;
                   3875: }
                   3876:
1.340     schwarze 3877: static int
1.105     kristaps 3878: roff_so(ROFF_ARGS)
                   3879: {
1.249     schwarze 3880:        char *name, *cp;
1.105     kristaps 3881:
1.238     schwarze 3882:        name = buf->buf + pos;
1.350     schwarze 3883:        mandoc_msg(MANDOCERR_SO, ln, ppos, "so %s", name);
1.105     kristaps 3884:
                   3885:        /*
                   3886:         * Handle `so'.  Be EXTREMELY careful, as we shouldn't be
                   3887:         * opening anything that's not in our cwd or anything beneath
                   3888:         * it.  Thus, explicitly disallow traversing up the file-system
                   3889:         * or using absolute paths.
                   3890:         */
                   3891:
1.238     schwarze 3892:        if (*name == '/' || strstr(name, "../") || strstr(name, "/..")) {
1.350     schwarze 3893:                mandoc_msg(MANDOCERR_SO_PATH, ln, ppos, ".so %s", name);
1.249     schwarze 3894:                buf->sz = mandoc_asprintf(&cp,
                   3895:                    ".sp\nSee the file %s.\n.sp", name) + 1;
                   3896:                free(buf->buf);
                   3897:                buf->buf = cp;
                   3898:                *offs = 0;
1.277     schwarze 3899:                return ROFF_REPARSE;
1.105     kristaps 3900:        }
                   3901:
                   3902:        *offs = pos;
1.277     schwarze 3903:        return ROFF_SO;
1.105     kristaps 3904: }
1.92      schwarze 3905:
1.266     schwarze 3906: /* --- user defined strings and macros ------------------------------------ */
                   3907:
1.340     schwarze 3908: static int
1.106     kristaps 3909: roff_userdef(ROFF_ARGS)
1.99      kristaps 3910: {
1.339     schwarze 3911:        struct mctx      *ctx;
                   3912:        char             *arg, *ap, *dst, *src;
                   3913:        size_t            sz;
                   3914:
1.365     schwarze 3915:        /* If the macro is empty, ignore it altogether. */
                   3916:
                   3917:        if (*r->current_string == '\0')
                   3918:                return ROFF_IGN;
                   3919:
1.339     schwarze 3920:        /* Initialize a new macro stack context. */
                   3921:
                   3922:        if (++r->mstackpos == r->mstacksz) {
                   3923:                r->mstack = mandoc_recallocarray(r->mstack,
                   3924:                    r->mstacksz, r->mstacksz + 8, sizeof(*r->mstack));
                   3925:                r->mstacksz += 8;
                   3926:        }
                   3927:        ctx = r->mstack + r->mstackpos;
                   3928:        ctx->argsz = 0;
                   3929:        ctx->argc = 0;
                   3930:        ctx->argv = NULL;
                   3931:
                   3932:        /*
                   3933:         * Collect pointers to macro argument strings,
                   3934:         * NUL-terminating them and escaping quotes.
                   3935:         */
                   3936:
                   3937:        src = buf->buf + pos;
                   3938:        while (*src != '\0') {
                   3939:                if (ctx->argc == ctx->argsz) {
                   3940:                        ctx->argsz += 8;
                   3941:                        ctx->argv = mandoc_reallocarray(ctx->argv,
                   3942:                            ctx->argsz, sizeof(*ctx->argv));
                   3943:                }
1.355     schwarze 3944:                arg = roff_getarg(r, &src, ln, &pos);
1.339     schwarze 3945:                sz = 1;  /* For the terminating NUL. */
                   3946:                for (ap = arg; *ap != '\0'; ap++)
                   3947:                        sz += *ap == '"' ? 4 : 1;
                   3948:                ctx->argv[ctx->argc++] = dst = mandoc_malloc(sz);
                   3949:                for (ap = arg; *ap != '\0'; ap++) {
                   3950:                        if (*ap == '"') {
                   3951:                                memcpy(dst, "\\(dq", 4);
                   3952:                                dst += 4;
                   3953:                        } else
                   3954:                                *dst++ = *ap;
1.106     kristaps 3955:                }
1.339     schwarze 3956:                *dst = '\0';
1.355     schwarze 3957:                free(arg);
1.337     schwarze 3958:        }
                   3959:
1.339     schwarze 3960:        /* Replace the macro invocation by the macro definition. */
1.263     schwarze 3961:
1.238     schwarze 3962:        free(buf->buf);
1.339     schwarze 3963:        buf->buf = mandoc_strdup(r->current_string);
                   3964:        buf->sz = strlen(buf->buf) + 1;
1.248     schwarze 3965:        *offs = 0;
1.106     kristaps 3966:
1.365     schwarze 3967:        return buf->buf[buf->sz - 2] == '\n' ?
1.340     schwarze 3968:            ROFF_REPARSE | ROFF_USERCALL : ROFF_IGN | ROFF_APPEND;
1.99      kristaps 3969: }
1.121     schwarze 3970:
1.306     schwarze 3971: /*
                   3972:  * Calling a high-level macro that was renamed with .rn.
                   3973:  * r->current_string has already been set up by roff_parse().
                   3974:  */
1.340     schwarze 3975: static int
1.306     schwarze 3976: roff_renamed(ROFF_ARGS)
                   3977: {
                   3978:        char    *nbuf;
                   3979:
1.315     schwarze 3980:        buf->sz = mandoc_asprintf(&nbuf, ".%s%s%s", r->current_string,
                   3981:            buf->buf[pos] == '\0' ? "" : " ", buf->buf + pos) + 1;
1.306     schwarze 3982:        free(buf->buf);
                   3983:        buf->buf = nbuf;
1.329     schwarze 3984:        *offs = 0;
1.306     schwarze 3985:        return ROFF_CONT;
                   3986: }
                   3987:
1.362     schwarze 3988: /*
                   3989:  * Measure the length in bytes of the roff identifier at *cpp
                   3990:  * and advance the pointer to the next word.
                   3991:  */
1.212     schwarze 3992: static size_t
1.121     schwarze 3993: roff_getname(struct roff *r, char **cpp, int ln, int pos)
                   3994: {
                   3995:        char     *name, *cp;
1.212     schwarze 3996:        size_t    namesz;
1.121     schwarze 3997:
                   3998:        name = *cpp;
1.362     schwarze 3999:        if (*name == '\0')
1.277     schwarze 4000:                return 0;
1.121     schwarze 4001:
1.362     schwarze 4002:        /* Advance cp to the byte after the end of the name. */
                   4003:
1.212     schwarze 4004:        for (cp = name; 1; cp++) {
1.362     schwarze 4005:                namesz = cp - name;
1.363     schwarze 4006:                if (*cp == '\0')
1.212     schwarze 4007:                        break;
1.363     schwarze 4008:                if (*cp == ' ' || *cp == '\t') {
                   4009:                        cp++;
                   4010:                        break;
                   4011:                }
1.362     schwarze 4012:                if (*cp != '\\')
1.121     schwarze 4013:                        continue;
1.362     schwarze 4014:                if (cp[1] == '{' || cp[1] == '}')
1.215     schwarze 4015:                        break;
1.362     schwarze 4016:                if (*++cp == '\\')
1.121     schwarze 4017:                        continue;
1.350     schwarze 4018:                mandoc_msg(MANDOCERR_NAMESC, ln, pos,
1.224     schwarze 4019:                    "%.*s", (int)(cp - name + 1), name);
1.212     schwarze 4020:                mandoc_escape((const char **)&cp, NULL, NULL);
                   4021:                break;
1.121     schwarze 4022:        }
                   4023:
                   4024:        /* Read past spaces. */
1.362     schwarze 4025:
                   4026:        while (*cp == ' ')
1.121     schwarze 4027:                cp++;
                   4028:
                   4029:        *cpp = cp;
1.277     schwarze 4030:        return namesz;
1.121     schwarze 4031: }
                   4032:
1.106     kristaps 4033: /*
                   4034:  * Store *string into the user-defined string called *name.
                   4035:  * To clear an existing entry, call with (*r, *name, NULL, 0).
1.193     schwarze 4036:  * append == 0: replace mode
                   4037:  * append == 1: single-line append mode
                   4038:  * append == 2: multiline append mode, append '\n' after each call
1.106     kristaps 4039:  */
1.94      kristaps 4040: static void
1.106     kristaps 4041: roff_setstr(struct roff *r, const char *name, const char *string,
1.193     schwarze 4042:        int append)
1.92      schwarze 4043: {
1.315     schwarze 4044:        size_t   namesz;
1.164     kristaps 4045:
1.315     schwarze 4046:        namesz = strlen(name);
                   4047:        roff_setstrn(&r->strtab, name, namesz, string,
1.207     schwarze 4048:            string ? strlen(string) : 0, append);
1.315     schwarze 4049:        roff_setstrn(&r->rentab, name, namesz, NULL, 0, 0);
1.164     kristaps 4050: }
                   4051:
                   4052: static void
1.166     kristaps 4053: roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
1.193     schwarze 4054:                const char *string, size_t stringsz, int append)
1.164     kristaps 4055: {
1.166     kristaps 4056:        struct roffkv   *n;
1.164     kristaps 4057:        char            *c;
                   4058:        int              i;
                   4059:        size_t           oldch, newch;
1.92      schwarze 4060:
1.106     kristaps 4061:        /* Search for an existing string with the same name. */
1.164     kristaps 4062:        n = *r;
                   4063:
1.211     schwarze 4064:        while (n && (namesz != n->key.sz ||
                   4065:                        strncmp(n->key.p, name, namesz)))
1.92      schwarze 4066:                n = n->next;
1.94      kristaps 4067:
                   4068:        if (NULL == n) {
1.106     kristaps 4069:                /* Create a new string table entry. */
1.166     kristaps 4070:                n = mandoc_malloc(sizeof(struct roffkv));
                   4071:                n->key.p = mandoc_strndup(name, namesz);
                   4072:                n->key.sz = namesz;
                   4073:                n->val.p = NULL;
                   4074:                n->val.sz = 0;
1.164     kristaps 4075:                n->next = *r;
                   4076:                *r = n;
1.193     schwarze 4077:        } else if (0 == append) {
1.166     kristaps 4078:                free(n->val.p);
                   4079:                n->val.p = NULL;
                   4080:                n->val.sz = 0;
1.106     kristaps 4081:        }
                   4082:
                   4083:        if (NULL == string)
                   4084:                return;
                   4085:
                   4086:        /*
                   4087:         * One additional byte for the '\n' in multiline mode,
                   4088:         * and one for the terminating '\0'.
                   4089:         */
1.193     schwarze 4090:        newch = stringsz + (1 < append ? 2u : 1u);
1.164     kristaps 4091:
1.166     kristaps 4092:        if (NULL == n->val.p) {
                   4093:                n->val.p = mandoc_malloc(newch);
                   4094:                *n->val.p = '\0';
1.106     kristaps 4095:                oldch = 0;
                   4096:        } else {
1.166     kristaps 4097:                oldch = n->val.sz;
                   4098:                n->val.p = mandoc_realloc(n->val.p, oldch + newch);
1.106     kristaps 4099:        }
                   4100:
                   4101:        /* Skip existing content in the destination buffer. */
1.166     kristaps 4102:        c = n->val.p + (int)oldch;
1.106     kristaps 4103:
                   4104:        /* Append new content to the destination buffer. */
1.164     kristaps 4105:        i = 0;
                   4106:        while (i < (int)stringsz) {
1.106     kristaps 4107:                /*
                   4108:                 * Rudimentary roff copy mode:
                   4109:                 * Handle escaped backslashes.
                   4110:                 */
1.164     kristaps 4111:                if ('\\' == string[i] && '\\' == string[i + 1])
                   4112:                        i++;
                   4113:                *c++ = string[i++];
1.106     kristaps 4114:        }
1.94      kristaps 4115:
1.106     kristaps 4116:        /* Append terminating bytes. */
1.193     schwarze 4117:        if (1 < append)
1.106     kristaps 4118:                *c++ = '\n';
1.163     kristaps 4119:
1.106     kristaps 4120:        *c = '\0';
1.166     kristaps 4121:        n->val.sz = (int)(c - n->val.p);
1.92      schwarze 4122: }
                   4123:
1.94      kristaps 4124: static const char *
1.325     schwarze 4125: roff_getstrn(struct roff *r, const char *name, size_t len,
1.315     schwarze 4126:     int *deftype)
1.92      schwarze 4127: {
1.315     schwarze 4128:        const struct roffkv     *n;
1.325     schwarze 4129:        int                      found, i;
1.315     schwarze 4130:        enum roff_tok            tok;
                   4131:
1.325     schwarze 4132:        found = 0;
                   4133:        for (n = r->strtab; n != NULL; n = n->next) {
                   4134:                if (strncmp(name, n->key.p, len) != 0 ||
                   4135:                    n->key.p[len] != '\0' || n->val.p == NULL)
                   4136:                        continue;
                   4137:                if (*deftype & ROFFDEF_USER) {
                   4138:                        *deftype = ROFFDEF_USER;
                   4139:                        return n->val.p;
                   4140:                } else {
                   4141:                        found = 1;
                   4142:                        break;
                   4143:                }
                   4144:        }
                   4145:        for (n = r->rentab; n != NULL; n = n->next) {
                   4146:                if (strncmp(name, n->key.p, len) != 0 ||
                   4147:                    n->key.p[len] != '\0' || n->val.p == NULL)
                   4148:                        continue;
                   4149:                if (*deftype & ROFFDEF_REN) {
                   4150:                        *deftype = ROFFDEF_REN;
                   4151:                        return n->val.p;
                   4152:                } else {
                   4153:                        found = 1;
                   4154:                        break;
1.315     schwarze 4155:                }
                   4156:        }
1.325     schwarze 4157:        for (i = 0; i < PREDEFS_MAX; i++) {
                   4158:                if (strncmp(name, predefs[i].name, len) != 0 ||
                   4159:                    predefs[i].name[len] != '\0')
                   4160:                        continue;
                   4161:                if (*deftype & ROFFDEF_PRE) {
                   4162:                        *deftype = ROFFDEF_PRE;
                   4163:                        return predefs[i].str;
                   4164:                } else {
                   4165:                        found = 1;
                   4166:                        break;
1.315     schwarze 4167:                }
                   4168:        }
1.356     schwarze 4169:        if (r->man->meta.macroset != MACROSET_MAN) {
1.325     schwarze 4170:                for (tok = MDOC_Dd; tok < MDOC_MAX; tok++) {
                   4171:                        if (strncmp(name, roff_name[tok], len) != 0 ||
                   4172:                            roff_name[tok][len] != '\0')
                   4173:                                continue;
                   4174:                        if (*deftype & ROFFDEF_STD) {
                   4175:                                *deftype = ROFFDEF_STD;
                   4176:                                return NULL;
                   4177:                        } else {
                   4178:                                found = 1;
                   4179:                                break;
1.315     schwarze 4180:                        }
                   4181:                }
                   4182:        }
1.356     schwarze 4183:        if (r->man->meta.macroset != MACROSET_MDOC) {
1.325     schwarze 4184:                for (tok = MAN_TH; tok < MAN_MAX; tok++) {
                   4185:                        if (strncmp(name, roff_name[tok], len) != 0 ||
                   4186:                            roff_name[tok][len] != '\0')
                   4187:                                continue;
                   4188:                        if (*deftype & ROFFDEF_STD) {
                   4189:                                *deftype = ROFFDEF_STD;
                   4190:                                return NULL;
                   4191:                        } else {
                   4192:                                found = 1;
                   4193:                                break;
1.315     schwarze 4194:                        }
                   4195:                }
1.325     schwarze 4196:        }
                   4197:
                   4198:        if (found == 0 && *deftype != ROFFDEF_ANY) {
                   4199:                if (*deftype & ROFFDEF_REN) {
                   4200:                        /*
                   4201:                         * This might still be a request,
                   4202:                         * so do not treat it as undefined yet.
                   4203:                         */
                   4204:                        *deftype = ROFFDEF_UNDEF;
                   4205:                        return NULL;
1.315     schwarze 4206:                }
1.325     schwarze 4207:
                   4208:                /* Using an undefined string defines it to be empty. */
                   4209:
                   4210:                roff_setstrn(&r->strtab, name, len, "", 0, 0);
                   4211:                roff_setstrn(&r->rentab, name, len, NULL, 0, 0);
1.315     schwarze 4212:        }
1.325     schwarze 4213:
1.315     schwarze 4214:        *deftype = 0;
1.277     schwarze 4215:        return NULL;
1.92      schwarze 4216: }
                   4217:
1.94      kristaps 4218: static void
1.167     kristaps 4219: roff_freestr(struct roffkv *r)
1.92      schwarze 4220: {
1.166     kristaps 4221:        struct roffkv    *n, *nn;
1.92      schwarze 4222:
1.167     kristaps 4223:        for (n = r; n; n = nn) {
1.166     kristaps 4224:                free(n->key.p);
                   4225:                free(n->val.p);
1.92      schwarze 4226:                nn = n->next;
                   4227:                free(n);
                   4228:        }
1.114     kristaps 4229: }
1.266     schwarze 4230:
                   4231: /* --- accessors and utility functions ------------------------------------ */
1.164     kristaps 4232:
                   4233: /*
                   4234:  * Duplicate an input string, making the appropriate character
                   4235:  * conversations (as stipulated by `tr') along the way.
                   4236:  * Returns a heap-allocated string with all the replacements made.
                   4237:  */
                   4238: char *
                   4239: roff_strdup(const struct roff *r, const char *p)
                   4240: {
1.166     kristaps 4241:        const struct roffkv *cp;
1.164     kristaps 4242:        char            *res;
                   4243:        const char      *pp;
                   4244:        size_t           ssz, sz;
                   4245:        enum mandoc_esc  esc;
                   4246:
1.167     kristaps 4247:        if (NULL == r->xmbtab && NULL == r->xtab)
1.277     schwarze 4248:                return mandoc_strdup(p);
1.164     kristaps 4249:        else if ('\0' == *p)
1.277     schwarze 4250:                return mandoc_strdup("");
1.164     kristaps 4251:
                   4252:        /*
                   4253:         * Step through each character looking for term matches
                   4254:         * (remember that a `tr' can be invoked with an escape, which is
                   4255:         * a glyph but the escape is multi-character).
                   4256:         * We only do this if the character hash has been initialised
                   4257:         * and the string is >0 length.
                   4258:         */
                   4259:
                   4260:        res = NULL;
                   4261:        ssz = 0;
                   4262:
                   4263:        while ('\0' != *p) {
1.289     schwarze 4264:                assert((unsigned int)*p < 128);
                   4265:                if ('\\' != *p && r->xtab && r->xtab[(unsigned int)*p].p) {
1.167     kristaps 4266:                        sz = r->xtab[(int)*p].sz;
                   4267:                        res = mandoc_realloc(res, ssz + sz + 1);
                   4268:                        memcpy(res + ssz, r->xtab[(int)*p].p, sz);
                   4269:                        ssz += sz;
                   4270:                        p++;
                   4271:                        continue;
                   4272:                } else if ('\\' != *p) {
                   4273:                        res = mandoc_realloc(res, ssz + 2);
                   4274:                        res[ssz++] = *p++;
                   4275:                        continue;
                   4276:                }
                   4277:
1.164     kristaps 4278:                /* Search for term matches. */
1.167     kristaps 4279:                for (cp = r->xmbtab; cp; cp = cp->next)
1.166     kristaps 4280:                        if (0 == strncmp(p, cp->key.p, cp->key.sz))
1.164     kristaps 4281:                                break;
                   4282:
                   4283:                if (NULL != cp) {
                   4284:                        /*
                   4285:                         * A match has been found.
                   4286:                         * Append the match to the array and move
                   4287:                         * forward by its keysize.
                   4288:                         */
1.207     schwarze 4289:                        res = mandoc_realloc(res,
                   4290:                            ssz + cp->val.sz + 1);
1.166     kristaps 4291:                        memcpy(res + ssz, cp->val.p, cp->val.sz);
                   4292:                        ssz += cp->val.sz;
                   4293:                        p += (int)cp->key.sz;
1.164     kristaps 4294:                        continue;
                   4295:                }
                   4296:
1.167     kristaps 4297:                /*
                   4298:                 * Handle escapes carefully: we need to copy
                   4299:                 * over just the escape itself, or else we might
                   4300:                 * do replacements within the escape itself.
                   4301:                 * Make sure to pass along the bogus string.
                   4302:                 */
                   4303:                pp = p++;
                   4304:                esc = mandoc_escape(&p, NULL, NULL);
                   4305:                if (ESCAPE_ERROR == esc) {
                   4306:                        sz = strlen(pp);
1.164     kristaps 4307:                        res = mandoc_realloc(res, ssz + sz + 1);
                   4308:                        memcpy(res + ssz, pp, sz);
1.167     kristaps 4309:                        break;
1.164     kristaps 4310:                }
1.207     schwarze 4311:                /*
                   4312:                 * We bail out on bad escapes.
1.167     kristaps 4313:                 * No need to warn: we already did so when
1.355     schwarze 4314:                 * roff_expand() was called.
1.167     kristaps 4315:                 */
                   4316:                sz = (int)(p - pp);
                   4317:                res = mandoc_realloc(res, ssz + sz + 1);
                   4318:                memcpy(res + ssz, pp, sz);
                   4319:                ssz += sz;
1.164     kristaps 4320:        }
                   4321:
                   4322:        res[(int)ssz] = '\0';
1.277     schwarze 4323:        return res;
1.227     schwarze 4324: }
                   4325:
                   4326: int
                   4327: roff_getformat(const struct roff *r)
                   4328: {
                   4329:
1.277     schwarze 4330:        return r->format;
1.174     kristaps 4331: }
                   4332:
                   4333: /*
1.207     schwarze 4334:  * Find out whether a line is a macro line or not.
1.174     kristaps 4335:  * If it is, adjust the current position and return one; if it isn't,
                   4336:  * return zero and don't change the current position.
                   4337:  * If the control character has been set with `.cc', then let that grain
                   4338:  * precedence.
                   4339:  * This is slighly contrary to groff, where using the non-breaking
                   4340:  * control character when `cc' has been invoked will cause the
                   4341:  * non-breaking macro contents to be printed verbatim.
                   4342:  */
                   4343: int
                   4344: roff_getcontrol(const struct roff *r, const char *cp, int *ppos)
                   4345: {
                   4346:        int             pos;
                   4347:
                   4348:        pos = *ppos;
                   4349:
1.303     schwarze 4350:        if (r->control != '\0' && cp[pos] == r->control)
1.174     kristaps 4351:                pos++;
1.303     schwarze 4352:        else if (r->control != '\0')
1.277     schwarze 4353:                return 0;
1.174     kristaps 4354:        else if ('\\' == cp[pos] && '.' == cp[pos + 1])
                   4355:                pos += 2;
                   4356:        else if ('.' == cp[pos] || '\'' == cp[pos])
                   4357:                pos++;
                   4358:        else
1.277     schwarze 4359:                return 0;
1.174     kristaps 4360:
                   4361:        while (' ' == cp[pos] || '\t' == cp[pos])
                   4362:                pos++;
                   4363:
                   4364:        *ppos = pos;
1.277     schwarze 4365:        return 1;
1.74      kristaps 4366: }

CVSweb