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

Annotation of mandoc/roff.c, Revision 1.235

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

CVSweb