summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--usr.sbin/makemandb/apropos-utils.c51
-rw-r--r--usr.sbin/makemandb/apropos.145
-rw-r--r--usr.sbin/makemandb/apropos.c27
3 files changed, 77 insertions, 46 deletions
diff --git a/usr.sbin/makemandb/apropos-utils.c b/usr.sbin/makemandb/apropos-utils.c
index b32cc53aed6..ec667ee0c41 100644
--- a/usr.sbin/makemandb/apropos-utils.c
+++ b/usr.sbin/makemandb/apropos-utils.c
@@ -1,4 +1,4 @@
-/* $NetBSD: apropos-utils.c,v 1.45 2019/06/07 16:43:58 leot Exp $ */
+/* $NetBSD: apropos-utils.c,v 1.45.2.1 2022/06/03 12:25:14 martin Exp $ */
/*-
* Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadhyay@gmail.com>
* All rights reserved.
@@ -31,7 +31,7 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: apropos-utils.c,v 1.45 2019/06/07 16:43:58 leot Exp $");
+__RCSID("$NetBSD: apropos-utils.c,v 1.45.2.1 2022/06/03 12:25:14 martin Exp $");
#include <sys/queue.h>
#include <sys/stat.h>
@@ -654,11 +654,18 @@ RETURN:
return query;
}
+static const char *
+get_stmt_col_text(sqlite3_stmt *stmt, int col)
+{
+ const char *t = (const char *) sqlite3_column_text(stmt, col);
+ return t == NULL ? "*?*" : t;
+}
+
/*
* Execute the full text search query and return the number of results
* obtained.
*/
-static unsigned int
+static int
execute_search_query(sqlite3 *db, char *query, query_args *args)
{
sqlite3_stmt *stmt;
@@ -692,16 +699,17 @@ execute_search_query(sqlite3 *db, char *query, query_args *args)
return -1;
}
- unsigned int nresults = 0;
- while (sqlite3_step(stmt) == SQLITE_ROW) {
+ int nresults = rc = 0;
+ while (rc == 0 && sqlite3_step(stmt) == SQLITE_ROW) {
nresults++;
- callback_args.section = (const char *) sqlite3_column_text(stmt, 0);
- name_temp = (const char *) sqlite3_column_text(stmt, 1);
- callback_args.name_desc = (const char *) sqlite3_column_text(stmt, 2);
+ callback_args.section = get_stmt_col_text(stmt, 0);
+ name_temp = get_stmt_col_text(stmt, 1);
+ callback_args.name_desc = get_stmt_col_text(stmt, 2);
callback_args.machine = (const char *) sqlite3_column_text(stmt, 3);
if (!args->legacy) {
- callback_args.snippet = (const char *) sqlite3_column_text(stmt, 4);
- callback_args.snippet_length = strlen(callback_args.snippet);
+ callback_args.snippet = get_stmt_col_text(stmt, 4);
+ callback_args.snippet_length =
+ strlen(callback_args.snippet);
} else {
callback_args.snippet = "";
callback_args.snippet_length = 1;
@@ -713,16 +721,15 @@ execute_search_query(sqlite3 *db, char *query, query_args *args)
easprintf(&name, "%s/%s", lower(m), name_temp);
free(m);
} else {
- name = estrdup((const char *)
- sqlite3_column_text(stmt, 1));
+ name = estrdup(get_stmt_col_text(stmt, 1));
}
callback_args.name = name;
callback_args.other_data = args->callback_data;
- (args->callback)(&callback_args);
+ rc = (args->callback)(&callback_args);
free(name);
}
sqlite3_finalize(stmt);
- return nresults;
+ return (rc < 0) ? rc : nresults;
}
@@ -745,9 +752,9 @@ run_query_internal(sqlite3 *db, const char *snippet_args[3], query_args *args)
return -1;
}
- execute_search_query(db, query, args);
+ int rc = execute_search_query(db, query, args);
sqlite3_free(query);
- return *(args->errmsg) == NULL ? 0 : -1;
+ return (rc < 0 || *(args->errmsg) != NULL) ? -1 : 0;
}
static char *
@@ -838,10 +845,10 @@ callback_html(query_callback_args *callback_args)
callback_args->snippet = qsnippet;
callback_args->snippet_length = length;
callback_args->other_data = orig_data->data;
- (*callback)(callback_args);
+ int rc = (*callback)(callback_args);
free(qsnippet);
free(qname_description);
- return 0;
+ return rc;
}
/*
@@ -961,12 +968,12 @@ callback_pager(query_callback_args *callback_args)
callback_args->snippet = psnippet;
callback_args->snippet_length = psnippet_length;
callback_args->other_data = orig_data->data;
- (orig_data->callback)(callback_args);
+ int rc = (orig_data->callback)(callback_args);
free(ul_section);
free(ul_name);
free(ul_name_desc);
free(psnippet);
- return 0;
+ return rc;
}
struct term_args {
@@ -1006,11 +1013,11 @@ callback_term(query_callback_args *callback_args)
callback_args->name = ul_name;
callback_args->name_desc = ul_name_desc;
callback_args->other_data = orig_data->data;
- (orig_data->callback)(callback_args);
+ int rc = (orig_data->callback)(callback_args);
free(ul_section);
free(ul_name);
free(ul_name_desc);
- return 0;
+ return rc;
}
/*
diff --git a/usr.sbin/makemandb/apropos.1 b/usr.sbin/makemandb/apropos.1
index f521497363c..6a48b67e21c 100644
--- a/usr.sbin/makemandb/apropos.1
+++ b/usr.sbin/makemandb/apropos.1
@@ -1,4 +1,4 @@
-.\" $NetBSD: apropos.1,v 1.18 2016/06/17 18:48:07 abhinav Exp $
+.\" $NetBSD: apropos.1,v 1.18.18.1 2022/06/03 12:25:14 martin Exp $
.\"
.\" Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadhyay@gmail.com>
.\" All rights reserved.
@@ -29,7 +29,7 @@
.\" OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
-.Dd June 16, 2016
+.Dd May 17, 2022
.Dt APROPOS 1
.Os
.Sh NAME
@@ -47,7 +47,7 @@
The
.Nm
utility performs a full text search over the complete content of all man pages.
-It uses the FTS engine of Sqlite to perform the search.
+It uses the FTS engine of SQLite to perform the search.
The database is created with the help of the
.Xr makemandb 8
utility.
@@ -62,9 +62,13 @@ relevance.
Quotes are optional for specifying multiword queries.
.Pp
It supports the following options:
-.Bl -tag -width indent
-.It Fl [1-9]
-Search only within the specified section of manual pages.
+.Bl -tag -width Fl
+.It Fl 1 , No \&... , Fl 9
+Limit the search to the specified section of the manual.
+By default, pages from all sections are shown.
+These options are abbreviations for the
+.Fl s
+option for single digit sections.
.It Fl C Ar path
Use different
.Xr man 1
@@ -95,26 +99,32 @@ Turn off formatting.
Limit the search to the pages for the specified machine architecture.
By default pages for all architectures are shown in the search results.
.It Fl s Ar section
-Restrict the search to the specified section of the manual.
+Limit the search to the specified section of the manual.
By default, pages from all sections are shown.
-This option is for backwards compatibility with the classic version of apropos,
-using it is equivalent to using the
-.Op 123456789
-options directly.
+For single digit sections you can use abbreviations:
+.Fl 1 , No \&... , Fl 9 .
.El
.Sh FILES
.Bl -hang -width /etc/man.conf -compact
.It Pa /etc/man.conf
-The location of the Sqlite FTS database can be configured using the
+The location of the SQLite FTS database can be configured using the
.Cd _mandb
tag.
.El
.Sh ENVIRONMENT VARIABLES
-The
-.Ev APROPOS
-environment variable is white-space tokenized as an argument vector
+.Bl -tag -width Ev
+.It Ev APROPOS
+This environment variable is white-space tokenized as an argument vector
and the options in it are parsed and set.
Command line options override the environment options.
+.It Ev PAGER
+The pagination command used for writing the output, should this be requested.
+If the
+.Ev PAGER
+environment variable is null or not set, the standard pagination program
+.Xr more 1
+will be used.
+.El
.Sh SEE ALSO
.Xr man 1 ,
.Xr whatis 1 ,
@@ -123,9 +133,10 @@ Command line options override the environment options.
.Sh HISTORY
The
.Nm
-command appeared in 3.0BSD.
+command appeared in
+.Bx 3 .
It was rewritten in
.Nx 6.0
-to support full text search using Sqlite.
+to support full text search using SQLite.
.Sh AUTHORS
.An Abhinav Upadhyay
diff --git a/usr.sbin/makemandb/apropos.c b/usr.sbin/makemandb/apropos.c
index 3292b33a748..144946271eb 100644
--- a/usr.sbin/makemandb/apropos.c
+++ b/usr.sbin/makemandb/apropos.c
@@ -1,4 +1,4 @@
-/* $NetBSD: apropos.c,v 1.24 2017/11/25 14:29:38 abhinav Exp $ */
+/* $NetBSD: apropos.c,v 1.24.6.1 2022/06/03 12:25:14 martin Exp $ */
/*-
* Copyright (c) 2011 Abhinav Upadhyay <er.abhinav.upadhyay@gmail.com>
* All rights reserved.
@@ -31,9 +31,10 @@
*/
#include <sys/cdefs.h>
-__RCSID("$NetBSD: apropos.c,v 1.24 2017/11/25 14:29:38 abhinav Exp $");
+__RCSID("$NetBSD: apropos.c,v 1.24.6.1 2022/06/03 12:25:14 martin Exp $");
#include <err.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -157,6 +158,7 @@ main(int argc, char *argv[])
char *query = NULL; // the user query
char *errmsg = NULL;
char *str;
+ int pc = 0;
int rc = 0;
size_t i;
int s;
@@ -222,6 +224,10 @@ main(int argc, char *argv[])
const char *pager = getenv("PAGER");
if (pager == NULL)
pager = _PATH_PAGER;
+
+ /* Don't get killed by a broken pipe */
+ signal(SIGPIPE, SIG_IGN);
+
/* Open a pipe to the pager */
if ((cbdata.out = popen(pager, "w")) == NULL) {
close_db(db);
@@ -249,6 +255,8 @@ main(int argc, char *argv[])
if (aflags.format == APROPOS_HTML)
fprintf(cbdata.out, "</table>\n</body>\n</html>\n");
+ if (aflags.pager)
+ pc = pclose(cbdata.out);
free(query);
if (aflags.sections) {
@@ -264,10 +272,15 @@ main(int argc, char *argv[])
exit(EXIT_FAILURE);
}
- if (rc < 0) {
- /* Something wrong with the database. Exit */
+ if (pc == -1)
+ err(EXIT_FAILURE, "pclose error");
+
+ /*
+ * Something wrong with the database, writing output, or a non-existent
+ * pager.
+ */
+ if (rc < 0)
exit(EXIT_FAILURE);
- }
if (cbdata.count == 0) {
warnx("No relevant results obtained.\n"
@@ -280,7 +293,7 @@ main(int argc, char *argv[])
/*
* query_callback --
* Callback function for run_query.
- * It simply outputs the results from do_query. If the user specified the -p
+ * It simply outputs the results from run_query. If the user specified the -p
* option, then the output is sent to a pager, otherwise stdout is the default
* output stream.
*/
@@ -302,7 +315,7 @@ query_callback(query_callback_args *qargs)
fprintf(out, "<tr><td colspan=2>%s</td></tr>\n", qargs->snippet);
}
- return 0;
+ return fflush(out);
}
#include "stopwords.c"