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

Annotation of mandoc/man_macro.c, Revision 1.32

1.32    ! kristaps    1: /*     $Id: man_macro.c,v 1.31 2010/03/22 05:59:32 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.19      kristaps   28: #define        REW_REWIND      (0)             /* See rew_scope(). */
                     29: #define        REW_NOHALT      (1)             /* See rew_scope(). */
                     30: #define        REW_HALT        (2)             /* See rew_scope(). */
                     31:
                     32: static int              in_line_eoln(MACRO_PROT_ARGS);
                     33: static int              blk_imp(MACRO_PROT_ARGS);
1.21      kristaps   34: static int              blk_close(MACRO_PROT_ARGS);
1.19      kristaps   35:
                     36: static int              rew_scope(enum man_type, struct man *, int);
                     37: static int              rew_dohalt(int, enum man_type,
                     38:                                const struct man_node *);
1.21      kristaps   39: static int              rew_block(int, enum man_type,
                     40:                                const struct man_node *);
1.19      kristaps   41:
                     42: const  struct man_macro __man_macros[MAN_MAX] = {
1.31      kristaps   43:        { in_line_eoln, MAN_NSCOPED }, /* br */
1.19      kristaps   44:        { in_line_eoln, 0 }, /* TH */
1.24      kristaps   45:        { blk_imp, MAN_SCOPED }, /* SH */
                     46:        { blk_imp, MAN_SCOPED }, /* SS */
1.27      kristaps   47:        { blk_imp, MAN_SCOPED | MAN_FSCOPED }, /* TP */
1.19      kristaps   48:        { blk_imp, 0 }, /* LP */
                     49:        { blk_imp, 0 }, /* PP */
                     50:        { blk_imp, 0 }, /* P */
                     51:        { blk_imp, 0 }, /* IP */
                     52:        { blk_imp, 0 }, /* HP */
                     53:        { in_line_eoln, MAN_SCOPED }, /* SM */
                     54:        { in_line_eoln, MAN_SCOPED }, /* SB */
                     55:        { in_line_eoln, 0 }, /* BI */
                     56:        { in_line_eoln, 0 }, /* IB */
                     57:        { in_line_eoln, 0 }, /* BR */
                     58:        { in_line_eoln, 0 }, /* RB */
                     59:        { in_line_eoln, MAN_SCOPED }, /* R */
                     60:        { in_line_eoln, MAN_SCOPED }, /* B */
                     61:        { in_line_eoln, MAN_SCOPED }, /* I */
                     62:        { in_line_eoln, 0 }, /* IR */
                     63:        { in_line_eoln, 0 }, /* RI */
1.31      kristaps   64:        { in_line_eoln, MAN_NSCOPED }, /* na */
1.19      kristaps   65:        { in_line_eoln, 0 }, /* i */
1.31      kristaps   66:        { in_line_eoln, MAN_NSCOPED }, /* sp */
1.19      kristaps   67:        { in_line_eoln, 0 }, /* nf */
                     68:        { in_line_eoln, 0 }, /* fi */
                     69:        { in_line_eoln, 0 }, /* r */
1.21      kristaps   70:        { blk_close, 0 }, /* RE */
1.22      kristaps   71:        { blk_imp, MAN_EXPLICIT }, /* RS */
1.23      kristaps   72:        { in_line_eoln, 0 }, /* DT */
1.28      kristaps   73:        { in_line_eoln, 0 }, /* UC */
1.29      kristaps   74:        { in_line_eoln, 0 }, /* PD */
1.32    ! kristaps   75:        { in_line_eoln, MAN_NSCOPED }, /* Sp */
        !            76:        { in_line_eoln, 0 }, /* Vb */
        !            77:        { in_line_eoln, 0 }, /* Ve */
1.19      kristaps   78: };
1.9       kristaps   79:
1.19      kristaps   80: const  struct man_macro * const man_macros = __man_macros;
1.1       kristaps   81:
                     82:
1.3       kristaps   83: int
1.19      kristaps   84: man_unscope(struct man *m, const struct man_node *n)
1.1       kristaps   85: {
                     86:
1.19      kristaps   87:        assert(n);
                     88:        m->next = MAN_NEXT_SIBLING;
                     89:
                     90:        /* LINTED */
                     91:        while (m->last != n) {
                     92:                if ( ! man_valid_post(m))
                     93:                        return(0);
                     94:                if ( ! man_action_post(m))
                     95:                        return(0);
                     96:                m->last = m->last->parent;
                     97:                assert(m->last);
                     98:        }
                     99:
                    100:        if ( ! man_valid_post(m))
1.1       kristaps  101:                return(0);
1.19      kristaps  102:        return(man_action_post(m));
                    103: }
1.1       kristaps  104:
                    105:
1.21      kristaps  106: static int
                    107: rew_block(int ntok, enum man_type type, const struct man_node *n)
                    108: {
                    109:
                    110:        if (MAN_BLOCK == type && ntok == n->parent->tok &&
                    111:                        MAN_BODY == n->parent->type)
                    112:                return(REW_REWIND);
                    113:        return(ntok == n->tok ? REW_HALT : REW_NOHALT);
                    114: }
                    115:
                    116:
1.19      kristaps  117: /*
                    118:  * There are three scope levels: scoped to the root (all), scoped to the
                    119:  * section (all less sections), and scoped to subsections (all less
                    120:  * sections and subsections).
                    121:  */
                    122: static int
                    123: rew_dohalt(int tok, enum man_type type, const struct man_node *n)
                    124: {
1.21      kristaps  125:        int              c;
1.1       kristaps  126:
1.19      kristaps  127:        if (MAN_ROOT == n->type)
                    128:                return(REW_HALT);
                    129:        assert(n->parent);
                    130:        if (MAN_ROOT == n->parent->type)
                    131:                return(REW_REWIND);
                    132:        if (MAN_VALID & n->flags)
                    133:                return(REW_NOHALT);
                    134:
1.21      kristaps  135:        /* Rewind to ourselves, first. */
                    136:        if (type == n->type && tok == n->tok)
                    137:                return(REW_REWIND);
                    138:
1.19      kristaps  139:        switch (tok) {
                    140:        case (MAN_SH):
                    141:                break;
                    142:        case (MAN_SS):
1.20      kristaps  143:                /* Rewind to a section, if a block. */
1.21      kristaps  144:                if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
                    145:                        return(c);
                    146:                break;
                    147:        case (MAN_RS):
                    148:                /* Rewind to a subsection, if a block. */
                    149:                if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
                    150:                        return(c);
                    151:                /* Rewind to a section, if a block. */
                    152:                if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
                    153:                        return(c);
1.19      kristaps  154:                break;
                    155:        default:
1.21      kristaps  156:                /* Rewind to an offsetter, if a block. */
                    157:                if (REW_NOHALT != (c = rew_block(MAN_RS, type, n)))
                    158:                        return(c);
1.20      kristaps  159:                /* Rewind to a subsection, if a block. */
1.21      kristaps  160:                if (REW_NOHALT != (c = rew_block(MAN_SS, type, n)))
                    161:                        return(c);
1.20      kristaps  162:                /* Rewind to a section, if a block. */
1.21      kristaps  163:                if (REW_NOHALT != (c = rew_block(MAN_SH, type, n)))
                    164:                        return(c);
1.19      kristaps  165:                break;
1.3       kristaps  166:        }
1.1       kristaps  167:
1.19      kristaps  168:        return(REW_NOHALT);
                    169: }
1.9       kristaps  170:
                    171:
1.19      kristaps  172: /*
                    173:  * Rewinding entails ascending the parse tree until a coherent point,
                    174:  * for example, the `SH' macro will close out any intervening `SS'
                    175:  * scopes.  When a scope is closed, it must be validated and actioned.
                    176:  */
                    177: static int
                    178: rew_scope(enum man_type type, struct man *m, int tok)
                    179: {
                    180:        struct man_node *n;
                    181:        int              c;
1.7       kristaps  182:
1.19      kristaps  183:        /* LINTED */
                    184:        for (n = m->last; n; n = n->parent) {
                    185:                /*
                    186:                 * Whether we should stop immediately (REW_HALT), stop
                    187:                 * and rewind until this point (REW_REWIND), or keep
                    188:                 * rewinding (REW_NOHALT).
                    189:                 */
                    190:                c = rew_dohalt(tok, type, n);
                    191:                if (REW_HALT == c)
                    192:                        return(1);
                    193:                if (REW_REWIND == c)
1.7       kristaps  194:                        break;
1.6       kristaps  195:        }
1.1       kristaps  196:
1.19      kristaps  197:        /* Rewind until the current point. */
                    198:
                    199:        assert(n);
                    200:        return(man_unscope(m, n));
                    201: }
                    202:
1.6       kristaps  203:
1.21      kristaps  204: /* ARGSUSED */
                    205: int
                    206: blk_close(MACRO_PROT_ARGS)
                    207: {
                    208:        int                      ntok;
                    209:        const struct man_node   *nn;
                    210:
                    211:        switch (tok) {
                    212:        case (MAN_RE):
                    213:                ntok = MAN_RS;
                    214:                break;
                    215:        default:
                    216:                abort();
                    217:                /* NOTREACHED */
                    218:        }
                    219:
                    220:        for (nn = m->last->parent; nn; nn = nn->parent)
                    221:                if (ntok == nn->tok)
                    222:                        break;
                    223:
                    224:        if (NULL == nn)
                    225:                if ( ! man_pwarn(m, line, ppos, WNOSCOPE))
                    226:                        return(0);
                    227:
                    228:        if ( ! rew_scope(MAN_BODY, m, ntok))
                    229:                return(0);
                    230:        if ( ! rew_scope(MAN_BLOCK, m, ntok))
                    231:                return(0);
                    232:        m->next = MAN_NEXT_SIBLING;
                    233:        return(1);
                    234: }
                    235:
                    236:
1.19      kristaps  237: /*
                    238:  * Parse an implicit-block macro.  These contain a MAN_HEAD and a
                    239:  * MAN_BODY contained within a MAN_BLOCK.  Rules for closing out other
                    240:  * scopes, such as `SH' closing out an `SS', are defined in the rew
                    241:  * routines.
                    242:  */
                    243: int
                    244: blk_imp(MACRO_PROT_ARGS)
                    245: {
                    246:        int              w, la;
                    247:        char            *p;
1.25      kristaps  248:        struct man_node *n;
1.19      kristaps  249:
                    250:        /* Close out prior scopes. */
1.7       kristaps  251:
1.19      kristaps  252:        if ( ! rew_scope(MAN_BODY, m, tok))
1.5       kristaps  253:                return(0);
1.19      kristaps  254:        if ( ! rew_scope(MAN_BLOCK, m, tok))
1.6       kristaps  255:                return(0);
1.1       kristaps  256:
1.19      kristaps  257:        /* Allocate new block & head scope. */
                    258:
                    259:        if ( ! man_block_alloc(m, line, ppos, tok))
                    260:                return(0);
                    261:        if ( ! man_head_alloc(m, line, ppos, tok))
                    262:                return(0);
1.1       kristaps  263:
1.25      kristaps  264:        n = m->last;
                    265:
1.19      kristaps  266:        /* Add line arguments. */
1.3       kristaps  267:
1.19      kristaps  268:        for (;;) {
                    269:                la = *pos;
                    270:                w = man_args(m, line, pos, buf, &p);
1.4       kristaps  271:
1.19      kristaps  272:                if (-1 == w)
1.6       kristaps  273:                        return(0);
1.19      kristaps  274:                if (0 == w)
                    275:                        break;
                    276:
                    277:                if ( ! man_word_alloc(m, line, la, p))
1.6       kristaps  278:                        return(0);
                    279:        }
                    280:
1.19      kristaps  281:        /* Close out head and open body (unless MAN_SCOPE). */
                    282:
1.27      kristaps  283:        if (MAN_SCOPED & man_macros[tok].flags) {
                    284:                /* If we're forcing scope (`TP'), keep it open. */
                    285:                if (MAN_FSCOPED & man_macros[tok].flags) {
                    286:                        m->flags |= MAN_BLINE;
                    287:                        return(1);
                    288:                } else if (n == m->last) {
                    289:                        m->flags |= MAN_BLINE;
                    290:                        return(1);
                    291:                }
                    292:        }
                    293:
                    294:        if ( ! rew_scope(MAN_HEAD, m, tok))
1.6       kristaps  295:                return(0);
                    296:
1.19      kristaps  297:        return(man_body_alloc(m, line, ppos, tok));
1.4       kristaps  298: }
                    299:
                    300:
1.19      kristaps  301: int
                    302: in_line_eoln(MACRO_PROT_ARGS)
