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

Annotation of mandoc/roff.c, Revision 1.151

1.151   ! kristaps    1: /*     $Id: roff.c,v 1.150 2011/07/23 18:41:18 kristaps Exp $ */
1.1       kristaps    2: /*
1.119     schwarze    3:  * Copyright (c) 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.66      kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.106     kristaps   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.66      kristaps   11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.106     kristaps   12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.66      kristaps   13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   17:  */
1.66      kristaps   18: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
                     20: #endif
1.30      kristaps   21:
1.67      kristaps   22: #include <assert.h>
1.85      kristaps   23: #include <ctype.h>
1.1       kristaps   24: #include <stdlib.h>
1.67      kristaps   25: #include <string.h>
1.1       kristaps   26:
1.67      kristaps   27: #include "mandoc.h"
1.109     kristaps   28: #include "libroff.h"
1.94      kristaps   29: #include "libmandoc.h"
1.33      kristaps   30:
1.141     kristaps   31: /* Maximum number of nested if-else conditionals. */
1.82      kristaps   32: #define        RSTACK_MAX      128
                     33:
1.67      kristaps   34: enum   rofft {
1.103     kristaps   35:        ROFF_ad,
1.80      kristaps   36:        ROFF_am,
                     37:        ROFF_ami,
                     38:        ROFF_am1,
                     39:        ROFF_de,
                     40:        ROFF_dei,
                     41:        ROFF_de1,
1.83      schwarze   42:        ROFF_ds,
1.82      kristaps   43:        ROFF_el,
1.103     kristaps   44:        ROFF_hy,
1.82      kristaps   45:        ROFF_ie,
1.75      kristaps   46:        ROFF_if,
1.76      kristaps   47:        ROFF_ig,
1.123     schwarze   48:        ROFF_it,
1.103     kristaps   49:        ROFF_ne,
                     50:        ROFF_nh,
1.104     kristaps   51:        ROFF_nr,
1.124     schwarze   52:        ROFF_ns,
                     53:        ROFF_ps,
1.83      schwarze   54:        ROFF_rm,
1.105     kristaps   55:        ROFF_so,
1.124     schwarze   56:        ROFF_ta,
1.83      schwarze   57:        ROFF_tr,
1.109     kristaps   58:        ROFF_TS,
                     59:        ROFF_TE,
1.112     kristaps   60:        ROFF_T_,
1.125     kristaps   61:        ROFF_EQ,
                     62:        ROFF_EN,
1.76      kristaps   63:        ROFF_cblock,
1.141     kristaps   64:        ROFF_ccond,
1.106     kristaps   65:        ROFF_USERDEF,
1.67      kristaps   66:        ROFF_MAX
                     67: };
                     68:
1.82      kristaps   69: enum   roffrule {
                     70:        ROFFRULE_ALLOW,
                     71:        ROFFRULE_DENY
                     72: };
                     73:
1.147     kristaps   74: /*
                     75:  * A single register entity.  If "set" is zero, the value of the
                     76:  * register should be the default one, which is per-register.
                     77:  * Registers are assumed to be unsigned ints for now.
                     78:  */
                     79: struct reg {
                     80:        int               set; /* whether set or not */
                     81:        unsigned int      u; /* unsigned integer */
                     82: };
                     83:
1.94      kristaps   84: struct roffstr {
                     85:        char            *name; /* key of symbol */
                     86:        char            *string; /* current value */
                     87:        struct roffstr  *next; /* next in list */
                     88: };
                     89:
1.67      kristaps   90: struct roff {
1.128     kristaps   91:        struct mparse   *parse; /* parse point */
1.67      kristaps   92:        struct roffnode *last; /* leaf of stack */
1.82      kristaps   93:        enum roffrule    rstack[RSTACK_MAX]; /* stack of !`ie' rules */
                     94:        int              rstackpos; /* position in rstack */
1.147     kristaps   95:        struct reg       regs[REG__MAX];
1.106     kristaps   96:        struct roffstr  *first_string; /* user-defined strings & macros */
                     97:        const char      *current_string; /* value of last called user macro */
1.118     kristaps   98:        struct tbl_node *first_tbl; /* first table parsed */
                     99:        struct tbl_node *last_tbl; /* last table parsed */
                    100:        struct tbl_node *tbl; /* current table being parsed */
1.125     kristaps  101:        struct eqn_node *last_eqn; /* last equation parsed */
                    102:        struct eqn_node *first_eqn; /* first equation parsed */
                    103:        struct eqn_node *eqn; /* current equation being parsed */
1.79      kristaps  104: };
                    105:
1.67      kristaps  106: struct roffnode {
                    107:        enum rofft       tok; /* type of node */
                    108:        struct roffnode *parent; /* up one in stack */
                    109:        int              line; /* parse line */
                    110:        int              col; /* parse col */
1.106     kristaps  111:        char            *name; /* node name, e.g. macro name */
1.79      kristaps  112:        char            *end; /* end-rules: custom token */
                    113:        int              endspan; /* end-rules: next-line or infty */
1.82      kristaps  114:        enum roffrule    rule; /* current evaluation rule */
1.67      kristaps  115: };
                    116:
                    117: #define        ROFF_ARGS        struct roff *r, /* parse ctx */ \
1.72      kristaps  118:                         enum rofft tok, /* tok of macro */ \
1.67      kristaps  119:                         char **bufp, /* input buffer */ \
                    120:                         size_t *szp, /* size of input buffer */ \
                    121:                         int ln, /* parse line */ \
1.75      kristaps  122:                         int ppos, /* original pos in buffer */ \
                    123:                         int pos, /* current pos in buffer */ \
1.74      kristaps  124:                         int *offs /* reset offset of buffer data */
1.67      kristaps  125:
                    126: typedef        enum rofferr (*roffproc)(ROFF_ARGS);
                    127:
                    128: struct roffmac {
                    129:        const char      *name; /* macro name */
1.79      kristaps  130:        roffproc         proc; /* process new macro */
                    131:        roffproc         text; /* process as child text of macro */
                    132:        roffproc         sub; /* process as child of macro */
                    133:        int              flags;
                    134: #define        ROFFMAC_STRUCT  (1 << 0) /* always interpret */
1.85      kristaps  135:        struct roffmac  *next;
1.67      kristaps  136: };
                    137:
1.141     kristaps  138: struct predef {
                    139:        const char      *name; /* predefined input name */
                    140:        const char      *str; /* replacement symbol */
                    141: };
                    142:
                    143: #define        PREDEF(__name, __str) \
                    144:        { (__name), (__str) },
                    145:
1.80      kristaps  146: static enum rofferr     roff_block(ROFF_ARGS);
                    147: static enum rofferr     roff_block_text(ROFF_ARGS);
                    148: static enum rofferr     roff_block_sub(ROFF_ARGS);
                    149: static enum rofferr     roff_cblock(ROFF_ARGS);
                    150: static enum rofferr     roff_ccond(ROFF_ARGS);
1.82      kristaps  151: static enum rofferr     roff_cond(ROFF_ARGS);
                    152: static enum rofferr     roff_cond_text(ROFF_ARGS);
                    153: static enum rofferr     roff_cond_sub(ROFF_ARGS);
1.92      schwarze  154: static enum rofferr     roff_ds(ROFF_ARGS);
1.94      kristaps  155: static enum roffrule    roff_evalcond(const char *, int *);
                    156: static void             roff_freestr(struct roff *);
1.121     schwarze  157: static char            *roff_getname(struct roff *, char **, int, int);
1.94      kristaps  158: static const char      *roff_getstrn(const struct roff *,
                    159:                                const char *, size_t);
1.103     kristaps  160: static enum rofferr     roff_line_ignore(ROFF_ARGS);
1.89      kristaps  161: static enum rofferr     roff_nr(ROFF_ARGS);
1.95      kristaps  162: static int              roff_res(struct roff *,
1.142     kristaps  163:                                char **, size_t *, int, int);
1.122     schwarze  164: static enum rofferr     roff_rm(ROFF_ARGS);
1.94      kristaps  165: static void             roff_setstr(struct roff *,
1.106     kristaps  166:                                const char *, const char *, int);
1.105     kristaps  167: static enum rofferr     roff_so(ROFF_ARGS);
1.109     kristaps  168: static enum rofferr     roff_TE(ROFF_ARGS);
                    169: static enum rofferr     roff_TS(ROFF_ARGS);
