]> git.cameronkatri.com Git - mandoc.git/blob - apropos.c
17435768b4bf3635a6581b9bc9635d2e5d6a3e0b
[mandoc.git] / apropos.c
1 /* $Id: apropos.c,v 1.16 2011/11/20 16:29:50 kristaps Exp $ */
2 /*
3 * Copyright (c) 2011 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
22 #include <assert.h>
23 #include <getopt.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "apropos_db.h"
30 #include "mandoc.h"
31
32 /*
33 * List of paths to be searched for manual databases.
34 */
35 struct manpaths {
36 int sz;
37 char **paths;
38 };
39
40 static int cmp(const void *, const void *);
41 static void list(struct res *, size_t, void *);
42 static void manpath_add(struct manpaths *, const char *);
43 static void manpath_parse(struct manpaths *, char *);
44 static void usage(void);
45
46 static char *progname;
47
48 int
49 main(int argc, char *argv[])
50 {
51 int i, ch, rc;
52 struct manpaths paths;
53 size_t terms;
54 struct opts opts;
55 struct expr *e;
56 extern int optind;
57 extern char *optarg;
58
59 progname = strrchr(argv[0], '/');
60 if (progname == NULL)
61 progname = argv[0];
62 else
63 ++progname;
64
65 memset(&paths, 0, sizeof(struct manpaths));
66 memset(&opts, 0, sizeof(struct opts));
67
68 e = NULL;
69 rc = 0;
70
71 while (-1 != (ch = getopt(argc, argv, "m:S:s:")))
72 switch (ch) {
73 case ('m'):
74 manpath_parse(&paths, optarg);
75 break;
76 case ('S'):
77 opts.arch = optarg;
78 break;
79 case ('s'):
80 opts.cat = optarg;
81 break;
82 default:
83 usage();
84 goto out;
85 }
86
87 argc -= optind;
88 argv += optind;
89
90 if (0 == argc) {
91 rc = 1;
92 goto out;
93 }
94
95 /*
96 * Let MANPATH override our default paths.
97 */
98
99 if (NULL != getenv("MANPATH"))
100 manpath_add(&paths, getenv("MANPATH"));
101 else
102 manpath_add(&paths, ".");
103
104 if (NULL == (e = exprcomp(argc, argv, &terms))) {
105 fprintf(stderr, "%s: Bad expression\n", progname);
106 goto out;
107 }
108
109 rc = apropos_search
110 (paths.sz, paths.paths,
111 &opts, e, terms, NULL, list);
112
113 if (0 == rc)
114 fprintf(stderr, "%s: Error reading "
115 "manual database\n", progname);
116
117 out:
118 for (i = 0; i < paths.sz; i++)
119 free(paths.paths[i]);
120
121 free(paths.paths);
122 exprfree(e);
123
124 return(rc ? EXIT_SUCCESS : EXIT_FAILURE);
125 }
126
127 /* ARGSUSED */
128 static void
129 list(struct res *res, size_t sz, void *arg)
130 {
131 int i;
132
133 qsort(res, sz, sizeof(struct res), cmp);
134
135 for (i = 0; i < (int)sz; i++)
136 printf("%s(%s%s%s) - %s\n", res[i].title,
137 res[i].cat,
138 *res[i].arch ? "/" : "",
139 *res[i].arch ? res[i].arch : "",
140 res[i].desc);
141 }
142
143 static int
144 cmp(const void *p1, const void *p2)
145 {
146
147 return(strcmp(((const struct res *)p1)->title,
148 ((const struct res *)p2)->title));
149 }
150
151 static void
152 usage(void)
153 {
154
155 fprintf(stderr, "usage: %s "
156 "[-m dirs] "
157 "[-S arch] "
158 "[-s section] "
159 "expression...\n", progname);
160 }
161
162 /*
163 * Parse a FULL pathname from a colon-separated list of arrays.
164 */
165 static void
166 manpath_parse(struct manpaths *dirs, char *path)
167 {
168 char *dir;
169
170 for (dir = strtok(path, ":"); dir; dir = strtok(NULL, ":"))
171 manpath_add(dirs, dir);
172 }
173
174 /*
175 * Add a directory to the array, ignoring bad directories.
176 * Grow the array one-by-one for simplicity's sake.
177 */
178 static void
179 manpath_add(struct manpaths *dirs, const char *dir)
180 {
181 char buf[PATH_MAX];
182 char *cp;
183
184 if (NULL == (cp = realpath(dir, buf)))
185 return;
186
187 dirs->paths = mandoc_realloc
188 (dirs->paths,
189 ((size_t)dirs->sz + 1) * sizeof(char *));
190
191 dirs->paths[dirs->sz++] = mandoc_strdup(cp);
192 }