[BACK]Return to read.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/read.c, Revision 1.51

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