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

Annotation of mandoc/read.c, Revision 1.172

1.172   ! schwarze    1: /*     $Id: read.c,v 1.171 2017/06/06 15:01:04 schwarze Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.156     schwarze    4:  * Copyright (c) 2010-2017 Ingo Schwarze <schwarze@openbsd.org>
1.42      schwarze    5:  * Copyright (c) 2010, 2012 Joerg Sonnenberger <joerg@netbsd.org>
1.1       kristaps    6:  *
                      7:  * Permission to use, copy, modify, and distribute this software for any
                      8:  * purpose with or without fee is hereby granted, provided that the above
                      9:  * copyright notice and this permission notice appear in all copies.
                     10:  *
1.133     schwarze   11:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1       kristaps   12:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.133     schwarze   13:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1       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.
                     18:  */
1.11      kristaps   19: #include "config.h"
                     20:
1.80      schwarze   21: #include <sys/types.h>
1.82      schwarze   22: #include <sys/mman.h>
1.80      schwarze   23: #include <sys/stat.h>
1.1       kristaps   24:
                     25: #include <assert.h>
                     26: #include <ctype.h>
1.146     schwarze   27: #if HAVE_ERR
1.143     schwarze   28: #include <err.h>
1.146     schwarze   29: #endif
1.40      schwarze   30: #include <errno.h>
1.1       kristaps   31: #include <fcntl.h>
1.3       kristaps   32: #include <stdarg.h>
1.1       kristaps   33: #include <stdio.h>
                     34: #include <stdlib.h>
                     35: #include <string.h>
                     36: #include <unistd.h>
1.140     schwarze   37: #include <zlib.h>
1.1       kristaps   38:
1.133     schwarze   39: #include "mandoc_aux.h"
1.1       kristaps   40: #include "mandoc.h"
1.133     schwarze   41: #include "roff.h"
1.1       kristaps   42: #include "mdoc.h"
                     43: #include "man.h"
1.133     schwarze   44: #include "libmandoc.h"
1.139     schwarze   45: #include "roff_int.h"
1.1       kristaps   46:
                     47: #define        REPARSE_LIMIT   1000
                     48:
                     49: struct mparse {
1.160     schwarze   50:        struct roff      *roff; /* roff parser (!NULL) */
1.134     schwarze   51:        struct roff_man  *man; /* man parser */
1.45      schwarze   52:        char             *sodest; /* filename pointed to by .so */
1.83      schwarze   53:        const char       *file; /* filename of current input file */
                     54:        struct buf       *primary; /* buffer currently being parsed */
                     55:        struct buf       *secondary; /* preprocessed copy of input */
                     56:        const char       *defos; /* default operating system */
                     57:        mandocmsg         mmsg; /* warning/error message handler */
                     58:        enum mandoclevel  file_status; /* status of current parse */
                     59:        enum mandoclevel  wlevel; /* ignore messages below this */
                     60:        int               options; /* parser options */
1.140     schwarze   61:        int               gzip; /* current input file is gzipped */
1.93      schwarze   62:        int               filenc; /* encoding of the current file */
1.1       kristaps   63:        int               reparse_count; /* finite interp. stack */
1.83      schwarze   64:        int               line; /* line number in the file */
1.1       kristaps   65: };
                     66:
1.84      schwarze   67: static void      choose_parser(struct mparse *);
1.1       kristaps   68: static void      resize_buf(struct buf *, size_t);
1.163     schwarze   69: static int       mparse_buf_r(struct mparse *, struct buf, size_t, int);
1.40      schwarze   70: static int       read_whole_file(struct mparse *, const char *, int,
                     71:                                struct buf *, int *);
1.1       kristaps   72: static void      mparse_end(struct mparse *);
1.37      schwarze   73: static void      mparse_parse_buffer(struct mparse *, struct buf,
                     74:                        const char *);
1.1       kristaps   75:
1.3       kristaps   76: static const enum mandocerr    mandoclimits[MANDOCLEVEL_MAX] = {
                     77:        MANDOCERR_OK,
1.165     schwarze   78:        MANDOCERR_STYLE,
1.3       kristaps   79:        MANDOCERR_WARNING,
                     80:        MANDOCERR_ERROR,
1.112     schwarze   81:        MANDOCERR_UNSUPP,
1.3       kristaps   82:        MANDOCERR_MAX,
                     83:        MANDOCERR_MAX
                     84: };
                     85:
1.7       kristaps   86: static const char * const      mandocerrs[MANDOCERR_MAX] = {
                     87:        "ok",
                     88:
1.165     schwarze   89:        "generic style suggestion",
1.166     schwarze   90:
                     91:        "useless macro",
1.167     schwarze   92:        "consider using OS macro",
1.172   ! schwarze   93:        "errnos out of order",
        !            94:        "duplicate errno",
1.168     schwarze   95:        "description line ends with a full stop",
1.166     schwarze   96:
1.7       kristaps   97:        "generic warning",
                     98:
                     99:        /* related to the prologue */
1.79      schwarze  100:        "missing manual title, using UNTITLED",
                    101:        "missing manual title, using \"\"",
1.54      schwarze  102:        "lower case character in document title",
1.79      schwarze  103:        "missing manual section, using \"\"",
1.7       kristaps  104:        "unknown manual section",
1.54      schwarze  105:        "missing date, using today's date",
1.7       kristaps  106:        "cannot parse date, using it verbatim",
1.79      schwarze  107:        "missing Os macro, using \"\"",
                    108:        "duplicate prologue macro",
                    109:        "late prologue macro",
                    110:        "skipping late title macro",
1.7       kristaps  111:        "prologue macros out of order",
                    112:
                    113:        /* related to document structure */
                    114:        ".so is fragile, better use ln(1)",
1.50      schwarze  115:        "no document body",
1.54      schwarze  116:        "content before first section header",
                    117:        "first section is not \"NAME\"",
1.156     schwarze  118:        "NAME section without Nm before Nd",
1.128     schwarze  119:        "NAME section without description",
                    120:        "description not at the end of NAME",
                    121:        "bad NAME section content",
1.156     schwarze  122:        "missing comma before name",
1.121     schwarze  123:        "missing description line, using \"\"",
1.162     schwarze  124:        "description line outside NAME section",
1.7       kristaps  125:        "sections out of conventional order",
1.54      schwarze  126:        "duplicate section title",
                    127:        "unexpected section",
1.87      schwarze  128:        "unusual Xr order",
                    129:        "unusual Xr punctuation",
1.86      schwarze  130:        "AUTHORS section without An macro",
1.7       kristaps  131:
                    132:        /* related to macros and nesting */
1.55      schwarze  133:        "obsolete macro",
1.102     schwarze  134:        "macro neither callable nor escaped",
1.7       kristaps  135:        "skipping paragraph macro",
1.31      schwarze  136:        "moving paragraph macro out of list",
1.7       kristaps  137:        "skipping no-space macro",
                    138:        "blocks badly nested",
                    139:        "nested displays are not portable",
1.57      schwarze  140:        "moving content out of list",
1.78      schwarze  141:        "fill mode already enabled, skipping",
                    142:        "fill mode already disabled, skipping",
1.7       kristaps  143:        "line scope broken",
1.169     schwarze  144:        "skipping blank line in line scope",
1.7       kristaps  145:
                    146:        /* related to missing macro arguments */
1.58      schwarze  147:        "skipping empty request",
                    148:        "conditional request controls empty scope",
1.7       kristaps  149:        "skipping empty macro",
1.124     schwarze  150:        "empty block",
1.62      schwarze  151:        "empty argument, using 0n",
1.60      schwarze  152:        "missing display type, using -ragged",
                    153:        "list type is not the first argument",
1.152     schwarze  154:        "missing -width in -tag list, using 6n",
1.78      schwarze  155:        "missing utility name, using \"\"",
1.123     schwarze  156:        "missing function name, using \"\"",
1.60      schwarze  157:        "empty head in list item",
                    158:        "empty list item",
1.61      schwarze  159:        "missing font type, using \\fR",
                    160:        "unknown font type, using \\fR",
1.103     schwarze  161:        "nothing follows prefix",
1.122     schwarze  162:        "empty reference block",
1.155     schwarze  163:        "missing section argument",
1.60      schwarze  164:        "missing -std argument, adding it",
1.125     schwarze  165:        "missing option string, using \"\"",
                    166:        "missing resource identifier, using \"\"",
1.90      schwarze  167:        "missing eqn box, using \"\"",
1.7       kristaps  168:
                    169:        /* related to bad macro arguments */
1.64      schwarze  170:        "unterminated quoted argument",
1.7       kristaps  171:        "duplicate argument",
1.76      schwarze  172:        "skipping duplicate argument",
1.63      schwarze  173:        "skipping duplicate display type",
                    174:        "skipping duplicate list type",
1.76      schwarze  175:        "skipping -width argument",
1.126     schwarze  176:        "wrong number of cells",
1.7       kristaps  177:        "unknown AT&T UNIX version",
1.88      schwarze  178:        "comma in function argument",
1.89      schwarze  179:        "parenthesis in function name",
1.67      schwarze  180:        "invalid content in Rs block",
1.63      schwarze  181:        "invalid Boolean argument",
                    182:        "unknown font, skipping request",
1.126     schwarze  183:        "odd number of characters in request",
1.7       kristaps  184:
                    185:        /* related to plain text */
1.64      schwarze  186:        "blank line in fill mode, using .sp",
                    187:        "tab in filled text",
                    188:        "whitespace at end of input line",
1.158     schwarze  189:        "new sentence, new line",
1.7       kristaps  190:        "bad comment style",
1.64      schwarze  191:        "invalid escape sequence",
                    192:        "undefined string, using \"\"",
1.16      kristaps  193:
1.118     schwarze  194:        /* related to tables */
                    195:        "tbl line starts with span",
                    196:        "tbl column starts with span",
                    197:        "skipping vertical bar in tbl layout",
                    198:
1.7       kristaps  199:        "generic error",
                    200:
                    201:        /* related to tables */
1.116     schwarze  202:        "non-alphabetic character in tbl options",
                    203:        "skipping unknown tbl option",
                    204:        "missing tbl option argument",
                    205:        "wrong tbl option argument size",
1.118     schwarze  206:        "empty tbl layout",
                    207:        "invalid character in tbl layout",
                    208:        "unmatched parenthesis in tbl layout",
1.119     schwarze  209:        "tbl without any data cells",
                    210:        "ignoring data in spanned tbl cell",
                    211:        "ignoring extra tbl data cells",
                    212:        "data block open at end of tbl",
1.7       kristaps  213:
1.68      schwarze  214:        /* related to document structure and macros */
1.108     schwarze  215:        NULL,
1.7       kristaps  216:        "input stack limit exceeded, infinite loop?",
                    217:        "skipping bad character",
1.68      schwarze  218:        "skipping unknown macro",
1.112     schwarze  219:        "skipping insecure request",
1.73      schwarze  220:        "skipping item outside list",
1.68      schwarze  221:        "skipping column outside column list",
                    222:        "skipping end of block that is not open",
1.115     schwarze  223:        "fewer RS blocks open, skipping",
1.68      schwarze  224:        "inserting missing end of block",
                    225:        "appending missing end of block",
                    226:
                    227:        /* related to request and macro arguments */
1.7       kristaps  228:        "escaped character not allowed in a name",
1.99      schwarze  229:        "NOT IMPLEMENTED: Bd -file",
1.145     schwarze  230:        "skipping display without arguments",
1.71      schwarze  231:        "missing list type, using -item",
1.171     schwarze  232:        "argument is not numeric, using 1",
1.70      schwarze  233:        "missing manual name, using \"\"",
1.71      schwarze  234:        "uname(3) system call failed, using UNKNOWN",
1.63      schwarze  235:        "unknown standard specifier",
1.71      schwarze  236:        "skipping request without numeric argument",
1.109     schwarze  237:        "NOT IMPLEMENTED: .so with absolute path or \"..\"",
1.110     schwarze  238:        ".so request failed",
1.61      schwarze  239:        "skipping all arguments",
                    240:        "skipping excess arguments",
1.92      kristaps  241:        "divide by zero",
1.112     schwarze  242:
                    243:        "unsupported feature",
                    244:        "input too large",
1.114     schwarze  245:        "unsupported control character",
1.112     schwarze  246:        "unsupported roff request",
1.119     schwarze  247:        "eqn delim option in tbl",
1.118     schwarze  248:        "unsupported tbl layout modifier",
1.112     schwarze  249:        "ignoring macro in table",
1.7       kristaps  250: };
                    251:
                    252: static const char * const      mandoclevels[MANDOCLEVEL_MAX] = {
                    253:        "SUCCESS",
1.165     schwarze  254:        "STYLE",
1.7       kristaps  255:        "WARNING",
                    256:        "ERROR",
1.112     schwarze  257:        "UNSUPP",
1.7       kristaps  258:        "BADARG",
                    259:        "SYSERR"
                    260: };
                    261:
1.47      schwarze  262:
1.1       kristaps  263: static void
                    264: resize_buf(struct buf *buf, size_t initial)
                    265: {
                    266:
                    267:        buf->sz = buf->sz > initial/2 ? 2 * buf->sz : initial;
                    268:        buf->buf = mandoc_realloc(buf->buf, buf->sz);
                    269: }
                    270:
                    271: static void
1.84      schwarze  272: choose_parser(struct mparse *curp)
1.1       kristaps  273: {
1.83      schwarze  274:        char            *cp, *ep;
                    275:        int              format;
1.1       kristaps  276:
1.83      schwarze  277:        /*
                    278:         * If neither command line arguments -mdoc or -man select
                    279:         * a parser nor the roff parser found a .Dd or .TH macro
                    280:         * yet, look ahead in the main input buffer.
                    281:         */
                    282:
                    283:        if ((format = roff_getformat(curp->roff)) == 0) {
                    284:                cp = curp->primary->buf;
                    285:                ep = cp + curp->primary->sz;
                    286:                while (cp < ep) {
1.85      schwarze  287:                        if (*cp == '.' || *cp == '\'') {
1.83      schwarze  288:                                cp++;
                    289:                                if (cp[0] == 'D' && cp[1] == 'd') {
                    290:                                        format = MPARSE_MDOC;
                    291:                                        break;
                    292:                                }
                    293:                                if (cp[0] == 'T' && cp[1] == 'H') {
                    294:                                        format = MPARSE_MAN;
                    295:                                        break;
                    296:                                }
                    297:                        }
                    298:                        cp = memchr(cp, '\n', ep - cp);
                    299:                        if (cp == NULL)
                    300:                                break;
                    301:                        cp++;
                    302:                }
1.1       kristaps  303:        }
                    304:
1.83      schwarze  305:        if (format == MPARSE_MDOC) {
1.137     schwarze  306:                curp->man->macroset = MACROSET_MDOC;
1.164     schwarze  307:                if (curp->man->mdocmac == NULL)
                    308:                        curp->man->mdocmac = roffhash_alloc(MDOC_Dd, MDOC_MAX);
1.137     schwarze  309:        } else {
                    310:                curp->man->macroset = MACROSET_MAN;
1.164     schwarze  311:                if (curp->man->manmac == NULL)
                    312:                        curp->man->manmac = roffhash_alloc(MAN_TH, MAN_MAX);
1.47      schwarze  313:        }
1.164     schwarze  314:        curp->man->first->tok = TOKEN_NONE;
1.1       kristaps  315: }
                    316:
                    317: /*
1.95      schwarze  318:  * Main parse routine for a buffer.
                    319:  * It assumes encoding and line numbering are already set up.
                    320:  * It can recurse directly (for invocations of user-defined
                    321:  * macros, inline equations, and input line traps)
                    322:  * and indirectly (for .so file inclusion).
1.1       kristaps  323:  */
1.163     schwarze  324: static int
1.95      schwarze  325: mparse_buf_r(struct mparse *curp, struct buf blk, size_t i, int start)
1.1       kristaps  326: {
                    327:        const struct tbl_span   *span;
                    328:        struct buf       ln;
1.113     schwarze  329:        const char      *save_file;
1.110     schwarze  330:        char            *cp;
1.95      schwarze  331:        size_t           pos; /* byte number in the ln buffer */
1.1       kristaps  332:        enum rofferr     rr;
1.100     schwarze  333:        int              of;
1.1       kristaps  334:        int              lnn; /* line number in the real file */
1.110     schwarze  335:        int              fd;
1.1       kristaps  336:        unsigned char    c;
                    337:
1.95      schwarze  338:        memset(&ln, 0, sizeof(ln));
1.1       kristaps  339:
1.47      schwarze  340:        lnn = curp->line;
                    341:        pos = 0;
1.1       kristaps  342:
1.95      schwarze  343:        while (i < blk.sz) {
1.1       kristaps  344:                if (0 == pos && '\0' == blk.buf[i])
                    345:                        break;
                    346:
                    347:                if (start) {
                    348:                        curp->line = lnn;
                    349:                        curp->reparse_count = 0;
1.93      schwarze  350:
                    351:                        if (lnn < 3 &&
                    352:                            curp->filenc & MPARSE_UTF8 &&
1.95      schwarze  353:                            curp->filenc & MPARSE_LATIN1)
                    354:                                curp->filenc = preconv_cue(&blk, i);
1.1       kristaps  355:                }
                    356:
1.95      schwarze  357:                while (i < blk.sz && (start || blk.buf[i] != '\0')) {
1.1       kristaps  358:
                    359:                        /*
                    360:                         * When finding an unescaped newline character,
                    361:                         * leave the character loop to process the line.
                    362:                         * Skip a preceding carriage return, if any.
                    363:                         */
                    364:
1.95      schwarze  365:                        if ('\r' == blk.buf[i] && i + 1 < blk.sz &&
1.1       kristaps  366:                            '\n' == blk.buf[i + 1])
                    367:                                ++i;
                    368:                        if ('\n' == blk.buf[i]) {
                    369:                                ++i;
                    370:                                ++lnn;
                    371:                                break;
                    372:                        }
                    373:
1.35      schwarze  374:                        /*
1.93      schwarze  375:                         * Make sure we have space for the worst
                    376:                         * case of 11 bytes: "\\[u10ffff]\0"
1.35      schwarze  377:                         */
                    378:
1.95      schwarze  379:                        if (pos + 11 > ln.sz)
1.35      schwarze  380:                                resize_buf(&ln, 256);
                    381:
1.47      schwarze  382:                        /*
1.93      schwarze  383:                         * Encode 8-bit input.
1.1       kristaps  384:                         */
                    385:
1.93      schwarze  386:                        c = blk.buf[i];
                    387:                        if (c & 0x80) {
1.95      schwarze  388:                                if ( ! (curp->filenc && preconv_encode(
                    389:                                    &blk, &i, &ln, &pos, &curp->filenc))) {
1.114     schwarze  390:                                        mandoc_vmsg(MANDOCERR_CHAR_BAD, curp,
                    391:                                            curp->line, pos, "0x%x", c);
1.93      schwarze  392:                                        ln.buf[pos++] = '?';
                    393:                                        i++;
                    394:                                }
                    395:                                continue;
                    396:                        }
                    397:
                    398:                        /*
                    399:                         * Exclude control characters.
                    400:                         */
1.1       kristaps  401:
1.93      schwarze  402:                        if (c == 0x7f || (c < 0x20 && c != 0x09)) {
1.114     schwarze  403:                                mandoc_vmsg(c == 0x00 || c == 0x04 ||
                    404:                                    c > 0x0a ? MANDOCERR_CHAR_BAD :
                    405:                                    MANDOCERR_CHAR_UNSUPP,
                    406:                                    curp, curp->line, pos, "0x%x", c);
1.1       kristaps  407:                                i++;
1.127     schwarze  408:                                if (c != '\r')
                    409:                                        ln.buf[pos++] = '?';
1.1       kristaps  410:                                continue;
                    411:                        }
                    412:
                    413:                        ln.buf[pos++] = blk.buf[i++];
                    414:                }
                    415:
1.170     schwarze  416:                if (pos + 1 >= ln.sz)
1.1       kristaps  417:                        resize_buf(&ln, 256);
                    418:
1.170     schwarze  419:                if (i == blk.sz || blk.buf[i] == '\0')
                    420:                        ln.buf[pos++] = '\n';
1.1       kristaps  421:                ln.buf[pos] = '\0';
                    422:
                    423:                /*
                    424:                 * A significant amount of complexity is contained by
                    425:                 * the roff preprocessor.  It's line-oriented but can be
                    426:                 * expressed on one line, so we need at times to
                    427:                 * readjust our starting point and re-run it.  The roff
                    428:                 * preprocessor can also readjust the buffers with new
                    429:                 * data, so we pass them in wholesale.
                    430:                 */
                    431:
                    432:                of = 0;
                    433:
1.24      kristaps  434:                /*
                    435:                 * Maintain a lookaside buffer of all parsed lines.  We
                    436:                 * only do this if mparse_keep() has been invoked (the
                    437:                 * buffer may be accessed with mparse_getkeep()).
                    438:                 */
                    439:
                    440:                if (curp->secondary) {
1.47      schwarze  441:                        curp->secondary->buf = mandoc_realloc(
                    442:                            curp->secondary->buf,
                    443:                            curp->secondary->sz + pos + 2);
                    444:                        memcpy(curp->secondary->buf +
                    445:                            curp->secondary->sz,
                    446:                            ln.buf, pos);
1.24      kristaps  447:                        curp->secondary->sz += pos;
                    448:                        curp->secondary->buf
                    449:                                [curp->secondary->sz] = '\n';
                    450:                        curp->secondary->sz++;
                    451:                        curp->secondary->buf
                    452:                                [curp->secondary->sz] = '\0';
                    453:                }
1.1       kristaps  454: rerun:
1.96      schwarze  455:                rr = roff_parseln(curp->roff, curp->line, &ln, &of);
1.1       kristaps  456:
                    457:                switch (rr) {
1.47      schwarze  458:                case ROFF_REPARSE:
1.163     schwarze  459:                        if (++curp->reparse_count > REPARSE_LIMIT)
1.3       kristaps  460:                                mandoc_msg(MANDOCERR_ROFFLOOP, curp,
1.47      schwarze  461:                                    curp->line, pos, NULL);
1.163     schwarze  462:                        else if (mparse_buf_r(curp, ln, of, 0) == 1 ||
                    463:                            start == 1) {
                    464:                                pos = 0;
                    465:                                continue;
                    466:                        }
                    467:                        free(ln.buf);
                    468:                        return 0;
1.47      schwarze  469:                case ROFF_APPEND:
1.95      schwarze  470:                        pos = strlen(ln.buf);
1.1       kristaps  471:                        continue;
1.47      schwarze  472:                case ROFF_RERUN:
1.1       kristaps  473:                        goto rerun;
1.47      schwarze  474:                case ROFF_IGN:
1.1       kristaps  475:                        pos = 0;
                    476:                        continue;
1.47      schwarze  477:                case ROFF_SO:
1.95      schwarze  478:                        if ( ! (curp->options & MPARSE_SO) &&
                    479:                            (i >= blk.sz || blk.buf[i] == '\0')) {
1.45      schwarze  480:                                curp->sodest = mandoc_strdup(ln.buf + of);
                    481:                                free(ln.buf);
1.163     schwarze  482:                                return 1;
1.45      schwarze  483:                        }
1.24      kristaps  484:                        /*
                    485:                         * We remove `so' clauses from our lookaside
                    486:                         * buffer because we're going to descend into
                    487:                         * the file recursively.
                    488:                         */
1.47      schwarze  489:                        if (curp->secondary)
1.25      kristaps  490:                                curp->secondary->sz -= pos + 1;
1.113     schwarze  491:                        save_file = curp->file;
1.148     schwarze  492:                        if ((fd = mparse_open(curp, ln.buf + of)) != -1) {
1.110     schwarze  493:                                mparse_readfd(curp, fd, ln.buf + of);
1.147     schwarze  494:                                close(fd);
1.113     schwarze  495:                                curp->file = save_file;
                    496:                        } else {
                    497:                                curp->file = save_file;
1.52      schwarze  498:                                mandoc_vmsg(MANDOCERR_SO_FAIL,
                    499:                                    curp, curp->line, pos,
                    500:                                    ".so %s", ln.buf + of);
1.110     schwarze  501:                                ln.sz = mandoc_asprintf(&cp,
                    502:                                    ".sp\nSee the file %s.\n.sp",
                    503:                                    ln.buf + of);
                    504:                                free(ln.buf);
                    505:                                ln.buf = cp;
                    506:                                of = 0;
                    507:                                mparse_buf_r(curp, ln, of, 0);
1.52      schwarze  508:                        }
1.1       kristaps  509:                        pos = 0;
                    510:                        continue;
                    511:                default:
                    512:                        break;
                    513:                }
                    514:
1.150     schwarze  515:                if (curp->man->macroset == MACROSET_NONE)
1.84      schwarze  516:                        choose_parser(curp);
1.1       kristaps  517:
1.47      schwarze  518:                /*
1.84      schwarze  519:                 * Lastly, push down into the parsers themselves.
1.1       kristaps  520:                 * If libroff returns ROFF_TBL, then add it to the
                    521:                 * currently open parse.  Since we only get here if
                    522:                 * there does exist data (see tbl_data.c), we're
                    523:                 * guaranteed that something's been allocated.
                    524:                 * Do the same for ROFF_EQN.
                    525:                 */
                    526:
1.139     schwarze  527:                if (rr == ROFF_TBL)
1.100     schwarze  528:                        while ((span = roff_span(curp->roff)) != NULL)
1.139     schwarze  529:                                roff_addtbl(curp->man, span);
                    530:                else if (rr == ROFF_EQN)
                    531:                        roff_addeqn(curp->man, roff_eqn(curp->roff));
                    532:                else if ((curp->man->macroset == MACROSET_MDOC ?
1.135     schwarze  533:                    mdoc_parseln(curp->man, curp->line, ln.buf, of) :
1.100     schwarze  534:                    man_parseln(curp->man, curp->line, ln.buf, of)) == 2)
                    535:                                break;
1.1       kristaps  536:
                    537:                /* Temporary buffers typically are not full. */
                    538:
                    539:                if (0 == start && '\0' == blk.buf[i])
                    540:                        break;
                    541:
                    542:                /* Start the next input line. */
                    543:
                    544:                pos = 0;
                    545:        }
                    546:
                    547:        free(ln.buf);
1.163     schwarze  548:        return 1;
1.1       kristaps  549: }
                    550:
                    551: static int
