From c42e06dc99fcaa70831e2984bec29d5b0586be90 Mon Sep 17 00:00:00 2001 From: Arthur-gtr Date: Wed, 9 Apr 2025 11:59:06 +0200 Subject: [PATCH 01/23] [ADD] history parsing command --- src/builtin_history.c | 103 ++++++++++++++++++++++++++++++++++++++++++ src/history.h | 43 ++++++++++++++++++ src/parse_history.c | 98 ++++++++++++++++++++++++++++++++++++++++ src/shell.c | 6 ++- 4 files changed, 248 insertions(+), 2 deletions(-) create mode 100644 src/builtin_history.c create mode 100644 src/history.h create mode 100644 src/parse_history.c diff --git a/src/builtin_history.c b/src/builtin_history.c new file mode 100644 index 0000000..00d9465 --- /dev/null +++ b/src/builtin_history.c @@ -0,0 +1,103 @@ +/* +** EPITECH PROJECT, 2025 +** history_42sh +** File description: +** builtin_history +*/ + +#include +#include + +#include "history.h" + +/* +**Il faut deux \0 parce que dans le gettokeniser +** y un truc qui regarde +**après le premier \0 +*/ + +/* +**cat in str prend un +** his_variable_t en +** parametre pour +** connaitre la coord +** d' ou commencer a concaténer +** mais aussi le nombre de charactère a retiré +** il vas free le buffer +*/ + +static +char *cat_in_str() +{ + return NULL; +} + +char *his_last_command(char *line, his_variable_t *his_variable, his_command_t *his_command) +{ + char *new_line = malloc(sizeof(char) * 10); + + if (new_line == NULL) + return NULL; + new_line[0] = 'l'; + new_line[1] = 's'; + new_line[2] = '\0'; + new_line[3] = '\0'; + free(line); + return new_line; +} + +char *his_last_same_command(char *line, his_variable_t *his_variable, his_command_t *his_command) +{ + char *new_line = malloc(sizeof(char) * 10); + + if (new_line == NULL) + return NULL; + new_line[0] = 'l'; + new_line[1] = 's'; + new_line[2] = '\0'; + new_line[3] = '\0'; + free(line); + return new_line; +} + +char *his_id_command(char *line, his_variable_t *his_variable, his_command_t *his_command) +{ + char *new_line = malloc(sizeof(char) * 10); + + if (new_line == NULL) + return NULL; + new_line[0] = 'l'; + new_line[1] = 's'; + new_line[2] = '\0'; + new_line[3] = '\0'; + free(line); + return new_line; +} + +char *his_last_word(char *line, his_variable_t *his_variable, his_command_t *his_command) +{ + char *new_line = malloc(sizeof(char) * 10); + + if (new_line == NULL) + return NULL; + new_line[0] = 'l'; + new_line[1] = 's'; + new_line[2] = '\0'; + new_line[3] = '\0'; + free(line); + return new_line; +} + +char *his_last_arg(char *line, his_variable_t *his_variable, his_command_t *his_command) +{ + char *new_line = malloc(sizeof(char) * 10); + + if (new_line == NULL) + return NULL; + new_line[0] = 'l'; + new_line[1] = 's'; + new_line[2] = '\0'; + new_line[3] = '\0'; + free(line); + return new_line; +} diff --git a/src/history.h b/src/history.h new file mode 100644 index 0000000..a5ab251 --- /dev/null +++ b/src/history.h @@ -0,0 +1,43 @@ +/* +** EPITECH PROJECT, 2025 +** __ +** File description: +** _ +*/ + +#ifndef HISTORY_H + #define HISTORY_H + + #include + +#define CHAR_HIST '!' +#define TWO_CHAR_CMD 3 + +typedef struct history_variable_s { + int coord_variable; + int size_variable; + int type; +} his_variable_t; + +typedef struct history_command_s { + int id; + char time[5];// []h[] + char *command; + char *arg; + int sz; +} his_command_t; + +typedef struct parsing_history_s { + char *name; + char *(*funct)(char *, his_variable_t *, his_command_t *); +} parsing_history_t; + +char *parse_history(char *line, size_t *buffer); +char *his_last_command(char *line, his_variable_t *his_variable, his_command_t *his_command); +char *his_last_same_command(char *line, his_variable_t *his_variable, his_command_t *his_command); +char *his_id_command(char *line, his_variable_t *his_variable, his_command_t *his_command); +char *his_last_word(char *line, his_variable_t *his_variable, his_command_t *his_command); +char *his_last_arg(char *line, his_variable_t *his_variable, his_command_t *his_command); +#endif /* HISTORY_H */ + + diff --git a/src/parse_history.c b/src/parse_history.c new file mode 100644 index 0000000..a80bfaf --- /dev/null +++ b/src/parse_history.c @@ -0,0 +1,98 @@ +/* +** EPITECH PROJECT, 2025 +** history_42sh +** File description: +** his for history +*/ +#include +#include +#include +#include +#include + +#include "common.h" +#include "env.h" +#include "exec.h" +#include "u_mem.h" +#include "u_str.h" +#include "history.h" + +const parsing_history_t tab_fnct[] = { + {"!!", &his_last_command},//last command + {"!$", &his_last_word},//last word command + {"!*", &his_last_arg},//last argument commmand + {"![command]", &his_last_same_command},//derniere commande + arg sur la derniere meme command dans l historique dans le cas ou il n y a qu un charactère il prend le dernier qui commence par la meme chaine + {"![number]", &his_id_command},//id command + {NULL, NULL}, +}; + +static +int cmd_history_is_in(char *line) +{ + for (int i = 0; line[i] != 0; i++) + if (line[i] == CHAR_HIST && + (line[i + 1] != ' ' && line[i + 1] != '\t')) + return 0; + return 1; +} + +static +int is_two_char_cmd(char *line, int coord_x) +{ + if (line[coord_x] != CHAR_HIST) + return -1; + coord_x++; + switch (line[coord_x]){ + case '!': + return 0; + case '$': + return 1; + case '*': + return 2; + } + return -1; +} + +static +int which_his_cmd(his_variable_t *his_variable, char *line) +{ + for (int i = 0; line[i] != '\0' ; i++){ + his_variable->type = is_two_char_cmd(line, i); + if (his_variable->type != -1){ + his_variable->coord_variable = i; + his_variable->size_variable = 2; + printf("command find; %s\n", tab_fnct[his_variable->type].name); + return 0; + } + } + return 0; +} + +static +char *replace_history(char *line) +{ + his_variable_t his_variable = {0, 0, -1}; + + which_his_cmd(&his_variable, line); + while(his_variable.type != -1){ + line = tab_fnct[his_variable.type].funct(line, &his_variable, NULL); + if (line == NULL) + return NULL; + which_his_cmd(&his_variable, line); + } + return line; +} + +char *parse_history(char *line, size_t *buffer_len)//Faire passer une structure avec l historique +{ + if (cmd_history_is_in(line) == 0){ + line = replace_history(line); + if (line == NULL) + return NULL; + *buffer_len = u_strlen(line) + 1; + printf("FIND ! |%s| in line\n-------------\n", line); + return line; + } + printf("PARSING HIS; %s try to find : %c\n", line, CHAR_HIST); + return line; +} \ No newline at end of file diff --git a/src/shell.c b/src/shell.c index 411b628..f086b55 100644 --- a/src/shell.c +++ b/src/shell.c @@ -17,6 +17,7 @@ #include "env.h" #include "shell.h" #include "u_str.h" +#include "history.h" __attribute__((unused)) static @@ -55,20 +56,21 @@ int shell_loop(env_t *env, int is_a_tty, history_t *history) while (true) { if (is_a_tty) WRITE_CONST(STDOUT_FILENO, SHELL_PROMPT); - if (getline(&buffer, &buffer_sz, stdin) == -1) + if (getline(&buffer, &buffer_sz, stdin) == -1)//passer la ligne 59 a 63 dans une fonction break; buffer_len = u_strlen(buffer); + buffer = parse_history(buffer, &buffer_len); if (buffer_len < 2 || !u_str_is_alnum(buffer)) { check_basic_error(buffer); continue; } + /*SAVE COMMAND pour evité le cas !4 !3*/ buffer[buffer_len - 1] = '\0'; U_DEBUG("Buffer [%lu] [%s]\n", buffer_len, buffer); visitor(buffer, env, history); } return (free(buffer), history->last_exit_code); } - int shell(char **env_ptr) { env_t env = parse_env(env_ptr); From e486212e3466160376c194c53e66884642e424af Mon Sep 17 00:00:00 2001 From: Arthur-gtr Date: Thu, 10 Apr 2025 14:09:55 +0200 Subject: [PATCH 02/23] [FIX] parsing history --- Makefile | 2 +- src/builtin_history.c | 48 +++++++++++++++++++++++++++++++++++++++++-- src/history.h | 4 +--- src/parse_history.c | 16 +++++++++------ src/shell.c | 6 +++--- 5 files changed, 61 insertions(+), 15 deletions(-) diff --git a/Makefile b/Makefile index 22fcba6..31bd6fb 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,7 @@ BUILD_DIR := .build CC := gcc CFLAGS += -Wall -Wextra -Werror=write-strings -iquote ulib -iquote src -CFLAGS += -Wno-unused-parameter -Wunused-result -fanalyzer +CFLAGS += -Wno-unused-parameter -Wunused-result -fanalyzer -g CFLAGS += -Wp,-U_FORTIFY_SOURCE -Wcast-qual -Wduplicated-branches CFLAGS += -Wduplicated-cond -Wformat=2 -Wshadow -fno-builtin CFLAGS += -Wstrict-aliasing=0 -Wstrict-prototypes -Wunreachable-code diff --git a/src/builtin_history.c b/src/builtin_history.c index 00d9465..64ff6c3 100644 --- a/src/builtin_history.c +++ b/src/builtin_history.c @@ -26,15 +26,57 @@ ** il vas free le buffer */ +#include + static -char *cat_in_str() +char *cat_in_str(his_variable_t *his_variable, char *str, char *cpy) { + int i = 0; + int j = 0; + int x = his_variable->coord_variable; + int len_str = strlen(str) - 1;//pour l \n + int size_right = len_str - x - his_variable->size_variable; + int size_left = (len_str - size_right) - his_variable->size_variable; + char *new_str = malloc(sizeof(char) * (size_right + size_left + strlen(cpy) + 2));//ajout de deux \0 + //pas besoin de midlle car cpy existe + + printf("Size right: %d\nSize left: %d\nSize cpy: %d\nSize \\0: 2\n", size_right , size_left , strlen(cpy)); + if (new_str == NULL) + return NULL; + new_str[0] = 0; + for (; i < size_left; i++){ + printf("left\n"); + new_str[i] = str[i]; + } + //printf("first loop; %s\n", new_str); + /* + **boucle for pour concatener cpy dans str + */ + for (; cpy[j] != 0; j++){ + + new_str[i] = cpy[j]; + i++; + } + printf("J-0; %d\n", j); + //j+= his_variable->size_variable; + printf("J-1; %d\n", j); + for (int k = 0; k < size_right; k++){ + printf("right\n"); + new_str[i] = str[k + size_left + his_variable->size_variable]; + i++; + j++; + } + new_str[i] = '\0'; + new_str[i + 1] = '\0'; + printf("New str: %s\n", new_str); + free(new_str); return NULL; } char *his_last_command(char *line, his_variable_t *his_variable, his_command_t *his_command) { - char *new_line = malloc(sizeof(char) * 10); + char *new_line = malloc(sizeof(char) * 4); + //char *new_str = NULL; if (new_line == NULL) return NULL; @@ -42,6 +84,8 @@ char *his_last_command(char *line, his_variable_t *his_variable, his_command_t * new_line[1] = 's'; new_line[2] = '\0'; new_line[3] = '\0'; + //cat_in_str(his_variable, line, new_line); + //printf("Adresse; %p && str; %s\n", new_line, new_line); free(line); return new_line; } diff --git a/src/history.h b/src/history.h index a5ab251..3656f35 100644 --- a/src/history.h +++ b/src/history.h @@ -32,12 +32,10 @@ typedef struct parsing_history_s { char *(*funct)(char *, his_variable_t *, his_command_t *); } parsing_history_t; -char *parse_history(char *line, size_t *buffer); +int parse_history(char **pointer_line, size_t *buffer_len, size_t *buffer_sz); char *his_last_command(char *line, his_variable_t *his_variable, his_command_t *his_command); char *his_last_same_command(char *line, his_variable_t *his_variable, his_command_t *his_command); char *his_id_command(char *line, his_variable_t *his_variable, his_command_t *his_command); char *his_last_word(char *line, his_variable_t *his_variable, his_command_t *his_command); char *his_last_arg(char *line, his_variable_t *his_variable, his_command_t *his_command); #endif /* HISTORY_H */ - - diff --git a/src/parse_history.c b/src/parse_history.c index a80bfaf..96bd759 100644 --- a/src/parse_history.c +++ b/src/parse_history.c @@ -54,7 +54,7 @@ int is_two_char_cmd(char *line, int coord_x) } static -int which_his_cmd(his_variable_t *his_variable, char *line) +int which_his_cmd(his_variable_t *his_variable, char const *line) { for (int i = 0; line[i] != '\0' ; i++){ his_variable->type = is_two_char_cmd(line, i); @@ -83,16 +83,20 @@ char *replace_history(char *line) return line; } -char *parse_history(char *line, size_t *buffer_len)//Faire passer une structure avec l historique +int parse_history(char **pointer_line, size_t *buffer_len, size_t *buffer_sz)//Faire passer une structure avec l historique { + char *line = *pointer_line; + + *buffer_sz = 0; if (cmd_history_is_in(line) == 0){ line = replace_history(line); if (line == NULL) - return NULL; + return 84; *buffer_len = u_strlen(line) + 1; - printf("FIND ! |%s| in line\n-------------\n", line); - return line; + *pointer_line = line; + printf("FIND ! |%s && %p| in line\n-------------\n", line, line); + return 0; } printf("PARSING HIS; %s try to find : %c\n", line, CHAR_HIST); - return line; + return 0; } \ No newline at end of file diff --git a/src/shell.c b/src/shell.c index f086b55..772a312 100644 --- a/src/shell.c +++ b/src/shell.c @@ -50,8 +50,8 @@ static int shell_loop(env_t *env, int is_a_tty, history_t *history) { char *buffer = NULL; - size_t buffer_sz; - size_t buffer_len; + size_t buffer_sz = 0; + size_t buffer_len = 0; while (true) { if (is_a_tty) @@ -59,7 +59,7 @@ int shell_loop(env_t *env, int is_a_tty, history_t *history) if (getline(&buffer, &buffer_sz, stdin) == -1)//passer la ligne 59 a 63 dans une fonction break; buffer_len = u_strlen(buffer); - buffer = parse_history(buffer, &buffer_len); + parse_history(&buffer, &buffer_len, &buffer_sz); if (buffer_len < 2 || !u_str_is_alnum(buffer)) { check_basic_error(buffer); continue; From 66c213800f0c75e105610a7f67e77a4c01c2a042 Mon Sep 17 00:00:00 2001 From: Arthur-gtr Date: Thu, 10 Apr 2025 14:19:28 +0200 Subject: [PATCH 03/23] [FIX] coding style --- src/builtin_history.c | 35 +++++++++-------------------------- 1 file changed, 9 insertions(+), 26 deletions(-) diff --git a/src/builtin_history.c b/src/builtin_history.c index 64ff6c3..e69d240 100644 --- a/src/builtin_history.c +++ b/src/builtin_history.c @@ -32,51 +32,33 @@ static char *cat_in_str(his_variable_t *his_variable, char *str, char *cpy) { int i = 0; - int j = 0; - int x = his_variable->coord_variable; int len_str = strlen(str) - 1;//pour l \n - int size_right = len_str - x - his_variable->size_variable; + int size_right = len_str - his_variable->coord_variable - his_variable->size_variable; int size_left = (len_str - size_right) - his_variable->size_variable; char *new_str = malloc(sizeof(char) * (size_right + size_left + strlen(cpy) + 2));//ajout de deux \0 - //pas besoin de midlle car cpy existe - printf("Size right: %d\nSize left: %d\nSize cpy: %d\nSize \\0: 2\n", size_right , size_left , strlen(cpy)); if (new_str == NULL) return NULL; new_str[0] = 0; - for (; i < size_left; i++){ - printf("left\n"); + for (; i < size_left; i++) new_str[i] = str[i]; - } - //printf("first loop; %s\n", new_str); - /* - **boucle for pour concatener cpy dans str - */ - for (; cpy[j] != 0; j++){ - + for (int j = 0; cpy[j] != 0; j++){ new_str[i] = cpy[j]; i++; } - printf("J-0; %d\n", j); - //j+= his_variable->size_variable; - printf("J-1; %d\n", j); for (int k = 0; k < size_right; k++){ - printf("right\n"); new_str[i] = str[k + size_left + his_variable->size_variable]; i++; - j++; } new_str[i] = '\0'; new_str[i + 1] = '\0'; - printf("New str: %s\n", new_str); - free(new_str); - return NULL; + return new_str; } char *his_last_command(char *line, his_variable_t *his_variable, his_command_t *his_command) { char *new_line = malloc(sizeof(char) * 4); - //char *new_str = NULL; + char *new_str = NULL; if (new_line == NULL) return NULL; @@ -84,10 +66,11 @@ char *his_last_command(char *line, his_variable_t *his_variable, his_command_t * new_line[1] = 's'; new_line[2] = '\0'; new_line[3] = '\0'; - //cat_in_str(his_variable, line, new_line); - //printf("Adresse; %p && str; %s\n", new_line, new_line); + new_str = cat_in_str(his_variable, line, new_line); + printf("Adresse; %p && str; %s\n", new_line, new_line); + free(new_line); free(line); - return new_line; + return new_str; } char *his_last_same_command(char *line, his_variable_t *his_variable, his_command_t *his_command) From c9e5ee1bf2e989c26c3d0782f6f3ea17ad61fc2c Mon Sep 17 00:00:00 2001 From: Arthur-gtr Date: Fri, 11 Apr 2025 00:46:07 +0200 Subject: [PATCH 04/23] [ADD] handler a structure that contains builtins structures and shortcut some function --- src/ast.h | 5 ++- src/builtin_history.c | 80 ++++++++++++++++++++++-------------------- src/builtins.h | 4 +-- src/builtins_handler.h | 23 ++++++++++++ src/exec.c | 2 +- src/history.h | 45 ++++++++++++++---------- src/parse_history.c | 31 ++++++++-------- src/shell.c | 66 +++++++++++++++++++++++++++------- src/update_command.c | 22 ++++++++++++ src/visitor.c | 10 +++--- 10 files changed, 193 insertions(+), 95 deletions(-) create mode 100644 src/builtins_handler.h create mode 100644 src/update_command.c diff --git a/src/ast.h b/src/ast.h index 8caa200..3c16964 100644 --- a/src/ast.h +++ b/src/ast.h @@ -9,9 +9,8 @@ #define AST_H #include #include - #include "shell.h" - #include "env.h" + #include "builtins_handler.h" #define DEFAULT_AST_CAP 128 #define T_ALL 0xff @@ -96,7 +95,7 @@ extern const tokens_list_t TOKENS_LIST[]; ast_t *parse_expression(ast_ctx_t *ctx); void print_ast(ast_ctx_t *ctx, ast_t *ast, size_t depth); token_t get_next_token(ast_ctx_t *ctx); -int visitor(char *buffer, env_t *env, history_t *history); +int visitor(char *buffer, builtin_handler_t *builtin_handler); ast_t *create_node(ast_ctx_t *ctx); bool ensure_node_cap(ast_t *node); bool ensure_list_cap(ast_t *node); diff --git a/src/builtin_history.c b/src/builtin_history.c index e69d240..9f2d21e 100644 --- a/src/builtin_history.c +++ b/src/builtin_history.c @@ -11,18 +11,18 @@ #include "history.h" /* -**Il faut deux \0 parce que dans le gettokeniser +**Il faut deux \0 parce que dans le gettokeniser ** y un truc qui regarde **après le premier \0 */ /* **cat in str prend un -** his_variable_t en -** parametre pour -** connaitre la coord +** his_variable_t en +** parametre pour +** connaitre la coord ** d' ou commencer a concaténer -** mais aussi le nombre de charactère a retiré +** mais aussi le nombre de charactère a retiré ** il vas free le buffer */ @@ -32,7 +32,7 @@ static char *cat_in_str(his_variable_t *his_variable, char *str, char *cpy) { int i = 0; - int len_str = strlen(str) - 1;//pour l \n + int len_str = strlen(str); int size_right = len_str - his_variable->coord_variable - his_variable->size_variable; int size_left = (len_str - size_right) - his_variable->size_variable; char *new_str = malloc(sizeof(char) * (size_right + size_left + strlen(cpy) + 2));//ajout de deux \0 @@ -55,7 +55,8 @@ char *cat_in_str(his_variable_t *his_variable, char *str, char *cpy) return new_str; } -char *his_last_command(char *line, his_variable_t *his_variable, his_command_t *his_command) +char *his_last_command(char *line, + his_variable_t *his_variable, his_command_t *his_command) { char *new_line = malloc(sizeof(char) * 4); char *new_str = NULL; @@ -67,13 +68,13 @@ char *his_last_command(char *line, his_variable_t *his_variable, his_command_t * new_line[2] = '\0'; new_line[3] = '\0'; new_str = cat_in_str(his_variable, line, new_line); - printf("Adresse; %p && str; %s\n", new_line, new_line); free(new_line); free(line); return new_str; } -char *his_last_same_command(char *line, his_variable_t *his_variable, his_command_t *his_command) +char *his_last_same_command(char *line, + his_variable_t *his_variable, his_command_t *his_command) { char *new_line = malloc(sizeof(char) * 10); @@ -87,35 +88,38 @@ char *his_last_same_command(char *line, his_variable_t *his_variable, his_comman return new_line; } -char *his_id_command(char *line, his_variable_t *his_variable, his_command_t *his_command) -{ - char *new_line = malloc(sizeof(char) * 10); - - if (new_line == NULL) - return NULL; - new_line[0] = 'l'; - new_line[1] = 's'; - new_line[2] = '\0'; - new_line[3] = '\0'; - free(line); - return new_line; -} - -char *his_last_word(char *line, his_variable_t *his_variable, his_command_t *his_command) -{ - char *new_line = malloc(sizeof(char) * 10); - - if (new_line == NULL) - return NULL; - new_line[0] = 'l'; - new_line[1] = 's'; - new_line[2] = '\0'; - new_line[3] = '\0'; - free(line); - return new_line; -} - -char *his_last_arg(char *line, his_variable_t *his_variable, his_command_t *his_command) +char *his_id_command(char *line, + his_variable_t *his_variable, his_command_t *his_command) +{ + char *new_line = malloc(sizeof(char) * 10); + + if (new_line == NULL) + return NULL; + new_line[0] = 'l'; + new_line[1] = 's'; + new_line[2] = '\0'; + new_line[3] = '\0'; + free(line); + return new_line; +} + +char *his_last_word(char *line, + his_variable_t *his_variable, his_command_t *his_command) +{ + char *new_line = malloc(sizeof(char) * 10); + + if (new_line == NULL) + return NULL; + new_line[0] = 'l'; + new_line[1] = 's'; + new_line[2] = '\0'; + new_line[3] = '\0'; + free(line); + return new_line; +} + +char *his_last_arg(char *line, + his_variable_t *his_variable, his_command_t *his_command) { char *new_line = malloc(sizeof(char) * 10); diff --git a/src/builtins.h b/src/builtins.h index 1e44fcb..fdce417 100644 --- a/src/builtins.h +++ b/src/builtins.h @@ -13,9 +13,9 @@ typedef struct { char const *name; int (*ptr)(ef_t *ef, char **args); -} builtins_t; +} builtins_funcs_t; -extern const builtins_t BUILTINS[]; +extern const builtins_funcs_t BUILTINS[]; extern const size_t BUILTINS_SZ; int builtins_exit(ef_t *ef, char **args); diff --git a/src/builtins_handler.h b/src/builtins_handler.h new file mode 100644 index 0000000..808edf5 --- /dev/null +++ b/src/builtins_handler.h @@ -0,0 +1,23 @@ +/* +** EPITECH PROJECT, 2025 +** __ +** File description: +** _ +*/ + + +#ifndef BUILTINS_HANDLER_H + #define BUILTINS_HANDLER_H + #include "env.h" + #include "history.h" + #include "shell.h" + +typedef struct { + env_t *env; + history_t *history; + his_command_t *history_command; +} builtin_handler_t; + +size_t update_command(char **buffer, + size_t *buffer_sz, builtin_handler_t *builtin_handler); +#endif /* BUILTINS_HANDLER_H */ diff --git a/src/exec.c b/src/exec.c index 716b042..a994fa4 100644 --- a/src/exec.c +++ b/src/exec.c @@ -23,7 +23,7 @@ #include "u_mem.h" #include "u_str.h" -const builtins_t BUILTINS[] = { +const builtins_funcs_t BUILTINS[] = { { "builtins", &builtins_builtins }, { "cd", &builtins_cd }, { "chdir", &builtins_cd }, diff --git a/src/history.h b/src/history.h index 3656f35..ad85953 100644 --- a/src/history.h +++ b/src/history.h @@ -7,35 +7,42 @@ #ifndef HISTORY_H #define HISTORY_H + #define CHAR_HIST '!' + #define TWO_CHAR_CMD 3 #include -#define CHAR_HIST '!' -#define TWO_CHAR_CMD 3 + typedef struct history_variable_s { - int coord_variable; - int size_variable; - int type; + int coord_variable; + int size_variable; + int type; } his_variable_t; typedef struct history_command_s { - int id; - char time[5];// []h[] - char *command; - char *arg; - int sz; + int id; + char time[5];// []h[] + char *command; + char *arg; + int sz; } his_command_t; - + typedef struct parsing_history_s { - char *name; - char *(*funct)(char *, his_variable_t *, his_command_t *); + char *name; + char *(*funct)(char *, his_variable_t *, his_command_t *); } parsing_history_t; -int parse_history(char **pointer_line, size_t *buffer_len, size_t *buffer_sz); -char *his_last_command(char *line, his_variable_t *his_variable, his_command_t *his_command); -char *his_last_same_command(char *line, his_variable_t *his_variable, his_command_t *his_command); -char *his_id_command(char *line, his_variable_t *his_variable, his_command_t *his_command); -char *his_last_word(char *line, his_variable_t *his_variable, his_command_t *his_command); -char *his_last_arg(char *line, his_variable_t *his_variable, his_command_t *his_command); +int parse_history(char **pointer_line, + size_t *buffer_len, size_t *buffer_sz, his_command_t **cmd_history); +char *his_last_command(char *line, + his_variable_t *his_variable, his_command_t *his_command); +char *his_last_same_command(char *line, + his_variable_t *his_variable, his_command_t *his_command); +char *his_id_command(char *line, + his_variable_t *his_variable, his_command_t *his_command); +char *his_last_word(char *line, + his_variable_t *his_variable, his_command_t *his_command); +char *his_last_arg(char *line, + his_variable_t *his_variable, his_command_t *his_command); #endif /* HISTORY_H */ diff --git a/src/parse_history.c b/src/parse_history.c index 96bd759..a942374 100644 --- a/src/parse_history.c +++ b/src/parse_history.c @@ -18,11 +18,11 @@ #include "history.h" const parsing_history_t tab_fnct[] = { - {"!!", &his_last_command},//last command - {"!$", &his_last_word},//last word command - {"!*", &his_last_arg},//last argument commmand - {"![command]", &his_last_same_command},//derniere commande + arg sur la derniere meme command dans l historique dans le cas ou il n y a qu un charactère il prend le dernier qui commence par la meme chaine - {"![number]", &his_id_command},//id command + {"!!", &his_last_command}, //last command + {"!$", &his_last_word}, //last word command + {"!*", &his_last_arg}, //last argument commmand + {"![command]", &his_last_same_command}, //derniere commande + arg sur la derniere meme command dans l historique dans le cas ou il n y a qu un charactère il prend le dernier qui commence par la meme chaine + {"![number]", &his_id_command}, //id command {NULL, NULL}, }; @@ -30,12 +30,17 @@ static int cmd_history_is_in(char *line) { for (int i = 0; line[i] != 0; i++) - if (line[i] == CHAR_HIST && + if (line[i] == CHAR_HIST && (line[i + 1] != ' ' && line[i + 1] != '\t')) return 0; return 1; } +/* +** Pour savoir si c est +** des builtins a deux +** charactères +*/ static int is_two_char_cmd(char *line, int coord_x) { @@ -56,12 +61,11 @@ int is_two_char_cmd(char *line, int coord_x) static int which_his_cmd(his_variable_t *his_variable, char const *line) { - for (int i = 0; line[i] != '\0' ; i++){ + for (int i = 0; line[i] != '\0'; i++){ his_variable->type = is_two_char_cmd(line, i); if (his_variable->type != -1){ his_variable->coord_variable = i; his_variable->size_variable = 2; - printf("command find; %s\n", tab_fnct[his_variable->type].name); return 0; } } @@ -72,9 +76,9 @@ static char *replace_history(char *line) { his_variable_t his_variable = {0, 0, -1}; - + which_his_cmd(&his_variable, line); - while(his_variable.type != -1){ + while (his_variable.type != -1){ line = tab_fnct[his_variable.type].funct(line, &his_variable, NULL); if (line == NULL) return NULL; @@ -83,7 +87,8 @@ char *replace_history(char *line) return line; } -int parse_history(char **pointer_line, size_t *buffer_len, size_t *buffer_sz)//Faire passer une structure avec l historique +int parse_history(char **pointer_line, + size_t *buffer_len, size_t *buffer_sz, his_command_t **cmd_history)//faut la passer au autre fonction { char *line = *pointer_line; @@ -94,9 +99,7 @@ int parse_history(char **pointer_line, size_t *buffer_len, size_t *buffer_sz)//F return 84; *buffer_len = u_strlen(line) + 1; *pointer_line = line; - printf("FIND ! |%s && %p| in line\n-------------\n", line, line); return 0; } - printf("PARSING HIS; %s try to find : %c\n", line, CHAR_HIST); return 0; -} \ No newline at end of file +} diff --git a/src/shell.c b/src/shell.c index 772a312..31da9bd 100644 --- a/src/shell.c +++ b/src/shell.c @@ -18,6 +18,7 @@ #include "shell.h" #include "u_str.h" #include "history.h" +#include "exec.h" __attribute__((unused)) static @@ -47,41 +48,80 @@ void ignore_sigint(int sig __attribute__((unused))) } static -int shell_loop(env_t *env, int is_a_tty, history_t *history) +void write_prompt(int is_a_tty) +{ + if (is_a_tty) + WRITE_CONST(STDOUT_FILENO, SHELL_PROMPT); +} + +/* +** Noeud de fonction +** Pour changer la commande +** passer en parametre +** si besoin +** historique, alias ... +*/ +static +int shell_loop(int is_a_tty, builtin_handler_t *builtin_handler) { char *buffer = NULL; size_t buffer_sz = 0; size_t buffer_len = 0; while (true) { - if (is_a_tty) - WRITE_CONST(STDOUT_FILENO, SHELL_PROMPT); - if (getline(&buffer, &buffer_sz, stdin) == -1)//passer la ligne 59 a 63 dans une fonction + write_prompt(is_a_tty); + if (getline(&buffer, &buffer_sz, stdin) == -1) break; - buffer_len = u_strlen(buffer); - parse_history(&buffer, &buffer_len, &buffer_sz); - if (buffer_len < 2 || !u_str_is_alnum(buffer)) { + buffer_len = update_command(&buffer, &buffer_sz, builtin_handler); + if (buffer_len < 1 || !u_str_is_alnum(buffer)) { check_basic_error(buffer); continue; } /*SAVE COMMAND pour evité le cas !4 !3*/ - buffer[buffer_len - 1] = '\0'; U_DEBUG("Buffer [%lu] [%s]\n", buffer_len, buffer); - visitor(buffer, env, history); + visitor(buffer, builtin_handler); } - return (free(buffer), history->last_exit_code); + free(builtin_handler->history_command); + return (free(buffer), builtin_handler->history->last_exit_code); } + +his_command_t *init_cmd_history(void) +{ + his_command_t *cmd_history = malloc(sizeof(his_command_t)); + + if (cmd_history == NULL) + return NULL; + cmd_history->sz = 1; + cmd_history[0].arg = NULL; + cmd_history[0].command = NULL; + cmd_history[0].id = 0; + return cmd_history; +} + +/* +** verifier le retour du malloc et passer +** l initalisation de builtin handler dans +** une fonction pour l env l' history et les futurs builtins +*/ int shell(char **env_ptr) { env_t env = parse_env(env_ptr); - history_t history = { .cmd_history = NULL, 0, .last_chdir = NULL }; + history_t history = { .cmd_history = NULL, 0, .last_chdir = NULL}; + his_command_t *cmd_history = init_cmd_history(); + builtin_handler_t builtin_handler = {.env = &env, + .history = &history, .history_command = cmd_history}; int shell_result; - if (!env.env) + if (!cmd_history || !env.env){ + if (cmd_history) + free(cmd_history); + if (env.env) + free(env.env); return RETURN_FAILURE; + } U_DEBUG_CALL(debug_env_entries, &env); signal(SIGINT, ignore_sigint); - shell_result = shell_loop(&env, isatty(STDIN_FILENO), &history); + shell_result = shell_loop(isatty(STDIN_FILENO), &builtin_handler); free_env(&env); return shell_result; } diff --git a/src/update_command.c b/src/update_command.c new file mode 100644 index 0000000..63e0904 --- /dev/null +++ b/src/update_command.c @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2025 +** 42sh +** File description: +** update_command +*/ +#include "history.h" +#include "builtins_handler.h" +#include "u_str.h" + +size_t update_command(char **buffer, + size_t *buffer_sz, builtin_handler_t *builtin_handler) +{ + size_t buffer_len = 0; + + buffer_len = u_strlen(*buffer); + (*buffer)[buffer_len - 1] = '\0'; + /*parse_alias*/ + parse_history(buffer, &buffer_len, + buffer_sz, &builtin_handler->history_command); + return buffer_len; +} diff --git a/src/visitor.c b/src/visitor.c index a3b40e1..939655b 100644 --- a/src/visitor.c +++ b/src/visitor.c @@ -146,18 +146,18 @@ void remove_trailing_semi(char *str) } } -int visitor(char *buffer, env_t *env, history_t *history) +int visitor(char *buffer, builtin_handler_t *builtin_handler) { ast_ctx_t ctx = { 0, .str = buffer, .cap = u_strlen(buffer) + 10, .ast = malloc(sizeof *ctx.ast * (u_strlen(buffer) + 10)) }; - ef_t ef = { .buffer = buffer, .env = env, - .history = history, .ctx = &ctx, .pout_fd = STDOUT_FILENO, - .flags = 0, 0 }; + ef_t ef = { .buffer = buffer, .env = builtin_handler->env, + .history = builtin_handler->history, .ctx + = &ctx, .pout_fd = STDOUT_FILENO, .flags = 0, 0 }; int result = RETURN_FAILURE; ctx.first_node = ctx.ast; remove_trailing_semi(ctx.str); - history->last_exit_code = 0; + builtin_handler->history->last_exit_code = 0; if (ctx.ast == NULL) return RETURN_FAILURE; result = visitor_launcher(&ef); From c54c6447a97f5d461bfa6f25758787bcc354178f Mon Sep 17 00:00:00 2001 From: Arthur-gtr Date: Fri, 11 Apr 2025 09:43:47 +0200 Subject: [PATCH 05/23] [ADD] parsing for history command --- src/history.h | 2 ++ src/parse_history.c | 81 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/src/history.h b/src/history.h index ad85953..a8d9225 100644 --- a/src/history.h +++ b/src/history.h @@ -18,6 +18,8 @@ typedef struct history_variable_s { int coord_variable; int size_variable; int type; + char *str; + int id; } his_variable_t; typedef struct history_command_s { diff --git a/src/parse_history.c b/src/parse_history.c index a942374..bc67f10 100644 --- a/src/parse_history.c +++ b/src/parse_history.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "common.h" #include "env.h" @@ -26,6 +27,27 @@ const parsing_history_t tab_fnct[] = { {NULL, NULL}, }; +//const size_t TOKENS_LIST_SZ = sizeof TOKENS_LIST / sizeof *TOKENS_LIST; + +int my_atoi(const char *str) +{ + int result = 0; + int sign = 1; + + while ((*str > '9' && *str < '0') || *str == '-') + str++; + if (*str == '-') { + sign = -1; + str++; + } else if (*str == '+') { + str++; + } + for (; *str >= '0' && *str <= '9'; str++) { + result = result * 10 + (*str - '0'); + } + return result * sign; +} + static int cmd_history_is_in(char *line) { @@ -58,6 +80,59 @@ int is_two_char_cmd(char *line, int coord_x) return -1; } +static +bool is_a_token(char *str, int index_str) +{ + str+=index_str; + for (size_t i = 0; i < 16; i++) { + if (strncmp(str, TOKENS_LIST[i].str, 2) == 0){ + return true; + } + } + return false; +} + +static +char *strn_to_ndup(int start, int size, char *str) +{ + char *new_str = malloc(sizeof(char) * (size + 1)); + int count = 0; + + if (new_str == NULL) + return NULL; + new_str[size] = '\0'; + for (int i = start; i != start + size; i++){ + new_str[count] = str[i]; + count++; + } + return new_str; +} + +static +int choose_id_or_last(his_variable_t *his_variable, int index_str, char *str) +{ + int mode = 0; + const int cpy_index = index_str; + char *last_or_id = NULL; + + if (str[index_str] != CHAR_HIST && is_a_token(str, index_str + 1) == false) + return -1; + index_str++; + for (;str[index_str] != 0; index_str++){ + if (is_a_token(str, index_str) == true || isblank(str[index_str])) + break; + if (!isdigit(str[index_str])) + mode = 1; + } + his_variable->coord_variable = cpy_index; + his_variable->size_variable = index_str - cpy_index; + his_variable->str = strn_to_ndup(cpy_index, (index_str - cpy_index), str); + if (his_variable->str == NULL) + return 3;//La fonction last command dois verifier le NULL + his_variable->id = my_atoi(his_variable->str + 1); + return (mode == 1) ? 3 : 4; +} + static int which_his_cmd(his_variable_t *his_variable, char const *line) { @@ -68,6 +143,9 @@ int which_his_cmd(his_variable_t *his_variable, char const *line) his_variable->size_variable = 2; return 0; } + his_variable->type = choose_id_or_last(his_variable, i, line); + if (his_variable->type != -1) + return 0; } return 0; } @@ -75,7 +153,7 @@ int which_his_cmd(his_variable_t *his_variable, char const *line) static char *replace_history(char *line) { - his_variable_t his_variable = {0, 0, -1}; + his_variable_t his_variable = {.coord_variable = 0, .id = 0, .size_variable = 0, .str = NULL, .type = -1}; which_his_cmd(&his_variable, line); while (his_variable.type != -1){ @@ -94,6 +172,7 @@ int parse_history(char **pointer_line, *buffer_sz = 0; if (cmd_history_is_in(line) == 0){ + //printf("JE REMPLACE...\n......\n....\n..\n.\n"); line = replace_history(line); if (line == NULL) return 84; From 09bdc0b61680cb98a065e8e15c63d24c90d2d6ed Mon Sep 17 00:00:00 2001 From: Arthur-gtr Date: Sat, 12 Apr 2025 19:06:22 +0200 Subject: [PATCH 06/23] [MOOVE] moove some function to the utils folder and add utils.h --- Makefile | 1 + src/builtin_history.c | 28 +--------------- src/parse_history.c | 70 +++++----------------------------------- src/utils.h | 17 ++++++++++ src/utils/cat_in_str.c | 36 +++++++++++++++++++++ src/utils/is_a_token.c | 20 ++++++++++++ src/utils/strn_to_ndup.c | 22 +++++++++++++ 7 files changed, 105 insertions(+), 89 deletions(-) create mode 100644 src/utils.h create mode 100644 src/utils/cat_in_str.c create mode 100644 src/utils/is_a_token.c create mode 100644 src/utils/strn_to_ndup.c diff --git a/Makefile b/Makefile index 31bd6fb..4e4fe1f 100644 --- a/Makefile +++ b/Makefile @@ -13,6 +13,7 @@ LIB_NAME := libu.a SRC := $(wildcard src/*.c) SRC += $(wildcard src/builtins/*.c) +SRC += $(wildcard src/utils/*.c) LIB_SRC := $(wildcard ulib/*.c) LIB_SRC += $(wildcard ulib/write/printf/*.c) diff --git a/src/builtin_history.c b/src/builtin_history.c index 9f2d21e..90b4b6e 100644 --- a/src/builtin_history.c +++ b/src/builtin_history.c @@ -27,33 +27,7 @@ */ #include - -static -char *cat_in_str(his_variable_t *his_variable, char *str, char *cpy) -{ - int i = 0; - int len_str = strlen(str); - int size_right = len_str - his_variable->coord_variable - his_variable->size_variable; - int size_left = (len_str - size_right) - his_variable->size_variable; - char *new_str = malloc(sizeof(char) * (size_right + size_left + strlen(cpy) + 2));//ajout de deux \0 - - if (new_str == NULL) - return NULL; - new_str[0] = 0; - for (; i < size_left; i++) - new_str[i] = str[i]; - for (int j = 0; cpy[j] != 0; j++){ - new_str[i] = cpy[j]; - i++; - } - for (int k = 0; k < size_right; k++){ - new_str[i] = str[k + size_left + his_variable->size_variable]; - i++; - } - new_str[i] = '\0'; - new_str[i + 1] = '\0'; - return new_str; -} +#include "utils.h" char *his_last_command(char *line, his_variable_t *his_variable, his_command_t *his_command) diff --git a/src/parse_history.c b/src/parse_history.c index bc67f10..f7f0866 100644 --- a/src/parse_history.c +++ b/src/parse_history.c @@ -11,6 +11,7 @@ #include #include +#include "utils.h" #include "common.h" #include "env.h" #include "exec.h" @@ -27,27 +28,6 @@ const parsing_history_t tab_fnct[] = { {NULL, NULL}, }; -//const size_t TOKENS_LIST_SZ = sizeof TOKENS_LIST / sizeof *TOKENS_LIST; - -int my_atoi(const char *str) -{ - int result = 0; - int sign = 1; - - while ((*str > '9' && *str < '0') || *str == '-') - str++; - if (*str == '-') { - sign = -1; - str++; - } else if (*str == '+') { - str++; - } - for (; *str >= '0' && *str <= '9'; str++) { - result = result * 10 + (*str - '0'); - } - return result * sign; -} - static int cmd_history_is_in(char *line) { @@ -58,11 +38,6 @@ int cmd_history_is_in(char *line) return 1; } -/* -** Pour savoir si c est -** des builtins a deux -** charactères -*/ static int is_two_char_cmd(char *line, int coord_x) { @@ -80,45 +55,16 @@ int is_two_char_cmd(char *line, int coord_x) return -1; } -static -bool is_a_token(char *str, int index_str) -{ - str+=index_str; - for (size_t i = 0; i < 16; i++) { - if (strncmp(str, TOKENS_LIST[i].str, 2) == 0){ - return true; - } - } - return false; -} - -static -char *strn_to_ndup(int start, int size, char *str) -{ - char *new_str = malloc(sizeof(char) * (size + 1)); - int count = 0; - - if (new_str == NULL) - return NULL; - new_str[size] = '\0'; - for (int i = start; i != start + size; i++){ - new_str[count] = str[i]; - count++; - } - return new_str; -} - static int choose_id_or_last(his_variable_t *his_variable, int index_str, char *str) { int mode = 0; - const int cpy_index = index_str; - char *last_or_id = NULL; + const int cpy_index = index_str; if (str[index_str] != CHAR_HIST && is_a_token(str, index_str + 1) == false) return -1; index_str++; - for (;str[index_str] != 0; index_str++){ + for (; str[index_str] != 0; index_str++){ if (is_a_token(str, index_str) == true || isblank(str[index_str])) break; if (!isdigit(str[index_str])) @@ -128,8 +74,8 @@ int choose_id_or_last(his_variable_t *his_variable, int index_str, char *str) his_variable->size_variable = index_str - cpy_index; his_variable->str = strn_to_ndup(cpy_index, (index_str - cpy_index), str); if (his_variable->str == NULL) - return 3;//La fonction last command dois verifier le NULL - his_variable->id = my_atoi(his_variable->str + 1); + return 3; + his_variable->id = atoi(his_variable->str + 1); return (mode == 1) ? 3 : 4; } @@ -153,7 +99,8 @@ int which_his_cmd(his_variable_t *his_variable, char const *line) static char *replace_history(char *line) { - his_variable_t his_variable = {.coord_variable = 0, .id = 0, .size_variable = 0, .str = NULL, .type = -1}; + his_variable_t his_variable = {.coord_variable = 0, + .id = 0, .size_variable = 0, .str = NULL, .type = -1}; which_his_cmd(&his_variable, line); while (his_variable.type != -1){ @@ -166,13 +113,12 @@ char *replace_history(char *line) } int parse_history(char **pointer_line, - size_t *buffer_len, size_t *buffer_sz, his_command_t **cmd_history)//faut la passer au autre fonction + size_t *buffer_len, size_t *buffer_sz, his_command_t **cmd_history) { char *line = *pointer_line; *buffer_sz = 0; if (cmd_history_is_in(line) == 0){ - //printf("JE REMPLACE...\n......\n....\n..\n.\n"); line = replace_history(line); if (line == NULL) return 84; diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..60cece0 --- /dev/null +++ b/src/utils.h @@ -0,0 +1,17 @@ +/* +** EPITECH PROJECT, 2025 +** __ +** File description: +** _ +*/ + + +#ifndef UTILS_H + #define UTILS_H + #include "history.h" + #include "u_str.h" + +char *strn_to_ndup(int start, int size, char *str); +bool is_a_token(char *str, int index_str); +char *cat_in_str(his_variable_t *his_variable, char *str, char *cpy); +#endif /* UTILS_H */ diff --git a/src/utils/cat_in_str.c b/src/utils/cat_in_str.c new file mode 100644 index 0000000..d27ef7c --- /dev/null +++ b/src/utils/cat_in_str.c @@ -0,0 +1,36 @@ +/* +** EPITECH PROJECT, 2025 +** 42sh +** File description: +** cat_in_str +*/ +#include "history.h" +#include +#include + +char *cat_in_str(his_variable_t *his_variable, char *str, char *cpy) +{ + int i = 0; + int len_str = strlen(str); + int size_right = len_str - + his_variable->coord_variable - his_variable->size_variable; + int size_left = (len_str - size_right) - his_variable->size_variable; + char *new_str = malloc(sizeof(char) * + (size_right + size_left + strlen(cpy) + 2)); + + if (new_str == NULL) + return NULL; + for (; i < size_left; i++) + new_str[i] = str[i]; + for (int j = 0; cpy[j] != 0; j++){ + new_str[i] = cpy[j]; + i++; + } + for (int k = 0; k < size_right; k++){ + new_str[i] = str[k + size_left + his_variable->size_variable]; + i++; + } + new_str[i] = '\0'; + new_str[i + 1] = '\0'; + return new_str; +} diff --git a/src/utils/is_a_token.c b/src/utils/is_a_token.c new file mode 100644 index 0000000..5ce25c3 --- /dev/null +++ b/src/utils/is_a_token.c @@ -0,0 +1,20 @@ +/* +** EPITECH PROJECT, 2025 +** 42sh +** File description: +** is_a_token +*/ +#include "string.h" +#include "u_str.h" +#include "ast.h" + +bool is_a_token(char *str, int index_str) +{ + str += index_str; + for (size_t i = 0; i < 16; i++) { + if (strncmp(str, TOKENS_LIST[i].str, 2) == 0){ + return true; + } + } + return false; +} diff --git a/src/utils/strn_to_ndup.c b/src/utils/strn_to_ndup.c new file mode 100644 index 0000000..29f2379 --- /dev/null +++ b/src/utils/strn_to_ndup.c @@ -0,0 +1,22 @@ +/* +** EPITECH PROJECT, 2025 +** 42sh +** File description: +** strn_to_ndup +*/ +#include + +char *strn_to_ndup(int start, int size, char *str) +{ + char *new_str = malloc(sizeof(char) * (size + 1)); + int count = 0; + + if (new_str == NULL) + return NULL; + new_str[size] = '\0'; + for (int i = start; i != start + size; i++){ + new_str[count] = str[i]; + count++; + } + return new_str; +} From 8b03e73a1d0f1387aab0e2693e56af597427d55b Mon Sep 17 00:00:00 2001 From: Arthur-gtr Date: Sat, 12 Apr 2025 21:23:18 +0200 Subject: [PATCH 07/23] [FIX] coding style --- src/parse_history.c | 2 +- src/utils/cat_in_str.c | 15 +++++++++++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/parse_history.c b/src/parse_history.c index f7f0866..d24ab82 100644 --- a/src/parse_history.c +++ b/src/parse_history.c @@ -23,7 +23,7 @@ const parsing_history_t tab_fnct[] = { {"!!", &his_last_command}, //last command {"!$", &his_last_word}, //last word command {"!*", &his_last_arg}, //last argument commmand - {"![command]", &his_last_same_command}, //derniere commande + arg sur la derniere meme command dans l historique dans le cas ou il n y a qu un charactère il prend le dernier qui commence par la meme chaine + {"![command]", &his_last_same_command}, //derniere commande + arg sur la derniere meme command dans l historique dans le cas ou il n y a qu un charactère il prend le dernier qui commence par la meme chaine {"![number]", &his_id_command}, //id command {NULL, NULL}, }; diff --git a/src/utils/cat_in_str.c b/src/utils/cat_in_str.c index d27ef7c..9fe5d0c 100644 --- a/src/utils/cat_in_str.c +++ b/src/utils/cat_in_str.c @@ -8,6 +8,16 @@ #include #include +static +int insert_in_str(char *dest, char *cpy, int start) +{ + for (int j = 0; cpy[j] != 0; j++){ + dest[start] = cpy[j]; + start++; + } + return start; +} + char *cat_in_str(his_variable_t *his_variable, char *str, char *cpy) { int i = 0; @@ -22,10 +32,7 @@ char *cat_in_str(his_variable_t *his_variable, char *str, char *cpy) return NULL; for (; i < size_left; i++) new_str[i] = str[i]; - for (int j = 0; cpy[j] != 0; j++){ - new_str[i] = cpy[j]; - i++; - } + i += insert_in_str(new_str, cpy, i); for (int k = 0; k < size_right; k++){ new_str[i] = str[k + size_left + his_variable->size_variable]; i++; From e2ddc952d14684f8f1493b1fb67f290ea4e6c31e Mon Sep 17 00:00:00 2001 From: Arthur-gtr Date: Sat, 12 Apr 2025 22:12:41 +0200 Subject: [PATCH 08/23] [UP] add free for the cmd line --- src/builtins/cd.c | 8 ++++---- src/builtins/exit.c | 2 +- src/exec.c | 9 +++++---- src/exec.h | 1 + src/parse_history.c | 2 ++ src/shell.c | 1 + src/visitor.c | 3 ++- 7 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/builtins/cd.c b/src/builtins/cd.c index d5e0b14..1f2fded 100644 --- a/src/builtins/cd.c +++ b/src/builtins/cd.c @@ -62,17 +62,17 @@ int builtins_cd_chdir(ef_t *ef, char **args, char *path) { char *act_pwd; - if (ef->history->last_chdir != NULL && args[1] != NULL + if (ef->builtin_handler->history->last_chdir != NULL && args[1] != NULL && u_strcmp(args[1], "-") == 0) - path = ef->history->last_chdir; + path = ef->builtin_handler->history->last_chdir; act_pwd = get_current_dir(); if (chdir(path) < 0) { write(STDERR_FILENO, path, u_strlen(path)); cd_print_error(); return RETURN_FAILURE; } - free(ef->history->last_chdir); - ef->history->last_chdir = act_pwd; + free(ef->builtin_handler->history->last_chdir); + ef->builtin_handler->history->last_chdir = act_pwd; return RETURN_SUCCESS; } diff --git a/src/builtins/exit.c b/src/builtins/exit.c index b7bd6b2..d18313a 100644 --- a/src/builtins/exit.c +++ b/src/builtins/exit.c @@ -23,5 +23,5 @@ int builtins_exit(ef_t *ef, char **args __attribute__((unused))) } free_env(ef->env); free(ef->buffer); - exit(ef->history->last_exit_code); + exit(ef->builtin_handler->history->last_exit_code); } diff --git a/src/exec.c b/src/exec.c index a994fa4..d82e109 100644 --- a/src/exec.c +++ b/src/exec.c @@ -137,8 +137,8 @@ int launch_bin(char *full_bin_path, char **args, ef_t *ef) else waitpid(pid, &status, WNOHANG); if (WIFEXITED(status)) - ef->history->last_exit_code = - ef->history->last_exit_code ?: WEXITSTATUS(status); + ef->builtin_handler->history->last_exit_code = + ef->builtin_handler->history->last_exit_code ?: WEXITSTATUS(status); return status; } @@ -171,7 +171,7 @@ bool builtins_launcher(ef_t *ef, char **args) if (u_strlen(BUILTINS[i].name) != bin_l) continue; if (u_strcmp(BUILTINS[i].name, args[0]) == 0) { - ef->history->last_exit_code = + ef->builtin_handler->history->last_exit_code = BUILTINS[i].ptr(ef, args); return true; } @@ -199,5 +199,6 @@ int execute(ef_t *ef) U_DEBUG("Exit code [%d]\n", ef->history->last_exit_code); free(full_bin_path); free((void *)args); - return ef->history->last_exit_code != 0 ? RETURN_FAILURE : RETURN_SUCCESS; + return ef->builtin_handler->history->last_exit_code + != 0 ? RETURN_FAILURE : RETURN_SUCCESS; } diff --git a/src/exec.h b/src/exec.h index 68fc9d6..a3328d7 100644 --- a/src/exec.h +++ b/src/exec.h @@ -43,6 +43,7 @@ typedef struct { int pout_fd; int in_fd; int out_fd; + builtin_handler_t *builtin_handler; } ef_t; __attribute__((nonnull)) diff --git a/src/parse_history.c b/src/parse_history.c index d24ab82..2b7d5e5 100644 --- a/src/parse_history.c +++ b/src/parse_history.c @@ -123,7 +123,9 @@ int parse_history(char **pointer_line, if (line == NULL) return 84; *buffer_len = u_strlen(line) + 1; + free(*pointer_line); *pointer_line = line; + //free(*pointer_line); return 0; } return 0; diff --git a/src/shell.c b/src/shell.c index 31da9bd..bd5de81 100644 --- a/src/shell.c +++ b/src/shell.c @@ -80,6 +80,7 @@ int shell_loop(int is_a_tty, builtin_handler_t *builtin_handler) /*SAVE COMMAND pour evité le cas !4 !3*/ U_DEBUG("Buffer [%lu] [%s]\n", buffer_len, buffer); visitor(buffer, builtin_handler); + free(buffer); } free(builtin_handler->history_command); return (free(buffer), builtin_handler->history->last_exit_code); diff --git a/src/visitor.c b/src/visitor.c index 939655b..e4c34f4 100644 --- a/src/visitor.c +++ b/src/visitor.c @@ -152,7 +152,8 @@ int visitor(char *buffer, builtin_handler_t *builtin_handler) .ast = malloc(sizeof *ctx.ast * (u_strlen(buffer) + 10)) }; ef_t ef = { .buffer = buffer, .env = builtin_handler->env, .history = builtin_handler->history, .ctx - = &ctx, .pout_fd = STDOUT_FILENO, .flags = 0, 0 }; + = &ctx, .pout_fd = STDOUT_FILENO, .flags = 0, + .builtin_handler = builtin_handler}; int result = RETURN_FAILURE; ctx.first_node = ctx.ast; From 479d4880a73c0e6b90f095f5e8cc5bc364c1bdbe Mon Sep 17 00:00:00 2001 From: Arthur-gtr Date: Sun, 13 Apr 2025 16:50:25 +0200 Subject: [PATCH 09/23] [FIX] double free --- src/exec.c | 2 +- src/parse_history.c | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/exec.c b/src/exec.c index d82e109..6f23066 100644 --- a/src/exec.c +++ b/src/exec.c @@ -138,7 +138,7 @@ int launch_bin(char *full_bin_path, char **args, ef_t *ef) waitpid(pid, &status, WNOHANG); if (WIFEXITED(status)) ef->builtin_handler->history->last_exit_code = - ef->builtin_handler->history->last_exit_code ?: WEXITSTATUS(status); + ef->builtin_handler->history->last_exit_code ?: WEXITSTATUS(status); return status; } diff --git a/src/parse_history.c b/src/parse_history.c index 2b7d5e5..d24ab82 100644 --- a/src/parse_history.c +++ b/src/parse_history.c @@ -123,9 +123,7 @@ int parse_history(char **pointer_line, if (line == NULL) return 84; *buffer_len = u_strlen(line) + 1; - free(*pointer_line); *pointer_line = line; - //free(*pointer_line); return 0; } return 0; From ac4928cf74c93a3523b2e37a93397fb1fc4048e7 Mon Sep 17 00:00:00 2001 From: Arthur-gtr Date: Mon, 14 Apr 2025 10:05:24 +0200 Subject: [PATCH 10/23] [FIX] init struct with fix size --- src/shell.c | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/shell.c b/src/shell.c index bd5de81..de5c4d5 100644 --- a/src/shell.c +++ b/src/shell.c @@ -88,14 +88,17 @@ int shell_loop(int is_a_tty, builtin_handler_t *builtin_handler) his_command_t *init_cmd_history(void) { - his_command_t *cmd_history = malloc(sizeof(his_command_t)); + his_command_t *cmd_history = malloc(sizeof(his_command_t) * 100); if (cmd_history == NULL) return NULL; - cmd_history->sz = 1; - cmd_history[0].arg = NULL; - cmd_history[0].command = NULL; - cmd_history[0].id = 0; + for (int i = 0; i != 100; i++){ + cmd_history[i].arg = NULL; + cmd_history[i].command = NULL; + cmd_history[i].id = i; + } + + cmd_history[0].command; return cmd_history; } From f4bce9b54b352901bd1c92eaa057ca23edb4c49f Mon Sep 17 00:00:00 2001 From: Titouanhct Date: Tue, 15 Apr 2025 14:51:52 +0200 Subject: [PATCH 11/23] [ADD] first version of the history with a fanalyser problem --- src/builtin_history.c | 80 +++++++++++++------ src/builtins/exit.c | 1 + src/exec.c | 2 +- src/history.h | 3 + src/init_history.c | 176 ++++++++++++++++++++++++++++++++++++++++++ src/parse_history.c | 9 ++- src/shell.c | 5 +- src/update_command.c | 13 +++- 8 files changed, 257 insertions(+), 32 deletions(-) create mode 100644 src/init_history.c diff --git a/src/builtin_history.c b/src/builtin_history.c index 90b4b6e..d71bdc5 100644 --- a/src/builtin_history.c +++ b/src/builtin_history.c @@ -29,19 +29,46 @@ #include #include "utils.h" +#include +#include + +static char *concat_cmd_arg(char *dest, char *src) +{ + int l; + int i; + char *r_value = NULL; + + if (!src) { + r_value = u_strdup(dest); + return r_value; + } else { + l = strlen(dest); + i = strlen(src); + r_value = malloc(sizeof(char)* (i + l + 2)); + if (r_value != NULL) { + strcpy(r_value, dest); + r_value[l] = ' '; + r_value[l +1] = '\0'; + strcat(r_value, src); + } + } + return r_value; +} + char *his_last_command(char *line, his_variable_t *his_variable, his_command_t *his_command) { - char *new_line = malloc(sizeof(char) * 4); + char *new_line; char *new_str = NULL; - if (new_line == NULL) - return NULL; - new_line[0] = 'l'; - new_line[1] = 's'; - new_line[2] = '\0'; - new_line[3] = '\0'; + if (his_command->sz == 0){ + printf("%d: Event not found\n", his_command->sz); + return new_str; + } + new_line = concat_cmd_arg(his_command[his_command->sz - 1].command, + his_command[his_command->sz - 1].arg); new_str = cat_in_str(his_variable, line, new_line); + printf("%s\n", new_line); free(new_line); free(line); return new_str; @@ -65,16 +92,20 @@ char *his_last_same_command(char *line, char *his_id_command(char *line, his_variable_t *his_variable, his_command_t *his_command) { - char *new_line = malloc(sizeof(char) * 10); + int id = ((int)line[1] - 48) - 1; + char *new_line; + char *new_str = NULL; - if (new_line == NULL) - return NULL; - new_line[0] = 'l'; - new_line[1] = 's'; - new_line[2] = '\0'; - new_line[3] = '\0'; + if (his_command[id].command == NULL){ + printf("%d: Event not found\n", id); + return new_str; + } + new_line = concat_cmd_arg(his_command[id].command, his_command[id].arg); + new_str = cat_in_str(his_variable, line, new_line); + printf("%s\n", new_line); + free(new_line); free(line); - return new_line; + return new_str; } char *his_last_word(char *line, @@ -95,14 +126,17 @@ char *his_last_word(char *line, char *his_last_arg(char *line, his_variable_t *his_variable, his_command_t *his_command) { - char *new_line = malloc(sizeof(char) * 10); + int id = his_command->sz - 1; + char *new_line = NULL; + char *new_str = NULL; - if (new_line == NULL) - return NULL; - new_line[0] = 'l'; - new_line[1] = 's'; - new_line[2] = '\0'; - new_line[3] = '\0'; + if (!his_command[id].arg) + new_line = u_strdup(" "); + else + new_line = u_strdup(his_command[id].arg); + new_str = cat_in_str(his_variable, line, new_line); + printf("%s\n", new_line); + free(new_line); free(line); - return new_line; + return new_str; } diff --git a/src/builtins/exit.c b/src/builtins/exit.c index d18313a..0590824 100644 --- a/src/builtins/exit.c +++ b/src/builtins/exit.c @@ -23,5 +23,6 @@ int builtins_exit(ef_t *ef, char **args __attribute__((unused))) } free_env(ef->env); free(ef->buffer); + save_cmd_history(ef->builtin_handler->history_command); exit(ef->builtin_handler->history->last_exit_code); } diff --git a/src/exec.c b/src/exec.c index 6f23066..5034f53 100644 --- a/src/exec.c +++ b/src/exec.c @@ -200,5 +200,5 @@ int execute(ef_t *ef) free(full_bin_path); free((void *)args); return ef->builtin_handler->history->last_exit_code - != 0 ? RETURN_FAILURE : RETURN_SUCCESS; + != 0 ? RETURN_FAILURE : RETURN_SUCCESS; } diff --git a/src/history.h b/src/history.h index a8d9225..b239d1c 100644 --- a/src/history.h +++ b/src/history.h @@ -47,4 +47,7 @@ char *his_last_word(char *line, his_variable_t *his_variable, his_command_t *his_command); char *his_last_arg(char *line, his_variable_t *his_variable, his_command_t *his_command); +his_command_t *fill_cmd_history(his_command_t *cmd_history); +int save_cmd_history(his_command_t *cmd_history); +his_command_t set_cmd(char *line, his_command_t cmd_struct); #endif /* HISTORY_H */ diff --git a/src/init_history.c b/src/init_history.c new file mode 100644 index 0000000..966fd25 --- /dev/null +++ b/src/init_history.c @@ -0,0 +1,176 @@ +/* +** EPITECH PROJECT, 2025 +** 42sh +** File description: +** init_history +*/ + +#include +#include +#include "stdlib.h" +#include "stdio.h" +#include "string.h" +#include "history.h" +#include "ctype.h" + +static long long file_size(char *file) +{ + int fd = 0; + char buffer[4096]; + long long size = 0; + int somme = 0; + + fd = open(file, O_RDONLY); + if (fd == -1) + return -1; + for (somme = 1; somme > 0; size += somme) + somme = read(fd, buffer, 4095); + if (somme == -1){ + close(fd); + return -1; + } + close(fd); + return size; +} + +char *my_catchar(char *file) +{ + int fd = open(file, O_RDONLY); + long long int size = file_size(file); + char *str = malloc(sizeof(char) * (size + 1)); + int err = 0; + + if (str == NULL || fd == -1) + return NULL; + err = read(fd, str, size); + if (err == -1) + return NULL; + close(fd); + str[size] = '\0'; + return str; +} + +static int check_delim(char *str, int x, char *delim, int before) +{ + for (int i = 0; delim[i] != '\0'; i++) { + if (str[x] == delim[i] && before != 1) + return 1; + } + return 0; +} + +static int get_len(char *str, char *delim) +{ + int before = 0; + int len = 1; + + if (!str) + return -1; + for (int x = 0; str[x] != '\0'; x++){ + if (check_delim(str, x, delim, before) == 1) { + len++; + before = 1; + } else + before = 0; + } + return len; +} + +char **word_array_tok(char *str, char *delim) +{ + char *tmp; + char *strToken; + char **tab = NULL; + int x = 0; + + if (!str || !delim) + return tab; + tmp = strdup(str); + tab = malloc(sizeof(char *) * (get_len(tmp, delim) + 1)); + if (tab != NULL) { + strToken = strtok(tmp, delim); + while (strToken != NULL) { + tab[x] = strdup(strToken); + x++; + strToken = strtok(NULL, delim); + } + tab[x] = NULL; + } + free(tmp); + return tab; +} + +static char *get_arg(char *line, int x, int end_cmd) +{ + char *tmp = malloc(sizeof(char) * (x - end_cmd) + 1); + + if (tmp != NULL) { + tmp = strncpy(tmp, &line[end_cmd], x - end_cmd); + tmp[x - end_cmd] = '\0'; + } + return tmp; +} + +his_command_t set_cmd(char *line, his_command_t cmd_struct) +{ + int x = 0; + int end_cmd; + + while (line[x] != '\0' && !isblank(line[x])) + x++; + 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'; + } + end_cmd = x + 1; + while (line[x] != '\0') + x++; + if (x <= end_cmd) + return cmd_struct; + cmd_struct.arg = get_arg(line, x, end_cmd); + return cmd_struct; +} + +his_command_t *fill_cmd_history(his_command_t *cmd_history) +{ + char *buffer = my_catchar(".historique"); + char **saved; + int i = 0; + + saved = word_array_tok(buffer, "\n"); + if (!saved) + return cmd_history; + for (; saved[i] != NULL || i >= 100; i++) { + cmd_history[i] = set_cmd(saved[i], cmd_history[i]); + } + cmd_history->sz = i; + free(buffer); + for (int x = 0; saved[x]; x++) + free(saved[x]); + free(saved); + return cmd_history; +} + +int save_cmd_history(his_command_t *cmd_history) +{ + int fd = open(".historique", O_CREAT | O_TRUNC | O_WRONLY, 0644); + + if (fd == -1){ + return 84; + } + printf("passed here\n"); + for (int i = 0; i < cmd_history->sz; i++){ + if (cmd_history[i].command != NULL) + write(fd, cmd_history[i].command, strlen(cmd_history[i].command)); + if (cmd_history[i].arg != NULL) { + write(fd, " ", 1); + write(fd, cmd_history[i].arg, strlen(cmd_history[i].arg)); + } + free(cmd_history[i].command); + free(cmd_history[i].arg); + write(fd, "\n", 1); + } + close(fd); + return 0; +} diff --git a/src/parse_history.c b/src/parse_history.c index d24ab82..1541b94 100644 --- a/src/parse_history.c +++ b/src/parse_history.c @@ -23,7 +23,7 @@ const parsing_history_t tab_fnct[] = { {"!!", &his_last_command}, //last command {"!$", &his_last_word}, //last word command {"!*", &his_last_arg}, //last argument commmand - {"![command]", &his_last_same_command}, //derniere commande + arg sur la derniere meme command dans l historique dans le cas ou il n y a qu un charactère il prend le dernier qui commence par la meme chaine + {"![command]", &his_last_same_command}, {"![number]", &his_id_command}, //id command {NULL, NULL}, }; @@ -97,14 +97,15 @@ int which_his_cmd(his_variable_t *his_variable, char const *line) } static -char *replace_history(char *line) +char *replace_history(char *line, his_command_t *cmd_history) { his_variable_t his_variable = {.coord_variable = 0, .id = 0, .size_variable = 0, .str = NULL, .type = -1}; which_his_cmd(&his_variable, line); while (his_variable.type != -1){ - line = tab_fnct[his_variable.type].funct(line, &his_variable, NULL); + line = tab_fnct[his_variable.type].funct(line, &his_variable, + cmd_history); if (line == NULL) return NULL; which_his_cmd(&his_variable, line); @@ -119,7 +120,7 @@ int parse_history(char **pointer_line, *buffer_sz = 0; if (cmd_history_is_in(line) == 0){ - line = replace_history(line); + line = replace_history(line, *cmd_history); if (line == NULL) return 84; *buffer_len = u_strlen(line) + 1; diff --git a/src/shell.c b/src/shell.c index de5c4d5..3b6af34 100644 --- a/src/shell.c +++ b/src/shell.c @@ -77,7 +77,6 @@ int shell_loop(int is_a_tty, builtin_handler_t *builtin_handler) check_basic_error(buffer); continue; } - /*SAVE COMMAND pour evité le cas !4 !3*/ U_DEBUG("Buffer [%lu] [%s]\n", buffer_len, buffer); visitor(buffer, builtin_handler); free(buffer); @@ -97,8 +96,8 @@ his_command_t *init_cmd_history(void) cmd_history[i].command = NULL; cmd_history[i].id = i; } - - cmd_history[0].command; + cmd_history->sz = 0; + cmd_history = fill_cmd_history(cmd_history); return cmd_history; } diff --git a/src/update_command.c b/src/update_command.c index 63e0904..4f18521 100644 --- a/src/update_command.c +++ b/src/update_command.c @@ -8,6 +8,15 @@ #include "builtins_handler.h" #include "u_str.h" +his_command_t *save_command(char *cmd, his_command_t *cmd_history) +{ + if (!cmd) + return cmd_history; + cmd_history[cmd_history->sz] = set_cmd(cmd, cmd_history[cmd_history->sz]); + cmd_history->sz++; + return cmd_history; +} + size_t update_command(char **buffer, size_t *buffer_sz, builtin_handler_t *builtin_handler) { @@ -15,8 +24,10 @@ size_t update_command(char **buffer, buffer_len = u_strlen(*buffer); (*buffer)[buffer_len - 1] = '\0'; - /*parse_alias*/ + //parse_alias parse_history(buffer, &buffer_len, buffer_sz, &builtin_handler->history_command); + builtin_handler->history_command = save_command(*buffer, + builtin_handler->history_command); return buffer_len; } From 4f26a02c8a874f8a21497fee6b7f70704b2695c0 Mon Sep 17 00:00:00 2001 From: Titouanhct Date: Tue, 15 Apr 2025 17:55:47 +0200 Subject: [PATCH 12/23] [FIX] fanalyser problems plus the rest of the basic history such as double exclamation marks --- src/{ => builtins}/builtin_history.c | 70 +++++++++++---- src/builtins/exit.c | 1 - src/init_history.c | 130 --------------------------- src/shell.c | 1 - src/update_command.c | 6 +- 5 files changed, 56 insertions(+), 152 deletions(-) rename src/{ => builtins}/builtin_history.c (63%) diff --git a/src/builtin_history.c b/src/builtins/builtin_history.c similarity index 63% rename from src/builtin_history.c rename to src/builtins/builtin_history.c index d71bdc5..3ed98f2 100644 --- a/src/builtin_history.c +++ b/src/builtins/builtin_history.c @@ -28,7 +28,7 @@ #include #include "utils.h" - +#include #include #include @@ -58,12 +58,12 @@ static char *concat_cmd_arg(char *dest, char *src) char *his_last_command(char *line, his_variable_t *his_variable, his_command_t *his_command) { - char *new_line; + char *new_line = NULL; char *new_str = NULL; if (his_command->sz == 0){ printf("%d: Event not found\n", his_command->sz); - return new_str; + return NULL; } new_line = concat_cmd_arg(his_command[his_command->sz - 1].command, his_command[his_command->sz - 1].arg); @@ -77,16 +77,25 @@ char *his_last_command(char *line, char *his_last_same_command(char *line, his_variable_t *his_variable, his_command_t *his_command) { - char *new_line = malloc(sizeof(char) * 10); + char *new_line = &line[1]; + char *new_str = NULL; - if (new_line == NULL) - return NULL; - new_line[0] = 'l'; - new_line[1] = 's'; - new_line[2] = '\0'; - new_line[3] = '\0'; + for (int i = his_command->sz - 1; i >= 0; i--) { + if (his_command[i].command == NULL) { + printf("%s: Event not found\n", new_line); + return NULL; + } + if (strncmp(his_command[i].command, new_line, strlen(new_line)) == 0) { + new_line = concat_cmd_arg(his_command[i].command, + his_command[i].arg); + new_str = cat_in_str(his_variable, line, new_line); + free(line); + return new_str; + } + } + printf("%s: Event not found\n", new_line); free(line); - return new_line; + return NULL; } char *his_id_command(char *line, @@ -108,19 +117,44 @@ char *his_id_command(char *line, return new_str; } +static char *get_last_word(char *str) +{ + char *last_word = NULL; + int last_space = 0; + int x = 0; + + if (!str) + return NULL; + while (str[x] != '\0') { + if(isblank(str[x])) + last_space = x + 1; + x++; + } + last_word = malloc(sizeof(char) * (x - last_space) + 1); + if (last_word != NULL) { + last_word = strncpy(last_word, &str[last_space], x - last_space); + last_word[x - last_space] = '\0'; + } + return last_word; +} + char *his_last_word(char *line, his_variable_t *his_variable, his_command_t *his_command) { - char *new_line = malloc(sizeof(char) * 10); + char *new_line = NULL; + char *new_str = NULL; - if (new_line == NULL) + if (his_command[his_command->sz - 1].arg == NULL){ + new_line = get_last_word(his_command[his_command->sz - 1].command); + } else + new_line = get_last_word(his_command[his_command->sz - 1].arg); + if (!new_line) return NULL; - new_line[0] = 'l'; - new_line[1] = 's'; - new_line[2] = '\0'; - new_line[3] = '\0'; + new_str = cat_in_str(his_variable, line, new_line); + printf("%s\n", new_line); + free(new_line); free(line); - return new_line; + return new_str; } char *his_last_arg(char *line, diff --git a/src/builtins/exit.c b/src/builtins/exit.c index 0590824..d18313a 100644 --- a/src/builtins/exit.c +++ b/src/builtins/exit.c @@ -23,6 +23,5 @@ int builtins_exit(ef_t *ef, char **args __attribute__((unused))) } free_env(ef->env); free(ef->buffer); - save_cmd_history(ef->builtin_handler->history_command); exit(ef->builtin_handler->history->last_exit_code); } diff --git a/src/init_history.c b/src/init_history.c index 966fd25..614db37 100644 --- a/src/init_history.c +++ b/src/init_history.c @@ -13,93 +13,6 @@ #include "history.h" #include "ctype.h" -static long long file_size(char *file) -{ - int fd = 0; - char buffer[4096]; - long long size = 0; - int somme = 0; - - fd = open(file, O_RDONLY); - if (fd == -1) - return -1; - for (somme = 1; somme > 0; size += somme) - somme = read(fd, buffer, 4095); - if (somme == -1){ - close(fd); - return -1; - } - close(fd); - return size; -} - -char *my_catchar(char *file) -{ - int fd = open(file, O_RDONLY); - long long int size = file_size(file); - char *str = malloc(sizeof(char) * (size + 1)); - int err = 0; - - if (str == NULL || fd == -1) - return NULL; - err = read(fd, str, size); - if (err == -1) - return NULL; - close(fd); - str[size] = '\0'; - return str; -} - -static int check_delim(char *str, int x, char *delim, int before) -{ - for (int i = 0; delim[i] != '\0'; i++) { - if (str[x] == delim[i] && before != 1) - return 1; - } - return 0; -} - -static int get_len(char *str, char *delim) -{ - int before = 0; - int len = 1; - - if (!str) - return -1; - for (int x = 0; str[x] != '\0'; x++){ - if (check_delim(str, x, delim, before) == 1) { - len++; - before = 1; - } else - before = 0; - } - return len; -} - -char **word_array_tok(char *str, char *delim) -{ - char *tmp; - char *strToken; - char **tab = NULL; - int x = 0; - - if (!str || !delim) - return tab; - tmp = strdup(str); - tab = malloc(sizeof(char *) * (get_len(tmp, delim) + 1)); - if (tab != NULL) { - strToken = strtok(tmp, delim); - while (strToken != NULL) { - tab[x] = strdup(strToken); - x++; - strToken = strtok(NULL, delim); - } - tab[x] = NULL; - } - free(tmp); - return tab; -} - static char *get_arg(char *line, int x, int end_cmd) { char *tmp = malloc(sizeof(char) * (x - end_cmd) + 1); @@ -131,46 +44,3 @@ his_command_t set_cmd(char *line, his_command_t cmd_struct) cmd_struct.arg = get_arg(line, x, end_cmd); return cmd_struct; } - -his_command_t *fill_cmd_history(his_command_t *cmd_history) -{ - char *buffer = my_catchar(".historique"); - char **saved; - int i = 0; - - saved = word_array_tok(buffer, "\n"); - if (!saved) - return cmd_history; - for (; saved[i] != NULL || i >= 100; i++) { - cmd_history[i] = set_cmd(saved[i], cmd_history[i]); - } - cmd_history->sz = i; - free(buffer); - for (int x = 0; saved[x]; x++) - free(saved[x]); - free(saved); - return cmd_history; -} - -int save_cmd_history(his_command_t *cmd_history) -{ - int fd = open(".historique", O_CREAT | O_TRUNC | O_WRONLY, 0644); - - if (fd == -1){ - return 84; - } - printf("passed here\n"); - for (int i = 0; i < cmd_history->sz; i++){ - if (cmd_history[i].command != NULL) - write(fd, cmd_history[i].command, strlen(cmd_history[i].command)); - if (cmd_history[i].arg != NULL) { - write(fd, " ", 1); - write(fd, cmd_history[i].arg, strlen(cmd_history[i].arg)); - } - free(cmd_history[i].command); - free(cmd_history[i].arg); - write(fd, "\n", 1); - } - close(fd); - return 0; -} diff --git a/src/shell.c b/src/shell.c index 3b6af34..b88556b 100644 --- a/src/shell.c +++ b/src/shell.c @@ -97,7 +97,6 @@ his_command_t *init_cmd_history(void) cmd_history[i].id = i; } cmd_history->sz = 0; - cmd_history = fill_cmd_history(cmd_history); return cmd_history; } diff --git a/src/update_command.c b/src/update_command.c index 4f18521..988e48e 100644 --- a/src/update_command.c +++ b/src/update_command.c @@ -12,7 +12,10 @@ his_command_t *save_command(char *cmd, his_command_t *cmd_history) { if (!cmd) return cmd_history; - cmd_history[cmd_history->sz] = set_cmd(cmd, cmd_history[cmd_history->sz]); + if (cmd_history->sz < 100) { + cmd_history[cmd_history->sz] = set_cmd(cmd, + cmd_history[cmd_history->sz]); + } cmd_history->sz++; return cmd_history; } @@ -24,7 +27,6 @@ size_t update_command(char **buffer, buffer_len = u_strlen(*buffer); (*buffer)[buffer_len - 1] = '\0'; - //parse_alias parse_history(buffer, &buffer_len, buffer_sz, &builtin_handler->history_command); builtin_handler->history_command = save_command(*buffer, From bb0463622ec767080af4b0503c80b801d68a790a Mon Sep 17 00:00:00 2001 From: Arthur-gtr Date: Tue, 15 Apr 2025 19:14:42 +0200 Subject: [PATCH 13/23] [FIX] error with one --- src/parse_history.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/parse_history.c b/src/parse_history.c index 1541b94..e23920c 100644 --- a/src/parse_history.c +++ b/src/parse_history.c @@ -33,7 +33,8 @@ int cmd_history_is_in(char *line) { for (int i = 0; line[i] != 0; i++) if (line[i] == CHAR_HIST && - (line[i + 1] != ' ' && line[i + 1] != '\t')) + (line[i + 1] != ' ' && line[i + 1] != '\t' + && line[i + 1] != '\0')) return 0; return 1; } From 23c43697e5202106990f42074c6d78d27406dcdc Mon Sep 17 00:00:00 2001 From: Titouanhct Date: Tue, 15 Apr 2025 19:18:19 +0200 Subject: [PATCH 14/23] [ADD] history builtins to display the history --- src/builtins.h | 1 + src/builtins/history.c | 24 ++++++++++++++++++++++++ src/exec.c | 3 ++- 3 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 src/builtins/history.c diff --git a/src/builtins.h b/src/builtins.h index fdce417..2ee4fb0 100644 --- a/src/builtins.h +++ b/src/builtins.h @@ -25,4 +25,5 @@ int builtins_unsetenv(ef_t *ef, char **args); int builtins_cd(ef_t *ef, char **args); int builtins_builtins(ef_t *ef, char **args); int builtins_funny_double_dot(ef_t *ef, char **args); +int builtins_history(ef_t *ef, char **args); #endif /* BUILTIND_H */ diff --git a/src/builtins/history.c b/src/builtins/history.c new file mode 100644 index 0000000..74d4cb3 --- /dev/null +++ b/src/builtins/history.c @@ -0,0 +1,24 @@ +/* +** EPITECH PROJECT, 2025 +** 42sh +** File description: +** history +*/ + +#include +#include "common.h" +#include "env.h" +#include "exec.h" + +int builtins_history(ef_t *ef, char **args __attribute__((unused))) +{ + his_command_t *cmd_history = ef->builtin_handler->history_command; + + for (int i = 0; i < cmd_history->sz; i++){ + if (cmd_history[i].arg) + printf("%d %s %s\n", i, cmd_history[i].command, cmd_history[i].arg); + else + printf("%d %s\n", i, cmd_history[i].command); + } + return RETURN_SUCCESS; +} \ No newline at end of file diff --git a/src/exec.c b/src/exec.c index 5034f53..2330f12 100644 --- a/src/exec.c +++ b/src/exec.c @@ -32,7 +32,8 @@ const builtins_funcs_t BUILTINS[] = { { "setenv", &builtins_setenv }, { "unsetenv", &builtins_unsetenv }, { ":", &builtins_funny_double_dot }, - { "exit", &builtins_exit } + { "exit", &builtins_exit }, + { "history", &builtins_history} }; const size_t BUILTINS_SZ = sizeof BUILTINS / sizeof *BUILTINS; From aa2a18ba03fdc68ca1bc8125918bc397522b1053 Mon Sep 17 00:00:00 2001 From: Titouanhct Date: Tue, 15 Apr 2025 19:21:53 +0200 Subject: [PATCH 15/23] [FIX] coding style --- src/builtins/builtin_history.c | 2 +- src/builtins/history.c | 9 +++++---- src/parse_history.c | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/builtins/builtin_history.c b/src/builtins/builtin_history.c index 3ed98f2..c13719e 100644 --- a/src/builtins/builtin_history.c +++ b/src/builtins/builtin_history.c @@ -126,7 +126,7 @@ static char *get_last_word(char *str) if (!str) return NULL; while (str[x] != '\0') { - if(isblank(str[x])) + if (isblank(str[x])) last_space = x + 1; x++; } diff --git a/src/builtins/history.c b/src/builtins/history.c index 74d4cb3..1779618 100644 --- a/src/builtins/history.c +++ b/src/builtins/history.c @@ -15,10 +15,11 @@ int builtins_history(ef_t *ef, char **args __attribute__((unused))) his_command_t *cmd_history = ef->builtin_handler->history_command; for (int i = 0; i < cmd_history->sz; i++){ - if (cmd_history[i].arg) - printf("%d %s %s\n", i, cmd_history[i].command, cmd_history[i].arg); - else + if (cmd_history[i].arg) { + printf("%d %s %s\n", i, cmd_history[i].command, + cmd_history[i].arg); + } else printf("%d %s\n", i, cmd_history[i].command); } return RETURN_SUCCESS; -} \ No newline at end of file +} diff --git a/src/parse_history.c b/src/parse_history.c index e23920c..03df93d 100644 --- a/src/parse_history.c +++ b/src/parse_history.c @@ -33,7 +33,7 @@ int cmd_history_is_in(char *line) { for (int i = 0; line[i] != 0; i++) if (line[i] == CHAR_HIST && - (line[i + 1] != ' ' && line[i + 1] != '\t' + (line[i + 1] != ' ' && line[i + 1] != '\t' && line[i + 1] != '\0')) return 0; return 1; From c620d6f8cef1dfda567105250370071863ce2177 Mon Sep 17 00:00:00 2001 From: Arthur-gtr Date: Wed, 16 Apr 2025 02:14:32 +0200 Subject: [PATCH 16/23] [ADD] first step for alias builtin add pointer function and struct --- src/alias.h | 20 ++++++++++++++++++++ src/builtins.h | 2 ++ src/builtins/builtins_alias.c | 34 ++++++++++++++++++++++++++++++++++ src/exec.c | 3 ++- 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 src/alias.h create mode 100644 src/builtins/builtins_alias.c diff --git a/src/alias.h b/src/alias.h new file mode 100644 index 0000000..0bc0aec --- /dev/null +++ b/src/alias.h @@ -0,0 +1,20 @@ +/* +** EPITECH PROJECT, 2025 +** __ +** File description: +** _ +*/ + + +#ifndef ALIAS_H + #define ALIAS_H + #include "env.h" + #include "history.h" + #include "shell.h" + +typedef struct alias_s{ + size_t size; + char **alias_array; +} alias_t; + +#endif /* ALIAS*/ diff --git a/src/builtins.h b/src/builtins.h index 2ee4fb0..d32f480 100644 --- a/src/builtins.h +++ b/src/builtins.h @@ -26,4 +26,6 @@ int builtins_cd(ef_t *ef, char **args); int builtins_builtins(ef_t *ef, char **args); int builtins_funny_double_dot(ef_t *ef, char **args); int builtins_history(ef_t *ef, char **args); +int builtins_alias(ef_t *ef, char **args); +int builtins_display_alias(ef_t *ef, char **args); #endif /* BUILTIND_H */ diff --git a/src/builtins/builtins_alias.c b/src/builtins/builtins_alias.c new file mode 100644 index 0000000..deed0f4 --- /dev/null +++ b/src/builtins/builtins_alias.c @@ -0,0 +1,34 @@ +/* +** EPITECH PROJECT, 2025 +** 42sh +** File description: +** history +*/ +#include +#include +#include "common.h" +#include "env.h" +#include "exec.h" +#include "alias.h" + +int builtins_display_alias(alias_t *alias) +{ + printf("DISPLAY ALIAS\n"); + return RETURN_SUCCESS; +} + +int builtins_alias(ef_t *ef, char **args) +{ + alias_t alias; //= ef->builtin_handler->history_command; + char *first_arg = args[1]; + + alias.size = 0; + alias.alias_array = NULL; + if (first_arg != NULL && strcmp(args[1], "--display") == 0) + return builtins_display_alias(&alias); + + printf("ARG 1: %s\n", args[1]); + printf("J ACTIVE LA SAUVEGARDE DE L ALIAS\n"); + return RETURN_SUCCESS; +} + diff --git a/src/exec.c b/src/exec.c index 2330f12..8b145fc 100644 --- a/src/exec.c +++ b/src/exec.c @@ -33,7 +33,8 @@ const builtins_funcs_t BUILTINS[] = { { "unsetenv", &builtins_unsetenv }, { ":", &builtins_funny_double_dot }, { "exit", &builtins_exit }, - { "history", &builtins_history} + { "history", &builtins_history}, + { "alias", &builtins_alias} }; const size_t BUILTINS_SZ = sizeof BUILTINS / sizeof *BUILTINS; From 0d825c9cdba4aa3999bf143b3b2feec4ef084b35 Mon Sep 17 00:00:00 2001 From: Titouanhct Date: Wed, 16 Apr 2025 14:51:51 +0200 Subject: [PATCH 17/23] [ADD] fix some issues when using it not solo for the history --- src/builtins/builtin_history.c | 4 ++-- src/history.h | 2 ++ src/shell.c | 2 ++ src/update_command.c | 19 ++++++++++++++++--- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/builtins/builtin_history.c b/src/builtins/builtin_history.c index c13719e..671638a 100644 --- a/src/builtins/builtin_history.c +++ b/src/builtins/builtin_history.c @@ -77,7 +77,7 @@ char *his_last_command(char *line, char *his_last_same_command(char *line, his_variable_t *his_variable, his_command_t *his_command) { - char *new_line = &line[1]; + char *new_line = &line[his_variable->coord_variable + 1]; char *new_str = NULL; for (int i = his_command->sz - 1; i >= 0; i--) { @@ -101,7 +101,7 @@ char *his_last_same_command(char *line, char *his_id_command(char *line, his_variable_t *his_variable, his_command_t *his_command) { - int id = ((int)line[1] - 48) - 1; + int id = atoi(&line[his_variable->coord_variable + 1]); char *new_line; char *new_str = NULL; diff --git a/src/history.h b/src/history.h index b239d1c..2c423d7 100644 --- a/src/history.h +++ b/src/history.h @@ -35,6 +35,8 @@ typedef struct parsing_history_s { char *(*funct)(char *, his_variable_t *, his_command_t *); } parsing_history_t; +extern const parsing_history_t tab_fnct[]; + int parse_history(char **pointer_line, size_t *buffer_len, size_t *buffer_sz, his_command_t **cmd_history); char *his_last_command(char *line, diff --git a/src/shell.c b/src/shell.c index b88556b..304d663 100644 --- a/src/shell.c +++ b/src/shell.c @@ -34,6 +34,8 @@ void debug_env_entries(env_t *env) static void check_basic_error(char const *buffer) { + if (buffer == NULL) + return; if (*buffer == '|') WRITE_CONST(STDERR_FILENO, "Invalid null command.\n"); if (*buffer == '>' || *buffer == '<') diff --git a/src/update_command.c b/src/update_command.c index 988e48e..dd2d83d 100644 --- a/src/update_command.c +++ b/src/update_command.c @@ -8,9 +8,21 @@ #include "builtins_handler.h" #include "u_str.h" -his_command_t *save_command(char *cmd, his_command_t *cmd_history) +static int check_cmd(char *cmd) { if (!cmd) + return 84; + for (int i = 0; cmd[i] != 0; i++) + if (cmd[i] == CHAR_HIST && + (cmd[i + 1] != ' ' && cmd[i + 1] != '\t' + && cmd[i + 1] != '\0')) + return 84; + return 0; +} + +his_command_t *save_command(char *cmd, his_command_t *cmd_history) +{ + if (check_cmd(cmd) == 84) return cmd_history; if (cmd_history->sz < 100) { cmd_history[cmd_history->sz] = set_cmd(cmd, @@ -27,8 +39,9 @@ size_t update_command(char **buffer, buffer_len = u_strlen(*buffer); (*buffer)[buffer_len - 1] = '\0'; - parse_history(buffer, &buffer_len, - buffer_sz, &builtin_handler->history_command); + if (parse_history(buffer, &buffer_len, + buffer_sz, &builtin_handler->history_command) == 84) + return -1; builtin_handler->history_command = save_command(*buffer, builtin_handler->history_command); return buffer_len; From fa76f0d715b64f5127c8424c248909a7e81801ac Mon Sep 17 00:00:00 2001 From: Arthur-gtr Date: Wed, 16 Apr 2025 14:58:28 +0200 Subject: [PATCH 18/23] [ADD] Alias builtin --- src/alias.h | 3 +++ src/builtins/builtins_alias.c | 33 +++++++++++++++++++++------ src/builtins_handler.h | 2 ++ src/shell.c | 42 +++++++++++++++++++++++++++++------ src/utils.h | 1 + src/utils/len_array.c | 17 ++++++++++++++ 6 files changed, 84 insertions(+), 14 deletions(-) create mode 100644 src/utils/len_array.c diff --git a/src/alias.h b/src/alias.h index 0bc0aec..713f0d7 100644 --- a/src/alias.h +++ b/src/alias.h @@ -15,6 +15,9 @@ typedef struct alias_s{ size_t size; char **alias_array; + char **alias_to_replace; } alias_t; +void free_alias(alias_t *alias); + #endif /* ALIAS*/ diff --git a/src/builtins/builtins_alias.c b/src/builtins/builtins_alias.c index deed0f4..09143b0 100644 --- a/src/builtins/builtins_alias.c +++ b/src/builtins/builtins_alias.c @@ -10,25 +10,44 @@ #include "env.h" #include "exec.h" #include "alias.h" +#include +#include "utils.h" +#include + +void free_alias(alias_t *alias) +{ + for (size_t i = 0; i != alias->size; i++){ + free(alias->alias_array[i]); + free(alias->alias_to_replace[i]); + } + free(alias->alias_array); + free(alias->alias_to_replace); + return ; +} int builtins_display_alias(alias_t *alias) { - printf("DISPLAY ALIAS\n"); + for (size_t i = 0; i != alias->size; i++){ + printf("|| Alias: %s || ", alias->alias_array[i]); + printf("Command: %s ||\n", alias->alias_to_replace[i]); + } return RETURN_SUCCESS; } int builtins_alias(ef_t *ef, char **args) { - alias_t alias; //= ef->builtin_handler->history_command; + alias_t *alias = ef->builtin_handler->alias; char *first_arg = args[1]; - alias.size = 0; - alias.alias_array = NULL; if (first_arg != NULL && strcmp(args[1], "--display") == 0) - return builtins_display_alias(&alias); - - printf("ARG 1: %s\n", args[1]); + return builtins_display_alias(alias); + if (len_array(args) != 3) + return RETURN_FAILURE; + alias->alias_array[alias->size - 1] = strdup(args[1]); + alias->alias_to_replace[alias->size - 1] = strdup(args[2]); + printf("size alias %d\n",len_array(args));// alias->size); printf("J ACTIVE LA SAUVEGARDE DE L ALIAS\n"); + //return RETURN_FAILURE; return RETURN_SUCCESS; } diff --git a/src/builtins_handler.h b/src/builtins_handler.h index 808edf5..acd5074 100644 --- a/src/builtins_handler.h +++ b/src/builtins_handler.h @@ -11,11 +11,13 @@ #include "env.h" #include "history.h" #include "shell.h" + #include "alias.h" typedef struct { env_t *env; history_t *history; his_command_t *history_command; + alias_t *alias; } builtin_handler_t; size_t update_command(char **buffer, diff --git a/src/shell.c b/src/shell.c index b88556b..041093b 100644 --- a/src/shell.c +++ b/src/shell.c @@ -19,6 +19,7 @@ #include "u_str.h" #include "history.h" #include "exec.h" +#include "alias.h" __attribute__((unused)) static @@ -105,25 +106,52 @@ his_command_t *init_cmd_history(void) ** l initalisation de builtin handler dans ** une fonction pour l env l' history et les futurs builtins */ +alias_t init_alias(void) +{ + alias_t alias; + + alias.size = 1; + alias.alias_array = malloc(sizeof(char *) * alias.size); + alias.alias_to_replace = malloc(sizeof(char *) * alias.size); + return alias; +} + +static +bool error_in_init(builtin_handler_t *builtin_handler) +{ + if (!builtin_handler->history_command || !builtin_handler->env->env || !builtin_handler->alias->alias_array || !builtin_handler->alias->alias_to_replace){ + if (builtin_handler->history_command) + free(builtin_handler->history_command); + if (builtin_handler->env->env) + free(builtin_handler->env->env); + if (builtin_handler->alias->alias_array) + free(builtin_handler->alias->alias_array); + if (!builtin_handler->alias->alias_to_replace) + free(builtin_handler->alias->alias_to_replace); + return true; + } + return false; +} + int shell(char **env_ptr) { + alias_t alias = init_alias(); env_t env = parse_env(env_ptr); history_t history = { .cmd_history = NULL, 0, .last_chdir = NULL}; his_command_t *cmd_history = init_cmd_history(); builtin_handler_t builtin_handler = {.env = &env, - .history = &history, .history_command = cmd_history}; + .history = &history, .history_command = cmd_history, + .alias = &alias}; int shell_result; - if (!cmd_history || !env.env){ - if (cmd_history) - free(cmd_history); - if (env.env) - free(env.env); + if (error_in_init(&builtin_handler) == true){ + free_alias(builtin_handler.alias); return RETURN_FAILURE; } U_DEBUG_CALL(debug_env_entries, &env); signal(SIGINT, ignore_sigint); shell_result = shell_loop(isatty(STDIN_FILENO), &builtin_handler); - free_env(&env); + free_env(builtin_handler.env); + free_alias(builtin_handler.alias); return shell_result; } diff --git a/src/utils.h b/src/utils.h index 60cece0..3431f06 100644 --- a/src/utils.h +++ b/src/utils.h @@ -14,4 +14,5 @@ char *strn_to_ndup(int start, int size, char *str); bool is_a_token(char *str, int index_str); char *cat_in_str(his_variable_t *his_variable, char *str, char *cpy); +int len_array(char **array); #endif /* UTILS_H */ diff --git a/src/utils/len_array.c b/src/utils/len_array.c new file mode 100644 index 0000000..5b40e62 --- /dev/null +++ b/src/utils/len_array.c @@ -0,0 +1,17 @@ +/* +** EPITECH PROJECT, 2025 +** 42sh +** File description: +** len_array +*/ + +#include + +int len_array(char **array) +{ + int i = 0; + + while(array[i] != NULL) + i++; + return i; +} \ No newline at end of file From 87373b6de4e6a0795c45d42e91c51ceabe0ee447 Mon Sep 17 00:00:00 2001 From: Titouanhct Date: Wed, 16 Apr 2025 15:13:32 +0200 Subject: [PATCH 19/23] [FIX} history problem begininning by 0 instead of 1 --- src/builtins/builtin_history.c | 6 +++--- src/builtins/history.c | 2 +- src/shell.c | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/builtins/builtin_history.c b/src/builtins/builtin_history.c index 671638a..19db479 100644 --- a/src/builtins/builtin_history.c +++ b/src/builtins/builtin_history.c @@ -80,7 +80,7 @@ char *his_last_same_command(char *line, char *new_line = &line[his_variable->coord_variable + 1]; char *new_str = NULL; - for (int i = his_command->sz - 1; i >= 0; i--) { + for (int i = his_command->sz - 1; i > 0; i--) { if (his_command[i].command == NULL) { printf("%s: Event not found\n", new_line); return NULL; @@ -111,7 +111,7 @@ char *his_id_command(char *line, } new_line = concat_cmd_arg(his_command[id].command, his_command[id].arg); new_str = cat_in_str(his_variable, line, new_line); - printf("%s\n", new_line); + printf("%s\n", new_str); free(new_line); free(line); return new_str; @@ -151,7 +151,7 @@ char *his_last_word(char *line, if (!new_line) return NULL; new_str = cat_in_str(his_variable, line, new_line); - printf("%s\n", new_line); + printf("%s\n", new_str); free(new_line); free(line); return new_str; diff --git a/src/builtins/history.c b/src/builtins/history.c index 1779618..c28f1f8 100644 --- a/src/builtins/history.c +++ b/src/builtins/history.c @@ -14,7 +14,7 @@ int builtins_history(ef_t *ef, char **args __attribute__((unused))) { his_command_t *cmd_history = ef->builtin_handler->history_command; - for (int i = 0; i < cmd_history->sz; i++){ + for (int i = 1; i < cmd_history->sz; i++){ if (cmd_history[i].arg) { printf("%d %s %s\n", i, cmd_history[i].command, cmd_history[i].arg); diff --git a/src/shell.c b/src/shell.c index 304d663..723a3f8 100644 --- a/src/shell.c +++ b/src/shell.c @@ -93,12 +93,12 @@ his_command_t *init_cmd_history(void) if (cmd_history == NULL) return NULL; - for (int i = 0; i != 100; i++){ + for (int i = 1; i != 100; i++){ cmd_history[i].arg = NULL; cmd_history[i].command = NULL; cmd_history[i].id = i; } - cmd_history->sz = 0; + cmd_history->sz = 1; return cmd_history; } From 84e5669f89cbf17d015f68b0120830139f0f9ca0 Mon Sep 17 00:00:00 2001 From: Titouanhct Date: Wed, 16 Apr 2025 17:41:42 +0200 Subject: [PATCH 20/23] [FIX] double free on some history commands --- src/builtins/builtin_history.c | 16 ++++++++-------- src/builtins/history.c | 6 +++--- src/shell.c | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/builtins/builtin_history.c b/src/builtins/builtin_history.c index 19db479..39421e0 100644 --- a/src/builtins/builtin_history.c +++ b/src/builtins/builtin_history.c @@ -83,7 +83,7 @@ char *his_last_same_command(char *line, for (int i = his_command->sz - 1; i > 0; i--) { if (his_command[i].command == NULL) { printf("%s: Event not found\n", new_line); - return NULL; + return new_str; } if (strncmp(his_command[i].command, new_line, strlen(new_line)) == 0) { new_line = concat_cmd_arg(his_command[i].command, @@ -94,19 +94,18 @@ char *his_last_same_command(char *line, } } printf("%s: Event not found\n", new_line); - free(line); - return NULL; + return new_str; } char *his_id_command(char *line, his_variable_t *his_variable, his_command_t *his_command) { - int id = atoi(&line[his_variable->coord_variable + 1]); + int id = -1 + atoi(&line[his_variable->coord_variable + 1]); char *new_line; char *new_str = NULL; - if (his_command[id].command == NULL){ - printf("%d: Event not found\n", id); + if (id < 0 || id > 100 || his_command[id].command == NULL){ + printf("%d: Event not found\n", id + 1); return new_str; } new_line = concat_cmd_arg(his_command[id].command, his_command[id].arg); @@ -165,12 +164,13 @@ char *his_last_arg(char *line, char *new_str = NULL; if (!his_command[id].arg) - new_line = u_strdup(" "); + new_line = " "; else new_line = u_strdup(his_command[id].arg); new_str = cat_in_str(his_variable, line, new_line); printf("%s\n", new_line); - free(new_line); + if (his_command[id].arg) + free(new_line); free(line); return new_str; } diff --git a/src/builtins/history.c b/src/builtins/history.c index c28f1f8..543b09d 100644 --- a/src/builtins/history.c +++ b/src/builtins/history.c @@ -14,12 +14,12 @@ int builtins_history(ef_t *ef, char **args __attribute__((unused))) { his_command_t *cmd_history = ef->builtin_handler->history_command; - for (int i = 1; i < cmd_history->sz; i++){ + for (int i = 0; i < cmd_history->sz; i++){ if (cmd_history[i].arg) { - printf("%d %s %s\n", i, cmd_history[i].command, + printf("%d %s %s\n", i + 1, cmd_history[i].command, cmd_history[i].arg); } else - printf("%d %s\n", i, cmd_history[i].command); + printf("%d %s\n", i + 1, cmd_history[i].command); } return RETURN_SUCCESS; } diff --git a/src/shell.c b/src/shell.c index 1ed7eff..26f8fe2 100644 --- a/src/shell.c +++ b/src/shell.c @@ -94,12 +94,12 @@ his_command_t *init_cmd_history(void) if (cmd_history == NULL) return NULL; - for (int i = 1; i != 100; i++){ + for (int i = 0; i != 100; i++){ cmd_history[i].arg = NULL; cmd_history[i].command = NULL; cmd_history[i].id = i; } - cmd_history->sz = 1; + cmd_history->sz = 0; return cmd_history; } From 85f34a218e198db7c5e5265a95e7202307361360 Mon Sep 17 00:00:00 2001 From: savalet Date: Wed, 16 Apr 2025 17:58:53 +0200 Subject: [PATCH 21/23] Remove ALL alias in the wrong branch --- src/alias.h | 23 --------------- src/builtins.h | 2 -- src/builtins/builtins_alias.c | 53 ----------------------------------- src/builtins_handler.h | 2 -- src/exec.c | 3 +- src/shell.c | 40 ++++---------------------- 6 files changed, 7 insertions(+), 116 deletions(-) delete mode 100644 src/alias.h delete mode 100644 src/builtins/builtins_alias.c diff --git a/src/alias.h b/src/alias.h deleted file mode 100644 index 713f0d7..0000000 --- a/src/alias.h +++ /dev/null @@ -1,23 +0,0 @@ -/* -** EPITECH PROJECT, 2025 -** __ -** File description: -** _ -*/ - - -#ifndef ALIAS_H - #define ALIAS_H - #include "env.h" - #include "history.h" - #include "shell.h" - -typedef struct alias_s{ - size_t size; - char **alias_array; - char **alias_to_replace; -} alias_t; - -void free_alias(alias_t *alias); - -#endif /* ALIAS*/ diff --git a/src/builtins.h b/src/builtins.h index d32f480..2ee4fb0 100644 --- a/src/builtins.h +++ b/src/builtins.h @@ -26,6 +26,4 @@ int builtins_cd(ef_t *ef, char **args); int builtins_builtins(ef_t *ef, char **args); int builtins_funny_double_dot(ef_t *ef, char **args); int builtins_history(ef_t *ef, char **args); -int builtins_alias(ef_t *ef, char **args); -int builtins_display_alias(ef_t *ef, char **args); #endif /* BUILTIND_H */ diff --git a/src/builtins/builtins_alias.c b/src/builtins/builtins_alias.c deleted file mode 100644 index 09143b0..0000000 --- a/src/builtins/builtins_alias.c +++ /dev/null @@ -1,53 +0,0 @@ -/* -** EPITECH PROJECT, 2025 -** 42sh -** File description: -** history -*/ -#include -#include -#include "common.h" -#include "env.h" -#include "exec.h" -#include "alias.h" -#include -#include "utils.h" -#include - -void free_alias(alias_t *alias) -{ - for (size_t i = 0; i != alias->size; i++){ - free(alias->alias_array[i]); - free(alias->alias_to_replace[i]); - } - free(alias->alias_array); - free(alias->alias_to_replace); - return ; -} - -int builtins_display_alias(alias_t *alias) -{ - for (size_t i = 0; i != alias->size; i++){ - printf("|| Alias: %s || ", alias->alias_array[i]); - printf("Command: %s ||\n", alias->alias_to_replace[i]); - } - return RETURN_SUCCESS; -} - -int builtins_alias(ef_t *ef, char **args) -{ - alias_t *alias = ef->builtin_handler->alias; - char *first_arg = args[1]; - - if (first_arg != NULL && strcmp(args[1], "--display") == 0) - return builtins_display_alias(alias); - if (len_array(args) != 3) - return RETURN_FAILURE; - alias->alias_array[alias->size - 1] = strdup(args[1]); - alias->alias_to_replace[alias->size - 1] = strdup(args[2]); - printf("size alias %d\n",len_array(args));// alias->size); - printf("J ACTIVE LA SAUVEGARDE DE L ALIAS\n"); - //return RETURN_FAILURE; - return RETURN_SUCCESS; -} - diff --git a/src/builtins_handler.h b/src/builtins_handler.h index acd5074..808edf5 100644 --- a/src/builtins_handler.h +++ b/src/builtins_handler.h @@ -11,13 +11,11 @@ #include "env.h" #include "history.h" #include "shell.h" - #include "alias.h" typedef struct { env_t *env; history_t *history; his_command_t *history_command; - alias_t *alias; } builtin_handler_t; size_t update_command(char **buffer, diff --git a/src/exec.c b/src/exec.c index 8b145fc..2330f12 100644 --- a/src/exec.c +++ b/src/exec.c @@ -33,8 +33,7 @@ const builtins_funcs_t BUILTINS[] = { { "unsetenv", &builtins_unsetenv }, { ":", &builtins_funny_double_dot }, { "exit", &builtins_exit }, - { "history", &builtins_history}, - { "alias", &builtins_alias} + { "history", &builtins_history} }; const size_t BUILTINS_SZ = sizeof BUILTINS / sizeof *BUILTINS; diff --git a/src/shell.c b/src/shell.c index 26f8fe2..b9edda9 100644 --- a/src/shell.c +++ b/src/shell.c @@ -15,11 +15,9 @@ #include "common.h" #include "debug.h" #include "env.h" +#include "history.h" #include "shell.h" #include "u_str.h" -#include "history.h" -#include "exec.h" -#include "alias.h" __attribute__((unused)) static @@ -62,7 +60,6 @@ void write_prompt(int is_a_tty) ** Pour changer la commande ** passer en parametre ** si besoin -** historique, alias ... */ static int shell_loop(int is_a_tty, builtin_handler_t *builtin_handler) @@ -103,57 +100,32 @@ his_command_t *init_cmd_history(void) return cmd_history; } -/* -** verifier le retour du malloc et passer -** l initalisation de builtin handler dans -** une fonction pour l env l' history et les futurs builtins -*/ -alias_t init_alias(void) -{ - alias_t alias; - - alias.size = 1; - alias.alias_array = malloc(sizeof(char *) * alias.size); - alias.alias_to_replace = malloc(sizeof(char *) * alias.size); - return alias; -} - static bool error_in_init(builtin_handler_t *builtin_handler) { - if (!builtin_handler->history_command || !builtin_handler->env->env || !builtin_handler->alias->alias_array || !builtin_handler->alias->alias_to_replace){ - if (builtin_handler->history_command) - free(builtin_handler->history_command); - if (builtin_handler->env->env) - free(builtin_handler->env->env); - if (builtin_handler->alias->alias_array) - free(builtin_handler->alias->alias_array); - if (!builtin_handler->alias->alias_to_replace) - free(builtin_handler->alias->alias_to_replace); + if (!builtin_handler->history_command || !builtin_handler->env->env) { + free(builtin_handler->history_command); + free(builtin_handler->env->env); return true; - } + } return false; } int shell(char **env_ptr) { - alias_t alias = init_alias(); env_t env = parse_env(env_ptr); history_t history = { .cmd_history = NULL, 0, .last_chdir = NULL}; his_command_t *cmd_history = init_cmd_history(); builtin_handler_t builtin_handler = {.env = &env, - .history = &history, .history_command = cmd_history, - .alias = &alias}; + .history = &history, .history_command = cmd_history }; int shell_result; if (error_in_init(&builtin_handler) == true){ - free_alias(builtin_handler.alias); return RETURN_FAILURE; } U_DEBUG_CALL(debug_env_entries, &env); signal(SIGINT, ignore_sigint); shell_result = shell_loop(isatty(STDIN_FILENO), &builtin_handler); free_env(builtin_handler.env); - free_alias(builtin_handler.alias); return shell_result; } From fc16e65bb9b788bb957f279be96551f1d3f29e30 Mon Sep 17 00:00:00 2001 From: Titouanhct Date: Wed, 16 Apr 2025 18:13:54 +0200 Subject: [PATCH 22/23] [FIX] coding style on len_array --- src/utils/len_array.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/len_array.c b/src/utils/len_array.c index 5b40e62..7c9d807 100644 --- a/src/utils/len_array.c +++ b/src/utils/len_array.c @@ -11,7 +11,7 @@ int len_array(char **array) { int i = 0; - while(array[i] != NULL) + while (array[i] != NULL) i++; return i; -} \ No newline at end of file +} From bf421ce1ee58ec16e846bd1e36e81cc134483dc1 Mon Sep 17 00:00:00 2001 From: Titouanhct Date: Wed, 16 Apr 2025 18:32:56 +0200 Subject: [PATCH 23/23] [FIX] changes sugested during the PR --- Makefile | 2 +- src/ast.h | 2 +- src/builtins/cd.c | 8 ++++---- src/builtins/exit.c | 2 +- src/builtins/history.c | 2 +- src/builtins_handler.h | 4 ++-- src/exec.c | 8 ++++---- src/exec.h | 2 +- src/history.h | 1 - src/parse_history.c | 9 ++++----- src/shell.c | 26 +++++++++++++------------- src/update_command.c | 8 ++++---- src/visitor.c | 10 +++++----- 13 files changed, 41 insertions(+), 43 deletions(-) diff --git a/Makefile b/Makefile index 4e4fe1f..f3a87c1 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ BUILD_DIR := .build CC := gcc CFLAGS += -Wall -Wextra -Werror=write-strings -iquote ulib -iquote src -CFLAGS += -Wno-unused-parameter -Wunused-result -fanalyzer -g +CFLAGS += -Wno-unused-parameter -Wunused-result -fanalyzer CFLAGS += -Wp,-U_FORTIFY_SOURCE -Wcast-qual -Wduplicated-branches CFLAGS += -Wduplicated-cond -Wformat=2 -Wshadow -fno-builtin CFLAGS += -Wstrict-aliasing=0 -Wstrict-prototypes -Wunreachable-code diff --git a/src/ast.h b/src/ast.h index 3c16964..94e1c96 100644 --- a/src/ast.h +++ b/src/ast.h @@ -95,7 +95,7 @@ extern const tokens_list_t TOKENS_LIST[]; ast_t *parse_expression(ast_ctx_t *ctx); void print_ast(ast_ctx_t *ctx, ast_t *ast, size_t depth); token_t get_next_token(ast_ctx_t *ctx); -int visitor(char *buffer, builtin_handler_t *builtin_handler); +int visitor(char *buffer, exec_ctx_t *exec_ctx); ast_t *create_node(ast_ctx_t *ctx); bool ensure_node_cap(ast_t *node); bool ensure_list_cap(ast_t *node); diff --git a/src/builtins/cd.c b/src/builtins/cd.c index 1f2fded..f888618 100644 --- a/src/builtins/cd.c +++ b/src/builtins/cd.c @@ -62,17 +62,17 @@ int builtins_cd_chdir(ef_t *ef, char **args, char *path) { char *act_pwd; - if (ef->builtin_handler->history->last_chdir != NULL && args[1] != NULL + if (ef->exec_ctx->history->last_chdir != NULL && args[1] != NULL && u_strcmp(args[1], "-") == 0) - path = ef->builtin_handler->history->last_chdir; + path = ef->exec_ctx->history->last_chdir; act_pwd = get_current_dir(); if (chdir(path) < 0) { write(STDERR_FILENO, path, u_strlen(path)); cd_print_error(); return RETURN_FAILURE; } - free(ef->builtin_handler->history->last_chdir); - ef->builtin_handler->history->last_chdir = act_pwd; + free(ef->exec_ctx->history->last_chdir); + ef->exec_ctx->history->last_chdir = act_pwd; return RETURN_SUCCESS; } diff --git a/src/builtins/exit.c b/src/builtins/exit.c index d18313a..881909a 100644 --- a/src/builtins/exit.c +++ b/src/builtins/exit.c @@ -23,5 +23,5 @@ int builtins_exit(ef_t *ef, char **args __attribute__((unused))) } free_env(ef->env); free(ef->buffer); - exit(ef->builtin_handler->history->last_exit_code); + exit(ef->exec_ctx->history->last_exit_code); } diff --git a/src/builtins/history.c b/src/builtins/history.c index 543b09d..ccc1754 100644 --- a/src/builtins/history.c +++ b/src/builtins/history.c @@ -12,7 +12,7 @@ int builtins_history(ef_t *ef, char **args __attribute__((unused))) { - his_command_t *cmd_history = ef->builtin_handler->history_command; + his_command_t *cmd_history = ef->exec_ctx->history_command; for (int i = 0; i < cmd_history->sz; i++){ if (cmd_history[i].arg) { diff --git a/src/builtins_handler.h b/src/builtins_handler.h index 808edf5..a47f2dc 100644 --- a/src/builtins_handler.h +++ b/src/builtins_handler.h @@ -16,8 +16,8 @@ typedef struct { env_t *env; history_t *history; his_command_t *history_command; -} builtin_handler_t; +} exec_ctx_t; size_t update_command(char **buffer, - size_t *buffer_sz, builtin_handler_t *builtin_handler); + size_t *buffer_sz, exec_ctx_t *exec_ctx); #endif /* BUILTINS_HANDLER_H */ diff --git a/src/exec.c b/src/exec.c index 2330f12..65f042f 100644 --- a/src/exec.c +++ b/src/exec.c @@ -138,8 +138,8 @@ int launch_bin(char *full_bin_path, char **args, ef_t *ef) else waitpid(pid, &status, WNOHANG); if (WIFEXITED(status)) - ef->builtin_handler->history->last_exit_code = - ef->builtin_handler->history->last_exit_code ?: WEXITSTATUS(status); + ef->exec_ctx->history->last_exit_code = + ef->exec_ctx->history->last_exit_code ?: WEXITSTATUS(status); return status; } @@ -172,7 +172,7 @@ bool builtins_launcher(ef_t *ef, char **args) if (u_strlen(BUILTINS[i].name) != bin_l) continue; if (u_strcmp(BUILTINS[i].name, args[0]) == 0) { - ef->builtin_handler->history->last_exit_code = + ef->exec_ctx->history->last_exit_code = BUILTINS[i].ptr(ef, args); return true; } @@ -200,6 +200,6 @@ int execute(ef_t *ef) U_DEBUG("Exit code [%d]\n", ef->history->last_exit_code); free(full_bin_path); free((void *)args); - return ef->builtin_handler->history->last_exit_code + return ef->exec_ctx->history->last_exit_code != 0 ? RETURN_FAILURE : RETURN_SUCCESS; } diff --git a/src/exec.h b/src/exec.h index a3328d7..4bb4a2c 100644 --- a/src/exec.h +++ b/src/exec.h @@ -43,7 +43,7 @@ typedef struct { int pout_fd; int in_fd; int out_fd; - builtin_handler_t *builtin_handler; + exec_ctx_t *exec_ctx; } ef_t; __attribute__((nonnull)) diff --git a/src/history.h b/src/history.h index 2c423d7..ac2eabe 100644 --- a/src/history.h +++ b/src/history.h @@ -24,7 +24,6 @@ typedef struct history_variable_s { typedef struct history_command_s { int id; - char time[5];// []h[] char *command; char *arg; int sz; diff --git a/src/parse_history.c b/src/parse_history.c index 03df93d..350d9c8 100644 --- a/src/parse_history.c +++ b/src/parse_history.c @@ -20,12 +20,11 @@ #include "history.h" const parsing_history_t tab_fnct[] = { - {"!!", &his_last_command}, //last command - {"!$", &his_last_word}, //last word command - {"!*", &his_last_arg}, //last argument commmand + {"!!", &his_last_command}, + {"!$", &his_last_word}, + {"!*", &his_last_arg}, {"![command]", &his_last_same_command}, - {"![number]", &his_id_command}, //id command - {NULL, NULL}, + {"![number]", &his_id_command}, }; static diff --git a/src/shell.c b/src/shell.c index b9edda9..62751d3 100644 --- a/src/shell.c +++ b/src/shell.c @@ -62,7 +62,7 @@ void write_prompt(int is_a_tty) ** si besoin */ static -int shell_loop(int is_a_tty, builtin_handler_t *builtin_handler) +int shell_loop(int is_a_tty, exec_ctx_t *exec_ctx) { char *buffer = NULL; size_t buffer_sz = 0; @@ -72,17 +72,17 @@ int shell_loop(int is_a_tty, builtin_handler_t *builtin_handler) write_prompt(is_a_tty); if (getline(&buffer, &buffer_sz, stdin) == -1) break; - buffer_len = update_command(&buffer, &buffer_sz, builtin_handler); + buffer_len = update_command(&buffer, &buffer_sz, exec_ctx); if (buffer_len < 1 || !u_str_is_alnum(buffer)) { check_basic_error(buffer); continue; } U_DEBUG("Buffer [%lu] [%s]\n", buffer_len, buffer); - visitor(buffer, builtin_handler); + visitor(buffer, exec_ctx); free(buffer); } - free(builtin_handler->history_command); - return (free(buffer), builtin_handler->history->last_exit_code); + free(exec_ctx->history_command); + return (free(buffer), exec_ctx->history->last_exit_code); } his_command_t *init_cmd_history(void) @@ -101,11 +101,11 @@ his_command_t *init_cmd_history(void) } static -bool error_in_init(builtin_handler_t *builtin_handler) +bool error_in_init(exec_ctx_t *exec_ctx) { - if (!builtin_handler->history_command || !builtin_handler->env->env) { - free(builtin_handler->history_command); - free(builtin_handler->env->env); + if (!exec_ctx->history_command || !exec_ctx->env->env) { + free(exec_ctx->history_command); + free(exec_ctx->env->env); return true; } return false; @@ -116,16 +116,16 @@ int shell(char **env_ptr) env_t env = parse_env(env_ptr); history_t history = { .cmd_history = NULL, 0, .last_chdir = NULL}; his_command_t *cmd_history = init_cmd_history(); - builtin_handler_t builtin_handler = {.env = &env, + exec_ctx_t exec_ctx = {.env = &env, .history = &history, .history_command = cmd_history }; int shell_result; - if (error_in_init(&builtin_handler) == true){ + if (error_in_init(&exec_ctx) == true){ return RETURN_FAILURE; } U_DEBUG_CALL(debug_env_entries, &env); signal(SIGINT, ignore_sigint); - shell_result = shell_loop(isatty(STDIN_FILENO), &builtin_handler); - free_env(builtin_handler.env); + shell_result = shell_loop(isatty(STDIN_FILENO), &exec_ctx); + free_env(exec_ctx.env); return shell_result; } diff --git a/src/update_command.c b/src/update_command.c index dd2d83d..0f0ca4c 100644 --- a/src/update_command.c +++ b/src/update_command.c @@ -33,16 +33,16 @@ his_command_t *save_command(char *cmd, his_command_t *cmd_history) } size_t update_command(char **buffer, - size_t *buffer_sz, builtin_handler_t *builtin_handler) + size_t *buffer_sz, exec_ctx_t *exec_ctx) { size_t buffer_len = 0; buffer_len = u_strlen(*buffer); (*buffer)[buffer_len - 1] = '\0'; if (parse_history(buffer, &buffer_len, - buffer_sz, &builtin_handler->history_command) == 84) + buffer_sz, &exec_ctx->history_command) == 84) return -1; - builtin_handler->history_command = save_command(*buffer, - builtin_handler->history_command); + exec_ctx->history_command = save_command(*buffer, + exec_ctx->history_command); return buffer_len; } diff --git a/src/visitor.c b/src/visitor.c index e4c34f4..8811a99 100644 --- a/src/visitor.c +++ b/src/visitor.c @@ -146,19 +146,19 @@ void remove_trailing_semi(char *str) } } -int visitor(char *buffer, builtin_handler_t *builtin_handler) +int visitor(char *buffer, exec_ctx_t *exec_ctx) { ast_ctx_t ctx = { 0, .str = buffer, .cap = u_strlen(buffer) + 10, .ast = malloc(sizeof *ctx.ast * (u_strlen(buffer) + 10)) }; - ef_t ef = { .buffer = buffer, .env = builtin_handler->env, - .history = builtin_handler->history, .ctx + ef_t ef = { .buffer = buffer, .env = exec_ctx->env, + .history = exec_ctx->history, .ctx = &ctx, .pout_fd = STDOUT_FILENO, .flags = 0, - .builtin_handler = builtin_handler}; + .exec_ctx = exec_ctx}; int result = RETURN_FAILURE; ctx.first_node = ctx.ast; remove_trailing_semi(ctx.str); - builtin_handler->history->last_exit_code = 0; + exec_ctx->history->last_exit_code = 0; if (ctx.ast == NULL) return RETURN_FAILURE; result = visitor_launcher(&ef);