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

Annotation of mandoc/man.c, Revision 1.136

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

CVSweb