[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.57

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