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

Annotation of mandoc/man.c, Revision 1.157

1.157   ! schwarze    1: /*     $Id: man.c,v 1.156 2015/04/18 17:28:36 schwarze Exp $ */
1.1       kristaps    2: /*
1.102     schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.149     schwarze    4:  * Copyright (c) 2013, 2014, 2015 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.150     schwarze   11:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.18      kristaps   12:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.150     schwarze   13:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.18      kristaps   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.150     schwarze   30: #include "mandoc_aux.h"
                     31: #include "mandoc.h"
                     32: #include "roff.h"
1.105     kristaps   33: #include "man.h"
1.150     schwarze   34: #include "libmandoc.h"
1.1       kristaps   35: #include "libman.h"
                     36:
1.129     schwarze   37: const  char *const __man_macronames[MAN_MAX] = {
1.21      kristaps   38:        "br",           "TH",           "SH",           "SS",
1.129     schwarze   39:        "TP",           "LP",           "PP",           "P",
1.1       kristaps   40:        "IP",           "HP",           "SM",           "SB",
                     41:        "BI",           "IB",           "BR",           "RB",
1.11      kristaps   42:        "R",            "B",            "I",            "IR",
1.148     schwarze   43:        "RI",           "sp",           "nf",
1.92      kristaps   44:        "fi",           "RE",           "RS",           "DT",
                     45:        "UC",           "PD",           "AT",           "in",
1.120     schwarze   46:        "ft",           "OP",           "EX",           "EE",
1.128     schwarze   47:        "UR",           "UE",           "ll"
1.1       kristaps   48:        };
                     49:
                     50: const  char * const *man_macronames = __man_macronames;
                     51:
1.153     schwarze   52: static void             man_breakscope(struct roff_man *, int);
                     53: static void             man_descope(struct roff_man *, int, int);
                     54: static struct roff_node *man_node_alloc(struct roff_man *, int, int,
1.151     schwarze   55:                                enum roff_type, int);
1.153     schwarze   56: static void             man_node_append(struct roff_man *,
                     57:                                struct roff_node *);
1.151     schwarze   58: static void             man_node_free(struct roff_node *);
1.153     schwarze   59: static void             man_node_unlink(struct roff_man *,
                     60:                                struct roff_node *);
                     61: static int              man_ptext(struct roff_man *, int, char *, int);
                     62: static int              man_pmacro(struct roff_man *, int, char *, int);
1.1       kristaps   63:
                     64:
1.45      kristaps   65: void
1.153     schwarze   66: man_endparse(struct roff_man *man)
1.1       kristaps   67: {
                     68:
1.144     schwarze   69:        man_macroend(man);
1.1       kristaps   70: }
                     71:
                     72: int
1.153     schwarze   73: man_parseln(struct roff_man *man, int ln, char *buf, int offs)
1.1       kristaps   74: {
                     75:
1.150     schwarze   76:        if (man->last->type != ROFFT_EQN || ln > man->last->line)
1.141     schwarze   77:                man->flags |= MAN_NEWLINE;
1.97      kristaps   78:
1.119     schwarze   79:        return (roff_getcontrol(man->roff, buf, &offs) ?
1.129     schwarze   80:            man_pmacro(man, ln, buf, offs) :
                     81:            man_ptext(man, ln, buf, offs));
1.1       kristaps   82: }
1.2       kristaps   83:
1.144     schwarze   84: static void
1.153     schwarze   85: man_node_append(struct roff_man *man, struct roff_node *p)
1.1       kristaps   86: {
                     87:
                     88:        assert(man->last);
                     89:        assert(man->first);
1.150     schwarze   90:        assert(p->type != ROFFT_ROOT);
1.1       kristaps   91:
                     92:        switch (man->next) {
1.153     schwarze   93:        case ROFF_NEXT_SIBLING:
1.1       kristaps   94:                man->last->next = p;
                     95:                p->prev = man->last;
                     96:                p->parent = man->last->parent;
                     97:                break;
1.153     schwarze   98:        case ROFF_NEXT_CHILD:
1.1       kristaps   99:                man->last->child = p;
                    100:                p->parent = man->last;
                    101:                break;
                    102:        default:
                    103:                abort();
                    104:                /* NOTREACHED */
                    105:        }
