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

Annotation of mandoc/main.c, Revision 1.113

1.113   ! kristaps    1: /*     $Id: main.c,v 1.112 2010/12/01 16:28:23 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:        "superfluous width argument",
                    137:        "bad date argument",
                    138:        "bad width argument",
1.81      kristaps  139:        "unknown manual section",
1.79      kristaps  140:        "section not in conventional manual section",
                    141:        "end of line whitespace",
1.95      schwarze  142:        "blocks badly nested",
1.94      schwarze  143:
                    144:        "generic error",
                    145:
1.79      kristaps  146:        "NAME section must come first",
                    147:        "bad Boolean value",
                    148:        "child violates parent syntax",
                    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.79      kristaps  172:        "macro requires line argument(s)",
                    173:        "macro requires body argument(s)",
                    174:        "macro requires argument(s)",
                    175:        "no title in document",
1.82      kristaps  176:        "missing list type",
1.87      kristaps  177:        "missing display type",
1.96      kristaps  178:        "missing font type",
1.79      kristaps  179:        "line argument(s) will be lost",
                    180:        "body argument(s) will be lost",
1.107     kristaps  181:        "paragraph macro ignored",
1.94      schwarze  182:
                    183:        "generic fatal error",
                    184:
1.80      kristaps  185:        "column syntax is inconsistent",
1.79      kristaps  186:        "displays may not be nested",
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",
                    197:        "utsname system call failed",
1.103     schwarze  198:        "static buffer exhausted",
1.5       kristaps  199: };
1.1       kristaps  200:
1.111     kristaps  201: static void              parsebuf(struct curparse *, struct buf, int);
                    202: static void              pdesc(struct curparse *);
1.66      kristaps  203: static void              fdesc(struct curparse *);
                    204: static void              ffile(const char *, struct curparse *);
1.111     kristaps  205: static int               pfile(const char *, struct curparse *);
1.10      kristaps  206: static int               moptions(enum intt *, char *);
1.71      kristaps  207: static int               mmsg(enum mandocerr, void *,
                    208:                                int, int, const char *);
1.112     kristaps  209: static void              pset(const char *, int, struct curparse *);
1.66      kristaps  210: static int               toptions(struct curparse *, char *);
                    211: static void              usage(void) __attribute__((noreturn));
1.55      kristaps  212: static void              version(void) __attribute__((noreturn));
1.103     schwarze  213: static int               woptions(struct curparse *, char *);
1.1       kristaps  214:
1.54      kristaps  215: static const char       *progname;
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);
                    352:                exit_status = MANDOCLEVEL_SYSERR;
                    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:
                    370:        return(MANDOCLEVEL_FATAL > exit_status ? 1 : 0);
                    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;
                    474:
                    475:        /* Make sure the mandotory roff parser is initialised. */
                    476:
                    477:        if (NULL == curp->roff) {
                    478:                curp->roff = roff_alloc(&curp->regs, curp, mmsg);
                    479:                assert(curp->roff);
                    480:        }
                    481:
                    482:        /* Fully parse the file. */
1.10      kristaps  483:
1.111     kristaps  484:        pdesc(curp);
1.92      kristaps  485:
1.111     kristaps  486:        if (MANDOCLEVEL_FATAL <= exit_status)
                    487:                goto cleanup;
                    488:
                    489:        /* NOTE a parser may not have been assigned, yet. */
                    490:
1.112     kristaps  491:        if ( ! (curp->man || curp->mdoc)) {
1.111     kristaps  492:                fprintf(stderr, "%s: Not a manual\n", curp->file);
                    493:                exit_status = MANDOCLEVEL_FATAL;
                    494:                goto cleanup;
                    495:        }
                    496:
                    497:        /* Clean up the parse routine ASTs. */
                    498:
1.112     kristaps  499:        if (curp->mdoc && ! mdoc_endparse(curp->mdoc)) {
1.111     kristaps  500:                assert(MANDOCLEVEL_FATAL <= exit_status);
                    501:                goto cleanup;
                    502:        }
1.112     kristaps  503:
                    504:        if (curp->man && ! man_endparse(curp->man)) {
1.111     kristaps  505:                assert(MANDOCLEVEL_FATAL <= exit_status);
                    506:                goto cleanup;
                    507:        }
