apropos.c revision 1.6.2.2 1 1.6.2.2 yamt /* $NetBSD: apropos.c,v 1.6.2.2 2012/04/17 00:09:49 yamt Exp $ */
2 1.6.2.2 yamt /*-
3 1.6.2.2 yamt * Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadhyay (at) gmail.com>
4 1.6.2.2 yamt * All rights reserved.
5 1.6.2.2 yamt *
6 1.6.2.2 yamt * This code was developed as part of Google's Summer of Code 2011 program.
7 1.6.2.2 yamt *
8 1.6.2.2 yamt * Redistribution and use in source and binary forms, with or without
9 1.6.2.2 yamt * modification, are permitted provided that the following conditions
10 1.6.2.2 yamt * are met:
11 1.6.2.2 yamt *
12 1.6.2.2 yamt * 1. Redistributions of source code must retain the above copyright
13 1.6.2.2 yamt * notice, this list of conditions and the following disclaimer.
14 1.6.2.2 yamt * 2. Redistributions in binary form must reproduce the above copyright
15 1.6.2.2 yamt * notice, this list of conditions and the following disclaimer in
16 1.6.2.2 yamt * the documentation and/or other materials provided with the
17 1.6.2.2 yamt * distribution.
18 1.6.2.2 yamt *
19 1.6.2.2 yamt * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 1.6.2.2 yamt * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 1.6.2.2 yamt * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 1.6.2.2 yamt * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 1.6.2.2 yamt * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24 1.6.2.2 yamt * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
25 1.6.2.2 yamt * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 1.6.2.2 yamt * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 1.6.2.2 yamt * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 1.6.2.2 yamt * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 1.6.2.2 yamt * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 1.6.2.2 yamt * SUCH DAMAGE.
31 1.6.2.2 yamt */
32 1.6.2.2 yamt
33 1.6.2.2 yamt #include <sys/cdefs.h>
34 1.6.2.2 yamt __RCSID("$NetBSD: apropos.c,v 1.6.2.2 2012/04/17 00:09:49 yamt Exp $");
35 1.6.2.2 yamt
36 1.6.2.2 yamt #include <err.h>
37 1.6.2.2 yamt #include <search.h>
38 1.6.2.2 yamt #include <stdio.h>
39 1.6.2.2 yamt #include <stdlib.h>
40 1.6.2.2 yamt #include <string.h>
41 1.6.2.2 yamt #include <unistd.h>
42 1.6.2.2 yamt #include <util.h>
43 1.6.2.2 yamt
44 1.6.2.2 yamt #include "apropos-utils.h"
45 1.6.2.2 yamt #include "sqlite3.h"
46 1.6.2.2 yamt
47 1.6.2.2 yamt typedef struct apropos_flags {
48 1.6.2.2 yamt int sec_nums[SECMAX];
49 1.6.2.2 yamt int nresults;
50 1.6.2.2 yamt int pager;
51 1.6.2.2 yamt int no_context;
52 1.6.2.2 yamt const char *machine;
53 1.6.2.2 yamt } apropos_flags;
54 1.6.2.2 yamt
55 1.6.2.2 yamt typedef struct callback_data {
56 1.6.2.2 yamt int count;
57 1.6.2.2 yamt FILE *out;
58 1.6.2.2 yamt apropos_flags *aflags;
59 1.6.2.2 yamt } callback_data;
60 1.6.2.2 yamt
61 1.6.2.2 yamt static char *remove_stopwords(const char *);
62 1.6.2.2 yamt static int query_callback(void *, const char * , const char *, const char *,
63 1.6.2.2 yamt const char *, size_t);
64 1.6.2.2 yamt __dead static void usage(void);
65 1.6.2.2 yamt
66 1.6.2.2 yamt #define _PATH_PAGER "/usr/bin/more -s"
67 1.6.2.2 yamt
68 1.6.2.2 yamt int
69 1.6.2.2 yamt main(int argc, char *argv[])
70 1.6.2.2 yamt {
71 1.6.2.2 yamt #ifdef NOTYET
72 1.6.2.2 yamt static const char *snippet_args[] = {"\033[1m", "\033[0m", "..."};
73 1.6.2.2 yamt #endif
74 1.6.2.2 yamt query_args args;
75 1.6.2.2 yamt char *query = NULL; // the user query
76 1.6.2.2 yamt char *errmsg = NULL;
77 1.6.2.2 yamt char *str;
78 1.6.2.2 yamt int ch, rc = 0;
79 1.6.2.2 yamt int s;
80 1.6.2.2 yamt callback_data cbdata;
81 1.6.2.2 yamt cbdata.out = stdout; // the default output stream
82 1.6.2.2 yamt cbdata.count = 0;
83 1.6.2.2 yamt apropos_flags aflags;
84 1.6.2.2 yamt cbdata.aflags = &aflags;
85 1.6.2.2 yamt sqlite3 *db;
86 1.6.2.2 yamt setprogname(argv[0]);
87 1.6.2.2 yamt if (argc < 2)
88 1.6.2.2 yamt usage();
89 1.6.2.2 yamt
90 1.6.2.2 yamt memset(&aflags, 0, sizeof(aflags));
91 1.6.2.2 yamt
92 1.6.2.2 yamt /*If the user specifies a section number as an option, the corresponding
93 1.6.2.2 yamt * index element in sec_nums is set to the string representing that
94 1.6.2.2 yamt * section number.
95 1.6.2.2 yamt */
96 1.6.2.2 yamt while ((ch = getopt(argc, argv, "123456789Ccn:pS:s:")) != -1) {
97 1.6.2.2 yamt switch (ch) {
98 1.6.2.2 yamt case '1':
99 1.6.2.2 yamt case '2':
100 1.6.2.2 yamt case '3':
101 1.6.2.2 yamt case '4':
102 1.6.2.2 yamt case '5':
103 1.6.2.2 yamt case '6':
104 1.6.2.2 yamt case '7':
105 1.6.2.2 yamt case '8':
106 1.6.2.2 yamt case '9':
107 1.6.2.2 yamt aflags.sec_nums[ch - '1'] = 1;
108 1.6.2.2 yamt break;
109 1.6.2.2 yamt case 'C':
110 1.6.2.2 yamt aflags.no_context = 1;
111 1.6.2.2 yamt break;
112 1.6.2.2 yamt case 'c':
113 1.6.2.2 yamt aflags.no_context = 0;
114 1.6.2.2 yamt break;
115 1.6.2.2 yamt case 'n':
116 1.6.2.2 yamt aflags.nresults = atoi(optarg);
117 1.6.2.2 yamt break;
118 1.6.2.2 yamt case 'p': //user wants to view more than 10 results and page them
119 1.6.2.2 yamt aflags.pager = 1;
120 1.6.2.2 yamt aflags.nresults = -1; // Fetch all records
121 1.6.2.2 yamt break;
122 1.6.2.2 yamt case 'S':
123 1.6.2.2 yamt aflags.machine = optarg;
124 1.6.2.2 yamt break;
125 1.6.2.2 yamt case 's':
126 1.6.2.2 yamt s = atoi(optarg);
127 1.6.2.2 yamt if (s < 1 || s > 9)
128 1.6.2.2 yamt errx(EXIT_FAILURE, "Invalid section");
129 1.6.2.2 yamt aflags.sec_nums[s - 1] = 1;
130 1.6.2.2 yamt break;
131 1.6.2.2 yamt case '?':
132 1.6.2.2 yamt default:
133 1.6.2.2 yamt usage();
134 1.6.2.2 yamt }
135 1.6.2.2 yamt }
136 1.6.2.2 yamt
137 1.6.2.2 yamt argc -= optind;
138 1.6.2.2 yamt argv += optind;
139 1.6.2.2 yamt
140 1.6.2.2 yamt if (!argc)
141 1.6.2.2 yamt usage();
142 1.6.2.2 yamt
143 1.6.2.2 yamt str = NULL;
144 1.6.2.2 yamt while (argc--)
145 1.6.2.2 yamt concat(&str, *argv++);
146 1.6.2.2 yamt /* Eliminate any stopwords from the query */
147 1.6.2.2 yamt query = remove_stopwords(lower(str));
148 1.6.2.2 yamt free(str);
149 1.6.2.2 yamt
150 1.6.2.2 yamt /* if any error occured in remove_stopwords, exit */
151 1.6.2.2 yamt if (query == NULL)
152 1.6.2.2 yamt errx(EXIT_FAILURE, "Try using more relevant keywords");
153 1.6.2.2 yamt
154 1.6.2.2 yamt if ((db = init_db(MANDB_READONLY)) == NULL)
155 1.6.2.2 yamt exit(EXIT_FAILURE);
156 1.6.2.2 yamt
157 1.6.2.2 yamt /* If user wants to page the output, then set some settings */
158 1.6.2.2 yamt if (aflags.pager) {
159 1.6.2.2 yamt const char *pager = getenv("PAGER");
160 1.6.2.2 yamt if (pager == NULL)
161 1.6.2.2 yamt pager = _PATH_PAGER;
162 1.6.2.2 yamt /* Open a pipe to the pager */
163 1.6.2.2 yamt if ((cbdata.out = popen(pager, "w")) == NULL) {
164 1.6.2.2 yamt close_db(db);
165 1.6.2.2 yamt err(EXIT_FAILURE, "pipe failed");
166 1.6.2.2 yamt }
167 1.6.2.2 yamt }
168 1.6.2.2 yamt
169 1.6.2.2 yamt args.search_str = query;
170 1.6.2.2 yamt args.sec_nums = aflags.sec_nums;
171 1.6.2.2 yamt args.nrec = aflags.nresults ? aflags.nresults : 10;
172 1.6.2.2 yamt args.offset = 0;
173 1.6.2.2 yamt args.machine = aflags.machine;
174 1.6.2.2 yamt args.callback = &query_callback;
175 1.6.2.2 yamt args.callback_data = &cbdata;
176 1.6.2.2 yamt args.errmsg = &errmsg;
177 1.6.2.2 yamt
178 1.6.2.2 yamt #ifdef NOTYET
179 1.6.2.2 yamt rc = run_query(db, snippet_args, &args);
180 1.6.2.2 yamt #else
181 1.6.2.2 yamt rc = run_query_pager(db, &args);
182 1.6.2.2 yamt #endif
183 1.6.2.2 yamt
184 1.6.2.2 yamt free(query);
185 1.6.2.2 yamt close_db(db);
186 1.6.2.2 yamt if (errmsg) {
187 1.6.2.2 yamt warnx("%s", errmsg);
188 1.6.2.2 yamt free(errmsg);
189 1.6.2.2 yamt exit(EXIT_FAILURE);
190 1.6.2.2 yamt }
191 1.6.2.2 yamt
192 1.6.2.2 yamt if (rc < 0) {
193 1.6.2.2 yamt /* Something wrong with the database. Exit */
194 1.6.2.2 yamt exit(EXIT_FAILURE);
195 1.6.2.2 yamt }
196 1.6.2.2 yamt
197 1.6.2.2 yamt if (cbdata.count == 0) {
198 1.6.2.2 yamt warnx("No relevant results obtained.\n"
199 1.6.2.2 yamt "Please make sure that you spelled all the terms correctly "
200 1.6.2.2 yamt "or try using better keywords.");
201 1.6.2.2 yamt }
202 1.6.2.2 yamt return 0;
203 1.6.2.2 yamt }
204 1.6.2.2 yamt
205 1.6.2.2 yamt /*
206 1.6.2.2 yamt * query_callback --
207 1.6.2.2 yamt * Callback function for run_query.
208 1.6.2.2 yamt * It simply outputs the results from do_query. If the user specified the -p
209 1.6.2.2 yamt * option, then the output is sent to a pager, otherwise stdout is the default
210 1.6.2.2 yamt * output stream.
211 1.6.2.2 yamt */
212 1.6.2.2 yamt static int
213 1.6.2.2 yamt query_callback(void *data, const char *section, const char *name,
214 1.6.2.2 yamt const char *name_desc, const char *snippet, size_t snippet_length)
215 1.6.2.2 yamt {
216 1.6.2.2 yamt callback_data *cbdata = (callback_data *) data;
217 1.6.2.2 yamt FILE *out = cbdata->out;
218 1.6.2.2 yamt cbdata->count++;
219 1.6.2.2 yamt fprintf(out, "%s (%s)\t%s\n", name, section, name_desc);
220 1.6.2.2 yamt
221 1.6.2.2 yamt if (cbdata->aflags->no_context == 0)
222 1.6.2.2 yamt fprintf(out, "%s\n\n", snippet);
223 1.6.2.2 yamt
224 1.6.2.2 yamt return 0;
225 1.6.2.2 yamt }
226 1.6.2.2 yamt
227 1.6.2.2 yamt #include "stopwords.c"
228 1.6.2.2 yamt
229 1.6.2.2 yamt /*
230 1.6.2.2 yamt * remove_stopwords--
231 1.6.2.2 yamt * Scans the query and removes any stop words from it.
232 1.6.2.2 yamt * Returns the modified query or NULL, if it contained only stop words.
233 1.6.2.2 yamt */
234 1.6.2.2 yamt
235 1.6.2.2 yamt static char *
236 1.6.2.2 yamt remove_stopwords(const char *query)
237 1.6.2.2 yamt {
238 1.6.2.2 yamt size_t len, idx;
239 1.6.2.2 yamt char *output, *buf;
240 1.6.2.2 yamt const char *sep, *next;
241 1.6.2.2 yamt
242 1.6.2.2 yamt output = buf = emalloc(strlen(query) + 1);
243 1.6.2.2 yamt
244 1.6.2.2 yamt for (; query[0] != '\0'; query = next) {
245 1.6.2.2 yamt sep = strchr(query, ' ');
246 1.6.2.2 yamt if (sep == NULL) {
247 1.6.2.2 yamt len = strlen(query);
248 1.6.2.2 yamt next = query + len;
249 1.6.2.2 yamt } else {
250 1.6.2.2 yamt len = sep - query;
251 1.6.2.2 yamt next = sep + 1;
252 1.6.2.2 yamt }
253 1.6.2.2 yamt if (len == 0)
254 1.6.2.2 yamt continue;
255 1.6.2.2 yamt idx = stopwords_hash(query, len);
256 1.6.2.2 yamt if (memcmp(stopwords[idx], query, len) == 0 &&
257 1.6.2.2 yamt stopwords[idx][len] == '\0')
258 1.6.2.2 yamt continue;
259 1.6.2.2 yamt memcpy(buf, query, len);
260 1.6.2.2 yamt buf += len;
261 1.6.2.2 yamt *buf++ = ' ';
262 1.6.2.2 yamt }
263 1.6.2.2 yamt
264 1.6.2.2 yamt if (output == buf) {
265 1.6.2.2 yamt free(output);
266 1.6.2.2 yamt return NULL;
267 1.6.2.2 yamt }
268 1.6.2.2 yamt buf[-1] = '\0';
269 1.6.2.2 yamt return output;
270 1.6.2.2 yamt }
271 1.6.2.2 yamt
272 1.6.2.2 yamt /*
273 1.6.2.2 yamt * usage --
274 1.6.2.2 yamt * print usage message and die
275 1.6.2.2 yamt */
276 1.6.2.2 yamt static void
277 1.6.2.2 yamt usage(void)
278 1.6.2.2 yamt {
279 1.6.2.2 yamt fprintf(stderr,
280 1.6.2.2 yamt "Usage: %s [-n Number of records] [-p] [-123456789] [-S machine] query\n",
281 1.6.2.2 yamt getprogname());
282 1.6.2.2 yamt exit(1);
283 1.6.2.2 yamt }
284