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

Annotation of mandoc/roff.c, Revision 1.387

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

CVSweb