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

Annotation of mandoc/roff.c, Revision 1.124

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

CVSweb