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

Annotation of mandoc/main.c, Revision 1.59

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

CVSweb