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

Annotation of mandoc/roff.c, Revision 1.128

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

CVSweb