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