[BACK]Return to main.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/main.c, Revision 1.53

1.53    ! schwarze    1: /*     $Id: main.c,v 1.52 2010/10/26 22:28:56 schwarze Exp $ */
1.1       kristaps    2: /*
1.42      schwarze    3:  * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
                      4:  * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.2       schwarze    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.2       schwarze   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.
1.1       kristaps   17:  */
1.27      schwarze   18: #include <sys/types.h>
                     19: #include <sys/mman.h>
1.1       kristaps   20: #include <sys/stat.h>
                     21:
                     22: #include <assert.h>
1.43      schwarze   23: #include <ctype.h>
1.1       kristaps   24: #include <fcntl.h>
                     25: #include <stdio.h>
1.17      schwarze   26: #include <stdint.h>
1.1       kristaps   27: #include <stdlib.h>
                     28: #include <string.h>
                     29: #include <unistd.h>
                     30:
1.30      schwarze   31: #include "mandoc.h"
1.38      schwarze   32: #include "main.h"
1.1       kristaps   33: #include "mdoc.h"
                     34: #include "man.h"
1.30      schwarze   35: #include "roff.h"
1.17      schwarze   36:
                     37: #define        UNCONST(a)      ((void *)(uintptr_t)(const void *)(a))
1.1       kristaps   38:
1.16      schwarze   39: typedef        void            (*out_mdoc)(void *, const struct mdoc *);
                     40: typedef        void            (*out_man)(void *, const struct man *);
1.1       kristaps   41: typedef        void            (*out_free)(void *);
                     42:
                     43: struct buf {
                     44:        char             *buf;
                     45:        size_t            sz;
                     46: };
                     47:
                     48: enum   intt {
                     49:        INTT_AUTO,
                     50:        INTT_MDOC,
                     51:        INTT_MAN
                     52: };
                     53:
                     54: enum   outt {
                     55:        OUTT_ASCII = 0,
                     56:        OUTT_TREE,
1.17      schwarze   57:        OUTT_HTML,
1.21      schwarze   58:        OUTT_XHTML,
1.36      schwarze   59:        OUTT_LINT,
1.43      schwarze   60:        OUTT_PS,
                     61:        OUTT_PDF
1.1       kristaps   62: };
                     63:
                     64: struct curparse {
                     65:        const char       *file;         /* Current parse. */
                     66:        int               fd;           /* Current parse. */
1.45      schwarze   67:        enum mandoclevel  wlevel;       /* Ignore messages below this. */
                     68:        int               wstop;        /* Stop after a file with a warning. */
1.33      schwarze   69:        enum intt         inttype;      /* which parser to use */
                     70:        struct man       *man;          /* man parser */
                     71:        struct mdoc      *mdoc;         /* mdoc parser */
                     72:        struct roff      *roff;         /* roff parser (!NULL) */
1.38      schwarze   73:        struct regset     regs;         /* roff registers */
1.33      schwarze   74:        enum outt         outtype;      /* which output to use */
                     75:        out_mdoc          outmdoc;      /* mdoc output ptr */
                     76:        out_man           outman;       /* man output ptr */
                     77:        out_free          outfree;      /* free output ptr */
                     78:        void             *outdata;      /* data for output */
                     79:        char              outopts[BUFSIZ]; /* buf of output opts */
                     80: };
                     81:
1.45      schwarze   82: static const char * const      mandoclevels[MANDOCLEVEL_MAX] = {
                     83:        "SUCCESS",
                     84:        "RESERVED",
                     85:        "WARNING",
                     86:        "ERROR",
                     87:        "FATAL",
                     88:        "BADARG",
                     89:        "SYSERR"
                     90: };
                     91:
                     92: static const enum mandocerr    mandoclimits[MANDOCLEVEL_MAX] = {
                     93:        MANDOCERR_OK,
                     94:        MANDOCERR_WARNING,
                     95:        MANDOCERR_WARNING,
                     96:        MANDOCERR_ERROR,
                     97:        MANDOCERR_FATAL,
                     98:        MANDOCERR_MAX,
                     99:        MANDOCERR_MAX
                    100: };
                    101:
1.33      schwarze  102: static const char * const      mandocerrs[MANDOCERR_MAX] = {
                    103:        "ok",
1.40      schwarze  104:
                    105:        "generic warning",
                    106:
1.53    ! schwarze  107:        /* related to the prologue */
        !           108:        "no title in document",
        !           109:        "document title should be all caps",
        !           110:        "unknown manual section",
        !           111:        "cannot parse date argument",
        !           112:        "prologue macros out of order",
        !           113:        "duplicate prologue macro",
        !           114:        "macro not allowed in prologue",
        !           115:        "macro not allowed in body",
        !           116:
        !           117:        /* related to document structure */
        !           118:        "NAME section must come first",
        !           119:        "bad NAME section contents",
        !           120:        "manual name not yet set",
1.34      schwarze  121:        "sections out of conventional order",
1.53    ! schwarze  122:        "duplicate section name",
        !           123:        "section not in conventional manual section",
        !           124:
        !           125:        /* related to macros and nesting */
        !           126:        "skipping obsolete macro",
        !           127:        "skipping paragraph macro",
        !           128:        "blocks badly nested",
        !           129:        "child violates parent syntax",
        !           130:        "nested displays are not portable",
        !           131:        "already in literal mode",
        !           132:
        !           133:        /* related to missing macro arguments */
        !           134:        "skipping empty macro",
        !           135:        "missing display type",
1.33      schwarze  136:        "list type must come first",
1.53    ! schwarze  137:        "tag lists require a width argument",
        !           138:        "missing font type",
        !           139:
        !           140:        /* related to bad macro arguments */
        !           141:        "skipping argument",
        !           142:        "duplicate argument",
        !           143:        "duplicate display type",
        !           144:        "duplicate list type",
        !           145:        "unknown AT&T UNIX version",
        !           146:        "bad Boolean value",
        !           147:        "unknown library specifier",
        !           148:        "unknown standard specifier",
        !           149:        "bad width argument",
        !           150:
        !           151:        /* related to plain text */
        !           152:        "blank line in non-literal context",
1.43      schwarze  153:        "tab in non-literal context",
1.53    ! schwarze  154:        "end of line whitespace",
        !           155:        "bad comment style",
        !           156:        "unknown escape sequence",
1.33      schwarze  157:        "unterminated quoted string",
1.40      schwarze  158:
                    159:        "generic error",
                    160:
1.53    ! schwarze  161:        "skipping bad character",
        !           162:        "skipping text before the first section header",
        !           163:        "skipping unknown macro",
1.33      schwarze  164:        "line scope broken",
                    165:        "argument count wrong",
1.53    ! schwarze  166:        "skipping end of block that is not open",
1.50      schwarze  167:        "missing end of block",
1.47      schwarze  168:        "scope open on exit",
1.50      schwarze  169:        "uname(3) system call failed",
1.33      schwarze  170:        "macro requires line argument(s)",
                    171:        "macro requires body argument(s)",
                    172:        "macro requires argument(s)",
1.34      schwarze  173:        "missing list type",
1.33      schwarze  174:        "line argument(s) will be lost",
                    175:        "body argument(s) will be lost",
1.49      schwarze  176:        "tbl(1) error",
1.40      schwarze  177:
                    178:        "generic fatal error",
                    179:
1.34      schwarze  180:        "column syntax is inconsistent",
1.37      schwarze  181:        "unsupported display type",
1.33      schwarze  182:        "line scope broken, syntax violated",
                    183:        "argument count wrong, violates syntax",
                    184:        "child violates parent syntax",
                    185:        "argument count wrong, violates syntax",
1.52      schwarze  186:        "invalid path in include directive",
1.33      schwarze  187:        "no document body",
                    188:        "no document prologue",
1.45      schwarze  189:        "static buffer exhausted",
1.1       kristaps  190: };
                    191:
1.51      schwarze  192: static void              pdesc(struct curparse *);
1.27      schwarze  193: static void              fdesc(struct curparse *);
                    194: static void              ffile(const char *, struct curparse *);
1.52      schwarze  195: static int               pfile(const char *, struct curparse *, int);
1.1       kristaps  196: static int               moptions(enum intt *, char *);
1.30      schwarze  197: static int               mmsg(enum mandocerr, void *,
                    198:                                int, int, const char *);
1.45      schwarze  199: static void              pset(const char *, int, struct curparse *,
1.1       kristaps  200:                                struct man **, struct mdoc **);
1.27      schwarze  201: static int               toptions(struct curparse *, char *);
                    202: static void              usage(void) __attribute__((noreturn));
