Add ignoreof

This commit is contained in:
savalet
2025-05-09 17:41:53 +02:00
parent 148e738680
commit a69acc0295
4 changed files with 15 additions and 3 deletions

View File

@@ -50,6 +50,12 @@ int special_case(ef_t *ef, char **args)
static
bool handle_special_variables(ef_t *ef, char **args, char *var, int i)
{
if (strcmp(var, "ignoreof") == 0) {
ef->exec_ctx->ignoreof = true;
return true;
}
if (args[i] == nullptr)
return false;
if (strcmp(var, "precmd") == 0) {
ef->exec_ctx->precmd = strdup(args[i]);
return true;
@@ -87,6 +93,8 @@ int builtins_set(ef_t *ef, char **args)
{
char *var = nullptr;
if (args[1] != nullptr && handle_special_variables(ef, args, args[1], 2))
return RETURN_SUCCESS;
if (my_array_len(args) < 3)
return special_case(ef, args);
return handle_set(ef, args, var);

View File

@@ -30,6 +30,7 @@ typedef struct {
size_t prompt_len;
char *precmd;
char *cwdcmd;
bool ignoreof;
} exec_ctx_t;
size_t update_command(char **buffer,

View File

@@ -19,8 +19,10 @@ bool handle_key_ctrl_c(readline_helper_t *rh, exec_ctx_t *ec, buff_t *buff)
return false;
}
bool handle_key_ctrl_d(readline_helper_t *, exec_ctx_t *, buff_t *buff)
bool handle_key_ctrl_d(readline_helper_t *, exec_ctx_t *ec, buff_t *buff)
{
if (ec->ignoreof)
return false;
buff->sz = 0;
return true;
}

View File

@@ -82,6 +82,7 @@ int shell_loop(int is_a_tty, exec_ctx_t *exec_ctx)
buff_t buff = { .str = nullptr, 0, .cap = 0 };
struct termios repl_settings;
U_DEBUG_CALL(debug_env_entries, exec_ctx->env);
exec_ctx->isatty = is_a_tty;
tcgetattr(STDIN_FILENO, &repl_settings);
exec_ctx->saved_term_settings = repl_settings;
@@ -152,12 +153,12 @@ int shell(opt_t *opt, char **env_ptr)
local_t local = create_local();
exec_ctx_t exec_ctx = {.env = &env, .local = &local, .opt = opt,
.read_fd = get_read_fd(opt), .history = &history, .precmd = nullptr,
.history_command = cmd_history, .alias = &alias, 0, .cwdcmd = nullptr};
.history_command = cmd_history, .alias = &alias, 0, .cwdcmd = nullptr,
.ignoreof = false };
int shell_result;
if (exec_ctx.read_fd == -1 || (int)error_in_init(&exec_ctx))
return RETURN_FAILURE;
U_DEBUG_CALL(debug_env_entries, &env);
shell_result = shell_loop(isatty(exec_ctx.read_fd), &exec_ctx);
if (opt->cmd == NULL && isatty(exec_ctx.read_fd)) {
WRITE_CONST(STDOUT_FILENO, "exit\n");