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

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