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

Annotation of mandoc/man_macro.c, Revision 1.45

1.45    ! joerg       1: /*     $Id: man_macro.c,v 1.44 2010/05/15 22:44:04 kristaps Exp $ */
1.1       kristaps    2: /*
1.15      kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
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:
                     26: #include "libman.h"
                     27:
1.34      kristaps   28: enum   rew {
                     29:        REW_REWIND,
                     30:        REW_NOHALT,
1.40      kristaps   31:        REW_HALT
1.34      kristaps   32: };
1.19      kristaps   33:
1.35      kristaps   34: static int              blk_close(MACRO_PROT_ARGS);
                     35: static int              blk_exp(MACRO_PROT_ARGS);
                     36: static int              blk_imp(MACRO_PROT_ARGS);
1.19      kristaps   37: static int              in_line_eoln(MACRO_PROT_ARGS);
                     38:
1.33      kristaps   39: static int              rew_scope(enum man_type,
                     40:                                struct man *, enum mant);
1.34      kristaps   41: static enum rew         rew_dohalt(enum mant, enum man_type,
1.19      kristaps   42:                                const struct man_node *);
1.34      kristaps   43: static enum rew         rew_block(enum mant, enum man_type,
1.21      kristaps   44:                                const struct man_node *);
1.38      kristaps   45: static int              rew_warn(struct man *,
                     46:                                struct man_node *, enum merr);
1.19      kristaps   47:
                     48: const  struct man_macro __man_macros[MAN_MAX] = {
1.31      kristaps   49:        { in_line_eoln, MAN_NSCOPED }, /* br */
1.19      kristaps   50:        { in_line_eoln, 0 }, /* TH */
1.24      kristaps   51:        { blk_imp, MAN_SCOPED }, /* SH */
                     52:        { blk_imp, MAN_SCOPED }, /* SS */
1.27      kristaps   53:        { blk_imp, MAN_SCOPED | MAN_FSCOPED }, /* TP */
1.19      kristaps   54:        { blk_imp, 0 }, /* LP */
                     55:        { blk_imp, 0 }, /* PP */
                     56:        { blk_imp, 0 }, /* P */
                     57:        { blk_imp, 0 }, /* IP */
                     58:        { blk_imp, 0 }, /* HP */
                     59:        { in_line_eoln, MAN_SCOPED }, /* SM */
                     60:        { in_line_eoln, MAN_SCOPED }, /* SB */
                     61:        { in_line_eoln, 0 }, /* BI */
                     62:        { in_line_eoln, 0 }, /* IB */
                     63:        { in_line_eoln, 0 }, /* BR */
                     64:        { in_line_eoln, 0 }, /* RB */
                     65:        { in_line_eoln, MAN_SCOPED }, /* R */
                     66:        { in_line_eoln, MAN_SCOPED }, /* B */
                     67:        { in_line_eoln, MAN_SCOPED }, /* I */
                     68:        { in_line_eoln, 0 }, /* IR */
                     69:        { in_line_eoln, 0 }, /* RI */
1.31      kristaps   70:        { in_line_eoln, MAN_NSCOPED }, /* na */
1.19      kristaps   71:        { in_line_eoln, 0 }, /* i */
1.31      kristaps   72:        { in_line_eoln, MAN_NSCOPED }, /* sp */
1.19      kristaps   73:        { in_line_eoln, 0 }, /* nf */
                     74:        { in_line_eoln, 0 }, /* fi */
                     75:        { in_line_eoln, 0 }, /* r */
1.21      kristaps   76:        { blk_close, 0 }, /* RE */
1.35      kristaps   77:        { blk_exp, MAN_EXPLICIT }, /* RS */
1.23      kristaps   78:        { in_line_eoln, 0 }, /* DT */
1.28      kristaps   79:        { in_line_eoln, 0 }, /* UC */
1.29      kristaps   80:        { in_line_eoln, 0 }, /* PD */
1.32      kristaps   81:        { in_line_eoln, MAN_NSCOPED }, /* Sp */
                     82:        { in_line_eoln, 0 }, /* Vb */
                     83:        { in_line_eoln, 0 }, /* Ve */
