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

Annotation of mandoc/man_macro.c, Revision 1.36

1.36    ! kristaps    1: /*     $Id: man_macro.c,v 1.35 2010/03/24 20:10:53 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,
                     31:        REW_HALT,
                     32: };
1.19      kristaps   33:
1.35      kristaps   34: static int              blk_close(MACRO_PROT_ARGS);
                     35: static int              blk_dotted(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.19      kristaps   46:
                     47: const  struct man_macro __man_macros[MAN_MAX] = {
1.31      kristaps   48:        { in_line_eoln, MAN_NSCOPED }, /* br */
1.19      kristaps   49:        { in_line_eoln, 0 }, /* TH */
1.24      kristaps   50:        { blk_imp, MAN_SCOPED }, /* SH */
                     51:        { blk_imp, MAN_SCOPED }, /* SS */
1.27      kristaps   52:        { blk_imp, MAN_SCOPED | MAN_FSCOPED }, /* TP */
1.19      kristaps   53:        { blk_imp, 0 }, /* LP */
                     54:        { blk_imp, 0 }, /* PP */
                     55:        { blk_imp, 0 }, /* P */
                     56:        { blk_imp, 0 }, /* IP */
                     57:        { blk_imp, 0 }, /* HP */
                     58:        { in_line_eoln, MAN_SCOPED }, /* SM */
                     59:        { in_line_eoln, MAN_SCOPED }, /* SB */
                     60:        { in_line_eoln, 0 }, /* BI */
                     61:        { in_line_eoln, 0 }, /* IB */
                     62:        { in_line_eoln, 0 }, /* BR */
                     63:        { in_line_eoln, 0 }, /* RB */
                     64:        { in_line_eoln, MAN_SCOPED }, /* R */
                     65:        { in_line_eoln, MAN_SCOPED }, /* B */
                     66:        { in_line_eoln, MAN_SCOPED }, /* I */
                     67:        { in_line_eoln, 0 }, /* IR */
                     68:        { in_line_eoln, 0 }, /* RI */
1.31      kristaps   69:        { in_line_eoln, MAN_NSCOPED }, /* na */
1.19      kristaps   70:        { in_line_eoln, 0 }, /* i */
1.31      kristaps   71:        { in_line_eoln, MAN_NSCOPED }, /* sp */
1.19      kristaps   72:        { in_line_eoln, 0 }, /* nf */
                     73:        { in_line_eoln, 0 }, /* fi */
                     74:        { in_line_eoln, 0 }, /* r */
1.21      kristaps   75:        { blk_close, 0 }, /* RE */
1.35      kristaps   76:        { blk_exp, MAN_EXPLICIT }, /* RS */
1.23      kristaps   77:        { in_line_eoln, 0 }, /* DT */
1.28      kristaps   78:        { in_line_eoln, 0 }, /* UC */
1.29      kristaps   79:        { in_line_eoln, 0 }, /* PD */
1.32      kristaps   80:        { in_line_eoln, MAN_NSCOPED }, /* Sp */
                     81:        { in_line_eoln, 0 }, /* Vb */
                     82:        { in_line_eoln, 0 }, /* Ve */
1.35      kristaps   83:        { blk_exp, MAN_EXPLICIT | MAN_NOCLOSE}, /* de */
                     84:        { blk_exp, MAN_EXPLICIT | MAN_NOCLOSE}, /* dei */
                     85:        { blk_exp, MAN_EXPLICIT | MAN_NOCLOSE}, /* am */
                     86:        { blk_exp, MAN_EXPLICIT | MAN_NOCLOSE}, /* ami */
                     87:        { blk_exp, MAN_EXPLICIT | MAN_NOCLOSE}, /* ig */
                     88:        { blk_dotted, 0 }, /* . */
1.19      kristaps   89: };
1.9       kristaps   90:
1.19      kristaps   91: const  struct man_macro * const man_macros = __man_macros;
1.1       kristaps   92:
                     93:
1.3       kristaps   94: int
1.19      kristaps   95: man_unscope(struct man *m, const struct man_node *n)
1.1       kristaps   96: {
                     97:
1.19      kristaps   98:        assert(n);
                     99:
                    100:        /* LINTED */
                    101:        while (m->last != n) {
                    102:                if ( ! man_valid_post(m))
                    103:                        return(0);
                    104:                if ( ! man_action_post(m))
                    105:                        return(0);
                    106:                m->last = m->last->parent;
                    107:                assert(m->last);
                    108:        }
                    109:
                    110:        if ( ! man_valid_post(m))
1.1       kristaps  111:                return(0);
1.35      kristaps  112:        if ( ! man_action_post(m))
                    113:                return(0);
                    114:
                    115:        m->next = MAN_ROOT == m->last->type ?
                    116:                MAN_NEXT_CHILD : MAN_NEXT_SIBLING;
                    117:
                    118:        return(1);
1.19      kristaps  119: }
1.1       kristaps  120:
                    121:
1.34      kristaps  122: static enum rew
1.33      kristaps  123: rew_block(enum mant ntok, enum man_type type, const struct man_node *n)
1.21      kristaps  124: {
                    125:
                    126:        if (MAN_BLOCK == type && ntok == n->parent->tok &&
                    127:                        MAN_BODY == n->parent->type)
                    128:                return(REW_REWIND);
                    129:        return(ntok == n->tok ? REW_HALT : REW_NOHALT);
                    130: }
                    131:
                    132:
1.19      kristaps  133: /*
                    134:  * There are three scope levels: scoped to the root (all), scoped to the
                    135:  * section (all less sections), and scoped to subsections (all less
                    136:  * sections and subsections).
                    137:  */
1.34      kristaps  138: static enum rew
1.33      kristaps  139: rew_dohalt(enum mant tok, enum man_type type, const struct man_node *n)
1.19      kristaps  140: {
1.34      kristaps  141:        enum rew         c;
1.1       kristaps  142:
1.19      kristaps  143:        if (MAN_ROOT == n->type)
                    144:                return(REW_HALT);
                    145:        assert(n->parent);
                    146:        if (MAN_ROOT == n->parent->type)
                    147:                return(REW_REWIND);
                    148:        if (MAN_VALID & n->flags)
                    149:                return(REW_NOHALT);
                    150:
1.21      kristaps  151:        /* Rewind to ourselves, first. */
                    152:        if (type == n->type && tok == n->tok)
                    153:                return(REW_REWIND);
                    154:
1.19      kristaps  155:        switch (tok) {
                    156:        case (MAN_SH):
                    157:                break;
                    158:        case (MAN_SS):
1.20      kristaps  159:                /* Rewind to a section, if a block. */
1.21      kristaps  160:                if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
                    161:                        return(c);
                    162:                break;
                    163:        case (MAN_RS):
                    164:                /* Rewind to a subsection, if a block. */
                    165:                if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
                    166:                        return(c);
                    167:                /* Rewind to a section, if a block. */
                    168:                if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
                    169:                        return(c);
1.19      kristaps  170:                break;
                    171:        default:
1.21      kristaps  172:                /* Rewind to an offsetter, if a block. */
                    173:                if (REW_NOHALT != (c = rew_block(MAN_RS, type, n)))
                    174:                        return(c);
1.20      kristaps  175:                /* Rewind to a subsection, if a block. */
1.21      kristaps  176:                if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
                    177:                        return(c);
1.20      kristaps  178:                /* Rewind to a section, if a block. */
1.21      kristaps  179:                if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
                    180:                        return(c);
1.19      kristaps  181:                break;
1.3       kristaps  182:        }
1.1       kristaps  183:
1.19      kristaps  184:        return(REW_NOHALT);
                    185: }
1.9       kristaps  186:
                    187:
1.19      kristaps  188: /*
                    189:  * Rewinding entails ascending the parse tree until a coherent point,
                    190:  * for example, the `SH' macro will close out any intervening `SS'
                    191:  * scopes.  When a scope is closed, it must be validated and actioned.
                    192:  */
                    193: static int
