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

Annotation of mandoc/read.c, Revision 1.214

1.214   ! schwarze    1: /*     $Id: read.c,v 1.213 2019/06/03 19:58:02 schwarze Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
1.211     schwarze    4:  * Copyright (c) 2010-2019 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.83      schwarze   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:        int               options; /* parser options */
1.140     schwarze   55:        int               gzip; /* current input file is gzipped */
1.93      schwarze   56:        int               filenc; /* encoding of the current file */
1.1       kristaps   57:        int               reparse_count; /* finite interp. stack */
1.83      schwarze   58:        int               line; /* line number in the file */
1.1       kristaps   59: };
                     60:
1.84      schwarze   61: static void      choose_parser(struct mparse *);
1.198     schwarze   62: static void      free_buf_list(struct buf *);
1.1       kristaps   63: static void      resize_buf(struct buf *, size_t);
1.199     schwarze   64: static int       mparse_buf_r(struct mparse *, struct buf, size_t, int);
1.203     schwarze   65: static int       read_whole_file(struct mparse *, int, struct buf *, int *);
1.1       kristaps   66: static void      mparse_end(struct mparse *);
                     67:
1.47      schwarze   68:
1.1       kristaps   69: static void
                     70: resize_buf(struct buf *buf, size_t initial)
                     71: {
                     72:
                     73:        buf->sz = buf->sz > initial/2 ? 2 * buf->sz : initial;
                     74:        buf->buf = mandoc_realloc(buf->buf, buf->sz);
                     75: }
                     76:
                     77: static void
1.198     schwarze   78: free_buf_list(struct buf *buf)
                     79: {
                     80:        struct buf *tmp;
                     81:
                     82:        while (buf != NULL) {
                     83:                tmp = buf;
                     84:                buf = tmp->next;
                     85:                free(tmp->buf);
                     86:                free(tmp);
                     87:        }
                     88: }
                     89:
                     90: static void
