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

Annotation of mandoc/main.c, Revision 1.15

1.15    ! kristaps    1: /* $Id: main.c,v 1.14 2009/03/25 15:17:49 kristaps Exp $ */
1.1       kristaps    2: /*
                      3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the
                      7:  * above copyright notice and this permission notice appear in all
                      8:  * copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
                     11:  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
                     12:  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
                     13:  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
                     14:  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
                     15:  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
                     16:  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
                     17:  * PERFORMANCE OF THIS SOFTWARE.
                     18:  */
                     19: #include <sys/stat.h>
                     20:
                     21: #include <assert.h>
                     22: #include <err.h>
                     23: #include <fcntl.h>
                     24: #include <stdio.h>
                     25: #include <stdlib.h>
                     26: #include <string.h>
                     27: #include <unistd.h>
                     28:
                     29: #include "mdoc.h"
1.10      kristaps   30: #include "man.h"
1.1       kristaps   31:
1.3       kristaps   32: #ifdef __linux__
                     33: extern int               getsubopt(char **, char * const *, char **);
                     34: # ifndef __dead
                     35: #  define __dead __attribute__((__noreturn__))
                     36: # endif
1.13      kristaps   37: #elif defined(__dead2)
1.10      kristaps   38: # ifndef __dead
                     39: #  define __dead __dead2
                     40: # endif
1.3       kristaps   41: #endif
                     42:
1.5       kristaps   43: struct buf {
                     44:        char             *buf;
                     45:        size_t            sz;
                     46: };
                     47:
                     48: struct curparse {
                     49:        const char       *file;
                     50:        int               wflags;
1.1       kristaps   51: #define        WARN_WALL         0x03          /* All-warnings mask. */
                     52: #define        WARN_WCOMPAT     (1 << 0)       /* Compatibility warnings. */
                     53: #define        WARN_WSYNTAX     (1 << 1)       /* Syntax warnings. */
                     54: #define        WARN_WERR        (1 << 2)       /* Warnings->errors. */
1.5       kristaps   55: };
1.1       kristaps   56:
1.15    ! kristaps   57: #define        IGN_SCOPE        (1 << 0) /* Flag to ignore scope. */
        !            58: #define        IGN_ESCAPE       (1 << 1) /* Flag to ignore bad escapes. */
        !            59: #define        IGN_MACRO        (1 << 2) /* Flag to ignore unknown macros. */
        !            60:
1.10      kristaps   61: enum   intt {
                     62:        INTT_MDOC = 0,
                     63:        INTT_MAN
                     64: };
                     65:
                     66: enum   outt {
                     67:        OUTT_ASCII = 0,
1.1       kristaps   68:        OUTT_LATIN1,
                     69:        OUTT_UTF8,
                     70:        OUTT_TREE,
                     71:        OUTT_LINT
                     72: };
                     73:
1.11      kristaps   74: typedef        int             (*out_run)(void *, const struct man *,
                     75:                                const struct mdoc *);
1.1       kristaps   76: typedef        void            (*out_free)(void *);
                     77:
                     78: extern char             *__progname;
                     79:
                     80: extern void             *ascii_alloc(void);
                     81: extern void             *latin1_alloc(void);
                     82: extern void             *utf8_alloc(void);
1.11      kristaps   83: extern int               terminal_run(void *, const struct man *,
                     84:                                const struct mdoc *);
                     85: extern int               tree_run(void *, const struct man *,
                     86:                                const struct mdoc *);
1.1       kristaps   87: extern void              terminal_free(void *);
                     88:
                     89: static int               foptions(int *, char *);
                     90: static int               toptions(enum outt *, char *);
1.10      kristaps   91: static int               moptions(enum intt *, char *);
1.1       kristaps   92: static int               woptions(int *, char *);
                     93: static int               merr(void *, int, int, const char *);
