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

Annotation of mandoc/main.c, Revision 1.140

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

CVSweb