1.129     schwarze  106:
1.54      kristaps  107:        assert(p->parent);
1.22      kristaps  108:        p->parent->nchild++;
1.1       kristaps  109:
1.29      kristaps  110:        switch (p->type) {
1.150     schwarze  111:        case ROFFT_BLOCK:
1.137     schwarze  112:                if (p->tok == MAN_SH || p->tok == MAN_SS)
                    113:                        man->flags &= ~MAN_LITERAL;
                    114:                break;
1.150     schwarze  115:        case ROFFT_HEAD:
                    116:                assert(p->parent->type == ROFFT_BLOCK);
1.29      kristaps  117:                p->parent->head = p;
                    118:                break;
1.150     schwarze  119:        case ROFFT_BODY:
                    120:                assert(p->parent->type == ROFFT_BLOCK);
1.29      kristaps  121:                p->parent->body = p;
                    122:                break;
                    123:        default:
                    124:                break;
                    125:        }
                    126:
1.2       kristaps  127:        man->last = p;
                    128:
1.1       kristaps  129:        switch (p->type) {
1.150     schwarze  130:        case ROFFT_TBL:
1.94      kristaps  131:                /* FALLTHROUGH */
1.150     schwarze  132:        case ROFFT_TEXT:
1.144     schwarze  133:                man_valid_post(man);
1.1       kristaps  134:                break;
                    135:        default:
                    136:                break;
                    137:        }
                    138: }
                    139:
1.151     schwarze  140: static struct roff_node *
1.153     schwarze  141: man_node_alloc(struct roff_man *man, int line, int pos,
1.151     schwarze  142:                enum roff_type type, int tok)
1.1       kristaps  143: {
1.151     schwarze  144:        struct roff_node *p;
1.1       kristaps  145:
1.151     schwarze  146:        p = mandoc_calloc(1, sizeof(*p));
1.1       kristaps  147:        p->line = line;
                    148:        p->pos = pos;
                    149:        p->type = type;
1.16      kristaps  150:        p->tok = tok;
1.97      kristaps  151:
1.144     schwarze  152:        if (man->flags & MAN_NEWLINE)
1.97      kristaps  153:                p->flags |= MAN_LINE;
1.119     schwarze  154:        man->flags &= ~MAN_NEWLINE;
1.1       kristaps  155:        return(p);
                    156: }
                    157:
1.144     schwarze  158: void
1.153     schwarze  159: man_elem_alloc(struct roff_man *man, int line, int pos, int tok)
1.1       kristaps  160: {
1.151     schwarze  161:        struct roff_node *p;
1.1       kristaps  162:
1.150     schwarze  163:        p = man_node_alloc(man, line, pos, ROFFT_ELEM, tok);
1.144     schwarze  164:        man_node_append(man, p);
1.153     schwarze  165:        man->next = ROFF_NEXT_CHILD;
1.106     kristaps  166: }
                    167:
1.144     schwarze  168: void
1.153     schwarze  169: man_head_alloc(struct roff_man *man, int line, int pos, int tok)
1.29      kristaps  170: {
1.151     schwarze  171:        struct roff_node *p;
1.29      kristaps  172:
1.150     schwarze  173:        p = man_node_alloc(man, line, pos, ROFFT_HEAD, tok);
1.144     schwarze  174:        man_node_append(man, p);
1.153     schwarze  175:        man->next = ROFF_NEXT_CHILD;
1.29      kristaps  176: }
                    177:
1.144     schwarze  178: void
1.153     schwarze  179: man_body_alloc(struct roff_man *man, int line, int pos, int tok)
1.29      kristaps  180: {
1.151     schwarze  181:        struct roff_node *p;
1.29      kristaps  182:
1.150     schwarze  183:        p = man_node_alloc(man, line, pos, ROFFT_BODY, tok);
1.144     schwarze  184:        man_node_append(man, p);
1.153     schwarze  185:        man->next = ROFF_NEXT_CHILD;
1.29      kristaps  186: }
                    187:
