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

Annotation of mandoc/roff.c, Revision 1.213

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

CVSweb