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

Annotation of mandoc/main.c, Revision 1.47

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

CVSweb