Module Name: src
Committed By: christos
Date: Fri Mar 29 20:07:32 UTC 2013
Modified Files:
src/usr.sbin/makemandb: apropos-utils.c apropos-utils.h apropos.1
apropos.c
Log Message:
- If the stdout is not a tty, prevent formatting unless forced with -i
- Don't ever page unless asked for with -p
- Introduce "legacy mode" (-l)
1. searches only name and name_desc, prints name(section) - name_description
2. turns off escape formatting (can be forced on with -i)
3. turns off context printing (can be forced on with -c)
- Parse the environment $APROPOS variable as an argument vector.
With these changes one can simply 'export APROPOS=-l' and get the old apropos
behavior.
To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/usr.sbin/makemandb/apropos-utils.c
cvs rdiff -u -r1.7 -r1.8 src/usr.sbin/makemandb/apropos-utils.h \
src/usr.sbin/makemandb/apropos.1
cvs rdiff -u -r1.12 -r1.13 src/usr.sbin/makemandb/apropos.c
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/usr.sbin/makemandb/apropos-utils.c
diff -u src/usr.sbin/makemandb/apropos-utils.c:1.11 src/usr.sbin/makemandb/apropos-utils.c:1.12
--- src/usr.sbin/makemandb/apropos-utils.c:1.11 Sun Feb 10 18:58:27 2013
+++ src/usr.sbin/makemandb/apropos-utils.c Fri Mar 29 16:07:31 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: apropos-utils.c,v 1.11 2013/02/10 23:58:27 christos Exp $ */
+/* $NetBSD: apropos-utils.c,v 1.12 2013/03/29 20:07:31 christos Exp $ */
/*-
* Copyright (c) 2011 Abhinav Upadhyay <[email protected]>
* All rights reserved.
@@ -31,7 +31,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: apropos-utils.c,v 1.11 2013/02/10 23:58:27 christos Exp $");
+__RCSID("$NetBSD: apropos-utils.c,v 1.12 2013/03/29 20:07:31 christos Exp $");
#include <sys/queue.h>
#include <sys/stat.h>
@@ -549,18 +549,42 @@ run_query(sqlite3 *db, const char *snipp
default_snippet_args[2] = "...";
snippet_args = default_snippet_args;
}
- query = sqlite3_mprintf("SELECT section, name, name_desc, machine,"
- " snippet(mandb, %Q, %Q, %Q, -1, 40 ),"
- " rank_func(matchinfo(mandb, \"pclxn\")) AS rank"
- " FROM mandb"
- " WHERE mandb MATCH %Q %s "
- "%s"
- " ORDER BY rank DESC"
- "%s",
- snippet_args[0], snippet_args[1], snippet_args[2], args->search_str,
- machine_clause ? machine_clause : "",
- section_clause ? section_clause : "",
- limit_clause ? limit_clause : "");
+ if (args->legacy) {
+ query = sqlite3_mprintf("SELECT section, name, name_desc, machine,"
+ " snippet(mandb, %Q, %Q, %Q, -1, 40 ),"
+ " rank_func(matchinfo(mandb, \"pclxn\")) AS rank"
+ " FROM mandb"
+ " WHERE name MATCH %Q "
+ "%s"
+ " UNION SELECT section, name, name_desc, machine,"
+ " snippet(mandb, %Q, %Q, %Q, -1, 40 ),"
+ " rank_func(matchinfo(mandb, \"pclxn\")) AS rank"
+ " FROM mandb"
+ " WHERE name_desc MATCH %Q "
+ "%s"
+ " ORDER BY rank DESC"
+ "%s",
+ snippet_args[0], snippet_args[1], snippet_args[2],
+ args->search_str,
+ section_clause ? section_clause : "",
+ snippet_args[0], snippet_args[1], snippet_args[2],
+ args->search_str,
+ section_clause ? section_clause : "",
+ limit_clause ? limit_clause : "");
+ } else {
+ query = sqlite3_mprintf("SELECT section, name, name_desc, machine,"
+ " snippet(mandb, %Q, %Q, %Q, -1, 40 ),"
+ " rank_func(matchinfo(mandb, \"pclxn\")) AS rank"
+ " FROM mandb"
+ " WHERE mandb MATCH %Q %s "
+ "%s"
+ " ORDER BY rank DESC"
+ "%s",
+ snippet_args[0], snippet_args[1], snippet_args[2],
+ args->search_str, machine_clause ? machine_clause : "",
+ section_clause ? section_clause : "",
+ limit_clause ? limit_clause : "");
+ }
free(machine_clause);
free(section_clause);
@@ -862,7 +886,15 @@ run_query_pager(sqlite3 *db, query_args
struct orig_callback_data orig_data;
orig_data.callback = args->callback;
orig_data.data = args->callback_data;
- const char *snippet_args[] = {"\002", "\003", "..."};
+ const char *snippet_args[3];
+
+ if (args->flags & APROPOS_NOFORMAT) {
+ snippet_args[0] = snippet_args[1] = "";
+ } else {
+ snippet_args[0] = "\002";
+ snippet_args[1] = "\003";
+ }
+ snippet_args[2] = "...";
args->callback = &callback_pager;
args->callback_data = (void *) &orig_data;
return run_query(db, snippet_args, args);
Index: src/usr.sbin/makemandb/apropos-utils.h
diff -u src/usr.sbin/makemandb/apropos-utils.h:1.7 src/usr.sbin/makemandb/apropos-utils.h:1.8
--- src/usr.sbin/makemandb/apropos-utils.h:1.7 Sun Feb 10 18:58:27 2013
+++ src/usr.sbin/makemandb/apropos-utils.h Fri Mar 29 16:07:31 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: apropos-utils.h,v 1.7 2013/02/10 23:58:27 christos Exp $ */
+/* $NetBSD: apropos-utils.h,v 1.8 2013/03/29 20:07:31 christos Exp $ */
/*-
* Copyright (c) 2011 Abhinav Upadhyay <[email protected]>
* All rights reserved.
@@ -74,6 +74,7 @@ typedef struct query_args {
int *sec_nums; // Section in which to do the search
int nrec; // number of records to fetch
int offset; //From which position to start processing the records
+ int legacy;
const char *machine;
int (*callback) (void *, const char *, const char *, const char *,
const char *, size_t); // The callback function
Index: src/usr.sbin/makemandb/apropos.1
diff -u src/usr.sbin/makemandb/apropos.1:1.7 src/usr.sbin/makemandb/apropos.1:1.8
--- src/usr.sbin/makemandb/apropos.1:1.7 Sun Feb 10 18:58:28 2013
+++ src/usr.sbin/makemandb/apropos.1 Fri Mar 29 16:07:31 2013
@@ -1,4 +1,4 @@
-.\" $NetBSD: apropos.1,v 1.7 2013/02/10 23:58:28 christos Exp $
+.\" $NetBSD: apropos.1,v 1.8 2013/03/29 20:07:31 christos Exp $
.\"
.\" Copyright (c) 2011 Abhinav Upadhyay <[email protected]>
.\" All rights reserved.
@@ -29,7 +29,7 @@
.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd February 10, 2013
+.Dd March 29, 2013
.Dt APROPOS 1
.Os
.Sh NAME
@@ -37,7 +37,7 @@
.Nd search the complete content of all man pages
.Sh SYNOPSIS
.Nm
-.Op Fl 123456789Ccpr
+.Op Fl 123456789Ccilpr
.Op Fl n Ar Number of results
.Op Fl S Ar machine
.Op Fl s Ar section
@@ -88,11 +88,16 @@ Search only within section 9 manual page
Do not show the context of the match.
.It Fl c
Do show the context of the match (default).
+.It Fl i
+Turn on escape code formatting.
+.It Fl l
+Legacy mode: Only searches name and name description.
+Does not print context, or escape format the text.
.It Fl n
Output up to the specified number of search results.
-The default limit is 10.
+The default limit is infinity.
.It Fl p
-Display all matching results and pipe them through a pager (defaulting to
+Pipe the results through a pager (defaulting to
.Xr more 1 ) .
.It Fl r
On tty output don't issue any formatting escape codes.
@@ -114,6 +119,12 @@ The location of the Sqlite FTS database
.Cd _mandb
tag.
.El
+.Sh ENVIRONMENT VARIABLES
+The
+.Dv APROPOS
+environment variables is white-space tokenized as an argument vector
+and the options in it are parsed and set.
+Command line options override the environment options.
.Sh SEE ALSO
.Xr man 1 ,
.Xr whatis 1 ,
Index: src/usr.sbin/makemandb/apropos.c
diff -u src/usr.sbin/makemandb/apropos.c:1.12 src/usr.sbin/makemandb/apropos.c:1.13
--- src/usr.sbin/makemandb/apropos.c:1.12 Sun Feb 10 18:58:28 2013
+++ src/usr.sbin/makemandb/apropos.c Fri Mar 29 16:07:31 2013
@@ -1,4 +1,4 @@
-/* $NetBSD: apropos.c,v 1.12 2013/02/10 23:58:28 christos Exp $ */
+/* $NetBSD: apropos.c,v 1.13 2013/03/29 20:07:31 christos Exp $ */
/*-
* Copyright (c) 2011 Abhinav Upadhyay <[email protected]>
* All rights reserved.
@@ -31,7 +31,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: apropos.c,v 1.12 2013/02/10 23:58:28 christos Exp $");
+__RCSID("$NetBSD: apropos.c,v 1.13 2013/03/29 20:07:31 christos Exp $");
#include <err.h>
#include <search.h>
@@ -50,6 +50,7 @@ typedef struct apropos_flags {
int pager;
int no_context;
int no_format;
+ int legacy;
const char *machine;
} apropos_flags;
@@ -66,32 +67,11 @@ __dead static void usage(void);
#define _PATH_PAGER "/usr/bin/more -s"
-int
-main(int argc, char *argv[])
+static void
+parseargs(int argc, char **argv, struct apropos_flags *aflags)
{
- query_args args;
- char *query = NULL; // the user query
- char *errmsg = NULL;
- char *str;
- int ch, rc = 0;
- int s;
- callback_data cbdata;
- cbdata.out = stdout; // the default output stream
- cbdata.count = 0;
- apropos_flags aflags;
- cbdata.aflags = &aflags;
- sqlite3 *db;
- setprogname(argv[0]);
- if (argc < 2)
- usage();
-
- memset(&aflags, 0, sizeof(aflags));
-
- /*If the user specifies a section number as an option, the corresponding
- * index element in sec_nums is set to the string representing that
- * section number.
- */
- while ((ch = getopt(argc, argv, "123456789Ccn:prS:s:")) != -1) {
+ int ch;
+ while ((ch = getopt(argc, argv, "123456789Cciln:prS:s:")) != -1) {
switch (ch) {
case '1':
case '2':
@@ -102,38 +82,92 @@ main(int argc, char *argv[])
case '7':
case '8':
case '9':
- aflags.sec_nums[ch - '1'] = 1;
+ aflags->sec_nums[ch - '1'] = 1;
break;
case 'C':
- aflags.no_context = 1;
+ aflags->no_context = 1;
break;
case 'c':
- aflags.no_context = 0;
+ aflags->no_context = 0;
+ break;
+ case 'i':
+ aflags->no_format = 0;
+ break;
+ case 'l':
+ aflags->legacy = 1;
+ aflags->no_context = 1;
+ aflags->no_format = 1;
break;
case 'n':
- aflags.nresults = atoi(optarg);
+ aflags->nresults = atoi(optarg);
break;
- case 'p': //user wants to view more than 10 results and page them
- aflags.pager = 1;
- aflags.nresults = -1; // Fetch all records
+ case 'p': // user wants a pager
+ aflags->pager = 1;
break;
case 'r':
- aflags.no_format = 1;
+ aflags->no_format = 1;
break;
case 'S':
- aflags.machine = optarg;
+ aflags->machine = optarg;
break;
case 's':
- s = atoi(optarg);
- if (s < 1 || s > 9)
+ ch = atoi(optarg);
+ if (ch < 1 || ch > 9)
errx(EXIT_FAILURE, "Invalid section");
- aflags.sec_nums[s - 1] = 1;
+ aflags->sec_nums[ch - 1] = 1;
break;
case '?':
default:
usage();
}
}
+}
+
+int
+main(int argc, char *argv[])
+{
+ query_args args;
+ char *query = NULL; // the user query
+ char *errmsg = NULL;
+ char *str;
+ int rc = 0;
+ int s;
+ callback_data cbdata;
+ cbdata.out = stdout; // the default output stream
+ cbdata.count = 0;
+ apropos_flags aflags;
+ cbdata.aflags = &aflags;
+ sqlite3 *db;
+ setprogname(argv[0]);
+ if (argc < 2)
+ usage();
+
+ memset(&aflags, 0, sizeof(aflags));
+
+ if (!isatty(STDOUT_FILENO))
+ aflags.no_format = 1;
+
+ if ((str = getenv("APROPOS")) != NULL) {
+ char **ptr = emalloc((strlen(str) + 2) * sizeof(*ptr));
+#define WS "\t\n\r "
+ ptr[0] = __UNCONST(getprogname());
+ for (s = 1, str = strtok(str, WS); str;
+ str = strtok(NULL, WS), s++)
+ ptr[s] = str;
+ ptr[s] = NULL;
+ parseargs(s, ptr, &aflags);
+ free(ptr);
+ optreset = 1;
+ optind = 1;
+ }
+
+ parseargs(argc, argv, &aflags);
+
+ /*
+ * If the user specifies a section number as an option, the
+ * corresponding index element in sec_nums is set to the string
+ * representing that section number.
+ */
argc -= optind;
argv += optind;
@@ -169,6 +203,7 @@ main(int argc, char *argv[])
args.search_str = query;
args.sec_nums = aflags.sec_nums;
+ args.legacy = aflags.legacy;
args.nrec = aflags.nresults ? aflags.nresults : 10;
args.offset = 0;
args.machine = aflags.machine;
@@ -177,10 +212,10 @@ main(int argc, char *argv[])
args.errmsg = &errmsg;
args.flags = aflags.no_format ? APROPOS_NOFORMAT : 0;
- if (isatty(STDOUT_FILENO))
- rc = run_query_term(db, &args);
- else
+ if (aflags.pager)
rc = run_query_pager(db, &args);
+ else
+ rc = run_query_term(db, &args);
free(query);
close_db(db);
@@ -197,8 +232,8 @@ main(int argc, char *argv[])
if (cbdata.count == 0) {
warnx("No relevant results obtained.\n"
- "Please make sure that you spelled all the terms correctly "
- "or try using better keywords.");
+ "Please make sure that you spelled all the terms correctly "
+ "or try using better keywords.");
}
return 0;
}
@@ -217,7 +252,8 @@ query_callback(void *data, const char *s
callback_data *cbdata = (callback_data *) data;
FILE *out = cbdata->out;
cbdata->count++;
- fprintf(out, "%s (%s)\t%s\n", name, section, name_desc);
+ fprintf(out, cbdata->aflags->legacy ? "%s(%s)\t- %s\n" :
+ "%s (%s)\t%s\n", name, section, name_desc);
if (cbdata->aflags->no_context == 0)
fprintf(out, "%s\n\n", snippet);
@@ -277,8 +313,8 @@ remove_stopwords(const char *query)
static void
usage(void)
{
- fprintf(stderr,
- "Usage: %s [-n Number of records] [-123456789Ccp] [-S machine] query\n",
- getprogname());
+ fprintf(stderr, "Usage: %s [-123456789Ccilpr] [-n <results>] "
+ "[-s <section>] [-S <machine>] <query>\n",
+ getprogname());
exit(1);
}