[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.245

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