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

Annotation of mandoc/man_macro.c, Revision 1.53

1.53    ! kristaps    1: /*     $Id: man_macro.c,v 1.52 2010/12/05 16:14:16 kristaps Exp $ */
1.1       kristaps    2: /*
1.50      schwarze    3:  * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.14      kristaps    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.14      kristaps    9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   16:  */
1.30      kristaps   17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
1.1       kristaps   21: #include <assert.h>
                     22: #include <ctype.h>
                     23: #include <stdlib.h>
                     24: #include <string.h>
                     25:
1.46      kristaps   26: #include "mandoc.h"
1.1       kristaps   27: #include "libman.h"
                     28:
1.34      kristaps   29: enum   rew {
                     30:        REW_REWIND,
                     31:        REW_NOHALT,
1.40      kristaps   32:        REW_HALT
1.34      kristaps   33: };
1.19      kristaps   34:
1.35      kristaps   35: static int              blk_close(MACRO_PROT_ARGS);
                     36: static int              blk_exp(MACRO_PROT_ARGS);
                     37: static int              blk_imp(MACRO_PROT_ARGS);
1.19      kristaps   38: static int              in_line_eoln(MACRO_PROT_ARGS);
                     39:
1.33      kristaps   40: static int              rew_scope(enum man_type,
                     41:                                struct man *, enum mant);
1.34      kristaps   42: static enum rew         rew_dohalt(enum mant, enum man_type,
1.19      kristaps   43:                                const struct man_node *);
1.34      kristaps   44: static enum rew         rew_block(enum mant, enum man_type,
1.21      kristaps   45:                                const struct man_node *);
1.38      kristaps   46: static int              rew_warn(struct man *,
1.46      kristaps   47:                                struct man_node *, enum mandocerr);
1.19      kristaps   48:
                     49: const  struct man_macro __man_macros[MAN_MAX] = {
1.31      kristaps   50:        { in_line_eoln, MAN_NSCOPED }, /* br */
1.19      kristaps   51:        { in_line_eoln, 0 }, /* TH */
1.24      kristaps   52:        { blk_imp, MAN_SCOPED }, /* SH */
                     53:        { blk_imp, MAN_SCOPED }, /* SS */
1.27      kristaps   54:        { blk_imp, MAN_SCOPED | MAN_FSCOPED }, /* TP */
1.19      kristaps   55:        { blk_imp, 0 }, /* LP */
                     56:        { blk_imp, 0 }, /* PP */
                     57:        { blk_imp, 0 }, /* P */
                     58:        { blk_imp, 0 }, /* IP */
                     59:        { blk_imp, 0 }, /* HP */
                     60:        { in_line_eoln, MAN_SCOPED }, /* SM */
                     61:        { in_line_eoln, MAN_SCOPED }, /* SB */
                     62:        { in_line_eoln, 0 }, /* BI */
                     63:        { in_line_eoln, 0 }, /* IB */
                     64:        { in_line_eoln, 0 }, /* BR */
                     65:        { in_line_eoln, 0 }, /* RB */
                     66:        { in_line_eoln, MAN_SCOPED }, /* R */
                     67:        { in_line_eoln, MAN_SCOPED }, /* B */
                     68:        { in_line_eoln, MAN_SCOPED }, /* I */
                     69:        { in_line_eoln, 0 }, /* IR */
                     70:        { in_line_eoln, 0 }, /* RI */
1.31      kristaps   71:        { in_line_eoln, MAN_NSCOPED }, /* na */
1.19      kristaps   72:        { in_line_eoln, 0 }, /* i */
1.31      kristaps   73:        { in_line_eoln, MAN_NSCOPED }, /* sp */
1.19      kristaps   74:        { in_line_eoln, 0 }, /* nf */
                     75:        { in_line_eoln, 0 }, /* fi */
                     76:        { in_line_eoln, 0 }, /* r */
1.21      kristaps   77:        { blk_close, 0 }, /* RE */
1.35      kristaps   78:        { blk_exp, MAN_EXPLICIT }, /* RS */
1.23      kristaps   79:        { in_line_eoln, 0 }, /* DT */
1.28      kristaps   80:        { in_line_eoln, 0 }, /* UC */
1.29      kristaps   81:        { in_line_eoln, 0 }, /* PD */
1.45      joerg      82:        { in_line_eoln, 0 }, /* AT */
1.49      kristaps   83:        { in_line_eoln, 0 }, /* in */
1.53    ! kristaps   84:        { in_line_eoln, 0 }, /* ft */
1.19      kristaps   85: };
1.9       kristaps   86:
1.19      kristaps   87: const  struct man_macro * const man_macros = __man_macros;
1.1       kristaps   88:
                     89:
