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

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