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

Annotation of mandoc/main.c, Revision 1.11

1.11    ! kristaps    1: /* $Id: main.c,v 1.10 2009/03/23 14:22:11 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.10      kristaps   37: #elif defined(__FreeBSD__)
                     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.10      kristaps   57: enum   intt {
                     58:        INTT_MDOC = 0,
                     59:        INTT_MAN
                     60: };
                     61:
                     62: enum   outt {
                     63:        OUTT_ASCII = 0,
1.1       kristaps   64:        OUTT_LATIN1,
                     65:        OUTT_UTF8,
                     66:        OUTT_TREE,
                     67:        OUTT_LINT
                     68: };
                     69:
1.11    ! kristaps   70: typedef        int             (*out_run)(void *, const struct man *,
        !            71:                                const struct mdoc *);
1.1       kristaps   72: typedef        void            (*out_free)(void *);
                     73:
                     74: extern char             *__progname;
                     75:
                     76: extern void             *ascii_alloc(void);
                     77: extern void             *latin1_alloc(void);
                     78: extern void             *utf8_alloc(void);
1.11    ! kristaps   79: extern int               terminal_run(void *, const struct man *,
        !            80:                                const struct mdoc *);
        !            81: extern int               tree_run(void *, const struct man *,
        !            82:                                const struct mdoc *);
1.1       kristaps   83: extern void              terminal_free(void *);
                     84:
                     85: static int               foptions(int *, char *);
                     86: static int               toptions(enum outt *, char *);
1.10      kristaps   87: static int               moptions(enum intt *, char *);
1.1       kristaps   88: static int               woptions(int *, char *);
                     89: static int               merr(void *, int, int, const char *);
                     90: static int               mwarn(void *, int, int,
                     91:                                enum mdoc_warn, const char *);
1.10      kristaps   92: static int               file(struct buf *, struct buf *,
                     93:                                const char *,
                     94:                                struct man *, struct mdoc *);
1.5       kristaps   95: static int               fdesc(struct buf *, struct buf *,
1.10      kristaps   96:                                const char *, int,
                     97:                                struct man *, struct mdoc *);
                     98:
                     99: __dead static void       version(void);
                    100: __dead static void       usage(void);
1.1       kristaps  101:
                    102:
                    103: int
                    104: main(int argc, char *argv[])
                    105: {
1.7       kristaps  106:        int              c, rc, fflags;
1.1       kristaps  107:        struct mdoc_cb   cb;
1.10      kristaps  108:        struct man      *man;
1.1       kristaps  109:        struct mdoc     *mdoc;
                    110:        void            *outdata;
                    111:        enum outt        outtype;
1.10      kristaps  112:        enum intt        inttype;
1.5       kristaps  113:        struct buf       ln, blk;
1.1       kristaps  114:        out_run          outrun;
                    115:        out_free         outfree;
1.5       kristaps  116:        struct curparse  curp;
1.1       kristaps  117:
1.7       kristaps  118:        fflags = 0;
1.1       kristaps  119:        outtype = OUTT_ASCII;
1.10      kristaps  120:        inttype = INTT_MDOC;
1.1       kristaps  121:
1.5       kristaps  122:        bzero(&curp, sizeof(struct curparse));
                    123:
1.1       kristaps  124:        /* LINTED */
1.10      kristaps  125:        while (-1 != (c = getopt(argc, argv, "f:m:VW:T:")))
1.1       kristaps  126:                switch (c) {
                    127:                case ('f'):
                    128:                        if ( ! foptions(&fflags, optarg))
                    129:                                return(0);
                    130:                        break;
1.10      kristaps  131:                case ('m'):
                    132:                        if ( ! moptions(&inttype, optarg))
                    133:                                return(0);
                    134:                        break;
1.1       kristaps  135:                case ('T'):
                    136:                        if ( ! toptions(&outtype, optarg))
                    137:                                return(0);
                    138:                        break;
                    139:                case ('W'):
1.5       kristaps  140:                        if ( ! woptions(&curp.wflags, optarg))
1.1       kristaps  141:                                return(0);
                    142:                        break;
                    143:                case ('V'):
                    144:                        version();
                    145:                        /* NOTREACHED */
                    146:                default:
                    147:                        usage();
                    148:                        /* NOTREACHED */
                    149:                }
                    150:
                    151:        argc -= optind;
                    152:        argv += optind;
                    153:
                    154:        /*
                    155:         * Allocate the appropriate front-end.  Note that utf8, ascii
                    156:         * and latin1 all resolve to the terminal front-end with
                    157:         * different encodings (see terminal.c).  Not all frontends have
                    158:         * cleanup or alloc routines.
                    159:         */
                    160:
                    161:        switch (outtype) {
                    162:        case (OUTT_LATIN1):
                    163:                outdata = latin1_alloc();
                    164:                outrun = terminal_run;
                    165:                outfree = terminal_free;
                    166:                break;
                    167:        case (OUTT_UTF8):
                    168:                outdata = utf8_alloc();
                    169:                outrun = terminal_run;
                    170:                outfree = terminal_free;
                    171:                break;
                    172:        case (OUTT_TREE):
                    173:                outdata = NULL;
                    174:                outrun = tree_run;
                    175:                outfree = NULL;
                    176:                break;
                    177:        case (OUTT_LINT):
                    178:                outdata = NULL;
                    179:                outrun = NULL;
                    180:                outfree = NULL;
                    181:                break;
                    182:        default:
                    183:                outdata = ascii_alloc();
                    184:                outrun = terminal_run;
                    185:                outfree = terminal_free;
                    186:                break;
                    187:        }
                    188:
                    189:        /*
                    190:         * All callbacks route into here, where we print them onto the
                    191:         * screen.  XXX - for now, no path for debugging messages.
                    192:         */
                    193:
                    194:        cb.mdoc_msg = NULL;
                    195:        cb.mdoc_err = merr;
                    196:        cb.mdoc_warn = mwarn;
                    197:
1.5       kristaps  198:        bzero(&ln, sizeof(struct buf));
                    199:        bzero(&blk, sizeof(struct buf));
1.1       kristaps  200:
1.10      kristaps  201:        man = NULL;
                    202:        mdoc = NULL;
                    203:
                    204:        switch (inttype) {
                    205:        case (INTT_MAN):
                    206:                man = man_alloc();
                    207:                break;
                    208:        default:
                    209:                mdoc = mdoc_alloc(&curp, fflags, &cb);
                    210:                break;
                    211:        }
1.1       kristaps  212:
1.4       kristaps  213:        /*
                    214:         * Loop around available files.
                    215:         */
1.1       kristaps  216:
1.4       kristaps  217:        if (NULL == *argv) {
1.5       kristaps  218:                curp.file = "<stdin>";
1.4       kristaps  219:                rc = 0;
1.10      kristaps  220:                c = fdesc(&blk, &ln, "stdin",
                    221:                                STDIN_FILENO, man, mdoc);
                    222:
1.4       kristaps  223:                if (c && NULL == outrun)
                    224:                        rc = 1;
1.11    ! kristaps  225:                else if (c && outrun && (*outrun)(outdata, man, mdoc))
1.4       kristaps  226:                        rc = 1;
                    227:        } else {
                    228:                while (*argv) {
1.5       kristaps  229:                        curp.file = *argv;
1.10      kristaps  230:                        c = file(&blk, &ln, *argv, man, mdoc);
1.4       kristaps  231:                        if ( ! c)
                    232:                                break;
1.11    ! kristaps  233:                        if (outrun && ! (*outrun)(outdata, man, mdoc))
1.4       kristaps  234:                                break;
1.10      kristaps  235:                        if (man)
                    236:                                man_reset(man);
                    237:                        if (mdoc)
                    238:                                mdoc_reset(mdoc);
                    239:
1.4       kristaps  240:                        argv++;
                    241:                }
                    242:                rc = NULL == *argv;
1.1       kristaps  243:        }
                    244:
1.5       kristaps  245:        if (blk.buf)
                    246:                free(blk.buf);
                    247:        if (ln.buf)
                    248:                free(ln.buf);
1.1       kristaps  249:        if (outfree)
                    250:                (*outfree)(outdata);
1.10      kristaps  251:        if (mdoc)
                    252:                mdoc_free(mdoc);
                    253:        if (man)
                    254:                man_free(man);
1.1       kristaps  255:
                    256:        return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
                    257: }
                    258:
                    259:
                    260: __dead static void
                    261: version(void)
                    262: {
                    263:
                    264:        (void)printf("%s %s\n", __progname, VERSION);
                    265:        exit(0);
                    266:        /* NOTREACHED */
                    267: }
                    268:
                    269:
                    270: __dead static void
                    271: usage(void)
                    272: {
                    273:
                    274:        (void)fprintf(stderr, "usage: %s\n", __progname);
                    275:        exit(1);
                    276:        /* NOTREACHED */
                    277: }
                    278:
                    279:
                    280: static int
1.10      kristaps  281: file(struct buf *blk, struct buf *ln, const char *file,
                    282:                struct man *man, struct mdoc *mdoc)
1.1       kristaps  283: {
                    284:        int              fd, c;
                    285:
                    286:        if (-1 == (fd = open(file, O_RDONLY, 0))) {
                    287:                warn("%s", file);
                    288:                return(0);
                    289:        }
                    290:
1.10      kristaps  291:        c = fdesc(blk, ln, file, fd, man, mdoc);
1.1       kristaps  292:
                    293:        if (-1 == close(fd))
                    294:                warn("%s", file);
                    295:
                    296:        return(c);
                    297: }
                    298:
                    299:
                    300: static int
1.5       kristaps  301: fdesc(struct buf *blk, struct buf *ln,
1.10      kristaps  302:                const char *f, int fd,
                    303:                struct man *man, struct mdoc *mdoc)
