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

Annotation of mandoc/read.c, Revision 1.180

1.180   ! schwarze    1: /*     $Id: read.c,v 1.179 2017/06/24 14:38:33 schwarze Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.156     schwarze    4:  * Copyright (c) 2010-2017 Ingo Schwarze <schwarze@openbsd.org>
1.42      schwarze    5:  * Copyright (c) 2010, 2012 Joerg Sonnenberger <joerg@netbsd.org>
1.1       kristaps    6:  *
                      7:  * Permission to use, copy, modify, and distribute this software for any
                      8:  * purpose with or without fee is hereby granted, provided that the above
                      9:  * copyright notice and this permission notice appear in all copies.
                     10:  *
1.133     schwarze   11:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL WARRANTIES
1.1       kristaps   12:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
1.133     schwarze   13:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR
1.1       kristaps   14:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     15:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     16:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     17:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     18:  */
1.11      kristaps   19: #include "config.h"
                     20:
1.80      schwarze   21: #include <sys/types.h>
1.82      schwarze   22: #include <sys/mman.h>
1.80      schwarze   23: #include <sys/stat.h>
1.1       kristaps   24:
                     25: #include <assert.h>
                     26: #include <ctype.h>
1.146     schwarze   27: #if HAVE_ERR
1.143     schwarze   28: #include <err.h>
1.146     schwarze   29: #endif
1.40      schwarze   30: #include <errno.h>
1.1       kristaps   31: #include <fcntl.h>
1.3       kristaps   32: #include <stdarg.h>
1.1       kristaps   33: #include <stdio.h>
                     34: #include <stdlib.h>
                     35: #include <string.h>
                     36: #include <unistd.h>
1.140     schwarze   37: #include <zlib.h>
1.1       kristaps   38:
1.133     schwarze   39: #include "mandoc_aux.h"
1.1       kristaps   40: #include "mandoc.h"
1.133     schwarze   41: #include "roff.h"
1.1       kristaps   42: #include "mdoc.h"
                     43: #include "man.h"
1.133     schwarze   44: #include "libmandoc.h"
1.139     schwarze   45: #include "roff_int.h"
1.1       kristaps   46:
                     47: #define        REPARSE_LIMIT   1000
                     48:
                     49: struct mparse {
1.160     schwarze   50:        struct roff      *roff; /* roff parser (!NULL) */
1.134     schwarze   51:        struct roff_man  *man; /* man parser */
1.45      schwarze   52:        char             *sodest; /* filename pointed to by .so */
1.83      schwarze   53:        const char       *file; /* filename of current input file */
                     54:        struct buf       *primary; /* buffer currently being parsed */
                     55:        struct buf       *secondary; /* preprocessed copy of input */
1.179     schwarze   56:        const char       *os_s; /* default operating system */
1.83      schwarze   57:        mandocmsg         mmsg; /* warning/error message handler */
                     58:        enum mandoclevel  file_status; /* status of current parse */
1.179     schwarze   59:        enum mandocerr    mmin; /* ignore messages below this */
1.83      schwarze   60:        int               options; /* parser options */
1.140     schwarze   61:        int               gzip; /* current input file is gzipped */
1.93      schwarze   62:        int               filenc; /* encoding of the current file */
1.1       kristaps   63:        int               reparse_count; /* finite interp. stack */
1.83      schwarze   64:        int               line; /* line number in the file */
1.1       kristaps   65: };
                     66:
1.84      schwarze   67: static void      choose_parser(struct mparse *);
1.1       kristaps   68: static void      resize_buf(struct buf *, size_t);
1.163     schwarze   69: static int       mparse_buf_r(struct mparse *, struct buf, size_t, int);
1.40      schwarze   70: static int       read_whole_file(struct mparse *, const char *, int,
                     71:                                struct buf *, int *);
1.1       kristaps   72: static void      mparse_end(struct mparse *);
1.37      schwarze   73: static void      mparse_parse_buffer(struct mparse *, struct buf,
                     74:                        const char *);
1.1       kristaps   75:
1.3       kristaps   76: static const enum mandocerr    mandoclimits[MANDOCLEVEL_MAX] = {
                     77:        MANDOCERR_OK,
1.165     schwarze   78:        MANDOCERR_STYLE,
1.3       kristaps   79:        MANDOCERR_WARNING,
                     80:        MANDOCERR_ERROR,
1.112     schwarze   81:        MANDOCERR_UNSUPP,
1.3       kristaps   82:        MANDOCERR_MAX,
                     83:        MANDOCERR_MAX
                     84: };
                     85:
