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

Annotation of mandoc/man.c, Revision 1.120

1.120   ! schwarze    1: /*     $Id: man.c,v 1.119 2012/11/17 00:26:33 schwarze Exp $ */
1.1       kristaps    2: /*
1.102     schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.18      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.18      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.47      kristaps   17: #ifdef HAVE_CONFIG_H
                     18: #include "config.h"
                     19: #endif
                     20:
1.41      kristaps   21: #include <sys/types.h>
                     22:
1.1       kristaps   23: #include <assert.h>
                     24: #include <stdarg.h>
                     25: #include <stdlib.h>
                     26: #include <stdio.h>
                     27: #include <string.h>
                     28:
1.105     kristaps   29: #include "man.h"
1.74      kristaps   30: #include "mandoc.h"
1.1       kristaps   31: #include "libman.h"
1.45      kristaps   32: #include "libmandoc.h"
1.1       kristaps   33:
                     34: const  char *const __man_macronames[MAN_MAX] = {
1.21      kristaps   35:        "br",           "TH",           "SH",           "SS",
1.1       kristaps   36:        "TP",           "LP",           "PP",           "P",
                     37:        "IP",           "HP",           "SM",           "SB",
                     38:        "BI",           "IB",           "BR",           "RB",
1.11      kristaps   39:        "R",            "B",            "I",            "IR",
1.92      kristaps   40:        "RI",           "na",           "sp",           "nf",
                     41:        "fi",           "RE",           "RS",           "DT",
                     42:        "UC",           "PD",           "AT",           "in",
1.120   ! schwarze   43:        "ft",           "OP",           "EX",           "EE",
        !            44:        "UR",           "UE"
1.1       kristaps   45:        };
                     46:
                     47: const  char * const *man_macronames = __man_macronames;
                     48:
1.97      kristaps   49: static struct man_node *man_node_alloc(struct man *, int, int,
1.53      kristaps   50:                                enum man_type, enum mant);
1.1       kristaps   51: static int              man_node_append(struct man *,
                     52:                                struct man_node *);
1.54      kristaps   53: static void             man_node_free(struct man_node *);
                     54: static void             man_node_unlink(struct man *,
                     55:                                struct man_node *);
1.72      kristaps   56: static int              man_ptext(struct man *, int, char *, int);
1.79      kristaps   57: static int              man_pmacro(struct man *, int, char *, int);
1.2       kristaps   58: static void             man_free1(struct man *);
1.45      kristaps   59: static void             man_alloc1(struct man *);
1.94      kristaps   60: static int              man_descope(struct man *, int, int);
1.1       kristaps   61:
                     62:
                     63: const struct man_node *
1.119     schwarze   64: man_node(const struct man *man)
1.1       kristaps   65: {
                     66:
1.119     schwarze   67:        assert( ! (MAN_HALT & man->flags));
                     68:        return(man->first);
1.1       kristaps   69: }
                     70:
                     71:
                     72: const struct man_meta *
1.119     schwarze   73: man_meta(const struct man *man)
1.1       kristaps   74: {
                     75:
1.119     schwarze   76:        assert( ! (MAN_HALT & man->flags));
                     77:        return(&man->meta);
1.1       kristaps   78: }
                     79:
                     80:
1.45      kristaps   81: void
1.1       kristaps   82: man_reset(struct man *man)
                     83: {
                     84:
1.2       kristaps   85:        man_free1(man);
1.45      kristaps   86:        man_alloc1(man);
1.1       kristaps   87: }
                     88:
                     89:
                     90: void
                     91: man_free(struct man *man)
                     92: {
                     93:
1.2       kristaps   94:        man_free1(man);
1.1       kristaps   95:        free(man);
                     96: }
                     97:
                     98:
                     99: struct man *
1.108     kristaps  100: man_alloc(struct roff *roff, struct mparse *parse)
1.1       kristaps  101: {
                    102:        struct man      *p;
                    103:
1.45      kristaps  104:        p = mandoc_calloc(1, sizeof(struct man));
1.2       kristaps  105:
1.40      kristaps  106:        man_hash_init();
1.104     kristaps  107:        p->parse = parse;
1.108     kristaps  108:        p->roff = roff;
1.45      kristaps  109:
                    110:        man_alloc1(p);
1.1       kristaps  111:        return(p);
                    112: }
                    113:
                    114:
                    115: int
1.119     schwarze  116: man_endparse(struct man *man)
1.1       kristaps  117: {
                    118:
1.119     schwarze  119:        assert( ! (MAN_HALT & man->flags));
                    120:        if (man_macroend(man))
1.3       kristaps  121:                return(1);
1.119     schwarze  122:        man->flags |= MAN_HALT;
1.3       kristaps  123:        return(0);
1.1       kristaps  124: }
                    125:
                    126:
                    127: int
1.119     schwarze  128: man_parseln(struct man *man, int ln, char *buf, int offs)
1.1       kristaps  129: {
                    130:
1.119     schwarze  131:        man->flags |= MAN_NEWLINE;
1.97      kristaps  132:
1.119     schwarze  133:        assert( ! (MAN_HALT & man->flags));
1.107     kristaps  134:
1.119     schwarze  135:        return (roff_getcontrol(man->roff, buf, &offs) ?
                    136:                        man_pmacro(man, ln, buf, offs) :
                    137:                        man_ptext(man, ln, buf, offs));
1.1       kristaps  138: }
                    139:
                    140:
1.2       kristaps  141: static void
                    142: man_free1(struct man *man)
                    143: {
                    144:
                    145:        if (man->first)
1.54      kristaps  146:                man_node_delete(man, man->first);
1.2       kristaps  147:        if (man->meta.title)
                    148:                free(man->meta.title);
1.6       kristaps  149:        if (man->meta.source)
                    150:                free(man->meta.source);
1.102     schwarze  151:        if (man->meta.date)
                    152:                free(man->meta.date);
1.2       kristaps  153:        if (man->meta.vol)
                    154:                free(man->meta.vol);
1.68      kristaps  155:        if (man->meta.msec)
                    156:                free(man->meta.msec);
1.2       kristaps  157: }
                    158:
                    159:
1.45      kristaps  160: static void
1.119     schwarze  161: man_alloc1(struct man *man)
1.2       kristaps  162: {
                    163:
1.119     schwarze  164:        memset(&man->meta, 0, sizeof(struct man_meta));
                    165:        man->flags = 0;
                    166:        man->last = mandoc_calloc(1, sizeof(struct man_node));
                    167:        man->first = man->last;
                    168:        man->last->type = MAN_ROOT;
                    169:        man->last->tok = MAN_MAX;
                    170:        man->next = MAN_NEXT_CHILD;
1.2       kristaps  171: }
                    172:
                    173:
1.1       kristaps  174: static int
                    175: man_node_append(struct man *man, struct man_node *p)
                    176: {
                    177:
                    178:        assert(man->last);
                    179:        assert(man->first);
                    180:        assert(MAN_ROOT != p->type);
                    181:
                    182:        switch (man->next) {
                    183:        case (MAN_NEXT_SIBLING):
                    184:                man->last->next = p;
                    185:                p->prev = man->last;
                    186:                p->parent = man->last->parent;
                    187:                break;
                    188:        case (MAN_NEXT_CHILD):
                    189:                man->last->child = p;
                    190:                p->parent = man->last;
                    191:                break;
                    192:        default:
                    193:                abort();
                    194:                /* NOTREACHED */
                    195:        }
