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

Annotation of mandoc/main.c, Revision 1.207

1.207   ! schwarze    1: /*     $Id: main.c,v 1.206 2014/12/15 18:05:57 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.185     schwarze   24: #include <ctype.h>
1.183     schwarze   25: #include <errno.h>
1.186     schwarze   26: #include <fcntl.h>
1.1       kristaps   27: #include <stdio.h>
1.45      kristaps   28: #include <stdint.h>
1.1       kristaps   29: #include <stdlib.h>
                     30: #include <string.h>
                     31: #include <unistd.h>
                     32:
1.71      kristaps   33: #include "mandoc.h"
1.172     schwarze   34: #include "mandoc_aux.h"
1.90      kristaps   35: #include "main.h"
1.1       kristaps   36: #include "mdoc.h"
1.10      kristaps   37: #include "man.h"
1.180     schwarze   38: #include "manpath.h"
                     39: #include "mansearch.h"
1.101     joerg      40:
1.55      kristaps   41: #if !defined(__GNUC__) || (__GNUC__ < 2)
1.56      kristaps   42: # if !defined(lint)
                     43: #  define __attribute__(x)
                     44: # endif
1.55      kristaps   45: #endif /* !defined(__GNUC__) || (__GNUC__ < 2) */
1.16      kristaps   46:
1.181     schwarze   47: enum   outmode {
                     48:        OUTMODE_DEF = 0,
                     49:        OUTMODE_FLN,
                     50:        OUTMODE_LST,
                     51:        OUTMODE_ALL,
                     52:        OUTMODE_INT,
                     53:        OUTMODE_ONE
                     54: };
                     55:
1.42      kristaps   56: typedef        void            (*out_mdoc)(void *, const struct mdoc *);
                     57: typedef        void            (*out_man)(void *, const struct man *);
1.22      kristaps   58: typedef        void            (*out_free)(void *);
                     59:
1.19      kristaps   60: enum   outt {
1.154     kristaps   61:        OUTT_ASCII = 0, /* -Tascii */
1.162     kristaps   62:        OUTT_LOCALE,    /* -Tlocale */
1.163     kristaps   63:        OUTT_UTF8,      /* -Tutf8 */
1.154     kristaps   64:        OUTT_TREE,      /* -Ttree */
1.164     schwarze   65:        OUTT_MAN,       /* -Tman */
1.154     kristaps   66:        OUTT_HTML,      /* -Thtml */
                     67:        OUTT_LINT,      /* -Tlint */
                     68:        OUTT_PS,        /* -Tps */
                     69:        OUTT_PDF        /* -Tpdf */
1.19      kristaps   70: };
                     71:
1.5       kristaps   72: struct curparse {
1.154     kristaps   73:        struct mparse    *mp;
1.195     schwarze   74:        struct mchars    *mchars;       /* character table */
1.148     kristaps   75:        enum mandoclevel  wlevel;       /* ignore messages below this */
                     76:        int               wstop;        /* stop after a file with a warning */
1.173     schwarze   77:        enum outt         outtype;      /* which output to use */
1.79      kristaps   78:        out_mdoc          outmdoc;      /* mdoc output ptr */
1.173     schwarze   79:        out_man           outman;       /* man output ptr */
1.79      kristaps   80:        out_free          outfree;      /* free output ptr */
                     81:        void             *outdata;      /* data for output */
                     82:        char              outopts[BUFSIZ]; /* buf of output opts */
                     83: };
                     84:
1.194     schwarze   85: static int               koptions(int *, char *);
1.203     schwarze   86: #if HAVE_SQLITE3
                     87: int                      mandocdb(int, char**);
                     88: #endif
1.170     schwarze   89: static int               moptions(int *, char *);
1.155     kristaps   90: static void              mmsg(enum mandocerr, enum mandoclevel,
                     91:                                const char *, int, int, const char *);
1.173     schwarze   92: static void              parse(struct curparse *, int,
1.154     kristaps   93:                                const char *, enum mandoclevel *);
1.205     schwarze   94: #if HAVE_SQLITE3
1.197     schwarze   95: static enum mandoclevel  passthrough(const char *, int, int);
1.205     schwarze   96: #endif
1.183     schwarze   97: static void              spawn_pager(void);
1.66      kristaps   98: static int               toptions(struct curparse *, char *);
1.180     schwarze   99: static void              usage(enum argmode) __attribute__((noreturn));
1.55      kristaps  100: static void              version(void) __attribute__((noreturn));
1.103     schwarze  101: static int               woptions(struct curparse *, char *);
1.1       kristaps  102:
1.182     schwarze  103: static const int sec_prios[] = {1, 4, 5, 8, 6, 3, 7, 2, 9};
1.202     schwarze  104: static char              help_arg[] = "help";
                    105: static char             *help_argv[] = {help_arg, NULL};
