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

Annotation of mandoc/main.c, Revision 1.58

1.58    ! kristaps    1: /*     $Id: main.c,v 1.57 2009/11/02 08:29: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:  */
1.58    ! kristaps   17: #ifdef HAVE_CONFIG_H
        !            18: #include "config.h"
        !            19: #endif
        !            20:
1.1       kristaps   21: #include <sys/stat.h>
                     22:
                     23: #include <assert.h>
                     24: #include <fcntl.h>
                     25: #include <stdio.h>
1.45      kristaps   26: #include <stdint.h>
1.1       kristaps   27: #include <stdlib.h>
                     28: #include <string.h>
                     29: #include <unistd.h>
                     30:
                     31: #include "mdoc.h"
1.10      kristaps   32: #include "man.h"
1.46      kristaps   33: #include "main.h"
1.1       kristaps   34:
1.45      kristaps   35: #define        UNCONST(a)      ((void *)(uintptr_t)(const void *)(a))
                     36:
1.55      kristaps   37: /* FIXME: Intel's compiler?  LLVM?  pcc?  */
                     38:
                     39: #if !defined(__GNUC__) || (__GNUC__ < 2)
1.56      kristaps   40: # if !defined(lint)
                     41: #  define __attribute__(x)
                     42: # endif
1.55      kristaps   43: #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
1.16      kristaps   44:
1.42      kristaps   45: typedef        void            (*out_mdoc)(void *, const struct mdoc *);
                     46: typedef        void            (*out_man)(void *, const struct man *);
1.22      kristaps   47: typedef        void            (*out_free)(void *);
                     48:
1.5       kristaps   49: struct buf {
                     50:        char             *buf;
                     51:        size_t            sz;
                     52: };
                     53:
1.19      kristaps   54: enum   intt {
                     55:        INTT_AUTO,
                     56:        INTT_MDOC,
                     57:        INTT_MAN
                     58: };
                     59:
                     60: enum   outt {
                     61:        OUTT_ASCII = 0,
                     62:        OUTT_TREE,
1.43      kristaps   63:        OUTT_HTML,
1.19      kristaps   64:        OUTT_LINT
                     65: };
                     66:
1.5       kristaps   67: struct curparse {
1.22      kristaps   68:        const char       *file;         /* Current parse. */
                     69:        int               fd;           /* Current parse. */
1.5       kristaps   70:        int               wflags;
1.36      kristaps   71: #define        WARN_WALL        (1 << 0)       /* All-warnings mask. */
1.1       kristaps   72: #define        WARN_WERR        (1 << 2)       /* Warnings->errors. */
1.23      kristaps   73:        int               fflags;
                     74: #define        IGN_SCOPE        (1 << 0)       /* Ignore scope errors. */
1.24      kristaps   75: #define        NO_IGN_ESCAPE    (1 << 1)       /* Don't ignore bad escapes. */
                     76: #define        NO_IGN_MACRO     (1 << 2)       /* Don't ignore bad macros. */
                     77: #define        NO_IGN_CHARS     (1 << 3)       /* Don't ignore bad chars. */
1.39      kristaps   78: #define        IGN_ERRORS       (1 << 4)       /* Ignore failed parse. */
1.29      kristaps   79:        enum intt         inttype;      /* Input parsers... */
1.19      kristaps   80:        struct man       *man;
1.22      kristaps   81:        struct man       *lastman;
1.19      kristaps   82:        struct mdoc      *mdoc;
1.22      kristaps   83:        struct mdoc      *lastmdoc;
1.29      kristaps   84:        enum outt         outtype;      /* Output devices... */
1.22      kristaps   85:        out_mdoc          outmdoc;
                     86:        out_man           outman;
                     87:        out_free          outfree;
                     88:        void             *outdata;
1.49      kristaps   89:        char              outopts[BUFSIZ];
1.5       kristaps   90: };
1.1       kristaps   91:
                     92: static int               foptions(int *, char *);
                     93: static int               toptions(enum outt *, char *);
1.10      kristaps   94: static int               moptions(enum intt *, char *);
1.1       kristaps   95: static int               woptions(int *, char *);
                     96: static int               merr(void *, int, int, const char *);
