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

Annotation of mandoc/read.c, Revision 1.36

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

CVSweb