1.22      kristaps  196:
1.54      kristaps  197:        assert(p->parent);
1.22      kristaps  198:        p->parent->nchild++;
1.1       kristaps  199:
1.29      kristaps  200:        if ( ! man_valid_pre(man, p))
                    201:                return(0);
                    202:
                    203:        switch (p->type) {
                    204:        case (MAN_HEAD):
                    205:                assert(MAN_BLOCK == p->parent->type);
                    206:                p->parent->head = p;
                    207:                break;
1.106     kristaps  208:        case (MAN_TAIL):
                    209:                assert(MAN_BLOCK == p->parent->type);
                    210:                p->parent->tail = p;
                    211:                break;
1.29      kristaps  212:        case (MAN_BODY):
                    213:                assert(MAN_BLOCK == p->parent->type);
                    214:                p->parent->body = p;
                    215:                break;
                    216:        default:
                    217:                break;
                    218:        }
                    219:
1.2       kristaps  220:        man->last = p;
                    221:
1.1       kristaps  222:        switch (p->type) {
1.94      kristaps  223:        case (MAN_TBL):
                    224:                /* FALLTHROUGH */
1.2       kristaps  225:        case (MAN_TEXT):
                    226:                if ( ! man_valid_post(man))
                    227:                        return(0);
1.1       kristaps  228:                break;
                    229:        default:
                    230:                break;
                    231:        }
                    232:
                    233:        return(1);
                    234: }
                    235:
                    236:
                    237: static struct man_node *