1.45    ! joerg      84:        { in_line_eoln, 0 }, /* AT */
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
                     94: rew_warn(struct man *m, struct man_node *n, enum merr er)
                     95: {
                     96:
                     97:        if (er == WERRMAX || MAN_BLOCK != n->type)
                     98:                return(1);
                     99:        if (MAN_VALID & n->flags)
                    100:                return(1);
                    101:        if ( ! (MAN_EXPLICIT & man_macros[n->tok].flags))
                    102:                return(1);
                    103:        return(man_nwarn(m, n, er));
                    104: }
                    105:
                    106:
                    107: /*
                    108:  * Rewind scope.  If a code "er" != WERRMAX has been provided, it will
                    109:  * be used if an explicit block scope is being closed out.
                    110:  */
1.3       kristaps  111: int
1.38      kristaps  112: man_unscope(struct man *m, const struct man_node *n, enum merr er)
1.1       kristaps  113: {
                    114:
1.19      kristaps  115:        assert(n);
                    116:
                    117:        /* LINTED */
                    118:        while (m->last != n) {
1.38      kristaps  119:                if ( ! rew_warn(m, m->last, er))
                    120:                        return(0);
1.19      kristaps  121:                if ( ! man_valid_post(m))
                    122:                        return(0);
                    123:                if ( ! man_action_post(m))
                    124:                        return(0);
                    125:                m->last = m->last->parent;
                    126:                assert(m->last);
                    127:        }
                    128:
1.38      kristaps  129:        if ( ! rew_warn(m, m->last, er))
                    130:                return(0);
1.19      kristaps  131:        if ( ! man_valid_post(m))
1.1       kristaps  132:                return(0);
1.35      kristaps  133:        if ( ! man_action_post(m))
                    134:                return(0);
                    135:
                    136:        m->next = MAN_ROOT == m->last->type ?
                    137:                MAN_NEXT_CHILD : MAN_NEXT_SIBLING;
                    138:
                    139:        return(1);
1.19      kristaps  140: }
1.1       kristaps  141:
                    142:
1.34      kristaps  143: static enum rew
1.33      kristaps  144: rew_block(enum mant ntok, enum man_type type, const struct man_node *n)
1.21      kristaps  145: {
                    146:
                    147:        if (MAN_BLOCK == type && ntok == n->parent->tok &&
                    148:                        MAN_BODY == n->parent->type)
                    149:                return(REW_REWIND);
                    150:        return(ntok == n->tok ? REW_HALT : REW_NOHALT);
                    151: }
                    152:
                    153:
1.19      kristaps  154: /*
                    155:  * There are three scope levels: scoped to the root (all), scoped to the
                    156:  * section (all less sections), and scoped to subsections (all less
                    157:  * sections and subsections).
                    158:  */
1.34      kristaps  159: static enum rew
1.33      kristaps  160: rew_dohalt(enum mant tok, enum man_type type, const struct man_node *n)
1.19      kristaps  161: {
1.34      kristaps  162:        enum rew         c;
1.1       kristaps  163:
1.38      kristaps  164:        /* We cannot progress beyond the root ever. */
1.19      kristaps  165:        if (MAN_ROOT == n->type)
                    166:                return(REW_HALT);
1.38      kristaps  167:
1.19      kristaps  168:        assert(n->parent);
1.38      kristaps  169:
                    170:        /* Normal nodes shouldn't go to the level of the root. */
1.19      kristaps  171:        if (MAN_ROOT == n->parent->type)
                    172:                return(REW_REWIND);
1.38      kristaps  173:
                    174:        /* Already-validated nodes should be closed out. */
1.19      kristaps  175:        if (MAN_VALID & n->flags)
                    176:                return(REW_NOHALT);
                    177:
1.38      kristaps  178:        /* First: rewind to ourselves. */
1.21      kristaps  179:        if (type == n->type && tok == n->tok)
                    180:                return(REW_REWIND);
                    181:
1.38      kristaps  182:        /*
                    183:         * Next follow the implicit scope-smashings as defined by man.7:
                    184:         * section, sub-section, etc.
                    185:         */
                    186:
1.19      kristaps  187:        switch (tok) {
                    188:        case (MAN_SH):
                    189:                break;
                    190:        case (MAN_SS):
1.20      kristaps  191:                /* Rewind to a section, if a block. */
1.21      kristaps  192:                if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
                    193:                        return(c);
                    194:                break;
                    195:        case (MAN_RS):
                    196:                /* Rewind to a subsection, if a block. */
                    197:                if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
                    198:                        return(c);
                    199:                /* Rewind to a section, if a block. */
                    200:                if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
                    201:                        return(c);
1.19      kristaps  202:                break;
                    203:        default:
1.21      kristaps  204:                /* Rewind to an offsetter, if a block. */
                    205:                if (REW_NOHALT != (c = rew_block(MAN_RS, type, n)))
                    206:                        return(c);
1.20      kristaps  207:                /* Rewind to a subsection, if a block. */
1.21      kristaps  208:                if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
                    209:                        return(c);
1.20      kristaps  210:                /* Rewind to a section, if a block. */
1.21      kristaps  211:                if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
                    212:                        return(c);
1.19      kristaps  213:                break;
1.3       kristaps  214:        }
1.1       kristaps  215:
1.19      kristaps  216:        return(REW_NOHALT);
                    217: }