1.144     schwarze  188: void
1.153     schwarze  189: man_block_alloc(struct roff_man *man, int line, int pos, int tok)
1.29      kristaps  190: {
1.151     schwarze  191:        struct roff_node *p;
1.29      kristaps  192:
1.150     schwarze  193:        p = man_node_alloc(man, line, pos, ROFFT_BLOCK, tok);
1.144     schwarze  194:        man_node_append(man, p);
1.153     schwarze  195:        man->next = ROFF_NEXT_CHILD;
1.29      kristaps  196: }
                    197:
1.144     schwarze  198: void
1.153     schwarze  199: man_word_alloc(struct roff_man *man, int line, int pos, const char *word)
1.1       kristaps  200: {
1.151     schwarze  201:        struct roff_node *n;
1.1       kristaps  202:
1.150     schwarze  203:        n = man_node_alloc(man, line, pos, ROFFT_TEXT, MAN_MAX);
1.119     schwarze  204:        n->string = roff_strdup(man->roff, word);
1.144     schwarze  205:        man_node_append(man, n);
1.153     schwarze  206:        man->next = ROFF_NEXT_SIBLING;
1.142     schwarze  207: }
                    208:
                    209: void
1.153     schwarze  210: man_word_append(struct roff_man *man, const char *word)
1.142     schwarze  211: {
1.151     schwarze  212:        struct roff_node *n;
1.142     schwarze  213:        char            *addstr, *newstr;
                    214:
                    215:        n = man->last;
                    216:        addstr = roff_strdup(man->roff, word);
                    217:        mandoc_asprintf(&newstr, "%s %s", n->string, addstr);
                    218:        free(addstr);
                    219:        free(n->string);
                    220:        n->string = newstr;
1.153     schwarze  221:        man->next = ROFF_NEXT_SIBLING;
1.1       kristaps  222: }
                    223:
1.54      kristaps  224: /*
                    225:  * Free all of the resources held by a node.  This does NOT unlink a
                    226:  * node from its context; for that, see man_node_unlink().
                    227:  */
                    228: static void
1.151     schwarze  229: man_node_free(struct roff_node *p)
1.1       kristaps  230: {
                    231:
1.144     schwarze  232:        free(p->string);
1.1       kristaps  233:        free(p);
                    234: }
                    235:
                    236: void
1.153     schwarze  237: man_node_delete(struct roff_man *man, struct roff_node *p)
1.1       kristaps  238: {
                    239:
1.54      kristaps  240:        while (p->child)
1.119     schwarze  241:                man_node_delete(man, p->child);
1.54      kristaps  242:
1.119     schwarze  243:        man_node_unlink(man, p);
1.1       kristaps  244:        man_node_free(p);
                    245: }
                    246:
1.145     schwarze  247: void
1.153     schwarze  248: man_addeqn(struct roff_man *man, const struct eqn *ep)
1.101     kristaps  249: {
1.151     schwarze  250:        struct roff_node *n;
1.101     kristaps  251:
1.150     schwarze  252:        n = man_node_alloc(man, ep->ln, ep->pos, ROFFT_EQN, MAN_MAX);
1.101     kristaps  253:        n->eqn = ep;
1.140     schwarze  254:        if (ep->ln > man->last->line)
                    255:                n->flags |= MAN_LINE;
1.144     schwarze  256:        man_node_append(man, n);
1.153     schwarze  257:        man->next = ROFF_NEXT_SIBLING;
1.144     schwarze  258:        man_descope(man, ep->ln, ep->pos);
1.101     kristaps  259: }
1.1       kristaps  260:
1.145     schwarze  261: void
1.153     schwarze  262: man_addspan(struct roff_man *man, const struct tbl_span *sp)
1.94      kristaps  263: {
1.151     schwarze  264:        struct roff_node *n;
1.94      kristaps  265:
1.149     schwarze  266:        man_breakscope(man, MAN_MAX);
1.150     schwarze  267:        n = man_node_alloc(man, sp->line, 0, ROFFT_TBL, MAN_MAX);
1.100     kristaps  268:        n->span = sp;
1.144     schwarze  269:        man_node_append(man, n);
1.153     schwarze  270:        man->next = ROFF_NEXT_SIBLING;
1.144     schwarze  271:        man_descope(man, sp->line, 0);
1.94      kristaps  272: }
                    273:
1.144     schwarze  274: static void
1.153     schwarze  275: man_descope(struct roff_man *man, int line, int offs)
1.94      kristaps  276: {
                    277:        /*
                    278:         * Co-ordinate what happens with having a next-line scope open:
                    279:         * first close out the element scope (if applicable), then close
                    280:         * out the block scope (also if applicable).
                    281:         */
                    282:
1.144     schwarze  283:        if (man->flags & MAN_ELINE) {
1.119     schwarze  284:                man->flags &= ~MAN_ELINE;
1.144     schwarze  285:                man_unscope(man, man->last->parent);
1.94      kristaps  286:        }
1.144     schwarze  287:        if ( ! (man->flags & MAN_BLINE))
                    288:                return;
1.119     schwarze  289:        man->flags &= ~MAN_BLINE;
1.144     schwarze  290:        man_unscope(man, man->last->parent);
                    291:        man_body_alloc(man, line, offs, man->last->tok);
1.94      kristaps  292: }
                    293:
1.1       kristaps  294: static int
1.153     schwarze  295: man_ptext(struct roff_man *man, int line, char *buf, int offs)
1.1       kristaps  296: {
1.61      kristaps  297:        int              i;
1.60      kristaps  298:
1.35      kristaps  299:        /* Literal free-form text whitespace is preserved. */
                    300:
1.144     schwarze  301:        if (man->flags & MAN_LITERAL) {
                    302:                man_word_alloc(man, line, offs, buf + offs);
                    303:                man_descope(man, line, offs);
                    304:                return(1);
1.35      kristaps  305:        }
                    306:
1.144     schwarze  307:        for (i = offs; buf[i] == ' '; i++)
1.31      kristaps  308:                /* Skip leading whitespace. */ ;
1.48      kristaps  309:
1.121     schwarze  310:        /*
                    311:         * Blank lines are ignored right after headings
                    312:         * but add a single vertical space elsewhere.
                    313:         */
                    314:
1.144     schwarze  315:        if (buf[i] == '\0') {
1.61      kristaps  316:                /* Allocate a blank entry. */
1.144     schwarze  317:                if (man->last->tok != MAN_SH &&
                    318:                    man->last->tok != MAN_SS) {
                    319:                        man_elem_alloc(man, line, offs, MAN_sp);
1.153     schwarze  320:                        man->next = ROFF_NEXT_SIBLING;
1.121     schwarze  321:                }
1.118     schwarze  322:                return(1);
1.31      kristaps  323:        }
                    324:
1.129     schwarze  325:        /*
1.63      kristaps  326:         * Warn if the last un-escaped character is whitespace. Then
1.129     schwarze  327:         * strip away the remaining spaces (tabs stay!).
1.63      kristaps  328:         */
1.29      kristaps  329:
1.61      kristaps  330:        i = (int)strlen(buf);
                    331:        assert(i);
1.49      kristaps  332:
1.63      kristaps  333:        if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
1.64      kristaps  334:                if (i > 1 && '\\' != buf[i - 2])
1.131     schwarze  335:                        mandoc_msg(MANDOCERR_SPACE_EOL, man->parse,
                    336:                            line, i - 1, NULL);
1.63      kristaps  337:
                    338:                for (--i; i && ' ' == buf[i]; i--)
                    339:                        /* Spin back to non-space. */ ;
                    340:
                    341:                /* Jump ahead of escaped whitespace. */
                    342:                i += '\\' == buf[i] ? 2 : 1;
                    343:
                    344:                buf[i] = '\0';
                    345:        }
