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

Annotation of mandoc/main.c, Revision 1.50

1.50    ! kristaps    1: /*     $Id: main.c,v 1.49 2009/10/26 08:42:37 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.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.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.5       kristaps  161:        bzero(&ln, sizeof(struct buf));
                    162:        bzero(&blk, 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)
                    187:                                if ( ! man_reset(curp.lastman))
                    188:                                        rc = 0;
                    189:                        if (curp.lastmdoc)
                    190:                                if ( ! mdoc_reset(curp.lastmdoc))
                    191:                                        rc = 0;
                    192:                        curp.lastman = NULL;
                    193:                        curp.lastmdoc = NULL;
1.4       kristaps  194:                }
1.1       kristaps  195:        }
                    196:
1.5       kristaps  197:        if (blk.buf)
                    198:                free(blk.buf);
                    199:        if (ln.buf)
                    200:                free(ln.buf);
1.22      kristaps  201:        if (curp.outfree)
                    202:                (*curp.outfree)(curp.outdata);
1.19      kristaps  203:        if (curp.mdoc)
                    204:                mdoc_free(curp.mdoc);
                    205:        if (curp.man)
                    206:                man_free(curp.man);
1.1       kristaps  207:
                    208:        return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
                    209: }
                    210:
                    211:
                    212: __dead static void
                    213: version(void)
                    214: {
                    215:
                    216:        (void)printf("%s %s\n", __progname, VERSION);
1.18      kristaps  217:        exit(EXIT_SUCCESS);
1.1       kristaps  218: }
                    219:
                    220:
                    221: __dead static void
                    222: usage(void)
                    223: {
                    224:
1.12      kristaps  225:        (void)fprintf(stderr, "usage: %s [-V] [-foption...] "
1.48      kristaps  226:                        "[-mformat] [-Ooption] [-Toutput] "
                    227:                        "[-Werr...]\n", __progname);
1.18      kristaps  228:        exit(EXIT_FAILURE);
1.1       kristaps  229: }
                    230:
                    231:
1.19      kristaps  232: static struct man *
                    233: man_init(struct curparse *curp)
                    234: {
                    235:        int              pflags;
                    236:        struct man      *man;
                    237:        struct man_cb    mancb;
                    238:
                    239:        mancb.man_err = merr;
1.37      kristaps  240:        mancb.man_warn = mwarn;
1.19      kristaps  241:
1.30      kristaps  242:        /* Defaults from mandoc.1. */
1.27      kristaps  243:
1.33      kristaps  244:        pflags = MAN_IGN_MACRO | MAN_IGN_ESCAPE | MAN_IGN_CHARS;
1.27      kristaps  245:
1.20      kristaps  246:        if (curp->fflags & NO_IGN_MACRO)
                    247:                pflags &= ~MAN_IGN_MACRO;
1.31      kristaps  248:        if (curp->fflags & NO_IGN_CHARS)
                    249:                pflags &= ~MAN_IGN_CHARS;
1.33      kristaps  250:        if (curp->fflags & NO_IGN_ESCAPE)
                    251:                pflags &= ~MAN_IGN_ESCAPE;
1.19      kristaps  252:
                    253:        if (NULL == (man = man_alloc(curp, pflags, &mancb)))
                    254:                warnx("memory exhausted");
                    255:
                    256:        return(man);
                    257: }
                    258:
                    259:
                    260: static struct mdoc *
                    261: mdoc_init(struct curparse *curp)
                    262: {
                    263:        int              pflags;
                    264:        struct mdoc     *mdoc;
                    265:        struct mdoc_cb   mdoccb;
                    266:
                    267:        mdoccb.mdoc_err = merr;
1.37      kristaps  268:        mdoccb.mdoc_warn = mwarn;
1.19      kristaps  269:
1.30      kristaps  270:        /* Defaults from mandoc.1. */
1.27      kristaps  271:
1.24      kristaps  272:        pflags = MDOC_IGN_MACRO | MDOC_IGN_ESCAPE | MDOC_IGN_CHARS;
1.19      kristaps  273:
                    274:        if (curp->fflags & IGN_SCOPE)
                    275:                pflags |= MDOC_IGN_SCOPE;
1.24      kristaps  276:        if (curp->fflags & NO_IGN_ESCAPE)
                    277:                pflags &= ~MDOC_IGN_ESCAPE;
                    278:        if (curp->fflags & NO_IGN_MACRO)
                    279:                pflags &= ~MDOC_IGN_MACRO;
                    280:        if (curp->fflags & NO_IGN_CHARS)
                    281:                pflags &= ~MDOC_IGN_CHARS;
1.19      kristaps  282:
                    283:        if (NULL == (mdoc = mdoc_alloc(curp, pflags, &mdoccb)))
1.24      kristaps  284:                warnx("memory exhausted");
1.19      kristaps  285:
                    286:        return(mdoc);
                    287: }
                    288:
                    289:
                    290: static int
                    291: ffile(struct buf *blk, struct buf *ln,
                    292:                const char *file, struct curparse *curp)