1.7       kristaps   86: static const char * const      mandocerrs[MANDOCERR_MAX] = {
                     87:        "ok",
                     88:
1.179     schwarze   89:        "base system convention",
1.166     schwarze   90:
1.176     schwarze   91:        "Mdocdate found",
                     92:        "Mdocdate missing",
1.180   ! schwarze   93:        "operating system explicitly specified",
1.179     schwarze   94:        "RCS id missing",
                     95:
                     96:        "generic style suggestion",
                     97:
1.176     schwarze   98:        "legacy man(7) date format",
1.178     schwarze   99:        "duplicate RCS id",
1.166     schwarze  100:        "useless macro",
1.167     schwarze  101:        "consider using OS macro",
1.172     schwarze  102:        "errnos out of order",
                    103:        "duplicate errno",
1.168     schwarze  104:        "description line ends with a full stop",
1.174     schwarze  105:        "no blank before trailing delimiter",
1.175     schwarze  106:        "function name without markup",
1.166     schwarze  107:
1.7       kristaps  108:        "generic warning",
                    109:
                    110:        /* related to the prologue */
1.79      schwarze  111:        "missing manual title, using UNTITLED",
                    112:        "missing manual title, using \"\"",
1.54      schwarze  113:        "lower case character in document title",
1.79      schwarze  114:        "missing manual section, using \"\"",
1.7       kristaps  115:        "unknown manual section",
1.54      schwarze  116:        "missing date, using today's date",
1.7       kristaps  117:        "cannot parse date, using it verbatim",
1.79      schwarze  118:        "missing Os macro, using \"\"",
                    119:        "duplicate prologue macro",
                    120:        "late prologue macro",
                    121:        "skipping late title macro",
1.7       kristaps  122:        "prologue macros out of order",
                    123:
                    124:        /* related to document structure */
                    125:        ".so is fragile, better use ln(1)",
1.50      schwarze  126:        "no document body",
1.54      schwarze  127:        "content before first section header",
                    128:        "first section is not \"NAME\"",
1.156     schwarze  129:        "NAME section without Nm before Nd",
1.128     schwarze  130:        "NAME section without description",
                    131:        "description not at the end of NAME",
                    132:        "bad NAME section content",
1.156     schwarze  133:        "missing comma before name",
1.121     schwarze  134:        "missing description line, using \"\"",
1.162     schwarze  135:        "description line outside NAME section",
1.7       kristaps  136:        "sections out of conventional order",
1.54      schwarze  137:        "duplicate section title",
                    138:        "unexpected section",
1.87      schwarze  139:        "unusual Xr order",
                    140:        "unusual Xr punctuation",
1.86      schwarze  141:        "AUTHORS section without An macro",
1.7       kristaps  142:
                    143:        /* related to macros and nesting */
1.55      schwarze  144:        "obsolete macro",
1.102     schwarze  145:        "macro neither callable nor escaped",
1.7       kristaps  146:        "skipping paragraph macro",
1.31      schwarze  147:        "moving paragraph macro out of list",
1.7       kristaps  148:        "skipping no-space macro",
                    149:        "blocks badly nested",
                    150:        "nested displays are not portable",
1.57      schwarze  151:        "moving content out of list",
1.78      schwarze  152:        "fill mode already enabled, skipping",
                    153:        "fill mode already disabled, skipping",
1.7       kristaps  154:        "line scope broken",
1.169     schwarze  155:        "skipping blank line in line scope",
1.7       kristaps  156:
                    157:        /* related to missing macro arguments */
1.58      schwarze  158:        "skipping empty request",
                    159:        "conditional request controls empty scope",
1.7       kristaps  160:        "skipping empty macro",
1.124     schwarze  161:        "empty block",
1.62      schwarze  162:        "empty argument, using 0n",
1.60      schwarze  163:        "missing display type, using -ragged",
                    164:        "list type is not the first argument",
1.152     schwarze  165:        "missing -width in -tag list, using 6n",
1.78      schwarze  166:        "missing utility name, using \"\"",
1.123     schwarze  167:        "missing function name, using \"\"",
1.60      schwarze  168:        "empty head in list item",
                    169:        "empty list item",
1.61      schwarze  170:        "missing font type, using \\fR",
                    171:        "unknown font type, using \\fR",
1.103     schwarze  172:        "nothing follows prefix",
1.122     schwarze  173:        "empty reference block",
1.155     schwarze  174:        "missing section argument",
1.60      schwarze  175:        "missing -std argument, adding it",
1.125     schwarze  176:        "missing option string, using \"\"",
                    177:        "missing resource identifier, using \"\"",
1.90      schwarze  178:        "missing eqn box, using \"\"",
1.7       kristaps  179:
                    180:        /* related to bad macro arguments */
1.64      schwarze  181:        "unterminated quoted argument",
1.7       kristaps  182:        "duplicate argument",
1.76      schwarze  183:        "skipping duplicate argument",
1.63      schwarze  184:        "skipping duplicate display type",
                    185:        "skipping duplicate list type",
1.76      schwarze  186:        "skipping -width argument",
1.126     schwarze  187:        "wrong number of cells",
1.7       kristaps  188:        "unknown AT&T UNIX version",
1.88      schwarze  189:        "comma in function argument",
1.89      schwarze  190:        "parenthesis in function name",
1.173     schwarze  191:        "unknown library name",
1.67      schwarze  192:        "invalid content in Rs block",
1.63      schwarze  193:        "invalid Boolean argument",
                    194:        "unknown font, skipping request",
1.126     schwarze  195:        "odd number of characters in request",
1.7       kristaps  196:
                    197:        /* related to plain text */
1.64      schwarze  198:        "blank line in fill mode, using .sp",
                    199:        "tab in filled text",
                    200:        "whitespace at end of input line",
1.158     schwarze  201:        "new sentence, new line",
1.7       kristaps  202:        "bad comment style",
1.64      schwarze  203:        "invalid escape sequence",
                    204:        "undefined string, using \"\"",
1.16      kristaps  205:
1.118     schwarze  206:        /* related to tables */
                    207:        "tbl line starts with span",
                    208:        "tbl column starts with span",
                    209:        "skipping vertical bar in tbl layout",
                    210:
1.7       kristaps  211:        "generic error",
                    212:
                    213:        /* related to tables */
1.116     schwarze  214:        "non-alphabetic character in tbl options",
                    215:        "skipping unknown tbl option",
                    216:        "missing tbl option argument",
                    217:        "wrong tbl option argument size",
1.118     schwarze  218:        "empty tbl layout",
                    219:        "invalid character in tbl layout",
                    220:        "unmatched parenthesis in tbl layout",
1.119     schwarze  221:        "tbl without any data cells",
                    222:        "ignoring data in spanned tbl cell",
                    223:        "ignoring extra tbl data cells",
                    224:        "data block open at end of tbl",
1.7       kristaps  225:
1.68      schwarze  226:        /* related to document structure and macros */
1.108     schwarze  227:        NULL,
1.7       kristaps  228:        "input stack limit exceeded, infinite loop?",
                    229:        "skipping bad character",
1.68      schwarze  230:        "skipping unknown macro",
1.112     schwarze  231:        "skipping insecure request",
1.73      schwarze  232:        "skipping item outside list",
1.68      schwarze  233:        "skipping column outside column list",
                    234:        "skipping end of block that is not open",
1.115     schwarze  235:        "fewer RS blocks open, skipping",
1.68      schwarze  236:        "inserting missing end of block",
                    237:        "appending missing end of block",
                    238:
                    239:        /* related to request and macro arguments */
1.7       kristaps  240:        "escaped character not allowed in a name",
1.99      schwarze  241:        "NOT IMPLEMENTED: Bd -file",
1.145     schwarze  242:        "skipping display without arguments",
1.71      schwarze  243:        "missing list type, using -item",
1.171     schwarze  244:        "argument is not numeric, using 1",
1.70      schwarze  245:        "missing manual name, using \"\"",
1.71      schwarze  246:        "uname(3) system call failed, using UNKNOWN",
1.63      schwarze  247:        "unknown standard specifier",
1.71      schwarze  248:        "skipping request without numeric argument",
1.109     schwarze  249:        "NOT IMPLEMENTED: .so with absolute path or \"..\"",
1.110     schwarze  250:        ".so request failed",
1.61      schwarze  251:        "skipping all arguments",
                    252:        "skipping excess arguments",
1.92      kristaps  253:        "divide by zero",
1.112     schwarze  254:
                    255:        "unsupported feature",
                    256:        "input too large",
1.114     schwarze  257:        "unsupported control character",
1.112     schwarze  258:        "unsupported roff request",
1.119     schwarze  259:        "eqn delim option in tbl",
1.118     schwarze  260:        "unsupported tbl layout modifier",
1.112     schwarze  261:        "ignoring macro in table",
1.7       kristaps  262: };
                    263:
                    264: static const char * const      mandoclevels[MANDOCLEVEL_MAX] = {
                    265:        "SUCCESS",
1.165     schwarze  266:        "STYLE",
1.7       kristaps  267:        "WARNING",
                    268:        "ERROR",
1.112     schwarze  269:        "UNSUPP",
1.7       kristaps  270:        "BADARG",
                    271:        "SYSERR"
                    272: };
                    273:
1.47      schwarze  274:
1.1       kristaps  275: static void
                    276: resize_buf(struct buf *buf, size_t initial)
                    277: {
                    278:
                    279:        buf->sz = buf->sz > initial/2 ? 2 * buf->sz : initial;
                    280:        buf->buf = mandoc_realloc(buf->buf, buf->sz);
                    281: }
                    282:
                    283: static void
1.84      schwarze  284: choose_parser(struct mparse *curp)
1.1       kristaps  285: {
1.83      schwarze  286:        char            *cp, *ep;
                    287:        int              format;
1.1       kristaps  288:
1.83      schwarze  289:        /*
                    290:         * If neither command line arguments -mdoc or -man select
                    291:         * a parser nor the roff parser found a .Dd or .TH macro
                    292:         * yet, look ahead in the main input buffer.
                    293:         */
                    294:
                    295:        if ((format = roff_getformat(curp->roff)) == 0) {
                    296:                cp = curp->primary->buf;
                    297:                ep = cp + curp->primary->sz;
                    298:                while (cp < ep) {
1.85      schwarze  299:                        if (*cp == '.' || *cp == '\'') {
1.83      schwarze  300:                                cp++;
                    301:                                if (cp[0] == 'D' && cp[1] == 'd') {
                    302:                                        format = MPARSE_MDOC;
                    303:                                        break;
                    304:                                }
                    305:                                if (cp[0] == 'T' && cp[1] == 'H') {
                    306:                                        format = MPARSE_MAN;
                    307:                                        break;
                    308:                                }
                    309:                        }
                    310:                        cp = memchr(cp, '\n', ep - cp);
                    311:                        if (cp == NULL)
                    312:                                break;
                    313:                        cp++;
                    314:                }
1.1       kristaps  315:        }
                    316:
1.83      schwarze  317:        if (format == MPARSE_MDOC) {
1.137     schwarze  318:                curp->man->macroset = MACROSET_MDOC;
1.164     schwarze  319:                if (curp->man->mdocmac == NULL)
                    320:                        curp->man->mdocmac = roffhash_alloc(MDOC_Dd, MDOC_MAX);
1.137     schwarze  321:        } else {
                    322:                curp->man->macroset = MACROSET_MAN;
1.164     schwarze  323:                if (curp->man->manmac == NULL)
                    324:                        curp->man->manmac = roffhash_alloc(MAN_TH, MAN_MAX);
1.47      schwarze  325:        }
1.164     schwarze  326:        curp->man->first->tok = TOKEN_NONE;
1.1       kristaps  327: }
                    328:
                    329: /*
1.95      schwarze  330:  * Main parse routine for a buffer.
                    331:  * It assumes encoding and line numbering are already set up.
                    332:  * It can recurse directly (for invocations of user-defined
                    333:  * macros, inline equations, and input line traps)
                    334:  * and indirectly (for .so file inclusion).
1.1       kristaps  335:  */
1.163     schwarze  336: static int
1.95      schwarze  337: mparse_buf_r(struct mparse *curp, struct buf blk, size_t i, int start)
1.1       kristaps  338: {
                    339:        const struct tbl_span   *span;
                    340:        struct buf       ln;
1.113     schwarze  341:        const char      *save_file;
1.110     schwarze  342:        char            *cp;
1.95      schwarze  343:        size_t           pos; /* byte number in the ln buffer */
1.1       kristaps  344:        enum rofferr     rr;
1.100     schwarze  345:        int              of;
1.1       kristaps  346:        int              lnn; /* line number in the real file */
1.110     schwarze  347:        int              fd;
1.1       kristaps  348:        unsigned char    c;
                    349:
1.95      schwarze  350:        memset(&ln, 0, sizeof(ln));
1.1       kristaps  351:
1.47      schwarze  352:        lnn = curp->line;
                    353:        pos = 0;
1.1       kristaps  354:
1.95      schwarze  355:        while (i < blk.sz) {
1.1       kristaps  356:                if (0 == pos && '\0' == blk.buf[i])
                    357:                        break;
                    358:
                    359:                if (start) {
                    360:                        curp->line = lnn;
                    361:                        curp->reparse_count = 0;
1.93      schwarze  362:
                    363:                        if (lnn < 3 &&
                    364:                            curp->filenc & MPARSE_UTF8 &&
1.95      schwarze  365:                            curp->filenc & MPARSE_LATIN1)
                    366:                                curp->filenc = preconv_cue(&blk, i);
1.1       kristaps  367:                }
                    368:
1.95      schwarze  369:                while (i < blk.sz && (start || blk.buf[i] != '\0')) {
1.1       kristaps  370:
                    371:                        /*
                    372:                         * When finding an unescaped newline character,
                    373:                         * leave the character loop to process the line.
                    374:                         * Skip a preceding carriage return, if any.
                    375:                         */
                    376:
1.95      schwarze  377:                        if ('\r' == blk.buf[i] && i + 1 < blk.sz &&
1.1       kristaps  378:                            '\n' == blk.buf[i + 1])
                    379:                                ++i;
                    380:                        if ('\n' == blk.buf[i]) {
                    381:                                ++i;
                    382:                                ++lnn;
                    383:                                break;
                    384:                        }
                    385:
1.35      schwarze  386:                        /*
1.93      schwarze  387:                         * Make sure we have space for the worst
                    388:                         * case of 11 bytes: "\\[u10ffff]\0"
1.35      schwarze  389:                         */
                    390:
1.95      schwarze  391:                        if (pos + 11 > ln.sz)
1.35      schwarze  392:                                resize_buf(&ln, 256);
                    393:
1.47      schwarze  394:                        /*
1.93      schwarze  395:                         * Encode 8-bit input.
1.1       kristaps  396:                         */
                    397:
1.93      schwarze  398:                        c = blk.buf[i];
                    399:                        if (c & 0x80) {
1.95      schwarze  400:                                if ( ! (curp->filenc && preconv_encode(
                    401:                                    &blk, &i, &ln, &pos, &curp->filenc))) {
1.114     schwarze  402:                                        mandoc_vmsg(MANDOCERR_CHAR_BAD, curp,
                    403:                                            curp->line, pos, "0x%x", c);
1.93      schwarze  404:                                        ln.buf[pos++] = '?';
                    405:                                        i++;
                    406:                                }
                    407:                                continue;
                    408:                        }
                    409:
                    410:                        /*
                    411:                         * Exclude control characters.
                    412:                         */
1.1       kristaps  413:
1.93      schwarze  414:                        if (c == 0x7f || (c < 0x20 && c != 0x09)) {
1.114     schwarze  415:                                mandoc_vmsg(c == 0x00 || c == 0x04 ||
                    416:                                    c > 0x0a ? MANDOCERR_CHAR_BAD :
                    417:                                    MANDOCERR_CHAR_UNSUPP,
                    418:                                    curp, curp->line, pos, "0x%x", c);
1.1       kristaps  419:                                i++;
1.127     schwarze  420:                                if (c != '\r')
                    421:                                        ln.buf[pos++] = '?';
1.1       kristaps  422:                                continue;
                    423:                        }
                    424:
                    425:                        ln.buf[pos++] = blk.buf[i++];
                    426:                }
                    427:
1.170     schwarze  428:                if (pos + 1 >= ln.sz)
1.1       kristaps  429:                        resize_buf(&ln, 256);
                    430:
1.170     schwarze  431:                if (i == blk.sz || blk.buf[i] == '\0')
                    432:                        ln.buf[pos++] = '\n';
1.1       kristaps  433:                ln.buf[pos] = '\0';
                    434:
                    435:                /*
                    436:                 * A significant amount of complexity is contained by
                    437:                 * the roff preprocessor.  It's line-oriented but can be
                    438:                 * expressed on one line, so we need at times to
                    439:                 * readjust our starting point and re-run it.  The roff
                    440:                 * preprocessor can also readjust the buffers with new
                    441:                 * data, so we pass them in wholesale.
                    442:                 */
                    443:
                    444:                of = 0;
                    445:
1.24      kristaps  446:                /*
                    447:                 * Maintain a lookaside buffer of all parsed lines.  We
                    448:                 * only do this if mparse_keep() has been invoked (the
                    449:                 * buffer may be accessed with mparse_getkeep()).
                    450:                 */
                    451:
                    452:                if (curp->secondary) {
1.47      schwarze  453:                        curp->secondary->buf = mandoc_realloc(
                    454:                            curp->secondary->buf,
                    455:                            curp->secondary->sz + pos + 2);
                    456:                        memcpy(curp->secondary->buf +
                    457:                            curp->secondary->sz,
                    458:                            ln.buf, pos);
1.24      kristaps  459:                        curp->secondary->sz += pos;
                    460:                        curp->secondary->buf
                    461:                                [curp->secondary->sz] = '\n';
                    462:                        curp->secondary->sz++;
                    463:                        curp->secondary->buf
                    464:                                [curp->secondary->sz] = '\0';
                    465:                }
1.1       kristaps  466: rerun:
1.96      schwarze  467:                rr = roff_parseln(curp->roff, curp->line, &ln, &of);
1.1       kristaps  468:
                    469:                switch (rr) {
1.47      schwarze  470:                case ROFF_REPARSE:
1.163     schwarze  471:                        if (++curp->reparse_count > REPARSE_LIMIT)
1.3       kristaps  472:                                mandoc_msg(MANDOCERR_ROFFLOOP, curp,
1.47      schwarze  473:                                    curp->line, pos, NULL);
1.163     schwarze  474:                        else if (mparse_buf_r(curp, ln, of, 0) == 1 ||
                    475:                            start == 1) {
                    476:                                pos = 0;
                    477:                                continue;
                    478:                        }
                    479:                        free(ln.buf);
                    480:                        return 0;
1.47      schwarze  481:                case ROFF_APPEND:
1.95      schwarze  482:                        pos = strlen(ln.buf);
1.1       kristaps  483:                        continue;
1.47      schwarze  484:                case ROFF_RERUN:
1.1       kristaps  485:                        goto rerun;
1.47      schwarze  486:                case ROFF_IGN:
1.1       kristaps  487:                        pos = 0;
                    488:                        continue;
1.47      schwarze  489:                case ROFF_SO:
1.95      schwarze  490:                        if ( ! (curp->options & MPARSE_SO) &&
                    491:                            (i >= blk.sz || blk.buf[i] == '\0')) {
1.45      schwarze  492:                                curp->sodest = mandoc_strdup(ln.buf + of);
                    493:                                free(ln.buf);
1.163     schwarze  494:                                return 1;
1.45      schwarze  495:                        }
1.24      kristaps  496:                        /*
                    497:                         * We remove `so' clauses from our lookaside
                    498:                         * buffer because we're going to descend into
                    499:                         * the file recursively.
                    500:                         */
1.47      schwarze  501:                        if (curp->secondary)
1.25      kristaps  502:                                curp->secondary->sz -= pos + 1;
1.113     schwarze  503:                        save_file = curp->file;
1.148     schwarze  504:                        if ((fd = mparse_open(curp, ln.buf + of)) != -1) {
1.110     schwarze  505:                                mparse_readfd(curp, fd, ln.buf + of);
1.147     schwarze  506:                                close(fd);
1.113     schwarze  507:                                curp->file = save_file;
                    508:                        } else {
                    509:                                curp->file = save_file;
1.52      schwarze  510:                                mandoc_vmsg(MANDOCERR_SO_FAIL,
                    511:                                    curp, curp->line, pos,
                    512:                                    ".so %s", ln.buf + of);
1.110     schwarze  513:                                ln.sz = mandoc_asprintf(&cp,
                    514:                                    ".sp\nSee the file %s.\n.sp",
                    515:                                    ln.buf + of);
                    516:                                free(ln.buf);
                    517:                                ln.buf = cp;
                    518:                                of = 0;
                    519:                                mparse_buf_r(curp, ln, of, 0);
1.52      schwarze  520:                        }
1.1       kristaps  521:                        pos = 0;
                    522:                        continue;
                    523:                default:
                    524:                        break;
                    525:                }
                    526:
1.150     schwarze  527:                if (curp->man->macroset == MACROSET_NONE)
1.84      schwarze  528:                        choose_parser(curp);
1.1       kristaps  529:
1.47      schwarze  530:                /*
1.84      schwarze  531:                 * Lastly, push down into the parsers themselves.
1.1       kristaps  532:                 * If libroff returns ROFF_TBL, then add it to the
                    533:                 * currently open parse.  Since we only get here if
                    534:                 * there does exist data (see tbl_data.c), we're
                    535:                 * guaranteed that something's been allocated.
                    536:                 * Do the same for ROFF_EQN.
                    537:                 */
                    538:
1.139     schwarze  539:                if (rr == ROFF_TBL)
1.100     schwarze  540:                        while ((span = roff_span(curp->roff)) != NULL)
1.139     schwarze  541:                                roff_addtbl(curp->man, span);
                    542:                else if (rr == ROFF_EQN)
                    543:                        roff_addeqn(curp->man, roff_eqn(curp->roff));
                    544:                else if ((curp->man->macroset == MACROSET_MDOC ?
1.135     schwarze  545:                    mdoc_parseln(curp->man, curp->line, ln.buf, of) :
1.100     schwarze  546:                    man_parseln(curp->man, curp->line, ln.buf, of)) == 2)
                    547:                                break;
1.1       kristaps  548:
                    549:                /* Temporary buffers typically are not full. */
                    550:
                    551:                if (0 == start && '\0' == blk.buf[i])
                    552:                        break;
                    553:
                    554:                /* Start the next input line. */
                    555:
                    556:                pos = 0;
                    557:        }
                    558:
                    559:        free(ln.buf);
1.163     schwarze  560:        return 1;
1.1       kristaps  561: }
                    562:
                    563: static int
