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

Annotation of mandoc/roff.c, Revision 1.217

1.217   ! schwarze    1: /*     $Id: roff.c,v 1.216 2014/07/03 21:23:54 schwarze Exp $ */
1.1       kristaps    2: /*
1.175     schwarze    3:  * Copyright (c) 2010, 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.190     schwarze    4:  * Copyright (c) 2010-2014 Ingo Schwarze <schwarze@openbsd.org>
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.1       kristaps   17:  */
1.66      kristaps   18: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
                     20: #endif
1.30      kristaps   21:
1.67      kristaps   22: #include <assert.h>
1.85      kristaps   23: #include <ctype.h>
1.178     schwarze   24: #include <stdio.h>
1.1       kristaps   25: #include <stdlib.h>
1.67      kristaps   26: #include <string.h>
1.1       kristaps   27:
1.67      kristaps   28: #include "mandoc.h"
1.201     schwarze   29: #include "mandoc_aux.h"
1.109     kristaps   30: #include "libroff.h"
1.94      kristaps   31: #include "libmandoc.h"
1.33      kristaps   32:
1.141     kristaps   33: /* Maximum number of nested if-else conditionals. */
1.82      kristaps   34: #define        RSTACK_MAX      128
                     35:
1.170     schwarze   36: /* Maximum number of string expansions per line, to break infinite loops. */
                     37: #define        EXPAND_LIMIT    1000
                     38:
1.67      kristaps   39: enum   rofft {
1.103     kristaps   40:        ROFF_ad,
1.80      kristaps   41:        ROFF_am,
                     42:        ROFF_ami,
                     43:        ROFF_am1,
1.193     schwarze   44:        ROFF_as,
1.174     kristaps   45:        ROFF_cc,
1.194     schwarze   46:        ROFF_ce,
1.80      kristaps   47:        ROFF_de,
                     48:        ROFF_dei,
                     49:        ROFF_de1,
1.83      schwarze   50:        ROFF_ds,
1.82      kristaps   51:        ROFF_el,
1.185     schwarze   52:        ROFF_fam,
1.186     schwarze   53:        ROFF_hw,
1.103     kristaps   54:        ROFF_hy,
1.82      kristaps   55:        ROFF_ie,
1.75      kristaps   56:        ROFF_if,
1.76      kristaps   57:        ROFF_ig,
1.123     schwarze   58:        ROFF_it,
1.103     kristaps   59:        ROFF_ne,
                     60:        ROFF_nh,
1.104     kristaps   61:        ROFF_nr,
1.124     schwarze   62:        ROFF_ns,
                     63:        ROFF_ps,
1.83      schwarze   64:        ROFF_rm,
1.203     schwarze   65:        ROFF_rr,
1.105     kristaps   66:        ROFF_so,
1.124     schwarze   67:        ROFF_ta,
1.83      schwarze   68:        ROFF_tr,
1.175     schwarze   69:        ROFF_Dd,
                     70:        ROFF_TH,
1.109     kristaps   71:        ROFF_TS,
                     72:        ROFF_TE,
1.112     kristaps   73:        ROFF_T_,
1.125     kristaps   74:        ROFF_EQ,
                     75:        ROFF_EN,
1.76      kristaps   76:        ROFF_cblock,
1.106     kristaps   77:        ROFF_USERDEF,
1.67      kristaps   78:        ROFF_MAX
                     79: };
                     80:
1.147     kristaps   81: /*
1.167     kristaps   82:  * An incredibly-simple string buffer.
                     83:  */
1.94      kristaps   84: struct roffstr {
1.167     kristaps   85:        char            *p; /* nil-terminated buffer */
                     86:        size_t           sz; /* saved strlen(p) */
1.166     kristaps   87: };
                     88:
                     89: /*
1.167     kristaps   90:  * A key-value roffstr pair as part of a singly-linked list.
1.166     kristaps   91:  */
                     92: struct roffkv {
                     93:        struct roffstr   key;
                     94:        struct roffstr   val;
                     95:        struct roffkv   *next; /* next in list */
1.94      kristaps   96: };
                     97:
1.180     schwarze   98: /*
                     99:  * A single number register as part of a singly-linked list.
                    100:  */
                    101: struct roffreg {
                    102:        struct roffstr   key;
1.181     schwarze  103:        int              val;
1.180     schwarze  104:        struct roffreg  *next;
                    105: };
                    106:
1.67      kristaps  107: struct roff {
1.128     kristaps  108:        struct mparse   *parse; /* parse point */
1.199     schwarze  109:        int              options; /* parse options */
1.67      kristaps  110:        struct roffnode *last; /* leaf of stack */
1.198     schwarze  111:        int              rstack[RSTACK_MAX]; /* stack of !`ie' rules */
1.174     kristaps  112:        char             control; /* control character */
1.82      kristaps  113:        int              rstackpos; /* position in rstack */
1.180     schwarze  114:        struct roffreg  *regtab; /* number registers */
1.166     kristaps  115:        struct roffkv   *strtab; /* user-defined strings & macros */
1.167     kristaps  116:        struct roffkv   *xmbtab; /* multi-byte trans table (`tr') */
                    117:        struct roffstr  *xtab; /* single-byte trans table (`tr') */
1.106     kristaps  118:        const char      *current_string; /* value of last called user macro */
1.118     kristaps  119:        struct tbl_node *first_tbl; /* first table parsed */
                    120:        struct tbl_node *last_tbl; /* last table parsed */
                    121:        struct tbl_node *tbl; /* current table being parsed */
1.125     kristaps  122:        struct eqn_node *last_eqn; /* last equation parsed */
                    123:        struct eqn_node *first_eqn; /* first equation parsed */
                    124:        struct eqn_node *eqn; /* current equation being parsed */
1.79      kristaps  125: };
                    126:
1.67      kristaps  127: struct roffnode {
                    128:        enum rofft       tok; /* type of node */
                    129:        struct roffnode *parent; /* up one in stack */
                    130:        int              line; /* parse line */
                    131:        int              col; /* parse col */
1.106     kristaps  132:        char            *name; /* node name, e.g. macro name */
1.79      kristaps  133:        char            *end; /* end-rules: custom token */
                    134:        int              endspan; /* end-rules: next-line or infty */
1.198     schwarze  135:        int              rule; /* current evaluation rule */
1.67      kristaps  136: };
                    137:
                    138: #define        ROFF_ARGS        struct roff *r, /* parse ctx */ \
1.72      kristaps  139:                         enum rofft tok, /* tok of macro */ \
1.207     schwarze  140:                         char **bufp, /* input buffer */ \
1.67      kristaps  141:                         size_t *szp, /* size of input buffer */ \
                    142:                         int ln, /* parse line */ \
1.75      kristaps  143:                         int ppos, /* original pos in buffer */ \
                    144:                         int pos, /* current pos in buffer */ \
1.74      kristaps  145:                         int *offs /* reset offset of buffer data */
1.67      kristaps  146:
                    147: typedef        enum rofferr (*roffproc)(ROFF_ARGS);
                    148:
                    149: struct roffmac {
                    150:        const char      *name; /* macro name */
1.79      kristaps  151:        roffproc         proc; /* process new macro */
                    152:        roffproc         text; /* process as child text of macro */
                    153:        roffproc         sub; /* process as child of macro */
                    154:        int              flags;
                    155: #define        ROFFMAC_STRUCT  (1 << 0) /* always interpret */
1.85      kristaps  156:        struct roffmac  *next;
1.67      kristaps  157: };
                    158:
1.141     kristaps  159: struct predef {
                    160:        const char      *name; /* predefined input name */
                    161:        const char      *str; /* replacement symbol */
                    162: };
                    163:
                    164: #define        PREDEF(__name, __str) \
                    165:        { (__name), (__str) },
                    166:
1.155     kristaps  167: static enum rofft       roffhash_find(const char *, size_t);
                    168: static void             roffhash_init(void);
                    169: static void             roffnode_cleanscope(struct roff *);
                    170: static void             roffnode_pop(struct roff *);
                    171: static void             roffnode_push(struct roff *, enum rofft,
                    172:                                const char *, int, int);
1.80      kristaps  173: static enum rofferr     roff_block(ROFF_ARGS);
                    174: static enum rofferr     roff_block_text(ROFF_ARGS);
                    175: static enum rofferr     roff_block_sub(ROFF_ARGS);
                    176: static enum rofferr     roff_cblock(ROFF_ARGS);
1.174     kristaps  177: static enum rofferr     roff_cc(ROFF_ARGS);
1.195     schwarze  178: static void             roff_ccond(struct roff *, int, int);
1.82      kristaps  179: static enum rofferr     roff_cond(ROFF_ARGS);
                    180: static enum rofferr     roff_cond_text(ROFF_ARGS);
                    181: static enum rofferr     roff_cond_sub(ROFF_ARGS);
1.92      schwarze  182: static enum rofferr     roff_ds(ROFF_ARGS);
1.198     schwarze  183: static int              roff_evalcond(const char *, int *);
1.204     schwarze  184: static int              roff_evalnum(const char *, int *, int *, int);
                    185: static int              roff_evalpar(const char *, int *, int *);
1.198     schwarze  186: static int              roff_evalstrcond(const char *, int *);
1.155     kristaps  187: static void             roff_free1(struct roff *);
1.180     schwarze  188: static void             roff_freereg(struct roffreg *);
1.167     kristaps  189: static void             roff_freestr(struct roffkv *);
1.212     schwarze  190: static size_t           roff_getname(struct roff *, char **, int, int);
1.184     schwarze  191: static int              roff_getnum(const char *, int *, int *);
                    192: static int              roff_getop(const char *, int *, char *);
1.181     schwarze  193: static int              roff_getregn(const struct roff *,
                    194:                                const char *, size_t);
1.192     schwarze  195: static int              roff_getregro(const char *name);
1.207     schwarze  196: static const char      *roff_getstrn(const struct roff *,
1.94      kristaps  197:                                const char *, size_t);
1.178     schwarze  198: static enum rofferr     roff_it(ROFF_ARGS);
1.103     kristaps  199: static enum rofferr     roff_line_ignore(ROFF_ARGS);
1.89      kristaps  200: static enum rofferr     roff_nr(ROFF_ARGS);
1.169     schwarze  201: static void             roff_openeqn(struct roff *, const char *,
1.156     kristaps  202:                                int, int, const char *);
1.214     schwarze  203: static enum rofft       roff_parse(struct roff *, char *, int *,
                    204:                                int, int);
1.178     schwarze  205: static enum rofferr     roff_parsetext(char **, size_t *, int, int *);
1.207     schwarze  206: static enum rofferr     roff_res(struct roff *,
1.142     kristaps  207:                                char **, size_t *, int, int);
1.122     schwarze  208: static enum rofferr     roff_rm(ROFF_ARGS);
1.203     schwarze  209: static enum rofferr     roff_rr(ROFF_ARGS);
1.94      kristaps  210: static void             roff_setstr(struct roff *,
1.106     kristaps  211:                                const char *, const char *, int);
1.207     schwarze  212: static void             roff_setstrn(struct roffkv **, const char *,
1.164     kristaps  213:                                size_t, const char *, size_t, int);
1.105     kristaps  214: static enum rofferr     roff_so(ROFF_ARGS);
1.164     kristaps  215: static enum rofferr     roff_tr(ROFF_ARGS);
1.175     schwarze  216: static enum rofferr     roff_Dd(ROFF_ARGS);
                    217: static enum rofferr     roff_TH(ROFF_ARGS);
1.109     kristaps  218: static enum rofferr     roff_TE(ROFF_ARGS);
                    219: static enum rofferr     roff_TS(ROFF_ARGS);
1.125     kristaps  220: static enum rofferr     roff_EQ(ROFF_ARGS);
                    221: static enum rofferr     roff_EN(ROFF_ARGS);
