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

Annotation of mandoc/main.c, Revision 1.29

1.29    ! kristaps    1: /*     $Id: main.c,v 1.28 2009/06/15 10:36:01 kristaps Exp $ */
1.1       kristaps    2: /*
1.26      kristaps    3:  * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@kth.se>
1.1       kristaps    4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
1.25      kristaps    6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps    8:  *
1.25      kristaps    9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   16:  */
                     17: #include <sys/stat.h>
                     18:
                     19: #include <assert.h>
                     20: #include <err.h>
                     21: #include <fcntl.h>
                     22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24: #include <string.h>
                     25: #include <unistd.h>
                     26:
                     27: #include "mdoc.h"
1.10      kristaps   28: #include "man.h"
1.1       kristaps   29:
1.16      kristaps   30: /* Account for FreeBSD and Linux in our declarations. */
                     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.22      kristaps   43: typedef        int             (*out_mdoc)(void *, const struct mdoc *);
                     44: typedef        int             (*out_man)(void *, const struct man *);
                     45: typedef        void            (*out_free)(void *);
                     46:
1.5       kristaps   47: struct buf {
                     48:        char             *buf;
                     49:        size_t            sz;
                     50: };
                     51:
1.19      kristaps   52: enum   intt {
                     53:        INTT_AUTO,
                     54:        INTT_MDOC,
                     55:        INTT_MAN
                     56: };
                     57:
                     58: enum   outt {
                     59:        OUTT_ASCII = 0,
                     60:        OUTT_TREE,
                     61:        OUTT_LINT
                     62: };
                     63:
1.5       kristaps   64: struct curparse {
1.22      kristaps   65:        const char       *file;         /* Current parse. */
                     66:        int               fd;           /* Current parse. */
1.5       kristaps   67:        int               wflags;
1.1       kristaps   68: #define        WARN_WALL         0x03          /* All-warnings mask. */
                     69: #define        WARN_WCOMPAT     (1 << 0)       /* Compatibility warnings. */
                     70: #define        WARN_WSYNTAX     (1 << 1)       /* Syntax warnings. */
                     71: #define        WARN_WERR        (1 << 2)       /* Warnings->errors. */
1.23      kristaps   72:        int               fflags;
                     73: #define        IGN_SCOPE        (1 << 0)       /* Ignore scope errors. */
1.24      kristaps   74: #define        NO_IGN_ESCAPE    (1 << 1)       /* Don't ignore bad escapes. */
                     75: #define        NO_IGN_MACRO     (1 << 2)       /* Don't ignore bad macros. */
                     76: #define        NO_IGN_CHARS     (1 << 3)       /* Don't ignore bad chars. */
1.29    ! kristaps   77:        enum intt         inttype;      /* Input parsers... */
1.19      kristaps   78:        struct man       *man;
1.22      kristaps   79:        struct man       *lastman;
1.19      kristaps   80:        struct mdoc      *mdoc;
1.22      kristaps   81:        struct mdoc      *lastmdoc;
1.29    ! kristaps   82:        enum outt         outtype;      /* Output devices... */
1.22      kristaps   83:        out_mdoc          outmdoc;
                     84:        out_man           outman;
                     85:        out_free          outfree;
                     86:        void             *outdata;
1.5       kristaps   87: };
1.1       kristaps   88:
                     89: extern void             *ascii_alloc(void);
1.22      kristaps   90: extern int               tree_mdoc(void *, const struct mdoc *);
                     91: extern int               tree_man(void *, const struct man *);
                     92: extern int               terminal_mdoc(void *, const struct mdoc *);
                     93: extern int               terminal_man(void *, const struct man *);
1.1       kristaps   94: extern void              terminal_free(void *);
                     95:
                     96: static int               foptions(int *, char *);
                     97: static int               toptions(enum outt *, char *);
1.10      kristaps   98: static int               moptions(enum intt *, char *);
1.1       kristaps   99: static int               woptions(int *, char *);
                    100: static int               merr(void *, int, int, const char *);
1.14      kristaps  101: static int               manwarn(void *, int, int, const char *);
                    102: static int               mdocwarn(void *, int, int,
1.1       kristaps  103:                                enum mdoc_warn, const char *);
1.19      kristaps  104: static int               fstdin(struct buf *, struct buf *,
                    105:                                struct curparse *);
                    106: static int               ffile(struct buf *, struct buf *,
                    107:                                const char *, struct curparse *);
