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

Annotation of mandoc/man_macro.c, Revision 1.27

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

CVSweb