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

Annotation of mandoc/main.c, Revision 1.118

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

CVSweb