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

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