Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 305 lines (284 sloc) 9.155 kB
93cff0e @abhinav-upadhyay Added license clauses
authored
1 /*-
327bf46 @abhinav-upadhyay Updated copyright notice and added credits to Google
authored
2 * Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadhyay@gmail.com>
93cff0e @abhinav-upadhyay Added license clauses
authored
3 * All rights reserved.
4 *
327bf46 @abhinav-upadhyay Updated copyright notice and added credits to Google
authored
5 * This code was developed as part of Google's Summer of Code 2011 program.
6 * Thanks to Google for sponsoring.
93cff0e @abhinav-upadhyay Added license clauses
authored
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:
327bf46 @abhinav-upadhyay Updated copyright notice and added credits to Google
authored
11 *
93cff0e @abhinav-upadhyay Added license clauses
authored
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
327bf46 @abhinav-upadhyay Updated copyright notice and added credits to Google
authored
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
93cff0e @abhinav-upadhyay Added license clauses
authored
18 *
327bf46 @abhinav-upadhyay Updated copyright notice and added credits to Google
authored
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.
93cff0e @abhinav-upadhyay Added license clauses
authored
31 */
32
7e87787 @abhinav-upadhyay Added support for incremental updation of the database. First of all …
authored
33 #include <err.h>
85f6136 @abhinav-upadhyay apropos.c: Implemented remove_stopwords to remove stopwords from the …
authored
34 #include <search.h>
e653ec9 @abhinav-upadhyay apropos.c: Wrote a bare bones implementation of apropos.
authored
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
b846fd2 @abhinav-upadhyay Implemented option in search within a specific section. One can speci…
authored
38 #include <unistd.h>
1ce5a46 @abhinav-upadhyay Replaced malloc/strdup/asprintf and similar memory allocation calls w…
authored
39 #include <util.h>
e653ec9 @abhinav-upadhyay apropos.c: Wrote a bare bones implementation of apropos.
authored
40
10a1cfb @abhinav-upadhyay Separated out utility functions out of makemandb.c and put them in a …
authored
41 #include "apropos-utils.h"
76b4569 @abhinav-upadhyay Implemented a stopword tokenizer by extended the porter tokenizer fro…
authored
42 #include "fts3_tokenizer.h"
e653ec9 @abhinav-upadhyay apropos.c: Wrote a bare bones implementation of apropos.
authored
43 #include "sqlite3.h"
76b4569 @abhinav-upadhyay Implemented a stopword tokenizer by extended the porter tokenizer fro…
authored
44 #include "stopword_tokenizer.h"
e653ec9 @abhinav-upadhyay apropos.c: Wrote a bare bones implementation of apropos.
authored
45
f1187fa @abhinav-upadhyay apropos.c: Added a new option 'p' which would remove the limit of onl…
authored
46 typedef struct apropos_flags {
223e846 @abhinav-upadhyay Added few descriptive comments and fixed some code formatting
authored
47 const char *sec_nums[SECMAX];
48 int pager;
f1187fa @abhinav-upadhyay apropos.c: Added a new option 'p' which would remove the limit of onl…
authored
49 } apropos_flags;
50
da801f2 @abhinav-upadhyay Display an error message in case no results are obtained
authored
51 typedef struct callback_data {
52 int count;
53 FILE *out;
54 } callback_data;
55
85f6136 @abhinav-upadhyay apropos.c: Implemented remove_stopwords to remove stopwords from the …
authored
56 static void remove_stopwords(char **);
a19c646 @abhinav-upadhyay Implemented do_query in the form of a generic library function in apr…
authored
57 static int query_callback(void *, int , char **, char **);
e653ec9 @abhinav-upadhyay apropos.c: Wrote a bare bones implementation of apropos.
authored
58 static void usage(void);
59
60 int
61 main(int argc, char *argv[])
62 {
63 char *query = NULL; // the user query
b846fd2 @abhinav-upadhyay Implemented option in search within a specific section. One can speci…
authored
64 char ch;
a19c646 @abhinav-upadhyay Implemented do_query in the form of a generic library function in apr…
authored
65 char *errmsg = NULL;
da801f2 @abhinav-upadhyay Display an error message in case no results are obtained
authored
66 callback_data cbdata;
38fce5b @abhinav-upadhyay Fix the apropos-utils API by changing the type of the nrec element of…
authored
67 int nrec = 10; // The number of records to fetch from db
a19c646 @abhinav-upadhyay Implemented do_query in the form of a generic library function in apr…
authored
68 const char *snippet_args[] = {"\033[1m", "\033[0m", "..."};
da801f2 @abhinav-upadhyay Display an error message in case no results are obtained
authored
69 cbdata.out = stdout; // the default stream for the search output
70 cbdata.count = 0;
5c25ec9 @abhinav-upadhyay makemandb.c: Fixed a regression caused by last commit, all rows from …
authored
71 apropos_flags aflags = {{0}, 0};
a19c646 @abhinav-upadhyay Implemented do_query in the form of a generic library function in apr…
authored
72 sqlite3 *db;
b846fd2 @abhinav-upadhyay Implemented option in search within a specific section. One can speci…
authored
73 setprogname(argv[0]);
e653ec9 @abhinav-upadhyay apropos.c: Wrote a bare bones implementation of apropos.
authored
74 if (argc < 2)
75 usage();
76
b66c9b1 @abhinav-upadhyay apropos.c: Fixed some typos in the comments.
authored
77 /*If the user specifies a section number as an option, the corresponding
78 * index element in sec_nums is set to the string representing that
746e771 @abhinav-upadhyay Fixed some code formatting, comments and indentation. Should be harmless
authored
79 * section number.
80 */
f1187fa @abhinav-upadhyay apropos.c: Added a new option 'p' which would remove the limit of onl…
authored
81 while ((ch = getopt(argc, argv, "123456789p")) != -1) {
966c7ba @abhinav-upadhyay Changed the option for section specific search from -s to digits (the…
authored
82 switch (ch) {
83 case '1':
f3e5f10 @abhinav-upadhyay apropos.c: Fixed option handling code. Removed unnecessary casts.
authored
84 aflags.sec_nums[0] = "1";
966c7ba @abhinav-upadhyay Changed the option for section specific search from -s to digits (the…
authored
85 break;
86 case '2':
f3e5f10 @abhinav-upadhyay apropos.c: Fixed option handling code. Removed unnecessary casts.
authored
87 aflags.sec_nums[1] = "2";
966c7ba @abhinav-upadhyay Changed the option for section specific search from -s to digits (the…
authored
88 break;
89 case '3':
f3e5f10 @abhinav-upadhyay apropos.c: Fixed option handling code. Removed unnecessary casts.
authored
90 aflags.sec_nums[2] = "3";
966c7ba @abhinav-upadhyay Changed the option for section specific search from -s to digits (the…
authored
91 break;
92 case '4':
f3e5f10 @abhinav-upadhyay apropos.c: Fixed option handling code. Removed unnecessary casts.
authored
93 aflags.sec_nums[3] = "4";
966c7ba @abhinav-upadhyay Changed the option for section specific search from -s to digits (the…
authored
94 break;
95 case '5':
f3e5f10 @abhinav-upadhyay apropos.c: Fixed option handling code. Removed unnecessary casts.
authored
96 aflags.sec_nums[4] = "5";
966c7ba @abhinav-upadhyay Changed the option for section specific search from -s to digits (the…
authored
97 break;
98 case '6':
f3e5f10 @abhinav-upadhyay apropos.c: Fixed option handling code. Removed unnecessary casts.
authored
99 aflags.sec_nums[5] = "6";
966c7ba @abhinav-upadhyay Changed the option for section specific search from -s to digits (the…
authored
100 break;
101 case '7':
f3e5f10 @abhinav-upadhyay apropos.c: Fixed option handling code. Removed unnecessary casts.
authored
102 aflags.sec_nums[6] = "7";
966c7ba @abhinav-upadhyay Changed the option for section specific search from -s to digits (the…
authored
103 break;
104 case '8':
f3e5f10 @abhinav-upadhyay apropos.c: Fixed option handling code. Removed unnecessary casts.
authored
105 aflags.sec_nums[7] = "8";
966c7ba @abhinav-upadhyay Changed the option for section specific search from -s to digits (the…
authored
106 break;
107 case '9':
f3e5f10 @abhinav-upadhyay apropos.c: Fixed option handling code. Removed unnecessary casts.
authored
108 aflags.sec_nums[8] = "9";
f1187fa @abhinav-upadhyay apropos.c: Added a new option 'p' which would remove the limit of onl…
authored
109 break;
110 case 'p': //user wants to view more than 10 results and page them
111 aflags.pager = 1;
d7c7e5d @abhinav-upadhyay Remove the function itoa as snprintf and family can be used to conver…
authored
112 nrec = -1; // Fetch all records
b846fd2 @abhinav-upadhyay Implemented option in search within a specific section. One can speci…
authored
113 break;
114 case '?':
966c7ba @abhinav-upadhyay Changed the option for section specific search from -s to digits (the…
authored
115 default:
8321d9e @abhinav-upadhyay apropos.c: Improved usage() and handling of options
authored
116 usage();
966c7ba @abhinav-upadhyay Changed the option for section specific search from -s to digits (the…
authored
117 break;
118 }
b846fd2 @abhinav-upadhyay Implemented option in search within a specific section. One can speci…
authored
119 }
120
121 argc -= optind;
122 argv += optind;
069cdce @abhinav-upadhyay apropos.c: Fixed a stupid mistake. After processing command line argu…
authored
123 query = *argv;
cf1a10a @abhinav-upadhyay Changed the name of function init to init_db and also changed it's si…
authored
124 if ((db = init_db(DB_READONLY)) == NULL)
a19c646 @abhinav-upadhyay Implemented do_query in the form of a generic library function in apr…
authored
125 errx(EXIT_FAILURE, "The database does not exist. Please run makemandb "
126 "first and then try again");
127
b846fd2 @abhinav-upadhyay Implemented option in search within a specific section. One can speci…
authored
128 /* Eliminate any stopwords from the query */
85f6136 @abhinav-upadhyay apropos.c: Implemented remove_stopwords to remove stopwords from the …
authored
129 remove_stopwords(&query);
130
131 /* if any error occured in remove_stopwords, we continue with the initial
746e771 @abhinav-upadhyay Fixed some code formatting, comments and indentation. Should be harmless
authored
132 * query input by the user
133 */
85f6136 @abhinav-upadhyay apropos.c: Implemented remove_stopwords to remove stopwords from the …
authored
134 if (query == NULL)
069cdce @abhinav-upadhyay apropos.c: Fixed a stupid mistake. After processing command line argu…
authored
135 query = *argv;
2a706b3 @abhinav-upadhyay Replaced most of the fprintf calls in the error handling code with wa…
authored
136 else if (!strcmp(query, ""))
137 errx(EXIT_FAILURE, "Try specifying more relevant keywords to get some "
138 "matches");
139
a19c646 @abhinav-upadhyay Implemented do_query in the form of a generic library function in apr…
authored
140 /* If user wants to page the output, then set some settings */
141 if (aflags.pager) {
142 /* Open a pipe to the pager */
da801f2 @abhinav-upadhyay Display an error message in case no results are obtained
authored
143 if ((cbdata.out = popen("more", "w")) == NULL) {
4f7a32a @abhinav-upadhyay Added function close_db() to apropos-utils.c to complete the API.
authored
144 close_db(db);
a19c646 @abhinav-upadhyay Implemented do_query in the form of a generic library function in apr…
authored
145 err(EXIT_FAILURE, "pipe failed");
146 }
147 }
148
149 query_args args;
150 args.search_str = query;
151 args.sec_nums = aflags.sec_nums;
152 args.nrec = nrec;
238eae2 @abhinav-upadhyay Add a new field 'offset' to the struct query_args, fix the API and th…
authored
153 args.offset = 0;
a19c646 @abhinav-upadhyay Implemented do_query in the form of a generic library function in apr…
authored
154 args.callback = &query_callback;
da801f2 @abhinav-upadhyay Display an error message in case no results are obtained
authored
155 args.callback_data = &cbdata;
a19c646 @abhinav-upadhyay Implemented do_query in the form of a generic library function in apr…
authored
156 args.errmsg = &errmsg;
80f1b24 @abhinav-upadhyay Implemented another utility function run_query_pager, which is being …
authored
157 if (aflags.pager) {
158 if (run_query_pager(db, &args) < 0)
159 errx(EXIT_FAILURE, "%s", errmsg);
160 }
161 else {
05880c1 @abhinav-upadhyay Fixed a blinder. run_query_pager was being called all the time whethe…
authored
162 if (run_query(db, snippet_args, &args) < 0)
80f1b24 @abhinav-upadhyay Implemented another utility function run_query_pager, which is being …
authored
163 errx(EXIT_FAILURE, "%s", errmsg);
164 }
165
da801f2 @abhinav-upadhyay Display an error message in case no results are obtained
authored
166 if (cbdata.count == 0)
167 warnx("No relevant results obtained.\n"
168 "Please make sure that you spelled all the terms correctly\n"
169 "Or try using better keywords. For example:\n"
170 "\"removing files\" instead of \"deleting files\"");
d39fa84 @abhinav-upadhyay apropos.c: Fixed some more loose ends in remove_stopwords.
authored
171 free(query);
a19c646 @abhinav-upadhyay Implemented do_query in the form of a generic library function in apr…
authored
172 free(errmsg);
173 if (aflags.pager)
da801f2 @abhinav-upadhyay Display an error message in case no results are obtained
authored
174 pclose(cbdata.out);
4f7a32a @abhinav-upadhyay Added function close_db() to apropos-utils.c to complete the API.
authored
175 close_db(db);
e653ec9 @abhinav-upadhyay apropos.c: Wrote a bare bones implementation of apropos.
authored
176 return 0;
177 }
178
179 /*
a19c646 @abhinav-upadhyay Implemented do_query in the form of a generic library function in apr…
authored
180 * query_callback --
89bf711 @abhinav-upadhyay apropos.c: Fixed a comment typo
authored
181 * Callback function for run_query.
a19c646 @abhinav-upadhyay Implemented do_query in the form of a generic library function in apr…
authored
182 * It simply outputs the results from do_query. If the user specified the -p
183 * option, then the output is sent to a pager, otherwise stdout is the default
184 * output stream.
746e771 @abhinav-upadhyay Fixed some code formatting, comments and indentation. Should be harmless
authored
185 */
e653ec9 @abhinav-upadhyay apropos.c: Wrote a bare bones implementation of apropos.
authored
186 static int
a19c646 @abhinav-upadhyay Implemented do_query in the form of a generic library function in apr…
authored
187 query_callback(void *data, int ncol, char **col_values, char **col_names)
e653ec9 @abhinav-upadhyay apropos.c: Wrote a bare bones implementation of apropos.
authored
188 {
189 char *name = NULL;
87216ed @abhinav-upadhyay makemandb.c: Added function get_section() which extracts the section …
authored
190 char *section = NULL;
e653ec9 @abhinav-upadhyay apropos.c: Wrote a bare bones implementation of apropos.
authored
191 char *snippet = NULL;
a64a4f4 @abhinav-upadhyay Now displaying the name_desc value along with search results. Also up…
authored
192 char *name_desc = NULL;
da801f2 @abhinav-upadhyay Display an error message in case no results are obtained
authored
193 callback_data *cbdata = (callback_data *) data;
194 FILE *out = cbdata->out;
195 cbdata->count++;
1ce5a46 @abhinav-upadhyay Replaced malloc/strdup/asprintf and similar memory allocation calls w…
authored
196
a19c646 @abhinav-upadhyay Implemented do_query in the form of a generic library function in apr…
authored
197 section = col_values[0];
198 name = col_values[1];
199 name_desc = col_values[2];
200 snippet = col_values[3];
201 fprintf(out, "%s(%s)\t%s\n%s\n\n", name, section, name_desc,
202 snippet);
e653ec9 @abhinav-upadhyay apropos.c: Wrote a bare bones implementation of apropos.
authored
203 return 0;
204 }
85f6136 @abhinav-upadhyay apropos.c: Implemented remove_stopwords to remove stopwords from the …
authored
205 /*
746e771 @abhinav-upadhyay Fixed some code formatting, comments and indentation. Should be harmless
authored
206 * remove_stopwords--
207 * Scans the query and removes any stop words from it.
208 * It scans the query word by word, and looks up a hash table of stop words
209 * to check if it is a stopword or a valid keyword. In the end we only have the
210 * relevant keywords left in the query.
211 * Error Cases:
212 * 1. In case of any error, it will set the query to NULL.
213 * 2. In case the query is found to be consisting only of stop words, it will
214 * set the query to a blank string ""
215 */
85f6136 @abhinav-upadhyay apropos.c: Implemented remove_stopwords to remove stopwords from the …
authored
216 static void
217 remove_stopwords(char **query)
218 {
219 int i = 0;
1828b68 @abhinav-upadhyay Fixed compiler warnings and errors.
authored
220 const char *temp;
221 char *buf = NULL;
dcbd358 @abhinav-upadhyay apropos.c: Removed a few stopwords and also put them in alphabetical …
authored
222 const char *stopwords[] = {"a", "b", "d", "e", "f", "g", "h", "i", "j",
746e771 @abhinav-upadhyay Fixed some code formatting, comments and indentation. Should be harmless
authored
223 "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y",
dcbd358 @abhinav-upadhyay apropos.c: Removed a few stopwords and also put them in alphabetical …
authored
224 "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
225 "about", "also", "all", "an", "another", "and", "are", "as", "ask", "at",
226 "again", "always", "any", "around",
227 "back", "be", "been", "before", "between", "below", "by", "bye", "but",
228 "because",
229 "case", "can", "consist", "could",
230 "did", "does", "down",
231 "each", "early","either", "end", "enough", "even", "every",
232 "fact", "far", "few", "four", "further", "follow", "from", "full",
233 "general", "good", "got", "great", "give", "given",
234 "have", "has", "had", "here", "how", "having", "high", "him", "his",
235 "however",
236 "if", "important", "in", "interest", "into", "is", "it",
237 "just",
238 "keep", "keeps", "kind", "knew", "know",
746e771 @abhinav-upadhyay Fixed some code formatting, comments and indentation. Should be harmless
authored
239 "large", "larger", "last", "later", "latter", "latest", "least", "let",
dcbd358 @abhinav-upadhyay apropos.c: Removed a few stopwords and also put them in alphabetical …
authored
240 "like", "likely", "long", "longer",
241 "made", "many", "may", "me", "might", "most", "mostly", "much", "must",
242 "my",
243 "necessary", "need", "never", "needs", "next", "no", "non", "noone", "not",
244 "nothing", "names", "new",
245 "often", "old", "older", "once", "only", "order", "our", "out", "over",
246 "of", "off", "on", "or",
247 "part", "per", "perhaps", "possible", "present", "problem",
248 "quite",
249 "rather", "really", "right", "room",
250 "said", "same", "saw", "say", "says", "second", "see", "seem", "seemed",
251 "seems", "sees", "several", "shall", "should", "side", "sides", "small",
252 "smaller", "so", "some", "something", "state", "states", "still", "such",
253 "sure",
254 "take", "taken", "then", "them", "their", "there", "therefore", "thing",
255 "think", "thinks", "though", "three", "thus", "together", "too", "took",
256 "toward", "turn", "two", "the", "this", "up", "that", "to", "these",
257 "those",
258 "until", "upon", "us", "use", "used", "uses",
259 "very",
260 "want", "wanted", "wants", "was", "way", "ways", "we", "well", "went",
261 "were", "whether", "with", "within", "without", "work", "would", "what",
262 "when", "why", "will", "willing",
263 "year", "yet", "you",
264 NULL
a45c0c8 @abhinav-upadhyay apropos.c: Increased the stopwords list.
authored
265 };
9077e94 @abhinav-upadhyay makemandb is now much more fast. Brought down all the weight calculat…
authored
266
85f6136 @abhinav-upadhyay apropos.c: Implemented remove_stopwords to remove stopwords from the …
authored
267 /* initialize the hash table for stop words */
268 if (!hcreate(sizeof(stopwords) * sizeof(char)))
269 return;
270
271 /* store the stopwords in the hashtable */
272 for (temp = stopwords[i]; temp != NULL; temp = stopwords[i++]) {
273 ENTRY ent;
274 ent.key = strdup(temp);
275 ent.data = (void *) "y";
276 hsearch(ent, ENTER);
277 }
278
279 /* filter out the stop words from the query */
280 for (temp = strtok(*query, " "); temp; temp = strtok(NULL, " ")) {
281 ENTRY ent;
1828b68 @abhinav-upadhyay Fixed compiler warnings and errors.
authored
282 ent.key = (char *)temp;
85f6136 @abhinav-upadhyay apropos.c: Implemented remove_stopwords to remove stopwords from the …
authored
283 ent.data = NULL;
8ae5b74 @abhinav-upadhyay apropos.c: Removed a lot needless code in remove_stopwords with a sin…
authored
284 if (hsearch(ent, FIND) == NULL)
285 concat(&buf, temp, strlen(temp));
85f6136 @abhinav-upadhyay apropos.c: Implemented remove_stopwords to remove stopwords from the …
authored
286 }
287
288 hdestroy();
289 if (buf != NULL)
d39fa84 @abhinav-upadhyay apropos.c: Fixed some more loose ends in remove_stopwords.
authored
290 *query = buf;
85f6136 @abhinav-upadhyay apropos.c: Implemented remove_stopwords to remove stopwords from the …
authored
291 else
d39fa84 @abhinav-upadhyay apropos.c: Fixed some more loose ends in remove_stopwords.
authored
292 *query = (char *)"";
85f6136 @abhinav-upadhyay apropos.c: Implemented remove_stopwords to remove stopwords from the …
authored
293 }
e653ec9 @abhinav-upadhyay apropos.c: Wrote a bare bones implementation of apropos.
authored
294
295 /*
296 * usage --
297 * print usage message and die
298 */
299 static void
300 usage(void)
301 {
915d222 @abhinav-upadhyay apropos.c: Fixed usage
authored
302 (void)warnx("Usage: %s [-p] [-123456789] query", getprogname());
e653ec9 @abhinav-upadhyay apropos.c: Wrote a bare bones implementation of apropos.
authored
303 exit(1);
304 }
Something went wrong with that request. Please try again.