[BACK]Return to main.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / mandoc

Annotation of src/usr.bin/mandoc/main.c, Revision 1.5

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