1.14      kristaps   94: static int               manwarn(void *, int, int, const char *);
                     95: static int               mdocwarn(void *, int, int,
1.1       kristaps   96:                                enum mdoc_warn, const char *);
1.10      kristaps   97: static int               file(struct buf *, struct buf *,
                     98:                                const char *,
                     99:                                struct man *, struct mdoc *);
1.5       kristaps  100: static int               fdesc(struct buf *, struct buf *,
1.10      kristaps  101:                                const char *, int,
                    102:                                struct man *, struct mdoc *);
                    103:
                    104: __dead static void       version(void);
                    105: __dead static void       usage(void);
1.1       kristaps  106:
                    107:
                    108: int
                    109: main(int argc, char *argv[])
                    110: {
1.15    ! kristaps  111:        int              c, rc, fflags, pflags;
1.14      kristaps  112:        struct mdoc_cb   mdoccb;
                    113:        struct man_cb    mancb;
1.10      kristaps  114:        struct man      *man;
1.1       kristaps  115:        struct mdoc     *mdoc;
                    116:        void            *outdata;
                    117:        enum outt        outtype;
1.10      kristaps  118:        enum intt        inttype;
1.5       kristaps  119:        struct buf       ln, blk;
1.1       kristaps  120:        out_run          outrun;
                    121:        out_free         outfree;
1.5       kristaps  122:        struct curparse  curp;
1.1       kristaps  123:
1.7       kristaps  124:        fflags = 0;
1.1       kristaps  125:        outtype = OUTT_ASCII;
1.10      kristaps  126:        inttype = INTT_MDOC;
1.1       kristaps  127:
1.5       kristaps  128:        bzero(&curp, sizeof(struct curparse));
                    129:
1.1       kristaps  130:        /* LINTED */
1.10      kristaps  131:        while (-1 != (c = getopt(argc, argv, "f:m:VW:T:")))
1.1       kristaps  132:                switch (c) {
                    133:                case ('f'):
                    134:                        if ( ! foptions(&fflags, optarg))
                    135:                                return(0);
                    136:                        break;
1.10      kristaps  137:                case ('m'):
                    138:                        if ( ! moptions(&inttype, optarg))
                    139:                                return(0);
                    140:                        break;
1.1       kristaps  141:                case ('T'):
                    142:                        if ( ! toptions(&outtype, optarg))
                    143:                                return(0);
                    144:                        break;
                    145:                case ('W'):
1.5       kristaps  146:                        if ( ! woptions(&curp.wflags, optarg))
1.1       kristaps  147:                                return(0);
                    148:                        break;
                    149:                case ('V'):
                    150:                        version();
                    151:                        /* NOTREACHED */
                    152:                default:
                    153:                        usage();
                    154:                        /* NOTREACHED */
                    155:                }
                    156:
                    157:        argc -= optind;
                    158:        argv += optind;
                    159:
                    160:        /*
                    161:         * Allocate the appropriate front-end.  Note that utf8, ascii
                    162:         * and latin1 all resolve to the terminal front-end with
                    163:         * different encodings (see terminal.c).  Not all frontends have
                    164:         * cleanup or alloc routines.
                    165:         */
                    166:
                    167:        switch (outtype) {
                    168:        case (OUTT_LATIN1):
                    169:                outdata = latin1_alloc();
                    170:                outrun = terminal_run;
                    171:                outfree = terminal_free;
                    172:                break;
                    173:        case (OUTT_UTF8):
                    174:                outdata = utf8_alloc();
                    175:                outrun = terminal_run;
                    176:                outfree = terminal_free;
                    177:                break;
                    178:        case (OUTT_TREE):
                    179:                outdata = NULL;
                    180:                outrun = tree_run;
                    181:                outfree = NULL;
                    182:                break;
                    183:        case (OUTT_LINT):
                    184:                outdata = NULL;
                    185:                outrun = NULL;
                    186:                outfree = NULL;
                    187:                break;
                    188:        default:
                    189:                outdata = ascii_alloc();
                    190:                outrun = terminal_run;
                    191:                outfree = terminal_free;
                    192:                break;
                    193:        }
                    194:
                    195:        /*
                    196:         * All callbacks route into here, where we print them onto the
                    197:         * screen.  XXX - for now, no path for debugging messages.
                    198:         */
                    199:
1.14      kristaps  200:        mdoccb.mdoc_msg = NULL;
                    201:        mdoccb.mdoc_err = merr;
                    202:        mdoccb.mdoc_warn = mdocwarn;
                    203:
                    204:        mancb.man_err = merr;
                    205:        mancb.man_warn = manwarn;
1.1       kristaps  206:
1.5       kristaps  207:        bzero(&ln, sizeof(struct buf));
                    208:        bzero(&blk, sizeof(struct buf));
1.1       kristaps  209:
1.10      kristaps  210:        man = NULL;
                    211:        mdoc = NULL;
1.15    ! kristaps  212:        pflags = 0;
1.10      kristaps  213:
                    214:        switch (inttype) {
                    215:        case (INTT_MAN):
1.15    ! kristaps  216:                if (fflags & IGN_MACRO)
        !           217:                        pflags |= MAN_IGN_MACRO;
        !           218:
        !           219:                man = man_alloc(&curp, pflags, &mancb);
1.10      kristaps  220:                break;
                    221:        default:
1.15    ! kristaps  222:                if (fflags & IGN_SCOPE)
        !           223:                        pflags |= MDOC_IGN_SCOPE;
        !           224:                if (fflags & IGN_ESCAPE)
        !           225:                        pflags |= MDOC_IGN_ESCAPE;
        !           226:                if (fflags & IGN_MACRO)
        !           227:                        pflags |= MDOC_IGN_MACRO;
        !           228:
        !           229:                mdoc = mdoc_alloc(&curp, pflags, &mdoccb);
1.10      kristaps  230:                break;
                    231:        }
1.1       kristaps  232:
1.4       kristaps  233:        /*
                    234:         * Loop around available files.
                    235:         */
1.1       kristaps  236:
1.4       kristaps  237:        if (NULL == *argv) {
1.5       kristaps  238:                curp.file = "<stdin>";
1.4       kristaps  239:                rc = 0;
1.10      kristaps  240:                c = fdesc(&blk, &ln, "stdin",
                    241:                                STDIN_FILENO, man, mdoc);
                    242:
1.4       kristaps  243:                if (c && NULL == outrun)
                    244:                        rc = 1;
1.11      kristaps  245:                else if (c && outrun && (*outrun)(outdata, man, mdoc))
1.4       kristaps  246:                        rc = 1;
                    247:        } else {
                    248:                while (*argv) {
1.5       kristaps  249:                        curp.file = *argv;
1.10      kristaps  250:                        c = file(&blk, &ln, *argv, man, mdoc);
1.4       kristaps  251:                        if ( ! c)
                    252:                                break;
1.11      kristaps  253:                        if (outrun && ! (*outrun)(outdata, man, mdoc))
1.4       kristaps  254:                                break;
1.10      kristaps  255:                        if (man)
                    256:                                man_reset(man);
                    257:                        if (mdoc)
                    258:                                mdoc_reset(mdoc);
                    259:
1.4       kristaps  260:                        argv++;
                    261:                }
                    262:                rc = NULL == *argv;
1.1       kristaps  263:        }
                    264:
1.5       kristaps  265:        if (blk.buf)
                    266:                free(blk.buf);
                    267:        if (ln.buf)
                    268:                free(ln.buf);
1.1       kristaps  269:        if (outfree)
                    270:                (*outfree)(outdata);
1.10      kristaps  271:        if (mdoc)
                    272:                mdoc_free(mdoc);
                    273:        if (man)
                    274:                man_free(man);
1.1       kristaps  275:
                    276:        return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
                    277: }
                    278:
                    279:
                    280: __dead static void
                    281: version(void)
                    282: {
                    283:
                    284:        (void)printf("%s %s\n", __progname, VERSION);
                    285:        exit(0);
                    286:        /* NOTREACHED */
                    287: }
                    288:
                    289:
                    290: __dead static void
                    291: usage(void)
                    292: {
                    293:
1.12      kristaps  294:        (void)fprintf(stderr, "usage: %s [-V] [-foption...] "
                    295:                        "[-mformat] [-Toutput] [-Werr...]\n",
                    296:                        __progname);
1.1       kristaps  297:        exit(1);
                    298:        /* NOTREACHED */
                    299: }
                    300:
                    301:
                    302: static int
