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

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