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