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

1.28    ! schwarze    1: /*     $Id: read.c,v 1.27 2014/06/20 17:23:09 schwarze Exp $ */
1.1       schwarze    2: /*
                      3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.18      schwarze    4:  * Copyright (c) 2010-2014 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:  *
                     11:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     12:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     13:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     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:  */
                     19: #include <sys/stat.h>
                     20: #include <sys/mman.h>
                     21:
                     22: #include <assert.h>
                     23: #include <ctype.h>
1.18      schwarze   24: #include <errno.h>
1.1       schwarze   25: #include <fcntl.h>
                     26: #include <stdarg.h>
                     27: #include <stdio.h>
                     28: #include <stdlib.h>
                     29: #include <string.h>
                     30: #include <unistd.h>
                     31:
                     32: #include "mandoc.h"
1.24      schwarze   33: #include "mandoc_aux.h"
1.1       schwarze   34: #include "libmandoc.h"
                     35: #include "mdoc.h"
                     36: #include "man.h"
                     37:
                     38: #define        REPARSE_LIMIT   1000
                     39:
                     40: struct buf {
1.25      schwarze   41:        char             *buf; /* binary input buffer */
1.1       schwarze   42:        size_t            sz; /* size of binary buffer */
                     43: };
                     44:
                     45: struct mparse {
                     46:        enum mandoclevel  file_status; /* status of current parse */
                     47:        enum mandoclevel  wlevel; /* ignore messages below this */
                     48:        int               line; /* line number in the file */
1.22      schwarze   49:        int               options; /* parser options */
1.1       schwarze   50:        struct man       *pman; /* persistent man parser */
                     51:        struct mdoc      *pmdoc; /* persistent mdoc parser */
                     52:        struct man       *man; /* man parser */
                     53:        struct mdoc      *mdoc; /* mdoc parser */
                     54:        struct roff      *roff; /* roff parser (!NULL) */
1.23      schwarze   55:        char             *sodest; /* filename pointed to by .so */
1.1       schwarze   56:        int               reparse_count; /* finite interp. stack */
                     57:        mandocmsg         mmsg; /* warning/error message handler */
1.25      schwarze   58:        const char       *file;
1.4       schwarze   59:        struct buf       *secondary;
1.7       schwarze   60:        char             *defos; /* default operating system */
1.1       schwarze   61: };
                     62:
                     63: static void      resize_buf(struct buf *, size_t);
                     64: static void      mparse_buf_r(struct mparse *, struct buf, int);
                     65: static void      pset(const char *, int, struct mparse *);
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,
                     77:        MANDOCERR_FATAL,
                     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.27      schwarze   88:        "no TH macro in document",
1.1       schwarze   89:        "document title should be all caps",
                     90:        "unknown manual section",
1.11      schwarze   91:        "unknown manual volume or arch",
1.1       schwarze   92:        "date missing, using today's date",
                     93:        "cannot parse date, using it verbatim",
                     94:        "prologue macros out of order",
                     95:        "duplicate prologue macro",
                     96:        "macro not allowed in prologue",
                     97:        "macro not allowed in body",
                     98:
                     99:        /* related to document structure */
                    100:        ".so is fragile, better use ln(1)",
1.28    ! schwarze  101:        "no document body",
        !           102:        "content before the first section header",
1.1       schwarze  103:        "NAME section must come first",
                    104:        "bad NAME section contents",
                    105:        "sections out of conventional order",
                    106:        "duplicate section name",
1.17      schwarze  107:        "section header suited to sections 2, 3, and 9 only",
1.1       schwarze  108:
                    109:        /* related to macros and nesting */
                    110:        "skipping obsolete macro",
                    111:        "skipping paragraph macro",
1.10      schwarze  112:        "moving paragraph macro out of list",
1.1       schwarze  113:        "skipping no-space macro",
                    114:        "blocks badly nested",
                    115:        "child violates parent syntax",
                    116:        "nested displays are not portable",
                    117:        "already in literal mode",
                    118:        "line scope broken",
                    119:
                    120:        /* related to missing macro arguments */
                    121:        "skipping empty macro",
                    122:        "argument count wrong",
                    123:        "missing display type",
                    124:        "list type must come first",
                    125:        "tag lists require a width argument",
                    126:        "missing font type",
                    127:        "skipping end of block that is not open",
                    128:
                    129:        /* related to bad macro arguments */
                    130:        "skipping argument",
                    131:        "duplicate argument",
                    132:        "duplicate display type",
                    133:        "duplicate list type",
                    134:        "unknown AT&T UNIX version",
                    135:        "bad Boolean value",
                    136:        "unknown font",
                    137:        "unknown standard specifier",
                    138:        "bad width argument",
                    139:
                    140:        /* related to plain text */
                    141:        "blank line in non-literal context",
                    142:        "tab in non-literal context",
                    143:        "end of line whitespace",
                    144:        "bad comment style",
