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

Annotation of mandoc/main.c, Revision 1.111

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

CVSweb