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

Annotation of mandoc/man_macro.c, Revision 1.28

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

CVSweb