Module Name: src
Committed By: martin
Date: Fri Jun 3 12:25:14 UTC 2022
Modified Files:
src/usr.sbin/makemandb [netbsd-9]: apropos-utils.c apropos.1 apropos.c
Log Message:
Pull up following revision(s) (requested by gutteridge in ticket #1461):
usr.sbin/makemandb/apropos.1: revision 1.19
usr.sbin/makemandb/apropos.c: revision 1.25
usr.sbin/makemandb/apropos.c: revision 1.26
usr.sbin/makemandb/apropos.1: revision 1.20
usr.sbin/makemandb/apropos.1: revision 1.21
usr.sbin/makemandb/apropos.1: revision 1.22
usr.sbin/makemandb/apropos.1: revision 1.23
usr.sbin/makemandb/apropos-utils.c: revision 1.46
usr.sbin/makemandb/apropos-utils.c: revision 1.47
usr.sbin/makemandb/apropos-utils.c: revision 1.49
PR/54343: Prevent NULL pointers in callback strings; use "*?*" for now to
identify them.
PR bin/54343: We want the callback_args.machine to be NULL if it is not
present in the DB.
The previous commit fixed the problem of allowing apropos to not crash and
produce output even if the database is missing values for certain mandatory
fields, such as name, section etc. Normally we don't expect those values
to be missing in the database but in case of parsing errors it can happen.
However, the machine architecture is an optional field since not all man pages
are hardware specific so that should be allowed to be set to NULL if not
present in the database.
apropos.c: fix pager functionality
Issue reported by Rocky Hotas on NetBSD-Users, patch input from RVP on
same, adjustments by me.
apropos.1: document the PAGER environment variable
apropos(1): use proper -width
apropos(1): use proper -width for the list of options too
apropos(1): Tweak the description of -1, ... -9, and -s
-s is not for compatibility only, because section names can be
anything. E.g. we have 3lua and 9lua in base. We have rudiments of
3f (for FORTRAN libs). Some packages in pkgsrc also use suffixed 1
and 3 sections.
apropos(1): Use the official spelling for "SQLite".
While here, use .Bx to refer to 3BSD.
apropos(1): improve error handling in edge cases
Patch from RVP on NetBSD-Users, with an additional comment tweak by me.
Summary from RVP:
1. Ignore SIGPIPE so that we're not killed in the middle of some
DB operation by a botched $PAGER:
$ env PAGER=3D/non-existent apropos -p ...
2. Return proper exit status in case of write errors:
$ apropos ... >/dev/full || echo fail
To generate a diff of this commit:
cvs rdiff -u -r1.45 -r1.45.2.1 src/usr.sbin/makemandb/apropos-utils.c
cvs rdiff -u -r1.18 -r1.18.18.1 src/usr.sbin/makemandb/apropos.1
cvs rdiff -u -r1.24 -r1.24.6.1 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.45 src/usr.sbin/makemandb/apropos-utils.c:1.45.2.1
--- src/usr.sbin/makemandb/apropos-utils.c:1.45 Fri Jun 7 16:43:58 2019
+++ src/usr.sbin/makemandb/apropos-utils.c Fri Jun 3 12:25:14 2022
@@ -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 <[email protected]>
* 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 *
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 *
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 ch
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 *callb
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 *call
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 *callb
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;
}
/*
Index: src/usr.sbin/makemandb/apropos.1
diff -u src/usr.sbin/makemandb/apropos.1:1.18 src/usr.sbin/makemandb/apropos.1:1.18.18.1
--- src/usr.sbin/makemandb/apropos.1:1.18 Fri Jun 17 18:48:07 2016
+++ src/usr.sbin/makemandb/apropos.1 Fri Jun 3 12:25:14 2022
@@ -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 <[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 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 enviro
.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
Index: src/usr.sbin/makemandb/apropos.c
diff -u src/usr.sbin/makemandb/apropos.c:1.24 src/usr.sbin/makemandb/apropos.c:1.24.6.1
--- src/usr.sbin/makemandb/apropos.c:1.24 Sat Nov 25 14:29:38 2017
+++ src/usr.sbin/makemandb/apropos.c Fri Jun 3 12:25:14 2022
@@ -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 <[email protected]>
* 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 *qarg
fprintf(out, "<tr><td colspan=2>%s</td></tr>\n", qargs->snippet);
}
- return 0;
+ return fflush(out);
}
#include "stopwords.c"