From b3d0215c388c51a7e9838d16fe06023844af5c8b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Wed, 29 Jul 2020 19:42:12 +0200 Subject: [PATCH] term: add capability to print warnings *inside* the terminal This is intended to be used to print e.g. deprecation warnings inside the terminal, *before* the shell is started. --- config.c | 6 ++++++ config.h | 2 ++ slave.c | 34 +++++++++++++++++++++++++++++++--- slave.h | 5 ++++- terminal.c | 3 ++- terminal.h | 6 ++++++ 6 files changed, 51 insertions(+), 5 deletions(-) diff --git a/config.c b/config.c index fc029884..a7c93ad9 100644 --- a/config.c +++ b/config.c @@ -1170,6 +1170,8 @@ config_load(struct config *conf, const char *conf_path) .delayed_render_upper_ns = 16666666 / 2, /* half a frame period (60Hz) */ .max_shm_pool_size = 512 * 1024 * 1024, }, + + .warnings = tll_init(), }; struct config_key_binding_normal scrollback_up = {BIND_ACTION_SCROLLBACK_UP, strdup("Shift+Page_Up")}; @@ -1284,6 +1286,10 @@ config_free(struct config conf) tll_free(conf.bindings.key); tll_free(conf.bindings.mouse); tll_free(conf.bindings.search); + + tll_foreach(conf.warnings, it) + free(it->item.text); + tll_free(conf.warnings); } struct config_font diff --git a/config.h b/config.h index 9de282a0..aaaa3591 100644 --- a/config.h +++ b/config.h @@ -119,6 +119,8 @@ struct config { uint64_t delayed_render_upper_ns; off_t max_shm_pool_size; } tweak; + + user_warning_list_t warnings; }; bool config_load(struct config *conf, const char *path); diff --git a/slave.c b/slave.c index 30b5c4d9..6b14dd61 100644 --- a/slave.c +++ b/slave.c @@ -18,6 +18,7 @@ #define LOG_ENABLE_DBG 0 #include "log.h" +#include "terminal.h" #include "tokenize.h" static bool @@ -67,7 +68,8 @@ err: } static void -slave_exec(int ptmx, char *argv[], int err_fd, bool login_shell) +slave_exec(int ptmx, char *argv[], int err_fd, bool login_shell, + size_t warning_count, struct user_warning warnings[static warning_count]) { int pts = -1; const char *pts_name = ptsname(ptmx); @@ -122,6 +124,18 @@ slave_exec(int ptmx, char *argv[], int err_fd, bool login_shell) goto err; } + for (size_t i = 0; i < warning_count; i++) { + switch (warnings[i].kind) { + case USER_WARNING_DEPRECATION: + write(pts, "\e[33;1;5mdeprecated:\e[39;21;25m ", 32); + } + + write(pts, warnings[i].text, strlen(warnings[i].text)); + write(pts, "\e[m\n", 4); + free(warnings[i].text); + } + free(warnings); + close(pts); pts = -1; @@ -152,7 +166,8 @@ err: pid_t slave_spawn(int ptmx, int argc, const char *cwd, char *const *argv, - const char *term_env, const char *conf_shell, bool login_shell) + const char *term_env, const char *conf_shell, bool login_shell, + const user_warning_list_t *warnings) { int fork_pipe[2]; if (pipe2(fork_pipe, O_CLOEXEC) < 0) { @@ -220,7 +235,20 @@ slave_spawn(int ptmx, int argc, const char *cwd, char *const *argv, if (is_valid_shell(shell_argv[0])) setenv("SHELL", shell_argv[0], 1); - slave_exec(ptmx, shell_argv, fork_pipe[1], login_shell); + struct user_warning *warnings_copy = malloc( + tll_length(*warnings) * sizeof(warnings_copy[0])); + { + size_t i = 0; + tll_foreach(*warnings, it){ + warnings_copy[i] = (struct user_warning){ + .kind = it->item.kind, + .text = strdup(it->item.text), + }; + } + } + + slave_exec(ptmx, shell_argv, fork_pipe[1], login_shell, + tll_length(*warnings), warnings_copy); assert(false); break; diff --git a/slave.h b/slave.h index 83891590..f7324704 100644 --- a/slave.h +++ b/slave.h @@ -3,6 +3,9 @@ #include +#include "terminal.h" + pid_t slave_spawn( int ptmx, int argc, const char *cwd, char *const *argv, const char *term_env, - const char *conf_shell, bool login_shell); + const char *conf_shell, bool login_shell, + const user_warning_list_t *warnings); diff --git a/terminal.c b/terminal.c index 81592710..6b8ed69d 100644 --- a/terminal.c +++ b/terminal.c @@ -943,7 +943,8 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper, /* Start the slave/client */ if ((term->slave = slave_spawn( term->ptmx, argc, term->cwd, argv, - conf->term, conf->shell, conf->login_shell)) == -1) + conf->term, conf->shell, conf->login_shell, + &conf->warnings)) == -1) { goto err; } diff --git a/terminal.h b/terminal.h index 31316e37..db3ec555 100644 --- a/terminal.h +++ b/terminal.h @@ -206,6 +206,12 @@ enum term_surface { TERM_SURF_BUTTON_CLOSE, }; +struct user_warning { + enum { USER_WARNING_DEPRECATION } kind; + char *text; +}; +typedef tll(struct user_warning) user_warning_list_t; + struct terminal { struct fdm *fdm; struct reaper *reaper;