1.2       schwarze  145:        "bad escape sequence",
1.1       schwarze  146:        "unterminated quoted string",
1.3       schwarze  147:
                    148:        /* related to equations */
                    149:        "unexpected literal in equation",
1.25      schwarze  150:
1.1       schwarze  151:        "generic error",
                    152:
1.3       schwarze  153:        /* related to equations */
                    154:        "unexpected equation scope closure",
                    155:        "equation scope open on exit",
                    156:        "overlapping equation scopes",
                    157:        "unexpected end of equation",
                    158:        "equation syntax error",
                    159:
1.1       schwarze  160:        /* related to tables */
                    161:        "bad table syntax",
                    162:        "bad table option",
                    163:        "bad table layout",
                    164:        "no table layout cells specified",
                    165:        "no table data cells specified",
                    166:        "ignore data in cell",
                    167:        "data block still open",
                    168:        "ignoring extra data cells",
                    169:
                    170:        "input stack limit exceeded, infinite loop?",
                    171:        "skipping bad character",
                    172:        "escaped character not allowed in a name",
1.9       schwarze  173:        "manual name not yet set",
1.1       schwarze  174:        "skipping text before the first section header",
                    175:        "skipping unknown macro",
                    176:        "NOT IMPLEMENTED, please use groff: skipping request",
                    177:        "argument count wrong",
1.12      schwarze  178:        "skipping column outside column list",
1.1       schwarze  179:        "skipping end of block that is not open",
                    180:        "missing end of block",
                    181:        "scope open on exit",
                    182:        "uname(3) system call failed",
                    183:        "macro requires line argument(s)",
                    184:        "macro requires body argument(s)",
                    185:        "macro requires argument(s)",
1.16      schwarze  186:        "request requires a numeric argument",
1.1       schwarze  187:        "missing list type",
                    188:        "line argument(s) will be lost",
                    189:        "body argument(s) will be lost",
                    190:
                    191:        "generic fatal error",
                    192:
1.18      schwarze  193:        "input too large",
1.1       schwarze  194:        "not a manual",
                    195:        "column syntax is inconsistent",
                    196:        "NOT IMPLEMENTED: .Bd -file",
                    197:        "argument count wrong, violates syntax",
                    198:        "child violates parent syntax",
                    199:        "argument count wrong, violates syntax",
                    200:        "NOT IMPLEMENTED: .so with absolute path or \"..\"",
                    201:        "no document prologue",
                    202:        "static buffer exhausted",
1.18      schwarze  203:
                    204:        /* system errors */
                    205:        "cannot open file",
                    206:        "cannot stat file",
                    207:        "cannot read file",
1.1       schwarze  208: };
                    209:
                    210: static const char * const      mandoclevels[MANDOCLEVEL_MAX] = {
                    211:        "SUCCESS",
                    212:        "RESERVED",
                    213:        "WARNING",
                    214:        "ERROR",
                    215:        "FATAL",
                    216:        "BADARG",
                    217:        "SYSERR"
                    218: };
                    219:
1.25      schwarze  220:
1.1       schwarze  221: static void
                    222: resize_buf(struct buf *buf, size_t initial)
                    223: {
                    224:
                    225:        buf->sz = buf->sz > initial/2 ? 2 * buf->sz : initial;
                    226:        buf->buf = mandoc_realloc(buf->buf, buf->sz);
                    227: }
                    228:
                    229: static void
                    230: pset(const char *buf, int pos, struct mparse *curp)
                    231: {
                    232:        int              i;
                    233:
                    234:        /*
                    235:         * Try to intuit which kind of manual parser should be used.  If
                    236:         * passed in by command-line (-man, -mdoc), then use that
                    237:         * explicitly.  If passed as -mandoc, then try to guess from the
                    238:         * line: either skip dot-lines, use -mdoc when finding `.Dt', or
                    239:         * default to -man, which is more lenient.
                    240:         *
                    241:         * Separate out pmdoc/pman from mdoc/man: the first persists
                    242:         * through all parsers, while the latter is used per-parse.
                    243:         */
                    244:
                    245:        if ('.' == buf[0] || '\'' == buf[0]) {
                    246:                for (i = 1; buf[i]; i++)
                    247:                        if (' ' != buf[i] && '\t' != buf[i])
                    248:                                break;
                    249:                if ('\0' == buf[i])
                    250:                        return;
                    251:        }
                    252:
1.22      schwarze  253:        if (MPARSE_MDOC & curp->options) {
1.25      schwarze  254:                if (NULL == curp->pmdoc)
1.22      schwarze  255:                        curp->pmdoc = mdoc_alloc(
                    256:                            curp->roff, curp, curp->defos,
                    257:                            MPARSE_QUICK & curp->options ? 1 : 0);
1.1       schwarze  258:                assert(curp->pmdoc);
                    259:                curp->mdoc = curp->pmdoc;
                    260:                return;
1.22      schwarze  261:        } else if (MPARSE_MAN & curp->options) {
1.25      schwarze  262:                if (NULL == curp->pman)
1.19      schwarze  263:                        curp->pman = man_alloc(curp->roff, curp,
1.22      schwarze  264:                            MPARSE_QUICK & curp->options ? 1 : 0);
1.1       schwarze  265:                assert(curp->pman);
                    266:                curp->man = curp->pman;
                    267:                return;
                    268:        }
                    269:
                    270:        if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3))  {
1.25      schwarze  271:                if (NULL == curp->pmdoc)
1.22      schwarze  272:                        curp->pmdoc = mdoc_alloc(
                    273:                            curp->roff, curp, curp->defos,
                    274:                            MPARSE_QUICK & curp->options ? 1 : 0);
1.1       schwarze  275:                assert(curp->pmdoc);
                    276:                curp->mdoc = curp->pmdoc;
                    277:                return;
1.25      schwarze  278:        }
1.1       schwarze  279:
1.25      schwarze  280:        if (NULL == curp->pman)
1.22      schwarze  281:                curp->pman = man_alloc(curp->roff, curp,
                    282:                    MPARSE_QUICK & curp->options ? 1 : 0);
