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

1.22    ! schwarze    1: /*     $Id: man.c,v 1.21 2010/03/25 23:23:01 schwarze Exp $ */
1.1       kristaps    2: /*
1.2       schwarze    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.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 <ctype.h>
                     21: #include <stdarg.h>
                     22: #include <stdlib.h>
                     23: #include <stdio.h>
                     24: #include <string.h>
                     25:
                     26: #include "libman.h"
1.16      schwarze   27: #include "libmandoc.h"
1.1       kristaps   28:
1.7       schwarze   29: const  char *const __man_merrnames[WERRMAX] = {
                     30:        "invalid character", /* WNPRINT */
                     31:        "invalid manual section", /* WMSEC */
                     32:        "invalid date format", /* WDATE */
                     33:        "scope of prior line violated", /* WLNSCOPE */
1.21      schwarze   34:        "over-zealous prior line scope violation", /* WLNSCOPE2 */
1.7       schwarze   35:        "trailing whitespace", /* WTSPACE */
                     36:        "unterminated quoted parameter", /* WTQUOTE */
                     37:        "document has no body", /* WNODATA */
                     38:        "document has no title/section", /* WNOTITLE */
                     39:        "invalid escape sequence", /* WESCAPE */
1.8       schwarze   40:        "invalid number format", /* WNUMFMT */
1.9       schwarze   41:        "expected block head arguments", /* WHEADARGS */
                     42:        "expected block body arguments", /* WBODYARGS */
                     43:        "expected empty block head", /* WNHEADARGS */
                     44:        "ill-formed macro", /* WMACROFORM */
1.10      schwarze   45:        "scope open on exit", /* WEXITSCOPE */
                     46:        "no scope context", /* WNOSCOPE */
                     47:        "literal context already open", /* WOLITERAL */
1.22    ! schwarze   48:        "no literal context open", /* WNLITERAL */
        !            49:        "invalid nesting of roff declarations", /* WROFFNEST */
1.7       schwarze   50: };
                     51:
1.1       kristaps   52: const  char *const __man_macronames[MAN_MAX] = {
1.3       schwarze   53:        "br",           "TH",           "SH",           "SS",
1.1       kristaps   54:        "TP",           "LP",           "PP",           "P",
                     55:        "IP",           "HP",           "SM",           "SB",
                     56:        "BI",           "IB",           "BR",           "RB",
                     57:        "R",            "B",            "I",            "IR",
1.9       schwarze   58:        "RI",           "na",           "i",            "sp",
1.10      schwarze   59:        "nf",           "fi",           "r",            "RE",
1.20      schwarze   60:        "RS",           "DT",           "UC",           "PD",
1.22    ! schwarze   61:        "Sp",           "Vb",           "Ve",           "de",
        !            62:        "dei",          "am",           "ami",          "ig",
        !            63:        ".",
1.1       kristaps   64:        };
                     65:
                     66: const  char * const *man_macronames = __man_macronames;
                     67:
                     68: static struct man_node *man_node_alloc(int, int,
1.22    ! schwarze   69:                                enum man_type, enum mant);
1.1       kristaps   70: static int              man_node_append(struct man *,
                     71:                                struct man_node *);
1.22    ! schwarze   72: static void             man_node_free(struct man_node *);
        !            73: static void             man_node_unlink(struct man *,
        !            74:                                struct man_node *);
1.1       kristaps   75: static int              man_ptext(struct man *, int, char *);
                     76: static int              man_pmacro(struct man *, int, char *);
                     77: static void             man_free1(struct man *);
1.16      schwarze   78: static void             man_alloc1(struct man *);
1.10      schwarze   79: static int              pstring(struct man *, int, int,
                     80:                                const char *, size_t);
1.15      schwarze   81: static int              macrowarn(struct man *, int, const char *);
1.1       kristaps   82:
                     83:
                     84: const struct man_node *
                     85: man_node(const struct man *m)
                     86: {
                     87:
                     88:        return(MAN_HALT & m->flags ? NULL : m->first);
                     89: }
                     90:
                     91:
                     92: const struct man_meta *
                     93: man_meta(const struct man *m)
                     94: {
                     95:
                     96:        return(MAN_HALT & m->flags ? NULL : &m->meta);
                     97: }
                     98:
                     99:
1.16      schwarze  100: void
1.1       kristaps  101: man_reset(struct man *man)
                    102: {
                    103:
                    104:        man_free1(man);
1.16      schwarze  105:        man_alloc1(man);
1.1       kristaps  106: }
                    107:
                    108:
                    109: void
                    110: man_free(struct man *man)
                    111: {
                    112:
                    113:        man_free1(man);
                    114:        free(man);
                    115: }
                    116:
                    117:
                    118: struct man *
                    119: man_alloc(void *data, int pflags, const struct man_cb *cb)
                    120: {
                    121:        struct man      *p;
                    122:
1.16      schwarze  123:        p = mandoc_calloc(1, sizeof(struct man));
1.1       kristaps  124:
1.16      schwarze  125:        if (cb)
                    126:                memcpy(&p->cb, cb, sizeof(struct man_cb));
1.1       kristaps  127:
1.13      schwarze  128:        man_hash_init();
1.1       kristaps  129:        p->data = data;
                    130:        p->pflags = pflags;
1.16      schwarze  131:
                    132:        man_alloc1(p);
1.1       kristaps  133:        return(p);
                    134: }
                    135:
                    136:
                    137: int
                    138: man_endparse(struct man *m)
                    139: {
                    140:
                    141:        if (MAN_HALT & m->flags)
                    142:                return(0);
                    143:        else if (man_macroend(m))
                    144:                return(1);
                    145:        m->flags |= MAN_HALT;
                    146:        return(0);
                    147: }
                    148:
                    149:
                    150: int
                    151: man_parseln(struct man *m, int ln, char *buf)
                    152: {
                    153:
                    154:        return('.' == *buf ?
                    155:                        man_pmacro(m, ln, buf) :
                    156:                        man_ptext(m, ln, buf));
                    157: }
                    158:
                    159:
                    160: static void
                    161: man_free1(struct man *man)
                    162: {
                    163:
                    164:        if (man->first)
1.22    ! schwarze  165:                man_node_delete(man, man->first);
1.1       kristaps  166:        if (man->meta.title)
                    167:                free(man->meta.title);
                    168:        if (man->meta.source)
                    169:                free(man->meta.source);
                    170:        if (man->meta.vol)
                    171:                free(man->meta.vol);
                    172: }
                    173:
                    174:
1.16      schwarze  175: static void
1.1       kristaps  176: man_alloc1(struct man *m)
                    177: {
                    178:
1.16      schwarze  179:        memset(&m->meta, 0, sizeof(struct man_meta));
1.1       kristaps  180:        m->flags = 0;
1.16      schwarze  181:        m->last = mandoc_calloc(1, sizeof(struct man_node));
1.1       kristaps  182:        m->first = m->last;
                    183:        m->last->type = MAN_ROOT;
1.22    ! schwarze  184:        m->last->tok = MAN_MAX;
1.1       kristaps  185:        m->next = MAN_NEXT_CHILD;
                    186: }
                    187:
                    188:
                    189: static int
                    190: man_node_append(struct man *man, struct man_node *p)
                    191: {
                    192:
                    193:        assert(man->last);
                    194:        assert(man->first);
                    195:        assert(MAN_ROOT != p->type);
                    196:
                    197:        switch (man->next) {
                    198:        case (MAN_NEXT_SIBLING):
                    199:                man->last->next = p;
                    200:                p->prev = man->last;
                    201:                p->parent = man->last->parent;
                    202:                break;
                    203:        case (MAN_NEXT_CHILD):
                    204:                man->last->child = p;
                    205:                p->parent = man->last;
                    206:                break;
                    207:        default:
                    208:                abort();
                    209:                /* NOTREACHED */
                    210:        }
1.5       schwarze  211:
1.22    ! schwarze  212:        assert(p->parent);
1.5       schwarze  213:        p->parent->nchild++;
1.1       kristaps  214:
1.9       schwarze  215:        if ( ! man_valid_pre(man, p))
                    216:                return(0);
                    217:
                    218:        switch (p->type) {
                    219:        case (MAN_HEAD):
                    220:                assert(MAN_BLOCK == p->parent->type);
                    221:                p->parent->head = p;
                    222:                break;
                    223:        case (MAN_BODY):
                    224:                assert(MAN_BLOCK == p->parent->type);
                    225:                p->parent->body = p;
                    226:                break;
                    227:        default:
                    228:                break;
                    229:        }
                    230:
1.1       kristaps  231:        man->last = p;
                    232:
                    233:        switch (p->type) {
                    234:        case (MAN_TEXT):
                    235:                if ( ! man_valid_post(man))
                    236:                        return(0);
                    237:                if ( ! man_action_post(man))
                    238:                        return(0);
                    239:                break;
                    240:        default:
                    241:                break;
                    242:        }
                    243:
                    244:        return(1);
                    245: }
                    246:
                    247:
                    248: static struct man_node *
