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

Annotation of mandoc/main.c, Revision 1.184

1.184   ! schwarze    1: /*     $Id: main.c,v 1.183 2014/08/22 03:42:18 schwarze Exp $ */
1.1       kristaps    2: /*
1.183     schwarze    3:  * Copyright (c) 2008-2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.170     schwarze    4:  * Copyright (c) 2010, 2011, 2012, 2014 Ingo Schwarze <schwarze@openbsd.org>
1.169     schwarze    5:  * Copyright (c) 2010 Joerg Sonnenberger <joerg@netbsd.org>
1.1       kristaps    6:  *
                      7:  * Permission to use, copy, modify, and distribute this software for any
1.25      kristaps    8:  * purpose with or without fee is hereby granted, provided that the above
                      9:  * copyright notice and this permission notice appear in all copies.
1.1       kristaps   10:  *
1.25      kristaps   11:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     12:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     13:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     14:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     15:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     16:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     17:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
1.1       kristaps   18:  */
1.58      kristaps   19: #include "config.h"
1.178     schwarze   20:
                     21: #include <sys/types.h>
1.58      kristaps   22:
1.1       kristaps   23: #include <assert.h>
1.183     schwarze   24: #include <errno.h>
1.1       kristaps   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:
1.71      kristaps   31: #include "mandoc.h"
1.172     schwarze   32: #include "mandoc_aux.h"
1.90      kristaps   33: #include "main.h"
1.1       kristaps   34: #include "mdoc.h"
1.10      kristaps   35: #include "man.h"
1.180     schwarze   36: #include "manpath.h"
                     37: #include "mansearch.h"
1.101     joerg      38:
1.55      kristaps   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.181     schwarze   45: enum   outmode {
                     46:        OUTMODE_DEF = 0,
                     47:        OUTMODE_FLN,
                     48:        OUTMODE_LST,
                     49:        OUTMODE_ALL,
                     50:        OUTMODE_INT,
                     51:        OUTMODE_ONE
                     52: };
                     53:
1.42      kristaps   54: typedef        void            (*out_mdoc)(void *, const struct mdoc *);
                     55: typedef        void            (*out_man)(void *, const struct man *);
1.22      kristaps   56: typedef        void            (*out_free)(void *);
                     57:
1.19      kristaps   58: enum   outt {
1.154     kristaps   59:        OUTT_ASCII = 0, /* -Tascii */
1.162     kristaps   60:        OUTT_LOCALE,    /* -Tlocale */
1.163     kristaps   61:        OUTT_UTF8,      /* -Tutf8 */
1.154     kristaps   62:        OUTT_TREE,      /* -Ttree */
1.164     schwarze   63:        OUTT_MAN,       /* -Tman */
1.154     kristaps   64:        OUTT_HTML,      /* -Thtml */
                     65:        OUTT_XHTML,     /* -Txhtml */
                     66:        OUTT_LINT,      /* -Tlint */
                     67:        OUTT_PS,        /* -Tps */
                     68:        OUTT_PDF        /* -Tpdf */
1.19      kristaps   69: };
                     70:
1.5       kristaps   71: struct curparse {
1.154     kristaps   72:        struct mparse    *mp;
1.148     kristaps   73:        enum mandoclevel  wlevel;       /* ignore messages below this */
                     74:        int               wstop;        /* stop after a file with a warning */
1.173     schwarze   75:        enum outt         outtype;      /* which output to use */
1.79      kristaps   76:        out_mdoc          outmdoc;      /* mdoc output ptr */
1.173     schwarze   77:        out_man           outman;       /* man output ptr */
1.79      kristaps   78:        out_free          outfree;      /* free output ptr */
                     79:        void             *outdata;      /* data for output */
                     80:        char              outopts[BUFSIZ]; /* buf of output opts */
                     81: };
                     82:
1.170     schwarze   83: static int               moptions(int *, char *);
1.155     kristaps   84: static void              mmsg(enum mandocerr, enum mandoclevel,
                     85:                                const char *, int, int, const char *);