1.40      schwarze  564: read_whole_file(struct mparse *curp, const char *file, int fd,
                    565:                struct buf *fb, int *with_mmap)
1.1       kristaps  566: {
1.161     schwarze  567:        struct stat      st;
1.140     schwarze  568:        gzFile           gz;
1.1       kristaps  569:        size_t           off;
                    570:        ssize_t          ssz;
1.143     schwarze  571:
                    572:        if (fstat(fd, &st) == -1)
                    573:                err((int)MANDOCLEVEL_SYSERR, "%s", file);
1.1       kristaps  574:
                    575:        /*
                    576:         * If we're a regular file, try just reading in the whole entry
                    577:         * via mmap().  This is faster than reading it into blocks, and
                    578:         * since each file is only a few bytes to begin with, I'm not
                    579:         * concerned that this is going to tank any machines.
                    580:         */
                    581:
1.140     schwarze  582:        if (curp->gzip == 0 && S_ISREG(st.st_mode)) {
1.131     schwarze  583:                if (st.st_size > 0x7fffffff) {
1.111     schwarze  584:                        mandoc_msg(MANDOCERR_TOOLARGE, curp, 0, 0, NULL);
1.142     schwarze  585:                        return 0;
1.1       kristaps  586:                }
                    587:                *with_mmap = 1;
                    588:                fb->sz = (size_t)st.st_size;
1.37      schwarze  589:                fb->buf = mmap(NULL, fb->sz, PROT_READ, MAP_SHARED, fd, 0);
1.1       kristaps  590:                if (fb->buf != MAP_FAILED)
1.142     schwarze  591:                        return 1;
1.1       kristaps  592:        }
                    593:
1.140     schwarze  594:        if (curp->gzip) {
1.143     schwarze  595:                if ((gz = gzdopen(fd, "rb")) == NULL)
                    596:                        err((int)MANDOCLEVEL_SYSERR, "%s", file);
1.140     schwarze  597:        } else
                    598:                gz = NULL;
                    599:
1.1       kristaps  600:        /*
                    601:         * If this isn't a regular file (like, say, stdin), then we must
                    602:         * go the old way and just read things in bit by bit.
                    603:         */
                    604:
                    605:        *with_mmap = 0;
                    606:        off = 0;
                    607:        fb->sz = 0;
                    608:        fb->buf = NULL;
                    609:        for (;;) {
                    610:                if (off == fb->sz) {
                    611:                        if (fb->sz == (1U << 31)) {
1.111     schwarze  612:                                mandoc_msg(MANDOCERR_TOOLARGE, curp,
                    613:                                    0, 0, NULL);
1.1       kristaps  614:                                break;
                    615:                        }
                    616:                        resize_buf(fb, 65536);
                    617:                }
1.140     schwarze  618:                ssz = curp->gzip ?
                    619:                    gzread(gz, fb->buf + (int)off, fb->sz - off) :
                    620:                    read(fd, fb->buf + (int)off, fb->sz - off);
1.1       kristaps  621:                if (ssz == 0) {
                    622:                        fb->sz = off;
1.142     schwarze  623:                        return 1;
1.1       kristaps  624:                }
1.143     schwarze  625:                if (ssz == -1)
                    626:                        err((int)MANDOCLEVEL_SYSERR, "%s", file);
1.1       kristaps  627:                off += (size_t)ssz;
                    628:        }
                    629:
                    630:        free(fb->buf);
                    631:        fb->buf = NULL;
1.142     schwarze  632:        return 0;
1.1       kristaps  633: }
                    634:
                    635: static void
                    636: mparse_end(struct mparse *curp)
                    637: {
1.135     schwarze  638:        if (curp->man->macroset == MACROSET_NONE)
                    639:                curp->man->macroset = MACROSET_MAN;
                    640:        if (curp->man->macroset == MACROSET_MDOC)
                    641:                mdoc_endparse(curp->man);
                    642:        else
1.111     schwarze  643:                man_endparse(curp->man);
1.1       kristaps  644:        roff_endparse(curp->roff);
                    645: }
                    646:
                    647: static void
