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

Annotation of mandoc/roff.c, Revision 1.88

1.88    ! kristaps    1: /*     $Id: roff.c,v 1.87 2010/06/09 20:00:38 kristaps Exp $ */
1.1       kristaps    2: /*
1.67      kristaps    3:  * Copyright (c) 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.66      kristaps    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.66      kristaps    9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   16:  */
1.66      kristaps   17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
1.30      kristaps   20:
1.67      kristaps   21: #include <assert.h>
1.85      kristaps   22: #include <ctype.h>
1.1       kristaps   23: #include <stdlib.h>
1.67      kristaps   24: #include <string.h>
1.75      kristaps   25: #include <stdio.h>
1.1       kristaps   26:
1.67      kristaps   27: #include "mandoc.h"
1.43      kristaps   28: #include "roff.h"
1.33      kristaps   29:
1.82      kristaps   30: #define        RSTACK_MAX      128
                     31:
1.75      kristaps   32: #define        ROFF_CTL(c) \
                     33:        ('.' == (c) || '\'' == (c))
                     34:
1.88    ! kristaps   35: #if 1
        !            36: #define        ROFF_DEBUG(fmt, args...) \
        !            37:        do { /* Nothing. */ } while (/*CONSTCOND*/ 0)
        !            38: #else
        !            39: #define        ROFF_DEBUG(fmt, args...) \
        !            40:        do { fprintf(stderr, fmt , ##args); } while (/*CONSTCOND*/ 0)
        !            41: #endif
        !            42:
1.67      kristaps   43: enum   rofft {
1.80      kristaps   44:        ROFF_am,
                     45:        ROFF_ami,
                     46:        ROFF_am1,
                     47:        ROFF_de,
                     48:        ROFF_dei,
                     49:        ROFF_de1,
1.83      schwarze   50:        ROFF_ds,
1.82      kristaps   51:        ROFF_el,
                     52:        ROFF_ie,
1.75      kristaps   53:        ROFF_if,
1.76      kristaps   54:        ROFF_ig,
1.83      schwarze   55:        ROFF_rm,
                     56:        ROFF_tr,
1.76      kristaps   57:        ROFF_cblock,
1.75      kristaps   58:        ROFF_ccond,
1.67      kristaps   59:        ROFF_MAX
                     60: };
                     61:
1.82      kristaps   62: enum   roffrule {
                     63:        ROFFRULE_ALLOW,
                     64:        ROFFRULE_DENY
                     65: };
                     66:
1.67      kristaps   67: struct roff {
                     68:        struct roffnode *last; /* leaf of stack */
                     69:        mandocmsg        msg; /* err/warn/fatal messages */
                     70:        void            *data; /* privdata for messages */
1.82      kristaps   71:        enum roffrule    rstack[RSTACK_MAX]; /* stack of !`ie' rules */
                     72:        int              rstackpos; /* position in rstack */
1.79      kristaps   73: };
                     74:
1.67      kristaps   75: struct roffnode {
                     76:        enum rofft       tok; /* type of node */
                     77:        struct roffnode *parent; /* up one in stack */
                     78:        int              line; /* parse line */
                     79:        int              col; /* parse col */
1.79      kristaps   80:        char            *end; /* end-rules: custom token */
                     81:        int              endspan; /* end-rules: next-line or infty */
1.82      kristaps   82:        enum roffrule    rule; /* current evaluation rule */
1.67      kristaps   83: };
                     84:
                     85: #define        ROFF_ARGS        struct roff *r, /* parse ctx */ \
1.72      kristaps   86:                         enum rofft tok, /* tok of macro */ \
1.67      kristaps   87:                         char **bufp, /* input buffer */ \
                     88:                         size_t *szp, /* size of input buffer */ \
                     89:                         int ln, /* parse line */ \
1.75      kristaps   90:                         int ppos, /* original pos in buffer */ \
                     91:                         int pos, /* current pos in buffer */ \
1.74      kristaps   92:                         int *offs /* reset offset of buffer data */
1.67      kristaps   93:
                     94: typedef        enum rofferr (*roffproc)(ROFF_ARGS);
                     95:
                     96: struct roffmac {
                     97:        const char      *name; /* macro name */
1.79      kristaps   98:        roffproc         proc; /* process new macro */
                     99:        roffproc         text; /* process as child text of macro */
                    100:        roffproc         sub; /* process as child of macro */
                    101:        int              flags;
                    102: #define        ROFFMAC_STRUCT  (1 << 0) /* always interpret */
1.85      kristaps  103:        struct roffmac  *next;
1.67      kristaps  104: };
                    105:
1.80      kristaps  106: static enum rofferr     roff_block(ROFF_ARGS);
                    107: static enum rofferr     roff_block_text(ROFF_ARGS);
                    108: static enum rofferr     roff_block_sub(ROFF_ARGS);
                    109: static enum rofferr     roff_cblock(ROFF_ARGS);
                    110: static enum rofferr     roff_ccond(ROFF_ARGS);
1.82      kristaps  111: static enum rofferr     roff_cond(ROFF_ARGS);
                    112: static enum rofferr     roff_cond_text(ROFF_ARGS);
                    113: static enum rofferr     roff_cond_sub(ROFF_ARGS);
1.88    ! kristaps  114: static enum roffrule    roff_evalcond(const char *, int *);
1.83      schwarze  115: static enum rofferr     roff_line(ROFF_ARGS);
1.67      kristaps  116:
1.85      kristaps  117: /* See roff_hash_find() */
                    118:
                    119: #define        ASCII_HI         126
                    120: #define        ASCII_LO         33
                    121: #define        HASHWIDTH       (ASCII_HI - ASCII_LO + 1)
                    122:
                    123: static struct roffmac  *hash[HASHWIDTH];
                    124:
                    125: static struct roffmac   roffs[ROFF_MAX] = {
                    126:        { "am", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    127:        { "ami", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    128:        { "am1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    129:        { "de", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    130:        { "dei", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    131:        { "de1", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    132:        { "ds", roff_line, NULL, NULL, 0, NULL },
                    133:        { "el", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
                    134:        { "ie", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
                    135:        { "if", roff_cond, roff_cond_text, roff_cond_sub, ROFFMAC_STRUCT, NULL },
                    136:        { "ig", roff_block, roff_block_text, roff_block_sub, 0, NULL },
                    137:        { "rm", roff_line, NULL, NULL, 0, NULL },
                    138:        { "tr", roff_line, NULL, NULL, 0, NULL },
                    139:        { ".", roff_cblock, NULL, NULL, 0, NULL },
                    140:        { "\\}", roff_ccond, NULL, NULL, 0, NULL },
1.67      kristaps  141: };
                    142:
                    143: static void             roff_free1(struct roff *);
                    144: static enum rofft       roff_hash_find(const char *);
1.85      kristaps  145: static void             roff_hash_init(void);
1.76      kristaps  146: static void             roffnode_cleanscope(struct roff *);
1.67      kristaps  147: static int              roffnode_push(struct roff *,
                    148:                                enum rofft, int, int);
                    149: static void             roffnode_pop(struct roff *);
                    150: static enum rofft       roff_parse(const char *, int *);
                    151:
1.85      kristaps  152: /* See roff_hash_find() */
                    153: #define        ROFF_HASH(p)    (p[0] - ASCII_LO)
                    154:
                    155: static void
                    156: roff_hash_init(void)
                    157: {
                    158:        struct roffmac   *n;
                    159:        int               buc, i;
                    160:
                    161:        for (i = 0; i < (int)ROFF_MAX; i++) {
                    162:                assert(roffs[i].name[0] >= ASCII_LO);
                    163:                assert(roffs[i].name[0] <= ASCII_HI);
                    164:
                    165:                buc = ROFF_HASH(roffs[i].name);
                    166:
                    167:                if (NULL != (n = hash[buc])) {
                    168:                        for ( ; n->next; n = n->next)
                    169:                                /* Do nothing. */ ;
                    170:                        n->next = &roffs[i];
                    171:                } else
                    172:                        hash[buc] = &roffs[i];
                    173:        }
                    174: }
                    175:
1.67      kristaps  176:
                    177: /*
                    178:  * Look up a roff token by its name.  Returns ROFF_MAX if no macro by
                    179:  * the nil-terminated string name could be found.
                    180:  */
                    181: static enum rofft
                    182: roff_hash_find(const char *p)
                    183: {
1.85      kristaps  184:        int              buc;
                    185:        struct roffmac  *n;
1.67      kristaps  186:
1.85      kristaps  187:        /*
                    188:         * libroff has an extremely simple hashtable, for the time
                    189:         * being, which simply keys on the first character, which must
                    190:         * be printable, then walks a chain.  It works well enough until
                    191:         * optimised.
                    192:         */
                    193:
                    194:        if (p[0] < ASCII_LO || p[0] > ASCII_HI)
                    195:                return(ROFF_MAX);
                    196:
                    197:        buc = ROFF_HASH(p);
                    198:
                    199:        if (NULL == (n = hash[buc]))
                    200:                return(ROFF_MAX);
                    201:        for ( ; n; n = n->next)
                    202:                if (0 == strcmp(n->name, p))
                    203:                        return((enum rofft)(n - roffs));
1.67      kristaps  204:
                    205:        return(ROFF_MAX);
                    206: }
                    207:
                    208:
                    209: /*
                    210:  * Pop the current node off of the stack of roff instructions currently
                    211:  * pending.
                    212:  */
                    213: static void
                    214: roffnode_pop(struct roff *r)
                    215: {
                    216:        struct roffnode *p;
                    217:
1.75      kristaps  218:        assert(r->last);
                    219:        p = r->last;
1.82      kristaps  220:
                    221:        if (ROFF_el == p->tok)
                    222:                if (r->rstackpos > -1)
                    223:                        r->rstackpos--;
                    224:
1.75      kristaps  225:        r->last = r->last->parent;
1.74      kristaps  226:        if (p->end)
                    227:                free(p->end);
1.67      kristaps  228:        free(p);
                    229: }
                    230:
                    231:
                    232: /*
                    233:  * Push a roff node onto the instruction stack.  This must later be
                    234:  * removed with roffnode_pop().
                    235:  */
                    236: static int
                    237: roffnode_push(struct roff *r, enum rofft tok, int line, int col)
                    238: {
                    239:        struct roffnode *p;
                    240:
                    241:        if (NULL == (p = calloc(1, sizeof(struct roffnode)))) {
                    242:                (*r->msg)(MANDOCERR_MEM, r->data, line, col, NULL);
                    243:                return(0);
                    244:        }
                    245:
                    246:        p->tok = tok;
                    247:        p->parent = r->last;
                    248:        p->line = line;
                    249:        p->col = col;
1.79      kristaps  250:        p->rule = p->parent ? p->parent->rule : ROFFRULE_DENY;
1.67      kristaps  251:
                    252:        r->last = p;
                    253:        return(1);
                    254: }
                    255:
                    256:
                    257: static void
                    258: roff_free1(struct roff *r)
                    259: {
                    260:
                    261:        while (r->last)
                    262:                roffnode_pop(r);
                    263: }
                    264:
                    265:
                    266: void
                    267: roff_reset(struct roff *r)
                    268: {
                    269:
                    270:        roff_free1(r);
                    271: }
                    272:
                    273:
                    274: void
                    275: roff_free(struct roff *r)
                    276: {
                    277:
                    278:        roff_free1(r);
                    279:        free(r);
                    280: }
                    281:
                    282:
                    283: struct roff *
                    284: roff_alloc(const mandocmsg msg, void *data)
                    285: {
                    286:        struct roff     *r;
                    287:
                    288:        if (NULL == (r = calloc(1, sizeof(struct roff)))) {
                    289:                (*msg)(MANDOCERR_MEM, data, 0, 0, NULL);
                    290:                return(0);
                    291:        }
                    292:
                    293:        r->msg = msg;
                    294:        r->data = data;
1.82      kristaps  295:        r->rstackpos = -1;
1.85      kristaps  296:
                    297:        roff_hash_init();
1.67      kristaps  298:        return(r);
                    299: }
                    300:
                    301:
                    302: enum rofferr
1.74      kristaps  303: roff_parseln(struct roff *r, int ln,
                    304:                char **bufp, size_t *szp, int pos, int *offs)
1.67      kristaps  305: {
                    306:        enum rofft       t;
1.79      kristaps  307:        int              ppos;
                    308:
                    309:        /*
                    310:         * First, if a scope is open and we're not a macro, pass the
                    311:         * text through the macro's filter.  If a scope isn't open and
                    312:         * we're not a macro, just let it through.
                    313:         */
1.74      kristaps  314:
1.75      kristaps  315:        if (r->last && ! ROFF_CTL((*bufp)[pos])) {
1.78      kristaps  316:                t = r->last->tok;
                    317:                assert(roffs[t].text);
1.88    ! kristaps  318:                ROFF_DEBUG("roff: intercept scoped text: %s, [%s]\n",
        !           319:                                roffs[t].name, &(*bufp)[pos]);
1.78      kristaps  320:                return((*roffs[t].text)
                    321:                                (r, t, bufp, szp, ln, pos, pos, offs));
1.88    ! kristaps  322:        } else if ( ! ROFF_CTL((*bufp)[pos])) {
        !           323:                ROFF_DEBUG("roff: pass non-scoped text: [%s]\n",
        !           324:                                &(*bufp)[pos]);
1.67      kristaps  325:                return(ROFF_CONT);
1.88    ! kristaps  326:        }
1.67      kristaps  327:
1.79      kristaps  328:        /*
                    329:         * If a scope is open, go to the child handler for that macro,
                    330:         * as it may want to preprocess before doing anything with it.
                    331:         */
1.78      kristaps  332:
1.79      kristaps  333:        if (r->last) {
                    334:                t = r->last->tok;
                    335:                assert(roffs[t].sub);
1.88    ! kristaps  336:                ROFF_DEBUG("roff: intercept scoped context: %s\n",
        !           337:                                roffs[t].name);
1.79      kristaps  338:                return((*roffs[t].sub)
                    339:                                (r, t, bufp, szp, ln, pos, pos, offs));
                    340:        }
1.78      kristaps  341:
1.79      kristaps  342:        /*
                    343:         * Lastly, as we've no scope open, try to look up and execute
                    344:         * the new macro.  If no macro is found, simply return and let
                    345:         * the compilers handle it.
                    346:         */
1.67      kristaps  347:
1.75      kristaps  348:        ppos = pos;
1.88    ! kristaps  349:        if (ROFF_MAX == (t = roff_parse(*bufp, &pos))) {
        !           350:                ROFF_DEBUG("roff: pass non-scoped non-macro: [%s]\n",
        !           351:                                &(*bufp)[pos]);
1.79      kristaps  352:                return(ROFF_CONT);
1.88    ! kristaps  353:        }
1.67      kristaps  354:
1.88    ! kristaps  355:        ROFF_DEBUG("roff: intercept new-scope: %s, [%s]\n",
        !           356:                        roffs[t].name, &(*bufp)[pos]);
1.75      kristaps  357:        assert(roffs[t].proc);
1.78      kristaps  358:        return((*roffs[t].proc)
                    359:                        (r, t, bufp, szp, ln, ppos, pos, offs));
1.74      kristaps  360: }
                    361:
                    362:
                    363: int
                    364: roff_endparse(struct roff *r)
                    365: {
                    366:
                    367:        if (NULL == r->last)
                    368:                return(1);
                    369:        return((*r->msg)(MANDOCERR_SCOPEEXIT, r->data, r->last->line,
                    370:                                r->last->col, NULL));
1.67      kristaps  371: }
                    372:
                    373:
                    374: /*
                    375:  * Parse a roff node's type from the input buffer.  This must be in the
                    376:  * form of ".foo xxx" in the usual way.
                    377:  */
                    378: static enum rofft
                    379: roff_parse(const char *buf, int *pos)
                    380: {
                    381:        int              j;
                    382:        char             mac[5];
                    383:        enum rofft       t;
                    384:
1.75      kristaps  385:        assert(ROFF_CTL(buf[*pos]));
                    386:        (*pos)++;
1.67      kristaps  387:
                    388:        while (buf[*pos] && (' ' == buf[*pos] || '\t' == buf[*pos]))
                    389:                (*pos)++;
                    390:
                    391:        if ('\0' == buf[*pos])
                    392:                return(ROFF_MAX);
                    393:
                    394:        for (j = 0; j < 4; j++, (*pos)++)
                    395:                if ('\0' == (mac[j] = buf[*pos]))
                    396:                        break;
1.82      kristaps  397:                else if (' ' == buf[*pos] || (j && '\\' == buf[*pos]))
1.67      kristaps  398:                        break;
                    399:
                    400:        if (j == 4 || j < 1)
                    401:                return(ROFF_MAX);
                    402:
                    403:        mac[j] = '\0';
                    404:
                    405:        if (ROFF_MAX == (t = roff_hash_find(mac)))
                    406:                return(t);
                    407:
                    408:        while (buf[*pos] && ' ' == buf[*pos])
                    409:                (*pos)++;
                    410:
                    411:        return(t);
                    412: }
                    413:
                    414:
                    415: /* ARGSUSED */
                    416: static enum rofferr
1.76      kristaps  417: roff_cblock(ROFF_ARGS)
1.67      kristaps  418: {
                    419:
1.79      kristaps  420:        /*
                    421:         * A block-close `..' should only be invoked as a child of an
                    422:         * ignore macro, otherwise raise a warning and just ignore it.
                    423:         */
                    424:
1.76      kristaps  425:        if (NULL == r->last) {
                    426:                if ( ! (*r->msg)(MANDOCERR_NOSCOPE, r->data, ln, ppos, NULL))
                    427:                        return(ROFF_ERR);
                    428:                return(ROFF_IGN);
                    429:        }
1.67      kristaps  430:
1.81      kristaps  431:        switch (r->last->tok) {
                    432:        case (ROFF_am):
                    433:                /* FALLTHROUGH */
                    434:        case (ROFF_ami):
                    435:                /* FALLTHROUGH */
                    436:        case (ROFF_am1):
                    437:                /* FALLTHROUGH */
                    438:        case (ROFF_de):
                    439:                /* FALLTHROUGH */
                    440:        case (ROFF_dei):
                    441:                /* FALLTHROUGH */
                    442:        case (ROFF_de1):
                    443:                /* FALLTHROUGH */
                    444:        case (ROFF_ig):
                    445:                break;
                    446:        default:
1.76      kristaps  447:                if ( ! (*r->msg)(MANDOCERR_NOSCOPE, r->data, ln, ppos, NULL))
                    448:                        return(ROFF_ERR);
1.67      kristaps  449:                return(ROFF_IGN);
1.76      kristaps  450:        }
1.67      kristaps  451:
1.76      kristaps  452:        if ((*bufp)[pos])
                    453:                if ( ! (*r->msg)(MANDOCERR_ARGSLOST, r->data, ln, pos, NULL))
                    454:                        return(ROFF_ERR);
1.71      kristaps  455:
                    456:        roffnode_pop(r);
1.76      kristaps  457:        roffnode_cleanscope(r);
                    458:        return(ROFF_IGN);
1.71      kristaps  459:
1.67      kristaps  460: }
                    461:
                    462:
1.76      kristaps  463: static void
                    464: roffnode_cleanscope(struct roff *r)
1.67      kristaps  465: {
                    466:
1.76      kristaps  467:        while (r->last) {
                    468:                if (--r->last->endspan < 0)
                    469:                        break;
                    470:                roffnode_pop(r);
                    471:        }
1.67      kristaps  472: }
                    473:
                    474:
1.75      kristaps  475: /* ARGSUSED */
1.74      kristaps  476: static enum rofferr
1.75      kristaps  477: roff_ccond(ROFF_ARGS)
1.74      kristaps  478: {
                    479:
1.76      kristaps  480:        if (NULL == r->last) {
                    481:                if ( ! (*r->msg)(MANDOCERR_NOSCOPE, r->data, ln, ppos, NULL))
                    482:                        return(ROFF_ERR);
                    483:                return(ROFF_IGN);
                    484:        }
                    485:
1.82      kristaps  486:        switch (r->last->tok) {
                    487:        case (ROFF_el):
                    488:                /* FALLTHROUGH */
                    489:        case (ROFF_ie):
                    490:                /* FALLTHROUGH */
                    491:        case (ROFF_if):
                    492:                break;
                    493:        default:
1.75      kristaps  494:                if ( ! (*r->msg)(MANDOCERR_NOSCOPE, r->data, ln, ppos, NULL))
                    495:                        return(ROFF_ERR);
                    496:                return(ROFF_IGN);
                    497:        }
                    498:
1.76      kristaps  499:        if (r->last->endspan > -1) {
                    500:                if ( ! (*r->msg)(MANDOCERR_NOSCOPE, r->data, ln, ppos, NULL))
                    501:                        return(ROFF_ERR);
                    502:                return(ROFF_IGN);
                    503:        }
                    504:
                    505:        if ((*bufp)[pos])
                    506:                if ( ! (*r->msg)(MANDOCERR_ARGSLOST, r->data, ln, pos, NULL))
                    507:                        return(ROFF_ERR);
                    508:
1.75      kristaps  509:        roffnode_pop(r);
1.76      kristaps  510:        roffnode_cleanscope(r);
                    511:        return(ROFF_IGN);
                    512: }
                    513:
1.75      kristaps  514:
1.76      kristaps  515: /* ARGSUSED */
                    516: static enum rofferr
1.80      kristaps  517: roff_block(ROFF_ARGS)
1.76      kristaps  518: {
1.78      kristaps  519:        int             sv;
                    520:        size_t          sz;
1.76      kristaps  521:
1.80      kristaps  522:        if (ROFF_ig != tok && '\0' == (*bufp)[pos]) {
                    523:                if ( ! (*r->msg)(MANDOCERR_NOARGS, r->data, ln, ppos, NULL))
                    524:                        return(ROFF_ERR);
                    525:                return(ROFF_IGN);
                    526:        } else if (ROFF_ig != tok) {
                    527:                while ((*bufp)[pos] && ' ' != (*bufp)[pos])
                    528:                        pos++;
                    529:                while (' ' == (*bufp)[pos])
                    530:                        pos++;
                    531:        }
                    532:
1.76      kristaps  533:        if ( ! roffnode_push(r, tok, ln, ppos))
                    534:                return(ROFF_ERR);
                    535:
1.79      kristaps  536:        if ('\0' == (*bufp)[pos])
1.78      kristaps  537:                return(ROFF_IGN);
                    538:
                    539:        sv = pos;
                    540:        while ((*bufp)[pos] && ' ' != (*bufp)[pos] &&
                    541:                        '\t' != (*bufp)[pos])
                    542:                pos++;
                    543:
                    544:        /*
                    545:         * Note: groff does NOT like escape characters in the input.
                    546:         * Instead of detecting this, we're just going to let it fly and
                    547:         * to hell with it.
                    548:         */
                    549:
                    550:        assert(pos > sv);
                    551:        sz = (size_t)(pos - sv);
                    552:
1.79      kristaps  553:        if (1 == sz && '.' == (*bufp)[sv])
                    554:                return(ROFF_IGN);
                    555:
1.78      kristaps  556:        r->last->end = malloc(sz + 1);
                    557:
                    558:        if (NULL == r->last->end) {
                    559:                (*r->msg)(MANDOCERR_MEM, r->data, ln, pos, NULL);
                    560:                return(ROFF_ERR);
                    561:        }
                    562:
                    563:        memcpy(r->last->end, *bufp + sv, sz);
                    564:        r->last->end[(int)sz] = '\0';
                    565:
1.77      kristaps  566:        if ((*bufp)[pos])
                    567:                if ( ! (*r->msg)(MANDOCERR_ARGSLOST, r->data, ln, pos, NULL))
                    568:                        return(ROFF_ERR);
1.74      kristaps  569:
1.78      kristaps  570:        return(ROFF_IGN);
                    571: }
                    572:
                    573:
                    574: /* ARGSUSED */
                    575: static enum rofferr
1.80      kristaps  576: roff_block_sub(ROFF_ARGS)
1.79      kristaps  577: {
                    578:        enum rofft      t;
                    579:        int             i, j;
                    580:
                    581:        /*
                    582:         * First check whether a custom macro exists at this level.  If
                    583:         * it does, then check against it.  This is some of groff's
                    584:         * stranger behaviours.  If we encountered a custom end-scope
                    585:         * tag and that tag also happens to be a "real" macro, then we
                    586:         * need to try interpreting it again as a real macro.  If it's
                    587:         * not, then return ignore.  Else continue.
                    588:         */
                    589:
                    590:        if (r->last->end) {
                    591:                i = pos + 1;
                    592:                while (' ' == (*bufp)[i] || '\t' == (*bufp)[i])
                    593:                        i++;
                    594:
                    595:                for (j = 0; r->last->end[j]; j++, i++)
                    596:                        if ((*bufp)[i] != r->last->end[j])
                    597:                                break;
                    598:
                    599:                if ('\0' == r->last->end[j] &&
                    600:                                ('\0' == (*bufp)[i] ||
                    601:                                 ' ' == (*bufp)[i] ||
                    602:                                 '\t' == (*bufp)[i])) {
                    603:                        roffnode_pop(r);
                    604:                        roffnode_cleanscope(r);
                    605:
                    606:                        if (ROFF_MAX != roff_parse(*bufp, &pos))
                    607:                                return(ROFF_RERUN);
                    608:                        return(ROFF_IGN);
                    609:                }
                    610:        }
                    611:
                    612:        /*
                    613:         * If we have no custom end-query or lookup failed, then try
                    614:         * pulling it out of the hashtable.
                    615:         */
                    616:
                    617:        ppos = pos;
                    618:        t = roff_parse(*bufp, &pos);
                    619:
                    620:        /* If we're not a comment-end, then throw it away. */
                    621:        if (ROFF_cblock != t)
                    622:                return(ROFF_IGN);
                    623:
                    624:        assert(roffs[t].proc);
                    625:        return((*roffs[t].proc)(r, t, bufp,
                    626:                        szp, ln, ppos, pos, offs));
                    627: }
                    628:
                    629:
                    630: /* ARGSUSED */
                    631: static enum rofferr
1.80      kristaps  632: roff_block_text(ROFF_ARGS)
1.78      kristaps  633: {
                    634:
                    635:        return(ROFF_IGN);
                    636: }
                    637:
                    638:
                    639: /* ARGSUSED */
                    640: static enum rofferr
1.82      kristaps  641: roff_cond_sub(ROFF_ARGS)
                    642: {
                    643:        enum rofft       t;
                    644:        enum roffrule    rr;
1.87      kristaps  645:        struct roffnode *l;
1.82      kristaps  646:
                    647:        ppos = pos;
                    648:        rr = r->last->rule;
                    649:
1.87      kristaps  650:        /*
                    651:         * Clean out scope.  If we've closed ourselves, then don't
                    652:         * continue.
                    653:         */
                    654:
                    655:        l = r->last;
                    656:        roffnode_cleanscope(r);
                    657:
                    658:        if (l != r->last)
                    659:                return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1.82      kristaps  660:
                    661:        if (ROFF_MAX == (t = roff_parse(*bufp, &pos)))
                    662:                return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
                    663:
                    664:        /*
                    665:         * A denied conditional must evaluate its children if and only
                    666:         * if they're either structurally required (such as loops and
                    667:         * conditionals) or a closing macro.
                    668:         */
                    669:        if (ROFFRULE_DENY == rr)
                    670:                if ( ! (ROFFMAC_STRUCT & roffs[t].flags))
                    671:                        if (ROFF_ccond != t)
                    672:                                return(ROFF_IGN);
                    673:
                    674:        assert(roffs[t].proc);
                    675:        return((*roffs[t].proc)
                    676:                        (r, t, bufp, szp, ln, ppos, pos, offs));
                    677: }
                    678:
                    679:
                    680: /* ARGSUSED */
                    681: static enum rofferr
                    682: roff_cond_text(ROFF_ARGS)
1.78      kristaps  683: {
                    684:        char            *ep, *st;
1.82      kristaps  685:        enum roffrule    rr;
                    686:
                    687:        rr = r->last->rule;
                    688:
                    689:        /*
                    690:         * We display the value of the text if out current evaluation
                    691:         * scope permits us to do so.
                    692:         */
1.78      kristaps  693:
                    694:        st = &(*bufp)[pos];
                    695:        if (NULL == (ep = strstr(st, "\\}"))) {
                    696:                roffnode_cleanscope(r);
1.82      kristaps  697:                return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1.78      kristaps  698:        }
                    699:
1.86      kristaps  700:        if (ep == st || (ep > st && '\\' != *(ep - 1)))
1.78      kristaps  701:                roffnode_pop(r);
                    702:
                    703:        roffnode_cleanscope(r);
1.82      kristaps  704:        return(ROFFRULE_DENY == rr ? ROFF_IGN : ROFF_CONT);
1.74      kristaps  705: }
                    706:
                    707:
1.88    ! kristaps  708: static enum roffrule
        !           709: roff_evalcond(const char *v, int *pos)
        !           710: {
        !           711:
        !           712:        switch (v[*pos]) {
        !           713:        case ('n'):
        !           714:                (*pos)++;
        !           715:                return(ROFFRULE_ALLOW);
        !           716:        case ('e'):
        !           717:                /* FALLTHROUGH */
        !           718:        case ('o'):
        !           719:                /* FALLTHROUGH */
        !           720:        case ('t'):
        !           721:                (*pos)++;
        !           722:                return(ROFFRULE_DENY);
        !           723:        default:
        !           724:                break;
        !           725:        }
        !           726:
        !           727:        while (v[*pos] && ' ' != v[*pos])
        !           728:                (*pos)++;
        !           729:        return(ROFFRULE_DENY);
        !           730: }
        !           731:
        !           732:
1.75      kristaps  733: /* ARGSUSED */
1.74      kristaps  734: static enum rofferr
1.82      kristaps  735: roff_cond(ROFF_ARGS)
1.74      kristaps  736: {
1.77      kristaps  737:        int              sv;
1.88    ! kristaps  738:        enum roffrule    rule;
1.74      kristaps  739:
1.82      kristaps  740:        /* Stack overflow! */
                    741:
                    742:        if (ROFF_ie == tok && r->rstackpos == RSTACK_MAX - 1) {
                    743:                (*r->msg)(MANDOCERR_MEM, r->data, ln, ppos, NULL);
                    744:                return(ROFF_ERR);
                    745:        }
1.74      kristaps  746:
1.88    ! kristaps  747:        /* First, evaluate the conditional. */
1.84      schwarze  748:
1.88    ! kristaps  749:        if (ROFF_el == tok) {
        !           750:                /*
        !           751:                 * An `.el' will get the value of the current rstack
        !           752:                 * entry set in prior `ie' calls or defaults to DENY.
        !           753:                 */
        !           754:                if (r->rstackpos < 0)
        !           755:                        rule = ROFFRULE_DENY;
        !           756:                else
        !           757:                        rule = r->rstack[r->rstackpos];
        !           758:        } else
        !           759:                rule = roff_evalcond(*bufp, &pos);
1.77      kristaps  760:
                    761:        sv = pos;
1.88    ! kristaps  762:
1.75      kristaps  763:        while (' ' == (*bufp)[pos])
                    764:                pos++;
1.74      kristaps  765:
1.77      kristaps  766:        /*
                    767:         * Roff is weird.  If we have just white-space after the
                    768:         * conditional, it's considered the BODY and we exit without
                    769:         * really doing anything.  Warn about this.  It's probably
                    770:         * wrong.
                    771:         */
1.88    ! kristaps  772:
1.77      kristaps  773:        if ('\0' == (*bufp)[pos] && sv != pos) {
1.88    ! kristaps  774:                if ((*r->msg)(MANDOCERR_NOARGS, r->data, ln, ppos, NULL))
        !           775:                        return(ROFF_IGN);
        !           776:                return(ROFF_ERR);
1.77      kristaps  777:        }
                    778:
                    779:        if ( ! roffnode_push(r, tok, ln, ppos))
                    780:                return(ROFF_ERR);
                    781:
1.88    ! kristaps  782:        r->last->rule = rule;
        !           783:
        !           784:        ROFF_DEBUG("roff: cond: %s -> %s\n", roffs[tok].name,
        !           785:                        ROFFRULE_ALLOW == rule ?  "allow" : "deny");
1.82      kristaps  786:
1.84      schwarze  787:        if (ROFF_ie == tok) {
1.82      kristaps  788:                /*
                    789:                 * An if-else will put the NEGATION of the current
                    790:                 * evaluated conditional into the stack.
                    791:                 */
                    792:                r->rstackpos++;
                    793:                if (ROFFRULE_DENY == r->last->rule)
                    794:                        r->rstack[r->rstackpos] = ROFFRULE_ALLOW;
                    795:                else
                    796:                        r->rstack[r->rstackpos] = ROFFRULE_DENY;
                    797:        }
1.88    ! kristaps  798:
        !           799:        /* If the parent has false as its rule, then so do we. */
        !           800:
        !           801:        if (r->last->parent && ROFFRULE_DENY == r->last->parent->rule) {
1.84      schwarze  802:                r->last->rule = ROFFRULE_DENY;
1.88    ! kristaps  803:                ROFF_DEBUG("roff: cond override: %s -> deny\n",
        !           804:                                roffs[tok].name);
        !           805:        }
        !           806:
        !           807:        /*
        !           808:         * Determine scope.  If we're invoked with "\{" trailing the
        !           809:         * conditional, then we're in a multiline scope.  Else our scope
        !           810:         * expires on the next line.
        !           811:         */
1.74      kristaps  812:
1.75      kristaps  813:        r->last->endspan = 1;
                    814:
                    815:        if ('\\' == (*bufp)[pos] && '{' == (*bufp)[pos + 1]) {
                    816:                r->last->endspan = -1;
                    817:                pos += 2;
1.88    ! kristaps  818:                ROFF_DEBUG("roff: cond-scope: %s, multi-line\n",
        !           819:                                roffs[tok].name);
        !           820:        } else
        !           821:                ROFF_DEBUG("roff: cond-scope: %s, one-line\n",
        !           822:                                roffs[tok].name);
1.74      kristaps  823:
1.77      kristaps  824:        /*
                    825:         * If there are no arguments on the line, the next-line scope is
                    826:         * assumed.
                    827:         */
                    828:
1.75      kristaps  829:        if ('\0' == (*bufp)[pos])
                    830:                return(ROFF_IGN);
1.77      kristaps  831:
                    832:        /* Otherwise re-run the roff parser after recalculating. */
1.74      kristaps  833:
1.75      kristaps  834:        *offs = pos;
                    835:        return(ROFF_RERUN);
1.83      schwarze  836: }
                    837:
                    838:
                    839: /* ARGSUSED */
                    840: static enum rofferr
                    841: roff_line(ROFF_ARGS)
                    842: {
                    843:
                    844:        return(ROFF_IGN);
1.74      kristaps  845: }

CVSweb