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

1.12    ! schwarze    1: /*     $Id: read.c,v 1.11 2012/11/16 22:20:40 schwarze Exp $ */
1.1       schwarze    2: /*
                      3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.12    ! schwarze    4:  * Copyright (c) 2010, 2011, 2012 Ingo Schwarze <schwarze@openbsd.org>
1.1       schwarze    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",
1.12    ! schwarze  172:        "skipping column outside column list",
1.1       schwarze  173:        "skipping end of block that is not open",
                    174:        "missing end of block",
                    175:        "scope open on exit",
                    176:        "uname(3) system call failed",
                    177:        "macro requires line argument(s)",
                    178:        "macro requires body argument(s)",
                    179:        "macro requires argument(s)",
                    180:        "missing list type",
                    181:        "line argument(s) will be lost",
                    182:        "body argument(s) will be lost",
                    183:
                    184:        "generic fatal error",
                    185:
                    186:        "not a manual",
                    187:        "column syntax is inconsistent",
                    188:        "NOT IMPLEMENTED: .Bd -file",
                    189:        "argument count wrong, violates syntax",
                    190:        "child violates parent syntax",
                    191:        "argument count wrong, violates syntax",
                    192:        "NOT IMPLEMENTED: .so with absolute path or \"..\"",
                    193:        "no document body",
                    194:        "no document prologue",
                    195:        "static buffer exhausted",
                    196: };
                    197:
                    198: static const char * const      mandoclevels[MANDOCLEVEL_MAX] = {
                    199:        "SUCCESS",
                    200:        "RESERVED",
                    201:        "WARNING",
                    202:        "ERROR",
                    203:        "FATAL",
                    204:        "BADARG",
                    205:        "SYSERR"
                    206: };
                    207:
                    208: static void
                    209: resize_buf(struct buf *buf, size_t initial)
                    210: {
                    211:
                    212:        buf->sz = buf->sz > initial/2 ? 2 * buf->sz : initial;
                    213:        buf->buf = mandoc_realloc(buf->buf, buf->sz);
                    214: }
                    215:
                    216: static void
                    217: pset(const char *buf, int pos, struct mparse *curp)
                    218: {
                    219:        int              i;
                    220:
                    221:        /*
                    222:         * Try to intuit which kind of manual parser should be used.  If
                    223:         * passed in by command-line (-man, -mdoc), then use that
                    224:         * explicitly.  If passed as -mandoc, then try to guess from the
                    225:         * line: either skip dot-lines, use -mdoc when finding `.Dt', or
                    226:         * default to -man, which is more lenient.
                    227:         *
                    228:         * Separate out pmdoc/pman from mdoc/man: the first persists
                    229:         * through all parsers, while the latter is used per-parse.
                    230:         */
                    231:
                    232:        if ('.' == buf[0] || '\'' == buf[0]) {
                    233:                for (i = 1; buf[i]; i++)
                    234:                        if (' ' != buf[i] && '\t' != buf[i])
                    235:                                break;
                    236:                if ('\0' == buf[i])
                    237:                        return;
                    238:        }
                    239:
                    240:        switch (curp->inttype) {
                    241:        case (MPARSE_MDOC):
                    242:                if (NULL == curp->pmdoc)
1.7       schwarze  243:                        curp->pmdoc = mdoc_alloc(curp->roff, curp,
                    244:                                        curp->defos);
1.1       schwarze  245:                assert(curp->pmdoc);
                    246:                curp->mdoc = curp->pmdoc;
                    247:                return;
                    248:        case (MPARSE_MAN):
                    249:                if (NULL == curp->pman)
1.3       schwarze  250:                        curp->pman = man_alloc(curp->roff, curp);
1.1       schwarze  251:                assert(curp->pman);
                    252:                curp->man = curp->pman;
                    253:                return;
                    254:        default:
                    255:                break;
                    256:        }
                    257:
                    258:        if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3))  {
                    259:                if (NULL == curp->pmdoc)
1.7       schwarze  260:                        curp->pmdoc = mdoc_alloc(curp->roff, curp,
                    261:                                        curp->defos);
1.1       schwarze  262:                assert(curp->pmdoc);
                    263:                curp->mdoc = curp->pmdoc;
                    264:                return;
                    265:        }
                    266:
                    267:        if (NULL == curp->pman)
