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