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