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

Annotation of mandoc/main.c, Revision 1.22

1.22    ! kristaps    1: /* $Id: main.c,v 1.21 2009/04/02 16:42:35 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.16      kristaps   32: /* Account for FreeBSD and Linux in our declarations. */
                     33:
1.3       kristaps   34: #ifdef __linux__
                     35: extern int               getsubopt(char **, char * const *, char **);
                     36: # ifndef __dead
                     37: #  define __dead __attribute__((__noreturn__))
                     38: # endif
1.13      kristaps   39: #elif defined(__dead2)
1.10      kristaps   40: # ifndef __dead
                     41: #  define __dead __dead2
                     42: # endif
1.3       kristaps   43: #endif
                     44:
1.22    ! kristaps   45: typedef        int             (*out_mdoc)(void *, const struct mdoc *);
        !            46: typedef        int             (*out_man)(void *, const struct man *);
        !            47: typedef        void            (*out_free)(void *);
        !            48:
1.5       kristaps   49: struct buf {
                     50:        char             *buf;
                     51:        size_t            sz;
                     52: };
                     53:
1.19      kristaps   54: enum   intt {
                     55:        INTT_AUTO,
                     56:        INTT_MDOC,
                     57:        INTT_MAN
                     58: };
                     59:
                     60: enum   outt {
                     61:        OUTT_ASCII = 0,
                     62:        OUTT_TREE,
                     63:        OUTT_LINT
                     64: };
                     65:
1.5       kristaps   66: struct curparse {
1.22    ! kristaps   67:        const char       *file;         /* Current parse. */
        !            68:        int               fd;           /* Current parse. */
1.5       kristaps   69:        int               wflags;
1.1       kristaps   70: #define        WARN_WALL         0x03          /* All-warnings mask. */
                     71: #define        WARN_WCOMPAT     (1 << 0)       /* Compatibility warnings. */
                     72: #define        WARN_WSYNTAX     (1 << 1)       /* Syntax warnings. */
                     73: #define        WARN_WERR        (1 << 2)       /* Warnings->errors. */
1.22    ! kristaps   74:        int               fflags;       /* Per-intt flags. */
        !            75:        enum intt         inttype;      /* Input parsers. */
1.19      kristaps   76:        struct man       *man;
1.22    ! kristaps   77:        struct man       *lastman;
1.19      kristaps   78:        struct mdoc      *mdoc;
1.22    ! kristaps   79:        struct mdoc      *lastmdoc;
        !            80:        enum outt         outtype;      /* Output devices. */
        !            81:        out_mdoc          outmdoc;
        !            82:        out_man           outman;
        !            83:        out_free          outfree;
        !            84:        void             *outdata;
1.5       kristaps   85: };
1.1       kristaps   86:
1.16      kristaps   87: #define        IGN_SCOPE        (1 << 0)       /* Ignore scope errors. */
                     88: #define        IGN_ESCAPE       (1 << 1)       /* Ignore bad escapes. */
                     89: #define        IGN_MACRO        (1 << 2)       /* Ignore unknown macros. */
1.20      kristaps   90: #define        NO_IGN_MACRO     (1 << 3)
1.15      kristaps   91:
1.1       kristaps   92: extern void             *ascii_alloc(void);
1.22    ! kristaps   93: extern int               tree_mdoc(void *, const struct mdoc *);
        !            94: extern int               tree_man(void *, const struct man *);
        !            95: extern int               terminal_mdoc(void *, const struct mdoc *);
        !            96: extern int               terminal_man(void *, const struct man *);
1.1       kristaps   97: extern void              terminal_free(void *);
                     98:
                     99: static int               foptions(int *, char *);
                    100: static int               toptions(enum outt *, char *);
1.10      kristaps  101: static int               moptions(enum intt *, char *);
1.1       kristaps  102: static int               woptions(int *, char *);
                    103: static int               merr(void *, int, int, const char *);
