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

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