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

Annotation of mandoc/roff.c, Revision 1.106

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

CVSweb