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

1.28    ! schwarze    1: /*     $Id: man.c,v 1.27 2010/05/14 01:54:37 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.28    ! schwarze  435:
        !           436:        /*
        !           437:         * End-of-sentence check.  If the last character is an unescaped
        !           438:         * EOS character, then flag the node as being the end of a
        !           439:         * sentence.  The front-end will know how to interpret this.
        !           440:         */
        !           441:
        !           442:        assert(i);
        !           443:
        !           444:        if (mandoc_eos(buf, (size_t)i))
        !           445:                m->last->flags |= MAN_EOS;
1.10      schwarze  446:
                    447: descope:
1.1       kristaps  448:        /*
1.9       schwarze  449:         * Co-ordinate what happens with having a next-line scope open:
                    450:         * first close out the element scope (if applicable), then close
                    451:         * out the block scope (also if applicable).
1.1       kristaps  452:         */
                    453:
1.9       schwarze  454:        if (MAN_ELINE & m->flags) {
                    455:                m->flags &= ~MAN_ELINE;
1.23      schwarze  456:                if ( ! man_unscope(m, m->last->parent, WERRMAX))
1.9       schwarze  457:                        return(0);
                    458:        }
                    459:
                    460:        if ( ! (MAN_BLINE & m->flags))
1.1       kristaps  461:                return(1);
1.9       schwarze  462:        m->flags &= ~MAN_BLINE;
1.1       kristaps  463:
1.23      schwarze  464:        if ( ! man_unscope(m, m->last->parent, WERRMAX))
1.1       kristaps  465:                return(0);
1.9       schwarze  466:        return(man_body_alloc(m, line, 0, m->last->tok));
1.1       kristaps  467: }
                    468:
                    469:
1.15      schwarze  470: static int
                    471: macrowarn(struct man *m, int ln, const char *buf)
                    472: {
                    473:        if ( ! (MAN_IGN_MACRO & m->pflags))
1.27      schwarze  474:                return(man_verr(m, ln, 0, "unknown macro: %s%s",
1.15      schwarze  475:                                buf, strlen(buf) > 3 ? "..." : ""));
                    476:        return(man_vwarn(m, ln, 0, "unknown macro: %s%s",
                    477:                                buf, strlen(buf) > 3 ? "..." : ""));
                    478: }
                    479:
                    480:
1.1       kristaps  481: int
                    482: man_pmacro(struct man *m, int ln, char *buf)
                    483: {
1.24      schwarze  484:        int              i, j, ppos;
1.22      schwarze  485:        enum mant        tok;
1.10      schwarze  486:        char             mac[5];
                    487:        struct man_node *n;
1.1       kristaps  488:
                    489:        /* Comments and empties are quickly ignored. */
                    490:
1.17      schwarze  491:        if ('\0' == buf[1])
                    492:                return(1);
1.1       kristaps  493:
                    494:        i = 1;
                    495:
1.23      schwarze  496:        /*
                    497:         * Skip whitespace between the control character and initial
                    498:         * text.  "Whitespace" is both spaces and tabs.
                    499:         */
1.27      schwarze  500:
1.23      schwarze  501:        if (' ' == buf[i] || '\t' == buf[i]) {
1.1       kristaps  502:                i++;
1.23      schwarze  503:                while (buf[i] && (' ' == buf[i] || '\t' == buf[i]))
1.1       kristaps  504:                        i++;
1.18      schwarze  505:                if ('\0' == buf[i])
1.1       kristaps  506:                        goto out;
                    507:        }
                    508:
                    509:        ppos = i;
                    510:
                    511:        /* Copy the first word into a nil-terminated buffer. */
                    512:
                    513:        for (j = 0; j < 4; j++, i++) {
1.18      schwarze  514:                if ('\0' == (mac[j] = buf[i]))
1.1       kristaps  515:                        break;
                    516:                else if (' ' == buf[i])
                    517:                        break;
1.11      schwarze  518:
                    519:                /* Check for invalid characters. */
                    520:
                    521:                if (isgraph((u_char)buf[i]))
                    522:                        continue;
                    523:                return(man_perr(m, ln, i, WNPRINT));
1.1       kristaps  524:        }
                    525:
1.17      schwarze  526:        mac[j] = '\0';
1.1       kristaps  527:
                    528:        if (j == 4 || j < 1) {
                    529:                if ( ! (MAN_IGN_MACRO & m->pflags)) {
1.9       schwarze  530:                        (void)man_perr(m, ln, ppos, WMACROFORM);
1.1       kristaps  531:                        goto err;
                    532:                }
1.9       schwarze  533:                if ( ! man_pwarn(m, ln, ppos, WMACROFORM))
1.1       kristaps  534:                        goto err;
                    535:                return(1);
                    536:        }
                    537:
1.22      schwarze  538:        if (MAN_MAX == (tok = man_hash_find(mac))) {
1.15      schwarze  539:                if ( ! macrowarn(m, ln, mac))
1.1       kristaps  540:                        goto err;
                    541:                return(1);
                    542:        }
                    543:
                    544:        /* The macro is sane.  Jump to the next word. */
                    545:
                    546:        while (buf[i] && ' ' == buf[i])
                    547:                i++;
1.18      schwarze  548:
1.27      schwarze  549:        /*
                    550:         * Trailing whitespace.  Note that tabs are allowed to be passed
                    551:         * into the parser as "text", so we only warn about spaces here.
                    552:         */
1.18      schwarze  553:
                    554:        if ('\0' == buf[i] && ' ' == buf[i - 1])
                    555:                if ( ! man_pwarn(m, ln, i - 1, WTSPACE))
                    556:                        goto err;
1.1       kristaps  557:
1.21      schwarze  558:        /*
                    559:         * Remove prior ELINE macro, as it's being clobbering by a new
                    560:         * macro.  Note that NSCOPED macros do not close out ELINE
                    561:         * macros---they don't print text---so we let those slip by.
1.24      schwarze  562:         * NOTE: we don't allow roff blocks (NOCLOSE) to be embedded
                    563:         * here because that would stipulate blocks as children of
                    564:         * elements!
1.21      schwarze  565:         */
                    566:
1.22      schwarze  567:        if ( ! (MAN_NSCOPED & man_macros[tok].flags) &&
1.21      schwarze  568:                        m->flags & MAN_ELINE) {
                    569:                assert(MAN_TEXT != m->last->type);
                    570:
                    571:                /*
                    572:                 * This occurs in the following construction:
                    573:                 *   .B
                    574:                 *   .br
                    575:                 *   .B
                    576:                 *   .br
                    577:                 *   I hate man macros.
                    578:                 * Flat-out disallow this madness.
                    579:                 */
                    580:                if (MAN_NSCOPED & man_macros[m->last->tok].flags)
                    581:                        return(man_perr(m, ln, ppos, WLNSCOPE));
1.10      schwarze  582:
                    583:                n = m->last;
1.21      schwarze  584:
                    585:                assert(n);
1.10      schwarze  586:                assert(NULL == n->child);
                    587:                assert(0 == n->nchild);
1.21      schwarze  588:
1.10      schwarze  589:                if ( ! man_nwarn(m, n, WLNSCOPE))
                    590:                        return(0);
                    591:
1.22      schwarze  592:                man_node_delete(m, n);
1.10      schwarze  593:                m->flags &= ~MAN_ELINE;
                    594:        }
                    595:
1.24      schwarze  596:        /*
                    597:         * Save the fact that we're in the next-line for a block.  In
                    598:         * this way, embedded roff instructions can "remember" state
                    599:         * when they exit.
                    600:         */
                    601:
                    602:        if (MAN_BLINE & m->flags)
                    603:                m->flags |= MAN_BPLINE;
                    604:
                    605:        /* Call to handler... */
1.1       kristaps  606:
1.22      schwarze  607:        assert(man_macros[tok].fp);
                    608:        if ( ! (*man_macros[tok].fp)(m, tok, ln, ppos, &i, buf))
1.1       kristaps  609:                goto err;
                    610:
                    611: out:
1.21      schwarze  612:        /*
                    613:         * We weren't in a block-line scope when entering the
                    614:         * above-parsed macro, so return.
                    615:         */
                    616:
1.24      schwarze  617:        if ( ! (MAN_BPLINE & m->flags)) {
1.21      schwarze  618:                m->flags &= ~MAN_ILINE;
1.9       schwarze  619:                return(1);
1.21      schwarze  620:        }
1.24      schwarze  621:        m->flags &= ~MAN_BPLINE;
1.21      schwarze  622:
                    623:        /*
                    624:         * If we're in a block scope, then allow this macro to slip by
                    625:         * without closing scope around it.
                    626:         */
                    627:
                    628:        if (MAN_ILINE & m->flags) {
                    629:                m->flags &= ~MAN_ILINE;
                    630:                return(1);
                    631:        }
1.9       schwarze  632:
                    633:        /*
                    634:         * If we've opened a new next-line element scope, then return
                    635:         * now, as the next line will close out the block scope.
                    636:         */
                    637:
                    638:        if (MAN_ELINE & m->flags)
                    639:                return(1);
                    640:
                    641:        /* Close out the block scope opened in the prior line.  */
1.1       kristaps  642:
1.9       schwarze  643:        assert(MAN_BLINE & m->flags);
                    644:        m->flags &= ~MAN_BLINE;
1.1       kristaps  645:
1.23      schwarze  646:        if ( ! man_unscope(m, m->last->parent, WERRMAX))
1.9       schwarze  647:                return(0);
                    648:        return(man_body_alloc(m, ln, 0, m->last->tok));
1.1       kristaps  649:
                    650: err:   /* Error out. */
                    651:
                    652:        m->flags |= MAN_HALT;
                    653:        return(0);
                    654: }
                    655:
                    656:
                    657: int
                    658: man_verr(struct man *man, int ln, int pos, const char *fmt, ...)
                    659: {
                    660:        char             buf[256];
                    661:        va_list          ap;
                    662:
                    663:        if (NULL == man->cb.man_err)
                    664:                return(0);
                    665:
                    666:        va_start(ap, fmt);
                    667:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    668:        va_end(ap);
                    669:        return((*man->cb.man_err)(man->data, ln, pos, buf));
                    670: }
                    671:
                    672:
                    673: int
                    674: man_vwarn(struct man *man, int ln, int pos, const char *fmt, ...)
                    675: {
                    676:        char             buf[256];
                    677:        va_list          ap;
                    678:
                    679:        if (NULL == man->cb.man_warn)
                    680:                return(0);
                    681:
                    682:        va_start(ap, fmt);
                    683:        (void)vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    684:        va_end(ap);
                    685:        return((*man->cb.man_warn)(man->data, ln, pos, buf));
                    686: }
                    687:
                    688:
1.4       schwarze  689: int
1.7       schwarze  690: man_err(struct man *m, int line, int pos, int iserr, enum merr type)
1.4       schwarze  691: {
                    692:        const char       *p;
                    693:
1.7       schwarze  694:        p = __man_merrnames[(int)type];
1.4       schwarze  695:        assert(p);
                    696:
                    697:        if (iserr)
                    698:                return(man_verr(m, line, pos, p));
                    699:
                    700:        return(man_vwarn(m, line, pos, p));
1.21      schwarze  701: }
                    702:
                    703:
1.22      schwarze  704: /*
                    705:  * Unlink a node from its context.  If "m" is provided, the last parse
                    706:  * point will also be adjusted accordingly.
                    707:  */
                    708: static void
1.21      schwarze  709: man_node_unlink(struct man *m, struct man_node *n)
                    710: {
                    711:
1.22      schwarze  712:        /* Adjust siblings. */
                    713:
                    714:        if (n->prev)
1.21      schwarze  715:                n->prev->next = n->next;
1.22      schwarze  716:        if (n->next)
                    717:                n->next->prev = n->prev;
                    718:
                    719:        /* Adjust parent. */
                    720:
                    721:        if (n->parent) {
                    722:                n->parent->nchild--;
                    723:                if (n->parent->child == n)
                    724:                        n->parent->child = n->prev ? n->prev : n->next;
                    725:        }
                    726:
                    727:        /* Adjust parse point, if applicable. */
                    728:
                    729:        if (m && m->last == n) {
                    730:                /*XXX: this can occur when bailing from validation. */
                    731:                /*assert(NULL == n->next);*/
                    732:                if (n->prev) {
1.21      schwarze  733:                        m->last = n->prev;
                    734:                        m->next = MAN_NEXT_SIBLING;
1.22      schwarze  735:                } else {
1.21      schwarze  736:                        m->last = n->parent;
                    737:                        m->next = MAN_NEXT_CHILD;
                    738:                }
                    739:        }
                    740:
1.22      schwarze  741:        if (m && m->first == n)
                    742:                m->first = NULL;
1.4       schwarze  743: }