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

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

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