NetBSD-Users archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: Option -p in apropos(1)



On Mon, 16 May 2022, David H. Gutteridge wrote:

Thanks for this, I've applied a similar patch to HEAD. Assuming
everyone's satisfied, I could request a pullup to netbsd-9.


Thanks for that. Another patch below:

1. Ignore SIGPIPE so that we're not killed in the middle of some
   DB operation by a botched $PAGER:

$ env PAGER=/non-existent apropos -p ...

2. Return proper exit status in case of write errors:

$ apropos ... >/dev/full || echo fail

---START---
diff -urN usr.sbin/makemandb.orig/apropos-utils.c usr.sbin/makemandb/apropos-utils.c
--- usr.sbin/makemandb.orig/apropos-utils.c	2021-11-29 01:30:38.992104592 +0000
+++ usr.sbin/makemandb/apropos-utils.c	2022-05-17 07:13:57.441043153 +0000
@@ -665,7 +665,7 @@
  * 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;
@@ -699,8 +699,8 @@
 		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 = get_stmt_col_text(stmt, 0);
 		name_temp = get_stmt_col_text(stmt, 1);
@@ -725,11 +725,11 @@
 		}
 		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;
 }


@@ -752,9 +752,9 @@
 		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 *
@@ -845,10 +845,10 @@
 	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;
 }

 /*
@@ -968,12 +968,12 @@
 	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 {
@@ -1013,11 +1013,11 @@
 	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 -urN usr.sbin/makemandb.orig/apropos.c usr.sbin/makemandb/apropos.c
--- usr.sbin/makemandb.orig/apropos.c	2022-05-17 06:33:28.378038655 +0000
+++ usr.sbin/makemandb/apropos.c	2022-05-17 07:39:44.951522546 +0000
@@ -34,6 +34,7 @@
 __RCSID("$NetBSD: apropos.c,v 1.25 2022/05/17 00:21:22 gutteridge Exp $");

 #include <err.h>
+#include <signal.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -223,6 +224,10 @@
 		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);
@@ -286,7 +291,7 @@
 /*
  * 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.
  */
@@ -308,7 +313,7 @@
 		    fprintf(out, "<tr><td colspan=2>%s</td></tr>\n", qargs->snippet);
 	}

-	return 0;
+	return fflush(out);
 }

 #include "stopwords.c"
---END---

Thx,
-RVP


Home | Main Index | Thread Index | Old Index