From: Christopher Uhlig Date: Thu, 23 Jan 2025 17:41:18 +0000 (+0100) Subject: a X-Git-Url: https://git.dkaiser.de/?a=commitdiff_plain;h=18fb7cb8ae69fc3439266a154aa6b0f947d6805d;p=42%2Fminishell.git a --- diff --git a/include/debug_tools.h b/include/debug_tools.h index 3f3dd26..9a24875 100644 --- a/include/debug_tools.h +++ b/include/debug_tools.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 18:34:37 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 19:38:02 by dkaiser ### ########.fr */ +/* Updated: 2025/01/23 15:22:14 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ # define DEBUG_TOOLS_H # include "libft.h" # include +# include "token.h" # ifndef DEBUG # define DEBUG 0 @@ -22,5 +23,6 @@ void dbg(char *str); void panic(char *msg); +void print_token_list(t_token *token_list); #endif diff --git a/lib/libft/_obj/ft_atoi.o b/lib/libft/_obj/ft_atoi.o deleted file mode 100644 index b5601c8..0000000 Binary files a/lib/libft/_obj/ft_atoi.o and /dev/null differ diff --git a/lib/libft/_obj/ft_atol.o b/lib/libft/_obj/ft_atol.o deleted file mode 100644 index ae1eb17..0000000 Binary files a/lib/libft/_obj/ft_atol.o and /dev/null differ diff --git a/lib/libft/_obj/ft_bzero.o b/lib/libft/_obj/ft_bzero.o deleted file mode 100644 index 14b59f2..0000000 Binary files a/lib/libft/_obj/ft_bzero.o and /dev/null differ diff --git a/lib/libft/_obj/ft_calloc.o b/lib/libft/_obj/ft_calloc.o deleted file mode 100644 index 7e3b83b..0000000 Binary files a/lib/libft/_obj/ft_calloc.o and /dev/null differ diff --git a/lib/libft/_obj/ft_isalnum.o b/lib/libft/_obj/ft_isalnum.o deleted file mode 100644 index ffdb654..0000000 Binary files a/lib/libft/_obj/ft_isalnum.o and /dev/null differ diff --git a/lib/libft/_obj/ft_isalpha.o b/lib/libft/_obj/ft_isalpha.o deleted file mode 100644 index 3989dd3..0000000 Binary files a/lib/libft/_obj/ft_isalpha.o and /dev/null differ diff --git a/lib/libft/_obj/ft_isascii.o b/lib/libft/_obj/ft_isascii.o deleted file mode 100644 index 2023ffc..0000000 Binary files a/lib/libft/_obj/ft_isascii.o and /dev/null differ diff --git a/lib/libft/_obj/ft_isdigit.o b/lib/libft/_obj/ft_isdigit.o deleted file mode 100644 index befcb64..0000000 Binary files a/lib/libft/_obj/ft_isdigit.o and /dev/null differ diff --git a/lib/libft/_obj/ft_isprint.o b/lib/libft/_obj/ft_isprint.o deleted file mode 100644 index f75f178..0000000 Binary files a/lib/libft/_obj/ft_isprint.o and /dev/null differ diff --git a/lib/libft/_obj/ft_isspace.o b/lib/libft/_obj/ft_isspace.o deleted file mode 100644 index 6325fbb..0000000 Binary files a/lib/libft/_obj/ft_isspace.o and /dev/null differ diff --git a/lib/libft/_obj/ft_itoa.o b/lib/libft/_obj/ft_itoa.o deleted file mode 100644 index a861d2b..0000000 Binary files a/lib/libft/_obj/ft_itoa.o and /dev/null differ diff --git a/lib/libft/_obj/ft_lstadd_back_bonus.o b/lib/libft/_obj/ft_lstadd_back_bonus.o deleted file mode 100644 index b579431..0000000 Binary files a/lib/libft/_obj/ft_lstadd_back_bonus.o and /dev/null differ diff --git a/lib/libft/_obj/ft_lstadd_front_bonus.o b/lib/libft/_obj/ft_lstadd_front_bonus.o deleted file mode 100644 index e72302f..0000000 Binary files a/lib/libft/_obj/ft_lstadd_front_bonus.o and /dev/null differ diff --git a/lib/libft/_obj/ft_lstclear_bonus.o b/lib/libft/_obj/ft_lstclear_bonus.o deleted file mode 100644 index 5bad20b..0000000 Binary files a/lib/libft/_obj/ft_lstclear_bonus.o and /dev/null differ diff --git a/lib/libft/_obj/ft_lstdelone_bonus.o b/lib/libft/_obj/ft_lstdelone_bonus.o deleted file mode 100644 index 3bc3219..0000000 Binary files a/lib/libft/_obj/ft_lstdelone_bonus.o and /dev/null differ diff --git a/lib/libft/_obj/ft_lstiter_bonus.o b/lib/libft/_obj/ft_lstiter_bonus.o deleted file mode 100644 index 025f21a..0000000 Binary files a/lib/libft/_obj/ft_lstiter_bonus.o and /dev/null differ diff --git a/lib/libft/_obj/ft_lstlast_bonus.o b/lib/libft/_obj/ft_lstlast_bonus.o deleted file mode 100644 index b968ed3..0000000 Binary files a/lib/libft/_obj/ft_lstlast_bonus.o and /dev/null differ diff --git a/lib/libft/_obj/ft_lstmap_bonus.o b/lib/libft/_obj/ft_lstmap_bonus.o deleted file mode 100644 index aa0df25..0000000 Binary files a/lib/libft/_obj/ft_lstmap_bonus.o and /dev/null differ diff --git a/lib/libft/_obj/ft_lstnew_bonus.o b/lib/libft/_obj/ft_lstnew_bonus.o deleted file mode 100644 index 1e5a07e..0000000 Binary files a/lib/libft/_obj/ft_lstnew_bonus.o and /dev/null differ diff --git a/lib/libft/_obj/ft_lstsize_bonus.o b/lib/libft/_obj/ft_lstsize_bonus.o deleted file mode 100644 index f3ac188..0000000 Binary files a/lib/libft/_obj/ft_lstsize_bonus.o and /dev/null differ diff --git a/lib/libft/_obj/ft_memchr.o b/lib/libft/_obj/ft_memchr.o deleted file mode 100644 index 70158a2..0000000 Binary files a/lib/libft/_obj/ft_memchr.o and /dev/null differ diff --git a/lib/libft/_obj/ft_memcmp.o b/lib/libft/_obj/ft_memcmp.o deleted file mode 100644 index 1d24639..0000000 Binary files a/lib/libft/_obj/ft_memcmp.o and /dev/null differ diff --git a/lib/libft/_obj/ft_memcpy.o b/lib/libft/_obj/ft_memcpy.o deleted file mode 100644 index 62d92ff..0000000 Binary files a/lib/libft/_obj/ft_memcpy.o and /dev/null differ diff --git a/lib/libft/_obj/ft_memmove.o b/lib/libft/_obj/ft_memmove.o deleted file mode 100644 index c49861b..0000000 Binary files a/lib/libft/_obj/ft_memmove.o and /dev/null differ diff --git a/lib/libft/_obj/ft_memset.o b/lib/libft/_obj/ft_memset.o deleted file mode 100644 index f3d6f95..0000000 Binary files a/lib/libft/_obj/ft_memset.o and /dev/null differ diff --git a/lib/libft/_obj/ft_printaddr.o b/lib/libft/_obj/ft_printaddr.o deleted file mode 100644 index 1a108d4..0000000 Binary files a/lib/libft/_obj/ft_printaddr.o and /dev/null differ diff --git a/lib/libft/_obj/ft_printf.o b/lib/libft/_obj/ft_printf.o deleted file mode 100644 index a2584ef..0000000 Binary files a/lib/libft/_obj/ft_printf.o and /dev/null differ diff --git a/lib/libft/_obj/ft_printhex.o b/lib/libft/_obj/ft_printhex.o deleted file mode 100644 index 3fec031..0000000 Binary files a/lib/libft/_obj/ft_printhex.o and /dev/null differ diff --git a/lib/libft/_obj/ft_printnbr.o b/lib/libft/_obj/ft_printnbr.o deleted file mode 100644 index 173d69e..0000000 Binary files a/lib/libft/_obj/ft_printnbr.o and /dev/null differ diff --git a/lib/libft/_obj/ft_putchar_fd.o b/lib/libft/_obj/ft_putchar_fd.o deleted file mode 100644 index a3b55db..0000000 Binary files a/lib/libft/_obj/ft_putchar_fd.o and /dev/null differ diff --git a/lib/libft/_obj/ft_putendl_fd.o b/lib/libft/_obj/ft_putendl_fd.o deleted file mode 100644 index 99fe4b3..0000000 Binary files a/lib/libft/_obj/ft_putendl_fd.o and /dev/null differ diff --git a/lib/libft/_obj/ft_putnbr_fd.o b/lib/libft/_obj/ft_putnbr_fd.o deleted file mode 100644 index 7f2b2c3..0000000 Binary files a/lib/libft/_obj/ft_putnbr_fd.o and /dev/null differ diff --git a/lib/libft/_obj/ft_putstr_fd.o b/lib/libft/_obj/ft_putstr_fd.o deleted file mode 100644 index dc75865..0000000 Binary files a/lib/libft/_obj/ft_putstr_fd.o and /dev/null differ diff --git a/lib/libft/_obj/ft_split.o b/lib/libft/_obj/ft_split.o deleted file mode 100644 index c0742f5..0000000 Binary files a/lib/libft/_obj/ft_split.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strcat.o b/lib/libft/_obj/ft_strcat.o deleted file mode 100644 index 7ff5887..0000000 Binary files a/lib/libft/_obj/ft_strcat.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strchr.o b/lib/libft/_obj/ft_strchr.o deleted file mode 100644 index 632528a..0000000 Binary files a/lib/libft/_obj/ft_strchr.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strcmp.o b/lib/libft/_obj/ft_strcmp.o deleted file mode 100644 index 0a6aa67..0000000 Binary files a/lib/libft/_obj/ft_strcmp.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strcpy.o b/lib/libft/_obj/ft_strcpy.o deleted file mode 100644 index 5e4d60f..0000000 Binary files a/lib/libft/_obj/ft_strcpy.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strdup.o b/lib/libft/_obj/ft_strdup.o deleted file mode 100644 index 177e976..0000000 Binary files a/lib/libft/_obj/ft_strdup.o and /dev/null differ diff --git a/lib/libft/_obj/ft_striteri.o b/lib/libft/_obj/ft_striteri.o deleted file mode 100644 index 5f45ab5..0000000 Binary files a/lib/libft/_obj/ft_striteri.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strjoin.o b/lib/libft/_obj/ft_strjoin.o deleted file mode 100644 index 1bd4c7a..0000000 Binary files a/lib/libft/_obj/ft_strjoin.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strlcat.o b/lib/libft/_obj/ft_strlcat.o deleted file mode 100644 index 2d4a576..0000000 Binary files a/lib/libft/_obj/ft_strlcat.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strlcpy.o b/lib/libft/_obj/ft_strlcpy.o deleted file mode 100644 index 9ff1435..0000000 Binary files a/lib/libft/_obj/ft_strlcpy.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strlen.o b/lib/libft/_obj/ft_strlen.o deleted file mode 100644 index 00d6890..0000000 Binary files a/lib/libft/_obj/ft_strlen.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strmapi.o b/lib/libft/_obj/ft_strmapi.o deleted file mode 100644 index 309084c..0000000 Binary files a/lib/libft/_obj/ft_strmapi.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strncmp.o b/lib/libft/_obj/ft_strncmp.o deleted file mode 100644 index a6cfedf..0000000 Binary files a/lib/libft/_obj/ft_strncmp.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strncpy.o b/lib/libft/_obj/ft_strncpy.o deleted file mode 100644 index 978f1ef..0000000 Binary files a/lib/libft/_obj/ft_strncpy.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strnstr.o b/lib/libft/_obj/ft_strnstr.o deleted file mode 100644 index 5a06fae..0000000 Binary files a/lib/libft/_obj/ft_strnstr.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strrchr.o b/lib/libft/_obj/ft_strrchr.o deleted file mode 100644 index 0c99b66..0000000 Binary files a/lib/libft/_obj/ft_strrchr.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strtrim.o b/lib/libft/_obj/ft_strtrim.o deleted file mode 100644 index d7332af..0000000 Binary files a/lib/libft/_obj/ft_strtrim.o and /dev/null differ diff --git a/lib/libft/_obj/ft_substr.o b/lib/libft/_obj/ft_substr.o deleted file mode 100644 index d3dbbc4..0000000 Binary files a/lib/libft/_obj/ft_substr.o and /dev/null differ diff --git a/lib/libft/_obj/ft_tolower.o b/lib/libft/_obj/ft_tolower.o deleted file mode 100644 index 17dbc32..0000000 Binary files a/lib/libft/_obj/ft_tolower.o and /dev/null differ diff --git a/lib/libft/_obj/ft_toupper.o b/lib/libft/_obj/ft_toupper.o deleted file mode 100644 index fb08203..0000000 Binary files a/lib/libft/_obj/ft_toupper.o and /dev/null differ diff --git a/lib/libft/_obj/get_next_line.o b/lib/libft/_obj/get_next_line.o deleted file mode 100644 index 23c2b36..0000000 Binary files a/lib/libft/_obj/get_next_line.o and /dev/null differ diff --git a/lib/libft/_obj/get_next_line_utils.o b/lib/libft/_obj/get_next_line_utils.o deleted file mode 100644 index 204f4dc..0000000 Binary files a/lib/libft/_obj/get_next_line_utils.o and /dev/null differ diff --git a/lib/libft/libft.a b/lib/libft/libft.a deleted file mode 100644 index fe3527d..0000000 Binary files a/lib/libft/libft.a and /dev/null differ diff --git a/src/collect_redirs.c b/src/collect_redirs.c index d40cee4..8f5dadc 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */ -/* Updated: 2025/01/22 16:08:25 by chuhlig ### ########.fr */ +/* Updated: 2025/01/23 18:22:04 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ static int set_heredoc_data(t_token *cur, t_redirection *result, t_env *env); t_redirection *collect_redirs(t_token **tokens, t_env *env, - t_list **create_files) + t_list **create_files)// tokes is possition after pipe or first token { t_redirection *result; t_token *cur; @@ -31,14 +31,15 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env, result = malloc(sizeof(t_redirection) * 2); if (result == NULL) return (free_tokens(*tokens), NULL); - free(set_redir(&result[0], 0, NULL, env)); - free(set_redir(&result[1], 0, NULL, env)); + free(set_redir(&result[0], 0, NULL, env));// no token use + free(set_redir(&result[1], 0, NULL, env));// no token use data.create_files = create_files; data.env = env; while (cur != NULL) { - if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) - collect_and_check_redir(result, &cur, &data, tokens); + if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN)//could this be a problem with recursion? + collect_and_check_redir(result, &cur, &data, tokens);// cur=token is just in there to free and connect dont know if its ok + //return or better said does else if (cur->type == REDIR_TOKEN) return (free(result), NULL); else @@ -56,20 +57,21 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, str = ft_strdup((*cur)->next->content.string); if ((*cur)->content.redir_type == INPUT_LIMITER) { - if (!set_heredoc_data(*cur, result, data->env)) + if (!set_heredoc_data(*cur, result, data->env))// set here doc data with token head or first token after pipe return ; } else if ((*cur)->content.redir_type == INPUT_FILE) q4fc(data->create_files, set_redir(&result[0], INPUT_FILE, - format_string(str, data->env, 0), data->env)); + format_string(str, data->env, 0), data->env));//here toke list should be not messed up else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE) q4fc(data->create_files, set_redir(&result[1], OUTPUT_OVERRIDE, format_string(str, data->env, 0), data->env)); else if ((*cur)->content.redir_type == OUTPUT_APPEND) q4fc(data->create_files, set_redir(&result[1], OUTPUT_APPEND, format_string(str, data->env, 0), data->env)); - i_love_the_norme(cur, tokens); -} + i_love_the_norme(cur, tokens);// takes adress of token head or pos after pipe and the token specifer +}// i love the norm simple does free token and connect +//later more detailes static t_redirection *set_redir(t_redirection *redir, int type, char *spec, t_env *env) diff --git a/src/debug_tools.c b/src/debug_tools.c index 6bee1b0..2c8ee7e 100644 --- a/src/debug_tools.c +++ b/src/debug_tools.c @@ -6,13 +6,15 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 15:34:14 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 12:50:36 by chuhlig ### ########.fr */ +/* Updated: 2025/01/23 15:21:11 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "debug_tools.h" #include #include +#include "token.h" +#include void dbg(char *msg) { @@ -33,3 +35,21 @@ void panic(char *msg) ft_putendl_fd("\e[0m", 1); } } + + + +void print_token_list(t_token *token_list) { + t_token *current = token_list; + while (current != NULL) { + if (current->type == STRING_TOKEN) { + printf("STRING_TOKEN: %s\n", current->content.string); + } else if (current->type == REDIR_TOKEN) { + printf("REDIR_TOKEN: %d\n", current->content.redir_type); + } else if (current->type == PIPE_TOKEN) { + printf("PIPE_TOKEN\n"); + } else if (current->type == NEWLINE_TOKEN) { + printf("NEWLINE_TOKEN\n"); + } + current = current->next; + } +} diff --git a/src/format_string.c b/src/format_string.c index ccc7084..06e5210 100644 --- a/src/format_string.c +++ b/src/format_string.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:30:11 by chuhlig #+# #+# */ -/* Updated: 2025/01/22 14:31:29 by chuhlig ### ########.fr */ +/* Updated: 2025/01/23 14:34:35 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,8 +49,8 @@ static void append_slice(char **dst, char *src, int start, int end) i++; } result[len + i] = '\0'; - if (*dst != NULL) - free(*dst); + // if (*dst != NULL) + // free(*dst); *dst = result; // free(src); // src = *dst; diff --git a/src/parse_cmd.c b/src/parse_cmd.c index 02c4bda..a978c99 100644 --- a/src/parse_cmd.c +++ b/src/parse_cmd.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */ -/* Updated: 2025/01/22 17:26:14 by dkaiser ### ########.fr */ +/* Updated: 2025/01/23 18:04:50 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,10 +21,16 @@ t_node *parse_cmd(t_token *tokens, t_env **env) t_list *create_files; create_files = NULL; - redirs = collect_redirs(&tokens, *env, &create_files); + printf("parse_cmd\n"); + print_token_list(tokens); + redirs = collect_redirs(&tokens, *env, &create_files);// takes pos next to pipe or first token + printf("parse_cmdafter collect redir\n"); + print_token_list(tokens);// still existing tokenlist if (redirs == NULL) return (NULL); args = collect_args(&tokens, env); + printf("parse_cmdafter collect args\n"); + // print_token_list(tokens);//here it gone if (args == NULL) { free(redirs); @@ -33,33 +39,64 @@ t_node *parse_cmd(t_token *tokens, t_env **env) return (new_cmd_node(args, redirs, create_files)); } -static char **collect_args(t_token **tokens, t_env **env) -{ - t_token *cur; - char **result; - int i; - t_token *next; +// static char **collect_args(t_token **tokens, t_env **env) +// { +// t_token *cur; +// char **result; +// int i; +// t_token *next; - cur = *tokens; - i = 0; - while (cur != NULL && ++i) - cur = cur->next; - result = malloc(sizeof(char *) * (i + 1)); - if (result == NULL) - return (free_tokens(*tokens), NULL); - cur = *tokens; - i = 0; - while (cur != NULL && cur->type == STRING_TOKEN) - { - next = cur->next; - if (cur->previous) - free_token(cur->previous); - result[i] = format_string(cur->content.string, *env, ft_atoi("0")); - i++; - if (cur->next == NULL) - free_token(cur); - cur = next; - } - result[i] = NULL; - return (result); -} +// cur = *tokens; +// i = 0; +// while (cur != NULL && ++i) +// cur = cur->next; +// result = malloc(sizeof(char *) * (i + 1)); +// if (result == NULL) +// return (free_tokens(*tokens), NULL); +// cur = *tokens; +// i = 0; +// while (cur != NULL && cur->type == STRING_TOKEN) +// { +// next = cur->next; +// if (cur->previous) +// free_token(cur->previous); +// result[i] = format_string(cur->content.string, *env, ft_atoi("0")); +// i++; +// if (cur->next == NULL) +// free_token(cur); +// cur = next; +// } +// result[i] = NULL; +// return (result); +// } + +static char **collect_args(t_token **tokens, t_env **env) { + t_token *cur; + char **result; + int i; + t_token *next; + + cur = *tokens; + i = 0; + while (cur != NULL && ++i) + cur = cur->next; + result = malloc(sizeof(char *) * (i + 1)); + if (result == NULL) + return (free_tokens(*tokens), NULL); + cur = *tokens; + i = 0; + while (cur != NULL && cur->type == STRING_TOKEN) { + next = cur->next; + result[i] = format_string(cur->content.string, *env, ft_atoi("0")); + i++; + if (cur->previous) + cur->previous->next = cur->next; + if (cur->next) + cur->next->previous = cur->previous; + free_token(cur); + cur = next; + } + result[i] = NULL; + *tokens = cur; // Update the head of the token list + return (result); +}// need to later the rest of the tokenlist diff --git a/src/parser.c b/src/parser.c index 2d748c9..68ab372 100644 --- a/src/parser.c +++ b/src/parser.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/29 15:53:29 by dkaiser #+# #+# */ -/* Updated: 2025/01/22 17:13:50 by dkaiser ### ########.fr */ +/* Updated: 2025/01/23 18:00:18 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ static t_node *parse_statement(t_token *tokens, t_env **env); t_node *parse(t_token *tokens, t_env **env) { - t_node *result; + t_node *result;// if ((*tokens).type == PIPE_TOKEN) result = NULL; @@ -29,15 +29,20 @@ t_node *parse(t_token *tokens, t_env **env) result = parse_statement(tokens, env); if (result == NULL) printf("Parsing error.\n"); + // if (tokens != NULL) + // print_token_list(tokens); return (result); } static t_node *parse_statement(t_token *tokens, t_env **env) { t_token *left_side_tokens; - - left_side_tokens = split_at_first(&tokens, PIPE_TOKEN); - if (left_side_tokens == NULL) + + print_token_list(tokens);//until her is fine + left_side_tokens = split_at_first(&tokens, PIPE_TOKEN);//by pipe usage parse cmd gets reacls so also check there the token list changes + //leftside toke has the pos of pipe -> next + //or if we have pipe we return token head and also change the *tokens to after pipe + if (left_side_tokens == NULL)// we never return NULL exept token is already null { free_tokens(tokens); tokens = NULL; @@ -46,12 +51,13 @@ static t_node *parse_statement(t_token *tokens, t_env **env) else if (tokens != NULL) { return (new_pipe_node(parse_cmd(left_side_tokens, env), - parse_statement(tokens, env))); + parse_statement(tokens, env)));//here new pipe node } else { - return (parse_cmd(left_side_tokens, env)); - } + print_token_list(left_side_tokens); + return (parse_cmd(left_side_tokens, env));//here return is cmd node + }// here he takt left side token so } t_token *split_at_first(t_token **tokens, int type) @@ -59,21 +65,24 @@ t_token *split_at_first(t_token **tokens, int type) t_token *split; t_token *result; - split = find_token_by_type(*tokens, type); + split = find_token_by_type(*tokens, type);//split has the pos of where pipe appears// if no pipe in tokenlist 1st if case if (split == NULL) { result = *tokens; - *tokens = NULL; + *tokens = NULL;//we are change to pointing token to NULL return (result); } result = *tokens; - *tokens = split->next; + *tokens = split->next;// is this part enought reconnetion if (result == split) result = NULL; - free_token2(split); + free_token2(split);//why free here? bv would free pipe node but what is with the connection of the tokenlist does this mess up the connection? split = NULL; - return (result); + return (result);// at this return return is at tokenlist pos of split next and result is the tokenlist before split } +//free token seems not right here even +// or at least no the right funtion here +// static t_token *find_token_by_type(t_token *tokens, int type) { diff --git a/src/praise_the_norme.c b/src/praise_the_norme.c index 6af28c3..5639b7d 100644 --- a/src/praise_the_norme.c +++ b/src/praise_the_norme.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* praise_the_norme.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/20 18:35:41 by dkaiser #+# #+# */ -/* Updated: 2025/01/22 17:30:02 by dkaiser ### ########.fr */ +/* Updated: 2025/01/23 18:31:49 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,15 +16,19 @@ void i_love_the_norme(t_token **cur, t_token **tokens) { t_token *next_token; - next_token = (*cur)->next; - free_token_and_connect2(*cur); - if (next_token) + next_token = (*cur)->next;//setting next token to the adress of the next token + free_token_and_connect(*cur);// do i neee a double call here + //but technically it would remove the redir token + if (next_token)// if after redir is stuff { - if (next_token->previous == NULL) - *tokens = next_token->next; + if (next_token->previous == NULL)// then if is the first token or token after pipe + *tokens = next_token->next;// how does here come no error + //anyways i twould the redir adress token to the next token *cur = next_token->next; - free_token_and_connect2(next_token); + free_token_and_connect(next_token); } - else + else // else makes sense *cur = NULL; } + +// takes adress of token head or pos after pipe and the token specifer \ No newline at end of file diff --git a/src/repl.c b/src/repl.c index 3afb2a3..3988a42 100644 --- a/src/repl.c +++ b/src/repl.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2025/01/23 12:42:57 by dkaiser ### ########.fr */ +/* Updated: 2025/01/23 17:42:33 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -75,10 +75,12 @@ void repl(const char *prompt, t_env **env, int *promptflag) token_list = NULL; tokenizer(input, &token_list, '\0'); tokens_copy = shallow_copy_tokens(token_list); + free_tokens(tokens_copy); + // print_token_list(token_list); ast = parse(token_list, env); if (ast) set_return_code(eval(ast, env), env); free_repl(input, ast); - free_tokens(tokens_copy); + print_token_list(tokens_copy); } } diff --git a/x.c b/x.c new file mode 100644 index 0000000..4348da1 --- /dev/null +++ b/x.c @@ -0,0 +1,528 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* x.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/23 12:37:33 by chuhlig #+# #+# */ +/* Updated: 2025/01/23 17:37:13 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +Code Review +init.c +This file initializes the terminal settings and signal handling. It looks correct, but consider checking if tcgetattr and tcsetattr return errors and handle them appropriately. + +C + +static void init_terminal(void) { + struct termios term; + + if (tcgetattr(0, &term) == -1) { + perror("tcgetattr failed"); + exit(EXIT_FAILURE); + } + term.c_lflag &= ~ECHOCTL; + if (tcsetattr(0, TCSANOW, &term) == -1) { + perror("tcsetattr failed"); + exit(EXIT_FAILURE); + } +} +env.c +In the getenvlst function, modifying the environment variable string directly can lead to undefined behavior. Instead, consider using strdup to make a copy of the string. + +C + +void getenvlst(t_env **env, char **en) { + char *tmp; + int i; + t_env *current; + + i = 0; + while (en[i] != NULL) { + tmp = ft_strchr(en[i], '='); + if (!tmp) continue; + *tmp = '\0'; + current = malloc(sizeof(t_env)); + if (!current) exit(EXIT_FAILURE); // Handle malloc failure + current->name = ft_strdup(en[i]); + current->value = ft_strdup(tmp + 1); + current->next = *env; + *env = current; + i++; + } +} +repl.c +The repl function looks good, but consider checking the readline return value for NULL and handling memory allocation errors. + +error.c +The command_not_found_error function should ensure correct file descriptors: + +C + +void command_not_found_error(char *cmd) { + ft_printf("%s:", cmd); + ft_putstr_fd(" command not found", 2); // file descriptor 2 for stderr + ft_printf("\n"); +} +new_node.c +The new_cmd_node function calls free(redirs) but then returns NULL without freeing the allocated node. + +C + +t_node *new_cmd_node(char **args, t_redirection redirs[2], t_list *create_files) { + t_node *node = new_node(CMD_NODE); + if (!node) return NULL; + node->content.cmd.args = args; + if (redirs) { + node->content.cmd.redirs[0] = redirs[0]; + node->content.cmd.redirs[1] = redirs[1]; + node->content.cmd.create_files = create_files; + free(redirs); + redirs = NULL; + return node; + } + free(node); // Free the allocated node + return NULL; +} +free_node.c +In free_cmd_node, ensure you free both redirections correctly. + +C + +static void free_cmd_node(t_node *node) { + int i = 0; + while (node->content.cmd.args && node->content.cmd.args[i]) { + free(node->content.cmd.args[i]); + i++; + } + free(node->content.cmd.args); + if (node->content.cmd.redirs[0].type && node->content.cmd.redirs[0].specifier) + free(node->content.cmd.redirs[0].specifier); + if (node->content.cmd.redirs[1].type && node->content.cmd.redirs[1].specifier) + free(node->content.cmd.redirs[1].specifier); +} +Summary +The provided code generally looks well-structured, but there are some areas where error checking and memory management could be improved. Implementing these suggestions will help make the code more robust and easier to maintain. If you have any specific concerns or files you would like to focus on, please let me know! + + +anything else? +The initial analysis covered some key areas and potential improvements in the provided Makefile and source files. Here are a few more specific suggestions for certain files to ensure robustness and maintainability: + +Additional Suggestions +env.c (Handling Environment Variables): +Error Handling: Add error handling for ft_strdup and malloc to ensure memory allocation was successful. +Environment String Handling: Avoid modifying the original environment strings directly. +C + +void getenvlst(t_env **env, char **en) { + char *tmp; + int i; + t_env *current; + + i = 0; + while (en[i] != NULL) { + tmp = ft_strchr(en[i], '='); + if (!tmp) continue; + *tmp = '\0'; + current = malloc(sizeof(t_env)); + if (!current || !(current->name = ft_strdup(en[i])) || !(current->value = ft_strdup(tmp + 1))) { + perror("malloc or strdup failed"); + exit(EXIT_FAILURE); + } + current->next = *env; + *env = current; + *tmp = '='; // Restore the original string + i++; + } +} +repl.c (Read-Evaluate-Print Loop): +Memory Management: Ensure all allocated memory is freed. +Error Handling: Handle potential errors from functions like readline. +C + +void repl(const char *prompt, t_env **env, int *promptflag) { + char *input; + t_token *token_list; + t_list *lines; + + (*promptflag)++; + while (1) { + input = readline(prompt); + if (input == NULL) { + if (*promptflag > 1) + (*promptflag)--; + printf("exit\n"); + break; + } + if (input[0] == '\0') { + free(input); + continue; + } + add_history(input); + token_list = NULL; + tokenizer(input, &token_list, '\0'); + lines = parse(token_list, env); + if (lines) + set_return_code(eval(lines->content, env), env); + free(input); + free_tokens(token_list); + ft_lstclear(&lines, (void (*)(void *))free_node); + } +} +1 vulnerability detected +get_cmd_path.c (Command Path Resolution): +Error Handling: Ensure errors are correctly handled and resources are freed. +C + +static char *get_absolute_cmd_path(char *cmd, t_env *env, int *return_code) { + char *cur_dir; + char *result; + + cur_dir = ft_strjoin(env_get(env, "PWD"), "/"); + if (!cur_dir) { + return error(ENOMEM, cmd, 127, return_code); + } + result = ft_strjoin(cur_dir, cmd); + free(cur_dir); + if (!result) { + return error(ENOENT, cmd, 127, return_code); + } + if (access(result, F_OK) == -1) { + free(result); + return error(ENOENT, cmd, 127, return_code); + } + if (access(result, X_OK) == -1) { + free(result); + return error(EACCES, cmd, 126, return_code); + } + if (is_directory(result)) { + free(result); + return error(EISDIR, cmd, 126, return_code); + } + return result; +} +General Best Practices: +Consistent Error Handling: Ensure all functions consistently handle errors and free resources appropriately. +Code Comments: Add comments to explain complex logic and improve code readability. +Memory Management: Regularly check for potential memory leaks and ensure all allocated memory is freed. +If you have any specific files or sections you want to dive deeper into, feel free to let me know! + + +ok ill look over it: now after your analyze it seem tokens are not freed properly what i mean with that we are missing it seems on more free but im not sure where to place it especial when the input is invalid and cant be used properly +It looks like you may need to ensure that tokens are properly freed, especially in cases where the input is invalid. Let's take a look at the relevant parts of the code that handle tokens and ensure that memory is managed correctly. + +Token Freeing Logic +To help ensure that tokens are freed properly, let's review the primary functions that deal with token management and identify where additional freeing might be needed. + +Tokenizer Function +The tokenizer function creates tokens. If the input is invalid, we should ensure we free any partially created tokens. + +C + +void tokenizer(char *s, t_token **token_list, char quote_check) { + int pos = 0; + int i = -1; + int f = 0; + while (s[++i]) { + if (!f && ft_strchr("|<>\n", s[i])) + handle_special_chars(s, &i, &pos, token_list); + else if (f && s[i] == quote_check) + f = 0; + else if (!f && ft_strchr("\'\"", s[i])) { + f = 1; + quote_check = s[i]; + } + if ((!f && (ft_isspace(s[i + 1]))) || i == ft_strlen(s) - 1) { + snap_string_token(s, pos, i, token_list); + pos = i + 1; + } + } + *token_list = reverse_token_list(*token_list); +} +Free Tokens Function +The free_tokens function should be used to free the entire token list: + +C + +void free_tokens(t_token *tokens) { + while (tokens != NULL) { + t_token *next = tokens->next; + if (tokens->type == STRING_TOKEN) + free(tokens->content.string); + free(tokens); + tokens = next; + } +} +Parsing Logic +In the parsing logic, ensure that tokens are freed if an error occurs: + +C + +t_list *parse(t_token *tokens, t_env **env) { + t_node *result; + + if (tokens->type == PIPE_TOKEN) { + free_tokens(tokens); // Free tokens if the first token is invalid + return NULL; + } else { + result = parse_statement(tokens, env); + } + if (result == NULL) { + printf("Parsing error.\n"); + free_tokens(tokens); // Free tokens if parsing fails + } + return ft_lstnew(result); +} +Example Fix +Here is a more comprehensive example of how you can ensure tokens are freed correctly in the parse function and the collect_redirs function: + +C + +t_list *parse(t_token *tokens, t_env **env) { + t_node *result; + + if (tokens == NULL || tokens->type == PIPE_TOKEN) { + if (tokens) free_tokens(tokens); // Free tokens if the first token is invalid or NULL + return NULL; + } + result = parse_statement(tokens, env); + if (result == NULL) { + printf("Parsing error.\n"); + free_tokens(tokens); // Free tokens if parsing fails + } + return ft_lstnew(result); +} + +t_redirection *collect_redirs(t_token **tokens, t_env *env, t_list **create_files) { + t_redirection *result; + t_token *cur; + t_minidata data; + + cur = *tokens; + result = malloc(sizeof(t_redirection) * 2); + if (result == NULL) + return free_tokens(*tokens), NULL; + set_redir(&result[0], 0, NULL, env); + set_redir(&result[1], 0, NULL, env); + data.create_files = create_files; + data.env = env; + while (cur != NULL) { + if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) + collect_and_check_redir(result, &cur, &data, tokens); + else if (cur->type == REDIR_TOKEN) + return free(result), free_tokens(*tokens), NULL; + else + cur = cur->next; + } + if (cur && cur->type == REDIR_TOKEN) + return free(result), free_tokens(*tokens), NULL; + return result; +} +Summary +By ensuring that tokens are always freed when an error occurs or when they are no longer needed, you can avoid memory leaks and improve the robustness of your shell. If you have specific cases where you suspect tokens are not being freed correctly, please share those, and I can provide more targeted advice! + + +tokens are never NULL except when i press enter but this is handle in repl and to your free token the problem is with our aproach is that parts of the token will be updated while running like in parser.c +Thank you for the clarification. It seems like the tokens are being updated during parsing, which complicates the token freeing process. Let's ensure that we handle token freeing correctly in all cases, especially when errors occur during parsing. + +Reviewing the Token Management +Since tokens are updated during parsing, we need to be careful to free tokens correctly without disrupting the parsing logic. We'll focus on ensuring that tokens are freed appropriately in various scenarios, including error cases. + +Parsing Logic and Freeing Tokens +Parser Function: Ensure that tokens are freed if parsing fails. +Free Tokens Helper Function: Improve the free_tokens function to handle partial frees. +Example Fixes +Free Tokens Function +First, let's have a robust free_tokens function that can handle the complete or partial token lists:' + +C + +void free_tokens(t_token *tokens) { + t_token *next; + while (tokens != NULL) { + next = tokens->next; + if (tokens->type == STRING_TOKEN && tokens->content.string != NULL) { + free(tokens->content.string); + } + free(tokens); + tokens = next; + } +} +Parsing Functions +Next, ensure that the parsing logic properly frees tokens if an error occurs: + +parse Function: +C + +t_list *parse(t_token *tokens, t_env **env) { + t_node *result; + + if (tokens == NULL || tokens->type == PIPE_TOKEN) { + if (tokens) free_tokens(tokens); // Free tokens if the first token is invalid or NULL + return NULL; + } + result = parse_statement(tokens, env); + if (result == NULL) { + printf("Parsing error.\n"); + free_tokens(tokens); // Free tokens if parsing fails + } + return ft_lstnew(result); +} +parse_statement Function: +C + +static t_node *parse_statement(t_token *tokens, t_env **env) { + t_token *left_side_tokens; + + left_side_tokens = split_at_first(&tokens, PIPE_TOKEN); + if (left_side_tokens == NULL) { + free_tokens(tokens); // Free remaining tokens if split fails + return NULL; + } + if (tokens != NULL) { + t_node *left_node = parse_cmd(left_side_tokens, env); + t_node *right_node = parse_statement(tokens, env); + if (left_node == NULL || right_node == NULL) { + free_tokens(left_side_tokens); + free_tokens(tokens); + return NULL; + } + return new_pipe_node(left_node, right_node); + } + return parse_cmd(left_side_tokens, env); +} +Handling Token Updates +When tokens are updated, ensure that the old parts are freed correctly before being replaced. For example: + +C + +void update_token(t_token *token, int new_type, char *new_content) { + if (token->type == STRING_TOKEN && token->content.string != NULL) { + free(token->content.string); + } + token->type = new_type; + token->content.string = new_content; +} +Summary +By ensuring that tokens are properly freed in all scenarios, including when parsing errors occur and when tokens are updated, you can avoid memory leaks and ensure robust memory management. If you have any specific cases or additional concerns, please let me know! + + +i never update tokens static char **collect_args(t_token **tokens, t_env **env) { t_token *cur; char **result; int i; t_token *next; cur = *tokens; i = 0; while (cur != NULL && ++i) cur = cur->next; result = malloc(sizeof(char *) * (i + 1)); if (result == NULL) return (free_tokens(*tokens), NULL); cur = *tokens; i = 0; while (cur != NULL && cur->type == STRING_TOKEN) { next = cur->next; if (cur->previous) free_token(cur->previous); result[i] = format_string(cur->content.string, *env, ft_atoi("0")); i++; if (cur->next == NULL) free_token(cur); cur = next; } result[i] = NULL; return (result); } any suggestions for this? static char **collect_args(t_token **tokens, t_env **env) { t_token *cur; char **result; int i; t_token *next; cur = *tokens; i = 0; while (cur != NULL && ++i) cur = cur->next; result = malloc(sizeof(char *) * (i + 1)); if (result == NULL) return (free_tokens(*tokens), NULL); cur = *tokens; i = 0; while (cur != NULL && cur->type == STRING_TOKEN) { next = cur->next; if (cur->previous) free_token(cur->previous); result[i] = format_string(cur->content.string, *env, ft_atoi("0")); i++; if (cur->next == NULL) free_token(cur); cur = next; } result[i] = NULL; return (result); } just take as example input : a and check the hole code for yourself +Let's take a closer look at the collect_args function to ensure that tokens are freed properly and memory is managed correctly. The goal is to collect arguments from tokens, format them, and ensure that all tokens are freed without causing memory leaks or dangling pointers. + +Review of collect_args +The function collect_args traverses the token list, collects string tokens, and formats them into an array of arguments. It also attempts to free tokens as it processes them. + +Here’s the function with comments and suggestions for improvement:' + +C + +static char **collect_args(t_token **tokens, t_env **env) { + t_token *cur; + char **result; + int i; + t_token *next; + + // Count the number of tokens + cur = *tokens; + i = 0; + while (cur != NULL && ++i) + cur = cur->next; + + // Allocate memory for the result array + result = malloc(sizeof(char *) * (i + 1)); + if (result == NULL) { + free_tokens(*tokens); // Free tokens if memory allocation fails + return NULL; + } + + // Collect and format string tokens + cur = *tokens; + i = 0; + while (cur != NULL && cur->type == STRING_TOKEN) { + next = cur->next; + if (cur->previous) + free_token(cur->previous); // Free the previous token + + result[i] = format_string(cur->content.string, *env, 0); + if (result[i] == NULL) { // Check if format_string failed + free_tokens(next); // Free remaining tokens + while (i > 0) { + free(result[--i]); + } + free(result); + return NULL; + } + + i++; + if (cur->next == NULL) + free_token(cur); // Free the current token if it's the last one + cur = next; + } + result[i] = NULL; + return result; +} +Improvements and Error Handling +Error Handling: Improved error handling by checking if format_string failed and freeing the remaining tokens and allocated memory. +Memory Allocation Check: Ensured that memory allocation failures for result and format_string are handled. +Freeing Tokens: Ensured that tokens are properly freed, including the current token if it's the last one. +Example Input: "a" +For the input "a", the function should create a single argument "a" and ensure that all tokens are freed. + +Updated Function +Here’s the updated function with the improvements: + +C' + +static char **collect_args(t_token **tokens, t_env **env) { + t_token *cur; + char **result; + int i; + t_token *next; + + // Count the number of tokens + cur = *tokens; + i = 0; + while (cur != NULL && ++i) + cur = cur->next; + + // Allocate memory for the result array + result = malloc(sizeof(char *) * (i + 1)); + if (result == NULL) { + free_tokens(*tokens); // Free tokens if memory allocation fails + return NULL; + } + + // Collect and format string tokens + cur = *tokens; + i = 0; + while (cur != NULL && cur->type == STRING_TOKEN) { + next = cur->next; + if (cur->previous) + free_token(cur->previous); // Free the previous token + + result[i] = format_string(cur->content.string, *env, 0); + if (result[i] == NULL) { // Check if format_string failed + free_tokens(next); // Free remaining tokens + while (i > 0) { + free(result[--i]); + } + free(result); + return NULL; + } + + i++; + if (cur->next == NULL) + free_token(cur); // Free the current token if it's the last one + cur = next; + } + result[i] = NULL; + return result; +} + + +tendenziel sollte in collect arg free token until + +parse ist das erste was nach token passiert und da wird token zu node umgewandelt \ No newline at end of file