1.1       kristaps  293: {
1.19      kristaps  294:        int              c;
1.1       kristaps  295:
1.19      kristaps  296:        curp->file = file;
                    297:        if (-1 == (curp->fd = open(curp->file, O_RDONLY, 0))) {
                    298:                warn("%s", curp->file);
1.39      kristaps  299:                return(-1);
1.1       kristaps  300:        }
                    301:
1.19      kristaps  302:        c = fdesc(blk, ln, curp);
1.1       kristaps  303:
1.19      kristaps  304:        if (-1 == close(curp->fd))
                    305:                warn("%s", curp->file);
1.1       kristaps  306:
                    307:        return(c);
                    308: }
                    309:
                    310:
                    311: static int
1.19      kristaps  312: fdesc(struct buf *blk, struct buf *ln, struct curparse *curp)
1.1       kristaps  313: {
                    314:        size_t           sz;
                    315:        ssize_t          ssz;
                    316:        struct stat      st;
1.29      kristaps  317:        int              j, i, pos, lnn, comment;
1.19      kristaps  318:        struct man      *man;
                    319:        struct mdoc     *mdoc;
1.10      kristaps  320:
1.19      kristaps  321:        sz = BUFSIZ;
                    322:        man = NULL;
                    323:        mdoc = NULL;
1.1       kristaps  324:
                    325:        /*
1.19      kristaps  326:         * Two buffers: ln and buf.  buf is the input buffer optimised
                    327:         * here for each file's block size.  ln is a line buffer.  Both
1.1       kristaps  328:         * growable, hence passed in by ptr-ptr.
                    329:         */
                    330:
1.19      kristaps  331:        if (-1 == fstat(curp->fd, &st))
1.32      kristaps  332:                warn("%s", curp->file);
1.6       kristaps  333:        else if ((size_t)st.st_blksize > sz)
                    334:                sz = st.st_blksize;
1.1       kristaps  335:
1.5       kristaps  336:        if (sz > blk->sz) {
                    337:                blk->buf = realloc(blk->buf, sz);
1.19      kristaps  338:                if (NULL == blk->buf) {
                    339:                        warn("realloc");
1.39      kristaps  340:                        return(-1);
1.19      kristaps  341:                }
1.5       kristaps  342:                blk->sz = sz;
1.1       kristaps  343:        }
                    344:
1.19      kristaps  345:        /* Fill buf with file blocksize. */
1.1       kristaps  346:
1.29      kristaps  347:        for (lnn = pos = comment = 0; ; ) {
1.19      kristaps  348:                if (-1 == (ssz = read(curp->fd, blk->buf, sz))) {
                    349:                        warn("%s", curp->file);
1.39      kristaps  350:                        return(-1);
1.1       kristaps  351:                } else if (0 == ssz)
                    352:                        break;
                    353:
1.19      kristaps  354:                /* Parse the read block into partial or full lines. */
                    355:
1.1       kristaps  356:                for (i = 0; i < (int)ssz; i++) {
1.5       kristaps  357:                        if (pos >= (int)ln->sz) {
                    358:                                ln->sz += 256; /* Step-size. */
                    359:                                ln->buf = realloc(ln->buf, ln->sz);
1.19      kristaps  360:                                if (NULL == ln->buf) {
                    361:                                        warn("realloc");
1.39      kristaps  362:                                        return(-1);
1.19      kristaps  363:                                }
1.1       kristaps  364:                        }
                    365:
1.5       kristaps  366:                        if ('\n' != blk->buf[i]) {
1.29      kristaps  367:                                if (comment)
                    368:                                        continue;
1.5       kristaps  369:                                ln->buf[pos++] = blk->buf[i];
1.29      kristaps  370:
                    371:                                /* Handle in-line `\"' comments. */
                    372:
                    373:                                if (1 == pos || '\"' != ln->buf[pos - 1])
                    374:                                        continue;
                    375:
                    376:                                for (j = pos - 2; j >= 0; j--)
                    377:                                        if ('\\' != ln->buf[j])
                    378:                                                break;
                    379:
                    380:                                if ( ! ((pos - 2 - j) % 2))
                    381:                                        continue;
                    382:
                    383:                                comment = 1;
                    384:                                pos -= 2;
1.1       kristaps  385:                                continue;
1.29      kristaps  386:                        }
1.1       kristaps  387:
1.29      kristaps  388:                        /* Handle escaped `\\n' newlines. */
1.1       kristaps  389:
1.29      kristaps  390:                        if (pos > 0 && 0 == comment &&
                    391:                                        '\\' == ln->buf[pos - 1]) {
1.1       kristaps  392:                                for (j = pos - 1; j >= 0; j--)
1.5       kristaps  393:                                        if ('\\' != ln->buf[j])
1.1       kristaps  394:                                                break;
                    395:                                if ( ! ((pos - j) % 2)) {
                    396:                                        pos--;
                    397:                                        lnn++;
                    398:                                        continue;
                    399:                                }
                    400:                        }
                    401:
1.5       kristaps  402:                        ln->buf[pos] = 0;
1.19      kristaps  403:                        lnn++;
1.29      kristaps  404:
                    405:                        /* If unset, assign parser in pset(). */
1.19      kristaps  406:
                    407:                        if ( ! (man || mdoc) && ! pset(ln->buf,
                    408:                                                pos, curp, &man, &mdoc))
1.39      kristaps  409:                                return(-1);
1.19      kristaps  410:
1.29      kristaps  411:                        pos = comment = 0;
1.19      kristaps  412:
1.30      kristaps  413:                        /* Pass down into parsers. */
                    414:
1.10      kristaps  415:                        if (man && ! man_parseln(man, lnn, ln->buf))
1.1       kristaps  416:                                return(0);
1.19      kristaps  417:                        if (mdoc && ! mdoc_parseln(mdoc, lnn, ln->buf))
                    418:                                return(0);
1.1       kristaps  419:                }
                    420:        }
                    421:
1.29      kristaps  422:        /* NOTE a parser may not have been assigned, yet. */
1.19      kristaps  423:
1.22      kristaps  424:        if ( ! (man || mdoc)) {
1.41      kristaps  425:                (void)fprintf(stderr, "%s: not a manual\n",
                    426:                                curp->file);
1.22      kristaps  427:                return(0);
                    428:        }
                    429:
                    430:        if (mdoc && ! mdoc_endparse(mdoc))
                    431:                return(0);
                    432:        if (man && ! man_endparse(man))
                    433:                return(0);
1.19      kristaps  434:
1.29      kristaps  435:        /* If unset, allocate output dev now (if applicable). */
1.22      kristaps  436:
                    437:        if ( ! (curp->outman && curp->outmdoc)) {
                    438:                switch (curp->outtype) {
1.43      kristaps  439:                case (OUTT_HTML):
1.44      kristaps  440:                        curp->outdata = html_alloc(curp->outopts);
1.43      kristaps  441:                        curp->outman = html_man;
                    442:                        curp->outmdoc = html_mdoc;
                    443:                        curp->outfree = html_free;
                    444:                        break;
1.22      kristaps  445:                case (OUTT_TREE):
                    446:                        curp->outman = tree_man;
                    447:                        curp->outmdoc = tree_mdoc;
                    448:                        break;
                    449:                case (OUTT_LINT):
                    450:                        break;
                    451:                default:
                    452:                        curp->outdata = ascii_alloc();
                    453:                        curp->outman = terminal_man;
                    454:                        curp->outmdoc = terminal_mdoc;
                    455:                        curp->outfree = terminal_free;
                    456:                        break;
                    457:                }
                    458:        }
                    459:
                    460:        /* Execute the out device, if it exists. */
                    461:
                    462:        if (man && curp->outman)
1.42      kristaps  463:                (*curp->outman)(curp->outdata, man);
1.22      kristaps  464:        if (mdoc && curp->outmdoc)
1.42      kristaps  465:                (*curp->outmdoc)(curp->outdata, mdoc);
1.22      kristaps  466:
                    467:        return(1);
1.19      kristaps  468: }
                    469:
                    470:
                    471: static int