1.173     schwarze   86: static void              parse(struct curparse *, int,
1.154     kristaps   87:                                const char *, enum mandoclevel *);
1.183     schwarze   88: static void              spawn_pager(void);
1.66      kristaps   89: static int               toptions(struct curparse *, char *);
1.180     schwarze   90: static void              usage(enum argmode) __attribute__((noreturn));
1.55      kristaps   91: static void              version(void) __attribute__((noreturn));
1.103     schwarze   92: static int               woptions(struct curparse *, char *);
1.1       kristaps   93:
1.182     schwarze   94: static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
1.54      kristaps   95: static const char       *progname;
1.1       kristaps   96:
1.173     schwarze   97:
1.1       kristaps   98: int
                     99: main(int argc, char *argv[])
                    100: {
1.5       kristaps  101:        struct curparse  curp;
1.180     schwarze  102:        struct mansearch search;
                    103:        struct manpaths  paths;
                    104:        char            *conf_file, *defpaths, *auxpaths;
                    105:        char            *defos;
                    106: #if HAVE_SQLITE3
                    107:        struct manpage  *res;
1.182     schwarze  108:        char            **auxargv;
                    109:        size_t           isec, i, sz;
                    110:        int              prio, best_prio;
                    111:        char             sec;
1.180     schwarze  112: #endif
                    113:        enum mandoclevel rc;
1.181     schwarze  114:        enum outmode     outmode;
1.180     schwarze  115:        int              show_usage;
1.183     schwarze  116:        int              use_pager;
1.170     schwarze  117:        int              options;
1.180     schwarze  118:        int              c;
1.1       kristaps  119:
1.54      kristaps  120:        progname = strrchr(argv[0], '/');
                    121:        if (progname == NULL)
                    122:                progname = argv[0];
                    123:        else
                    124:                ++progname;
1.179     schwarze  125:
1.180     schwarze  126:        /* Search options. */
                    127:
                    128:        memset(&paths, 0, sizeof(struct manpaths));
                    129:        conf_file = defpaths = auxpaths = NULL;
                    130:
                    131:        memset(&search, 0, sizeof(struct mansearch));
                    132:        search.outkey = "Nd";
                    133:
                    134:        if (strcmp(progname, "man") == 0)
                    135:                search.argmode = ARG_NAME;
                    136:        else if (strncmp(progname, "apropos", 7) == 0)
                    137:                search.argmode = ARG_EXPR;
                    138:        else if (strncmp(progname, "whatis", 6) == 0)
                    139:                search.argmode = ARG_WORD;
                    140:        else
                    141:                search.argmode = ARG_FILE;
                    142:
                    143:        /* Parser and formatter options. */
1.54      kristaps  144:
1.51      kristaps  145:        memset(&curp, 0, sizeof(struct curparse));
1.22      kristaps  146:        curp.outtype = OUTT_ASCII;
1.103     schwarze  147:        curp.wlevel  = MANDOCLEVEL_FATAL;
1.180     schwarze  148:        options = MPARSE_SO;
1.166     schwarze  149:        defos = NULL;
1.19      kristaps  150:
1.183     schwarze  151:        use_pager = 1;
1.180     schwarze  152:        show_usage = 0;
1.181     schwarze  153:        outmode = OUTMODE_DEF;
1.183     schwarze  154:
                    155:        while (-1 != (c = getopt(argc, argv, "aC:cfI:ikM:m:O:S:s:T:VW:w"))) {
1.1       kristaps  156:                switch (c) {
1.181     schwarze  157:                case 'a':
                    158:                        outmode = OUTMODE_ALL;
                    159:                        break;
1.180     schwarze  160:                case 'C':
                    161:                        conf_file = optarg;
                    162:                        break;
1.183     schwarze  163:                case 'c':
                    164:                        use_pager = 0;
                    165:                        break;
1.180     schwarze  166:                case 'f':
                    167:                        search.argmode = ARG_WORD;
                    168:                        break;
1.173     schwarze  169:                case 'I':
1.166     schwarze  170:                        if (strncmp(optarg, "os=", 3)) {
1.173     schwarze  171:                                fprintf(stderr,
1.176     schwarze  172:                                    "%s: -I%s: Bad argument\n",
                    173:                                    progname, optarg);
1.166     schwarze  174:                                return((int)MANDOCLEVEL_BADARG);
                    175:                        }
                    176:                        if (defos) {
1.173     schwarze  177:                                fprintf(stderr,
1.176     schwarze  178:                                    "%s: -I%s: Duplicate argument\n",
                    179:                                    progname, optarg);
1.166     schwarze  180:                                return((int)MANDOCLEVEL_BADARG);
                    181:                        }
                    182:                        defos = mandoc_strdup(optarg + 3);
                    183:                        break;
1.181     schwarze  184:                case 'i':
                    185:                        outmode = OUTMODE_INT;
                    186:                        break;
1.180     schwarze  187:                case 'k':
                    188:                        search.argmode = ARG_EXPR;
                    189:                        break;
                    190:                case 'M':
                    191:                        defpaths = optarg;
                    192:                        break;
1.173     schwarze  193:                case 'm':
1.180     schwarze  194:                        auxpaths = optarg;
1.10      kristaps  195:                        break;
1.173     schwarze  196:                case 'O':
1.180     schwarze  197:                        search.outkey = optarg;
1.49      kristaps  198:                        (void)strlcat(curp.outopts, optarg, BUFSIZ);
                    199:                        (void)strlcat(curp.outopts, ",", BUFSIZ);
1.44      kristaps  200:                        break;
1.180     schwarze  201:                case 'S':
                    202:                        search.arch = optarg;
                    203:                        break;
                    204:                case 's':
                    205:                        search.sec = optarg;
                    206:                        break;
1.173     schwarze  207:                case 'T':
1.60      kristaps  208:                        if ( ! toptions(&curp, optarg))
1.105     kristaps  209:                                return((int)MANDOCLEVEL_BADARG);
1.1       kristaps  210:                        break;
1.173     schwarze  211:                case 'W':
1.103     schwarze  212:                        if ( ! woptions(&curp, optarg))
1.105     kristaps  213:                                return((int)MANDOCLEVEL_BADARG);
1.1       kristaps  214:                        break;
1.181     schwarze  215:                case 'w':
                    216:                        outmode = OUTMODE_FLN;
                    217:                        break;
1.173     schwarze  218:                case 'V':
1.1       kristaps  219:                        version();
                    220:                        /* NOTREACHED */
                    221:                default:
1.180     schwarze  222:                        show_usage = 1;
                    223:                        break;
1.1       kristaps  224:                }
1.180     schwarze  225:        }
                    226:
                    227:        if (show_usage)
                    228:                usage(search.argmode);
                    229:
1.181     schwarze  230:        if (outmode == OUTMODE_DEF) {
                    231:                switch (search.argmode) {
                    232:                case ARG_FILE:
                    233:                        outmode = OUTMODE_ALL;
1.183     schwarze  234:                        use_pager = 0;
1.181     schwarze  235:                        break;
                    236:                case ARG_NAME:
                    237:                        outmode = OUTMODE_ONE;
                    238:                        break;
                    239:                default:
                    240:                        outmode = OUTMODE_LST;
                    241:                        break;
                    242:                }
                    243:        }
                    244:
1.180     schwarze  245:        argc -= optind;
                    246:        argv += optind;
1.182     schwarze  247: #if HAVE_SQLITE3
                    248:        auxargv = NULL;
                    249: #endif
                    250:
                    251:        rc = MANDOCLEVEL_OK;
1.180     schwarze  252:
                    253:        /* man(1), whatis(1), apropos(1) */
                    254:
                    255:        if (search.argmode != ARG_FILE) {
                    256: #if HAVE_SQLITE3
                    257:                if (argc == 0)
                    258:                        usage(search.argmode);
1.182     schwarze  259:
                    260:                /* Access the mandoc database. */
                    261:
1.180     schwarze  262:                manpath_parse(&paths, conf_file, defpaths, auxpaths);
                    263:                mansearch_setup(1);
                    264:                if( ! mansearch(&search, &paths, argc, argv, &res, &sz))
                    265:                        usage(search.argmode);
                    266:                manpath_free(&paths);
1.182     schwarze  267:
                    268:                /*
                    269:                 * For standard man(1) and -a output mode,
                    270:                 * prepare for copying filename pointers
                    271:                 * into the program parameter array.
                    272:                 */
                    273:
                    274:                if (outmode == OUTMODE_ONE) {
                    275:                        argc = 1;
                    276:                        argv[0] = res[0].file;
                    277:                        argv[1] = NULL;
                    278:                        best_prio = 10;
                    279:                } else if (outmode == OUTMODE_ALL) {
                    280:                        argc = (int)sz;
                    281:                        argv = auxargv = mandoc_reallocarray(
                    282:                            NULL, sz + 1, sizeof(char *));
                    283:                        argv[argc] = NULL;
                    284:                }
                    285:
                    286:                /* Iterate all matching manuals. */
                    287:
1.181     schwarze  288:                for (i = 0; i < sz; i++) {
                    289:                        if (outmode == OUTMODE_FLN)
                    290:                                puts(res[i].file);
1.182     schwarze  291:                        else if (outmode == OUTMODE_LST)
1.181     schwarze  292:                                printf("%s - %s\n", res[i].names,
                    293:                                    res[i].output == NULL ? "" :
                    294:                                    res[i].output);
1.182     schwarze  295:                        else if (outmode == OUTMODE_ALL)
                    296:                                argv[i] = res[i].file;
                    297:                        else {
                    298:                                /* Search for the best section. */
                    299:                                isec = strcspn(res[i].file, "123456789");
                    300:                                sec = res[i].file[isec];
                    301:                                if ('\0' == sec)
                    302:                                        continue;
                    303:                                prio = sec_prios[sec - '1'];
                    304:                                if (prio >= best_prio)
                    305:                                        continue;
                    306:                                best_prio = prio;
                    307:                                argv[0] = res[i].file;
                    308:                        }
1.181     schwarze  309:                }
1.182     schwarze  310:
                    311:                /*
                    312:                 * For man(1), -a and -i output mode, fall through
                    313:                 * to the main mandoc(1) code iterating files
                    314:                 * and running the parsers on each of them.
                    315:                 */
                    316:
                    317:                if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
                    318:                        goto out;
1.180     schwarze  319: #else
                    320:                fputs("mandoc: database support not compiled in\n",
                    321:                    stderr);
                    322:                return((int)MANDOCLEVEL_BADARG);
                    323: #endif
                    324:        }
                    325:
                    326:        /* mandoc(1) */
                    327:
                    328:        if ( ! moptions(&options, auxpaths))
                    329:                return((int)MANDOCLEVEL_BADARG);
1.1       kristaps  330:
1.183     schwarze  331:        if (use_pager && isatty(STDOUT_FILENO))
                    332:                spawn_pager();
                    333:
1.170     schwarze  334:        curp.mp = mparse_alloc(options, curp.wlevel, mmsg, defos);
1.154     kristaps  335:
1.165     kristaps  336:        /*
                    337:         * Conditionally start up the lookaside buffer before parsing.
                    338:         */
                    339:        if (OUTT_MAN == curp.outtype)
                    340:                mparse_keep(curp.mp);
                    341:
1.154     kristaps  342:        if (NULL == *argv)
                    343:                parse(&curp, STDIN_FILENO, "<stdin>", &rc);
1.64      kristaps  344:
                    345:        while (*argv) {
1.154     kristaps  346:                parse(&curp, -1, *argv, &rc);
                    347:                if (MANDOCLEVEL_OK != rc && curp.wstop)
1.64      kristaps  348:                        break;
                    349:                ++argv;
1.1       kristaps  350:        }
                    351:
1.22      kristaps  352:        if (curp.outfree)
                    353:                (*curp.outfree)(curp.outdata);
1.154     kristaps  354:        if (curp.mp)
                    355:                mparse_free(curp.mp);
1.182     schwarze  356:
                    357: #if HAVE_SQLITE3
                    358: out:
                    359:        if (search.argmode != ARG_FILE) {
                    360:                mansearch_free(res, sz);
                    361:                mansearch_setup(0);
                    362:                free(auxargv);
                    363:        }
                    364: #endif
                    365:
1.166     schwarze  366:        free(defos);
1.1       kristaps  367:
1.154     kristaps  368:        return((int)rc);
1.1       kristaps  369: }
                    370:
1.55      kristaps  371: static void
1.1       kristaps  372: version(void)
                    373: {
                    374:
1.180     schwarze  375:        printf("mandoc %s\n", VERSION);
1.105     kristaps  376:        exit((int)MANDOCLEVEL_OK);
1.1       kristaps  377: }
                    378:
1.55      kristaps  379: static void
1.180     schwarze  380: usage(enum argmode argmode)
1.1       kristaps  381: {
                    382:
1.180     schwarze  383:        switch (argmode) {
                    384:        case ARG_FILE:
                    385:                fputs("usage: mandoc [-V] [-Ios=name] [-mformat]"
                    386:                    " [-Ooption] [-Toutput] [-Wlevel]\n"
                    387:                    "\t      [file ...]\n", stderr);
                    388:                break;
                    389:        case ARG_NAME:
                    390:                fputs("usage: man [-acfhkVw] [-C file] "
                    391:                    "[-M path] [-m path] [-S arch] [-s section]\n"
                    392:                    "\t   [section] name ...\n", stderr);
                    393:                break;
                    394:        case ARG_WORD:
                    395:                fputs("usage: whatis [-V] [-C file] [-M path] [-m path] "
                    396:                    "[-S arch] [-s section] name ...\n", stderr);
                    397:                break;
                    398:        case ARG_EXPR:
                    399:                fputs("usage: apropos [-V] [-C file] [-M path] [-m path] "
                    400:                    "[-O outkey] [-S arch]\n"
                    401:                    "\t       [-s section] expression ...\n", stderr);
                    402:                break;
                    403:        }
1.105     kristaps  404:        exit((int)MANDOCLEVEL_BADARG);
1.68      joerg     405: }
                    406:
1.64      kristaps  407: static void
1.173     schwarze  408: parse(struct curparse *curp, int fd, const char *file,
                    409:        enum mandoclevel *level)
1.1       kristaps  410: {
1.154     kristaps  411:        enum mandoclevel  rc;
                    412:        struct mdoc      *mdoc;
                    413:        struct man       *man;
1.112     kristaps  414:
1.154     kristaps  415:        /* Begin by parsing the file itself. */
1.112     kristaps  416:
1.154     kristaps  417:        assert(file);
                    418:        assert(fd >= -1);
1.112     kristaps  419:
1.154     kristaps  420:        rc = mparse_readfd(curp->mp, fd, file);
1.112     kristaps  421:
1.154     kristaps  422:        /* Stop immediately if the parse has failed. */
1.92      kristaps  423:
1.154     kristaps  424:        if (MANDOCLEVEL_FATAL <= rc)
1.111     kristaps  425:                goto cleanup;
                    426:
1.1       kristaps  427:        /*
1.154     kristaps  428:         * With -Wstop and warnings or errors of at least the requested
                    429:         * level, do not produce output.
1.1       kristaps  430:         */
                    431:
1.154     kristaps  432:        if (MANDOCLEVEL_OK != rc && curp->wstop)
1.111     kristaps  433:                goto cleanup;
                    434:
                    435:        /* If unset, allocate output dev now (if applicable). */
                    436:
                    437:        if ( ! (curp->outman && curp->outmdoc)) {
                    438:                switch (curp->outtype) {
1.173     schwarze  439:                case OUTT_XHTML:
1.111     kristaps  440:                        curp->outdata = xhtml_alloc(curp->outopts);
1.162     kristaps  441:                        curp->outfree = html_free;
1.111     kristaps  442:                        break;
1.173     schwarze  443:                case OUTT_HTML:
1.111     kristaps  444:                        curp->outdata = html_alloc(curp->outopts);
1.162     kristaps  445:                        curp->outfree = html_free;
                    446:                        break;
1.173     schwarze  447:                case OUTT_UTF8:
1.163     kristaps  448:                        curp->outdata = utf8_alloc(curp->outopts);
                    449:                        curp->outfree = ascii_free;
                    450:                        break;
1.173     schwarze  451:                case OUTT_LOCALE:
1.162     kristaps  452:                        curp->outdata = locale_alloc(curp->outopts);
                    453:                        curp->outfree = ascii_free;
1.111     kristaps  454:                        break;
1.173     schwarze  455:                case OUTT_ASCII:
1.111     kristaps  456:                        curp->outdata = ascii_alloc(curp->outopts);
                    457:                        curp->outfree = ascii_free;
                    458:                        break;
1.173     schwarze  459:                case OUTT_PDF:
1.111     kristaps  460:                        curp->outdata = pdf_alloc(curp->outopts);
                    461:                        curp->outfree = pspdf_free;
                    462:                        break;
1.173     schwarze  463:                case OUTT_PS:
1.111     kristaps  464:                        curp->outdata = ps_alloc(curp->outopts);
                    465:                        curp->outfree = pspdf_free;
                    466:                        break;
                    467:                default:
                    468:                        break;
                    469:                }
                    470:
                    471:                switch (curp->outtype) {
1.173     schwarze  472:                case OUTT_HTML:
1.111     kristaps  473:                        /* FALLTHROUGH */
1.173     schwarze  474:                case OUTT_XHTML:
1.111     kristaps  475:                        curp->outman = html_man;
                    476:                        curp->outmdoc = html_mdoc;
                    477:                        break;
1.173     schwarze  478:                case OUTT_TREE:
1.111     kristaps  479:                        curp->outman = tree_man;
                    480:                        curp->outmdoc = tree_mdoc;
                    481:                        break;
1.173     schwarze  482:                case OUTT_MAN:
1.164     schwarze  483:                        curp->outmdoc = man_mdoc;
1.165     kristaps  484:                        curp->outman = man_man;
1.164     schwarze  485:                        break;
1.173     schwarze  486:                case OUTT_PDF:
1.111     kristaps  487:                        /* FALLTHROUGH */
1.173     schwarze  488:                case OUTT_ASCII:
1.111     kristaps  489:                        /* FALLTHROUGH */
1.173     schwarze  490:                case OUTT_UTF8:
1.163     kristaps  491:                        /* FALLTHROUGH */
1.173     schwarze  492:                case OUTT_LOCALE:
1.162     kristaps  493:                        /* FALLTHROUGH */
1.173     schwarze  494:                case OUTT_PS:
1.111     kristaps  495:                        curp->outman = terminal_man;
                    496:                        curp->outmdoc = terminal_mdoc;
                    497:                        break;
                    498:                default:
                    499:                        break;
                    500:                }
                    501:        }
                    502:
1.171     schwarze  503:        mparse_result(curp->mp, &mdoc, &man, NULL);
1.154     kristaps  504:
1.111     kristaps  505:        /* Execute the out device, if it exists. */
                    506:
1.154     kristaps  507:        if (man && curp->outman)
                    508:                (*curp->outman)(curp->outdata, man);
                    509:        if (mdoc && curp->outmdoc)
                    510:                (*curp->outmdoc)(curp->outdata, mdoc);
1.111     kristaps  511:
                    512:  cleanup:
1.112     kristaps  513:
1.154     kristaps  514:        mparse_reset(curp->mp);
1.111     kristaps  515:
1.154     kristaps  516:        if (*level < rc)
                    517:                *level = rc;
1.10      kristaps  518: }
                    519:
                    520: static int