1.37      kristaps   97: static int               mwarn(void *, int, int, const char *);
1.19      kristaps   98: static int               ffile(struct buf *, struct buf *,
                     99:                                const char *, struct curparse *);
1.5       kristaps  100: static int               fdesc(struct buf *, struct buf *,
1.19      kristaps  101:                                struct curparse *);
1.22      kristaps  102: static int               pset(const char *, int, struct curparse *,
1.19      kristaps  103:                                struct man **, struct mdoc **);
                    104: static struct man       *man_init(struct curparse *);
                    105: static struct mdoc      *mdoc_init(struct curparse *);
1.55      kristaps  106: static void              version(void) __attribute__((noreturn));
                    107: static void              usage(void) __attribute__((noreturn));
1.1       kristaps  108:
1.54      kristaps  109: static const char       *progname;
1.22      kristaps  110:
1.1       kristaps  111:
                    112: int
                    113: main(int argc, char *argv[])
                    114: {
1.19      kristaps  115:        int              c, rc;
1.5       kristaps  116:        struct buf       ln, blk;
                    117:        struct curparse  curp;
1.1       kristaps  118:
1.54      kristaps  119:        progname = strrchr(argv[0], '/');
                    120:        if (progname == NULL)
                    121:                progname = argv[0];
                    122:        else
                    123:                ++progname;
                    124:
1.51      kristaps  125:        memset(&curp, 0, sizeof(struct curparse));
1.5       kristaps  126:
1.19      kristaps  127:        curp.inttype = INTT_AUTO;
1.22      kristaps  128:        curp.outtype = OUTT_ASCII;
1.19      kristaps  129:
1.1       kristaps  130:        /* LINTED */
1.47      kristaps  131:        while (-1 != (c = getopt(argc, argv, "f:m:O:T:VW:")))
1.1       kristaps  132:                switch (c) {
                    133:                case ('f'):
1.19      kristaps  134:                        if ( ! foptions(&curp.fflags, optarg))
1.32      kristaps  135:                                return(EXIT_FAILURE);
1.1       kristaps  136:                        break;
1.10      kristaps  137:                case ('m'):
1.19      kristaps  138:                        if ( ! moptions(&curp.inttype, optarg))
1.32      kristaps  139:                                return(EXIT_FAILURE);
1.10      kristaps  140:                        break;
1.48      kristaps  141:                case ('O'):
1.49      kristaps  142:                        (void)strlcat(curp.outopts, optarg, BUFSIZ);
                    143:                        (void)strlcat(curp.outopts, ",", BUFSIZ);
1.44      kristaps  144:                        break;
1.1       kristaps  145:                case ('T'):
1.22      kristaps  146:                        if ( ! toptions(&curp.outtype, optarg))
1.32      kristaps  147:                                return(EXIT_FAILURE);
1.1       kristaps  148:                        break;
                    149:                case ('W'):
1.5       kristaps  150:                        if ( ! woptions(&curp.wflags, optarg))
1.32      kristaps  151:                                return(EXIT_FAILURE);
1.1       kristaps  152:                        break;
                    153:                case ('V'):
                    154:                        version();
                    155:                        /* NOTREACHED */
                    156:                default:
                    157:                        usage();
                    158:                        /* NOTREACHED */
                    159:                }
                    160:
                    161:        argc -= optind;
                    162:        argv += optind;
                    163:
1.51      kristaps  164:        memset(&ln, 0, sizeof(struct buf));
                    165:        memset(&blk, 0, sizeof(struct buf));
1.1       kristaps  166:
1.22      kristaps  167:        rc = 1;
1.1       kristaps  168:
1.30      kristaps  169:        if (NULL == *argv) {
                    170:                curp.file = "<stdin>";
                    171:                curp.fd = STDIN_FILENO;
1.39      kristaps  172:
                    173:                c = fdesc(&blk, &ln, &curp);
                    174:                if ( ! (IGN_ERRORS & curp.fflags))
                    175:                        rc = 1 == c ? 1 : 0;
                    176:                else
                    177:                        rc = -1 == c ? 0 : 1;
1.30      kristaps  178:        }
1.22      kristaps  179:
                    180:        while (rc && *argv) {
1.39      kristaps  181:                c = ffile(&blk, &ln, *argv, &curp);
                    182:                if ( ! (IGN_ERRORS & curp.fflags))
                    183:                        rc = 1 == c ? 1 : 0;
                    184:                else
                    185:                        rc = -1 == c ? 0 : 1;
                    186:
1.22      kristaps  187:                argv++;
                    188:                if (*argv && rc) {
                    189:                        if (curp.lastman)
1.52      kristaps  190:                                man_reset(curp.lastman);
1.22      kristaps  191:                        if (curp.lastmdoc)
1.52      kristaps  192:                                mdoc_reset(curp.lastmdoc);
1.22      kristaps  193:                        curp.lastman = NULL;
                    194:                        curp.lastmdoc = NULL;
1.4       kristaps  195:                }
1.1       kristaps  196:        }
                    197:
1.5       kristaps  198:        if (blk.buf)
                    199:                free(blk.buf);
                    200:        if (ln.buf)
                    201:                free(ln.buf);
1.22      kristaps  202:        if (curp.outfree)
                    203:                (*curp.outfree)(curp.outdata);
1.19      kristaps  204:        if (curp.mdoc)
                    205:                mdoc_free(curp.mdoc);
                    206:        if (curp.man)
                    207:                man_free(curp.man);
1.1       kristaps  208:
                    209:        return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
                    210: }
                    211:
                    212:
1.55      kristaps  213: static void
1.1       kristaps  214: version(void)
                    215: {
                    216:
1.54      kristaps  217:        (void)printf("%s %s\n", progname, VERSION);
1.18      kristaps  218:        exit(EXIT_SUCCESS);
1.1       kristaps  219: }
                    220:
                    221:
1.55      kristaps  222: static void
1.1       kristaps  223: usage(void)
                    224: {
                    225:
1.12      kristaps  226:        (void)fprintf(stderr, "usage: %s [-V] [-foption...] "
1.48      kristaps  227:                        "[-mformat] [-Ooption] [-Toutput] "
1.54      kristaps  228:                        "[-Werr...]\n", progname);
1.18      kristaps  229:        exit(EXIT_FAILURE);
1.1       kristaps  230: }
                    231:
                    232:
1.19      kristaps  233: static struct man *
                    234: man_init(struct curparse *curp)
                    235: {
                    236:        int              pflags;
                    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:
1.52      kristaps  253:        return(man_alloc(curp, pflags, &mancb));
1.19      kristaps  254: }
                    255:
                    256:
                    257: static struct mdoc *
                    258: mdoc_init(struct curparse *curp)
                    259: {
                    260:        int              pflags;
                    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:
1.52      kristaps  279:        return(mdoc_alloc(curp, pflags, &mdoccb));
1.19      kristaps  280: }
                    281:
                    282:
                    283: static int
                    284: ffile(struct buf *blk, struct buf *ln,
                    285:                const char *file, struct curparse *curp)
1.1       kristaps  286: {
1.19      kristaps  287:        int              c;
1.1       kristaps  288:
1.19      kristaps  289:        curp->file = file;
                    290:        if (-1 == (curp->fd = open(curp->file, O_RDONLY, 0))) {
1.53      kristaps  291:                perror(curp->file);
1.39      kristaps  292:                return(-1);
1.1       kristaps  293:        }
                    294:
1.19      kristaps  295:        c = fdesc(blk, ln, curp);
1.1       kristaps  296:
1.19      kristaps  297:        if (-1 == close(curp->fd))
1.53      kristaps  298:                perror(curp->file);
1.1       kristaps  299:
                    300:        return(c);
                    301: }
                    302:
                    303:
                    304: static int
