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

Annotation of mandoc/read.c, Revision 1.46

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

CVSweb