1.22    ! schwarze  249: man_node_alloc(int line, int pos, enum man_type type, enum mant tok)
1.1       kristaps  250: {
                    251:        struct man_node *p;
                    252:
1.16      schwarze  253:        p = mandoc_calloc(1, sizeof(struct man_node));
1.1       kristaps  254:        p->line = line;
                    255:        p->pos = pos;
                    256:        p->type = type;
                    257:        p->tok = tok;
                    258:        return(p);
                    259: }
                    260:
                    261:
                    262: int
1.22    ! schwarze  263: man_elem_alloc(struct man *m, int line, int pos, enum mant tok)
1.1       kristaps  264: {
                    265:        struct man_node *p;
                    266:
                    267:        p = man_node_alloc(line, pos, MAN_ELEM, tok);
1.10      schwarze  268:        if ( ! man_node_append(m, p))
                    269:                return(0);
                    270:        m->next = MAN_NEXT_CHILD;
                    271:        return(1);
1.1       kristaps  272: }
                    273:
                    274:
                    275: int
1.22    ! schwarze  276: man_head_alloc(struct man *m, int line, int pos, enum mant tok)
1.9       schwarze  277: {
                    278:        struct man_node *p;
                    279:
                    280:        p = man_node_alloc(line, pos, MAN_HEAD, tok);
                    281:        if ( ! man_node_append(m, p))
                    282:                return(0);
                    283:        m->next = MAN_NEXT_CHILD;
                    284:        return(1);
                    285: }
                    286:
                    287:
                    288: int
1.22    ! schwarze  289: man_body_alloc(struct man *m, int line, int pos, enum mant tok)
1.9       schwarze  290: {
                    291:        struct man_node *p;
                    292:
                    293:        p = man_node_alloc(line, pos, MAN_BODY, tok);
                    294:        if ( ! man_node_append(m, p))
                    295:                return(0);
                    296:        m->next = MAN_NEXT_CHILD;
                    297:        return(1);
                    298: }
                    299:
                    300:
                    301: int
1.22    ! schwarze  302: man_block_alloc(struct man *m, int line, int pos, enum mant tok)
1.9       schwarze  303: {
                    304:        struct man_node *p;
                    305:
                    306:        p = man_node_alloc(line, pos, MAN_BLOCK, tok);
                    307:        if ( ! man_node_append(m, p))
                    308:                return(0);
                    309:        m->next = MAN_NEXT_CHILD;
                    310:        return(1);
                    311: }
                    312:
                    313:
1.10      schwarze  314: static int
                    315: pstring(struct man *m, int line, int pos,
                    316:                const char *p, size_t len)
