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

Annotation of mandoc/main.c, Revision 1.88

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

CVSweb