From 5932e7ca612ac243ff7987a32aae0d16c64d6560 Mon Sep 17 00:00:00 2001 From: Anonymus Raccoon Date: Sun, 24 May 2020 18:06:24 +0200 Subject: [PATCH 1/6] Handling the SIGINT --- Makefile | 3 ++- include/key_functions.h | 4 ++- include/my_ncurses.h | 4 ++- include/shell.h | 1 + src/execute.c | 2 +- src/key_bindings/default_bindings.c | 2 ++ src/key_bindings/signals.c | 42 +++++++++++++++++++++++++++++ src/shell.c | 1 + tests/loop.c | 5 ++++ 9 files changed, 60 insertions(+), 4 deletions(-) create mode 100644 src/key_bindings/signals.c create mode 100644 tests/loop.c diff --git a/Makefile b/Makefile index 577c084..34ab18a 100644 --- a/Makefile +++ b/Makefile @@ -54,7 +54,8 @@ SRC = src/shell.c \ src/key_bindings/autocompletion.c \ src/builtin/builtin_bindkey.c \ src/key_bindings/other_bindings.c \ - src/builtin/builtin_vars.c + src/builtin/builtin_vars.c \ + src/key_bindings/signals.c OBJ = $(SRC:%.c=%.o) OBJ += src/main.o diff --git a/include/key_functions.h b/include/key_functions.h index 46022fb..f4f577b 100644 --- a/include/key_functions.h +++ b/include/key_functions.h @@ -46,4 +46,6 @@ int down_history_command(int key, buffer_t *buffer, env_t *env); int quoted_insert_command(int key, buffer_t *buffer, env_t *env); int clear_screen_command(int key, buffer_t *buffer, env_t *env); -int complete_command(int key, buffer_t *buffer, env_t *env); \ No newline at end of file +int complete_command(int key, buffer_t *buffer, env_t *env); + +int tty_sigintr_command(int key, buffer_t *buffer, env_t *env); \ No newline at end of file diff --git a/include/my_ncurses.h b/include/my_ncurses.h index 8cac82c..5c6cf40 100644 --- a/include/my_ncurses.h +++ b/include/my_ncurses.h @@ -71,4 +71,6 @@ my_addstr(window, str)) #define my_clrtobot() (printf("\x1B[J")) void my_npause(my_window *window); -void my_nresume(my_window *window); \ No newline at end of file +void my_nresume(my_window *window); + +extern my_window *stdwin; \ No newline at end of file diff --git a/include/shell.h b/include/shell.h index 556cbfb..73964ee 100644 --- a/include/shell.h +++ b/include/shell.h @@ -85,5 +85,6 @@ char *get_alias(char *cmd, alias_t *alias); char *add_separator(char *cmd, int *return_values, int index); char *get_alias_command(char *cmd, alias_t *alias); +void setup_sigint(void); #define ERROR 84 \ No newline at end of file diff --git a/src/execute.c b/src/execute.c index 10c7e46..d4ef0fe 100644 --- a/src/execute.c +++ b/src/execute.c @@ -101,7 +101,7 @@ bool handle_parent_inout(redirection *inout[2], env_t *env, bool builtin) void run_cmd(char **argv, redirection *inout[2], env_t *env) { pid_t pid; - int status; + int status = 0; char *path; if (strlen(argv[0]) == 0) diff --git a/src/key_bindings/default_bindings.c b/src/key_bindings/default_bindings.c index aa86ead..0f95aca 100644 --- a/src/key_bindings/default_bindings.c +++ b/src/key_bindings/default_bindings.c @@ -25,6 +25,7 @@ const key_function_t key_functions[] = { {"complete-command", &complete_command}, {"clear-screen", &clear_screen_command}, {"quoted-insert", "ed_insert_command}, + {"tty-sigintr", &tty_sigintr_command}, {NULL, NULL} }; @@ -43,6 +44,7 @@ const binding_t emacs_bindings[] = { {'\t', &complete_command}, {CTRL('l'), &clear_screen_command}, {CTRL('v'), "ed_insert_command}, + {CTRL('c'), &tty_sigintr_command}, {0, NULL} }; diff --git a/src/key_bindings/signals.c b/src/key_bindings/signals.c new file mode 100644 index 0000000..c075280 --- /dev/null +++ b/src/key_bindings/signals.c @@ -0,0 +1,42 @@ +/* +** EPITECH PROJECT, 2020 +** ash +** File description: +** signals +*/ + +#include +#include +#include +#include "shell.h" +#include "key_functions.h" +#include "my_ncurses.h" + +void on_sigint(int sig, siginfo_t *info, void *context) +{ + my_addstr(stdwin, "\n"); +} + +void setup_sigint(void) +{ + struct sigaction sa; + + sa.sa_sigaction = &on_sigint; + sigemptyset(&sa.sa_mask); + sa.sa_flags = SA_SIGINFO; + sigaction(SIGINT, &sa, NULL); +} + +int tty_sigintr_command(int key, buffer_t *buffer, env_t *env) +{ + if (buffer->buffer) { + buffer->buffer[0] = '\0'; + buffer->pos = 0; + env->vars = my_setenv(env->vars, "?", "130"); + } + if (env->window) { + my_addstr(env->window, "\n"); + prompt_prepare(buffer, env); + } + return (0); +} \ No newline at end of file diff --git a/src/shell.c b/src/shell.c index dc0f330..f75f34c 100644 --- a/src/shell.c +++ b/src/shell.c @@ -74,6 +74,7 @@ void start_shell(env_t *env) buffer_t buffer = {NULL, 0, 0, 0, 0, NULL, false}; int key; + setup_sigint(); if (isatty(0)) { env->window = my_initwin(); prompt_prepare(&buffer, env); diff --git a/tests/loop.c b/tests/loop.c new file mode 100644 index 0000000..0d30f42 --- /dev/null +++ b/tests/loop.c @@ -0,0 +1,5 @@ +int main() +{ + while (1); + return (0); +} From c93fef145a5c8ffe2ec34c0ca5b3da05c03892bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Sun, 24 May 2020 18:18:59 +0200 Subject: [PATCH 2/6] exit takes care about the argument --- src/builtin/builtin_manager.c | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/builtin/builtin_manager.c b/src/builtin/builtin_manager.c index 801284d..461bb61 100644 --- a/src/builtin/builtin_manager.c +++ b/src/builtin/builtin_manager.c @@ -8,11 +8,12 @@ #include "shell.h" #include "builtin.h" #include "redirections.h" - #include #include #include #include +#include +#include int run_builtin(const builtin *cmd, char **a, redirection *inout[2], env_t *env) { @@ -63,9 +64,31 @@ int builtin_cd(char **argv, env_t *env) return (0); } +bool my_strisnum(char *str) +{ + for (int i = 0; str[i]; i++) { + if (!isdigit(str[i])) + return (false); + } + return (true); +} + int builtin_exit(char **argv, env_t *env) { - if (argv[1]) - env->vars = my_setenv(env->vars, "?", "1"); + int ret = 0; + char *ptr = argv[1]; + + if (!ptr) + exit(0); + if (argv[1][0] == '-') { + ptr = &ptr[1]; + } + if (!my_strisnum(ptr)) { + dprintf(2, "exit: Expression Syntax.\n"); + return (0); + } + if (my_strisnum(ptr)) + ret = atoi(argv[1]); + exit(ret); return (-1); } From d22d22eb3be1611e1ed99842acf2b28c0b324f31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Sun, 24 May 2020 18:19:40 +0200 Subject: [PATCH 3/6] rm a trailing line --- src/builtin/builtin_manager.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/builtin/builtin_manager.c b/src/builtin/builtin_manager.c index 461bb61..91b5060 100644 --- a/src/builtin/builtin_manager.c +++ b/src/builtin/builtin_manager.c @@ -91,4 +91,4 @@ int builtin_exit(char **argv, env_t *env) ret = atoi(argv[1]); exit(ret); return (-1); -} +} \ No newline at end of file From 27a2ee18e44c062f7b9c57cc9026389d11ec647b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Sun, 24 May 2020 18:24:35 +0200 Subject: [PATCH 4/6] rm useless stuff in exit --- src/builtin/builtin_manager.c | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/builtin/builtin_manager.c b/src/builtin/builtin_manager.c index 91b5060..fbeae70 100644 --- a/src/builtin/builtin_manager.c +++ b/src/builtin/builtin_manager.c @@ -14,6 +14,7 @@ #include #include #include +#include int run_builtin(const builtin *cmd, char **a, redirection *inout[2], env_t *env) { @@ -75,7 +76,6 @@ bool my_strisnum(char *str) int builtin_exit(char **argv, env_t *env) { - int ret = 0; char *ptr = argv[1]; if (!ptr) @@ -87,8 +87,5 @@ int builtin_exit(char **argv, env_t *env) dprintf(2, "exit: Expression Syntax.\n"); return (0); } - if (my_strisnum(ptr)) - ret = atoi(argv[1]); - exit(ret); - return (-1); + exit(atoi(argv[1])); } \ No newline at end of file From 143f25cd9d24e5f0b2aa10f5c85a1477824b0ad9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Le=20Bihan?= Date: Sun, 24 May 2020 18:33:44 +0200 Subject: [PATCH 5/6] using internal var to set the exit code --- src/builtin/builtin_manager.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/builtin/builtin_manager.c b/src/builtin/builtin_manager.c index fbeae70..7ec053f 100644 --- a/src/builtin/builtin_manager.c +++ b/src/builtin/builtin_manager.c @@ -78,8 +78,10 @@ int builtin_exit(char **argv, env_t *env) { char *ptr = argv[1]; - if (!ptr) - exit(0); + if (!ptr) { + env->vars = my_setenv(env->vars, "?", "0"); + return (-1); + } if (argv[1][0] == '-') { ptr = &ptr[1]; } @@ -87,5 +89,6 @@ int builtin_exit(char **argv, env_t *env) dprintf(2, "exit: Expression Syntax.\n"); return (0); } - exit(atoi(argv[1])); + env->vars = my_setenv(env->vars, "?", argv[1]); + return (-1); } \ No newline at end of file From 75e860158bbe6be2f1c906a63bfc98057587ad61 Mon Sep 17 00:00:00 2001 From: Anonymus Raccoon Date: Sun, 24 May 2020 18:53:21 +0200 Subject: [PATCH 6/6] Solving a bug with the history --- include/my_ncurses.h | 1 + src/key_bindings/control_commands.c | 26 ++++++++++++++++---------- src/key_bindings/other_bindings.c | 8 ++++++++ 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/include/my_ncurses.h b/include/my_ncurses.h index 5c6cf40..daca394 100644 --- a/include/my_ncurses.h +++ b/include/my_ncurses.h @@ -21,6 +21,7 @@ #define KEY_DC CSI(0x7e33) #include +#include typedef struct { diff --git a/src/key_bindings/control_commands.c b/src/key_bindings/control_commands.c index 5237d57..e7e3eb6 100644 --- a/src/key_bindings/control_commands.c +++ b/src/key_bindings/control_commands.c @@ -34,18 +34,32 @@ int eof_command(int key, buffer_t *buffer, env_t *env) return (0); } +int history_size(history_t *hist) +{ + int i = 1; + + if (!hist) + return (0); + while (hist->next) { + hist = hist->next; + i++; + } + return (i); +} + bool set_buffer_to_history(buffer_t *buffer, env_t *env) { history_t *hist = env->history; char *cmd = NULL; int len; + int hist_index = history_size(env->history) - buffer->history_index; if (buffer->history_index == 0) cmd = buffer->saved_buffer != NULL ? buffer->saved_buffer : ""; else - for (int i = 1; i < buffer->history_index && hist; i++) + for (int i = 0; i < hist_index && hist; i++) hist = hist->next; - if (!cmd && (!hist || !(cmd = hist->command))) + if (hist_index < 0 || (!cmd && (!hist || !(cmd = hist->command)))) return (false); if (!buffer->buffer || buffer->size < (len = strlen(cmd))) { buffer->buffer = realloc(buffer->buffer, buffer->size + len); @@ -75,12 +89,4 @@ int down_history_command(int key, buffer_t *buffer, env_t *env) buffer->history_index--; set_buffer_to_history(buffer, env); return (0); -} - -int clear_screen_command(int key, buffer_t *buffer, env_t *env) -{ - my_move(env->window, 0, 0); - my_clrtobot(); - prompt_prepare(buffer, env); - return (0); } \ No newline at end of file diff --git a/src/key_bindings/other_bindings.c b/src/key_bindings/other_bindings.c index 622f66c..112e994 100644 --- a/src/key_bindings/other_bindings.c +++ b/src/key_bindings/other_bindings.c @@ -13,4 +13,12 @@ int quoted_insert_command(int key, buffer_t *buffer, env_t *env) { buffer->quoted_insert = true; return (0); +} + +int clear_screen_command(int key, buffer_t *buffer, env_t *env) +{ + my_move(env->window, 0, 0); + my_clrtobot(); + prompt_prepare(buffer, env); + return (0); } \ No newline at end of file