From 70c9c5aac1b197b00fc19dea0fb44762b2434f31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl?= Date: Sun, 31 Mar 2024 20:09:19 +0200 Subject: [PATCH] Changing the parsing to be traited in string and not in char --- Makefile | 6 +- includes/minishell.h | 7 +- libft/ft_split.c | 93 +++++++++++++++++++ libft/{get_next_line.h => ft_strcmp.c} | 42 +++++---- libft/get_next_line.c | 78 ---------------- libft/get_next_line_utils.c | 119 ------------------------- libft/main.c | 20 ----- sources/main.c | 95 +++++++++----------- 8 files changed, 161 insertions(+), 299 deletions(-) create mode 100644 libft/ft_split.c rename libft/{get_next_line.h => ft_strcmp.c} (50%) delete mode 100644 libft/get_next_line.c delete mode 100644 libft/get_next_line_utils.c delete mode 100644 libft/main.c diff --git a/Makefile b/Makefile index 109be384..09af8325 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2024/03/29 11:45:30 by rparodi ### ########.fr # +# Updated: 2024/03/31 19:52:25 by rparodi ### ########.fr # # # # **************************************************************************** # @@ -26,8 +26,8 @@ RM = rm -rf CFLAGS = -Werror -Wextra -Wall -Wno-unused-command-line-argument -g3 -MMD -lreadline # Sources -LIB = ./libft/get_next_line.c \ - ./libft/get_next_line_utils.c +LIB = ./libft/ft_split.c \ + ./libft/ft_strcmp.c SRC = ./sources/ft_exit.c\ ./sources/main.c diff --git a/includes/minishell.h b/includes/minishell.h index 67605f19..d18a8124 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 14:41:15 by rparodi #+# #+# */ -/* Updated: 2024/03/29 14:46:30 by rparodi ### ########.fr */ +/* Updated: 2024/03/31 19:52:56 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,6 @@ #define MINISHELL_H # include "./type_rust.h" -# include "../libft/get_next_line.h" # include # include @@ -28,7 +27,9 @@ # include # include -i32 main(void); +i32 main(void); +i32 ft_strcmp(const char *s1, const char *s2); void ft_exit(i8 *str, u8 exit_status); +i8 **ft_split(i8 const *s, i8 c); #endif diff --git a/libft/ft_split.c b/libft/ft_split.c new file mode 100644 index 00000000..0454e507 --- /dev/null +++ b/libft/ft_split.c @@ -0,0 +1,93 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_split.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/09 13:56:02 by rparodi #+# #+# */ +/* Updated: 2024/03/31 19:35:26 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/minishell.h" + +static int count_words(const char *str, char sep) +{ + int i; + int count; + + i = 0; + count = 0; + while (str[i]) + { + while (str[i] == sep && str[i]) + i++; + if (str[i] != sep && str[i]) + { + while (str[i] != sep && str[i]) + i++; + count++; + } + } + return (count); +} + +static char *ft_strndup(const char *s, int j) +{ + int i; + char *str; + + i = 0; + str = (char *)malloc((j + 1)); + if (!str) + return (NULL); + while (s[i] && i < j) + { + str[i] = s[i]; + i++; + } + str[i] = '\0'; + return (str); +} + +static char **ext_w(char **words_array, const char *str, char sep, int size) +{ + int i; + int j; + + i = 0; + j = 0; + while (j < size) + { + while (str[i] == sep && str[i]) + i++; + str = str + i; + i = 0; + while (str[i] != sep && str[i]) + i++; + words_array[j++] = ft_strndup(str, i); + str = str + i; + i = 0; + } + words_array[j] = 0; + return (words_array); +} + +char **ft_split(char const *s, char c) +{ + int size; + char **words_array; + + size = count_words(s, c); + words_array = (char **)malloc(sizeof(char *) * (size + 1)); + if (!words_array) + return (NULL); + if (size == 0) + { + words_array[0] = NULL; + return (words_array); + } + words_array = ext_w(words_array, s, c, size); + return (words_array); +} diff --git a/libft/get_next_line.h b/libft/ft_strcmp.c similarity index 50% rename from libft/get_next_line.h rename to libft/ft_strcmp.c index 3f76342e..92835be9 100644 --- a/libft/get_next_line.h +++ b/libft/ft_strcmp.c @@ -1,33 +1,31 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* get_next_line.h :+: :+: :+: */ +/* ft_strncmp.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/18 17:12:04 by rparodi #+# #+# */ -/* Updated: 2023/11/23 18:15:34 by rparodi ### ########.fr */ +/* Created: 2023/11/07 16:56:56 by rparodi #+# #+# */ +/* Updated: 2024/03/31 19:51:17 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef GET_NEXT_LINE_H -# define GET_NEXT_LINE_H -# ifndef BUFFER_SIZE -# define BUFFER_SIZE 420 -# endif +#include "../includes/minishell.h" -# include -# include +int ft_strcmp(const char *s1, const char *s2) +{ + size_t i; + int diff; -char *ft_get_line(char *str); -char *get_next_line(int fd); -int ft_strchr(char *str, char c); -char *ft_get_next(char *str); -char *ft_get_line(char *str); -char *ft_strjoin(char *s1, char *s2, int i, int j); -size_t ft_strlen(char const *str); -char *file_converted(int fd); -char *ft_free(char *str); -char *ft_check(char *memory, int fd); - -#endif + i = 0; + while ((s1[i] || s2[i])) + { + if (s1[i] != s2[i]) + { + diff = (unsigned char)s1[i] - (unsigned char)s2[i]; + return (diff); + } + i++; + } + return (0); +} diff --git a/libft/get_next_line.c b/libft/get_next_line.c deleted file mode 100644 index b4c2928d..00000000 --- a/libft/get_next_line.c +++ /dev/null @@ -1,78 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* get_next_line.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rparodi +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/18 17:12:02 by rparodi #+# #+# */ -/* Updated: 2023/11/22 13:31:13 by rparodi ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "get_next_line.h" - -char *ft_check(char *memory, int fd) -{ - char *buffer; - int bytescopy; - - bytescopy = 1; - buffer = (char *)malloc(sizeof(char) * (BUFFER_SIZE + 1)); - if (!buffer) - return (NULL); - while (ft_strchr(memory, '\n') <= 0 && bytescopy) - { - bytescopy = read(fd, buffer, BUFFER_SIZE); - if (bytescopy == -1) - return (ft_free(buffer)); - buffer[bytescopy] = '\0'; - memory = ft_strjoin(memory, buffer, 0, -1); - } - free(buffer); - return (memory); -} - -char *file_converted(int fd) -{ - size_t i; - int read_bits; - char *str; - char c; - - i = 0; - c = 1; - read_bits = 1; - str = (char *)malloc(sizeof(char) * BUFFER_SIZE); - if (!str) - return (NULL); - while (read_bits != 0 && i < BUFFER_SIZE) - { - read_bits = read(fd, &c, 1); - str[i] = c; - i++; - } - str[i] = '\0'; - return (str); -} - -char *ft_free(char *str) -{ - free(str); - return (NULL); -} - -char *get_next_line(int fd) -{ - static char *memory = NULL; - char *line; - - if (fd < 0 || BUFFER_SIZE <= 0) - return (NULL); - memory = ft_check(memory, fd); - if (!memory) - return (ft_free(memory)); - line = ft_get_line(memory); - memory = ft_get_next(memory); - return (line); -} diff --git a/libft/get_next_line_utils.c b/libft/get_next_line_utils.c deleted file mode 100644 index b4ad6c49..00000000 --- a/libft/get_next_line_utils.c +++ /dev/null @@ -1,119 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* get_next_line_utils.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: rparodi +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/11/18 17:14:47 by rparodi #+# #+# */ -/* Updated: 2023/11/22 13:35:03 by rparodi ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "get_next_line.h" - -size_t ft_strlen(char const *str) -{ - size_t i; - - i = 0; - if (str) - { - while (str[i]) - i++; - } - return (i); -} - -char *ft_strjoin(char *s1, char *s2, int i, int j) -{ - char *temp; - - if (!s1) - { - s1 = (char *)malloc(1); - s1[i] = '\0'; - } - if (!s2 || !s1) - return (NULL); - temp = (char *)malloc((ft_strlen(s1) + ft_strlen(s2) + 1)); - if (!temp) - return (NULL); - while (s1[i]) - { - temp[i] = s1[i]; - i++; - } - while (s2[++j]) - temp[i + j] = s2[j]; - temp[i + j] = '\0'; - free(s1); - return (temp); -} - -char *ft_get_line(char *str) -{ - char *temp; - int i; - - i = 0; - if (!str[i]) - return (NULL); - while (str[i] && str[i] != '\n') - i++; - temp = (char *)malloc((i + 2)); - if (!temp) - return (NULL); - i = 0; - while (str[i] && str[i] != '\n') - { - temp[i] = str[i]; - i++; - } - if (str[i] == '\n') - { - temp[i] = str[i]; - i++; - } - temp[i] = '\0'; - return (temp); -} - -char *ft_get_next(char *str) -{ - char *temp; - int i; - int j; - - i = 0; - while (str[i] && str[i] != '\n') - i++; - if (!str[i]) - return (ft_free(str)); - temp = (char *)malloc((ft_strlen(str) - i)); - if (!temp) - return (NULL); - j = i + 1; - while (str[++i]) - temp[i - j] = str[i]; - temp[i - j] = '\0'; - free(str); - return (temp); -} - -int ft_strchr(char *str, char c) -{ - int i; - - i = 0; - if (str) - { - while (str[i]) - { - if (str[i] == c) - return (i); - i++; - } - } - return (-1); -} diff --git a/libft/main.c b/libft/main.c deleted file mode 100644 index 88ea8631..00000000 --- a/libft/main.c +++ /dev/null @@ -1,20 +0,0 @@ -#include "get_next_line.h" -#include -#include - -int main(void) -{ - int fd = open("get_next_line.c", O_RDONLY); - int i = 0; - char *next_line = NULL; - while (i < 10000000) - { - next_line = get_next_line(fd); - if (!next_line) - return (printf("next_line[%d] = %s\n", i, next_line)); - printf("next_line[%d] = %s", i, next_line); - free(next_line); - i++; - } - return (0); -} diff --git a/sources/main.c b/sources/main.c index e1ce5bfd..8785b967 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,79 +6,66 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */ -/* Updated: 2024/03/29 14:58:04 by rparodi ### ########.fr */ +/* Updated: 2024/03/31 20:08:28 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ #include "../includes/minishell.h" -i32 ft_check_type_cmd(i8 *cmd) +i32 ft_check_type_cmd(i8 **cmd) { - usize i; + usize j; - i = 0; - while (cmd[i] != '\0') + j = 0; + while (cmd[j]) { - if (cmd[i] == '>') - { - if (cmd[++i] == '>') - printf("Have to redirect at the end of the file after\n"); - else if (cmd[i] == '&') - printf("Have to redirect the stdout in the file\n"); - else - printf("Have to redirect in the file\n"); - } - else if (cmd[i] == '<') - { - if (cmd[++i] == '<') - printf("Have to redirect at the end of the file before\n"); - else if (cmd[i] == '&') - printf("Have to redirect the stdin in the file\n"); - else - printf("Have to redirect in the file\n"); - } - else if (cmd[i] == ';') + if (ft_strcmp(cmd[j], ">") == 0) + printf("Have to redirect in the file\n"); + else if (ft_strcmp(cmd[j], ">>") == 0) + printf("Have to redirect at the end of the file after\n"); + else if (ft_strcmp(cmd[j], ">&") == 0) + printf("Have to redirect the stdout in the file\n"); + else if (ft_strcmp(cmd[j], "<") == 0) + printf("Have to redirect at the end of the file before\n"); + else if (ft_strcmp(cmd[j], "<<") == 0) + printf("Have to redirect at the end of the file after\n"); + else if (ft_strcmp(cmd[j], "<&") == 0) + printf("Have to redirect the stdout in the file\n"); + else if (ft_strcmp(cmd[j], ";") == 0) printf("Have to execute one more command\n"); - else if (cmd[i] == '"') - { - i++; - while (cmd[i] != '\0') - { - if (cmd[i] == '"') - { - printf("Quote span found !\n"); - break ; - } - i++; - } - if (cmd[i] == '\0') - printf("Quote span not found !\n"); - } - else if (cmd[i] == '|') - { - if (cmd[++i] == '|') - printf("Or something\n"); - else - printf("I have to pipe a cmd !\n"); - - } + else if (ft_strcmp(cmd[j], ";") == 0) + printf("Have to execute one more command\n"); + else if (ft_strcmp(cmd[j], "|") == 0) + printf("I have to pipe a cmd !\n"); + else if (ft_strcmp(cmd[j], "||") == 0) + printf("Or something\n"); + else if (ft_strcmp(cmd[j], "&&") == 0) + printf("Only if the first has exit status 0\n"); + else if (ft_strcmp(cmd[j], "&") == 0) + printf("Parreil mais chelou\n"); else printf("Error!\n"); - i++; + j++; } return (1); } void ft_take_cmd(void) { - i32 i = 0; + i32 i; i8 *user_input = NULL; - while (i < 10000000) - { + i8 **args = NULL; + + i = 0; + while (i < 10000000) + { user_input = readline("shcat > "); - if (!user_input || strcmp("exit", user_input) == 0) - ft_exit(user_input, 0); - ft_check_type_cmd(user_input); + if (!user_input || strcmp("exit", user_input) == 0) + ft_exit(user_input, 0); + args = ft_split(user_input, ' '); + if (!user_input) + exit(1); + ft_check_type_cmd(args); add_history(user_input); free(user_input); i++;