1.40      schwarze  552: read_whole_file(struct mparse *curp, const char *file, int fd,
                    553:                struct buf *fb, int *with_mmap)
1.1       kristaps  554: {
1.161     schwarze  555:        struct stat      st;
1.140     schwarze  556:        gzFile           gz;
1.1       kristaps  557:        size_t           off;
                    558:        ssize_t          ssz;
1.143     schwarze  559:
                    560:        if (fstat(fd, &st) == -1)
                    561:                err((int)MANDOCLEVEL_SYSERR, "%s", file);
1.1       kristaps  562:
                    563:        /*
                    564:         * If we're a regular file, try just reading in the whole entry
                    565:         * via mmap().  This is faster than reading it into blocks, and
                    566:         * since each file is only a few bytes to begin with, I'm not
                    567:         * concerned that this is going to tank any machines.
                    568:         */
                    569:
1.140     schwarze  570:        if (curp->gzip == 0 && S_ISREG(st.st_mode)) {
1.131     schwarze  571:                if (st.st_size > 0x7fffffff) {
1.111     schwarze  572:                        mandoc_msg(MANDOCERR_TOOLARGE, curp, 0, 0, NULL);
1.142     schwarze  573:                        return 0;
1.1       kristaps  574:                }
                    575:                *with_mmap = 1;
                    576:                fb->sz = (size_t)st.st_size;
1.37      schwarze  577:                fb->buf = mmap(NULL, fb->sz, PROT_READ, MAP_SHARED, fd, 0);
1.1       kristaps  578:                if (fb->buf != MAP_FAILED)
1.142     schwarze  579:                        return 1;
1.1       kristaps  580:        }
                    581:
1.140     schwarze  582:        if (curp->gzip) {
1.143     schwarze  583:                if ((gz = gzdopen(fd, "rb")) == NULL)
                    584:                        err((int)MANDOCLEVEL_SYSERR, "%s", file);
1.140     schwarze  585:        } else
                    586:                gz = NULL;
                    587:
1.1       kristaps  588:        /*
                    589:         * If this isn't a regular file (like, say, stdin), then we must
                    590:         * go the old way and just read things in bit by bit.
                    591:         */
                    592:
                    593:        *with_mmap = 0;
                    594:        off = 0;
                    595:        fb->sz = 0;
                    596:        fb->buf = NULL;
                    597:        for (;;) {
                    598:                if (off == fb->sz) {
                    599:                        if (fb->sz == (1U << 31)) {
1.111     schwarze  600:                                mandoc_msg(MANDOCERR_TOOLARGE, curp,
                    601:                                    0, 0, NULL);
1.1       kristaps  602:                                break;
                    603:                        }
                    604:                        resize_buf(fb, 65536);
                    605:                }
1.140     schwarze  606:                ssz = curp->gzip ?
                    607:                    gzread(gz, fb->buf + (int)off, fb->sz - off) :
                    608:                    read(fd, fb->buf + (int)off, fb->sz - off);
1.1       kristaps  609:                if (ssz == 0) {
                    610:                        fb->sz = off;
1.142     schwarze  611:                        return 1;
1.1       kristaps  612:                }
1.143     schwarze  613:                if (ssz == -1)
                    614:                        err((int)MANDOCLEVEL_SYSERR, "%s", file);
1.1       kristaps  615:                off += (size_t)ssz;
                    616:        }
                    617:
                    618:        free(fb->buf);
                    619:        fb->buf = NULL;
1.142     schwarze  620:        return 0;
1.1       kristaps  621: }
                    622:
                    623: static void
                    624: mparse_end(struct mparse *curp)
                    625: {
1.135     schwarze  626:        if (curp->man->macroset == MACROSET_NONE)
                    627:                curp->man->macroset = MACROSET_MAN;
                    628:        if (curp->man->macroset == MACROSET_MDOC)
                    629:                mdoc_endparse(curp->man);
                    630:        else
1.111     schwarze  631:                man_endparse(curp->man);
1.1       kristaps  632:        roff_endparse(curp->roff);
                    633: }
                    634:
                    635: static void
