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

Annotation of mandoc/roff.c, Revision 1.122

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

CVSweb