1.119     schwarze  238: man_node_alloc(struct man *man, int line, int pos,
1.97      kristaps  239:                enum man_type type, enum mant tok)
1.1       kristaps  240: {
                    241:        struct man_node *p;
                    242:
1.45      kristaps  243:        p = mandoc_calloc(1, sizeof(struct man_node));
1.1       kristaps  244:        p->line = line;
                    245:        p->pos = pos;
                    246:        p->type = type;
1.16      kristaps  247:        p->tok = tok;
1.97      kristaps  248:
1.119     schwarze  249:        if (MAN_NEWLINE & man->flags)
1.97      kristaps  250:                p->flags |= MAN_LINE;
1.119     schwarze  251:        man->flags &= ~MAN_NEWLINE;
1.1       kristaps  252:        return(p);
                    253: }
                    254:
                    255:
                    256: int
1.119     schwarze  257: man_elem_alloc(struct man *man, int line, int pos, enum mant tok)
1.1       kristaps  258: {
                    259:        struct man_node *p;
                    260:
1.119     schwarze  261:        p = man_node_alloc(man, line, pos, MAN_ELEM, tok);
                    262:        if ( ! man_node_append(man, p))
1.106     kristaps  263:                return(0);
1.119     schwarze  264:        man->next = MAN_NEXT_CHILD;
1.106     kristaps  265:        return(1);
                    266: }
                    267:
                    268:
                    269: int
1.119     schwarze  270: man_tail_alloc(struct man *man, int line, int pos, enum mant tok)
1.106     kristaps  271: {
                    272:        struct man_node *p;
                    273:
1.119     schwarze  274:        p = man_node_alloc(man, line, pos, MAN_TAIL, tok);
                    275:        if ( ! man_node_append(man, p))
1.30      kristaps  276:                return(0);
1.119     schwarze  277:        man->next = MAN_NEXT_CHILD;
1.30      kristaps  278:        return(1);
1.1       kristaps  279: }
                    280:
                    281:
                    282: int
1.119     schwarze  283: man_head_alloc(struct man *man, int line, int pos, enum mant tok)
1.29      kristaps  284: {
                    285:        struct man_node *p;
                    286:
1.119     schwarze  287:        p = man_node_alloc(man, line, pos, MAN_HEAD, tok);
                    288:        if ( ! man_node_append(man, p))
1.29      kristaps  289:                return(0);
1.119     schwarze  290:        man->next = MAN_NEXT_CHILD;
1.29      kristaps  291:        return(1);
                    292: }
                    293:
                    294:
                    295: int
1.119     schwarze  296: man_body_alloc(struct man *man, int line, int pos, enum mant tok)
1.29      kristaps  297: {
                    298:        struct man_node *p;
                    299:
1.119     schwarze  300:        p = man_node_alloc(man, line, pos, MAN_BODY, tok);
                    301:        if ( ! man_node_append(man, p))
1.29      kristaps  302:                return(0);
1.119     schwarze  303:        man->next = MAN_NEXT_CHILD;
1.29      kristaps  304:        return(1);
                    305: }
                    306:
                    307:
                    308: int
1.119     schwarze  309: man_block_alloc(struct man *man, int line, int pos, enum mant tok)
1.29      kristaps  310: {
                    311:        struct man_node *p;
                    312:
1.119     schwarze  313:        p = man_node_alloc(man, line, pos, MAN_BLOCK, tok);
                    314:        if ( ! man_node_append(man, p))
1.29      kristaps  315:                return(0);
1.119     schwarze  316:        man->next = MAN_NEXT_CHILD;
1.29      kristaps  317:        return(1);
                    318: }
                    319:
1.61      kristaps  320: int
1.119     schwarze  321: man_word_alloc(struct man *man, int line, int pos, const char *word)
1.1       kristaps  322: {
1.31      kristaps  323:        struct man_node *n;
1.1       kristaps  324:
1.119     schwarze  325:        n = man_node_alloc(man, line, pos, MAN_TEXT, MAN_MAX);
                    326:        n->string = roff_strdup(man->roff, word);
1.31      kristaps  327:
1.119     schwarze  328:        if ( ! man_node_append(man, n))
1.30      kristaps  329:                return(0);
1.61      kristaps  330:
1.119     schwarze  331:        man->next = MAN_NEXT_SIBLING;
1.30      kristaps  332:        return(1);
1.1       kristaps  333: }
                    334:
                    335:
1.54      kristaps  336: /*
                    337:  * Free all of the resources held by a node.  This does NOT unlink a
                    338:  * node from its context; for that, see man_node_unlink().
                    339:  */
                    340: static void
1.1       kristaps  341: man_node_free(struct man_node *p)
                    342: {
                    343:
                    344:        if (p->string)
                    345:                free(p->string);
                    346:        free(p);
                    347: }
                    348:
                    349:
                    350: void
1.119     schwarze  351: man_node_delete(struct man *man, struct man_node *p)
1.1       kristaps  352: {
                    353:
1.54      kristaps  354:        while (p->child)
1.119     schwarze  355:                man_node_delete(man, p->child);
1.54      kristaps  356:
1.119     schwarze  357:        man_node_unlink(man, p);
1.1       kristaps  358:        man_node_free(p);
                    359: }
                    360:
1.101     kristaps  361: int
1.119     schwarze  362: man_addeqn(struct man *man, const struct eqn *ep)
1.101     kristaps  363: {
                    364:        struct man_node *n;
                    365:
1.119     schwarze  366:        assert( ! (MAN_HALT & man->flags));
1.101     kristaps  367:
1.119     schwarze  368:        n = man_node_alloc(man, ep->ln, ep->pos, MAN_EQN, MAN_MAX);
1.101     kristaps  369:        n->eqn = ep;
                    370:
1.119     schwarze  371:        if ( ! man_node_append(man, n))
1.101     kristaps  372:                return(0);
                    373:
1.119     schwarze  374:        man->next = MAN_NEXT_SIBLING;
                    375:        return(man_descope(man, ep->ln, ep->pos));
1.101     kristaps  376: }
1.1       kristaps  377:
1.94      kristaps  378: int
1.119     schwarze  379: man_addspan(struct man *man, const struct tbl_span *sp)
1.94      kristaps  380: {
1.100     kristaps  381:        struct man_node *n;
1.94      kristaps  382:
1.119     schwarze  383:        assert( ! (MAN_HALT & man->flags));
1.100     kristaps  384:
1.119     schwarze  385:        n = man_node_alloc(man, sp->line, 0, MAN_TBL, MAN_MAX);
1.100     kristaps  386:        n->span = sp;
                    387:
1.119     schwarze  388:        if ( ! man_node_append(man, n))
1.94      kristaps  389:                return(0);
1.100     kristaps  390:
1.119     schwarze  391:        man->next = MAN_NEXT_SIBLING;
                    392:        return(man_descope(man, sp->line, 0));
1.94      kristaps  393: }
                    394:
                    395: static int
1.119     schwarze  396: man_descope(struct man *man, int line, int offs)
1.94      kristaps  397: {
                    398:        /*
                    399:         * Co-ordinate what happens with having a next-line scope open:
                    400:         * first close out the element scope (if applicable), then close
                    401:         * out the block scope (also if applicable).
                    402:         */
                    403:
1.119     schwarze  404:        if (MAN_ELINE & man->flags) {
                    405:                man->flags &= ~MAN_ELINE;
                    406:                if ( ! man_unscope(man, man->last->parent, MANDOCERR_MAX))
1.94      kristaps  407:                        return(0);
                    408:        }
                    409:
1.119     schwarze  410:        if ( ! (MAN_BLINE & man->flags))
1.94      kristaps  411:                return(1);
1.119     schwarze  412:        man->flags &= ~MAN_BLINE;
1.94      kristaps  413:
1.119     schwarze  414:        if ( ! man_unscope(man, man->last->parent, MANDOCERR_MAX))
1.94      kristaps  415:                return(0);
1.119     schwarze  416:        return(man_body_alloc(man, line, offs, man->last->tok));
1.94      kristaps  417: }
                    418:
1.1       kristaps  419: static int
1.119     schwarze  420: man_ptext(struct man *man, int line, char *buf, int offs)
1.1       kristaps  421: {
1.61      kristaps  422:        int              i;
1.60      kristaps  423:
1.35      kristaps  424:        /* Literal free-form text whitespace is preserved. */
                    425:
1.119     schwarze  426:        if (MAN_LITERAL & man->flags) {
                    427:                if ( ! man_word_alloc(man, line, offs, buf + offs))
1.35      kristaps  428:                        return(0);
1.119     schwarze  429:                return(man_descope(man, line, offs));
1.35      kristaps  430:        }
                    431:
1.61      kristaps  432:        /* Pump blank lines directly into the backend. */
1.31      kristaps  433:
1.72      kristaps  434:        for (i = offs; ' ' == buf[i]; i++)
1.31      kristaps  435:                /* Skip leading whitespace. */ ;
1.48      kristaps  436:
1.49      kristaps  437:        if ('\0' == buf[i]) {
1.61      kristaps  438:                /* Allocate a blank entry. */
1.119     schwarze  439:                if ( ! man_elem_alloc(man, line, offs, MAN_sp))
1.31      kristaps  440:                        return(0);
1.119     schwarze  441:                man->next = MAN_NEXT_SIBLING;
1.118     schwarze  442:                return(1);
1.31      kristaps  443:        }
                    444:
1.63      kristaps  445:        /*
                    446:         * Warn if the last un-escaped character is whitespace. Then
                    447:         * strip away the remaining spaces (tabs stay!).
                    448:         */
1.29      kristaps  449:
1.61      kristaps  450:        i = (int)strlen(buf);
                    451:        assert(i);
1.49      kristaps  452:
1.63      kristaps  453:        if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
1.64      kristaps  454:                if (i > 1 && '\\' != buf[i - 2])
1.119     schwarze  455:                        man_pmsg(man, line, i - 1, MANDOCERR_EOLNSPACE);
1.63      kristaps  456:
                    457:                for (--i; i && ' ' == buf[i]; i--)
                    458:                        /* Spin back to non-space. */ ;
                    459:
                    460:                /* Jump ahead of escaped whitespace. */
                    461:                i += '\\' == buf[i] ? 2 : 1;
                    462:
                    463:                buf[i] = '\0';
                    464:        }
1.49      kristaps  465:
1.119     schwarze  466:        if ( ! man_word_alloc(man, line, offs, buf + offs))
1.1       kristaps  467:                return(0);
1.65      kristaps  468:
                    469:        /*
                    470:         * End-of-sentence check.  If the last character is an unescaped
                    471:         * EOS character, then flag the node as being the end of a
                    472:         * sentence.  The front-end will know how to interpret this.
                    473:         */
1.67      kristaps  474:
1.65      kristaps  475:        assert(i);
1.83      schwarze  476:        if (mandoc_eos(buf, (size_t)i, 0))
1.119     schwarze  477:                man->last->flags |= MAN_EOS;
1.31      kristaps  478:
1.119     schwarze  479:        return(man_descope(man, line, offs));
1.1       kristaps  480: }
                    481:
1.96      kristaps  482: static int
1.119     schwarze  483: man_pmacro(struct man *man, int ln, char *buf, int offs)
1.1       kristaps  484: {
1.107     kristaps  485:        int              i, ppos;
1.53      kristaps  486:        enum mant        tok;
1.34      kristaps  487:        char             mac[5];
                    488:        struct man_node *n;
1.1       kristaps  489:
1.107     kristaps  490:        if ('"' == buf[offs]) {
1.119     schwarze  491:                man_pmsg(man, ln, offs, MANDOCERR_BADCOMMENT);
1.107     kristaps  492:                return(1);
                    493:        } else if ('\0' == buf[offs])
1.46      kristaps  494:                return(1);
1.1       kristaps  495:
1.107     kristaps  496:        ppos = offs;
1.9       kristaps  497:
1.56      kristaps  498:        /*
1.107     kristaps  499:         * Copy the first word into a nil-terminated buffer.
                    500:         * Stop copying when a tab, space, or eoln is encountered.
1.56      kristaps  501:         */
1.62      kristaps  502:
1.107     kristaps  503:        i = 0;
                    504:        while (i < 4 && '\0' != buf[offs] &&
                    505:                        ' ' != buf[offs] && '\t' != buf[offs])
                    506:                mac[i++] = buf[offs++];
1.10      kristaps  507:
1.107     kristaps  508:        mac[i] = '\0';
1.1       kristaps  509:
1.107     kristaps  510:        tok = (i > 0 && i < 4) ? man_hash_find(mac) : MAN_MAX;
1.1       kristaps  511:
1.87      schwarze  512:        if (MAN_MAX == tok) {
1.119     schwarze  513:                mandoc_vmsg(MANDOCERR_MACRO, man->parse, ln,
1.104     kristaps  514:                                ppos, "%s", buf + ppos - 1);
1.12      kristaps  515:                return(1);
1.1       kristaps  516:        }
                    517:
                    518:        /* The macro is sane.  Jump to the next word. */
                    519:
1.107     kristaps  520:        while (buf[offs] && ' ' == buf[offs])
                    521:                offs++;
1.1       kristaps  522:
1.62      kristaps  523:        /*
                    524:         * Trailing whitespace.  Note that tabs are allowed to be passed
                    525:         * into the parser as "text", so we only warn about spaces here.
                    526:         */
1.48      kristaps  527:
1.107     kristaps  528:        if ('\0' == buf[offs] && ' ' == buf[offs - 1])
1.119     schwarze  529:                man_pmsg(man, ln, offs - 1, MANDOCERR_EOLNSPACE);
1.48      kristaps  530:
1.50      kristaps  531:        /*
1.90      kristaps  532:         * Remove prior ELINE macro, as it's being clobbered by a new
1.51      kristaps  533:         * macro.  Note that NSCOPED macros do not close out ELINE
                    534:         * macros---they don't print text---so we let those slip by.
1.50      kristaps  535:         */
1.34      kristaps  536:
1.53      kristaps  537:        if ( ! (MAN_NSCOPED & man_macros[tok].flags) &&
1.119     schwarze  538:                        man->flags & MAN_ELINE) {
                    539:                n = man->last;
1.90      kristaps  540:                assert(MAN_TEXT != n->type);
1.51      kristaps  541:
1.90      kristaps  542:                /* Remove repeated NSCOPED macros causing ELINE. */
1.51      kristaps  543:
1.90      kristaps  544:                if (MAN_NSCOPED & man_macros[n->tok].flags)
                    545:                        n = n->parent;
1.51      kristaps  546:
1.119     schwarze  547:                mandoc_vmsg(MANDOCERR_LINESCOPE, man->parse, n->line,
1.113     schwarze  548:                    n->pos, "%s breaks %s", man_macronames[tok],
                    549:                    man_macronames[n->tok]);
1.34      kristaps  550:
1.119     schwarze  551:                man_node_delete(man, n);
                    552:                man->flags &= ~MAN_ELINE;
1.113     schwarze  553:        }
                    554:
                    555:        /*
                    556:         * Remove prior BLINE macro that is being clobbered.
                    557:         */
1.119     schwarze  558:        if ((man->flags & MAN_BLINE) &&
1.113     schwarze  559:            (MAN_BSCOPE & man_macros[tok].flags)) {
1.119     schwarze  560:                n = man->last;
1.114     joerg     561:
                    562:                /* Might be a text node like 8 in
                    563:                 * .TP 8
                    564:                 * .SH foo
                    565:                 */
                    566:                if (MAN_TEXT == n->type)
                    567:                        n = n->parent;
1.113     schwarze  568:
                    569:                /* Remove element that didn't end BLINE, if any. */
                    570:                if ( ! (MAN_BSCOPE & man_macros[n->tok].flags))
                    571:                        n = n->parent;
                    572:
                    573:                assert(MAN_HEAD == n->type);
                    574:                n = n->parent;
                    575:                assert(MAN_BLOCK == n->type);
                    576:                assert(MAN_SCOPED & man_macros[n->tok].flags);
                    577:
1.119     schwarze  578:                mandoc_vmsg(MANDOCERR_LINESCOPE, man->parse, n->line,
1.113     schwarze  579:                    n->pos, "%s breaks %s", man_macronames[tok],
                    580:                    man_macronames[n->tok]);
                    581:
1.119     schwarze  582:                man_node_delete(man, n);
                    583:                man->flags &= ~MAN_BLINE;
1.34      kristaps  584:        }
                    585:
1.59      kristaps  586:        /*
                    587:         * Save the fact that we're in the next-line for a block.  In
                    588:         * this way, embedded roff instructions can "remember" state
                    589:         * when they exit.
                    590:         */
                    591:
1.119     schwarze  592:        if (MAN_BLINE & man->flags)
                    593:                man->flags |= MAN_BPLINE;
1.59      kristaps  594:
                    595:        /* Call to handler... */
1.1       kristaps  596:
1.53      kristaps  597:        assert(man_macros[tok].fp);
1.119     schwarze  598:        if ( ! (*man_macros[tok].fp)(man, tok, ln, ppos, &offs, buf))
1.1       kristaps  599:                goto err;
                    600:
1.50      kristaps  601:        /*
                    602:         * We weren't in a block-line scope when entering the
                    603:         * above-parsed macro, so return.
                    604:         */
                    605:
1.119     schwarze  606:        if ( ! (MAN_BPLINE & man->flags)) {
                    607:                man->flags &= ~MAN_ILINE;
1.29      kristaps  608:                return(1);
1.50      kristaps  609:        }
1.119     schwarze  610:        man->flags &= ~MAN_BPLINE;
1.50      kristaps  611:
                    612:        /*
                    613:         * If we're in a block scope, then allow this macro to slip by
                    614:         * without closing scope around it.
                    615:         */
                    616:
1.119     schwarze  617:        if (MAN_ILINE & man->flags) {
                    618:                man->flags &= ~MAN_ILINE;
1.50      kristaps  619:                return(1);
                    620:        }
1.29      kristaps  621:
                    622:        /*
                    623:         * If we've opened a new next-line element scope, then return
                    624:         * now, as the next line will close out the block scope.
                    625:         */
                    626:
1.119     schwarze  627:        if (MAN_ELINE & man->flags)
1.29      kristaps  628:                return(1);
                    629:
                    630:        /* Close out the block scope opened in the prior line.  */
1.11      kristaps  631:
1.119     schwarze  632:        assert(MAN_BLINE & man->flags);
                    633:        man->flags &= ~MAN_BLINE;
1.11      kristaps  634:
1.119     schwarze  635:        if ( ! man_unscope(man, man->last->parent, MANDOCERR_MAX))
1.29      kristaps  636:                return(0);
1.119     schwarze  637:        return(man_body_alloc(man, ln, ppos, man->last->tok));
1.1       kristaps  638:
                    639: err:   /* Error out. */
                    640:
1.119     schwarze  641:        man->flags |= MAN_HALT;
1.1       kristaps  642:        return(0);
                    643: }
