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

Annotation of mandoc/main.c, Revision 1.53

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

CVSweb