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

Annotation of mandoc/roff.c, Revision 1.123

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

CVSweb