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

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