1.36      schwarze  636: mparse_parse_buffer(struct mparse *curp, struct buf blk, const char *file)
1.28      joerg     637: {
1.85      schwarze  638:        struct buf      *svprimary;
1.28      joerg     639:        const char      *svfile;
1.95      schwarze  640:        size_t           offset;
1.36      schwarze  641:        static int       recursion_depth;
                    642:
                    643:        if (64 < recursion_depth) {
                    644:                mandoc_msg(MANDOCERR_ROFFLOOP, curp, curp->line, 0, NULL);
                    645:                return;
                    646:        }
1.28      joerg     647:
                    648:        /* Line number is per-file. */
                    649:        svfile = curp->file;
                    650:        curp->file = file;
1.85      schwarze  651:        svprimary = curp->primary;
1.83      schwarze  652:        curp->primary = &blk;
1.28      joerg     653:        curp->line = 1;
1.36      schwarze  654:        recursion_depth++;
1.28      joerg     655:
1.93      schwarze  656:        /* Skip an UTF-8 byte order mark. */
                    657:        if (curp->filenc & MPARSE_UTF8 && blk.sz > 2 &&
                    658:            (unsigned char)blk.buf[0] == 0xef &&
                    659:            (unsigned char)blk.buf[1] == 0xbb &&
                    660:            (unsigned char)blk.buf[2] == 0xbf) {
1.95      schwarze  661:                offset = 3;
1.93      schwarze  662:                curp->filenc &= ~MPARSE_LATIN1;
1.95      schwarze  663:        } else
                    664:                offset = 0;
1.93      schwarze  665:
1.95      schwarze  666:        mparse_buf_r(curp, blk, offset, 1);
1.28      joerg     667:
1.111     schwarze  668:        if (--recursion_depth == 0)
1.28      joerg     669:                mparse_end(curp);
                    670:
1.85      schwarze  671:        curp->primary = svprimary;
1.28      joerg     672:        curp->file = svfile;
                    673: }
                    674:
                    675: enum mandoclevel
