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

Annotation of mandoc/main.c, Revision 1.54

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

CVSweb