1.36      schwarze  648: mparse_parse_buffer(struct mparse *curp, struct buf blk, const char *file)
1.28      joerg     649: {
1.85      schwarze  650:        struct buf      *svprimary;
1.28      joerg     651:        const char      *svfile;
1.95      schwarze  652:        size_t           offset;
1.36      schwarze  653:        static int       recursion_depth;
                    654:
                    655:        if (64 < recursion_depth) {
                    656:                mandoc_msg(MANDOCERR_ROFFLOOP, curp, curp->line, 0, NULL);
                    657:                return;
                    658:        }
1.28      joerg     659:
                    660:        /* Line number is per-file. */
                    661:        svfile = curp->file;
                    662:        curp->file = file;
1.85      schwarze  663:        svprimary = curp->primary;
1.83      schwarze  664:        curp->primary = &blk;
1.28      joerg     665:        curp->line = 1;
1.36      schwarze  666:        recursion_depth++;
1.28      joerg     667:
1.93      schwarze  668:        /* Skip an UTF-8 byte order mark. */
                    669:        if (curp->filenc & MPARSE_UTF8 && blk.sz > 2 &&
                    670:            (unsigned char)blk.buf[0] == 0xef &&
                    671:            (unsigned char)blk.buf[1] == 0xbb &&
                    672:            (unsigned char)blk.buf[2] == 0xbf) {
1.95      schwarze  673:                offset = 3;
1.93      schwarze  674:                curp->filenc &= ~MPARSE_LATIN1;
1.95      schwarze  675:        } else
                    676:                offset = 0;
1.93      schwarze  677:
1.95      schwarze  678:        mparse_buf_r(curp, blk, offset, 1);
1.28      joerg     679:
1.111     schwarze  680:        if (--recursion_depth == 0)
1.28      joerg     681:                mparse_end(curp);
                    682:
1.85      schwarze  683:        curp->primary = svprimary;
1.28      joerg     684:        curp->file = svfile;
                    685: }
                    686:
                    687: enum mandoclevel
