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

Annotation of mandoc/read.c, Revision 1.2

1.2     ! kristaps    1: /*     $Id: read.c,v 1.1 2011/03/20 11:41:24 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2008, 2009, 2010, 2011 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  * Copyright (c) 2010, 2011 Ingo Schwarze <schwarze@openbsd.org>
                      5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
1.2     ! kristaps   18: #include <sys/stat.h>
1.1       kristaps   19: #include <sys/mman.h>
                     20:
                     21: #include <assert.h>
                     22: #include <ctype.h>
                     23: #include <fcntl.h>
                     24: #include <stdio.h>
                     25: #include <stdlib.h>
                     26: #include <string.h>
                     27: #include <unistd.h>
                     28:
                     29: #include "mandoc.h"
                     30: #include "mdoc.h"
                     31: #include "man.h"
                     32: #include "roff.h"
                     33:
                     34: #ifndef MAP_FILE
                     35: #define        MAP_FILE        0
                     36: #endif
                     37:
                     38: #define        REPARSE_LIMIT   1000
                     39:
                     40: struct buf {
                     41:        char             *buf; /* binary input buffer */
                     42:        size_t            sz; /* size of binary buffer */
                     43: };
                     44:
                     45: struct mparse {
                     46:        enum mandoclevel  file_status; /* status of current parse */
                     47:        int               line; /* line number in the file */
                     48:        enum mparset      inttype; /* which parser to use */
                     49:        struct man       *pman; /* persistent man parser */
                     50:        struct mdoc      *pmdoc; /* persistent mdoc parser */
                     51:        struct man       *man; /* man parser */
                     52:        struct mdoc      *mdoc; /* mdoc parser */
                     53:        struct roff      *roff; /* roff parser (!NULL) */
                     54:        struct regset     regs; /* roff registers */
                     55:        int               reparse_count; /* finite interp. stack */
                     56:        mandocmsg         mmsg; /* warning/error message handler */
                     57:        void             *arg; /* argument to mmsg */
                     58:        mevt_open         evt_open; /* file-open event */
                     59:        mevt_close        evt_close; /* file-close event */
                     60:        const char       *svfile;
                     61: };
                     62:
                     63: static void      resize_buf(struct buf *, size_t);
                     64: static void      mparse_buf_r(struct mparse *, struct buf, int);
                     65: static void      mparse_readfd_r(struct mparse *, int, const char *, int);
                     66: static void      pset(const char *, int, struct mparse *);
                     67: static void      pdesc(struct mparse *, const char *, int);
                     68: static int       read_whole_file(const char *, int, struct buf *, int *);
                     69: static void      mparse_end(struct mparse *);
                     70:
                     71: static void
                     72: resize_buf(struct buf *buf, size_t initial)
                     73: {
                     74:
                     75:        buf->sz = buf->sz > initial/2 ? 2 * buf->sz : initial;
                     76:        buf->buf = mandoc_realloc(buf->buf, buf->sz);
                     77: }
                     78:
                     79: static void
                     80: pset(const char *buf, int pos, struct mparse *curp)
                     81: {
                     82:        int              i;
                     83:
                     84:        /*
                     85:         * Try to intuit which kind of manual parser should be used.  If
                     86:         * passed in by command-line (-man, -mdoc), then use that
                     87:         * explicitly.  If passed as -mandoc, then try to guess from the
                     88:         * line: either skip dot-lines, use -mdoc when finding `.Dt', or
                     89:         * default to -man, which is more lenient.
                     90:         *
                     91:         * Separate out pmdoc/pman from mdoc/man: the first persists
                     92:         * through all parsers, while the latter is used per-parse.
                     93:         */
                     94:
                     95:        if ('.' == buf[0] || '\'' == buf[0]) {
                     96:                for (i = 1; buf[i]; i++)
                     97:                        if (' ' != buf[i] && '\t' != buf[i])
                     98:                                break;
                     99:                if ('\0' == buf[i])
                    100:                        return;
                    101:        }
                    102:
                    103:        switch (curp->inttype) {
                    104:        case (MPARSE_MDOC):
                    105:                if (NULL == curp->pmdoc)
                    106:                        curp->pmdoc = mdoc_alloc
                    107:                                (&curp->regs, curp->arg, curp->mmsg);
                    108:                assert(curp->pmdoc);
                    109:                curp->mdoc = curp->pmdoc;
                    110:                return;
                    111:        case (MPARSE_MAN):
                    112:                if (NULL == curp->pman)
                    113:                        curp->pman = man_alloc
                    114:                                (&curp->regs, curp->arg, curp->mmsg);
                    115:                assert(curp->pman);
                    116:                curp->man = curp->pman;
                    117:                return;
                    118:        default:
                    119:                break;
                    120:        }
                    121:
                    122:        if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3))  {
                    123:                if (NULL == curp->pmdoc)
                    124:                        curp->pmdoc = mdoc_alloc
                    125:                                (&curp->regs, curp->arg, curp->mmsg);
                    126:                assert(curp->pmdoc);
                    127:                curp->mdoc = curp->pmdoc;
                    128:                return;
                    129:        }
                    130:
                    131:        if (NULL == curp->pman)
                    132:                curp->pman = man_alloc
                    133:                        (&curp->regs, curp->arg, curp->mmsg);
                    134:        assert(curp->pman);
                    135:        curp->man = curp->pman;
                    136: }
                    137:
                    138: /*
                    139:  * Main parse routine for an opened file.  This is called for each
                    140:  * opened file and simply loops around the full input file, possibly
                    141:  * nesting (i.e., with `so').
                    142:  */
                    143: static void
                    144: mparse_buf_r(struct mparse *curp, struct buf blk, int start)
                    145: {
                    146:        const struct tbl_span   *span;
                    147:        struct buf       ln;
                    148:        enum rofferr     rr;
                    149:        int              i, of, rc;
                    150:        int              pos; /* byte number in the ln buffer */
                    151:        int              lnn; /* line number in the real file */
                    152:        unsigned char    c;
                    153:
                    154:        memset(&ln, 0, sizeof(struct buf));
                    155:
                    156:        lnn = curp->line;
                    157:        pos = 0;
                    158:
                    159:        for (i = 0; i < (int)blk.sz; ) {
                    160:                if (0 == pos && '\0' == blk.buf[i])
                    161:                        break;
                    162:
                    163:                if (start) {
                    164:                        curp->line = lnn;
                    165:                        curp->reparse_count = 0;
                    166:                }
                    167:
                    168:                while (i < (int)blk.sz && (start || '\0' != blk.buf[i])) {
                    169:
                    170:                        /*
                    171:                         * When finding an unescaped newline character,
                    172:                         * leave the character loop to process the line.
                    173:                         * Skip a preceding carriage return, if any.
                    174:                         */
                    175:
                    176:                        if ('\r' == blk.buf[i] && i + 1 < (int)blk.sz &&
                    177:                            '\n' == blk.buf[i + 1])
                    178:                                ++i;
                    179:                        if ('\n' == blk.buf[i]) {
                    180:                                ++i;
                    181:                                ++lnn;
                    182:                                break;
                    183:                        }
                    184:
                    185:                        /*
                    186:                         * Warn about bogus characters.  If you're using
                    187:                         * non-ASCII encoding, you're screwing your
                    188:                         * readers.  Since I'd rather this not happen,
                    189:                         * I'll be helpful and drop these characters so
                    190:                         * we don't display gibberish.  Note to manual
                    191:                         * writers: use special characters.
                    192:                         */
                    193:
                    194:                        c = (unsigned char) blk.buf[i];
                    195:
                    196:                        if ( ! (isascii(c) &&
                    197:                                        (isgraph(c) || isblank(c)))) {
                    198:                                curp->mmsg(MANDOCERR_BADCHAR, curp->arg,
                    199:                                                curp->line, pos, "ignoring byte");
                    200:                                i++;
                    201:                                continue;
                    202:                        }
                    203:
                    204:                        /* Trailing backslash = a plain char. */
                    205:
                    206:                        if ('\\' != blk.buf[i] || i + 1 == (int)blk.sz) {
                    207:                                if (pos >= (int)ln.sz)
                    208:                                        resize_buf(&ln, 256);
                    209:                                ln.buf[pos++] = blk.buf[i++];
                    210:                                continue;
                    211:                        }
                    212:
                    213:                        /*
                    214:                         * Found escape and at least one other character.
                    215:                         * When it's a newline character, skip it.
                    216:                         * When there is a carriage return in between,
                    217:                         * skip that one as well.
                    218:                         */
                    219:
                    220:                        if ('\r' == blk.buf[i + 1] && i + 2 < (int)blk.sz &&
                    221:                            '\n' == blk.buf[i + 2])
                    222:                                ++i;
                    223:                        if ('\n' == blk.buf[i + 1]) {
                    224:                                i += 2;
                    225:                                ++lnn;
                    226:                                continue;
                    227:                        }
                    228:
                    229:                        if ('"' == blk.buf[i + 1]) {
                    230:                                i += 2;
                    231:                                /* Comment, skip to end of line */
                    232:                                for (; i < (int)blk.sz; ++i) {
                    233:                                        if ('\n' == blk.buf[i]) {
                    234:                                                ++i;
                    235:                                                ++lnn;
                    236:                                                break;
                    237:                                        }
                    238:                                }
                    239:
                    240:                                /* Backout trailing whitespaces */
                    241:                                for (; pos > 0; --pos) {
                    242:                                        if (ln.buf[pos - 1] != ' ')
                    243:                                                break;
                    244:                                        if (pos > 2 && ln.buf[pos - 2] == '\\')
                    245:                                                break;
                    246:                                }
                    247:                                break;
                    248:                        }
                    249:
                    250:                        /* Some other escape sequence, copy & cont. */
                    251:
                    252:                        if (pos + 1 >= (int)ln.sz)
                    253:                                resize_buf(&ln, 256);
                    254:
                    255:                        ln.buf[pos++] = blk.buf[i++];
                    256:                        ln.buf[pos++] = blk.buf[i++];
                    257:                }
                    258:
                    259:                if (pos >= (int)ln.sz)
                    260:                        resize_buf(&ln, 256);
                    261:
                    262:                ln.buf[pos] = '\0';
                    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:
                    275: rerun:
                    276:                rr = roff_parseln
                    277:                        (curp->roff, curp->line,
                    278:                         &ln.buf, &ln.sz, of, &of);
                    279:
                    280:                switch (rr) {
                    281:                case (ROFF_REPARSE):
                    282:                        if (REPARSE_LIMIT >= ++curp->reparse_count)
                    283:                                mparse_buf_r(curp, ln, 0);
                    284:                        else
                    285:                                curp->mmsg(MANDOCERR_ROFFLOOP, curp->arg,
                    286:                                        curp->line, pos, NULL);
                    287:                        pos = 0;
                    288:                        continue;
                    289:                case (ROFF_APPEND):
                    290:                        pos = (int)strlen(ln.buf);
                    291:                        continue;
                    292:                case (ROFF_RERUN):
                    293:                        goto rerun;
                    294:                case (ROFF_IGN):
                    295:                        pos = 0;
                    296:                        continue;
                    297:                case (ROFF_ERR):
                    298:                        assert(MANDOCLEVEL_FATAL <= curp->file_status);
                    299:                        break;
                    300:                case (ROFF_SO):
                    301:                        mparse_readfd_r(curp, -1, ln.buf + of, 1);
                    302:                        if (MANDOCLEVEL_FATAL <= curp->file_status)
                    303:                                break;
                    304:                        pos = 0;
                    305:                        continue;
                    306:                default:
                    307:                        break;
                    308:                }
                    309:
                    310:                /*
                    311:                 * If we encounter errors in the recursive parse, make
                    312:                 * sure we don't continue parsing.
                    313:                 */
                    314:
                    315:                if (MANDOCLEVEL_FATAL <= curp->file_status)
                    316:                        break;
                    317:
                    318:                /*
                    319:                 * If input parsers have not been allocated, do so now.
                    320:                 * We keep these instanced betwen parsers, but set them
                    321:                 * locally per parse routine since we can use different
                    322:                 * parsers with each one.
                    323:                 */
                    324:
                    325:                if ( ! (curp->man || curp->mdoc))
                    326:                        pset(ln.buf + of, pos - of, curp);
                    327:
                    328:                /*
                    329:                 * Lastly, push down into the parsers themselves.  One
                    330:                 * of these will have already been set in the pset()
                    331:                 * routine.
                    332:                 * If libroff returns ROFF_TBL, then add it to the
                    333:                 * currently open parse.  Since we only get here if
                    334:                 * there does exist data (see tbl_data.c), we're
                    335:                 * guaranteed that something's been allocated.
                    336:                 * Do the same for ROFF_EQN.
                    337:                 */
                    338:
                    339:                rc = -1;
                    340:
                    341:                if (ROFF_TBL == rr)
                    342:                        while (NULL != (span = roff_span(curp->roff))) {
                    343:                                rc = curp->man ?
                    344:                                        man_addspan(curp->man, span) :
                    345:                                        mdoc_addspan(curp->mdoc, span);
                    346:                                if (0 == rc)
                    347:                                        break;
                    348:                        }
                    349:                else if (ROFF_EQN == rr)
                    350:                        rc = curp->mdoc ?
                    351:                                mdoc_addeqn(curp->mdoc,
                    352:                                        roff_eqn(curp->roff)) :
                    353:                                man_addeqn(curp->man,
                    354:                                        roff_eqn(curp->roff));
                    355:                else if (curp->man || curp->mdoc)
                    356:                        rc = curp->man ?
                    357:                                man_parseln(curp->man,
                    358:                                        curp->line, ln.buf, of) :
                    359:                                mdoc_parseln(curp->mdoc,
                    360:                                        curp->line, ln.buf, of);
                    361:
                    362:                if (0 == rc) {
                    363:                        assert(MANDOCLEVEL_FATAL <= curp->file_status);
                    364:                        break;
                    365:                }
                    366:
                    367:                /* Temporary buffers typically are not full. */
                    368:
                    369:                if (0 == start && '\0' == blk.buf[i])
                    370:                        break;
                    371:
                    372:                /* Start the next input line. */
                    373:
                    374:                pos = 0;
                    375:        }
                    376:
                    377:        free(ln.buf);
                    378: }
                    379:
                    380: static void
                    381: pdesc(struct mparse *curp, const char *file, int fd)
                    382: {
                    383:        struct buf       blk;
                    384:        int              with_mmap;
                    385:
                    386:        /*
                    387:         * Run for each opened file; may be called more than once for
                    388:         * each full parse sequence if the opened file is nested (i.e.,
                    389:         * from `so').  Simply sucks in the whole file and moves into
                    390:         * the parse phase for the file.
                    391:         */
                    392:
                    393:        if ( ! read_whole_file(file, fd, &blk, &with_mmap)) {
                    394:                curp->file_status = MANDOCLEVEL_SYSERR;
                    395:                return;
                    396:        }
                    397:
                    398:        /* Line number is per-file. */
                    399:
                    400:        curp->line = 1;
                    401:
                    402:        mparse_buf_r(curp, blk, 1);
                    403:
                    404:        if (with_mmap)
                    405:                munmap(blk.buf, blk.sz);
                    406:        else
                    407:                free(blk.buf);
                    408: }
                    409:
                    410: static int
                    411: read_whole_file(const char *file, int fd, struct buf *fb, int *with_mmap)
                    412: {
                    413:        struct stat      st;
                    414:        size_t           off;
                    415:        ssize_t          ssz;
                    416:
                    417:        if (-1 == fstat(fd, &st)) {
                    418:                perror(file);
                    419:                return(0);
                    420:        }
                    421:
                    422:        /*
                    423:         * If we're a regular file, try just reading in the whole entry
                    424:         * via mmap().  This is faster than reading it into blocks, and
                    425:         * since each file is only a few bytes to begin with, I'm not
                    426:         * concerned that this is going to tank any machines.
                    427:         */
                    428:
                    429:        if (S_ISREG(st.st_mode)) {
                    430:                if (st.st_size >= (1U << 31)) {
                    431:                        fprintf(stderr, "%s: input too large\n", file);
                    432:                        return(0);
                    433:                }
                    434:                *with_mmap = 1;
                    435:                fb->sz = (size_t)st.st_size;
                    436:                fb->buf = mmap(NULL, fb->sz, PROT_READ,
                    437:                                MAP_FILE|MAP_SHARED, fd, 0);
                    438:                if (fb->buf != MAP_FAILED)
                    439:                        return(1);
                    440:        }
                    441:
                    442:        /*
                    443:         * If this isn't a regular file (like, say, stdin), then we must
                    444:         * go the old way and just read things in bit by bit.
                    445:         */
                    446:
                    447:        *with_mmap = 0;
                    448:        off = 0;
                    449:        fb->sz = 0;
                    450:        fb->buf = NULL;
                    451:        for (;;) {
                    452:                if (off == fb->sz) {
                    453:                        if (fb->sz == (1U << 31)) {
                    454:                                fprintf(stderr, "%s: input too large\n", file);
                    455:                                break;
                    456:                        }
                    457:                        resize_buf(fb, 65536);
                    458:                }
                    459:                ssz = read(fd, fb->buf + (int)off, fb->sz - off);
                    460:                if (ssz == 0) {
                    461:                        fb->sz = off;
                    462:                        return(1);
                    463:                }
                    464:                if (ssz == -1) {
                    465:                        perror(file);
                    466:                        break;
                    467:                }
                    468:                off += (size_t)ssz;
                    469:        }
                    470:
                    471:        free(fb->buf);
                    472:        fb->buf = NULL;
                    473:        return(0);
                    474: }
                    475:
                    476: static void
                    477: mparse_end(struct mparse *curp)
                    478: {
                    479:
                    480:        if (MANDOCLEVEL_FATAL <= curp->file_status)
                    481:                return;
                    482:
                    483:        if (curp->mdoc && ! mdoc_endparse(curp->mdoc)) {
                    484:                assert(MANDOCLEVEL_FATAL <= curp->file_status);
                    485:                return;
                    486:        }
                    487:
                    488:        if (curp->man && ! man_endparse(curp->man)) {
                    489:                assert(MANDOCLEVEL_FATAL <= curp->file_status);
                    490:                return;
                    491:        }
                    492:
                    493: #if 0
                    494:        /* NOTE a parser may not have been assigned, yet. */
                    495:
                    496:        if ( ! (curp->man || curp->mdoc)) {
                    497:                /* FIXME: make into an mandoc.h error. */
                    498:                fprintf(stderr, "%s: Not a manual\n", curp->file);
                    499:                curp->file_status = MANDOCLEVEL_FATAL;
                    500:                goto cleanup;
                    501:        }
                    502: #endif
                    503:
                    504:        roff_endparse(curp->roff);
                    505: }
                    506:
                    507: static void
                    508: mparse_readfd_r(struct mparse *curp, int fd, const char *file, int re)
                    509: {
                    510:        const char      *svfile;
                    511:
                    512:        if ( ! (*curp->evt_open)(curp->arg, file)) {
                    513:                curp->file_status = MANDOCLEVEL_SYSERR;
                    514:                return;
                    515:        }
                    516:
                    517:        if (-1 == fd)
                    518:                if (-1 == (fd = open(file, O_RDONLY, 0))) {
                    519:                        perror(file);
                    520:                        curp->file_status = MANDOCLEVEL_SYSERR;
                    521:                        return;
                    522:                }
                    523:
                    524:        svfile = curp->svfile;
                    525:        curp->svfile = file;
                    526:
                    527:        pdesc(curp, file, fd);
                    528:
                    529:        if (0 == re && MANDOCLEVEL_FATAL > curp->file_status)
                    530:                mparse_end(curp);
                    531:
                    532:        if (STDIN_FILENO != fd && -1 == close(fd))
                    533:                perror(file);
                    534:
                    535:        (*curp->evt_close)(curp->arg, svfile);
                    536:        curp->svfile = svfile;
                    537: }
                    538:
                    539: enum mandoclevel
                    540: mparse_readfd(struct mparse *curp, int fd, const char *file)
                    541: {
                    542:
                    543:        mparse_readfd_r(curp, fd, file, 0);
                    544:        return(curp->file_status);
                    545: }
                    546:
                    547: void
                    548: mparse_setstatus(struct mparse *curp, enum mandoclevel lvl)
                    549: {
                    550:
                    551:        if (curp->file_status < lvl)
                    552:                curp->file_status = lvl;
                    553: }
                    554:
                    555: struct mparse *
                    556: mparse_alloc(enum mparset inttype, mevt_open eopen,
                    557:                mevt_close eclose, mandocmsg mmsg, void *arg)
                    558: {
                    559:        struct mparse   *curp;
                    560:
                    561:        curp = mandoc_calloc(1, sizeof(struct mparse));
                    562:
                    563:        curp->mmsg = mmsg;
                    564:        curp->arg = arg;
                    565:        curp->inttype = inttype;
                    566:        curp->evt_open = eopen;
                    567:        curp->evt_close = eclose;
                    568:
                    569:        curp->roff = roff_alloc(&curp->regs, arg, mmsg);
                    570:        return(curp);
                    571: }
                    572:
                    573: void
                    574: mparse_reset(struct mparse *curp)
                    575: {
                    576:
                    577:        memset(&curp->regs, 0, sizeof(struct regset));
                    578:
                    579:        roff_reset(curp->roff);
                    580:
                    581:        if (curp->mdoc)
                    582:                mdoc_reset(curp->mdoc);
                    583:        if (curp->man)
                    584:                man_reset(curp->man);
                    585:
                    586:        curp->file_status = MANDOCLEVEL_OK;
                    587:        curp->mdoc = NULL;
                    588:        curp->man = NULL;
                    589: }
                    590:
                    591: void
                    592: mparse_free(struct mparse *curp)
                    593: {
                    594:
                    595:        if (curp->pmdoc)
                    596:                mdoc_free(curp->pmdoc);
                    597:        if (curp->pman)
                    598:                man_free(curp->pman);
                    599:        if (curp->roff)
                    600:                roff_free(curp->roff);
                    601:
                    602:        free(curp);
                    603: }
                    604:
                    605: void
                    606: mparse_result(struct mparse *curp, struct mdoc **mdoc, struct man **man)
                    607: {
                    608:
                    609:        *mdoc = curp->mdoc;
                    610:        *man = curp->man;
                    611: }

CVSweb