[BACK]Return to read.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/read.c, Revision 1.87

1.87    ! schwarze    1: /*     $Id: read.c,v 1.86 2014/09/07 23:25:01 schwarze Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.40      schwarze    4:  * Copyright (c) 2010-2014 Ingo Schwarze <schwarze@openbsd.org>
1.42      schwarze    5:  * Copyright (c) 2010, 2012 Joerg Sonnenberger <joerg@netbsd.org>
1.1       kristaps    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.11      kristaps   19: #include "config.h"
                     20:
1.80      schwarze   21: #include <sys/types.h>
1.81      schwarze   22: #if HAVE_MMAP
1.82      schwarze   23: #include <sys/mman.h>
1.80      schwarze   24: #include <sys/stat.h>
1.15      kristaps   25: #endif
1.82      schwarze   26: #include <sys/wait.h>
1.1       kristaps   27:
                     28: #include <assert.h>
                     29: #include <ctype.h>
1.40      schwarze   30: #include <errno.h>
1.1       kristaps   31: #include <fcntl.h>
1.3       kristaps   32: #include <stdarg.h>
1.28      joerg      33: #include <stdint.h>
1.1       kristaps   34: #include <stdio.h>
                     35: #include <stdlib.h>
                     36: #include <string.h>
                     37: #include <unistd.h>
                     38:
                     39: #include "mandoc.h"
1.46      schwarze   40: #include "mandoc_aux.h"
1.3       kristaps   41: #include "libmandoc.h"
1.1       kristaps   42: #include "mdoc.h"
                     43: #include "man.h"
1.28      joerg      44: #include "main.h"
1.1       kristaps   45:
                     46: #define        REPARSE_LIMIT   1000
                     47:
                     48: struct buf {
1.47      schwarze   49:        char             *buf; /* binary input buffer */
1.1       kristaps   50:        size_t            sz; /* size of binary buffer */
                     51: };
                     52:
                     53: struct mparse {
                     54:        struct man       *pman; /* persistent man parser */
                     55:        struct mdoc      *pmdoc; /* persistent mdoc parser */
                     56:        struct man       *man; /* man parser */
                     57:        struct mdoc      *mdoc; /* mdoc parser */
                     58:        struct roff      *roff; /* roff parser (!NULL) */
1.45      schwarze   59:        char             *sodest; /* filename pointed to by .so */
1.83      schwarze   60:        const char       *file; /* filename of current input file */
                     61:        struct buf       *primary; /* buffer currently being parsed */
                     62:        struct buf       *secondary; /* preprocessed copy of input */
                     63:        const char       *defos; /* default operating system */
                     64:        mandocmsg         mmsg; /* warning/error message handler */
                     65:        enum mandoclevel  file_status; /* status of current parse */
                     66:        enum mandoclevel  wlevel; /* ignore messages below this */
                     67:        int               options; /* parser options */
1.1       kristaps   68:        int               reparse_count; /* finite interp. stack */
1.83      schwarze   69:        int               line; /* line number in the file */
1.1       kristaps   70: };
                     71:
1.84      schwarze   72: static void      choose_parser(struct mparse *);
1.1       kristaps   73: static void      resize_buf(struct buf *, size_t);
                     74: static void      mparse_buf_r(struct mparse *, struct buf, int);
1.40      schwarze   75: static int       read_whole_file(struct mparse *, const char *, int,
                     76:                                struct buf *, int *);
1.1       kristaps   77: static void      mparse_end(struct mparse *);
1.37      schwarze   78: static void      mparse_parse_buffer(struct mparse *, struct buf,
                     79:                        const char *);
1.1       kristaps   80:
1.3       kristaps   81: static const enum mandocerr    mandoclimits[MANDOCLEVEL_MAX] = {
                     82:        MANDOCERR_OK,
                     83:        MANDOCERR_WARNING,
                     84:        MANDOCERR_WARNING,
                     85:        MANDOCERR_ERROR,
                     86:        MANDOCERR_FATAL,
                     87:        MANDOCERR_MAX,
                     88:        MANDOCERR_MAX
                     89: };
                     90:
1.7       kristaps   91: static const char * const      mandocerrs[MANDOCERR_MAX] = {
                     92:        "ok",
                     93:
                     94:        "generic warning",
                     95:
                     96:        /* related to the prologue */
1.79      schwarze   97:        "missing manual title, using UNTITLED",
                     98:        "missing manual title, using \"\"",
1.54      schwarze   99:        "lower case character in document title",
1.79      schwarze  100:        "missing manual section, using \"\"",
1.7       kristaps  101:        "unknown manual section",
1.32      schwarze  102:        "unknown manual volume or arch",
1.54      schwarze  103:        "missing date, using today's date",
1.7       kristaps  104:        "cannot parse date, using it verbatim",
1.79      schwarze  105:        "missing Os macro, using \"\"",
                    106:        "duplicate prologue macro",
                    107:        "late prologue macro",
                    108:        "skipping late title macro",
1.7       kristaps  109:        "prologue macros out of order",
                    110:
                    111:        /* related to document structure */
                    112:        ".so is fragile, better use ln(1)",
1.50      schwarze  113:        "no document body",
1.54      schwarze  114:        "content before first section header",
                    115:        "first section is not \"NAME\"",
1.7       kristaps  116:        "bad NAME section contents",
                    117:        "sections out of conventional order",
1.54      schwarze  118:        "duplicate section title",
                    119:        "unexpected section",
1.87    ! schwarze  120:        "unusual Xr order",
        !           121:        "unusual Xr punctuation",
1.86      schwarze  122:        "AUTHORS section without An macro",
1.7       kristaps  123:
                    124:        /* related to macros and nesting */
1.55      schwarze  125:        "obsolete macro",
1.7       kristaps  126:        "skipping paragraph macro",
1.31      schwarze  127:        "moving paragraph macro out of list",
1.7       kristaps  128:        "skipping no-space macro",
                    129:        "blocks badly nested",
                    130:        "nested displays are not portable",
1.57      schwarze  131:        "moving content out of list",
                    132:        ".Vt block has child macro",
1.78      schwarze  133:        "fill mode already enabled, skipping",
                    134:        "fill mode already disabled, skipping",
1.7       kristaps  135:        "line scope broken",
                    136:
                    137:        /* related to missing macro arguments */
1.58      schwarze  138:        "skipping empty request",
                    139:        "conditional request controls empty scope",
1.7       kristaps  140:        "skipping empty macro",
1.62      schwarze  141:        "empty argument, using 0n",
1.7       kristaps  142:        "argument count wrong",
1.60      schwarze  143:        "missing display type, using -ragged",
                    144:        "list type is not the first argument",
                    145:        "missing -width in -tag list, using 8n",
1.78      schwarze  146:        "missing utility name, using \"\"",
1.60      schwarze  147:        "empty head in list item",
                    148:        "empty list item",
1.61      schwarze  149:        "missing font type, using \\fR",
                    150:        "unknown font type, using \\fR",
1.60      schwarze  151:        "missing -std argument, adding it",
1.7       kristaps  152:
                    153:        /* related to bad macro arguments */
1.64      schwarze  154:        "unterminated quoted argument",
1.7       kristaps  155:        "duplicate argument",
1.76      schwarze  156:        "skipping duplicate argument",
1.63      schwarze  157:        "skipping duplicate display type",
                    158:        "skipping duplicate list type",
1.76      schwarze  159:        "skipping -width argument",
1.7       kristaps  160:        "unknown AT&T UNIX version",
1.67      schwarze  161:        "invalid content in Rs block",
1.63      schwarze  162:        "invalid Boolean argument",
                    163:        "unknown font, skipping request",
1.7       kristaps  164:
                    165:        /* related to plain text */
1.64      schwarze  166:        "blank line in fill mode, using .sp",
                    167:        "tab in filled text",
                    168:        "whitespace at end of input line",
1.7       kristaps  169:        "bad comment style",
1.64      schwarze  170:        "invalid escape sequence",
                    171:        "undefined string, using \"\"",
1.16      kristaps  172:
1.7       kristaps  173:        "generic error",
1.17      kristaps  174:
                    175:        /* related to equations */
1.20      kristaps  176:        "unexpected equation scope closure",
                    177:        "equation scope open on exit",
1.21      kristaps  178:        "overlapping equation scopes",
                    179:        "unexpected end of equation",
                    180:        "equation syntax error",
1.7       kristaps  181:
                    182:        /* related to tables */
                    183:        "bad table syntax",
                    184:        "bad table option",
                    185:        "bad table layout",
                    186:        "no table layout cells specified",
                    187:        "no table data cells specified",
                    188:        "ignore data in cell",
                    189:        "data block still open",
                    190:        "ignoring extra data cells",
                    191:
1.68      schwarze  192:        /* related to document structure and macros */
1.7       kristaps  193:        "input stack limit exceeded, infinite loop?",
                    194:        "skipping bad character",
1.68      schwarze  195:        "skipping unknown macro",
1.73      schwarze  196:        "skipping item outside list",
1.68      schwarze  197:        "skipping column outside column list",
                    198:        "skipping end of block that is not open",
                    199:        "inserting missing end of block",
                    200:        "appending missing end of block",
                    201:
                    202:        /* related to request and macro arguments */
1.7       kristaps  203:        "escaped character not allowed in a name",
                    204:        "argument count wrong",
1.71      schwarze  205:        "missing list type, using -item",
1.70      schwarze  206:        "missing manual name, using \"\"",
1.71      schwarze  207:        "uname(3) system call failed, using UNKNOWN",
1.63      schwarze  208:        "unknown standard specifier",
1.71      schwarze  209:        "skipping request without numeric argument",
1.61      schwarze  210:        "skipping all arguments",
                    211:        "skipping excess arguments",
1.7       kristaps  212:
                    213:        "generic fatal error",
                    214:
1.40      schwarze  215:        "input too large",
1.78      schwarze  216:        "NOT IMPLEMENTED: Bd -file",
1.7       kristaps  217:        "NOT IMPLEMENTED: .so with absolute path or \"..\"",
1.52      schwarze  218:        ".so request failed",
1.40      schwarze  219:
                    220:        /* system errors */
1.82      schwarze  221:        "cannot dup file descriptor",
                    222:        "cannot exec",
                    223:        "gunzip failed with code",
                    224:        "cannot fork",
1.51      schwarze  225:        NULL,
1.82      schwarze  226:        "cannot open pipe",
                    227:        "cannot read file",
                    228:        "gunzip died from signal",
1.40      schwarze  229:        "cannot stat file",
1.82      schwarze  230:        "wait failed",
1.7       kristaps  231: };
                    232:
                    233: static const char * const      mandoclevels[MANDOCLEVEL_MAX] = {
                    234:        "SUCCESS",
                    235:        "RESERVED",
                    236:        "WARNING",
                    237:        "ERROR",
                    238:        "FATAL",
                    239:        "BADARG",
                    240:        "SYSERR"
                    241: };
                    242:
1.47      schwarze  243:
1.1       kristaps  244: static void
                    245: resize_buf(struct buf *buf, size_t initial)
                    246: {
                    247:
                    248:        buf->sz = buf->sz > initial/2 ? 2 * buf->sz : initial;
                    249:        buf->buf = mandoc_realloc(buf->buf, buf->sz);
                    250: }
                    251:
                    252: static void
1.84      schwarze  253: choose_parser(struct mparse *curp)
1.1       kristaps  254: {
1.83      schwarze  255:        char            *cp, *ep;
                    256:        int              format;
1.1       kristaps  257:
1.83      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) {
1.85      schwarze  268:                        if (*cp == '.' || *cp == '\'') {
1.83      schwarze  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       kristaps  284:        }
                    285:
1.83      schwarze  286:        if (format == MPARSE_MDOC) {
1.47      schwarze  287:                if (NULL == curp->pmdoc)
1.44      schwarze  288:                        curp->pmdoc = mdoc_alloc(
                    289:                            curp->roff, curp, curp->defos,
                    290:                            MPARSE_QUICK & curp->options ? 1 : 0);
1.1       kristaps  291:                assert(curp->pmdoc);
                    292:                curp->mdoc = curp->pmdoc;
                    293:                return;
1.47      schwarze  294:        }
1.1       kristaps  295:
1.83      schwarze  296:        /* Fall back to man(7) as a last resort. */
                    297:
1.47      schwarze  298:        if (NULL == curp->pman)
1.44      schwarze  299:                curp->pman = man_alloc(curp->roff, curp,
                    300:                    MPARSE_QUICK & curp->options ? 1 : 0);
1.1       kristaps  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.47      schwarze  323:        lnn = curp->line;
                    324:        pos = 0;
1.1       kristaps  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.35      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.47      schwarze  361:                        /*
1.1       kristaps  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.27      joerg     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       kristaps  368:                         */
                    369:
                    370:                        c = (unsigned char) blk.buf[i];
                    371:
1.47      schwarze  372:                        if ( ! (isascii(c) &&
                    373:                            (isgraph(c) || isblank(c)))) {
1.78      schwarze  374:                                mandoc_vmsg(MANDOCERR_BADCHAR, curp,
                    375:                                    curp->line, pos, "0x%x", c);
1.1       kristaps  376:                                i++;
1.27      joerg     377:                                ln.buf[pos++] = '?';
1.1       kristaps  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:
1.13      kristaps  404:                        if ('"' == blk.buf[i + 1] || '#' == blk.buf[i + 1]) {
1.1       kristaps  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.35      schwarze  425:                        /* Catch escaped bogus characters. */
                    426:
                    427:                        c = (unsigned char) blk.buf[i+1];
                    428:
1.47      schwarze  429:                        if ( ! (isascii(c) &&
                    430:                            (isgraph(c) || isblank(c)))) {
1.78      schwarze  431:                                mandoc_vmsg(MANDOCERR_BADCHAR, curp,
                    432:                                    curp->line, pos, "0x%x", c);
1.35      schwarze  433:                                i += 2;
                    434:                                ln.buf[pos++] = '?';
                    435:                                continue;
                    436:                        }
                    437:
1.1       kristaps  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.47      schwarze  444:                if (pos >= (int)ln.sz)
1.1       kristaps  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.24      kristaps  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.47      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.24      kristaps  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       kristaps  480: rerun:
1.47      schwarze  481:                rr = roff_parseln(curp->roff, curp->line,
                    482:                    &ln.buf, &ln.sz, of, &of);
1.1       kristaps  483:
                    484:                switch (rr) {
1.47      schwarze  485:                case ROFF_REPARSE:
1.1       kristaps  486:                        if (REPARSE_LIMIT >= ++curp->reparse_count)
                    487:                                mparse_buf_r(curp, ln, 0);
                    488:                        else
1.3       kristaps  489:                                mandoc_msg(MANDOCERR_ROFFLOOP, curp,
1.47      schwarze  490:                                    curp->line, pos, NULL);
1.1       kristaps  491:                        pos = 0;
                    492:                        continue;
1.47      schwarze  493:                case ROFF_APPEND:
1.1       kristaps  494:                        pos = (int)strlen(ln.buf);
                    495:                        continue;
1.47      schwarze  496:                case ROFF_RERUN:
1.1       kristaps  497:                        goto rerun;
1.47      schwarze  498:                case ROFF_IGN:
1.1       kristaps  499:                        pos = 0;
                    500:                        continue;
1.47      schwarze  501:                case ROFF_ERR:
1.1       kristaps  502:                        assert(MANDOCLEVEL_FATAL <= curp->file_status);
                    503:                        break;
1.47      schwarze  504:                case ROFF_SO:
1.45      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.24      kristaps  511:                        /*
                    512:                         * We remove `so' clauses from our lookaside
                    513:                         * buffer because we're going to descend into
                    514:                         * the file recursively.
                    515:                         */
1.47      schwarze  516:                        if (curp->secondary)
1.25      kristaps  517:                                curp->secondary->sz -= pos + 1;
1.36      schwarze  518:                        mparse_readfd(curp, -1, ln.buf + of);
1.52      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       kristaps  523:                                break;
1.52      schwarze  524:                        }
1.1       kristaps  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.14      kristaps  541:                 * We keep these instanced between parsers, but set them
1.1       kristaps  542:                 * locally per parse routine since we can use different
                    543:                 * parsers with each one.
                    544:                 */
                    545:
                    546:                if ( ! (curp->man || curp->mdoc))
1.84      schwarze  547:                        choose_parser(curp);
1.1       kristaps  548:
1.47      schwarze  549:                /*
1.84      schwarze  550:                 * Lastly, push down into the parsers themselves.
1.1       kristaps  551:                 * If libroff returns ROFF_TBL, then add it to the
                    552:                 * currently open parse.  Since we only get here if
                    553:                 * there does exist data (see tbl_data.c), we're
                    554:                 * guaranteed that something's been allocated.
                    555:                 * Do the same for ROFF_EQN.
                    556:                 */
                    557:
                    558:                rc = -1;
                    559:
                    560:                if (ROFF_TBL == rr)
                    561:                        while (NULL != (span = roff_span(curp->roff))) {
                    562:                                rc = curp->man ?
1.47      schwarze  563:                                    man_addspan(curp->man, span) :
                    564:                                    mdoc_addspan(curp->mdoc, span);
1.1       kristaps  565:                                if (0 == rc)
                    566:                                        break;
                    567:                        }
                    568:                else if (ROFF_EQN == rr)
1.47      schwarze  569:                        rc = curp->mdoc ?
                    570:                            mdoc_addeqn(curp->mdoc,
                    571:                                roff_eqn(curp->roff)) :
                    572:                            man_addeqn(curp->man,
                    573:                                roff_eqn(curp->roff));
1.1       kristaps  574:                else if (curp->man || curp->mdoc)
                    575:                        rc = curp->man ?
1.47      schwarze  576:                            man_parseln(curp->man,
                    577:                                curp->line, ln.buf, of) :
                    578:                            mdoc_parseln(curp->mdoc,
                    579:                                curp->line, ln.buf, of);
1.1       kristaps  580:
                    581:                if (0 == rc) {
                    582:                        assert(MANDOCLEVEL_FATAL <= curp->file_status);
                    583:                        break;
1.41      schwarze  584:                } else if (2 == rc)
                    585:                        break;
1.1       kristaps  586:
                    587:                /* Temporary buffers typically are not full. */
                    588:
                    589:                if (0 == start && '\0' == blk.buf[i])
                    590:                        break;
                    591:
                    592:                /* Start the next input line. */
                    593:
                    594:                pos = 0;
                    595:        }
                    596:
                    597:        free(ln.buf);
                    598: }
                    599:
                    600: static int
1.40      schwarze  601: read_whole_file(struct mparse *curp, const char *file, int fd,
                    602:                struct buf *fb, int *with_mmap)
1.1       kristaps  603: {
                    604:        size_t           off;
                    605:        ssize_t          ssz;
                    606:
1.81      schwarze  607: #if HAVE_MMAP
1.15      kristaps  608:        struct stat      st;
1.1       kristaps  609:        if (-1 == fstat(fd, &st)) {
1.40      schwarze  610:                curp->file_status = MANDOCLEVEL_SYSERR;
                    611:                if (curp->mmsg)
                    612:                        (*curp->mmsg)(MANDOCERR_SYSSTAT, curp->file_status,
                    613:                            file, 0, 0, strerror(errno));
1.1       kristaps  614:                return(0);
                    615:        }
                    616:
                    617:        /*
                    618:         * If we're a regular file, try just reading in the whole entry
                    619:         * via mmap().  This is faster than reading it into blocks, and
                    620:         * since each file is only a few bytes to begin with, I'm not
                    621:         * concerned that this is going to tank any machines.
                    622:         */
                    623:
                    624:        if (S_ISREG(st.st_mode)) {
                    625:                if (st.st_size >= (1U << 31)) {
1.40      schwarze  626:                        curp->file_status = MANDOCLEVEL_FATAL;
                    627:                        if (curp->mmsg)
                    628:                                (*curp->mmsg)(MANDOCERR_TOOLARGE,
                    629:                                    curp->file_status, file, 0, 0, NULL);
1.1       kristaps  630:                        return(0);
                    631:                }
                    632:                *with_mmap = 1;
                    633:                fb->sz = (size_t)st.st_size;
1.37      schwarze  634:                fb->buf = mmap(NULL, fb->sz, PROT_READ, MAP_SHARED, fd, 0);
1.1       kristaps  635:                if (fb->buf != MAP_FAILED)
                    636:                        return(1);
                    637:        }
1.15      kristaps  638: #endif
1.1       kristaps  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.40      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       kristaps  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.40      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       kristaps  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.72      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       kristaps  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:
                    715: static void
1.36      schwarze  716: mparse_parse_buffer(struct mparse *curp, struct buf blk, const char *file)
1.28      joerg     717: {
1.85      schwarze  718:        struct buf      *svprimary;
1.28      joerg     719:        const char      *svfile;
1.36      schwarze  720:        static int       recursion_depth;
                    721:
                    722:        if (64 < recursion_depth) {
                    723:                mandoc_msg(MANDOCERR_ROFFLOOP, curp, curp->line, 0, NULL);
                    724:                return;
                    725:        }
1.28      joerg     726:
                    727:        /* Line number is per-file. */
                    728:        svfile = curp->file;
                    729:        curp->file = file;
1.85      schwarze  730:        svprimary = curp->primary;
1.83      schwarze  731:        curp->primary = &blk;
1.28      joerg     732:        curp->line = 1;
1.36      schwarze  733:        recursion_depth++;
1.28      joerg     734:
                    735:        mparse_buf_r(curp, blk, 1);
                    736:
1.36      schwarze  737:        if (0 == --recursion_depth && MANDOCLEVEL_FATAL > curp->file_status)
1.28      joerg     738:                mparse_end(curp);
                    739:
1.85      schwarze  740:        curp->primary = svprimary;
1.28      joerg     741:        curp->file = svfile;
                    742: }
                    743:
                    744: enum mandoclevel
                    745: mparse_readmem(struct mparse *curp, const void *buf, size_t len,
                    746:                const char *file)
                    747: {
                    748:        struct buf blk;
                    749:
                    750:        blk.buf = UNCONST(buf);
                    751:        blk.sz = len;
                    752:
1.36      schwarze  753:        mparse_parse_buffer(curp, blk, file);
1.28      joerg     754:        return(curp->file_status);
                    755: }
                    756:
1.36      schwarze  757: enum mandoclevel
                    758: mparse_readfd(struct mparse *curp, int fd, const char *file)
1.1       kristaps  759: {
1.28      joerg     760:        struct buf       blk;
                    761:        int              with_mmap;
1.1       kristaps  762:
1.40      schwarze  763:        if (-1 == fd && -1 == (fd = open(file, O_RDONLY, 0))) {
                    764:                curp->file_status = MANDOCLEVEL_SYSERR;
                    765:                if (curp->mmsg)
                    766:                        (*curp->mmsg)(MANDOCERR_SYSOPEN,
                    767:                            curp->file_status,
                    768:                            file, 0, 0, strerror(errno));
                    769:                goto out;
                    770:        }
                    771:
1.28      joerg     772:        /*
                    773:         * Run for each opened file; may be called more than once for
                    774:         * each full parse sequence if the opened file is nested (i.e.,
                    775:         * from `so').  Simply sucks in the whole file and moves into
                    776:         * the parse phase for the file.
                    777:         */
1.1       kristaps  778:
1.40      schwarze  779:        if ( ! read_whole_file(curp, file, fd, &blk, &with_mmap))
1.36      schwarze  780:                goto out;
1.1       kristaps  781:
1.36      schwarze  782:        mparse_parse_buffer(curp, blk, file);
1.1       kristaps  783:
1.81      schwarze  784: #if HAVE_MMAP
1.28      joerg     785:        if (with_mmap)
                    786:                munmap(blk.buf, blk.sz);
                    787:        else
                    788: #endif
                    789:                free(blk.buf);
1.1       kristaps  790:
                    791:        if (STDIN_FILENO != fd && -1 == close(fd))
                    792:                perror(file);
1.36      schwarze  793: out:
1.1       kristaps  794:        return(curp->file_status);
1.82      schwarze  795: }
                    796:
                    797: enum mandoclevel
                    798: mparse_open(struct mparse *curp, int *fd, const char *file,
                    799:        pid_t *child_pid)
                    800: {
                    801:        int               pfd[2];
                    802:        char             *cp;
                    803:        enum mandocerr    err;
                    804:
                    805:        pfd[1] = -1;
                    806:        curp->file = file;
                    807:        if ((cp = strrchr(file, '.')) == NULL ||
                    808:            strcmp(cp + 1, "gz")) {
                    809:                *child_pid = 0;
                    810:                if ((*fd = open(file, O_RDONLY)) == -1) {
                    811:                        err = MANDOCERR_SYSOPEN;
                    812:                        goto out;
                    813:                }
                    814:                return(MANDOCLEVEL_OK);
                    815:        }
                    816:
                    817:        if (pipe(pfd) == -1) {
                    818:                err = MANDOCERR_SYSPIPE;
                    819:                goto out;
                    820:        }
                    821:
                    822:        switch (*child_pid = fork()) {
                    823:        case -1:
                    824:                err = MANDOCERR_SYSFORK;
                    825:                close(pfd[0]);
                    826:                close(pfd[1]);
                    827:                pfd[1] = -1;
                    828:                break;
                    829:        case 0:
                    830:                close(pfd[0]);
                    831:                if (dup2(pfd[1], STDOUT_FILENO) == -1) {
                    832:                        err = MANDOCERR_SYSDUP;
                    833:                        break;
                    834:                }
                    835:                execlp("gunzip", "gunzip", "-c", file, NULL);
                    836:                err = MANDOCERR_SYSEXEC;
                    837:                break;
                    838:        default:
                    839:                close(pfd[1]);
                    840:                *fd = pfd[0];
                    841:                return(MANDOCLEVEL_OK);
                    842:        }
                    843:
                    844: out:
                    845:        *fd = -1;
                    846:        *child_pid = 0;
                    847:        curp->file_status = MANDOCLEVEL_SYSERR;
                    848:        if (curp->mmsg)
                    849:                (*curp->mmsg)(err, curp->file_status, file,
                    850:                    0, 0, strerror(errno));
                    851:        if (pfd[1] != -1)
                    852:                exit(1);
                    853:        return(curp->file_status);
                    854: }
                    855:
                    856: enum mandoclevel
                    857: mparse_wait(struct mparse *curp, pid_t child_pid)
                    858: {
                    859:        int       status;
                    860:
                    861:        if (waitpid(child_pid, &status, 0) == -1) {
                    862:                mandoc_msg(MANDOCERR_SYSWAIT, curp, 0, 0,
                    863:                    strerror(errno));
                    864:                curp->file_status = MANDOCLEVEL_SYSERR;
                    865:                return(curp->file_status);
                    866:        }
                    867:        if (WIFSIGNALED(status)) {
                    868:                mandoc_vmsg(MANDOCERR_SYSSIG, curp, 0, 0,
                    869:                    "%d", WTERMSIG(status));
                    870:                curp->file_status = MANDOCLEVEL_SYSERR;
                    871:                return(curp->file_status);
                    872:        }
                    873:        if (WEXITSTATUS(status)) {
                    874:                mandoc_vmsg(MANDOCERR_SYSEXIT, curp, 0, 0,
                    875:                    "%d", WEXITSTATUS(status));
                    876:                curp->file_status = MANDOCLEVEL_SYSERR;
                    877:                return(curp->file_status);
                    878:        }
                    879:        return(MANDOCLEVEL_OK);
1.1       kristaps  880: }
                    881:
                    882: struct mparse *
1.44      schwarze  883: mparse_alloc(int options, enum mandoclevel wlevel,
1.69      schwarze  884:                mandocmsg mmsg, const char *defos)
1.1       kristaps  885: {
                    886:        struct mparse   *curp;
1.10      kristaps  887:
                    888:        assert(wlevel <= MANDOCLEVEL_FATAL);
1.1       kristaps  889:
                    890:        curp = mandoc_calloc(1, sizeof(struct mparse));
                    891:
1.44      schwarze  892:        curp->options = options;
1.3       kristaps  893:        curp->wlevel = wlevel;
1.1       kristaps  894:        curp->mmsg = mmsg;
1.29      schwarze  895:        curp->defos = defos;
1.1       kristaps  896:
1.44      schwarze  897:        curp->roff = roff_alloc(curp, options);
1.72      schwarze  898:        if (curp->options & MPARSE_MDOC)
                    899:                curp->pmdoc = mdoc_alloc(
                    900:                    curp->roff, curp, curp->defos,
                    901:                    curp->options & MPARSE_QUICK ? 1 : 0);
                    902:        if (curp->options & MPARSE_MAN)
                    903:                curp->pman = man_alloc(curp->roff, curp,
                    904:                    curp->options & MPARSE_QUICK ? 1 : 0);
                    905:
1.1       kristaps  906:        return(curp);
                    907: }
                    908:
                    909: void
                    910: mparse_reset(struct mparse *curp)
                    911: {
                    912:
                    913:        roff_reset(curp->roff);
                    914:
                    915:        if (curp->mdoc)
                    916:                mdoc_reset(curp->mdoc);
                    917:        if (curp->man)
                    918:                man_reset(curp->man);
1.24      kristaps  919:        if (curp->secondary)
                    920:                curp->secondary->sz = 0;
1.1       kristaps  921:
                    922:        curp->file_status = MANDOCLEVEL_OK;
                    923:        curp->mdoc = NULL;
                    924:        curp->man = NULL;
1.45      schwarze  925:
                    926:        free(curp->sodest);
                    927:        curp->sodest = NULL;
1.1       kristaps  928: }
                    929:
                    930: void
                    931: mparse_free(struct mparse *curp)
                    932: {
                    933:
                    934:        if (curp->pmdoc)
                    935:                mdoc_free(curp->pmdoc);
                    936:        if (curp->pman)
                    937:                man_free(curp->pman);
                    938:        if (curp->roff)
                    939:                roff_free(curp->roff);
1.24      kristaps  940:        if (curp->secondary)
                    941:                free(curp->secondary->buf);
1.1       kristaps  942:
1.24      kristaps  943:        free(curp->secondary);
1.45      schwarze  944:        free(curp->sodest);
1.1       kristaps  945:        free(curp);
                    946: }
                    947:
                    948: void
1.45      schwarze  949: mparse_result(struct mparse *curp,
                    950:        struct mdoc **mdoc, struct man **man, char **sodest)
1.1       kristaps  951: {
                    952:
1.45      schwarze  953:        if (sodest && NULL != (*sodest = curp->sodest)) {
                    954:                *mdoc = NULL;
                    955:                *man = NULL;
                    956:                return;
                    957:        }
1.9       kristaps  958:        if (mdoc)
                    959:                *mdoc = curp->mdoc;
                    960:        if (man)
                    961:                *man = curp->man;
1.3       kristaps  962: }
                    963:
                    964: void
                    965: mandoc_vmsg(enum mandocerr t, struct mparse *m,
                    966:                int ln, int pos, const char *fmt, ...)
                    967: {
                    968:        char             buf[256];
                    969:        va_list          ap;
                    970:
                    971:        va_start(ap, fmt);
1.48      schwarze  972:        (void)vsnprintf(buf, sizeof(buf), fmt, ap);
1.3       kristaps  973:        va_end(ap);
                    974:
                    975:        mandoc_msg(t, m, ln, pos, buf);
                    976: }
                    977:
                    978: void
1.47      schwarze  979: mandoc_msg(enum mandocerr er, struct mparse *m,
1.3       kristaps  980:                int ln, int col, const char *msg)
                    981: {
                    982:        enum mandoclevel level;
                    983:
                    984:        level = MANDOCLEVEL_FATAL;
                    985:        while (er < mandoclimits[level])
                    986:                level--;
                    987:
                    988:        if (level < m->wlevel)
                    989:                return;
                    990:
1.8       kristaps  991:        if (m->mmsg)
                    992:                (*m->mmsg)(er, level, m->file, ln, col, msg);
1.3       kristaps  993:
                    994:        if (m->file_status < level)
                    995:                m->file_status = level;
1.7       kristaps  996: }
                    997:
                    998: const char *
                    999: mparse_strerror(enum mandocerr er)
                   1000: {
                   1001:
                   1002:        return(mandocerrs[er]);
                   1003: }
                   1004:
                   1005: const char *
                   1006: mparse_strlevel(enum mandoclevel lvl)
                   1007: {
                   1008:        return(mandoclevels[lvl]);
1.24      kristaps 1009: }
                   1010:
                   1011: void
                   1012: mparse_keep(struct mparse *p)
                   1013: {
                   1014:
                   1015:        assert(NULL == p->secondary);
                   1016:        p->secondary = mandoc_calloc(1, sizeof(struct buf));
                   1017: }
                   1018:
                   1019: const char *
                   1020: mparse_getkeep(const struct mparse *p)
                   1021: {
                   1022:
                   1023:        assert(p->secondary);
                   1024:        return(p->secondary->sz ? p->secondary->buf : NULL);
1.1       kristaps 1025: }

CVSweb