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

Annotation of mandoc/read.c, Revision 1.21

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

CVSweb