1.14      kristaps  104: static int               manwarn(void *, int, int, const char *);
                    105: static int               mdocwarn(void *, int, int,
1.1       kristaps  106:                                enum mdoc_warn, const char *);
1.19      kristaps  107: static int               fstdin(struct buf *, struct buf *,
                    108:                                struct curparse *);
                    109: static int               ffile(struct buf *, struct buf *,
                    110:                                const char *, struct curparse *);
1.5       kristaps  111: static int               fdesc(struct buf *, struct buf *,
1.19      kristaps  112:                                struct curparse *);
1.22    ! kristaps  113: static int               pset(const char *, int, struct curparse *,
1.19      kristaps  114:                                struct man **, struct mdoc **);
                    115: static struct man       *man_init(struct curparse *);
                    116: static struct mdoc      *mdoc_init(struct curparse *);
1.10      kristaps  117: __dead static void       version(void);
                    118: __dead static void       usage(void);
1.1       kristaps  119:
1.22    ! kristaps  120: extern char             *__progname;
        !           121:
1.1       kristaps  122:
                    123: int
                    124: main(int argc, char *argv[])
                    125: {
1.19      kristaps  126:        int              c, rc;
1.5       kristaps  127:        struct buf       ln, blk;
                    128:        struct curparse  curp;
1.1       kristaps  129:
1.5       kristaps  130:        bzero(&curp, sizeof(struct curparse));
                    131:
1.19      kristaps  132:        curp.inttype = INTT_AUTO;
1.22    ! kristaps  133:        curp.outtype = OUTT_ASCII;
1.19      kristaps  134:
1.1       kristaps  135:        /* LINTED */
1.10      kristaps  136:        while (-1 != (c = getopt(argc, argv, "f:m:VW:T:")))
1.1       kristaps  137:                switch (c) {
                    138:                case ('f'):
1.19      kristaps  139:                        if ( ! foptions(&curp.fflags, optarg))
1.1       kristaps  140:                                return(0);
                    141:                        break;
1.10      kristaps  142:                case ('m'):
1.19      kristaps  143:                        if ( ! moptions(&curp.inttype, optarg))
1.10      kristaps  144:                                return(0);
                    145:                        break;
1.1       kristaps  146:                case ('T'):
1.22    ! kristaps  147:                        if ( ! toptions(&curp.outtype, optarg))
1.1       kristaps  148:                                return(0);
                    149:                        break;
                    150:                case ('W'):
1.5       kristaps  151:                        if ( ! woptions(&curp.wflags, optarg))
1.1       kristaps  152:                                return(0);
                    153:                        break;
                    154:                case ('V'):
                    155:                        version();
                    156:                        /* NOTREACHED */
                    157:                default:
                    158:                        usage();
                    159:                        /* NOTREACHED */
                    160:                }
                    161:
                    162:        argc -= optind;
                    163:        argv += optind;
                    164:
1.16      kristaps  165:        /* Configure buffers. */
                    166:
1.5       kristaps  167:        bzero(&ln, sizeof(struct buf));
                    168:        bzero(&blk, sizeof(struct buf));
1.1       kristaps  169:
1.22    ! kristaps  170:        rc = 1;
1.1       kristaps  171:
1.22    ! kristaps  172:        if (NULL == *argv)
        !           173:                if ( ! fstdin(&blk, &ln, &curp))
        !           174:                        rc = 0;
        !           175:
        !           176:        while (rc && *argv) {
        !           177:                if ( ! ffile(&blk, &ln, *argv, &curp))
        !           178:                        rc = 0;
        !           179:                argv++;
        !           180:                if (*argv && rc) {
        !           181:                        if (curp.lastman)
        !           182:                                if ( ! man_reset(curp.lastman))
        !           183:                                        rc = 0;
        !           184:                        if (curp.lastmdoc)
        !           185:                                if ( ! mdoc_reset(curp.lastmdoc))
        !           186:                                        rc = 0;
        !           187:                        curp.lastman = NULL;
        !           188:                        curp.lastmdoc = NULL;
1.4       kristaps  189:                }
1.1       kristaps  190:        }
                    191:
1.5       kristaps  192:        if (blk.buf)
                    193:                free(blk.buf);
                    194:        if (ln.buf)
                    195:                free(ln.buf);
1.22    ! kristaps  196:        if (curp.outfree)
        !           197:                (*curp.outfree)(curp.outdata);
1.19      kristaps  198:        if (curp.mdoc)
                    199:                mdoc_free(curp.mdoc);
                    200:        if (curp.man)
                    201:                man_free(curp.man);
1.1       kristaps  202:
                    203:        return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
                    204: }
                    205:
                    206:
                    207: __dead static void
                    208: version(void)
                    209: {
                    210:
                    211:        (void)printf("%s %s\n", __progname, VERSION);
1.18      kristaps  212:        exit(EXIT_SUCCESS);
1.1       kristaps  213: }
                    214:
                    215:
                    216: __dead static void
                    217: usage(void)
                    218: {
                    219:
1.12      kristaps  220:        (void)fprintf(stderr, "usage: %s [-V] [-foption...] "
                    221:                        "[-mformat] [-Toutput] [-Werr...]\n",
                    222:                        __progname);
1.18      kristaps  223:        exit(EXIT_FAILURE);
1.1       kristaps  224: }
                    225:
                    226:
1.19      kristaps  227: static struct man *
                    228: man_init(struct curparse *curp)
                    229: {
                    230:        int              pflags;
                    231:        struct man      *man;
                    232:        struct man_cb    mancb;
                    233:
                    234:        mancb.man_err = merr;
                    235:        mancb.man_warn = manwarn;
                    236:
1.22    ! kristaps  237:        pflags = MAN_IGN_MACRO; /* XXX */
1.19      kristaps  238:
1.20      kristaps  239:        if (curp->fflags & NO_IGN_MACRO)
                    240:                pflags &= ~MAN_IGN_MACRO;
1.19      kristaps  241:
                    242:        if (NULL == (man = man_alloc(curp, pflags, &mancb)))
                    243:                warnx("memory exhausted");
                    244:
                    245:        return(man);
                    246: }
                    247:
                    248:
                    249: static struct mdoc *
                    250: mdoc_init(struct curparse *curp)
                    251: {
                    252:        int              pflags;
                    253:        struct mdoc     *mdoc;
                    254:        struct mdoc_cb   mdoccb;
                    255:
                    256:        mdoccb.mdoc_msg = NULL;
                    257:        mdoccb.mdoc_err = merr;
                    258:        mdoccb.mdoc_warn = mdocwarn;
                    259:
1.22    ! kristaps  260:        pflags = 0; /* XXX */
1.19      kristaps  261:
                    262:        if (curp->fflags & IGN_SCOPE)
                    263:                pflags |= MDOC_IGN_SCOPE;
                    264:        if (curp->fflags & IGN_ESCAPE)
                    265:                pflags |= MDOC_IGN_ESCAPE;
                    266:        if (curp->fflags & IGN_MACRO)
                    267:                pflags |= MDOC_IGN_MACRO;
                    268:
                    269:        if (NULL == (mdoc = mdoc_alloc(curp, pflags, &mdoccb)))
                    270:                warnx("memory allocated");
                    271:
                    272:        return(mdoc);
                    273: }
                    274:
                    275:
                    276: static int
                    277: fstdin(struct buf *blk, struct buf *ln, struct curparse *curp)
                    278: {
                    279:
                    280:        curp->file = "<stdin>";
                    281:        curp->fd = STDIN_FILENO;
                    282:        return(fdesc(blk, ln, curp));
                    283: }
                    284:
                    285:
1.1       kristaps  286: static int
1.19      kristaps  287: ffile(struct buf *blk, struct buf *ln,
                    288:                const char *file, struct curparse *curp)