1.84      schwarze   91: choose_parser(struct mparse *curp)
1.1       kristaps   92: {
1.83      schwarze   93:        char            *cp, *ep;
                     94:        int              format;
1.1       kristaps   95:
1.83      schwarze   96:        /*
                     97:         * If neither command line arguments -mdoc or -man select
                     98:         * a parser nor the roff parser found a .Dd or .TH macro
                     99:         * yet, look ahead in the main input buffer.
                    100:         */
                    101:
                    102:        if ((format = roff_getformat(curp->roff)) == 0) {
                    103:                cp = curp->primary->buf;
                    104:                ep = cp + curp->primary->sz;
                    105:                while (cp < ep) {
1.85      schwarze  106:                        if (*cp == '.' || *cp == '\'') {
1.83      schwarze  107:                                cp++;
                    108:                                if (cp[0] == 'D' && cp[1] == 'd') {
                    109:                                        format = MPARSE_MDOC;
                    110:                                        break;
                    111:                                }
                    112:                                if (cp[0] == 'T' && cp[1] == 'H') {
                    113:                                        format = MPARSE_MAN;
                    114:                                        break;
                    115:                                }
                    116:                        }
                    117:                        cp = memchr(cp, '\n', ep - cp);
                    118:                        if (cp == NULL)
                    119:                                break;
                    120:                        cp++;
                    121:                }
1.1       kristaps  122:        }
                    123:
1.83      schwarze  124:        if (format == MPARSE_MDOC) {
1.209     schwarze  125:                curp->man->meta.macroset = MACROSET_MDOC;
1.164     schwarze  126:                if (curp->man->mdocmac == NULL)
                    127:                        curp->man->mdocmac = roffhash_alloc(MDOC_Dd, MDOC_MAX);
1.137     schwarze  128:        } else {
1.209     schwarze  129:                curp->man->meta.macroset = MACROSET_MAN;
1.164     schwarze  130:                if (curp->man->manmac == NULL)
                    131:                        curp->man->manmac = roffhash_alloc(MAN_TH, MAN_MAX);
1.47      schwarze  132:        }
1.209     schwarze  133:        curp->man->meta.first->tok = TOKEN_NONE;
1.1       kristaps  134: }
                    135:
                    136: /*
1.95      schwarze  137:  * Main parse routine for a buffer.
                    138:  * It assumes encoding and line numbering are already set up.
                    139:  * It can recurse directly (for invocations of user-defined
                    140:  * macros, inline equations, and input line traps)
                    141:  * and indirectly (for .so file inclusion).
1.1       kristaps  142:  */
1.199     schwarze  143: static int
1.95      schwarze  144: mparse_buf_r(struct mparse *curp, struct buf blk, size_t i, int start)
1.1       kristaps  145: {
                    146:        struct buf       ln;
1.199     schwarze  147:        struct buf      *firstln, *lastln, *thisln, *loop;
1.110     schwarze  148:        char            *cp;
1.95      schwarze  149:        size_t           pos; /* byte number in the ln buffer */
1.199     schwarze  150:        int              line_result, result;
1.100     schwarze  151:        int              of;
1.1       kristaps  152:        int              lnn; /* line number in the real file */
1.110     schwarze  153:        int              fd;
1.199     schwarze  154:        int              inloop; /* Saw .while on this level. */
1.1       kristaps  155:        unsigned char    c;
                    156:
1.198     schwarze  157:        ln.sz = 256;
                    158:        ln.buf = mandoc_malloc(ln.sz);
                    159:        ln.next = NULL;
1.213     schwarze  160:        firstln = lastln = loop = NULL;
1.47      schwarze  161:        lnn = curp->line;
                    162:        pos = 0;
1.199     schwarze  163:        inloop = 0;
1.198     schwarze  164:        result = ROFF_CONT;
1.1       kristaps  165:
1.199     schwarze  166:        while (i < blk.sz && (blk.buf[i] != '\0' || pos != 0)) {
1.1       kristaps  167:                if (start) {
                    168:                        curp->line = lnn;
                    169:                        curp->reparse_count = 0;
1.93      schwarze  170:
                    171:                        if (lnn < 3 &&
                    172:                            curp->filenc & MPARSE_UTF8 &&
1.95      schwarze  173:                            curp->filenc & MPARSE_LATIN1)
                    174:                                curp->filenc = preconv_cue(&blk, i);
1.1       kristaps  175:                }
                    176:
1.95      schwarze  177:                while (i < blk.sz && (start || blk.buf[i] != '\0')) {
1.1       kristaps  178:
                    179:                        /*
                    180:                         * When finding an unescaped newline character,
                    181:                         * leave the character loop to process the line.
                    182:                         * Skip a preceding carriage return, if any.
                    183:                         */
                    184:
1.95      schwarze  185:                        if ('\r' == blk.buf[i] && i + 1 < blk.sz &&
1.1       kristaps  186:                            '\n' == blk.buf[i + 1])
                    187:                                ++i;
                    188:                        if ('\n' == blk.buf[i]) {
                    189:                                ++i;
                    190:                                ++lnn;
                    191:                                break;
                    192:                        }
                    193:
1.35      schwarze  194:                        /*
1.93      schwarze  195:                         * Make sure we have space for the worst
1.198     schwarze  196:                         * case of 12 bytes: "\\[u10ffff]\n\0"
1.35      schwarze  197:                         */
                    198:
1.198     schwarze  199:                        if (pos + 12 > ln.sz)
1.35      schwarze  200:                                resize_buf(&ln, 256);
                    201:
1.47      schwarze  202:                        /*
1.93      schwarze  203:                         * Encode 8-bit input.
1.1       kristaps  204:                         */
                    205:
1.93      schwarze  206:                        c = blk.buf[i];
                    207:                        if (c & 0x80) {
1.95      schwarze  208:                                if ( ! (curp->filenc && preconv_encode(
                    209:                                    &blk, &i, &ln, &pos, &curp->filenc))) {
1.206     schwarze  210:                                        mandoc_msg(MANDOCERR_CHAR_BAD,
1.114     schwarze  211:                                            curp->line, pos, "0x%x", c);
1.93      schwarze  212:                                        ln.buf[pos++] = '?';
                    213:                                        i++;
                    214:                                }
                    215:                                continue;
                    216:                        }
                    217:
                    218:                        /*
                    219:                         * Exclude control characters.
                    220:                         */
1.1       kristaps  221:
1.93      schwarze  222:                        if (c == 0x7f || (c < 0x20 && c != 0x09)) {
1.206     schwarze  223:                                mandoc_msg(c == 0x00 || c == 0x04 ||
1.114     schwarze  224:                                    c > 0x0a ? MANDOCERR_CHAR_BAD :
                    225:                                    MANDOCERR_CHAR_UNSUPP,
1.206     schwarze  226:                                    curp->line, pos, "0x%x", c);
1.1       kristaps  227:                                i++;
1.127     schwarze  228:                                if (c != '\r')
                    229:                                        ln.buf[pos++] = '?';
1.1       kristaps  230:                                continue;
                    231:                        }
                    232:
                    233:                        ln.buf[pos++] = blk.buf[i++];
                    234:                }
1.198     schwarze  235:                ln.buf[pos] = '\0';
                    236:
                    237:                /*
                    238:                 * Maintain a lookaside buffer of all lines.
                    239:                 * parsed from this input source.
                    240:                 */
                    241:
                    242:                thisln = mandoc_malloc(sizeof(*thisln));
                    243:                thisln->buf = mandoc_strdup(ln.buf);
                    244:                thisln->sz = strlen(ln.buf) + 1;
                    245:                thisln->next = NULL;
                    246:                if (firstln == NULL) {
                    247:                        firstln = lastln = thisln;
                    248:                        if (curp->secondary == NULL)
                    249:                                curp->secondary = firstln;
                    250:                } else {
                    251:                        lastln->next = thisln;
                    252:                        lastln = thisln;
                    253:                }
1.1       kristaps  254:
1.198     schwarze  255:                /* XXX Ugly hack to mark the end of the input. */
1.1       kristaps  256:
1.198     schwarze  257:                if (i == blk.sz || blk.buf[i] == '\0') {
1.212     schwarze  258:                        if (pos + 2 > ln.sz)
                    259:                                resize_buf(&ln, 256);
1.170     schwarze  260:                        ln.buf[pos++] = '\n';
1.198     schwarze  261:                        ln.buf[pos] = '\0';
                    262:                }
1.1       kristaps  263:
                    264:                /*
                    265:                 * A significant amount of complexity is contained by
                    266:                 * the roff preprocessor.  It's line-oriented but can be
                    267:                 * expressed on one line, so we need at times to
                    268:                 * readjust our starting point and re-run it.  The roff
                    269:                 * preprocessor can also readjust the buffers with new
                    270:                 * data, so we pass them in wholesale.
                    271:                 */
                    272:
                    273:                of = 0;
                    274: rerun:
1.197     schwarze  275:                line_result = roff_parseln(curp->roff, curp->line, &ln, &of);
1.1       kristaps  276:
1.199     schwarze  277:                /* Process options. */
                    278:
                    279:                if (line_result & ROFF_APPEND)
                    280:                        assert(line_result == (ROFF_IGN | ROFF_APPEND));
                    281:
                    282:                if (line_result & ROFF_USERCALL)
                    283:                        assert((line_result & ROFF_MASK) == ROFF_REPARSE);
                    284:
                    285:                if (line_result & ROFF_USERRET) {
                    286:                        assert(line_result == (ROFF_IGN | ROFF_USERRET));
                    287:                        if (start == 0) {
                    288:                                /* Return from the current macro. */
                    289:                                result = ROFF_USERRET;
                    290:                                goto out;
                    291:                        }
                    292:                }
                    293:
                    294:                switch (line_result & ROFF_LOOPMASK) {
                    295:                case ROFF_IGN:
                    296:                        break;
                    297:                case ROFF_WHILE:
                    298:                        if (curp->loop != NULL) {
                    299:                                if (loop == curp->loop)
                    300:                                        break;
                    301:                                mandoc_msg(MANDOCERR_WHILE_NEST,
1.206     schwarze  302:                                    curp->line, pos, NULL);
1.199     schwarze  303:                        }
                    304:                        curp->loop = thisln;
                    305:                        loop = NULL;
                    306:                        inloop = 1;
                    307:                        break;
                    308:                case ROFF_LOOPCONT:
                    309:                case ROFF_LOOPEXIT:
                    310:                        if (curp->loop == NULL) {
                    311:                                mandoc_msg(MANDOCERR_WHILE_FAIL,
1.206     schwarze  312:                                    curp->line, pos, NULL);
1.199     schwarze  313:                                break;
                    314:                        }
                    315:                        if (inloop == 0) {
                    316:                                mandoc_msg(MANDOCERR_WHILE_INTO,
1.206     schwarze  317:                                    curp->line, pos, NULL);
1.199     schwarze  318:                                curp->loop = loop = NULL;
                    319:                                break;
                    320:                        }
                    321:                        if (line_result & ROFF_LOOPCONT)
                    322:                                loop = curp->loop;
                    323:                        else {
                    324:                                curp->loop = loop = NULL;
                    325:                                inloop = 0;
                    326:                        }
                    327:                        break;
                    328:                default:
                    329:                        abort();
                    330:                }
                    331:
                    332:                /* Process the main instruction from the roff parser. */
                    333:
                    334:                switch (line_result & ROFF_MASK) {
                    335:                case ROFF_IGN:
                    336:                        break;
                    337:                case ROFF_CONT:
1.209     schwarze  338:                        if (curp->man->meta.macroset == MACROSET_NONE)
1.199     schwarze  339:                                choose_parser(curp);
1.209     schwarze  340:                        if ((curp->man->meta.macroset == MACROSET_MDOC ?
1.199     schwarze  341:                             mdoc_parseln(curp->man, curp->line, ln.buf, of) :
                    342:                             man_parseln(curp->man, curp->line, ln.buf, of)
                    343:                            ) == 2)
                    344:                                goto out;
                    345:                        break;
                    346:                case ROFF_RERUN:
                    347:                        goto rerun;
1.47      schwarze  348:                case ROFF_REPARSE:
1.197     schwarze  349:                        if (++curp->reparse_count > REPARSE_LIMIT) {
1.199     schwarze  350:                                /* Abort and return to the top level. */
1.198     schwarze  351:                                result = ROFF_IGN;
1.206     schwarze  352:                                mandoc_msg(MANDOCERR_ROFFLOOP,
1.47      schwarze  353:                                    curp->line, pos, NULL);
1.199     schwarze  354:                                goto out;
1.197     schwarze  355:                        }
1.199     schwarze  356:                        result = mparse_buf_r(curp, ln, of, 0);
                    357:                        if (line_result & ROFF_USERCALL) {
                    358:                                roff_userret(curp->roff);
                    359:                                /* Continue normally. */
                    360:                                if (result & ROFF_USERRET)
                    361:                                        result = ROFF_CONT;
1.163     schwarze  362:                        }
1.199     schwarze  363:                        if (start == 0 && result != ROFF_CONT)
                    364:                                goto out;
                    365:                        break;
1.47      schwarze  366:                case ROFF_SO:
1.95      schwarze  367:                        if ( ! (curp->options & MPARSE_SO) &&
                    368:                            (i >= blk.sz || blk.buf[i] == '\0')) {
1.209     schwarze  369:                                curp->man->meta.sodest =
                    370:                                    mandoc_strdup(ln.buf + of);
1.198     schwarze  371:                                goto out;
1.45      schwarze  372:                        }
1.148     schwarze  373:                        if ((fd = mparse_open(curp, ln.buf + of)) != -1) {
1.110     schwarze  374:                                mparse_readfd(curp, fd, ln.buf + of);
1.147     schwarze  375:                                close(fd);
1.113     schwarze  376:                        } else {
1.208     schwarze  377:                                mandoc_msg(MANDOCERR_SO_FAIL,
                    378:                                    curp->line, of, ".so %s: %s",
                    379:                                    ln.buf + of, strerror(errno));
1.110     schwarze  380:                                ln.sz = mandoc_asprintf(&cp,
                    381:                                    ".sp\nSee the file %s.\n.sp",
                    382:                                    ln.buf + of);
                    383:                                free(ln.buf);
                    384:                                ln.buf = cp;
                    385:                                of = 0;
                    386:                                mparse_buf_r(curp, ln, of, 0);
1.52      schwarze  387:                        }
1.199     schwarze  388:                        break;
1.1       kristaps  389:                default:
1.199     schwarze  390:                        abort();
1.1       kristaps  391:                }
                    392:
1.199     schwarze  393:                /* Start the next input line. */
1.1       kristaps  394:
1.199     schwarze  395:                if (loop != NULL &&
                    396:                    (line_result & ROFF_LOOPMASK) == ROFF_IGN)
                    397:                        loop = loop->next;
                    398:
                    399:                if (loop != NULL) {
                    400:                        if ((line_result & ROFF_APPEND) == 0)
                    401:                                *ln.buf = '\0';
                    402:                        if (ln.sz < loop->sz)
                    403:                                resize_buf(&ln, loop->sz);
                    404:                        (void)strlcat(ln.buf, loop->buf, ln.sz);
                    405:                        of = 0;
                    406:                        goto rerun;
                    407:                }
1.1       kristaps  408:
1.199     schwarze  409:                pos = (line_result & ROFF_APPEND) ? strlen(ln.buf) : 0;
1.1       kristaps  410:        }
1.198     schwarze  411: out:
1.199     schwarze  412:        if (inloop) {
                    413:                if (result != ROFF_USERRET)
1.206     schwarze  414:                        mandoc_msg(MANDOCERR_WHILE_OUTOF,
1.199     schwarze  415:                            curp->line, pos, NULL);
                    416:                curp->loop = NULL;
                    417:        }
1.1       kristaps  418:        free(ln.buf);
1.198     schwarze  419:        if (firstln != curp->secondary)
                    420:                free_buf_list(firstln);
                    421:        return result;
1.1       kristaps  422: }
                    423:
                    424: static int