1.1       kristaps  317: {
1.10      schwarze  318:        struct man_node *n;
                    319:        size_t           sv;
1.1       kristaps  320:
1.22    ! schwarze  321:        n = man_node_alloc(line, pos, MAN_TEXT, MAN_MAX);
1.16      schwarze  322:        n->string = mandoc_malloc(len + 1);
1.10      schwarze  323:        sv = strlcpy(n->string, p, len + 1);
                    324:
                    325:        /* Prohibit truncation. */
                    326:        assert(sv < len + 1);
                    327:
                    328:        if ( ! man_node_append(m, n))
1.1       kristaps  329:                return(0);
1.10      schwarze  330:        m->next = MAN_NEXT_SIBLING;
                    331:        return(1);
                    332: }
                    333:
                    334:
                    335: int
                    336: man_word_alloc(struct man *m, int line, int pos, const char *word)
                    337: {
                    338:
                    339:        return(pstring(m, line, pos, word, strlen(word)));
1.1       kristaps  340: }
                    341:
                    342:
1.22    ! schwarze  343: /*
        !           344:  * Free all of the resources held by a node.  This does NOT unlink a
        !           345:  * node from its context; for that, see man_node_unlink().
        !           346:  */
        !           347: static void
1.1       kristaps  348: man_node_free(struct man_node *p)
                    349: {
                    350:
                    351:        if (p->string)
                    352:                free(p->string);
                    353:        free(p);
                    354: }
                    355:
                    356:
                    357: void