1.112     kristaps  508:
                    509:        assert(curp->roff);
                    510:        if ( ! roff_endparse(curp->roff)) {
1.111     kristaps  511:                assert(MANDOCLEVEL_FATAL <= exit_status);
                    512:                goto cleanup;
                    513:        }
1.1       kristaps  514:
                    515:        /*
1.111     kristaps  516:         * With -Wstop and warnings or errors of at least
                    517:         * the requested level, do not produce output.
1.1       kristaps  518:         */
                    519:
1.111     kristaps  520:        if (MANDOCLEVEL_OK != exit_status && curp->wstop)
                    521:                goto cleanup;
                    522:
                    523:        /* If unset, allocate output dev now (if applicable). */
                    524:
                    525:        if ( ! (curp->outman && curp->outmdoc)) {
                    526:                switch (curp->outtype) {
                    527:                case (OUTT_XHTML):
                    528:                        curp->outdata = xhtml_alloc(curp->outopts);
                    529:                        break;
                    530:                case (OUTT_HTML):
                    531:                        curp->outdata = html_alloc(curp->outopts);
                    532:                        break;
                    533:                case (OUTT_ASCII):
                    534:                        curp->outdata = ascii_alloc(curp->outopts);
                    535:                        curp->outfree = ascii_free;
                    536:                        break;
                    537:                case (OUTT_PDF):
                    538:                        curp->outdata = pdf_alloc(curp->outopts);
                    539:                        curp->outfree = pspdf_free;
                    540:                        break;
                    541:                case (OUTT_PS):
                    542:                        curp->outdata = ps_alloc(curp->outopts);
                    543:                        curp->outfree = pspdf_free;
                    544:                        break;
                    545:                default:
                    546:                        break;
                    547:                }
                    548:
                    549:                switch (curp->outtype) {
                    550:                case (OUTT_HTML):
                    551:                        /* FALLTHROUGH */
                    552:                case (OUTT_XHTML):
                    553:                        curp->outman = html_man;
                    554:                        curp->outmdoc = html_mdoc;
                    555:                        curp->outfree = html_free;
                    556:                        break;
                    557:                case (OUTT_TREE):
                    558:                        curp->outman = tree_man;
                    559:                        curp->outmdoc = tree_mdoc;
                    560:                        break;
                    561:                case (OUTT_PDF):
                    562:                        /* FALLTHROUGH */
                    563:                case (OUTT_ASCII):
                    564:                        /* FALLTHROUGH */
                    565:                case (OUTT_PS):
                    566:                        curp->outman = terminal_man;
                    567:                        curp->outmdoc = terminal_mdoc;
                    568:                        break;
                    569:                default:
                    570:                        break;
                    571:                }
                    572:        }
                    573:
                    574:        /* Execute the out device, if it exists. */
                    575:
1.112     kristaps  576:        if (curp->man && curp->outman)
                    577:                (*curp->outman)(curp->outdata, curp->man);
                    578:        if (curp->mdoc && curp->outmdoc)
                    579:                (*curp->outmdoc)(curp->outdata, curp->mdoc);
1.111     kristaps  580:
                    581:  cleanup:
1.112     kristaps  582:
1.111     kristaps  583:        memset(&curp->regs, 0, sizeof(struct regset));
1.112     kristaps  584:
                    585:        /* Reset the current-parse compilers. */
                    586:
                    587:        if (curp->mdoc)
                    588:                mdoc_reset(curp->mdoc);
                    589:        if (curp->man)
                    590:                man_reset(curp->man);
                    591:
                    592:        assert(curp->roff);
                    593:        roff_reset(curp->roff);
