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

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

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