1.1       kristaps  289: {
1.19      kristaps  290:        int              c;
1.1       kristaps  291:
1.19      kristaps  292:        curp->file = file;
                    293:        if (-1 == (curp->fd = open(curp->file, O_RDONLY, 0))) {
                    294:                warn("%s", curp->file);
1.1       kristaps  295:                return(0);
                    296:        }
                    297:
1.19      kristaps  298:        c = fdesc(blk, ln, curp);
1.1       kristaps  299:
1.19      kristaps  300:        if (-1 == close(curp->fd))
                    301:                warn("%s", curp->file);
1.1       kristaps  302:
                    303:        return(c);
                    304: }
                    305:
                    306:
                    307: static int
1.19      kristaps  308: fdesc(struct buf *blk, struct buf *ln, struct curparse *curp)
1.1       kristaps  309: {
                    310:        size_t           sz;
                    311:        ssize_t          ssz;
                    312:        struct stat      st;
                    313:        int              j, i, pos, lnn;
1.19      kristaps  314:        struct man      *man;
                    315:        struct mdoc     *mdoc;
1.10      kristaps  316:
1.19      kristaps  317:        sz = BUFSIZ;
                    318:        man = NULL;
                    319:        mdoc = NULL;
1.1       kristaps  320:
                    321:        /*
1.19      kristaps  322:         * Two buffers: ln and buf.  buf is the input buffer optimised
                    323:         * here for each file's block size.  ln is a line buffer.  Both
1.1       kristaps  324:         * growable, hence passed in by ptr-ptr.
                    325:         */
                    326:
1.19      kristaps  327:        if (-1 == fstat(curp->fd, &st))
                    328:                warnx("%s", curp->file);
1.6       kristaps  329:        else if ((size_t)st.st_blksize > sz)
                    330:                sz = st.st_blksize;
1.1       kristaps  331:
1.5       kristaps  332:        if (sz > blk->sz) {
                    333:                blk->buf = realloc(blk->buf, sz);
1.19      kristaps  334:                if (NULL == blk->buf) {
                    335:                        warn("realloc");
                    336:                        return(0);
                    337:                }
1.5       kristaps  338:                blk->sz = sz;
1.1       kristaps  339:        }
                    340:
1.19      kristaps  341:        /* Fill buf with file blocksize. */
1.1       kristaps  342:
1.19      kristaps  343:        for (lnn = 0, pos = 0; ; ) {
                    344:                if (-1 == (ssz = read(curp->fd, blk->buf, sz))) {
                    345:                        warn("%s", curp->file);
1.1       kristaps  346:                        return(0);
                    347:                } else if (0 == ssz)
                    348:                        break;
                    349:
1.19      kristaps  350:                /* Parse the read block into partial or full lines. */
                    351:
1.1       kristaps  352:                for (i = 0; i < (int)ssz; i++) {
1.5       kristaps  353:                        if (pos >= (int)ln->sz) {
                    354:                                ln->sz += 256; /* Step-size. */
                    355:                                ln->buf = realloc(ln->buf, ln->sz);
1.19      kristaps  356:                                if (NULL == ln->buf) {
                    357:                                        warn("realloc");
                    358:                                        return(0);
                    359:                                }
1.1       kristaps  360:                        }
                    361:
1.5       kristaps  362:                        if ('\n' != blk->buf[i]) {
                    363:                                ln->buf[pos++] = blk->buf[i];
1.1       kristaps  364:                                continue;
                    365:                        }
                    366:
                    367:                        /* Check for CPP-escaped newline.  */
                    368:
1.5       kristaps  369:                        if (pos > 0 && '\\' == ln->buf[pos - 1]) {
1.1       kristaps  370:                                for (j = pos - 1; j >= 0; j--)
1.5       kristaps  371:                                        if ('\\' != ln->buf[j])
1.1       kristaps  372:                                                break;
                    373:
                    374:                                if ( ! ((pos - j) % 2)) {
                    375:                                        pos--;
                    376:                                        lnn++;
                    377:                                        continue;
                    378:                                }
                    379:                        }
                    380:
1.5       kristaps  381:                        ln->buf[pos] = 0;
1.19      kristaps  382:                        lnn++;
                    383:
                    384:                        /*
                    385:                         * If no manual parser has been assigned, then
                    386:                         * try to assign one in pset(), which may do
                    387:                         * nothing at all.  After this, parse the manual
                    388:                         * line accordingly.
                    389:                         */
                    390:
                    391:                        if ( ! (man || mdoc) && ! pset(ln->buf,
                    392:                                                pos, curp, &man, &mdoc))
1.10      kristaps  393:                                return(0);
1.19      kristaps  394:
                    395:                        pos = 0;
                    396:
1.10      kristaps  397:                        if (man && ! man_parseln(man, lnn, ln->buf))
1.1       kristaps  398:                                return(0);
1.19      kristaps  399:                        if (mdoc && ! mdoc_parseln(mdoc, lnn, ln->buf))
                    400:                                return(0);
1.1       kristaps  401:                }
                    402:        }
                    403:
1.19      kristaps  404:        /* Note that a parser may not have been assigned, yet. */
                    405:
1.22    ! kristaps  406:        if ( ! (man || mdoc)) {
        !           407:                warnx("%s: not a manual", curp->file);
        !           408:                return(0);
        !           409:        }
        !           410:
        !           411:        if (mdoc && ! mdoc_endparse(mdoc))
        !           412:                return(0);
        !           413:        if (man && ! man_endparse(man))
        !           414:                return(0);
1.19      kristaps  415:
1.22    ! kristaps  416:        /*
        !           417:         * If an output device hasn't been allocated, see if we should
        !           418:         * do so now.  Note that not all outtypes have functions, so
        !           419:         * this switch statement may be superfluous, but it's
        !           420:         * low-overhead enough not to matter very much.
        !           421:         */
        !           422:
        !           423:        if ( ! (curp->outman && curp->outmdoc)) {
        !           424:                switch (curp->outtype) {
        !           425:                case (OUTT_TREE):
        !           426:                        curp->outman = tree_man;
        !           427:                        curp->outmdoc = tree_mdoc;
        !           428:                        break;
        !           429:                case (OUTT_LINT):
        !           430:                        break;
        !           431:                default:
        !           432:                        curp->outdata = ascii_alloc();
        !           433:                        curp->outman = terminal_man;
        !           434:                        curp->outmdoc = terminal_mdoc;
        !           435:                        curp->outfree = terminal_free;
        !           436:                        break;
        !           437:                }
        !           438:        }
        !           439:
        !           440:        /* Execute the out device, if it exists. */
        !           441:
        !           442:        if (man && curp->outman)
        !           443:                if ( ! (*curp->outman)(curp->outdata, man))
        !           444:                        return(0);
        !           445:        if (mdoc && curp->outmdoc)
        !           446:                if ( ! (*curp->outmdoc)(curp->outdata, mdoc))
        !           447:                        return(0);
        !           448:
        !           449:        return(1);
1.19      kristaps  450: }
                    451:
                    452:
                    453: static int
