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

Annotation of mandoc/main.c, Revision 1.147

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

CVSweb