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

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