1.19      kristaps  305: fdesc(struct buf *blk, struct buf *ln, struct curparse *curp)
1.1       kristaps  306: {
                    307:        size_t           sz;
                    308:        ssize_t          ssz;
                    309:        struct stat      st;
1.29      kristaps  310:        int              j, i, pos, lnn, comment;
1.19      kristaps  311:        struct man      *man;
                    312:        struct mdoc     *mdoc;
1.10      kristaps  313:
1.19      kristaps  314:        sz = BUFSIZ;
                    315:        man = NULL;
                    316:        mdoc = NULL;
1.1       kristaps  317:
                    318:        /*
1.19      kristaps  319:         * Two buffers: ln and buf.  buf is the input buffer optimised
                    320:         * here for each file's block size.  ln is a line buffer.  Both
1.1       kristaps  321:         * growable, hence passed in by ptr-ptr.
                    322:         */
                    323:
1.19      kristaps  324:        if (-1 == fstat(curp->fd, &st))
1.53      kristaps  325:                perror(curp->file);
1.6       kristaps  326:        else if ((size_t)st.st_blksize > sz)
                    327:                sz = st.st_blksize;
1.1       kristaps  328:
1.5       kristaps  329:        if (sz > blk->sz) {
                    330:                blk->buf = realloc(blk->buf, sz);
1.19      kristaps  331:                if (NULL == blk->buf) {
1.53      kristaps  332:                        perror(NULL);
                    333:                        exit(EXIT_FAILURE);
1.19      kristaps  334:                }
1.5       kristaps  335:                blk->sz = sz;
1.1       kristaps  336:        }
                    337:
1.19      kristaps  338:        /* Fill buf with file blocksize. */
1.1       kristaps  339:
1.29      kristaps  340:        for (lnn = pos = comment = 0; ; ) {
1.19      kristaps  341:                if (-1 == (ssz = read(curp->fd, blk->buf, sz))) {
1.53      kristaps  342:                        perror(curp->file);
1.39      kristaps  343:                        return(-1);
1.1       kristaps  344:                } else if (0 == ssz)
                    345:                        break;
                    346:
1.19      kristaps  347:                /* Parse the read block into partial or full lines. */
                    348:
1.1       kristaps  349:                for (i = 0; i < (int)ssz; i++) {
1.5       kristaps  350:                        if (pos >= (int)ln->sz) {
                    351:                                ln->sz += 256; /* Step-size. */
                    352:                                ln->buf = realloc(ln->buf, ln->sz);
1.19      kristaps  353:                                if (NULL == ln->buf) {
1.53      kristaps  354:                                        perror(NULL);
                    355:                                        return(EXIT_FAILURE);
1.19      kristaps  356:                                }
1.1       kristaps  357:                        }
                    358:
1.5       kristaps  359:                        if ('\n' != blk->buf[i]) {
1.29      kristaps  360:                                if (comment)
                    361:                                        continue;
1.5       kristaps  362:                                ln->buf[pos++] = blk->buf[i];
1.29      kristaps  363:
                    364:                                /* Handle in-line `\"' comments. */
                    365:
                    366:                                if (1 == pos || '\"' != ln->buf[pos - 1])
                    367:                                        continue;
                    368:
                    369:                                for (j = pos - 2; j >= 0; j--)
                    370:                                        if ('\\' != ln->buf[j])
                    371:                                                break;
                    372:
                    373:                                if ( ! ((pos - 2 - j) % 2))
                    374:                                        continue;
                    375:
                    376:                                comment = 1;
                    377:                                pos -= 2;
1.1       kristaps  378:                                continue;
1.29      kristaps  379:                        }
1.1       kristaps  380:
1.29      kristaps  381:                        /* Handle escaped `\\n' newlines. */
1.1       kristaps  382:
1.29      kristaps  383:                        if (pos > 0 && 0 == comment &&
                    384:                                        '\\' == ln->buf[pos - 1]) {
1.1       kristaps  385:                                for (j = pos - 1; j >= 0; j--)
1.5       kristaps  386:                                        if ('\\' != ln->buf[j])
1.1       kristaps  387:                                                break;
                    388:                                if ( ! ((pos - j) % 2)) {
                    389:                                        pos--;
                    390:                                        lnn++;
                    391:                                        continue;
                    392:                                }
                    393:                        }
                    394:
1.5       kristaps  395:                        ln->buf[pos] = 0;
1.19      kristaps  396:                        lnn++;
1.29      kristaps  397:
                    398:                        /* If unset, assign parser in pset(). */
1.19      kristaps  399:
                    400:                        if ( ! (man || mdoc) && ! pset(ln->buf,
                    401:                                                pos, curp, &man, &mdoc))
1.39      kristaps  402:                                return(-1);
1.19      kristaps  403:
1.29      kristaps  404:                        pos = comment = 0;
1.19      kristaps  405:
1.30      kristaps  406:                        /* Pass down into parsers. */
                    407:
1.10      kristaps  408:                        if (man && ! man_parseln(man, lnn, ln->buf))
1.1       kristaps  409:                                return(0);
1.19      kristaps  410:                        if (mdoc && ! mdoc_parseln(mdoc, lnn, ln->buf))
                    411:                                return(0);
1.1       kristaps  412:                }
                    413:        }
                    414:
1.29      kristaps  415:        /* NOTE a parser may not have been assigned, yet. */
1.19      kristaps  416:
1.22      kristaps  417:        if ( ! (man || mdoc)) {
1.53      kristaps  418:                fprintf(stderr, "%s: Not a manual\n", curp->file);
1.22      kristaps  419:                return(0);
                    420:        }
                    421:
                    422:        if (mdoc && ! mdoc_endparse(mdoc))
                    423:                return(0);
                    424:        if (man && ! man_endparse(man))
                    425:                return(0);
1.19      kristaps  426:
1.29      kristaps  427:        /* If unset, allocate output dev now (if applicable). */
1.22      kristaps  428:
                    429:        if ( ! (curp->outman && curp->outmdoc)) {
                    430:                switch (curp->outtype) {
1.43      kristaps  431:                case (OUTT_HTML):
1.44      kristaps  432:                        curp->outdata = html_alloc(curp->outopts);
1.43      kristaps  433:                        curp->outman = html_man;
                    434:                        curp->outmdoc = html_mdoc;
                    435:                        curp->outfree = html_free;
                    436:                        break;
1.22      kristaps  437:                case (OUTT_TREE):
                    438:                        curp->outman = tree_man;
                    439:                        curp->outmdoc = tree_mdoc;
                    440:                        break;
                    441:                case (OUTT_LINT):
                    442:                        break;
                    443:                default:
                    444:                        curp->outdata = ascii_alloc();
                    445:                        curp->outman = terminal_man;
                    446:                        curp->outmdoc = terminal_mdoc;
                    447:                        curp->outfree = terminal_free;
                    448:                        break;
                    449:                }
                    450:        }
                    451:
                    452:        /* Execute the out device, if it exists. */
                    453:
                    454:        if (man && curp->outman)
1.42      kristaps  455:                (*curp->outman)(curp->outdata, man);
1.22      kristaps  456:        if (mdoc && curp->outmdoc)
1.42      kristaps  457:                (*curp->outmdoc)(curp->outdata, mdoc);
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 {
1.57      kristaps  533:                fprintf(stderr, "%s: Bad argument\n", arg);
1.10      kristaps  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;
1.43      kristaps  551:        else if (0 == strcmp(arg, "html"))
                    552:                *tflags = OUTT_HTML;
1.1       kristaps  553:        else {
1.57      kristaps  554:                fprintf(stderr, "%s: Bad argument\n", arg);
1.1       kristaps  555:                return(0);
                    556:        }
                    557:
                    558:        return(1);
                    559: }
                    560:
                    561:
                    562: static int
                    563: foptions(int *fflags, char *arg)
                    564: {
1.34      kristaps  565:        char            *v, *o;
1.50      kristaps  566:        const char      *toks[8];
1.1       kristaps  567:
                    568:        toks[0] = "ign-scope";
1.24      kristaps  569:        toks[1] = "no-ign-escape";
                    570:        toks[2] = "no-ign-macro";
                    571:        toks[3] = "no-ign-chars";
1.39      kristaps  572:        toks[4] = "ign-errors";
                    573:        toks[5] = "strict";
1.50      kristaps  574:        toks[6] = "ign-escape";
                    575:        toks[7] = NULL;
1.1       kristaps  576:
1.34      kristaps  577:        while (*arg) {
                    578:                o = arg;
1.45      kristaps  579:                switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1       kristaps  580:                case (0):
1.15      kristaps  581:                        *fflags |= IGN_SCOPE;
1.1       kristaps  582:                        break;
                    583:                case (1):
1.24      kristaps  584:                        *fflags |= NO_IGN_ESCAPE;
1.1       kristaps  585:                        break;
                    586:                case (2):
1.24      kristaps  587:                        *fflags |= NO_IGN_MACRO;
1.1       kristaps  588:                        break;
1.20      kristaps  589:                case (3):
1.24      kristaps  590:                        *fflags |= NO_IGN_CHARS;
                    591:                        break;
                    592:                case (4):
1.39      kristaps  593:                        *fflags |= IGN_ERRORS;
                    594:                        break;
                    595:                case (5):
1.24      kristaps  596:                        *fflags |= NO_IGN_ESCAPE |
                    597:                                   NO_IGN_MACRO | NO_IGN_CHARS;
1.20      kristaps  598:                        break;
1.50      kristaps  599:                case (6):
                    600:                        *fflags &= ~NO_IGN_ESCAPE;
                    601:                        break;
1.1       kristaps  602:                default:
1.57      kristaps  603:                        fprintf(stderr, "%s: Bad argument\n", o);
1.1       kristaps  604:                        return(0);
                    605:                }
1.34      kristaps  606:        }
1.1       kristaps  607:
                    608:        return(1);
                    609: }
                    610:
                    611:
                    612: static int
                    613: woptions(int *wflags, char *arg)
                    614: {
1.34      kristaps  615:        char            *v, *o;
1.45      kristaps  616:        const char      *toks[3];
1.1       kristaps  617:
                    618:        toks[0] = "all";
1.36      kristaps  619:        toks[1] = "error";
                    620:        toks[2] = NULL;
1.1       kristaps  621:
1.34      kristaps  622:        while (*arg) {
                    623:                o = arg;
1.45      kristaps  624:                switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.1       kristaps  625:                case (0):
                    626:                        *wflags |= WARN_WALL;
                    627:                        break;
                    628:                case (1):
                    629:                        *wflags |= WARN_WERR;
                    630:                        break;
                    631:                default:
1.57      kristaps  632:                        fprintf(stderr, "%s: Bad argument\n", o);
1.1       kristaps  633:                        return(0);
                    634:                }
1.34      kristaps  635:        }
1.1       kristaps  636:
                    637:        return(1);
                    638: }
                    639:
                    640:
1.2       kristaps  641: /* ARGSUSED */
1.1       kristaps  642: static int
                    643: merr(void *arg, int line, int col, const char *msg)
                    644: {
1.5       kristaps  645:        struct curparse *curp;
                    646:
                    647:        curp = (struct curparse *)arg;
1.36      kristaps  648:
1.40      kristaps  649:        (void)fprintf(stderr, "%s:%d:%d: error: %s\n",
                    650:                        curp->file, line, col + 1, msg);
1.27      kristaps  651:
1.1       kristaps  652:        return(0);
                    653: }
                    654:
                    655:
                    656: static int
1.37      kristaps  657: mwarn(void *arg, int line, int col, const char *msg)
1.1       kristaps  658: {
1.5       kristaps  659:        struct curparse *curp;
1.1       kristaps  660:
1.5       kristaps  661:        curp = (struct curparse *)arg;
1.1       kristaps  662:
1.36      kristaps  663:        if ( ! (curp->wflags & WARN_WALL))
                    664:                return(1);
                    665:
1.40      kristaps  666:        (void)fprintf(stderr, "%s:%d:%d: warning: %s\n",
                    667:                        curp->file, line, col + 1, msg);
1.1       kristaps  668:
1.5       kristaps  669:        if ( ! (curp->wflags & WARN_WERR))
1.1       kristaps  670:                return(1);
1.27      kristaps  671:
1.1       kristaps  672:        return(0);
                    673: }
                    674:

CVSweb