1.1       schwarze  283:        assert(curp->pman);
                    284:        curp->man = curp->pman;
                    285: }
                    286:
                    287: /*
                    288:  * Main parse routine for an opened file.  This is called for each
                    289:  * opened file and simply loops around the full input file, possibly
                    290:  * nesting (i.e., with `so').
                    291:  */
                    292: static void
                    293: mparse_buf_r(struct mparse *curp, struct buf blk, int start)
                    294: {
                    295:        const struct tbl_span   *span;
                    296:        struct buf       ln;
                    297:        enum rofferr     rr;
                    298:        int              i, of, rc;
                    299:        int              pos; /* byte number in the ln buffer */
                    300:        int              lnn; /* line number in the real file */
                    301:        unsigned char    c;
                    302:
                    303:        memset(&ln, 0, sizeof(struct buf));
                    304:
1.25      schwarze  305:        lnn = curp->line;
                    306:        pos = 0;
1.1       schwarze  307:
                    308:        for (i = 0; i < (int)blk.sz; ) {
                    309:                if (0 == pos && '\0' == blk.buf[i])
                    310:                        break;
                    311:
                    312:                if (start) {
                    313:                        curp->line = lnn;
                    314:                        curp->reparse_count = 0;
                    315:                }
                    316:
                    317:                while (i < (int)blk.sz && (start || '\0' != blk.buf[i])) {
                    318:
                    319:                        /*
                    320:                         * When finding an unescaped newline character,
                    321:                         * leave the character loop to process the line.
                    322:                         * Skip a preceding carriage return, if any.
                    323:                         */
                    324:
                    325:                        if ('\r' == blk.buf[i] && i + 1 < (int)blk.sz &&
                    326:                            '\n' == blk.buf[i + 1])
                    327:                                ++i;
                    328:                        if ('\n' == blk.buf[i]) {
                    329:                                ++i;
                    330:                                ++lnn;
                    331:                                break;
                    332:                        }
                    333:
1.13      schwarze  334:                        /*
                    335:                         * Make sure we have space for at least
                    336:                         * one backslash and one other character
                    337:                         * and the trailing NUL byte.
                    338:                         */
                    339:
                    340:                        if (pos + 2 >= (int)ln.sz)
                    341:                                resize_buf(&ln, 256);
                    342:
1.25      schwarze  343:                        /*
1.1       schwarze  344:                         * Warn about bogus characters.  If you're using
                    345:                         * non-ASCII encoding, you're screwing your
                    346:                         * readers.  Since I'd rather this not happen,
1.6       schwarze  347:                         * I'll be helpful and replace these characters
                    348:                         * with "?", so we don't display gibberish.
                    349:                         * Note to manual writers: use special characters.
1.1       schwarze  350:                         */
                    351:
                    352:                        c = (unsigned char) blk.buf[i];
                    353:
1.25      schwarze  354:                        if ( ! (isascii(c) &&
                    355:                            (isgraph(c) || isblank(c)))) {
1.1       schwarze  356:                                mandoc_msg(MANDOCERR_BADCHAR, curp,
1.25      schwarze  357:                                    curp->line, pos, NULL);
1.1       schwarze  358:                                i++;
1.6       schwarze  359:                                ln.buf[pos++] = '?';
1.1       schwarze  360:                                continue;
                    361:                        }
                    362:
                    363:                        /* Trailing backslash = a plain char. */
                    364:
                    365:                        if ('\\' != blk.buf[i] || i + 1 == (int)blk.sz) {
                    366:                                ln.buf[pos++] = blk.buf[i++];
                    367:                                continue;
                    368:                        }
                    369:
                    370:                        /*
                    371:                         * Found escape and at least one other character.
                    372:                         * When it's a newline character, skip it.
                    373:                         * When there is a carriage return in between,
                    374:                         * skip that one as well.
                    375:                         */
                    376:
                    377:                        if ('\r' == blk.buf[i + 1] && i + 2 < (int)blk.sz &&
                    378:                            '\n' == blk.buf[i + 2])
                    379:                                ++i;
                    380:                        if ('\n' == blk.buf[i + 1]) {
                    381:                                i += 2;
                    382:                                ++lnn;
                    383:                                continue;
                    384:                        }
                    385:
                    386:                        if ('"' == blk.buf[i + 1] || '#' == blk.buf[i + 1]) {
                    387:                                i += 2;
                    388:                                /* Comment, skip to end of line */
                    389:                                for (; i < (int)blk.sz; ++i) {
                    390:                                        if ('\n' == blk.buf[i]) {
                    391:                                                ++i;
                    392:                                                ++lnn;
                    393:                                                break;
                    394:                                        }
                    395:                                }
                    396:
                    397:                                /* Backout trailing whitespaces */
                    398:                                for (; pos > 0; --pos) {
                    399:                                        if (ln.buf[pos - 1] != ' ')
                    400:                                                break;
                    401:                                        if (pos > 2 && ln.buf[pos - 2] == '\\')
                    402:                                                break;
                    403:                                }
                    404:                                break;
                    405:                        }
                    406:
1.13      schwarze  407:                        /* Catch escaped bogus characters. */
                    408:
                    409:                        c = (unsigned char) blk.buf[i+1];
                    410:
1.25      schwarze  411:                        if ( ! (isascii(c) &&
                    412:                            (isgraph(c) || isblank(c)))) {
1.13      schwarze  413:                                mandoc_msg(MANDOCERR_BADCHAR, curp,
1.25      schwarze  414:                                    curp->line, pos, NULL);
1.13      schwarze  415:                                i += 2;
                    416:                                ln.buf[pos++] = '?';
                    417:                                continue;
                    418:                        }
                    419:
1.1       schwarze  420:                        /* Some other escape sequence, copy & cont. */
                    421:
                    422:                        ln.buf[pos++] = blk.buf[i++];
                    423:                        ln.buf[pos++] = blk.buf[i++];
                    424:                }
                    425:
1.25      schwarze  426:                if (pos >= (int)ln.sz)
1.1       schwarze  427:                        resize_buf(&ln, 256);
                    428:
                    429:                ln.buf[pos] = '\0';
                    430:
                    431:                /*
                    432:                 * A significant amount of complexity is contained by
                    433:                 * the roff preprocessor.  It's line-oriented but can be
                    434:                 * expressed on one line, so we need at times to
                    435:                 * readjust our starting point and re-run it.  The roff
                    436:                 * preprocessor can also readjust the buffers with new
                    437:                 * data, so we pass them in wholesale.
                    438:                 */
                    439:
                    440:                of = 0;
                    441:
1.4       schwarze  442:                /*
                    443:                 * Maintain a lookaside buffer of all parsed lines.  We
                    444:                 * only do this if mparse_keep() has been invoked (the
                    445:                 * buffer may be accessed with mparse_getkeep()).
                    446:                 */
                    447:
                    448:                if (curp->secondary) {
1.25      schwarze  449:                        curp->secondary->buf = mandoc_realloc(
                    450:                            curp->secondary->buf,
                    451:                            curp->secondary->sz + pos + 2);
                    452:                        memcpy(curp->secondary->buf +
                    453:                            curp->secondary->sz,
                    454:                            ln.buf, pos);
1.4       schwarze  455:                        curp->secondary->sz += pos;
                    456:                        curp->secondary->buf
                    457:                                [curp->secondary->sz] = '\n';
                    458:                        curp->secondary->sz++;
                    459:                        curp->secondary->buf
                    460:                                [curp->secondary->sz] = '\0';
                    461:                }
1.1       schwarze  462: rerun:
1.25      schwarze  463:                rr = roff_parseln(curp->roff, curp->line,
                    464:                    &ln.buf, &ln.sz, of, &of);
1.1       schwarze  465:
                    466:                switch (rr) {
1.25      schwarze  467:                case ROFF_REPARSE:
1.1       schwarze  468:                        if (REPARSE_LIMIT >= ++curp->reparse_count)
                    469:                                mparse_buf_r(curp, ln, 0);
                    470:                        else
                    471:                                mandoc_msg(MANDOCERR_ROFFLOOP, curp,
1.25      schwarze  472:                                    curp->line, pos, NULL);
1.1       schwarze  473:                        pos = 0;
                    474:                        continue;
1.25      schwarze  475:                case ROFF_APPEND:
1.1       schwarze  476:                        pos = (int)strlen(ln.buf);
                    477:                        continue;
1.25      schwarze  478:                case ROFF_RERUN:
1.1       schwarze  479:                        goto rerun;
1.25      schwarze  480:                case ROFF_IGN:
1.1       schwarze  481:                        pos = 0;
                    482:                        continue;
1.25      schwarze  483:                case ROFF_ERR:
1.1       schwarze  484:                        assert(MANDOCLEVEL_FATAL <= curp->file_status);
                    485:                        break;
1.25      schwarze  486:                case ROFF_SO:
1.23      schwarze  487:                        if (0 == (MPARSE_SO & curp->options) &&
                    488:                            (i >= (int)blk.sz || '\0' == blk.buf[i])) {
                    489:                                curp->sodest = mandoc_strdup(ln.buf + of);
                    490:                                free(ln.buf);
                    491:                                return;
                    492:                        }
1.4       schwarze  493:                        /*
                    494:                         * We remove `so' clauses from our lookaside
                    495:                         * buffer because we're going to descend into
                    496:                         * the file recursively.
                    497:                         */
1.25      schwarze  498:                        if (curp->secondary)
1.4       schwarze  499:                                curp->secondary->sz -= pos + 1;
1.14      schwarze  500:                        mparse_readfd(curp, -1, ln.buf + of);
1.1       schwarze  501:                        if (MANDOCLEVEL_FATAL <= curp->file_status)
                    502:                                break;
                    503:                        pos = 0;
                    504:                        continue;
                    505:                default:
                    506:                        break;
                    507:                }
                    508:
                    509:                /*
                    510:                 * If we encounter errors in the recursive parse, make
                    511:                 * sure we don't continue parsing.
                    512:                 */
                    513:
                    514:                if (MANDOCLEVEL_FATAL <= curp->file_status)
                    515:                        break;
                    516:
                    517:                /*
                    518:                 * If input parsers have not been allocated, do so now.
1.2       schwarze  519:                 * We keep these instanced between parsers, but set them
1.1       schwarze  520:                 * locally per parse routine since we can use different
                    521:                 * parsers with each one.
                    522:                 */
                    523:
                    524:                if ( ! (curp->man || curp->mdoc))
                    525:                        pset(ln.buf + of, pos - of, curp);
                    526:
1.25      schwarze  527:                /*
1.1       schwarze  528:                 * Lastly, push down into the parsers themselves.  One
                    529:                 * of these will have already been set in the pset()
                    530:                 * routine.
                    531:                 * If libroff returns ROFF_TBL, then add it to the
                    532:                 * currently open parse.  Since we only get here if
                    533:                 * there does exist data (see tbl_data.c), we're
                    534:                 * guaranteed that something's been allocated.
                    535:                 * Do the same for ROFF_EQN.
                    536:                 */
                    537:
                    538:                rc = -1;
                    539:
                    540:                if (ROFF_TBL == rr)
                    541:                        while (NULL != (span = roff_span(curp->roff))) {
                    542:                                rc = curp->man ?
1.25      schwarze  543:                                    man_addspan(curp->man, span) :
                    544:                                    mdoc_addspan(curp->mdoc, span);
1.1       schwarze  545:                                if (0 == rc)
                    546:                                        break;
                    547:                        }
                    548:                else if (ROFF_EQN == rr)
1.25      schwarze  549:                        rc = curp->mdoc ?
                    550:                            mdoc_addeqn(curp->mdoc,
                    551:                                roff_eqn(curp->roff)) :
                    552:                            man_addeqn(curp->man,
                    553:                                roff_eqn(curp->roff));
1.1       schwarze  554:                else if (curp->man || curp->mdoc)
                    555:                        rc = curp->man ?
1.25      schwarze  556:                            man_parseln(curp->man,
                    557:                                curp->line, ln.buf, of) :
                    558:                            mdoc_parseln(curp->mdoc,
                    559:                                curp->line, ln.buf, of);
1.1       schwarze  560:
                    561:                if (0 == rc) {
                    562:                        assert(MANDOCLEVEL_FATAL <= curp->file_status);
                    563:                        break;
1.19      schwarze  564:                } else if (2 == rc)
                    565:                        break;
1.1       schwarze  566:
                    567:                /* Temporary buffers typically are not full. */
                    568:
                    569:                if (0 == start && '\0' == blk.buf[i])
                    570:                        break;
                    571:
                    572:                /* Start the next input line. */
                    573:
                    574:                pos = 0;
                    575:        }
                    576:
                    577:        free(ln.buf);
                    578: }
                    579:
                    580: static int
