]> git.cameronkatri.com Git - mandoc.git/blob - apropos.c
Support -Ios='OpenBSD 5.1' to override uname(3) as the source of the
[mandoc.git] / apropos.c
1 /* $Id: apropos.c,v 1.30 2012/03/24 02:18:51 kristaps Exp $ */
2 /*
3 * Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
4 * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
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 */
18 #ifdef HAVE_CONFIG_H
19 #include "config.h"
20 #endif
21 #include <sys/param.h>
22
23 #include <assert.h>
24 #include <getopt.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include "apropos_db.h"
31 #include "mandoc.h"
32 #include "manpath.h"
33
34 #define SINGLETON(_res, _sz) \
35 ((_sz) && (_res)[0].matched && \
36 (1 == (_sz) || 0 == (_res)[1].matched))
37 #define EMPTYSET(_res, _sz) \
38 ((0 == (_sz)) || 0 == (_res)[0].matched)
39
40 static int cmp(const void *, const void *);
41 static void list(struct res *, size_t, void *);
42 static void usage(void);
43
44 static char *progname;
45
46 int
47 main(int argc, char *argv[])
48 {
49 int ch, rc, whatis, usecat;
50 struct res *res;
51 struct manpaths paths;
52 const char *prog;
53 pid_t pid;
54 char path[PATH_MAX];
55 int fds[2];
56 size_t terms, ressz, sz;
57 struct opts opts;
58 struct expr *e;
59 char *defpaths, *auxpaths, *conf_file, *cp;
60 extern int optind;
61 extern char *optarg;
62
63 progname = strrchr(argv[0], '/');
64 if (progname == NULL)
65 progname = argv[0];
66 else
67 ++progname;
68
69 whatis = 0 == strncmp(progname, "whatis", 6);
70
71 memset(&paths, 0, sizeof(struct manpaths));
72 memset(&opts, 0, sizeof(struct opts));
73
74 usecat = 0;
75 ressz = 0;
76 res = NULL;
77 auxpaths = defpaths = NULL;
78 conf_file = NULL;
79 e = NULL;
80 path[0] = '\0';
81
82 while (-1 != (ch = getopt(argc, argv, "C:M:m:S:s:")))
83 switch (ch) {
84 case ('C'):
85 conf_file = optarg;
86 break;
87 case ('M'):
88 defpaths = optarg;
89 break;
90 case ('m'):
91 auxpaths = optarg;
92 break;
93 case ('S'):
94 opts.arch = optarg;
95 break;
96 case ('s'):
97 opts.cat = optarg;
98 break;
99 default:
100 usage();
101 return(EXIT_FAILURE);
102 }
103
104 argc -= optind;
105 argv += optind;
106
107 if (0 == argc)
108 return(EXIT_SUCCESS);
109
110 rc = 0;
111
112 manpath_parse(&paths, conf_file, defpaths, auxpaths);
113
114 e = whatis ? termcomp(argc, argv, &terms) :
115 exprcomp(argc, argv, &terms);
116
117 if (NULL == e) {
118 fprintf(stderr, "%s: Bad expression\n", progname);
119 goto out;
120 }
121
122 rc = apropos_search
123 (paths.sz, paths.paths, &opts,
124 e, terms, NULL, &ressz, &res, list);
125
126 terms = 1;
127
128 if (0 == rc) {
129 fprintf(stderr, "%s: Bad database\n", progname);
130 goto out;
131 } else if ( ! isatty(STDOUT_FILENO) || EMPTYSET(res, ressz))
132 goto out;
133
134 if ( ! SINGLETON(res, ressz)) {
135 printf("Which manpage would you like [1]? ");
136 fflush(stdout);
137 if (NULL != (cp = fgetln(stdin, &sz)) &&
138 sz > 1 && '\n' == cp[--sz]) {
139 if ((ch = atoi(cp)) <= 0)
140 goto out;
141 terms = (size_t)ch;
142 }
143 }
144
145 if (--terms < ressz && res[terms].matched) {
146 chdir(paths.paths[res[terms].volume]);
147 strlcpy(path, res[terms].file, PATH_MAX);
148 usecat = RESTYPE_CAT == res[terms].type;
149 }
150 out:
151 manpath_free(&paths);
152 resfree(res, ressz);
153 exprfree(e);
154
155 if ('\0' == path[0])
156 return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
157
158 if (-1 == pipe(fds)) {
159 perror(NULL);
160 exit(EXIT_FAILURE);
161 }
162
163 if (-1 == (pid = fork())) {
164 perror(NULL);
165 exit(EXIT_FAILURE);
166 } else if (pid > 0) {
167 dup2(fds[0], STDIN_FILENO);
168 close(fds[1]);
169 prog = NULL != getenv("MANPAGER") ?
170 getenv("MANPAGER") :
171 (NULL != getenv("PAGER") ?
172 getenv("PAGER") : "more");
173 execlp(prog, prog, (char *)NULL);
174 perror(prog);
175 return(EXIT_FAILURE);
176 }
177
178 dup2(fds[1], STDOUT_FILENO);
179 close(fds[0]);
180 prog = usecat ? "cat" : "mandoc";
181 execlp(prog, prog, path, (char *)NULL);
182 perror(prog);
183 return(EXIT_FAILURE);
184 }
185
186 /* ARGSUSED */
187 static void
188 list(struct res *res, size_t sz, void *arg)
189 {
190 size_t i;
191
192 qsort(res, sz, sizeof(struct res), cmp);
193
194 if (EMPTYSET(res, sz) || SINGLETON(res, sz))
195 return;
196
197 if ( ! isatty(STDOUT_FILENO))
198 for (i = 0; i < sz && res[i].matched; i++)
199 printf("%s(%s%s%s) - %.70s\n",
200 res[i].title, res[i].cat,
201 *res[i].arch ? "/" : "",
202 *res[i].arch ? res[i].arch : "",
203 res[i].desc);
204 else
205 for (i = 0; i < sz && res[i].matched; i++)
206 printf("[%zu] %s(%s%s%s) - %.70s\n", i + 1,
207 res[i].title, res[i].cat,
208 *res[i].arch ? "/" : "",
209 *res[i].arch ? res[i].arch : "",
210 res[i].desc);
211 }
212
213 static int
214 cmp(const void *p1, const void *p2)
215 {
216 const struct res *r1 = p1;
217 const struct res *r2 = p2;
218
219 if (0 == r1->matched)
220 return(1);
221 else if (0 == r2->matched)
222 return(1);
223
224 return(strcasecmp(r1->title, r2->title));
225 }
226
227 static void
228 usage(void)
229 {
230
231 fprintf(stderr, "usage: %s "
232 "[-C file] "
233 "[-M manpath] "
234 "[-m manpath] "
235 "[-S arch] "
236 "[-s section] "
237 "expression ...\n",
238 progname);
239 }