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

Annotation of mandoc/main.c, Revision 1.97

1.97    ! schwarze    1: /*     $Id: main.c,v 1.96 2010/07/02 12:54:33 kristaps Exp $ */
1.1       kristaps    2: /*
1.97    ! schwarze    3:  * Copyright (c) 2008, 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
        !             4:  * Copyright (c) 2010 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
1.25      kristaps    7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    9:  *
1.25      kristaps   10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   17:  */
1.58      kristaps   18: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
                     20: #endif
                     21:
1.66      kristaps   22: #include <sys/mman.h>
1.1       kristaps   23: #include <sys/stat.h>
                     24:
                     25: #include <assert.h>
                     26: #include <fcntl.h>
                     27: #include <stdio.h>
1.45      kristaps   28: #include <stdint.h>
1.1       kristaps   29: #include <stdlib.h>
                     30: #include <string.h>
                     31: #include <unistd.h>
                     32:
1.71      kristaps   33: #include "mandoc.h"
1.90      kristaps   34: #include "regs.h"
                     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.1       kristaps   39:
1.45      kristaps   40: #define        UNCONST(a)      ((void *)(uintptr_t)(const void *)(a))
                     41:
1.55      kristaps   42: /* FIXME: Intel's compiler?  LLVM?  pcc?  */
                     43:
                     44: #if !defined(__GNUC__) || (__GNUC__ < 2)
1.56      kristaps   45: # if !defined(lint)
                     46: #  define __attribute__(x)
                     47: # endif
1.55      kristaps   48: #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
1.16      kristaps   49:
1.42      kristaps   50: typedef        void            (*out_mdoc)(void *, const struct mdoc *);
                     51: typedef        void            (*out_man)(void *, const struct man *);
1.22      kristaps   52: typedef        void            (*out_free)(void *);
                     53:
1.5       kristaps   54: struct buf {
                     55:        char             *buf;
                     56:        size_t            sz;
                     57: };
                     58:
1.19      kristaps   59: enum   intt {
                     60:        INTT_AUTO,
                     61:        INTT_MDOC,
                     62:        INTT_MAN
                     63: };
                     64:
                     65: enum   outt {
                     66:        OUTT_ASCII = 0,
                     67:        OUTT_TREE,
1.43      kristaps   68:        OUTT_HTML,
1.59      kristaps   69:        OUTT_XHTML,
1.85      kristaps   70:        OUTT_LINT,
                     71:        OUTT_PS
1.19      kristaps   72: };
                     73:
1.5       kristaps   74: struct curparse {
1.22      kristaps   75:        const char       *file;         /* Current parse. */
                     76:        int               fd;           /* Current parse. */
1.5       kristaps   77:        int               wflags;
1.71      kristaps   78:        /* FIXME: set by max error */
1.36      kristaps   79: #define        WARN_WALL        (1 << 0)       /* All-warnings mask. */
1.1       kristaps   80: #define        WARN_WERR        (1 << 2)       /* Warnings->errors. */
1.23      kristaps   81:        int               fflags;
1.60      kristaps   82: #define        FL_IGN_SCOPE     (1 << 0)       /* Ignore scope errors. */
                     83: #define        FL_NIGN_ESCAPE   (1 << 1)       /* Don't ignore bad escapes. */
                     84: #define        FL_NIGN_MACRO    (1 << 2)       /* Don't ignore bad macros. */
                     85: #define        FL_IGN_ERRORS    (1 << 4)       /* Ignore failed parse. */
1.66      kristaps   86: #define        FL_STRICT         FL_NIGN_ESCAPE | \
1.79      kristaps   87:                          FL_NIGN_MACRO /* ignore nothing */
                     88:        enum intt         inttype;      /* which parser to use */
                     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.79      kristaps   93:        enum outt         outtype;      /* which output to use */
                     94:        out_mdoc          outmdoc;      /* mdoc output ptr */
                     95:        out_man           outman;       /* man output ptr */
                     96:        out_free          outfree;      /* free output ptr */
                     97:        void             *outdata;      /* data for output */
                     98:        char              outopts[BUFSIZ]; /* buf of output opts */
                     99: };
                    100:
                    101: static const char * const      mandocerrs[MANDOCERR_MAX] = {
                    102:        "ok",
1.94      schwarze  103:
                    104:        "generic warning",
                    105:
1.79      kristaps  106:        "text should be uppercase",
1.81      kristaps  107:        "sections out of conventional order",
1.79      kristaps  108:        "section name repeats",
                    109:        "out of order prologue",
                    110:        "repeated prologue entry",
                    111:        "list type must come first",
                    112:        "bad standard",
                    113:        "bad library",
                    114:        "bad escape sequence",
                    115:        "unterminated quoted string",
                    116:        "argument requires the width argument",
                    117:        "superfluous width argument",
1.88      kristaps  118:        "ignoring argument",
1.79      kristaps  119:        "bad date argument",
                    120:        "bad width argument",
1.81      kristaps  121:        "unknown manual section",
1.79      kristaps  122:        "section not in conventional manual section",
                    123:        "end of line whitespace",
1.95      schwarze  124:        "blocks badly nested",
1.79      kristaps  125:        "scope open on exit",
1.94      schwarze  126:
                    127:        "generic error",
                    128:
1.79      kristaps  129:        "NAME section must come first",
                    130:        "bad Boolean value",
                    131:        "child violates parent syntax",
                    132:        "bad AT&T symbol",
                    133:        "list type repeated",
                    134:        "display type repeated",
                    135:        "argument repeated",
                    136:        "manual name not yet set",
                    137:        "obsolete macro ignored",
                    138:        "empty macro ignored",
                    139:        "macro not allowed in body",
                    140:        "macro not allowed in prologue",
                    141:        "bad character",
                    142:        "bad NAME section contents",
                    143:        "no blank lines",
                    144:        "no text in this context",
                    145:        "bad comment style",
                    146:        "unknown macro will be lost",
                    147:        "line scope broken",
                    148:        "argument count wrong",
                    149:        "request scope close w/none open",
                    150:        "scope already open",
                    151:        "macro requires line argument(s)",
                    152:        "macro requires body argument(s)",
                    153:        "macro requires argument(s)",
                    154:        "no title in document",
1.82      kristaps  155:        "missing list type",
1.87      kristaps  156:        "missing display type",
1.96      kristaps  157:        "missing font type",
1.79      kristaps  158:        "line argument(s) will be lost",
                    159:        "body argument(s) will be lost",
1.94      schwarze  160:
                    161:        "generic fatal error",
                    162:
1.80      kristaps  163:        "column syntax is inconsistent",
1.79      kristaps  164:        "displays may not be nested",
1.87      kristaps  165:        "unsupported display type",
1.95      schwarze  166:        "blocks badly nested",
                    167:        "no such block is open",
1.79      kristaps  168:        "scope broken, syntax violated",
                    169:        "line scope broken, syntax violated",
                    170:        "argument count wrong, violates syntax",
                    171:        "child violates parent syntax",
                    172:        "argument count wrong, violates syntax",
                    173:        "no document body",
                    174:        "no document prologue",
                    175:        "utsname system call failed",
                    176:        "memory exhausted",
1.5       kristaps  177: };
1.1       kristaps  178:
1.66      kristaps  179: static void              fdesc(struct curparse *);
                    180: static void              ffile(const char *, struct curparse *);
1.1       kristaps  181: static int               foptions(int *, char *);
1.66      kristaps  182: static struct man       *man_init(struct curparse *);
                    183: static struct mdoc      *mdoc_init(struct curparse *);
1.71      kristaps  184: static struct roff      *roff_init(struct curparse *);
1.10      kristaps  185: static int               moptions(enum intt *, char *);
1.71      kristaps  186: static int               mmsg(enum mandocerr, void *,
                    187:                                int, int, const char *);
1.22      kristaps  188: static int               pset(const char *, int, struct curparse *,
1.19      kristaps  189:                                struct man **, struct mdoc **);
1.66      kristaps  190: static int               toptions(struct curparse *, char *);
                    191: static void              usage(void) __attribute__((noreturn));
1.55      kristaps  192: static void              version(void) __attribute__((noreturn));
1.66      kristaps  193: static int               woptions(int *, char *);
1.1       kristaps  194:
1.54      kristaps  195: static const char       *progname;
1.94      schwarze  196: static int               with_fatal;
                    197: static int               with_error;
