[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.51

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