1.3       kristaps  303: {
1.19      kristaps  304:        int              w, la;
                    305:        char            *p;
                    306:        struct man_node *n;
1.3       kristaps  307:
1.19      kristaps  308:        if ( ! man_elem_alloc(m, line, ppos, tok))
1.3       kristaps  309:                return(0);
                    310:
1.19      kristaps  311:        n = m->last;
1.3       kristaps  312:
1.19      kristaps  313:        for (;;) {
                    314:                la = *pos;
                    315:                w = man_args(m, line, pos, buf, &p);
1.3       kristaps  316:
1.19      kristaps  317:                if (-1 == w)
                    318:                        return(0);
                    319:                if (0 == w)
                    320:                        break;
1.32    ! kristaps  321:
        !           322:                /* XXX ignore Vb arguments for now */
        !           323:                if (MAN_Vb == tok)
        !           324:                        continue;
1.3       kristaps  325:
1.19      kristaps  326:                if ( ! man_word_alloc(m, line, la, p))
                    327:                        return(0);
                    328:        }
1.3       kristaps  329:
1.31      kristaps  330:        /*
                    331:         * If no arguments are specified and this is MAN_SCOPED (i.e.,
                    332:         * next-line scoped), then set our mode to indicate that we're
                    333:         * waiting for terms to load into our context.
                    334:         */
                    335:
1.25      kristaps  336:        if (n == m->last && MAN_SCOPED & man_macros[tok].flags) {
1.31      kristaps  337:                assert( ! (MAN_NSCOPED & man_macros[tok].flags));
1.19      kristaps  338:                m->flags |= MAN_ELINE;
                    339:                return(1);
                    340:        }
1.3       kristaps  341:
1.31      kristaps  342:        /* Set ignorable context, if applicable. */
                    343:
                    344:        if (MAN_NSCOPED & man_macros[tok].flags) {
                    345:                assert( ! (MAN_SCOPED & man_macros[tok].flags));
                    346:                m->flags |= MAN_ILINE;
                    347:        }
                    348:
1.19      kristaps  349:        /*
1.31      kristaps  350:         * Rewind our element scope.  Note that when TH is pruned, we'll
                    351:         * be back at the root, so make sure that we don't clobber as
                    352:         * its sibling.
1.19      kristaps  353:         */
1.3       kristaps  354:
1.19      kristaps  355:        for ( ; m->last; m->last = m->last->parent) {
                    356:                if (m->last == n)
                    357:                        break;
                    358:                if (m->last->type == MAN_ROOT)
                    359:                        break;
                    360:                if ( ! man_valid_post(m))
                    361:                        return(0);
                    362:                if ( ! man_action_post(m))
                    363:                        return(0);
                    364:        }
1.3       kristaps  365:
1.19      kristaps  366:        assert(m->last);
1.3       kristaps  367:
                    368:        /*
1.19      kristaps  369:         * Same here regarding whether we're back at the root.
1.3       kristaps  370:         */
                    371:
1.19      kristaps  372:        if (m->last->type != MAN_ROOT && ! man_valid_post(m))
                    373:                return(0);
                    374:        if (m->last->type != MAN_ROOT && ! man_action_post(m))
                    375:                return(0);
                    376:        if (m->last->type != MAN_ROOT)
                    377:                m->next = MAN_NEXT_SIBLING;
1.3       kristaps  378:
1.19      kristaps  379:        return(1);
                    380: }
1.3       kristaps  381:
                    382:
1.19      kristaps  383: int
                    384: man_macroend(struct man *m)
                    385: {
1.22      kristaps  386:        struct man_node *n;
                    387:
                    388:        n = MAN_VALID & m->last->flags ?
                    389:                m->last->parent : m->last;
                    390:
                    391:        for ( ; n; n = n->parent) {
                    392:                if (MAN_BLOCK != n->type)
                    393:                        continue;
                    394:                if ( ! (MAN_EXPLICIT & man_macros[n->tok].flags))
                    395:                        continue;
1.26      kristaps  396:                if ( ! man_nwarn(m, n, WEXITSCOPE))
                    397:                        return(0);
1.22      kristaps  398:        }
1.3       kristaps  399:
1.19      kristaps  400:        return(man_unscope(m, m->first));
                    401: }
1.3       kristaps  402:

CVSweb