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

Annotation of mandoc/roff.c, Revision 1.133

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

CVSweb