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

Annotation of mandoc/roff.c, Revision 1.98

1.98    ! schwarze    1: /*     $Id: roff.c,v 1.97 2010/07/27 19:56:50 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.66      kristaps   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     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.80      kristaps   48:        ROFF_am,
                     49:        ROFF_ami,
                     50:        ROFF_am1,
                     51:        ROFF_de,
                     52:        ROFF_dei,
                     53:        ROFF_de1,
1.83      schwarze   54:        ROFF_ds,
1.82      kristaps   55:        ROFF_el,
                     56:        ROFF_ie,
1.75      kristaps   57:        ROFF_if,
1.76      kristaps   58:        ROFF_ig,
1.83      schwarze   59:        ROFF_rm,
                     60:        ROFF_tr,
1.76      kristaps   61:        ROFF_cblock,
1.75      kristaps   62:        ROFF_ccond,
1.89      kristaps   63:        ROFF_nr,
1.67      kristaps   64:        ROFF_MAX
                     65: };
                     66:
1.82      kristaps   67: enum   roffrule {
                     68:        ROFFRULE_ALLOW,
                     69:        ROFFRULE_DENY
                     70: };
                     71:
1.94      kristaps   72:
                     73: struct roffstr {
                     74:        char            *name; /* key of symbol */
                     75:        char            *string; /* current value */
                     76:        struct roffstr  *next; /* next in list */
                     77: };
                     78:
1.67      kristaps   79: struct roff {
                     80:        struct roffnode *last; /* leaf of stack */
                     81:        mandocmsg        msg; /* err/warn/fatal messages */
                     82:        void            *data; /* privdata for messages */
1.82      kristaps   83:        enum roffrule    rstack[RSTACK_MAX]; /* stack of !`ie' rules */
                     84:        int              rstackpos; /* position in rstack */
1.90      kristaps   85:        struct regset   *regs; /* read/writable registers */
1.94      kristaps   86:        struct roffstr  *first_string;
1.79      kristaps   87: };
                     88:
1.67      kristaps   89: struct roffnode {
                     90:        enum rofft       tok; /* type of node */
                     91:        struct roffnode *parent; /* up one in stack */
                     92:        int              line; /* parse line */
                     93:        int              col; /* parse col */
1.79      kristaps   94:        char            *end; /* end-rules: custom token */
                     95:        int              endspan; /* end-rules: next-line or infty */
1.82      kristaps   96:        enum roffrule    rule; /* current evaluation rule */
1.67      kristaps   97: };
                     98:
                     99: #define        ROFF_ARGS        struct roff *r, /* parse ctx */ \
1.72      kristaps  100:                         enum rofft tok, /* tok of macro */ \
1.67      kristaps  101:                         char **bufp, /* input buffer */ \
                    102:                         size_t *szp, /* size of input buffer */ \
                    103:                         int ln, /* parse line */ \
1.75      kristaps  104:                         int ppos, /* original pos in buffer */ \
                    105:                         int pos, /* current pos in buffer */ \
1.74      kristaps  106:                         int *offs /* reset offset of buffer data */
1.67      kristaps  107:
                    108: typedef        enum rofferr (*roffproc)(ROFF_ARGS);
                    109:
                    110: struct roffmac {
                    111:        const char      *name; /* macro name */
1.79      kristaps  112:        roffproc         proc; /* process new macro */
                    113:        roffproc         text; /* process as child text of macro */
                    114:        roffproc         sub; /* process as child of macro */
                    115:        int              flags;
                    116: #define        ROFFMAC_STRUCT  (1 << 0) /* always interpret */
1.85      kristaps  117:        struct roffmac  *next;
1.67      kristaps  118: };
                    119:
1.80      kristaps  120: static enum rofferr     roff_block(ROFF_ARGS);
                    121: static enum rofferr     roff_block_text(ROFF_ARGS);
                    122: static enum rofferr     roff_block_sub(ROFF_ARGS);
                    123: static enum rofferr     roff_cblock(ROFF_ARGS);
                    124: static enum rofferr     roff_ccond(ROFF_ARGS);
1.82      kristaps  125: static enum rofferr     roff_cond(ROFF_ARGS);
                    126: static enum rofferr     roff_cond_text(ROFF_ARGS);
                    127: static enum rofferr     roff_cond_sub(ROFF_ARGS);
1.92      schwarze  128: static enum rofferr     roff_ds(ROFF_ARGS);
1.94      kristaps  129: static enum roffrule    roff_evalcond(const char *, int *);
                    130: static void             roff_freestr(struct roff *);
                    131: static const char      *roff_getstrn(const struct roff *,
                    132:                                const char *, size_t);
1.89      kristaps  133: static enum rofferr     roff_line(ROFF_ARGS);
                    134: static enum rofferr     roff_nr(ROFF_ARGS);
1.95      kristaps  135: static int              roff_res(struct roff *,
                    136:                                char **, size_t *, int);
1.94      kristaps  137: static void             roff_setstr(struct roff *,
                    138:                                const char *, const char *);
