[BACK]Return to read.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/read.c, Revision 1.20

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

CVSweb