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

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