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

Annotation of mandoc/man_macro.c, Revision 1.112

1.112   ! schwarze    1: /*     $Id: man_macro.c,v 1.111 2015/09/26 00:54:04 schwarze Exp $ */
1.1       kristaps    2: /*
1.65      schwarze    3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.93      schwarze    4:  * Copyright (c) 2012, 2013, 2014, 2015 Ingo Schwarze <schwarze@openbsd.org>
1.79      schwarze    5:  * Copyright (c) 2013 Franco Fichtner <franco@lastsummer.de>
1.1       kristaps    6:  *
                      7:  * Permission to use, copy, modify, and distribute this software for any
1.14      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.101     schwarze   11:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.14      kristaps   12:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.101     schwarze   13:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.14      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.30      kristaps   19: #include "config.h"
1.88      schwarze   20:
                     21: #include <sys/types.h>
1.30      kristaps   22:
1.1       kristaps   23: #include <assert.h>
                     24: #include <ctype.h>
                     25: #include <stdlib.h>
                     26: #include <string.h>
                     27:
1.101     schwarze   28: #include "mandoc.h"
                     29: #include "roff.h"
1.58      kristaps   30: #include "man.h"
1.57      kristaps   31: #include "libmandoc.h"
1.106     schwarze   32: #include "roff_int.h"
1.1       kristaps   33: #include "libman.h"
                     34:
1.91      schwarze   35: static void             blk_close(MACRO_PROT_ARGS);
                     36: static void             blk_exp(MACRO_PROT_ARGS);
                     37: static void             blk_imp(MACRO_PROT_ARGS);
                     38: static void             in_line_eoln(MACRO_PROT_ARGS);
1.105     schwarze   39: static int              man_args(struct roff_man *, int,
1.60      kristaps   40:                                int *, char *, char **);
1.105     schwarze   41: static void             rew_scope(struct roff_man *, int);
1.19      kristaps   42:
                     43: const  struct man_macro __man_macros[MAN_MAX] = {
1.31      kristaps   44:        { in_line_eoln, MAN_NSCOPED }, /* br */
1.66      schwarze   45:        { in_line_eoln, MAN_BSCOPE }, /* TH */
                     46:        { blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SH */
                     47:        { blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* SS */
1.99      schwarze   48:        { blk_imp, MAN_BSCOPE | MAN_SCOPED }, /* TP */
1.66      schwarze   49:        { blk_imp, MAN_BSCOPE }, /* LP */
                     50:        { blk_imp, MAN_BSCOPE }, /* PP */
                     51:        { blk_imp, MAN_BSCOPE }, /* P */
                     52:        { blk_imp, MAN_BSCOPE }, /* IP */
                     53:        { blk_imp, MAN_BSCOPE }, /* HP */
1.90      schwarze   54:        { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* SM */
                     55:        { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* SB */
1.19      kristaps   56:        { in_line_eoln, 0 }, /* BI */
                     57:        { in_line_eoln, 0 }, /* IB */
                     58:        { in_line_eoln, 0 }, /* BR */
                     59:        { in_line_eoln, 0 }, /* RB */
1.90      schwarze   60:        { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* R */
                     61:        { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* B */
                     62:        { in_line_eoln, MAN_SCOPED | MAN_JOIN }, /* I */
1.19      kristaps   63:        { in_line_eoln, 0 }, /* IR */
                     64:        { in_line_eoln, 0 }, /* RI */
1.31      kristaps   65:        { in_line_eoln, MAN_NSCOPED }, /* sp */
1.110     schwarze   66:        { in_line_eoln, MAN_NSCOPED }, /* nf */
                     67:        { in_line_eoln, MAN_NSCOPED }, /* fi */
1.92      schwarze   68:        { blk_close, MAN_BSCOPE }, /* RE */
1.99      schwarze   69:        { blk_exp, MAN_BSCOPE }, /* RS */
1.23      kristaps   70:        { in_line_eoln, 0 }, /* DT */
1.28      kristaps   71:        { in_line_eoln, 0 }, /* UC */
1.100     schwarze   72:        { in_line_eoln, MAN_NSCOPED }, /* PD */
1.45      joerg      73:        { in_line_eoln, 0 }, /* AT */
1.49      kristaps   74:        { in_line_eoln, 0 }, /* in */
1.53      kristaps   75:        { in_line_eoln, 0 }, /* ft */
1.71      kristaps   76:        { in_line_eoln, 0 }, /* OP */
1.72      schwarze   77:        { in_line_eoln, MAN_BSCOPE }, /* EX */
                     78:        { in_line_eoln, MAN_BSCOPE }, /* EE */
1.99      schwarze   79:        { blk_exp, MAN_BSCOPE }, /* UR */
1.92      schwarze   80:        { blk_close, MAN_BSCOPE }, /* UE */
1.81      schwarze   81:        { in_line_eoln, 0 }, /* ll */
1.19      kristaps   82: };
1.9       kristaps   83:
1.19      kristaps   84: const  struct man_macro * const man_macros = __man_macros;
1.1       kristaps   85:
                     86:
1.91      schwarze   87: void
1.105     schwarze   88: man_unscope(struct roff_man *man, const struct roff_node *to)
1.1       kristaps   89: {
1.102     schwarze   90:        struct roff_node *n;
1.1       kristaps   91:
1.83      schwarze   92:        to = to->parent;
                     93:        n = man->last;
                     94:        while (n != to) {
1.87      schwarze   95:
                     96:                /* Reached the end of the document? */
                     97:
                     98:                if (to == NULL && ! (n->flags & MAN_VALID)) {
                     99:                        if (man->flags & (MAN_BLINE | MAN_ELINE) &&
                    100:                            man_macros[n->tok].flags & MAN_SCOPED) {
                    101:                                mandoc_vmsg(MANDOCERR_BLK_LINE,
                    102:                                    man->parse, n->line, n->pos,
                    103:                                    "EOF breaks %s",
                    104:                                    man_macronames[n->tok]);
                    105:                                if (man->flags & MAN_ELINE)
                    106:                                        man->flags &= ~MAN_ELINE;
                    107:                                else {
1.101     schwarze  108:                                        assert(n->type == ROFFT_HEAD);
1.87      schwarze  109:                                        n = n->parent;
                    110:                                        man->flags &= ~MAN_BLINE;
                    111:                                }
                    112:                                man->last = n;
                    113:                                n = n->parent;
1.106     schwarze  114:                                roff_node_delete(man, man->last);
1.87      schwarze  115:                                continue;
                    116:                        }
1.101     schwarze  117:                        if (n->type == ROFFT_BLOCK &&
1.99      schwarze  118:                            man_macros[n->tok].fp == blk_exp)
1.87      schwarze  119:                                mandoc_msg(MANDOCERR_BLK_NOEND,
                    120:                                    man->parse, n->line, n->pos,
                    121:                                    man_macronames[n->tok]);
                    122:                }
                    123:
1.55      kristaps  124:                /*
1.83      schwarze  125:                 * We might delete the man->last node
                    126:                 * in the post-validation phase.
                    127:                 * Save a pointer to the parent such that
                    128:                 * we know where to continue the iteration.
1.55      kristaps  129:                 */
1.89      schwarze  130:
1.83      schwarze  131:                man->last = n;
                    132:                n = n->parent;
1.91      schwarze  133:                man_valid_post(man);
1.19      kristaps  134:        }
1.89      schwarze  135:
                    136:        /*
                    137:         * If we ended up at the parent of the node we were
                    138:         * supposed to rewind to, that means the target node
                    139:         * got deleted, so add the next node we parse as a child
                    140:         * of the parent instead of as a sibling of the target.
                    141:         */
                    142:
                    143:        man->next = (man->last == to) ?
1.105     schwarze  144:            ROFF_NEXT_CHILD : ROFF_NEXT_SIBLING;
1.19      kristaps  145: }
1.1       kristaps  146:
1.19      kristaps  147: /*
                    148:  * Rewinding entails ascending the parse tree until a coherent point,
                    149:  * for example, the `SH' macro will close out any intervening `SS'
                    150:  * scopes.  When a scope is closed, it must be validated and actioned.
                    151:  */
1.91      schwarze  152: static void
1.105     schwarze  153: rew_scope(struct roff_man *man, int tok)
1.19      kristaps  154: {
1.102     schwarze  155:        struct roff_node *n;
1.7       kristaps  156:
1.104     schwarze  157:        /* Preserve empty paragraphs before RS. */
                    158:
                    159:        n = man->last;
                    160:        if (tok == MAN_RS && n->nchild == 0 &&
                    161:            (n->tok == MAN_P || n->tok == MAN_PP || n->tok == MAN_LP))
                    162:                return;
                    163:
                    164:        for (;;) {
                    165:                if (n->type == ROFFT_ROOT)
                    166:                        return;
                    167:                if (n->flags & MAN_VALID) {
                    168:                        n = n->parent;
                    169:                        continue;
                    170:                }
                    171:                if (n->type != ROFFT_BLOCK) {
                    172:                        if (n->parent->type == ROFFT_ROOT) {
                    173:                                man_unscope(man, n);
                    174:                                return;
                    175:                        } else {
                    176:                                n = n->parent;
                    177:                                continue;
                    178:                        }
                    179:                }
                    180:                if (tok != MAN_SH && (n->tok == MAN_SH ||
                    181:                    (tok != MAN_SS && (n->tok == MAN_SS ||
                    182:                     man_macros[n->tok].fp == blk_exp))))
1.91      schwarze  183:                        return;
1.104     schwarze  184:                man_unscope(man, n);
                    185:                n = man->last;
1.6       kristaps  186:        }
1.19      kristaps  187: }
                    188:
1.6       kristaps  189:
1.36      kristaps  190: /*
                    191:  * Close out a generic explicit macro.
                    192:  */
1.91      schwarze  193: void
1.21      kristaps  194: blk_close(MACRO_PROT_ARGS)
                    195: {
1.102     schwarze  196:        int                      ntok;
                    197:        const struct roff_node  *nn;
1.93      schwarze  198:        char                    *p;
                    199:        int                      nrew, target;
1.21      kristaps  200:
1.93      schwarze  201:        nrew = 1;
1.21      kristaps  202:        switch (tok) {
1.82      schwarze  203:        case MAN_RE:
1.21      kristaps  204:                ntok = MAN_RS;
1.93      schwarze  205:                if ( ! man_args(man, line, pos, buf, &p))
                    206:                        break;
                    207:                for (nn = man->last->parent; nn; nn = nn->parent)
1.101     schwarze  208:                        if (nn->tok == ntok && nn->type == ROFFT_BLOCK)
1.93      schwarze  209:                                nrew++;
                    210:                target = strtol(p, &p, 10);
                    211:                if (*p != '\0')
                    212:                        mandoc_vmsg(MANDOCERR_ARG_EXCESS, man->parse,
                    213:                            line, p - buf, "RE ... %s", p);
                    214:                if (target == 0)
                    215:                        target = 1;
                    216:                nrew -= target;
                    217:                if (nrew < 1) {
                    218:                        mandoc_vmsg(MANDOCERR_RE_NOTOPEN, man->parse,
                    219:                            line, ppos, "RE %d", target);
                    220:                        return;
                    221:                }
1.76      schwarze  222:                break;
1.82      schwarze  223:        case MAN_UE:
1.76      schwarze  224:                ntok = MAN_UR;
1.21      kristaps  225:                break;
                    226:        default:
                    227:                abort();
                    228:        }
                    229:
1.75      schwarze  230:        for (nn = man->last->parent; nn; nn = nn->parent)
1.101     schwarze  231:                if (nn->tok == ntok && nn->type == ROFFT_BLOCK && ! --nrew)
1.21      kristaps  232:                        break;
                    233:
1.91      schwarze  234:        if (nn == NULL) {
1.84      schwarze  235:                mandoc_msg(MANDOCERR_BLK_NOTOPEN, man->parse,
                    236:                    line, ppos, man_macronames[tok]);
1.104     schwarze  237:                rew_scope(man, MAN_PP);
1.95      schwarze  238:        } else {
                    239:                line = man->last->line;
                    240:                ppos = man->last->pos;
                    241:                ntok = man->last->tok;
1.83      schwarze  242:                man_unscope(man, nn);
1.95      schwarze  243:
                    244:                /* Move a trailing paragraph behind the block. */
                    245:
                    246:                if (ntok == MAN_LP || ntok == MAN_PP || ntok == MAN_P) {
                    247:                        *pos = strlen(buf);
                    248:                        blk_imp(man, ntok, line, ppos, pos, buf);
                    249:                }
                    250:        }
1.21      kristaps  251: }
                    252:
1.91      schwarze  253: void
1.35      kristaps  254: blk_exp(MACRO_PROT_ARGS)
                    255: {
1.102     schwarze  256:        struct roff_node *head;
1.98      schwarze  257:        char            *p;
1.59      kristaps  258:        int              la;
1.35      kristaps  259:
1.104     schwarze  260:        rew_scope(man, tok);
1.108     schwarze  261:        roff_block_alloc(man, line, ppos, tok);
1.106     schwarze  262:        head = roff_head_alloc(man, line, ppos, tok);
1.35      kristaps  263:
1.98      schwarze  264:        la = *pos;
                    265:        if (man_args(man, line, pos, buf, &p))
1.107     schwarze  266:                roff_word_alloc(man, line, la, p);
1.35      kristaps  267:
1.98      schwarze  268:        if (buf[*pos] != '\0')
                    269:                mandoc_vmsg(MANDOCERR_ARG_EXCESS,
                    270:                    man->parse, line, *pos, "%s ... %s",
                    271:                    man_macronames[tok], buf + *pos);
1.73      schwarze  272:
1.98      schwarze  273:        man_unscope(man, head);
1.106     schwarze  274:        roff_body_alloc(man, line, ppos, tok);
1.35      kristaps  275: }
                    276:
1.19      kristaps  277: /*
1.101     schwarze  278:  * Parse an implicit-block macro.  These contain a ROFFT_HEAD and a
                    279:  * ROFFT_BODY contained within a ROFFT_BLOCK.  Rules for closing out other
1.19      kristaps  280:  * scopes, such as `SH' closing out an `SS', are defined in the rew
                    281:  * routines.
                    282:  */
1.91      schwarze  283: void
1.19      kristaps  284: blk_imp(MACRO_PROT_ARGS)
                    285: {
1.59      kristaps  286:        int              la;
1.19      kristaps  287:        char            *p;
1.102     schwarze  288:        struct roff_node *n;
1.19      kristaps  289:
1.104     schwarze  290:        rew_scope(man, tok);
1.108     schwarze  291:        n = roff_block_alloc(man, line, ppos, tok);
1.106     schwarze  292:        if (n->tok == MAN_SH || n->tok == MAN_SS)
                    293:                man->flags &= ~MAN_LITERAL;
                    294:        n = roff_head_alloc(man, line, ppos, tok);
1.25      kristaps  295:
1.19      kristaps  296:        /* Add line arguments. */
1.3       kristaps  297:
1.19      kristaps  298:        for (;;) {
                    299:                la = *pos;
1.75      schwarze  300:                if ( ! man_args(man, line, pos, buf, &p))
1.19      kristaps  301:                        break;
1.107     schwarze  302:                roff_word_alloc(man, line, la, p);
1.6       kristaps  303:        }
                    304:
1.99      schwarze  305:        /*
                    306:         * For macros having optional next-line scope,
                    307:         * keep the head open if there were no arguments.
                    308:         * For `TP', always keep the head open.
                    309:         */
1.19      kristaps  310:
1.99      schwarze  311:        if (man_macros[tok].flags & MAN_SCOPED &&
                    312:            (tok == MAN_TP || n == man->last)) {
                    313:                man->flags |= MAN_BLINE;
                    314:                return;
1.27      kristaps  315:        }
1.99      schwarze  316:
                    317:        /* Close out the head and open the body. */
                    318:
1.103     schwarze  319:        man_unscope(man, n);
1.106     schwarze  320:        roff_body_alloc(man, line, ppos, tok);
1.4       kristaps  321: }
                    322:
1.91      schwarze  323: void
1.19      kristaps  324: in_line_eoln(MACRO_PROT_ARGS)
1.3       kristaps  325: {
1.59      kristaps  326:        int              la;
1.19      kristaps  327:        char            *p;
1.102     schwarze  328:        struct roff_node *n;
1.3       kristaps  329:
1.108     schwarze  330:        roff_elem_alloc(man, line, ppos, tok);
1.75      schwarze  331:        n = man->last;
1.3       kristaps  332:
1.19      kristaps  333:        for (;;) {
1.96      schwarze  334:                if (buf[*pos] != '\0' && (tok == MAN_br ||
                    335:                    tok == MAN_fi || tok == MAN_nf)) {
                    336:                        mandoc_vmsg(MANDOCERR_ARG_SKIP,
                    337:                            man->parse, line, *pos, "%s %s",
1.97      schwarze  338:                            man_macronames[tok], buf + *pos);
                    339:                        break;
                    340:                }
                    341:                if (buf[*pos] != '\0' && man->last != n &&
                    342:                    (tok == MAN_PD || tok == MAN_ft || tok == MAN_sp)) {
                    343:                        mandoc_vmsg(MANDOCERR_ARG_EXCESS,
                    344:                            man->parse, line, *pos, "%s ... %s",
1.96      schwarze  345:                            man_macronames[tok], buf + *pos);
                    346:                        break;
                    347:                }
1.19      kristaps  348:                la = *pos;
1.75      schwarze  349:                if ( ! man_args(man, line, pos, buf, &p))
1.19      kristaps  350:                        break;
1.90      schwarze  351:                if (man_macros[tok].flags & MAN_JOIN &&
1.101     schwarze  352:                    man->last->type == ROFFT_TEXT)
1.107     schwarze  353:                        roff_word_append(man, p);
1.91      schwarze  354:                else
1.107     schwarze  355:                        roff_word_alloc(man, line, la, p);
1.19      kristaps  356:        }
1.78      schwarze  357:
                    358:        /*
                    359:         * Append MAN_EOS in case the last snipped argument
                    360:         * ends with a dot, e.g. `.IR syslog (3).'
                    361:         */
                    362:
                    363:        if (n != man->last &&
1.80      schwarze  364:            mandoc_eos(man->last->string, strlen(man->last->string)))
1.78      schwarze  365:                man->last->flags |= MAN_EOS;
1.3       kristaps  366:
1.31      kristaps  367:        /*
                    368:         * If no arguments are specified and this is MAN_SCOPED (i.e.,
                    369:         * next-line scoped), then set our mode to indicate that we're
                    370:         * waiting for terms to load into our context.
                    371:         */
                    372:
1.91      schwarze  373:        if (n == man->last && man_macros[tok].flags & MAN_SCOPED) {
                    374:                assert( ! (man_macros[tok].flags & MAN_NSCOPED));
1.75      schwarze  375:                man->flags |= MAN_ELINE;
1.91      schwarze  376:                return;
1.31      kristaps  377:        }
1.64      kristaps  378:
1.101     schwarze  379:        assert(man->last->type != ROFFT_ROOT);
1.105     schwarze  380:        man->next = ROFF_NEXT_SIBLING;
1.82      schwarze  381:
1.19      kristaps  382:        /*
1.31      kristaps  383:         * Rewind our element scope.  Note that when TH is pruned, we'll
                    384:         * be back at the root, so make sure that we don't clobber as
                    385:         * its sibling.
1.19      kristaps  386:         */
1.3       kristaps  387:
1.75      schwarze  388:        for ( ; man->last; man->last = man->last->parent) {
                    389:                if (man->last == n)
1.19      kristaps  390:                        break;
1.101     schwarze  391:                if (man->last->type == ROFFT_ROOT)
1.19      kristaps  392:                        break;
1.91      schwarze  393:                man_valid_post(man);
1.19      kristaps  394:        }
1.3       kristaps  395:
1.75      schwarze  396:        assert(man->last);
1.3       kristaps  397:
                    398:        /*
1.82      schwarze  399:         * Same here regarding whether we're back at the root.
1.3       kristaps  400:         */
                    401:
1.101     schwarze  402:        if (man->last->type != ROFFT_ROOT)
1.91      schwarze  403:                man_valid_post(man);
1.19      kristaps  404: }
1.3       kristaps  405:
1.91      schwarze  406: void
1.109     schwarze  407: man_endparse(struct roff_man *man)
1.19      kristaps  408: {
1.22      kristaps  409:
1.91      schwarze  410:        man_unscope(man, man->first);
1.19      kristaps  411: }
1.3       kristaps  412:
1.60      kristaps  413: static int
1.105     schwarze  414: man_args(struct roff_man *man, int line, int *pos, char *buf, char **v)
1.60      kristaps  415: {
                    416:        char     *start;
                    417:
                    418:        assert(*pos);
                    419:        *v = start = buf + *pos;
                    420:        assert(' ' != *start);
                    421:
                    422:        if ('\0' == *start)
1.112   ! schwarze  423:                return 0;
1.60      kristaps  424:
1.75      schwarze  425:        *v = mandoc_getarg(man->parse, v, line, pos);
1.112   ! schwarze  426:        return 1;
1.60      kristaps  427: }

CVSweb