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

Annotation of mandoc/man.c, Revision 1.78

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

CVSweb