1.5       kristaps  108: static int               fdesc(struct buf *, struct buf *,
1.19      kristaps  109:                                struct curparse *);
1.22      kristaps  110: static int               pset(const char *, int, struct curparse *,
1.19      kristaps  111:                                struct man **, struct mdoc **);
                    112: static struct man       *man_init(struct curparse *);
                    113: static struct mdoc      *mdoc_init(struct curparse *);
1.10      kristaps  114: __dead static void       version(void);
                    115: __dead static void       usage(void);
1.1       kristaps  116:
1.22      kristaps  117: extern char             *__progname;
                    118:
1.1       kristaps  119:
                    120: int
                    121: main(int argc, char *argv[])
                    122: {
1.19      kristaps  123:        int              c, rc;
1.5       kristaps  124:        struct buf       ln, blk;
                    125:        struct curparse  curp;
1.1       kristaps  126:
1.5       kristaps  127:        bzero(&curp, sizeof(struct curparse));
                    128:
1.19      kristaps  129:        curp.inttype = INTT_AUTO;
1.22      kristaps  130:        curp.outtype = OUTT_ASCII;
1.19      kristaps  131:
1.1       kristaps  132:        /* LINTED */
1.10      kristaps  133:        while (-1 != (c = getopt(argc, argv, "f:m:VW:T:")))
1.1       kristaps  134:                switch (c) {
                    135:                case ('f'):
1.19      kristaps  136:                        if ( ! foptions(&curp.fflags, optarg))
1.1       kristaps  137:                                return(0);
                    138:                        break;
1.10      kristaps  139:                case ('m'):
1.19      kristaps  140:                        if ( ! moptions(&curp.inttype, optarg))
1.10      kristaps  141:                                return(0);
                    142:                        break;
1.1       kristaps  143:                case ('T'):
1.22      kristaps  144:                        if ( ! toptions(&curp.outtype, optarg))
1.1       kristaps  145:                                return(0);
                    146:                        break;
                    147:                case ('W'):
1.5       kristaps  148:                        if ( ! woptions(&curp.wflags, optarg))
1.1       kristaps  149:                                return(0);
                    150:                        break;
                    151:                case ('V'):
                    152:                        version();
                    153:                        /* NOTREACHED */
                    154:                default:
                    155:                        usage();
                    156:                        /* NOTREACHED */
                    157:                }
                    158:
                    159:        argc -= optind;
                    160:        argv += optind;
                    161:
1.16      kristaps  162:        /* Configure buffers. */
                    163:
1.5       kristaps  164:        bzero(&ln, sizeof(struct buf));
                    165:        bzero(&blk, sizeof(struct buf));
1.1       kristaps  166:
1.22      kristaps  167:        rc = 1;
1.1       kristaps  168:
1.22      kristaps  169:        if (NULL == *argv)
                    170:                if ( ! fstdin(&blk, &ln, &curp))
                    171:                        rc = 0;
                    172:
                    173:        while (rc && *argv) {
                    174:                if ( ! ffile(&blk, &ln, *argv, &curp))
                    175:                        rc = 0;
                    176:                argv++;
                    177:                if (*argv && rc) {
                    178:                        if (curp.lastman)
                    179:                                if ( ! man_reset(curp.lastman))
                    180:                                        rc = 0;
                    181:                        if (curp.lastmdoc)
                    182:                                if ( ! mdoc_reset(curp.lastmdoc))
                    183:                                        rc = 0;
                    184:                        curp.lastman = NULL;
                    185:                        curp.lastmdoc = NULL;
1.4       kristaps  186:                }
1.1       kristaps  187:        }
                    188:
1.5       kristaps  189:        if (blk.buf)
                    190:                free(blk.buf);
                    191:        if (ln.buf)
                    192:                free(ln.buf);
1.27      kristaps  193:
                    194:        /* TODO: have a curp_free routine. */
1.22      kristaps  195:        if (curp.outfree)
                    196:                (*curp.outfree)(curp.outdata);
1.19      kristaps  197:        if (curp.mdoc)
                    198:                mdoc_free(curp.mdoc);
                    199:        if (curp.man)
                    200:                man_free(curp.man);
1.1       kristaps  201:
                    202:        return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
                    203: }
                    204:
                    205:
                    206: __dead static void
                    207: version(void)
                    208: {
                    209:
                    210:        (void)printf("%s %s\n", __progname, VERSION);
1.18      kristaps  211:        exit(EXIT_SUCCESS);
1.1       kristaps  212: }
                    213:
                    214:
                    215: __dead static void
                    216: usage(void)
                    217: {
                    218:
1.12      kristaps  219:        (void)fprintf(stderr, "usage: %s [-V] [-foption...] "
                    220:                        "[-mformat] [-Toutput] [-Werr...]\n",
                    221:                        __progname);
1.18      kristaps  222:        exit(EXIT_FAILURE);
1.1       kristaps  223: }
                    224:
                    225:
1.19      kristaps  226: static struct man *
                    227: man_init(struct curparse *curp)
                    228: {
                    229:        int              pflags;
                    230:        struct man      *man;
                    231:        struct man_cb    mancb;
                    232:
                    233:        mancb.man_err = merr;
                    234:        mancb.man_warn = manwarn;
                    235:
1.27      kristaps  236:        /*
                    237:         * Default behaviour is to ignore unknown macros.  This is
                    238:         * specified in mandoc.1.
                    239:         */
                    240:
                    241:        pflags = MAN_IGN_MACRO;
                    242:
                    243:        /* Override default behaviour... */
1.19      kristaps  244:
1.20      kristaps  245:        if (curp->fflags & NO_IGN_MACRO)
                    246:                pflags &= ~MAN_IGN_MACRO;
1.19      kristaps  247:
                    248:        if (NULL == (man = man_alloc(curp, pflags, &mancb)))
                    249:                warnx("memory exhausted");
                    250:
                    251:        return(man);
                    252: }
                    253:
                    254:
                    255: static struct mdoc *
                    256: mdoc_init(struct curparse *curp)
                    257: {
                    258:        int              pflags;
                    259:        struct mdoc     *mdoc;
                    260:        struct mdoc_cb   mdoccb;
                    261:
                    262:        mdoccb.mdoc_err = merr;
                    263:        mdoccb.mdoc_warn = mdocwarn;
                    264:
1.27      kristaps  265:        /*
                    266:         * Default behaviour is to ignore unknown macros, escape
                    267:         * sequences and characters (very liberal).  This is specified
                    268:         * in mandoc.1.
                    269:         */
                    270:
1.24      kristaps  271:        pflags = MDOC_IGN_MACRO | MDOC_IGN_ESCAPE | MDOC_IGN_CHARS;
1.19      kristaps  272:
1.27      kristaps  273:        /* Override default behaviour... */
                    274:
1.19      kristaps  275:        if (curp->fflags & IGN_SCOPE)
                    276:                pflags |= MDOC_IGN_SCOPE;
1.24      kristaps  277:        if (curp->fflags & NO_IGN_ESCAPE)
                    278:                pflags &= ~MDOC_IGN_ESCAPE;
                    279:        if (curp->fflags & NO_IGN_MACRO)
                    280:                pflags &= ~MDOC_IGN_MACRO;
                    281:        if (curp->fflags & NO_IGN_CHARS)
                    282:                pflags &= ~MDOC_IGN_CHARS;
1.19      kristaps  283:
                    284:        if (NULL == (mdoc = mdoc_alloc(curp, pflags, &mdoccb)))
1.24      kristaps  285:                warnx("memory exhausted");
1.19      kristaps  286:
                    287:        return(mdoc);
                    288: }
                    289:
                    290:
                    291: static int
                    292: fstdin(struct buf *blk, struct buf *ln, struct curparse *curp)
                    293: {
                    294:
                    295:        curp->file = "<stdin>";
                    296:        curp->fd = STDIN_FILENO;
                    297:        return(fdesc(blk, ln, curp));
                    298: }
                    299:
                    300:
1.1       kristaps  301: static int
1.19      kristaps  302: ffile(struct buf *blk, struct buf *ln,
                    303:                const char *file, struct curparse *curp)