1.144     schwarze  346:        man_word_alloc(man, line, offs, buf + offs);
1.65      kristaps  347:
                    348:        /*
                    349:         * End-of-sentence check.  If the last character is an unescaped
                    350:         * EOS character, then flag the node as being the end of a
                    351:         * sentence.  The front-end will know how to interpret this.
                    352:         */
1.67      kristaps  353:
1.65      kristaps  354:        assert(i);
1.122     schwarze  355:        if (mandoc_eos(buf, (size_t)i))
1.119     schwarze  356:                man->last->flags |= MAN_EOS;
1.31      kristaps  357:
1.144     schwarze  358:        man_descope(man, line, offs);
                    359:        return(1);
1.1       kristaps  360: }
                    361:
1.96      kristaps  362: static int
1.153     schwarze  363: man_pmacro(struct roff_man *man, int ln, char *buf, int offs)
1.1       kristaps  364: {
1.151     schwarze  365:        struct roff_node *n;
1.143     schwarze  366:        const char      *cp;
1.151     schwarze  367:        int              tok;
1.134     schwarze  368:        int              i, ppos;
                    369:        int              bline;
1.143     schwarze  370:        char             mac[5];
1.1       kristaps  371:
1.107     kristaps  372:        ppos = offs;
1.9       kristaps  373:
1.56      kristaps  374:        /*
1.107     kristaps  375:         * Copy the first word into a nil-terminated buffer.
1.143     schwarze  376:         * Stop when a space, tab, escape, or eoln is encountered.
1.56      kristaps  377:         */
1.62      kristaps  378:
1.107     kristaps  379:        i = 0;
1.143     schwarze  380:        while (i < 4 && strchr(" \t\\", buf[offs]) == NULL)
1.107     kristaps  381:                mac[i++] = buf[offs++];
1.10      kristaps  382:
1.107     kristaps  383:        mac[i] = '\0';
1.1       kristaps  384:
1.107     kristaps  385:        tok = (i > 0 && i < 4) ? man_hash_find(mac) : MAN_MAX;
1.1       kristaps  386:
1.143     schwarze  387:        if (tok == MAN_MAX) {
1.136     schwarze  388:                mandoc_msg(MANDOCERR_MACRO, man->parse,
                    389:                    ln, ppos, buf + ppos - 1);
1.12      kristaps  390:                return(1);
1.1       kristaps  391:        }
                    392:
1.143     schwarze  393:        /* Skip a leading escape sequence or tab. */
                    394:
                    395:        switch (buf[offs]) {
                    396:        case '\\':
                    397:                cp = buf + offs + 1;
                    398:                mandoc_escape(&cp, NULL, NULL);
                    399:                offs = cp - buf;
                    400:                break;
                    401:        case '\t':
                    402:                offs++;
                    403:                break;
                    404:        default:
                    405:                break;
                    406:        }
                    407:
                    408:        /* Jump to the next non-whitespace word. */
1.1       kristaps  409:
1.144     schwarze  410:        while (buf[offs] && buf[offs] == ' ')
1.107     kristaps  411:                offs++;
1.1       kristaps  412:
1.129     schwarze  413:        /*
1.62      kristaps  414:         * Trailing whitespace.  Note that tabs are allowed to be passed
                    415:         * into the parser as "text", so we only warn about spaces here.
                    416:         */
1.48      kristaps  417:
1.144     schwarze  418:        if (buf[offs] == '\0' && buf[offs - 1] == ' ')
1.131     schwarze  419:                mandoc_msg(MANDOCERR_SPACE_EOL, man->parse,
                    420:                    ln, offs - 1, NULL);
1.48      kristaps  421:
1.129     schwarze  422:        /*
1.149     schwarze  423:         * Some macros break next-line scopes; otherwise, remember
                    424:         * whether we are in next-line scope for a block head.
1.50      kristaps  425:         */
1.34      kristaps  426:
1.149     schwarze  427:        man_breakscope(man, tok);
1.134     schwarze  428:        bline = man->flags & MAN_BLINE;
1.59      kristaps  429:
                    430:        /* Call to handler... */
1.1       kristaps  431:
1.53      kristaps  432:        assert(man_macros[tok].fp);
1.144     schwarze  433:        (*man_macros[tok].fp)(man, tok, ln, ppos, &offs, buf);
1.123     schwarze  434:
                    435:        /* In quick mode (for mandocdb), abort after the NAME section. */
                    436:
1.144     schwarze  437:        if (man->quick && tok == MAN_SH) {
1.130     schwarze  438:                n = man->last;
1.150     schwarze  439:                if (n->type == ROFFT_BODY &&
1.130     schwarze  440:                    strcmp(n->prev->child->string, "NAME"))
                    441:                        return(2);
                    442:        }
1.1       kristaps  443:
1.129     schwarze  444:        /*
1.135     schwarze  445:         * If we are in a next-line scope for a block head,
                    446:         * close it out now and switch to the body,
                    447:         * unless the next-line scope is allowed to continue.
1.29      kristaps  448:         */
                    449:
1.135     schwarze  450:        if ( ! bline || man->flags & MAN_ELINE ||
                    451:            man_macros[tok].flags & MAN_NSCOPED)
1.29      kristaps  452:                return(1);
                    453:
1.144     schwarze  454:        assert(man->flags & MAN_BLINE);
1.119     schwarze  455:        man->flags &= ~MAN_BLINE;
1.11      kristaps  456:
1.144     schwarze  457:        man_unscope(man, man->last->parent);
                    458:        man_body_alloc(man, ln, ppos, man->last->tok);
                    459:        return(1);
1.149     schwarze  460: }
                    461:
                    462: void