1.104     schwarze  676: mparse_readmem(struct mparse *curp, void *buf, size_t len,
1.28      joerg     677:                const char *file)
                    678: {
                    679:        struct buf blk;
                    680:
1.104     schwarze  681:        blk.buf = buf;
1.28      joerg     682:        blk.sz = len;
                    683:
1.36      schwarze  684:        mparse_parse_buffer(curp, blk, file);
1.142     schwarze  685:        return curp->file_status;
1.28      joerg     686: }
                    687:
1.98      schwarze  688: /*
                    689:  * Read the whole file into memory and call the parsers.
                    690:  * Called recursively when an .so request is encountered.
                    691:  */
1.36      schwarze  692: enum mandoclevel
                    693: mparse_readfd(struct mparse *curp, int fd, const char *file)
1.1       kristaps  694: {
1.28      joerg     695:        struct buf       blk;
                    696:        int              with_mmap;
1.93      schwarze  697:        int              save_filenc;
1.1       kristaps  698:
1.91      schwarze  699:        if (read_whole_file(curp, file, fd, &blk, &with_mmap)) {
1.93      schwarze  700:                save_filenc = curp->filenc;
                    701:                curp->filenc = curp->options &
                    702:                    (MPARSE_UTF8 | MPARSE_LATIN1);
1.91      schwarze  703:                mparse_parse_buffer(curp, blk, file);
1.93      schwarze  704:                curp->filenc = save_filenc;
1.91      schwarze  705:                if (with_mmap)
                    706:                        munmap(blk.buf, blk.sz);
                    707:                else
                    708:                        free(blk.buf);
                    709:        }
1.142     schwarze  710:        return curp->file_status;
1.82      schwarze  711: }
                    712:
1.148     schwarze  713: int
                    714: mparse_open(struct mparse *curp, const char *file)
1.82      schwarze  715: {
                    716:        char             *cp;
1.148     schwarze  717:        int               fd;
1.82      schwarze  718:
                    719:        curp->file = file;
1.140     schwarze  720:        cp = strrchr(file, '.');
                    721:        curp->gzip = (cp != NULL && ! strcmp(cp + 1, "gz"));
1.98      schwarze  722:
1.140     schwarze  723:        /* First try to use the filename as it is. */
1.98      schwarze  724:
1.148     schwarze  725:        if ((fd = open(file, O_RDONLY)) != -1)
                    726:                return fd;
1.98      schwarze  727:
1.140     schwarze  728:        /*
                    729:         * If that doesn't work and the filename doesn't
                    730:         * already  end in .gz, try appending .gz.
                    731:         */
1.98      schwarze  732:
1.140     schwarze  733:        if ( ! curp->gzip) {
1.98      schwarze  734:                mandoc_asprintf(&cp, "%s.gz", file);
1.149     schwarze  735:                fd = open(cp, O_RDONLY);
1.108     schwarze  736:                free(cp);
1.148     schwarze  737:                if (fd != -1) {
1.140     schwarze  738:                        curp->gzip = 1;
1.148     schwarze  739:                        return fd;
1.82      schwarze  740:                }
                    741:        }
                    742:
1.140     schwarze  743:        /* Neither worked, give up. */
1.97      schwarze  744:
1.140     schwarze  745:        mandoc_msg(MANDOCERR_FILE, curp, 0, 0, strerror(errno));
1.148     schwarze  746:        return -1;
1.1       kristaps  747: }
                    748:
                    749: struct mparse *