1.1       kristaps  304: {
                    305:        size_t           sz;
                    306:        ssize_t          ssz;
                    307:        struct stat      st;
                    308:        int              j, i, pos, lnn;
1.10      kristaps  309:
                    310:        assert( ! (man && mdoc));
1.1       kristaps  311:
                    312:        /*
                    313:         * Two buffers: ln and buf.  buf is the input buffer, optimised
                    314:         * for each file's block size.  ln is a line buffer.  Both
                    315:         * growable, hence passed in by ptr-ptr.
                    316:         */
                    317:
1.6       kristaps  318:        sz = BUFSIZ;
                    319:
                    320:        if (-1 == fstat(fd, &st))
1.1       kristaps  321:                warnx("%s", f);
1.6       kristaps  322:        else if ((size_t)st.st_blksize > sz)
                    323:                sz = st.st_blksize;
1.1       kristaps  324:
1.5       kristaps  325:        if (sz > blk->sz) {
                    326:                blk->buf = realloc(blk->buf, sz);
                    327:                if (NULL == blk->buf)
1.1       kristaps  328:                        err(1, "realloc");
1.5       kristaps  329:                blk->sz = sz;
1.1       kristaps  330:        }
                    331:
                    332:        /*
                    333:         * Fill buf with file blocksize and parse newlines into ln.
                    334:         */
                    335:
                    336:        for (lnn = 1, pos = 0; ; ) {
1.5       kristaps  337:                if (-1 == (ssz = read(fd, blk->buf, sz))) {
1.1       kristaps  338:                        warn("%s", f);
                    339:                        return(0);
                    340:                } else if (0 == ssz)
                    341:                        break;
                    342:
                    343:                for (i = 0; i < (int)ssz; i++) {
1.5       kristaps  344:                        if (pos >= (int)ln->sz) {
                    345:                                ln->sz += 256; /* Step-size. */
                    346:                                ln->buf = realloc(ln->buf, ln->sz);
                    347:                                if (NULL == ln->buf)
1.1       kristaps  348:                                        err(1, "realloc");
                    349:                        }
                    350:
1.5       kristaps  351:                        if ('\n' != blk->buf[i]) {
                    352:                                ln->buf[pos++] = blk->buf[i];
1.1       kristaps  353:                                continue;
                    354:                        }
                    355:
                    356:                        /* Check for CPP-escaped newline.  */
                    357:
1.5       kristaps  358:                        if (pos > 0 && '\\' == ln->buf[pos - 1]) {
1.1       kristaps  359:                                for (j = pos - 1; j >= 0; j--)
1.5       kristaps  360:                                        if ('\\' != ln->buf[j])
1.1       kristaps  361:                                                break;
                    362:
                    363:                                if ( ! ((pos - j) % 2)) {
                    364:                                        pos--;
                    365:                                        lnn++;
                    366:                                        continue;
                    367:                                }
                    368:                        }
                    369:
1.5       kristaps  370:                        ln->buf[pos] = 0;
1.10      kristaps  371:                        if (mdoc && ! mdoc_parseln(mdoc, lnn, ln->buf))
                    372:                                return(0);
                    373:                        if (man && ! man_parseln(man, lnn, ln->buf))
1.1       kristaps  374:                                return(0);
                    375:                        lnn++;
1.9       kristaps  376:                        pos = 0;
1.1       kristaps  377:                }
                    378:        }
                    379:
1.10      kristaps  380:        if (mdoc)
                    381:               return(mdoc_endparse(mdoc));
                    382:
                    383:        return(man_endparse(man));
                    384: }
                    385:
                    386:
                    387: static int
                    388: moptions(enum intt *tflags, char *arg)
                    389: {
                    390:
                    391:        if (0 == strcmp(arg, "mdoc"))
                    392:                *tflags = INTT_MDOC;
                    393:        else if (0 == strcmp(arg, "man"))
                    394:                *tflags = INTT_MAN;
                    395:        else {
                    396:                warnx("bad argument: -m%s", arg);
                    397:                return(0);
                    398:        }
                    399:
                    400:        return(1);
1.1       kristaps  401: }
                    402:
                    403:
                    404: static int
                    405: toptions(enum outt *tflags, char *arg)
                    406: {
                    407:
                    408:        if (0 == strcmp(arg, "ascii"))
                    409:                *tflags = OUTT_ASCII;
                    410:        else if (0 == strcmp(arg, "latin1"))
                    411:                *tflags = OUTT_LATIN1;
                    412:        else if (0 == strcmp(arg, "utf8"))
                    413:                *tflags = OUTT_UTF8;
                    414:        else if (0 == strcmp(arg, "lint"))
                    415:                *tflags = OUTT_LINT;
                    416:        else if (0 == strcmp(arg, "tree"))
                    417:                *tflags = OUTT_TREE;
                    418:        else {
                    419:                warnx("bad argument: -T%s", arg);
                    420:                return(0);
                    421:        }
                    422:
                    423:        return(1);
                    424: }
                    425:
                    426:
                    427: /*
                    428:  * Parse out the options for [-fopt...] setting compiler options.  These
                    429:  * can be comma-delimited or called again.
                    430:  */
                    431: static int
                    432: foptions(int *fflags, char *arg)
                    433: {
                    434:        char            *v;
                    435:        char            *toks[4];
                    436:
                    437:        toks[0] = "ign-scope";
                    438:        toks[1] = "ign-escape";
                    439:        toks[2] = "ign-macro";
                    440:        toks[3] = NULL;
                    441:
                    442:        while (*arg)
                    443:                switch (getsubopt(&arg, toks, &v)) {
                    444:                case (0):
                    445:                        *fflags |= MDOC_IGN_SCOPE;
                    446:                        break;
                    447:                case (1):
                    448:                        *fflags |= MDOC_IGN_ESCAPE;
                    449:                        break;
                    450:                case (2):
                    451:                        *fflags |= MDOC_IGN_MACRO;
                    452:                        break;
                    453:                default:
                    454:                        warnx("bad argument: -f%s", arg);
                    455:                        return(0);
                    456:                }
                    457:
                    458:        return(1);
                    459: }
                    460:
                    461:
                    462: /*
                    463:  * Parse out the options for [-Werr...], which sets warning modes.
                    464:  * These can be comma-delimited or called again.
                    465:  */
                    466: static int
                    467: woptions(int *wflags, char *arg)
                    468: {
                    469:        char            *v;
                    470:        char            *toks[5];
                    471:
                    472:        toks[0] = "all";
                    473:        toks[1] = "compat";
                    474:        toks[2] = "syntax";
                    475:        toks[3] = "error";
                    476:        toks[4] = NULL;
                    477:
                    478:        while (*arg)
                    479:                switch (getsubopt(&arg, toks, &v)) {
                    480:                case (0):
                    481:                        *wflags |= WARN_WALL;
                    482:                        break;
                    483:                case (1):
                    484:                        *wflags |= WARN_WCOMPAT;
                    485:                        break;
                    486:                case (2):
                    487:                        *wflags |= WARN_WSYNTAX;
                    488:                        break;
                    489:                case (3):
                    490:                        *wflags |= WARN_WERR;
                    491:                        break;
                    492:                default:
                    493:                        warnx("bad argument: -W%s", arg);
                    494:                        return(0);
                    495:                }
                    496:
                    497:        return(1);
                    498: }
                    499:
                    500:
1.2       kristaps  501: /* ARGSUSED */
1.1       kristaps  502: static int
                    503: merr(void *arg, int line, int col, const char *msg)
                    504: {
1.5       kristaps  505:        struct curparse *curp;
                    506:
                    507:        curp = (struct curparse *)arg;
1.1       kristaps  508:
1.5       kristaps  509:        warnx("%s:%d: error: %s (column %d)",
                    510:                        curp->file, line, msg, col);
1.1       kristaps  511:        return(0);
                    512: }
                    513:
                    514:
                    515: static int
                    516: mwarn(void *arg, int line, int col,
                    517:                enum mdoc_warn type, const char *msg)
                    518: {
1.5       kristaps  519:        struct curparse *curp;
1.1       kristaps  520:        char            *wtype;
                    521:
1.5       kristaps  522:        curp = (struct curparse *)arg;
1.1       kristaps  523:        wtype = NULL;
                    524:
                    525:        switch (type) {
                    526:        case (WARN_COMPAT):
                    527:                wtype = "compat";
1.5       kristaps  528:                if (curp->wflags & WARN_WCOMPAT)
1.1       kristaps  529:                        break;
                    530:                return(1);
                    531:        case (WARN_SYNTAX):
                    532:                wtype = "syntax";
1.5       kristaps  533:                if (curp->wflags & WARN_WSYNTAX)
1.1       kristaps  534:                        break;
                    535:                return(1);
                    536:        }
                    537:
                    538:        assert(wtype);
1.5       kristaps  539:        warnx("%s:%d: %s warning: %s (column %d)",
                    540:                        curp->file, line, wtype, msg, col);
1.1       kristaps  541:
1.5       kristaps  542:        if ( ! (curp->wflags & WARN_WERR))
1.1       kristaps  543:                return(1);
                    544:
                    545:        warnx("%s: considering warnings as errors",
                    546:                        __progname);
                    547:        return(0);
                    548: }
                    549:
                    550:

CVSweb