1.1       kristaps  198:
                    199: int
                    200: main(int argc, char *argv[])
                    201: {
1.64      kristaps  202:        int              c;
1.5       kristaps  203:        struct curparse  curp;
1.1       kristaps  204:
1.54      kristaps  205:        progname = strrchr(argv[0], '/');
                    206:        if (progname == NULL)
                    207:                progname = argv[0];
                    208:        else
                    209:                ++progname;
                    210:
1.51      kristaps  211:        memset(&curp, 0, sizeof(struct curparse));
1.5       kristaps  212:
1.19      kristaps  213:        curp.inttype = INTT_AUTO;
1.22      kristaps  214:        curp.outtype = OUTT_ASCII;
1.19      kristaps  215:
1.1       kristaps  216:        /* LINTED */
1.47      kristaps  217:        while (-1 != (c = getopt(argc, argv, "f:m:O:T:VW:")))
1.1       kristaps  218:                switch (c) {
                    219:                case ('f'):
1.19      kristaps  220:                        if ( ! foptions(&curp.fflags, optarg))
1.32      kristaps  221:                                return(EXIT_FAILURE);
1.1       kristaps  222:                        break;
1.10      kristaps  223:                case ('m'):
1.19      kristaps  224:                        if ( ! moptions(&curp.inttype, optarg))
1.32      kristaps  225:                                return(EXIT_FAILURE);
1.10      kristaps  226:                        break;
1.48      kristaps  227:                case ('O'):
1.49      kristaps  228:                        (void)strlcat(curp.outopts, optarg, BUFSIZ);
                    229:                        (void)strlcat(curp.outopts, ",", BUFSIZ);
1.44      kristaps  230:                        break;
1.1       kristaps  231:                case ('T'):
1.60      kristaps  232:                        if ( ! toptions(&curp, optarg))
1.32      kristaps  233:                                return(EXIT_FAILURE);
1.1       kristaps  234:                        break;
                    235:                case ('W'):
1.5       kristaps  236:                        if ( ! woptions(&curp.wflags, optarg))
1.32      kristaps  237:                                return(EXIT_FAILURE);
1.1       kristaps  238:                        break;
                    239:                case ('V'):
                    240:                        version();
                    241:                        /* NOTREACHED */
                    242:                default:
                    243:                        usage();
                    244:                        /* NOTREACHED */
                    245:                }
                    246:
                    247:        argc -= optind;
                    248:        argv += optind;
                    249:
1.30      kristaps  250:        if (NULL == *argv) {
                    251:                curp.file = "<stdin>";
                    252:                curp.fd = STDIN_FILENO;
1.39      kristaps  253:
1.66      kristaps  254:                fdesc(&curp);
1.64      kristaps  255:        }
                    256:
                    257:        while (*argv) {
1.66      kristaps  258:                ffile(*argv, &curp);
1.64      kristaps  259:
1.94      schwarze  260:                if (with_fatal && !(curp.fflags & FL_IGN_ERRORS))
1.64      kristaps  261:                        break;
                    262:                ++argv;
1.1       kristaps  263:        }
                    264:
1.22      kristaps  265:        if (curp.outfree)
                    266:                (*curp.outfree)(curp.outdata);
1.71      kristaps  267:        if (curp.mdoc)
                    268:                mdoc_free(curp.mdoc);
                    269:        if (curp.man)
                    270:                man_free(curp.man);
                    271:        if (curp.roff)
                    272:                roff_free(curp.roff);
1.1       kristaps  273:
1.94      schwarze  274:        return((with_fatal || with_error) ?
1.66      kristaps  275:                        EXIT_FAILURE :  EXIT_SUCCESS);
1.1       kristaps  276: }
                    277:
                    278:
1.55      kristaps  279: static void
1.1       kristaps  280: version(void)
                    281: {
                    282:
1.54      kristaps  283:        (void)printf("%s %s\n", progname, VERSION);
1.18      kristaps  284:        exit(EXIT_SUCCESS);
1.1       kristaps  285: }
                    286:
                    287:
1.55      kristaps  288: static void
1.1       kristaps  289: usage(void)
                    290: {
                    291:
1.61      kristaps  292:        (void)fprintf(stderr, "usage: %s [-V] [-foption] "
1.48      kristaps  293:                        "[-mformat] [-Ooption] [-Toutput] "
1.61      kristaps  294:                        "[-Werr] [file...]\n", progname);
1.18      kristaps  295:        exit(EXIT_FAILURE);
1.1       kristaps  296: }
                    297:
                    298:
1.19      kristaps  299: static struct man *
                    300: man_init(struct curparse *curp)
                    301: {
                    302:        int              pflags;
                    303:
1.30      kristaps  304:        /* Defaults from mandoc.1. */
1.27      kristaps  305:
1.62      kristaps  306:        pflags = MAN_IGN_MACRO | MAN_IGN_ESCAPE;
1.27      kristaps  307:
1.60      kristaps  308:        if (curp->fflags & FL_NIGN_MACRO)
1.20      kristaps  309:                pflags &= ~MAN_IGN_MACRO;
1.60      kristaps  310:        if (curp->fflags & FL_NIGN_ESCAPE)
1.33      kristaps  311:                pflags &= ~MAN_IGN_ESCAPE;
1.19      kristaps  312:
1.92      kristaps  313:        return(man_alloc(&curp->regs, curp, pflags, mmsg));
1.19      kristaps  314: }
                    315:
                    316:
1.71      kristaps  317: static struct roff *
                    318: roff_init(struct curparse *curp)
                    319: {
                    320:
1.92      kristaps  321:        return(roff_alloc(&curp->regs, mmsg, curp));
1.71      kristaps  322: }
                    323:
                    324:
1.19      kristaps  325: static struct mdoc *
                    326: mdoc_init(struct curparse *curp)
                    327: {
                    328:        int              pflags;
                    329:
1.30      kristaps  330:        /* Defaults from mandoc.1. */
1.27      kristaps  331:
1.62      kristaps  332:        pflags = MDOC_IGN_MACRO | MDOC_IGN_ESCAPE;
1.19      kristaps  333:
1.60      kristaps  334:        if (curp->fflags & FL_IGN_SCOPE)
1.19      kristaps  335:                pflags |= MDOC_IGN_SCOPE;
1.60      kristaps  336:        if (curp->fflags & FL_NIGN_ESCAPE)
1.24      kristaps  337:                pflags &= ~MDOC_IGN_ESCAPE;
1.60      kristaps  338:        if (curp->fflags & FL_NIGN_MACRO)
1.24      kristaps  339:                pflags &= ~MDOC_IGN_MACRO;
1.19      kristaps  340:
1.92      kristaps  341:        return(mdoc_alloc(&curp->regs, curp, pflags, mmsg));
1.19      kristaps  342: }
                    343:
                    344:
1.64      kristaps  345: static void
1.66      kristaps  346: ffile(const char *file, struct curparse *curp)
1.1       kristaps  347: {
                    348:
1.19      kristaps  349:        curp->file = file;
                    350:        if (-1 == (curp->fd = open(curp->file, O_RDONLY, 0))) {
1.53      kristaps  351:                perror(curp->file);
1.94      schwarze  352:                with_fatal = 1;
1.64      kristaps  353:                return;
1.1       kristaps  354:        }
                    355:
1.66      kristaps  356:        fdesc(curp);
1.1       kristaps  357:
1.19      kristaps  358:        if (-1 == close(curp->fd))
1.53      kristaps  359:                perror(curp->file);
1.1       kristaps  360: }
                    361:
                    362:
1.66      kristaps  363: static int
1.68      joerg     364: resize_buf(struct buf *buf, size_t initial)
                    365: {
                    366:        void *tmp;
                    367:        size_t sz;
                    368:
                    369:        if (buf->sz == 0)
                    370:                sz = initial;
                    371:        else
                    372:                sz = 2 * buf->sz;
                    373:        tmp = realloc(buf->buf, sz);
                    374:        if (NULL == tmp) {
                    375:                perror(NULL);
                    376:                return(0);
                    377:        }
                    378:        buf->buf = tmp;
                    379:        buf->sz = sz;
                    380:        return(1);
                    381: }
                    382:
                    383:
                    384: static int
1.66      kristaps  385: read_whole_file(struct curparse *curp, struct buf *fb, int *with_mmap)
                    386: {
                    387:        struct stat      st;
1.68      joerg     388:        size_t           off;
1.66      kristaps  389:        ssize_t          ssz;
                    390:
                    391:        if (-1 == fstat(curp->fd, &st)) {
                    392:                perror(curp->file);
1.94      schwarze  393:                with_fatal = 1;
1.66      kristaps  394:                return(0);
                    395:        }
                    396:
                    397:        /*
                    398:         * If we're a regular file, try just reading in the whole entry
                    399:         * via mmap().  This is faster than reading it into blocks, and
                    400:         * since each file is only a few bytes to begin with, I'm not
                    401:         * concerned that this is going to tank any machines.
                    402:         */
                    403:
                    404:        if (S_ISREG(st.st_mode)) {
                    405:                if (st.st_size >= (1U << 31)) {
                    406:                        fprintf(stderr, "%s: input too large\n",
                    407:                                        curp->file);
1.94      schwarze  408:                        with_fatal = 1;
1.66      kristaps  409:                        return(0);
                    410:                }
                    411:                *with_mmap = 1;
1.71      kristaps  412:                fb->sz = (size_t)st.st_size;
1.66      kristaps  413:                fb->buf = mmap(NULL, fb->sz, PROT_READ,
1.83      joerg     414:                                MAP_FILE|MAP_SHARED, curp->fd, 0);
1.66      kristaps  415:                if (fb->buf != MAP_FAILED)
                    416:                        return(1);
                    417:        }
                    418:
                    419:        /*
                    420:         * If this isn't a regular file (like, say, stdin), then we must
                    421:         * go the old way and just read things in bit by bit.
                    422:         */
                    423:
                    424:        *with_mmap = 0;
                    425:        off = 0;
                    426:        fb->sz = 0;
                    427:        fb->buf = NULL;
                    428:        for (;;) {
                    429:                if (off == fb->sz) {
                    430:                        if (fb->sz == (1U << 31)) {
                    431:                                fprintf(stderr, "%s: input too large\n",
                    432:                                                curp->file);
                    433:                                break;
                    434:                        }
1.68      joerg     435:                        if (! resize_buf(fb, 65536))
1.66      kristaps  436:                                break;
                    437:                }
1.71      kristaps  438:                ssz = read(curp->fd, fb->buf + (int)off, fb->sz - off);
1.66      kristaps  439:                if (ssz == 0) {
                    440:                        fb->sz = off;
                    441:                        return(1);
                    442:                }
                    443:                if (ssz == -1) {
                    444:                        perror(curp->file);
                    445:                        break;
                    446:                }
1.71      kristaps  447:                off += (size_t)ssz;
1.66      kristaps  448:        }
                    449:
                    450:        free(fb->buf);
                    451:        fb->buf = NULL;
1.94      schwarze  452:        with_fatal = 1;
1.66      kristaps  453:        return(0);
                    454: }
                    455:
                    456:
1.64      kristaps  457: static void
1.66      kristaps  458: fdesc(struct curparse *curp)
1.1       kristaps  459: {
1.66      kristaps  460:        struct buf       ln, blk;
1.76      kristaps  461:        int              i, pos, lnn, lnn_start, with_mmap, of;
1.71      kristaps  462:        enum rofferr     re;
1.19      kristaps  463:        struct man      *man;
                    464:        struct mdoc     *mdoc;
1.71      kristaps  465:        struct roff     *roff;
1.10      kristaps  466:
1.19      kristaps  467:        man = NULL;
                    468:        mdoc = NULL;
1.71      kristaps  469:        roff = NULL;
1.92      kristaps  470:
1.66      kristaps  471:        memset(&ln, 0, sizeof(struct buf));
1.1       kristaps  472:
                    473:        /*
1.70      joerg     474:         * Two buffers: ln and buf.  buf is the input file and may be
                    475:         * memory mapped.  ln is a line buffer and grows on-demand.
1.1       kristaps  476:         */
                    477:
1.71      kristaps  478:        if ( ! read_whole_file(curp, &blk, &with_mmap))
1.64      kristaps  479:                return;
1.1       kristaps  480:
1.71      kristaps  481:        if (NULL == curp->roff)
                    482:                curp->roff = roff_init(curp);
                    483:        if (NULL == (roff = curp->roff))
                    484:                goto bailout;
                    485:
1.70      joerg     486:        for (i = 0, lnn = 1; i < (int)blk.sz;) {
                    487:                pos = 0;
                    488:                lnn_start = lnn;
                    489:                while (i < (int)blk.sz) {
                    490:                        if ('\n' == blk.buf[i]) {
                    491:                                ++i;
                    492:                                ++lnn;
                    493:                                break;
                    494:                        }
                    495:                        /* Trailing backslash is like a plain character. */
                    496:                        if ('\\' != blk.buf[i] || i + 1 == (int)blk.sz) {
                    497:                                if (pos >= (int)ln.sz)
                    498:                                        if (! resize_buf(&ln, 256))
                    499:                                                goto bailout;
                    500:                                ln.buf[pos++] = blk.buf[i++];
1.67      joerg     501:                                continue;
1.70      joerg     502:                        }
                    503:                        /* Found an escape and at least one other character. */
                    504:                        if ('\n' == blk.buf[i + 1]) {
                    505:                                /* Escaped newlines are skipped over */
                    506:                                i += 2;
                    507:                                ++lnn;
1.67      joerg     508:                                continue;
                    509:                        }
1.70      joerg     510:                        if ('"' == blk.buf[i + 1]) {
                    511:                                i += 2;
                    512:                                /* Comment, skip to end of line */
                    513:                                for (; i < (int)blk.sz; ++i) {
                    514:                                        if ('\n' == blk.buf[i]) {
                    515:                                                ++i;
                    516:                                                ++lnn;
                    517:                                                break;
                    518:                                        }
                    519:                                }
                    520:                                /* Backout trailing whitespaces */
                    521:                                for (; pos > 0; --pos) {
                    522:                                        if (ln.buf[pos - 1] != ' ')
                    523:                                                break;
                    524:                                        if (pos > 2 && ln.buf[pos - 2] == '\\')
                    525:                                                break;
                    526:                                }
                    527:                                break;
                    528:                        }
                    529:                        /* Some other escape sequence, copy and continue. */
                    530:                        if (pos + 1 >= (int)ln.sz)
                    531:                                if (! resize_buf(&ln, 256))
                    532:                                        goto bailout;
1.1       kristaps  533:
1.70      joerg     534:                        ln.buf[pos++] = blk.buf[i++];
                    535:                        ln.buf[pos++] = blk.buf[i++];
1.67      joerg     536:                }
1.1       kristaps  537:
1.70      joerg     538:                if (pos >= (int)ln.sz)
                    539:                        if (! resize_buf(&ln, 256))
                    540:                                goto bailout;
1.71      kristaps  541:                ln.buf[pos] = '\0';
                    542:
1.76      kristaps  543:                /*
                    544:                 * A significant amount of complexity is contained by
                    545:                 * the roff preprocessor.  It's line-oriented but can be
                    546:                 * expressed on one line, so we need at times to
                    547:                 * readjust our starting point and re-run it.  The roff
                    548:                 * preprocessor can also readjust the buffers with new
                    549:                 * data, so we pass them in wholesale.
                    550:                 */
                    551:
                    552:                of = 0;
                    553:                do {
1.92      kristaps  554:                        re = roff_parseln(roff, lnn_start,
1.76      kristaps  555:                                        &ln.buf, &ln.sz, of, &of);
                    556:                } while (ROFF_RERUN == re);
                    557:
1.71      kristaps  558:                if (ROFF_IGN == re)
                    559:                        continue;
                    560:                else if (ROFF_ERR == re)
                    561:                        goto bailout;
1.29      kristaps  562:
1.76      kristaps  563:                /*
                    564:                 * If input parsers have not been allocated, do so now.
                    565:                 * We keep these instanced betwen parsers, but set them
                    566:                 * locally per parse routine since we can use different
                    567:                 * parsers with each one.
                    568:                 */
1.19      kristaps  569:
1.76      kristaps  570:                if ( ! (man || mdoc))
                    571:                        if ( ! pset(ln.buf + of, pos - of, curp, &man, &mdoc))
                    572:                                goto bailout;
1.19      kristaps  573:
1.76      kristaps  574:                /* Lastly, push down into the parsers themselves. */
1.30      kristaps  575:
1.92      kristaps  576:                if (man && ! man_parseln(man, lnn_start, ln.buf, of))
1.67      joerg     577:                        goto bailout;
1.92      kristaps  578:                if (mdoc && ! mdoc_parseln(mdoc, lnn_start, ln.buf, of))
1.67      joerg     579:                        goto bailout;
1.1       kristaps  580:        }
                    581:
1.29      kristaps  582:        /* NOTE a parser may not have been assigned, yet. */
1.19      kristaps  583:
1.22      kristaps  584:        if ( ! (man || mdoc)) {
1.53      kristaps  585:                fprintf(stderr, "%s: Not a manual\n", curp->file);
1.64      kristaps  586:                goto bailout;
1.22      kristaps  587:        }
1.76      kristaps  588:
                    589:        /* Clean up the parse routine ASTs. */
1.22      kristaps  590:
                    591:        if (mdoc && ! mdoc_endparse(mdoc))
1.64      kristaps  592:                goto bailout;
1.22      kristaps  593:        if (man && ! man_endparse(man))
1.64      kristaps  594:                goto bailout;
1.71      kristaps  595:        if (roff && ! roff_endparse(roff))
                    596:                goto bailout;
1.19      kristaps  597:
1.29      kristaps  598:        /* If unset, allocate output dev now (if applicable). */
1.22      kristaps  599:
                    600:        if ( ! (curp->outman && curp->outmdoc)) {
                    601:                switch (curp->outtype) {
1.59      kristaps  602:                case (OUTT_XHTML):
                    603:                        curp->outdata = xhtml_alloc(curp->outopts);
                    604:                        break;
1.43      kristaps  605:                case (OUTT_HTML):
1.44      kristaps  606:                        curp->outdata = html_alloc(curp->outopts);
1.85      kristaps  607:                        break;
                    608:                case (OUTT_ASCII):
                    609:                        curp->outdata = ascii_alloc(curp->outopts);
1.86      kristaps  610:                        curp->outfree = ascii_free;
1.85      kristaps  611:                        break;
                    612:                case (OUTT_PS):
1.93      kristaps  613:                        curp->outdata = ps_alloc(curp->outopts);
1.86      kristaps  614:                        curp->outfree = ps_free;
1.85      kristaps  615:                        break;
                    616:                default:
                    617:                        break;
                    618:                }
                    619:
                    620:                switch (curp->outtype) {
                    621:                case (OUTT_HTML):
                    622:                        /* FALLTHROUGH */
                    623:                case (OUTT_XHTML):
1.43      kristaps  624:                        curp->outman = html_man;
                    625:                        curp->outmdoc = html_mdoc;
                    626:                        curp->outfree = html_free;
                    627:                        break;
1.22      kristaps  628:                case (OUTT_TREE):
                    629:                        curp->outman = tree_man;
                    630:                        curp->outmdoc = tree_mdoc;
                    631:                        break;
1.85      kristaps  632:                case (OUTT_ASCII):
                    633:                        /* FALLTHROUGH */
                    634:                case (OUTT_PS):
1.22      kristaps  635:                        curp->outman = terminal_man;
                    636:                        curp->outmdoc = terminal_mdoc;
                    637:                        break;
1.85      kristaps  638:                default:
                    639:                        break;
1.22      kristaps  640:                }
                    641:        }
                    642:
                    643:        /* Execute the out device, if it exists. */
                    644:
                    645:        if (man && curp->outman)
1.42      kristaps  646:                (*curp->outman)(curp->outdata, man);
1.22      kristaps  647:        if (mdoc && curp->outmdoc)
1.42      kristaps  648:                (*curp->outmdoc)(curp->outdata, mdoc);
1.22      kristaps  649:
1.64      kristaps  650:  cleanup:
1.92      kristaps  651:        memset(&curp->regs, 0, sizeof(struct regset));
1.71      kristaps  652:        if (mdoc)
                    653:                mdoc_reset(mdoc);
                    654:        if (man)
                    655:                man_reset(man);
                    656:        if (roff)
                    657:                roff_reset(roff);
1.66      kristaps  658:        if (ln.buf)
                    659:                free(ln.buf);
                    660:        if (with_mmap)
                    661:                munmap(blk.buf, blk.sz);
                    662:        else
                    663:                free(blk.buf);
1.71      kristaps  664:
1.64      kristaps  665:        return;
                    666:
                    667:  bailout:
1.94      schwarze  668:        with_fatal = 1;
1.64      kristaps  669:        goto cleanup;
1.19      kristaps  670: }
                    671:
                    672:
                    673: static int