1.38      kristaps   90: /*
                     91:  * Warn when "n" is an explicit non-roff macro.
                     92:  */
                     93: static int
1.46      kristaps   94: rew_warn(struct man *m, struct man_node *n, enum mandocerr er)
1.38      kristaps   95: {
                     96:
1.46      kristaps   97:        if (er == MANDOCERR_MAX || MAN_BLOCK != n->type)
1.38      kristaps   98:                return(1);
                     99:        if (MAN_VALID & n->flags)
                    100:                return(1);
                    101:        if ( ! (MAN_EXPLICIT & man_macros[n->tok].flags))
                    102:                return(1);
1.46      kristaps  103:        return(man_nmsg(m, n, er));
1.38      kristaps  104: }
                    105:
                    106:
                    107: /*
1.46      kristaps  108:  * Rewind scope.  If a code "er" != MANDOCERR_MAX has been provided, it
                    109:  * will be used if an explicit block scope is being closed out.
1.38      kristaps  110:  */
1.3       kristaps  111: int
1.46      kristaps  112: man_unscope(struct man *m, const struct man_node *n,
                    113:                enum mandocerr er)
1.1       kristaps  114: {
                    115:
1.19      kristaps  116:        assert(n);
                    117:
                    118:        /* LINTED */
                    119:        while (m->last != n) {
1.38      kristaps  120:                if ( ! rew_warn(m, m->last, er))
                    121:                        return(0);
1.19      kristaps  122:                if ( ! man_valid_post(m))
                    123:                        return(0);
                    124:                m->last = m->last->parent;
                    125:                assert(m->last);
                    126:        }
                    127:
1.38      kristaps  128:        if ( ! rew_warn(m, m->last, er))
                    129:                return(0);
1.19      kristaps  130:        if ( ! man_valid_post(m))
1.1       kristaps  131:                return(0);
1.35      kristaps  132:
                    133:        m->next = MAN_ROOT == m->last->type ?
                    134:                MAN_NEXT_CHILD : MAN_NEXT_SIBLING;
                    135:
                    136:        return(1);
1.19      kristaps  137: }
1.1       kristaps  138:
                    139:
1.34      kristaps  140: static enum rew
1.33      kristaps  141: rew_block(enum mant ntok, enum man_type type, const struct man_node *n)
1.21      kristaps  142: {
                    143:
                    144:        if (MAN_BLOCK == type && ntok == n->parent->tok &&
                    145:                        MAN_BODY == n->parent->type)
                    146:                return(REW_REWIND);
                    147:        return(ntok == n->tok ? REW_HALT : REW_NOHALT);
                    148: }
                    149:
                    150:
1.19      kristaps  151: /*
                    152:  * There are three scope levels: scoped to the root (all), scoped to the
                    153:  * section (all less sections), and scoped to subsections (all less
                    154:  * sections and subsections).
                    155:  */