1.54      kristaps  106: static const char       *progname;
1.1       kristaps  107:
1.173     schwarze  108:
1.1       kristaps  109: int
                    110: main(int argc, char *argv[])
                    111: {
1.5       kristaps  112:        struct curparse  curp;
1.180     schwarze  113:        struct mansearch search;
                    114:        struct manpaths  paths;
1.205     schwarze  115:        char            *auxpaths;
1.180     schwarze  116:        char            *defos;
1.207   ! schwarze  117:        unsigned char   *uc;
1.180     schwarze  118: #if HAVE_SQLITE3
1.186     schwarze  119:        struct manpage  *res, *resp;
1.205     schwarze  120:        char            *conf_file, *defpaths;
1.182     schwarze  121:        size_t           isec, i, sz;
1.205     schwarze  122:        int              prio, best_prio, synopsis_only;
1.182     schwarze  123:        char             sec;
1.180     schwarze  124: #endif
                    125:        enum mandoclevel rc;
1.181     schwarze  126:        enum outmode     outmode;
1.192     schwarze  127:        int              fd;
1.180     schwarze  128:        int              show_usage;
1.183     schwarze  129:        int              use_pager;
1.170     schwarze  130:        int              options;
1.180     schwarze  131:        int              c;
1.1       kristaps  132:
1.54      kristaps  133:        progname = strrchr(argv[0], '/');
                    134:        if (progname == NULL)
                    135:                progname = argv[0];
                    136:        else
                    137:                ++progname;
1.203     schwarze  138:
                    139: #if HAVE_SQLITE3
1.204     schwarze  140:        if (strcmp(progname, BINM_MAKEWHATIS) == 0)
1.203     schwarze  141:                return(mandocdb(argc, argv));
                    142: #endif
1.179     schwarze  143:
1.180     schwarze  144:        /* Search options. */
                    145:
                    146:        memset(&paths, 0, sizeof(struct manpaths));
1.205     schwarze  147: #if HAVE_SQLITE3
                    148:        conf_file = defpaths = NULL;
                    149: #endif
                    150:        auxpaths = NULL;
1.180     schwarze  151:
                    152:        memset(&search, 0, sizeof(struct mansearch));
                    153:        search.outkey = "Nd";
                    154:
1.204     schwarze  155:        if (strcmp(progname, BINM_MAN) == 0)
1.180     schwarze  156:                search.argmode = ARG_NAME;
1.204     schwarze  157:        else if (strcmp(progname, BINM_APROPOS) == 0)
1.180     schwarze  158:                search.argmode = ARG_EXPR;
1.204     schwarze  159:        else if (strcmp(progname, BINM_WHATIS) == 0)
1.180     schwarze  160:                search.argmode = ARG_WORD;
1.202     schwarze  161:        else if (strncmp(progname, "help", 4) == 0)
                    162:                search.argmode = ARG_NAME;
1.180     schwarze  163:        else
                    164:                search.argmode = ARG_FILE;
                    165:
                    166:        /* Parser and formatter options. */
1.54      kristaps  167:
1.51      kristaps  168:        memset(&curp, 0, sizeof(struct curparse));
1.201     schwarze  169:        curp.outtype = OUTT_LOCALE;
1.103     schwarze  170:        curp.wlevel  = MANDOCLEVEL_FATAL;
1.194     schwarze  171:        options = MPARSE_SO | MPARSE_UTF8 | MPARSE_LATIN1;
1.166     schwarze  172:        defos = NULL;
1.19      kristaps  173:
1.183     schwarze  174:        use_pager = 1;
1.180     schwarze  175:        show_usage = 0;
1.205     schwarze  176: #if HAVE_SQLITE3
1.197     schwarze  177:        synopsis_only = 0;
1.205     schwarze  178: #endif
1.181     schwarze  179:        outmode = OUTMODE_DEF;
1.183     schwarze  180:
1.194     schwarze  181:        while (-1 != (c = getopt(argc, argv,
                    182:                        "aC:cfhI:iK:klM:m:O:S:s:T:VW:w"))) {
1.1       kristaps  183:                switch (c) {
1.181     schwarze  184:                case 'a':
                    185:                        outmode = OUTMODE_ALL;
                    186:                        break;
1.180     schwarze  187:                case 'C':
1.205     schwarze  188: #if HAVE_SQLITE3
1.180     schwarze  189:                        conf_file = optarg;
1.205     schwarze  190: #endif
1.180     schwarze  191:                        break;
1.183     schwarze  192:                case 'c':
                    193:                        use_pager = 0;
                    194:                        break;
1.180     schwarze  195:                case 'f':
                    196:                        search.argmode = ARG_WORD;
                    197:                        break;
1.190     schwarze  198:                case 'h':
                    199:                        (void)strlcat(curp.outopts, "synopsis,", BUFSIZ);
1.205     schwarze  200: #if HAVE_SQLITE3
1.197     schwarze  201:                        synopsis_only = 1;
1.205     schwarze  202: #endif
1.198     schwarze  203:                        use_pager = 0;
1.190     schwarze  204:                        outmode = OUTMODE_ALL;
                    205:                        break;
1.173     schwarze  206:                case 'I':
1.166     schwarze  207:                        if (strncmp(optarg, "os=", 3)) {
1.173     schwarze  208:                                fprintf(stderr,
1.176     schwarze  209:                                    "%s: -I%s: Bad argument\n",
                    210:                                    progname, optarg);
1.166     schwarze  211:                                return((int)MANDOCLEVEL_BADARG);
                    212:                        }
                    213:                        if (defos) {
1.173     schwarze  214:                                fprintf(stderr,
1.176     schwarze  215:                                    "%s: -I%s: Duplicate argument\n",
                    216:                                    progname, optarg);
1.166     schwarze  217:                                return((int)MANDOCLEVEL_BADARG);
                    218:                        }
                    219:                        defos = mandoc_strdup(optarg + 3);
                    220:                        break;
1.181     schwarze  221:                case 'i':
                    222:                        outmode = OUTMODE_INT;
                    223:                        break;
1.194     schwarze  224:                case 'K':
                    225:                        if ( ! koptions(&options, optarg))
                    226:                                return((int)MANDOCLEVEL_BADARG);
                    227:                        break;
1.180     schwarze  228:                case 'k':
                    229:                        search.argmode = ARG_EXPR;
                    230:                        break;
1.188     schwarze  231:                case 'l':
                    232:                        search.argmode = ARG_FILE;
                    233:                        outmode = OUTMODE_ALL;
                    234:                        break;
1.180     schwarze  235:                case 'M':
1.205     schwarze  236: #if HAVE_SQLITE3
1.180     schwarze  237:                        defpaths = optarg;
1.205     schwarze  238: #endif
1.180     schwarze  239:                        break;
1.173     schwarze  240:                case 'm':
1.180     schwarze  241:                        auxpaths = optarg;
1.10      kristaps  242:                        break;
1.173     schwarze  243:                case 'O':
1.180     schwarze  244:                        search.outkey = optarg;
1.49      kristaps  245:                        (void)strlcat(curp.outopts, optarg, BUFSIZ);
                    246:                        (void)strlcat(curp.outopts, ",", BUFSIZ);
1.44      kristaps  247:                        break;
1.180     schwarze  248:                case 'S':
                    249:                        search.arch = optarg;
                    250:                        break;
                    251:                case 's':
                    252:                        search.sec = optarg;
                    253:                        break;
1.173     schwarze  254:                case 'T':
1.60      kristaps  255:                        if ( ! toptions(&curp, optarg))
1.105     kristaps  256:                                return((int)MANDOCLEVEL_BADARG);
1.1       kristaps  257:                        break;
1.173     schwarze  258:                case 'W':
1.103     schwarze  259:                        if ( ! woptions(&curp, optarg))
1.105     kristaps  260:                                return((int)MANDOCLEVEL_BADARG);
1.1       kristaps  261:                        break;
1.181     schwarze  262:                case 'w':
                    263:                        outmode = OUTMODE_FLN;
                    264:                        break;
1.173     schwarze  265:                case 'V':
1.1       kristaps  266:                        version();
                    267:                        /* NOTREACHED */
                    268:                default:
1.180     schwarze  269:                        show_usage = 1;
                    270:                        break;
1.1       kristaps  271:                }
1.180     schwarze  272:        }
                    273:
                    274:        if (show_usage)
                    275:                usage(search.argmode);
                    276:
1.185     schwarze  277:        /* Postprocess options. */
                    278:
1.181     schwarze  279:        if (outmode == OUTMODE_DEF) {
                    280:                switch (search.argmode) {
                    281:                case ARG_FILE:
                    282:                        outmode = OUTMODE_ALL;
1.183     schwarze  283:                        use_pager = 0;
1.181     schwarze  284:                        break;
                    285:                case ARG_NAME:
                    286:                        outmode = OUTMODE_ONE;
                    287:                        break;
                    288:                default:
                    289:                        outmode = OUTMODE_LST;
                    290:                        break;
                    291:                }
                    292:        }
                    293:
1.185     schwarze  294:        /* Parse arguments. */
                    295:
1.180     schwarze  296:        argc -= optind;
                    297:        argv += optind;
1.182     schwarze  298: #if HAVE_SQLITE3
1.186     schwarze  299:        resp = NULL;
1.182     schwarze  300: #endif
1.185     schwarze  301:
1.202     schwarze  302:        /*
                    303:         * Quirks for help(1)
                    304:         * and for a man(1) section argument without -s.
                    305:         */
                    306:
                    307:        if (search.argmode == ARG_NAME) {
                    308:                if (*progname == 'h') {
                    309:                        if (argc == 0) {
                    310:                                argv = help_argv;
                    311:                                argc = 1;
                    312:                        }
1.207   ! schwarze  313:                } else if (((uc = argv[0]) != NULL) &&
        !           314:                    ((isdigit(uc[0]) && (uc[1] == '\0' ||
        !           315:                      (isalpha(uc[1]) && uc[2] == '\0'))) ||
        !           316:                     (uc[0] == 'n' && uc[1] == '\0'))) {
        !           317:                        search.sec = uc;
1.202     schwarze  318:                        argv++;
                    319:                        argc--;
                    320:                }
1.185     schwarze  321:        }
1.182     schwarze  322:
                    323:        rc = MANDOCLEVEL_OK;
1.180     schwarze  324:
                    325:        /* man(1), whatis(1), apropos(1) */
                    326:
                    327:        if (search.argmode != ARG_FILE) {
                    328: #if HAVE_SQLITE3
                    329:                if (argc == 0)
                    330:                        usage(search.argmode);
1.199     schwarze  331:
                    332:                if (search.argmode == ARG_NAME &&
                    333:                    outmode == OUTMODE_ONE)
                    334:                        search.firstmatch = 1;
1.182     schwarze  335:
                    336:                /* Access the mandoc database. */
                    337:
1.180     schwarze  338:                manpath_parse(&paths, conf_file, defpaths, auxpaths);
                    339:                mansearch_setup(1);
                    340:                if( ! mansearch(&search, &paths, argc, argv, &res, &sz))
                    341:                        usage(search.argmode);
1.186     schwarze  342:                resp = res;
1.187     schwarze  343:
                    344:                if (sz == 0) {
                    345:                        if (search.argmode == ARG_NAME)
                    346:                                fprintf(stderr, "%s: No entry for %s "
                    347:                                    "in the manual.\n", progname, argv[0]);
                    348:                        rc = MANDOCLEVEL_BADARG;
                    349:                        goto out;
                    350:                }
1.182     schwarze  351:
                    352:                /*
                    353:                 * For standard man(1) and -a output mode,
                    354:                 * prepare for copying filename pointers
                    355:                 * into the program parameter array.
                    356:                 */
                    357:
                    358:                if (outmode == OUTMODE_ONE) {
                    359:                        argc = 1;
                    360:                        best_prio = 10;
1.186     schwarze  361:                } else if (outmode == OUTMODE_ALL)
1.182     schwarze  362:                        argc = (int)sz;
                    363:
                    364:                /* Iterate all matching manuals. */
                    365:
1.181     schwarze  366:                for (i = 0; i < sz; i++) {
                    367:                        if (outmode == OUTMODE_FLN)
                    368:                                puts(res[i].file);
1.182     schwarze  369:                        else if (outmode == OUTMODE_LST)
1.181     schwarze  370:                                printf("%s - %s\n", res[i].names,
                    371:                                    res[i].output == NULL ? "" :
                    372:                                    res[i].output);
1.186     schwarze  373:                        else if (outmode == OUTMODE_ONE) {
1.182     schwarze  374:                                /* Search for the best section. */
                    375:                                isec = strcspn(res[i].file, "123456789");
                    376:                                sec = res[i].file[isec];
                    377:                                if ('\0' == sec)
                    378:                                        continue;
                    379:                                prio = sec_prios[sec - '1'];
                    380:                                if (prio >= best_prio)
                    381:                                        continue;
                    382:                                best_prio = prio;
1.186     schwarze  383:                                resp = res + i;
1.182     schwarze  384:                        }
1.181     schwarze  385:                }
1.182     schwarze  386:
                    387:                /*
                    388:                 * For man(1), -a and -i output mode, fall through
                    389:                 * to the main mandoc(1) code iterating files
                    390:                 * and running the parsers on each of them.
                    391:                 */
                    392:
                    393:                if (outmode == OUTMODE_FLN || outmode == OUTMODE_LST)
                    394:                        goto out;
1.180     schwarze  395: #else
                    396:                fputs("mandoc: database support not compiled in\n",
                    397:                    stderr);
                    398:                return((int)MANDOCLEVEL_BADARG);
                    399: #endif
                    400:        }
                    401:
                    402:        /* mandoc(1) */
                    403:
                    404:        if ( ! moptions(&options, auxpaths))
                    405:                return((int)MANDOCLEVEL_BADARG);
1.1       kristaps  406:
1.183     schwarze  407:        if (use_pager && isatty(STDOUT_FILENO))
                    408:                spawn_pager();
                    409:
1.195     schwarze  410:        curp.mchars = mchars_alloc();
                    411:        curp.mp = mparse_alloc(options, curp.wlevel, mmsg,
                    412:            curp.mchars, defos);
1.154     kristaps  413:
1.165     kristaps  414:        /*
                    415:         * Conditionally start up the lookaside buffer before parsing.
                    416:         */
                    417:        if (OUTT_MAN == curp.outtype)
                    418:                mparse_keep(curp.mp);
                    419:
1.186     schwarze  420:        if (argc == 0)
1.154     kristaps  421:                parse(&curp, STDIN_FILENO, "<stdin>", &rc);
1.64      kristaps  422:
1.186     schwarze  423:        while (argc) {
                    424: #if HAVE_SQLITE3
                    425:                if (resp != NULL) {
1.200     schwarze  426:                        rc = mparse_open(curp.mp, &fd, resp->file);
1.192     schwarze  427:                        if (fd == -1)
                    428:                                /* nothing */;
                    429:                        else if (resp->form & FORM_SRC) {
1.189     schwarze  430:                                /* For .so only; ignore failure. */
                    431:                                chdir(paths.paths[resp->ipath]);
1.192     schwarze  432:                                parse(&curp, fd, resp->file, &rc);
1.189     schwarze  433:                        } else
1.197     schwarze  434:                                rc = passthrough(resp->file, fd,
                    435:                                    synopsis_only);
1.186     schwarze  436:                        resp++;
                    437:                } else
                    438: #endif
1.192     schwarze  439:                {
1.200     schwarze  440:                        rc = mparse_open(curp.mp, &fd, *argv++);
1.192     schwarze  441:                        if (fd != -1)
                    442:                                parse(&curp, fd, argv[-1], &rc);
                    443:                }
                    444:
1.200     schwarze  445:                if (mparse_wait(curp.mp) != MANDOCLEVEL_OK)
1.192     schwarze  446:                        rc = MANDOCLEVEL_SYSERR;
                    447:
1.154     kristaps  448:                if (MANDOCLEVEL_OK != rc && curp.wstop)
1.64      kristaps  449:                        break;
1.186     schwarze  450:                argc--;
1.1       kristaps  451:        }
                    452:
1.22      kristaps  453:        if (curp.outfree)
                    454:                (*curp.outfree)(curp.outdata);
1.195     schwarze  455:        mparse_free(curp.mp);
                    456:        mchars_free(curp.mchars);
1.182     schwarze  457:
                    458: #if HAVE_SQLITE3
                    459: out:
                    460:        if (search.argmode != ARG_FILE) {
1.189     schwarze  461:                manpath_free(&paths);
1.182     schwarze  462:                mansearch_free(res, sz);
                    463:                mansearch_setup(0);
                    464:        }
                    465: #endif
                    466:
1.166     schwarze  467:        free(defos);
1.1       kristaps  468:
1.154     kristaps  469:        return((int)rc);
1.1       kristaps  470: }
                    471:
1.55      kristaps  472: static void
1.1       kristaps  473: version(void)
                    474: {
                    475:
1.180     schwarze  476:        printf("mandoc %s\n", VERSION);
1.105     kristaps  477:        exit((int)MANDOCLEVEL_OK);
1.1       kristaps  478: }
                    479:
1.55      kristaps  480: static void
1.180     schwarze  481: usage(enum argmode argmode)
1.1       kristaps  482: {
                    483:
1.180     schwarze  484:        switch (argmode) {
                    485:        case ARG_FILE:
1.190     schwarze  486:                fputs("usage: mandoc [-acfhklV] [-Ios=name] "
1.196     schwarze  487:                    "[-Kencoding] [-mformat] [-Ooption]\n"
                    488:                    "\t      [-Toutput] [-Wlevel] [file ...]\n", stderr);
1.180     schwarze  489:                break;
                    490:        case ARG_NAME:
1.188     schwarze  491:                fputs("usage: man [-acfhklVw] [-C file] "
1.180     schwarze  492:                    "[-M path] [-m path] [-S arch] [-s section]\n"
                    493:                    "\t   [section] name ...\n", stderr);
                    494:                break;
                    495:        case ARG_WORD:
1.190     schwarze  496:                fputs("usage: whatis [-acfhklVw] [-C file] "
1.188     schwarze  497:                    "[-M path] [-m path] [-O outkey] [-S arch]\n"
                    498:                    "\t      [-s section] name ...\n", stderr);
1.180     schwarze  499:                break;
                    500:        case ARG_EXPR:
1.190     schwarze  501:                fputs("usage: apropos [-acfhklVw] [-C file] "
1.188     schwarze  502:                    "[-M path] [-m path] [-O outkey] [-S arch]\n"
1.180     schwarze  503:                    "\t       [-s section] expression ...\n", stderr);
                    504:                break;
                    505:        }
1.105     kristaps  506:        exit((int)MANDOCLEVEL_BADARG);
1.68      joerg     507: }
                    508:
1.64      kristaps  509: static void
1.173     schwarze  510: parse(struct curparse *curp, int fd, const char *file,
                    511:        enum mandoclevel *level)
1.1       kristaps  512: {
1.154     kristaps  513:        enum mandoclevel  rc;
                    514:        struct mdoc      *mdoc;
                    515:        struct man       *man;
1.112     kristaps  516:
1.154     kristaps  517:        /* Begin by parsing the file itself. */
1.112     kristaps  518:
1.154     kristaps  519:        assert(file);
                    520:        assert(fd >= -1);
1.112     kristaps  521:
1.154     kristaps  522:        rc = mparse_readfd(curp->mp, fd, file);
1.112     kristaps  523:
1.154     kristaps  524:        /* Stop immediately if the parse has failed. */
1.92      kristaps  525:
1.154     kristaps  526:        if (MANDOCLEVEL_FATAL <= rc)
1.111     kristaps  527:                goto cleanup;
                    528:
1.1       kristaps  529:        /*
1.154     kristaps  530:         * With -Wstop and warnings or errors of at least the requested
                    531:         * level, do not produce output.
1.1       kristaps  532:         */
                    533:
1.154     kristaps  534:        if (MANDOCLEVEL_OK != rc && curp->wstop)
1.111     kristaps  535:                goto cleanup;
                    536:
                    537:        /* If unset, allocate output dev now (if applicable). */
                    538:
                    539:        if ( ! (curp->outman && curp->outmdoc)) {
                    540:                switch (curp->outtype) {
1.173     schwarze  541:                case OUTT_HTML:
1.195     schwarze  542:                        curp->outdata = html_alloc(curp->mchars,
                    543:                            curp->outopts);
1.162     kristaps  544:                        curp->outfree = html_free;
                    545:                        break;
1.173     schwarze  546:                case OUTT_UTF8:
1.195     schwarze  547:                        curp->outdata = utf8_alloc(curp->mchars,
                    548:                            curp->outopts);
1.163     kristaps  549:                        curp->outfree = ascii_free;
                    550:                        break;
1.173     schwarze  551:                case OUTT_LOCALE:
1.195     schwarze  552:                        curp->outdata = locale_alloc(curp->mchars,
                    553:                            curp->outopts);
1.162     kristaps  554:                        curp->outfree = ascii_free;
1.111     kristaps  555:                        break;
1.173     schwarze  556:                case OUTT_ASCII:
1.195     schwarze  557:                        curp->outdata = ascii_alloc(curp->mchars,
                    558:                            curp->outopts);
1.111     kristaps  559:                        curp->outfree = ascii_free;
                    560:                        break;
1.173     schwarze  561:                case OUTT_PDF:
1.195     schwarze  562:                        curp->outdata = pdf_alloc(curp->mchars,
                    563:                            curp->outopts);
1.111     kristaps  564:                        curp->outfree = pspdf_free;
                    565:                        break;
1.173     schwarze  566:                case OUTT_PS:
1.195     schwarze  567:                        curp->outdata = ps_alloc(curp->mchars,
                    568:                            curp->outopts);
1.111     kristaps  569:                        curp->outfree = pspdf_free;
                    570:                        break;
                    571:                default:
                    572:                        break;
                    573:                }
                    574:
                    575:                switch (curp->outtype) {
1.173     schwarze  576:                case OUTT_HTML:
1.111     kristaps  577:                        curp->outman = html_man;
                    578:                        curp->outmdoc = html_mdoc;
                    579:                        break;
1.173     schwarze  580:                case OUTT_TREE:
1.111     kristaps  581:                        curp->outman = tree_man;
                    582:                        curp->outmdoc = tree_mdoc;
                    583:                        break;
1.173     schwarze  584:                case OUTT_MAN:
1.164     schwarze  585:                        curp->outmdoc = man_mdoc;
1.165     kristaps  586:                        curp->outman = man_man;
1.164     schwarze  587:                        break;
1.173     schwarze  588:                case OUTT_PDF:
1.111     kristaps  589:                        /* FALLTHROUGH */
1.173     schwarze  590:                case OUTT_ASCII:
1.111     kristaps  591:                        /* FALLTHROUGH */
1.173     schwarze  592:                case OUTT_UTF8:
1.163     kristaps  593:                        /* FALLTHROUGH */
1.173     schwarze  594:                case OUTT_LOCALE:
1.162     kristaps  595:                        /* FALLTHROUGH */
1.173     schwarze  596:                case OUTT_PS:
1.111     kristaps  597:                        curp->outman = terminal_man;
                    598:                        curp->outmdoc = terminal_mdoc;
                    599:                        break;
                    600:                default:
                    601:                        break;
                    602:                }
                    603:        }
                    604:
1.171     schwarze  605:        mparse_result(curp->mp, &mdoc, &man, NULL);
1.154     kristaps  606:
1.111     kristaps  607:        /* Execute the out device, if it exists. */
                    608:
1.154     kristaps  609:        if (man && curp->outman)
                    610:                (*curp->outman)(curp->outdata, man);
                    611:        if (mdoc && curp->outmdoc)
                    612:                (*curp->outmdoc)(curp->outdata, mdoc);
1.111     kristaps  613:
                    614:  cleanup:
1.112     kristaps  615:
1.154     kristaps  616:        mparse_reset(curp->mp);
1.111     kristaps  617:
1.154     kristaps  618:        if (*level < rc)
                    619:                *level = rc;
1.186     schwarze  620: }
                    621:
1.205     schwarze  622: #if HAVE_SQLITE3
1.186     schwarze  623: static enum mandoclevel
1.197     schwarze  624: passthrough(const char *file, int fd, int synopsis_only)
1.186     schwarze  625: {
1.197     schwarze  626:        const char       synb[] = "S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS";
                    627:        const char       synr[] = "SYNOPSIS";
                    628:
                    629:        FILE            *stream;
1.186     schwarze  630:        const char      *syscall;
1.197     schwarze  631:        char            *line;
                    632:        size_t           len, off;
                    633:        ssize_t          nw;
                    634:        int              print;
                    635:
                    636:        if ((stream = fdopen(fd, "r")) == NULL) {
                    637:                close(fd);
                    638:                syscall = "fdopen";
                    639:                goto fail;
                    640:        }
1.186     schwarze  641:
1.197     schwarze  642:        print = 0;
                    643:        while ((line = fgetln(stream, &len)) != NULL) {
                    644:                if (synopsis_only) {
                    645:                        if (print) {
                    646:                                if ( ! isspace((unsigned char)*line))
                    647:                                        goto done;
                    648:                                while (len &&
                    649:                                    isspace((unsigned char)*line)) {
                    650:                                        line++;
                    651:                                        len--;
                    652:                                }
                    653:                        } else {
                    654:                                if ((len == sizeof(synb) &&
                    655:                                     ! strncmp(line, synb, len - 1)) ||
                    656:                                    (len == sizeof(synr) &&
                    657:                                     ! strncmp(line, synr, len - 1)))
                    658:                                        print = 1;
                    659:                                continue;
                    660:                        }
                    661:                }
                    662:                for (off = 0; off < len; off += nw)
                    663:                        if ((nw = write(STDOUT_FILENO, line + off,
                    664:                            len - off)) == -1 || nw == 0) {
                    665:                                fclose(stream);
1.186     schwarze  666:                                syscall = "write";
                    667:                                goto fail;
                    668:                        }
1.197     schwarze  669:        }
1.186     schwarze  670:
1.197     schwarze  671:        if (ferror(stream)) {
                    672:                fclose(stream);
                    673:                syscall = "fgetln";
                    674:                goto fail;
                    675:        }
1.193     schwarze  676:
1.197     schwarze  677: done:
                    678:        fclose(stream);
                    679:        return(MANDOCLEVEL_OK);
1.186     schwarze  680:
                    681: fail:
                    682:        fprintf(stderr, "%s: %s: SYSERR: %s: %s",
                    683:            progname, file, syscall, strerror(errno));
                    684:        return(MANDOCLEVEL_SYSERR);
1.194     schwarze  685: }
1.205     schwarze  686: #endif
1.194     schwarze  687:
                    688: static int
                    689: koptions(int *options, char *arg)
                    690: {
                    691:
                    692:        if ( ! strcmp(arg, "utf-8")) {
                    693:                *options |=  MPARSE_UTF8;
                    694:                *options &= ~MPARSE_LATIN1;
                    695:        } else if ( ! strcmp(arg, "iso-8859-1")) {
                    696:                *options |=  MPARSE_LATIN1;
                    697:                *options &= ~MPARSE_UTF8;
                    698:        } else if ( ! strcmp(arg, "us-ascii")) {
                    699:                *options &= ~(MPARSE_UTF8 | MPARSE_LATIN1);
                    700:        } else {
                    701:                fprintf(stderr, "%s: -K%s: Bad argument\n",
                    702:                    progname, arg);
                    703:                return(0);
                    704:        }
                    705:        return(1);
1.10      kristaps  706: }
                    707:
                    708: static int
