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

Annotation of mandoc/read.c, Revision 1.83

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

CVSweb