1.112     kristaps  222: static enum rofferr     roff_T_(ROFF_ARGS);
1.106     kristaps  223: static enum rofferr     roff_userdef(ROFF_ARGS);
1.67      kristaps  224:
1.155     kristaps  225: /* See roffhash_find() */
1.85      kristaps  226:
                    227: #define        ASCII_HI         126
                    228: #define        ASCII_LO         33
                    229: #define        HASHWIDTH       (ASCII_HI - ASCII_LO + 1)
                    230:
                    231: static struct roffmac  *hash[HASHWIDTH];
                    232:
                    233: static struct roffmac   roffs[ROFF_MAX] = {
1.103     kristaps  234:        { "ad", roff_line_ignore, NULL, NULL, 0, NULL },
1.85      kristaps  235:        { "am", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    236:        { "ami", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    237:        { "am1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.193     schwarze  238:        { "as", roff_ds, NULL, NULL, 0, NULL },
1.174     kristaps  239:        { "cc", roff_cc, NULL, NULL, 0, NULL },
1.194     schwarze  240:        { "ce", roff_line_ignore, NULL, NULL, 0, NULL },
1.85      kristaps  241:        { "de", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    242:        { "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    243:        { "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.92      schwarze  244:        { "ds", roff_ds, NULL, NULL, 0, NULL },
1.85      kristaps  245:        { "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
1.185     schwarze  246:        { "fam", roff_line_ignore, NULL, NULL, 0, NULL },
1.186     schwarze  247:        { "hw", roff_line_ignore, NULL, NULL, 0, NULL },
1.103     kristaps  248:        { "hy", roff_line_ignore, NULL, NULL, 0, NULL },
1.85      kristaps  249:        { "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
                    250:        { "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
                    251:        { "ig", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.178     schwarze  252:        { "it", roff_it, NULL, NULL, 0, NULL },
1.103     kristaps  253:        { "ne", roff_line_ignore, NULL, NULL, 0, NULL },
                    254:        { "nh", roff_line_ignore, NULL, NULL, 0, NULL },
1.104     kristaps  255:        { "nr", roff_nr, NULL, NULL, 0, NULL },
1.124     schwarze  256:        { "ns", roff_line_ignore, NULL, NULL, 0, NULL },
                    257:        { "ps", roff_line_ignore, NULL, NULL, 0, NULL },
1.122     schwarze  258:        { "rm", roff_rm, NULL, NULL, 0, NULL },
1.203     schwarze  259:        { "rr", roff_rr, NULL, NULL, 0, NULL },
1.105     kristaps  260:        { "so", roff_so, NULL, NULL, 0, NULL },
1.124     schwarze  261:        { "ta", roff_line_ignore, NULL, NULL, 0, NULL },
1.164     kristaps  262:        { "tr", roff_tr, NULL, NULL, 0, NULL },
1.175     schwarze  263:        { "Dd", roff_Dd, NULL, NULL, 0, NULL },
                    264:        { "TH", roff_TH, NULL, NULL, 0, NULL },
1.109     kristaps  265:        { "TS", roff_TS, NULL, NULL, 0, NULL },
                    266:        { "TE", roff_TE, NULL, NULL, 0, NULL },
1.112     kristaps  267:        { "T&", roff_T_, NULL, NULL, 0, NULL },
1.125     kristaps  268:        { "EQ", roff_EQ, NULL, NULL, 0, NULL },
                    269:        { "EN", roff_EN, NULL, NULL, 0, NULL },
1.85      kristaps  270:        { ".", roff_cblock, NULL, NULL, 0, NULL },
1.106     kristaps  271:        { NULL, roff_userdef, NULL, NULL, 0, NULL },
1.67      kristaps  272: };
                    273:
1.200     schwarze  274: /* not currently implemented: Ds em Eq LP Me PP pp Or Rd Sf SH */
1.175     schwarze  275: const  char *const __mdoc_reserved[] = {
                    276:        "Ac", "Ad", "An", "Ao", "Ap", "Aq", "Ar", "At",
                    277:        "Bc", "Bd", "Bf", "Bk", "Bl", "Bo", "Bq",
                    278:        "Brc", "Bro", "Brq", "Bsx", "Bt", "Bx",
                    279:        "Cd", "Cm", "Db", "Dc", "Dd", "Dl", "Do", "Dq",
1.200     schwarze  280:        "Dt", "Dv", "Dx", "D1",
                    281:        "Ec", "Ed", "Ef", "Ek", "El", "Em",
                    282:        "En", "Eo", "Er", "Es", "Ev", "Ex",
1.175     schwarze  283:        "Fa", "Fc", "Fd", "Fl", "Fn", "Fo", "Fr", "Ft", "Fx",
1.200     schwarze  284:        "Hf", "Ic", "In", "It", "Lb", "Li", "Lk", "Lp",
                    285:        "Ms", "Mt", "Nd", "Nm", "No", "Ns", "Nx",
1.175     schwarze  286:        "Oc", "Oo", "Op", "Os", "Ot", "Ox",
1.200     schwarze  287:        "Pa", "Pc", "Pf", "Po", "Pp", "Pq",
                    288:        "Qc", "Ql", "Qo", "Qq", "Re", "Rs", "Rv",
                    289:        "Sc", "Sh", "Sm", "So", "Sq",
1.175     schwarze  290:        "Ss", "St", "Sx", "Sy",
                    291:        "Ta", "Tn", "Ud", "Ux", "Va", "Vt", "Xc", "Xo", "Xr",
1.200     schwarze  292:        "%A", "%B", "%C", "%D", "%I", "%J", "%N", "%O",
1.175     schwarze  293:        "%P", "%Q", "%R", "%T", "%U", "%V",
                    294:        NULL
                    295: };
                    296:
1.200     schwarze  297: /* not currently implemented: BT DE DS ME MT PT SY TQ YS */
1.175     schwarze  298: const  char *const __man_reserved[] = {
1.200     schwarze  299:        "AT", "B", "BI", "BR", "DT",
                    300:        "EE", "EN", "EQ", "EX", "HP", "I", "IB", "IP", "IR",
                    301:        "LP", "OP", "P", "PD", "PP",
                    302:        "R", "RB", "RE", "RI", "RS", "SB", "SH", "SM", "SS",
                    303:        "TE", "TH", "TP", "TS", "T&", "UC", "UE", "UR",
1.175     schwarze  304:        NULL
                    305: };
                    306:
1.141     kristaps  307: /* Array of injected predefined strings. */
                    308: #define        PREDEFS_MAX      38
                    309: static const struct predef predefs[PREDEFS_MAX] = {
                    310: #include "predefs.in"
                    311: };
                    312:
1.155     kristaps  313: /* See roffhash_find() */
1.85      kristaps  314: #define        ROFF_HASH(p)    (p[0] - ASCII_LO)
                    315:
1.178     schwarze  316: static int      roffit_lines;  /* number of lines to delay */
                    317: static char    *roffit_macro;  /* nil-terminated macro line */
                    318:
1.207     schwarze  319:
1.85      kristaps  320: static void
1.155     kristaps  321: roffhash_init(void)
1.85      kristaps  322: {
                    323:        struct roffmac   *n;
                    324:        int               buc, i;
                    325:
1.106     kristaps  326:        for (i = 0; i < (int)ROFF_USERDEF; i++) {
1.85      kristaps  327:                assert(roffs[i].name[0] >= ASCII_LO);
                    328:                assert(roffs[i].name[0] <= ASCII_HI);
                    329:
                    330:                buc = ROFF_HASH(roffs[i].name);
                    331:
                    332:                if (NULL != (n = hash[buc])) {
                    333:                        for ( ; n->next; n = n->next)
                    334:                                /* Do nothing. */ ;
                    335:                        n->next = &roffs[i];
                    336:                } else
                    337:                        hash[buc] = &roffs[i];
                    338:        }
                    339: }
                    340:
1.67      kristaps  341: /*
                    342:  * Look up a roff token by its name.  Returns ROFF_MAX if no macro by
                    343:  * the nil-terminated string name could be found.
                    344:  */
                    345: static enum rofft
1.155     kristaps  346: roffhash_find(const char *p, size_t s)
1.67      kristaps  347: {
1.85      kristaps  348:        int              buc;
                    349:        struct roffmac  *n;
1.67      kristaps  350:
1.85      kristaps  351:        /*
                    352:         * libroff has an extremely simple hashtable, for the time
                    353:         * being, which simply keys on the first character, which must
                    354:         * be printable, then walks a chain.  It works well enough until
                    355:         * optimised.
                    356:         */
                    357:
                    358:        if (p[0] < ASCII_LO || p[0] > ASCII_HI)
                    359:                return(ROFF_MAX);
                    360:
                    361:        buc = ROFF_HASH(p);
                    362:
                    363:        if (NULL == (n = hash[buc]))
                    364:                return(ROFF_MAX);
                    365:        for ( ; n; n = n->next)
1.106     kristaps  366:                if (0 == strncmp(n->name, p, s) && '\0' == n->name[(int)s])
1.85      kristaps  367:                        return((enum rofft)(n - roffs));
1.67      kristaps  368:
                    369:        return(ROFF_MAX);
                    370: }
                    371:
                    372: /*
                    373:  * Pop the current node off of the stack of roff instructions currently
                    374:  * pending.
                    375:  */
                    376: static void
                    377: roffnode_pop(struct roff *r)
                    378: {
                    379:        struct roffnode *p;
                    380:
1.75      kristaps  381:        assert(r->last);
1.207     schwarze  382:        p = r->last;
1.82      kristaps  383:
1.75      kristaps  384:        r->last = r->last->parent;
1.106     kristaps  385:        free(p->name);
                    386:        free(p->end);
1.67      kristaps  387:        free(p);
                    388: }
                    389:
                    390: /*
                    391:  * Push a roff node onto the instruction stack.  This must later be
                    392:  * removed with roffnode_pop().
                    393:  */
1.98      schwarze  394: static void
1.106     kristaps  395: roffnode_push(struct roff *r, enum rofft tok, const char *name,
                    396:                int line, int col)
1.67      kristaps  397: {
                    398:        struct roffnode *p;
                    399:
1.98      schwarze  400:        p = mandoc_calloc(1, sizeof(struct roffnode));
1.67      kristaps  401:        p->tok = tok;
1.106     kristaps  402:        if (name)
                    403:                p->name = mandoc_strdup(name);
1.67      kristaps  404:        p->parent = r->last;
                    405:        p->line = line;
                    406:        p->col = col;
1.198     schwarze  407:        p->rule = p->parent ? p->parent->rule : 0;
1.67      kristaps  408:
                    409:        r->last = p;
                    410: }
                    411:
                    412: static void
                    413: roff_free1(struct roff *r)
                    414: {
1.176     schwarze  415:        struct tbl_node *tbl;
1.125     kristaps  416:        struct eqn_node *e;
1.167     kristaps  417:        int              i;
1.67      kristaps  418:
1.176     schwarze  419:        while (NULL != (tbl = r->first_tbl)) {
                    420:                r->first_tbl = tbl->next;
                    421:                tbl_free(tbl);
1.109     kristaps  422:        }
                    423:
1.113     kristaps  424:        r->first_tbl = r->last_tbl = r->tbl = NULL;
                    425:
1.125     kristaps  426:        while (NULL != (e = r->first_eqn)) {
                    427:                r->first_eqn = e->next;
                    428:                eqn_free(e);
                    429:        }
                    430:
                    431:        r->first_eqn = r->last_eqn = r->eqn = NULL;
                    432:
1.67      kristaps  433:        while (r->last)
                    434:                roffnode_pop(r);
1.109     kristaps  435:
1.167     kristaps  436:        roff_freestr(r->strtab);
                    437:        roff_freestr(r->xmbtab);
                    438:
                    439:        r->strtab = r->xmbtab = NULL;
                    440:
1.180     schwarze  441:        roff_freereg(r->regtab);
                    442:
                    443:        r->regtab = NULL;
                    444:
1.167     kristaps  445:        if (r->xtab)
                    446:                for (i = 0; i < 128; i++)
                    447:                        free(r->xtab[i].p);
                    448:
                    449:        free(r->xtab);
                    450:        r->xtab = NULL;
1.67      kristaps  451: }
                    452:
                    453: void
                    454: roff_reset(struct roff *r)
                    455: {
                    456:
                    457:        roff_free1(r);
1.174     kristaps  458:        r->control = 0;
1.67      kristaps  459: }
                    460:
                    461: void
                    462: roff_free(struct roff *r)
                    463: {
                    464:
                    465:        roff_free1(r);
                    466:        free(r);
                    467: }
                    468:
                    469: struct roff *
1.199     schwarze  470: roff_alloc(struct mparse *parse, int options)
1.67      kristaps  471: {
                    472:        struct roff     *r;
                    473:
1.98      schwarze  474:        r = mandoc_calloc(1, sizeof(struct roff));
1.128     kristaps  475:        r->parse = parse;
1.199     schwarze  476:        r->options = options;
1.82      kristaps  477:        r->rstackpos = -1;
1.207     schwarze  478:
1.155     kristaps  479:        roffhash_init();
1.141     kristaps  480:
1.67      kristaps  481:        return(r);
                    482: }
                    483:
1.94      kristaps  484: /*
1.206     schwarze  485:  * In the current line, expand escape sequences that tend to get
                    486:  * used in numerical expressions and conditional requests.
                    487:  * Also check the syntax of the remaining escape sequences.
1.154     kristaps  488:  */
1.172     schwarze  489: static enum rofferr
1.142     kristaps  490: roff_res(struct roff *r, char **bufp, size_t *szp, int ln, int pos)
1.94      kristaps  491: {
1.208     schwarze  492:        char             ubuf[24]; /* buffer to print the number */
1.205     schwarze  493:        const char      *start; /* start of the string to process */
1.209     schwarze  494:        char            *stesc; /* start of an escape sequence ('\\') */
1.108     schwarze  495:        const char      *stnam; /* start of the name, after "[(*" */
                    496:        const char      *cp;    /* end of the name, e.g. before ']' */
                    497:        const char      *res;   /* the string to be substituted */
1.181     schwarze  498:        char            *nbuf;  /* new buffer to copy bufp to */
                    499:        size_t           maxl;  /* expected length of the escape name */
                    500:        size_t           naml;  /* actual length of the escape name */
                    501:        int              expand_count;  /* to avoid infinite loops */
1.206     schwarze  502:        int              npos;  /* position in numeric expression */
                    503:        int              irc;   /* return code from roff_evalnum() */
                    504:        char             term;  /* character terminating the escape */
1.94      kristaps  505:
1.170     schwarze  506:        expand_count = 0;
1.205     schwarze  507:        start = *bufp + pos;
                    508:        stesc = strchr(start, '\0') - 1;
                    509:        while (stesc-- > start) {
1.170     schwarze  510:
1.205     schwarze  511:                /* Search backwards for the next backslash. */
                    512:
                    513:                if ('\\' != *stesc)
                    514:                        continue;
                    515:
                    516:                /* If it is escaped, skip it. */
                    517:
                    518:                for (cp = stesc - 1; cp >= start; cp--)
                    519:                        if ('\\' != *cp)
                    520:                                break;
                    521:
                    522:                if (0 == (stesc - cp) % 2) {
1.209     schwarze  523:                        stesc = (char *)cp;
1.205     schwarze  524:                        continue;
                    525:                }
1.108     schwarze  526:
1.206     schwarze  527:                /* Decide whether to expand or to check only. */
1.108     schwarze  528:
1.206     schwarze  529:                term = '\0';
1.205     schwarze  530:                cp = stesc + 1;
1.181     schwarze  531:                switch (*cp) {
1.207     schwarze  532:                case '*':
1.181     schwarze  533:                        res = NULL;
                    534:                        break;
1.207     schwarze  535:                case 'B':
1.206     schwarze  536:                        /* FALLTHROUGH */
1.207     schwarze  537:                case 'w':
1.206     schwarze  538:                        term = cp[1];
                    539:                        /* FALLTHROUGH */
1.207     schwarze  540:                case 'n':
1.181     schwarze  541:                        res = ubuf;
                    542:                        break;
                    543:                default:
1.205     schwarze  544:                        if (ESCAPE_ERROR == mandoc_escape(&cp, NULL, NULL))
                    545:                                mandoc_msg(MANDOCERR_BADESCAPE, r->parse,
                    546:                                    ln, (int)(stesc - *bufp), NULL);
                    547:                        continue;
1.152     kristaps  548:                }
                    549:
1.205     schwarze  550:                if (EXPAND_LIMIT < ++expand_count) {
                    551:                        mandoc_msg(MANDOCERR_ROFFLOOP, r->parse,
                    552:                            ln, (int)(stesc - *bufp), NULL);
                    553:                        return(ROFF_IGN);
                    554:                }
1.108     schwarze  555:
                    556:                /*
                    557:                 * The third character decides the length
1.181     schwarze  558:                 * of the name of the string or register.
1.108     schwarze  559:                 * Save a pointer to the name.
                    560:                 */
                    561:
1.206     schwarze  562:                if ('\0' == term) {
                    563:                        switch (*++cp) {
1.207     schwarze  564:                        case '\0':
1.206     schwarze  565:                                maxl = 0;
                    566:                                break;
1.207     schwarze  567:                        case '(':
1.206     schwarze  568:                                cp++;
                    569:                                maxl = 2;
                    570:                                break;
1.207     schwarze  571:                        case '[':
1.206     schwarze  572:                                cp++;
                    573:                                term = ']';
                    574:                                maxl = 0;
                    575:                                break;
                    576:                        default:
                    577:                                maxl = 1;
                    578:                                break;
                    579:                        }
                    580:                } else {
                    581:                        cp += 2;
1.94      kristaps  582:                        maxl = 0;
                    583:                }
1.108     schwarze  584:                stnam = cp;
1.94      kristaps  585:
1.108     schwarze  586:                /* Advance to the end of the name. */
1.94      kristaps  587:
1.181     schwarze  588:                for (naml = 0; 0 == maxl || naml < maxl; naml++, cp++) {
1.153     kristaps  589:                        if ('\0' == *cp) {
1.207     schwarze  590:                                mandoc_msg(MANDOCERR_BADESCAPE, r->parse,
                    591:                                    ln, (int)(stesc - *bufp), NULL);
1.206     schwarze  592:                                break;
1.153     kristaps  593:                        }
1.206     schwarze  594:                        if (0 == maxl && *cp == term) {
                    595:                                cp++;
1.94      kristaps  596:                                break;
1.206     schwarze  597:                        }
1.94      kristaps  598:                }
                    599:
1.108     schwarze  600:                /*
                    601:                 * Retrieve the replacement string; if it is
                    602:                 * undefined, resume searching for escapes.
                    603:                 */
                    604:
1.206     schwarze  605:                switch (stesc[1]) {
1.207     schwarze  606:                case '*':
1.181     schwarze  607:                        res = roff_getstrn(r, stnam, naml);
1.206     schwarze  608:                        break;
1.207     schwarze  609:                case 'B':
1.206     schwarze  610:                        npos = 0;
                    611:                        irc = roff_evalnum(stnam, &npos, NULL, 0);
                    612:                        ubuf[0] = irc && stnam + npos + 1 == cp
                    613:                            ? '1' : '0';
                    614:                        ubuf[1] = '\0';
                    615:                        break;
1.207     schwarze  616:                case 'n':
1.208     schwarze  617:                        (void)snprintf(ubuf, sizeof(ubuf), "%d",
1.181     schwarze  618:                            roff_getregn(r, stnam, naml));
1.206     schwarze  619:                        break;
1.207     schwarze  620:                case 'w':
1.208     schwarze  621:                        (void)snprintf(ubuf, sizeof(ubuf), "%d",
1.206     schwarze  622:                            24 * (int)naml);
                    623:                        break;
                    624:                }
1.94      kristaps  625:
                    626:                if (NULL == res) {
1.207     schwarze  627:                        mandoc_msg(MANDOCERR_BADESCAPE, r->parse,
                    628:                            ln, (int)(stesc - *bufp), NULL);
1.142     kristaps  629:                        res = "";
1.94      kristaps  630:                }
                    631:
1.108     schwarze  632:                /* Replace the escape sequence by the string. */
                    633:
1.209     schwarze  634:                *stesc = '\0';
                    635:                *szp = mandoc_asprintf(&nbuf, "%s%s%s",
                    636:                    *bufp, res, cp) + 1;
1.94      kristaps  637:
1.205     schwarze  638:                /* Prepare for the next replacement. */
1.94      kristaps  639:
1.205     schwarze  640:                start = nbuf + pos;
1.209     schwarze  641:                stesc = nbuf + (stesc - *bufp) + strlen(res);
1.94      kristaps  642:                free(*bufp);
1.181     schwarze  643:                *bufp = nbuf;
1.154     kristaps  644:        }
1.172     schwarze  645:        return(ROFF_CONT);
1.154     kristaps  646: }
                    647:
                    648: /*
1.178     schwarze  649:  * Process text streams:
                    650:  * Convert all breakable hyphens into ASCII_HYPH.
                    651:  * Decrement and spring input line trap.
1.154     kristaps  652:  */
                    653: static enum rofferr
1.178     schwarze  654: roff_parsetext(char **bufp, size_t *szp, int pos, int *offs)
1.154     kristaps  655: {
                    656:        size_t           sz;
                    657:        const char      *start;
1.178     schwarze  658:        char            *p;
                    659:        int              isz;
1.154     kristaps  660:        enum mandoc_esc  esc;
                    661:
1.178     schwarze  662:        start = p = *bufp + pos;
1.154     kristaps  663:
                    664:        while ('\0' != *p) {
                    665:                sz = strcspn(p, "-\\");
                    666:                p += sz;
                    667:
1.159     kristaps  668:                if ('\0' == *p)
                    669:                        break;
                    670:
1.154     kristaps  671:                if ('\\' == *p) {
                    672:                        /* Skip over escapes. */
                    673:                        p++;
1.189     schwarze  674:                        esc = mandoc_escape((const char **)&p, NULL, NULL);
1.154     kristaps  675:                        if (ESCAPE_ERROR == esc)
                    676:                                break;
1.155     kristaps  677:                        continue;
1.159     kristaps  678:                } else if (p == start) {
1.158     kristaps  679:                        p++;
1.155     kristaps  680:                        continue;
1.158     kristaps  681:                }
1.155     kristaps  682:
1.171     schwarze  683:                if (isalpha((unsigned char)p[-1]) &&
                    684:                    isalpha((unsigned char)p[1]))
1.155     kristaps  685:                        *p = ASCII_HYPH;
                    686:                p++;
1.94      kristaps  687:        }
                    688:
1.178     schwarze  689:        /* Spring the input line trap. */
                    690:        if (1 == roffit_lines) {
1.202     schwarze  691:                isz = mandoc_asprintf(&p, "%s\n.%s", *bufp, roffit_macro);
1.178     schwarze  692:                free(*bufp);
                    693:                *bufp = p;
                    694:                *szp = isz + 1;
                    695:                *offs = 0;
                    696:                free(roffit_macro);
                    697:                roffit_lines = 0;
                    698:                return(ROFF_REPARSE);
                    699:        } else if (1 < roffit_lines)
                    700:                --roffit_lines;
1.154     kristaps  701:        return(ROFF_CONT);
1.94      kristaps  702: }
                    703:
1.67      kristaps  704: enum rofferr
1.207     schwarze  705: roff_parseln(struct roff *r, int ln, char **bufp,
1.90      kristaps  706:                size_t *szp, int pos, int *offs)
1.67      kristaps  707: {
                    708:        enum rofft       t;
1.109     kristaps  709:        enum rofferr     e;
1.130     kristaps  710:        int              ppos, ctl;
1.79      kristaps  711:
                    712:        /*
1.94      kristaps  713:         * Run the reserved-word filter only if we have some reserved
                    714:         * words to fill in.
                    715:         */
                    716:
1.172     schwarze  717:        e = roff_res(r, bufp, szp, ln, pos);
                    718:        if (ROFF_IGN == e)
                    719:                return(e);
                    720:        assert(ROFF_CONT == e);
1.94      kristaps  721:
1.130     kristaps  722:        ppos = pos;
1.174     kristaps  723:        ctl = roff_getcontrol(r, *bufp, &pos);
1.130     kristaps  724:
1.94      kristaps  725:        /*
1.79      kristaps  726:         * First, if a scope is open and we're not a macro, pass the
                    727:         * text through the macro's filter.  If a scope isn't open and
                    728:         * we're not a macro, just let it through.
1.125     kristaps  729:         * Finally, if there's an equation scope open, divert it into it
                    730:         * no matter our state.
1.79      kristaps  731:         */
1.74      kristaps  732:
1.130     kristaps  733:        if (r->last && ! ctl) {
1.78      kristaps  734:                t = r->last->tok;
                    735:                assert(roffs[t].text);
1.207     schwarze  736:                e = (*roffs[t].text)(r, t, bufp, szp, ln, pos, pos, offs);
1.109     kristaps  737:                assert(ROFF_IGN == e || ROFF_CONT == e);
1.125     kristaps  738:                if (ROFF_CONT != e)
                    739:                        return(e);
1.182     schwarze  740:        }
                    741:        if (r->eqn)
                    742:                return(eqn_read(&r->eqn, ln, *bufp, ppos, offs));
                    743:        if ( ! ctl) {
1.125     kristaps  744:                if (r->tbl)
1.130     kristaps  745:                        return(tbl_read(r->tbl, ln, *bufp, pos));
1.178     schwarze  746:                return(roff_parsetext(bufp, szp, pos, offs));
1.182     schwarze  747:        }
1.67      kristaps  748:
1.79      kristaps  749:        /*
                    750:         * If a scope is open, go to the child handler for that macro,
                    751:         * as it may want to preprocess before doing anything with it.
1.125     kristaps  752:         * Don't do so if an equation is open.
1.79      kristaps  753:         */
1.78      kristaps  754:
1.79      kristaps  755:        if (r->last) {
                    756:                t = r->last->tok;
                    757:                assert(roffs[t].sub);
1.207     schwarze  758:                return((*roffs[t].sub)(r, t, bufp, szp,
                    759:                    ln, ppos, pos, offs));
1.79      kristaps  760:        }
1.78      kristaps  761:
1.79      kristaps  762:        /*
                    763:         * Lastly, as we've no scope open, try to look up and execute
                    764:         * the new macro.  If no macro is found, simply return and let
                    765:         * the compilers handle it.
                    766:         */
1.67      kristaps  767:
1.214     schwarze  768:        if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos, ln, ppos)))
1.79      kristaps  769:                return(ROFF_CONT);
1.67      kristaps  770:
1.75      kristaps  771:        assert(roffs[t].proc);
1.207     schwarze  772:        return((*roffs[t].proc)(r, t, bufp, szp, ln, ppos, pos, offs));
1.74      kristaps  773: }
                    774:
1.117     kristaps  775: void
1.74      kristaps  776: roff_endparse(struct roff *r)
                    777: {
                    778:
1.110     kristaps  779:        if (r->last)
1.128     kristaps  780:                mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
1.207     schwarze  781:                    r->last->line, r->last->col, NULL);
1.117     kristaps  782:
1.125     kristaps  783:        if (r->eqn) {
1.207     schwarze  784:                mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
                    785:                    r->eqn->eqn.ln, r->eqn->eqn.pos, NULL);
1.151     kristaps  786:                eqn_end(&r->eqn);
1.125     kristaps  787:        }
                    788:
1.117     kristaps  789:        if (r->tbl) {
1.207     schwarze  790:                mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
                    791:                    r->tbl->line, r->tbl->pos, NULL);
1.151     kristaps  792:                tbl_end(&r->tbl);
1.117     kristaps  793:        }
1.67      kristaps  794: }
                    795:
                    796: /*
                    797:  * Parse a roff node's type from the input buffer.  This must be in the
                    798:  * form of ".foo xxx" in the usual way.
                    799:  */
                    800: static enum rofft
1.214     schwarze  801: roff_parse(struct roff *r, char *buf, int *pos, int ln, int ppos)
1.67      kristaps  802: {
1.214     schwarze  803:        char            *cp;
1.106     kristaps  804:        const char      *mac;
                    805:        size_t           maclen;
1.67      kristaps  806:        enum rofft       t;
                    807:
1.214     schwarze  808:        cp = buf + *pos;
                    809:
                    810:        if ('\0' == *cp || '"' == *cp || '\t' == *cp || ' ' == *cp)
1.67      kristaps  811:                return(ROFF_MAX);
                    812:
1.214     schwarze  813:        mac = cp;
                    814:        maclen = roff_getname(r, &cp, ln, ppos);
1.67      kristaps  815:
1.106     kristaps  816:        t = (r->current_string = roff_getstrn(r, mac, maclen))
1.155     kristaps  817:            ? ROFF_USERDEF : roffhash_find(mac, maclen);
1.67      kristaps  818:
1.214     schwarze  819:        if (ROFF_MAX != t)
                    820:                *pos = cp - buf;
1.67      kristaps  821:
                    822:        return(t);
                    823: }
                    824:
                    825: static enum rofferr
1.76      kristaps  826: roff_cblock(ROFF_ARGS)
1.67      kristaps  827: {
                    828:
1.79      kristaps  829:        /*
                    830:         * A block-close `..' should only be invoked as a child of an
                    831:         * ignore macro, otherwise raise a warning and just ignore it.
                    832:         */
                    833:
1.76      kristaps  834:        if (NULL == r->last) {
1.128     kristaps  835:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.76      kristaps  836:                return(ROFF_IGN);
                    837:        }
1.67      kristaps  838:
1.81      kristaps  839:        switch (r->last->tok) {
1.207     schwarze  840:        case ROFF_am:
1.81      kristaps  841:                /* FALLTHROUGH */
1.207     schwarze  842:        case ROFF_ami:
1.81      kristaps  843:                /* FALLTHROUGH */
1.207     schwarze  844:        case ROFF_am1:
1.81      kristaps  845:                /* FALLTHROUGH */
1.207     schwarze  846:        case ROFF_de:
1.108     schwarze  847:                /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1.81      kristaps  848:                /* FALLTHROUGH */
1.207     schwarze  849:        case ROFF_dei:
1.81      kristaps  850:                /* FALLTHROUGH */
1.207     schwarze  851:        case ROFF_ig:
1.81      kristaps  852:                break;
                    853:        default:
1.128     kristaps  854:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.67      kristaps  855:                return(ROFF_IGN);
1.76      kristaps  856:        }
1.67      kristaps  857:
1.76      kristaps  858:        if ((*bufp)[pos])
1.217   ! schwarze  859:                mandoc_vmsg(MANDOCERR_ARG_SKIP, r->parse, ln, pos,
        !           860:                    ".. %s", *bufp + pos);
1.71      kristaps  861:
                    862:        roffnode_pop(r);
1.76      kristaps  863:        roffnode_cleanscope(r);
                    864:        return(ROFF_IGN);
1.71      kristaps  865:
1.67      kristaps  866: }
                    867:
1.76      kristaps  868: static void
                    869: roffnode_cleanscope(struct roff *r)
1.67      kristaps  870: {
                    871:
1.76      kristaps  872:        while (r->last) {
1.173     schwarze  873:                if (--r->last->endspan != 0)
1.76      kristaps  874:                        break;
                    875:                roffnode_pop(r);
                    876:        }
1.67      kristaps  877: }
                    878:
1.195     schwarze  879: static void
                    880: roff_ccond(struct roff *r, int ln, int ppos)
1.74      kristaps  881: {
                    882:
1.76      kristaps  883:        if (NULL == r->last) {
1.128     kristaps  884:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.195     schwarze  885:                return;
1.76      kristaps  886:        }
                    887:
1.82      kristaps  888:        switch (r->last->tok) {
1.207     schwarze  889:        case ROFF_el:
1.82      kristaps  890:                /* FALLTHROUGH */
1.207     schwarze  891:        case ROFF_ie:
1.82      kristaps  892:                /* FALLTHROUGH */
1.207     schwarze  893:        case ROFF_if:
1.82      kristaps  894:                break;
                    895:        default:
1.128     kristaps  896:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.195     schwarze  897:                return;
1.75      kristaps  898:        }
                    899:
1.76      kristaps  900:        if (r->last->endspan > -1) {
1.128     kristaps  901:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.195     schwarze  902:                return;
1.76      kristaps  903:        }
                    904:
1.75      kristaps  905:        roffnode_pop(r);
1.76      kristaps  906:        roffnode_cleanscope(r);
1.195     schwarze  907:        return;
1.76      kristaps  908: }
                    909:
                    910: static enum rofferr
1.80      kristaps  911: roff_block(ROFF_ARGS)
1.76      kristaps  912: {
1.213     schwarze  913:        char            *name, *cp;
                    914:        size_t           namesz;
1.106     kristaps  915:
1.213     schwarze  916:        name = cp = *bufp + pos;
                    917:        namesz = 0;
1.76      kristaps  918:
1.106     kristaps  919:        if (ROFF_ig != tok) {
1.213     schwarze  920:                if ('\0' == *cp) {
1.216     schwarze  921:                        mandoc_msg(MANDOCERR_REQ_EMPTY, r->parse,
                    922:                            ln, ppos, roffs[tok].name);
1.106     kristaps  923:                        return(ROFF_IGN);
                    924:                }
1.107     kristaps  925:
                    926:                /*
                    927:                 * Re-write `de1', since we don't really care about
                    928:                 * groff's strange compatibility mode, into `de'.
                    929:                 */
                    930:
1.106     kristaps  931:                if (ROFF_de1 == tok)
                    932:                        tok = ROFF_de;
1.213     schwarze  933:                else if (ROFF_de != tok)
1.128     kristaps  934:                        mandoc_msg(MANDOCERR_REQUEST, r->parse, ln, ppos,
1.106     kristaps  935:                            roffs[tok].name);
1.107     kristaps  936:
1.213     schwarze  937:                namesz = roff_getname(r, &cp, ln, ppos);
                    938:                name[namesz] = '\0';
                    939:        } else
                    940:                name = NULL;
1.80      kristaps  941:
1.106     kristaps  942:        roffnode_push(r, tok, name, ln, ppos);
                    943:
                    944:        /*
                    945:         * At the beginning of a `de' macro, clear the existing string
                    946:         * with the same name, if there is one.  New content will be
1.193     schwarze  947:         * appended from roff_block_text() in multiline mode.
1.106     kristaps  948:         */
1.107     kristaps  949:
1.213     schwarze  950:        if (namesz && ROFF_de == tok)
                    951:                roff_setstrn(&r->strtab, name, namesz, "", 0, 0);
1.76      kristaps  952:
1.213     schwarze  953:        if ('\0' == *cp)
1.78      kristaps  954:                return(ROFF_IGN);
                    955:
1.107     kristaps  956:        /* If present, process the custom end-of-line marker. */
                    957:
1.213     schwarze  958:        name = cp;
                    959:        namesz = roff_getname(r, &cp, ln, ppos);
                    960:        if (namesz)
                    961:                r->last->end = mandoc_strndup(name, namesz);
1.78      kristaps  962:
1.213     schwarze  963:        if ('\0' != *cp)
1.217   ! schwarze  964:                mandoc_vmsg(MANDOCERR_ARG_EXCESS, r->parse,
        !           965:                    ln, pos, ".%s ... %s", roffs[tok].name, cp);
1.74      kristaps  966:
1.78      kristaps  967:        return(ROFF_IGN);
                    968: }
                    969:
                    970: static enum rofferr
1.80      kristaps  971: roff_block_sub(ROFF_ARGS)
1.79      kristaps  972: {
                    973:        enum rofft      t;
                    974:        int             i, j;
                    975:
                    976:        /*
                    977:         * First check whether a custom macro exists at this level.  If
                    978:         * it does, then check against it.  This is some of groff's
                    979:         * stranger behaviours.  If we encountered a custom end-scope
                    980:         * tag and that tag also happens to be a "real" macro, then we
                    981:         * need to try interpreting it again as a real macro.  If it's
                    982:         * not, then return ignore.  Else continue.
                    983:         */
                    984:
                    985:        if (r->last->end) {
1.130     kristaps  986:                for (i = pos, j = 0; r->last->end[j]; j++, i++)
1.79      kristaps  987:                        if ((*bufp)[i] != r->last->end[j])
                    988:                                break;
                    989:
1.207     schwarze  990:                if ('\0' == r->last->end[j] &&
                    991:                    ('\0' == (*bufp)[i] ||
                    992:                     ' '  == (*bufp)[i] ||
                    993:                     '\t' == (*bufp)[i])) {
1.79      kristaps  994:                        roffnode_pop(r);
                    995:                        roffnode_cleanscope(r);
                    996:
1.130     kristaps  997:                        while (' ' == (*bufp)[i] || '\t' == (*bufp)[i])
                    998:                                i++;
                    999:
                   1000:                        pos = i;
1.214     schwarze 1001:                        if (ROFF_MAX != roff_parse(r, *bufp, &pos, ln, ppos))
1.79      kristaps 1002:                                return(ROFF_RERUN);
                   1003:                        return(ROFF_IGN);
                   1004:                }
                   1005:        }
                   1006:
                   1007:        /*
                   1008:         * If we have no custom end-query or lookup failed, then try
                   1009:         * pulling it out of the hashtable.
                   1010:         */
                   1011:
1.214     schwarze 1012:        t = roff_parse(r, *bufp, &pos, ln, ppos);
1.79      kristaps 1013:
1.106     kristaps 1014:        /*
                   1015:         * Macros other than block-end are only significant
                   1016:         * in `de' blocks; elsewhere, simply throw them away.
                   1017:         */
                   1018:        if (ROFF_cblock != t) {
                   1019:                if (ROFF_de == tok)
1.193     schwarze 1020:                        roff_setstr(r, r->last->name, *bufp + ppos, 2);
1.79      kristaps 1021:                return(ROFF_IGN);
1.106     kristaps 1022:        }
1.79      kristaps 1023:
                   1024:        assert(roffs[t].proc);
1.207     schwarze 1025:        return((*roffs[t].proc)(r, t, bufp, szp, ln, ppos, pos, offs));
1.79      kristaps 1026: }
                   1027:
                   1028: static enum rofferr
1.80      kristaps 1029: roff_block_text(ROFF_ARGS)
1.78      kristaps 1030: {
                   1031:
1.106     kristaps 1032:        if (ROFF_de == tok)
1.193     schwarze 1033:                roff_setstr(r, r->last->name, *bufp + pos, 2);
1.106     kristaps 1034:
1.78      kristaps 1035:        return(ROFF_IGN);
                   1036: }
                   1037:
                   1038: static enum rofferr
1.82      kristaps 1039: roff_cond_sub(ROFF_ARGS)
                   1040: {
                   1041:        enum rofft       t;
1.139     kristaps 1042:        char            *ep;
1.198     schwarze 1043:        int              rr;
1.82      kristaps 1044:
                   1045:        rr = r->last->rule;
1.139     kristaps 1046:        roffnode_cleanscope(r);
1.214     schwarze 1047:        t = roff_parse(r, *bufp, &pos, ln, ppos);
1.82      kristaps 1048:
1.139     kristaps 1049:        /*
1.177     schwarze 1050:         * Fully handle known macros when they are structurally
                   1051:         * required or when the conditional evaluated to true.
1.87      kristaps 1052:         */
                   1053:
1.177     schwarze 1054:        if ((ROFF_MAX != t) &&
1.198     schwarze 1055:            (rr || ROFFMAC_STRUCT & roffs[t].flags)) {
1.177     schwarze 1056:                assert(roffs[t].proc);
                   1057:                return((*roffs[t].proc)(r, t, bufp, szp,
1.207     schwarze 1058:                    ln, ppos, pos, offs));
1.177     schwarze 1059:        }
1.144     kristaps 1060:
1.196     schwarze 1061:        /*
                   1062:         * If `\}' occurs on a macro line without a preceding macro,
                   1063:         * drop the line completely.
                   1064:         */
                   1065:
                   1066:        ep = *bufp + pos;
                   1067:        if ('\\' == ep[0] && '}' == ep[1])
1.198     schwarze 1068:                rr = 0;
1.196     schwarze 1069:
1.177     schwarze 1070:        /* Always check for the closing delimiter `\}'. */
1.144     kristaps 1071:
1.177     schwarze 1072:        while (NULL != (ep = strchr(ep, '\\'))) {
1.197     schwarze 1073:                if ('}' == *(++ep)) {
                   1074:                        *ep = '&';
                   1075:                        roff_ccond(r, ln, ep - *bufp - 1);
                   1076:                }
                   1077:                ++ep;
1.177     schwarze 1078:        }
1.198     schwarze 1079:        return(rr ? ROFF_CONT : ROFF_IGN);
1.82      kristaps 1080: }
                   1081:
                   1082: static enum rofferr
                   1083: roff_cond_text(ROFF_ARGS)
1.78      kristaps 1084: {
1.140     kristaps 1085:        char            *ep;
1.198     schwarze 1086:        int              rr;
1.82      kristaps 1087:
                   1088:        rr = r->last->rule;
1.140     kristaps 1089:        roffnode_cleanscope(r);
1.82      kristaps 1090:
1.197     schwarze 1091:        ep = *bufp + pos;
                   1092:        while (NULL != (ep = strchr(ep, '\\'))) {
                   1093:                if ('}' == *(++ep)) {
                   1094:                        *ep = '&';
                   1095:                        roff_ccond(r, ln, ep - *bufp - 1);
                   1096:                }
                   1097:                ++ep;
1.78      kristaps 1098:        }
1.198     schwarze 1099:        return(rr ? ROFF_CONT : ROFF_IGN);
1.74      kristaps 1100: }
                   1101:
1.204     schwarze 1102: /*
                   1103:  * Parse a single signed integer number.  Stop at the first non-digit.
                   1104:  * If there is at least one digit, return success and advance the
                   1105:  * parse point, else return failure and let the parse point unchanged.
                   1106:  * Ignore overflows, treat them just like the C language.
                   1107:  */
1.184     schwarze 1108: static int
                   1109: roff_getnum(const char *v, int *pos, int *res)
                   1110: {
1.206     schwarze 1111:        int      myres, n, p;
                   1112:
                   1113:        if (NULL == res)
                   1114:                res = &myres;
1.184     schwarze 1115:
                   1116:        p = *pos;
                   1117:        n = v[p] == '-';
                   1118:        if (n)
                   1119:                p++;
                   1120:
                   1121:        for (*res = 0; isdigit((unsigned char)v[p]); p++)
1.204     schwarze 1122:                *res = 10 * *res + v[p] - '0';
1.184     schwarze 1123:        if (p == *pos + n)
                   1124:                return 0;
                   1125:
                   1126:        if (n)
                   1127:                *res = -*res;
                   1128:
                   1129:        *pos = p;
                   1130:        return 1;
                   1131: }
                   1132:
1.198     schwarze 1133: /*
                   1134:  * Evaluate a string comparison condition.
                   1135:  * The first character is the delimiter.
                   1136:  * Succeed if the string up to its second occurrence
                   1137:  * matches the string up to its third occurence.
                   1138:  * Advance the cursor after the third occurrence
                   1139:  * or lacking that, to the end of the line.
                   1140:  */
                   1141: static int
                   1142: roff_evalstrcond(const char *v, int *pos)
                   1143: {
                   1144:        const char      *s1, *s2, *s3;
                   1145:        int              match;
                   1146:
                   1147:        match = 0;
                   1148:        s1 = v + *pos;          /* initial delimiter */
                   1149:        s2 = s1 + 1;            /* for scanning the first string */
                   1150:        s3 = strchr(s2, *s1);   /* for scanning the second string */
                   1151:
                   1152:        if (NULL == s3)         /* found no middle delimiter */
                   1153:                goto out;
                   1154:
                   1155:        while ('\0' != *++s3) {
                   1156:                if (*s2 != *s3) {  /* mismatch */
                   1157:                        s3 = strchr(s3, *s1);
                   1158:                        break;
                   1159:                }
                   1160:                if (*s3 == *s1) {  /* found the final delimiter */
                   1161:                        match = 1;
                   1162:                        break;
                   1163:                }
                   1164:                s2++;
                   1165:        }
                   1166:
                   1167: out:
                   1168:        if (NULL == s3)
                   1169:                s3 = strchr(s2, '\0');
                   1170:        else
                   1171:                s3++;
                   1172:        *pos = s3 - v;
                   1173:        return(match);
                   1174: }
                   1175:
1.204     schwarze 1176: /*
                   1177:  * Evaluate an optionally negated single character, numerical,
                   1178:  * or string condition.
                   1179:  */
1.198     schwarze 1180: static int
1.88      kristaps 1181: roff_evalcond(const char *v, int *pos)
                   1182: {
1.204     schwarze 1183:        int      wanttrue, number;
1.88      kristaps 1184:
1.198     schwarze 1185:        if ('!' == v[*pos]) {
                   1186:                wanttrue = 0;
                   1187:                (*pos)++;
                   1188:        } else
                   1189:                wanttrue = 1;
                   1190:
1.88      kristaps 1191:        switch (v[*pos]) {
1.207     schwarze 1192:        case 'n':
1.198     schwarze 1193:                /* FALLTHROUGH */
1.207     schwarze 1194:        case 'o':
1.88      kristaps 1195:                (*pos)++;
1.198     schwarze 1196:                return(wanttrue);
1.207     schwarze 1197:        case 'c':
1.198     schwarze 1198:                /* FALLTHROUGH */
1.207     schwarze 1199:        case 'd':
1.198     schwarze 1200:                /* FALLTHROUGH */
1.207     schwarze 1201:        case 'e':
1.88      kristaps 1202:                /* FALLTHROUGH */
1.207     schwarze 1203:        case 'r':
1.88      kristaps 1204:                /* FALLTHROUGH */
1.207     schwarze 1205:        case 't':
1.88      kristaps 1206:                (*pos)++;
1.198     schwarze 1207:                return(!wanttrue);
1.88      kristaps 1208:        default:
                   1209:                break;
                   1210:        }
                   1211:
1.204     schwarze 1212:        if (roff_evalnum(v, pos, &number, 0))
                   1213:                return((number > 0) == wanttrue);
                   1214:        else
1.198     schwarze 1215:                return(roff_evalstrcond(v, pos) == wanttrue);
1.88      kristaps 1216: }
                   1217:
1.74      kristaps 1218: static enum rofferr
1.103     kristaps 1219: roff_line_ignore(ROFF_ARGS)
1.89      kristaps 1220: {
1.123     schwarze 1221:
1.89      kristaps 1222:        return(ROFF_IGN);
                   1223: }
                   1224:
1.104     kristaps 1225: static enum rofferr
1.82      kristaps 1226: roff_cond(ROFF_ARGS)
1.74      kristaps 1227: {
1.173     schwarze 1228:
                   1229:        roffnode_push(r, tok, NULL, ln, ppos);
1.74      kristaps 1230:
1.207     schwarze 1231:        /*
1.134     kristaps 1232:         * An `.el' has no conditional body: it will consume the value
                   1233:         * of the current rstack entry set in prior `ie' calls or
1.207     schwarze 1234:         * defaults to DENY.
1.134     kristaps 1235:         *
                   1236:         * If we're not an `el', however, then evaluate the conditional.
                   1237:         */
1.133     kristaps 1238:
1.173     schwarze 1239:        r->last->rule = ROFF_el == tok ?
1.207     schwarze 1240:            (r->rstackpos < 0 ? 0 : r->rstack[r->rstackpos--]) :
                   1241:            roff_evalcond(*bufp, &pos);
1.77      kristaps 1242:
1.134     kristaps 1243:        /*
                   1244:         * An if-else will put the NEGATION of the current evaluated
                   1245:         * conditional into the stack of rules.
                   1246:         */
                   1247:
1.84      schwarze 1248:        if (ROFF_ie == tok) {
1.134     kristaps 1249:                if (r->rstackpos == RSTACK_MAX - 1) {
1.207     schwarze 1250:                        mandoc_msg(MANDOCERR_MEM,
                   1251:                            r->parse, ln, ppos, NULL);
1.134     kristaps 1252:                        return(ROFF_ERR);
                   1253:                }
1.198     schwarze 1254:                r->rstack[++r->rstackpos] = !r->last->rule;
1.82      kristaps 1255:        }
1.88      kristaps 1256:
                   1257:        /* If the parent has false as its rule, then so do we. */
                   1258:
1.198     schwarze 1259:        if (r->last->parent && !r->last->parent->rule)
                   1260:                r->last->rule = 0;
1.88      kristaps 1261:
                   1262:        /*
1.173     schwarze 1263:         * Determine scope.
                   1264:         * If there is nothing on the line after the conditional,
                   1265:         * not even whitespace, use next-line scope.
1.88      kristaps 1266:         */
1.74      kristaps 1267:
1.173     schwarze 1268:        if ('\0' == (*bufp)[pos]) {
                   1269:                r->last->endspan = 2;
                   1270:                goto out;
                   1271:        }
                   1272:
                   1273:        while (' ' == (*bufp)[pos])
                   1274:                pos++;
                   1275:
                   1276:        /* An opening brace requests multiline scope. */
1.75      kristaps 1277:
                   1278:        if ('\\' == (*bufp)[pos] && '{' == (*bufp)[pos + 1]) {
                   1279:                r->last->endspan = -1;
                   1280:                pos += 2;
1.173     schwarze 1281:                goto out;
1.207     schwarze 1282:        }
1.74      kristaps 1283:
1.77      kristaps 1284:        /*
1.173     schwarze 1285:         * Anything else following the conditional causes
                   1286:         * single-line scope.  Warn if the scope contains
                   1287:         * nothing but trailing whitespace.
1.77      kristaps 1288:         */
                   1289:
1.75      kristaps 1290:        if ('\0' == (*bufp)[pos])
1.216     schwarze 1291:                mandoc_msg(MANDOCERR_COND_EMPTY, r->parse,
                   1292:                    ln, ppos, roffs[tok].name);
1.77      kristaps 1293:
1.173     schwarze 1294:        r->last->endspan = 1;
1.74      kristaps 1295:
1.173     schwarze 1296: out:
1.75      kristaps 1297:        *offs = pos;
                   1298:        return(ROFF_RERUN);
1.83      schwarze 1299: }
                   1300:
                   1301: static enum rofferr
1.92      schwarze 1302: roff_ds(ROFF_ARGS)
                   1303: {
1.212     schwarze 1304:        char            *string;
                   1305:        const char      *name;
                   1306:        size_t           namesz;
1.96      kristaps 1307:
                   1308:        /*
1.212     schwarze 1309:         * The first word is the name of the string.
                   1310:         * If it is empty or terminated by an escape sequence,
                   1311:         * abort the `ds' request without defining anything.
1.96      kristaps 1312:         */
1.92      schwarze 1313:
1.212     schwarze 1314:        name = string = *bufp + pos;
1.92      schwarze 1315:        if ('\0' == *name)
                   1316:                return(ROFF_IGN);
                   1317:
1.212     schwarze 1318:        namesz = roff_getname(r, &string, ln, pos);
                   1319:        if ('\\' == name[namesz])
                   1320:                return(ROFF_IGN);
                   1321:
                   1322:        /* Read past the initial double-quote, if any. */
1.121     schwarze 1323:        if ('"' == *string)
1.92      schwarze 1324:                string++;
                   1325:
1.96      kristaps 1326:        /* The rest is the value. */
1.212     schwarze 1327:        roff_setstrn(&r->strtab, name, namesz, string, strlen(string),
                   1328:            ROFF_as == tok);
1.92      schwarze 1329:        return(ROFF_IGN);
                   1330: }
                   1331:
1.204     schwarze 1332: /*
                   1333:  * Parse a single operator, one or two characters long.
                   1334:  * If the operator is recognized, return success and advance the
                   1335:  * parse point, else return failure and let the parse point unchanged.
                   1336:  */
                   1337: static int
                   1338: roff_getop(const char *v, int *pos, char *res)
                   1339: {
                   1340:
                   1341:        *res = v[*pos];
                   1342:
                   1343:        switch (*res) {
1.207     schwarze 1344:        case '+':
1.204     schwarze 1345:                /* FALLTHROUGH */
1.207     schwarze 1346:        case '-':
1.204     schwarze 1347:                /* FALLTHROUGH */
1.207     schwarze 1348:        case '*':
1.204     schwarze 1349:                /* FALLTHROUGH */
1.207     schwarze 1350:        case '/':
1.204     schwarze 1351:                /* FALLTHROUGH */
1.207     schwarze 1352:        case '%':
1.204     schwarze 1353:                /* FALLTHROUGH */
1.207     schwarze 1354:        case '&':
1.204     schwarze 1355:                /* FALLTHROUGH */
1.207     schwarze 1356:        case ':':
1.204     schwarze 1357:                break;
                   1358:        case '<':
                   1359:                switch (v[*pos + 1]) {
1.207     schwarze 1360:                case '=':
1.204     schwarze 1361:                        *res = 'l';
                   1362:                        (*pos)++;
                   1363:                        break;
1.207     schwarze 1364:                case '>':
1.204     schwarze 1365:                        *res = '!';
                   1366:                        (*pos)++;
                   1367:                        break;
1.207     schwarze 1368:                case '?':
1.204     schwarze 1369:                        *res = 'i';
                   1370:                        (*pos)++;
                   1371:                        break;
                   1372:                default:
                   1373:                        break;
                   1374:                }
                   1375:                break;
                   1376:        case '>':
                   1377:                switch (v[*pos + 1]) {
1.207     schwarze 1378:                case '=':
1.204     schwarze 1379:                        *res = 'g';
                   1380:                        (*pos)++;
                   1381:                        break;
1.207     schwarze 1382:                case '?':
1.204     schwarze 1383:                        *res = 'a';
                   1384:                        (*pos)++;
                   1385:                        break;
                   1386:                default:
                   1387:                        break;
                   1388:                }
                   1389:                break;
                   1390:        case '=':
                   1391:                if ('=' == v[*pos + 1])
                   1392:                        (*pos)++;
                   1393:                break;
                   1394:        default:
                   1395:                return(0);
                   1396:        }
                   1397:        (*pos)++;
                   1398:
                   1399:        return(*res);
                   1400: }
                   1401:
                   1402: /*
                   1403:  * Evaluate either a parenthesized numeric expression
                   1404:  * or a single signed integer number.
                   1405:  */
                   1406: static int
                   1407: roff_evalpar(const char *v, int *pos, int *res)
                   1408: {
                   1409:
                   1410:        if ('(' != v[*pos])
                   1411:                return(roff_getnum(v, pos, res));
                   1412:
                   1413:        (*pos)++;
                   1414:        if ( ! roff_evalnum(v, pos, res, 1))
                   1415:                return(0);
                   1416:
1.206     schwarze 1417:        /*
                   1418:         * Omission of the closing parenthesis
                   1419:         * is an error in validation mode,
                   1420:         * but ignored in evaluation mode.
                   1421:         */
                   1422:
1.204     schwarze 1423:        if (')' == v[*pos])
                   1424:                (*pos)++;
1.206     schwarze 1425:        else if (NULL == res)
                   1426:                return(0);
1.204     schwarze 1427:
                   1428:        return(1);
                   1429: }
                   1430:
                   1431: /*
                   1432:  * Evaluate a complete numeric expression.
                   1433:  * Proceed left to right, there is no concept of precedence.
                   1434:  */
                   1435: static int
                   1436: roff_evalnum(const char *v, int *pos, int *res, int skipwhite)
                   1437: {
                   1438:        int              mypos, operand2;
                   1439:        char             operator;
                   1440:
                   1441:        if (NULL == pos) {
                   1442:                mypos = 0;
                   1443:                pos = &mypos;
                   1444:        }
                   1445:
                   1446:        if (skipwhite)
                   1447:                while (isspace((unsigned char)v[*pos]))
                   1448:                        (*pos)++;
                   1449:
                   1450:        if ( ! roff_evalpar(v, pos, res))
                   1451:                return(0);
                   1452:
                   1453:        while (1) {
                   1454:                if (skipwhite)
                   1455:                        while (isspace((unsigned char)v[*pos]))
                   1456:                                (*pos)++;
                   1457:
                   1458:                if ( ! roff_getop(v, pos, &operator))
                   1459:                        break;
                   1460:
                   1461:                if (skipwhite)
                   1462:                        while (isspace((unsigned char)v[*pos]))
                   1463:                                (*pos)++;
                   1464:
                   1465:                if ( ! roff_evalpar(v, pos, &operand2))
                   1466:                        return(0);
                   1467:
                   1468:                if (skipwhite)
                   1469:                        while (isspace((unsigned char)v[*pos]))
                   1470:                                (*pos)++;
1.206     schwarze 1471:
                   1472:                if (NULL == res)
                   1473:                        continue;
1.204     schwarze 1474:
                   1475:                switch (operator) {
1.207     schwarze 1476:                case '+':
1.204     schwarze 1477:                        *res += operand2;
                   1478:                        break;
1.207     schwarze 1479:                case '-':
1.204     schwarze 1480:                        *res -= operand2;
                   1481:                        break;
1.207     schwarze 1482:                case '*':
1.204     schwarze 1483:                        *res *= operand2;
                   1484:                        break;
1.207     schwarze 1485:                case '/':
1.204     schwarze 1486:                        *res /= operand2;
                   1487:                        break;
1.207     schwarze 1488:                case '%':
1.204     schwarze 1489:                        *res %= operand2;
                   1490:                        break;
1.207     schwarze 1491:                case '<':
1.204     schwarze 1492:                        *res = *res < operand2;
                   1493:                        break;
1.207     schwarze 1494:                case '>':
1.204     schwarze 1495:                        *res = *res > operand2;
                   1496:                        break;
1.207     schwarze 1497:                case 'l':
1.204     schwarze 1498:                        *res = *res <= operand2;
                   1499:                        break;
1.207     schwarze 1500:                case 'g':
1.204     schwarze 1501:                        *res = *res >= operand2;
                   1502:                        break;
1.207     schwarze 1503:                case '=':
1.204     schwarze 1504:                        *res = *res == operand2;
                   1505:                        break;
1.207     schwarze 1506:                case '!':
1.204     schwarze 1507:                        *res = *res != operand2;
                   1508:                        break;
1.207     schwarze 1509:                case '&':
1.204     schwarze 1510:                        *res = *res && operand2;
                   1511:                        break;
1.207     schwarze 1512:                case ':':
1.204     schwarze 1513:                        *res = *res || operand2;
                   1514:                        break;
1.207     schwarze 1515:                case 'i':
1.204     schwarze 1516:                        if (operand2 < *res)
                   1517:                                *res = operand2;
                   1518:                        break;
1.207     schwarze 1519:                case 'a':
1.204     schwarze 1520:                        if (operand2 > *res)
                   1521:                                *res = operand2;
                   1522:                        break;
                   1523:                default:
                   1524:                        abort();
                   1525:                }
                   1526:        }
                   1527:        return(1);
                   1528: }
                   1529:
1.180     schwarze 1530: void
1.187     schwarze 1531: roff_setreg(struct roff *r, const char *name, int val, char sign)
1.147     kristaps 1532: {
1.180     schwarze 1533:        struct roffreg  *reg;
                   1534:
                   1535:        /* Search for an existing register with the same name. */
                   1536:        reg = r->regtab;
                   1537:
                   1538:        while (reg && strcmp(name, reg->key.p))
                   1539:                reg = reg->next;
1.147     kristaps 1540:
1.180     schwarze 1541:        if (NULL == reg) {
                   1542:                /* Create a new register. */
                   1543:                reg = mandoc_malloc(sizeof(struct roffreg));
                   1544:                reg->key.p = mandoc_strdup(name);
                   1545:                reg->key.sz = strlen(name);
1.187     schwarze 1546:                reg->val = 0;
1.180     schwarze 1547:                reg->next = r->regtab;
                   1548:                r->regtab = reg;
                   1549:        }
                   1550:
1.187     schwarze 1551:        if ('+' == sign)
                   1552:                reg->val += val;
                   1553:        else if ('-' == sign)
                   1554:                reg->val -= val;
                   1555:        else
                   1556:                reg->val = val;
1.147     kristaps 1557: }
                   1558:
1.192     schwarze 1559: /*
                   1560:  * Handle some predefined read-only number registers.
                   1561:  * For now, return -1 if the requested register is not predefined;
                   1562:  * in case a predefined read-only register having the value -1
                   1563:  * were to turn up, another special value would have to be chosen.
                   1564:  */
                   1565: static int
                   1566: roff_getregro(const char *name)
                   1567: {
                   1568:
                   1569:        switch (*name) {
1.207     schwarze 1570:        case 'A':  /* ASCII approximation mode is always off. */
1.192     schwarze 1571:                return(0);
1.207     schwarze 1572:        case 'g':  /* Groff compatibility mode is always on. */
1.192     schwarze 1573:                return(1);
1.207     schwarze 1574:        case 'H':  /* Fixed horizontal resolution. */
1.192     schwarze 1575:                return (24);
1.207     schwarze 1576:        case 'j':  /* Always adjust left margin only. */
1.192     schwarze 1577:                return(0);
1.207     schwarze 1578:        case 'T':  /* Some output device is always defined. */
1.192     schwarze 1579:                return(1);
1.207     schwarze 1580:        case 'V':  /* Fixed vertical resolution. */
1.192     schwarze 1581:                return (40);
                   1582:        default:
                   1583:                return (-1);
                   1584:        }
                   1585: }
                   1586:
1.181     schwarze 1587: int
1.180     schwarze 1588: roff_getreg(const struct roff *r, const char *name)
1.147     kristaps 1589: {
1.180     schwarze 1590:        struct roffreg  *reg;
1.192     schwarze 1591:        int              val;
                   1592:
                   1593:        if ('.' == name[0] && '\0' != name[1] && '\0' == name[2]) {
                   1594:                val = roff_getregro(name + 1);
                   1595:                if (-1 != val)
                   1596:                        return (val);
                   1597:        }
1.180     schwarze 1598:
                   1599:        for (reg = r->regtab; reg; reg = reg->next)
                   1600:                if (0 == strcmp(name, reg->key.p))
1.181     schwarze 1601:                        return(reg->val);
                   1602:
                   1603:        return(0);
                   1604: }
                   1605:
                   1606: static int
                   1607: roff_getregn(const struct roff *r, const char *name, size_t len)
                   1608: {
                   1609:        struct roffreg  *reg;
1.192     schwarze 1610:        int              val;
                   1611:
                   1612:        if ('.' == name[0] && 2 == len) {
                   1613:                val = roff_getregro(name + 1);
                   1614:                if (-1 != val)
                   1615:                        return (val);
                   1616:        }
1.181     schwarze 1617:
                   1618:        for (reg = r->regtab; reg; reg = reg->next)
                   1619:                if (len == reg->key.sz &&
                   1620:                    0 == strncmp(name, reg->key.p, len))
                   1621:                        return(reg->val);
1.147     kristaps 1622:
1.180     schwarze 1623:        return(0);
1.147     kristaps 1624: }
                   1625:
1.180     schwarze 1626: static void
                   1627: roff_freereg(struct roffreg *reg)
1.147     kristaps 1628: {
1.180     schwarze 1629:        struct roffreg  *old_reg;
1.147     kristaps 1630:
1.180     schwarze 1631:        while (NULL != reg) {
                   1632:                free(reg->key.p);
                   1633:                old_reg = reg;
                   1634:                reg = reg->next;
                   1635:                free(old_reg);
                   1636:        }
1.147     kristaps 1637: }
1.92      schwarze 1638:
                   1639: static enum rofferr
1.89      kristaps 1640: roff_nr(ROFF_ARGS)
1.83      schwarze 1641: {
1.212     schwarze 1642:        char            *key, *val;
                   1643:        size_t           keysz;
1.138     kristaps 1644:        int              iv;
1.187     schwarze 1645:        char             sign;
1.89      kristaps 1646:
1.212     schwarze 1647:        key = val = *bufp + pos;
                   1648:        if ('\0' == *key)
                   1649:                return(ROFF_IGN);
                   1650:
                   1651:        keysz = roff_getname(r, &val, ln, pos);
                   1652:        if ('\\' == key[keysz])
                   1653:                return(ROFF_IGN);
                   1654:        key[keysz] = '\0';
1.89      kristaps 1655:
1.187     schwarze 1656:        sign = *val;
                   1657:        if ('+' == sign || '-' == sign)
                   1658:                val++;
                   1659:
1.204     schwarze 1660:        if (roff_evalnum(val, NULL, &iv, 0))
                   1661:                roff_setreg(r, key, iv, sign);
1.109     kristaps 1662:
1.203     schwarze 1663:        return(ROFF_IGN);
                   1664: }
                   1665:
                   1666: static enum rofferr
                   1667: roff_rr(ROFF_ARGS)
                   1668: {
                   1669:        struct roffreg  *reg, **prev;
1.212     schwarze 1670:        char            *name, *cp;
                   1671:        size_t           namesz;
1.203     schwarze 1672:
1.212     schwarze 1673:        name = cp = *bufp + pos;
                   1674:        if ('\0' == *name)
                   1675:                return(ROFF_IGN);
                   1676:        namesz = roff_getname(r, &cp, ln, pos);
                   1677:        name[namesz] = '\0';
1.203     schwarze 1678:
                   1679:        prev = &r->regtab;
                   1680:        while (1) {
                   1681:                reg = *prev;
                   1682:                if (NULL == reg || !strcmp(name, reg->key.p))
                   1683:                        break;
                   1684:                prev = &reg->next;
                   1685:        }
                   1686:        if (NULL != reg) {
                   1687:                *prev = reg->next;
                   1688:                free(reg->key.p);
                   1689:                free(reg);
                   1690:        }
1.122     schwarze 1691:        return(ROFF_IGN);
                   1692: }
                   1693:
                   1694: static enum rofferr
                   1695: roff_rm(ROFF_ARGS)
                   1696: {
                   1697:        const char       *name;
                   1698:        char             *cp;
1.212     schwarze 1699:        size_t            namesz;
1.122     schwarze 1700:
                   1701:        cp = *bufp + pos;
                   1702:        while ('\0' != *cp) {
1.212     schwarze 1703:                name = cp;
                   1704:                namesz = roff_getname(r, &cp, ln, (int)(cp - *bufp));
                   1705:                roff_setstrn(&r->strtab, name, namesz, NULL, 0, 0);
                   1706:                if ('\\' == name[namesz])
                   1707:                        break;
1.122     schwarze 1708:        }
1.178     schwarze 1709:        return(ROFF_IGN);
                   1710: }
                   1711:
                   1712: static enum rofferr
                   1713: roff_it(ROFF_ARGS)
                   1714: {
                   1715:        char            *cp;
                   1716:        size_t           len;
                   1717:        int              iv;
                   1718:
                   1719:        /* Parse the number of lines. */
                   1720:        cp = *bufp + pos;
                   1721:        len = strcspn(cp, " \t");
                   1722:        cp[len] = '\0';
                   1723:        if ((iv = mandoc_strntoi(cp, len, 10)) <= 0) {
                   1724:                mandoc_msg(MANDOCERR_NUMERIC, r->parse,
1.207     schwarze 1725:                    ln, ppos, *bufp + 1);
1.178     schwarze 1726:                return(ROFF_IGN);
                   1727:        }
                   1728:        cp += len + 1;
                   1729:
                   1730:        /* Arm the input line trap. */
                   1731:        roffit_lines = iv;
                   1732:        roffit_macro = mandoc_strdup(cp);
1.109     kristaps 1733:        return(ROFF_IGN);
1.175     schwarze 1734: }
                   1735:
                   1736: static enum rofferr
                   1737: roff_Dd(ROFF_ARGS)
                   1738: {
                   1739:        const char *const       *cp;
                   1740:
1.199     schwarze 1741:        if (0 == ((MPARSE_MDOC | MPARSE_QUICK) & r->options))
1.175     schwarze 1742:                for (cp = __mdoc_reserved; *cp; cp++)
                   1743:                        roff_setstr(r, *cp, NULL, 0);
                   1744:
                   1745:        return(ROFF_CONT);
                   1746: }
                   1747:
                   1748: static enum rofferr
                   1749: roff_TH(ROFF_ARGS)
                   1750: {
                   1751:        const char *const       *cp;
                   1752:
1.199     schwarze 1753:        if (0 == (MPARSE_QUICK & r->options))
1.175     schwarze 1754:                for (cp = __man_reserved; *cp; cp++)
                   1755:                        roff_setstr(r, *cp, NULL, 0);
                   1756:
                   1757:        return(ROFF_CONT);
1.109     kristaps 1758: }
                   1759:
                   1760: static enum rofferr
                   1761: roff_TE(ROFF_ARGS)
                   1762: {
                   1763:
                   1764:        if (NULL == r->tbl)
1.128     kristaps 1765:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.115     kristaps 1766:        else
1.151     kristaps 1767:                tbl_end(&r->tbl);
1.109     kristaps 1768:
1.112     kristaps 1769:        return(ROFF_IGN);
                   1770: }
                   1771:
                   1772: static enum rofferr
                   1773: roff_T_(ROFF_ARGS)
                   1774: {
                   1775:
                   1776:        if (NULL == r->tbl)
1.128     kristaps 1777:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.112     kristaps 1778:        else
1.116     kristaps 1779:                tbl_restart(ppos, ln, r->tbl);
1.112     kristaps 1780:
1.109     kristaps 1781:        return(ROFF_IGN);
                   1782: }
                   1783:
1.156     kristaps 1784: #if 0
                   1785: static int
1.151     kristaps 1786: roff_closeeqn(struct roff *r)
                   1787: {
                   1788:
                   1789:        return(r->eqn && ROFF_EQN == eqn_end(&r->eqn) ? 1 : 0);
                   1790: }
1.156     kristaps 1791: #endif
1.151     kristaps 1792:
1.156     kristaps 1793: static void
1.207     schwarze 1794: roff_openeqn(struct roff *r, const char *name, int line,
1.151     kristaps 1795:                int offs, const char *buf)
1.125     kristaps 1796: {
1.151     kristaps 1797:        struct eqn_node *e;
                   1798:        int              poff;
1.125     kristaps 1799:
                   1800:        assert(NULL == r->eqn);
1.151     kristaps 1801:        e = eqn_alloc(name, offs, line, r->parse);
1.125     kristaps 1802:
                   1803:        if (r->last_eqn)
                   1804:                r->last_eqn->next = e;
                   1805:        else
                   1806:                r->first_eqn = r->last_eqn = e;
                   1807:
                   1808:        r->eqn = r->last_eqn = e;
1.151     kristaps 1809:
                   1810:        if (buf) {
                   1811:                poff = 0;
                   1812:                eqn_read(&r->eqn, line, buf, offs, &poff);
                   1813:        }
                   1814: }
                   1815:
                   1816: static enum rofferr
                   1817: roff_EQ(ROFF_ARGS)
                   1818: {
                   1819:
                   1820:        roff_openeqn(r, *bufp + pos, ln, ppos, NULL);
1.125     kristaps 1821:        return(ROFF_IGN);
                   1822: }
                   1823:
                   1824: static enum rofferr
                   1825: roff_EN(ROFF_ARGS)
                   1826: {
                   1827:
1.128     kristaps 1828:        mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.125     kristaps 1829:        return(ROFF_IGN);
                   1830: }
                   1831:
                   1832: static enum rofferr
1.109     kristaps 1833: roff_TS(ROFF_ARGS)
                   1834: {
1.176     schwarze 1835:        struct tbl_node *tbl;
1.89      kristaps 1836:
1.115     kristaps 1837:        if (r->tbl) {
1.128     kristaps 1838:                mandoc_msg(MANDOCERR_SCOPEBROKEN, r->parse, ln, ppos, NULL);
1.151     kristaps 1839:                tbl_end(&r->tbl);
1.115     kristaps 1840:        }
1.83      schwarze 1841:
1.176     schwarze 1842:        tbl = tbl_alloc(ppos, ln, r->parse);
1.113     kristaps 1843:
                   1844:        if (r->last_tbl)
1.176     schwarze 1845:                r->last_tbl->next = tbl;
1.113     kristaps 1846:        else
1.176     schwarze 1847:                r->first_tbl = r->last_tbl = tbl;
1.113     kristaps 1848:
1.176     schwarze 1849:        r->tbl = r->last_tbl = tbl;
1.83      schwarze 1850:        return(ROFF_IGN);
1.92      schwarze 1851: }
                   1852:
1.105     kristaps 1853: static enum rofferr
1.174     kristaps 1854: roff_cc(ROFF_ARGS)
                   1855: {
                   1856:        const char      *p;
                   1857:
                   1858:        p = *bufp + pos;
                   1859:
                   1860:        if ('\0' == *p || '.' == (r->control = *p++))
                   1861:                r->control = 0;
                   1862:
                   1863:        if ('\0' != *p)
                   1864:                mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
                   1865:
                   1866:        return(ROFF_IGN);
                   1867: }
                   1868:
                   1869: static enum rofferr
1.164     kristaps 1870: roff_tr(ROFF_ARGS)
                   1871: {
                   1872:        const char      *p, *first, *second;
                   1873:        size_t           fsz, ssz;
                   1874:        enum mandoc_esc  esc;
                   1875:
                   1876:        p = *bufp + pos;
                   1877:
                   1878:        if ('\0' == *p) {
                   1879:                mandoc_msg(MANDOCERR_ARGCOUNT, r->parse, ln, ppos, NULL);
                   1880:                return(ROFF_IGN);
                   1881:        }
                   1882:
                   1883:        while ('\0' != *p) {
                   1884:                fsz = ssz = 1;
                   1885:
                   1886:                first = p++;
                   1887:                if ('\\' == *first) {
                   1888:                        esc = mandoc_escape(&p, NULL, NULL);
                   1889:                        if (ESCAPE_ERROR == esc) {
1.207     schwarze 1890:                                mandoc_msg(MANDOCERR_BADESCAPE,
                   1891:                                    r->parse, ln,
                   1892:                                    (int)(p - *bufp), NULL);
1.164     kristaps 1893:                                return(ROFF_IGN);
                   1894:                        }
                   1895:                        fsz = (size_t)(p - first);
                   1896:                }
                   1897:
                   1898:                second = p++;
                   1899:                if ('\\' == *second) {
                   1900:                        esc = mandoc_escape(&p, NULL, NULL);
                   1901:                        if (ESCAPE_ERROR == esc) {
1.207     schwarze 1902:                                mandoc_msg(MANDOCERR_BADESCAPE,
                   1903:                                    r->parse, ln,
                   1904:                                    (int)(p - *bufp), NULL);
1.164     kristaps 1905:                                return(ROFF_IGN);
                   1906:                        }
                   1907:                        ssz = (size_t)(p - second);
1.165     kristaps 1908:                } else if ('\0' == *second) {
1.207     schwarze 1909:                        mandoc_msg(MANDOCERR_ARGCOUNT, r->parse,
                   1910:                            ln, (int)(p - *bufp), NULL);
1.164     kristaps 1911:                        second = " ";
1.165     kristaps 1912:                        p--;
1.164     kristaps 1913:                }
                   1914:
1.167     kristaps 1915:                if (fsz > 1) {
1.207     schwarze 1916:                        roff_setstrn(&r->xmbtab, first, fsz,
                   1917:                            second, ssz, 0);
1.167     kristaps 1918:                        continue;
                   1919:                }
                   1920:
                   1921:                if (NULL == r->xtab)
1.207     schwarze 1922:                        r->xtab = mandoc_calloc(128,
                   1923:                            sizeof(struct roffstr));
1.167     kristaps 1924:
                   1925:                free(r->xtab[(int)*first].p);
                   1926:                r->xtab[(int)*first].p = mandoc_strndup(second, ssz);
                   1927:                r->xtab[(int)*first].sz = ssz;
1.164     kristaps 1928:        }
                   1929:
                   1930:        return(ROFF_IGN);
                   1931: }
                   1932:
                   1933: static enum rofferr
1.105     kristaps 1934: roff_so(ROFF_ARGS)
                   1935: {
                   1936:        char *name;
                   1937:
1.210     schwarze 1938:        name = *bufp + pos;
                   1939:        mandoc_vmsg(MANDOCERR_SO, r->parse, ln, ppos, ".so %s", name);
1.105     kristaps 1940:
                   1941:        /*
                   1942:         * Handle `so'.  Be EXTREMELY careful, as we shouldn't be
                   1943:         * opening anything that's not in our cwd or anything beneath
                   1944:         * it.  Thus, explicitly disallow traversing up the file-system
                   1945:         * or using absolute paths.
                   1946:         */
                   1947:
                   1948:        if ('/' == *name || strstr(name, "../") || strstr(name, "/..")) {
1.210     schwarze 1949:                mandoc_vmsg(MANDOCERR_SO_PATH, r->parse, ln, ppos,
                   1950:                    ".so %s", name);
1.105     kristaps 1951:                return(ROFF_ERR);
                   1952:        }
                   1953:
                   1954:        *offs = pos;
                   1955:        return(ROFF_SO);
                   1956: }
1.92      schwarze 1957:
1.106     kristaps 1958: static enum rofferr
                   1959: roff_userdef(ROFF_ARGS)
1.99      kristaps 1960: {
1.106     kristaps 1961:        const char       *arg[9];
                   1962:        char             *cp, *n1, *n2;
1.119     schwarze 1963:        int               i;
1.106     kristaps 1964:
                   1965:        /*
                   1966:         * Collect pointers to macro argument strings
1.188     schwarze 1967:         * and NUL-terminate them.
1.106     kristaps 1968:         */
                   1969:        cp = *bufp + pos;
1.119     schwarze 1970:        for (i = 0; i < 9; i++)
1.120     schwarze 1971:                arg[i] = '\0' == *cp ? "" :
1.136     kristaps 1972:                    mandoc_getarg(r->parse, &cp, ln, &pos);
1.99      kristaps 1973:
1.106     kristaps 1974:        /*
                   1975:         * Expand macro arguments.
1.99      kristaps 1976:         */
1.106     kristaps 1977:        *szp = 0;
                   1978:        n1 = cp = mandoc_strdup(r->current_string);
                   1979:        while (NULL != (cp = strstr(cp, "\\$"))) {
                   1980:                i = cp[2] - '1';
                   1981:                if (0 > i || 8 < i) {
                   1982:                        /* Not an argument invocation. */
                   1983:                        cp += 2;
                   1984:                        continue;
                   1985:                }
1.209     schwarze 1986:                *cp = '\0';
                   1987:                *szp = mandoc_asprintf(&n2, "%s%s%s",
                   1988:                    n1, arg[i], cp + 3) + 1;
1.106     kristaps 1989:                cp = n2 + (cp - n1);
                   1990:                free(n1);
                   1991:                n1 = n2;
1.99      kristaps 1992:        }
                   1993:
1.106     kristaps 1994:        /*
                   1995:         * Replace the macro invocation
                   1996:         * by the expanded macro.
                   1997:         */
                   1998:        free(*bufp);
                   1999:        *bufp = n1;
                   2000:        if (0 == *szp)
                   2001:                *szp = strlen(*bufp) + 1;
                   2002:
                   2003:        return(*szp > 1 && '\n' == (*bufp)[(int)*szp - 2] ?
                   2004:           ROFF_REPARSE : ROFF_APPEND);
1.99      kristaps 2005: }
1.121     schwarze 2006:
1.212     schwarze 2007: static size_t
1.121     schwarze 2008: roff_getname(struct roff *r, char **cpp, int ln, int pos)
                   2009: {
                   2010:        char     *name, *cp;
1.212     schwarze 2011:        size_t    namesz;
1.121     schwarze 2012:
                   2013:        name = *cpp;
                   2014:        if ('\0' == *name)
1.212     schwarze 2015:                return(0);
1.121     schwarze 2016:
1.212     schwarze 2017:        /* Read until end of name and terminate it with NUL. */
                   2018:        for (cp = name; 1; cp++) {
                   2019:                if ('\0' == *cp || ' ' == *cp) {
                   2020:                        namesz = cp - name;
                   2021:                        break;
                   2022:                }
1.121     schwarze 2023:                if ('\\' != *cp)
                   2024:                        continue;
1.215     schwarze 2025:                namesz = cp - name;
                   2026:                if ('{' == cp[1] || '}' == cp[1])
                   2027:                        break;
1.121     schwarze 2028:                cp++;
                   2029:                if ('\\' == *cp)
                   2030:                        continue;
1.128     kristaps 2031:                mandoc_msg(MANDOCERR_NAMESC, r->parse, ln, pos, NULL);
1.212     schwarze 2032:                mandoc_escape((const char **)&cp, NULL, NULL);
                   2033:                break;
1.121     schwarze 2034:        }
                   2035:
                   2036:        /* Read past spaces. */
                   2037:        while (' ' == *cp)
                   2038:                cp++;
                   2039:
                   2040:        *cpp = cp;
1.212     schwarze 2041:        return(namesz);
1.121     schwarze 2042: }
                   2043:
1.106     kristaps 2044: /*
                   2045:  * Store *string into the user-defined string called *name.
                   2046:  * To clear an existing entry, call with (*r, *name, NULL, 0).
1.193     schwarze 2047:  * append == 0: replace mode
                   2048:  * append == 1: single-line append mode
                   2049:  * append == 2: multiline append mode, append '\n' after each call
1.106     kristaps 2050:  */
1.94      kristaps 2051: static void
1.106     kristaps 2052: roff_setstr(struct roff *r, const char *name, const char *string,
1.193     schwarze 2053:        int append)
1.92      schwarze 2054: {
1.164     kristaps 2055:
                   2056:        roff_setstrn(&r->strtab, name, strlen(name), string,
1.207     schwarze 2057:            string ? strlen(string) : 0, append);
1.164     kristaps 2058: }
                   2059:
                   2060: static void
1.166     kristaps 2061: roff_setstrn(struct roffkv **r, const char *name, size_t namesz,
1.193     schwarze 2062:                const char *string, size_t stringsz, int append)
1.164     kristaps 2063: {
1.166     kristaps 2064:        struct roffkv   *n;
1.164     kristaps 2065:        char            *c;
                   2066:        int              i;
                   2067:        size_t           oldch, newch;
1.92      schwarze 2068:
1.106     kristaps 2069:        /* Search for an existing string with the same name. */
1.164     kristaps 2070:        n = *r;
                   2071:
1.211     schwarze 2072:        while (n && (namesz != n->key.sz ||
                   2073:                        strncmp(n->key.p, name, namesz)))
1.92      schwarze 2074:                n = n->next;
1.94      kristaps 2075:
                   2076:        if (NULL == n) {
1.106     kristaps 2077:                /* Create a new string table entry. */
1.166     kristaps 2078:                n = mandoc_malloc(sizeof(struct roffkv));
                   2079:                n->key.p = mandoc_strndup(name, namesz);
                   2080:                n->key.sz = namesz;
                   2081:                n->val.p = NULL;
                   2082:                n->val.sz = 0;
1.164     kristaps 2083:                n->next = *r;
                   2084:                *r = n;
1.193     schwarze 2085:        } else if (0 == append) {
1.166     kristaps 2086:                free(n->val.p);
                   2087:                n->val.p = NULL;
                   2088:                n->val.sz = 0;
1.106     kristaps 2089:        }
                   2090:
                   2091:        if (NULL == string)
                   2092:                return;
                   2093:
                   2094:        /*
                   2095:         * One additional byte for the '\n' in multiline mode,
                   2096:         * and one for the terminating '\0'.
                   2097:         */
1.193     schwarze 2098:        newch = stringsz + (1 < append ? 2u : 1u);
1.164     kristaps 2099:
1.166     kristaps 2100:        if (NULL == n->val.p) {
                   2101:                n->val.p = mandoc_malloc(newch);
                   2102:                *n->val.p = '\0';
1.106     kristaps 2103:                oldch = 0;
                   2104:        } else {
1.166     kristaps 2105:                oldch = n->val.sz;
                   2106:                n->val.p = mandoc_realloc(n->val.p, oldch + newch);
1.106     kristaps 2107:        }
                   2108:
                   2109:        /* Skip existing content in the destination buffer. */
1.166     kristaps 2110:        c = n->val.p + (int)oldch;
1.106     kristaps 2111:
                   2112:        /* Append new content to the destination buffer. */
1.164     kristaps 2113:        i = 0;
                   2114:        while (i < (int)stringsz) {
1.106     kristaps 2115:                /*
                   2116:                 * Rudimentary roff copy mode:
                   2117:                 * Handle escaped backslashes.
                   2118:                 */
1.164     kristaps 2119:                if ('\\' == string[i] && '\\' == string[i + 1])
                   2120:                        i++;
                   2121:                *c++ = string[i++];
1.106     kristaps 2122:        }
1.94      kristaps 2123:
1.106     kristaps 2124:        /* Append terminating bytes. */
1.193     schwarze 2125:        if (1 < append)
1.106     kristaps 2126:                *c++ = '\n';
1.163     kristaps 2127:
1.106     kristaps 2128:        *c = '\0';
1.166     kristaps 2129:        n->val.sz = (int)(c - n->val.p);
1.92      schwarze 2130: }
                   2131:
1.94      kristaps 2132: static const char *
                   2133: roff_getstrn(const struct roff *r, const char *name, size_t len)
1.92      schwarze 2134: {
1.166     kristaps 2135:        const struct roffkv *n;
1.191     schwarze 2136:        int i;
1.92      schwarze 2137:
1.164     kristaps 2138:        for (n = r->strtab; n; n = n->next)
1.207     schwarze 2139:                if (0 == strncmp(name, n->key.p, len) &&
                   2140:                    '\0' == n->key.p[(int)len])
1.166     kristaps 2141:                        return(n->val.p);
1.191     schwarze 2142:
                   2143:        for (i = 0; i < PREDEFS_MAX; i++)
                   2144:                if (0 == strncmp(name, predefs[i].name, len) &&
                   2145:                                '\0' == predefs[i].name[(int)len])
                   2146:                        return(predefs[i].str);
1.94      kristaps 2147:
1.157     kristaps 2148:        return(NULL);
1.92      schwarze 2149: }
                   2150:
1.94      kristaps 2151: static void
1.167     kristaps 2152: roff_freestr(struct roffkv *r)
1.92      schwarze 2153: {
1.166     kristaps 2154:        struct roffkv    *n, *nn;
1.92      schwarze 2155:
1.167     kristaps 2156:        for (n = r; n; n = nn) {
1.166     kristaps 2157:                free(n->key.p);
                   2158:                free(n->val.p);
1.92      schwarze 2159:                nn = n->next;
                   2160:                free(n);
                   2161:        }
1.114     kristaps 2162: }
                   2163:
                   2164: const struct tbl_span *
                   2165: roff_span(const struct roff *r)
                   2166: {
1.207     schwarze 2167:
1.114     kristaps 2168:        return(r->tbl ? tbl_span(r->tbl) : NULL);
1.125     kristaps 2169: }
                   2170:
                   2171: const struct eqn *
                   2172: roff_eqn(const struct roff *r)
                   2173: {
1.207     schwarze 2174:
1.125     kristaps 2175:        return(r->last_eqn ? &r->last_eqn->eqn : NULL);
1.164     kristaps 2176: }
                   2177:
                   2178: /*
                   2179:  * Duplicate an input string, making the appropriate character
                   2180:  * conversations (as stipulated by `tr') along the way.
                   2181:  * Returns a heap-allocated string with all the replacements made.
                   2182:  */
                   2183: char *
                   2184: roff_strdup(const struct roff *r, const char *p)
                   2185: {
1.166     kristaps 2186:        const struct roffkv *cp;
1.164     kristaps 2187:        char            *res;
                   2188:        const char      *pp;
                   2189:        size_t           ssz, sz;
                   2190:        enum mandoc_esc  esc;
                   2191:
1.167     kristaps 2192:        if (NULL == r->xmbtab && NULL == r->xtab)
1.164     kristaps 2193:                return(mandoc_strdup(p));
                   2194:        else if ('\0' == *p)
                   2195:                return(mandoc_strdup(""));
                   2196:
                   2197:        /*
                   2198:         * Step through each character looking for term matches
                   2199:         * (remember that a `tr' can be invoked with an escape, which is
                   2200:         * a glyph but the escape is multi-character).
                   2201:         * We only do this if the character hash has been initialised
                   2202:         * and the string is >0 length.
                   2203:         */
                   2204:
                   2205:        res = NULL;
                   2206:        ssz = 0;
                   2207:
                   2208:        while ('\0' != *p) {
1.167     kristaps 2209:                if ('\\' != *p && r->xtab && r->xtab[(int)*p].p) {
                   2210:                        sz = r->xtab[(int)*p].sz;
                   2211:                        res = mandoc_realloc(res, ssz + sz + 1);
                   2212:                        memcpy(res + ssz, r->xtab[(int)*p].p, sz);
                   2213:                        ssz += sz;
                   2214:                        p++;
                   2215:                        continue;
                   2216:                } else if ('\\' != *p) {
                   2217:                        res = mandoc_realloc(res, ssz + 2);
                   2218:                        res[ssz++] = *p++;
                   2219:                        continue;
                   2220:                }
                   2221:
1.164     kristaps 2222:                /* Search for term matches. */
1.167     kristaps 2223:                for (cp = r->xmbtab; cp; cp = cp->next)
1.166     kristaps 2224:                        if (0 == strncmp(p, cp->key.p, cp->key.sz))
1.164     kristaps 2225:                                break;
                   2226:
                   2227:                if (NULL != cp) {
                   2228:                        /*
                   2229:                         * A match has been found.
                   2230:                         * Append the match to the array and move
                   2231:                         * forward by its keysize.
                   2232:                         */
1.207     schwarze 2233:                        res = mandoc_realloc(res,
                   2234:                            ssz + cp->val.sz + 1);
1.166     kristaps 2235:                        memcpy(res + ssz, cp->val.p, cp->val.sz);
                   2236:                        ssz += cp->val.sz;
                   2237:                        p += (int)cp->key.sz;
1.164     kristaps 2238:                        continue;
                   2239:                }
                   2240:
1.167     kristaps 2241:                /*
                   2242:                 * Handle escapes carefully: we need to copy
                   2243:                 * over just the escape itself, or else we might
                   2244:                 * do replacements within the escape itself.
                   2245:                 * Make sure to pass along the bogus string.
                   2246:                 */
                   2247:                pp = p++;
                   2248:                esc = mandoc_escape(&p, NULL, NULL);
                   2249:                if (ESCAPE_ERROR == esc) {
                   2250:                        sz = strlen(pp);
1.164     kristaps 2251:                        res = mandoc_realloc(res, ssz + sz + 1);
                   2252:                        memcpy(res + ssz, pp, sz);
1.167     kristaps 2253:                        break;
1.164     kristaps 2254:                }
1.207     schwarze 2255:                /*
                   2256:                 * We bail out on bad escapes.
1.167     kristaps 2257:                 * No need to warn: we already did so when
                   2258:                 * roff_res() was called.
                   2259:                 */
                   2260:                sz = (int)(p - pp);
                   2261:                res = mandoc_realloc(res, ssz + sz + 1);
                   2262:                memcpy(res + ssz, pp, sz);
                   2263:                ssz += sz;
1.164     kristaps 2264:        }
                   2265:
                   2266:        res[(int)ssz] = '\0';
                   2267:        return(res);
1.174     kristaps 2268: }
                   2269:
                   2270: /*
1.207     schwarze 2271:  * Find out whether a line is a macro line or not.
1.174     kristaps 2272:  * If it is, adjust the current position and return one; if it isn't,
                   2273:  * return zero and don't change the current position.
                   2274:  * If the control character has been set with `.cc', then let that grain
                   2275:  * precedence.
                   2276:  * This is slighly contrary to groff, where using the non-breaking
                   2277:  * control character when `cc' has been invoked will cause the
                   2278:  * non-breaking macro contents to be printed verbatim.
                   2279:  */
                   2280: int
                   2281: roff_getcontrol(const struct roff *r, const char *cp, int *ppos)
                   2282: {
                   2283:        int             pos;
                   2284:
                   2285:        pos = *ppos;
                   2286:
                   2287:        if (0 != r->control && cp[pos] == r->control)
                   2288:                pos++;
                   2289:        else if (0 != r->control)
                   2290:                return(0);
                   2291:        else if ('\\' == cp[pos] && '.' == cp[pos + 1])
                   2292:                pos += 2;
                   2293:        else if ('.' == cp[pos] || '\'' == cp[pos])
                   2294:                pos++;
                   2295:        else
                   2296:                return(0);
                   2297:
                   2298:        while (' ' == cp[pos] || '\t' == cp[pos])
                   2299:                pos++;
                   2300:
                   2301:        *ppos = pos;
                   2302:        return(1);
1.74      kristaps 2303: }

CVSweb