Home | History | Annotate | Line # | Download | only in makemandb
apropos.c revision 1.4
      1 /*	$NetBSD: apropos.c,v 1.4 2012/02/07 23:03:34 joerg 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.4 2012/02/07 23:03:34 joerg 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 #ifdef NOTYET
     72 	static const char *snippet_args[] = {"\033[1m", "\033[0m", "..."};
     73 #endif
     74 	query_args args;
     75 	char *query = NULL;	// the user query
     76 	char *errmsg = NULL;
     77 	char *str;
     78 	int ch, rc = 0;
     79 	callback_data cbdata;
     80 	cbdata.out = stdout;		// the default output stream
     81 	cbdata.count = 0;
     82 	apropos_flags aflags;
     83 	cbdata.aflags = &aflags;
     84 	sqlite3 *db;
     85 	setprogname(argv[0]);
     86 	if (argc < 2)
     87 		usage();
     88 
     89 	memset(&aflags, 0, sizeof(aflags));
     90 
     91 	/*If the user specifies a section number as an option, the corresponding
     92 	 * index element in sec_nums is set to the string representing that
     93 	 * section number.
     94 	 */
     95 	while ((ch = getopt(argc, argv, "123456789Ccn:pS:")) != -1) {
     96 		switch (ch) {
     97 		case '1':
     98 		case '2':
     99 		case '3':
    100 		case '4':
    101 		case '5':
    102 		case '6':
    103 		case '7':
    104 		case '8':
    105 		case '9':
    106 			aflags.sec_nums[ch - '1'] = 1;
    107 			break;
    108 		case 'C':
    109 			aflags.no_context = 1;
    110 			break;
    111 		case 'c':
    112 			aflags.no_context = 0;
    113 			break;
    114 		case 'n':
    115 			aflags.nresults = atoi(optarg);
    116 			break;
    117 		case 'p':	//user wants to view more than 10 results and page them
    118 			aflags.pager = 1;
    119 			aflags.nresults = -1;	// Fetch all records
    120 			break;
    121 		case 'S':
    122 			aflags.machine = optarg;
    123 			break;
    124 		case '?':
    125 		default:
    126 			usage();
    127 		}
    128 	}
    129 
    130 	argc -= optind;
    131 	argv += optind;
    132 
    133 	if (!argc)
    134 		usage();
    135 
    136 	str = NULL;
    137 	while (argc--)
    138 		concat(&str, *argv++);
    139 	/* Eliminate any stopwords from the query */
    140 	query = remove_stopwords(lower(str));
    141 	free(str);
    142 
    143 	/* if any error occured in remove_stopwords, exit */
    144 	if (query == NULL)
    145 		errx(EXIT_FAILURE, "Try using more relevant keywords");
    146 
    147 	if ((db = init_db(MANDB_READONLY)) == NULL)
    148 		exit(EXIT_FAILURE);
    149 
    150 	/* If user wants to page the output, then set some settings */
    151 	if (aflags.pager) {
    152 		const char *pager = getenv("PAGER");
    153 		if (pager == NULL)
    154 			pager = _PATH_PAGER;
    155 		/* Open a pipe to the pager */
    156 		if ((cbdata.out = popen(pager, "w")) == NULL) {
    157 			close_db(db);
    158 			err(EXIT_FAILURE, "pipe failed");
    159 		}
    160 	}
    161 
    162 	args.search_str = query;
    163 	args.sec_nums = aflags.sec_nums;
    164 	args.nrec = aflags.nresults ? aflags.nresults : 10;
    165 	args.offset = 0;
    166 	args.machine = aflags.machine;
    167 	args.callback = &query_callback;
    168 	args.callback_data = &cbdata;
    169 	args.errmsg = &errmsg;
    170 
    171 #ifdef NOTYET
    172 	rc = run_query(db, snippet_args, &args);
    173 #else
    174 	rc = run_query_pager(db, &args);
    175 #endif
    176 
    177 	free(query);
    178 	close_db(db);
    179 	if (errmsg) {
    180 		warnx("%s", errmsg);
    181 		free(errmsg);
    182 		exit(EXIT_FAILURE);
    183 	}
    184 
    185 	if (rc < 0) {
    186 		/* Something wrong with the database. Exit */
    187 		exit(EXIT_FAILURE);
    188 	}
    189 
    190 	if (cbdata.count == 0) {
    191 		warnx("No relevant results obtained.\n"
    192 			  "Please make sure that you spelled all the terms correctly\n"
    193 			  "Or try using better keywords.");
    194 	}
    195 	return 0;
    196 }
    197 
    198 /*
    199  * query_callback --
    200  *  Callback function for run_query.
    201  *  It simply outputs the results from do_query. If the user specified the -p
    202  *  option, then the output is sent to a pager, otherwise stdout is the default
    203  *  output stream.
    204  */
    205 static int
    206 query_callback(void *data, const char *section, const char *name,
    207 	const char *name_desc, const char *snippet, size_t snippet_length)
    208 {
    209 	callback_data *cbdata = (callback_data *) data;
    210 	FILE *out = cbdata->out;
    211 	cbdata->count++;
    212 	fprintf(out, "%s(%s)\t%s\n", name, section, name_desc);
    213 
    214 	if (cbdata->aflags->no_context == 0)
    215 		fprintf(out, "%s\n\n", snippet);
    216 
    217 	return 0;
    218 }
    219 
    220 #include "stopwords.c"
    221 
    222 /*
    223  * remove_stopwords--
    224  *  Scans the query and removes any stop words from it.
    225  *  Returns the modified query or NULL, if it contained only stop words.
    226  */
    227 
    228 static char *
    229 remove_stopwords(const char *query)
    230 {
    231 	size_t len, idx;
    232 	char *output, *buf;
    233 	const char *sep, *next;
    234 
    235 	output = buf = emalloc(strlen(query) + 1);
    236 
    237 	for (; query[0] != '\0'; query = next) {
    238 		sep = strchr(query, ' ');
    239 		if (sep == NULL) {
    240 			len = strlen(query);
    241 			next = query + len;
    242 		} else {
    243 			len = sep - query;
    244 			next = sep + 1;
    245 		}
    246 		if (len == 0)
    247 			continue;
    248 		idx = stopwords_hash(query, len);
    249 		if (memcmp(stopwords[idx], query, len) == 0 &&
    250 		    stopwords[idx][len] == '\0')
    251 			continue;
    252 		memcpy(buf, query, len);
    253 		buf += len;
    254 		*buf++ = ' ';
    255 	}
    256 
    257 	if (output == buf) {
    258 		free(output);
    259 		return NULL;
    260 	}
    261 	buf[-1] = '\0';
    262 	return output;
    263 }
    264 
    265 /*
    266  * usage --
    267  *	print usage message and die
    268  */
    269 static void
    270 usage(void)
    271 {
    272 	fprintf(stderr,
    273 		"Usage: %s [-n Number of records] [-p] [-123456789] [-S machine] query\n",
    274 		getprogname());
    275 	exit(1);
    276 }
    277