1.20      schwarze  203: static void              version(void) __attribute__((noreturn));
1.45      schwarze  204: static int               woptions(struct curparse *, char *);
1.1       kristaps  205:
1.19      schwarze  206: static const char       *progname;
1.45      schwarze  207: static enum mandoclevel  exit_status = MANDOCLEVEL_OK;
1.1       kristaps  208:
                    209: int
                    210: main(int argc, char *argv[])
                    211: {
1.27      schwarze  212:        int              c;
1.1       kristaps  213:        struct curparse  curp;
                    214:
1.19      schwarze  215:        progname = strrchr(argv[0], '/');
                    216:        if (progname == NULL)
                    217:                progname = argv[0];
                    218:        else
                    219:                ++progname;
                    220:
                    221:        memset(&curp, 0, sizeof(struct curparse));
1.1       kristaps  222:
                    223:        curp.inttype = INTT_AUTO;
                    224:        curp.outtype = OUTT_ASCII;
1.45      schwarze  225:        curp.wlevel  = MANDOCLEVEL_FATAL;
1.1       kristaps  226:
                    227:        /* LINTED */
1.45      schwarze  228:        while (-1 != (c = getopt(argc, argv, "m:O:T:VW:")))
1.1       kristaps  229:                switch (c) {
                    230:                case ('m'):
                    231:                        if ( ! moptions(&curp.inttype, optarg))
1.47      schwarze  232:                                return((int)MANDOCLEVEL_BADARG);
1.1       kristaps  233:                        break;
1.18      schwarze  234:                case ('O'):
                    235:                        (void)strlcat(curp.outopts, optarg, BUFSIZ);
                    236:                        (void)strlcat(curp.outopts, ",", BUFSIZ);
1.17      schwarze  237:                        break;
1.1       kristaps  238:                case ('T'):
1.22      schwarze  239:                        if ( ! toptions(&curp, optarg))
1.47      schwarze  240:                                return((int)MANDOCLEVEL_BADARG);
1.1       kristaps  241:                        break;
                    242:                case ('W'):
1.45      schwarze  243:                        if ( ! woptions(&curp, optarg))
1.47      schwarze  244:                                return((int)MANDOCLEVEL_BADARG);
1.1       kristaps  245:                        break;
1.3       schwarze  246:                case ('V'):
                    247:                        version();
                    248:                        /* NOTREACHED */
1.1       kristaps  249:                default:
                    250:                        usage();
                    251:                        /* NOTREACHED */
                    252:                }
                    253:
                    254:        argc -= optind;
                    255:        argv += optind;
                    256:
1.7       schwarze  257:        if (NULL == *argv) {
                    258:                curp.file = "<stdin>";
                    259:                curp.fd = STDIN_FILENO;
1.14      schwarze  260:
1.27      schwarze  261:                fdesc(&curp);
                    262:        }
                    263:
                    264:        while (*argv) {
                    265:                ffile(*argv, &curp);
1.45      schwarze  266:                if (MANDOCLEVEL_OK != exit_status && curp.wstop)
1.27      schwarze  267:                        break;
                    268:                ++argv;
1.1       kristaps  269:        }
                    270:
                    271:        if (curp.outfree)
                    272:                (*curp.outfree)(curp.outdata);
1.30      schwarze  273:        if (curp.mdoc)
                    274:                mdoc_free(curp.mdoc);
                    275:        if (curp.man)
                    276:                man_free(curp.man);
                    277:        if (curp.roff)
                    278:                roff_free(curp.roff);
1.1       kristaps  279:
1.47      schwarze  280:        return((int)exit_status);
1.1       kristaps  281: }
                    282:
                    283:
1.20      schwarze  284: static void
1.3       schwarze  285: version(void)
                    286: {
                    287:
1.19      schwarze  288:        (void)printf("%s %s\n", progname, VERSION);
1.47      schwarze  289:        exit((int)MANDOCLEVEL_OK);
1.3       schwarze  290: }
                    291:
                    292:
1.20      schwarze  293: static void
1.1       kristaps  294: usage(void)
                    295: {
                    296:
1.23      jmc       297:        (void)fprintf(stderr, "usage: %s [-V] [-foption] "
1.18      schwarze  298:                        "[-mformat] [-Ooption] [-Toutput] "
1.23      jmc       299:                        "[-Werr] [file...]\n", progname);
1.47      schwarze  300:        exit((int)MANDOCLEVEL_BADARG);
1.1       kristaps  301: }
                    302:
                    303:
1.27      schwarze  304: static void
                    305: ffile(const char *file, struct curparse *curp)
1.1       kristaps  306: {
                    307:
                    308:        curp->file = file;
                    309:        if (-1 == (curp->fd = open(curp->file, O_RDONLY, 0))) {
1.19      schwarze  310:                perror(curp->file);
1.45      schwarze  311:                exit_status = MANDOCLEVEL_SYSERR;
1.27      schwarze  312:                return;
1.1       kristaps  313:        }
                    314:
1.27      schwarze  315:        fdesc(curp);
1.1       kristaps  316:
                    317:        if (-1 == close(curp->fd))
1.19      schwarze  318:                perror(curp->file);
1.27      schwarze  319: }
1.1       kristaps  320:
1.52      schwarze  321: static int
                    322: pfile(const char *file, struct curparse *curp, int ln)
                    323: {
                    324:        const char      *savefile;
                    325:        int              fd, savefd;
                    326:
                    327:        if (-1 == (fd = open(file, O_RDONLY, 0))) {
                    328:                perror(file);
                    329:                exit_status = MANDOCLEVEL_SYSERR;
                    330:                return(0);
                    331:        }
                    332:
                    333:        savefile = curp->file;
                    334:        savefd = curp->fd;
                    335:
                    336:        curp->file = file;
                    337:        curp->fd = fd;
                    338:
                    339:        pdesc(curp);
                    340:
                    341:        curp->file = savefile;
                    342:        curp->fd = savefd;
                    343:
                    344:        if (-1 == close(fd))
                    345:                perror(file);
                    346:
                    347:        return(MANDOCLEVEL_FATAL > exit_status ? 1 : 0);
                    348: }
                    349:
1.27      schwarze  350:
1.45      schwarze  351: static void
1.27      schwarze  352: resize_buf(struct buf *buf, size_t initial)
                    353: {
                    354:
1.45      schwarze  355:        buf->sz = buf->sz ? 2 * buf->sz : initial;
                    356:        buf->buf = realloc(buf->buf, buf->sz);
                    357:        if (NULL == buf->buf) {
1.27      schwarze  358:                perror(NULL);
1.47      schwarze  359:                exit((int)MANDOCLEVEL_SYSERR);
1.27      schwarze  360:        }
1.1       kristaps  361: }
                    362:
                    363:
                    364: static int
1.27      schwarze  365: read_whole_file(struct curparse *curp, struct buf *fb, int *with_mmap)
1.1       kristaps  366: {
1.27      schwarze  367:        struct stat      st;
                    368:        size_t           off;
1.1       kristaps  369:        ssize_t          ssz;
1.27      schwarze  370:
                    371:        if (-1 == fstat(curp->fd, &st)) {
                    372:                perror(curp->file);
                    373:                return(0);
                    374:        }
                    375:
                    376:        /*
                    377:         * If we're a regular file, try just reading in the whole entry
                    378:         * via mmap().  This is faster than reading it into blocks, and
                    379:         * since each file is only a few bytes to begin with, I'm not
                    380:         * concerned that this is going to tank any machines.
                    381:         */
                    382:
                    383:        if (S_ISREG(st.st_mode)) {
                    384:                if (st.st_size >= (1U << 31)) {
                    385:                        fprintf(stderr, "%s: input too large\n",
                    386:                                        curp->file);
                    387:                        return(0);
                    388:                }
                    389:                *with_mmap = 1;
1.30      schwarze  390:                fb->sz = (size_t)st.st_size;
1.27      schwarze  391:                fb->buf = mmap(NULL, fb->sz, PROT_READ,
1.35      schwarze  392:                                MAP_FILE|MAP_SHARED, curp->fd, 0);
1.27      schwarze  393:                if (fb->buf != MAP_FAILED)
                    394:                        return(1);
                    395:        }
                    396:
                    397:        /*
                    398:         * If this isn't a regular file (like, say, stdin), then we must
                    399:         * go the old way and just read things in bit by bit.
                    400:         */
                    401:
                    402:        *with_mmap = 0;
                    403:        off = 0;
                    404:        fb->sz = 0;
                    405:        fb->buf = NULL;
                    406:        for (;;) {
                    407:                if (off == fb->sz) {
                    408:                        if (fb->sz == (1U << 31)) {
                    409:                                fprintf(stderr, "%s: input too large\n",
                    410:                                                curp->file);
                    411:                                break;
                    412:                        }
1.45      schwarze  413:                        resize_buf(fb, 65536);
1.27      schwarze  414:                }
1.30      schwarze  415:                ssz = read(curp->fd, fb->buf + (int)off, fb->sz - off);
1.27      schwarze  416:                if (ssz == 0) {
                    417:                        fb->sz = off;
                    418:                        return(1);
                    419:                }
                    420:                if (ssz == -1) {
                    421:                        perror(curp->file);
                    422:                        break;
                    423:                }
1.30      schwarze  424:                off += (size_t)ssz;
1.27      schwarze  425:        }
                    426:
                    427:        free(fb->buf);
                    428:        fb->buf = NULL;
                    429:        return(0);
                    430: }
                    431:
                    432:
                    433: static void
                    434: fdesc(struct curparse *curp)
                    435: {
1.51      schwarze  436:        struct man      *man;
                    437:        struct mdoc     *mdoc;
                    438:        struct roff     *roff;
                    439:
                    440:        pdesc(curp);
                    441:
                    442:        man  = curp->man;
                    443:        mdoc = curp->mdoc;
                    444:        roff = curp->roff;
                    445:
                    446:        if (MANDOCLEVEL_FATAL <= exit_status)
                    447:                goto cleanup;
                    448:
                    449:        /* NOTE a parser may not have been assigned, yet. */
                    450:
                    451:        if ( ! (man || mdoc)) {
                    452:                fprintf(stderr, "%s: Not a manual\n", curp->file);
                    453:                exit_status = MANDOCLEVEL_FATAL;
                    454:                goto cleanup;
                    455:        }
                    456:
                    457:        /* Clean up the parse routine ASTs. */
                    458:
                    459:        if (mdoc && ! mdoc_endparse(mdoc)) {
                    460:                assert(MANDOCLEVEL_FATAL <= exit_status);
                    461:                goto cleanup;
                    462:        }
                    463:        if (man && ! man_endparse(man)) {
                    464:                assert(MANDOCLEVEL_FATAL <= exit_status);
                    465:                goto cleanup;
                    466:        }
                    467:        if (roff && ! roff_endparse(roff)) {
                    468:                assert(MANDOCLEVEL_FATAL <= exit_status);
                    469:                goto cleanup;
                    470:        }
                    471:
                    472:        /*
                    473:         * With -Wstop and warnings or errors of at least
                    474:         * the requested level, do not produce output.
                    475:         */
                    476:
                    477:        if (MANDOCLEVEL_OK != exit_status && curp->wstop)
                    478:                goto cleanup;
                    479:
                    480:        /* If unset, allocate output dev now (if applicable). */
                    481:
                    482:        if ( ! (curp->outman && curp->outmdoc)) {
                    483:                switch (curp->outtype) {
                    484:                case (OUTT_XHTML):
                    485:                        curp->outdata = xhtml_alloc(curp->outopts);
                    486:                        break;
                    487:                case (OUTT_HTML):
                    488:                        curp->outdata = html_alloc(curp->outopts);
                    489:                        break;
                    490:                case (OUTT_ASCII):
                    491:                        curp->outdata = ascii_alloc(curp->outopts);
                    492:                        curp->outfree = ascii_free;
                    493:                        break;
                    494:                case (OUTT_PDF):
                    495:                        curp->outdata = pdf_alloc(curp->outopts);
                    496:                        curp->outfree = pspdf_free;
                    497:                        break;
                    498:                case (OUTT_PS):
                    499:                        curp->outdata = ps_alloc(curp->outopts);
                    500:                        curp->outfree = pspdf_free;
                    501:                        break;
                    502:                default:
                    503:                        break;
                    504:                }
                    505:
                    506:                switch (curp->outtype) {
                    507:                case (OUTT_HTML):
                    508:                        /* FALLTHROUGH */
                    509:                case (OUTT_XHTML):
                    510:                        curp->outman = html_man;
                    511:                        curp->outmdoc = html_mdoc;
                    512:                        curp->outfree = html_free;
                    513:                        break;
                    514:                case (OUTT_TREE):
                    515:                        curp->outman = tree_man;
                    516:                        curp->outmdoc = tree_mdoc;
                    517:                        break;
                    518:                case (OUTT_PDF):
                    519:                        /* FALLTHROUGH */
                    520:                case (OUTT_ASCII):
                    521:                        /* FALLTHROUGH */
                    522:                case (OUTT_PS):
                    523:                        curp->outman = terminal_man;
                    524:                        curp->outmdoc = terminal_mdoc;
                    525:                        break;
                    526:                default:
                    527:                        break;
                    528:                }
                    529:        }
                    530:
                    531:        /* Execute the out device, if it exists. */
                    532:
                    533:        if (man && curp->outman)
                    534:                (*curp->outman)(curp->outdata, man);
                    535:        if (mdoc && curp->outmdoc)
                    536:                (*curp->outmdoc)(curp->outdata, mdoc);
                    537:
                    538:  cleanup:
                    539:        memset(&curp->regs, 0, sizeof(struct regset));
                    540:        if (mdoc)
                    541:                mdoc_reset(mdoc);
                    542:        if (man)
                    543:                man_reset(man);
                    544:        if (roff)
                    545:                roff_reset(roff);
                    546:
                    547:        return;
                    548: }
                    549:
                    550:
                    551: static void
                    552: pdesc(struct curparse *curp)
                    553: {
1.27      schwarze  554:        struct buf       ln, blk;
1.32      schwarze  555:        int              i, pos, lnn, lnn_start, with_mmap, of;
1.30      schwarze  556:        enum rofferr     re;
1.44      schwarze  557:        unsigned char    c;
1.1       kristaps  558:        struct man      *man;
                    559:        struct mdoc     *mdoc;
1.30      schwarze  560:        struct roff     *roff;
1.1       kristaps  561:
1.27      schwarze  562:        memset(&ln, 0, sizeof(struct buf));
1.1       kristaps  563:
                    564:        /*
1.29      schwarze  565:         * Two buffers: ln and buf.  buf is the input file and may be
                    566:         * memory mapped.  ln is a line buffer and grows on-demand.
1.1       kristaps  567:         */
                    568:
1.45      schwarze  569:        if ( ! read_whole_file(curp, &blk, &with_mmap)) {
                    570:                exit_status = MANDOCLEVEL_SYSERR;
1.27      schwarze  571:                return;
1.45      schwarze  572:        }
1.27      schwarze  573:
1.30      schwarze  574:        if (NULL == curp->roff)
1.45      schwarze  575:                curp->roff = roff_alloc(&curp->regs, curp, mmsg);
                    576:        assert(curp->roff);
                    577:        roff = curp->roff;
1.51      schwarze  578:        mdoc = curp->mdoc;
                    579:        man  = curp->man;
1.30      schwarze  580:
1.29      schwarze  581:        for (i = 0, lnn = 1; i < (int)blk.sz;) {
                    582:                pos = 0;
                    583:                lnn_start = lnn;
                    584:                while (i < (int)blk.sz) {
                    585:                        if ('\n' == blk.buf[i]) {
                    586:                                ++i;
                    587:                                ++lnn;
                    588:                                break;
                    589:                        }
1.43      schwarze  590:
                    591:                        /*
                    592:                         * Warn about bogus characters.  If you're using
                    593:                         * non-ASCII encoding, you're screwing your
                    594:                         * readers.  Since I'd rather this not happen,
                    595:                         * I'll be helpful and drop these characters so
                    596:                         * we don't display gibberish.  Note to manual
                    597:                         * writers: use special characters.
                    598:                         */
                    599:
1.44      schwarze  600:                        c = (unsigned char) blk.buf[i];
                    601:                        if ( ! (isascii(c) && (isgraph(c) || isblank(c)))) {
1.45      schwarze  602:                                mmsg(MANDOCERR_BADCHAR, curp,
                    603:                                    lnn_start, pos, "ignoring byte");
1.43      schwarze  604:                                i++;
                    605:                                continue;
                    606:                        }
                    607:
1.29      schwarze  608:                        /* Trailing backslash is like a plain character. */
                    609:                        if ('\\' != blk.buf[i] || i + 1 == (int)blk.sz) {
                    610:                                if (pos >= (int)ln.sz)
1.45      schwarze  611:                                        resize_buf(&ln, 256);
1.29      schwarze  612:                                ln.buf[pos++] = blk.buf[i++];
1.27      schwarze  613:                                continue;
1.29      schwarze  614:                        }
                    615:                        /* Found an escape and at least one other character. */
                    616:                        if ('\n' == blk.buf[i + 1]) {
                    617:                                /* Escaped newlines are skipped over */
                    618:                                i += 2;
                    619:                                ++lnn;
1.27      schwarze  620:                                continue;
                    621:                        }
1.29      schwarze  622:                        if ('"' == blk.buf[i + 1]) {
                    623:                                i += 2;
                    624:                                /* Comment, skip to end of line */
                    625:                                for (; i < (int)blk.sz; ++i) {
                    626:                                        if ('\n' == blk.buf[i]) {
                    627:                                                ++i;
                    628:                                                ++lnn;
                    629:                                                break;
                    630:                                        }
                    631:                                }
                    632:                                /* Backout trailing whitespaces */
                    633:                                for (; pos > 0; --pos) {
                    634:                                        if (ln.buf[pos - 1] != ' ')
                    635:                                                break;
                    636:                                        if (pos > 2 && ln.buf[pos - 2] == '\\')
                    637:                                                break;
                    638:                                }
                    639:                                break;
                    640:                        }
                    641:                        /* Some other escape sequence, copy and continue. */
                    642:                        if (pos + 1 >= (int)ln.sz)
1.45      schwarze  643:                                resize_buf(&ln, 256);
1.1       kristaps  644:
1.29      schwarze  645:                        ln.buf[pos++] = blk.buf[i++];
                    646:                        ln.buf[pos++] = blk.buf[i++];
1.27      schwarze  647:                }
1.1       kristaps  648:
1.29      schwarze  649:                if (pos >= (int)ln.sz)
1.45      schwarze  650:                        resize_buf(&ln, 256);
1.30      schwarze  651:                ln.buf[pos] = '\0';
                    652:
1.32      schwarze  653:                /*
                    654:                 * A significant amount of complexity is contained by
                    655:                 * the roff preprocessor.  It's line-oriented but can be
                    656:                 * expressed on one line, so we need at times to
                    657:                 * readjust our starting point and re-run it.  The roff
                    658:                 * preprocessor can also readjust the buffers with new
                    659:                 * data, so we pass them in wholesale.
                    660:                 */
                    661:
                    662:                of = 0;
                    663:                do {
                    664:                        re = roff_parseln(roff, lnn_start,
                    665:                                        &ln.buf, &ln.sz, of, &of);
                    666:                } while (ROFF_RERUN == re);
                    667:
1.45      schwarze  668:                if (ROFF_IGN == re) {
1.30      schwarze  669:                        continue;
1.45      schwarze  670:                } else if (ROFF_ERR == re) {
                    671:                        assert(MANDOCLEVEL_FATAL <= exit_status);
1.51      schwarze  672:                        break;
1.52      schwarze  673:                } else if (ROFF_SO == re) {
                    674:                        if (pfile(ln.buf + of, curp, lnn_start))
                    675:                                continue;
                    676:                        else
                    677:                                break;
1.45      schwarze  678:                }
1.5       schwarze  679:
1.32      schwarze  680:                /*
                    681:                 * If input parsers have not been allocated, do so now.
                    682:                 * We keep these instanced betwen parsers, but set them
                    683:                 * locally per parse routine since we can use different
                    684:                 * parsers with each one.
                    685:                 */
1.1       kristaps  686:
1.32      schwarze  687:                if ( ! (man || mdoc))
1.45      schwarze  688:                        pset(ln.buf + of, pos - of, curp, &man, &mdoc);
1.5       schwarze  689:
1.32      schwarze  690:                /* Lastly, push down into the parsers themselves. */
1.1       kristaps  691:
1.45      schwarze  692:                if (man && ! man_parseln(man, lnn_start, ln.buf, of)) {
                    693:                        assert(MANDOCLEVEL_FATAL <= exit_status);
1.51      schwarze  694:                        break;
1.45      schwarze  695:                }
                    696:                if (mdoc && ! mdoc_parseln(mdoc, lnn_start, ln.buf, of)) {
                    697:                        assert(MANDOCLEVEL_FATAL <= exit_status);
1.1       kristaps  698:                        break;
                    699:                }
                    700:        }
                    701:
1.51      schwarze  702:        free(ln.buf);
1.27      schwarze  703:        if (with_mmap)
                    704:                munmap(blk.buf, blk.sz);
                    705:        else
                    706:                free(blk.buf);
1.1       kristaps  707: }
                    708:
                    709:
1.45      schwarze  710: static void
1.1       kristaps  711: pset(const char *buf, int pos, struct curparse *curp,
                    712:                struct man **man, struct mdoc **mdoc)
                    713: {
1.5       schwarze  714:        int              i;
1.1       kristaps  715:
                    716:        /*
                    717:         * Try to intuit which kind of manual parser should be used.  If
                    718:         * passed in by command-line (-man, -mdoc), then use that
                    719:         * explicitly.  If passed as -mandoc, then try to guess from the
1.5       schwarze  720:         * line: either skip dot-lines, use -mdoc when finding `.Dt', or
1.1       kristaps  721:         * default to -man, which is more lenient.
                    722:         */
                    723:
1.31      schwarze  724:        if ('.' == buf[0] || '\'' == buf[0]) {
1.5       schwarze  725:                for (i = 1; buf[i]; i++)
                    726:                        if (' ' != buf[i] && '\t' != buf[i])
                    727:                                break;
1.45      schwarze  728:                if ('\0' == buf[i])
                    729:                        return;
1.5       schwarze  730:        }
1.1       kristaps  731:
                    732:        switch (curp->inttype) {
                    733:        case (INTT_MDOC):
                    734:                if (NULL == curp->mdoc)
1.45      schwarze  735:                        curp->mdoc = mdoc_alloc(&curp->regs, curp, mmsg);
                    736:                assert(curp->mdoc);
                    737:                *mdoc = curp->mdoc;
                    738:                return;
1.1       kristaps  739:        case (INTT_MAN):
                    740:                if (NULL == curp->man)
1.45      schwarze  741:                        curp->man = man_alloc(&curp->regs, curp, mmsg);
                    742:                assert(curp->man);
                    743:                *man = curp->man;
                    744:                return;
1.1       kristaps  745:        default:
                    746:                break;
                    747:        }
                    748:
                    749:        if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3))  {
                    750:                if (NULL == curp->mdoc)
1.45      schwarze  751:                        curp->mdoc = mdoc_alloc(&curp->regs, curp, mmsg);
                    752:                assert(curp->mdoc);
                    753:                *mdoc = curp->mdoc;
                    754:                return;
1.1       kristaps  755:        }
                    756:
                    757:        if (NULL == curp->man)
