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

Annotation of mandoc/man.c, Revision 1.154

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

CVSweb