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

Annotation of src/usr.bin/mandoc/man.c, Revision 1.69

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