1.22      kristaps  674: pset(const char *buf, int pos, struct curparse *curp,
1.19      kristaps  675:                struct man **man, struct mdoc **mdoc)
                    676: {
1.29      kristaps  677:        int              i;
1.19      kristaps  678:
                    679:        /*
                    680:         * Try to intuit which kind of manual parser should be used.  If
                    681:         * passed in by command-line (-man, -mdoc), then use that
                    682:         * explicitly.  If passed as -mandoc, then try to guess from the
1.30      kristaps  683:         * line: either skip dot-lines, use -mdoc when finding `.Dt', or
1.19      kristaps  684:         * default to -man, which is more lenient.
                    685:         */
                    686:
1.75      kristaps  687:        if ('.' == buf[0] || '\'' == buf[0]) {
1.29      kristaps  688:                for (i = 1; buf[i]; i++)
                    689:                        if (' ' != buf[i] && '\t' != buf[i])
                    690:                                break;
                    691:                if (0 == buf[i])
                    692:                        return(1);
                    693:        }
1.10      kristaps  694:
1.19      kristaps  695:        switch (curp->inttype) {
                    696:        case (INTT_MDOC):
                    697:                if (NULL == curp->mdoc)
                    698:                        curp->mdoc = mdoc_init(curp);
                    699:                if (NULL == (*mdoc = curp->mdoc))
                    700:                        return(0);
                    701:                return(1);
                    702:        case (INTT_MAN):
                    703:                if (NULL == curp->man)
                    704:                        curp->man = man_init(curp);
                    705:                if (NULL == (*man = curp->man))
                    706:                        return(0);
                    707:                return(1);
                    708:        default:
                    709:                break;
                    710:        }
                    711:
                    712:        if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3))  {
                    713:                if (NULL == curp->mdoc)
                    714:                        curp->mdoc = mdoc_init(curp);
                    715:                if (NULL == (*mdoc = curp->mdoc))
                    716:                        return(0);
                    717:                return(1);
                    718:        }
                    719:
                    720:        if (NULL == curp->man)
                    721:                curp->man = man_init(curp);
                    722:        if (NULL == (*man = curp->man))
                    723:                return(0);
                    724:        return(1);