1.45      schwarze  758:                curp->man = man_alloc(&curp->regs, curp, mmsg);
                    759:        assert(curp->man);
                    760:        *man = curp->man;
1.1       kristaps  761: }
                    762:
                    763:
                    764: static int
                    765: moptions(enum intt *tflags, char *arg)
                    766: {
                    767:
                    768:        if (0 == strcmp(arg, "doc"))
                    769:                *tflags = INTT_MDOC;
                    770:        else if (0 == strcmp(arg, "andoc"))
                    771:                *tflags = INTT_AUTO;
                    772:        else if (0 == strcmp(arg, "an"))
                    773:                *tflags = INTT_MAN;
                    774:        else {
1.20      schwarze  775:                fprintf(stderr, "%s: Bad argument\n", arg);
1.1       kristaps  776:                return(0);
                    777:        }
                    778:
                    779:        return(1);
                    780: }
                    781:
                    782:
                    783: static int
1.22      schwarze  784: toptions(struct curparse *curp, char *arg)
1.1       kristaps  785: {
                    786:
                    787:        if (0 == strcmp(arg, "ascii"))
1.22      schwarze  788:                curp->outtype = OUTT_ASCII;
                    789:        else if (0 == strcmp(arg, "lint")) {
                    790:                curp->outtype = OUTT_LINT;
1.45      schwarze  791:                curp->wlevel  = MANDOCLEVEL_WARNING;
1.22      schwarze  792:        }
1.1       kristaps  793:        else if (0 == strcmp(arg, "tree"))
1.22      schwarze  794:                curp->outtype = OUTT_TREE;
1.17      schwarze  795:        else if (0 == strcmp(arg, "html"))
1.22      schwarze  796:                curp->outtype = OUTT_HTML;
1.21      schwarze  797:        else if (0 == strcmp(arg, "xhtml"))
1.22      schwarze  798:                curp->outtype = OUTT_XHTML;
1.36      schwarze  799:        else if (0 == strcmp(arg, "ps"))
                    800:                curp->outtype = OUTT_PS;
1.43      schwarze  801:        else if (0 == strcmp(arg, "pdf"))
                    802:                curp->outtype = OUTT_PDF;
1.1       kristaps  803:        else {
1.20      schwarze  804:                fprintf(stderr, "%s: Bad argument\n", arg);
1.1       kristaps  805:                return(0);
                    806:        }
                    807:
                    808:        return(1);
                    809: }
                    810:
                    811:
                    812: static int