1.1       kristaps  304: {
1.19      kristaps  305:        int              c;
1.1       kristaps  306:
1.19      kristaps  307:        curp->file = file;
                    308:        if (-1 == (curp->fd = open(curp->file, O_RDONLY, 0))) {
                    309:                warn("%s", curp->file);
1.1       kristaps  310:                return(0);
                    311:        }
                    312:
1.19      kristaps  313:        c = fdesc(blk, ln, curp);
1.1       kristaps  314:
1.19      kristaps  315:        if (-1 == close(curp->fd))
                    316:                warn("%s", curp->file);
1.1       kristaps  317:
                    318:        return(c);
                    319: }
                    320:
                    321:
                    322: static int
1.19      kristaps  323: fdesc(struct buf *blk, struct buf *ln, struct curparse *curp)
1.1       kristaps  324: {
                    325:        size_t           sz;
                    326:        ssize_t          ssz;
                    327:        struct stat      st;
1.29    ! kristaps  328:        int              j, i, pos, lnn, comment;
1.19      kristaps  329:        struct man      *man;
                    330:        struct mdoc     *mdoc;
1.10      kristaps  331:
1.19      kristaps  332:        sz = BUFSIZ;
                    333:        man = NULL;
                    334:        mdoc = NULL;
1.1       kristaps  335:
                    336:        /*
1.19      kristaps  337:         * Two buffers: ln and buf.  buf is the input buffer optimised
                    338:         * here for each file's block size.  ln is a line buffer.  Both
1.1       kristaps  339:         * growable, hence passed in by ptr-ptr.
                    340:         */
                    341:
1.19      kristaps  342:        if (-1 == fstat(curp->fd, &st))
                    343:                warnx("%s", curp->file);
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);
1.19      kristaps  349:                if (NULL == blk->buf) {
                    350:                        warn("realloc");
                    351:                        return(0);
                    352:                }
1.5       kristaps  353:                blk->sz = sz;
1.1       kristaps  354:        }
                    355:
1.19      kristaps  356:        /* Fill buf with file blocksize. */
1.1       kristaps  357:
1.29    ! kristaps  358:        for (lnn = pos = comment = 0; ; ) {
1.19      kristaps  359:                if (-1 == (ssz = read(curp->fd, blk->buf, sz))) {
                    360:                        warn("%s", curp->file);
1.1       kristaps  361:                        return(0);
                    362:                } else if (0 == ssz)
                    363:                        break;
                    364:
1.19      kristaps  365:                /* Parse the read block into partial or full lines. */
                    366:
1.1       kristaps  367:                for (i = 0; i < (int)ssz; i++) {
1.5       kristaps  368:                        if (pos >= (int)ln->sz) {
                    369:                                ln->sz += 256; /* Step-size. */
                    370:                                ln->buf = realloc(ln->buf, ln->sz);
1.19      kristaps  371:                                if (NULL == ln->buf) {
                    372:                                        warn("realloc");
                    373:                                        return(0);
                    374:                                }
1.1       kristaps  375:                        }
                    376:
1.5       kristaps  377:                        if ('\n' != blk->buf[i]) {
1.29    ! kristaps  378:                                if (comment)
        !           379:                                        continue;
1.5       kristaps  380:                                ln->buf[pos++] = blk->buf[i];
1.29    ! kristaps  381:
        !           382:                                /* Handle in-line `\"' comments. */
        !           383:
        !           384:                                if (1 == pos || '\"' != ln->buf[pos - 1])
        !           385:                                        continue;
        !           386:
        !           387:                                for (j = pos - 2; j >= 0; j--)
        !           388:                                        if ('\\' != ln->buf[j])
        !           389:                                                break;
        !           390:
        !           391:                                if ( ! ((pos - 2 - j) % 2))
        !           392:                                        continue;
        !           393:
        !           394:                                comment = 1;
        !           395:                                pos -= 2;
1.1       kristaps  396:                                continue;
1.29    ! kristaps  397:                        }
1.1       kristaps  398:
1.29    ! kristaps  399:                        /* Handle escaped `\\n' newlines. */
1.1       kristaps  400:
1.29    ! kristaps  401:                        if (pos > 0 && 0 == comment &&
        !           402:                                        '\\' == ln->buf[pos - 1]) {
1.1       kristaps  403:                                for (j = pos - 1; j >= 0; j--)
1.5       kristaps  404:                                        if ('\\' != ln->buf[j])
1.1       kristaps  405:                                                break;
                    406:                                if ( ! ((pos - j) % 2)) {
                    407:                                        pos--;
                    408:                                        lnn++;
                    409:                                        continue;
                    410:                                }
                    411:                        }
                    412:
1.5       kristaps  413:                        ln->buf[pos] = 0;
1.19      kristaps  414:                        lnn++;
1.29    ! kristaps  415:
        !           416:                        /* If unset, assign parser in pset(). */
1.19      kristaps  417:
                    418:                        if ( ! (man || mdoc) && ! pset(ln->buf,
                    419:                                                pos, curp, &man, &mdoc))
1.10      kristaps  420:                                return(0);
1.19      kristaps  421:
1.29    ! kristaps  422:                        pos = comment = 0;
1.19      kristaps  423:
1.10      kristaps  424:                        if (man && ! man_parseln(man, lnn, ln->buf))
1.1       kristaps  425:                                return(0);
1.19      kristaps  426:                        if (mdoc && ! mdoc_parseln(mdoc, lnn, ln->buf))
                    427:                                return(0);
1.1       kristaps  428:                }
                    429:        }
                    430:
