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

Annotation of mandoc/man.c, Revision 1.54

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

CVSweb