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

Annotation of mandoc/man.c, Revision 1.124

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

CVSweb