1.22      kristaps  472: pset(const char *buf, int pos, struct curparse *curp,
1.19      kristaps  473:                struct man **man, struct mdoc **mdoc)
                    474: {
1.29      kristaps  475:        int              i;
1.19      kristaps  476:
                    477:        /*
                    478:         * Try to intuit which kind of manual parser should be used.  If
                    479:         * passed in by command-line (-man, -mdoc), then use that
                    480:         * explicitly.  If passed as -mandoc, then try to guess from the
1.30      kristaps  481:         * line: either skip dot-lines, use -mdoc when finding `.Dt', or
1.19      kristaps  482:         * default to -man, which is more lenient.
                    483:         */
                    484:
1.29      kristaps  485:        if (buf[0] == '.') {
                    486:                for (i = 1; buf[i]; i++)
                    487:                        if (' ' != buf[i] && '\t' != buf[i])
                    488:                                break;
                    489:                if (0 == buf[i])
                    490:                        return(1);
                    491:        }
1.10      kristaps  492:
1.19      kristaps  493:        switch (curp->inttype) {
                    494:        case (INTT_MDOC):
                    495:                if (NULL == curp->mdoc)
                    496:                        curp->mdoc = mdoc_init(curp);
                    497:                if (NULL == (*mdoc = curp->mdoc))
                    498:                        return(0);
1.22      kristaps  499:                curp->lastmdoc = *mdoc;
1.19      kristaps  500:                return(1);
                    501:        case (INTT_MAN):
                    502:                if (NULL == curp->man)
                    503:                        curp->man = man_init(curp);
                    504:                if (NULL == (*man = curp->man))
                    505:                        return(0);
1.22      kristaps  506:                curp->lastman = *man;
1.19      kristaps  507:                return(1);
                    508:        default:
                    509:                break;
                    510:        }
                    511:
                    512:        if (pos >= 3 && 0 == memcmp(buf, ".Dd", 3))  {
                    513:                if (NULL == curp->mdoc)
                    514:                        curp->mdoc = mdoc_init(curp);
                    515:                if (NULL == (*mdoc = curp->mdoc))
                    516:                        return(0);
1.22      kristaps  517:                curp->lastmdoc = *mdoc;
1.19      kristaps  518:                return(1);
                    519:        }
                    520:
                    521:        if (NULL == curp->man)
                    522:                curp->man = man_init(curp);
                    523:        if (NULL == (*man = curp->man))
                    524:                return(0);
1.22      kristaps  525:        curp->lastman = *man;
1.19      kristaps  526:        return(1);
1.10      kristaps  527: }
                    528:
                    529:
                    530: static int
                    531: moptions(enum intt *tflags, char *arg)
                    532: {
                    533:
1.17      kristaps  534:        if (0 == strcmp(arg, "doc"))
1.10      kristaps  535:                *tflags = INTT_MDOC;
1.19      kristaps  536:        else if (0 == strcmp(arg, "andoc"))
                    537:                *tflags = INTT_AUTO;
1.17      kristaps  538:        else if (0 == strcmp(arg, "an"))
1.10      kristaps  539:                *tflags = INTT_MAN;
                    540:        else {
                    541:                warnx("bad argument: -m%s", arg);
                    542:                return(0);
                    543:        }
                    544:
                    545:        return(1);
1.1       kristaps  546: }
                    547:
                    548:
                    549: static int
                    550: toptions(enum outt *tflags, char *arg)
                    551: {
                    552:
                    553:        if (0 == strcmp(arg, "ascii"))
                    554:                *tflags = OUTT_ASCII;
                    555:        else if (0 == strcmp(arg, "lint"))
                    556:                *tflags = OUTT_LINT;
                    557:        else if (0 == strcmp(arg, "tree"))
                    558:                *tflags = OUTT_TREE;
1.43      kristaps  559:        else if (0 == strcmp(arg, "html"))
                    560:                *tflags = OUTT_HTML;
1.1       kristaps  561:        else {
                    562:                warnx("bad argument: -T%s", arg);
                    563:                return(0);
                    564:        }
                    565:
                    566:        return(1);
                    567: }
                    568:
                    569:
                    570: static int
                    571: foptions(int *fflags, char *arg)
                    572: {
1.34      kristaps  573:        char            *v, *o;
1.50    ! kristaps  574:        const char      *toks[8];
1.1       kristaps  575:
                    576:        toks[0] = "ign-scope";
1.24      kristaps  577:        toks[1] = "no-ign-escape";
                    578:        toks[2] = "no-ign-macro";
                    579:        toks[3] = "no-ign-chars";
1.39      kristaps  580:        toks[4] = "ign-errors";
                    581:        toks[5] = "strict";
1.50    ! kristaps  582:        toks[6] = "ign-escape";
        !           583:        toks[7] = NULL;
1.1       kristaps  584:
1.34      kristaps  585:        while (*arg) {
                    586:                o = arg;
1.45      kristaps  587:                switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1       kristaps  588:                case (0):
1.15      kristaps  589:                        *fflags |= IGN_SCOPE;
1.1       kristaps  590:                        break;
                    591:                case (1):
1.24      kristaps  592:                        *fflags |= NO_IGN_ESCAPE;
1.1       kristaps  593:                        break;
                    594:                case (2):
1.24      kristaps  595:                        *fflags |= NO_IGN_MACRO;
1.1       kristaps  596:                        break;
1.20      kristaps  597:                case (3):
1.24      kristaps  598:                        *fflags |= NO_IGN_CHARS;
                    599:                        break;
                    600:                case (4):
1.39      kristaps  601:                        *fflags |= IGN_ERRORS;
                    602:                        break;
                    603:                case (5):
1.24      kristaps  604:                        *fflags |= NO_IGN_ESCAPE |
                    605:                                   NO_IGN_MACRO | NO_IGN_CHARS;
1.20      kristaps  606:                        break;
1.50    ! kristaps  607:                case (6):
        !           608:                        *fflags &= ~NO_IGN_ESCAPE;
        !           609:                        break;
1.1       kristaps  610:                default:
1.34      kristaps  611:                        warnx("bad argument: -f%s", o);
1.1       kristaps  612:                        return(0);
                    613:                }
1.34      kristaps  614:        }
1.1       kristaps  615:
                    616:        return(1);
                    617: }
                    618:
                    619:
                    620: static int
                    621: woptions(int *wflags, char *arg)
                    622: {
1.34      kristaps  623:        char            *v, *o;
1.45      kristaps  624:        const char      *toks[3];
1.1       kristaps  625:
                    626:        toks[0] = "all";
1.36      kristaps  627:        toks[1] = "error";
                    628:        toks[2] = NULL;
1.1       kristaps  629:
1.34      kristaps  630:        while (*arg) {
                    631:                o = arg;
1.45      kristaps  632:                switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1       kristaps  633:                case (0):
                    634:                        *wflags |= WARN_WALL;
                    635:                        break;
                    636:                case (1):
                    637:                        *wflags |= WARN_WERR;
                    638:                        break;
                    639:                default:
1.34      kristaps  640:                        warnx("bad argument: -W%s", o);
1.1       kristaps  641:                        return(0);
                    642:                }
1.34      kristaps  643:        }
1.1       kristaps  644:
                    645:        return(1);
                    646: }
                    647:
                    648:
1.2       kristaps  649: /* ARGSUSED */
1.1       kristaps  650: static int
                    651: merr(void *arg, int line, int col, const char *msg)
                    652: {
1.5       kristaps  653:        struct curparse *curp;
                    654:
                    655:        curp = (struct curparse *)arg;
1.36      kristaps  656:
1.40      kristaps  657:        (void)fprintf(stderr, "%s:%d:%d: error: %s\n",
                    658:                        curp->file, line, col + 1, msg);
1.27      kristaps  659:
1.1       kristaps  660:        return(0);
                    661: }
                    662:
                    663:
                    664: static int
1.37      kristaps  665: mwarn(void *arg, int line, int col, const char *msg)
1.1       kristaps  666: {
1.5       kristaps  667:        struct curparse *curp;
1.1       kristaps  668:
1.5       kristaps  669:        curp = (struct curparse *)arg;
1.1       kristaps  670:
1.36      kristaps  671:        if ( ! (curp->wflags & WARN_WALL))
                    672:                return(1);
                    673:
1.40      kristaps  674:        (void)fprintf(stderr, "%s:%d:%d: warning: %s\n",
                    675:                        curp->file, line, col + 1, msg);
1.1       kristaps  676:
1.5       kristaps  677:        if ( ! (curp->wflags & WARN_WERR))
1.1       kristaps  678:                return(1);
1.27      kristaps  679:
1.1       kristaps  680:        return(0);
                    681: }
                    682:

CVSweb