1.170     schwarze  521: moptions(int *options, char *arg)
1.10      kristaps  522: {
                    523:
1.180     schwarze  524:        if (arg == NULL)
                    525:                /* nothing to do */;
                    526:        else if (0 == strcmp(arg, "doc"))
1.170     schwarze  527:                *options |= MPARSE_MDOC;
1.19      kristaps  528:        else if (0 == strcmp(arg, "andoc"))
1.170     schwarze  529:                /* nothing to do */;
1.17      kristaps  530:        else if (0 == strcmp(arg, "an"))
1.170     schwarze  531:                *options |= MPARSE_MAN;
1.10      kristaps  532:        else {
1.176     schwarze  533:                fprintf(stderr, "%s: -m%s: Bad argument\n",
                    534:                    progname, arg);
1.10      kristaps  535:                return(0);
                    536:        }
                    537:
                    538:        return(1);
1.1       kristaps  539: }
                    540:
                    541: static int
1.60      kristaps  542: toptions(struct curparse *curp, char *arg)
1.1       kristaps  543: {
                    544:
                    545:        if (0 == strcmp(arg, "ascii"))
1.60      kristaps  546:                curp->outtype = OUTT_ASCII;
                    547:        else if (0 == strcmp(arg, "lint")) {
                    548:                curp->outtype = OUTT_LINT;
1.103     schwarze  549:                curp->wlevel  = MANDOCLEVEL_WARNING;
1.154     kristaps  550:        } else if (0 == strcmp(arg, "tree"))
1.60      kristaps  551:                curp->outtype = OUTT_TREE;
1.164     schwarze  552:        else if (0 == strcmp(arg, "man"))
                    553:                curp->outtype = OUTT_MAN;
1.43      kristaps  554:        else if (0 == strcmp(arg, "html"))
1.60      kristaps  555:                curp->outtype = OUTT_HTML;
1.163     kristaps  556:        else if (0 == strcmp(arg, "utf8"))
                    557:                curp->outtype = OUTT_UTF8;
1.162     kristaps  558:        else if (0 == strcmp(arg, "locale"))
                    559:                curp->outtype = OUTT_LOCALE;
1.59      kristaps  560:        else if (0 == strcmp(arg, "xhtml"))
1.60      kristaps  561:                curp->outtype = OUTT_XHTML;
1.85      kristaps  562:        else if (0 == strcmp(arg, "ps"))
                    563:                curp->outtype = OUTT_PS;
1.100     kristaps  564:        else if (0 == strcmp(arg, "pdf"))
                    565:                curp->outtype = OUTT_PDF;
1.1       kristaps  566:        else {
1.176     schwarze  567:                fprintf(stderr, "%s: -T%s: Bad argument\n",
                    568:                    progname, arg);
1.1       kristaps  569:                return(0);
                    570:        }
                    571:
                    572:        return(1);
                    573: }
                    574:
                    575: static int
