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

Annotation of mandoc/main.c, Revision 1.41

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

CVSweb