~emersion/mrsh-dev

readline: reset on SIGINT v1 PROPOSED

Sweet :)

Pushed:

To git.sr.ht:~emersion/mrsh
   5e6fe9c68eba..763feb2ee5dd  master -> master

Thanks!
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~emersion/mrsh-dev/%3C20190902025956.1233-1-sir%40cmpwn.com%3E/mbox | git am -3
Learn more about email & git

[PATCH] readline: reset on SIGINT Export this patch

---
 frontend/readline.c | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/frontend/readline.c b/frontend/readline.c
index 0673aa8..886f7ef 100644
--- a/frontend/readline.c
+++ b/frontend/readline.c
@@ -6,6 +6,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <signal.h>
+#include <unistd.h>
 #if defined(HAVE_READLINE)
 #include <readline/history.h>
 #include <readline/readline.h>
@@ -15,6 +17,17 @@
 #endif
 #include "frontend.h"
 
+static void sigint_handler(int n) {
+	/* Signal safety is done here on a best-effort basis. rl_redisplay is not
+	 * signal safe, but under these circumstances it's very likely that the
+	 * interrupted function will not be affected. */
+	char newline = '\n';
+	write(STDOUT_FILENO, &newline, 1);
+	rl_on_new_line();
+	rl_replace_line("", 0);
+	rl_redisplay();
+}
+
 static const char *get_history_path(void) {
 	static char history_path[PATH_MAX + 1];
 	snprintf(history_path, sizeof(history_path),
@@ -29,7 +42,11 @@ void interactive_init(struct mrsh_state *state) {
 
 size_t interactive_next(struct mrsh_state *state,
 		char **line, const char *prompt) {
+	struct sigaction sa = { .sa_handler = sigint_handler }, old;
+	sigaction(SIGINT, &sa, &old);
 	char *rline = readline(prompt);
+	sigaction(SIGINT, &old, NULL);
+
 	if (!rline) {
 		return 0;
 	}
-- 
2.23.0
Sweet :)

Pushed:

To git.sr.ht:~emersion/mrsh
   5e6fe9c68eba..763feb2ee5dd  master -> master

Thanks!
View this thread in the archives