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

Annotation of mandoc/main.c, Revision 1.94

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

CVSweb