Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/usr.sbin/makemandb - If the stdout is not a tty, prevent for...
details: https://anonhg.NetBSD.org/src/rev/7918ff96460d
branches: trunk
changeset: 785767:7918ff96460d
user: christos <christos%NetBSD.org@localhost>
date: Fri Mar 29 20:07:31 2013 +0000
description:
- 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.
diffstat:
usr.sbin/makemandb/apropos-utils.c | 62 +++++++++++---
usr.sbin/makemandb/apropos-utils.h | 3 +-
usr.sbin/makemandb/apropos.1 | 21 +++-
usr.sbin/makemandb/apropos.c | 152 ++++++++++++++++++++++--------------
4 files changed, 159 insertions(+), 79 deletions(-)
diffs (truncated from 396 to 300 lines):
diff -r b50a2ed45eed -r 7918ff96460d usr.sbin/makemandb/apropos-utils.c
--- a/usr.sbin/makemandb/apropos-utils.c Fri Mar 29 18:53:21 2013 +0000
+++ b/usr.sbin/makemandb/apropos-utils.c Fri Mar 29 20:07:31 2013 +0000
@@ -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 <er.abhinav.upadhyay%gmail.com@localhost>
* 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 @@
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 @@
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);
diff -r b50a2ed45eed -r 7918ff96460d usr.sbin/makemandb/apropos-utils.h
--- a/usr.sbin/makemandb/apropos-utils.h Fri Mar 29 18:53:21 2013 +0000
+++ b/usr.sbin/makemandb/apropos-utils.h Fri Mar 29 20:07:31 2013 +0000
@@ -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 <er.abhinav.upadhyay%gmail.com@localhost>
* All rights reserved.
@@ -74,6 +74,7 @@
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
diff -r b50a2ed45eed -r 7918ff96460d usr.sbin/makemandb/apropos.1
--- a/usr.sbin/makemandb/apropos.1 Fri Mar 29 18:53:21 2013 +0000
+++ b/usr.sbin/makemandb/apropos.1 Fri Mar 29 20:07:31 2013 +0000
@@ -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 <er.abhinav.upadhyay%gmail.com@localhost>
.\" 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 @@
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 @@
.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 ,
diff -r b50a2ed45eed -r 7918ff96460d usr.sbin/makemandb/apropos.c
--- a/usr.sbin/makemandb/apropos.c Fri Mar 29 18:53:21 2013 +0000
+++ b/usr.sbin/makemandb/apropos.c Fri Mar 29 20:07:31 2013 +0000
@@ -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 <er.abhinav.upadhyay%gmail.com@localhost>
* 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 @@
int pager;
int no_context;
int no_format;
+ int legacy;
const char *machine;
} apropos_flags;
@@ -66,6 +67,62 @@
#define _PATH_PAGER "/usr/bin/more -s"
+static void
+parseargs(int argc, char **argv, struct apropos_flags *aflags)
+{
+ int ch;
+ while ((ch = getopt(argc, argv, "123456789Cciln:prS:s:")) != -1) {
+ switch (ch) {
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ aflags->sec_nums[ch - '1'] = 1;
+ break;
+ case 'C':
+ aflags->no_context = 1;
+ break;
+ case 'c':
+ 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);
+ break;
+ case 'p': // user wants a pager
+ aflags->pager = 1;
+ break;
+ case 'r':
+ aflags->no_format = 1;
+ break;
+ case 'S':
+ aflags->machine = optarg;
+ break;
+ case 's':
+ ch = atoi(optarg);
+ if (ch < 1 || ch > 9)
+ errx(EXIT_FAILURE, "Invalid section");
+ aflags->sec_nums[ch - 1] = 1;
+ break;
+ case '?':
+ default:
+ usage();
+ }
+ }
+}
+
int
main(int argc, char *argv[])
{
@@ -73,7 +130,7 @@
char *query = NULL; // the user query
char *errmsg = NULL;
char *str;
- int ch, rc = 0;
+ int rc = 0;
int s;
callback_data cbdata;
cbdata.out = stdout; // the default output stream
@@ -87,53 +144,30 @@
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.
+ 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.
*/
- while ((ch = getopt(argc, argv, "123456789Ccn:prS:s:")) != -1) {
- switch (ch) {
- case '1':
- case '2':
- case '3':
Home |
Main Index |
Thread Index |
Old Index