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

Annotation of mandoc/man.c, Revision 1.123

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

CVSweb