From 35351387a197e55f5389545efe3941b58ca94ffd Mon Sep 17 00:00:00 2001 From: Daniel Santos Date: Fri, 24 Mar 2023 14:56:35 +0000 Subject: /executable async on|off * rename call_external() to call_external_async(). * add call_external_fork(). This function makes all executable calls to be forked and synchronous. So that running profanity inside a TTY, we can set all executables to be TTY programs (fbi, mpv, w3m, emacs eww, etc.), making possible to open urls or see images inside the TTY. * add '/executable async' command. * make call_external() use either call_external_async() or call_external_fork(), according to the '/executable async' configuration. Signed-off-by: Daniel Santos --- src/common.c | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) (limited to 'src/common.c') diff --git a/src/common.c b/src/common.c index ab5c6c8b..d2bbc1c6 100644 --- a/src/common.c +++ b/src/common.c @@ -46,6 +46,13 @@ #include #include +// fork / exec +#include +#include +#include "ui/ui.h" + +#include "config/preferences.h" + #include #include #include @@ -466,7 +473,7 @@ get_mentions(gboolean whole_word, gboolean case_sensitive, const char* const mes } gboolean -call_external(gchar** argv) +call_external_async(gchar** argv) { GError* spawn_error; gboolean is_successful; @@ -489,6 +496,38 @@ call_external(gchar** argv) return is_successful; } +gboolean +call_external_fork(gchar** argv) +{ + // Fork / exec external executable + pid_t pid = fork(); + if (pid == 0) { + int x = execvp(argv[0], argv); + if (x == -1) { + auto_gchar gchar* cmd = g_strjoinv(" ", argv); + log_error("Unable to call external executable '%s'", cmd); + } + _exit(EXIT_FAILURE); + } else { + if (pid == -1) { + return TRUE; + } + waitpid(pid, NULL, 0); + + // refresh display + ui_resize(); + rl_forced_update_display(); + } + + return TRUE; +} + +gboolean +call_external(gchar** argv) +{ + return prefs_get_boolean(PREF_EXECUTABLE_ASYNC) ? call_external_async(argv) : call_external_fork(argv); +} + gchar** format_call_external_argv(const char* template, const char* url, const char* filename) { -- cgit 1.4.1-2-gfad0