1.94      schwarze  750: mparse_alloc(int options, enum mandoclevel wlevel, mandocmsg mmsg,
1.144     schwarze  751:     const char *defos)
1.1       kristaps  752: {
                    753:        struct mparse   *curp;
1.10      kristaps  754:
1.1       kristaps  755:        curp = mandoc_calloc(1, sizeof(struct mparse));
                    756:
1.44      schwarze  757:        curp->options = options;
1.3       kristaps  758:        curp->wlevel = wlevel;
1.1       kristaps  759:        curp->mmsg = mmsg;
1.29      schwarze  760:        curp->defos = defos;
1.1       kristaps  761:
1.144     schwarze  762:        curp->roff = roff_alloc(curp, options);
1.137     schwarze  763:        curp->man = roff_man_alloc( curp->roff, curp, curp->defos,
                    764:                curp->options & MPARSE_QUICK ? 1 : 0);
1.136     schwarze  765:        if (curp->options & MPARSE_MDOC) {
1.137     schwarze  766:                curp->man->macroset = MACROSET_MDOC;
1.164     schwarze  767:                if (curp->man->mdocmac == NULL)
                    768:                        curp->man->mdocmac = roffhash_alloc(MDOC_Dd, MDOC_MAX);
1.137     schwarze  769:        } else if (curp->options & MPARSE_MAN) {
                    770:                curp->man->macroset = MACROSET_MAN;
1.164     schwarze  771:                if (curp->man->manmac == NULL)
                    772:                        curp->man->manmac = roffhash_alloc(MAN_TH, MAN_MAX);
1.136     schwarze  773:        }
1.138     schwarze  774:        curp->man->first->tok = TOKEN_NONE;
1.142     schwarze  775:        return curp;
1.1       kristaps  776: }
                    777:
                    778: void
                    779: mparse_reset(struct mparse *curp)
                    780: {
                    781:        roff_reset(curp->roff);
1.150     schwarze  782:        roff_man_reset(curp->man);
1.160     schwarze  783:
                    784:        free(curp->sodest);
                    785:        curp->sodest = NULL;
                    786:
1.24      kristaps  787:        if (curp->secondary)
                    788:                curp->secondary->sz = 0;
1.1       kristaps  789:
                    790:        curp->file_status = MANDOCLEVEL_OK;
1.159     schwarze  791:        curp->gzip = 0;
1.1       kristaps  792: }
                    793:
                    794: void
                    795: mparse_free(struct mparse *curp)
                    796: {
                    797:
1.164     schwarze  798:        roffhash_free(curp->man->mdocmac);
                    799:        roffhash_free(curp->man->manmac);
1.137     schwarze  800:        roff_man_free(curp->man);
1.160     schwarze  801:        roff_free(curp->roff);
1.24      kristaps  802:        if (curp->secondary)
                    803:                free(curp->secondary->buf);
1.1       kristaps  804:
1.24      kristaps  805:        free(curp->secondary);
1.45      schwarze  806:        free(curp->sodest);
1.1       kristaps  807:        free(curp);
                    808: }
                    809:
                    810: void
1.135     schwarze  811: mparse_result(struct mparse *curp, struct roff_man **man,
                    812:        char **sodest)
1.1       kristaps  813: {
                    814:
1.45      schwarze  815:        if (sodest && NULL != (*sodest = curp->sodest)) {
                    816:                *man = NULL;
                    817:                return;
                    818:        }
1.9       kristaps  819:        if (man)
                    820:                *man = curp->man;
1.157     schwarze  821: }
                    822:
                    823: void
                    824: mparse_updaterc(struct mparse *curp, enum mandoclevel *rc)
                    825: {
                    826:        if (curp->file_status > *rc)
                    827:                *rc = curp->file_status;
1.3       kristaps  828: }
                    829:
                    830: void
                    831: mandoc_vmsg(enum mandocerr t, struct mparse *m,
                    832:                int ln, int pos, const char *fmt, ...)
                    833: {
                    834:        char             buf[256];
                    835:        va_list          ap;
                    836:
                    837:        va_start(ap, fmt);
1.48      schwarze  838:        (void)vsnprintf(buf, sizeof(buf), fmt, ap);
1.3       kristaps  839:        va_end(ap);
                    840:
                    841:        mandoc_msg(t, m, ln, pos, buf);
                    842: }
                    843:
                    844: void
1.47      schwarze  845: mandoc_msg(enum mandocerr er, struct mparse *m,
1.3       kristaps  846:                int ln, int col, const char *msg)
                    847: {
                    848:        enum mandoclevel level;
                    849:
1.112     schwarze  850:        level = MANDOCLEVEL_UNSUPP;
1.3       kristaps  851:        while (er < mandoclimits[level])
                    852:                level--;
                    853:
1.108     schwarze  854:        if (level < m->wlevel && er != MANDOCERR_FILE)
1.3       kristaps  855:                return;
                    856:
1.8       kristaps  857:        if (m->mmsg)
                    858:                (*m->mmsg)(er, level, m->file, ln, col, msg);
1.3       kristaps  859:
                    860:        if (m->file_status < level)
                    861:                m->file_status = level;
1.7       kristaps  862: }
                    863:
                    864: const char *
                    865: mparse_strerror(enum mandocerr er)
                    866: {
                    867:
1.142     schwarze  868:        return mandocerrs[er];
1.7       kristaps  869: }
                    870:
                    871: const char *
                    872: mparse_strlevel(enum mandoclevel lvl)
                    873: {
1.142     schwarze  874:        return mandoclevels[lvl];
1.24      kristaps  875: }
                    876:
                    877: void
                    878: mparse_keep(struct mparse *p)
                    879: {
                    880:
                    881:        assert(NULL == p->secondary);
                    882:        p->secondary = mandoc_calloc(1, sizeof(struct buf));
                    883: }
                    884:
                    885: const char *
                    886: mparse_getkeep(const struct mparse *p)
                    887: {
                    888:
                    889:        assert(p->secondary);
1.142     schwarze  890:        return p->secondary->sz ? p->secondary->buf : NULL;
1.1       kristaps  891: }

CVSweb