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

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