[BACK]Return to main.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/main.c, Revision 1.132

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

CVSweb