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

Annotation of mandoc/man.c, Revision 1.188

1.188   ! schwarze    1: /* $Id: man.c,v 1.187 2019/01/05 00:36:50 schwarze Exp $ */
1.1       kristaps    2: /*
1.188   ! schwarze    3:  * Copyright (c) 2013-2015,2017-2019,2022 Ingo Schwarze <schwarze@openbsd.org>
1.102     schwarze    4:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
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.158     schwarze   35: #include "roff_int.h"
1.1       kristaps   36: #include "libman.h"
                     37:
1.179     schwarze   38: static char            *man_hasc(char *);
1.153     schwarze   39: static int              man_ptext(struct roff_man *, int, char *, int);
                     40: static int              man_pmacro(struct roff_man *, int, char *, int);
1.1       kristaps   41:
                     42:
                     43: int
1.153     schwarze   44: man_parseln(struct roff_man *man, int ln, char *buf, int offs)
1.1       kristaps   45: {
                     46:
1.150     schwarze   47:        if (man->last->type != ROFFT_EQN || ln > man->last->line)
1.141     schwarze   48:                man->flags |= MAN_NEWLINE;
1.97      kristaps   49:
1.165     schwarze   50:        return roff_getcontrol(man->roff, buf, &offs) ?
1.129     schwarze   51:            man_pmacro(man, ln, buf, offs) :
1.165     schwarze   52:            man_ptext(man, ln, buf, offs);
1.1       kristaps   53: }
1.2       kristaps   54:
1.179     schwarze   55: /*
                     56:  * If the string ends with \c, return a pointer to the backslash.
                     57:  * Otherwise, return NULL.
                     58:  */
                     59: static char *
                     60: man_hasc(char *start)
                     61: {
                     62:        char    *cp, *ep;
                     63:
                     64:        ep = strchr(start, '\0') - 2;
                     65:        if (ep < start || ep[0] != '\\' || ep[1] != 'c')
                     66:                return NULL;
                     67:        for (cp = ep; cp > start; cp--)
                     68:                if (cp[-1] != '\\')
                     69:                        break;
                     70:        return (ep - cp) % 2 ? NULL : ep;
                     71: }
                     72:
1.188   ! schwarze   73: /*
        !            74:  * Rewind all open next-line scopes.
        !            75:  */
1.180     schwarze   76: void
1.179     schwarze   77: man_descope(struct roff_man *man, int line, int offs, char *start)
1.94      kristaps   78: {
1.179     schwarze   79:        /* Trailing \c keeps next-line scope open. */
                     80:
1.180     schwarze   81:        if (start != NULL && man_hasc(start) != NULL)
1.179     schwarze   82:                return;
                     83:
1.94      kristaps   84:        /*
                     85:         * Co-ordinate what happens with having a next-line scope open:
1.180     schwarze   86:         * first close out the element scopes (if applicable),
                     87:         * then close out the block scope (also if applicable).
1.94      kristaps   88:         */
                     89:
1.144     schwarze   90:        if (man->flags & MAN_ELINE) {
1.180     schwarze   91:                while (man->last->parent->type != ROFFT_ROOT &&
                     92:                    man_macro(man->last->parent->tok)->flags & MAN_ESCOPED)
                     93:                        man_unscope(man, man->last->parent);
1.119     schwarze   94:                man->flags &= ~MAN_ELINE;
1.94      kristaps   95:        }
1.144     schwarze   96:        if ( ! (man->flags & MAN_BLINE))
                     97:                return;
                     98:        man_unscope(man, man->last->parent);
1.158     schwarze   99:        roff_body_alloc(man, line, offs, man->last->tok);
1.187     schwarze  100:        man->flags &= ~(MAN_BLINE | ROFF_NONOFILL);
1.94      kristaps  101: }
                    102:
1.1       kristaps  103: static int
1.153     schwarze  104: man_ptext(struct roff_man *man, int line, char *buf, int offs)
1.1       kristaps  105: {
1.61      kristaps  106:        int              i;
1.175     schwarze  107:        char            *ep;
1.60      kristaps  108:
1.184     schwarze  109:        /* In no-fill mode, whitespace is preserved on text lines. */
1.35      kristaps  110:
1.184     schwarze  111:        if (man->flags & ROFF_NOFILL) {
1.160     schwarze  112:                roff_word_alloc(man, line, offs, buf + offs);
1.179     schwarze  113:                man_descope(man, line, offs, buf + offs);
1.165     schwarze  114:                return 1;
1.35      kristaps  115:        }
                    116:
1.144     schwarze  117:        for (i = offs; buf[i] == ' '; i++)
1.31      kristaps  118:                /* Skip leading whitespace. */ ;
1.48      kristaps  119:
1.121     schwarze  120:        /*
1.175     schwarze  121:         * Blank lines are ignored in next line scope
                    122:         * and right after headings and cancel preceding \c,
                    123:         * but add a single vertical space elsewhere.
1.121     schwarze  124:         */
                    125:
1.144     schwarze  126:        if (buf[i] == '\0') {
1.175     schwarze  127:                if (man->flags & (MAN_ELINE | MAN_BLINE)) {
1.181     schwarze  128:                        mandoc_msg(MANDOCERR_BLK_BLANK, line, 0, NULL);
1.175     schwarze  129:                        return 1;
1.121     schwarze  130:                }
1.175     schwarze  131:                if (man->last->tok == MAN_SH || man->last->tok == MAN_SS)
                    132:                        return 1;
1.179     schwarze  133:                if (man->last->type == ROFFT_TEXT &&
                    134:                    ((ep = man_hasc(man->last->string)) != NULL)) {
1.175     schwarze  135:                        *ep = '\0';
                    136:                        return 1;
                    137:                }
                    138:                roff_elem_alloc(man, line, offs, ROFF_sp);
                    139:                man->next = ROFF_NEXT_SIBLING;
1.165     schwarze  140:                return 1;
1.31      kristaps  141:        }
                    142:
1.129     schwarze  143:        /*
1.63      kristaps  144:         * Warn if the last un-escaped character is whitespace. Then
1.129     schwarze  145:         * strip away the remaining spaces (tabs stay!).
1.63      kristaps  146:         */
1.29      kristaps  147:
1.61      kristaps  148:        i = (int)strlen(buf);
                    149:        assert(i);
1.49      kristaps  150:
1.63      kristaps  151:        if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
1.64      kristaps  152:                if (i > 1 && '\\' != buf[i - 2])
1.181     schwarze  153:                        mandoc_msg(MANDOCERR_SPACE_EOL, line, i - 1, NULL);
1.63      kristaps  154:
                    155:                for (--i; i && ' ' == buf[i]; i--)
                    156:                        /* Spin back to non-space. */ ;
                    157:
                    158:                /* Jump ahead of escaped whitespace. */
                    159:                i += '\\' == buf[i] ? 2 : 1;
                    160:
                    161:                buf[i] = '\0';
                    162:        }
1.160     schwarze  163:        roff_word_alloc(man, line, offs, buf + offs);
1.65      kristaps  164:
                    165:        /*
                    166:         * End-of-sentence check.  If the last character is an unescaped
                    167:         * EOS character, then flag the node as being the end of a
                    168:         * sentence.  The front-end will know how to interpret this.
                    169:         */
1.67      kristaps  170:
1.65      kristaps  171:        assert(i);
1.122     schwarze  172:        if (mandoc_eos(buf, (size_t)i))
1.167     schwarze  173:                man->last->flags |= NODE_EOS;
1.31      kristaps  174:
1.179     schwarze  175:        man_descope(man, line, offs, buf + offs);
1.165     schwarze  176:        return 1;
1.1       kristaps  177: }
                    178:
1.96      kristaps  179: static int
1.153     schwarze  180: man_pmacro(struct roff_man *man, int ln, char *buf, int offs)
1.1       kristaps  181: {
1.151     schwarze  182:        struct roff_node *n;
1.143     schwarze  183:        const char      *cp;
1.170     schwarze  184:        size_t           sz;
                    185:        enum roff_tok    tok;
                    186:        int              ppos;
1.134     schwarze  187:        int              bline;
1.170     schwarze  188:
                    189:        /* Determine the line macro. */
1.1       kristaps  190:
1.107     kristaps  191:        ppos = offs;
1.170     schwarze  192:        tok = TOKEN_NONE;
                    193:        for (sz = 0; sz < 4 && strchr(" \t\\", buf[offs]) == NULL; sz++)
                    194:                offs++;
                    195:        if (sz > 0 && sz < 4)
                    196:                tok = roffhash_find(man->manmac, buf + ppos, sz);
1.159     schwarze  197:        if (tok == TOKEN_NONE) {
1.181     schwarze  198:                mandoc_msg(MANDOCERR_MACRO, ln, ppos, "%s", buf + ppos - 1);
1.165     schwarze  199:                return 1;
1.1       kristaps  200:        }
                    201:
1.143     schwarze  202:        /* Skip a leading escape sequence or tab. */
                    203:
                    204:        switch (buf[offs]) {
                    205:        case '\\':
                    206:                cp = buf + offs + 1;
                    207:                mandoc_escape(&cp, NULL, NULL);
                    208:                offs = cp - buf;
                    209:                break;
                    210:        case '\t':
                    211:                offs++;
                    212:                break;
                    213:        default:
                    214:                break;
                    215:        }
                    216:
                    217:        /* Jump to the next non-whitespace word. */
1.1       kristaps  218:
1.168     schwarze  219:        while (buf[offs] == ' ')
1.107     kristaps  220:                offs++;
1.1       kristaps  221:
1.129     schwarze  222:        /*
1.62      kristaps  223:         * Trailing whitespace.  Note that tabs are allowed to be passed
                    224:         * into the parser as "text", so we only warn about spaces here.
                    225:         */
1.48      kristaps  226:
1.144     schwarze  227:        if (buf[offs] == '\0' && buf[offs - 1] == ' ')
1.181     schwarze  228:                mandoc_msg(MANDOCERR_SPACE_EOL, ln, offs - 1, NULL);
1.48      kristaps  229:
1.129     schwarze  230:        /*
1.149     schwarze  231:         * Some macros break next-line scopes; otherwise, remember
                    232:         * whether we are in next-line scope for a block head.
1.50      kristaps  233:         */
1.34      kristaps  234:
1.149     schwarze  235:        man_breakscope(man, tok);
1.134     schwarze  236:        bline = man->flags & MAN_BLINE;
1.171     schwarze  237:
                    238:        /*
                    239:         * If the line in next-line scope ends with \c, keep the
                    240:         * next-line scope open for the subsequent input line.
                    241:         * That is not at all portable, only groff >= 1.22.4
                    242:         * does it, but *if* this weird idiom occurs in a manual
                    243:         * page, that's very likely what the author intended.
                    244:         */
                    245:
1.180     schwarze  246:        if (bline && man_hasc(buf + offs))
                    247:                bline = 0;
1.59      kristaps  248:
                    249:        /* Call to handler... */
1.1       kristaps  250:
1.177     schwarze  251:        (*man_macro(tok)->fp)(man, tok, ln, ppos, &offs, buf);
1.123     schwarze  252:
                    253:        /* In quick mode (for mandocdb), abort after the NAME section. */
                    254:
1.144     schwarze  255:        if (man->quick && tok == MAN_SH) {
1.130     schwarze  256:                n = man->last;
1.150     schwarze  257:                if (n->type == ROFFT_BODY &&
1.130     schwarze  258:                    strcmp(n->prev->child->string, "NAME"))
1.165     schwarze  259:                        return 2;
1.130     schwarze  260:        }
1.1       kristaps  261:
1.129     schwarze  262:        /*
1.135     schwarze  263:         * If we are in a next-line scope for a block head,
                    264:         * close it out now and switch to the body,
                    265:         * unless the next-line scope is allowed to continue.
1.29      kristaps  266:         */
                    267:
1.180     schwarze  268:        if (bline == 0 ||
                    269:            (man->flags & MAN_BLINE) == 0 ||
                    270:            man->flags & MAN_ELINE ||
1.177     schwarze  271:            man_macro(tok)->flags & MAN_NSCOPED)
1.165     schwarze  272:                return 1;
1.29      kristaps  273:
1.144     schwarze  274:        man_unscope(man, man->last->parent);
1.158     schwarze  275:        roff_body_alloc(man, ln, ppos, man->last->tok);
1.187     schwarze  276:        man->flags &= ~(MAN_BLINE | ROFF_NONOFILL);
1.165     schwarze  277:        return 1;
1.149     schwarze  278: }
                    279:
1.188   ! schwarze  280: /*
        !           281:  * Rewind open next-line scopes
        !           282:  * unless the tok request or macro is allowed inside them.
        !           283:  */
1.149     schwarze  284: void
1.153     schwarze  285: man_breakscope(struct roff_man *man, int tok)
1.149     schwarze  286: {
1.151     schwarze  287:        struct roff_node *n;
1.149     schwarze  288:
                    289:        /*
                    290:         * An element next line scope is open,
                    291:         * and the new macro is not allowed inside elements.
                    292:         * Delete the element that is being broken.
                    293:         */
                    294:
1.173     schwarze  295:        if (man->flags & MAN_ELINE && (tok < MAN_TH ||
1.177     schwarze  296:            (man_macro(tok)->flags & MAN_NSCOPED) == 0)) {
1.149     schwarze  297:                n = man->last;
1.176     schwarze  298:                if (n->type == ROFFT_TEXT)
                    299:                        n = n->parent;
                    300:                if (n->tok < MAN_TH ||
1.180     schwarze  301:                    (man_macro(n->tok)->flags & (MAN_NSCOPED | MAN_ESCOPED))
                    302:                     == MAN_NSCOPED)
1.149     schwarze  303:                        n = n->parent;
1.188   ! schwarze  304:                for (;;) {
        !           305:                        mandoc_msg(MANDOCERR_BLK_LINE, n->line, n->pos,
        !           306:                            "%s breaks %s", roff_name[tok], roff_name[n->tok]);
        !           307:                        if (n->parent->type != ROFFT_ELEM ||
        !           308:                            (man_macro(n->parent->tok)->flags &
        !           309:                             MAN_ESCOPED) == 0)
        !           310:                                break;
        !           311:                        n = n->parent;
        !           312:                }
1.158     schwarze  313:                roff_node_delete(man, n);
1.149     schwarze  314:                man->flags &= ~MAN_ELINE;
1.164     schwarze  315:        }
                    316:
                    317:        /*
                    318:         * Weird special case:
                    319:         * Switching fill mode closes section headers.
                    320:         */
                    321:
                    322:        if (man->flags & MAN_BLINE &&
1.184     schwarze  323:            (tok == ROFF_nf || tok == ROFF_fi) &&
1.164     schwarze  324:            (man->last->tok == MAN_SH || man->last->tok == MAN_SS)) {
                    325:                n = man->last;
                    326:                man_unscope(man, n);
                    327:                roff_body_alloc(man, n->line, n->pos, n->tok);
1.187     schwarze  328:                man->flags &= ~(MAN_BLINE | ROFF_NONOFILL);
1.149     schwarze  329:        }
                    330:
                    331:        /*
                    332:         * A block header next line scope is open,
                    333:         * and the new macro is not allowed inside block headers.
                    334:         * Delete the block that is being broken.
                    335:         */
                    336:
1.184     schwarze  337:        if (man->flags & MAN_BLINE && tok != ROFF_nf && tok != ROFF_fi &&
                    338:            (tok < MAN_TH || man_macro(tok)->flags & MAN_XSCOPE)) {
1.149     schwarze  339:                n = man->last;
1.150     schwarze  340:                if (n->type == ROFFT_TEXT)
1.149     schwarze  341:                        n = n->parent;
1.176     schwarze  342:                if (n->tok < MAN_TH ||
1.180     schwarze  343:                    (man_macro(n->tok)->flags & MAN_XSCOPE) == 0)
1.149     schwarze  344:                        n = n->parent;
                    345:
1.150     schwarze  346:                assert(n->type == ROFFT_HEAD);
1.149     schwarze  347:                n = n->parent;
1.150     schwarze  348:                assert(n->type == ROFFT_BLOCK);
1.180     schwarze  349:                assert(man_macro(n->tok)->flags & MAN_BSCOPED);
1.149     schwarze  350:
1.181     schwarze  351:                mandoc_msg(MANDOCERR_BLK_LINE, n->line, n->pos,
                    352:                    "%s breaks %s", roff_name[tok], roff_name[n->tok]);
1.149     schwarze  353:
1.158     schwarze  354:                roff_node_delete(man, n);
1.187     schwarze  355:                man->flags &= ~(MAN_BLINE | ROFF_NONOFILL);
1.149     schwarze  356:        }
1.23      kristaps  357: }

CVSweb