1.22    ! schwarze  358: man_node_delete(struct man *m, struct man_node *p)
1.1       kristaps  359: {
                    360:
1.22    ! schwarze  361:        while (p->child)
        !           362:                man_node_delete(m, p->child);
        !           363:
        !           364:        man_node_unlink(m, p);
1.1       kristaps  365:        man_node_free(p);
                    366: }
                    367:
                    368:
                    369: static int
                    370: man_ptext(struct man *m, int line, char *buf)
                    371: {
1.10      schwarze  372:        int              i, j;
1.18      schwarze  373:        char             sv;
1.10      schwarze  374:
                    375:        /* Literal free-form text whitespace is preserved. */
                    376:
                    377:        if (MAN_LITERAL & m->flags) {
                    378:                if ( ! man_word_alloc(m, line, 0, buf))
                    379:                        return(0);
                    380:                goto descope;
                    381:        }
                    382:
                    383:        /* First de-chunk and allocate words. */
                    384:
                    385:        for (i = 0; ' ' == buf[i]; i++)
                    386:                /* Skip leading whitespace. */ ;
1.18      schwarze  387:
                    388:        if ('\0' == buf[i]) {
                    389:                /* Trailing whitespace? */
                    390:                if (i && ' ' == buf[i - 1])
                    391:                        if ( ! man_pwarn(m, line, i - 1, WTSPACE))
                    392:                                return(0);
1.10      schwarze  393:                if ( ! pstring(m, line, 0, &buf[i], 0))
                    394:                        return(0);
                    395:                goto descope;
                    396:        }
1.1       kristaps  397:
1.10      schwarze  398:        for (j = i; buf[i]; i++) {
                    399:                if (' ' != buf[i])
                    400:                        continue;
                    401:
                    402:                /* Escaped whitespace. */
                    403:                if (i && ' ' == buf[i] && '\\' == buf[i - 1])
                    404:                        continue;
                    405:
1.18      schwarze  406:                sv = buf[i];
                    407:                buf[i++] = '\0';
                    408:
1.10      schwarze  409:                if ( ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
                    410:                        return(0);
                    411:
1.18      schwarze  412:                /* Trailing whitespace?  Check at overwritten byte. */
                    413:
                    414:                if (' ' == sv && '\0' == buf[i])
                    415:                        if ( ! man_pwarn(m, line, i - 1, WTSPACE))
                    416:                                return(0);
                    417:
1.10      schwarze  418:                for ( ; ' ' == buf[i]; i++)
                    419:                        /* Skip trailing whitespace. */ ;
                    420:
                    421:                j = i;
1.18      schwarze  422:
                    423:                /* Trailing whitespace? */
                    424:
                    425:                if (' ' == buf[i - 1] && '\0' == buf[i])
                    426:                        if ( ! man_pwarn(m, line, i - 1, WTSPACE))
                    427:                                return(0);
                    428:
                    429:                if ('\0' == buf[i])
1.10      schwarze  430:                        break;
                    431:        }
1.9       schwarze  432:
1.10      schwarze  433:        if (j != i && ! pstring(m, line, j, &buf[j], (size_t)(i - j)))
1.1       kristaps  434:                return(0);
1.10      schwarze  435:
                    436: descope:
1.1       kristaps  437:
                    438:        /*
1.9       schwarze  439:         * Co-ordinate what happens with having a next-line scope open:
                    440:         * first close out the element scope (if applicable), then close
                    441:         * out the block scope (also if applicable).
1.1       kristaps  442:         */
                    443:
1.9       schwarze  444:        if (MAN_ELINE & m->flags) {
                    445:                m->flags &= ~MAN_ELINE;
                    446:                if ( ! man_unscope(m, m->last->parent))
                    447:                        return(0);
                    448:        }
                    449:
                    450:        if ( ! (MAN_BLINE & m->flags))
1.1       kristaps  451:                return(1);
1.9       schwarze  452:        m->flags &= ~MAN_BLINE;
1.1       kristaps  453:
1.9       schwarze  454:        if ( ! man_unscope(m, m->last->parent))
1.1       kristaps  455:                return(0);
1.9       schwarze  456:        return(man_body_alloc(m, line, 0, m->last->tok));
1.1       kristaps  457: }
                    458:
                    459:
1.15      schwarze  460: static int
                    461: macrowarn(struct man *m, int ln, const char *buf)
                    462: {
                    463:        if ( ! (MAN_IGN_MACRO & m->pflags))
                    464:                return(man_verr(m, ln, 0,
                    465:                                "unknown macro: %s%s",
                    466:                                buf, strlen(buf) > 3 ? "..." : ""));
                    467:        return(man_vwarn(m, ln, 0, "unknown macro: %s%s",
                    468:                                buf, strlen(buf) > 3 ? "..." : ""));
                    469: }
                    470:
                    471:
1.1       kristaps  472: int
                    473: man_pmacro(struct man *m, int ln, char *buf)
                    474: {
1.22    ! schwarze  475:        int              i, j, ppos, fl;
        !           476:        enum mant        tok;
1.10      schwarze  477:        char             mac[5];
                    478:        struct man_node *n;
1.1       kristaps  479:
                    480:        /* Comments and empties are quickly ignored. */
                    481:
1.9       schwarze  482:        fl = m->flags;
1.1       kristaps  483:
1.17      schwarze  484:        if ('\0' == buf[1])
                    485:                return(1);
1.1       kristaps  486:
                    487:        i = 1;
                    488:
                    489:        if (' ' == buf[i]) {
                    490:                i++;
                    491:                while (buf[i] && ' ' == buf[i])
                    492:                        i++;
1.18      schwarze  493:                if ('\0' == buf[i])
1.1       kristaps  494:                        goto out;
                    495:        }
                    496:
                    497:        ppos = i;
                    498:
                    499:        /* Copy the first word into a nil-terminated buffer. */
                    500:
                    501:        for (j = 0; j < 4; j++, i++) {
1.18      schwarze  502:                if ('\0' == (mac[j] = buf[i]))
1.1       kristaps  503:                        break;
                    504:                else if (' ' == buf[i])
                    505:                        break;
1.11      schwarze  506:
                    507:                /* Check for invalid characters. */
                    508:
                    509:                if (isgraph((u_char)buf[i]))
                    510:                        continue;
                    511:                return(man_perr(m, ln, i, WNPRINT));
1.1       kristaps  512:        }
                    513:
1.17      schwarze  514:        mac[j] = '\0';
1.1       kristaps  515:
                    516:        if (j == 4 || j < 1) {
                    517:                if ( ! (MAN_IGN_MACRO & m->pflags)) {
1.9       schwarze  518:                        (void)man_perr(m, ln, ppos, WMACROFORM);
1.1       kristaps  519:                        goto err;
                    520:                }
1.9       schwarze  521:                if ( ! man_pwarn(m, ln, ppos, WMACROFORM))
1.1       kristaps  522:                        goto err;
                    523:                return(1);
                    524:        }
                    525:
1.22    ! schwarze  526:        if (MAN_MAX == (tok = man_hash_find(mac))) {
1.15      schwarze  527:                if ( ! macrowarn(m, ln, mac))
1.1       kristaps  528:                        goto err;
                    529:                return(1);
                    530:        }
                    531:
                    532:        /* The macro is sane.  Jump to the next word. */
                    533:
                    534:        while (buf[i] && ' ' == buf[i])
                    535:                i++;
1.18      schwarze  536:
                    537:        /* Trailing whitespace? */
                    538:
                    539:        if ('\0' == buf[i] && ' ' == buf[i - 1])
                    540:                if ( ! man_pwarn(m, ln, i - 1, WTSPACE))
                    541:                        goto err;
1.1       kristaps  542:
1.21      schwarze  543:        /*
                    544:         * Remove prior ELINE macro, as it's being clobbering by a new
                    545:         * macro.  Note that NSCOPED macros do not close out ELINE
                    546:         * macros---they don't print text---so we let those slip by.
                    547:         */
                    548:
1.22    ! schwarze  549:        if ( ! (MAN_NSCOPED & man_macros[tok].flags) &&
1.21      schwarze  550:                        m->flags & MAN_ELINE) {
                    551:                assert(MAN_TEXT != m->last->type);
                    552:
                    553:                /*
                    554:                 * This occurs in the following construction:
                    555:                 *   .B
                    556:                 *   .br
                    557:                 *   .B
                    558:                 *   .br
                    559:                 *   I hate man macros.
                    560:                 * Flat-out disallow this madness.
                    561:                 */
                    562:                if (MAN_NSCOPED & man_macros[m->last->tok].flags)
                    563:                        return(man_perr(m, ln, ppos, WLNSCOPE));
1.10      schwarze  564:
                    565:                n = m->last;
1.21      schwarze  566:
                    567:                assert(n);
1.10      schwarze  568:                assert(NULL == n->child);
                    569:                assert(0 == n->nchild);
1.21      schwarze  570:
1.10      schwarze  571:                if ( ! man_nwarn(m, n, WLNSCOPE))
                    572:                        return(0);
                    573:
1.22    ! schwarze  574:                man_node_delete(m, n);
1.10      schwarze  575:                m->flags &= ~MAN_ELINE;
                    576:        }
                    577:
1.1       kristaps  578:        /* Begin recursive parse sequence. */
                    579:
1.22    ! schwarze  580:        assert(man_macros[tok].fp);
1.9       schwarze  581:
1.22    ! schwarze  582:        if ( ! (*man_macros[tok].fp)(m, tok, ln, ppos, &i, buf))
1.1       kristaps  583:                goto err;
                    584:
                    585: out:
1.21      schwarze  586:        /*
                    587:         * We weren't in a block-line scope when entering the
                    588:         * above-parsed macro, so return.
1.22    ! schwarze  589:         *
        !           590:         * FIXME: this prohibits the nesting of blocks (e.g., `de' and
        !           591:         * family) within BLINE or ELINE systems.  This is annoying.
1.21      schwarze  592:         */
                    593:
                    594:        if ( ! (MAN_BLINE & fl)) {
                    595:                m->flags &= ~MAN_ILINE;
1.9       schwarze  596:                return(1);
1.21      schwarze  597:        }
                    598:
                    599:        /*
                    600:         * If we're in a block scope, then allow this macro to slip by
                    601:         * without closing scope around it.
                    602:         */
                    603:
                    604:        if (MAN_ILINE & m->flags) {
                    605:                m->flags &= ~MAN_ILINE;
                    606:                return(1);
                    607:        }
1.9       schwarze  608:
                    609:        /*
                    610:         * If we've opened a new next-line element scope, then return
                    611:         * now, as the next line will close out the block scope.
                    612:         */
                    613:
                    614:        if (MAN_ELINE & m->flags)
                    615:                return(1);
                    616:
                    617:        /* Close out the block scope opened in the prior line.  */
1.1       kristaps  618:
1.9       schwarze  619:        assert(MAN_BLINE & m->flags);
                    620:        m->flags &= ~MAN_BLINE;
1.1       kristaps  621:
1.9       schwarze  622:        if ( ! man_unscope(m, m->last->parent))
                    623:                return(0);
                    624:        return(man_body_alloc(m, ln, 0, m->last->tok));
1.1       kristaps  625:
                    626: err:   /* Error out. */
                    627:
                    628:        m->flags |= MAN_HALT;
                    629:        return(0);
                    630: }
                    631:
                    632:
                    633: int
                    634: man_verr(struct man *man, int ln, int pos, const char *fmt, ...)
                    635: {
                    636:        char             buf[256];
                    637:        va_list          ap;
                    638:
                    639:        if (NULL == man->cb.man_err)
                    640:                return(0);
                    641:
                    642:        va_start(ap, fmt);
                    643:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    644:        va_end(ap);
                    645:        return((*man->cb.man_err)(man->data, ln, pos, buf));
                    646: }
                    647:
                    648:
                    649: int
                    650: man_vwarn(struct man *man, int ln, int pos, const char *fmt, ...)
                    651: {
                    652:        char             buf[256];
                    653:        va_list          ap;
                    654:
                    655:        if (NULL == man->cb.man_warn)
                    656:                return(0);
                    657:
                    658:        va_start(ap, fmt);
                    659:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    660:        va_end(ap);
                    661:        return((*man->cb.man_warn)(man->data, ln, pos, buf));
                    662: }
                    663:
                    664:
1.4       schwarze  665: int
1.7       schwarze  666: man_err(struct man *m, int line, int pos, int iserr, enum merr type)
1.4       schwarze  667: {
                    668:        const char       *p;
                    669:
1.7       schwarze  670:        p = __man_merrnames[(int)type];
1.4       schwarze  671:        assert(p);
                    672:
                    673:        if (iserr)
                    674:                return(man_verr(m, line, pos, p));
                    675:
                    676:        return(man_vwarn(m, line, pos, p));
1.21      schwarze  677: }
                    678:
                    679:
1.22    ! schwarze  680: /*
        !           681:  * Unlink a node from its context.  If "m" is provided, the last parse
        !           682:  * point will also be adjusted accordingly.
        !           683:  */
        !           684: static void
1.21      schwarze  685: man_node_unlink(struct man *m, struct man_node *n)
                    686: {
                    687:
1.22    ! schwarze  688:        /* Adjust siblings. */
        !           689:
        !           690:        if (n->prev)
1.21      schwarze  691:                n->prev->next = n->next;
1.22    ! schwarze  692:        if (n->next)
        !           693:                n->next->prev = n->prev;
        !           694:
        !           695:        /* Adjust parent. */
        !           696:
        !           697:        if (n->parent) {
        !           698:                n->parent->nchild--;
        !           699:                if (n->parent->child == n)
        !           700:                        n->parent->child = n->prev ? n->prev : n->next;
        !           701:        }
        !           702:
        !           703:        /* Adjust parse point, if applicable. */
        !           704:
        !           705:        if (m && m->last == n) {
        !           706:                /*XXX: this can occur when bailing from validation. */
        !           707:                /*assert(NULL == n->next);*/
        !           708:                if (n->prev) {
1.21      schwarze  709:                        m->last = n->prev;
                    710:                        m->next = MAN_NEXT_SIBLING;
1.22    ! schwarze  711:                } else {
1.21      schwarze  712:                        m->last = n->parent;
                    713:                        m->next = MAN_NEXT_CHILD;
                    714:                }
                    715:        }
                    716:
1.22    ! schwarze  717:        if (m && m->first == n)
        !           718:                m->first = NULL;
1.4       schwarze  719: }