1.33      kristaps  194: rew_scope(enum man_type type, struct man *m, enum mant tok)
1.19      kristaps  195: {
                    196:        struct man_node *n;
1.34      kristaps  197:        enum rew         c;
1.7       kristaps  198:
1.19      kristaps  199:        /* LINTED */
                    200:        for (n = m->last; n; n = n->parent) {
                    201:                /*
                    202:                 * Whether we should stop immediately (REW_HALT), stop
                    203:                 * and rewind until this point (REW_REWIND), or keep
                    204:                 * rewinding (REW_NOHALT).
                    205:                 */
                    206:                c = rew_dohalt(tok, type, n);
                    207:                if (REW_HALT == c)
                    208:                        return(1);
                    209:                if (REW_REWIND == c)
1.7       kristaps  210:                        break;
1.6       kristaps  211:        }
1.1       kristaps  212:
1.19      kristaps  213:        /* Rewind until the current point. */
                    214:
                    215:        assert(n);
                    216:        return(man_unscope(m, n));
                    217: }
                    218:
1.6       kristaps  219:
1.36    ! kristaps  220: /*
        !           221:  * Closure for dotted macros (de, dei, am, ami, ign).  This must handle
        !           222:  * any of these as the parent node, so it needs special handling.
        !           223:  * Beyond this, it's the same as blk_close().
        !           224:  */
1.21      kristaps  225: int
1.35      kristaps  226: blk_dotted(MACRO_PROT_ARGS)
                    227: {
                    228:        enum mant        ntok;
                    229:        struct man_node *nn;
                    230:
                    231:        for (nn = m->last->parent; nn; nn = nn->parent)
                    232:                if (nn->tok == MAN_de || nn->tok == MAN_dei ||
                    233:                                nn->tok == MAN_am ||
                    234:                                nn->tok == MAN_ami ||
                    235:                                nn->tok == MAN_ig) {
                    236:                        ntok = nn->tok;
                    237:                        break;
                    238:                }
                    239:
                    240:        if (NULL == nn) {
                    241:                if ( ! man_pwarn(m, line, ppos, WNOSCOPE))
                    242:                        return(0);
                    243:                return(1);
                    244:        }
                    245:
                    246:        if ( ! rew_scope(MAN_BODY, m, ntok))
                    247:                return(0);
                    248:        if ( ! rew_scope(MAN_BLOCK, m, ntok))
                    249:                return(0);
                    250:
                    251:        return(1);
                    252: }
                    253:
                    254:
1.36    ! kristaps  255: /*
        !           256:  * Close out a generic explicit macro.
        !           257:  */
1.35      kristaps  258: int
1.21      kristaps  259: blk_close(MACRO_PROT_ARGS)
                    260: {
1.33      kristaps  261:        enum mant                ntok;
1.21      kristaps  262:        const struct man_node   *nn;
                    263:
                    264:        switch (tok) {
                    265:        case (MAN_RE):
                    266:                ntok = MAN_RS;
                    267:                break;
                    268:        default:
                    269:                abort();
                    270:                /* NOTREACHED */
                    271:        }
                    272:
                    273:        for (nn = m->last->parent; nn; nn = nn->parent)
                    274:                if (ntok == nn->tok)
                    275:                        break;
                    276:
                    277:        if (NULL == nn)
                    278:                if ( ! man_pwarn(m, line, ppos, WNOSCOPE))
                    279:                        return(0);
                    280:
                    281:        if ( ! rew_scope(MAN_BODY, m, ntok))
                    282:                return(0);
                    283:        if ( ! rew_scope(MAN_BLOCK, m, ntok))
                    284:                return(0);
1.35      kristaps  285:
1.21      kristaps  286:        return(1);
                    287: }
                    288:
                    289:
1.35      kristaps  290: int
                    291: blk_exp(MACRO_PROT_ARGS)
                    292: {
                    293:        int              w, la;
                    294:        char            *p;
                    295:
                    296:        /*
                    297:         * Close out prior scopes.  "Regular" explicit macros cannot be
                    298:         * nested, but we allow roff macros to be placed just about
                    299:         * anywhere.
                    300:         */
                    301:
                    302:        if ( ! (MAN_NOCLOSE & man_macros[tok].flags)) {
                    303:                if ( ! rew_scope(MAN_BODY, m, tok))
                    304:                        return(0);
                    305:                if ( ! rew_scope(MAN_BLOCK, m, tok))
                    306:                        return(0);
                    307:        }
                    308:
                    309:        if ( ! man_block_alloc(m, line, ppos, tok))
                    310:                return(0);
                    311:        if ( ! man_head_alloc(m, line, ppos, tok))
                    312:                return(0);
                    313:
                    314:        for (;;) {
                    315:                la = *pos;
                    316:                w = man_args(m, line, pos, buf, &p);
                    317:
                    318:                if (-1 == w)
                    319:                        return(0);
                    320:                if (0 == w)
                    321:                        break;
                    322:
                    323:                if ( ! man_word_alloc(m, line, la, p))
                    324:                        return(0);
                    325:        }
                    326:
                    327:        assert(m);
                    328:        assert(tok != MAN_MAX);
                    329:
                    330:        if ( ! rew_scope(MAN_HEAD, m, tok))
                    331:                return(0);
                    332:        return(man_body_alloc(m, line, ppos, tok));
                    333: }
                    334:
                    335:
                    336:
1.19      kristaps  337: /*
                    338:  * Parse an implicit-block macro.  These contain a MAN_HEAD and a
                    339:  * MAN_BODY contained within a MAN_BLOCK.  Rules for closing out other
                    340:  * scopes, such as `SH' closing out an `SS', are defined in the rew
                    341:  * routines.
                    342:  */
                    343: int
                    344: blk_imp(MACRO_PROT_ARGS)
                    345: {
                    346:        int              w, la;
                    347:        char            *p;
1.25      kristaps  348:        struct man_node *n;
1.19      kristaps  349:
                    350:        /* Close out prior scopes. */
1.7       kristaps  351:
1.19      kristaps  352:        if ( ! rew_scope(MAN_BODY, m, tok))
1.5       kristaps  353:                return(0);
1.19      kristaps  354:        if ( ! rew_scope(MAN_BLOCK, m, tok))
1.6       kristaps  355:                return(0);
1.1       kristaps  356:
1.19      kristaps  357:        /* Allocate new block & head scope. */
                    358:
                    359:        if ( ! man_block_alloc(m, line, ppos, tok))
                    360:                return(0);
                    361:        if ( ! man_head_alloc(m, line, ppos, tok))
                    362:                return(0);
1.1       kristaps  363:
1.25      kristaps  364:        n = m->last;
                    365:
1.19      kristaps  366:        /* Add line arguments. */
1.3       kristaps  367:
1.19      kristaps  368:        for (;;) {
                    369:                la = *pos;
                    370:                w = man_args(m, line, pos, buf, &p);
1.4       kristaps  371:
1.19      kristaps  372:                if (-1 == w)
1.6       kristaps  373:                        return(0);
1.19      kristaps  374:                if (0 == w)
                    375:                        break;
                    376:
                    377:                if ( ! man_word_alloc(m, line, la, p))
1.6       kristaps  378:                        return(0);
                    379:        }
                    380:
1.19      kristaps  381:        /* Close out head and open body (unless MAN_SCOPE). */
                    382:
1.27      kristaps  383:        if (MAN_SCOPED & man_macros[tok].flags) {
                    384:                /* If we're forcing scope (`TP'), keep it open. */
                    385:                if (MAN_FSCOPED & man_macros[tok].flags) {
                    386:                        m->flags |= MAN_BLINE;
                    387:                        return(1);
                    388:                } else if (n == m->last) {
                    389:                        m->flags |= MAN_BLINE;
                    390:                        return(1);
                    391:                }
                    392:        }
                    393:
                    394:        if ( ! rew_scope(MAN_HEAD, m, tok))
1.6       kristaps  395:                return(0);
1.19      kristaps  396:        return(man_body_alloc(m, line, ppos, tok));
1.4       kristaps  397: }
                    398:
                    399:
1.19      kristaps  400: int
                    401: in_line_eoln(MACRO_PROT_ARGS)
