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

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