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

Annotation of mandoc/man.c, Revision 1.137

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

CVSweb