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

Annotation of mandoc/man.c, Revision 1.160

1.160   ! schwarze    1: /*     $Id: man.c,v 1.159 2015/04/19 14:00: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.158     schwarze   35: #include "roff_int.h"
1.1       kristaps   36: #include "libman.h"
                     37:
1.129     schwarze   38: const  char *const __man_macronames[MAN_MAX] = {
1.21      kristaps   39:        "br",           "TH",           "SH",           "SS",
1.129     schwarze   40:        "TP",           "LP",           "PP",           "P",
1.1       kristaps   41:        "IP",           "HP",           "SM",           "SB",
                     42:        "BI",           "IB",           "BR",           "RB",
1.11      kristaps   43:        "R",            "B",            "I",            "IR",
1.148     schwarze   44:        "RI",           "sp",           "nf",
1.92      kristaps   45:        "fi",           "RE",           "RS",           "DT",
                     46:        "UC",           "PD",           "AT",           "in",
1.120     schwarze   47:        "ft",           "OP",           "EX",           "EE",
1.128     schwarze   48:        "UR",           "UE",           "ll"
1.1       kristaps   49:        };
                     50:
                     51: const  char * const *man_macronames = __man_macronames;
                     52:
1.153     schwarze   53: static void             man_descope(struct roff_man *, int, int);
                     54: static int              man_ptext(struct roff_man *, int, char *, int);
                     55: static int              man_pmacro(struct roff_man *, int, char *, int);
1.1       kristaps   56:
                     57:
1.45      kristaps   58: void
1.153     schwarze   59: man_endparse(struct roff_man *man)
1.1       kristaps   60: {
                     61:
1.144     schwarze   62:        man_macroend(man);
1.1       kristaps   63: }
                     64:
                     65: int
1.153     schwarze   66: man_parseln(struct roff_man *man, int ln, char *buf, int offs)
1.1       kristaps   67: {
                     68:
1.150     schwarze   69:        if (man->last->type != ROFFT_EQN || ln > man->last->line)
1.141     schwarze   70:                man->flags |= MAN_NEWLINE;
1.97      kristaps   71:
1.119     schwarze   72:        return (roff_getcontrol(man->roff, buf, &offs) ?
1.129     schwarze   73:            man_pmacro(man, ln, buf, offs) :
                     74:            man_ptext(man, ln, buf, offs));
1.1       kristaps   75: }
1.2       kristaps   76:
1.144     schwarze   77: void
1.153     schwarze   78: man_elem_alloc(struct roff_man *man, int line, int pos, int tok)
1.1       kristaps   79: {
1.151     schwarze   80:        struct roff_node *p;
1.1       kristaps   81:
1.158     schwarze   82:        p = roff_node_alloc(man, line, pos, ROFFT_ELEM, tok);
                     83:        roff_node_append(man, p);
1.153     schwarze   84:        man->next = ROFF_NEXT_CHILD;
1.29      kristaps   85: }
                     86:
1.144     schwarze   87: void
1.153     schwarze   88: man_block_alloc(struct roff_man *man, int line, int pos, int tok)
1.29      kristaps   89: {
1.151     schwarze   90:        struct roff_node *p;
1.29      kristaps   91:
1.158     schwarze   92:        p = roff_node_alloc(man, line, pos, ROFFT_BLOCK, tok);
                     93:        roff_node_append(man, p);
1.153     schwarze   94:        man->next = ROFF_NEXT_CHILD;
1.29      kristaps   95: }
                     96:
1.144     schwarze   97: static void
1.153     schwarze   98: man_descope(struct roff_man *man, int line, int offs)
1.94      kristaps   99: {
                    100:        /*
                    101:         * Co-ordinate what happens with having a next-line scope open:
                    102:         * first close out the element scope (if applicable), then close
                    103:         * out the block scope (also if applicable).
                    104:         */
                    105:
1.144     schwarze  106:        if (man->flags & MAN_ELINE) {
1.119     schwarze  107:                man->flags &= ~MAN_ELINE;
1.144     schwarze  108:                man_unscope(man, man->last->parent);
1.94      kristaps  109:        }
1.144     schwarze  110:        if ( ! (man->flags & MAN_BLINE))
                    111:                return;
1.119     schwarze  112:        man->flags &= ~MAN_BLINE;
1.144     schwarze  113:        man_unscope(man, man->last->parent);
1.158     schwarze  114:        roff_body_alloc(man, line, offs, man->last->tok);
1.94      kristaps  115: }
                    116:
1.1       kristaps  117: static int
1.153     schwarze  118: man_ptext(struct roff_man *man, int line, char *buf, int offs)
1.1       kristaps  119: {
1.61      kristaps  120:        int              i;
1.60      kristaps  121:
1.35      kristaps  122:        /* Literal free-form text whitespace is preserved. */
                    123:
1.144     schwarze  124:        if (man->flags & MAN_LITERAL) {
1.160   ! schwarze  125:                roff_word_alloc(man, line, offs, buf + offs);
1.144     schwarze  126:                man_descope(man, line, offs);
                    127:                return(1);
1.35      kristaps  128:        }
                    129:
1.144     schwarze  130:        for (i = offs; buf[i] == ' '; i++)
1.31      kristaps  131:                /* Skip leading whitespace. */ ;
1.48      kristaps  132:
1.121     schwarze  133:        /*
                    134:         * Blank lines are ignored right after headings
                    135:         * but add a single vertical space elsewhere.
                    136:         */
                    137:
1.144     schwarze  138:        if (buf[i] == '\0') {
1.61      kristaps  139:                /* Allocate a blank entry. */
1.144     schwarze  140:                if (man->last->tok != MAN_SH &&
                    141:                    man->last->tok != MAN_SS) {
                    142:                        man_elem_alloc(man, line, offs, MAN_sp);
1.153     schwarze  143:                        man->next = ROFF_NEXT_SIBLING;
1.121     schwarze  144:                }
1.118     schwarze  145:                return(1);
1.31      kristaps  146:        }
                    147:
1.129     schwarze  148:        /*
1.63      kristaps  149:         * Warn if the last un-escaped character is whitespace. Then
1.129     schwarze  150:         * strip away the remaining spaces (tabs stay!).
1.63      kristaps  151:         */
1.29      kristaps  152:
1.61      kristaps  153:        i = (int)strlen(buf);
                    154:        assert(i);
1.49      kristaps  155:
1.63      kristaps  156:        if (' ' == buf[i - 1] || '\t' == buf[i - 1]) {
1.64      kristaps  157:                if (i > 1 && '\\' != buf[i - 2])
1.131     schwarze  158:                        mandoc_msg(MANDOCERR_SPACE_EOL, man->parse,
                    159:                            line, i - 1, NULL);
1.63      kristaps  160:
                    161:                for (--i; i && ' ' == buf[i]; i--)
                    162:                        /* Spin back to non-space. */ ;
                    163:
                    164:                /* Jump ahead of escaped whitespace. */
                    165:                i += '\\' == buf[i] ? 2 : 1;
                    166:
                    167:                buf[i] = '\0';
                    168:        }
1.160   ! schwarze  169:        roff_word_alloc(man, line, offs, buf + offs);
1.65      kristaps  170:
                    171:        /*
                    172:         * End-of-sentence check.  If the last character is an unescaped
                    173:         * EOS character, then flag the node as being the end of a
                    174:         * sentence.  The front-end will know how to interpret this.
                    175:         */
1.67      kristaps  176:
1.65      kristaps  177:        assert(i);
1.122     schwarze  178:        if (mandoc_eos(buf, (size_t)i))
1.119     schwarze  179:                man->last->flags |= MAN_EOS;
1.31      kristaps  180:
1.144     schwarze  181:        man_descope(man, line, offs);
                    182:        return(1);
1.1       kristaps  183: }
                    184:
1.96      kristaps  185: static int
1.153     schwarze  186: man_pmacro(struct roff_man *man, int ln, char *buf, int offs)
1.1       kristaps  187: {
1.151     schwarze  188:        struct roff_node *n;
1.143     schwarze  189:        const char      *cp;
1.151     schwarze  190:        int              tok;
1.134     schwarze  191:        int              i, ppos;
                    192:        int              bline;
1.143     schwarze  193:        char             mac[5];
1.1       kristaps  194:
1.107     kristaps  195:        ppos = offs;
1.9       kristaps  196:
1.56      kristaps  197:        /*
1.107     kristaps  198:         * Copy the first word into a nil-terminated buffer.
1.143     schwarze  199:         * Stop when a space, tab, escape, or eoln is encountered.
1.56      kristaps  200:         */
1.62      kristaps  201:
1.107     kristaps  202:        i = 0;
1.143     schwarze  203:        while (i < 4 && strchr(" \t\\", buf[offs]) == NULL)
1.107     kristaps  204:                mac[i++] = buf[offs++];
1.10      kristaps  205:
1.107     kristaps  206:        mac[i] = '\0';
1.1       kristaps  207:
1.159     schwarze  208:        tok = (i > 0 && i < 4) ? man_hash_find(mac) : TOKEN_NONE;
1.1       kristaps  209:
1.159     schwarze  210:        if (tok == TOKEN_NONE) {
1.136     schwarze  211:                mandoc_msg(MANDOCERR_MACRO, man->parse,
                    212:                    ln, ppos, buf + ppos - 1);
1.12      kristaps  213:                return(1);
1.1       kristaps  214:        }
                    215:
1.143     schwarze  216:        /* Skip a leading escape sequence or tab. */
                    217:
                    218:        switch (buf[offs]) {
                    219:        case '\\':
                    220:                cp = buf + offs + 1;
                    221:                mandoc_escape(&cp, NULL, NULL);
                    222:                offs = cp - buf;
                    223:                break;
                    224:        case '\t':
                    225:                offs++;
                    226:                break;
                    227:        default:
                    228:                break;
                    229:        }
                    230:
                    231:        /* Jump to the next non-whitespace word. */
1.1       kristaps  232:
1.144     schwarze  233:        while (buf[offs] && buf[offs] == ' ')
1.107     kristaps  234:                offs++;
1.1       kristaps  235:
1.129     schwarze  236:        /*
1.62      kristaps  237:         * Trailing whitespace.  Note that tabs are allowed to be passed
                    238:         * into the parser as "text", so we only warn about spaces here.
                    239:         */
1.48      kristaps  240:
1.144     schwarze  241:        if (buf[offs] == '\0' && buf[offs - 1] == ' ')
1.131     schwarze  242:                mandoc_msg(MANDOCERR_SPACE_EOL, man->parse,
                    243:                    ln, offs - 1, NULL);
1.48      kristaps  244:
1.129     schwarze  245:        /*
1.149     schwarze  246:         * Some macros break next-line scopes; otherwise, remember
                    247:         * whether we are in next-line scope for a block head.
1.50      kristaps  248:         */
1.34      kristaps  249:
1.149     schwarze  250:        man_breakscope(man, tok);
1.134     schwarze  251:        bline = man->flags & MAN_BLINE;
1.59      kristaps  252:
                    253:        /* Call to handler... */
1.1       kristaps  254:
1.53      kristaps  255:        assert(man_macros[tok].fp);
1.144     schwarze  256:        (*man_macros[tok].fp)(man, tok, ln, ppos, &offs, buf);
1.123     schwarze  257:
                    258:        /* In quick mode (for mandocdb), abort after the NAME section. */
                    259:
1.144     schwarze  260:        if (man->quick && tok == MAN_SH) {
1.130     schwarze  261:                n = man->last;
1.150     schwarze  262:                if (n->type == ROFFT_BODY &&
1.130     schwarze  263:                    strcmp(n->prev->child->string, "NAME"))
                    264:                        return(2);
                    265:        }
1.1       kristaps  266:
1.129     schwarze  267:        /*
1.135     schwarze  268:         * If we are in a next-line scope for a block head,
                    269:         * close it out now and switch to the body,
                    270:         * unless the next-line scope is allowed to continue.
1.29      kristaps  271:         */
                    272:
1.135     schwarze  273:        if ( ! bline || man->flags & MAN_ELINE ||
                    274:            man_macros[tok].flags & MAN_NSCOPED)
1.29      kristaps  275:                return(1);
                    276:
1.144     schwarze  277:        assert(man->flags & MAN_BLINE);
1.119     schwarze  278:        man->flags &= ~MAN_BLINE;
1.11      kristaps  279:
1.144     schwarze  280:        man_unscope(man, man->last->parent);
1.158     schwarze  281:        roff_body_alloc(man, ln, ppos, man->last->tok);
1.144     schwarze  282:        return(1);
1.149     schwarze  283: }
                    284:
                    285: void