1.153     schwarze  463: man_breakscope(struct roff_man *man, int tok)
1.149     schwarze  464: {
1.151     schwarze  465:        struct roff_node *n;
1.149     schwarze  466:
                    467:        /*
                    468:         * An element next line scope is open,
                    469:         * and the new macro is not allowed inside elements.
                    470:         * Delete the element that is being broken.
                    471:         */
                    472:
                    473:        if (man->flags & MAN_ELINE && (tok == MAN_MAX ||
                    474:            ! (man_macros[tok].flags & MAN_NSCOPED))) {
                    475:                n = man->last;
1.150     schwarze  476:                assert(n->type != ROFFT_TEXT);
1.149     schwarze  477:                if (man_macros[n->tok].flags & MAN_NSCOPED)
                    478:                        n = n->parent;
                    479:
                    480:                mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse,
                    481:                    n->line, n->pos, "%s breaks %s",
                    482:                    tok == MAN_MAX ? "TS" : man_macronames[tok],
                    483:                    man_macronames[n->tok]);
                    484:
                    485:                man_node_delete(man, n);
                    486:                man->flags &= ~MAN_ELINE;
                    487:        }
                    488:
                    489:        /*
                    490:         * A block header next line scope is open,
                    491:         * and the new macro is not allowed inside block headers.
                    492:         * Delete the block that is being broken.
                    493:         */
                    494:
                    495:        if (man->flags & MAN_BLINE && (tok == MAN_MAX ||
                    496:            man_macros[tok].flags & MAN_BSCOPE)) {
                    497:                n = man->last;
1.150     schwarze  498:                if (n->type == ROFFT_TEXT)
1.149     schwarze  499:                        n = n->parent;
                    500:                if ( ! (man_macros[n->tok].flags & MAN_BSCOPE))
                    501:                        n = n->parent;
                    502:
1.150     schwarze  503:                assert(n->type == ROFFT_HEAD);
1.149     schwarze  504:                n = n->parent;
1.150     schwarze  505:                assert(n->type == ROFFT_BLOCK);
1.149     schwarze  506:                assert(man_macros[n->tok].flags & MAN_SCOPED);
                    507:
                    508:                mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse,
                    509:                    n->line, n->pos, "%s breaks %s",
                    510:                    tok == MAN_MAX ? "TS" : man_macronames[tok],
                    511:                    man_macronames[n->tok]);
                    512:
                    513:                man_node_delete(man, n);
                    514:                man->flags &= ~MAN_BLINE;
                    515:        }
