[BACK]Return to read.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/read.c, Revision 1.146

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