1.22    ! kristaps  454: pset(const char *buf, int pos, struct curparse *curp,
1.19      kristaps  455:                struct man **man, struct mdoc **mdoc)
                    456: {
                    457:
                    458:        /*
                    459:         * Try to intuit which kind of manual parser should be used.  If
                    460:         * passed in by command-line (-man, -mdoc), then use that
                    461:         * explicitly.  If passed as -mandoc, then try to guess from the
                    462:         * line: either skip comments, use -mdoc when finding `.Dt', or
                    463:         * default to -man, which is more lenient.
                    464:         */
                    465:
                    466:        if (pos >= 3 && 0 == memcmp(buf, ".\\\"", 3))
                    467:                return(1);
1.10      kristaps  468:
1.19      kristaps  469:        switch (curp->inttype) {
                    470:        case (INTT_MDOC):
                    471:                if (NULL == curp->mdoc)
                    472:                        curp->mdoc = mdoc_init(curp);
                    473:                if (NULL == (*mdoc = curp->mdoc))
                    474:                        return(0);
1.22    ! kristaps  475:                curp->lastmdoc = *mdoc;
1.19      kristaps  476:                return(1);
                    477:        case (INTT_MAN):
                    478:                if (NULL == curp->man)
                    479:                        curp->man = man_init(curp);
                    480:                if (NULL == (*man = curp->man))
                    481:                        return(0);
1.22    ! kristaps  482:                curp->lastman = *man;
1.19      kristaps  483:                return(1);
                    484:        default:
                    485:                break;
                    486:        }
                    487:
                    488:        if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3))  {
                    489:                if (NULL == curp->mdoc)
                    490:                        curp->mdoc = mdoc_init(curp);
                    491:                if (NULL == (*mdoc = curp->mdoc))
                    492:                        return(0);
1.22    ! kristaps  493:                curp->lastmdoc = *mdoc;
1.19      kristaps  494:                return(1);
                    495:        }
                    496:
                    497:        if (NULL == curp->man)
                    498:                curp->man = man_init(curp);
                    499:        if (NULL == (*man = curp->man))
                    500:                return(0);