1.104     schwarze  688: mparse_readmem(struct mparse *curp, void *buf, size_t len,
1.28      joerg     689:                const char *file)
                    690: {
                    691:        struct buf blk;
                    692:
1.104     schwarze  693:        blk.buf = buf;
1.28      joerg     694:        blk.sz = len;
                    695:
1.36      schwarze  696:        mparse_parse_buffer(curp, blk, file);
1.142     schwarze  697:        return curp->file_status;
1.28      joerg     698: }
                    699:
1.98      schwarze  700: /*
                    701:  * Read the whole file into memory and call the parsers.
                    702:  * Called recursively when an .so request is encountered.
                    703:  */
1.36      schwarze  704: enum mandoclevel
                    705: mparse_readfd(struct mparse *curp, int fd, const char *file)
1.1       kristaps  706: {
1.28      joerg     707:        struct buf       blk;
                    708:        int              with_mmap;
1.93      schwarze  709:        int              save_filenc;
1.1       kristaps  710:
1.91      schwarze  711:        if (read_whole_file(curp, file, fd, &blk, &with_mmap)) {
1.93      schwarze  712:                save_filenc = curp->filenc;
                    713:                curp->filenc = curp->options &
                    714:                    (MPARSE_UTF8 | MPARSE_LATIN1);
1.91      schwarze  715:                mparse_parse_buffer(curp, blk, file);
1.93      schwarze  716:                curp->filenc = save_filenc;
1.91      schwarze  717:                if (with_mmap)
                    718:                        munmap(blk.buf, blk.sz);
                    719:                else
                    720:                        free(blk.buf);
                    721:        }
1.142     schwarze  722:        return curp->file_status;
1.82      schwarze  723: }
                    724:
1.148     schwarze  725: int
                    726: mparse_open(struct mparse *curp, const char *file)
1.82      schwarze  727: {
                    728:        char             *cp;
1.148     schwarze  729:        int               fd;
1.82      schwarze  730:
                    731:        curp->file = file;
1.140     schwarze  732:        cp = strrchr(file, '.');
                    733:        curp->gzip = (cp != NULL && ! strcmp(cp + 1, "gz"));
1.98      schwarze  734:
1.140     schwarze  735:        /* First try to use the filename as it is. */
1.98      schwarze  736:
1.148     schwarze  737:        if ((fd = open(file, O_RDONLY)) != -1)
                    738:                return fd;
1.98      schwarze  739:
1.140     schwarze  740:        /*
                    741:         * If that doesn't work and the filename doesn't
                    742:         * already  end in .gz, try appending .gz.
                    743:         */
1.98      schwarze  744:
1.140     schwarze  745:        if ( ! curp->gzip) {
1.98      schwarze  746:                mandoc_asprintf(&cp, "%s.gz", file);
1.149     schwarze  747:                fd = open(cp, O_RDONLY);
1.108     schwarze  748:                free(cp);
1.148     schwarze  749:                if (fd != -1) {
1.140     schwarze  750:                        curp->gzip = 1;
1.148     schwarze  751:                        return fd;
1.82      schwarze  752:                }
                    753:        }
                    754:
1.140     schwarze  755:        /* Neither worked, give up. */
1.97      schwarze  756:
1.140     schwarze  757:        mandoc_msg(MANDOCERR_FILE, curp, 0, 0, strerror(errno));
1.148     schwarze  758:        return -1;
1.1       kristaps  759: }
                    760:
                    761: struct mparse *
1.179     schwarze  762: mparse_alloc(int options, enum mandocerr mmin, mandocmsg mmsg,
                    763:     enum mandoc_os os_e, const char *os_s)
