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

Annotation of mandoc/main.c, Revision 1.134

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

CVSweb