1.125     kristaps  170: static enum rofferr     roff_EQ(ROFF_ARGS);
                    171: static enum rofferr     roff_EN(ROFF_ARGS);
1.112     kristaps  172: static enum rofferr     roff_T_(ROFF_ARGS);
1.106     kristaps  173: static enum rofferr     roff_userdef(ROFF_ARGS);
1.67      kristaps  174:
1.85      kristaps  175: /* See roff_hash_find() */
                    176:
                    177: #define        ASCII_HI         126
                    178: #define        ASCII_LO         33
                    179: #define        HASHWIDTH       (ASCII_HI - ASCII_LO + 1)
                    180:
                    181: static struct roffmac  *hash[HASHWIDTH];
                    182:
                    183: static struct roffmac   roffs[ROFF_MAX] = {
1.103     kristaps  184:        { "ad", roff_line_ignore, NULL, NULL, 0, NULL },
1.85      kristaps  185:        { "am", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    186:        { "ami", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    187:        { "am1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    188:        { "de", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    189:        { "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    190:        { "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.92      schwarze  191:        { "ds", roff_ds, NULL, NULL, 0, NULL },
1.85      kristaps  192:        { "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
1.103     kristaps  193:        { "hy", roff_line_ignore, NULL, NULL, 0, NULL },
1.85      kristaps  194:        { "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
                    195:        { "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
                    196:        { "ig", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.123     schwarze  197:        { "it", roff_line_ignore, NULL, NULL, 0, NULL },
1.103     kristaps  198:        { "ne", roff_line_ignore, NULL, NULL, 0, NULL },
                    199:        { "nh", roff_line_ignore, NULL, NULL, 0, NULL },
1.104     kristaps  200:        { "nr", roff_nr, NULL, NULL, 0, NULL },
1.124     schwarze  201:        { "ns", roff_line_ignore, NULL, NULL, 0, NULL },
                    202:        { "ps", roff_line_ignore, NULL, NULL, 0, NULL },
1.122     schwarze  203:        { "rm", roff_rm, NULL, NULL, 0, NULL },
1.105     kristaps  204:        { "so", roff_so, NULL, NULL, 0, NULL },
1.124     schwarze  205:        { "ta", roff_line_ignore, NULL, NULL, 0, NULL },
1.103     kristaps  206:        { "tr", roff_line_ignore, NULL, NULL, 0, NULL },
1.109     kristaps  207:        { "TS", roff_TS, NULL, NULL, 0, NULL },
                    208:        { "TE", roff_TE, NULL, NULL, 0, NULL },
1.112     kristaps  209:        { "T&", roff_T_, NULL, NULL, 0, NULL },
1.125     kristaps  210:        { "EQ", roff_EQ, NULL, NULL, 0, NULL },
                    211:        { "EN", roff_EN, NULL, NULL, 0, NULL },
1.85      kristaps  212:        { ".", roff_cblock, NULL, NULL, 0, NULL },
                    213:        { "\\}", roff_ccond, NULL, NULL, 0, NULL },
1.106     kristaps  214:        { NULL, roff_userdef, NULL, NULL, 0, NULL },
1.67      kristaps  215: };
                    216:
1.141     kristaps  217: /* Array of injected predefined strings. */
                    218: #define        PREDEFS_MAX      38
                    219: static const struct predef predefs[PREDEFS_MAX] = {
                    220: #include "predefs.in"
                    221: };
                    222:
1.67      kristaps  223: static void             roff_free1(struct roff *);
1.106     kristaps  224: static enum rofft       roff_hash_find(const char *, size_t);
1.85      kristaps  225: static void             roff_hash_init(void);
1.76      kristaps  226: static void             roffnode_cleanscope(struct roff *);
1.106     kristaps  227: static void             roffnode_push(struct roff *, enum rofft,
                    228:                                const char *, int, int);
1.67      kristaps  229: static void             roffnode_pop(struct roff *);
1.106     kristaps  230: static enum rofft       roff_parse(struct roff *, const char *, int *);
1.67      kristaps  231:
1.85      kristaps  232: /* See roff_hash_find() */
                    233: #define        ROFF_HASH(p)    (p[0] - ASCII_LO)
                    234:
                    235: static void
                    236: roff_hash_init(void)
                    237: {
                    238:        struct roffmac   *n;
                    239:        int               buc, i;
                    240:
1.106     kristaps  241:        for (i = 0; i < (int)ROFF_USERDEF; i++) {
1.85      kristaps  242:                assert(roffs[i].name[0] >= ASCII_LO);
                    243:                assert(roffs[i].name[0] <= ASCII_HI);
                    244:
                    245:                buc = ROFF_HASH(roffs[i].name);
                    246:
                    247:                if (NULL != (n = hash[buc])) {
                    248:                        for ( ; n->next; n = n->next)
                    249:                                /* Do nothing. */ ;
                    250:                        n->next = &roffs[i];
                    251:                } else
                    252:                        hash[buc] = &roffs[i];
                    253:        }
                    254: }
                    255:
1.67      kristaps  256: /*
                    257:  * Look up a roff token by its name.  Returns ROFF_MAX if no macro by
                    258:  * the nil-terminated string name could be found.
                    259:  */
                    260: static enum rofft
1.106     kristaps  261: roff_hash_find(const char *p, size_t s)
1.67      kristaps  262: {
1.85      kristaps  263:        int              buc;
                    264:        struct roffmac  *n;
1.67      kristaps  265:
1.85      kristaps  266:        /*
                    267:         * libroff has an extremely simple hashtable, for the time
                    268:         * being, which simply keys on the first character, which must
                    269:         * be printable, then walks a chain.  It works well enough until
                    270:         * optimised.
                    271:         */
                    272:
                    273:        if (p[0] < ASCII_LO || p[0] > ASCII_HI)
                    274:                return(ROFF_MAX);
                    275:
                    276:        buc = ROFF_HASH(p);
                    277:
                    278:        if (NULL == (n = hash[buc]))
                    279:                return(ROFF_MAX);
                    280:        for ( ; n; n = n->next)
1.106     kristaps  281:                if (0 == strncmp(n->name, p, s) && '\0' == n->name[(int)s])
1.85      kristaps  282:                        return((enum rofft)(n - roffs));
1.67      kristaps  283:
                    284:        return(ROFF_MAX);
                    285: }
                    286:
                    287:
                    288: /*
                    289:  * Pop the current node off of the stack of roff instructions currently
                    290:  * pending.
                    291:  */
                    292: static void
                    293: roffnode_pop(struct roff *r)
                    294: {
                    295:        struct roffnode *p;
                    296:
1.75      kristaps  297:        assert(r->last);
                    298:        p = r->last;
1.82      kristaps  299:
1.75      kristaps  300:        r->last = r->last->parent;
1.106     kristaps  301:        free(p->name);
                    302:        free(p->end);
1.67      kristaps  303:        free(p);
                    304: }
                    305:
                    306:
                    307: /*
                    308:  * Push a roff node onto the instruction stack.  This must later be
                    309:  * removed with roffnode_pop().
                    310:  */
1.98      schwarze  311: static void
1.106     kristaps  312: roffnode_push(struct roff *r, enum rofft tok, const char *name,
                    313:                int line, int col)
1.67      kristaps  314: {
                    315:        struct roffnode *p;
                    316:
1.98      schwarze  317:        p = mandoc_calloc(1, sizeof(struct roffnode));
1.67      kristaps  318:        p->tok = tok;
1.106     kristaps  319:        if (name)
                    320:                p->name = mandoc_strdup(name);
1.67      kristaps  321:        p->parent = r->last;
                    322:        p->line = line;
                    323:        p->col = col;
1.79      kristaps  324:        p->rule = p->parent ? p->parent->rule : ROFFRULE_DENY;
1.67      kristaps  325:
                    326:        r->last = p;
                    327: }
                    328:
                    329:
                    330: static void
                    331: roff_free1(struct roff *r)
                    332: {
1.118     kristaps  333:        struct tbl_node *t;
1.125     kristaps  334:        struct eqn_node *e;
1.67      kristaps  335:
1.125     kristaps  336:        while (NULL != (t = r->first_tbl)) {
1.113     kristaps  337:                r->first_tbl = t->next;
                    338:                tbl_free(t);
1.109     kristaps  339:        }
                    340:
1.113     kristaps  341:        r->first_tbl = r->last_tbl = r->tbl = NULL;
                    342:
1.125     kristaps  343:        while (NULL != (e = r->first_eqn)) {
                    344:                r->first_eqn = e->next;
                    345:                eqn_free(e);
                    346:        }
                    347:
                    348:        r->first_eqn = r->last_eqn = r->eqn = NULL;
                    349:
1.67      kristaps  350:        while (r->last)
                    351:                roffnode_pop(r);
1.109     kristaps  352:
1.94      kristaps  353:        roff_freestr(r);
1.67      kristaps  354: }
                    355:
                    356:
                    357: void
                    358: roff_reset(struct roff *r)
                    359: {
1.143     kristaps  360:        int              i;
1.67      kristaps  361:
                    362:        roff_free1(r);
1.143     kristaps  363:
1.147     kristaps  364:        memset(&r->regs, 0, sizeof(struct reg) * REG__MAX);
                    365:
1.143     kristaps  366:        for (i = 0; i < PREDEFS_MAX; i++)
                    367:                roff_setstr(r, predefs[i].name, predefs[i].str, 0);
1.67      kristaps  368: }
                    369:
                    370:
                    371: void
                    372: roff_free(struct roff *r)
                    373: {
                    374:
                    375:        roff_free1(r);
                    376:        free(r);
                    377: }
                    378:
                    379:
                    380: struct roff *
1.147     kristaps  381: roff_alloc(struct mparse *parse)
1.67      kristaps  382: {
                    383:        struct roff     *r;
1.141     kristaps  384:        int              i;
1.67      kristaps  385:
1.98      schwarze  386:        r = mandoc_calloc(1, sizeof(struct roff));
1.128     kristaps  387:        r->parse = parse;
1.82      kristaps  388:        r->rstackpos = -1;
1.85      kristaps  389:
                    390:        roff_hash_init();
1.141     kristaps  391:
                    392:        for (i = 0; i < PREDEFS_MAX; i++)
                    393:                roff_setstr(r, predefs[i].name, predefs[i].str, 0);
                    394:
1.67      kristaps  395:        return(r);
                    396: }
                    397:
                    398:
1.94      kristaps  399: /*
                    400:  * Pre-filter each and every line for reserved words (one beginning with
                    401:  * `\*', e.g., `\*(ab').  These must be handled before the actual line
                    402:  * is processed.
                    403:  */
                    404: static int
1.142     kristaps  405: roff_res(struct roff *r, char **bufp, size_t *szp, int ln, int pos)
1.94      kristaps  406: {
1.108     schwarze  407:        const char      *stesc; /* start of an escape sequence ('\\') */
                    408:        const char      *stnam; /* start of the name, after "[(*" */
                    409:        const char      *cp;    /* end of the name, e.g. before ']' */
                    410:        const char      *res;   /* the string to be substituted */
1.94      kristaps  411:        int              i, maxl;
                    412:        size_t           nsz;
                    413:        char            *n;
                    414:
1.108     schwarze  415:        /* Search for a leading backslash and save a pointer to it. */
                    416:
                    417:        cp = *bufp + pos;
                    418:        while (NULL != (cp = strchr(cp, '\\'))) {
                    419:                stesc = cp++;
                    420:
                    421:                /*
                    422:                 * The second character must be an asterisk.
                    423:                 * If it isn't, skip it anyway:  It is escaped,
                    424:                 * so it can't start another escape sequence.
                    425:                 */
                    426:
                    427:                if ('\0' == *cp)
                    428:                        return(1);
                    429:                if ('*' != *cp++)
                    430:                        continue;
                    431:
                    432:                /*
                    433:                 * The third character decides the length
                    434:                 * of the name of the string.
                    435:                 * Save a pointer to the name.
                    436:                 */
                    437:
1.94      kristaps  438:                switch (*cp) {
1.108     schwarze  439:                case ('\0'):
                    440:                        return(1);
1.94      kristaps  441:                case ('('):
                    442:                        cp++;
                    443:                        maxl = 2;
                    444:                        break;
                    445:                case ('['):
                    446:                        cp++;
                    447:                        maxl = 0;
                    448:                        break;
                    449:                default:
                    450:                        maxl = 1;
                    451:                        break;
                    452:                }
1.108     schwarze  453:                stnam = cp;
1.94      kristaps  454:
1.108     schwarze  455:                /* Advance to the end of the name. */
1.94      kristaps  456:
                    457:                for (i = 0; 0 == maxl || i < maxl; i++, cp++) {
                    458:                        if ('\0' == *cp)
                    459:                                return(1); /* Error. */
                    460:                        if (0 == maxl && ']' == *cp)
                    461:                                break;
                    462:                }
                    463:
1.108     schwarze  464:                /*
                    465:                 * Retrieve the replacement string; if it is
                    466:                 * undefined, resume searching for escapes.
                    467:                 */
                    468:
                    469:                res = roff_getstrn(r, stnam, (size_t)i);
1.94      kristaps  470:
                    471:                if (NULL == res) {
1.142     kristaps  472:                        /* TODO: keep track of the correct position. */
                    473:                        mandoc_msg(MANDOCERR_BADESCAPE, r->parse, ln, pos, NULL);
                    474:                        res = "";
1.94      kristaps  475:                }
                    476:
1.108     schwarze  477:                /* Replace the escape sequence by the string. */
                    478:
1.94      kristaps  479:                nsz = *szp + strlen(res) + 1;
                    480:                n = mandoc_malloc(nsz);
                    481:
1.108     schwarze  482:                strlcpy(n, *bufp, (size_t)(stesc - *bufp + 1));
1.94      kristaps  483:                strlcat(n, res, nsz);
                    484:                strlcat(n, cp + (maxl ? 0 : 1), nsz);
                    485:
                    486:                free(*bufp);
                    487:
                    488:                *bufp = n;
                    489:                *szp = nsz;
                    490:                return(0);
                    491:        }
                    492:
                    493:        return(1);
                    494: }
                    495:
                    496:
1.67      kristaps  497: enum rofferr
1.90      kristaps  498: roff_parseln(struct roff *r, int ln, char **bufp,
                    499:                size_t *szp, int pos, int *offs)
1.67      kristaps  500: {
                    501:        enum rofft       t;
1.109     kristaps  502:        enum rofferr     e;
1.130     kristaps  503:        int              ppos, ctl;
1.79      kristaps  504:
                    505:        /*
1.94      kristaps  506:         * Run the reserved-word filter only if we have some reserved
                    507:         * words to fill in.
                    508:         */
                    509:
1.142     kristaps  510:        if (r->first_string && ! roff_res(r, bufp, szp, ln, pos))
1.106     kristaps  511:                return(ROFF_REPARSE);
1.94      kristaps  512:
1.130     kristaps  513:        ppos = pos;
                    514:        ctl = mandoc_getcontrol(*bufp, &pos);
                    515:
1.94      kristaps  516:        /*
1.79      kristaps  517:         * First, if a scope is open and we're not a macro, pass the
                    518:         * text through the macro's filter.  If a scope isn't open and
                    519:         * we're not a macro, just let it through.
1.125     kristaps  520:         * Finally, if there's an equation scope open, divert it into it
                    521:         * no matter our state.
1.79      kristaps  522:         */
1.74      kristaps  523:
1.130     kristaps  524:        if (r->last && ! ctl) {
1.78      kristaps  525:                t = r->last->tok;
                    526:                assert(roffs[t].text);
1.109     kristaps  527:                e = (*roffs[t].text)
                    528:                        (r, t, bufp, szp, ln, pos, pos, offs);
                    529:                assert(ROFF_IGN == e || ROFF_CONT == e);
1.125     kristaps  530:                if (ROFF_CONT != e)
                    531:                        return(e);
                    532:                if (r->eqn)
1.146     kristaps  533:                        return(eqn_read(&r->eqn, ln, *bufp, pos, offs));
1.125     kristaps  534:                if (r->tbl)
1.130     kristaps  535:                        return(tbl_read(r->tbl, ln, *bufp, pos));
1.125     kristaps  536:                return(ROFF_CONT);
1.130     kristaps  537:        } else if ( ! ctl) {
1.125     kristaps  538:                if (r->eqn)
1.146     kristaps  539:                        return(eqn_read(&r->eqn, ln, *bufp, pos, offs));
1.109     kristaps  540:                if (r->tbl)
1.130     kristaps  541:                        return(tbl_read(r->tbl, ln, *bufp, pos));
1.67      kristaps  542:                return(ROFF_CONT);
1.125     kristaps  543:        } else if (r->eqn)
1.146     kristaps  544:                return(eqn_read(&r->eqn, ln, *bufp, ppos, offs));
1.67      kristaps  545:
1.79      kristaps  546:        /*
                    547:         * If a scope is open, go to the child handler for that macro,
                    548:         * as it may want to preprocess before doing anything with it.
1.125     kristaps  549:         * Don't do so if an equation is open.
1.79      kristaps  550:         */
1.78      kristaps  551:
1.79      kristaps  552:        if (r->last) {
                    553:                t = r->last->tok;
                    554:                assert(roffs[t].sub);
                    555:                return((*roffs[t].sub)
1.90      kristaps  556:                                (r, t, bufp, szp,
1.130     kristaps  557:                                 ln, ppos, pos, offs));
1.79      kristaps  558:        }
1.78      kristaps  559:
1.79      kristaps  560:        /*
                    561:         * Lastly, as we've no scope open, try to look up and execute
                    562:         * the new macro.  If no macro is found, simply return and let
                    563:         * the compilers handle it.
                    564:         */
1.67      kristaps  565:
1.106     kristaps  566:        if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos)))
1.79      kristaps  567:                return(ROFF_CONT);
1.67      kristaps  568:
1.75      kristaps  569:        assert(roffs[t].proc);
1.78      kristaps  570:        return((*roffs[t].proc)
1.90      kristaps  571:                        (r, t, bufp, szp,
                    572:                         ln, ppos, pos, offs));
1.74      kristaps  573: }
                    574:
                    575:
1.117     kristaps  576: void
1.74      kristaps  577: roff_endparse(struct roff *r)
                    578: {
                    579:
1.110     kristaps  580:        if (r->last)
1.128     kristaps  581:                mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
1.109     kristaps  582:                                r->last->line, r->last->col, NULL);
1.117     kristaps  583:
1.125     kristaps  584:        if (r->eqn) {
1.128     kristaps  585:                mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
1.148     kristaps  586:                                r->eqn->eqn.ln, r->eqn->eqn.pos, NULL);
1.151   ! kristaps  587:                eqn_end(&r->eqn);
1.125     kristaps  588:        }
                    589:
1.117     kristaps  590:        if (r->tbl) {
1.128     kristaps  591:                mandoc_msg(MANDOCERR_SCOPEEXIT, r->parse,
1.117     kristaps  592:                                r->tbl->line, r->tbl->pos, NULL);
1.151   ! kristaps  593:                tbl_end(&r->tbl);
1.117     kristaps  594:        }
1.67      kristaps  595: }
                    596:
                    597: /*
                    598:  * Parse a roff node's type from the input buffer.  This must be in the
                    599:  * form of ".foo xxx" in the usual way.
                    600:  */
                    601: static enum rofft
1.106     kristaps  602: roff_parse(struct roff *r, const char *buf, int *pos)
1.67      kristaps  603: {
1.106     kristaps  604:        const char      *mac;
                    605:        size_t           maclen;
1.67      kristaps  606:        enum rofft       t;
                    607:
1.144     kristaps  608:        if ('\0' == buf[*pos] || '"' == buf[*pos] ||
                    609:                        '\t' == buf[*pos] || ' ' == buf[*pos])
1.67      kristaps  610:                return(ROFF_MAX);
                    611:
1.144     kristaps  612:        /*
                    613:         * We stop the macro parse at an escape, tab, space, or nil.
                    614:         * However, `\}' is also a valid macro, so make sure we don't
                    615:         * clobber it by seeing the `\' as the end of token.
                    616:         */
                    617:
1.106     kristaps  618:        mac = buf + *pos;
1.144     kristaps  619:        maclen = strcspn(mac + 1, " \\\t\0") + 1;
1.67      kristaps  620:
1.106     kristaps  621:        t = (r->current_string = roff_getstrn(r, mac, maclen))
                    622:            ? ROFF_USERDEF : roff_hash_find(mac, maclen);
1.67      kristaps  623:
1.127     kristaps  624:        *pos += (int)maclen;
1.130     kristaps  625:
1.67      kristaps  626:        while (buf[*pos] && ' ' == buf[*pos])
                    627:                (*pos)++;
                    628:
                    629:        return(t);
                    630: }
                    631:
                    632: /* ARGSUSED */
                    633: static enum rofferr
1.76      kristaps  634: roff_cblock(ROFF_ARGS)
1.67      kristaps  635: {
                    636:
1.79      kristaps  637:        /*
                    638:         * A block-close `..' should only be invoked as a child of an
                    639:         * ignore macro, otherwise raise a warning and just ignore it.
                    640:         */
                    641:
1.76      kristaps  642:        if (NULL == r->last) {
1.128     kristaps  643:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.76      kristaps  644:                return(ROFF_IGN);
                    645:        }
1.67      kristaps  646:
1.81      kristaps  647:        switch (r->last->tok) {
                    648:        case (ROFF_am):
                    649:                /* FALLTHROUGH */
                    650:        case (ROFF_ami):
                    651:                /* FALLTHROUGH */
                    652:        case (ROFF_am1):
                    653:                /* FALLTHROUGH */
                    654:        case (ROFF_de):
1.108     schwarze  655:                /* ROFF_de1 is remapped to ROFF_de in roff_block(). */
1.81      kristaps  656:                /* FALLTHROUGH */
                    657:        case (ROFF_dei):
                    658:                /* FALLTHROUGH */
                    659:        case (ROFF_ig):
                    660:                break;
                    661:        default:
1.128     kristaps  662:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.67      kristaps  663:                return(ROFF_IGN);
1.76      kristaps  664:        }
1.67      kristaps  665:
1.76      kristaps  666:        if ((*bufp)[pos])
1.128     kristaps  667:                mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
1.71      kristaps  668:
                    669:        roffnode_pop(r);
1.76      kristaps  670:        roffnode_cleanscope(r);
                    671:        return(ROFF_IGN);
1.71      kristaps  672:
1.67      kristaps  673: }
                    674:
                    675:
1.76      kristaps  676: static void
                    677: roffnode_cleanscope(struct roff *r)
1.67      kristaps  678: {
                    679:
1.76      kristaps  680:        while (r->last) {
                    681:                if (--r->last->endspan < 0)
                    682:                        break;
                    683:                roffnode_pop(r);
                    684:        }
1.67      kristaps  685: }
                    686:
                    687:
1.75      kristaps  688: /* ARGSUSED */
1.74      kristaps  689: static enum rofferr
1.75      kristaps  690: roff_ccond(ROFF_ARGS)
1.74      kristaps  691: {
                    692:
1.76      kristaps  693:        if (NULL == r->last) {
1.128     kristaps  694:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.76      kristaps  695:                return(ROFF_IGN);
                    696:        }
                    697:
1.82      kristaps  698:        switch (r->last->tok) {
                    699:        case (ROFF_el):
                    700:                /* FALLTHROUGH */
                    701:        case (ROFF_ie):
                    702:                /* FALLTHROUGH */
                    703:        case (ROFF_if):
                    704:                break;
                    705:        default:
1.128     kristaps  706:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.75      kristaps  707:                return(ROFF_IGN);
                    708:        }
                    709:
1.76      kristaps  710:        if (r->last->endspan > -1) {
1.128     kristaps  711:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.76      kristaps  712:                return(ROFF_IGN);
                    713:        }
                    714:
                    715:        if ((*bufp)[pos])
1.128     kristaps  716:                mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
1.76      kristaps  717:
1.75      kristaps  718:        roffnode_pop(r);
1.76      kristaps  719:        roffnode_cleanscope(r);
                    720:        return(ROFF_IGN);
                    721: }
                    722:
1.75      kristaps  723:
1.76      kristaps  724: /* ARGSUSED */
                    725: static enum rofferr
1.80      kristaps  726: roff_block(ROFF_ARGS)
1.76      kristaps  727: {
1.78      kristaps  728:        int             sv;
                    729:        size_t          sz;
1.106     kristaps  730:        char            *name;
                    731:
                    732:        name = NULL;
1.76      kristaps  733:
1.106     kristaps  734:        if (ROFF_ig != tok) {
                    735:                if ('\0' == (*bufp)[pos]) {
1.128     kristaps  736:                        mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
1.106     kristaps  737:                        return(ROFF_IGN);
                    738:                }
1.107     kristaps  739:
                    740:                /*
                    741:                 * Re-write `de1', since we don't really care about
                    742:                 * groff's strange compatibility mode, into `de'.
                    743:                 */
                    744:
1.106     kristaps  745:                if (ROFF_de1 == tok)
                    746:                        tok = ROFF_de;
                    747:                if (ROFF_de == tok)
                    748:                        name = *bufp + pos;
                    749:                else
1.128     kristaps  750:                        mandoc_msg(MANDOCERR_REQUEST, r->parse, ln, ppos,
1.106     kristaps  751:                            roffs[tok].name);
1.107     kristaps  752:
1.131     schwarze  753:                while ((*bufp)[pos] && ! isspace((unsigned char)(*bufp)[pos]))
1.80      kristaps  754:                        pos++;
1.107     kristaps  755:
1.131     schwarze  756:                while (isspace((unsigned char)(*bufp)[pos]))
1.106     kristaps  757:                        (*bufp)[pos++] = '\0';
1.80      kristaps  758:        }
                    759:
1.106     kristaps  760:        roffnode_push(r, tok, name, ln, ppos);
                    761:
                    762:        /*
                    763:         * At the beginning of a `de' macro, clear the existing string
                    764:         * with the same name, if there is one.  New content will be
                    765:         * added from roff_block_text() in multiline mode.
                    766:         */
1.107     kristaps  767:
1.106     kristaps  768:        if (ROFF_de == tok)
1.108     schwarze  769:                roff_setstr(r, name, "", 0);
1.76      kristaps  770:
1.79      kristaps  771:        if ('\0' == (*bufp)[pos])
1.78      kristaps  772:                return(ROFF_IGN);
                    773:
1.107     kristaps  774:        /* If present, process the custom end-of-line marker. */
                    775:
1.78      kristaps  776:        sv = pos;
1.131     schwarze  777:        while ((*bufp)[pos] && ! isspace((unsigned char)(*bufp)[pos]))
1.78      kristaps  778:                pos++;
                    779:
                    780:        /*
                    781:         * Note: groff does NOT like escape characters in the input.
                    782:         * Instead of detecting this, we're just going to let it fly and
                    783:         * to hell with it.
                    784:         */
                    785:
                    786:        assert(pos > sv);
                    787:        sz = (size_t)(pos - sv);
                    788:
1.79      kristaps  789:        if (1 == sz && '.' == (*bufp)[sv])
                    790:                return(ROFF_IGN);
                    791:
1.98      schwarze  792:        r->last->end = mandoc_malloc(sz + 1);
1.78      kristaps  793:
                    794:        memcpy(r->last->end, *bufp + sv, sz);
                    795:        r->last->end[(int)sz] = '\0';
                    796:
1.77      kristaps  797:        if ((*bufp)[pos])
1.128     kristaps  798:                mandoc_msg(MANDOCERR_ARGSLOST, r->parse, ln, pos, NULL);
1.74      kristaps  799:
1.78      kristaps  800:        return(ROFF_IGN);
                    801: }
                    802:
                    803:
                    804: /* ARGSUSED */
                    805: static enum rofferr
1.80      kristaps  806: roff_block_sub(ROFF_ARGS)
1.79      kristaps  807: {
                    808:        enum rofft      t;
                    809:        int             i, j;
                    810:
                    811:        /*
                    812:         * First check whether a custom macro exists at this level.  If
                    813:         * it does, then check against it.  This is some of groff's
                    814:         * stranger behaviours.  If we encountered a custom end-scope
                    815:         * tag and that tag also happens to be a "real" macro, then we
                    816:         * need to try interpreting it again as a real macro.  If it's
                    817:         * not, then return ignore.  Else continue.
                    818:         */
                    819:
                    820:        if (r->last->end) {
1.130     kristaps  821:                for (i = pos, j = 0; r->last->end[j]; j++, i++)
1.79      kristaps  822:                        if ((*bufp)[i] != r->last->end[j])
                    823:                                break;
                    824:
                    825:                if ('\0' == r->last->end[j] &&
                    826:                                ('\0' == (*bufp)[i] ||
                    827:                                 ' ' == (*bufp)[i] ||
                    828:                                 '\t' == (*bufp)[i])) {
                    829:                        roffnode_pop(r);
                    830:                        roffnode_cleanscope(r);
                    831:
1.130     kristaps  832:                        while (' ' == (*bufp)[i] || '\t' == (*bufp)[i])
                    833:                                i++;
                    834:
                    835:                        pos = i;
1.106     kristaps  836:                        if (ROFF_MAX != roff_parse(r, *bufp, &pos))
1.79      kristaps  837:                                return(ROFF_RERUN);
                    838:                        return(ROFF_IGN);
                    839:                }
                    840:        }
                    841:
                    842:        /*
                    843:         * If we have no custom end-query or lookup failed, then try
                    844:         * pulling it out of the hashtable.
                    845:         */
                    846:
1.137     schwarze  847:        t = roff_parse(r, *bufp, &pos);
1.79      kristaps  848:
1.106     kristaps  849:        /*
                    850:         * Macros other than block-end are only significant
                    851:         * in `de' blocks; elsewhere, simply throw them away.
                    852:         */
                    853:        if (ROFF_cblock != t) {
                    854:                if (ROFF_de == tok)
                    855:                        roff_setstr(r, r->last->name, *bufp + ppos, 1);
1.79      kristaps  856:                return(ROFF_IGN);
1.106     kristaps  857:        }
1.79      kristaps  858:
                    859:        assert(roffs[t].proc);
1.90      kristaps  860:        return((*roffs[t].proc)(r, t, bufp, szp,
                    861:                                ln, ppos, pos, offs));
1.79      kristaps  862: }
                    863:
                    864:
                    865: /* ARGSUSED */
                    866: static enum rofferr
1.80      kristaps  867: roff_block_text(ROFF_ARGS)
1.78      kristaps  868: {
                    869:
1.106     kristaps  870:        if (ROFF_de == tok)
                    871:                roff_setstr(r, r->last->name, *bufp + pos, 1);
                    872:
1.78      kristaps  873:        return(ROFF_IGN);
                    874: }
                    875:
                    876:
                    877: /* ARGSUSED */
                    878: static enum rofferr
1.82      kristaps  879: roff_cond_sub(ROFF_ARGS)
                    880: {
                    881:        enum rofft       t;
                    882:        enum roffrule    rr;
1.139     kristaps  883:        char            *ep;
1.82      kristaps  884:
                    885:        rr = r->last->rule;
1.139     kristaps  886:        roffnode_cleanscope(r);
1.82      kristaps  887:
1.139     kristaps  888:        /*
                    889:         * If the macro is unknown, first check if it contains a closing
                    890:         * delimiter `\}'.  If it does, close out our scope and return
                    891:         * the currently-scoped rule (ignore or continue).  Else, drop
                    892:         * into the currently-scoped rule.
1.87      kristaps  893:         */
                    894:
1.106     kristaps  895:        if (ROFF_MAX == (t = roff_parse(r, *bufp, &pos))) {
1.139     kristaps  896:                ep = &(*bufp)[pos];
                    897:                for ( ; NULL != (ep = strchr(ep, '\\')); ep++) {
                    898:                        ep++;
                    899:                        if ('}' != *ep)
                    900:                                continue;
1.144     kristaps  901:
                    902:                        /*
                    903:                         * Make the \} go away.
                    904:                         * This is a little haphazard, as it's not quite
                    905:                         * clear how nroff does this.
                    906:                         * If we're at the end of line, then just chop
                    907:                         * off the \} and resize the buffer.
                    908:                         * If we aren't, then conver it to spaces.
                    909:                         */
                    910:
                    911:                        if ('\0' == *(ep + 1)) {
                    912:                                *--ep = '\0';
                    913:                                *szp -= 2;
                    914:                        } else
                    915:                                *(ep - 1) = *ep = ' ';
                    916:
1.139     kristaps  917:                        roff_ccond(r, ROFF_ccond, bufp, szp,
                    918:                                        ln, pos, pos + 2, offs);
                    919:                        break;
                    920:                }
1.82      kristaps  921:                return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1.100     kristaps  922:        }
1.82      kristaps  923:
                    924:        /*
                    925:         * A denied conditional must evaluate its children if and only
                    926:         * if they're either structurally required (such as loops and
                    927:         * conditionals) or a closing macro.
                    928:         */
1.139     kristaps  929:
1.82      kristaps  930:        if (ROFFRULE_DENY == rr)
                    931:                if ( ! (ROFFMAC_STRUCT & roffs[t].flags))
                    932:                        if (ROFF_ccond != t)
                    933:                                return(ROFF_IGN);
                    934:
                    935:        assert(roffs[t].proc);
1.90      kristaps  936:        return((*roffs[t].proc)(r, t, bufp, szp,
                    937:                                ln, ppos, pos, offs));
1.82      kristaps  938: }
                    939:
                    940: /* ARGSUSED */
                    941: static enum rofferr
                    942: roff_cond_text(ROFF_ARGS)
1.78      kristaps  943: {
1.140     kristaps  944:        char            *ep;
1.82      kristaps  945:        enum roffrule    rr;
                    946:
                    947:        rr = r->last->rule;
1.140     kristaps  948:        roffnode_cleanscope(r);
1.82      kristaps  949:
1.140     kristaps  950:        ep = &(*bufp)[pos];
                    951:        for ( ; NULL != (ep = strchr(ep, '\\')); ep++) {
                    952:                ep++;
                    953:                if ('}' != *ep)
                    954:                        continue;
                    955:                *ep = '&';
                    956:                roff_ccond(r, ROFF_ccond, bufp, szp,
                    957:                                ln, pos, pos + 2, offs);
1.78      kristaps  958:        }
1.82      kristaps  959:        return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1.74      kristaps  960: }
                    961:
1.88      kristaps  962: static enum roffrule
                    963: roff_evalcond(const char *v, int *pos)
                    964: {
                    965:
                    966:        switch (v[*pos]) {
                    967:        case ('n'):
                    968:                (*pos)++;
                    969:                return(ROFFRULE_ALLOW);
                    970:        case ('e'):
                    971:                /* FALLTHROUGH */
                    972:        case ('o'):
                    973:                /* FALLTHROUGH */
                    974:        case ('t'):
                    975:                (*pos)++;
                    976:                return(ROFFRULE_DENY);
                    977:        default:
                    978:                break;
                    979:        }
                    980:
                    981:        while (v[*pos] && ' ' != v[*pos])
                    982:                (*pos)++;
                    983:        return(ROFFRULE_DENY);
                    984: }
                    985:
1.75      kristaps  986: /* ARGSUSED */
1.74      kristaps  987: static enum rofferr
1.103     kristaps  988: roff_line_ignore(ROFF_ARGS)
1.89      kristaps  989: {
1.123     schwarze  990:
                    991:        if (ROFF_it == tok)
1.128     kristaps  992:                mandoc_msg(MANDOCERR_REQUEST, r->parse, ln, ppos, "it");
1.89      kristaps  993:
                    994:        return(ROFF_IGN);
                    995: }
                    996:
1.104     kristaps  997: /* ARGSUSED */
                    998: static enum rofferr
1.82      kristaps  999: roff_cond(ROFF_ARGS)
1.74      kristaps 1000: {
1.77      kristaps 1001:        int              sv;
1.88      kristaps 1002:        enum roffrule    rule;
1.74      kristaps 1003:
1.134     kristaps 1004:        /*
                   1005:         * An `.el' has no conditional body: it will consume the value
                   1006:         * of the current rstack entry set in prior `ie' calls or
                   1007:         * defaults to DENY.
                   1008:         *
                   1009:         * If we're not an `el', however, then evaluate the conditional.
                   1010:         */
1.133     kristaps 1011:
1.134     kristaps 1012:        rule = ROFF_el == tok ?
                   1013:                (r->rstackpos < 0 ?
                   1014:                 ROFFRULE_DENY : r->rstack[r->rstackpos--]) :
                   1015:                roff_evalcond(*bufp, &pos);
1.77      kristaps 1016:
                   1017:        sv = pos;
1.75      kristaps 1018:        while (' ' == (*bufp)[pos])
                   1019:                pos++;
1.74      kristaps 1020:
1.77      kristaps 1021:        /*
                   1022:         * Roff is weird.  If we have just white-space after the
                   1023:         * conditional, it's considered the BODY and we exit without
                   1024:         * really doing anything.  Warn about this.  It's probably
                   1025:         * wrong.
                   1026:         */
1.88      kristaps 1027:
1.77      kristaps 1028:        if ('\0' == (*bufp)[pos] && sv != pos) {
1.128     kristaps 1029:                mandoc_msg(MANDOCERR_NOARGS, r->parse, ln, ppos, NULL);
1.107     kristaps 1030:                return(ROFF_IGN);
1.77      kristaps 1031:        }
                   1032:
1.106     kristaps 1033:        roffnode_push(r, tok, NULL, ln, ppos);
1.77      kristaps 1034:
1.88      kristaps 1035:        r->last->rule = rule;
                   1036:
1.134     kristaps 1037:        /*
                   1038:         * An if-else will put the NEGATION of the current evaluated
                   1039:         * conditional into the stack of rules.
                   1040:         */
                   1041:
1.84      schwarze 1042:        if (ROFF_ie == tok) {
1.134     kristaps 1043:                if (r->rstackpos == RSTACK_MAX - 1) {
                   1044:                        mandoc_msg(MANDOCERR_MEM,
                   1045:                                r->parse, ln, ppos, NULL);
                   1046:                        return(ROFF_ERR);
                   1047:                }
                   1048:                r->rstack[++r->rstackpos] =
                   1049:                        ROFFRULE_DENY == r->last->rule ?
                   1050:                        ROFFRULE_ALLOW : ROFFRULE_DENY;
1.82      kristaps 1051:        }
1.88      kristaps 1052:
                   1053:        /* If the parent has false as its rule, then so do we. */
                   1054:
1.109     kristaps 1055:        if (r->last->parent && ROFFRULE_DENY == r->last->parent->rule)
1.84      schwarze 1056:                r->last->rule = ROFFRULE_DENY;
1.88      kristaps 1057:
                   1058:        /*
                   1059:         * Determine scope.  If we're invoked with "\{" trailing the
                   1060:         * conditional, then we're in a multiline scope.  Else our scope
                   1061:         * expires on the next line.
                   1062:         */
1.74      kristaps 1063:
1.75      kristaps 1064:        r->last->endspan = 1;
                   1065:
                   1066:        if ('\\' == (*bufp)[pos] && '{' == (*bufp)[pos + 1]) {
                   1067:                r->last->endspan = -1;
                   1068:                pos += 2;
1.109     kristaps 1069:        }
1.74      kristaps 1070:
1.77      kristaps 1071:        /*
                   1072:         * If there are no arguments on the line, the next-line scope is
                   1073:         * assumed.
                   1074:         */
                   1075:
1.75      kristaps 1076:        if ('\0' == (*bufp)[pos])
                   1077:                return(ROFF_IGN);
1.77      kristaps 1078:
                   1079:        /* Otherwise re-run the roff parser after recalculating. */
1.74      kristaps 1080:
1.75      kristaps 1081:        *offs = pos;
                   1082:        return(ROFF_RERUN);
1.83      schwarze 1083: }
                   1084:
                   1085:
                   1086: /* ARGSUSED */
                   1087: static enum rofferr
1.92      schwarze 1088: roff_ds(ROFF_ARGS)
                   1089: {
1.96      kristaps 1090:        char            *name, *string;
                   1091:
                   1092:        /*
                   1093:         * A symbol is named by the first word following the macro
                   1094:         * invocation up to a space.  Its value is anything after the
                   1095:         * name's trailing whitespace and optional double-quote.  Thus,
                   1096:         *
                   1097:         *  [.ds foo "bar  "     ]
                   1098:         *
                   1099:         * will have `bar  "     ' as its value.
                   1100:         */
1.92      schwarze 1101:
1.121     schwarze 1102:        string = *bufp + pos;
                   1103:        name = roff_getname(r, &string, ln, pos);
1.92      schwarze 1104:        if ('\0' == *name)
                   1105:                return(ROFF_IGN);
                   1106:
1.121     schwarze 1107:        /* Read past initial double-quote. */
                   1108:        if ('"' == *string)
1.92      schwarze 1109:                string++;
                   1110:
1.96      kristaps 1111:        /* The rest is the value. */
1.106     kristaps 1112:        roff_setstr(r, name, string, 0);
1.92      schwarze 1113:        return(ROFF_IGN);
                   1114: }
                   1115:
1.147     kristaps 1116: int
                   1117: roff_regisset(const struct roff *r, enum regs reg)
                   1118: {
                   1119:
                   1120:        return(r->regs[(int)reg].set);
                   1121: }
                   1122:
                   1123: unsigned int
                   1124: roff_regget(const struct roff *r, enum regs reg)
                   1125: {
                   1126:
                   1127:        return(r->regs[(int)reg].u);
                   1128: }
                   1129:
                   1130: void
                   1131: roff_regunset(struct roff *r, enum regs reg)
                   1132: {
                   1133:
                   1134:        r->regs[(int)reg].set = 0;
                   1135: }
1.92      schwarze 1136:
                   1137: /* ARGSUSED */
                   1138: static enum rofferr
1.89      kristaps 1139: roff_nr(ROFF_ARGS)
1.83      schwarze 1140: {
1.121     schwarze 1141:        const char      *key;
                   1142:        char            *val;
1.138     kristaps 1143:        int              iv;
1.89      kristaps 1144:
1.121     schwarze 1145:        val = *bufp + pos;
                   1146:        key = roff_getname(r, &val, ln, pos);
1.89      kristaps 1147:
                   1148:        if (0 == strcmp(key, "nS")) {
1.147     kristaps 1149:                r->regs[(int)REG_nS].set = 1;
1.149     kristaps 1150:                if ((iv = mandoc_strntoi(val, strlen(val), 10)) >= 0)
1.147     kristaps 1151:                        r->regs[(int)REG_nS].u = (unsigned)iv;
1.138     kristaps 1152:                else
1.147     kristaps 1153:                        r->regs[(int)REG_nS].u = 0u;
1.109     kristaps 1154:        }
                   1155:
1.122     schwarze 1156:        return(ROFF_IGN);
                   1157: }
                   1158:
                   1159: /* ARGSUSED */
                   1160: static enum rofferr
                   1161: roff_rm(ROFF_ARGS)
                   1162: {
                   1163:        const char       *name;
                   1164:        char             *cp;
                   1165:
                   1166:        cp = *bufp + pos;
                   1167:        while ('\0' != *cp) {
1.127     kristaps 1168:                name = roff_getname(r, &cp, ln, (int)(cp - *bufp));
1.122     schwarze 1169:                if ('\0' != *name)
                   1170:                        roff_setstr(r, name, NULL, 0);
                   1171:        }
1.109     kristaps 1172:        return(ROFF_IGN);
                   1173: }
                   1174:
                   1175: /* ARGSUSED */
                   1176: static enum rofferr
                   1177: roff_TE(ROFF_ARGS)
                   1178: {
                   1179:
                   1180:        if (NULL == r->tbl)
1.128     kristaps 1181:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.115     kristaps 1182:        else
1.151   ! kristaps 1183:                tbl_end(&r->tbl);
1.109     kristaps 1184:
1.112     kristaps 1185:        return(ROFF_IGN);
                   1186: }
                   1187:
                   1188: /* ARGSUSED */
                   1189: static enum rofferr
                   1190: roff_T_(ROFF_ARGS)
                   1191: {
                   1192:
                   1193:        if (NULL == r->tbl)
1.128     kristaps 1194:                mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.112     kristaps 1195:        else
1.116     kristaps 1196:                tbl_restart(ppos, ln, r->tbl);
1.112     kristaps 1197:
1.109     kristaps 1198:        return(ROFF_IGN);
                   1199: }
                   1200:
1.151   ! kristaps 1201: int
        !          1202: roff_closeeqn(struct roff *r)
        !          1203: {
        !          1204:
        !          1205:        return(r->eqn && ROFF_EQN == eqn_end(&r->eqn) ? 1 : 0);
        !          1206: }
        !          1207:
        !          1208: void
        !          1209: roff_openeqn(struct roff *r, const char *name, int line,
        !          1210:                int offs, const char *buf)
1.125     kristaps 1211: {
1.151   ! kristaps 1212:        struct eqn_node *e;
        !          1213:        int              poff;
1.125     kristaps 1214:
                   1215:        assert(NULL == r->eqn);
1.151   ! kristaps 1216:        e = eqn_alloc(name, offs, line, r->parse);
1.125     kristaps 1217:
                   1218:        if (r->last_eqn)
                   1219:                r->last_eqn->next = e;
                   1220:        else
                   1221:                r->first_eqn = r->last_eqn = e;
                   1222:
                   1223:        r->eqn = r->last_eqn = e;
1.151   ! kristaps 1224:
        !          1225:        if (buf) {
        !          1226:                poff = 0;
        !          1227:                eqn_read(&r->eqn, line, buf, offs, &poff);
        !          1228:        }
        !          1229: }
        !          1230:
        !          1231: /* ARGSUSED */
        !          1232: static enum rofferr
        !          1233: roff_EQ(ROFF_ARGS)
        !          1234: {
        !          1235:
        !          1236:        roff_openeqn(r, *bufp + pos, ln, ppos, NULL);
1.125     kristaps 1237:        return(ROFF_IGN);
                   1238: }
                   1239:
                   1240: /* ARGSUSED */
                   1241: static enum rofferr
                   1242: roff_EN(ROFF_ARGS)
                   1243: {
                   1244:
1.128     kristaps 1245:        mandoc_msg(MANDOCERR_NOSCOPE, r->parse, ln, ppos, NULL);
1.125     kristaps 1246:        return(ROFF_IGN);
                   1247: }
                   1248:
                   1249: /* ARGSUSED */
                   1250: static enum rofferr
1.109     kristaps 1251: roff_TS(ROFF_ARGS)
                   1252: {
1.118     kristaps 1253:        struct tbl_node *t;
1.89      kristaps 1254:
1.115     kristaps 1255:        if (r->tbl) {
1.128     kristaps 1256:                mandoc_msg(MANDOCERR_SCOPEBROKEN, r->parse, ln, ppos, NULL);
1.151   ! kristaps 1257:                tbl_end(&r->tbl);
1.115     kristaps 1258:        }
1.83      schwarze 1259:
1.128     kristaps 1260:        t = tbl_alloc(ppos, ln, r->parse);
1.113     kristaps 1261:
                   1262:        if (r->last_tbl)
                   1263:                r->last_tbl->next = t;
                   1264:        else
                   1265:                r->first_tbl = r->last_tbl = t;
                   1266:
                   1267:        r->tbl = r->last_tbl = t;
1.83      schwarze 1268:        return(ROFF_IGN);
1.92      schwarze 1269: }
                   1270:
1.105     kristaps 1271: /* ARGSUSED */
                   1272: static enum rofferr
                   1273: roff_so(ROFF_ARGS)
                   1274: {
                   1275:        char *name;
                   1276:
1.128     kristaps 1277:        mandoc_msg(MANDOCERR_SO, r->parse, ln, ppos, NULL);
1.105     kristaps 1278:
                   1279:        /*
                   1280:         * Handle `so'.  Be EXTREMELY careful, as we shouldn't be
                   1281:         * opening anything that's not in our cwd or anything beneath
                   1282:         * it.  Thus, explicitly disallow traversing up the file-system
                   1283:         * or using absolute paths.
                   1284:         */
                   1285:
                   1286:        name = *bufp + pos;
                   1287:        if ('/' == *name || strstr(name, "../") || strstr(name, "/..")) {
1.128     kristaps 1288:                mandoc_msg(MANDOCERR_SOPATH, r->parse, ln, pos, NULL);
1.105     kristaps 1289:                return(ROFF_ERR);
                   1290:        }
                   1291:
                   1292:        *offs = pos;
                   1293:        return(ROFF_SO);
                   1294: }
1.92      schwarze 1295:
1.106     kristaps 1296: /* ARGSUSED */
                   1297: static enum rofferr
                   1298: roff_userdef(ROFF_ARGS)
1.99      kristaps 1299: {
1.106     kristaps 1300:        const char       *arg[9];
                   1301:        char             *cp, *n1, *n2;
1.119     schwarze 1302:        int               i;
1.106     kristaps 1303:
                   1304:        /*
                   1305:         * Collect pointers to macro argument strings
                   1306:         * and null-terminate them.
                   1307:         */
                   1308:        cp = *bufp + pos;
1.119     schwarze 1309:        for (i = 0; i < 9; i++)
1.120     schwarze 1310:                arg[i] = '\0' == *cp ? "" :
1.136     kristaps 1311:                    mandoc_getarg(r->parse, &cp, ln, &pos);
1.99      kristaps 1312:
1.106     kristaps 1313:        /*
                   1314:         * Expand macro arguments.
1.99      kristaps 1315:         */
1.106     kristaps 1316:        *szp = 0;
                   1317:        n1 = cp = mandoc_strdup(r->current_string);
                   1318:        while (NULL != (cp = strstr(cp, "\\$"))) {
                   1319:                i = cp[2] - '1';
                   1320:                if (0 > i || 8 < i) {
                   1321:                        /* Not an argument invocation. */
                   1322:                        cp += 2;
                   1323:                        continue;
                   1324:                }
                   1325:
                   1326:                *szp = strlen(n1) - 3 + strlen(arg[i]) + 1;
                   1327:                n2 = mandoc_malloc(*szp);
                   1328:
                   1329:                strlcpy(n2, n1, (size_t)(cp - n1 + 1));
                   1330:                strlcat(n2, arg[i], *szp);
                   1331:                strlcat(n2, cp + 3, *szp);
                   1332:
                   1333:                cp = n2 + (cp - n1);
                   1334:                free(n1);
                   1335:                n1 = n2;
1.99      kristaps 1336:        }
                   1337:
1.106     kristaps 1338:        /*
                   1339:         * Replace the macro invocation
                   1340:         * by the expanded macro.
                   1341:         */
                   1342:        free(*bufp);
                   1343:        *bufp = n1;
                   1344:        if (0 == *szp)
                   1345:                *szp = strlen(*bufp) + 1;
                   1346:
                   1347:        return(*szp > 1 && '\n' == (*bufp)[(int)*szp - 2] ?
                   1348:           ROFF_REPARSE : ROFF_APPEND);
1.99      kristaps 1349: }
1.121     schwarze 1350:
                   1351: static char *
                   1352: roff_getname(struct roff *r, char **cpp, int ln, int pos)
                   1353: {
                   1354:        char     *name, *cp;
                   1355:
                   1356:        name = *cpp;
                   1357:        if ('\0' == *name)
                   1358:                return(name);
                   1359:
                   1360:        /* Read until end of name. */
                   1361:        for (cp = name; '\0' != *cp && ' ' != *cp; cp++) {
                   1362:                if ('\\' != *cp)
                   1363:                        continue;
                   1364:                cp++;
                   1365:                if ('\\' == *cp)
                   1366:                        continue;
1.128     kristaps 1367:                mandoc_msg(MANDOCERR_NAMESC, r->parse, ln, pos, NULL);
1.121     schwarze 1368:                *cp = '\0';
                   1369:                name = cp;
                   1370:        }
                   1371:
                   1372:        /* Nil-terminate name. */
                   1373:        if ('\0' != *cp)
                   1374:                *(cp++) = '\0';
                   1375:
                   1376:        /* Read past spaces. */
                   1377:        while (' ' == *cp)
                   1378:                cp++;
                   1379:
                   1380:        *cpp = cp;
                   1381:        return(name);
                   1382: }
                   1383:
1.106     kristaps 1384: /*
                   1385:  * Store *string into the user-defined string called *name.
                   1386:  * In multiline mode, append to an existing entry and append '\n';
                   1387:  * else replace the existing entry, if there is one.
                   1388:  * To clear an existing entry, call with (*r, *name, NULL, 0).
                   1389:  */
1.94      kristaps 1390: static void
1.106     kristaps 1391: roff_setstr(struct roff *r, const char *name, const char *string,
                   1392:        int multiline)
1.92      schwarze 1393: {
                   1394:        struct roffstr   *n;
1.106     kristaps 1395:        char             *c;
                   1396:        size_t            oldch, newch;
1.92      schwarze 1397:
1.106     kristaps 1398:        /* Search for an existing string with the same name. */
1.94      kristaps 1399:        n = r->first_string;
1.92      schwarze 1400:        while (n && strcmp(name, n->name))
                   1401:                n = n->next;
1.94      kristaps 1402:
                   1403:        if (NULL == n) {
1.106     kristaps 1404:                /* Create a new string table entry. */
1.94      kristaps 1405:                n = mandoc_malloc(sizeof(struct roffstr));
1.106     kristaps 1406:                n->name = mandoc_strdup(name);
                   1407:                n->string = NULL;
1.94      kristaps 1408:                n->next = r->first_string;
                   1409:                r->first_string = n;
1.106     kristaps 1410:        } else if (0 == multiline) {
                   1411:                /* In multiline mode, append; else replace. */
1.92      schwarze 1412:                free(n->string);
1.106     kristaps 1413:                n->string = NULL;
                   1414:        }
                   1415:
                   1416:        if (NULL == string)
                   1417:                return;
                   1418:
                   1419:        /*
                   1420:         * One additional byte for the '\n' in multiline mode,
                   1421:         * and one for the terminating '\0'.
                   1422:         */
1.127     kristaps 1423:        newch = strlen(string) + (multiline ? 2u : 1u);
1.106     kristaps 1424:        if (NULL == n->string) {
                   1425:                n->string = mandoc_malloc(newch);
                   1426:                *n->string = '\0';
                   1427:                oldch = 0;
                   1428:        } else {
                   1429:                oldch = strlen(n->string);
                   1430:                n->string = mandoc_realloc(n->string, oldch + newch);
                   1431:        }
                   1432:
                   1433:        /* Skip existing content in the destination buffer. */
1.127     kristaps 1434:        c = n->string + (int)oldch;
1.106     kristaps 1435:
                   1436:        /* Append new content to the destination buffer. */
                   1437:        while (*string) {
                   1438:                /*
                   1439:                 * Rudimentary roff copy mode:
                   1440:                 * Handle escaped backslashes.
                   1441:                 */
                   1442:                if ('\\' == *string && '\\' == *(string + 1))
                   1443:                        string++;
                   1444:                *c++ = *string++;
                   1445:        }
1.94      kristaps 1446:
1.106     kristaps 1447:        /* Append terminating bytes. */
                   1448:        if (multiline)
                   1449:                *c++ = '\n';
                   1450:        *c = '\0';
1.92      schwarze 1451: }
                   1452:
1.94      kristaps 1453: static const char *
                   1454: roff_getstrn(const struct roff *r, const char *name, size_t len)
1.92      schwarze 1455: {
1.94      kristaps 1456:        const struct roffstr *n;
1.92      schwarze 1457:
1.94      kristaps 1458:        n = r->first_string;
1.97      kristaps 1459:        while (n && (strncmp(name, n->name, len) || '\0' != n->name[(int)len]))
1.92      schwarze 1460:                n = n->next;
1.94      kristaps 1461:
                   1462:        return(n ? n->string : NULL);
1.92      schwarze 1463: }
                   1464:
1.94      kristaps 1465: static void
                   1466: roff_freestr(struct roff *r)
1.92      schwarze 1467: {
                   1468:        struct roffstr   *n, *nn;
                   1469:
1.94      kristaps 1470:        for (n = r->first_string; n; n = nn) {
1.92      schwarze 1471:                free(n->name);
                   1472:                free(n->string);
                   1473:                nn = n->next;
                   1474:                free(n);
                   1475:        }
1.94      kristaps 1476:
                   1477:        r->first_string = NULL;
1.114     kristaps 1478: }
                   1479:
                   1480: const struct tbl_span *
                   1481: roff_span(const struct roff *r)
                   1482: {
                   1483:
                   1484:        return(r->tbl ? tbl_span(r->tbl) : NULL);
1.125     kristaps 1485: }
                   1486:
                   1487: const struct eqn *
                   1488: roff_eqn(const struct roff *r)
                   1489: {
                   1490:
                   1491:        return(r->last_eqn ? &r->last_eqn->eqn : NULL);
1.151   ! kristaps 1492: }
        !          1493:
        !          1494: char
        !          1495: roff_eqndelim(const struct roff *r)
        !          1496: {
        !          1497:
        !          1498:        return('\0');
1.74      kristaps 1499: }

CVSweb