1.1       kristaps  764: {
                    765:        struct mparse   *curp;
1.10      kristaps  766:
1.1       kristaps  767:        curp = mandoc_calloc(1, sizeof(struct mparse));
                    768:
1.44      schwarze  769:        curp->options = options;
1.179     schwarze  770:        curp->mmin = mmin;
1.1       kristaps  771:        curp->mmsg = mmsg;
1.179     schwarze  772:        curp->os_s = os_s;
1.1       kristaps  773:
1.144     schwarze  774:        curp->roff = roff_alloc(curp, options);
1.179     schwarze  775:        curp->man = roff_man_alloc(curp->roff, curp, curp->os_s,
1.137     schwarze  776:                curp->options & MPARSE_QUICK ? 1 : 0);
1.136     schwarze  777:        if (curp->options & MPARSE_MDOC) {
1.137     schwarze  778:                curp->man->macroset = MACROSET_MDOC;
1.164     schwarze  779:                if (curp->man->mdocmac == NULL)
                    780:                        curp->man->mdocmac = roffhash_alloc(MDOC_Dd, MDOC_MAX);
1.137     schwarze  781:        } else if (curp->options & MPARSE_MAN) {
                    782:                curp->man->macroset = MACROSET_MAN;
1.164     schwarze  783:                if (curp->man->manmac == NULL)
                    784:                        curp->man->manmac = roffhash_alloc(MAN_TH, MAN_MAX);
1.136     schwarze  785:        }
1.138     schwarze  786:        curp->man->first->tok = TOKEN_NONE;
1.179     schwarze  787:        curp->man->meta.os_e = os_e;
1.142     schwarze  788:        return curp;
1.1       kristaps  789: }
                    790:
                    791: void
                    792: mparse_reset(struct mparse *curp)
                    793: {
                    794:        roff_reset(curp->roff);
1.150     schwarze  795:        roff_man_reset(curp->man);
1.160     schwarze  796:
                    797:        free(curp->sodest);
                    798:        curp->sodest = NULL;
                    799:
1.24      kristaps  800:        if (curp->secondary)
                    801:                curp->secondary->sz = 0;
1.1       kristaps  802:
                    803:        curp->file_status = MANDOCLEVEL_OK;
1.159     schwarze  804:        curp->gzip = 0;
1.1       kristaps  805: }
                    806:
                    807: void
                    808: mparse_free(struct mparse *curp)
                    809: {
                    810:
1.164     schwarze  811:        roffhash_free(curp->man->mdocmac);
                    812:        roffhash_free(curp->man->manmac);
1.137     schwarze  813:        roff_man_free(curp->man);
1.160     schwarze  814:        roff_free(curp->roff);
1.24      kristaps  815:        if (curp->secondary)
                    816:                free(curp->secondary->buf);
1.1       kristaps  817:
1.24      kristaps  818:        free(curp->secondary);
1.45      schwarze  819:        free(curp->sodest);
1.1       kristaps  820:        free(curp);
                    821: }
                    822:
                    823: void
1.135     schwarze  824: mparse_result(struct mparse *curp, struct roff_man **man,
                    825:        char **sodest)
1.1       kristaps  826: {
                    827:
1.45      schwarze  828:        if (sodest && NULL != (*sodest = curp->sodest)) {
                    829:                *man = NULL;
                    830:                return;
                    831:        }
1.9       kristaps  832:        if (man)
                    833:                *man = curp->man;
1.157     schwarze  834: }
                    835:
                    836: void
                    837: mparse_updaterc(struct mparse *curp, enum mandoclevel *rc)
                    838: {
                    839:        if (curp->file_status > *rc)
                    840:                *rc = curp->file_status;
1.3       kristaps  841: }
                    842:
                    843: void
                    844: mandoc_vmsg(enum mandocerr t, struct mparse *m,
                    845:                int ln, int pos, const char *fmt, ...)
                    846: {
                    847:        char             buf[256];
                    848:        va_list          ap;
                    849:
                    850:        va_start(ap, fmt);
1.48      schwarze  851:        (void)vsnprintf(buf, sizeof(buf), fmt, ap);
1.3       kristaps  852:        va_end(ap);
                    853:
                    854:        mandoc_msg(t, m, ln, pos, buf);
                    855: }
                    856:
                    857: void
1.47      schwarze  858: mandoc_msg(enum mandocerr er, struct mparse *m,
1.3       kristaps  859:                int ln, int col, const char *msg)
                    860: {
                    861:        enum mandoclevel level;
                    862:
1.179     schwarze  863:        if (er < m->mmin && er != MANDOCERR_FILE)
                    864:                return;
                    865:
1.112     schwarze  866:        level = MANDOCLEVEL_UNSUPP;
1.3       kristaps  867:        while (er < mandoclimits[level])
                    868:                level--;
                    869:
1.8       kristaps  870:        if (m->mmsg)
                    871:                (*m->mmsg)(er, level, m->file, ln, col, msg);
1.3       kristaps  872:
                    873:        if (m->file_status < level)
                    874:                m->file_status = level;
1.7       kristaps  875: }
                    876:
                    877: const char *
                    878: mparse_strerror(enum mandocerr er)
                    879: {
                    880:
1.142     schwarze  881:        return mandocerrs[er];
1.7       kristaps  882: }
                    883:
                    884: const char *
                    885: mparse_strlevel(enum mandoclevel lvl)
                    886: {
1.142     schwarze  887:        return mandoclevels[lvl];
1.24      kristaps  888: }
                    889:
                    890: void
                    891: mparse_keep(struct mparse *p)
                    892: {
                    893:
                    894:        assert(NULL == p->secondary);
                    895:        p->secondary = mandoc_calloc(1, sizeof(struct buf));
                    896: }
                    897:
                    898: const char *
                    899: mparse_getkeep(const struct mparse *p)
                    900: {
                    901:
                    902:        assert(p->secondary);
1.142     schwarze  903:        return p->secondary->sz ? p->secondary->buf : NULL;
1.1       kristaps  904: }

CVSweb