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

Annotation of mandoc/man.c, Revision 1.119

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

CVSweb