1.22    ! kristaps  501:        curp->lastman = *man;
1.19      kristaps  502:        return(1);
1.10      kristaps  503: }
                    504:
                    505:
                    506: static int
                    507: moptions(enum intt *tflags, char *arg)
                    508: {
                    509:
1.17      kristaps  510:        if (0 == strcmp(arg, "doc"))
1.10      kristaps  511:                *tflags = INTT_MDOC;
1.19      kristaps  512:        else if (0 == strcmp(arg, "andoc"))
                    513:                *tflags = INTT_AUTO;
1.17      kristaps  514:        else if (0 == strcmp(arg, "an"))
1.10      kristaps  515:                *tflags = INTT_MAN;
                    516:        else {
                    517:                warnx("bad argument: -m%s", arg);
                    518:                return(0);
                    519:        }
                    520:
                    521:        return(1);
1.1       kristaps  522: }
                    523:
                    524:
                    525: static int
                    526: toptions(enum outt *tflags, char *arg)
                    527: {
                    528:
                    529:        if (0 == strcmp(arg, "ascii"))
                    530:                *tflags = OUTT_ASCII;
                    531:        else if (0 == strcmp(arg, "lint"))
                    532:                *tflags = OUTT_LINT;
                    533:        else if (0 == strcmp(arg, "tree"))
                    534:                *tflags = OUTT_TREE;
                    535:        else {
                    536:                warnx("bad argument: -T%s", arg);
                    537:                return(0);
                    538:        }
                    539:
                    540:        return(1);
                    541: }
                    542:
                    543:
                    544: /*
                    545:  * Parse out the options for [-fopt...] setting compiler options.  These
                    546:  * can be comma-delimited or called again.
                    547:  */
                    548: static int
                    549: foptions(int *fflags, char *arg)
                    550: {
                    551:        char            *v;
1.20      kristaps  552:        char            *toks[5];
1.1       kristaps  553:
                    554:        toks[0] = "ign-scope";
                    555:        toks[1] = "ign-escape";
                    556:        toks[2] = "ign-macro";
1.22    ! kristaps  557:        toks[3] = "no-ign-macro";
        !           558:        toks[4] = NULL;
1.1       kristaps  559:
                    560:        while (*arg)
                    561:                switch (getsubopt(&arg, toks, &v)) {
                    562:                case (0):
1.15      kristaps  563:                        *fflags |= IGN_SCOPE;
1.1       kristaps  564:                        break;
                    565:                case (1):
1.15      kristaps  566:                        *fflags |= IGN_ESCAPE;
1.1       kristaps  567:                        break;
                    568:                case (2):
1.15      kristaps  569:                        *fflags |= IGN_MACRO;
1.1       kristaps  570:                        break;
1.20      kristaps  571:                case (3):
                    572:                        *fflags |= NO_IGN_MACRO;
                    573:                        break;
1.1       kristaps  574:                default:
                    575:                        warnx("bad argument: -f%s", arg);
                    576:                        return(0);
                    577:                }
                    578:
                    579:        return(1);
                    580: }
                    581:
                    582:
                    583: /*
                    584:  * Parse out the options for [-Werr...], which sets warning modes.
                    585:  * These can be comma-delimited or called again.
                    586:  */
                    587: static int
                    588: woptions(int *wflags, char *arg)
                    589: {
                    590:        char            *v;
                    591:        char            *toks[5];
                    592:
                    593:        toks[0] = "all";
                    594:        toks[1] = "compat";
                    595:        toks[2] = "syntax";
                    596:        toks[3] = "error";
                    597:        toks[4] = NULL;
                    598:
                    599:        while (*arg)
                    600:                switch (getsubopt(&arg, toks, &v)) {
                    601:                case (0):
                    602:                        *wflags |= WARN_WALL;
                    603:                        break;
                    604:                case (1):
                    605:                        *wflags |= WARN_WCOMPAT;
                    606:                        break;
                    607:                case (2):
                    608:                        *wflags |= WARN_WSYNTAX;
                    609:                        break;
                    610:                case (3):
                    611:                        *wflags |= WARN_WERR;
                    612:                        break;
                    613:                default:
                    614:                        warnx("bad argument: -W%s", arg);
                    615:                        return(0);
                    616:                }
                    617:
                    618:        return(1);
                    619: }
                    620:
                    621:
1.2       kristaps  622: /* ARGSUSED */
1.1       kristaps  623: static int
                    624: merr(void *arg, int line, int col, const char *msg)
                    625: {
1.5       kristaps  626:        struct curparse *curp;
                    627:
                    628:        curp = (struct curparse *)arg;
1.1       kristaps  629:
1.5       kristaps  630:        warnx("%s:%d: error: %s (column %d)",
                    631:                        curp->file, line, msg, col);
1.1       kristaps  632:        return(0);
                    633: }
                    634:
                    635:
                    636: static int
1.14      kristaps  637: mdocwarn(void *arg, int line, int col,
1.1       kristaps  638:                enum mdoc_warn type, const char *msg)
                    639: {
1.5       kristaps  640:        struct curparse *curp;
1.1       kristaps  641:        char            *wtype;
                    642:
1.5       kristaps  643:        curp = (struct curparse *)arg;
1.1       kristaps  644:        wtype = NULL;
                    645:
                    646:        switch (type) {
                    647:        case (WARN_COMPAT):
                    648:                wtype = "compat";
1.5       kristaps  649:                if (curp->wflags & WARN_WCOMPAT)
1.1       kristaps  650:                        break;
                    651:                return(1);
                    652:        case (WARN_SYNTAX):
                    653:                wtype = "syntax";
1.5       kristaps  654:                if (curp->wflags & WARN_WSYNTAX)
1.1       kristaps  655:                        break;
                    656:                return(1);
                    657:        }
                    658:
                    659:        assert(wtype);
1.5       kristaps  660:        warnx("%s:%d: %s warning: %s (column %d)",
                    661:                        curp->file, line, wtype, msg, col);
1.1       kristaps  662:
1.5       kristaps  663:        if ( ! (curp->wflags & WARN_WERR))
1.1       kristaps  664:                return(1);
                    665:
                    666:        warnx("%s: considering warnings as errors",
                    667:                        __progname);
                    668:        return(0);
                    669: }
                    670:
                    671:
1.14      kristaps  672: static int
                    673: manwarn(void *arg, int line, int col, const char *msg)
                    674: {
                    675:        struct curparse *curp;
                    676:
                    677:        curp = (struct curparse *)arg;
                    678:
                    679:        if ( ! (curp->wflags & WARN_WSYNTAX))
                    680:                return(1);
                    681:
                    682:        warnx("%s:%d: syntax warning: %s (column %d)",
                    683:                        curp->file, line, msg, col);
                    684:
                    685:        if ( ! (curp->wflags & WARN_WERR))
                    686:                return(1);
                    687:
                    688:        warnx("%s: considering warnings as errors",
                    689:                        __progname);
                    690:        return(0);
                    691: }

CVSweb