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

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