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

Annotation of mandoc/read.c, Revision 1.18

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

CVSweb