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

Annotation of mandoc/read.c, Revision 1.19

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

CVSweb