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

Annotation of mandoc/main.c, Revision 1.136

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

CVSweb