1.170     schwarze  709: moptions(int *options, char *arg)
1.10      kristaps  710: {
                    711:
1.180     schwarze  712:        if (arg == NULL)
                    713:                /* nothing to do */;
                    714:        else if (0 == strcmp(arg, "doc"))
1.170     schwarze  715:                *options |= MPARSE_MDOC;
1.19      kristaps  716:        else if (0 == strcmp(arg, "andoc"))
1.170     schwarze  717:                /* nothing to do */;
1.17      kristaps  718:        else if (0 == strcmp(arg, "an"))
1.170     schwarze  719:                *options |= MPARSE_MAN;
1.10      kristaps  720:        else {
1.176     schwarze  721:                fprintf(stderr, "%s: -m%s: Bad argument\n",
                    722:                    progname, arg);
1.10      kristaps  723:                return(0);
                    724:        }
                    725:
                    726:        return(1);
1.1       kristaps  727: }
                    728:
                    729: static int
1.60      kristaps  730: toptions(struct curparse *curp, char *arg)
1.1       kristaps  731: {
                    732:
                    733:        if (0 == strcmp(arg, "ascii"))
1.60      kristaps  734:                curp->outtype = OUTT_ASCII;
                    735:        else if (0 == strcmp(arg, "lint")) {
                    736:                curp->outtype = OUTT_LINT;
1.103     schwarze  737:                curp->wlevel  = MANDOCLEVEL_WARNING;
1.154     kristaps  738:        } else if (0 == strcmp(arg, "tree"))
1.60      kristaps  739:                curp->outtype = OUTT_TREE;
1.164     schwarze  740:        else if (0 == strcmp(arg, "man"))
                    741:                curp->outtype = OUTT_MAN;
1.43      kristaps  742:        else if (0 == strcmp(arg, "html"))
1.60      kristaps  743:                curp->outtype = OUTT_HTML;
1.163     kristaps  744:        else if (0 == strcmp(arg, "utf8"))
                    745:                curp->outtype = OUTT_UTF8;
1.162     kristaps  746:        else if (0 == strcmp(arg, "locale"))
                    747:                curp->outtype = OUTT_LOCALE;
1.59      kristaps  748:        else if (0 == strcmp(arg, "xhtml"))
1.195     schwarze  749:                curp->outtype = OUTT_HTML;
1.85      kristaps  750:        else if (0 == strcmp(arg, "ps"))
                    751:                curp->outtype = OUTT_PS;
1.100     kristaps  752:        else if (0 == strcmp(arg, "pdf"))
                    753:                curp->outtype = OUTT_PDF;
1.1       kristaps  754:        else {
1.176     schwarze  755:                fprintf(stderr, "%s: -T%s: Bad argument\n",
                    756:                    progname, arg);
1.1       kristaps  757:                return(0);
                    758:        }
                    759:
                    760:        return(1);
                    761: }
                    762:
                    763: static int
