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

Annotation of mandoc/read.c, Revision 1.202

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

CVSweb