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

Annotation of mandoc/main.c, Revision 1.143

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

CVSweb