1.103     schwarze  764: woptions(struct curparse *curp, char *arg)
1.1       kristaps  765: {
1.34      kristaps  766:        char            *v, *o;
1.173     schwarze  767:        const char      *toks[6];
1.1       kristaps  768:
1.103     schwarze  769:        toks[0] = "stop";
                    770:        toks[1] = "all";
                    771:        toks[2] = "warning";
                    772:        toks[3] = "error";
                    773:        toks[4] = "fatal";
                    774:        toks[5] = NULL;
1.1       kristaps  775:
1.34      kristaps  776:        while (*arg) {
                    777:                o = arg;
1.45      kristaps  778:                switch (getsubopt(&arg, UNCONST(toks), &v)) {
1.173     schwarze  779:                case 0:
1.103     schwarze  780:                        curp->wstop = 1;
1.1       kristaps  781:                        break;
1.173     schwarze  782:                case 1:
1.103     schwarze  783:                        /* FALLTHROUGH */
1.173     schwarze  784:                case 2:
1.103     schwarze  785:                        curp->wlevel = MANDOCLEVEL_WARNING;
1.1       kristaps  786:                        break;
1.173     schwarze  787:                case 3:
1.103     schwarze  788:                        curp->wlevel = MANDOCLEVEL_ERROR;
1.24      kristaps  789:                        break;
1.173     schwarze  790:                case 4:
1.103     schwarze  791:                        curp->wlevel = MANDOCLEVEL_FATAL;
1.50      kristaps  792:                        break;
1.1       kristaps  793:                default:
1.176     schwarze  794:                        fprintf(stderr, "%s: -W%s: Bad argument\n",
                    795:                            progname, o);
1.1       kristaps  796:                        return(0);
                    797:                }
1.34      kristaps  798:        }
1.1       kristaps  799:
                    800:        return(1);
                    801: }
                    802:
1.153     kristaps  803: static void
1.173     schwarze  804: mmsg(enum mandocerr t, enum mandoclevel lvl,
1.155     kristaps  805:                const char *file, int line, int col, const char *msg)
1.71      kristaps  806: {
1.177     schwarze  807:        const char      *mparse_msg;
1.103     schwarze  808:
1.175     schwarze  809:        fprintf(stderr, "%s: %s:", progname, file);
                    810:
                    811:        if (line)
                    812:                fprintf(stderr, "%d:%d:", line, col + 1);
                    813:
1.177     schwarze  814:        fprintf(stderr, " %s", mparse_strlevel(lvl));
                    815:
                    816:        if (NULL != (mparse_msg = mparse_strerror(t)))
                    817:                fprintf(stderr, ": %s", mparse_msg);
1.154     kristaps  818:
1.73      kristaps  819:        if (msg)
                    820:                fprintf(stderr, ": %s", msg);
1.154     kristaps  821:
1.73      kristaps  822:        fputc('\n', stderr);
1.183     schwarze  823: }
                    824:
                    825: static void
                    826: spawn_pager(void)
                    827: {
1.184     schwarze  828: #define MAX_PAGER_ARGS 16
                    829:        char            *argv[MAX_PAGER_ARGS];
                    830:        const char      *pager;
                    831:        char            *cp;
                    832:        int              fildes[2];
                    833:        int              argc;
1.183     schwarze  834:
                    835:        if (pipe(fildes) == -1) {
                    836:                fprintf(stderr, "%s: pipe: %s\n",
                    837:                    progname, strerror(errno));
                    838:                return;
                    839:        }
                    840:
                    841:        switch (fork()) {
                    842:        case -1:
                    843:                fprintf(stderr, "%s: fork: %s\n",
                    844:                    progname, strerror(errno));
                    845:                exit((int)MANDOCLEVEL_SYSERR);
                    846:        case 0:
                    847:                close(fildes[0]);
                    848:                if (dup2(fildes[1], STDOUT_FILENO) == -1) {
                    849:                        fprintf(stderr, "%s: dup output: %s\n",
                    850:                            progname, strerror(errno));
                    851:                        exit((int)MANDOCLEVEL_SYSERR);
                    852:                }
                    853:                return;
                    854:        default:
1.184     schwarze  855:                break;
                    856:        }
                    857:
                    858:        /* The original process becomes the pager. */
                    859:
                    860:        close(fildes[1]);
                    861:        if (dup2(fildes[0], STDIN_FILENO) == -1) {
                    862:                fprintf(stderr, "%s: dup input: %s\n",
                    863:                    progname, strerror(errno));
1.183     schwarze  864:                exit((int)MANDOCLEVEL_SYSERR);
                    865:        }
1.184     schwarze  866:
                    867:        pager = getenv("MANPAGER");
                    868:        if (pager == NULL || *pager == '\0')
                    869:                pager = getenv("PAGER");
                    870:        if (pager == NULL || *pager == '\0')
                    871:                pager = "/usr/bin/more -s";
                    872:        cp = mandoc_strdup(pager);
                    873:
                    874:        /*
                    875:         * Parse the pager command into words.
                    876:         * Intentionally do not do anything fancy here.
                    877:         */
                    878:
                    879:        argc = 0;
                    880:        while (argc + 1 < MAX_PAGER_ARGS) {
                    881:                argv[argc++] = cp;
                    882:                cp = strchr(cp, ' ');
                    883:                if (cp == NULL)
                    884:                        break;
                    885:                *cp++ = '\0';
                    886:                while (*cp == ' ')
                    887:                        cp++;
                    888:                if (*cp == '\0')
                    889:                        break;
                    890:        }
                    891:        argv[argc] = NULL;
                    892:
                    893:        /* Hand over to the pager. */
                    894:
                    895:        execvp(argv[0], argv);
                    896:        fprintf(stderr, "%s: exec: %s\n",
                    897:            progname, strerror(errno));
                    898:        exit((int)MANDOCLEVEL_SYSERR);
1.71      kristaps  899: }

CVSweb