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

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