1.29    ! kristaps  431:        /* NOTE a parser may not have been assigned, yet. */
1.19      kristaps  432:
1.22      kristaps  433:        if ( ! (man || mdoc)) {
                    434:                warnx("%s: not a manual", curp->file);
                    435:                return(0);
                    436:        }
                    437:
                    438:        if (mdoc && ! mdoc_endparse(mdoc))
                    439:                return(0);
                    440:        if (man && ! man_endparse(man))
                    441:                return(0);
1.19      kristaps  442:
1.29    ! kristaps  443:        /* If unset, allocate output dev now (if applicable). */
1.22      kristaps  444:
                    445:        if ( ! (curp->outman && curp->outmdoc)) {
                    446:                switch (curp->outtype) {
                    447:                case (OUTT_TREE):
                    448:                        curp->outman = tree_man;
                    449:                        curp->outmdoc = tree_mdoc;
                    450:                        break;
                    451:                case (OUTT_LINT):
                    452:                        break;
                    453:                default:
                    454:                        curp->outdata = ascii_alloc();
                    455:                        curp->outman = terminal_man;
                    456:                        curp->outmdoc = terminal_mdoc;
                    457:                        curp->outfree = terminal_free;
                    458:                        break;
                    459:                }
                    460:        }
                    461:
                    462:        /* Execute the out device, if it exists. */
                    463:
                    464:        if (man && curp->outman)
                    465:                if ( ! (*curp->outman)(curp->outdata, man))
                    466:                        return(0);
                    467:        if (mdoc && curp->outmdoc)
                    468:                if ( ! (*curp->outmdoc)(curp->outdata, mdoc))
                    469:                        return(0);
                    470:
                    471:        return(1);
1.19      kristaps  472: }
                    473:
                    474:
                    475: static int
1.22      kristaps  476: pset(const char *buf, int pos, struct curparse *curp,
1.19      kristaps  477:                struct man **man, struct mdoc **mdoc)
                    478: {
1.29    ! kristaps  479:        int              i;
1.19      kristaps  480:
                    481:        /*
                    482:         * Try to intuit which kind of manual parser should be used.  If
                    483:         * passed in by command-line (-man, -mdoc), then use that
                    484:         * explicitly.  If passed as -mandoc, then try to guess from the
                    485:         * line: either skip comments, use -mdoc when finding `.Dt', or
                    486:         * default to -man, which is more lenient.
                    487:         */
                    488:
1.29    ! kristaps  489:        if (buf[0] == '.') {
        !           490:                for (i = 1; buf[i]; i++)
        !           491:                        if (' ' != buf[i] && '\t' != buf[i])
        !           492:                                break;
        !           493:                if (0 == buf[i])
        !           494:                        return(1);
        !           495:        }
1.10      kristaps  496:
1.19      kristaps  497:        switch (curp->inttype) {
                    498:        case (INTT_MDOC):
                    499:                if (NULL == curp->mdoc)
                    500:                        curp->mdoc = mdoc_init(curp);
                    501:                if (NULL == (*mdoc = curp->mdoc))
                    502:                        return(0);
1.22      kristaps  503:                curp->lastmdoc = *mdoc;
1.19      kristaps  504:                return(1);
                    505:        case (INTT_MAN):
                    506:                if (NULL == curp->man)
                    507:                        curp->man = man_init(curp);
                    508:                if (NULL == (*man = curp->man))
                    509:                        return(0);
1.22      kristaps  510:                curp->lastman = *man;
1.19      kristaps  511:                return(1);
                    512:        default:
                    513:                break;
                    514:        }
                    515:
                    516:        if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3))  {
                    517:                if (NULL == curp->mdoc)
                    518:                        curp->mdoc = mdoc_init(curp);
                    519:                if (NULL == (*mdoc = curp->mdoc))
                    520:                        return(0);
1.22      kristaps  521:                curp->lastmdoc = *mdoc;
1.19      kristaps  522:                return(1);
                    523:        }
                    524:
                    525:        if (NULL == curp->man)
                    526:                curp->man = man_init(curp);
                    527:        if (NULL == (*man = curp->man))
                    528:                return(0);
