[BACK]Return to roff.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/roff.c, Revision 1.23

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