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

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