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

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