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

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