From ba088f0e19d3ecfcf68a9ac203673848fc9e38cd Mon Sep 17 00:00:00 2001 From: Melefo <42809472+Melefo@users.noreply.github.com> Date: Fri, 22 May 2020 11:36:55 +0200 Subject: [PATCH 1/4] Adding ignoreeof/^D support --- include/utility.h | 1 + src/key_bindings/control_commands.c | 41 ++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/include/utility.h b/include/utility.h index 94fe99d..42f514e 100644 --- a/include/utility.h +++ b/include/utility.h @@ -13,6 +13,7 @@ char *catpath(char *p1, char *p2); char **to_array(char *str); char *tostr(int n); bool is_alpha(char c); +bool is_num(char c); bool envvar_is_valid(const char *str); int count_str(char *str, char *delim); char **split_str(char *str, char delim); diff --git a/src/key_bindings/control_commands.c b/src/key_bindings/control_commands.c index 9a6095d..3c23b4a 100644 --- a/src/key_bindings/control_commands.c +++ b/src/key_bindings/control_commands.c @@ -5,9 +5,48 @@ ** control_commands */ +#include +#include +#include #include "shell.h" +#include "utility.h" + +int get_max_eof(char *ignoreeof) +{ + bool correct = true; + + for (int i = 0; ignoreeof[i] && correct; i++) + if(!is_num(ignoreeof[i])) + correct = false; + if (correct && strcmp(ignoreeof, "0") && strcmp(ignoreeof, "")) + return(strtol(ignoreeof, NULL, 10)); + return (26); +} + +int skip_eof(buffer_t *buffer, env_t *env) +{ + my_addstr(env->window, "\n"); + prompt_prepare(buffer, env); + return (0); +} int eof_command(int key, buffer_t *buffer, env_t *env) { - return (-1); + static unsigned count = 0; + unsigned max = 26; + char *ignoreeof = my_getenv(env->vars, "ignoreeof"); + + if (env->window && buffer->buffer && *buffer->buffer) + return (skip_eof(buffer, env)); + if (!ignoreeof || !env->window) + return (-1); + max = get_max_eof(ignoreeof); + count++; + if (count >= max) + return (-1); + my_addstr(env->window, "\n"); + clearerr(stdin); + fprintf(stdout, "Use \"exit\" to leave %s.\n", SHELL_NAME); + prompt_prepare(buffer, env); + return (0); } \ No newline at end of file From 04ae8429a8f443322dab1084828bf169914cbb16 Mon Sep 17 00:00:00 2001 From: Melefo <42809472+Melefo@users.noreply.github.com> Date: Fri, 22 May 2020 11:55:56 +0200 Subject: [PATCH 2/4] count only consecutive ^D --- src/key_bindings/basic_typing_functions.c | 1 + src/key_bindings/control_commands.c | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/key_bindings/basic_typing_functions.c b/src/key_bindings/basic_typing_functions.c index 21e6f89..0616747 100644 --- a/src/key_bindings/basic_typing_functions.c +++ b/src/key_bindings/basic_typing_functions.c @@ -62,6 +62,7 @@ int newline_command(int key, buffer_t *buffer, env_t *env) if (!buffer->buffer) return (0); + env->vars = my_unsetenv(env->vars, "eof"); add_to_history(buffer->buffer, env); if (env->window) my_addstr(env->window, "\n"); diff --git a/src/key_bindings/control_commands.c b/src/key_bindings/control_commands.c index 3c23b4a..21a94a3 100644 --- a/src/key_bindings/control_commands.c +++ b/src/key_bindings/control_commands.c @@ -32,16 +32,19 @@ int skip_eof(buffer_t *buffer, env_t *env) int eof_command(int key, buffer_t *buffer, env_t *env) { - static unsigned count = 0; - unsigned max = 26; char *ignoreeof = my_getenv(env->vars, "ignoreeof"); + char *eof = my_getenv(env->vars, "eof"); + char new[20]; + unsigned count = eof ? strtol(eof, NULL, 10) : 1; + unsigned max = 26; if (env->window && buffer->buffer && *buffer->buffer) return (skip_eof(buffer, env)); if (!ignoreeof || !env->window) return (-1); max = get_max_eof(ignoreeof); - count++; + sprintf(new, "%u", ++count); + env->vars = my_setenv(env->vars, "eof", new); if (count >= max) return (-1); my_addstr(env->window, "\n"); From 31889c7175202e1257a33ddc050806403e8f4eeb Mon Sep 17 00:00:00 2001 From: Melefo <42809472+Melefo@users.noreply.github.com> Date: Fri, 22 May 2020 11:59:47 +0200 Subject: [PATCH 3/4] check if env exists in unsetenv --- src/env.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/env.c b/src/env.c index fb92ea4..66d618e 100644 --- a/src/env.c +++ b/src/env.c @@ -73,7 +73,7 @@ char **my_unsetenv(char **env, char *name) { int max = env_get_length(env); - for (int i = 0; env[i]; i++) { + for (int i = 0; env && env[i]; i++) { if (same_var(env[i], name) == env[i]) { free(env[i]); env[i] = env[max - 1]; From bced1ff87937a8d74b84c0a5d89314006f5a9408 Mon Sep 17 00:00:00 2001 From: Melefo <42809472+Melefo@users.noreply.github.com> Date: Fri, 22 May 2020 23:03:50 +0200 Subject: [PATCH 4/4] Use of my_addstr instead of fprintf --- src/key_bindings/control_commands.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/key_bindings/control_commands.c b/src/key_bindings/control_commands.c index 95696b5..4e2b16e 100644 --- a/src/key_bindings/control_commands.c +++ b/src/key_bindings/control_commands.c @@ -28,9 +28,8 @@ int eof_command(int key, buffer_t *buffer, env_t *env) env->vars = my_setenv(env->vars, "eof", new); if (count >= max) return (-1); - my_addstr(env->window, "\n"); + my_addstr(env->window, "\nUse \"exit\" to leave " SHELL_NAME ".\n"); clearerr(stdin); - fprintf(stdout, "Use \"exit\" to leave %s.\n", SHELL_NAME); prompt_prepare(buffer, env); return (0); }