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

Annotation of mandoc/read.c, Revision 1.199

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

CVSweb