1.10      kristaps  303: file(struct buf *blk, struct buf *ln, const char *file,
                    304:                struct man *man, struct mdoc *mdoc)
1.1       kristaps  305: {
                    306:        int              fd, c;
                    307:
                    308:        if (-1 == (fd = open(file, O_RDONLY, 0))) {
                    309:                warn("%s", file);
                    310:                return(0);
                    311:        }
                    312:
1.10      kristaps  313:        c = fdesc(blk, ln, file, fd, man, mdoc);
1.1       kristaps  314:
                    315:        if (-1 == close(fd))
                    316:                warn("%s", file);
                    317:
                    318:        return(c);
                    319: }
                    320:
                    321:
                    322: static int
1.5       kristaps  323: fdesc(struct buf *blk, struct buf *ln,
1.10      kristaps  324:                const char *f, int fd,
                    325:                struct man *man, struct mdoc *mdoc)
1.1       kristaps  326: {
                    327:        size_t           sz;
                    328:        ssize_t          ssz;
                    329:        struct stat      st;
                    330:        int              j, i, pos, lnn;
1.10      kristaps  331:
                    332:        assert( ! (man && mdoc));
1.1       kristaps  333:
                    334:        /*
                    335:         * Two buffers: ln and buf.  buf is the input buffer, optimised
                    336:         * for each file's block size.  ln is a line buffer.  Both
                    337:         * growable, hence passed in by ptr-ptr.
                    338:         */
                    339:
1.6       kristaps  340:        sz = BUFSIZ;
                    341:
                    342:        if (-1 == fstat(fd, &st))
1.1       kristaps  343:                warnx("%s", f);
1.6       kristaps  344:        else if ((size_t)st.st_blksize > sz)
                    345:                sz = st.st_blksize;
1.1       kristaps  346:
1.5       kristaps  347:        if (sz > blk->sz) {
                    348:                blk->buf = realloc(blk->buf, sz);
                    349:                if (NULL == blk->buf)
1.1       kristaps  350:                        err(1, "realloc");
1.5       kristaps  351:                blk->sz = sz;
1.1       kristaps  352:        }
                    353:
                    354:        /*
                    355:         * Fill buf with file blocksize and parse newlines into ln.
                    356:         */
                    357:
                    358:        for (lnn = 1, pos = 0; ; ) {
1.5       kristaps  359:                if (-1 == (ssz = read(fd, blk->buf, sz))) {
1.1       kristaps  360:                        warn("%s", f);
                    361:                        return(0);
                    362:                } else if (0 == ssz)
                    363:                        break;
                    364:
                    365:                for (i = 0; i < (int)ssz; i++) {
1.5       kristaps  366:                        if (pos >= (int)ln->sz) {
                    367:                                ln->sz += 256; /* Step-size. */
                    368:                                ln->buf = realloc(ln->buf, ln->sz);
                    369:                                if (NULL == ln->buf)
1.1       kristaps  370:                                        err(1, "realloc");
                    371:                        }
                    372:
1.5       kristaps  373:                        if ('\n' != blk->buf[i]) {
                    374:                                ln->buf[pos++] = blk->buf[i];
1.1       kristaps  375:                                continue;
                    376:                        }
                    377:
                    378:                        /* Check for CPP-escaped newline.  */
                    379:
1.5       kristaps  380:                        if (pos > 0 && '\\' == ln->buf[pos - 1]) {
1.1       kristaps  381:                                for (j = pos - 1; j >= 0; j--)
1.5       kristaps  382:                                        if ('\\' != ln->buf[j])
1.1       kristaps  383:                                                break;
                    384:
                    385:                                if ( ! ((pos - j) % 2)) {
                    386:                                        pos--;
                    387:                                        lnn++;
                    388:                                        continue;
                    389:                                }
                    390:                        }
                    391:
1.5       kristaps  392:                        ln->buf[pos] = 0;
1.10      kristaps  393:                        if (mdoc && ! mdoc_parseln(mdoc, lnn, ln->buf))
                    394:                                return(0);
                    395:                        if (man && ! man_parseln(man, lnn, ln->buf))
1.1       kristaps  396:                                return(0);
                    397:                        lnn++;
1.9       kristaps  398:                        pos = 0;
1.1       kristaps  399:                }
                    400:        }
                    401:
1.10      kristaps  402:        if (mdoc)
                    403:               return(mdoc_endparse(mdoc));
                    404:
                    405:        return(man_endparse(man));
                    406: }
                    407:
                    408:
                    409: static int
                    410: moptions(enum intt *tflags, char *arg)
                    411: {
                    412:
                    413:        if (0 == strcmp(arg, "mdoc"))
                    414:                *tflags = INTT_MDOC;
                    415:        else if (0 == strcmp(arg, "man"))
                    416:                *tflags = INTT_MAN;
                    417:        else {
                    418:                warnx("bad argument: -m%s", arg);
                    419:                return(0);
                    420:        }
                    421:
                    422:        return(1);
1.1       kristaps  423: }
                    424:
                    425:
                    426: static int
                    427: toptions(enum outt *tflags, char *arg)
                    428: {
                    429:
                    430:        if (0 == strcmp(arg, "ascii"))
                    431:                *tflags = OUTT_ASCII;
                    432:        else if (0 == strcmp(arg, "latin1"))
                    433:                *tflags = OUTT_LATIN1;
                    434:        else if (0 == strcmp(arg, "utf8"))
                    435:                *tflags = OUTT_UTF8;
                    436:        else if (0 == strcmp(arg, "lint"))
                    437:                *tflags = OUTT_LINT;
                    438:        else if (0 == strcmp(arg, "tree"))
                    439:                *tflags = OUTT_TREE;
                    440:        else {
                    441:                warnx("bad argument: -T%s", arg);
                    442:                return(0);
                    443:        }
                    444:
                    445:        return(1);
                    446: }
                    447:
                    448:
                    449: /*
                    450:  * Parse out the options for [-fopt...] setting compiler options.  These
                    451:  * can be comma-delimited or called again.
                    452:  */
                    453: static int
                    454: foptions(int *fflags, char *arg)
                    455: {
                    456:        char            *v;
                    457:        char            *toks[4];
                    458:
                    459:        toks[0] = "ign-scope";
                    460:        toks[1] = "ign-escape";
                    461:        toks[2] = "ign-macro";
                    462:        toks[3] = NULL;
                    463:
                    464:        while (*arg)
                    465:                switch (getsubopt(&arg, toks, &v)) {
                    466:                case (0):
1.15    ! kristaps  467:                        *fflags |= IGN_SCOPE;
1.1       kristaps  468:                        break;
                    469:                case (1):
1.15    ! kristaps  470:                        *fflags |= IGN_ESCAPE;
1.1       kristaps  471:                        break;
                    472:                case (2):
1.15    ! kristaps  473:                        *fflags |= IGN_MACRO;
1.1       kristaps  474:                        break;
                    475:                default:
                    476:                        warnx("bad argument: -f%s", arg);
                    477:                        return(0);
                    478:                }
                    479:
                    480:        return(1);
                    481: }
                    482:
                    483:
                    484: /*
                    485:  * Parse out the options for [-Werr...], which sets warning modes.
                    486:  * These can be comma-delimited or called again.
                    487:  */
                    488: static int
                    489: woptions(int *wflags, char *arg)
                    490: {
                    491:        char            *v;
                    492:        char            *toks[5];
                    493:
                    494:        toks[0] = "all";
                    495:        toks[1] = "compat";
                    496:        toks[2] = "syntax";
                    497:        toks[3] = "error";
                    498:        toks[4] = NULL;
                    499:
                    500:        while (*arg)
                    501:                switch (getsubopt(&arg, toks, &v)) {
                    502:                case (0):
                    503:                        *wflags |= WARN_WALL;
                    504:                        break;
                    505:                case (1):
                    506:                        *wflags |= WARN_WCOMPAT;
                    507:                        break;
                    508:                case (2):
                    509:                        *wflags |= WARN_WSYNTAX;
                    510:                        break;
                    511:                case (3):
                    512:                        *wflags |= WARN_WERR;
                    513:                        break;
                    514:                default:
                    515:                        warnx("bad argument: -W%s", arg);
                    516:                        return(0);
                    517:                }
                    518:
                    519:        return(1);
                    520: }
                    521:
                    522:
