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

Annotation of mandoc/man.c, Revision 1.153

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

CVSweb