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

Annotation of mandoc/man.c, Revision 1.155

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

CVSweb