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

Annotation of mandoc/roff.c, Revision 1.163

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

CVSweb