1.103     schwarze  576: woptions(struct curparse *curp, char *arg)
1.1       kristaps  577: {
1.34      kristaps  578:        char            *v, *o;
1.173     schwarze  579:        const char      *toks[6];
1.1       kristaps  580:
1.103     schwarze  581:        toks[0] = "stop";
                    582:        toks[1] = "all";
                    583:        toks[2] = "warning";
                    584:        toks[3] = "error";
                    585:        toks[4] = "fatal";
                    586:        toks[5] = NULL;
1.1       kristaps  587:
1.34      kristaps  588:        while (*arg) {
                    589:                o = arg;
1.45      kristaps  590:                switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.173     schwarze  591:                case 0:
1.103     schwarze  592:                        curp->wstop = 1;
1.1       kristaps  593:                        break;
1.173     schwarze  594:                case 1:
1.103     schwarze  595:                        /* FALLTHROUGH */
1.173     schwarze  596:                case 2:
1.103     schwarze  597:                        curp->wlevel = MANDOCLEVEL_WARNING;
1.1       kristaps  598:                        break;
1.173     schwarze  599:                case 3:
1.103     schwarze  600:                        curp->wlevel = MANDOCLEVEL_ERROR;
1.24      kristaps  601:                        break;
1.173     schwarze  602:                case 4:
1.103     schwarze  603:                        curp->wlevel = MANDOCLEVEL_FATAL;
1.50      kristaps  604:                        break;
1.1       kristaps  605:                default:
1.176     schwarze  606:                        fprintf(stderr, "%s: -W%s: Bad argument\n",
                    607:                            progname, o);
1.1       kristaps  608:                        return(0);
                    609:                }
1.34      kristaps  610:        }
1.1       kristaps  611:
                    612:        return(1);
                    613: }
                    614:
1.153     kristaps  615: static void
1.173     schwarze  616: mmsg(enum mandocerr t, enum mandoclevel lvl,
1.155     kristaps  617:                const char *file, int line, int col, const char *msg)
1.71      kristaps  618: {
1.177     schwarze  619:        const char      *mparse_msg;
1.103     schwarze  620:
1.175     schwarze  621:        fprintf(stderr, "%s: %s:", progname, file);
                    622:
                    623:        if (line)
                    624:                fprintf(stderr, "%d:%d:", line, col + 1);
                    625:
1.177     schwarze  626:        fprintf(stderr, " %s", mparse_strlevel(lvl));
                    627:
                    628:        if (NULL != (mparse_msg = mparse_strerror(t)))
                    629:                fprintf(stderr, ": %s", mparse_msg);
1.154     kristaps  630:
1.73      kristaps  631:        if (msg)
                    632:                fprintf(stderr, ": %s", msg);
1.154     kristaps  633:
1.73      kristaps  634:        fputc('\n', stderr);
1.183     schwarze  635: }
                    636:
                    637: static void
                    638: spawn_pager(void)
                    639: {
1.184   ! schwarze  640: #define MAX_PAGER_ARGS 16
        !           641:        char            *argv[MAX_PAGER_ARGS];
        !           642:        const char      *pager;
        !           643:        char            *cp;
        !           644:        int              fildes[2];
        !           645:        int              argc;
1.183     schwarze  646:
                    647:        if (pipe(fildes) == -1) {
                    648:                fprintf(stderr, "%s: pipe: %s\n",
                    649:                    progname, strerror(errno));
                    650:                return;
                    651:        }
                    652:
                    653:        switch (fork()) {
                    654:        case -1:
                    655:                fprintf(stderr, "%s: fork: %s\n",
                    656:                    progname, strerror(errno));
                    657:                exit((int)MANDOCLEVEL_SYSERR);
                    658:        case 0:
                    659:                close(fildes[0]);
                    660:                if (dup2(fildes[1], STDOUT_FILENO) == -1) {
                    661:                        fprintf(stderr, "%s: dup output: %s\n",
                    662:                            progname, strerror(errno));
                    663:                        exit((int)MANDOCLEVEL_SYSERR);
                    664:                }
                    665:                return;
                    666:        default:
1.184   ! schwarze  667:                break;
        !           668:        }
        !           669:
        !           670:        /* The original process becomes the pager. */
        !           671:
        !           672:        close(fildes[1]);
        !           673:        if (dup2(fildes[0], STDIN_FILENO) == -1) {
        !           674:                fprintf(stderr, "%s: dup input: %s\n",
        !           675:                    progname, strerror(errno));
1.183     schwarze  676:                exit((int)MANDOCLEVEL_SYSERR);
                    677:        }
1.184   ! schwarze  678:
        !           679:        pager = getenv("MANPAGER");
        !           680:        if (pager == NULL || *pager == '\0')
        !           681:                pager = getenv("PAGER");
        !           682:        if (pager == NULL || *pager == '\0')
        !           683:                pager = "/usr/bin/more -s";
        !           684:        cp = mandoc_strdup(pager);
        !           685:
        !           686:        /*
        !           687:         * Parse the pager command into words.
        !           688:         * Intentionally do not do anything fancy here.
        !           689:         */
        !           690:
        !           691:        argc = 0;
        !           692:        while (argc + 1 < MAX_PAGER_ARGS) {
        !           693:                argv[argc++] = cp;
        !           694:                cp = strchr(cp, ' ');
        !           695:                if (cp == NULL)
        !           696:                        break;
        !           697:                *cp++ = '\0';
        !           698:                while (*cp == ' ')
        !           699:                        cp++;
        !           700:                if (*cp == '\0')
        !           701:                        break;
        !           702:        }
        !           703:        argv[argc] = NULL;
        !           704:
        !           705:        /* Hand over to the pager. */
        !           706:
        !           707:        execvp(argv[0], argv);
        !           708:        fprintf(stderr, "%s: exec: %s\n",
        !           709:            progname, strerror(errno));
        !           710:        exit((int)MANDOCLEVEL_SYSERR);
1.71      kristaps  711: }

CVSweb