1.67      kristaps  139:
1.85      kristaps  140: /* See roff_hash_find() */
                    141:
                    142: #define        ASCII_HI         126
                    143: #define        ASCII_LO         33
                    144: #define        HASHWIDTH       (ASCII_HI - ASCII_LO + 1)
                    145:
                    146: static struct roffmac  *hash[HASHWIDTH];
                    147:
                    148: static struct roffmac   roffs[ROFF_MAX] = {
                    149:        { "am", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    150:        { "ami", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    151:        { "am1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    152:        { "de", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    153:        { "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    154:        { "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
1.92      schwarze  155:        { "ds", roff_ds, NULL, NULL, 0, NULL },
1.85      kristaps  156:        { "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
                    157:        { "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
                    158:        { "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
                    159:        { "ig", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    160:        { "rm", roff_line, NULL, NULL, 0, NULL },
                    161:        { "tr", roff_line, NULL, NULL, 0, NULL },
                    162:        { ".", roff_cblock, NULL, NULL, 0, NULL },
                    163:        { "\\}", roff_ccond, NULL, NULL, 0, NULL },
1.89      kristaps  164:        { "nr", roff_nr, NULL, NULL, 0, NULL },
1.67      kristaps  165: };
                    166:
                    167: static void             roff_free1(struct roff *);
                    168: static enum rofft       roff_hash_find(const char *);
1.85      kristaps  169: static void             roff_hash_init(void);
1.76      kristaps  170: static void             roffnode_cleanscope(struct roff *);
1.98    ! schwarze  171: static void             roffnode_push(struct roff *,
1.67      kristaps  172:                                enum rofft, int, int);
                    173: static void             roffnode_pop(struct roff *);
                    174: static enum rofft       roff_parse(const char *, int *);
1.91      kristaps  175: static int              roff_parse_nat(const char *, unsigned int *);
1.67      kristaps  176:
1.85      kristaps  177: /* See roff_hash_find() */
                    178: #define        ROFF_HASH(p)    (p[0] - ASCII_LO)
                    179:
                    180: static void
                    181: roff_hash_init(void)
                    182: {
                    183:        struct roffmac   *n;
                    184:        int               buc, i;
                    185:
                    186:        for (i = 0; i < (int)ROFF_MAX; i++) {
                    187:                assert(roffs[i].name[0] >= ASCII_LO);
                    188:                assert(roffs[i].name[0] <= ASCII_HI);
                    189:
                    190:                buc = ROFF_HASH(roffs[i].name);
                    191:
                    192:                if (NULL != (n = hash[buc])) {
                    193:                        for ( ; n->next; n = n->next)
                    194:                                /* Do nothing. */ ;
                    195:                        n->next = &roffs[i];
                    196:                } else
                    197:                        hash[buc] = &roffs[i];
                    198:        }
                    199: }
                    200:
1.67      kristaps  201:
                    202: /*
                    203:  * Look up a roff token by its name.  Returns ROFF_MAX if no macro by
                    204:  * the nil-terminated string name could be found.
                    205:  */
                    206: static enum rofft
                    207: roff_hash_find(const char *p)
                    208: {
1.85      kristaps  209:        int              buc;
                    210:        struct roffmac  *n;
1.67      kristaps  211:
1.85      kristaps  212:        /*
                    213:         * libroff has an extremely simple hashtable, for the time
                    214:         * being, which simply keys on the first character, which must
                    215:         * be printable, then walks a chain.  It works well enough until
                    216:         * optimised.
                    217:         */
                    218:
                    219:        if (p[0] < ASCII_LO || p[0] > ASCII_HI)
                    220:                return(ROFF_MAX);
                    221:
                    222:        buc = ROFF_HASH(p);
                    223:
                    224:        if (NULL == (n = hash[buc]))
                    225:                return(ROFF_MAX);
                    226:        for ( ; n; n = n->next)
                    227:                if (0 == strcmp(n->name, p))
                    228:                        return((enum rofft)(n - roffs));
1.67      kristaps  229:
                    230:        return(ROFF_MAX);
                    231: }
                    232:
                    233:
                    234: /*
                    235:  * Pop the current node off of the stack of roff instructions currently
                    236:  * pending.
                    237:  */
                    238: static void
                    239: roffnode_pop(struct roff *r)
                    240: {
                    241:        struct roffnode *p;
                    242:
1.75      kristaps  243:        assert(r->last);
                    244:        p = r->last;
1.82      kristaps  245:
                    246:        if (ROFF_el == p->tok)
                    247:                if (r->rstackpos > -1)
                    248:                        r->rstackpos--;
                    249:
1.75      kristaps  250:        r->last = r->last->parent;
1.74      kristaps  251:        if (p->end)
                    252:                free(p->end);
1.67      kristaps  253:        free(p);
                    254: }
                    255:
                    256:
                    257: /*
                    258:  * Push a roff node onto the instruction stack.  This must later be
                    259:  * removed with roffnode_pop().
                    260:  */
1.98    ! schwarze  261: static void
1.67      kristaps  262: roffnode_push(struct roff *r, enum rofft tok, int line, int col)
                    263: {
                    264:        struct roffnode *p;
                    265:
1.98    ! schwarze  266:        p = mandoc_calloc(1, sizeof(struct roffnode));
1.67      kristaps  267:        p->tok = tok;
                    268:        p->parent = r->last;
                    269:        p->line = line;
                    270:        p->col = col;
1.79      kristaps  271:        p->rule = p->parent ? p->parent->rule : ROFFRULE_DENY;
1.67      kristaps  272:
                    273:        r->last = p;
                    274: }
                    275:
                    276:
                    277: static void
                    278: roff_free1(struct roff *r)
                    279: {
                    280:
                    281:        while (r->last)
                    282:                roffnode_pop(r);
1.94      kristaps  283:        roff_freestr(r);
1.67      kristaps  284: }
                    285:
                    286:
                    287: void
                    288: roff_reset(struct roff *r)
                    289: {
                    290:
                    291:        roff_free1(r);
                    292: }
                    293:
                    294:
                    295: void
                    296: roff_free(struct roff *r)
                    297: {
                    298:
                    299:        roff_free1(r);
                    300:        free(r);
                    301: }
                    302:
                    303:
                    304: struct roff *
1.98    ! schwarze  305: roff_alloc(struct regset *regs, void *data, const mandocmsg msg)
1.67      kristaps  306: {
                    307:        struct roff     *r;
                    308:
1.98    ! schwarze  309:        r = mandoc_calloc(1, sizeof(struct roff));
1.90      kristaps  310:        r->regs = regs;
1.67      kristaps  311:        r->msg = msg;
                    312:        r->data = data;
1.82      kristaps  313:        r->rstackpos = -1;
1.85      kristaps  314:
                    315:        roff_hash_init();
1.67      kristaps  316:        return(r);
                    317: }
                    318:
                    319:
1.94      kristaps  320: /*
                    321:  * Pre-filter each and every line for reserved words (one beginning with
                    322:  * `\*', e.g., `\*(ab').  These must be handled before the actual line
                    323:  * is processed.
                    324:  */
                    325: static int
1.95      kristaps  326: roff_res(struct roff *r, char **bufp, size_t *szp, int pos)
1.94      kristaps  327: {
                    328:        const char      *cp, *cpp, *st, *res;
                    329:        int              i, maxl;
                    330:        size_t           nsz;
                    331:        char            *n;
                    332:
1.95      kristaps  333:        /* LINTED */
1.94      kristaps  334:        for (cp = &(*bufp)[pos]; (cpp = strstr(cp, "\\*")); cp++) {
                    335:                cp = cpp + 2;
                    336:                switch (*cp) {
                    337:                case ('('):
                    338:                        cp++;
                    339:                        maxl = 2;
                    340:                        break;
                    341:                case ('['):
                    342:                        cp++;
                    343:                        maxl = 0;
                    344:                        break;
                    345:                default:
                    346:                        maxl = 1;
                    347:                        break;
                    348:                }
                    349:
                    350:                st = cp;
                    351:
                    352:                for (i = 0; 0 == maxl || i < maxl; i++, cp++) {
                    353:                        if ('\0' == *cp)
                    354:                                return(1); /* Error. */
                    355:                        if (0 == maxl && ']' == *cp)
                    356:                                break;
                    357:                }
                    358:
                    359:                res = roff_getstrn(r, st, (size_t)i);
                    360:
                    361:                if (NULL == res) {
                    362:                        cp -= maxl ? 1 : 0;
                    363:                        continue;
                    364:                }
                    365:
                    366:                ROFF_DEBUG("roff: splicing reserved: [%.*s]\n", i, st);
                    367:
                    368:                nsz = *szp + strlen(res) + 1;
                    369:                n = mandoc_malloc(nsz);
                    370:
                    371:                *n = '\0';
                    372:
                    373:                strlcat(n, *bufp, (size_t)(cpp - *bufp + 1));
                    374:                strlcat(n, res, nsz);
                    375:                strlcat(n, cp + (maxl ? 0 : 1), nsz);
                    376:
                    377:                free(*bufp);
                    378:
                    379:                *bufp = n;
                    380:                *szp = nsz;
                    381:                return(0);
                    382:        }
                    383:
                    384:        return(1);
                    385: }
                    386:
                    387:
1.67      kristaps  388: enum rofferr
1.90      kristaps  389: roff_parseln(struct roff *r, int ln, char **bufp,
                    390:                size_t *szp, int pos, int *offs)
1.67      kristaps  391: {
                    392:        enum rofft       t;
1.79      kristaps  393:        int              ppos;
                    394:
                    395:        /*
1.94      kristaps  396:         * Run the reserved-word filter only if we have some reserved
                    397:         * words to fill in.
                    398:         */
                    399:
1.95      kristaps  400:        if (r->first_string && ! roff_res(r, bufp, szp, pos))
1.94      kristaps  401:                return(ROFF_RERUN);
                    402:
                    403:        /*
1.79      kristaps  404:         * First, if a scope is open and we're not a macro, pass the
                    405:         * text through the macro's filter.  If a scope isn't open and
                    406:         * we're not a macro, just let it through.
                    407:         */
1.74      kristaps  408:
1.75      kristaps  409:        if (r->last && ! ROFF_CTL((*bufp)[pos])) {
1.78      kristaps  410:                t = r->last->tok;
                    411:                assert(roffs[t].text);
1.88      kristaps  412:                ROFF_DEBUG("roff: intercept scoped text: %s, [%s]\n",
                    413:                                roffs[t].name, &(*bufp)[pos]);
1.78      kristaps  414:                return((*roffs[t].text)
1.90      kristaps  415:                                (r, t, bufp, szp,
                    416:                                 ln, pos, pos, offs));
1.94      kristaps  417:        } else if ( ! ROFF_CTL((*bufp)[pos]))
1.67      kristaps  418:                return(ROFF_CONT);
                    419:
1.79      kristaps  420:        /*
                    421:         * If a scope is open, go to the child handler for that macro,
                    422:         * as it may want to preprocess before doing anything with it.
                    423:         */
1.78      kristaps  424:
1.79      kristaps  425:        if (r->last) {
                    426:                t = r->last->tok;
                    427:                assert(roffs[t].sub);
1.88      kristaps  428:                ROFF_DEBUG("roff: intercept scoped context: %s\n",
                    429:                                roffs[t].name);
1.79      kristaps  430:                return((*roffs[t].sub)
1.90      kristaps  431:                                (r, t, bufp, szp,
                    432:                                 ln, pos, pos, offs));
1.79      kristaps  433:        }
1.78      kristaps  434:
1.79      kristaps  435:        /*
                    436:         * Lastly, as we've no scope open, try to look up and execute
                    437:         * the new macro.  If no macro is found, simply return and let
                    438:         * the compilers handle it.
                    439:         */
1.67      kristaps  440:
1.75      kristaps  441:        ppos = pos;
1.94      kristaps  442:        if (ROFF_MAX == (t = roff_parse(*bufp, &pos)))
1.79      kristaps  443:                return(ROFF_CONT);
1.67      kristaps  444:
1.88      kristaps  445:        ROFF_DEBUG("roff: intercept new-scope: %s, [%s]\n",
                    446:                        roffs[t].name, &(*bufp)[pos]);
1.75      kristaps  447:        assert(roffs[t].proc);
1.78      kristaps  448:        return((*roffs[t].proc)
1.90      kristaps  449:                        (r, t, bufp, szp,
                    450:                         ln, ppos, pos, offs));
1.74      kristaps  451: }
                    452:
                    453:
                    454: int
                    455: roff_endparse(struct roff *r)
                    456: {
                    457:
                    458:        if (NULL == r->last)
                    459:                return(1);
                    460:        return((*r->msg)(MANDOCERR_SCOPEEXIT, r->data, r->last->line,
                    461:                                r->last->col, NULL));
1.67      kristaps  462: }
                    463:
                    464:
                    465: /*
                    466:  * Parse a roff node's type from the input buffer.  This must be in the
                    467:  * form of ".foo xxx" in the usual way.
                    468:  */
                    469: static enum rofft
                    470: roff_parse(const char *buf, int *pos)
                    471: {
                    472:        int              j;
                    473:        char             mac[5];
                    474:        enum rofft       t;
                    475:
1.75      kristaps  476:        assert(ROFF_CTL(buf[*pos]));
                    477:        (*pos)++;
1.67      kristaps  478:
                    479:        while (buf[*pos] && (' ' == buf[*pos] || '\t' == buf[*pos]))
                    480:                (*pos)++;
                    481:
                    482:        if ('\0' == buf[*pos])
                    483:                return(ROFF_MAX);
                    484:
                    485:        for (j = 0; j < 4; j++, (*pos)++)
                    486:                if ('\0' == (mac[j] = buf[*pos]))
                    487:                        break;
1.82      kristaps  488:                else if (' ' == buf[*pos] || (j && '\\' == buf[*pos]))
1.67      kristaps  489:                        break;
                    490:
                    491:        if (j == 4 || j < 1)
                    492:                return(ROFF_MAX);
                    493:
                    494:        mac[j] = '\0';
                    495:
                    496:        if (ROFF_MAX == (t = roff_hash_find(mac)))
                    497:                return(t);
                    498:
                    499:        while (buf[*pos] && ' ' == buf[*pos])
                    500:                (*pos)++;
                    501:
                    502:        return(t);
                    503: }
                    504:
                    505:
1.89      kristaps  506: static int
1.91      kristaps  507: roff_parse_nat(const char *buf, unsigned int *res)
1.89      kristaps  508: {
                    509:        char            *ep;
                    510:        long             lval;
                    511:
                    512:        errno = 0;
                    513:        lval = strtol(buf, &ep, 10);
                    514:        if (buf[0] == '\0' || *ep != '\0')
                    515:                return(0);
                    516:        if ((errno == ERANGE &&
                    517:                        (lval == LONG_MAX || lval == LONG_MIN)) ||
1.91      kristaps  518:                        (lval > INT_MAX || lval < 0))
1.89      kristaps  519:                return(0);
                    520:
1.91      kristaps  521:        *res = (unsigned int)lval;
1.89      kristaps  522:        return(1);
                    523: }
                    524:
                    525:
1.67      kristaps  526: /* ARGSUSED */
                    527: static enum rofferr
1.76      kristaps  528: roff_cblock(ROFF_ARGS)
1.67      kristaps  529: {
                    530:
1.79      kristaps  531:        /*
                    532:         * A block-close `..' should only be invoked as a child of an
                    533:         * ignore macro, otherwise raise a warning and just ignore it.
                    534:         */
                    535:
1.76      kristaps  536:        if (NULL == r->last) {
                    537:                if ( ! (*r->msg)(MANDOCERR_NOSCOPE, r->data, ln, ppos, NULL))
                    538:                        return(ROFF_ERR);
                    539:                return(ROFF_IGN);
                    540:        }
1.67      kristaps  541:
1.81      kristaps  542:        switch (r->last->tok) {
                    543:        case (ROFF_am):
                    544:                /* FALLTHROUGH */
                    545:        case (ROFF_ami):
                    546:                /* FALLTHROUGH */
                    547:        case (ROFF_am1):
                    548:                /* FALLTHROUGH */
                    549:        case (ROFF_de):
                    550:                /* FALLTHROUGH */
                    551:        case (ROFF_dei):
                    552:                /* FALLTHROUGH */
                    553:        case (ROFF_de1):
                    554:                /* FALLTHROUGH */
                    555:        case (ROFF_ig):
                    556:                break;
                    557:        default:
1.76      kristaps  558:                if ( ! (*r->msg)(MANDOCERR_NOSCOPE, r->data, ln, ppos, NULL))
                    559:                        return(ROFF_ERR);
1.67      kristaps  560:                return(ROFF_IGN);
1.76      kristaps  561:        }
1.67      kristaps  562:
1.76      kristaps  563:        if ((*bufp)[pos])
                    564:                if ( ! (*r->msg)(MANDOCERR_ARGSLOST, r->data, ln, pos, NULL))
                    565:                        return(ROFF_ERR);
1.71      kristaps  566:
                    567:        roffnode_pop(r);
1.76      kristaps  568:        roffnode_cleanscope(r);
                    569:        return(ROFF_IGN);
1.71      kristaps  570:
1.67      kristaps  571: }
                    572:
                    573:
1.76      kristaps  574: static void
                    575: roffnode_cleanscope(struct roff *r)
1.67      kristaps  576: {
                    577:
1.76      kristaps  578:        while (r->last) {
                    579:                if (--r->last->endspan < 0)
                    580:                        break;
                    581:                roffnode_pop(r);
                    582:        }
1.67      kristaps  583: }
                    584:
                    585:
1.75      kristaps  586: /* ARGSUSED */
1.74      kristaps  587: static enum rofferr
1.75      kristaps  588: roff_ccond(ROFF_ARGS)
1.74      kristaps  589: {
                    590:
1.76      kristaps  591:        if (NULL == r->last) {
                    592:                if ( ! (*r->msg)(MANDOCERR_NOSCOPE, r->data, ln, ppos, NULL))
                    593:                        return(ROFF_ERR);
                    594:                return(ROFF_IGN);
                    595:        }
                    596:
1.82      kristaps  597:        switch (r->last->tok) {
                    598:        case (ROFF_el):
                    599:                /* FALLTHROUGH */
                    600:        case (ROFF_ie):
                    601:                /* FALLTHROUGH */
                    602:        case (ROFF_if):
                    603:                break;
                    604:        default:
1.75      kristaps  605:                if ( ! (*r->msg)(MANDOCERR_NOSCOPE, r->data, ln, ppos, NULL))
                    606:                        return(ROFF_ERR);
                    607:                return(ROFF_IGN);
                    608:        }
                    609:
1.76      kristaps  610:        if (r->last->endspan > -1) {
                    611:                if ( ! (*r->msg)(MANDOCERR_NOSCOPE, r->data, ln, ppos, NULL))
                    612:                        return(ROFF_ERR);
                    613:                return(ROFF_IGN);
                    614:        }
                    615:
                    616:        if ((*bufp)[pos])
                    617:                if ( ! (*r->msg)(MANDOCERR_ARGSLOST, r->data, ln, pos, NULL))
                    618:                        return(ROFF_ERR);
                    619:
1.75      kristaps  620:        roffnode_pop(r);
1.76      kristaps  621:        roffnode_cleanscope(r);
                    622:        return(ROFF_IGN);
                    623: }
                    624:
1.75      kristaps  625:
1.76      kristaps  626: /* ARGSUSED */
                    627: static enum rofferr
1.80      kristaps  628: roff_block(ROFF_ARGS)
1.76      kristaps  629: {
1.78      kristaps  630:        int             sv;
                    631:        size_t          sz;
1.76      kristaps  632:
1.80      kristaps  633:        if (ROFF_ig != tok && '\0' == (*bufp)[pos]) {
                    634:                if ( ! (*r->msg)(MANDOCERR_NOARGS, r->data, ln, ppos, NULL))
                    635:                        return(ROFF_ERR);
                    636:                return(ROFF_IGN);
                    637:        } else if (ROFF_ig != tok) {
                    638:                while ((*bufp)[pos] && ' ' != (*bufp)[pos])
                    639:                        pos++;
                    640:                while (' ' == (*bufp)[pos])
                    641:                        pos++;
                    642:        }
                    643:
1.98    ! schwarze  644:        roffnode_push(r, tok, ln, ppos);
1.76      kristaps  645:
1.79      kristaps  646:        if ('\0' == (*bufp)[pos])
1.78      kristaps  647:                return(ROFF_IGN);
                    648:
                    649:        sv = pos;
                    650:        while ((*bufp)[pos] && ' ' != (*bufp)[pos] &&
                    651:                        '\t' != (*bufp)[pos])
                    652:                pos++;
                    653:
                    654:        /*
                    655:         * Note: groff does NOT like escape characters in the input.
                    656:         * Instead of detecting this, we're just going to let it fly and
                    657:         * to hell with it.
                    658:         */
                    659:
                    660:        assert(pos > sv);
                    661:        sz = (size_t)(pos - sv);
                    662:
1.79      kristaps  663:        if (1 == sz && '.' == (*bufp)[sv])
                    664:                return(ROFF_IGN);
                    665:
1.98    ! schwarze  666:        r->last->end = mandoc_malloc(sz + 1);
1.78      kristaps  667:
                    668:        memcpy(r->last->end, *bufp + sv, sz);
                    669:        r->last->end[(int)sz] = '\0';
                    670:
1.77      kristaps  671:        if ((*bufp)[pos])
                    672:                if ( ! (*r->msg)(MANDOCERR_ARGSLOST, r->data, ln, pos, NULL))
                    673:                        return(ROFF_ERR);
1.74      kristaps  674:
1.78      kristaps  675:        return(ROFF_IGN);
                    676: }
                    677:
                    678:
                    679: /* ARGSUSED */
                    680: static enum rofferr
1.80      kristaps  681: roff_block_sub(ROFF_ARGS)
1.79      kristaps  682: {
                    683:        enum rofft      t;
                    684:        int             i, j;
                    685:
                    686:        /*
                    687:         * First check whether a custom macro exists at this level.  If
                    688:         * it does, then check against it.  This is some of groff's
                    689:         * stranger behaviours.  If we encountered a custom end-scope
                    690:         * tag and that tag also happens to be a "real" macro, then we
                    691:         * need to try interpreting it again as a real macro.  If it's
                    692:         * not, then return ignore.  Else continue.
                    693:         */
                    694:
                    695:        if (r->last->end) {
                    696:                i = pos + 1;
                    697:                while (' ' == (*bufp)[i] || '\t' == (*bufp)[i])
                    698:                        i++;
                    699:
                    700:                for (j = 0; r->last->end[j]; j++, i++)
                    701:                        if ((*bufp)[i] != r->last->end[j])
                    702:                                break;
                    703:
                    704:                if ('\0' == r->last->end[j] &&
                    705:                                ('\0' == (*bufp)[i] ||
                    706:                                 ' ' == (*bufp)[i] ||
                    707:                                 '\t' == (*bufp)[i])) {
                    708:                        roffnode_pop(r);
                    709:                        roffnode_cleanscope(r);
                    710:
                    711:                        if (ROFF_MAX != roff_parse(*bufp, &pos))
                    712:                                return(ROFF_RERUN);
                    713:                        return(ROFF_IGN);
                    714:                }
                    715:        }
                    716:
                    717:        /*
                    718:         * If we have no custom end-query or lookup failed, then try
                    719:         * pulling it out of the hashtable.
                    720:         */
                    721:
                    722:        ppos = pos;
                    723:        t = roff_parse(*bufp, &pos);
                    724:
                    725:        /* If we're not a comment-end, then throw it away. */
                    726:        if (ROFF_cblock != t)
                    727:                return(ROFF_IGN);
                    728:
                    729:        assert(roffs[t].proc);
1.90      kristaps  730:        return((*roffs[t].proc)(r, t, bufp, szp,
                    731:                                ln, ppos, pos, offs));
1.79      kristaps  732: }
                    733:
                    734:
                    735: /* ARGSUSED */
                    736: static enum rofferr
1.80      kristaps  737: roff_block_text(ROFF_ARGS)
1.78      kristaps  738: {
                    739:
                    740:        return(ROFF_IGN);
                    741: }
                    742:
                    743:
                    744: /* ARGSUSED */
                    745: static enum rofferr
1.82      kristaps  746: roff_cond_sub(ROFF_ARGS)
                    747: {
                    748:        enum rofft       t;
                    749:        enum roffrule    rr;
1.87      kristaps  750:        struct roffnode *l;
1.82      kristaps  751:
                    752:        ppos = pos;
                    753:        rr = r->last->rule;
                    754:
1.87      kristaps  755:        /*
                    756:         * Clean out scope.  If we've closed ourselves, then don't
                    757:         * continue.
                    758:         */
                    759:
                    760:        l = r->last;
                    761:        roffnode_cleanscope(r);
                    762:
                    763:        if (l != r->last)
                    764:                return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1.82      kristaps  765:
                    766:        if (ROFF_MAX == (t = roff_parse(*bufp, &pos)))
                    767:                return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
                    768:
                    769:        /*
                    770:         * A denied conditional must evaluate its children if and only
                    771:         * if they're either structurally required (such as loops and
                    772:         * conditionals) or a closing macro.
                    773:         */
                    774:        if (ROFFRULE_DENY == rr)
                    775:                if ( ! (ROFFMAC_STRUCT & roffs[t].flags))
                    776:                        if (ROFF_ccond != t)
                    777:                                return(ROFF_IGN);
                    778:
                    779:        assert(roffs[t].proc);
1.90      kristaps  780:        return((*roffs[t].proc)(r, t, bufp, szp,
                    781:                                ln, ppos, pos, offs));
1.82      kristaps  782: }
                    783:
                    784:
                    785: /* ARGSUSED */
                    786: static enum rofferr
                    787: roff_cond_text(ROFF_ARGS)
1.78      kristaps  788: {
                    789:        char            *ep, *st;
1.82      kristaps  790:        enum roffrule    rr;
                    791:
                    792:        rr = r->last->rule;
                    793:
                    794:        /*
                    795:         * We display the value of the text if out current evaluation
                    796:         * scope permits us to do so.
                    797:         */
1.78      kristaps  798:
                    799:        st = &(*bufp)[pos];
                    800:        if (NULL == (ep = strstr(st, "\\}"))) {
                    801:                roffnode_cleanscope(r);
1.82      kristaps  802:                return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1.78      kristaps  803:        }
                    804:
1.86      kristaps  805:        if (ep == st || (ep > st && '\\' != *(ep - 1)))
1.78      kristaps  806:                roffnode_pop(r);
                    807:
                    808:        roffnode_cleanscope(r);
1.82      kristaps  809:        return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1.74      kristaps  810: }
                    811:
                    812:
1.88      kristaps  813: static enum roffrule
                    814: roff_evalcond(const char *v, int *pos)
                    815: {
                    816:
                    817:        switch (v[*pos]) {
                    818:        case ('n'):
                    819:                (*pos)++;
                    820:                return(ROFFRULE_ALLOW);
                    821:        case ('e'):
                    822:                /* FALLTHROUGH */
                    823:        case ('o'):
                    824:                /* FALLTHROUGH */
                    825:        case ('t'):
                    826:                (*pos)++;
                    827:                return(ROFFRULE_DENY);
                    828:        default:
                    829:                break;
                    830:        }
                    831:
                    832:        while (v[*pos] && ' ' != v[*pos])
                    833:                (*pos)++;
                    834:        return(ROFFRULE_DENY);
                    835: }
                    836:
                    837:
1.75      kristaps  838: /* ARGSUSED */
1.74      kristaps  839: static enum rofferr
1.89      kristaps  840: roff_line(ROFF_ARGS)
                    841: {
                    842:
                    843:        return(ROFF_IGN);
                    844: }
                    845:
                    846:
                    847: /* ARGSUSED */
                    848: static enum rofferr
1.82      kristaps  849: roff_cond(ROFF_ARGS)
1.74      kristaps  850: {
1.77      kristaps  851:        int              sv;
1.88      kristaps  852:        enum roffrule    rule;
1.74      kristaps  853:
1.82      kristaps  854:        /* Stack overflow! */
                    855:
                    856:        if (ROFF_ie == tok && r->rstackpos == RSTACK_MAX - 1) {
                    857:                (*r->msg)(MANDOCERR_MEM, r->data, ln, ppos, NULL);
                    858:                return(ROFF_ERR);
                    859:        }
1.74      kristaps  860:
1.88      kristaps  861:        /* First, evaluate the conditional. */
1.84      schwarze  862:
1.88      kristaps  863:        if (ROFF_el == tok) {
                    864:                /*
                    865:                 * An `.el' will get the value of the current rstack
                    866:                 * entry set in prior `ie' calls or defaults to DENY.
                    867:                 */
                    868:                if (r->rstackpos < 0)
                    869:                        rule = ROFFRULE_DENY;
                    870:                else
                    871:                        rule = r->rstack[r->rstackpos];
                    872:        } else
                    873:                rule = roff_evalcond(*bufp, &pos);
1.77      kristaps  874:
                    875:        sv = pos;
1.88      kristaps  876:
1.75      kristaps  877:        while (' ' == (*bufp)[pos])
                    878:                pos++;
1.74      kristaps  879:
1.77      kristaps  880:        /*
                    881:         * Roff is weird.  If we have just white-space after the
                    882:         * conditional, it's considered the BODY and we exit without
                    883:         * really doing anything.  Warn about this.  It's probably
                    884:         * wrong.
                    885:         */
1.88      kristaps  886:
1.77      kristaps  887:        if ('\0' == (*bufp)[pos] && sv != pos) {
1.88      kristaps  888:                if ((*r->msg)(MANDOCERR_NOARGS, r->data, ln, ppos, NULL))
                    889:                        return(ROFF_IGN);
                    890:                return(ROFF_ERR);
1.77      kristaps  891:        }
                    892:
1.98    ! schwarze  893:        roffnode_push(r, tok, ln, ppos);
1.77      kristaps  894:
1.88      kristaps  895:        r->last->rule = rule;
                    896:
                    897:        ROFF_DEBUG("roff: cond: %s -> %s\n", roffs[tok].name,
                    898:                        ROFFRULE_ALLOW == rule ?  "allow" : "deny");
1.82      kristaps  899:
1.84      schwarze  900:        if (ROFF_ie == tok) {
1.82      kristaps  901:                /*
                    902:                 * An if-else will put the NEGATION of the current
                    903:                 * evaluated conditional into the stack.
                    904:                 */
                    905:                r->rstackpos++;
                    906:                if (ROFFRULE_DENY == r->last->rule)
                    907:                        r->rstack[r->rstackpos] = ROFFRULE_ALLOW;
                    908:                else
                    909:                        r->rstack[r->rstackpos] = ROFFRULE_DENY;
                    910:        }
1.88      kristaps  911:
                    912:        /* If the parent has false as its rule, then so do we. */
                    913:
                    914:        if (r->last->parent && ROFFRULE_DENY == r->last->parent->rule) {
1.84      schwarze  915:                r->last->rule = ROFFRULE_DENY;
1.88      kristaps  916:                ROFF_DEBUG("roff: cond override: %s -> deny\n",
                    917:                                roffs[tok].name);
                    918:        }
                    919:
                    920:        /*
                    921:         * Determine scope.  If we're invoked with "\{" trailing the
                    922:         * conditional, then we're in a multiline scope.  Else our scope
                    923:         * expires on the next line.
                    924:         */
1.74      kristaps  925:
1.75      kristaps  926:        r->last->endspan = 1;
                    927:
                    928:        if ('\\' == (*bufp)[pos] && '{' == (*bufp)[pos + 1]) {
                    929:                r->last->endspan = -1;
                    930:                pos += 2;
1.88      kristaps  931:                ROFF_DEBUG("roff: cond-scope: %s, multi-line\n",
                    932:                                roffs[tok].name);
                    933:        } else
                    934:                ROFF_DEBUG("roff: cond-scope: %s, one-line\n",
                    935:                                roffs[tok].name);
1.74      kristaps  936:
1.77      kristaps  937:        /*
                    938:         * If there are no arguments on the line, the next-line scope is
                    939:         * assumed.
                    940:         */
                    941:
1.75      kristaps  942:        if ('\0' == (*bufp)[pos])
                    943:                return(ROFF_IGN);
1.77      kristaps  944:
                    945:        /* Otherwise re-run the roff parser after recalculating. */
1.74      kristaps  946:
1.75      kristaps  947:        *offs = pos;
                    948:        return(ROFF_RERUN);
1.83      schwarze  949: }
                    950:
                    951:
                    952: /* ARGSUSED */
                    953: static enum rofferr
1.92      schwarze  954: roff_ds(ROFF_ARGS)
                    955: {
1.96      kristaps  956:        char            *name, *string;
                    957:
                    958:        /*
                    959:         * A symbol is named by the first word following the macro
                    960:         * invocation up to a space.  Its value is anything after the
                    961:         * name's trailing whitespace and optional double-quote.  Thus,
                    962:         *
                    963:         *  [.ds foo "bar  "     ]
                    964:         *
                    965:         * will have `bar  "     ' as its value.
                    966:         */
1.92      schwarze  967:
                    968:        name = *bufp + pos;
                    969:        if ('\0' == *name)
                    970:                return(ROFF_IGN);
                    971:
                    972:        string = name;
1.96      kristaps  973:        /* Read until end of name. */
1.92      schwarze  974:        while (*string && ' ' != *string)
                    975:                string++;
1.96      kristaps  976:
                    977:        /* Nil-terminate name. */
1.92      schwarze  978:        if (*string)
1.96      kristaps  979:                *(string++) = '\0';
                    980:
                    981:        /* Read past spaces. */
                    982:        while (*string && ' ' == *string)
                    983:                string++;
                    984:
                    985:        /* Read passed initial double-quote. */
1.92      schwarze  986:        if (*string && '"' == *string)
                    987:                string++;
                    988:
1.96      kristaps  989:        /* The rest is the value. */
1.94      kristaps  990:        roff_setstr(r, name, string);
1.92      schwarze  991:        return(ROFF_IGN);
                    992: }
                    993:
                    994:
                    995: /* ARGSUSED */
                    996: static enum rofferr
1.89      kristaps  997: roff_nr(ROFF_ARGS)
1.83      schwarze  998: {
1.89      kristaps  999:        const char      *key, *val;
1.91      kristaps 1000:        struct reg      *rg;
1.89      kristaps 1001:
                   1002:        key = &(*bufp)[pos];
1.91      kristaps 1003:        rg = r->regs->regs;
1.89      kristaps 1004:
                   1005:        /* Parse register request. */
                   1006:        while ((*bufp)[pos] && ' ' != (*bufp)[pos])
                   1007:                pos++;
                   1008:
                   1009:        /*
                   1010:         * Set our nil terminator.  Because this line is going to be
                   1011:         * ignored anyway, we can munge it as we please.
                   1012:         */
                   1013:        if ((*bufp)[pos])
                   1014:                (*bufp)[pos++] = '\0';
                   1015:
                   1016:        /* Skip whitespace to register token. */
                   1017:        while ((*bufp)[pos] && ' ' == (*bufp)[pos])
                   1018:                pos++;
                   1019:
                   1020:        val = &(*bufp)[pos];
                   1021:
                   1022:        /* Process register token. */
                   1023:
                   1024:        if (0 == strcmp(key, "nS")) {
1.91      kristaps 1025:                rg[(int)REG_nS].set = 1;
                   1026:                if ( ! roff_parse_nat(val, &rg[(int)REG_nS].v.u))
                   1027:                        rg[(int)REG_nS].v.u = 0;
1.89      kristaps 1028:
1.91      kristaps 1029:                ROFF_DEBUG("roff: register nS: %u\n",
                   1030:                                rg[(int)REG_nS].v.u);
1.89      kristaps 1031:        } else
                   1032:                ROFF_DEBUG("roff: ignoring register: %s\n", key);
1.83      schwarze 1033:
                   1034:        return(ROFF_IGN);
1.92      schwarze 1035: }
                   1036:
                   1037:
1.94      kristaps 1038: static void
                   1039: roff_setstr(struct roff *r, const char *name, const char *string)
1.92      schwarze 1040: {
                   1041:        struct roffstr   *n;
                   1042:        char             *namecopy;
                   1043:
1.94      kristaps 1044:        n = r->first_string;
1.92      schwarze 1045:        while (n && strcmp(name, n->name))
                   1046:                n = n->next;
1.94      kristaps 1047:
                   1048:        if (NULL == n) {
                   1049:                namecopy = mandoc_strdup(name);
                   1050:                n = mandoc_malloc(sizeof(struct roffstr));
                   1051:                n->name = namecopy;
                   1052:                n->next = r->first_string;
                   1053:                r->first_string = n;
                   1054:        } else
1.92      schwarze 1055:                free(n->string);
1.94      kristaps 1056:
1.96      kristaps 1057:        ROFF_DEBUG("roff: new symbol: [%s] = [%s]\n", name, string);
1.94      kristaps 1058:        n->string = string ? strdup(string) : NULL;
1.92      schwarze 1059: }
                   1060:
                   1061:
1.94      kristaps 1062: static const char *
                   1063: roff_getstrn(const struct roff *r, const char *name, size_t len)
1.92      schwarze 1064: {
1.94      kristaps 1065:        const struct roffstr *n;
1.92      schwarze 1066:
1.94      kristaps 1067:        n = r->first_string;
1.97      kristaps 1068:        while (n && (strncmp(name, n->name, len) || '\0' != n->name[(int)len]))
1.92      schwarze 1069:                n = n->next;
1.94      kristaps 1070:
                   1071:        return(n ? n->string : NULL);
1.92      schwarze 1072: }
                   1073:
1.94      kristaps 1074:
                   1075: static void
                   1076: roff_freestr(struct roff *r)
1.92      schwarze 1077: {
                   1078:        struct roffstr   *n, *nn;
                   1079:
1.94      kristaps 1080:        for (n = r->first_string; n; n = nn) {
1.92      schwarze 1081:                free(n->name);
                   1082:                free(n->string);
                   1083:                nn = n->next;
                   1084:                free(n);
                   1085:        }
1.94      kristaps 1086:
                   1087:        r->first_string = NULL;
1.74      kristaps 1088: }

CVSweb