1.18      schwarze  581: read_whole_file(struct mparse *curp, const char *file, int fd,
                    582:                struct buf *fb, int *with_mmap)
1.1       schwarze  583: {
                    584:        struct stat      st;
                    585:        size_t           off;
                    586:        ssize_t          ssz;
                    587:
                    588:        if (-1 == fstat(fd, &st)) {
1.18      schwarze  589:                curp->file_status = MANDOCLEVEL_SYSERR;
                    590:                if (curp->mmsg)
                    591:                        (*curp->mmsg)(MANDOCERR_SYSSTAT, curp->file_status,
                    592:                            file, 0, 0, strerror(errno));
1.1       schwarze  593:                return(0);
                    594:        }
                    595:
                    596:        /*
                    597:         * If we're a regular file, try just reading in the whole entry
                    598:         * via mmap().  This is faster than reading it into blocks, and
                    599:         * since each file is only a few bytes to begin with, I'm not
                    600:         * concerned that this is going to tank any machines.
                    601:         */
                    602:
                    603:        if (S_ISREG(st.st_mode)) {
                    604:                if (st.st_size >= (1U << 31)) {
1.18      schwarze  605:                        curp->file_status = MANDOCLEVEL_FATAL;
                    606:                        if (curp->mmsg)
                    607:                                (*curp->mmsg)(MANDOCERR_TOOLARGE,
                    608:                                    curp->file_status, file, 0, 0, NULL);
1.1       schwarze  609:                        return(0);
                    610:                }
                    611:                *with_mmap = 1;
                    612:                fb->sz = (size_t)st.st_size;
1.15      schwarze  613:                fb->buf = mmap(NULL, fb->sz, PROT_READ, MAP_SHARED, fd, 0);
1.1       schwarze  614:                if (fb->buf != MAP_FAILED)
                    615:                        return(1);
                    616:        }
                    617:
                    618:        /*
                    619:         * If this isn't a regular file (like, say, stdin), then we must
                    620:         * go the old way and just read things in bit by bit.
                    621:         */
                    622:
                    623:        *with_mmap = 0;
                    624:        off = 0;
                    625:        fb->sz = 0;
                    626:        fb->buf = NULL;
                    627:        for (;;) {
                    628:                if (off == fb->sz) {
                    629:                        if (fb->sz == (1U << 31)) {
1.18      schwarze  630:                                curp->file_status = MANDOCLEVEL_FATAL;
                    631:                                if (curp->mmsg)
                    632:                                        (*curp->mmsg)(MANDOCERR_TOOLARGE,
                    633:                                            curp->file_status,
                    634:                                            file, 0, 0, NULL);
1.1       schwarze  635:                                break;
                    636:                        }
                    637:                        resize_buf(fb, 65536);
                    638:                }
                    639:                ssz = read(fd, fb->buf + (int)off, fb->sz - off);
                    640:                if (ssz == 0) {
                    641:                        fb->sz = off;
                    642:                        return(1);
                    643:                }
                    644:                if (ssz == -1) {
1.18      schwarze  645:                        curp->file_status = MANDOCLEVEL_SYSERR;
                    646:                        if (curp->mmsg)
                    647:                                (*curp->mmsg)(MANDOCERR_SYSREAD,
                    648:                                    curp->file_status, file, 0, 0,
                    649:                                    strerror(errno));
1.1       schwarze  650:                        break;
                    651:                }
                    652:                off += (size_t)ssz;
                    653:        }
                    654:
                    655:        free(fb->buf);
                    656:        fb->buf = NULL;
                    657:        return(0);
                    658: }
                    659:
                    660: static void
                    661: mparse_end(struct mparse *curp)
                    662: {
                    663:
                    664:        if (MANDOCLEVEL_FATAL <= curp->file_status)
                    665:                return;
                    666:
                    667:        if (curp->mdoc && ! mdoc_endparse(curp->mdoc)) {
                    668:                assert(MANDOCLEVEL_FATAL <= curp->file_status);
                    669:                return;
                    670:        }
                    671:
                    672:        if (curp->man && ! man_endparse(curp->man)) {
                    673:                assert(MANDOCLEVEL_FATAL <= curp->file_status);
                    674:                return;
                    675:        }
                    676:
1.23      schwarze  677:        if ( ! (curp->mdoc || curp->man || curp->sodest)) {
1.28    ! schwarze  678:                mandoc_msg(MANDOCERR_NOTMANUAL, curp, 0, 0, NULL);
1.1       schwarze  679:                curp->file_status = MANDOCLEVEL_FATAL;
                    680:                return;
                    681:        }
                    682:
                    683:        roff_endparse(curp->roff);
                    684: }
                    685:
1.15      schwarze  686: static void
                    687: mparse_parse_buffer(struct mparse *curp, struct buf blk, const char *file)
1.1       schwarze  688: {
                    689:        const char      *svfile;
1.14      schwarze  690:        static int       recursion_depth;
                    691:
                    692:        if (64 < recursion_depth) {
                    693:                mandoc_msg(MANDOCERR_ROFFLOOP, curp, curp->line, 0, NULL);
1.15      schwarze  694:                return;
1.14      schwarze  695:        }
1.1       schwarze  696:
1.15      schwarze  697:        /* Line number is per-file. */
                    698:        svfile = curp->file;
                    699:        curp->file = file;
                    700:        curp->line = 1;
                    701:        recursion_depth++;
                    702:
                    703:        mparse_buf_r(curp, blk, 1);
                    704:
                    705:        if (0 == --recursion_depth && MANDOCLEVEL_FATAL > curp->file_status)
                    706:                mparse_end(curp);
                    707:
                    708:        curp->file = svfile;
                    709: }
                    710:
                    711: enum mandoclevel
                    712: mparse_readfd(struct mparse *curp, int fd, const char *file)
                    713: {
                    714:        struct buf       blk;
                    715:        int              with_mmap;
                    716:
1.18      schwarze  717:        if (-1 == fd && -1 == (fd = open(file, O_RDONLY, 0))) {
                    718:                curp->file_status = MANDOCLEVEL_SYSERR;
                    719:                if (curp->mmsg)
                    720:                        (*curp->mmsg)(MANDOCERR_SYSOPEN,
                    721:                            curp->file_status,
                    722:                            file, 0, 0, strerror(errno));
                    723:                goto out;
                    724:        }
                    725:
1.15      schwarze  726:        /*
                    727:         * Run for each opened file; may be called more than once for
                    728:         * each full parse sequence if the opened file is nested (i.e.,
                    729:         * from `so').  Simply sucks in the whole file and moves into
                    730:         * the parse phase for the file.
                    731:         */
1.1       schwarze  732:
1.18      schwarze  733:        if ( ! read_whole_file(curp, file, fd, &blk, &with_mmap))
1.15      schwarze  734:                goto out;
1.1       schwarze  735:
1.15      schwarze  736:        mparse_parse_buffer(curp, blk, file);
1.1       schwarze  737:
1.15      schwarze  738:        if (with_mmap)
                    739:                munmap(blk.buf, blk.sz);
                    740:        else
                    741:                free(blk.buf);
1.1       schwarze  742:
                    743:        if (STDIN_FILENO != fd && -1 == close(fd))
                    744:                perror(file);
1.14      schwarze  745: out:
1.1       schwarze  746:        return(curp->file_status);
                    747: }
                    748:
                    749: struct mparse *
