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

Annotation of mandoc/main.c, Revision 1.52

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

CVSweb