1.3       schwarze  268:                curp->pman = man_alloc(curp->roff, curp);
1.1       schwarze  269:        assert(curp->pman);
                    270:        curp->man = curp->pman;
                    271: }
                    272:
                    273: /*
                    274:  * Main parse routine for an opened file.  This is called for each
                    275:  * opened file and simply loops around the full input file, possibly
                    276:  * nesting (i.e., with `so').
                    277:  */
                    278: static void
                    279: mparse_buf_r(struct mparse *curp, struct buf blk, int start)
                    280: {
                    281:        const struct tbl_span   *span;
                    282:        struct buf       ln;
                    283:        enum rofferr     rr;
                    284:        int              i, of, rc;
                    285:        int              pos; /* byte number in the ln buffer */
                    286:        int              lnn; /* line number in the real file */
                    287:        unsigned char    c;
                    288:
                    289:        memset(&ln, 0, sizeof(struct buf));
                    290:
                    291:        lnn = curp->line;
                    292:        pos = 0;
                    293:
                    294:        for (i = 0; i < (int)blk.sz; ) {
                    295:                if (0 == pos && '\0' == blk.buf[i])
                    296:                        break;
                    297:
                    298:                if (start) {
                    299:                        curp->line = lnn;
                    300:                        curp->reparse_count = 0;
                    301:                }
                    302:
                    303:                while (i < (int)blk.sz && (start || '\0' != blk.buf[i])) {
                    304:
                    305:                        /*
                    306:                         * When finding an unescaped newline character,
                    307:                         * leave the character loop to process the line.
                    308:                         * Skip a preceding carriage return, if any.
                    309:                         */
                    310:
                    311:                        if ('\r' == blk.buf[i] && i + 1 < (int)blk.sz &&
                    312:                            '\n' == blk.buf[i + 1])
                    313:                                ++i;
                    314:                        if ('\n' == blk.buf[i]) {
                    315:                                ++i;
                    316:                                ++lnn;
                    317:                                break;
                    318:                        }
                    319:
                    320:                        /*
                    321:                         * Warn about bogus characters.  If you're using
                    322:                         * non-ASCII encoding, you're screwing your
                    323:                         * readers.  Since I'd rather this not happen,
1.6       schwarze  324:                         * I'll be helpful and replace these characters
                    325:                         * with "?", so we don't display gibberish.
                    326:                         * Note to manual writers: use special characters.
1.1       schwarze  327:                         */
                    328:
                    329:                        c = (unsigned char) blk.buf[i];
                    330:
                    331:                        if ( ! (isascii(c) &&
                    332:                                        (isgraph(c) || isblank(c)))) {
                    333:                                mandoc_msg(MANDOCERR_BADCHAR, curp,
1.6       schwarze  334:                                                curp->line, pos, NULL);
1.1       schwarze  335:                                i++;
1.6       schwarze  336:                                if (pos >= (int)ln.sz)
                    337:                                        resize_buf(&ln, 256);
                    338:                                ln.buf[pos++] = '?';
1.1       schwarze  339:                                continue;
                    340:                        }
                    341:
                    342:                        /* Trailing backslash = a plain char. */
                    343:
                    344:                        if ('\\' != blk.buf[i] || i + 1 == (int)blk.sz) {
                    345:                                if (pos >= (int)ln.sz)
                    346:                                        resize_buf(&ln, 256);
                    347:                                ln.buf[pos++] = blk.buf[i++];
                    348:                                continue;
                    349:                        }
                    350:
                    351:                        /*
                    352:                         * Found escape and at least one other character.
                    353:                         * When it's a newline character, skip it.
                    354:                         * When there is a carriage return in between,
                    355:                         * skip that one as well.
                    356:                         */
                    357:
                    358:                        if ('\r' == blk.buf[i + 1] && i + 2 < (int)blk.sz &&
                    359:                            '\n' == blk.buf[i + 2])
                    360:                                ++i;
                    361:                        if ('\n' == blk.buf[i + 1]) {
                    362:                                i += 2;
                    363:                                ++lnn;
                    364:                                continue;
                    365:                        }
                    366:
                    367:                        if ('"' == blk.buf[i + 1] || '#' == blk.buf[i + 1]) {
                    368:                                i += 2;
                    369:                                /* Comment, skip to end of line */
                    370:                                for (; i < (int)blk.sz; ++i) {
                    371:                                        if ('\n' == blk.buf[i]) {
                    372:                                                ++i;
                    373:                                                ++lnn;
                    374:                                                break;
                    375:                                        }
                    376:                                }
                    377:
                    378:                                /* Backout trailing whitespaces */
                    379:                                for (; pos > 0; --pos) {
                    380:                                        if (ln.buf[pos - 1] != ' ')
                    381:                                                break;
                    382:                                        if (pos > 2 && ln.buf[pos - 2] == '\\')
                    383:                                                break;
                    384:                                }
                    385:                                break;
                    386:                        }
                    387:
                    388:                        /* Some other escape sequence, copy & cont. */
                    389:
                    390:                        if (pos + 1 >= (int)ln.sz)
                    391:                                resize_buf(&ln, 256);
                    392:
                    393:                        ln.buf[pos++] = blk.buf[i++];
                    394:                        ln.buf[pos++] = blk.buf[i++];
                    395:                }
                    396:
                    397:                if (pos >= (int)ln.sz)
                    398:                        resize_buf(&ln, 256);
                    399:
                    400:                ln.buf[pos] = '\0';
                    401:
                    402:                /*
                    403:                 * A significant amount of complexity is contained by
                    404:                 * the roff preprocessor.  It's line-oriented but can be
                    405:                 * expressed on one line, so we need at times to
                    406:                 * readjust our starting point and re-run it.  The roff
                    407:                 * preprocessor can also readjust the buffers with new
                    408:                 * data, so we pass them in wholesale.
                    409:                 */
                    410:
                    411:                of = 0;
                    412:
1.4       schwarze  413:                /*
                    414:                 * Maintain a lookaside buffer of all parsed lines.  We
                    415:                 * only do this if mparse_keep() has been invoked (the
                    416:                 * buffer may be accessed with mparse_getkeep()).
                    417:                 */
                    418:
                    419:                if (curp->secondary) {
                    420:                        curp->secondary->buf =
                    421:                                mandoc_realloc
                    422:                                (curp->secondary->buf,
                    423:                                 curp->secondary->sz + pos + 2);
                    424:                        memcpy(curp->secondary->buf +
                    425:                                        curp->secondary->sz,
                    426:                                        ln.buf, pos);
                    427:                        curp->secondary->sz += pos;
                    428:                        curp->secondary->buf
                    429:                                [curp->secondary->sz] = '\n';
                    430:                        curp->secondary->sz++;
                    431:                        curp->secondary->buf
                    432:                                [curp->secondary->sz] = '\0';
                    433:                }
1.1       schwarze  434: rerun:
                    435:                rr = roff_parseln
                    436:                        (curp->roff, curp->line,
                    437:                         &ln.buf, &ln.sz, of, &of);
                    438:
                    439:                switch (rr) {
                    440:                case (ROFF_REPARSE):
                    441:                        if (REPARSE_LIMIT >= ++curp->reparse_count)
                    442:                                mparse_buf_r(curp, ln, 0);
                    443:                        else
                    444:                                mandoc_msg(MANDOCERR_ROFFLOOP, curp,
                    445:                                        curp->line, pos, NULL);
                    446:                        pos = 0;
                    447:                        continue;
                    448:                case (ROFF_APPEND):
                    449:                        pos = (int)strlen(ln.buf);
                    450:                        continue;
                    451:                case (ROFF_RERUN):
                    452:                        goto rerun;
                    453:                case (ROFF_IGN):
                    454:                        pos = 0;
                    455:                        continue;
                    456:                case (ROFF_ERR):
                    457:                        assert(MANDOCLEVEL_FATAL <= curp->file_status);
                    458:                        break;
                    459:                case (ROFF_SO):
1.4       schwarze  460:                        /*
                    461:                         * We remove `so' clauses from our lookaside
                    462:                         * buffer because we're going to descend into
                    463:                         * the file recursively.
                    464:                         */
                    465:                        if (curp->secondary)
                    466:                                curp->secondary->sz -= pos + 1;
1.1       schwarze  467:                        mparse_readfd_r(curp, -1, ln.buf + of, 1);
                    468:                        if (MANDOCLEVEL_FATAL <= curp->file_status)
                    469:                                break;
                    470:                        pos = 0;
                    471:                        continue;
                    472:                default:
                    473:                        break;
                    474:                }
                    475:
                    476:                /*
                    477:                 * If we encounter errors in the recursive parse, make
                    478:                 * sure we don't continue parsing.
                    479:                 */
                    480:
                    481:                if (MANDOCLEVEL_FATAL <= curp->file_status)
                    482:                        break;
                    483:
                    484:                /*
                    485:                 * If input parsers have not been allocated, do so now.
1.2       schwarze  486:                 * We keep these instanced between parsers, but set them
1.1       schwarze  487:                 * locally per parse routine since we can use different
                    488:                 * parsers with each one.
                    489:                 */
                    490:
                    491:                if ( ! (curp->man || curp->mdoc))
                    492:                        pset(ln.buf + of, pos - of, curp);
                    493:
                    494:                /*
                    495:                 * Lastly, push down into the parsers themselves.  One
                    496:                 * of these will have already been set in the pset()
                    497:                 * routine.
                    498:                 * If libroff returns ROFF_TBL, then add it to the
                    499:                 * currently open parse.  Since we only get here if
                    500:                 * there does exist data (see tbl_data.c), we're
                    501:                 * guaranteed that something's been allocated.
                    502:                 * Do the same for ROFF_EQN.
                    503:                 */
                    504:
                    505:                rc = -1;
                    506:
                    507:                if (ROFF_TBL == rr)
                    508:                        while (NULL != (span = roff_span(curp->roff))) {
                    509:                                rc = curp->man ?
                    510:                                        man_addspan(curp->man, span) :
                    511:                                        mdoc_addspan(curp->mdoc, span);
                    512:                                if (0 == rc)
                    513:                                        break;
                    514:                        }
                    515:                else if (ROFF_EQN == rr)
                    516:                        rc = curp->mdoc ?
                    517:                                mdoc_addeqn(curp->mdoc,
                    518:                                        roff_eqn(curp->roff)) :
                    519:                                man_addeqn(curp->man,
                    520:                                        roff_eqn(curp->roff));
                    521:                else if (curp->man || curp->mdoc)
                    522:                        rc = curp->man ?
                    523:                                man_parseln(curp->man,
                    524:                                        curp->line, ln.buf, of) :
                    525:                                mdoc_parseln(curp->mdoc,
                    526:                                        curp->line, ln.buf, of);
                    527:
                    528:                if (0 == rc) {
                    529:                        assert(MANDOCLEVEL_FATAL <= curp->file_status);
                    530:                        break;
                    531:                }
                    532:
                    533:                /* Temporary buffers typically are not full. */
                    534:
                    535:                if (0 == start && '\0' == blk.buf[i])
                    536:                        break;
                    537:
                    538:                /* Start the next input line. */
                    539:
                    540:                pos = 0;
                    541:        }
                    542:
                    543:        free(ln.buf);
                    544: }
                    545:
                    546: static void
                    547: pdesc(struct mparse *curp, const char *file, int fd)
                    548: {
                    549:        struct buf       blk;
                    550:        int              with_mmap;
                    551:
                    552:        /*
                    553:         * Run for each opened file; may be called more than once for
                    554:         * each full parse sequence if the opened file is nested (i.e.,
                    555:         * from `so').  Simply sucks in the whole file and moves into
                    556:         * the parse phase for the file.
                    557:         */
                    558:
                    559:        if ( ! read_whole_file(file, fd, &blk, &with_mmap)) {
                    560:                curp->file_status = MANDOCLEVEL_SYSERR;
                    561:                return;
                    562:        }
                    563:
                    564:        /* Line number is per-file. */
                    565:
                    566:        curp->line = 1;
                    567:
                    568:        mparse_buf_r(curp, blk, 1);
                    569:
                    570:        if (with_mmap)
                    571:                munmap(blk.buf, blk.sz);
                    572:        else
                    573:                free(blk.buf);
                    574: }
                    575:
                    576: static int
                    577: read_whole_file(const char *file, int fd, struct buf *fb, int *with_mmap)
                    578: {
                    579:        struct stat      st;
                    580:        size_t           off;
                    581:        ssize_t          ssz;
                    582:
                    583:        if (-1 == fstat(fd, &st)) {
                    584:                perror(file);
                    585:                return(0);
                    586:        }
                    587:
                    588:        /*
                    589:         * If we're a regular file, try just reading in the whole entry
                    590:         * via mmap().  This is faster than reading it into blocks, and
                    591:         * since each file is only a few bytes to begin with, I'm not
                    592:         * concerned that this is going to tank any machines.
                    593:         */
                    594:
                    595:        if (S_ISREG(st.st_mode)) {
                    596:                if (st.st_size >= (1U << 31)) {
                    597:                        fprintf(stderr, "%s: input too large\n", file);
                    598:                        return(0);
                    599:                }
                    600:                *with_mmap = 1;
                    601:                fb->sz = (size_t)st.st_size;
                    602:                fb->buf = mmap(NULL, fb->sz, PROT_READ,
                    603:                                MAP_FILE|MAP_SHARED, fd, 0);
                    604:                if (fb->buf != MAP_FAILED)
                    605:                        return(1);
                    606:        }
                    607:
                    608:        /*
                    609:         * If this isn't a regular file (like, say, stdin), then we must
                    610:         * go the old way and just read things in bit by bit.
                    611:         */
                    612:
                    613:        *with_mmap = 0;
                    614:        off = 0;
                    615:        fb->sz = 0;
                    616:        fb->buf = NULL;
                    617:        for (;;) {
                    618:                if (off == fb->sz) {
                    619:                        if (fb->sz == (1U << 31)) {
                    620:                                fprintf(stderr, "%s: input too large\n", file);
                    621:                                break;
                    622:                        }
                    623:                        resize_buf(fb, 65536);
                    624:                }
                    625:                ssz = read(fd, fb->buf + (int)off, fb->sz - off);
                    626:                if (ssz == 0) {
                    627:                        fb->sz = off;
                    628:                        return(1);
                    629:                }
                    630:                if (ssz == -1) {
                    631:                        perror(file);
                    632:                        break;
                    633:                }
                    634:                off += (size_t)ssz;
                    635:        }
                    636:
                    637:        free(fb->buf);
                    638:        fb->buf = NULL;
                    639:        return(0);
                    640: }
                    641:
                    642: static void
                    643: mparse_end(struct mparse *curp)
                    644: {
                    645:
                    646:        if (MANDOCLEVEL_FATAL <= curp->file_status)
                    647:                return;
                    648:
                    649:        if (curp->mdoc && ! mdoc_endparse(curp->mdoc)) {
                    650:                assert(MANDOCLEVEL_FATAL <= curp->file_status);
                    651:                return;
                    652:        }
                    653:
                    654:        if (curp->man && ! man_endparse(curp->man)) {
                    655:                assert(MANDOCLEVEL_FATAL <= curp->file_status);
                    656:                return;
                    657:        }
                    658:
                    659:        if ( ! (curp->man || curp->mdoc)) {
                    660:                mandoc_msg(MANDOCERR_NOTMANUAL, curp, 1, 0, NULL);
                    661:                curp->file_status = MANDOCLEVEL_FATAL;
                    662:                return;
                    663:        }
                    664:
                    665:        roff_endparse(curp->roff);
                    666: }
                    667:
                    668: static void
                    669: mparse_readfd_r(struct mparse *curp, int fd, const char *file, int re)
                    670: {
                    671:        const char      *svfile;
                    672:
                    673:        if (-1 == fd)
                    674:                if (-1 == (fd = open(file, O_RDONLY, 0))) {
                    675:                        perror(file);
                    676:                        curp->file_status = MANDOCLEVEL_SYSERR;
                    677:                        return;
                    678:                }
                    679:
                    680:        svfile = curp->file;
                    681:        curp->file = file;
                    682:
                    683:        pdesc(curp, file, fd);
                    684:
                    685:        if (0 == re && MANDOCLEVEL_FATAL > curp->file_status)
                    686:                mparse_end(curp);
                    687:
                    688:        if (STDIN_FILENO != fd && -1 == close(fd))
                    689:                perror(file);
                    690:
                    691:        curp->file = svfile;
                    692: }
                    693:
                    694: enum mandoclevel
                    695: mparse_readfd(struct mparse *curp, int fd, const char *file)
                    696: {
                    697:
                    698:        mparse_readfd_r(curp, fd, file, 0);
                    699:        return(curp->file_status);
                    700: }
                    701:
                    702: struct mparse *