1.9       kristaps  218:
                    219:
1.19      kristaps  220: /*
                    221:  * Rewinding entails ascending the parse tree until a coherent point,
                    222:  * for example, the `SH' macro will close out any intervening `SS'
                    223:  * scopes.  When a scope is closed, it must be validated and actioned.
                    224:  */
                    225: static int
1.33      kristaps  226: rew_scope(enum man_type type, struct man *m, enum mant tok)
1.19      kristaps  227: {
                    228:        struct man_node *n;
1.34      kristaps  229:        enum rew         c;
1.7       kristaps  230:
1.19      kristaps  231:        /* LINTED */
                    232:        for (n = m->last; n; n = n->parent) {
                    233:                /*
                    234:                 * Whether we should stop immediately (REW_HALT), stop
                    235:                 * and rewind until this point (REW_REWIND), or keep
                    236:                 * rewinding (REW_NOHALT).
                    237:                 */
                    238:                c = rew_dohalt(tok, type, n);
                    239:                if (REW_HALT == c)
                    240:                        return(1);
                    241:                if (REW_REWIND == c)
1.7       kristaps  242:                        break;
1.6       kristaps  243:        }
1.1       kristaps  244:
1.38      kristaps  245:        /*
                    246:         * Rewind until the current point.  Warn if we're a roff
                    247:         * instruction that's mowing over explicit scopes.
                    248:         */
                    249:        assert(n);
1.19      kristaps  250:
1.38      kristaps  251:        return(man_unscope(m, n, WERRMAX));
1.19      kristaps  252: }
                    253:
1.6       kristaps  254:
1.36      kristaps  255: /*
                    256:  * Close out a generic explicit macro.
                    257:  */
1.37      kristaps  258: /* ARGSUSED */
1.35      kristaps  259: int
1.21      kristaps  260: blk_close(MACRO_PROT_ARGS)
                    261: {
1.33      kristaps  262:        enum mant                ntok;
1.21      kristaps  263:        const struct man_node   *nn;
                    264:
                    265:        switch (tok) {
                    266:        case (MAN_RE):
                    267:                ntok = MAN_RS;
                    268:                break;
                    269:        default:
                    270:                abort();
                    271:                /* NOTREACHED */
                    272:        }
                    273:
                    274:        for (nn = m->last->parent; nn; nn = nn->parent)
                    275:                if (ntok == nn->tok)
                    276:                        break;
                    277:
                    278:        if (NULL == nn)
                    279:                if ( ! man_pwarn(m, line, ppos, WNOSCOPE))
                    280:                        return(0);
                    281:
                    282:        if ( ! rew_scope(MAN_BODY, m, ntok))
                    283:                return(0);
                    284:        if ( ! rew_scope(MAN_BLOCK, m, ntok))
                    285:                return(0);
1.35      kristaps  286:
1.21      kristaps  287:        return(1);
                    288: }
                    289:
                    290:
1.35      kristaps  291: int
                    292: blk_exp(MACRO_PROT_ARGS)
                    293: {
                    294:        int              w, la;
                    295:        char            *p;
                    296:
                    297:        /*
                    298:         * Close out prior scopes.  "Regular" explicit macros cannot be
                    299:         * nested, but we allow roff macros to be placed just about
                    300:         * anywhere.
                    301:         */
                    302:
1.44      kristaps  303:        if ( ! rew_scope(MAN_BODY, m, tok))
                    304:                return(0);
                    305:        if ( ! rew_scope(MAN_BLOCK, m, tok))
                    306:                return(0);
1.35      kristaps  307:
                    308:        if ( ! man_block_alloc(m, line, ppos, tok))
                    309:                return(0);
                    310:        if ( ! man_head_alloc(m, line, ppos, tok))
                    311:                return(0);
                    312:
                    313:        for (;;) {
                    314:                la = *pos;
                    315:                w = man_args(m, line, pos, buf, &p);
                    316:
                    317:                if (-1 == w)
                    318:                        return(0);
                    319:                if (0 == w)
                    320:                        break;
                    321:
                    322:                if ( ! man_word_alloc(m, line, la, p))
                    323:                        return(0);
                    324:        }
                    325:
                    326:        assert(m);
                    327:        assert(tok != MAN_MAX);
                    328:
                    329:        if ( ! rew_scope(MAN_HEAD, m, tok))
                    330:                return(0);
                    331:        return(man_body_alloc(m, line, ppos, tok));
                    332: }
                    333:
                    334:
                    335:
1.19      kristaps  336: /*
                    337:  * Parse an implicit-block macro.  These contain a MAN_HEAD and a
                    338:  * MAN_BODY contained within a MAN_BLOCK.  Rules for closing out other
                    339:  * scopes, such as `SH' closing out an `SS', are defined in the rew
                    340:  * routines.
                    341:  */
                    342: int
                    343: blk_imp(MACRO_PROT_ARGS)
                    344: {
                    345:        int              w, la;
                    346:        char            *p;
1.25      kristaps  347:        struct man_node *n;
1.19      kristaps  348:
                    349:        /* Close out prior scopes. */
1.7       kristaps  350:
1.19      kristaps  351:        if ( ! rew_scope(MAN_BODY, m, tok))
1.5       kristaps  352:                return(0);
1.19      kristaps  353:        if ( ! rew_scope(MAN_BLOCK, m, tok))
1.6       kristaps  354:                return(0);
1.1       kristaps  355:
1.19      kristaps  356:        /* Allocate new block & head scope. */
                    357:
                    358:        if ( ! man_block_alloc(m, line, ppos, tok))
                    359:                return(0);
                    360:        if ( ! man_head_alloc(m, line, ppos, tok))
                    361:                return(0);
1.1       kristaps  362:
1.25      kristaps  363:        n = m->last;
                    364:
1.19      kristaps  365:        /* Add line arguments. */
1.3       kristaps  366:
1.19      kristaps  367:        for (;;) {
                    368:                la = *pos;
                    369:                w = man_args(m, line, pos, buf, &p);
1.4       kristaps  370:
1.19      kristaps  371:                if (-1 == w)
1.6       kristaps  372:                        return(0);
1.19      kristaps  373:                if (0 == w)
                    374:                        break;
                    375:
                    376:                if ( ! man_word_alloc(m, line, la, p))
1.6       kristaps  377:                        return(0);
                    378:        }
                    379:
1.19      kristaps  380:        /* Close out head and open body (unless MAN_SCOPE). */
                    381:
1.27      kristaps  382:        if (MAN_SCOPED & man_macros[tok].flags) {
                    383:                /* If we're forcing scope (`TP'), keep it open. */
                    384:                if (MAN_FSCOPED & man_macros[tok].flags) {
                    385:                        m->flags |= MAN_BLINE;
                    386:                        return(1);
                    387:                } else if (n == m->last) {
                    388:                        m->flags |= MAN_BLINE;
                    389:                        return(1);
                    390:                }
                    391:        }
                    392:
                    393:        if ( ! rew_scope(MAN_HEAD, m, tok))
1.6       kristaps  394:                return(0);
1.19      kristaps  395:        return(man_body_alloc(m, line, ppos, tok));
1.4       kristaps  396: }
                    397:
                    398:
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:                if ( ! man_action_post(m))
                    456:                        return(0);
                    457:        }
1.3       kristaps  458:
1.19      kristaps  459:        assert(m->last);
1.3       kristaps  460:
                    461:        /*
1.19      kristaps  462:         * Same here regarding whether we're back at the root.
1.3       kristaps  463:         */
                    464:
1.19      kristaps  465:        if (m->last->type != MAN_ROOT && ! man_valid_post(m))
                    466:                return(0);
                    467:        if (m->last->type != MAN_ROOT && ! man_action_post(m))
                    468:                return(0);
1.35      kristaps  469:
                    470:        m->next = MAN_ROOT == m->last->type ?
                    471:                MAN_NEXT_CHILD : MAN_NEXT_SIBLING;
1.3       kristaps  472:
1.19      kristaps  473:        return(1);
                    474: }
1.3       kristaps  475:
                    476:
1.19      kristaps  477: int
                    478: man_macroend(struct man *m)
                    479: {
1.22      kristaps  480:
1.38      kristaps  481:        return(man_unscope(m, m->first, WEXITSCOPE));
1.19      kristaps  482: }
1.3       kristaps  483:

CVSweb