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