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

Annotation of mandoc/man.c, Revision 1.152

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

CVSweb