1.51      kristaps  644:
1.54      kristaps  645: /*
1.119     schwarze  646:  * Unlink a node from its context.  If "man" is provided, the last parse
1.54      kristaps  647:  * point will also be adjusted accordingly.
                    648:  */
                    649: static void
1.119     schwarze  650: man_node_unlink(struct man *man, struct man_node *n)
1.51      kristaps  651: {
                    652:
1.54      kristaps  653:        /* Adjust siblings. */
                    654:
                    655:        if (n->prev)
1.51      kristaps  656:                n->prev->next = n->next;
1.54      kristaps  657:        if (n->next)
                    658:                n->next->prev = n->prev;
                    659:
                    660:        /* Adjust parent. */
                    661:
                    662:        if (n->parent) {
                    663:                n->parent->nchild--;
                    664:                if (n->parent->child == n)
                    665:                        n->parent->child = n->prev ? n->prev : n->next;
                    666:        }
                    667:
                    668:        /* Adjust parse point, if applicable. */
                    669:
1.119     schwarze  670:        if (man && man->last == n) {
1.54      kristaps  671:                /*XXX: this can occur when bailing from validation. */
                    672:                /*assert(NULL == n->next);*/
                    673:                if (n->prev) {
1.119     schwarze  674:                        man->last = n->prev;
                    675:                        man->next = MAN_NEXT_SIBLING;
1.54      kristaps  676:                } else {
1.119     schwarze  677:                        man->last = n->parent;
                    678:                        man->next = MAN_NEXT_CHILD;
1.51      kristaps  679:                }
                    680:        }
                    681:
1.119     schwarze  682:        if (man && man->first == n)
                    683:                man->first = NULL;
1.112     kristaps  684: }
                    685:
                    686: const struct mparse *
1.119     schwarze  687: man_mparse(const struct man *man)
1.112     kristaps  688: {
                    689:
1.119     schwarze  690:        assert(man && man->parse);
                    691:        return(man->parse);
1.23      kristaps  692: }

CVSweb