1 /* $Id: apropos.c,v 1.27.2.1 2013/09/17 23:23:10 schwarze Exp $ */
3 * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
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.
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.
21 #include <sys/param.h>
29 #include "apropos_db.h"
33 static int cmp(const void *, const void *);
34 static void list(struct res *, size_t, void *);
36 static char *progname;
39 main(int argc, char *argv[])
43 struct manpaths paths;
47 char *defpaths, *auxpaths;
52 progname = strrchr(argv[0], '/');
58 whatis = (0 == strncmp(progname, "whatis", 6));
60 memset(&paths, 0, sizeof(struct manpaths));
61 memset(&opts, 0, sizeof(struct opts));
65 auxpaths = defpaths = NULL;
69 while (-1 != (ch = getopt(argc, argv, "C:M:m:S:s:")))
98 manpath_parse(&paths, conf_file, defpaths, auxpaths);
100 e = whatis ? termcomp(argc, argv, &terms) :
101 exprcomp(argc, argv, &terms);
104 fprintf(stderr, "%s: Bad expression\n", progname);
109 (paths.sz, paths.paths, &opts,
110 e, terms, NULL, &ressz, &res, list);
113 fprintf(stderr, "%s: Bad database\n", progname);
118 manpath_free(&paths);
121 return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
124 fprintf(stderr, "usage: %s [-C file] [-M path] [-m path] "
125 "[-S arch] [-s section]%s ...\n", progname,
126 whatis ? " name" : "\n expression");
127 return(EXIT_FAILURE);
132 list(struct res *res, size_t sz, void *arg)
136 qsort(res, sz, sizeof(struct res), cmp);
138 for (i = 0; i < sz; i++) {
139 if ( ! res[i].matched)
141 printf("%s(%s%s%s) - %.70s\n",
144 *res[i].arch ? "/" : "",
145 *res[i].arch ? res[i].arch : "",
151 cmp(const void *p1, const void *p2)
154 return(strcasecmp(((const struct res *)p1)->title,
155 ((const struct res *)p2)->title));