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

Annotation of mandoc/man.c, Revision 1.145

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

CVSweb