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

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