1.2       kristaps  523: /* ARGSUSED */
1.1       kristaps  524: static int
                    525: merr(void *arg, int line, int col, const char *msg)
                    526: {
1.5       kristaps  527:        struct curparse *curp;
                    528:
                    529:        curp = (struct curparse *)arg;
1.1       kristaps  530:
1.5       kristaps  531:        warnx("%s:%d: error: %s (column %d)",
                    532:                        curp->file, line, msg, col);
1.1       kristaps  533:        return(0);
                    534: }
                    535:
                    536:
                    537: static int
1.14      kristaps  538: mdocwarn(void *arg, int line, int col,
1.1       kristaps  539:                enum mdoc_warn type, const char *msg)
                    540: {
1.5       kristaps  541:        struct curparse *curp;
1.1       kristaps  542:        char            *wtype;
                    543:
1.5       kristaps  544:        curp = (struct curparse *)arg;
1.1       kristaps  545:        wtype = NULL;
                    546:
                    547:        switch (type) {
                    548:        case (WARN_COMPAT):
                    549:                wtype = "compat";
1.5       kristaps  550:                if (curp->wflags & WARN_WCOMPAT)
1.1       kristaps  551:                        break;
                    552:                return(1);
                    553:        case (WARN_SYNTAX):
                    554:                wtype = "syntax";
1.5       kristaps  555:                if (curp->wflags & WARN_WSYNTAX)
1.1       kristaps  556:                        break;
                    557:                return(1);
                    558:        }
                    559:
                    560:        assert(wtype);
1.5       kristaps  561:        warnx("%s:%d: %s warning: %s (column %d)",
                    562:                        curp->file, line, wtype, msg, col);
1.1       kristaps  563:
1.5       kristaps  564:        if ( ! (curp->wflags & WARN_WERR))
1.1       kristaps  565:                return(1);
                    566:
                    567:        warnx("%s: considering warnings as errors",
                    568:                        __progname);
                    569:        return(0);
                    570: }
                    571:
                    572:
1.14      kristaps  573: static int
                    574: manwarn(void *arg, int line, int col, const char *msg)
                    575: {
                    576:        struct curparse *curp;
                    577:
                    578:        curp = (struct curparse *)arg;
                    579:
                    580:        if ( ! (curp->wflags & WARN_WSYNTAX))
                    581:                return(1);
                    582:
                    583:        warnx("%s:%d: syntax warning: %s (column %d)",
                    584:                        curp->file, line, msg, col);
                    585:
                    586:        if ( ! (curp->wflags & WARN_WERR))
                    587:                return(1);
                    588:
                    589:        warnx("%s: considering warnings as errors",
                    590:                        __progname);
                    591:        return(0);
                    592: }

CVSweb