[BACK]Return to man.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/man.c, Revision 1.60

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

CVSweb