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

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