[BACK]Return to main.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/main.c, Revision 1.57

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