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

Annotation of mandoc/main.c, Revision 1.102

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

CVSweb