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

Annotation of mandoc/man.c, Revision 1.97

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

CVSweb