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

Annotation of mandoc/main.c, Revision 1.92

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

CVSweb