1.203     schwarze  425: read_whole_file(struct mparse *curp, int fd, struct buf *fb, int *with_mmap)
1.1       kristaps  426: {
1.161     schwarze  427:        struct stat      st;
1.140     schwarze  428:        gzFile           gz;
1.1       kristaps  429:        size_t           off;
                    430:        ssize_t          ssz;
1.194     schwarze  431:        int              gzerrnum, retval;
1.143     schwarze  432:
1.192     schwarze  433:        if (fstat(fd, &st) == -1) {
1.214   ! schwarze  434:                mandoc_msg(MANDOCERR_FSTAT, 0, 0, "%s", strerror(errno));
        !           435:                return -1;
1.192     schwarze  436:        }
1.1       kristaps  437:
                    438:        /*
                    439:         * If we're a regular file, try just reading in the whole entry
                    440:         * via mmap().  This is faster than reading it into blocks, and
                    441:         * since each file is only a few bytes to begin with, I'm not
                    442:         * concerned that this is going to tank any machines.
                    443:         */
                    444:
1.140     schwarze  445:        if (curp->gzip == 0 && S_ISREG(st.st_mode)) {
1.131     schwarze  446:                if (st.st_size > 0x7fffffff) {
1.206     schwarze  447:                        mandoc_msg(MANDOCERR_TOOLARGE, 0, 0, NULL);
1.214   ! schwarze  448:                        return -1;
1.1       kristaps  449:                }
                    450:                *with_mmap = 1;
                    451:                fb->sz = (size_t)st.st_size;
1.37      schwarze  452:                fb->buf = mmap(NULL, fb->sz, PROT_READ, MAP_SHARED, fd, 0);
1.1       kristaps  453:                if (fb->buf != MAP_FAILED)
1.214   ! schwarze  454:                        return 0;
1.1       kristaps  455:        }
                    456:
1.140     schwarze  457:        if (curp->gzip) {
1.194     schwarze  458:                /*
                    459:                 * Duplicating the file descriptor is required
                    460:                 * because we will have to call gzclose(3)
                    461:                 * to free memory used internally by zlib,
                    462:                 * but that will also close the file descriptor,
                    463:                 * which this function must not do.
                    464:                 */
                    465:                if ((fd = dup(fd)) == -1) {
1.214   ! schwarze  466:                        mandoc_msg(MANDOCERR_DUP, 0, 0,
        !           467:                            "%s", strerror(errno));
        !           468:                        return -1;
1.194     schwarze  469:                }
1.192     schwarze  470:                if ((gz = gzdopen(fd, "rb")) == NULL) {
1.214   ! schwarze  471:                        mandoc_msg(MANDOCERR_GZDOPEN, 0, 0,
        !           472:                            "%s", strerror(errno));
1.194     schwarze  473:                        close(fd);
1.214   ! schwarze  474:                        return -1;
1.192     schwarze  475:                }
1.140     schwarze  476:        } else
                    477:                gz = NULL;
                    478:
1.1       kristaps  479:        /*
                    480:         * If this isn't a regular file (like, say, stdin), then we must
                    481:         * go the old way and just read things in bit by bit.
                    482:         */
                    483:
                    484:        *with_mmap = 0;
                    485:        off = 0;
1.214   ! schwarze  486:        retval = -1;
1.1       kristaps  487:        fb->sz = 0;
                    488:        fb->buf = NULL;
                    489:        for (;;) {
                    490:                if (off == fb->sz) {
                    491:                        if (fb->sz == (1U << 31)) {
1.206     schwarze  492:                                mandoc_msg(MANDOCERR_TOOLARGE, 0, 0, NULL);
1.1       kristaps  493:                                break;
                    494:                        }
                    495:                        resize_buf(fb, 65536);
                    496:                }
1.140     schwarze  497:                ssz = curp->gzip ?
                    498:                    gzread(gz, fb->buf + (int)off, fb->sz - off) :
                    499:                    read(fd, fb->buf + (int)off, fb->sz - off);
1.1       kristaps  500:                if (ssz == 0) {
                    501:                        fb->sz = off;
1.214   ! schwarze  502:                        retval = 0;
1.194     schwarze  503:                        break;
1.1       kristaps  504:                }
1.192     schwarze  505:                if (ssz == -1) {
1.194     schwarze  506:                        if (curp->gzip)
                    507:                                (void)gzerror(gz, &gzerrnum);
1.214   ! schwarze  508:                        mandoc_msg(MANDOCERR_READ, 0, 0, "%s",
1.194     schwarze  509:                            curp->gzip && gzerrnum != Z_ERRNO ?
                    510:                            zError(gzerrnum) : strerror(errno));
1.192     schwarze  511:                        break;
                    512:                }
1.1       kristaps  513:                off += (size_t)ssz;
                    514:        }
                    515:
1.194     schwarze  516:        if (curp->gzip && (gzerrnum = gzclose(gz)) != Z_OK)
1.214   ! schwarze  517:                mandoc_msg(MANDOCERR_GZCLOSE, 0, 0, "%s",
1.194     schwarze  518:                    gzerrnum == Z_ERRNO ? strerror(errno) :
                    519:                    zError(gzerrnum));
1.214   ! schwarze  520:        if (retval == -1) {
1.194     schwarze  521:                free(fb->buf);
                    522:                fb->buf = NULL;
                    523:        }
                    524:        return retval;
1.1       kristaps  525: }
                    526:
                    527: static void
                    528: mparse_end(struct mparse *curp)
                    529: {
1.209     schwarze  530:        if (curp->man->meta.macroset == MACROSET_NONE)
                    531:                curp->man->meta.macroset = MACROSET_MAN;
                    532:        if (curp->man->meta.macroset == MACROSET_MDOC)
1.135     schwarze  533:                mdoc_endparse(curp->man);
                    534:        else
1.111     schwarze  535:                man_endparse(curp->man);
1.1       kristaps  536:        roff_endparse(curp->roff);
                    537: }
                    538:
1.205     schwarze  539: /*
                    540:  * Read the whole file into memory and call the parsers.
                    541:  * Called recursively when an .so request is encountered.
                    542:  */
                    543: void
                    544: mparse_readfd(struct mparse *curp, int fd, const char *filename)
1.28      joerg     545: {
1.205     schwarze  546:        static int       recursion_depth;
                    547:
                    548:        struct buf       blk;
                    549:        struct buf      *save_primary;
                    550:        const char      *save_filename;
1.95      schwarze  551:        size_t           offset;
1.205     schwarze  552:        int              save_filenc, save_lineno;
                    553:        int              with_mmap;
1.36      schwarze  554:
1.205     schwarze  555:        if (recursion_depth > 64) {
1.206     schwarze  556:                mandoc_msg(MANDOCERR_ROFFLOOP, curp->line, 0, NULL);
1.36      schwarze  557:                return;
                    558:        }
1.214   ! schwarze  559:        if (read_whole_file(curp, fd, &blk, &with_mmap) == -1)
1.205     schwarze  560:                return;
                    561:
                    562:        /*
                    563:         * Save some properties of the parent file.
                    564:         */
                    565:
                    566:        save_primary = curp->primary;
                    567:        save_filenc = curp->filenc;
                    568:        save_lineno = curp->line;
                    569:        save_filename = mandoc_msg_getinfilename();
1.28      joerg     570:
1.83      schwarze  571:        curp->primary = &blk;
1.205     schwarze  572:        curp->filenc = curp->options & (MPARSE_UTF8 | MPARSE_LATIN1);
1.28      joerg     573:        curp->line = 1;
1.205     schwarze  574:        mandoc_msg_setinfilename(filename);
1.28      joerg     575:
1.93      schwarze  576:        /* Skip an UTF-8 byte order mark. */
                    577:        if (curp->filenc & MPARSE_UTF8 && blk.sz > 2 &&
                    578:            (unsigned char)blk.buf[0] == 0xef &&
                    579:            (unsigned char)blk.buf[1] == 0xbb &&
                    580:            (unsigned char)blk.buf[2] == 0xbf) {
1.95      schwarze  581:                offset = 3;
1.93      schwarze  582:                curp->filenc &= ~MPARSE_LATIN1;
1.95      schwarze  583:        } else
                    584:                offset = 0;
1.93      schwarze  585:
1.205     schwarze  586:        recursion_depth++;
1.95      schwarze  587:        mparse_buf_r(curp, blk, offset, 1);
1.111     schwarze  588:        if (--recursion_depth == 0)
1.28      joerg     589:                mparse_end(curp);
                    590:
1.205     schwarze  591:        /*
                    592:         * Clean up and restore saved parent properties.
                    593:         */
1.28      joerg     594:
1.205     schwarze  595:        if (with_mmap)
                    596:                munmap(blk.buf, blk.sz);
                    597:        else
                    598:                free(blk.buf);
1.1       kristaps  599:
1.205     schwarze  600:        curp->primary = save_primary;
                    601:        curp->filenc = save_filenc;
                    602:        curp->line = save_lineno;
                    603:        if (save_filename != NULL)
                    604:                mandoc_msg_setinfilename(save_filename);
1.82      schwarze  605: }
                    606:
1.148     schwarze  607: int
                    608: mparse_open(struct mparse *curp, const char *file)
1.82      schwarze  609: {
                    610:        char             *cp;
1.211     schwarze  611:        int               fd, save_errno;
1.82      schwarze  612:
1.140     schwarze  613:        cp = strrchr(file, '.');
                    614:        curp->gzip = (cp != NULL && ! strcmp(cp + 1, "gz"));
1.98      schwarze  615:
1.140     schwarze  616:        /* First try to use the filename as it is. */
1.98      schwarze  617:
1.148     schwarze  618:        if ((fd = open(file, O_RDONLY)) != -1)
                    619:                return fd;
1.98      schwarze  620:
1.140     schwarze  621:        /*
                    622:         * If that doesn't work and the filename doesn't
                    623:         * already  end in .gz, try appending .gz.
                    624:         */
1.98      schwarze  625:
1.140     schwarze  626:        if ( ! curp->gzip) {
1.211     schwarze  627:                save_errno = errno;
1.98      schwarze  628:                mandoc_asprintf(&cp, "%s.gz", file);
1.149     schwarze  629:                fd = open(cp, O_RDONLY);
1.108     schwarze  630:                free(cp);
1.211     schwarze  631:                errno = save_errno;
1.148     schwarze  632:                if (fd != -1) {
1.140     schwarze  633:                        curp->gzip = 1;
1.148     schwarze  634:                        return fd;
1.82      schwarze  635:                }
                    636:        }
                    637:
1.140     schwarze  638:        /* Neither worked, give up. */
1.97      schwarze  639:
1.148     schwarze  640:        return -1;
1.1       kristaps  641: }
                    642:
                    643: struct mparse *
