From dfb2543378ce28ade86b27cdae0308f07ea170e2 Mon Sep 17 00:00:00 2001 From: Arthur-gtr Date: Thu, 17 Apr 2025 10:55:34 +0200 Subject: [PATCH 1/6] [ADD] free in shell loop and parse history --- src/parse_history.c | 1 + src/shell.c | 1 + 2 files changed, 2 insertions(+) diff --git a/src/parse_history.c b/src/parse_history.c index 350d9c8..16e5bae 100644 --- a/src/parse_history.c +++ b/src/parse_history.c @@ -76,6 +76,7 @@ int choose_id_or_last(his_variable_t *his_variable, int index_str, char *str) if (his_variable->str == NULL) return 3; his_variable->id = atoi(his_variable->str + 1); + free(his_variable->str); return (mode == 1) ? 3 : 4; } diff --git a/src/shell.c b/src/shell.c index 62751d3..8138cff 100644 --- a/src/shell.c +++ b/src/shell.c @@ -75,6 +75,7 @@ int shell_loop(int is_a_tty, exec_ctx_t *exec_ctx) buffer_len = update_command(&buffer, &buffer_sz, exec_ctx); if (buffer_len < 1 || !u_str_is_alnum(buffer)) { check_basic_error(buffer); + free(buffer); continue; } U_DEBUG("Buffer [%lu] [%s]\n", buffer_len, buffer); From 3e14bbb05d2f7c77734646e25618c7f1b2200ec4 Mon Sep 17 00:00:00 2001 From: Arthur-gtr Date: Thu, 17 Apr 2025 11:01:07 +0200 Subject: [PATCH 2/6] [FIX] error with the malloc due to the missing parentheses --- src/init_history.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/init_history.c b/src/init_history.c index 614db37..99b2d5b 100644 --- a/src/init_history.c +++ b/src/init_history.c @@ -15,7 +15,7 @@ static char *get_arg(char *line, int x, int end_cmd) { - char *tmp = malloc(sizeof(char) * (x - end_cmd) + 1); + char *tmp = malloc(sizeof(char) * ((x - end_cmd) + 1)); if (tmp != NULL) { tmp = strncpy(tmp, &line[end_cmd], x - end_cmd); @@ -31,7 +31,7 @@ his_command_t set_cmd(char *line, his_command_t cmd_struct) while (line[x] != '\0' && !isblank(line[x])) x++; - cmd_struct.command = malloc(sizeof(char) * x + 1); + cmd_struct.command = malloc((sizeof(char) * x + 1)); if (cmd_struct.command != NULL) { cmd_struct.command = strncpy(cmd_struct.command, line, x); cmd_struct.command[x] = '\0'; From fb7bc2a19c4b2cce4effc72110c01814c5914ecb Mon Sep 17 00:00:00 2001 From: Arthur-gtr Date: Thu, 17 Apr 2025 19:36:32 +0200 Subject: [PATCH 3/6] [ADD] divide shell loop --- src/shell.c | 41 +++++++++++++++++++++++------------------ src/update_command.c | 2 ++ 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/shell.c b/src/shell.c index 8138cff..67f6357 100644 --- a/src/shell.c +++ b/src/shell.c @@ -55,32 +55,37 @@ void write_prompt(int is_a_tty) WRITE_CONST(STDOUT_FILENO, SHELL_PROMPT); } -/* -** Noeud de fonction -** Pour changer la commande -** passer en parametre -** si besoin -*/ +static +int change_shell_command(char **buffer, exec_ctx_t *exec_ctx, size_t buffer_sz) +{ + size_t buffer_len = 0; + char *tmp_buff = NULL; + + if (getline(buffer, &buffer_sz, stdin) == -1) + return 1; + tmp_buff = (*buffer); + buffer_len = update_command(&tmp_buff, &buffer_sz, exec_ctx); + if (buffer_len < 1 || !u_str_is_alnum(tmp_buff)) { + check_basic_error(tmp_buff); + free(tmp_buff); + return 0; + } + U_DEBUG("Buffer [%lu] [%s]\n", buffer_len, buffer); + visitor(tmp_buff, exec_ctx); + free(tmp_buff); + return 0; +} + static int shell_loop(int is_a_tty, exec_ctx_t *exec_ctx) { char *buffer = NULL; size_t buffer_sz = 0; - size_t buffer_len = 0; while (true) { write_prompt(is_a_tty); - if (getline(&buffer, &buffer_sz, stdin) == -1) - break; - buffer_len = update_command(&buffer, &buffer_sz, exec_ctx); - if (buffer_len < 1 || !u_str_is_alnum(buffer)) { - check_basic_error(buffer); - free(buffer); - continue; - } - U_DEBUG("Buffer [%lu] [%s]\n", buffer_len, buffer); - visitor(buffer, exec_ctx); - free(buffer); + if (change_shell_command(&buffer, exec_ctx, buffer_sz) == 1) + return exec_ctx->history->last_exit_code; } free(exec_ctx->history_command); return (free(buffer), exec_ctx->history->last_exit_code); diff --git a/src/update_command.c b/src/update_command.c index 0f0ca4c..7dbd333 100644 --- a/src/update_command.c +++ b/src/update_command.c @@ -38,6 +38,8 @@ size_t update_command(char **buffer, size_t buffer_len = 0; buffer_len = u_strlen(*buffer); + if (buffer_len < 2) + return 1; (*buffer)[buffer_len - 1] = '\0'; if (parse_history(buffer, &buffer_len, buffer_sz, &exec_ctx->history_command) == 84) From 67c07cd204b85ead2713bf932d03d3349f246a1c Mon Sep 17 00:00:00 2001 From: Arthur-gtr Date: Thu, 17 Apr 2025 19:49:03 +0200 Subject: [PATCH 4/6] [FIX] int by bool --- src/shell.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/shell.c b/src/shell.c index 67f6357..b06723d 100644 --- a/src/shell.c +++ b/src/shell.c @@ -56,24 +56,24 @@ void write_prompt(int is_a_tty) } static -int change_shell_command(char **buffer, exec_ctx_t *exec_ctx, size_t buffer_sz) +bool change_shell_command(char **buffer, exec_ctx_t *exec_ctx, size_t buffer_sz) { size_t buffer_len = 0; char *tmp_buff = NULL; if (getline(buffer, &buffer_sz, stdin) == -1) - return 1; + return false; tmp_buff = (*buffer); buffer_len = update_command(&tmp_buff, &buffer_sz, exec_ctx); if (buffer_len < 1 || !u_str_is_alnum(tmp_buff)) { check_basic_error(tmp_buff); free(tmp_buff); - return 0; + return true; } U_DEBUG("Buffer [%lu] [%s]\n", buffer_len, buffer); visitor(tmp_buff, exec_ctx); free(tmp_buff); - return 0; + return false; } static @@ -84,7 +84,7 @@ int shell_loop(int is_a_tty, exec_ctx_t *exec_ctx) while (true) { write_prompt(is_a_tty); - if (change_shell_command(&buffer, exec_ctx, buffer_sz) == 1) + if (change_shell_command(&buffer, exec_ctx, buffer_sz) == falsel) return exec_ctx->history->last_exit_code; } free(exec_ctx->history_command); From 9745cad74a4190887f96ba8d95b7ed01166bca4f Mon Sep 17 00:00:00 2001 From: Arthur-gtr Date: Thu, 17 Apr 2025 19:52:00 +0200 Subject: [PATCH 5/6] [FIX] --- src/shell.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shell.c b/src/shell.c index b06723d..3ce1dd0 100644 --- a/src/shell.c +++ b/src/shell.c @@ -84,7 +84,7 @@ int shell_loop(int is_a_tty, exec_ctx_t *exec_ctx) while (true) { write_prompt(is_a_tty); - if (change_shell_command(&buffer, exec_ctx, buffer_sz) == falsel) + if (change_shell_command(&buffer, exec_ctx, buffer_sz) == false) return exec_ctx->history->last_exit_code; } free(exec_ctx->history_command); From 3fd9690902e242672e9e14e198bd875a33d7300d Mon Sep 17 00:00:00 2001 From: Arthur-gtr Date: Thu, 17 Apr 2025 19:59:24 +0200 Subject: [PATCH 6/6] [FIX] coding style --- src/history.h | 2 +- src/shell.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/history.h b/src/history.h index ac2eabe..9226af2 100644 --- a/src/history.h +++ b/src/history.h @@ -30,7 +30,7 @@ typedef struct history_command_s { } his_command_t; typedef struct parsing_history_s { - char *name; + char const *name; char *(*funct)(char *, his_variable_t *, his_command_t *); } parsing_history_t; diff --git a/src/shell.c b/src/shell.c index 3ce1dd0..73558e7 100644 --- a/src/shell.c +++ b/src/shell.c @@ -56,7 +56,8 @@ void write_prompt(int is_a_tty) } static -bool change_shell_command(char **buffer, exec_ctx_t *exec_ctx, size_t buffer_sz) +bool change_shell_command(char **buffer, exec_ctx_t *exec_ctx, + size_t buffer_sz) { size_t buffer_len = 0; char *tmp_buff = NULL;