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

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