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.
This commit is contained in:
Daniel Eklöf 2020-07-29 19:42:12 +02:00
parent 906c8f4d16
commit b3d0215c38
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
6 changed files with 51 additions and 5 deletions

View file

@ -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

View file

@ -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);

34
slave.c
View file

@ -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;

View file

@ -3,6 +3,9 @@
#include <sys/types.h>
#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);

View file

@ -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;
}

View file

@ -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;