1.111     kristaps  594:
                    595:        return;
                    596: }
                    597:
                    598: static void
                    599: pdesc(struct curparse *curp)
                    600: {
                    601:        struct buf       blk;
                    602:        int              with_mmap;
                    603:
1.112     kristaps  604:        /*
                    605:         * Run for each opened file; may be called more than once for
                    606:         * each full parse sequence if the opened file is nested (i.e.,
                    607:         * from `so').  Simply sucks in the whole file and moves into
                    608:         * the parse phase for the file.
                    609:         */
                    610:
1.103     schwarze  611:        if ( ! read_whole_file(curp, &blk, &with_mmap)) {
                    612:                exit_status = MANDOCLEVEL_SYSERR;
1.64      kristaps  613:                return;
1.103     schwarze  614:        }
1.1       kristaps  615:
1.112     kristaps  616:        /* Line number is per-file. */
1.111     kristaps  617:
                    618:        curp->line = 1;
1.112     kristaps  619:
1.111     kristaps  620:        parsebuf(curp, blk, 1);
                    621:
                    622:        if (with_mmap)
                    623:                munmap(blk.buf, blk.sz);
                    624:        else
                    625:                free(blk.buf);
                    626: }
                    627:
                    628: static void
                    629: parsebuf(struct curparse *curp, struct buf blk, int start)
                    630: {
                    631:        struct buf       ln;
1.112     kristaps  632:        int              i, of, rc;
                    633:        int              pos; /* byte number in the ln buffer */
                    634:        int              lnn; /* line number in the real file */
1.111     kristaps  635:        unsigned char    c;
1.112     kristaps  636:
                    637:        /*
                    638:         * Main parse routine for an opened file.  This is called for
                    639:         * each opened file and simply loops around the full input file,
                    640:         * possibly nesting (i.e., with `so').
                    641:         */
1.71      kristaps  642:
1.111     kristaps  643:        memset(&ln, 0, sizeof(struct buf));
                    644:
1.112     kristaps  645:        lnn = curp->line;
                    646:        pos = 0;
1.111     kristaps  647:
1.112     kristaps  648:        for (i = 0; i < (int)blk.sz; ) {
1.111     kristaps  649:                if (0 == pos && '\0' == blk.buf[i])
                    650:                        break;
1.112     kristaps  651:
1.111     kristaps  652:                if (start)
                    653:                        curp->line = lnn;
                    654:
                    655:                while (i < (int)blk.sz && (start || '\0' != blk.buf[i])) {
1.70      joerg     656:                        if ('\n' == blk.buf[i]) {
                    657:                                ++i;
                    658:                                ++lnn;
                    659:                                break;
                    660:                        }
1.99      kristaps  661:
                    662:                        /*
                    663:                         * Warn about bogus characters.  If you're using
                    664:                         * non-ASCII encoding, you're screwing your
                    665:                         * readers.  Since I'd rather this not happen,
                    666:                         * I'll be helpful and drop these characters so
                    667:                         * we don't display gibberish.  Note to manual
                    668:                         * writers: use special characters.
                    669:                         */
                    670:
1.102     schwarze  671:                        c = (unsigned char) blk.buf[i];
1.112     kristaps  672:
                    673:                        if ( ! (isascii(c) &&
                    674:                                        (isgraph(c) || isblank(c)))) {
1.103     schwarze  675:                                mmsg(MANDOCERR_BADCHAR, curp,
1.111     kristaps  676:                                    curp->line, pos, "ignoring byte");
1.99      kristaps  677:                                i++;
                    678:                                continue;
                    679:                        }
                    680:
1.112     kristaps  681:                        /* Trailing backslash = a plain char. */
                    682:
1.70      joerg     683:                        if ('\\' != blk.buf[i] || i + 1 == (int)blk.sz) {
                    684:                                if (pos >= (int)ln.sz)
1.103     schwarze  685:                                        resize_buf(&ln, 256);
1.70      joerg     686:                                ln.buf[pos++] = blk.buf[i++];
1.67      joerg     687:                                continue;
1.70      joerg     688:                        }
1.112     kristaps  689:
                    690:                        /* Found escape & at least one other char. */
                    691:
1.70      joerg     692:                        if ('\n' == blk.buf[i + 1]) {
1.112     kristaps  693:                                i += 2;
1.70      joerg     694:                                /* Escaped newlines are skipped over */
                    695:                                ++lnn;
1.67      joerg     696:                                continue;
                    697:                        }
1.112     kristaps  698:
1.70      joerg     699:                        if ('"' == blk.buf[i + 1]) {
                    700:                                i += 2;
                    701:                                /* Comment, skip to end of line */
                    702:                                for (; i < (int)blk.sz; ++i) {
                    703:                                        if ('\n' == blk.buf[i]) {
                    704:                                                ++i;
                    705:                                                ++lnn;
                    706:                                                break;
                    707:                                        }
                    708:                                }
1.112     kristaps  709:
1.70      joerg     710:                                /* Backout trailing whitespaces */
                    711:                                for (; pos > 0; --pos) {
                    712:                                        if (ln.buf[pos - 1] != ' ')
                    713:                                                break;
                    714:                                        if (pos > 2 && ln.buf[pos - 2] == '\\')
                    715:                                                break;
                    716:                                }
                    717:                                break;
                    718:                        }
1.112     kristaps  719:
                    720:                        /* Some other escape sequence, copy & cont. */
                    721:
1.70      joerg     722:                        if (pos + 1 >= (int)ln.sz)
1.103     schwarze  723:                                resize_buf(&ln, 256);
1.1       kristaps  724:
1.70      joerg     725:                        ln.buf[pos++] = blk.buf[i++];
                    726:                        ln.buf[pos++] = blk.buf[i++];
1.67      joerg     727:                }
1.1       kristaps  728:
1.70      joerg     729:                if (pos >= (int)ln.sz)
1.103     schwarze  730:                        resize_buf(&ln, 256);
1.112     kristaps  731:
1.71      kristaps  732:                ln.buf[pos] = '\0';
                    733:
1.76      kristaps  734:                /*
                    735:                 * A significant amount of complexity is contained by
                    736:                 * the roff preprocessor.  It's line-oriented but can be
                    737:                 * expressed on one line, so we need at times to
                    738:                 * readjust our starting point and re-run it.  The roff
                    739:                 * preprocessor can also readjust the buffers with new
                    740:                 * data, so we pass them in wholesale.
                    741:                 */
                    742:
                    743:                of = 0;
1.112     kristaps  744:
1.111     kristaps  745: rerun:
1.112     kristaps  746:                rc = roff_parseln
                    747:                        (curp->roff, curp->line,
                    748:                         &ln.buf, &ln.sz, of, &of);
                    749:
                    750:                switch (rc) {
1.111     kristaps  751:                case (ROFF_REPARSE):
                    752:                        parsebuf(curp, ln, 0);
                    753:                        pos = 0;
                    754:                        continue;
                    755:                case (ROFF_APPEND):
                    756:                        pos = strlen(ln.buf);
                    757:                        continue;
                    758:                case (ROFF_RERUN):
                    759:                        goto rerun;
                    760:                case (ROFF_IGN):
                    761:                        pos = 0;
1.71      kristaps  762:                        continue;
1.111     kristaps  763:                case (ROFF_ERR):
1.103     schwarze  764:                        assert(MANDOCLEVEL_FATAL <= exit_status);
1.111     kristaps  765:                        break;
                    766:                case (ROFF_SO):
                    767:                        if (pfile(ln.buf + of, curp)) {
                    768:                                pos = 0;
                    769:                                continue;
                    770:                        } else
                    771:                                break;
                    772:                case (ROFF_CONT):
                    773:                        break;
1.103     schwarze  774:                }
1.29      kristaps  775:
1.76      kristaps  776:                /*
                    777:                 * If input parsers have not been allocated, do so now.
                    778:                 * We keep these instanced betwen parsers, but set them
                    779:                 * locally per parse routine since we can use different
                    780:                 * parsers with each one.
                    781:                 */
1.19      kristaps  782:
1.112     kristaps  783:                if ( ! (curp->man || curp->mdoc))
                    784:                        pset(ln.buf + of, pos - of, curp);
1.19      kristaps  785:
1.112     kristaps  786:                /*
                    787:                 * Lastly, push down into the parsers themselves.  One
                    788:                 * of these will have already been set in the pset()
                    789:                 * routine.
                    790:                 */
                    791:
                    792:                if (curp->man || curp->mdoc) {
                    793:                        rc = curp->man ?
                    794:                                man_parseln(curp->man,
                    795:                                        curp->line, ln.buf, of) :
                    796:                                mdoc_parseln(curp->mdoc,
                    797:                                        curp->line, ln.buf, of);
1.30      kristaps  798:
1.112     kristaps  799:                        if ( ! rc) {
                    800:                                assert(MANDOCLEVEL_FATAL <= exit_status);
                    801:                                break;
                    802:                        }
1.103     schwarze  803:                }
1.19      kristaps  804:
1.111     kristaps  805:                /* Temporary buffers typically are not full. */
1.112     kristaps  806:
1.111     kristaps  807:                if (0 == start && '\0' == blk.buf[i])
1.59      kristaps  808:                        break;
1.85      kristaps  809:
1.111     kristaps  810:                /* Start the next input line. */
1.112     kristaps  811:
1.111     kristaps  812:                pos = 0;
1.22      kristaps  813:        }
                    814:
1.111     kristaps  815:        free(ln.buf);
1.19      kristaps  816: }
                    817:
1.103     schwarze  818: static void
1.112     kristaps  819: pset(const char *buf, int pos, struct curparse *curp)
1.19      kristaps  820: {
1.29      kristaps  821:        int              i;
1.19      kristaps  822:
                    823:        /*
                    824:         * Try to intuit which kind of manual parser should be used.  If
                    825:         * passed in by command-line (-man, -mdoc), then use that
                    826:         * explicitly.  If passed as -mandoc, then try to guess from the
1.30      kristaps  827:         * line: either skip dot-lines, use -mdoc when finding `.Dt', or
1.19      kristaps  828:         * default to -man, which is more lenient.
1.112     kristaps  829:         *
                    830:         * Separate out pmdoc/pman from mdoc/man: the first persists
                    831:         * through all parsers, while the latter is used per-parse.
1.19      kristaps  832:         */
                    833:
1.75      kristaps  834:        if ('.' == buf[0] || '\'' == buf[0]) {
1.29      kristaps  835:                for (i = 1; buf[i]; i++)
                    836:                        if (' ' != buf[i] && '\t' != buf[i])
                    837:                                break;
1.103     schwarze  838:                if ('\0' == buf[i])
                    839:                        return;
1.29      kristaps  840:        }
1.10      kristaps  841:
1.19      kristaps  842:        switch (curp->inttype) {
                    843:        case (INTT_MDOC):
1.112     kristaps  844:                if (NULL == curp->pmdoc)
                    845:                        curp->pmdoc = mdoc_alloc
                    846:                                (&curp->regs, curp, mmsg);
                    847:                assert(curp->pmdoc);
                    848:                curp->mdoc = curp->pmdoc;
1.103     schwarze  849:                return;
1.19      kristaps  850:        case (INTT_MAN):
1.112     kristaps  851:                if (NULL == curp->pman)
                    852:                        curp->pman = man_alloc
                    853:                                (&curp->regs, curp, mmsg);
                    854:                assert(curp->pman);
                    855:                curp->man = curp->pman;
1.103     schwarze  856:                return;
1.19      kristaps  857:        default:
                    858:                break;
                    859:        }
                    860:
                    861:        if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3))  {
1.112     kristaps  862:                if (NULL == curp->pmdoc)
                    863:                        curp->pmdoc = mdoc_alloc
                    864:                                (&curp->regs, curp, mmsg);
                    865:                assert(curp->pmdoc);
                    866:                curp->mdoc = curp->pmdoc;
1.103     schwarze  867:                return;
1.19      kristaps  868:        }
                    869:
1.112     kristaps  870:        if (NULL == curp->pman)
                    871:                curp->pman = man_alloc(&curp->regs, curp, mmsg);
                    872:        assert(curp->pman);
                    873:        curp->man = curp->pman;
1.10      kristaps  874: }
                    875:
                    876: static int
                    877: moptions(enum intt *tflags, char *arg)
                    878: {
                    879:
1.17      kristaps  880:        if (0 == strcmp(arg, "doc"))
1.10      kristaps  881:                *tflags = INTT_MDOC;
1.19      kristaps  882:        else if (0 == strcmp(arg, "andoc"))
                    883:                *tflags = INTT_AUTO;
1.17      kristaps  884:        else if (0 == strcmp(arg, "an"))
1.10      kristaps  885:                *tflags = INTT_MAN;
                    886:        else {
1.57      kristaps  887:                fprintf(stderr, "%s: Bad argument\n", arg);
1.10      kristaps  888:                return(0);
                    889:        }
                    890:
                    891:        return(1);
1.1       kristaps  892: }
                    893:
                    894: static int
1.60      kristaps  895: toptions(struct curparse *curp, char *arg)
1.1       kristaps  896: {
                    897:
                    898:        if (0 == strcmp(arg, "ascii"))
1.60      kristaps  899:                curp->outtype = OUTT_ASCII;
                    900:        else if (0 == strcmp(arg, "lint")) {
                    901:                curp->outtype = OUTT_LINT;
1.103     schwarze  902:                curp->wlevel  = MANDOCLEVEL_WARNING;
1.60      kristaps  903:        }
1.1       kristaps  904:        else if (0 == strcmp(arg, "tree"))
1.60      kristaps  905:                curp->outtype = OUTT_TREE;
1.43      kristaps  906:        else if (0 == strcmp(arg, "html"))
1.60      kristaps  907:                curp->outtype = OUTT_HTML;
1.59      kristaps  908:        else if (0 == strcmp(arg, "xhtml"))
1.60      kristaps  909:                curp->outtype = OUTT_XHTML;
1.85      kristaps  910:        else if (0 == strcmp(arg, "ps"))
                    911:                curp->outtype = OUTT_PS;
1.100     kristaps  912:        else if (0 == strcmp(arg, "pdf"))
                    913:                curp->outtype = OUTT_PDF;
1.1       kristaps  914:        else {
1.57      kristaps  915:                fprintf(stderr, "%s: Bad argument\n", arg);
1.1       kristaps  916:                return(0);
                    917:        }
                    918:
                    919:        return(1);
                    920: }
                    921:
                    922: static int