1.153     schwarze  286: man_breakscope(struct roff_man *man, int tok)
1.149     schwarze  287: {
1.151     schwarze  288:        struct roff_node *n;
1.149     schwarze  289:
                    290:        /*
                    291:         * An element next line scope is open,
                    292:         * and the new macro is not allowed inside elements.
                    293:         * Delete the element that is being broken.
                    294:         */
                    295:
1.159     schwarze  296:        if (man->flags & MAN_ELINE && (tok == TOKEN_NONE ||
1.149     schwarze  297:            ! (man_macros[tok].flags & MAN_NSCOPED))) {
                    298:                n = man->last;
1.150     schwarze  299:                assert(n->type != ROFFT_TEXT);
1.149     schwarze  300:                if (man_macros[n->tok].flags & MAN_NSCOPED)
                    301:                        n = n->parent;
                    302:
                    303:                mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse,
                    304:                    n->line, n->pos, "%s breaks %s",
1.159     schwarze  305:                    tok == TOKEN_NONE ? "TS" : man_macronames[tok],
1.149     schwarze  306:                    man_macronames[n->tok]);
                    307:
1.158     schwarze  308:                roff_node_delete(man, n);
1.149     schwarze  309:                man->flags &= ~MAN_ELINE;
                    310:        }
                    311:
                    312:        /*
                    313:         * A block header next line scope is open,
                    314:         * and the new macro is not allowed inside block headers.
                    315:         * Delete the block that is being broken.
                    316:         */
                    317:
1.159     schwarze  318:        if (man->flags & MAN_BLINE && (tok == TOKEN_NONE ||
1.149     schwarze  319:            man_macros[tok].flags & MAN_BSCOPE)) {
                    320:                n = man->last;
1.150     schwarze  321:                if (n->type == ROFFT_TEXT)
1.149     schwarze  322:                        n = n->parent;
                    323:                if ( ! (man_macros[n->tok].flags & MAN_BSCOPE))
                    324:                        n = n->parent;
                    325:
1.150     schwarze  326:                assert(n->type == ROFFT_HEAD);
1.149     schwarze  327:                n = n->parent;
1.150     schwarze  328:                assert(n->type == ROFFT_BLOCK);
1.149     schwarze  329:                assert(man_macros[n->tok].flags & MAN_SCOPED);
                    330:
                    331:                mandoc_vmsg(MANDOCERR_BLK_LINE, man->parse,
                    332:                    n->line, n->pos, "%s breaks %s",
1.159     schwarze  333:                    tok == TOKEN_NONE ? "TS" : man_macronames[tok],
1.149     schwarze  334:                    man_macronames[n->tok]);
                    335:
1.158     schwarze  336:                roff_node_delete(man, n);
1.149     schwarze  337:                man->flags &= ~MAN_BLINE;
                    338:        }
1.112     kristaps  339: }
                    340:
                    341: const struct mparse *
1.153     schwarze  342: man_mparse(const struct roff_man *man)
1.112     kristaps  343: {
                    344:
1.119     schwarze  345:        assert(man && man->parse);
                    346:        return(man->parse);
1.126     schwarze  347: }
                    348:
                    349: void
1.151     schwarze  350: man_deroff(char **dest, const struct roff_node *n)
1.126     schwarze  351: {
                    352:        char    *cp;
                    353:        size_t   sz;
                    354:
1.150     schwarze  355:        if (n->type != ROFFT_TEXT) {
1.126     schwarze  356:                for (n = n->child; n; n = n->next)
                    357:                        man_deroff(dest, n);
                    358:                return;
                    359:        }
                    360:
1.127     schwarze  361:        /* Skip leading whitespace and escape sequences. */
1.126     schwarze  362:
1.127     schwarze  363:        cp = n->string;
                    364:        while ('\0' != *cp) {
                    365:                if ('\\' == *cp) {
                    366:                        cp++;
                    367:                        mandoc_escape((const char **)&cp, NULL, NULL);
                    368:                } else if (isspace((unsigned char)*cp))
                    369:                        cp++;
                    370:                else
1.126     schwarze  371:                        break;
1.127     schwarze  372:        }
1.126     schwarze  373:
                    374:        /* Skip trailing whitespace. */
                    375:
                    376:        for (sz = strlen(cp); sz; sz--)
                    377:                if (0 == isspace((unsigned char)cp[sz-1]))
                    378:                        break;
                    379:
                    380:        /* Skip empty strings. */
                    381:
                    382:        if (0 == sz)
                    383:                return;
                    384:
                    385:        if (NULL == *dest) {
                    386:                *dest = mandoc_strndup(cp, sz);
                    387:                return;
                    388:        }
                    389:
                    390:        mandoc_asprintf(&cp, "%s %*s", *dest, (int)sz, cp);
                    391:        free(*dest);
                    392:        *dest = cp;
1.23      kristaps  393: }

CVSweb