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

Annotation of mandoc/man_macro.c, Revision 1.23

1.23    ! kristaps    1: /*     $Id: man_macro.c,v 1.22 2009/08/19 12:15:58 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 */
                     41:        { blk_imp, 0 }, /* SH */
                     42:        { blk_imp, 0 }, /* SS */
                     43:        { blk_imp, MAN_SCOPED }, /* TP */
                     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;
                    239:
                    240:        /* Close out prior scopes. */
1.7       kristaps  241:
1.19      kristaps  242:        if ( ! rew_scope(MAN_BODY, m, tok))
1.5       kristaps  243:                return(0);
1.19      kristaps  244:        if ( ! rew_scope(MAN_BLOCK, m, tok))
1.6       kristaps  245:                return(0);
1.1       kristaps  246:
1.19      kristaps  247:        /* Allocate new block & head scope. */
                    248:
                    249:        if ( ! man_block_alloc(m, line, ppos, tok))
                    250:                return(0);
                    251:        if ( ! man_head_alloc(m, line, ppos, tok))
                    252:                return(0);
1.1       kristaps  253:
1.19      kristaps  254:        /* Add line arguments. */
1.3       kristaps  255:
1.19      kristaps  256:        for (;;) {
                    257:                la = *pos;
                    258:                w = man_args(m, line, pos, buf, &p);
1.4       kristaps  259:
1.19      kristaps  260:                if (-1 == w)
1.6       kristaps  261:                        return(0);
1.19      kristaps  262:                if (0 == w)
                    263:                        break;
                    264:
                    265:                if ( ! man_word_alloc(m, line, la, p))
1.6       kristaps  266:                        return(0);
                    267:        }
                    268:
1.19      kristaps  269:        /* Close out head and open body (unless MAN_SCOPE). */
                    270:
                    271:        if (MAN_SCOPED & man_macros[tok].flags) {
                    272:                m->flags |= MAN_BLINE;
                    273:                return(1);
                    274:        } else if ( ! rew_scope(MAN_HEAD, m, tok))
1.6       kristaps  275:                return(0);
                    276:
1.19      kristaps  277:        return(man_body_alloc(m, line, ppos, tok));
1.4       kristaps  278: }
                    279:
                    280:
1.19      kristaps  281: int
                    282: in_line_eoln(MACRO_PROT_ARGS)
1.3       kristaps  283: {
1.19      kristaps  284:        int              w, la;
                    285:        char            *p;
                    286:        struct man_node *n;
1.3       kristaps  287:
1.19      kristaps  288:        if ( ! man_elem_alloc(m, line, ppos, tok))
1.3       kristaps  289:                return(0);
                    290:
1.19      kristaps  291:        n = m->last;
1.3       kristaps  292:
1.19      kristaps  293:        for (;;) {
                    294:                la = *pos;
                    295:                w = man_args(m, line, pos, buf, &p);
1.3       kristaps  296:
1.19      kristaps  297:                if (-1 == w)
                    298:                        return(0);
                    299:                if (0 == w)
                    300:                        break;
1.3       kristaps  301:
1.19      kristaps  302:                if ( ! man_word_alloc(m, line, la, p))
                    303:                        return(0);
                    304:        }
1.3       kristaps  305:
1.19      kristaps  306:        if (n == m->last && (MAN_SCOPED & man_macros[tok].flags)) {
                    307:                m->flags |= MAN_ELINE;
                    308:                return(1);
                    309:        }
1.3       kristaps  310:
1.19      kristaps  311:        /*
                    312:         * Note that when TH is pruned, we'll be back at the root, so
                    313:         * make sure that we don't clobber as its sibling.
                    314:         */
1.3       kristaps  315:
1.19      kristaps  316:        for ( ; m->last; m->last = m->last->parent) {
                    317:                if (m->last == n)
                    318:                        break;
                    319:                if (m->last->type == MAN_ROOT)
                    320:                        break;
                    321:                if ( ! man_valid_post(m))
                    322:                        return(0);
                    323:                if ( ! man_action_post(m))
                    324:                        return(0);
                    325:        }
1.3       kristaps  326:
1.19      kristaps  327:        assert(m->last);
1.3       kristaps  328:
                    329:        /*
1.19      kristaps  330:         * Same here regarding whether we're back at the root.
1.3       kristaps  331:         */
                    332:
1.19      kristaps  333:        if (m->last->type != MAN_ROOT && ! man_valid_post(m))
                    334:                return(0);
                    335:        if (m->last->type != MAN_ROOT && ! man_action_post(m))
                    336:                return(0);
                    337:        if (m->last->type != MAN_ROOT)
                    338:                m->next = MAN_NEXT_SIBLING;
1.3       kristaps  339:
1.19      kristaps  340:        return(1);
                    341: }
1.3       kristaps  342:
                    343:
1.19      kristaps  344: int
                    345: man_macroend(struct man *m)
                    346: {
1.22      kristaps  347:        struct man_node *n;
                    348:
                    349:        n = MAN_VALID & m->last->flags ?
                    350:                m->last->parent : m->last;
                    351:
                    352:        for ( ; n; n = n->parent) {
                    353:                if (MAN_BLOCK != n->type)
                    354:                        continue;
                    355:                if ( ! (MAN_EXPLICIT & man_macros[n->tok].flags))
                    356:                        continue;
                    357:                return(man_nerr(m, n, WEXITSCOPE));
                    358:        }
1.3       kristaps  359:
1.19      kristaps  360:        return(man_unscope(m, m->first));
                    361: }
1.3       kristaps  362:

CVSweb