1.22      kristaps  529:        curp->lastman = *man;
1.19      kristaps  530:        return(1);
1.10      kristaps  531: }
                    532:
                    533:
                    534: static int
                    535: moptions(enum intt *tflags, char *arg)
                    536: {
                    537:
1.17      kristaps  538:        if (0 == strcmp(arg, "doc"))
1.10      kristaps  539:                *tflags = INTT_MDOC;
1.19      kristaps  540:        else if (0 == strcmp(arg, "andoc"))
                    541:                *tflags = INTT_AUTO;
1.17      kristaps  542:        else if (0 == strcmp(arg, "an"))
1.10      kristaps  543:                *tflags = INTT_MAN;
                    544:        else {
                    545:                warnx("bad argument: -m%s", arg);
                    546:                return(0);
                    547:        }
                    548:
                    549:        return(1);
1.1       kristaps  550: }
                    551:
                    552:
                    553: static int
                    554: toptions(enum outt *tflags, char *arg)
                    555: {
                    556:
                    557:        if (0 == strcmp(arg, "ascii"))
                    558:                *tflags = OUTT_ASCII;
                    559:        else if (0 == strcmp(arg, "lint"))
                    560:                *tflags = OUTT_LINT;
                    561:        else if (0 == strcmp(arg, "tree"))
                    562:                *tflags = OUTT_TREE;
                    563:        else {
                    564:                warnx("bad argument: -T%s", arg);
                    565:                return(0);
                    566:        }
                    567:
                    568:        return(1);
                    569: }
                    570:
                    571:
                    572: /*
                    573:  * Parse out the options for [-fopt...] setting compiler options.  These
                    574:  * can be comma-delimited or called again.
                    575:  */
                    576: static int
                    577: foptions(int *fflags, char *arg)
                    578: {
                    579:        char            *v;
1.24      kristaps  580:        char            *toks[6];
1.1       kristaps  581:
                    582:        toks[0] = "ign-scope";
1.24      kristaps  583:        toks[1] = "no-ign-escape";
                    584:        toks[2] = "no-ign-macro";
                    585:        toks[3] = "no-ign-chars";
                    586:        toks[4] = "strict";
                    587:        toks[5] = NULL;
1.1       kristaps  588:
                    589:        while (*arg)
                    590:                switch (getsubopt(&arg, toks, &v)) {
                    591:                case (0):
1.15      kristaps  592:                        *fflags |= IGN_SCOPE;
1.1       kristaps  593:                        break;
                    594:                case (1):
1.24      kristaps  595:                        *fflags |= NO_IGN_ESCAPE;
1.1       kristaps  596:                        break;
                    597:                case (2):
1.24      kristaps  598:                        *fflags |= NO_IGN_MACRO;
1.1       kristaps  599:                        break;
1.20      kristaps  600:                case (3):
1.24      kristaps  601:                        *fflags |= NO_IGN_CHARS;
                    602:                        break;
                    603:                case (4):
                    604:                        *fflags |= NO_IGN_ESCAPE |
                    605:                                   NO_IGN_MACRO | NO_IGN_CHARS;
1.20      kristaps  606:                        break;
1.1       kristaps  607:                default:
                    608:                        warnx("bad argument: -f%s", arg);
                    609:                        return(0);
                    610:                }
                    611:
                    612:        return(1);
                    613: }
                    614:
                    615:
                    616: /*
                    617:  * Parse out the options for [-Werr...], which sets warning modes.
                    618:  * These can be comma-delimited or called again.
                    619:  */
                    620: static int
                    621: woptions(int *wflags, char *arg)
                    622: {
                    623:        char            *v;
                    624:        char            *toks[5];
                    625:
                    626:        toks[0] = "all";
                    627:        toks[1] = "compat";
                    628:        toks[2] = "syntax";
                    629:        toks[3] = "error";
                    630:        toks[4] = NULL;
                    631:
                    632:        while (*arg)
                    633:                switch (getsubopt(&arg, toks, &v)) {
                    634:                case (0):
                    635:                        *wflags |= WARN_WALL;
                    636:                        break;
                    637:                case (1):
                    638:                        *wflags |= WARN_WCOMPAT;
                    639:                        break;
                    640:                case (2):
                    641:                        *wflags |= WARN_WSYNTAX;
                    642:                        break;
                    643:                case (3):
                    644:                        *wflags |= WARN_WERR;
                    645:                        break;
                    646:                default:
                    647:                        warnx("bad argument: -W%s", arg);
                    648:                        return(0);
                    649:                }
                    650:
                    651:        return(1);
                    652: }
                    653:
                    654:
1.2       kristaps  655: /* ARGSUSED */
1.1       kristaps  656: static int
                    657: merr(void *arg, int line, int col, const char *msg)
                    658: {
1.5       kristaps  659:        struct curparse *curp;
                    660:
                    661:        curp = (struct curparse *)arg;
                    662:        warnx("%s:%d: error: %s (column %d)",
                    663:                        curp->file, line, msg, col);
1.27      kristaps  664:
                    665:        /* Always exit on errors... */
1.1       kristaps  666:        return(0);
                    667: }
                    668:
                    669:
                    670: static int
1.14      kristaps  671: mdocwarn(void *arg, int line, int col,
1.1       kristaps  672:                enum mdoc_warn type, const char *msg)
                    673: {
1.5       kristaps  674:        struct curparse *curp;
1.1       kristaps  675:        char            *wtype;
                    676:
1.5       kristaps  677:        curp = (struct curparse *)arg;
1.1       kristaps  678:        wtype = NULL;
                    679:
                    680:        switch (type) {
                    681:        case (WARN_COMPAT):
                    682:                wtype = "compat";
1.5       kristaps  683:                if (curp->wflags & WARN_WCOMPAT)
1.1       kristaps  684:                        break;
                    685:                return(1);
                    686:        case (WARN_SYNTAX):
                    687:                wtype = "syntax";
1.5       kristaps  688:                if (curp->wflags & WARN_WSYNTAX)
1.1       kristaps  689:                        break;
                    690:                return(1);
                    691:        }
                    692:
                    693:        assert(wtype);
1.5       kristaps  694:        warnx("%s:%d: %s warning: %s (column %d)",
                    695:                        curp->file, line, wtype, msg, col);
1.1       kristaps  696:
1.5       kristaps  697:        if ( ! (curp->wflags & WARN_WERR))
1.1       kristaps  698:                return(1);
1.27      kristaps  699:
                    700:        /*
                    701:         * If the -Werror flag is passed in, as in gcc, then all
                    702:         * warnings are considered as errors.
                    703:         */
1.1       kristaps  704:
                    705:        warnx("%s: considering warnings as errors",
                    706:                        __progname);
                    707:        return(0);
                    708: }
                    709:
                    710:
1.14      kristaps  711: static int
                    712: manwarn(void *arg, int line, int col, const char *msg)
                    713: {
                    714:        struct curparse *curp;
                    715:
                    716:        curp = (struct curparse *)arg;
                    717:
                    718:        if ( ! (curp->wflags & WARN_WSYNTAX))
                    719:                return(1);
                    720:
                    721:        warnx("%s:%d: syntax warning: %s (column %d)",
                    722:                        curp->file, line, msg, col);
                    723:
                    724:        if ( ! (curp->wflags & WARN_WERR))
                    725:                return(1);
                    726:
1.27      kristaps  727:        /*
                    728:         * If the -Werror flag is passed in, as in gcc, then all
                    729:         * warnings are considered as errors.
                    730:         */
                    731:
1.14      kristaps  732:        warnx("%s: considering warnings as errors",
                    733:                        __progname);
                    734:        return(0);
                    735: }

CVSweb