1.7       schwarze  703: mparse_alloc(enum mparset inttype, enum mandoclevel wlevel,
                    704:                mandocmsg mmsg, void *arg, char *defos)
1.1       schwarze  705: {
                    706:        struct mparse   *curp;
                    707:
                    708:        assert(wlevel <= MANDOCLEVEL_FATAL);
                    709:
                    710:        curp = mandoc_calloc(1, sizeof(struct mparse));
                    711:
                    712:        curp->wlevel = wlevel;
                    713:        curp->mmsg = mmsg;
                    714:        curp->arg = arg;
                    715:        curp->inttype = inttype;
1.7       schwarze  716:        curp->defos = defos;
1.1       schwarze  717:
1.8       schwarze  718:        curp->roff = roff_alloc(inttype, curp);
1.1       schwarze  719:        return(curp);
                    720: }
                    721:
                    722: void
                    723: mparse_reset(struct mparse *curp)
                    724: {
                    725:
                    726:        roff_reset(curp->roff);
                    727:
                    728:        if (curp->mdoc)
                    729:                mdoc_reset(curp->mdoc);
                    730:        if (curp->man)
                    731:                man_reset(curp->man);
1.4       schwarze  732:        if (curp->secondary)
                    733:                curp->secondary->sz = 0;
1.1       schwarze  734:
                    735:        curp->file_status = MANDOCLEVEL_OK;
                    736:        curp->mdoc = NULL;
                    737:        curp->man = NULL;
                    738: }
                    739:
                    740: void
                    741: mparse_free(struct mparse *curp)
                    742: {
                    743:
                    744:        if (curp->pmdoc)
                    745:                mdoc_free(curp->pmdoc);
                    746:        if (curp->pman)
                    747:                man_free(curp->pman);
                    748:        if (curp->roff)
                    749:                roff_free(curp->roff);
1.4       schwarze  750:        if (curp->secondary)
                    751:                free(curp->secondary->buf);
1.1       schwarze  752:
1.4       schwarze  753:        free(curp->secondary);
1.1       schwarze  754:        free(curp);
                    755: }
                    756:
                    757: void
                    758: mparse_result(struct mparse *curp, struct mdoc **mdoc, struct man **man)
                    759: {
                    760:
                    761:        if (mdoc)
                    762:                *mdoc = curp->mdoc;
                    763:        if (man)
                    764:                *man = curp->man;
                    765: }
                    766:
                    767: void
                    768: mandoc_vmsg(enum mandocerr t, struct mparse *m,
                    769:                int ln, int pos, const char *fmt, ...)
                    770: {
                    771:        char             buf[256];
                    772:        va_list          ap;
                    773:
                    774:        va_start(ap, fmt);
                    775:        vsnprintf(buf, sizeof(buf) - 1, fmt, ap);
                    776:        va_end(ap);
                    777:
                    778:        mandoc_msg(t, m, ln, pos, buf);
                    779: }
                    780:
                    781: void
                    782: mandoc_msg(enum mandocerr er, struct mparse *m,
                    783:                int ln, int col, const char *msg)
                    784: {
                    785:        enum mandoclevel level;
                    786:
                    787:        level = MANDOCLEVEL_FATAL;
                    788:        while (er < mandoclimits[level])
                    789:                level--;
                    790:
                    791:        if (level < m->wlevel)
                    792:                return;
                    793:
                    794:        if (m->mmsg)
                    795:                (*m->mmsg)(er, level, m->file, ln, col, msg);
                    796:
                    797:        if (m->file_status < level)
                    798:                m->file_status = level;
                    799: }
                    800:
                    801: const char *
                    802: mparse_strerror(enum mandocerr er)
                    803: {
                    804:
                    805:        return(mandocerrs[er]);
                    806: }
                    807:
                    808: const char *
                    809: mparse_strlevel(enum mandoclevel lvl)
                    810: {
                    811:        return(mandoclevels[lvl]);
1.4       schwarze  812: }
                    813:
                    814: void
                    815: mparse_keep(struct mparse *p)
                    816: {
                    817:
                    818:        assert(NULL == p->secondary);
                    819:        p->secondary = mandoc_calloc(1, sizeof(struct buf));
                    820: }
                    821:
                    822: const char *
                    823: mparse_getkeep(const struct mparse *p)
                    824: {
                    825:
                    826:        assert(p->secondary);
                    827:        return(p->secondary->sz ? p->secondary->buf : NULL);
1.1       schwarze  828: }