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

Annotation of mandoc/read.c, Revision 1.41

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

CVSweb