1.45      schwarze  813: woptions(struct curparse *curp, char *arg)
1.1       kristaps  814: {
1.10      schwarze  815:        char            *v, *o;
1.45      schwarze  816:        const char      *toks[6];
1.1       kristaps  817:
1.45      schwarze  818:        toks[0] = "stop";
                    819:        toks[1] = "all";
                    820:        toks[2] = "warning";
                    821:        toks[3] = "error";
                    822:        toks[4] = "fatal";
                    823:        toks[5] = NULL;
1.1       kristaps  824:
1.10      schwarze  825:        while (*arg) {
                    826:                o = arg;
1.17      schwarze  827:                switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1       kristaps  828:                case (0):
1.45      schwarze  829:                        curp->wstop = 1;
1.1       kristaps  830:                        break;
                    831:                case (1):
1.45      schwarze  832:                        /* FALLTHROUGH */
1.1       kristaps  833:                case (2):
1.45      schwarze  834:                        curp->wlevel = MANDOCLEVEL_WARNING;
1.1       kristaps  835:                        break;
                    836:                case (3):
1.45      schwarze  837:                        curp->wlevel = MANDOCLEVEL_ERROR;
1.1       kristaps  838:                        break;
                    839:                case (4):
1.45      schwarze  840:                        curp->wlevel = MANDOCLEVEL_FATAL;
1.19      schwarze  841:                        break;
1.1       kristaps  842:                default:
1.45      schwarze  843:                        fprintf(stderr, "-W%s: Bad argument\n", o);
1.1       kristaps  844:                        return(0);
                    845:                }
1.10      schwarze  846:        }
1.1       kristaps  847:
                    848:        return(1);
                    849: }
                    850:
                    851:
1.30      schwarze  852: static int
                    853: mmsg(enum mandocerr t, void *arg, int ln, int col, const char *msg)
                    854: {
                    855:        struct curparse *cp;
1.45      schwarze  856:        enum mandoclevel level;
                    857:
                    858:        level = MANDOCLEVEL_FATAL;
                    859:        while (t < mandoclimits[level])
1.47      schwarze  860:                /* LINTED */
1.45      schwarze  861:                level--;
1.30      schwarze  862:
                    863:        cp = (struct curparse *)arg;
1.45      schwarze  864:        if (level < cp->wlevel)
                    865:                return(1);
1.30      schwarze  866:
1.45      schwarze  867:        fprintf(stderr, "%s:%d:%d: %s: %s",
                    868:            cp->file, ln, col + 1, mandoclevels[level], mandocerrs[t]);
1.30      schwarze  869:        if (msg)
                    870:                fprintf(stderr, ": %s", msg);
                    871:        fputc('\n', stderr);
1.33      schwarze  872:
1.45      schwarze  873:        if (exit_status < level)
                    874:                exit_status = level;
                    875:
                    876:        return(level < MANDOCLEVEL_FATAL);
1.30      schwarze  877: }