1.22      schwarze  750: mparse_alloc(int options, enum mandoclevel wlevel,
                    751:                mandocmsg mmsg, char *defos)
1.1       schwarze  752: {
                    753:        struct mparse   *curp;
                    754:
                    755:        assert(wlevel <= MANDOCLEVEL_FATAL);
                    756:
                    757:        curp = mandoc_calloc(1, sizeof(struct mparse));
                    758:
1.22      schwarze  759:        curp->options = options;
1.1       schwarze  760:        curp->wlevel = wlevel;
                    761:        curp->mmsg = mmsg;
1.7       schwarze  762:        curp->defos = defos;
1.1       schwarze  763:
1.22      schwarze  764:        curp->roff = roff_alloc(curp, options);
1.1       schwarze  765:        return(curp);
                    766: }
                    767:
                    768: void
                    769: mparse_reset(struct mparse *curp)
                    770: {
                    771:
                    772:        roff_reset(curp->roff);
                    773:
                    774:        if (curp->mdoc)
                    775:                mdoc_reset(curp->mdoc);
                    776:        if (curp->man)
                    777:                man_reset(curp->man);
1.4       schwarze  778:        if (curp->secondary)
                    779:                curp->secondary->sz = 0;
1.1       schwarze  780:
                    781:        curp->file_status = MANDOCLEVEL_OK;
                    782:        curp->mdoc = NULL;
                    783:        curp->man = NULL;
1.23      schwarze  784:
                    785:        free(curp->sodest);
                    786:        curp->sodest = NULL;
1.1       schwarze  787: }
                    788:
                    789: void
                    790: mparse_free(struct mparse *curp)
                    791: {
                    792:
                    793:        if (curp->pmdoc)
                    794:                mdoc_free(curp->pmdoc);
                    795:        if (curp->pman)
                    796:                man_free(curp->pman);
                    797:        if (curp->roff)
                    798:                roff_free(curp->roff);
1.4       schwarze  799:        if (curp->secondary)
                    800:                free(curp->secondary->buf);
1.1       schwarze  801:
1.4       schwarze  802:        free(curp->secondary);
1.23      schwarze  803:        free(curp->sodest);
1.1       schwarze  804:        free(curp);
                    805: }
                    806:
                    807: void
