[BACK]Return to read.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/read.c, Revision 1.74

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