[BACK]Return to man_macro.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/man_macro.c, Revision 1.11

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