[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.42

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