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

Annotation of mandoc/main.c, Revision 1.129

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

CVSweb