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

Annotation of mandoc/roff.c, Revision 1.101

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

CVSweb