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

Annotation of mandoc/man.c, Revision 1.147

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

CVSweb