[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.145

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