1.23      schwarze  808: mparse_result(struct mparse *curp,
                    809:        struct mdoc **mdoc, struct man **man, char **sodest)
1.1       schwarze  810: {
                    811:
1.23      schwarze  812:        if (sodest && NULL != (*sodest = curp->sodest)) {
                    813:                *mdoc = NULL;
                    814:                *man = NULL;
                    815:                return;
                    816:        }
1.1       schwarze  817:        if (mdoc)
                    818:                *mdoc = curp->mdoc;
                    819:        if (man)
                    820:                *man = curp->man;
                    821: }
                    822:
                    823: void
                    824: mandoc_vmsg(enum mandocerr t, struct mparse *m,
                    825:                int ln, int pos, const char *fmt, ...)
                    826: {
                    827:        char             buf[256];
                    828:        va_list          ap;
                    829:
                    830:        va_start(ap, fmt);
1.26      schwarze  831:        (void)vsnprintf(buf, sizeof(buf), fmt, ap);
1.1       schwarze  832:        va_end(ap);
                    833:
                    834:        mandoc_msg(t, m, ln, pos, buf);
                    835: }
                    836:
                    837: void
1.25      schwarze  838: mandoc_msg(enum mandocerr er, struct mparse *m,
1.1       schwarze  839:                int ln, int col, const char *msg)
                    840: {
                    841:        enum mandoclevel level;
                    842:
                    843:        level = MANDOCLEVEL_FATAL;
                    844:        while (er < mandoclimits[level])
                    845:                level--;
                    846:
                    847:        if (level < m->wlevel)
                    848:                return;
                    849:
                    850:        if (m->mmsg)
                    851:                (*m->mmsg)(er, level, m->file, ln, col, msg);
                    852:
                    853:        if (m->file_status < level)
                    854:                m->file_status = level;
                    855: }
                    856:
                    857: const char *
                    858: mparse_strerror(enum mandocerr er)
                    859: {
                    860:
                    861:        return(mandocerrs[er]);
                    862: }
                    863:
                    864: const char *
                    865: mparse_strlevel(enum mandoclevel lvl)
                    866: {
                    867:        return(mandoclevels[lvl]);
1.4       schwarze  868: }
                    869:
                    870: void
                    871: mparse_keep(struct mparse *p)
                    872: {
                    873:
                    874:        assert(NULL == p->secondary);
                    875:        p->secondary = mandoc_calloc(1, sizeof(struct buf));
                    876: }
                    877:
                    878: const char *
                    879: mparse_getkeep(const struct mparse *p)
                    880: {
                    881:
                    882:        assert(p->secondary);
                    883:        return(p->secondary->sz ? p->secondary->buf : NULL);
1.1       schwarze  884: }