]> git.cameronkatri.com Git - mandoc.git/blob - apropos.c
Small tweaks for release.
[mandoc.git] / apropos.c
1 /* $Id: apropos.c,v 1.29 2012/03/24 02:07:32 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 strlcpy(path, res[terms].file, PATH_MAX);
147 usecat = RESTYPE_CAT == res[terms].type;
148 }
149 out:
150 manpath_free(&paths);
151 resfree(res, ressz);
152 exprfree(e);
153
154 if ('\0' == path[0])
155 return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
156
157 if (-1 == pipe(fds)) {
158 perror(NULL);
159 exit(EXIT_FAILURE);
160 }
161
162 if (-1 == (pid = fork())) {
163 perror(NULL);
164 exit(EXIT_FAILURE);
165 } else if (pid > 0) {
166 dup2(fds[0], STDIN_FILENO);
167 close(fds[1]);
168 prog = NULL != getenv("MANPAGER") ?
169 getenv("MANPAGER") :
170 (NULL != getenv("PAGER") ?
171 getenv("PAGER") : "more");
172 execlp(prog, prog, (char *)NULL);
173 perror(prog);
174 return(EXIT_FAILURE);
175 }
176
177 dup2(fds[1], STDOUT_FILENO);
178 close(fds[0]);
179 prog = usecat ? "cat" : "mandoc";
180 execlp(prog, prog, path, (char *)NULL);
181 perror(prog);
182 return(EXIT_FAILURE);
183 }
184
185 /* ARGSUSED */
186 static void
187 list(struct res *res, size_t sz, void *arg)
188 {
189 size_t i;
190
191 qsort(res, sz, sizeof(struct res), cmp);
192
193 if (EMPTYSET(res, sz) || SINGLETON(res, sz))
194 return;
195
196 if ( ! isatty(STDOUT_FILENO))
197 for (i = 0; i < sz && res[i].matched; i++)
198 printf("%s(%s%s%s) - %.70s\n",
199 res[i].title, res[i].cat,
200 *res[i].arch ? "/" : "",
201 *res[i].arch ? res[i].arch : "",
202 res[i].desc);
203 else
204 for (i = 0; i < sz && res[i].matched; i++)
205 printf("[%zu] %s(%s%s%s) - %.70s\n", i + 1,
206 res[i].title, res[i].cat,
207 *res[i].arch ? "/" : "",
208 *res[i].arch ? res[i].arch : "",
209 res[i].desc);
210 }
211
212 static int
213 cmp(const void *p1, const void *p2)
214 {
215 const struct res *r1 = p1;
216 const struct res *r2 = p2;
217
218 if (0 == r1->matched)
219 return(1);
220 else if (0 == r2->matched)
221 return(1);
222
223 return(strcasecmp(r1->title, r2->title));
224 }
225
226 static void
227 usage(void)
228 {
229
230 fprintf(stderr, "usage: %s "
231 "[-C file] "
232 "[-M manpath] "
233 "[-m manpath] "
234 "[-S arch] "
235 "[-s section] "
236 "expression ...\n",
237 progname);
238 }