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

Annotation of mandoc/main.c, Revision 1.126

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

CVSweb