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