1.3       kristaps  402: {
1.19      kristaps  403:        int              w, la;
                    404:        char            *p;
                    405:        struct man_node *n;
1.3       kristaps  406:
1.19      kristaps  407:        if ( ! man_elem_alloc(m, line, ppos, tok))
1.3       kristaps  408:                return(0);
                    409:
1.19      kristaps  410:        n = m->last;
1.3       kristaps  411:
1.19      kristaps  412:        for (;;) {
                    413:                la = *pos;
                    414:                w = man_args(m, line, pos, buf, &p);
1.3       kristaps  415:
1.19      kristaps  416:                if (-1 == w)
                    417:                        return(0);
                    418:                if (0 == w)
                    419:                        break;
                    420:                if ( ! man_word_alloc(m, line, la, p))
                    421:                        return(0);
                    422:        }
1.3       kristaps  423:
1.31      kristaps  424:        /*
                    425:         * If no arguments are specified and this is MAN_SCOPED (i.e.,
                    426:         * next-line scoped), then set our mode to indicate that we're
                    427:         * waiting for terms to load into our context.
                    428:         */
                    429:
1.25      kristaps  430:        if (n == m->last && MAN_SCOPED & man_macros[tok].flags) {
1.31      kristaps  431:                assert( ! (MAN_NSCOPED & man_macros[tok].flags));
1.19      kristaps  432:                m->flags |= MAN_ELINE;
                    433:                return(1);
                    434:        }
1.3       kristaps  435:
1.31      kristaps  436:        /* Set ignorable context, if applicable. */
                    437:
                    438:        if (MAN_NSCOPED & man_macros[tok].flags) {
                    439:                assert( ! (MAN_SCOPED & man_macros[tok].flags));
                    440:                m->flags |= MAN_ILINE;
                    441:        }
                    442:
1.19      kristaps  443:        /*
1.31      kristaps  444:         * Rewind our element scope.  Note that when TH is pruned, we'll
                    445:         * be back at the root, so make sure that we don't clobber as
                    446:         * its sibling.
1.19      kristaps  447:         */
1.3       kristaps  448:
1.19      kristaps  449:        for ( ; m->last; m->last = m->last->parent) {
                    450:                if (m->last == n)
                    451:                        break;
                    452:                if (m->last->type == MAN_ROOT)
                    453:                        break;
                    454:                if ( ! man_valid_post(m))
                    455:                        return(0);
                    456:                if ( ! man_action_post(m))
                    457:                        return(0);
                    458:        }
1.3       kristaps  459:
1.19      kristaps  460:        assert(m->last);
1.3       kristaps  461:
                    462:        /*
1.19      kristaps  463:         * Same here regarding whether we're back at the root.
1.3       kristaps  464:         */
                    465:
1.19      kristaps  466:        if (m->last->type != MAN_ROOT && ! man_valid_post(m))
                    467:                return(0);
                    468:        if (m->last->type != MAN_ROOT && ! man_action_post(m))
                    469:                return(0);
1.35      kristaps  470:
                    471:        m->next = MAN_ROOT == m->last->type ?
                    472:                MAN_NEXT_CHILD : MAN_NEXT_SIBLING;
1.3       kristaps  473:
1.19      kristaps  474:        return(1);
                    475: }
1.3       kristaps  476:
                    477:
1.19      kristaps  478: int
                    479: man_macroend(struct man *m)
                    480: {
1.22      kristaps  481:        struct man_node *n;
                    482:
                    483:        n = MAN_VALID & m->last->flags ?
                    484:                m->last->parent : m->last;
                    485:
                    486:        for ( ; n; n = n->parent) {
                    487:                if (MAN_BLOCK != n->type)
                    488:                        continue;
                    489:                if ( ! (MAN_EXPLICIT & man_macros[n->tok].flags))
                    490:                        continue;
1.26      kristaps  491:                if ( ! man_nwarn(m, n, WEXITSCOPE))
                    492:                        return(0);
1.22      kristaps  493:        }
1.3       kristaps  494:
1.19      kristaps  495:        return(man_unscope(m, m->first));
                    496: }
1.3       kristaps  497:

CVSweb