1.10      kristaps  725: }
                    726:
                    727:
                    728: static int
                    729: moptions(enum intt *tflags, char *arg)
                    730: {
                    731:
1.17      kristaps  732:        if (0 == strcmp(arg, "doc"))
1.10      kristaps  733:                *tflags = INTT_MDOC;
1.19      kristaps  734:        else if (0 == strcmp(arg, "andoc"))
                    735:                *tflags = INTT_AUTO;
1.17      kristaps  736:        else if (0 == strcmp(arg, "an"))
1.10      kristaps  737:                *tflags = INTT_MAN;
                    738:        else {
1.57      kristaps  739:                fprintf(stderr, "%s: Bad argument\n", arg);
1.10      kristaps  740:                return(0);
                    741:        }
                    742:
                    743:        return(1);
1.1       kristaps  744: }
                    745:
                    746:
                    747: static int
1.60      kristaps  748: toptions(struct curparse *curp, char *arg)
1.1       kristaps  749: {
                    750:
                    751:        if (0 == strcmp(arg, "ascii"))
1.60      kristaps  752:                curp->outtype = OUTT_ASCII;
                    753:        else if (0 == strcmp(arg, "lint")) {
                    754:                curp->outtype = OUTT_LINT;
                    755:                curp->wflags |= WARN_WALL;
                    756:                curp->fflags |= FL_STRICT;
                    757:        }
1.1       kristaps  758:        else if (0 == strcmp(arg, "tree"))
1.60      kristaps  759:                curp->outtype = OUTT_TREE;
1.43      kristaps  760:        else if (0 == strcmp(arg, "html"))
1.60      kristaps  761:                curp->outtype = OUTT_HTML;
1.59      kristaps  762:        else if (0 == strcmp(arg, "xhtml"))
1.60      kristaps  763:                curp->outtype = OUTT_XHTML;
1.85      kristaps  764:        else if (0 == strcmp(arg, "ps"))
                    765:                curp->outtype = OUTT_PS;
1.1       kristaps  766:        else {
1.57      kristaps  767:                fprintf(stderr, "%s: Bad argument\n", arg);
1.1       kristaps  768:                return(0);
                    769:        }
                    770:
                    771:        return(1);
                    772: }
                    773:
                    774:
                    775: static int
                    776: foptions(int *fflags, char *arg)
                    777: {
1.34      kristaps  778:        char            *v, *o;
1.50      kristaps  779:        const char      *toks[8];
1.1       kristaps  780:
                    781:        toks[0] = "ign-scope";
1.24      kristaps  782:        toks[1] = "no-ign-escape";
                    783:        toks[2] = "no-ign-macro";
1.62      kristaps  784:        toks[3] = "ign-errors";
                    785:        toks[4] = "strict";
                    786:        toks[5] = "ign-escape";
                    787:        toks[6] = NULL;
1.1       kristaps  788:
1.34      kristaps  789:        while (*arg) {
                    790:                o = arg;
1.45      kristaps  791:                switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1       kristaps  792:                case (0):
1.60      kristaps  793:                        *fflags |= FL_IGN_SCOPE;
1.1       kristaps  794:                        break;
                    795:                case (1):
1.60      kristaps  796:                        *fflags |= FL_NIGN_ESCAPE;
1.1       kristaps  797:                        break;
                    798:                case (2):
1.60      kristaps  799:                        *fflags |= FL_NIGN_MACRO;
1.1       kristaps  800:                        break;
1.20      kristaps  801:                case (3):
1.62      kristaps  802:                        *fflags |= FL_IGN_ERRORS;
1.24      kristaps  803:                        break;
                    804:                case (4):
1.62      kristaps  805:                        *fflags |= FL_STRICT;
1.39      kristaps  806:                        break;
                    807:                case (5):
1.60      kristaps  808:                        *fflags &= ~FL_NIGN_ESCAPE;
1.50      kristaps  809:                        break;
1.1       kristaps  810:                default:
1.57      kristaps  811:                        fprintf(stderr, "%s: Bad argument\n", o);
1.1       kristaps  812:                        return(0);
                    813:                }
1.34      kristaps  814:        }
1.1       kristaps  815:
                    816:        return(1);
                    817: }
                    818:
                    819:
                    820: static int
                    821: woptions(int *wflags, char *arg)
                    822: {
1.34      kristaps  823:        char            *v, *o;
1.45      kristaps  824:        const char      *toks[3];
1.1       kristaps  825:
                    826:        toks[0] = "all";
1.36      kristaps  827:        toks[1] = "error";
                    828:        toks[2] = NULL;
1.1       kristaps  829:
1.34      kristaps  830:        while (*arg) {
                    831:                o = arg;
1.45      kristaps  832:                switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1       kristaps  833:                case (0):
                    834:                        *wflags |= WARN_WALL;
                    835:                        break;
                    836:                case (1):
                    837:                        *wflags |= WARN_WERR;
                    838:                        break;
                    839:                default:
1.57      kristaps  840:                        fprintf(stderr, "%s: Bad argument\n", o);
1.1       kristaps  841:                        return(0);
                    842:                }
1.34      kristaps  843:        }
1.1       kristaps  844:
                    845:        return(1);
                    846: }
                    847:
                    848:
1.71      kristaps  849: static int
                    850: mmsg(enum mandocerr t, void *arg, int ln, int col, const char *msg)
                    851: {
                    852:        struct curparse *cp;
1.94      schwarze  853:        const char *level;
                    854:        int rc;
1.71      kristaps  855:
                    856:        cp = (struct curparse *)arg;
1.94      schwarze  857:        level = NULL;
                    858:        rc = 1;
1.71      kristaps  859:
1.94      schwarze  860:        if (t >= MANDOCERR_FATAL) {
                    861:                with_fatal = 1;
                    862:                level = "FATAL";
                    863:                rc = 0;
                    864:        } else {
                    865:                if ( ! (WARN_WALL & cp->wflags))
1.79      kristaps  866:                        return(1);
1.94      schwarze  867:                if (t >= MANDOCERR_ERROR) {
                    868:                        with_error = 1;
                    869:                        level = "ERROR";
                    870:                }
                    871:                if (WARN_WERR & cp->wflags) {
                    872:                        with_fatal = 1;
                    873:                        rc = 0;
                    874:                }
                    875:        }
1.73      kristaps  876:
1.94      schwarze  877:        fprintf(stderr, "%s:%d:%d:", cp->file, ln, col + 1);
                    878:        if (level)
                    879:                fprintf(stderr, " %s:", level);
                    880:        fprintf(stderr, " %s", mandocerrs[t]);
1.73      kristaps  881:        if (msg)
                    882:                fprintf(stderr, ": %s", msg);
                    883:        fputc('\n', stderr);
1.79      kristaps  884:
1.94      schwarze  885:        return(rc);
1.71      kristaps  886: }

CVSweb