Home | History | Annotate | Line # | Download | only in makemandb
apropos.c revision 1.20
      1  1.20  christos /*	$NetBSD: apropos.c,v 1.20 2016/04/23 14:15:36 christos 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.20  christos __RCSID("$NetBSD: apropos.c,v 1.20 2016/04/23 14:15:36 christos Exp $");
     35   1.1     joerg 
     36   1.1     joerg #include <err.h>
     37   1.1     joerg #include <stdio.h>
     38   1.1     joerg #include <stdlib.h>
     39   1.1     joerg #include <string.h>
     40   1.1     joerg #include <unistd.h>
     41   1.1     joerg #include <util.h>
     42   1.1     joerg 
     43   1.1     joerg #include "apropos-utils.h"
     44   1.1     joerg 
     45   1.1     joerg typedef struct apropos_flags {
     46  1.19  christos 	char *sec_nums;
     47   1.1     joerg 	int nresults;
     48   1.1     joerg 	int pager;
     49   1.1     joerg 	int no_context;
     50  1.16  christos 	query_format format;
     51  1.13  christos 	int legacy;
     52   1.1     joerg 	const char *machine;
     53   1.1     joerg } apropos_flags;
     54   1.1     joerg 
     55   1.1     joerg typedef struct callback_data {
     56   1.1     joerg 	int count;
     57   1.1     joerg 	FILE *out;
     58   1.1     joerg 	apropos_flags *aflags;
     59   1.1     joerg } callback_data;
     60   1.1     joerg 
     61  1.19  christos static const unsigned int sections_args_length = 16;
     62  1.19  christos 
     63   1.1     joerg static char *remove_stopwords(const char *);
     64   1.1     joerg static int query_callback(void *, const char * , const char *, const char *,
     65   1.1     joerg 	const char *, size_t);
     66   1.1     joerg __dead static void usage(void);
     67   1.1     joerg 
     68   1.1     joerg #define _PATH_PAGER	"/usr/bin/more -s"
     69   1.1     joerg 
     70  1.13  christos static void
     71  1.13  christos parseargs(int argc, char **argv, struct apropos_flags *aflags)
     72   1.1     joerg {
     73  1.13  christos 	int ch;
     74  1.19  christos 	char sec[2] = {0, 0};
     75  1.20  christos 
     76  1.16  christos 	while ((ch = getopt(argc, argv, "123456789Cchiln:PprS:s:")) != -1) {
     77   1.1     joerg 		switch (ch) {
     78   1.1     joerg 		case '1':
     79   1.1     joerg 		case '2':
     80   1.1     joerg 		case '3':
     81   1.1     joerg 		case '4':
     82   1.1     joerg 		case '5':
     83   1.1     joerg 		case '6':
     84   1.1     joerg 		case '7':
     85   1.1     joerg 		case '8':
     86   1.1     joerg 		case '9':
     87  1.19  christos 			/*
     88  1.19  christos 			 *Generate a space separated list of all the
     89  1.19  christos 			 * requested sections
     90  1.19  christos 			 */
     91  1.19  christos 			sec[0] = (char) ch ;
     92  1.19  christos 			if (aflags->sec_nums == NULL) {
     93  1.19  christos 				aflags->sec_nums =
     94  1.19  christos 				    emalloc(sections_args_length);
     95  1.19  christos 				memcpy(aflags->sec_nums, sec, 2);
     96  1.19  christos 			} else
     97  1.19  christos 				concat2(&aflags->sec_nums, sec, 1);
     98   1.1     joerg 			break;
     99   1.1     joerg 		case 'C':
    100  1.13  christos 			aflags->no_context = 1;
    101   1.1     joerg 			break;
    102   1.1     joerg 		case 'c':
    103  1.13  christos 			aflags->no_context = 0;
    104  1.13  christos 			break;
    105  1.16  christos 		case 'h':
    106  1.16  christos 			aflags->format = APROPOS_HTML;
    107  1.16  christos 			break;
    108  1.13  christos 		case 'i':
    109  1.16  christos 			aflags->format = APROPOS_TERM;
    110  1.13  christos 			break;
    111  1.13  christos 		case 'l':
    112  1.13  christos 			aflags->legacy = 1;
    113  1.13  christos 			aflags->no_context = 1;
    114  1.16  christos 			aflags->format = APROPOS_NONE;
    115   1.1     joerg 			break;
    116   1.1     joerg 		case 'n':
    117  1.13  christos 			aflags->nresults = atoi(optarg);
    118   1.1     joerg 			break;
    119  1.13  christos 		case 'p':	// user wants a pager
    120  1.13  christos 			aflags->pager = 1;
    121  1.16  christos 			/*FALLTHROUGH*/
    122  1.16  christos 		case 'P':
    123  1.16  christos 			aflags->format = APROPOS_PAGER;
    124   1.1     joerg 			break;
    125  1.12  christos 		case 'r':
    126  1.16  christos 			aflags->format = APROPOS_NONE;
    127  1.12  christos 			break;
    128   1.1     joerg 		case 'S':
    129  1.13  christos 			aflags->machine = optarg;
    130   1.1     joerg 			break;
    131   1.5     joerg 		case 's':
    132  1.19  christos 			if (aflags->sec_nums == NULL) {
    133  1.19  christos 				size_t arglen = strlen(optarg);
    134  1.19  christos 				aflags->sec_nums =
    135  1.19  christos 				    arglen > sections_args_length
    136  1.19  christos 					? emalloc(arglen + 1)
    137  1.19  christos 					: emalloc(sections_args_length);
    138  1.19  christos 				memcpy(aflags->sec_nums, optarg, arglen + 1);
    139  1.19  christos 			} else
    140  1.19  christos 				concat(&aflags->sec_nums, optarg);
    141   1.5     joerg 			break;
    142   1.1     joerg 		case '?':
    143   1.1     joerg 		default:
    144   1.1     joerg 			usage();
    145   1.1     joerg 		}
    146   1.1     joerg 	}
    147  1.13  christos }
    148  1.13  christos 
    149  1.13  christos int
    150  1.13  christos main(int argc, char *argv[])
    151  1.13  christos {
    152  1.13  christos 	query_args args;
    153  1.13  christos 	char *query = NULL;	// the user query
    154  1.13  christos 	char *errmsg = NULL;
    155  1.13  christos 	char *str;
    156  1.13  christos 	int rc = 0;
    157  1.13  christos 	int s;
    158  1.13  christos 	callback_data cbdata;
    159  1.13  christos 	cbdata.out = stdout;		// the default output stream
    160  1.13  christos 	cbdata.count = 0;
    161  1.13  christos 	apropos_flags aflags;
    162  1.19  christos 	aflags.sec_nums = NULL;
    163  1.13  christos 	cbdata.aflags = &aflags;
    164  1.13  christos 	sqlite3 *db;
    165  1.13  christos 	setprogname(argv[0]);
    166  1.13  christos 	if (argc < 2)
    167  1.13  christos 		usage();
    168  1.13  christos 
    169  1.13  christos 	memset(&aflags, 0, sizeof(aflags));
    170  1.13  christos 
    171  1.13  christos 	if (!isatty(STDOUT_FILENO))
    172  1.16  christos 		aflags.format = APROPOS_NONE;
    173  1.16  christos 	else
    174  1.16  christos 		aflags.format = APROPOS_TERM;
    175  1.13  christos 
    176  1.13  christos 	if ((str = getenv("APROPOS")) != NULL) {
    177  1.13  christos 		char **ptr = emalloc((strlen(str) + 2) * sizeof(*ptr));
    178  1.13  christos #define WS "\t\n\r "
    179  1.13  christos 		ptr[0] = __UNCONST(getprogname());
    180  1.13  christos 		for (s = 1, str = strtok(str, WS); str;
    181  1.13  christos 		    str = strtok(NULL, WS), s++)
    182  1.13  christos 			ptr[s] = str;
    183  1.13  christos 		ptr[s] = NULL;
    184  1.13  christos 		parseargs(s, ptr, &aflags);
    185  1.13  christos 		free(ptr);
    186  1.13  christos 		optreset = 1;
    187  1.13  christos 		optind = 1;
    188  1.13  christos 	}
    189  1.13  christos 
    190  1.13  christos 	parseargs(argc, argv, &aflags);
    191  1.13  christos 
    192   1.1     joerg 	argc -= optind;
    193   1.1     joerg 	argv += optind;
    194  1.11  christos 
    195   1.1     joerg 	if (!argc)
    196   1.1     joerg 		usage();
    197   1.1     joerg 
    198   1.1     joerg 	str = NULL;
    199   1.1     joerg 	while (argc--)
    200   1.1     joerg 		concat(&str, *argv++);
    201   1.1     joerg 	/* Eliminate any stopwords from the query */
    202   1.1     joerg 	query = remove_stopwords(lower(str));
    203   1.1     joerg 
    204  1.17  christos 	/*
    205  1.17  christos 	 * If the query consisted only of stopwords and we removed all of
    206  1.17  christos 	 * them, use the original query.
    207  1.17  christos 	 */
    208   1.1     joerg 	if (query == NULL)
    209  1.17  christos 		query = str;
    210  1.17  christos 	else
    211  1.17  christos 		free(str);
    212   1.1     joerg 
    213   1.8       wiz 	if ((db = init_db(MANDB_READONLY, MANCONF)) == NULL)
    214   1.1     joerg 		exit(EXIT_FAILURE);
    215   1.1     joerg 
    216   1.1     joerg 	/* If user wants to page the output, then set some settings */
    217   1.1     joerg 	if (aflags.pager) {
    218   1.1     joerg 		const char *pager = getenv("PAGER");
    219   1.1     joerg 		if (pager == NULL)
    220   1.1     joerg 			pager = _PATH_PAGER;
    221   1.1     joerg 		/* Open a pipe to the pager */
    222   1.1     joerg 		if ((cbdata.out = popen(pager, "w")) == NULL) {
    223   1.1     joerg 			close_db(db);
    224   1.1     joerg 			err(EXIT_FAILURE, "pipe failed");
    225   1.1     joerg 		}
    226   1.1     joerg 	}
    227   1.1     joerg 
    228   1.1     joerg 	args.search_str = query;
    229   1.1     joerg 	args.sec_nums = aflags.sec_nums;
    230  1.13  christos 	args.legacy = aflags.legacy;
    231  1.14  christos 	args.nrec = aflags.nresults ? aflags.nresults : -1;
    232   1.1     joerg 	args.offset = 0;
    233   1.1     joerg 	args.machine = aflags.machine;
    234   1.1     joerg 	args.callback = &query_callback;
    235   1.1     joerg 	args.callback_data = &cbdata;
    236   1.1     joerg 	args.errmsg = &errmsg;
    237   1.1     joerg 
    238  1.16  christos 	if (aflags.format == APROPOS_HTML) {
    239  1.16  christos 		fprintf(cbdata.out, "<html>\n<header>\n<title>apropos results "
    240  1.16  christos 		    "for %s</title></header>\n<body>\n<table cellpadding=\"4\""
    241  1.16  christos 		    "style=\"border: 1px solid #000000; border-collapse:"
    242  1.16  christos 		    "collapse;\" border=\"1\">\n", query);
    243  1.16  christos 	}
    244  1.16  christos 	rc = run_query(db, aflags.format, &args);
    245  1.16  christos 	if (aflags.format == APROPOS_HTML)
    246  1.16  christos 		fprintf(cbdata.out, "</table>\n</body>\n</html>\n");
    247  1.11  christos 
    248   1.1     joerg 	free(query);
    249  1.19  christos 	free(aflags.sec_nums);
    250   1.1     joerg 	close_db(db);
    251   1.1     joerg 	if (errmsg) {
    252   1.1     joerg 		warnx("%s", errmsg);
    253   1.1     joerg 		free(errmsg);
    254   1.1     joerg 		exit(EXIT_FAILURE);
    255   1.1     joerg 	}
    256   1.1     joerg 
    257   1.1     joerg 	if (rc < 0) {
    258   1.1     joerg 		/* Something wrong with the database. Exit */
    259   1.1     joerg 		exit(EXIT_FAILURE);
    260   1.1     joerg 	}
    261  1.11  christos 
    262   1.1     joerg 	if (cbdata.count == 0) {
    263   1.1     joerg 		warnx("No relevant results obtained.\n"
    264  1.13  christos 		    "Please make sure that you spelled all the terms correctly "
    265  1.13  christos 		    "or try using better keywords.");
    266   1.1     joerg 	}
    267   1.1     joerg 	return 0;
    268   1.1     joerg }
    269   1.1     joerg 
    270   1.1     joerg /*
    271   1.1     joerg  * query_callback --
    272   1.1     joerg  *  Callback function for run_query.
    273   1.1     joerg  *  It simply outputs the results from do_query. If the user specified the -p
    274   1.1     joerg  *  option, then the output is sent to a pager, otherwise stdout is the default
    275   1.1     joerg  *  output stream.
    276   1.1     joerg  */
    277   1.1     joerg static int
    278   1.1     joerg query_callback(void *data, const char *section, const char *name,
    279   1.1     joerg 	const char *name_desc, const char *snippet, size_t snippet_length)
    280   1.1     joerg {
    281   1.1     joerg 	callback_data *cbdata = (callback_data *) data;
    282   1.1     joerg 	FILE *out = cbdata->out;
    283   1.1     joerg 	cbdata->count++;
    284  1.16  christos 	if (cbdata->aflags->format != APROPOS_HTML) {
    285  1.16  christos 	    fprintf(out, cbdata->aflags->legacy ? "%s(%s) - %s\n" :
    286  1.16  christos 		"%s (%s)\t%s\n", name, section, name_desc);
    287  1.16  christos 	    if (cbdata->aflags->no_context == 0)
    288  1.16  christos 		    fprintf(out, "%s\n\n", snippet);
    289  1.16  christos 	} else {
    290  1.16  christos 	    fprintf(out, "<tr><td>%s(%s)</td><td>%s</td></tr>\n", name,
    291  1.16  christos 		section, name_desc);
    292  1.16  christos 	    if (cbdata->aflags->no_context == 0)
    293  1.16  christos 		    fprintf(out, "<tr><td colspan=2>%s</td></tr>\n", snippet);
    294  1.16  christos 	}
    295   1.1     joerg 
    296   1.1     joerg 	return 0;
    297   1.1     joerg }
    298   1.1     joerg 
    299   1.1     joerg #include "stopwords.c"
    300   1.1     joerg 
    301   1.1     joerg /*
    302   1.1     joerg  * remove_stopwords--
    303   1.1     joerg  *  Scans the query and removes any stop words from it.
    304   1.1     joerg  *  Returns the modified query or NULL, if it contained only stop words.
    305   1.1     joerg  */
    306   1.1     joerg 
    307   1.1     joerg static char *
    308   1.1     joerg remove_stopwords(const char *query)
    309   1.1     joerg {
    310   1.1     joerg 	size_t len, idx;
    311   1.1     joerg 	char *output, *buf;
    312   1.1     joerg 	const char *sep, *next;
    313   1.1     joerg 
    314   1.1     joerg 	output = buf = emalloc(strlen(query) + 1);
    315   1.1     joerg 
    316   1.1     joerg 	for (; query[0] != '\0'; query = next) {
    317   1.1     joerg 		sep = strchr(query, ' ');
    318   1.1     joerg 		if (sep == NULL) {
    319   1.1     joerg 			len = strlen(query);
    320   1.1     joerg 			next = query + len;
    321   1.1     joerg 		} else {
    322   1.1     joerg 			len = sep - query;
    323   1.1     joerg 			next = sep + 1;
    324   1.1     joerg 		}
    325   1.1     joerg 		if (len == 0)
    326   1.1     joerg 			continue;
    327   1.1     joerg 		idx = stopwords_hash(query, len);
    328   1.1     joerg 		if (memcmp(stopwords[idx], query, len) == 0 &&
    329   1.1     joerg 		    stopwords[idx][len] == '\0')
    330   1.1     joerg 			continue;
    331   1.1     joerg 		memcpy(buf, query, len);
    332   1.1     joerg 		buf += len;
    333   1.1     joerg 		*buf++ = ' ';
    334   1.1     joerg 	}
    335   1.1     joerg 
    336   1.1     joerg 	if (output == buf) {
    337   1.1     joerg 		free(output);
    338   1.1     joerg 		return NULL;
    339   1.1     joerg 	}
    340   1.1     joerg 	buf[-1] = '\0';
    341   1.1     joerg 	return output;
    342   1.1     joerg }
    343   1.1     joerg 
    344   1.1     joerg /*
    345   1.1     joerg  * usage --
    346   1.1     joerg  *	print usage message and die
    347   1.1     joerg  */
    348   1.1     joerg static void
    349   1.1     joerg usage(void)
    350   1.1     joerg {
    351  1.15       wiz 	fprintf(stderr, "Usage: %s [-123456789Ccilpr] [-n results] "
    352  1.15       wiz 	    "[-S machine] [-s section] query\n",
    353  1.13  christos 	    getprogname());
    354   1.1     joerg 	exit(1);
    355   1.1     joerg }
    356