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

Annotation of mandoc/roff.c, Revision 1.158

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

CVSweb