1.34      kristaps  156: static enum rew
1.33      kristaps  157: rew_dohalt(enum mant tok, enum man_type type, const struct man_node *n)
1.19      kristaps  158: {
1.34      kristaps  159:        enum rew         c;
1.1       kristaps  160:
1.38      kristaps  161:        /* We cannot progress beyond the root ever. */
1.19      kristaps  162:        if (MAN_ROOT == n->type)
                    163:                return(REW_HALT);
1.38      kristaps  164:
1.19      kristaps  165:        assert(n->parent);
1.38      kristaps  166:
                    167:        /* Normal nodes shouldn't go to the level of the root. */
1.19      kristaps  168:        if (MAN_ROOT == n->parent->type)
                    169:                return(REW_REWIND);
1.38      kristaps  170:
                    171:        /* Already-validated nodes should be closed out. */
1.19      kristaps  172:        if (MAN_VALID & n->flags)
                    173:                return(REW_NOHALT);
                    174:
1.38      kristaps  175:        /* First: rewind to ourselves. */
1.21      kristaps  176:        if (type == n->type && tok == n->tok)
                    177:                return(REW_REWIND);
                    178:
1.38      kristaps  179:        /*
                    180:         * Next follow the implicit scope-smashings as defined by man.7:
                    181:         * section, sub-section, etc.
                    182:         */
                    183:
1.19      kristaps  184:        switch (tok) {
                    185:        case (MAN_SH):
                    186:                break;
                    187:        case (MAN_SS):
1.20      kristaps  188:                /* Rewind to a section, if a block. */
1.21      kristaps  189:                if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
                    190:                        return(c);
                    191:                break;
                    192:        case (MAN_RS):
                    193:                /* Rewind to a subsection, if a block. */
                    194:                if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
                    195:                        return(c);
                    196:                /* Rewind to a section, if a block. */
                    197:                if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
                    198:                        return(c);
1.19      kristaps  199:                break;
                    200:        default:
1.21      kristaps  201:                /* Rewind to an offsetter, if a block. */
                    202:                if (REW_NOHALT != (c = rew_block(MAN_RS, type, n)))
                    203:                        return(c);
1.20      kristaps  204:                /* Rewind to a subsection, if a block. */
1.21      kristaps  205:                if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
                    206:                        return(c);
1.20      kristaps  207:                /* Rewind to a section, if a block. */
1.21      kristaps  208:                if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
                    209:                        return(c);
1.19      kristaps  210:                break;
1.3       kristaps  211:        }
1.1       kristaps  212:
1.19      kristaps  213:        return(REW_NOHALT);
                    214: }
1.9       kristaps  215:
                    216:
1.19      kristaps  217: /*
                    218:  * Rewinding entails ascending the parse tree until a coherent point,
                    219:  * for example, the `SH' macro will close out any intervening `SS'
                    220:  * scopes.  When a scope is closed, it must be validated and actioned.
                    221:  */
                    222: static int
1.33      kristaps  223: rew_scope(enum man_type type, struct man *m, enum mant tok)
1.19      kristaps  224: {
                    225:        struct man_node *n;
1.34      kristaps  226:        enum rew         c;
1.7       kristaps  227:
1.19      kristaps  228:        /* LINTED */
                    229:        for (n = m->last; n; n = n->parent) {
                    230:                /*
                    231:                 * Whether we should stop immediately (REW_HALT), stop
                    232:                 * and rewind until this point (REW_REWIND), or keep
                    233:                 * rewinding (REW_NOHALT).
                    234:                 */
                    235:                c = rew_dohalt(tok, type, n);
                    236:                if (REW_HALT == c)
                    237:                        return(1);
                    238:                if (REW_REWIND == c)
1.7       kristaps  239:                        break;
1.6       kristaps  240:        }
1.1       kristaps  241:
1.38      kristaps  242:        /*
                    243:         * Rewind until the current point.  Warn if we're a roff
                    244:         * instruction that's mowing over explicit scopes.
                    245:         */
                    246:        assert(n);
1.19      kristaps  247:
1.46      kristaps  248:        return(man_unscope(m, n, MANDOCERR_MAX));
1.19      kristaps  249: }
                    250:
1.6       kristaps  251:
1.36      kristaps  252: /*
                    253:  * Close out a generic explicit macro.
                    254:  */
1.37      kristaps  255: /* ARGSUSED */
1.35      kristaps  256: int
1.21      kristaps  257: blk_close(MACRO_PROT_ARGS)
                    258: {
1.33      kristaps  259:        enum mant                ntok;
1.21      kristaps  260:        const struct man_node   *nn;
                    261:
                    262:        switch (tok) {
                    263:        case (MAN_RE):
                    264:                ntok = MAN_RS;
                    265:                break;
                    266:        default:
                    267:                abort();
                    268:                /* NOTREACHED */
                    269:        }
                    270:
                    271:        for (nn = m->last->parent; nn; nn = nn->parent)
                    272:                if (ntok == nn->tok)
                    273:                        break;
                    274:
                    275:        if (NULL == nn)
1.46      kristaps  276:                if ( ! man_pmsg(m, line, ppos, MANDOCERR_NOSCOPE))
1.21      kristaps  277:                        return(0);
                    278:
                    279:        if ( ! rew_scope(MAN_BODY, m, ntok))
                    280:                return(0);
                    281:        if ( ! rew_scope(MAN_BLOCK, m, ntok))
                    282:                return(0);
1.35      kristaps  283:
1.21      kristaps  284:        return(1);
                    285: }
                    286:
                    287:
1.48      kristaps  288: /* ARGSUSED */
1.35      kristaps  289: int
                    290: blk_exp(MACRO_PROT_ARGS)
                    291: {
                    292:        int              w, la;
                    293:        char            *p;
                    294:
                    295:        /*
                    296:         * Close out prior scopes.  "Regular" explicit macros cannot be
                    297:         * nested, but we allow roff macros to be placed just about
                    298:         * anywhere.
                    299:         */
                    300:
1.44      kristaps  301:        if ( ! rew_scope(MAN_BODY, m, tok))
                    302:                return(0);
                    303:        if ( ! rew_scope(MAN_BLOCK, m, tok))
                    304:                return(0);
1.35      kristaps  305:
                    306:        if ( ! man_block_alloc(m, line, ppos, tok))
                    307:                return(0);
                    308:        if ( ! man_head_alloc(m, line, ppos, tok))
                    309:                return(0);
                    310:
                    311:        for (;;) {
                    312:                la = *pos;
                    313:                w = man_args(m, line, pos, buf, &p);
                    314:
                    315:                if (-1 == w)
                    316:                        return(0);
                    317:                if (0 == w)
                    318:                        break;
                    319:
                    320:                if ( ! man_word_alloc(m, line, la, p))
                    321:                        return(0);
                    322:        }
                    323:
                    324:        assert(m);
                    325:        assert(tok != MAN_MAX);
                    326:
                    327:        if ( ! rew_scope(MAN_HEAD, m, tok))
                    328:                return(0);
                    329:        return(man_body_alloc(m, line, ppos, tok));
                    330: }
                    331:
                    332:
                    333:
1.19      kristaps  334: /*
                    335:  * Parse an implicit-block macro.  These contain a MAN_HEAD and a
                    336:  * MAN_BODY contained within a MAN_BLOCK.  Rules for closing out other
                    337:  * scopes, such as `SH' closing out an `SS', are defined in the rew
                    338:  * routines.
                    339:  */
1.48      kristaps  340: /* ARGSUSED */
1.19      kristaps  341: int
                    342: blk_imp(MACRO_PROT_ARGS)
                    343: {
                    344:        int              w, la;
                    345:        char            *p;
1.25      kristaps  346:        struct man_node *n;
1.19      kristaps  347:
                    348:        /* Close out prior scopes. */
1.7       kristaps  349:
1.19      kristaps  350:        if ( ! rew_scope(MAN_BODY, m, tok))
1.5       kristaps  351:                return(0);
1.19      kristaps  352:        if ( ! rew_scope(MAN_BLOCK, m, tok))
1.6       kristaps  353:                return(0);
1.1       kristaps  354:
1.19      kristaps  355:        /* Allocate new block & head scope. */
                    356:
                    357:        if ( ! man_block_alloc(m, line, ppos, tok))
                    358:                return(0);
                    359:        if ( ! man_head_alloc(m, line, ppos, tok))
                    360:                return(0);
1.1       kristaps  361:
1.25      kristaps  362:        n = m->last;
                    363:
1.19      kristaps  364:        /* Add line arguments. */
1.3       kristaps  365:
1.19      kristaps  366:        for (;;) {
                    367:                la = *pos;
                    368:                w = man_args(m, line, pos, buf, &p);
1.4       kristaps  369:
1.19      kristaps  370:                if (-1 == w)
1.6       kristaps  371:                        return(0);
1.19      kristaps  372:                if (0 == w)
                    373:                        break;
                    374:
                    375:                if ( ! man_word_alloc(m, line, la, p))
1.6       kristaps  376:                        return(0);
                    377:        }
                    378:
1.19      kristaps  379:        /* Close out head and open body (unless MAN_SCOPE). */
                    380:
1.27      kristaps  381:        if (MAN_SCOPED & man_macros[tok].flags) {
                    382:                /* If we're forcing scope (`TP'), keep it open. */
                    383:                if (MAN_FSCOPED & man_macros[tok].flags) {
                    384:                        m->flags |= MAN_BLINE;
                    385:                        return(1);
                    386:                } else if (n == m->last) {
                    387:                        m->flags |= MAN_BLINE;
                    388:                        return(1);
                    389:                }
                    390:        }
                    391:
                    392:        if ( ! rew_scope(MAN_HEAD, m, tok))
1.6       kristaps  393:                return(0);
1.19      kristaps  394:        return(man_body_alloc(m, line, ppos, tok));
1.4       kristaps  395: }
                    396:
                    397:
1.48      kristaps  398: /* ARGSUSED */
1.19      kristaps  399: int
                    400: in_line_eoln(MACRO_PROT_ARGS)
1.3       kristaps  401: {
1.19      kristaps  402:        int              w, la;
                    403:        char            *p;
                    404:        struct man_node *n;
1.3       kristaps  405:
1.19      kristaps  406:        if ( ! man_elem_alloc(m, line, ppos, tok))
1.3       kristaps  407:                return(0);
                    408:
1.19      kristaps  409:        n = m->last;
1.3       kristaps  410:
1.19      kristaps  411:        for (;;) {
                    412:                la = *pos;
                    413:                w = man_args(m, line, pos, buf, &p);
1.3       kristaps  414:
1.19      kristaps  415:                if (-1 == w)
                    416:                        return(0);
                    417:                if (0 == w)
                    418:                        break;
                    419:                if ( ! man_word_alloc(m, line, la, p))
                    420:                        return(0);
                    421:        }
1.3       kristaps  422:
1.31      kristaps  423:        /*
                    424:         * If no arguments are specified and this is MAN_SCOPED (i.e.,
                    425:         * next-line scoped), then set our mode to indicate that we're
                    426:         * waiting for terms to load into our context.
                    427:         */
                    428:
1.25      kristaps  429:        if (n == m->last && MAN_SCOPED & man_macros[tok].flags) {
1.31      kristaps  430:                assert( ! (MAN_NSCOPED & man_macros[tok].flags));
1.19      kristaps  431:                m->flags |= MAN_ELINE;
                    432:                return(1);
                    433:        }
1.3       kristaps  434:
1.31      kristaps  435:        /* Set ignorable context, if applicable. */
                    436:
                    437:        if (MAN_NSCOPED & man_macros[tok].flags) {
                    438:                assert( ! (MAN_SCOPED & man_macros[tok].flags));
                    439:                m->flags |= MAN_ILINE;
                    440:        }
                    441:
1.19      kristaps  442:        /*
1.31      kristaps  443:         * Rewind our element scope.  Note that when TH is pruned, we'll
                    444:         * be back at the root, so make sure that we don't clobber as
                    445:         * its sibling.
1.19      kristaps  446:         */
1.3       kristaps  447:
1.19      kristaps  448:        for ( ; m->last; m->last = m->last->parent) {
                    449:                if (m->last == n)
                    450:                        break;
                    451:                if (m->last->type == MAN_ROOT)
                    452:                        break;
                    453:                if ( ! man_valid_post(m))
                    454:                        return(0);
                    455:        }
1.3       kristaps  456:
1.19      kristaps  457:        assert(m->last);
1.3       kristaps  458:
                    459:        /*
1.19      kristaps  460:         * Same here regarding whether we're back at the root.
1.3       kristaps  461:         */
                    462:
1.19      kristaps  463:        if (m->last->type != MAN_ROOT && ! man_valid_post(m))
                    464:                return(0);
1.35      kristaps  465:
                    466:        m->next = MAN_ROOT == m->last->type ?
                    467:                MAN_NEXT_CHILD : MAN_NEXT_SIBLING;
1.3       kristaps  468:
1.19      kristaps  469:        return(1);
                    470: }
1.3       kristaps  471:
                    472:
1.19      kristaps  473: int
                    474: man_macroend(struct man *m)
                    475: {
1.22      kristaps  476:
1.46      kristaps  477:        return(man_unscope(m, m->first, MANDOCERR_SCOPEEXIT));
1.19      kristaps  478: }
1.3       kristaps  479:

CVSweb