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

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