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