1.203     schwarze  644: mparse_alloc(int options, enum mandoc_os os_e, const char *os_s)
1.1       kristaps  645: {
                    646:        struct mparse   *curp;
1.10      kristaps  647:
1.1       kristaps  648:        curp = mandoc_calloc(1, sizeof(struct mparse));
                    649:
1.44      schwarze  650:        curp->options = options;
1.179     schwarze  651:        curp->os_s = os_s;
1.1       kristaps  652:
1.207     schwarze  653:        curp->roff = roff_alloc(options);
                    654:        curp->man = roff_man_alloc(curp->roff, curp->os_s,
1.137     schwarze  655:                curp->options & MPARSE_QUICK ? 1 : 0);
1.136     schwarze  656:        if (curp->options & MPARSE_MDOC) {
1.209     schwarze  657:                curp->man->meta.macroset = MACROSET_MDOC;
1.164     schwarze  658:                if (curp->man->mdocmac == NULL)
                    659:                        curp->man->mdocmac = roffhash_alloc(MDOC_Dd, MDOC_MAX);
1.137     schwarze  660:        } else if (curp->options & MPARSE_MAN) {
1.209     schwarze  661:                curp->man->meta.macroset = MACROSET_MAN;
1.164     schwarze  662:                if (curp->man->manmac == NULL)
                    663:                        curp->man->manmac = roffhash_alloc(MAN_TH, MAN_MAX);
1.136     schwarze  664:        }
1.209     schwarze  665:        curp->man->meta.first->tok = TOKEN_NONE;
1.179     schwarze  666:        curp->man->meta.os_e = os_e;
1.142     schwarze  667:        return curp;
1.1       kristaps  668: }
                    669:
                    670: void
                    671: mparse_reset(struct mparse *curp)
                    672: {
                    673:        roff_reset(curp->roff);
1.150     schwarze  674:        roff_man_reset(curp->man);
1.198     schwarze  675:        free_buf_list(curp->secondary);
                    676:        curp->secondary = NULL;
1.159     schwarze  677:        curp->gzip = 0;
1.1       kristaps  678: }
                    679:
                    680: void
                    681: mparse_free(struct mparse *curp)
                    682: {
1.164     schwarze  683:        roffhash_free(curp->man->mdocmac);
                    684:        roffhash_free(curp->man->manmac);
1.137     schwarze  685:        roff_man_free(curp->man);
1.160     schwarze  686:        roff_free(curp->roff);
1.198     schwarze  687:        free_buf_list(curp->secondary);
1.1       kristaps  688:        free(curp);
                    689: }
                    690:
1.209     schwarze  691: struct roff_meta *
                    692: mparse_result(struct mparse *curp)
1.1       kristaps  693: {
1.210     schwarze  694:        roff_state_reset(curp->man);
1.209     schwarze  695:        if (curp->options & MPARSE_VALIDATE) {
                    696:                if (curp->man->meta.macroset == MACROSET_MDOC)
                    697:                        mdoc_validate(curp->man);
                    698:                else
                    699:                        man_validate(curp->man);
1.45      schwarze  700:        }
1.209     schwarze  701:        return &curp->man->meta;
1.24      kristaps  702: }
                    703:
                    704: void
1.198     schwarze  705: mparse_copy(const struct mparse *p)
1.24      kristaps  706: {
1.198     schwarze  707:        struct buf      *buf;
1.24      kristaps  708:
1.198     schwarze  709:        for (buf = p->secondary; buf != NULL; buf = buf->next)
                    710:                puts(buf->buf);
1.1       kristaps  711: }

CVSweb