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

Annotation of mandoc/main.c, Revision 1.121

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

CVSweb