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

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