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

Annotation of mandoc/read.c, Revision 1.197

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

CVSweb