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

Annotation of mandoc/apropos.c, Revision 1.39

1.39    ! schwarze    1: /*     $Id: apropos.c,v 1.38 2014/04/11 15:46:52 schwarze Exp $ */
1.1       kristaps    2: /*
1.31      kristaps    3:  * Copyright (c) 2012 Kristaps Dzonsons <kristaps@bsd.lv>
1.36      schwarze    4:  * Copyright (c) 2013 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
1.13      kristaps   18: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
                     20: #endif
1.27      kristaps   21: #include <sys/param.h>
1.13      kristaps   22:
1.1       kristaps   23: #include <assert.h>
                     24: #include <getopt.h>
1.33      kristaps   25: #include <stdint.h>
1.1       kristaps   26: #include <stdio.h>
                     27: #include <stdlib.h>
                     28: #include <string.h>
1.29      kristaps   29: #include <unistd.h>
1.1       kristaps   30:
1.18      kristaps   31: #include "manpath.h"
1.31      kristaps   32: #include "mansearch.h"
1.1       kristaps   33:
1.39    ! schwarze   34:
1.1       kristaps   35: int
                     36: main(int argc, char *argv[])
                     37: {
1.32      kristaps   38:        int              ch, whatis;
                     39:        struct mansearch search;
1.31      kristaps   40:        size_t           i, sz;
                     41:        struct manpage  *res;
1.15      kristaps   42:        struct manpaths  paths;
1.34      schwarze   43:        char            *defpaths, *auxpaths;
                     44:        char            *conf_file;
1.31      kristaps   45:        char            *progname;
1.37      schwarze   46:        const char      *outkey;
1.31      kristaps   47:        extern char     *optarg;
1.1       kristaps   48:        extern int       optind;
                     49:
                     50:        progname = strrchr(argv[0], '/');
                     51:        if (progname == NULL)
                     52:                progname = argv[0];
                     53:        else
                     54:                ++progname;
                     55:
1.34      schwarze   56:        whatis = (0 == strncmp(progname, "whatis", 6));
                     57:
1.15      kristaps   58:        memset(&paths, 0, sizeof(struct manpaths));
1.32      kristaps   59:        memset(&search, 0, sizeof(struct mansearch));
1.34      schwarze   60:
                     61:        auxpaths = defpaths = NULL;
                     62:        conf_file = NULL;
1.37      schwarze   63:        outkey = "Nd";
1.15      kristaps   64:
1.36      schwarze   65:        while (-1 != (ch = getopt(argc, argv, "C:M:m:O:S:s:")))
1.1       kristaps   66:                switch (ch) {
1.39    ! schwarze   67:                case 'C':
1.24      schwarze   68:                        conf_file = optarg;
                     69:                        break;
1.39    ! schwarze   70:                case 'M':
1.17      kristaps   71:                        defpaths = optarg;
                     72:                        break;
1.39    ! schwarze   73:                case 'm':
1.17      kristaps   74:                        auxpaths = optarg;
1.15      kristaps   75:                        break;
1.39    ! schwarze   76:                case 'O':
1.36      schwarze   77:                        outkey = optarg;
                     78:                        break;
1.39    ! schwarze   79:                case 'S':
1.32      kristaps   80:                        search.arch = optarg;
1.1       kristaps   81:                        break;
1.39    ! schwarze   82:                case 's':
1.32      kristaps   83:                        search.sec = optarg;
1.1       kristaps   84:                        break;
                     85:                default:
1.31      kristaps   86:                        goto usage;
1.1       kristaps   87:                }
                     88:
                     89:        argc -= optind;
                     90:        argv += optind;
                     91:
1.31      kristaps   92:        if (0 == argc)
                     93:                goto usage;
1.15      kristaps   94:
1.32      kristaps   95:        search.deftype = whatis ? TYPE_Nm : TYPE_Nm | TYPE_Nd;
                     96:        search.flags = whatis ? MANSEARCH_WHATIS : 0;
                     97:
1.24      schwarze   98:        manpath_parse(&paths, conf_file, defpaths, auxpaths);
1.38      schwarze   99:        mansearch_setup(1);
1.36      schwarze  100:        ch = mansearch(&search, &paths, argc, argv, outkey, &res, &sz);
1.18      kristaps  101:        manpath_free(&paths);
1.28      kristaps  102:
1.31      kristaps  103:        if (0 == ch)
                    104:                goto usage;
1.28      kristaps  105:
1.31      kristaps  106:        for (i = 0; i < sz; i++) {
1.36      schwarze  107:                printf("%s - %s\n", res[i].names,
                    108:                    NULL == res[i].output ? "" : res[i].output);
1.35      schwarze  109:                free(res[i].file);
                    110:                free(res[i].names);
1.36      schwarze  111:                free(res[i].output);
1.28      kristaps  112:        }
                    113:
1.31      kristaps  114:        free(res);
1.38      schwarze  115:        mansearch_setup(0);
1.31      kristaps  116:        return(sz ? EXIT_SUCCESS : EXIT_FAILURE);
                    117: usage:
1.34      schwarze  118:        fprintf(stderr, "usage: %s [-C file] [-M path] [-m path] "
1.36      schwarze  119:                        "[-O outkey] "
1.34      schwarze  120:                        "[-S arch] [-s section]%s ...\n", progname,
                    121:                        whatis ? " name" : "\n               expression");
1.28      kristaps  122:        return(EXIT_FAILURE);
1.1       kristaps  123: }

CVSweb