1.103     schwarze  923: woptions(struct curparse *curp, char *arg)
1.1       kristaps  924: {
1.34      kristaps  925:        char            *v, *o;
1.103     schwarze  926:        const char      *toks[6];
1.1       kristaps  927:
1.103     schwarze  928:        toks[0] = "stop";
                    929:        toks[1] = "all";
                    930:        toks[2] = "warning";
                    931:        toks[3] = "error";
                    932:        toks[4] = "fatal";
                    933:        toks[5] = NULL;
1.1       kristaps  934:
1.34      kristaps  935:        while (*arg) {
                    936:                o = arg;
1.45      kristaps  937:                switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1       kristaps  938:                case (0):
1.103     schwarze  939:                        curp->wstop = 1;
1.1       kristaps  940:                        break;
                    941:                case (1):
1.103     schwarze  942:                        /* FALLTHROUGH */
1.1       kristaps  943:                case (2):
1.103     schwarze  944:                        curp->wlevel = MANDOCLEVEL_WARNING;
1.1       kristaps  945:                        break;
1.20      kristaps  946:                case (3):
1.103     schwarze  947:                        curp->wlevel = MANDOCLEVEL_ERROR;
1.24      kristaps  948:                        break;
                    949:                case (4):
1.103     schwarze  950:                        curp->wlevel = MANDOCLEVEL_FATAL;
1.50      kristaps  951:                        break;
1.1       kristaps  952:                default:
1.103     schwarze  953:                        fprintf(stderr, "-W%s: Bad argument\n", o);
1.1       kristaps  954:                        return(0);
                    955:                }
1.34      kristaps  956:        }
1.1       kristaps  957:
                    958:        return(1);
                    959: }
                    960:
1.71      kristaps  961: static int
                    962: mmsg(enum mandocerr t, void *arg, int ln, int col, const char *msg)
                    963: {
                    964:        struct curparse *cp;
1.103     schwarze  965:        enum mandoclevel level;
                    966:
                    967:        level = MANDOCLEVEL_FATAL;
                    968:        while (t < mandoclimits[level])
1.105     kristaps  969:                /* LINTED */
1.103     schwarze  970:                level--;
1.71      kristaps  971:
                    972:        cp = (struct curparse *)arg;
1.103     schwarze  973:        if (level < cp->wlevel)
                    974:                return(1);
1.73      kristaps  975:
1.103     schwarze  976:        fprintf(stderr, "%s:%d:%d: %s: %s",
                    977:            cp->file, ln, col + 1, mandoclevels[level], mandocerrs[t]);
1.73      kristaps  978:        if (msg)
                    979:                fprintf(stderr, ": %s", msg);
                    980:        fputc('\n', stderr);
1.79      kristaps  981:
1.103     schwarze  982:        if (exit_status < level)
                    983:                exit_status = level;
                    984:
                    985:        return(level < MANDOCLEVEL_FATAL);
1.71      kristaps  986: }

CVSweb