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

Annotation of mandoc/read.c, Revision 1.208

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