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

Annotation of mandoc/main.c, Revision 1.120

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

CVSweb