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

Annotation of mandoc/man.c, Revision 1.104

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

CVSweb