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

Annotation of mandoc/roff.c, Revision 1.138

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

CVSweb