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

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