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

Annotation of mandoc/main.c, Revision 1.9

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

CVSweb