1.1       kristaps  516: }
1.51      kristaps  517:
1.54      kristaps  518: /*
1.119     schwarze  519:  * Unlink a node from its context.  If "man" is provided, the last parse
1.54      kristaps  520:  * point will also be adjusted accordingly.
                    521:  */
                    522: static void
1.153     schwarze  523: man_node_unlink(struct roff_man *man, struct roff_node *n)
1.51      kristaps  524: {
                    525:
1.54      kristaps  526:        /* Adjust siblings. */
                    527:
                    528:        if (n->prev)
1.51      kristaps  529:                n->prev->next = n->next;
1.54      kristaps  530:        if (n->next)
                    531:                n->next->prev = n->prev;
                    532:
                    533:        /* Adjust parent. */
                    534:
                    535:        if (n->parent) {
                    536:                n->parent->nchild--;
                    537:                if (n->parent->child == n)
                    538:                        n->parent->child = n->prev ? n->prev : n->next;
                    539:        }
                    540:
                    541:        /* Adjust parse point, if applicable. */
                    542:
1.119     schwarze  543:        if (man && man->last == n) {
1.54      kristaps  544:                /*XXX: this can occur when bailing from validation. */
                    545:                /*assert(NULL == n->next);*/
                    546:                if (n->prev) {
1.119     schwarze  547:                        man->last = n->prev;
1.153     schwarze  548:                        man->next = ROFF_NEXT_SIBLING;
1.54      kristaps  549:                } else {
1.119     schwarze  550:                        man->last = n->parent;
1.153     schwarze  551:                        man->next = ROFF_NEXT_CHILD;
1.51      kristaps  552:                }
                    553:        }
                    554:
1.119     schwarze  555:        if (man && man->first == n)
                    556:                man->first = NULL;
1.112     kristaps  557: }
                    558:
                    559: const struct mparse *
1.153     schwarze  560: man_mparse(const struct roff_man *man)
1.112     kristaps  561: {
                    562:
1.119     schwarze  563:        assert(man && man->parse);
                    564:        return(man->parse);
1.126     schwarze  565: }
                    566:
                    567: void
1.151     schwarze  568: man_deroff(char **dest, const struct roff_node *n)
1.126     schwarze  569: {
                    570:        char    *cp;
                    571:        size_t   sz;
                    572:
1.150     schwarze  573:        if (n->type != ROFFT_TEXT) {
1.126     schwarze  574:                for (n = n->child; n; n = n->next)
                    575:                        man_deroff(dest, n);
                    576:                return;
                    577:        }
                    578:
1.127     schwarze  579:        /* Skip leading whitespace and escape sequences. */
1.126     schwarze  580:
1.127     schwarze  581:        cp = n->string;
                    582:        while ('\0' != *cp) {
                    583:                if ('\\' == *cp) {
                    584:                        cp++;
                    585:                        mandoc_escape((const char **)&cp, NULL, NULL);
                    586:                } else if (isspace((unsigned char)*cp))
                    587:                        cp++;
                    588:                else
1.126     schwarze  589:                        break;
1.127     schwarze  590:        }
1.126     schwarze  591:
                    592:        /* Skip trailing whitespace. */
                    593:
                    594:        for (sz = strlen(cp); sz; sz--)
                    595:                if (0 == isspace((unsigned char)cp[sz-1]))
                    596:                        break;
                    597:
                    598:        /* Skip empty strings. */
                    599:
                    600:        if (0 == sz)
                    601:                return;
                    602:
                    603:        if (NULL == *dest) {
                    604:                *dest = mandoc_strndup(cp, sz);
                    605:                return;
                    606:        }
                    607:
                    608:        mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
                    609:        free(*dest);
                    610:        *dest = cp;
1.23      kristaps  611: }

CVSweb