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

Annotation of mandoc/man.c, Revision 1.139

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

CVSweb