From 0683ca56caac8d8c939b0642228154c69e4c77ce Mon Sep 17 00:00:00 2001 From: Mees Dekker Date: Thu, 13 Jul 2023 18:03:58 +0200 Subject: [PATCH 01/91] =?UTF-8?q?=F0=9F=94=A7=20chore(Makefile):=20update?= =?UTF-8?q?=20SRC=20variable=20to=20include=20'lists/init'=20file=20?= =?UTF-8?q?=F0=9F=93=9D=20docs(enum.h):=20add=20enum=20for=20different=20t?= =?UTF-8?q?ypes=20of=20tokens?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🔧 fix(minishell.h): update last modified timestamp in file header 🔧 fix(minishell.h): update last modified timestamp in file header 🔧 fix(structs.h): update last modified timestamp in file header ✨ feat(minishell.h): add support for bool type ✨ feat(minishell.h): add support for unistd.h library ✨ feat(minishell.h): add support for list_append function ✨ feat(minishell.h): add support for list_insert function ✨ feat(structs.h): add support for t_token struct ✨ feat(structs.h): add support for t_token type field ✨ feat(structs.h): add support for t_token value field ✨ feat(structs.h): add support for t_token prev field ✨ feat(structs.h): add support for t_token next field 📝 docs(init.c): add documentation for list_append and list_insert functions --- Makefile | 2 +- includes/enum.h | 45 ++++++++++++++++++ includes/minishell.h | 12 ++++- includes/structs.h | 22 +++++++-- src/lists/init.c | 110 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 183 insertions(+), 8 deletions(-) create mode 100644 includes/enum.h create mode 100644 src/lists/init.c diff --git a/Makefile b/Makefile index b081c4d..32c7f01 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = minishell -SRC = main +SRC = main lists/init SRCS = $(addsuffix .c, $(addprefix src/, $(SRC))) OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a diff --git a/includes/enum.h b/includes/enum.h new file mode 100644 index 0000000..7729e50 --- /dev/null +++ b/includes/enum.h @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* enum.h :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/07/13 17:41:00 by mdekker/jde #+# #+# */ +/* Updated: 2023/07/13 17:42:54 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#ifndef ENUM_H +# define ENUM_H + +/** + * @brief The enum for the different types of tokens + * + * @param DOUBLE_QUOTE (") + * @param SINGLE_QUOTE (') + * @param SPACE ( ) + * @param PIPE (|) + * @param SEMICOLON (;) + * @param REDIRECT (< >) + * @param END (\0) + * @param ENV ($) + * @param ENV_VAR The name of the environment variable + * @param CHAR Any other character + */ +typedef enum e_types +{ + DOUBLE_QUOTE, + SINGLE_QUOTE, + SPACE, + PIPE, + SEMICOLON, + REDIRECT, + END, + ENV, + ENV_VAR, + CHAR, + +} t_types; + +#endif diff --git a/includes/minishell.h b/includes/minishell.h index 73963ae..4f895cb 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/07/12 14:21:40 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/13 18:00:44 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,10 +14,18 @@ # define MINISHELL_H # include -# include # include # include +# include # include # include +# include +# include + +/* + Lists +*/ +t_token *list_append(t_token *head, char *str, t_types type); +t_token *list_insert(t_token *head, char *str, t_types type, size_t index); #endif diff --git a/includes/structs.h b/includes/structs.h index e4b6b23..a751f62 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,17 +6,29 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/07/12 14:11:33 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/13 18:02:52 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #ifndef STRUCTS_H # define STRUCTS_H -typedef struct s_env +# include + +/** + * @brief The struct for the tokens + * + * @param type The type of token + * @param value The value of the token + * @param prev The previous token + * @param next The next token + */ +typedef struct s_token { - char *key; - char *value; -} t_env; + t_types type; + char *value; + struct t_token *prev; + struct t_token *next; +} t_token; #endif diff --git a/src/lists/init.c b/src/lists/init.c new file mode 100644 index 0000000..40f59e7 --- /dev/null +++ b/src/lists/init.c @@ -0,0 +1,110 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* init.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/07/13 17:36:56 by mdekker/jde #+# #+# */ +/* Updated: 2023/07/13 17:45:08 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +/** + * @brief Creates a new node + * + * @param value value to put in node + * @return t_token* pointer to new node + */ +static t_token *create_node(char *str, t_types type) +{ + t_token *new; + + new = malloc(sizeof(t_token)); + if (!new) + return (NULL); + new->value = str; + new->type = type; + new->prev = NULL; + new->next = NULL; + return (new); +} + +/** + * @brief links two nodes + * + * @param node node to link to + * @param newnode node to link + * @return t_token* pointer to HEAD + */ +static t_token *link_node(t_token *node, t_token *newnode) +{ + if (node == NULL || newnode == NULL) + return (NULL); + while (node->next != NULL) + node = node->next; + newnode->prev = node; + node->next = newnode; + return (newnode); +} + +/** + * @brief Append a node to the list or create a new list + * + * @param head Pointer to the head of the list or NULL + * @param str String to put in the node + * @return t_token* The head of the list + */ +t_token *list_append(t_token *head, char *str, t_types type) +{ + t_token *new; + + if (head == NULL) + { + head = create_node(str, type); + if (head == NULL) + return (NULL); + } + else + { + new = create_node(str, type); + if (new == NULL) + return (NULL); + if (link_node(head, new) == NULL) + return (NULL); + } + return (head); +} + +/** + * @brief Insert a node at a given index + * + * @param head The list to insert in + * @param str The string to put in the node + * @param type The type of the node + * @param index The index to insert at + * @return t_token* The head of the list + */ +t_token *list_insert(t_token *head, char *str, t_types type, size_t index) +{ + t_token *new; + t_token *tmp; + size_t i; + + new = create_node(str, type); + if (new == NULL) + return (NULL); + tmp = head; + i = 0; + while (i < index && tmp->next != NULL) + { + tmp = tmp->next; + i++; + } + new->next = tmp->next; + new->prev = tmp; + tmp->next = new; + return (head); +} From 5cc22c0e5a378fffbfef2e0e57c30065d5c816b4 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Sat, 15 Jul 2023 15:09:46 +0200 Subject: [PATCH 02/91] =?UTF-8?q?=F0=9F=94=80=20chore(enum.h):=20update=20?= =?UTF-8?q?enum=20values=20and=20comments=20=F0=9F=94=80=20chore(structs.h?= =?UTF-8?q?):=20update=20timestamp=20in=20comments?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/enum.h | 25 +++++++++++++------------ includes/structs.h | 2 +- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/includes/enum.h b/includes/enum.h index 7729e50..777245c 100644 --- a/includes/enum.h +++ b/includes/enum.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/13 17:41:00 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/13 17:42:54 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/15 15:09:31 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,28 +18,29 @@ * * @param DOUBLE_QUOTE (") * @param SINGLE_QUOTE (') - * @param SPACE ( ) * @param PIPE (|) * @param SEMICOLON (;) - * @param REDIRECT (< >) - * @param END (\0) + * @param OR (||) * @param ENV ($) - * @param ENV_VAR The name of the environment variable - * @param CHAR Any other character + * @param DQ_ENV An environment variable in double quotes ("$") + * @param STRING A string + * @param O_REDIRECT (>) + * @param I_REDIRECT (<) + * @param HERE_DOC (<<) */ typedef enum e_types { DOUBLE_QUOTE, SINGLE_QUOTE, - SPACE, PIPE, SEMICOLON, - REDIRECT, - END, + OR, ENV, - ENV_VAR, - CHAR, - + DQ_ENV, + STRING, + O_REDIRECT, + I_REDIRECT, + HERE_DOC } t_types; #endif diff --git a/includes/structs.h b/includes/structs.h index a751f62..edcb3a8 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/07/13 18:02:52 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/15 14:39:06 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ From 4a240dc6662348efe03fcc8741a2efa8055d03b0 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Sat, 15 Jul 2023 22:57:54 +0200 Subject: [PATCH 03/91] =?UTF-8?q?=F0=9F=94=A7=20chore(c=5Fcpp=5Fproperties?= =?UTF-8?q?.json):=20update=20includePath=20for=20Mac=20configuration=20?= =?UTF-8?q?=F0=9F=94=A7=20chore(Makefile):=20update=20INCLUDES=20variable?= =?UTF-8?q?=20to=20include=20additional=20directories=20=F0=9F=94=A7=20cho?= =?UTF-8?q?re(libft):=20update=20libft=20submodule=20commit=20reference=20?= =?UTF-8?q?=F0=9F=94=A7=20chore(src/lexer):=20add=20new=20files=20index.c?= =?UTF-8?q?=20and=20utils.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/c_cpp_properties.json | 3 +-- Makefile | 4 ++-- libft | 2 +- src/lexer/index.c | 0 src/lexer/utils.c | 0 5 files changed, 4 insertions(+), 5 deletions(-) create mode 100644 src/lexer/index.c create mode 100644 src/lexer/utils.c diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 43bb943..2eadf51 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -4,8 +4,7 @@ "name": "Mac", "includePath": [ "${workspaceFolder}/includes", - "${workspaceFolder}/../libft", - "../libft" + "${workspaceFolder}/libft/includes", ], "macFrameworkPath": [ "/Library/Frameworks" diff --git a/Makefile b/Makefile index 225381c..5354401 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ G_FLAGS = -DREADLINE_LIBRARY CODAM_FLAGS = -Wall -Wextra -Werror LIBS = libft/libft.a readline/libreadline.a readline/libhistory.a LINKER = -lncurses -INCLUDES = -I $(CURDIR)/includes -I $(CURDIR)/libft -I $(CURDER)/readline +INCLUDES = -I $(CURDIR)/includes -I $(CURDIR)/libft/includes -I $(CURDER)/readline COLOR_INFO = \033[1;36m COLOR_SUCCESS = \033[1;32m @@ -28,7 +28,7 @@ $(NAME): $(LIBFT) $(READLINE) $(OBJS) @sleep 0.25 @printf "✅\n" -build/%.o: src/%.c includes/minishell.h +build/%.o: src/%.c includes/minishell.h includes/structs.h includes/enum.h @mkdir -p $(@D) @cc $(INCLUDES) $(CODAM_FLAGS) -c $< -o $@ diff --git a/libft b/libft index 9e1d039..60591fe 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 9e1d03934be54944071ce0a5fd08df78910b660d +Subproject commit 60591fe91f3af687d4a2d892931e00d148bc93a3 diff --git a/src/lexer/index.c b/src/lexer/index.c new file mode 100644 index 0000000..e69de29 diff --git a/src/lexer/utils.c b/src/lexer/utils.c new file mode 100644 index 0000000..e69de29 From 61c53f6f05ef1b05810b4058eb9ba8b7726100f1 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Sun, 16 Jul 2023 18:20:24 +0200 Subject: [PATCH 04/91] =?UTF-8?q?=F0=9F=94=A7=20fix(c=5Fcpp=5Fproperties.j?= =?UTF-8?q?son):=20update=20include=20paths=20to=20include=20subdirectorie?= =?UTF-8?q?s=20=F0=9F=94=A7=20fix(Makefile):=20remove=20lists/init=20from?= =?UTF-8?q?=20the=20list=20of=20source=20files=20=F0=9F=94=A7=20fix(includ?= =?UTF-8?q?es/minishell.h):=20update=20timestamp=20=F0=9F=94=A7=20fix(incl?= =?UTF-8?q?udes/structs.h):=20update=20timestamp=20=F0=9F=94=A7=20fix(src/?= =?UTF-8?q?main.c):=20add=20tokenization=20logic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/c_cpp_properties.json | 4 ++-- Makefile | 4 ++-- includes/minishell.h | 2 +- includes/structs.h | 10 ++++----- src/main.c | 40 ++++++++++++++++++++++++++++++++--- 5 files changed, 46 insertions(+), 14 deletions(-) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 2eadf51..cec6ed1 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -3,8 +3,8 @@ { "name": "Mac", "includePath": [ - "${workspaceFolder}/includes", - "${workspaceFolder}/libft/includes", + "${workspaceFolder}/includes/**", + "${workspaceFolder}/libft/includes/**" ], "macFrameworkPath": [ "/Library/Frameworks" diff --git a/Makefile b/Makefile index 5354401..7fc0cc8 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = minishell -SRC = main lists/init check_input +SRC = main check_input SRCS = $(addsuffix .c, $(addprefix src/, $(SRC))) OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a @@ -9,7 +9,7 @@ G_FLAGS = -DREADLINE_LIBRARY CODAM_FLAGS = -Wall -Wextra -Werror LIBS = libft/libft.a readline/libreadline.a readline/libhistory.a LINKER = -lncurses -INCLUDES = -I $(CURDIR)/includes -I $(CURDIR)/libft/includes -I $(CURDER)/readline +INCLUDES = -I $(CURDIR)/includes -I $(CURDIR)/libft -I $(CURDER)/readline COLOR_INFO = \033[1;36m COLOR_SUCCESS = \033[1;32m diff --git a/includes/minishell.h b/includes/minishell.h index bd4bec1..aa2d56c 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/07/15 22:06:11 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/16 01:28:26 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/includes/structs.h b/includes/structs.h index ecd150e..edcda02 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/07/15 22:06:35 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/16 01:35:08 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -24,10 +24,8 @@ */ typedef struct s_token { - t_types type; - char *value; - struct t_token *prev; - struct t_token *next; -} t_token; + t_types type; + char *value; +} t_token; #endif diff --git a/src/main.c b/src/main.c index c320b8c..ca23871 100644 --- a/src/main.c +++ b/src/main.c @@ -6,19 +6,50 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/14 16:17:48 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/16 17:38:30 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include +static void tokenize(char *input, t_vector tokens) +{ + char **tmp; + t_token *token; + + tmp = ft_split(input, ' '); + while (*tmp) + { + printf("tmp: %s\n", *tmp); + token = malloc(sizeof(t_token)); + printf("token: %p\n", token); + token->type = STRING; + token->value = *tmp; + printf("token->value: %s\n", token->value); + printf("tokens.size: %zu\n", tokens.size); + ft_vec_push(&tokens, (void *)token); + printf("tokens.size: %zu\n", tokens.size); + free(*tmp); + tmp++; + } +} + int main(int ac, char **av, char **env) { - char *input; + char *input; + t_vector tokens; + t_token *token; (void)ac; (void)av; (void)env; + ft_vec_init(&tokens, 5, sizeof(t_token)); + // print the address of the vector + printf("tokens: %p\n", &tokens); + // print the vavtor type_size and size + printf("tokens.type_size: %zu\n", tokens.type_size); + printf("tokens.size: %zu\n", tokens.size); + printf("\033[1;32mWelcome to minishell!\033[0m\n"); while (1) { input = readline("\n\033[1;32mminishell$ \033[0m"); @@ -30,7 +61,10 @@ int main(int ac, char **av, char **env) else if (!check_quotes_parantheses(input)) printf("minishell: syntax error: unfinished quote or parantheses\n"); else - printf("input: %s\n", input); + tokenize(input, tokens); + printf("tokens.size: %zu\n", tokens.size); + printf("tokens.type_size: %zu\n", tokens.type_size); + token = ft_vec_get(&tokens, 1); free(input); } return (0); From 5c1cbd5eb0c21cd88cef45d1af181f9095a5ac14 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Mon, 17 Jul 2023 21:34:27 +0200 Subject: [PATCH 05/91] =?UTF-8?q?=F0=9F=94=A7=20fix(minishell.h):=20update?= =?UTF-8?q?=20last=20modified=20timestamp=20=F0=9F=94=A7=20fix(libft):=20u?= =?UTF-8?q?pdate=20libft=20submodule=20commit=20=F0=9F=94=A7=20fix(main.c)?= =?UTF-8?q?:=20fix=20memory=20leaks=20and=20add=20debug=20print=20statemen?= =?UTF-8?q?ts=20=E2=9C=A8=20feat(main.c):=20add=20support=20for=20tokenizi?= =?UTF-8?q?ng=20input=20commands=20in=20minishell?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/minishell.h | 4 ++- libft | 2 +- src/main.c | 58 +++++++++++++++++++++++++++++--------------- 3 files changed, 42 insertions(+), 22 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index aa2d56c..c9782e6 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/07/16 01:28:26 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/17 15:44:32 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -22,6 +22,8 @@ # include # include +# define DEBUG 1 + /* Lists */ diff --git a/libft b/libft index 60591fe..3b434ed 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 60591fe91f3af687d4a2d892931e00d148bc93a3 +Subproject commit 3b434ed9099cb27dea26db07541cc96a9ad97659 diff --git a/src/main.c b/src/main.c index ca23871..2d1b929 100644 --- a/src/main.c +++ b/src/main.c @@ -6,52 +6,71 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/16 17:38:30 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/17 21:05:59 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include +static void print_vec(t_vector *tokens) +{ + t_token *token; + + printf("tokens->used: %zu\n", tokens->used); + for (size_t i = 0; i < tokens->used; i++) + { + token = (t_token *)ft_vec_get(tokens, i); + if (DEBUG) + printf("[%zu]: %s (%i)\n", i, token->value, token->type); + } +} + +static void clear_structs_vec(t_vector *tokens) +{ + t_token *token; + + while (tokens->used > 0) + { + token = (t_token *)ft_vec_pop(tokens); + printf("token->value: %p\n", token->value); + free(token); + } +} + static void tokenize(char *input, t_vector tokens) { char **tmp; t_token *token; + size_t i; + i = 0; tmp = ft_split(input, ' '); - while (*tmp) + while (tmp[i]) { - printf("tmp: %s\n", *tmp); token = malloc(sizeof(t_token)); - printf("token: %p\n", token); - token->type = STRING; - token->value = *tmp; + token->type = DOUBLE_QUOTE; + token->value = tmp[i]; printf("token->value: %s\n", token->value); - printf("tokens.size: %zu\n", tokens.size); ft_vec_push(&tokens, (void *)token); - printf("tokens.size: %zu\n", tokens.size); free(*tmp); - tmp++; + i++; } + print_vec(&tokens); } int main(int ac, char **av, char **env) { char *input; t_vector tokens; - t_token *token; (void)ac; (void)av; (void)env; - ft_vec_init(&tokens, 5, sizeof(t_token)); - // print the address of the vector - printf("tokens: %p\n", &tokens); - // print the vavtor type_size and size - printf("tokens.type_size: %zu\n", tokens.type_size); - printf("tokens.size: %zu\n", tokens.size); - printf("\033[1;32mWelcome to minishell!\033[0m\n"); + tokens = (t_vector){NULL, 0, 0, 0}; while (1) { + if (tokens.data == NULL) + ft_vec_init(&tokens, 5, sizeof(t_token)); input = readline("\n\033[1;32mminishell$ \033[0m"); if (!input) break ; @@ -62,10 +81,9 @@ int main(int ac, char **av, char **env) printf("minishell: syntax error: unfinished quote or parantheses\n"); else tokenize(input, tokens); - printf("tokens.size: %zu\n", tokens.size); - printf("tokens.type_size: %zu\n", tokens.type_size); - token = ft_vec_get(&tokens, 1); free(input); + clear_structs_vec(&tokens); + printf("tokens->used: %zu\n", tokens.used); } return (0); } From e92691102b0e4feafa78b62be06193956874b2b9 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Mon, 17 Jul 2023 22:01:40 +0200 Subject: [PATCH 06/91] =?UTF-8?q?=F0=9F=94=A7=20chore(Makefile):=20fix=20t?= =?UTF-8?q?ypo=20in=20INCLUDES=20variable=20=F0=9F=94=A7=20chore(libft):?= =?UTF-8?q?=20update=20libft=20submodule=20to=20commit=20e940f84ca25690b4f?= =?UTF-8?q?7d9300ed027922dff3e5a4b=20=F0=9F=94=A7=20chore(main.c):=20refac?= =?UTF-8?q?tor=20print=5Fvec=20function=20to=20pretty=5Fprint=5Fvector=20?= =?UTF-8?q?=F0=9F=94=A7=20chore(main.c):=20refactor=20clear=5Fstructs=5Fve?= =?UTF-8?q?c=20function=20to=20clear=5Ftoken=20=F0=9F=94=A7=20chore(main.c?= =?UTF-8?q?):=20update=20print=5Fvec=20function=20to=20pretty=20print=20to?= =?UTF-8?q?kens=20=F0=9F=94=A7=20chore(main.c):=20update=20clear=5Fstructs?= =?UTF-8?q?=5Fvec=20function=20to=20properly=20free=20memory?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- libft | 2 +- src/main.c | 42 +++++++++++++++++++++++------------------- 3 files changed, 25 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 7fc0cc8..b6a66cd 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ G_FLAGS = -DREADLINE_LIBRARY CODAM_FLAGS = -Wall -Wextra -Werror LIBS = libft/libft.a readline/libreadline.a readline/libhistory.a LINKER = -lncurses -INCLUDES = -I $(CURDIR)/includes -I $(CURDIR)/libft -I $(CURDER)/readline +INCLUDES = -I $(CURDIR)/includes -I $(CURDIR)/libft/includes -I $(CURDER)/readline COLOR_INFO = \033[1;36m COLOR_SUCCESS = \033[1;32m diff --git a/libft b/libft index 3b434ed..e940f84 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 3b434ed9099cb27dea26db07541cc96a9ad97659 +Subproject commit e940f84ca25690b4f7d9300ed027922dff3e5a4b diff --git a/src/main.c b/src/main.c index 2d1b929..e2f8a87 100644 --- a/src/main.c +++ b/src/main.c @@ -6,35 +6,38 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/17 21:05:59 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/17 21:52:06 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -static void print_vec(t_vector *tokens) +void pretty_print_vector(t_vector *tokens) { t_token *token; + size_t i; - printf("tokens->used: %zu\n", tokens->used); - for (size_t i = 0; i < tokens->used; i++) + i = 0; + while (i < tokens->size) { token = (t_token *)ft_vec_get(tokens, i); - if (DEBUG) - printf("[%zu]: %s (%i)\n", i, token->value, token->type); + if (token != NULL) + { + printf("Token %zu:\n", i); + printf(" Value: %s\n", token->value); + printf(" Type: %i\n", token->type); + } + i++; } } -static void clear_structs_vec(t_vector *tokens) +static void clear_token(void *data) { t_token *token; - while (tokens->used > 0) - { - token = (t_token *)ft_vec_pop(tokens); - printf("token->value: %p\n", token->value); - free(token); - } + token = (t_token *)data; + free(token->value); + free(token); } static void tokenize(char *input, t_vector tokens) @@ -49,13 +52,15 @@ static void tokenize(char *input, t_vector tokens) { token = malloc(sizeof(t_token)); token->type = DOUBLE_QUOTE; - token->value = tmp[i]; - printf("token->value: %s\n", token->value); + if (tmp[i] != NULL) + token->value = tmp[i]; + else + token->value = ""; ft_vec_push(&tokens, (void *)token); - free(*tmp); i++; } - print_vec(&tokens); + pretty_print_vector(&tokens); + ft_vec_free(&tokens, clear_token); } int main(int ac, char **av, char **env) @@ -82,8 +87,7 @@ int main(int ac, char **av, char **env) else tokenize(input, tokens); free(input); - clear_structs_vec(&tokens); - printf("tokens->used: %zu\n", tokens.used); + tokens = (t_vector){NULL, 0, 0, 0}; } return (0); } From 02dfb7f23060df039683035018ba2c047ba02f92 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Mon, 17 Jul 2023 22:14:34 +0200 Subject: [PATCH 07/91] =?UTF-8?q?=F0=9F=90=9B=20fix(libft):=20update=20sub?= =?UTF-8?q?project=20commit=20hash=20=F0=9F=90=9B=20fix(main.c):=20remove?= =?UTF-8?q?=20unnecessary=20initialization=20of=20tokens=20vector?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libft | 2 +- src/main.c | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/libft b/libft index e940f84..be035d2 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit e940f84ca25690b4f7d9300ed027922dff3e5a4b +Subproject commit be035d294b53db3834346b763c15073be30afefb diff --git a/src/main.c b/src/main.c index e2f8a87..44cb2cd 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/17 21:52:06 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/17 22:10:29 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -56,11 +56,13 @@ static void tokenize(char *input, t_vector tokens) token->value = tmp[i]; else token->value = ""; + printf("Token:\n Value: %s\n Type: %i\n", token->value, token->type); ft_vec_push(&tokens, (void *)token); i++; } pretty_print_vector(&tokens); ft_vec_free(&tokens, clear_token); + ft_vec_init(&tokens, 5, sizeof(t_token)); } int main(int ac, char **av, char **env) @@ -71,11 +73,9 @@ int main(int ac, char **av, char **env) (void)ac; (void)av; (void)env; - tokens = (t_vector){NULL, 0, 0, 0}; + ft_vec_init(&tokens, 1, sizeof(t_token)); while (1) { - if (tokens.data == NULL) - ft_vec_init(&tokens, 5, sizeof(t_token)); input = readline("\n\033[1;32mminishell$ \033[0m"); if (!input) break ; @@ -87,7 +87,6 @@ int main(int ac, char **av, char **env) else tokenize(input, tokens); free(input); - tokens = (t_vector){NULL, 0, 0, 0}; } return (0); } From f5a7657b5321669b2cebdfb0be49165361c1ec2a Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Mon, 17 Jul 2023 22:20:03 +0200 Subject: [PATCH 08/91] =?UTF-8?q?=F0=9F=94=80=20chore(libft):=20update=20s?= =?UTF-8?q?ubproject=20commit=20hash=20=F0=9F=90=9B=20fix(main.c):=20fix?= =?UTF-8?q?=20memory=20leak=20by=20initializing=20tokens=20vector=20before?= =?UTF-8?q?=20each=20iteration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libft | 2 +- src/main.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/libft b/libft index be035d2..314e7ab 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit be035d294b53db3834346b763c15073be30afefb +Subproject commit 314e7abd6709fac0ea7e7afc8c9c30b1a42ac858 diff --git a/src/main.c b/src/main.c index 44cb2cd..b4624a7 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/17 22:10:29 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/17 22:16:32 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -62,7 +62,6 @@ static void tokenize(char *input, t_vector tokens) } pretty_print_vector(&tokens); ft_vec_free(&tokens, clear_token); - ft_vec_init(&tokens, 5, sizeof(t_token)); } int main(int ac, char **av, char **env) @@ -87,6 +86,7 @@ int main(int ac, char **av, char **env) else tokenize(input, tokens); free(input); + ft_vec_init(&tokens, 5, sizeof(t_token)); } return (0); } From 02b94e5e42a53e50a820289823584e35ba53f1d1 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Mon, 17 Jul 2023 22:25:49 +0200 Subject: [PATCH 09/91] =?UTF-8?q?=F0=9F=90=9B=20fix(main.c):=20remove=20un?= =?UTF-8?q?necessary=20printf=20statement=20and=20free=20memory=20in=20tok?= =?UTF-8?q?enize=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/main.c b/src/main.c index b4624a7..530a4e6 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/17 22:16:32 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/17 22:23:47 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -54,12 +54,10 @@ static void tokenize(char *input, t_vector tokens) token->type = DOUBLE_QUOTE; if (tmp[i] != NULL) token->value = tmp[i]; - else - token->value = ""; - printf("Token:\n Value: %s\n Type: %i\n", token->value, token->type); ft_vec_push(&tokens, (void *)token); i++; } + free(tmp); pretty_print_vector(&tokens); ft_vec_free(&tokens, clear_token); } From eb4349f2c799d02cc9f96209c93f3cc02a21ed41 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Mon, 17 Jul 2023 23:17:48 +0200 Subject: [PATCH 10/91] =?UTF-8?q?=F0=9F=93=9D=20chore(launch.json):=20add?= =?UTF-8?q?=20launch=20configuration=20for=20debugging=20=F0=9F=94=84=20ch?= =?UTF-8?q?ore(libft):=20update=20libft=20submodule=20to=20commit=207e3359?= =?UTF-8?q?b=20=F0=9F=90=9B=20fix(main.c):=20fix=20pretty=5Fprint=5Fvector?= =?UTF-8?q?=20function=20formatting=20=F0=9F=90=9B=20fix(main.c):=20fix=20?= =?UTF-8?q?token=20type=20assignment=20in=20tokenize=20function=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(main.c):=20remove=20check=5Fquotes=5Fparanth?= =?UTF-8?q?eses=20function=20call=20in=20main=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 16 ++++++++++++++++ libft | 2 +- src/main.c | 29 ++++++++++++++++++----------- 3 files changed, 35 insertions(+), 12 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..30b9a33 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug", + "program": "${workspaceFolder}/minishell", + "args": ["hey ther"], + "cwd": "${workspaceFolder}" + } + ] +} diff --git a/libft b/libft index 314e7ab..7e3359b 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 314e7abd6709fac0ea7e7afc8c9c30b1a42ac858 +Subproject commit 7e3359bb0f06555276c0f73527883b8dd8ea957d diff --git a/src/main.c b/src/main.c index 530a4e6..9ed8489 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/17 22:23:47 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/17 23:09:34 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,9 +23,13 @@ void pretty_print_vector(t_vector *tokens) token = (t_token *)ft_vec_get(tokens, i); if (token != NULL) { - printf("Token %zu:\n", i); - printf(" Value: %s\n", token->value); - printf(" Type: %i\n", token->type); + printf("\033[1;34m"); + printf("┌─────────────────────────────\n"); + printf("│ Token %zu: \n", i); + printf("│ Value: %s \n", token->value); + printf("│ Type: %i \n", token->type); + printf("└─────────────────────────────\n"); + printf("\033[0m"); } i++; } @@ -52,8 +56,9 @@ static void tokenize(char *input, t_vector tokens) { token = malloc(sizeof(t_token)); token->type = DOUBLE_QUOTE; - if (tmp[i] != NULL) - token->value = tmp[i]; + if (i % 2) + token->type = SINGLE_QUOTE; + token->value = tmp[i]; ft_vec_push(&tokens, (void *)token); i++; } @@ -67,10 +72,14 @@ int main(int ac, char **av, char **env) char *input; t_vector tokens; - (void)ac; - (void)av; (void)env; - ft_vec_init(&tokens, 1, sizeof(t_token)); + ft_vec_init(&tokens, 5, sizeof(t_token)); + if (ac == 2) + { + tokenize(av[1], tokens); + ft_vec_free(&tokens, clear_token); + return (0); + } while (1) { input = readline("\n\033[1;32mminishell$ \033[0m"); @@ -79,8 +88,6 @@ int main(int ac, char **av, char **env) add_history(input); if (mini_strcmp(input, "exit")) return (free(input), 0); - else if (!check_quotes_parantheses(input)) - printf("minishell: syntax error: unfinished quote or parantheses\n"); else tokenize(input, tokens); free(input); From 3af23d3bcdd987b7159e3ace97f5c8cebdf65cdc Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Mon, 17 Jul 2023 23:42:23 +0200 Subject: [PATCH 11/91] =?UTF-8?q?=F0=9F=90=9B=20fix(launch.json):=20update?= =?UTF-8?q?=20launch=20arguments=20to=20a=20longer=20string=20=F0=9F=90=9B?= =?UTF-8?q?=20fix(main.c):=20fix=20comparison=20function=20from=20mini=5Fs?= =?UTF-8?q?trcmp=20to=20ft=5Fstrcmp=20=E2=9C=A8=20feat(main.c):=20improve?= =?UTF-8?q?=20pretty=20printing=20of=20tokens=20in=20the=20vector?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 2 +- src/main.c | 17 ++++++++--------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 30b9a33..4578ddb 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "request": "launch", "name": "Debug", "program": "${workspaceFolder}/minishell", - "args": ["hey ther"], + "args": ["hey there delilah whats it like in new york city I'm a thousand miles away."], "cwd": "${workspaceFolder}" } ] diff --git a/src/main.c b/src/main.c index 9ed8489..89c3474 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/17 23:09:34 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/17 23:41:49 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,12 +23,12 @@ void pretty_print_vector(t_vector *tokens) token = (t_token *)ft_vec_get(tokens, i); if (token != NULL) { - printf("\033[1;34m"); - printf("┌─────────────────────────────\n"); - printf("│ Token %zu: \n", i); - printf("│ Value: %s \n", token->value); - printf("│ Type: %i \n", token->type); - printf("└─────────────────────────────\n"); + printf("\033[1;32m●\n"); + printf("\033[1;34m│\n"); + printf("├── Token %zu:\n", i); + printf("│ ├── Value: %s\n", token->value); + printf("│ └── Type: %i\n", token->type); + printf("\033[1;34m│\n"); printf("\033[0m"); } i++; @@ -77,7 +77,6 @@ int main(int ac, char **av, char **env) if (ac == 2) { tokenize(av[1], tokens); - ft_vec_free(&tokens, clear_token); return (0); } while (1) @@ -86,7 +85,7 @@ int main(int ac, char **av, char **env) if (!input) break ; add_history(input); - if (mini_strcmp(input, "exit")) + if (!ft_strcmp(input, "exit")) return (free(input), 0); else tokenize(input, tokens); From ed49ebdba6fc362e501336496a320e1742248709 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Mon, 17 Jul 2023 23:55:11 +0200 Subject: [PATCH 12/91] =?UTF-8?q?=F0=9F=94=84=20chore(libft):=20update=20s?= =?UTF-8?q?ubproject=20commit=20hash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libft | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libft b/libft index 7e3359b..716b402 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 7e3359bb0f06555276c0f73527883b8dd8ea957d +Subproject commit 716b40269e6104a3cf1400824bbd25444ae7c96f From 761a2cceb90e5b4494eed73a198a50252f8622fc Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Tue, 18 Jul 2023 00:20:28 +0200 Subject: [PATCH 13/91] =?UTF-8?q?=F0=9F=94=84=20chore(libft):=20update=20s?= =?UTF-8?q?ubproject=20commit=20hash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libft | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libft b/libft index 716b402..40a592d 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 716b40269e6104a3cf1400824bbd25444ae7c96f +Subproject commit 40a592dad2fb0ed5fdb0f1a772f56c7a5159918c From 43b3dbcb332b389e8e949f0e574ac3c7e3046f1c Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Tue, 18 Jul 2023 00:43:34 +0200 Subject: [PATCH 14/91] =?UTF-8?q?=F0=9F=94=84=20chore(libft):=20update=20s?= =?UTF-8?q?ubproject=20commit=20hash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libft | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libft b/libft index 40a592d..6a0755d 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 40a592dad2fb0ed5fdb0f1a772f56c7a5159918c +Subproject commit 6a0755dbfdec104284a4413d6da84e8b4aca864a From b731b035aa264758f64fc2b4e3c4f57f73d33ec0 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Tue, 18 Jul 2023 00:52:50 +0200 Subject: [PATCH 15/91] =?UTF-8?q?=F0=9F=90=9B=20fix(main.c):=20fix=20typo?= =?UTF-8?q?=20in=20variable=20name=20'lenght'=20to=20'length'=20in=20prett?= =?UTF-8?q?y=5Fprint=5Fvector=20function=20=F0=9F=90=9B=20fix(main.c):=20f?= =?UTF-8?q?ix=20memory=20leak=20by=20freeing=20token=20in=20clear=5Ftoken?= =?UTF-8?q?=20function=20=F0=9F=90=9B=20fix(main.c):=20fix=20incorrect=20a?= =?UTF-8?q?ssignment=20of=20token=20value=20in=20tokenize=20function=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(main.c):=20fix=20incorrect=20push=20of=20tok?= =?UTF-8?q?en=20address=20in=20tokenize=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main.c b/src/main.c index 89c3474..5776911 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/17 23:41:49 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/18 00:50:24 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ void pretty_print_vector(t_vector *tokens) size_t i; i = 0; - while (i < tokens->size) + while (i < tokens->lenght) { token = (t_token *)ft_vec_get(tokens, i); if (token != NULL) @@ -28,6 +28,7 @@ void pretty_print_vector(t_vector *tokens) printf("├── Token %zu:\n", i); printf("│ ├── Value: %s\n", token->value); printf("│ └── Type: %i\n", token->type); + printf("│ └── Adress: %p\n", token); printf("\033[1;34m│\n"); printf("\033[0m"); } @@ -47,19 +48,18 @@ static void clear_token(void *data) static void tokenize(char *input, t_vector tokens) { char **tmp; - t_token *token; + t_token token; size_t i; i = 0; tmp = ft_split(input, ' '); while (tmp[i]) { - token = malloc(sizeof(t_token)); - token->type = DOUBLE_QUOTE; + token.type = DOUBLE_QUOTE; if (i % 2) - token->type = SINGLE_QUOTE; - token->value = tmp[i]; - ft_vec_push(&tokens, (void *)token); + token.type = SINGLE_QUOTE; + token.value = tmp[i]; + ft_vec_push(&tokens, (void *)&token); i++; } free(tmp); From 65a30055c1324843cfe3532281e49a6d67754a08 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Tue, 18 Jul 2023 12:26:41 +0200 Subject: [PATCH 16/91] =?UTF-8?q?=F0=9F=94=84=20chore(libft):=20update=20s?= =?UTF-8?q?ubproject=20commit=20hash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libft | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libft b/libft index 6a0755d..6d612af 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 6a0755dbfdec104284a4413d6da84e8b4aca864a +Subproject commit 6d612af619e4c7ab675a3289ea0c2a51dc14c24b From a54318d36a861ae1e54b36c6b02810cc870518de Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Tue, 18 Jul 2023 12:27:37 +0200 Subject: [PATCH 17/91] =?UTF-8?q?=F0=9F=90=9B=20fix(main.c):=20fix=20inden?= =?UTF-8?q?tation=20in=20pretty=5Fprint=5Fvector=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index 5776911..328be60 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/18 00:50:24 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/18 12:27:28 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -27,7 +27,7 @@ void pretty_print_vector(t_vector *tokens) printf("\033[1;34m│\n"); printf("├── Token %zu:\n", i); printf("│ ├── Value: %s\n", token->value); - printf("│ └── Type: %i\n", token->type); + printf("│ ├── Type: %i\n", token->type); printf("│ └── Adress: %p\n", token); printf("\033[1;34m│\n"); printf("\033[0m"); From 26a5da035cb6086908d9acba31092b1a18fd6089 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Tue, 18 Jul 2023 19:59:10 +0200 Subject: [PATCH 18/91] =?UTF-8?q?=F0=9F=94=A7=20chore(launch.json):=20upda?= =?UTF-8?q?te=20args=20in=20Debug=20configuration=20=F0=9F=93=A6=20chore(m?= =?UTF-8?q?ain.c):=20update=20timestamp=20in=20Updated=20comment=20?= =?UTF-8?q?=F0=9F=94=A7=20chore(print=5Fvector.c):=20add=20new=20file=20pr?= =?UTF-8?q?int=5Fvector.c?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 2 +- src/debug/print_vector.c | 0 src/main.c | 3 +-- 3 files changed, 2 insertions(+), 3 deletions(-) create mode 100644 src/debug/print_vector.c diff --git a/.vscode/launch.json b/.vscode/launch.json index 4578ddb..788a900 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "request": "launch", "name": "Debug", "program": "${workspaceFolder}/minishell", - "args": ["hey there delilah whats it like in new york city I'm a thousand miles away."], + "args": ["hey there delilah whats it like in new york city I'm a thousand miles away. but girl tonight you look so pretty yes you do! Ney york doesn't shine as bright as you. test test test test test test test test test test test test test test test"], "cwd": "${workspaceFolder}" } ] diff --git a/src/debug/print_vector.c b/src/debug/print_vector.c new file mode 100644 index 0000000..e69de29 diff --git a/src/main.c b/src/main.c index 328be60..ae003c5 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/18 12:27:28 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/18 19:58:23 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -42,7 +42,6 @@ static void clear_token(void *data) token = (t_token *)data; free(token->value); - free(token); } static void tokenize(char *input, t_vector tokens) From 28574ac24c367d7f1ece8f67214e4642f1008de9 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Tue, 18 Jul 2023 22:13:14 +0200 Subject: [PATCH 19/91] =?UTF-8?q?=F0=9F=94=80=20chore(libft):=20update=20l?= =?UTF-8?q?ibft=20submodule=20to=20commit=209f4b1373abd91ac57a2fa8785dd683?= =?UTF-8?q?d7c879b8d2=20=F0=9F=90=9B=20fix(main.c):=20fix=20token=20indexi?= =?UTF-8?q?ng=20in=20pretty=5Fprint=5Fvector=20function=20=E2=9C=A8=20feat?= =?UTF-8?q?(main.c):=20add=20capitalize=5Ftoken=20function=20to=20convert?= =?UTF-8?q?=20token=20values=20to=20lowercase?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libft | 2 +- src/main.c | 22 ++++++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/libft b/libft index 6d612af..9f4b137 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 6d612af619e4c7ab675a3289ea0c2a51dc14c24b +Subproject commit 9f4b1373abd91ac57a2fa8785dd683d7c879b8d2 diff --git a/src/main.c b/src/main.c index ae003c5..78549f1 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/18 19:58:23 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/18 21:02:50 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -25,14 +25,13 @@ void pretty_print_vector(t_vector *tokens) { printf("\033[1;32m●\n"); printf("\033[1;34m│\n"); - printf("├── Token %zu:\n", i); + printf("├── Token %zu:\n", i++); printf("│ ├── Value: %s\n", token->value); printf("│ ├── Type: %i\n", token->type); printf("│ └── Adress: %p\n", token); printf("\033[1;34m│\n"); printf("\033[0m"); } - i++; } } @@ -44,6 +43,20 @@ static void clear_token(void *data) free(token->value); } +static void capitalize_token(void *data) +{ + t_token *token; + char *temp; + + token = (t_token *)data; + temp = token->value; + while (*temp) + { + *temp = ft_tolower(*temp); + temp++; + } +} + static void tokenize(char *input, t_vector tokens) { char **tmp; @@ -55,13 +68,14 @@ static void tokenize(char *input, t_vector tokens) while (tmp[i]) { token.type = DOUBLE_QUOTE; - if (i % 2) + if (i % 3) token.type = SINGLE_QUOTE; token.value = tmp[i]; ft_vec_push(&tokens, (void *)&token); i++; } free(tmp); + ft_vec_apply(&tokens, capitalize_token); pretty_print_vector(&tokens); ft_vec_free(&tokens, clear_token); } From 7d9a9e65d7668c7456346fb404037f835cb70090 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Tue, 18 Jul 2023 22:49:23 +0200 Subject: [PATCH 20/91] =?UTF-8?q?=F0=9F=90=9B=20fix(enum.h):=20update=20ti?= =?UTF-8?q?mestamp=20in=20file=20header=20=F0=9F=90=9B=20fix(main.c):=20ch?= =?UTF-8?q?ange=20function=20name=20capitalize=5Ftoken=20to=20uncapitalize?= =?UTF-8?q?=5Ftoken=20=F0=9F=90=9B=20fix(main.c):=20change=20variable=20na?= =?UTF-8?q?me=20tokens=20to=20t=20in=20tokenize=20function=20=F0=9F=90=9B?= =?UTF-8?q?=20fix(main.c):=20change=20variable=20name=20tokens=20to=20t=20?= =?UTF-8?q?in=20main=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/enum.h | 2 +- src/main.c | 24 ++++++++++++------------ 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/includes/enum.h b/includes/enum.h index 777245c..fe10037 100644 --- a/includes/enum.h +++ b/includes/enum.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/13 17:41:00 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/15 15:09:31 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/18 22:45:44 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/main.c b/src/main.c index 78549f1..a5c040c 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/18 21:02:50 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/18 22:47:30 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -43,7 +43,7 @@ static void clear_token(void *data) free(token->value); } -static void capitalize_token(void *data) +static void uncapitalize_token(void *data) { t_token *token; char *temp; @@ -57,7 +57,7 @@ static void capitalize_token(void *data) } } -static void tokenize(char *input, t_vector tokens) +static void tokenize(char *input, t_vector t) { char **tmp; t_token token; @@ -71,25 +71,25 @@ static void tokenize(char *input, t_vector tokens) if (i % 3) token.type = SINGLE_QUOTE; token.value = tmp[i]; - ft_vec_push(&tokens, (void *)&token); + ft_vec_push(&t, (void *)&token); i++; } free(tmp); - ft_vec_apply(&tokens, capitalize_token); - pretty_print_vector(&tokens); - ft_vec_free(&tokens, clear_token); + ft_vec_apply(&t, uncapitalize_token); + pretty_print_vector(&t); + ft_vec_free(&t, clear_token); } int main(int ac, char **av, char **env) { char *input; - t_vector tokens; + t_vector t; (void)env; - ft_vec_init(&tokens, 5, sizeof(t_token)); + ft_vec_init(&t, 5, sizeof(t_token)); if (ac == 2) { - tokenize(av[1], tokens); + tokenize(av[1], t); return (0); } while (1) @@ -101,9 +101,9 @@ int main(int ac, char **av, char **env) if (!ft_strcmp(input, "exit")) return (free(input), 0); else - tokenize(input, tokens); + tokenize(input, t); free(input); - ft_vec_init(&tokens, 5, sizeof(t_token)); + ft_vec_init(&t, 5, sizeof(t_token)); } return (0); } From 9b29e5d67bba63fc71db35f8badc366c3bd74adb Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Tue, 18 Jul 2023 22:49:30 +0200 Subject: [PATCH 21/91] =?UTF-8?q?=F0=9F=90=9B=20fix(main.c):=20fix=20typo?= =?UTF-8?q?=20in=20variable=20name=20't'=20to=20'tokens'=20=E2=9C=A8=20fea?= =?UTF-8?q?t(main.c):=20add=20dynamic=20tokenization=20of=20input=20string?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main.c b/src/main.c index a5c040c..8b5170b 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/18 22:47:30 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/18 22:48:59 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -57,7 +57,7 @@ static void uncapitalize_token(void *data) } } -static void tokenize(char *input, t_vector t) +static void tokenize(char *input, t_vector tokens) { char **tmp; t_token token; @@ -71,25 +71,25 @@ static void tokenize(char *input, t_vector t) if (i % 3) token.type = SINGLE_QUOTE; token.value = tmp[i]; - ft_vec_push(&t, (void *)&token); + ft_vec_push(&tokens, (void *)&token); i++; } free(tmp); - ft_vec_apply(&t, uncapitalize_token); - pretty_print_vector(&t); - ft_vec_free(&t, clear_token); + ft_vec_apply(&tokens, uncapitalize_token); + pretty_print_vector(&tokens); + ft_vec_free(&tokens, clear_token); } int main(int ac, char **av, char **env) { char *input; - t_vector t; + t_vector tokens; (void)env; - ft_vec_init(&t, 5, sizeof(t_token)); + ft_vec_init(&tokens, 5, sizeof(t_token)); if (ac == 2) { - tokenize(av[1], t); + tokenize(av[1], tokens); return (0); } while (1) @@ -101,9 +101,9 @@ int main(int ac, char **av, char **env) if (!ft_strcmp(input, "exit")) return (free(input), 0); else - tokenize(input, t); + tokenize(input, tokens); free(input); - ft_vec_init(&t, 5, sizeof(t_token)); + ft_vec_init(&tokens, 5, sizeof(t_token)); } return (0); } From c9e353a7e8760388eac2b443a6d4e4f424cde944 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Tue, 18 Jul 2023 23:09:14 +0200 Subject: [PATCH 22/91] =?UTF-8?q?=F0=9F=94=80=20chore(structs.h):=20update?= =?UTF-8?q?=20include=20statement=20for=20enum.h=20=F0=9F=94=84=20refactor?= =?UTF-8?q?(structs.h):=20update=20timestamp=20in=20file=20header=20commen?= =?UTF-8?q?t?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/structs.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/structs.h b/includes/structs.h index edcda02..0df4d69 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,14 +6,14 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/07/16 01:35:08 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/18 23:06:38 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #ifndef STRUCTS_H # define STRUCTS_H -# include +# include "enum.h" /** * @brief The struct for the tokens * From 07610ccf69c71a58b1596a81848eabdf7deaf995 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Wed, 19 Jul 2023 20:57:28 +0200 Subject: [PATCH 23/91] =?UTF-8?q?=F0=9F=94=84=20chore(libft):=20update=20s?= =?UTF-8?q?ubproject=20commit=20hash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitmodules | 1 + Makefile | 2 +- src/main.c | 40 ++++++++++++++++++++-------------------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/.gitmodules b/.gitmodules index 79edb3f..e1a0a2c 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,6 +1,7 @@ [submodule "libft"] path = libft url = git@github.com:lithiumox-codam/libft.git + branch = minishell [submodule "builtins"] path = src/builtins url = git@github.com:lithiumox-codam/built-ins.git diff --git a/Makefile b/Makefile index b6a66cd..4390dfb 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ build/%.o: src/%.c includes/minishell.h includes/structs.h includes/enum.h $(LIBFT): @printf "$(COLOR_INFO)$(EMOJI_INFO) Initializing submodules...$(COLOR_RESET)\t" - @git submodule update --init --recursive > /dev/null + # @git submodule update --init --recursive > /dev/null @sleep 0.25 @printf "✅\n" @printf "$(COLOR_INFO)$(EMOJI_INFO) Building Libft...$(COLOR_RESET)\t\t" diff --git a/src/main.c b/src/main.c index 8b5170b..bc139ca 100644 --- a/src/main.c +++ b/src/main.c @@ -6,16 +6,16 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/18 22:48:59 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/19 20:41:39 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -void pretty_print_vector(t_vector *tokens) +void pretty_print_vector(t_vector *tokens) { - t_token *token; - size_t i; + t_token *token; + size_t i; i = 0; while (i < tokens->lenght) @@ -35,18 +35,18 @@ void pretty_print_vector(t_vector *tokens) } } -static void clear_token(void *data) +static void clear_token(void *data) { - t_token *token; + t_token *token; token = (t_token *)data; free(token->value); } -static void uncapitalize_token(void *data) +static void uncapitalize_token(void *data) { - t_token *token; - char *temp; + t_token *token; + char *temp; token = (t_token *)data; temp = token->value; @@ -57,11 +57,11 @@ static void uncapitalize_token(void *data) } } -static void tokenize(char *input, t_vector tokens) +static void tokenize(char *input, t_vector tokens) { - char **tmp; - t_token token; - size_t i; + char **tmp; + t_token token; + size_t i; i = 0; tmp = ft_split(input, ' '); @@ -77,16 +77,16 @@ static void tokenize(char *input, t_vector tokens) free(tmp); ft_vec_apply(&tokens, uncapitalize_token); pretty_print_vector(&tokens); - ft_vec_free(&tokens, clear_token); + ft_vec_free(&tokens, true); } -int main(int ac, char **av, char **env) +int main(int ac, char **av, char **env) { - char *input; - t_vector tokens; + char *input; + t_vector tokens; (void)env; - ft_vec_init(&tokens, 5, sizeof(t_token)); + ft_vec_init(&tokens, 5, sizeof(t_token), &clear_token); if (ac == 2) { tokenize(av[1], tokens); @@ -96,14 +96,14 @@ int main(int ac, char **av, char **env) { input = readline("\n\033[1;32mminishell$ \033[0m"); if (!input) - break ; + break; add_history(input); if (!ft_strcmp(input, "exit")) return (free(input), 0); else tokenize(input, tokens); free(input); - ft_vec_init(&tokens, 5, sizeof(t_token)); + ft_vec_init(&tokens, 5, sizeof(t_token), &clear_token); } return (0); } From e207d9841df47826ca08c9a0896f26a0a1d554c2 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Wed, 19 Jul 2023 21:01:50 +0200 Subject: [PATCH 24/91] =?UTF-8?q?=F0=9F=90=9B=20fix(lexer/index.c):=20fix?= =?UTF-8?q?=20memory=20leak=20in=20create=5Fquote=5Fstring=20function=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(main.c):=20fix=20memory=20leak=20in=20main?= =?UTF-8?q?=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lexer/index.c | 124 ++++++++++++++++++++++++++++++++++++++++++++++ src/main.c | 8 +-- 2 files changed, 128 insertions(+), 4 deletions(-) diff --git a/src/lexer/index.c b/src/lexer/index.c index e69de29..f89376c 100644 --- a/src/lexer/index.c +++ b/src/lexer/index.c @@ -0,0 +1,124 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* index.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/07/19 13:32:55 by mdekker/jde #+# #+# */ +/* Updated: 2023/07/19 19:47:50 by mdekker ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +t_token *create_token(char *value, int type) +{ + t_token *token; + + token = malloc(sizeof(t_token)); + if (!token) + return (NULL); + token->value = value; + token->type = type; + return (token); +} + +static bool check_next_quote(char *str) +{ + char quote; + + quote = *str; + str++; + while (*str != '\0' && *str != quote) + str++; + if (*str == '\0') + return (false); + return (true); +}; +static bool create_quote_string(char *str, size_t *i, t_vector *vec) +{ + size_t occur_left; + size_t occur_right; + char c; + char *value; + c = str[*i]; + if (i != 0) + { + occur_left = (*i) - 1; + while ((str[occur_left] != ' ' || str[occur_left] != '\'' || + str[occur_left != '\"']) && + occur_left > 0) + occur_left--; + value = ft_substr(str, occur_left, (*i) - occur_left + 1); + if (!value) + return (false); + if (!ft_vec_push(vec, (void *)create_token(value, -1))) + return (false); + } + occur_right = (*i)++; + while (str[occur_right] != c && str[occur_right]) + occur_right++; + value = ft_substr(str, *i, occur_right - *i); + if (!value) + return (false); + if (!ft_vec_push(vec, (void *)create_token(value, -1))) + return (false); + *i = occur_right; + return (true); +} + +static bool create_string(char *str, size_t *i, t_vector *vec) +{ + size_t left; + char *value; + + if (*i != 0) + { + left = (*i) - 1; + while (left != 0 && + (str[left] != ' ' && str[left] != '\'' && str[left] != '\"')) + left--; + if (str[left] == ' ' || str[left] == '\'' || str[left] == '\"') + left++; + value = ft_substr(str, left, ((*i) - left)); + if (!value) + return (false); + if (!ft_vec_push(vec, (void *)create_token(value, -1))) + return (false); + } + while (str[*i] == ' ' && str[*i]) + (*i)++; + return (true); +} + +bool lexer(char *input, t_vector *vec) +{ + size_t i; + + if (!input) + return (false); + i = 0; + while (input[i]) + { + if (input[i] == '\"' || input[i] == '\'') + { + if (!check_next_quote(&input[i])) + return (err("malloc", NULL, 1), false); + if (i > 0 && !create_quote_string(input, &i, vec)) + return (err("malloc", NULL, 1), false); + } + else if (input[i] == ' ') + { + if (!create_string(input, &i, vec)) + return (err("malloc", NULL, 1), false); + } + else + { + i++; + } + } + if (!create_string(input, &i, vec)) + return (err("malloc", NULL, 1), false); + return (true); +} diff --git a/src/main.c b/src/main.c index bc139ca..df7a479 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/19 20:41:39 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/19 20:59:23 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -77,7 +77,7 @@ static void tokenize(char *input, t_vector tokens) free(tmp); ft_vec_apply(&tokens, uncapitalize_token); pretty_print_vector(&tokens); - ft_vec_free(&tokens, true); + ft_vec_free(&tokens, clear_token); } int main(int ac, char **av, char **env) @@ -86,7 +86,7 @@ int main(int ac, char **av, char **env) t_vector tokens; (void)env; - ft_vec_init(&tokens, 5, sizeof(t_token), &clear_token); + ft_vec_init(&tokens, 5, sizeof(t_token)); if (ac == 2) { tokenize(av[1], tokens); @@ -103,7 +103,7 @@ int main(int ac, char **av, char **env) else tokenize(input, tokens); free(input); - ft_vec_init(&tokens, 5, sizeof(t_token), &clear_token); + ft_vec_init(&tokens, 5, sizeof(t_token)); } return (0); } From 1a766bb2d9b07b2ae19ef93299d3506f38cda3a3 Mon Sep 17 00:00:00 2001 From: Julius de Baaij Date: Wed, 19 Jul 2023 21:23:19 +0200 Subject: [PATCH 25/91] makefile debugger, header define DEBUG --- Makefile | 8 +++++--- includes/minishell.h | 6 ++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 4390dfb..6725e1b 100644 --- a/Makefile +++ b/Makefile @@ -5,6 +5,8 @@ OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a READLINE = readline/libreadline.a +DEBUG ?= 0 +DEBUG_FLAGS = -g G_FLAGS = -DREADLINE_LIBRARY CODAM_FLAGS = -Wall -Wextra -Werror LIBS = libft/libft.a readline/libreadline.a readline/libhistory.a @@ -24,17 +26,17 @@ all: $(NAME) $(NAME): $(LIBFT) $(READLINE) $(OBJS) @printf "$(COLOR_INFO)$(EMOJI_INFO) Compiling $(NAME)...$(COLOR_RESET)\t" - @cc $(OBJS) $(CODAM_FLAGS) $(LINKER) $(INCLUDES) $(LIBS) -o $@ + @cc $(OBJS) $(CODAM_FLAGS) $(if DEBUG, $(DEBUG_FLAGS)) -DDEBUG=$(DEBUG) $(LINKER) $(INCLUDES) $(LIBS) -o $@ @sleep 0.25 @printf "✅\n" build/%.o: src/%.c includes/minishell.h includes/structs.h includes/enum.h @mkdir -p $(@D) - @cc $(INCLUDES) $(CODAM_FLAGS) -c $< -o $@ + @cc $(INCLUDES) $(CODAM_FLAGS) $(if DEBUG, $(DEBUG_FLAGS)) -DDEBUG=$(DEBUG) -c $< -o $@ $(LIBFT): @printf "$(COLOR_INFO)$(EMOJI_INFO) Initializing submodules...$(COLOR_RESET)\t" - # @git submodule update --init --recursive > /dev/null +# @git submodule update --init --recursive > /dev/null @sleep 0.25 @printf "✅\n" @printf "$(COLOR_INFO)$(EMOJI_INFO) Building Libft...$(COLOR_RESET)\t\t" diff --git a/includes/minishell.h b/includes/minishell.h index 70499de..89d1a5a 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/07/17 15:44:32 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/19 21:08:33 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -22,7 +22,9 @@ # include # include -# define DEBUG 1 +# ifndef DEBUG +# define DEBUG 0 +# endif /* Lists From c677b864cb492b727a01b7aea337778f83917102 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Wed, 19 Jul 2023 21:30:24 +0200 Subject: [PATCH 26/91] =?UTF-8?q?=F0=9F=94=A7=20chore(main.c):=20update=20?= =?UTF-8?q?last=20updated=20timestamp=20in=20file=20header=20=F0=9F=94=A7?= =?UTF-8?q?=20chore(main.c):=20add=20check=5Fleaks=20function=20to=20detec?= =?UTF-8?q?t=20memory=20leaks=20=E2=9C=A8=20feat(main.c):=20add=20debug=20?= =?UTF-8?q?mode=20with=20debug=20information=20and=20memory=20leak=20check?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index df7a479..b428286 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/19 20:59:23 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/19 21:27:53 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -80,12 +80,26 @@ static void tokenize(char *input, t_vector tokens) ft_vec_free(&tokens, clear_token); } +void check_leaks(void) +{ + system("leaks minishell"); +} + int main(int ac, char **av, char **env) { char *input; t_vector tokens; (void)env; + if (DEBUG) + { + atexit(check_leaks); + printf("\033[1;32m●\n"); + printf("\033[1;34m│\n"); + printf("├── Debug mode enabled\n"); + printf("\033[1;34m│\n"); + printf("\033[0m"); + } ft_vec_init(&tokens, 5, sizeof(t_token)); if (ac == 2) { From 5129774614af249521dcb9c4fadd660235592c0f Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Wed, 19 Jul 2023 21:33:59 +0200 Subject: [PATCH 27/91] =?UTF-8?q?=F0=9F=94=84=20chore(libft):=20update=20l?= =?UTF-8?q?ibft=20submodule=20to=20commit=20db80f60?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libft | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libft b/libft index 9f4b137..db80f60 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 9f4b1373abd91ac57a2fa8785dd683d7c879b8d2 +Subproject commit db80f60ac7b1913e4fddb28b5edf3fe6cd9f3fe7 From 4b4cd9c5268d3f2f51668c54702e0234d8303107 Mon Sep 17 00:00:00 2001 From: Julius de Baaij Date: Wed, 19 Jul 2023 22:25:31 +0200 Subject: [PATCH 28/91] =?UTF-8?q?=F0=9F=94=A7=20chore(Makefile):=20add=20n?= =?UTF-8?q?ew=20source=20files=20to=20the=20build=20process=20to=20ensure?= =?UTF-8?q?=20they=20are=20compiled=20=F0=9F=94=A7=20chore(minishell.h):?= =?UTF-8?q?=20update=20the=20last=20modified=20timestamp=20to=20reflect=20?= =?UTF-8?q?recent=20changes=20=F0=9F=94=A7=20chore(libft):=20update=20libf?= =?UTF-8?q?t=20submodule=20to=20the=20latest=20commit=20=F0=9F=94=A7=20cho?= =?UTF-8?q?re(lexer/index.c):=20update=20the=20last=20modified=20timestamp?= =?UTF-8?q?=20to=20reflect=20recent=20changes=20=F0=9F=94=A7=20chore(main.?= =?UTF-8?q?c):=20update=20the=20last=20modified=20timestamp=20to=20reflect?= =?UTF-8?q?=20recent=20changes=20=E2=9C=A8=20feat(utils/error.c):=20add=20?= =?UTF-8?q?new=20error=20handling=20function=20to=20handle=20and=20display?= =?UTF-8?q?=20errors=20in=20a=20formatted=20way?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- includes/minishell.h | 14 +++++++------- libft | 2 +- src/lexer/index.c | 2 +- src/main.c | 11 +++++++++-- src/utils/error.c | 23 +++++++++++++++++++++++ 6 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 src/utils/error.c diff --git a/Makefile b/Makefile index 6725e1b..43d36c2 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = minishell -SRC = main check_input +SRC = main check_input lexer/index utils/error SRCS = $(addsuffix .c, $(addprefix src/, $(SRC))) OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a diff --git a/includes/minishell.h b/includes/minishell.h index 89d1a5a..0a5a397 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/07/19 21:08:33 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/19 21:52:29 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -26,13 +26,13 @@ # define DEBUG 0 # endif -/* - Lists -*/ -t_token *list_append(t_token *head, char *str, t_types type); -t_token *list_insert(t_token *head, char *str, t_types type, size_t index); - /* input_check */ bool check_quotes_parantheses(char *input); +bool lexer(char *input, t_vector *vec); + + + +/* utils */ +void err(char *err, char *cmd, int exit_code); #endif diff --git a/libft b/libft index db80f60..f39c12e 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit db80f60ac7b1913e4fddb28b5edf3fe6cd9f3fe7 +Subproject commit f39c12e2837eef2658543b32eb9f86b8ab956ce9 diff --git a/src/lexer/index.c b/src/lexer/index.c index f89376c..d811871 100644 --- a/src/lexer/index.c +++ b/src/lexer/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 13:32:55 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/19 19:47:50 by mdekker ######## odam.nl */ +/* Updated: 2023/07/19 21:59:55 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/main.c b/src/main.c index b428286..6794f79 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/19 21:27:53 by mdekker ######## odam.nl */ +/* Updated: 2023/07/19 22:01:20 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -103,7 +103,14 @@ int main(int ac, char **av, char **env) ft_vec_init(&tokens, 5, sizeof(t_token)); if (ac == 2) { - tokenize(av[1], tokens); + // tokenize(av[1], tokens); + if(!lexer(av[1], &tokens)) + { + printf("lexer error"); + return (1); + } + pretty_print_vector(&tokens); + ft_vec_free(&tokens, clear_token); return (0); } while (1) diff --git a/src/utils/error.c b/src/utils/error.c new file mode 100644 index 0000000..21bfda0 --- /dev/null +++ b/src/utils/error.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* error.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/07/19 21:42:24 by mdekker/jde #+# #+# */ +/* Updated: 2023/07/19 21:42:31 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +void err(char *err, char *cmd, int exit_code) +{ + printf("\033[1;31m"); + printf("❗️ Error: %s\n", err); + if (cmd) + printf("In command: %s\n", cmd); + printf("Exit code: %d\n", exit_code); + printf("\033[0m"); +} From f7505637bea1921bd9db9259207c9fa3c47799d1 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Wed, 19 Jul 2023 23:25:14 +0200 Subject: [PATCH 29/91] =?UTF-8?q?=F0=9F=94=A8=20chore(libft):=20update=20l?= =?UTF-8?q?ibft=20submodule=20to=20commit=201e79ca0e5fccbe5157bc9e788876f5?= =?UTF-8?q?f6ae8b716c=20=F0=9F=94=A8=20chore(main.c):=20comment=20out=20un?= =?UTF-8?q?capitalize=5Ftoken=20and=20tokenize=20functions=20=F0=9F=94=A8?= =?UTF-8?q?=20chore(main.c):=20comment=20out=20tokenize=20function=20call?= =?UTF-8?q?=20and=20add=20error=20message=20for=20lexer=20error=20?= =?UTF-8?q?=F0=9F=94=A8=20chore(main.c):=20free=20tokens=20vector=20and=20?= =?UTF-8?q?reinitialize=20it=20after=20each=20input?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libft | 2 +- src/main.c | 77 +++++++++++++++++++++++++++++------------------------- 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/libft b/libft index f39c12e..1e79ca0 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit f39c12e2837eef2658543b32eb9f86b8ab956ce9 +Subproject commit 1e79ca0e5fccbe5157bc9e788876f5f6ae8b716c diff --git a/src/main.c b/src/main.c index 6794f79..48d1fd8 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/19 22:01:20 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/19 22:38:02 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -43,42 +43,42 @@ static void clear_token(void *data) free(token->value); } -static void uncapitalize_token(void *data) -{ - t_token *token; - char *temp; +// static void uncapitalize_token(void *data) +// { +// t_token *token; +// char *temp; - token = (t_token *)data; - temp = token->value; - while (*temp) - { - *temp = ft_tolower(*temp); - temp++; - } -} +// token = (t_token *)data; +// temp = token->value; +// while (*temp) +// { +// *temp = ft_tolower(*temp); +// temp++; +// } +// } -static void tokenize(char *input, t_vector tokens) -{ - char **tmp; - t_token token; - size_t i; +// static void tokenize(char *input, t_vector tokens) +// { +// char **tmp; +// t_token token; +// size_t i; - i = 0; - tmp = ft_split(input, ' '); - while (tmp[i]) - { - token.type = DOUBLE_QUOTE; - if (i % 3) - token.type = SINGLE_QUOTE; - token.value = tmp[i]; - ft_vec_push(&tokens, (void *)&token); - i++; - } - free(tmp); - ft_vec_apply(&tokens, uncapitalize_token); - pretty_print_vector(&tokens); - ft_vec_free(&tokens, clear_token); -} +// i = 0; +// tmp = ft_split(input, ' '); +// while (tmp[i]) +// { +// token.type = DOUBLE_QUOTE; +// if (i % 3) +// token.type = SINGLE_QUOTE; +// token.value = tmp[i]; +// ft_vec_push(&tokens, (void *)&token); +// i++; +// } +// free(tmp); +// ft_vec_apply(&tokens, uncapitalize_token); +// pretty_print_vector(&tokens); +// ft_vec_free(&tokens, clear_token); +// } void check_leaks(void) { @@ -104,7 +104,7 @@ int main(int ac, char **av, char **env) if (ac == 2) { // tokenize(av[1], tokens); - if(!lexer(av[1], &tokens)) + if (!lexer(av[1], &tokens)) { printf("lexer error"); return (1); @@ -122,8 +122,13 @@ int main(int ac, char **av, char **env) if (!ft_strcmp(input, "exit")) return (free(input), 0); else - tokenize(input, tokens); + { + if (!lexer(input, &tokens)) + printf("lexer error"); + pretty_print_vector(&tokens); + } free(input); + ft_vec_free(&tokens, clear_token); ft_vec_init(&tokens, 5, sizeof(t_token)); } return (0); From ed0ec86ce5c4eaeb58fbb87df59651339b7170f9 Mon Sep 17 00:00:00 2001 From: Julius de Baaij Date: Wed, 19 Jul 2023 23:27:36 +0200 Subject: [PATCH 30/91] =?UTF-8?q?=F0=9F=90=9B=20fix(server.ts):=20change?= =?UTF-8?q?=20port=20variable=20case=20from=20lowercase=20port=20to=20uppe?= =?UTF-8?q?rcase=20PORT=20to=20improve=20semantics=20=E2=9C=A8=20feat(serv?= =?UTF-8?q?er.ts):=20add=20support=20for=20process.env.PORT=20environment?= =?UTF-8?q?=20variable=20to=20be=20able=20to=20run=20app=20on=20a=20config?= =?UTF-8?q?urable=20port=20=F0=9F=94=A7=20chore(launch.json):=20update=20l?= =?UTF-8?q?aunch.json=20with=20new=20arguments=20for=20debugging=20?= =?UTF-8?q?=F0=9F=94=A7=20chore(Makefile):=20add=20structs/token=20to=20th?= =?UTF-8?q?e=20list=20of=20source=20files=20to=20be=20compiled=20?= =?UTF-8?q?=F0=9F=94=A7=20chore(minishell.h):=20add=20function=20prototype?= =?UTF-8?q?=20for=20create=5Ftoken=20in=20token.c=20=F0=9F=94=A7=20chore(l?= =?UTF-8?q?ibft):=20update=20libft=20submodule=20to=20commit=20f39c12e=20?= =?UTF-8?q?=E2=9C=A8=20feat(index.c):=20add=20create=5Ftoken=20function=20?= =?UTF-8?q?to=20create=20a=20token=20struct=20=F0=9F=94=A7=20chore(index.c?= =?UTF-8?q?):=20refactor=20lexer=20function=20to=20handle=20quotes=20and?= =?UTF-8?q?=20create=20tokens?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 6 ++- Makefile | 2 +- includes/minishell.h | 5 ++- libft | 2 +- src/lexer/index.c | 95 ++++++++++++++++++++++++-------------------- src/structs/token.c | 25 ++++++++++++ 6 files changed, 85 insertions(+), 50 deletions(-) create mode 100644 src/structs/token.c diff --git a/.vscode/launch.json b/.vscode/launch.json index 788a900..62835ba 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,8 +9,10 @@ "request": "launch", "name": "Debug", "program": "${workspaceFolder}/minishell", - "args": ["hey there delilah whats it like in new york city I'm a thousand miles away. but girl tonight you look so pretty yes you do! Ney york doesn't shine as bright as you. test test test test test test test test test test test test test test test"], + "args": [ + "hello \"123\"" + ], "cwd": "${workspaceFolder}" } ] -} +} \ No newline at end of file diff --git a/Makefile b/Makefile index 43d36c2..4f86336 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = minishell -SRC = main check_input lexer/index utils/error +SRC = main check_input lexer/index utils/error structs/token SRCS = $(addsuffix .c, $(addprefix src/, $(SRC))) OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a diff --git a/includes/minishell.h b/includes/minishell.h index 0a5a397..3c3668f 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/07/19 21:52:29 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/19 22:37:41 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -30,7 +30,8 @@ bool check_quotes_parantheses(char *input); bool lexer(char *input, t_vector *vec); - +/* structs */ +t_token *create_token(char *value, int type); /* utils */ void err(char *err, char *cmd, int exit_code); diff --git a/libft b/libft index 1e79ca0..f39c12e 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 1e79ca0e5fccbe5157bc9e788876f5f6ae8b716c +Subproject commit f39c12e2837eef2658543b32eb9f86b8ab956ce9 diff --git a/src/lexer/index.c b/src/lexer/index.c index d811871..9de29dc 100644 --- a/src/lexer/index.c +++ b/src/lexer/index.c @@ -6,24 +6,12 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 13:32:55 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/19 21:59:55 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/19 23:27:00 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -t_token *create_token(char *value, int type) -{ - t_token *token; - - token = malloc(sizeof(t_token)); - if (!token) - return (NULL); - token->value = value; - token->type = type; - return (token); -} - static bool check_next_quote(char *str) { char quote; @@ -36,30 +24,67 @@ static bool check_next_quote(char *str) return (false); return (true); }; + +static bool create_string(char *str, size_t *i, t_vector *vec) +{ + size_t left; + char *value; + + left = (*i); + while (str[left] == ' ') + left++; + if (str[left] == '\0') + { + *i = left; + return (true); + } + if (*i != 0) + { + left = (*i) - 1; + while (left != 0 && + (str[left] != ' ' && str[left] != '\'' && str[left] != '\"')) + left--; + if (str[left] == ' ' || str[left] == '\'' || str[left] == '\"') + left++; + value = ft_substr(str, left, ((*i) - left)); + if (!value) + return (false); + if (!ft_vec_push(vec, (void *)create_token(value, -1))) + return (false); + } + while (str[*i] == ' ' && str[*i]) + (*i)++; + return (true); +} +/** + * @todo " hello'gmarmopg grnrga' 'hey'"; check spaces runback +*/ static bool create_quote_string(char *str, size_t *i, t_vector *vec) { size_t occur_left; size_t occur_right; char c; char *value; + c = str[*i]; - if (i != 0) + if (i != 0 && str[(*i) - 1] != ' ') { occur_left = (*i) - 1; - while ((str[occur_left] != ' ' || str[occur_left] != '\'' || + while ((str[occur_left] != ' ' && str[occur_left] != '\'' && str[occur_left != '\"']) && occur_left > 0) occur_left--; - value = ft_substr(str, occur_left, (*i) - occur_left + 1); + value = ft_substr(str, occur_left, (*i) - occur_left); if (!value) return (false); if (!ft_vec_push(vec, (void *)create_token(value, -1))) return (false); } - occur_right = (*i)++; - while (str[occur_right] != c && str[occur_right]) + occur_right = (*i) + 1; + while (str[occur_right] && str[occur_right] != c) occur_right++; - value = ft_substr(str, *i, occur_right - *i); + occur_right++; + value = ft_substr(str, *i, occur_right - (*i)); if (!value) return (false); if (!ft_vec_push(vec, (void *)create_token(value, -1))) @@ -68,29 +93,7 @@ static bool create_quote_string(char *str, size_t *i, t_vector *vec) return (true); } -static bool create_string(char *str, size_t *i, t_vector *vec) -{ - size_t left; - char *value; - if (*i != 0) - { - left = (*i) - 1; - while (left != 0 && - (str[left] != ' ' && str[left] != '\'' && str[left] != '\"')) - left--; - if (str[left] == ' ' || str[left] == '\'' || str[left] == '\"') - left++; - value = ft_substr(str, left, ((*i) - left)); - if (!value) - return (false); - if (!ft_vec_push(vec, (void *)create_token(value, -1))) - return (false); - } - while (str[*i] == ' ' && str[*i]) - (*i)++; - return (true); -} bool lexer(char *input, t_vector *vec) { @@ -104,7 +107,7 @@ bool lexer(char *input, t_vector *vec) if (input[i] == '\"' || input[i] == '\'') { if (!check_next_quote(&input[i])) - return (err("malloc", NULL, 1), false); + return (err("unfinished quote", NULL, 1), false); if (i > 0 && !create_quote_string(input, &i, vec)) return (err("malloc", NULL, 1), false); } @@ -118,7 +121,11 @@ bool lexer(char *input, t_vector *vec) i++; } } - if (!create_string(input, &i, vec)) - return (err("malloc", NULL, 1), false); + if (input[i - 1] != ' ' && input[i - 1] != '\'' && input[i - 1] != '\"') + { + printf("test:%zu %c\n", (i - 1), input[i-1]); + if (!create_string(input, &i, vec)) + return (err("malloc", NULL, 1), false); + } return (true); } diff --git a/src/structs/token.c b/src/structs/token.c new file mode 100644 index 0000000..19d2c84 --- /dev/null +++ b/src/structs/token.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* token.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/07/19 22:36:40 by mdekker/jde #+# #+# */ +/* Updated: 2023/07/19 22:38:18 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +t_token *create_token(char *value, int type) +{ + t_token *token; + + token = malloc(sizeof(t_token)); + if (!token) + return (NULL); + token->value = value; + token->type = type; + return (token); +} From db7eea907b4c7db9e40bfc7842449f6c91ca8115 Mon Sep 17 00:00:00 2001 From: Julius de Baaij Date: Thu, 20 Jul 2023 12:19:57 +0200 Subject: [PATCH 31/91] =?UTF-8?q?=F0=9F=90=9B=20fix(launch.json):=20update?= =?UTF-8?q?=20launch=20arguments=20to=20include=20additional=20test=20case?= =?UTF-8?q?s=20for=20the=20program=20=F0=9F=90=9B=20fix(index.c):=20fix=20?= =?UTF-8?q?logic=20error=20in=20create=5Fstring=20function=20to=20correctl?= =?UTF-8?q?y=20handle=20right=20index=20and=20add=20missing=20increment=20?= =?UTF-8?q?of=20i=20=F0=9F=90=9B=20fix(index.c):=20fix=20logic=20error=20i?= =?UTF-8?q?n=20create=5Fquote=5Fstring=20function=20to=20correctly=20handl?= =?UTF-8?q?e=20left=20index=20and=20add=20missing=20increment=20of=20i?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 2 +- src/lexer/index.c | 59 ++++++++++++++++----------------------------- 2 files changed, 22 insertions(+), 39 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 62835ba..abbccfe 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "name": "Debug", "program": "${workspaceFolder}/minishell", "args": [ - "hello \"123\"" + "'hello' 1 2 '34' grarg'5' 'laatste''echt' " ], "cwd": "${workspaceFolder}" } diff --git a/src/lexer/index.c b/src/lexer/index.c index 9de29dc..60544f8 100644 --- a/src/lexer/index.c +++ b/src/lexer/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 13:32:55 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/19 23:27:00 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 12:19:25 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -28,28 +28,28 @@ static bool check_next_quote(char *str) static bool create_string(char *str, size_t *i, t_vector *vec) { size_t left; + size_t right; char *value; - left = (*i); - while (str[left] == ' ') - left++; - if (str[left] == '\0') + right = (*i); + while (right > 0 && str[right] == ' ') + right--; + if (right > 0 && (str[right] == '\'' || str[right] == '\"')) + right--; + left = right; + if (left > 0) + left--; + if (left != 0) { - *i = left; - return (true); - } - if (*i != 0) - { - left = (*i) - 1; while (left != 0 && (str[left] != ' ' && str[left] != '\'' && str[left] != '\"')) left--; if (str[left] == ' ' || str[left] == '\'' || str[left] == '\"') left++; - value = ft_substr(str, left, ((*i) - left)); + value = ft_substr(str, left, right - left + 1); if (!value) return (false); - if (!ft_vec_push(vec, (void *)create_token(value, -1))) + if (!ft_vec_push(vec, (void *)create_token(value, 0))) return (false); } while (str[*i] == ' ' && str[*i]) @@ -61,25 +61,13 @@ static bool create_string(char *str, size_t *i, t_vector *vec) */ static bool create_quote_string(char *str, size_t *i, t_vector *vec) { - size_t occur_left; size_t occur_right; char c; char *value; + if ((*i) > 0 && (str[(*i) - 1] != ' ' && str[(*i) - 1] != '\'' && str[(*i) - 1] != '\"' )) + create_string(str, i, vec); c = str[*i]; - if (i != 0 && str[(*i) - 1] != ' ') - { - occur_left = (*i) - 1; - while ((str[occur_left] != ' ' && str[occur_left] != '\'' && - str[occur_left != '\"']) && - occur_left > 0) - occur_left--; - value = ft_substr(str, occur_left, (*i) - occur_left); - if (!value) - return (false); - if (!ft_vec_push(vec, (void *)create_token(value, -1))) - return (false); - } occur_right = (*i) + 1; while (str[occur_right] && str[occur_right] != c) occur_right++; @@ -87,9 +75,11 @@ static bool create_quote_string(char *str, size_t *i, t_vector *vec) value = ft_substr(str, *i, occur_right - (*i)); if (!value) return (false); - if (!ft_vec_push(vec, (void *)create_token(value, -1))) + if (!ft_vec_push(vec, (void *)create_token(value, 0))) return (false); *i = occur_right; + while (str[*i] == ' ' && str[*i]) + (*i)++; return (true); } @@ -97,35 +87,28 @@ static bool create_quote_string(char *str, size_t *i, t_vector *vec) bool lexer(char *input, t_vector *vec) { - size_t i; + size_t i; if (!input) return (false); i = 0; while (input[i]) { - if (input[i] == '\"' || input[i] == '\'') + if (input[i] == '\"' || input[i] == '\'' ) { if (!check_next_quote(&input[i])) return (err("unfinished quote", NULL, 1), false); - if (i > 0 && !create_quote_string(input, &i, vec)) + if (!create_quote_string(input, &i, vec)) return (err("malloc", NULL, 1), false); } else if (input[i] == ' ') - { if (!create_string(input, &i, vec)) return (err("malloc", NULL, 1), false); - } else - { i++; - } } if (input[i - 1] != ' ' && input[i - 1] != '\'' && input[i - 1] != '\"') - { - printf("test:%zu %c\n", (i - 1), input[i-1]); if (!create_string(input, &i, vec)) return (err("malloc", NULL, 1), false); - } return (true); } From 5b4cc2f9a674a7b574c42644738ce8970be89387 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Thu, 20 Jul 2023 14:09:00 +0200 Subject: [PATCH 32/91] =?UTF-8?q?=F0=9F=90=9B=20fix(Makefile):=20remove=20?= =?UTF-8?q?unnecessary=20dependency=20on=20$(READLINE)=20in=20$(NAME)=20ta?= =?UTF-8?q?rget=20=E2=9C=A8=20feat(Makefile):=20add=20readline=20target=20?= =?UTF-8?q?to=20build=20$(READLINE)=20library=20=F0=9F=90=9B=20fix(enum.h)?= =?UTF-8?q?:=20fix=20typo=20in=20ENUM=5FH=20macro=20definition=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(minishell.h):=20fix=20indentation=20in=20inc?= =?UTF-8?q?lude=20statements=20=E2=9C=A8=20feat(parser):=20add=20parser=20?= =?UTF-8?q?function=20and=20helper=20functions=20for=20parsing=20tokens=20?= =?UTF-8?q?=E2=9C=A8=20feat(debug):=20add=20print=5Ftoken=5Fvector=20funct?= =?UTF-8?q?ion=20to=20print=20token=20vector=20in=20a=20pretty=20way=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(lexer/index.c):=20remove=20unnecessary=20che?= =?UTF-8?q?ck=20for=20spaces=20before=20creating=20quote=20string?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🔥 refactor(init.c): remove unused code and delete init.c file ✨ feat(main.c): add debug mode and refactor loop logic 🆕 feat(parser/index.c): add parser function to handle token types and check for environment variables 📝 docs(quotes.c): add quotes.c file with functions to check if a string is enclosed in double or single quotes 🔨 refactor(token.c): change function signature of create_token to match coding style 🔨 refactor(token.c): add clear_token function to free memory of token --- Makefile | 8 ++- includes/enum.h | 7 ++- includes/minishell.h | 42 ++++++++----- libft | 2 +- src/debug/print_vector.c | 41 ++++++++++++ src/lexer/index.c | 16 +++-- src/lists/init.c | 110 -------------------------------- src/main.c | 131 ++++++++++++--------------------------- src/parser/index.c | 44 +++++++++++++ src/parser/quotes.c | 27 ++++++++ src/structs/token.c | 12 +++- 11 files changed, 204 insertions(+), 236 deletions(-) delete mode 100644 src/lists/init.c create mode 100644 src/parser/index.c create mode 100644 src/parser/quotes.c diff --git a/Makefile b/Makefile index 4f86336..563c0a2 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = minishell -SRC = main check_input lexer/index utils/error structs/token +SRC = main check_input lexer/index utils/error structs/token parser/index parser/quotes debug/print_vector SRCS = $(addsuffix .c, $(addprefix src/, $(SRC))) OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a @@ -24,7 +24,7 @@ EMOJI_RUN = 🚀 all: $(NAME) -$(NAME): $(LIBFT) $(READLINE) $(OBJS) +$(NAME): $(LIBFT) $(OBJS) @printf "$(COLOR_INFO)$(EMOJI_INFO) Compiling $(NAME)...$(COLOR_RESET)\t" @cc $(OBJS) $(CODAM_FLAGS) $(if DEBUG, $(DEBUG_FLAGS)) -DDEBUG=$(DEBUG) $(LINKER) $(INCLUDES) $(LIBS) -o $@ @sleep 0.25 @@ -62,7 +62,7 @@ clean: fclean: clean @printf "$(COLOR_INFO)$(EMOJI_CLEAN) Removing executable...$(COLOR_RESET)\t" @$(MAKE) -C libft fclean > /dev/null - @rm -f $(LIBS) + @rm -f libft/libft.a @rm -f $(NAME) @sleep 0.25 @printf "✅\n" @@ -78,6 +78,8 @@ re: fclean $(NAME) bonus: all +readline: $(READLINE) + module-update: @printf "$(COLOR_INFO)$(EMOJI_INFO) Updating submodules...$(COLOR_RESET)\t" @git submodule update --remote --merge diff --git a/includes/enum.h b/includes/enum.h index fe10037..fe625cc 100644 --- a/includes/enum.h +++ b/includes/enum.h @@ -6,12 +6,12 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/13 17:41:00 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/18 22:45:44 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 11:24:07 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #ifndef ENUM_H -# define ENUM_H +#define ENUM_H /** * @brief The enum for the different types of tokens @@ -30,6 +30,7 @@ */ typedef enum e_types { + UNKNOWN, DOUBLE_QUOTE, SINGLE_QUOTE, PIPE, @@ -41,6 +42,6 @@ typedef enum e_types O_REDIRECT, I_REDIRECT, HERE_DOC -} t_types; +} t_types; #endif diff --git a/includes/minishell.h b/includes/minishell.h index 3c3668f..d1cfad7 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,34 +6,42 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/07/19 22:37:41 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 14:05:44 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #ifndef MINISHELL_H -# define MINISHELL_H +#define MINISHELL_H -# include -# include -# include -# include -# include -# include -# include -# include +#include +#include +#include +#include +#include +#include +#include +#include -# ifndef DEBUG -# define DEBUG 0 -# endif +#ifndef DEBUG +#define DEBUG 0 +#endif /* input_check */ -bool check_quotes_parantheses(char *input); -bool lexer(char *input, t_vector *vec); +bool check_quotes_parantheses(char *input); +bool lexer(char *input, t_vector *vec); /* structs */ -t_token *create_token(char *value, int type); - +t_token *create_token(char *value, int type); +void clear_token(void *data); /* utils */ void err(char *err, char *cmd, int exit_code); +/* parser */ +void parser(t_vector *vec); +bool is_encased_dq(char *str); +bool is_encased_sq(char *str); + +/* debug */ +void print_token_vector(t_vector *tokens); + #endif diff --git a/libft b/libft index f39c12e..cbff6b6 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit f39c12e2837eef2658543b32eb9f86b8ab956ce9 +Subproject commit cbff6b6c04ac529b89480b00d43b0d4533f0ac6c diff --git a/src/debug/print_vector.c b/src/debug/print_vector.c index e69de29..6bad540 100644 --- a/src/debug/print_vector.c +++ b/src/debug/print_vector.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* print_vector.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/07/20 13:51:45 by mdekker/jde #+# #+# */ +/* Updated: 2023/07/20 13:52:28 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +/** + * @brief Prints a t_token vector in a pretty way + * + * @param tokens t_vector of t_token structs + */ +void print_token_vector(t_vector *tokens) +{ + t_token *token; + size_t i; + + i = 0; + while (i < tokens->lenght) + { + token = (t_token *)ft_vec_get(tokens, i); + if (token != NULL) + { + printf("\033[1;32m●\n"); + printf("\033[1;34m│\n"); + printf("├── Token %zu:\n", i++); + printf("│ ├── Value: %s\n", token->value); + printf("│ ├── Type: %i\n", token->type); + printf("│ └── Adress: %p\n", token); + printf("\033[1;34m│\n"); + printf("\033[0m"); + } + } +} diff --git a/src/lexer/index.c b/src/lexer/index.c index 60544f8..9b3502f 100644 --- a/src/lexer/index.c +++ b/src/lexer/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 13:32:55 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 12:19:25 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 13:36:55 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -58,14 +58,14 @@ static bool create_string(char *str, size_t *i, t_vector *vec) } /** * @todo " hello'gmarmopg grnrga' 'hey'"; check spaces runback -*/ + */ static bool create_quote_string(char *str, size_t *i, t_vector *vec) { size_t occur_right; char c; char *value; - if ((*i) > 0 && (str[(*i) - 1] != ' ' && str[(*i) - 1] != '\'' && str[(*i) - 1] != '\"' )) + if ((*i) > 0 && (str[(*i) - 1] != ' ' && str[(*i) - 1] != '\'' && str[(*i) - 1] != '\"')) create_string(str, i, vec); c = str[*i]; occur_right = (*i) + 1; @@ -83,18 +83,14 @@ static bool create_quote_string(char *str, size_t *i, t_vector *vec) return (true); } - - bool lexer(char *input, t_vector *vec) { - size_t i; + size_t i; - if (!input) - return (false); i = 0; while (input[i]) { - if (input[i] == '\"' || input[i] == '\'' ) + if (input[i] == '\"' || input[i] == '\'') { if (!check_next_quote(&input[i])) return (err("unfinished quote", NULL, 1), false); @@ -102,8 +98,10 @@ bool lexer(char *input, t_vector *vec) return (err("malloc", NULL, 1), false); } else if (input[i] == ' ') + { if (!create_string(input, &i, vec)) return (err("malloc", NULL, 1), false); + } else i++; } diff --git a/src/lists/init.c b/src/lists/init.c deleted file mode 100644 index 40f59e7..0000000 --- a/src/lists/init.c +++ /dev/null @@ -1,110 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* init.c :+: :+: */ -/* +:+ */ -/* By: mdekker/jde-baai +#+ */ -/* +#+ */ -/* Created: 2023/07/13 17:36:56 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/13 17:45:08 by mdekker/jde ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include - -/** - * @brief Creates a new node - * - * @param value value to put in node - * @return t_token* pointer to new node - */ -static t_token *create_node(char *str, t_types type) -{ - t_token *new; - - new = malloc(sizeof(t_token)); - if (!new) - return (NULL); - new->value = str; - new->type = type; - new->prev = NULL; - new->next = NULL; - return (new); -} - -/** - * @brief links two nodes - * - * @param node node to link to - * @param newnode node to link - * @return t_token* pointer to HEAD - */ -static t_token *link_node(t_token *node, t_token *newnode) -{ - if (node == NULL || newnode == NULL) - return (NULL); - while (node->next != NULL) - node = node->next; - newnode->prev = node; - node->next = newnode; - return (newnode); -} - -/** - * @brief Append a node to the list or create a new list - * - * @param head Pointer to the head of the list or NULL - * @param str String to put in the node - * @return t_token* The head of the list - */ -t_token *list_append(t_token *head, char *str, t_types type) -{ - t_token *new; - - if (head == NULL) - { - head = create_node(str, type); - if (head == NULL) - return (NULL); - } - else - { - new = create_node(str, type); - if (new == NULL) - return (NULL); - if (link_node(head, new) == NULL) - return (NULL); - } - return (head); -} - -/** - * @brief Insert a node at a given index - * - * @param head The list to insert in - * @param str The string to put in the node - * @param type The type of the node - * @param index The index to insert at - * @return t_token* The head of the list - */ -t_token *list_insert(t_token *head, char *str, t_types type, size_t index) -{ - t_token *new; - t_token *tmp; - size_t i; - - new = create_node(str, type); - if (new == NULL) - return (NULL); - tmp = head; - i = 0; - while (i < index && tmp->next != NULL) - { - tmp = tmp->next; - i++; - } - new->next = tmp->next; - new->prev = tmp; - tmp->next = new; - return (head); -} diff --git a/src/main.c b/src/main.c index 48d1fd8..9f3cbd9 100644 --- a/src/main.c +++ b/src/main.c @@ -6,130 +6,79 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/19 22:38:02 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 14:05:25 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -void pretty_print_vector(t_vector *tokens) +void check_leaks(void) { - t_token *token; - size_t i; - - i = 0; - while (i < tokens->lenght) - { - token = (t_token *)ft_vec_get(tokens, i); - if (token != NULL) - { - printf("\033[1;32m●\n"); - printf("\033[1;34m│\n"); - printf("├── Token %zu:\n", i++); - printf("│ ├── Value: %s\n", token->value); - printf("│ ├── Type: %i\n", token->type); - printf("│ └── Adress: %p\n", token); - printf("\033[1;34m│\n"); - printf("\033[0m"); - } - } + system("leaks minishell"); } -static void clear_token(void *data) +static void debug(void) { - t_token *token; - - token = (t_token *)data; - free(token->value); + atexit(check_leaks); + printf("\033[1;32m●\n"); + printf("\033[1;34m│\n"); + printf("├── Debug mode enabled\n"); + printf("\033[1;34m│\n"); + printf("\033[0m"); } -// static void uncapitalize_token(void *data) -// { -// t_token *token; -// char *temp; - -// token = (t_token *)data; -// temp = token->value; -// while (*temp) -// { -// *temp = ft_tolower(*temp); -// temp++; -// } -// } - -// static void tokenize(char *input, t_vector tokens) -// { -// char **tmp; -// t_token token; -// size_t i; - -// i = 0; -// tmp = ft_split(input, ' '); -// while (tmp[i]) -// { -// token.type = DOUBLE_QUOTE; -// if (i % 3) -// token.type = SINGLE_QUOTE; -// token.value = tmp[i]; -// ft_vec_push(&tokens, (void *)&token); -// i++; -// } -// free(tmp); -// ft_vec_apply(&tokens, uncapitalize_token); -// pretty_print_vector(&tokens); -// ft_vec_free(&tokens, clear_token); -// } - -void check_leaks(void) +static void loop(t_vector *vec) { - system("leaks minishell"); + char *input; + + while (1) + { + input = readline("\n\033[1;32mminishell$ \033[0m"); + if (!input) + { + free(input); + break; + } + add_history(input); + if (!ft_strcmp(input, "exit")) + return (free(input), ft_vec_free(vec, clear_token)); + else + { + if (!lexer(input, vec)) + printf("lexer error"); + print_token_vector(vec); + } + free(input); + ft_vec_free(vec, clear_token); + ft_vec_init(vec, 5, sizeof(t_token)); + } } int main(int ac, char **av, char **env) { - char *input; t_vector tokens; (void)env; if (DEBUG) - { - atexit(check_leaks); - printf("\033[1;32m●\n"); - printf("\033[1;34m│\n"); - printf("├── Debug mode enabled\n"); - printf("\033[1;34m│\n"); - printf("\033[0m"); - } + debug(); ft_vec_init(&tokens, 5, sizeof(t_token)); if (ac == 2) { - // tokenize(av[1], tokens); if (!lexer(av[1], &tokens)) { printf("lexer error"); return (1); } - pretty_print_vector(&tokens); + print_token_vector(&tokens); ft_vec_free(&tokens, clear_token); return (0); } - while (1) + else if (ac == 1) + loop(&tokens); + else { - input = readline("\n\033[1;32mminishell$ \033[0m"); - if (!input) - break; - add_history(input); - if (!ft_strcmp(input, "exit")) - return (free(input), 0); - else - { - if (!lexer(input, &tokens)) - printf("lexer error"); - pretty_print_vector(&tokens); - } - free(input); + printf("Too many arguments"); ft_vec_free(&tokens, clear_token); - ft_vec_init(&tokens, 5, sizeof(t_token)); } return (0); } diff --git a/src/parser/index.c b/src/parser/index.c new file mode 100644 index 0000000..f9974e2 --- /dev/null +++ b/src/parser/index.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* index.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/07/20 11:12:20 by mdekker #+# #+# */ +/* Updated: 2023/07/20 13:38:05 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +static bool contains_env_var(char *str) +{ + if (ft_strchr(str, '$')) + return (true); + return (false); +} + +void parser(t_vector *vec) +{ + t_token *token; + size_t i; + + i = 0; + while (i < vec->lenght) + { + token = (t_token *)ft_vec_get(vec, i); + if (token->type == 0) + { + if (is_encased_dq(token->value)) + token->type = 1; + else if (is_encased_sq(token->value)) + token->type = 2; + else if (contains_env_var(token->value)) + token->type = 3; + printf("Token %zu: %s\n", i, token->value); + printf("Type: %i\n", token->type); + } + i++; + } +} diff --git a/src/parser/quotes.c b/src/parser/quotes.c new file mode 100644 index 0000000..65d0153 --- /dev/null +++ b/src/parser/quotes.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* quotes.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/07/20 11:37:28 by mdekker/jde #+# #+# */ +/* Updated: 2023/07/20 11:39:48 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +bool is_encased_dq(char *str) +{ + if (str[0] == '\"' && str[ft_strlen(str) - 1] == '\"') + return (true); + return (false); +} + +bool is_encased_sq(char *str) +{ + if (str[0] == '\'' && str[ft_strlen(str) - 1] == '\'') + return (true); + return (false); +} diff --git a/src/structs/token.c b/src/structs/token.c index 19d2c84..b78fc6a 100644 --- a/src/structs/token.c +++ b/src/structs/token.c @@ -6,13 +6,13 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 22:36:40 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/19 22:38:18 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 14:06:29 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -t_token *create_token(char *value, int type) +t_token *create_token(char *value, int type) { t_token *token; @@ -23,3 +23,11 @@ t_token *create_token(char *value, int type) token->type = type; return (token); } + +void clear_token(void *data) +{ + t_token *token; + + token = (t_token *)data; + free(token->value); +} From 495a5f89bdd13621fa4fc843e4b07249708276b5 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Thu, 20 Jul 2023 16:40:02 +0200 Subject: [PATCH 33/91] =?UTF-8?q?=F0=9F=90=9B=20fix(Makefile):=20add=20mis?= =?UTF-8?q?sing=20parser/tokens2=20file=20to=20the=20SRC=20variable=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(enum.h):=20change=20HERE=5FDOC=20to=20O=5FHE?= =?UTF-8?q?REDOC=20and=20I=5FHEREDOC=20=F0=9F=90=9B=20fix(minishell.h):=20?= =?UTF-8?q?add=20missing=20whitespace=20before=20define=20directive=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(minishell.h):=20add=20missing=20whitespace?= =?UTF-8?q?=20before=20include=20directives=20=F0=9F=90=9B=20fix(minishell?= =?UTF-8?q?.h):=20add=20missing=20whitespace=20before=20ifndef=20directive?= =?UTF-8?q?=20=F0=9F=90=9B=20fix(minishell.h):=20add=20missing=20whitespac?= =?UTF-8?q?e=20before=20ifndef=20directive=20=F0=9F=90=9B=20fix(minishell.?= =?UTF-8?q?h):=20add=20missing=20whitespace=20before=20ifndef=20directive?= =?UTF-8?q?=20=F0=9F=90=9B=20fix(minishell.h):=20add=20missing=20whitespac?= =?UTF-8?q?e=20before=20ifndef=20directive=20=F0=9F=90=9B=20fix(minishell.?= =?UTF-8?q?h):=20add=20missing=20whitespace=20before=20ifndef=20directive?= =?UTF-8?q?=20=F0=9F=90=9B=20fix(minishell.h):=20add=20missing=20whitespac?= =?UTF-8?q?e=20before=20ifndef=20directive=20=F0=9F=90=9B=20fix(minishell.?= =?UTF-8?q?h):=20add=20missing=20whitespace=20before=20ifndef=20directive?= =?UTF-8?q?=20=F0=9F=90=9B=20fix(minishell.h):?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🐛 fix(parser/index.c): fix return type of return_map function ✨ feat(parser/index.c): add function map for token types 📝 docs(parser/tokens.c): add functions for pipe and redirect tokens 📝 docs(parser/tokens2.c): add functions for environment variable and logical operators tokens --- Makefile | 2 +- includes/enum.h | 14 +++++++----- includes/minishell.h | 53 +++++++++++++++++++++++++------------------- includes/structs.h | 19 +++++++++++++++- src/main.c | 18 ++++++++------- src/parser/index.c | 53 ++++++++++++++++++++++++++++++-------------- src/parser/tokens.c | 48 +++++++++++++++++++++++++++++++++++++++ src/parser/tokens2.c | 34 ++++++++++++++++++++++++++++ 8 files changed, 186 insertions(+), 55 deletions(-) create mode 100644 src/parser/tokens.c create mode 100644 src/parser/tokens2.c diff --git a/Makefile b/Makefile index 563c0a2..f4b4c43 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = minishell -SRC = main check_input lexer/index utils/error structs/token parser/index parser/quotes debug/print_vector +SRC = main check_input lexer/index utils/error structs/token parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector SRCS = $(addsuffix .c, $(addprefix src/, $(SRC))) OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a diff --git a/includes/enum.h b/includes/enum.h index fe625cc..dd4b6ad 100644 --- a/includes/enum.h +++ b/includes/enum.h @@ -6,12 +6,12 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/13 17:41:00 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 11:24:07 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 16:32:57 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #ifndef ENUM_H -#define ENUM_H +# define ENUM_H /** * @brief The enum for the different types of tokens @@ -21,12 +21,14 @@ * @param PIPE (|) * @param SEMICOLON (;) * @param OR (||) + * @param AND (&&) * @param ENV ($) * @param DQ_ENV An environment variable in double quotes ("$") * @param STRING A string * @param O_REDIRECT (>) * @param I_REDIRECT (<) - * @param HERE_DOC (<<) + * @param O_HEREDOC (>>) + * @param I_HEREDOC (<<) */ typedef enum e_types { @@ -36,12 +38,14 @@ typedef enum e_types PIPE, SEMICOLON, OR, + AND, ENV, DQ_ENV, STRING, O_REDIRECT, I_REDIRECT, - HERE_DOC -} t_types; + O_HEREDOC, + I_HEREDOC +} t_types; #endif diff --git a/includes/minishell.h b/includes/minishell.h index d1cfad7..74a0ccc 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,42 +6,49 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/07/20 14:05:44 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 16:30:15 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #ifndef MINISHELL_H -#define MINISHELL_H +# define MINISHELL_H -#include -#include -#include -#include -#include -#include -#include -#include +# include +# include +# include +# include +# include +# include +# include +# include -#ifndef DEBUG -#define DEBUG 0 -#endif +# ifndef DEBUG +# define DEBUG 0 +# endif /* input_check */ -bool check_quotes_parantheses(char *input); -bool lexer(char *input, t_vector *vec); +bool check_quotes_parantheses(char *input); +bool lexer(char *input, t_vector *vec); /* structs */ -t_token *create_token(char *value, int type); -void clear_token(void *data); +t_token *create_token(char *value, int type); +void clear_token(void *data); /* utils */ -void err(char *err, char *cmd, int exit_code); +void err(char *err, char *cmd, int exit_code); /* parser */ -void parser(t_vector *vec); -bool is_encased_dq(char *str); -bool is_encased_sq(char *str); - +void parser(t_vector *vec); +bool is_encased_dq(char *str); +bool is_encased_sq(char *str); +bool is_pipe(char *str); +bool is_r_redirect(char *str); +bool is_l_redirect(char *str); +bool is_r_hd(char *str); +bool is_l_hd(char *str); +bool contains_env_var(char *str); +bool is_or(char *str); +bool is_and(char *str); /* debug */ -void print_token_vector(t_vector *tokens); +void print_token_vector(t_vector *tokens); #endif diff --git a/includes/structs.h b/includes/structs.h index 0df4d69..9ce1c90 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/07/18 23:06:38 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 16:26:31 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -28,4 +28,21 @@ typedef struct s_token char *value; } t_token; +/** + * @brief The struct for the parser functions. + * + * @param func The function to be called + * @param type The type of token the function is for + * + * @see t_types + * + * @note The function should return true if the token + * is that type, false if not. + */ +typedef struct s_func_map +{ + bool (*func)(char *); + t_types type; +} t_func_map; + #endif diff --git a/src/main.c b/src/main.c index 9f3cbd9..bb61473 100644 --- a/src/main.c +++ b/src/main.c @@ -6,18 +6,18 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 14:05:25 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 15:51:07 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -void check_leaks(void) +void check_leaks(void) { system("leaks minishell"); } -static void debug(void) +static void debug(void) { atexit(check_leaks); printf("\033[1;32m●\n"); @@ -27,9 +27,9 @@ static void debug(void) printf("\033[0m"); } -static void loop(t_vector *vec) +static void loop(t_vector *vec) { - char *input; + char *input; while (1) { @@ -37,7 +37,7 @@ static void loop(t_vector *vec) if (!input) { free(input); - break; + break ; } add_history(input); if (!ft_strcmp(input, "exit")) @@ -46,6 +46,7 @@ static void loop(t_vector *vec) { if (!lexer(input, vec)) printf("lexer error"); + parser(vec); print_token_vector(vec); } free(input); @@ -54,9 +55,9 @@ static void loop(t_vector *vec) } } -int main(int ac, char **av, char **env) +int main(int ac, char **av, char **env) { - t_vector tokens; + t_vector tokens; (void)env; if (DEBUG) @@ -69,6 +70,7 @@ int main(int ac, char **av, char **env) printf("lexer error"); return (1); } + parser(&tokens); print_token_vector(&tokens); ft_vec_free(&tokens, clear_token); return (0); diff --git a/src/parser/index.c b/src/parser/index.c index f9974e2..3ca0403 100644 --- a/src/parser/index.c +++ b/src/parser/index.c @@ -6,39 +6,58 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 11:12:20 by mdekker #+# #+# */ -/* Updated: 2023/07/20 13:38:05 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 16:33:32 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -static bool contains_env_var(char *str) +t_func_map *return_map(void) { - if (ft_strchr(str, '$')) - return (true); - return (false); -} + t_func_map *func_map; -void parser(t_vector *vec) + func_map = (t_func_map *)malloc(sizeof(t_func_map) * 12); + if (func_map == NULL) + { + return (NULL); + } + func_map[0] = (t_func_map){is_encased_dq, DOUBLE_QUOTE}; + func_map[1] = (t_func_map){is_encased_sq, SINGLE_QUOTE}; + func_map[2] = (t_func_map){contains_env_var, ENV}; + func_map[3] = (t_func_map){is_and, AND}; + func_map[4] = (t_func_map){is_or, OR}; + func_map[5] = (t_func_map){is_pipe, PIPE}; + func_map[6] = (t_func_map){is_r_redirect, O_REDIRECT}; + func_map[7] = (t_func_map){is_l_redirect, I_REDIRECT}; + func_map[8] = (t_func_map){is_r_hd, O_HEREDOC}; + func_map[9] = (t_func_map){is_l_hd, I_HEREDOC}; + func_map[10] = (t_func_map){NULL, STRING}; + func_map[11] = (t_func_map){NULL, 0}; + return (func_map); +} +void parser(t_vector *vec) { - t_token *token; - size_t i; + t_token *token; + t_func_map *func_map; + size_t j; + size_t i; + func_map = return_map(); i = 0; while (i < vec->lenght) { token = (t_token *)ft_vec_get(vec, i); if (token->type == 0) { - if (is_encased_dq(token->value)) - token->type = 1; - else if (is_encased_sq(token->value)) - token->type = 2; - else if (contains_env_var(token->value)) - token->type = 3; - printf("Token %zu: %s\n", i, token->value); - printf("Type: %i\n", token->type); + j = 0; + while (func_map[j].func != NULL) + if (!func_map[j].func(token->value)) + j++; + else + break ; + token->type = func_map[j].type; } i++; } + free(func_map); } diff --git a/src/parser/tokens.c b/src/parser/tokens.c new file mode 100644 index 0000000..0d03238 --- /dev/null +++ b/src/parser/tokens.c @@ -0,0 +1,48 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* tokens.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/07/20 14:50:46 by mdekker/jde #+# #+# */ +/* Updated: 2023/07/20 16:28:50 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +bool is_pipe(char *str) +{ + if (ft_strcmp(str, "|") == 0) + return (true); + return (false); +} + +bool is_r_redirect(char *str) +{ + if (ft_strcmp(str, ">") == 0) + return (true); + return (false); +} + +bool is_l_redirect(char *str) +{ + if (ft_strcmp(str, "<") == 0) + return (true); + return (false); +} + +bool is_r_hd(char *str) +{ + if (ft_strcmp(str, ">>") == 0) + return (true); + return (false); +} + +bool is_l_hd(char *str) +{ + if (ft_strcmp(str, "<<") == 0) + return (true); + return (false); +} diff --git a/src/parser/tokens2.c b/src/parser/tokens2.c new file mode 100644 index 0000000..1ed3493 --- /dev/null +++ b/src/parser/tokens2.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* tokens2.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/07/20 16:29:02 by mdekker/jde #+# #+# */ +/* Updated: 2023/07/20 16:29:52 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +bool contains_env_var(char *str) +{ + if (ft_strchr(str, '$')) + return (true); + return (false); +} + +bool is_and(char *str) +{ + if (ft_strcmp(str, "&&") == 0) + return (true); + return (false); +} + +bool is_or(char *str) +{ + if (ft_strcmp(str, "||") == 0) + return (true); + return (false); +} From 505f16ac92c031a1c8c17730118e6ccaeeb7807a Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Thu, 20 Jul 2023 16:53:37 +0200 Subject: [PATCH 34/91] =?UTF-8?q?=F0=9F=94=A7=20fix(enum.h):=20add=20PAREN?= =?UTF-8?q?THESES=20enum=20value=20=F0=9F=94=A7=20fix(minishell.h):=20add?= =?UTF-8?q?=20is=5Fencased=5Fparentheses=20function=20declaration=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(main.c):=20return=20from=20loop=20function?= =?UTF-8?q?=20if=20lexer=20returns=20false=20=F0=9F=94=A7=20fix(parser/ind?= =?UTF-8?q?ex.c):=20handle=20malloc=20failure=20in=20return=5Fmap=20functi?= =?UTF-8?q?on=20=F0=9F=94=A7=20fix(parser/index.c):=20add=20PARENTHESES=20?= =?UTF-8?q?enum=20value=20to=20func=5Fmap=20array=20=F0=9F=94=A7=20fix(par?= =?UTF-8?q?ser/index.c):=20handle=20malloc=20failure=20in=20parser=20funct?= =?UTF-8?q?ion=20=F0=9F=94=A7=20fix(parser/quotes.c):=20add=20is=5Fencased?= =?UTF-8?q?=5Fparentheses=20function=20implementation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/enum.h | 4 +++- includes/minishell.h | 3 ++- src/main.c | 17 ++++++++++------- src/parser/index.c | 29 +++++++++++++++-------------- src/parser/quotes.c | 13 ++++++++++--- 5 files changed, 40 insertions(+), 26 deletions(-) diff --git a/includes/enum.h b/includes/enum.h index dd4b6ad..03ba3fe 100644 --- a/includes/enum.h +++ b/includes/enum.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/13 17:41:00 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 16:32:57 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 16:51:32 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ * @param DOUBLE_QUOTE (") * @param SINGLE_QUOTE (') * @param PIPE (|) + * @param PARENTHESES (()) * @param SEMICOLON (;) * @param OR (||) * @param AND (&&) @@ -35,6 +36,7 @@ typedef enum e_types UNKNOWN, DOUBLE_QUOTE, SINGLE_QUOTE, + PARENTHESES, PIPE, SEMICOLON, OR, diff --git a/includes/minishell.h b/includes/minishell.h index 74a0ccc..ad9a350 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/07/20 16:30:15 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 16:46:36 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -40,6 +40,7 @@ void err(char *err, char *cmd, int exit_code); void parser(t_vector *vec); bool is_encased_dq(char *str); bool is_encased_sq(char *str); +bool is_encased_parentheses(char *str); bool is_pipe(char *str); bool is_r_redirect(char *str); bool is_l_redirect(char *str); diff --git a/src/main.c b/src/main.c index bb61473..fbddae7 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 15:51:07 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 16:52:59 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -26,7 +26,13 @@ static void debug(void) printf("\033[1;34m│\n"); printf("\033[0m"); } - +/** + * @brief The main loop of the program + * + * @param vec The vector to store the tokens in + * + * @todo Add a way to exit the program but not the shell + */ static void loop(t_vector *vec) { char *input; @@ -45,7 +51,7 @@ static void loop(t_vector *vec) else { if (!lexer(input, vec)) - printf("lexer error"); + return (free(input), ft_vec_free(vec, clear_token)); parser(vec); print_token_vector(vec); } @@ -66,10 +72,7 @@ int main(int ac, char **av, char **env) if (ac == 2) { if (!lexer(av[1], &tokens)) - { - printf("lexer error"); - return (1); - } + return (ft_vec_free(&tokens, clear_token), 1); parser(&tokens); print_token_vector(&tokens); ft_vec_free(&tokens, clear_token); diff --git a/src/parser/index.c b/src/parser/index.c index 3ca0403..2a3ee97 100644 --- a/src/parser/index.c +++ b/src/parser/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 11:12:20 by mdekker #+# #+# */ -/* Updated: 2023/07/20 16:33:32 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 16:50:59 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -16,23 +16,22 @@ t_func_map *return_map(void) { t_func_map *func_map; - func_map = (t_func_map *)malloc(sizeof(t_func_map) * 12); + func_map = (t_func_map *)malloc(sizeof(t_func_map) * 13); if (func_map == NULL) - { return (NULL); - } func_map[0] = (t_func_map){is_encased_dq, DOUBLE_QUOTE}; func_map[1] = (t_func_map){is_encased_sq, SINGLE_QUOTE}; - func_map[2] = (t_func_map){contains_env_var, ENV}; - func_map[3] = (t_func_map){is_and, AND}; - func_map[4] = (t_func_map){is_or, OR}; - func_map[5] = (t_func_map){is_pipe, PIPE}; - func_map[6] = (t_func_map){is_r_redirect, O_REDIRECT}; - func_map[7] = (t_func_map){is_l_redirect, I_REDIRECT}; - func_map[8] = (t_func_map){is_r_hd, O_HEREDOC}; - func_map[9] = (t_func_map){is_l_hd, I_HEREDOC}; - func_map[10] = (t_func_map){NULL, STRING}; - func_map[11] = (t_func_map){NULL, 0}; + func_map[2] = (t_func_map){is_encased_parentheses, PARENTHESES}; + func_map[3] = (t_func_map){contains_env_var, ENV}; + func_map[4] = (t_func_map){is_and, AND}; + func_map[5] = (t_func_map){is_or, OR}; + func_map[6] = (t_func_map){is_pipe, PIPE}; + func_map[7] = (t_func_map){is_r_redirect, O_REDIRECT}; + func_map[8] = (t_func_map){is_l_redirect, I_REDIRECT}; + func_map[9] = (t_func_map){is_r_hd, O_HEREDOC}; + func_map[10] = (t_func_map){is_l_hd, I_HEREDOC}; + func_map[11] = (t_func_map){NULL, STRING}; + func_map[12] = (t_func_map){NULL, 0}; return (func_map); } void parser(t_vector *vec) @@ -43,6 +42,8 @@ void parser(t_vector *vec) size_t i; func_map = return_map(); + if (func_map == NULL) + return (err("Malloc failed", "parser", 1)); i = 0; while (i < vec->lenght) { diff --git a/src/parser/quotes.c b/src/parser/quotes.c index 65d0153..226cb14 100644 --- a/src/parser/quotes.c +++ b/src/parser/quotes.c @@ -6,22 +6,29 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 11:37:28 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 11:39:48 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 16:46:11 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -bool is_encased_dq(char *str) +bool is_encased_dq(char *str) { if (str[0] == '\"' && str[ft_strlen(str) - 1] == '\"') return (true); return (false); } -bool is_encased_sq(char *str) +bool is_encased_sq(char *str) { if (str[0] == '\'' && str[ft_strlen(str) - 1] == '\'') return (true); return (false); } + +bool is_encased_parentheses(char *str) +{ + if (str[0] == '(' && str[ft_strlen(str) - 1] == ')') + return (true); + return (false); +} From b255cc3a3f43e556d0d90ad8558ab2870efb0c2a Mon Sep 17 00:00:00 2001 From: Julius de Baaij Date: Thu, 20 Jul 2023 16:56:38 +0200 Subject: [PATCH 35/91] =?UTF-8?q?=F0=9F=90=9B=20fix(server.ts):=20change?= =?UTF-8?q?=20port=20variable=20case=20from=20lowercase=20port=20to=20uppe?= =?UTF-8?q?rcase=20PORT=20to=20improve=20semantics=20=E2=9C=A8=20feat(serv?= =?UTF-8?q?er.ts):=20add=20support=20for=20process.env.PORT=20environment?= =?UTF-8?q?=20variable=20to=20be=20able=20to=20run=20app=20on=20a=20config?= =?UTF-8?q?urable=20port=20=F0=9F=90=9B=20fix(launch.json):=20remove=20inv?= =?UTF-8?q?alid=20argument=20in=20debug=20configuration=20=F0=9F=90=9B=20f?= =?UTF-8?q?ix(Makefile):=20fix=20typo=20in=20lexer/index=20path=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(minishell.h):=20update=20timestamp=20in=20fi?= =?UTF-8?q?le=20header=20=F0=9F=90=9B=20fix(lexer/index.c):=20fix=20bug=20?= =?UTF-8?q?in=20check=5Fnext=5Fquote=20function=20=E2=9C=A8=20feat(lexer/i?= =?UTF-8?q?ndex.c):=20add=20support=20for=20parsing=20parantheses=20in=20l?= =?UTF-8?q?exer=20=E2=9C=A8=20feat(lexer/string.c):=20add=20functions=20to?= =?UTF-8?q?=20create=20string,=20quote=20string,=20and=20parantheses=20str?= =?UTF-8?q?ing=20in=20lexer=20=F0=9F=94=A5=20chore(lexer/utils.c):=20delet?= =?UTF-8?q?e=20unused=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 2 +- Makefile | 2 +- includes/minishell.h | 6 ++- src/lexer/index.c | 119 ++++++++++++++++++++++--------------------- src/lexer/string.c | 102 +++++++++++++++++++++++++++++++++++++ src/lexer/utils.c | 0 6 files changed, 169 insertions(+), 62 deletions(-) create mode 100644 src/lexer/string.c delete mode 100644 src/lexer/utils.c diff --git a/.vscode/launch.json b/.vscode/launch.json index abbccfe..314e0b2 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "name": "Debug", "program": "${workspaceFolder}/minishell", "args": [ - "'hello' 1 2 '34' grarg'5' 'laatste''echt' " + "'" ], "cwd": "${workspaceFolder}" } diff --git a/Makefile b/Makefile index f4b4c43..e80bf55 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = minishell -SRC = main check_input lexer/index utils/error structs/token parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector +SRC = main check_input utils/error structs/token parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string utils/error structs/token SRCS = $(addsuffix .c, $(addprefix src/, $(SRC))) OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a diff --git a/includes/minishell.h b/includes/minishell.h index ad9a350..f3efbec 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/07/20 16:46:36 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 17:01:08 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -29,6 +29,10 @@ /* input_check */ bool check_quotes_parantheses(char *input); bool lexer(char *input, t_vector *vec); +bool create_string(char *str, size_t *i, t_vector *vec); +bool create_quote_string(char *str, size_t *i, t_vector *vec); +bool create_paran_string(char *str, size_t *i, t_vector *vec); + /* structs */ t_token *create_token(char *value, int type); diff --git a/src/lexer/index.c b/src/lexer/index.c index 9b3502f..e43bad0 100644 --- a/src/lexer/index.c +++ b/src/lexer/index.c @@ -6,80 +6,81 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 13:32:55 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 13:36:55 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 17:02:59 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -static bool check_next_quote(char *str) +static bool check_next_quote(char *str, size_t *i) { char quote; - quote = *str; - str++; - while (*str != '\0' && *str != quote) - str++; - if (*str == '\0') + quote = str[(*i)]; + (*i)++; + while (str[(*i)]!= '\0' && str[(*i)] != quote) + (*i)++; + if (str[(*i)] == '\0') return (false); + (*i)++; return (true); -}; +} -static bool create_string(char *str, size_t *i, t_vector *vec) +static bool check_parantheses(char *str, size_t *i) { - size_t left; - size_t right; - char *value; + (*i)++; + while(str[*i] != '\0' && str[*i] != ')') + { + if (str[*i] == '(') + { + if (!check_parantheses(str, i)) + return (false); + } + else if (str[*i] == '\"' || str[*i] == '\'') + { + if (!check_next_quote(str, i)) + return (false); + } + else + (*i)++; + } + if (str[*i] == '\0') + return (false); + return (true); +} - right = (*i); - while (right > 0 && str[right] == ' ') - right--; - if (right > 0 && (str[right] == '\'' || str[right] == '\"')) - right--; - left = right; - if (left > 0) - left--; - if (left != 0) +static bool check_delimiters(char *str) +{ + size_t i; + + i = 0; + if (str[i] == '\'' || str[i] == '\"') { - while (left != 0 && - (str[left] != ' ' && str[left] != '\'' && str[left] != '\"')) - left--; - if (str[left] == ' ' || str[left] == '\'' || str[left] == '\"') - left++; - value = ft_substr(str, left, right - left + 1); - if (!value) + if (!check_next_quote(str, &i)) return (false); - if (!ft_vec_push(vec, (void *)create_token(value, 0))) + } + else if (str[i] == ')') + return (false); + else if (str[i] == '(') + { + if (!check_parantheses(str, &i)) return (false); } - while (str[*i] == ' ' && str[*i]) - (*i)++; return (true); } -/** - * @todo " hello'gmarmopg grnrga' 'hey'"; check spaces runback - */ -static bool create_quote_string(char *str, size_t *i, t_vector *vec) -{ - size_t occur_right; - char c; - char *value; - if ((*i) > 0 && (str[(*i) - 1] != ' ' && str[(*i) - 1] != '\'' && str[(*i) - 1] != '\"')) - create_string(str, i, vec); - c = str[*i]; - occur_right = (*i) + 1; - while (str[occur_right] && str[occur_right] != c) - occur_right++; - occur_right++; - value = ft_substr(str, *i, occur_right - (*i)); - if (!value) - return (false); - if (!ft_vec_push(vec, (void *)create_token(value, 0))) - return (false); - *i = occur_right; - while (str[*i] == ' ' && str[*i]) - (*i)++; +bool make_string(char *str, size_t *i, t_vector *vec) +{ + if (str[*i] == '\"' || str[*i] == '\'') + { + if (!create_quote_string(str, i, vec)) + return (false); + } + else if (str[*i] == '(') + { + if (!create_paran_string(str, i, vec)) + return (false); + } return (true); } @@ -90,11 +91,11 @@ bool lexer(char *input, t_vector *vec) i = 0; while (input[i]) { - if (input[i] == '\"' || input[i] == '\'') + if (input[i] == '\"' || input[i] == '\'' || input[i] == '(' || input[i] == ')') { - if (!check_next_quote(&input[i])) - return (err("unfinished quote", NULL, 1), false); - if (!create_quote_string(input, &i, vec)) + if (!check_delimiters(&input[i])) + return (err("bad quote(s) or parantheses", NULL, 1), false); + if (!make_string(input, &i, vec)) return (err("malloc", NULL, 1), false); } else if (input[i] == ' ') @@ -105,7 +106,7 @@ bool lexer(char *input, t_vector *vec) else i++; } - if (input[i - 1] != ' ' && input[i - 1] != '\'' && input[i - 1] != '\"') + if (input[i - 1] != ' ' && input[i - 1] != '\'' && input[i - 1] != '\"' && input[i - 1] != ')') if (!create_string(input, &i, vec)) return (err("malloc", NULL, 1), false); return (true); diff --git a/src/lexer/string.c b/src/lexer/string.c new file mode 100644 index 0000000..bfd44fc --- /dev/null +++ b/src/lexer/string.c @@ -0,0 +1,102 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* string.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/07/20 13:41:35 by mdekker/jde #+# #+# */ +/* Updated: 2023/07/20 16:35:25 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +bool create_string(char *str, size_t *i, t_vector *vec) +{ + size_t left; + size_t right; + char *value; + + right = (*i); + while (right > 0 && str[right] == ' ') + right--; + if (right > 0 && (str[right] == '\'' || str[right] == '\"' || str[right] == '(')) + right--; + left = right; + if (left > 0) + left--; + if (left != 0) + { + while (left != 0 && + (str[left] != ' ' && str[left] != '\'' && str[left] != '\"' && str[left] != ')')) + left--; + if (str[left] == ' ' || str[left] == '\'' || str[left] == '\"' || str[left] == ')') + left++; + value = ft_substr(str, left, right - left + 1); + if (!value) + return (false); + if (!ft_vec_push(vec, (void *)create_token(value, 0))) + return (false); + } + while (str[*i] == ' ' && str[*i]) + (*i)++; + return (true); +} + +/** + * @todo " hello'gmarmopg grnrga' 'hey'"; check spaces runback +*/ +bool create_quote_string(char *str, size_t *i, t_vector *vec) +{ + size_t occur_right; + char c; + char *value; + + if ((*i) > 0 && (str[(*i) - 1] != ' ' && str[(*i) - 1] != '\'' && str[(*i) - 1] != '\"' && str[(*i) - 1] != ')')) + create_string(str, i, vec); + c = str[*i]; + occur_right = (*i) + 1; + while (str[occur_right] && str[occur_right] != c) + occur_right++; + occur_right++; + value = ft_substr(str, *i, occur_right - (*i)); + if (!value) + return (false); + if (!ft_vec_push(vec, (void *)create_token(value, 0))) + return (false); + *i = occur_right; + while (str[*i] == ' ' && str[*i]) + (*i)++; + return (true); +} + + +bool create_paran_string(char *str, size_t *i, t_vector *vec) +{ + size_t occur_right; + int parantheses; + char *value; + + if ((*i) > 0 && (str[(*i) - 1] != ' ' && str[(*i) - 1] != '\'' && str[(*i) - 1] != '\"' && str[(*i) - 1] != ')')) + create_string(str, i, vec); + occur_right = (*i) + 1; + parantheses = 1; + while (str[occur_right] && parantheses > 0) + { + if (str[occur_right] == '(') + parantheses++; + else if (str[occur_right] == ')') + parantheses--; + occur_right++; + } + value = ft_substr(str, *i, occur_right - (*i)); + if (!value) + return (false); + if (!ft_vec_push(vec, (void *)create_token(value, 0))) + return (false); + *i = occur_right; + while (str[*i] == ' ' && str[*i]) + (*i)++; + return (true); +} diff --git a/src/lexer/utils.c b/src/lexer/utils.c deleted file mode 100644 index e69de29..0000000 From e3c5f8226884768041fe55c4d9d0cd3ef88c6272 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Thu, 20 Jul 2023 17:17:44 +0200 Subject: [PATCH 36/91] =?UTF-8?q?=F0=9F=90=9B=20fix(Makefile):=20remove=20?= =?UTF-8?q?unused=20file=20from=20source=20list=20=F0=9F=90=9B=20fix(enum.?= =?UTF-8?q?h):=20reorder=20enum=20values=20to=20match=20their=20correspond?= =?UTF-8?q?ing=20definitions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- includes/enum.h | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index e80bf55..8eaa4e8 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = minishell -SRC = main check_input utils/error structs/token parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string utils/error structs/token +SRC = main check_input utils/error structs/token parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string SRCS = $(addsuffix .c, $(addprefix src/, $(SRC))) OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a diff --git a/includes/enum.h b/includes/enum.h index 03ba3fe..2a7f6c3 100644 --- a/includes/enum.h +++ b/includes/enum.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/13 17:41:00 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 16:51:32 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 17:15:53 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,6 +34,7 @@ typedef enum e_types { UNKNOWN, + STRING, DOUBLE_QUOTE, SINGLE_QUOTE, PARENTHESES, @@ -43,7 +44,6 @@ typedef enum e_types AND, ENV, DQ_ENV, - STRING, O_REDIRECT, I_REDIRECT, O_HEREDOC, From af8cc97fa678dfe3c0be5df8dc0f2f0dec06e490 Mon Sep 17 00:00:00 2001 From: Julius de Baaij Date: Thu, 20 Jul 2023 17:28:07 +0200 Subject: [PATCH 37/91] =?UTF-8?q?=F0=9F=90=9B=20fix(launch.json):=20update?= =?UTF-8?q?=20args=20in=20Debug=20configuration=20to=20fix=20syntax=20erro?= =?UTF-8?q?r=20=F0=9F=90=9B=20fix(index.c):=20increment=20i=20after=20chec?= =?UTF-8?q?king=20for=20closing=20parentheses=20to=20prevent=20infinite=20?= =?UTF-8?q?loop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 2 +- src/lexer/index.c | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 314e0b2..9b4dbbc 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,7 +10,7 @@ "name": "Debug", "program": "${workspaceFolder}/minishell", "args": [ - "'" + "(()" ], "cwd": "${workspaceFolder}" } diff --git a/src/lexer/index.c b/src/lexer/index.c index e43bad0..b6ae384 100644 --- a/src/lexer/index.c +++ b/src/lexer/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 13:32:55 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 17:02:59 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 17:26:16 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -46,6 +46,7 @@ static bool check_parantheses(char *str, size_t *i) } if (str[*i] == '\0') return (false); + (*i)++; return (true); } From 335b2a49c0e029520e5e6bc03e88797e0875b38a Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Thu, 20 Jul 2023 18:16:39 +0200 Subject: [PATCH 38/91] =?UTF-8?q?=F0=9F=94=A7=20fix(minishell.h):=20update?= =?UTF-8?q?=20timestamp=20in=20file=20header=20comments=20=F0=9F=94=A7=20f?= =?UTF-8?q?ix(minishell.h):=20remove=20empty=20line=20=F0=9F=94=A7=20fix(p?= =?UTF-8?q?rint=5Fvector.c):=20fix=20indentation=20in=20print=5Ftoken=5Fve?= =?UTF-8?q?ctor=20function=20=F0=9F=94=A7=20fix(main.c):=20update=20timest?= =?UTF-8?q?amp=20in=20file=20header=20comments=20=F0=9F=94=A7=20fix(main.c?= =?UTF-8?q?):=20remove=20empty=20line=20=F0=9F=94=A7=20fix(parser/index.c)?= =?UTF-8?q?:=20update=20timestamp=20in=20file=20header=20comments=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(parser/index.c):=20update=20timestamp=20in?= =?UTF-8?q?=20return=5Fmap=20function=20=F0=9F=94=A7=20fix(parser/index.c)?= =?UTF-8?q?:=20update=20timestamp=20in=20parser=20function=20=F0=9F=94=A7?= =?UTF-8?q?=20fix(parser/index.c):=20remove=20empty=20line=20=F0=9F=94=A7?= =?UTF-8?q?=20fix(structs/token.c):=20fix=20indentation=20in=20create=5Fto?= =?UTF-8?q?ken=20function=20=F0=9F=94=A7=20fix(structs/token.c):=20fix=20i?= =?UTF-8?q?ndentation=20in=20clear=5Ftoken=20function=20=F0=9F=94=A7=20fix?= =?UTF-8?q?(utils/error.c):=20fix=20indentation=20in=20err=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/minishell.h | 3 +-- src/debug/print_vector.c | 6 +++--- src/main.c | 3 ++- src/parser/index.c | 42 +++++++++++++++++++++++++++++++--------- src/structs/token.c | 8 ++++---- src/utils/error.c | 12 ++++++------ 6 files changed, 49 insertions(+), 25 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index f3efbec..d49d0c9 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/07/20 17:01:08 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 18:11:23 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -33,7 +33,6 @@ bool create_string(char *str, size_t *i, t_vector *vec); bool create_quote_string(char *str, size_t *i, t_vector *vec); bool create_paran_string(char *str, size_t *i, t_vector *vec); - /* structs */ t_token *create_token(char *value, int type); void clear_token(void *data); diff --git a/src/debug/print_vector.c b/src/debug/print_vector.c index 6bad540..b89d0c2 100644 --- a/src/debug/print_vector.c +++ b/src/debug/print_vector.c @@ -17,10 +17,10 @@ * * @param tokens t_vector of t_token structs */ -void print_token_vector(t_vector *tokens) +void print_token_vector(t_vector *tokens) { - t_token *token; - size_t i; + t_token *token; + size_t i; i = 0; while (i < tokens->lenght) diff --git a/src/main.c b/src/main.c index fbddae7..bea20d5 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 16:52:59 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 18:10:35 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -26,6 +26,7 @@ static void debug(void) printf("\033[1;34m│\n"); printf("\033[0m"); } + /** * @brief The main loop of the program * diff --git a/src/parser/index.c b/src/parser/index.c index 2a3ee97..95e360a 100644 --- a/src/parser/index.c +++ b/src/parser/index.c @@ -6,12 +6,19 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 11:12:20 by mdekker #+# #+# */ -/* Updated: 2023/07/20 16:50:59 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 18:15:40 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include +/** + * @brief A cheeky way to return a struct with a function pointer and a type + * + * @return t_func_map* A pointer to a t_func_map struct + * @note The last element of the array is a struct with a NULL function pointer + * and a type of 0 so the parser knows when to stop looping over the array + */ t_func_map *return_map(void) { t_func_map *func_map; @@ -34,17 +41,15 @@ t_func_map *return_map(void) func_map[12] = (t_func_map){NULL, 0}; return (func_map); } -void parser(t_vector *vec) + +void parse_loop(t_vector *vec, t_func_map *func_map) { - t_token *token; - t_func_map *func_map; - size_t j; - size_t i; + t_token *token; + size_t i; + size_t j; - func_map = return_map(); - if (func_map == NULL) - return (err("Malloc failed", "parser", 1)); i = 0; + j = 0; while (i < vec->lenght) { token = (t_token *)ft_vec_get(vec, i); @@ -52,13 +57,32 @@ void parser(t_vector *vec) { j = 0; while (func_map[j].func != NULL) + { if (!func_map[j].func(token->value)) j++; else break ; + } token->type = func_map[j].type; } i++; } +} + +/** + * @brief Parses the tokens in the vector + * + * @note The parser will only parse tokens that have the type 0 so make sure + * to set the type of the tokens to 0 before calling this function + * @param vec The vector containing the tokens + */ +void parser(t_vector *vec) +{ + t_func_map *func_map; + + func_map = return_map(); + if (func_map == NULL) + return (err("Malloc failed", "parser", 1)); + parse_loop(vec, func_map); free(func_map); } diff --git a/src/structs/token.c b/src/structs/token.c index b78fc6a..6ad0a0a 100644 --- a/src/structs/token.c +++ b/src/structs/token.c @@ -12,9 +12,9 @@ #include -t_token *create_token(char *value, int type) +t_token *create_token(char *value, int type) { - t_token *token; + t_token *token; token = malloc(sizeof(t_token)); if (!token) @@ -24,9 +24,9 @@ t_token *create_token(char *value, int type) return (token); } -void clear_token(void *data) +void clear_token(void *data) { - t_token *token; + t_token *token; token = (t_token *)data; free(token->value); diff --git a/src/utils/error.c b/src/utils/error.c index 21bfda0..8e9fb6a 100644 --- a/src/utils/error.c +++ b/src/utils/error.c @@ -12,12 +12,12 @@ #include -void err(char *err, char *cmd, int exit_code) +void err(char *err, char *cmd, int exit_code) { - printf("\033[1;31m"); - printf("❗️ Error: %s\n", err); + printf("\033[1;31m"); + printf("❗️ Error: %s\n", err); if (cmd) - printf("In command: %s\n", cmd); - printf("Exit code: %d\n", exit_code); - printf("\033[0m"); + printf("In command: %s\n", cmd); + printf("Exit code: %d\n", exit_code); + printf("\033[0m"); } From 4c7d6eaa8a87e0591ecfa842a9acab26cb3404fb Mon Sep 17 00:00:00 2001 From: Julius de Baaij Date: Thu, 20 Jul 2023 18:35:39 +0200 Subject: [PATCH 39/91] =?UTF-8?q?=F0=9F=90=9B=20fix(lexer/index.c):=20fix?= =?UTF-8?q?=20indentation=20and=20spacing=20issues=20in=20check=5Fnext=5Fq?= =?UTF-8?q?uote=20and=20check=5Fparantheses=20functions=20=E2=9C=A8=20feat?= =?UTF-8?q?(lexer/index.c):=20improve=20code=20readability=20by=20using=20?= =?UTF-8?q?checkchar=20function=20to=20check=20for=20specific=20characters?= =?UTF-8?q?=20=F0=9F=90=9B=20fix(lexer/index.c):=20fix=20typo=20in=20lexer?= =?UTF-8?q?=20function,=20change=20checkchar=20condition=20from=201=20to?= =?UTF-8?q?=200=20=F0=9F=90=9B=20fix(lexer/string.c):=20fix=20indentation?= =?UTF-8?q?=20and=20spacing=20issues=20in=20create=5Fstring=20function=20?= =?UTF-8?q?=E2=9C=A8=20feat(lexer/string.c):=20refactor=20create=5Fstring?= =?UTF-8?q?=20function=20to=20improve=20code=20readability=20and=20maintai?= =?UTF-8?q?nability=20=F0=9F=90=9B=20fix(lexer/string.c):=20fix=20typo=20i?= =?UTF-8?q?n=20create=5Fquote=5Fstring=20function,=20change=20checkchar=20?= =?UTF-8?q?condition=20from=201=20to=200=20=F0=9F=90=9B=20fix(lexer/string?= =?UTF-8?q?.c):=20fix=20typo=20in=20create=5Fparan=5Fstring=20function,=20?= =?UTF-8?q?change=20checkchar=20condition=20from=201=20to=200?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lexer/index.c | 20 +++++------ src/lexer/string.c | 82 +++++++++++++++++++++++++--------------------- 2 files changed, 55 insertions(+), 47 deletions(-) diff --git a/src/lexer/index.c b/src/lexer/index.c index b6ae384..8ac4bd6 100644 --- a/src/lexer/index.c +++ b/src/lexer/index.c @@ -6,19 +6,19 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 13:32:55 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 17:26:16 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 18:33:58 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -static bool check_next_quote(char *str, size_t *i) +static bool check_next_quote(char *str, size_t *i) { - char quote; + char quote; quote = str[(*i)]; (*i)++; - while (str[(*i)]!= '\0' && str[(*i)] != quote) + while (str[(*i)] != '\0' && str[(*i)] != quote) (*i)++; if (str[(*i)] == '\0') return (false); @@ -29,7 +29,7 @@ static bool check_next_quote(char *str, size_t *i) static bool check_parantheses(char *str, size_t *i) { (*i)++; - while(str[*i] != '\0' && str[*i] != ')') + while (str[*i] != '\0' && str[*i] != ')') { if (str[*i] == '(') { @@ -42,7 +42,7 @@ static bool check_parantheses(char *str, size_t *i) return (false); } else - (*i)++; + (*i)++; } if (str[*i] == '\0') return (false); @@ -85,14 +85,14 @@ bool make_string(char *str, size_t *i, t_vector *vec) return (true); } -bool lexer(char *input, t_vector *vec) +bool lexer(char *input, t_vector *vec) { - size_t i; + size_t i; i = 0; while (input[i]) { - if (input[i] == '\"' || input[i] == '\'' || input[i] == '(' || input[i] == ')') + if (checkchar(input[i], "\"\'()") == 1) { if (!check_delimiters(&input[i])) return (err("bad quote(s) or parantheses", NULL, 1), false); @@ -107,7 +107,7 @@ bool lexer(char *input, t_vector *vec) else i++; } - if (input[i - 1] != ' ' && input[i - 1] != '\'' && input[i - 1] != '\"' && input[i - 1] != ')') + if (checkchar(input[i], "\"\') ") == 0) if (!create_string(input, &i, vec)) return (err("malloc", NULL, 1), false); return (true); diff --git a/src/lexer/string.c b/src/lexer/string.c index bfd44fc..492ba61 100644 --- a/src/lexer/string.c +++ b/src/lexer/string.c @@ -6,39 +6,48 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 13:41:35 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 16:35:25 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 18:30:31 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -bool create_string(char *str, size_t *i, t_vector *vec) +typedef struct s_local { - size_t left; - size_t right; - char *value; + int left; + int right; +} t_local; - right = (*i); - while (right > 0 && str[right] == ' ') - right--; - if (right > 0 && (str[right] == '\'' || str[right] == '\"' || str[right] == '(')) - right--; - left = right; - if (left > 0) - left--; - if (left != 0) - { - while (left != 0 && - (str[left] != ' ' && str[left] != '\'' && str[left] != '\"' && str[left] != ')')) - left--; - if (str[left] == ' ' || str[left] == '\'' || str[left] == '\"' || str[left] == ')') - left++; - value = ft_substr(str, left, right - left + 1); - if (!value) - return (false); - if (!ft_vec_push(vec, (void *)create_token(value, 0))) - return (false); - } +static bool build_string(char *str, size_t *i, t_vector *vec, t_local x) +{ + char *value; + + while (x.left != 0 && checkchar(str[*i], "\'\") ") == 0) + x.left--; + if (checkchar(str[*i], "\'\") ") == 1) + x.left++; + value = ft_substr(str, x.left, x.right - x.left + 1); + if (!value) + return (false); + if (!ft_vec_push(vec, (void *)create_token(value, 0))) + return (false); + return (true); +} + +bool create_string(char *str, size_t *i, t_vector *vec) +{ + t_local x; + + x.right = (*i); + while (x.right > 0 && str[x.right] == ' ') + x.right--; + if (x.right > 0 && checkchar(str[*i], "\'\"(") == 1) + x.right--; + x.left = x.right; + if (x.left > 0) + x.left--; + if (x.left != 0) + build_string(str, i, vec, x); while (str[*i] == ' ' && str[*i]) (*i)++; return (true); @@ -47,13 +56,13 @@ bool create_string(char *str, size_t *i, t_vector *vec) /** * @todo " hello'gmarmopg grnrga' 'hey'"; check spaces runback */ -bool create_quote_string(char *str, size_t *i, t_vector *vec) +bool create_quote_string(char *str, size_t *i, t_vector *vec) { - size_t occur_right; - char c; - char *value; + size_t occur_right; + char c; + char *value; - if ((*i) > 0 && (str[(*i) - 1] != ' ' && str[(*i) - 1] != '\'' && str[(*i) - 1] != '\"' && str[(*i) - 1] != ')')) + if ((*i) > 0 && checkchar(str[*i], "\'\") ") == 0) create_string(str, i, vec); c = str[*i]; occur_right = (*i) + 1; @@ -71,14 +80,13 @@ bool create_quote_string(char *str, size_t *i, t_vector *vec) return (true); } - -bool create_paran_string(char *str, size_t *i, t_vector *vec) +bool create_paran_string(char *str, size_t *i, t_vector *vec) { - size_t occur_right; - int parantheses; - char *value; + size_t occur_right; + int parantheses; + char *value; - if ((*i) > 0 && (str[(*i) - 1] != ' ' && str[(*i) - 1] != '\'' && str[(*i) - 1] != '\"' && str[(*i) - 1] != ')')) + if ((*i) > 0 && checkchar(str[*i], "\'\") ") == 0) create_string(str, i, vec); occur_right = (*i) + 1; parantheses = 1; From 59ecb843e6ed9f9d40e929fc698dfb05f72f1edc Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Thu, 20 Jul 2023 21:14:21 +0200 Subject: [PATCH 40/91] =?UTF-8?q?=F0=9F=90=9B=20fix(server.ts):=20change?= =?UTF-8?q?=20port=20variable=20case=20from=20lowercase=20port=20to=20uppe?= =?UTF-8?q?rcase=20PORT=20=E2=9C=A8=20feat(server.ts):=20add=20support=20f?= =?UTF-8?q?or=20process.env.PORT=20environment=20variable=20=F0=9F=94=A7?= =?UTF-8?q?=20chore(launch.json):=20update=20launch=20configuration=20argu?= =?UTF-8?q?ments=20=F0=9F=94=A7=20chore(Makefile):=20add=20utils/init=20to?= =?UTF-8?q?=20source=20files=20=F0=9F=94=A7=20chore(minishell.h):=20update?= =?UTF-8?q?=20last=20updated=20timestamp=20=F0=9F=94=A7=20chore(structs.h)?= =?UTF-8?q?:=20update=20last=20updated=20timestamp=20=F0=9F=94=A7=20chore(?= =?UTF-8?q?index.c):=20update=20last=20updated=20timestamp=20=F0=9F=94=A7?= =?UTF-8?q?=20chore(string.c):=20update=20last=20updated=20timestamp=20?= =?UTF-8?q?=F0=9F=94=A7=20chore(main.c):=20add=20global=20data=20variable?= =?UTF-8?q?=20and=20call=20init=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🎉 feat(init.c): add init function to initialize global data and signal struct The `init.c` file is added to the project and contains the `init` function. This function is responsible for initializing the global data and signal struct used in the project. It creates a signal struct with initial values for the `inte`, `quit`, and `pipe` fields. It also initializes two vectors, `tokens` and `env`, with initial capacities. Finally, it sets the `exit_status` field to "0" using `ft_strdup`. --- .vscode/launch.json | 4 ++-- Makefile | 2 +- includes/minishell.h | 4 +++- includes/structs.h | 49 +++++++++++++++++++++++++++++++++++++------- src/lexer/index.c | 4 ++-- src/lexer/string.c | 20 +++++++++--------- src/main.c | 22 ++++++++++---------- src/utils/init.c | 36 ++++++++++++++++++++++++++++++++ 8 files changed, 107 insertions(+), 34 deletions(-) create mode 100644 src/utils/init.c diff --git a/.vscode/launch.json b/.vscode/launch.json index 9b4dbbc..262f486 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -10,9 +10,9 @@ "name": "Debug", "program": "${workspaceFolder}/minishell", "args": [ - "(()" + "city test 'test' > < >> << || | && () " ], "cwd": "${workspaceFolder}" } ] -} \ No newline at end of file +} diff --git a/Makefile b/Makefile index 8eaa4e8..3eec93b 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = minishell -SRC = main check_input utils/error structs/token parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string +SRC = main check_input utils/error structs/token parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string utils/init SRCS = $(addsuffix .c, $(addprefix src/, $(SRC))) OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a diff --git a/includes/minishell.h b/includes/minishell.h index d49d0c9..5127a3e 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/07/20 18:11:23 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 20:28:56 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -26,6 +26,8 @@ # define DEBUG 0 # endif +bool init(void); + /* input_check */ bool check_quotes_parantheses(char *input); bool lexer(char *input, t_vector *vec); diff --git a/includes/structs.h b/includes/structs.h index 9ce1c90..742e725 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/07/20 16:26:31 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 20:30:18 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -24,9 +24,44 @@ */ typedef struct s_token { - t_types type; - char *value; -} t_token; + t_types type; + char *value; +} t_token; + +typedef struct env +{ + char *key; + char *value; +} t_env; + +/** + * @brief THe strt for signals + * + * @param quit The quit signal + * @param interrupt The interrupt signal + * @param pipe The pipe signal + */ +typedef struct s_signal +{ + bool quit; + bool inte; + bool pipe; +} t_signal; + +/** + * @brief The global struct + * + * @param tokens The tokens vector from the lexer and parser + * @param env The environment variables + */ +typedef struct s_global +{ + t_vector tokens; + t_vector env; + t_signal signal; + char *exit_status; + +} t_global; /** * @brief The struct for the parser functions. @@ -41,8 +76,8 @@ typedef struct s_token */ typedef struct s_func_map { - bool (*func)(char *); - t_types type; -} t_func_map; + bool (*func)(char *); + t_types type; +} t_func_map; #endif diff --git a/src/lexer/index.c b/src/lexer/index.c index 8ac4bd6..98c9fa5 100644 --- a/src/lexer/index.c +++ b/src/lexer/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 13:32:55 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 18:33:58 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 21:13:35 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -107,7 +107,7 @@ bool lexer(char *input, t_vector *vec) else i++; } - if (checkchar(input[i], "\"\') ") == 0) + if (i > 0 && checkchar(input[i - 1], "\"\') ") == 0) if (!create_string(input, &i, vec)) return (err("malloc", NULL, 1), false); return (true); diff --git a/src/lexer/string.c b/src/lexer/string.c index 492ba61..497e4aa 100644 --- a/src/lexer/string.c +++ b/src/lexer/string.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 13:41:35 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 18:30:31 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 21:12:29 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,17 +14,17 @@ typedef struct s_local { - int left; - int right; -} t_local; + int left; + int right; +} t_local; -static bool build_string(char *str, size_t *i, t_vector *vec, t_local x) +static bool build_string(char *str, t_vector *vec, t_local x) { char *value; - while (x.left != 0 && checkchar(str[*i], "\'\") ") == 0) + while (x.left != 0 && checkchar(str[x.left], "\'\") ") == 0) x.left--; - if (checkchar(str[*i], "\'\") ") == 1) + if (checkchar(str[x.left], "\'\") ") == 1) x.left++; value = ft_substr(str, x.left, x.right - x.left + 1); if (!value) @@ -47,7 +47,7 @@ bool create_string(char *str, size_t *i, t_vector *vec) if (x.left > 0) x.left--; if (x.left != 0) - build_string(str, i, vec, x); + build_string(str, vec, x); while (str[*i] == ' ' && str[*i]) (*i)++; return (true); @@ -62,7 +62,7 @@ bool create_quote_string(char *str, size_t *i, t_vector *vec) char c; char *value; - if ((*i) > 0 && checkchar(str[*i], "\'\") ") == 0) + if ((*i) > 0 && checkchar(str[(*i) - 1], " \'\")") == 0) create_string(str, i, vec); c = str[*i]; occur_right = (*i) + 1; @@ -86,7 +86,7 @@ bool create_paran_string(char *str, size_t *i, t_vector *vec) int parantheses; char *value; - if ((*i) > 0 && checkchar(str[*i], "\'\") ") == 0) + if ((*i) > 0 && checkchar(str[(*i) - 1], " \'\")") == 0) create_string(str, i, vec); occur_right = (*i) + 1; parantheses = 1; diff --git a/src/main.c b/src/main.c index bea20d5..6b0bca8 100644 --- a/src/main.c +++ b/src/main.c @@ -6,12 +6,14 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 18:10:35 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 20:38:55 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include +t_global g_data; + void check_leaks(void) { system("leaks minishell"); @@ -64,27 +66,25 @@ static void loop(t_vector *vec) int main(int ac, char **av, char **env) { - t_vector tokens; - (void)env; if (DEBUG) debug(); - ft_vec_init(&tokens, 5, sizeof(t_token)); + init(); if (ac == 2) { - if (!lexer(av[1], &tokens)) - return (ft_vec_free(&tokens, clear_token), 1); - parser(&tokens); - print_token_vector(&tokens); - ft_vec_free(&tokens, clear_token); + if (!lexer(av[1], &g_data.tokens)) + return (ft_vec_free(&g_data.tokens, clear_token), 1); + // parser(&g_data.tokens); + print_token_vector(&g_data.tokens); + ft_vec_free(&g_data.tokens, clear_token); return (0); } else if (ac == 1) - loop(&tokens); + loop(&g_data.tokens); else { printf("Too many arguments"); - ft_vec_free(&tokens, clear_token); + ft_vec_free(&g_data.tokens, clear_token); } return (0); } diff --git a/src/utils/init.c b/src/utils/init.c new file mode 100644 index 0000000..86b148e --- /dev/null +++ b/src/utils/init.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* init.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/07/20 20:09:52 by mdekker/jde #+# #+# */ +/* Updated: 2023/07/20 20:35:00 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +extern t_global g_data; + +static t_signal create_signal_struct(void) +{ + t_signal signal; + + signal.inte = false; + signal.quit = false; + signal.pipe = false; + return (signal); +} + +bool init(void) +{ + g_data.signal = create_signal_struct(); + ft_vec_init(&g_data.tokens, 3, sizeof(t_token)); + ft_vec_init(&g_data.env, 50, sizeof(t_env)); + g_data.exit_status = ft_strdup("0"); + if (!g_data.exit_status) + return (false); + return (true); +} From 6610f93af4378251ef12f612078cb2db0c7e8119 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Thu, 20 Jul 2023 21:15:34 +0200 Subject: [PATCH 41/91] =?UTF-8?q?=F0=9F=90=9B=20fix(main.c):=20uncomment?= =?UTF-8?q?=20parser=20function=20call=20=F0=9F=94=A5=20chore(main.c):=20r?= =?UTF-8?q?emove=20commented=20out=20code=20=F0=9F=94=80=20merge(main.c):?= =?UTF-8?q?=20merge=20changes=20from=20mdekker/jde=20branch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.c b/src/main.c index 6b0bca8..ecfdfb5 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 20:38:55 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/20 21:15:12 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -74,7 +74,7 @@ int main(int ac, char **av, char **env) { if (!lexer(av[1], &g_data.tokens)) return (ft_vec_free(&g_data.tokens, clear_token), 1); - // parser(&g_data.tokens); + parser(&g_data.tokens); print_token_vector(&g_data.tokens); ft_vec_free(&g_data.tokens, clear_token); return (0); From 9f3068908285efc6cec25a6e02b5738f3c325cb0 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Thu, 20 Jul 2023 23:22:52 +0200 Subject: [PATCH 42/91] =?UTF-8?q?=F0=9F=94=84=20chore(libft):=20update=20l?= =?UTF-8?q?ibft=20submodule=20to=20commit=203a821f1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libft | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libft b/libft index cbff6b6..3a821f1 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit cbff6b6c04ac529b89480b00d43b0d4533f0ac6c +Subproject commit 3a821f189b1cf51e8c922ec088b44a13ec5bcd1d From 6128e43a57cb6b32bd31bd623d2519ba9c156138 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Fri, 21 Jul 2023 02:51:29 +0200 Subject: [PATCH 43/91] =?UTF-8?q?=F0=9F=94=A8=20chore(libft):=20update=20s?= =?UTF-8?q?ubproject=20commit=20hash=20=F0=9F=90=9B=20fix(main.c):=20fix?= =?UTF-8?q?=20typo=20in=20comment=20=F0=9F=94=A8=20chore(main.c):=20add=20?= =?UTF-8?q?debug=20print=20statements=20for=20counting=20pipes=20in=20toke?= =?UTF-8?q?n=20vector=20=F0=9F=94=A8=20chore(main.c):=20add=20debug=20prin?= =?UTF-8?q?t=20statements=20for=20counting=20pipes=20in=20g=5Fdata.tokens?= =?UTF-8?q?=20vector?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libft | 2 +- src/main.c | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/libft b/libft index 3a821f1..22827c9 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 3a821f189b1cf51e8c922ec088b44a13ec5bcd1d +Subproject commit 22827c93e86ead48470f22413e029d221b7b22ff diff --git a/src/main.c b/src/main.c index ecfdfb5..57036d7 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 21:15:12 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 02:49:36 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -29,6 +29,14 @@ static void debug(void) printf("\033[0m"); } +static bool token_is_pipe(void *data) +{ + t_token *token; + + token = (t_token *)data; + return (token->type == PIPE); +} + /** * @brief The main loop of the program * @@ -59,8 +67,10 @@ static void loop(t_vector *vec) print_token_vector(vec); } free(input); + printf("\n\n\n\amount of pipes: %zu\n\n\n", ft_vec_count(vec, + token_is_pipe)); ft_vec_free(vec, clear_token); - ft_vec_init(vec, 5, sizeof(t_token)); + ft_vec_init(vec, 5, sizeof(t_token), clear_token); } } @@ -76,6 +86,8 @@ int main(int ac, char **av, char **env) return (ft_vec_free(&g_data.tokens, clear_token), 1); parser(&g_data.tokens); print_token_vector(&g_data.tokens); + printf("\n\n\n\amount of pipes: %zu\n\n\n", ft_vec_count(&g_data.tokens, + token_is_pipe)); ft_vec_free(&g_data.tokens, clear_token); return (0); } From 5aa4fd081ed8f69abecc6898d5289ae2c2c222ee Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Fri, 21 Jul 2023 02:52:20 +0200 Subject: [PATCH 44/91] =?UTF-8?q?=F0=9F=94=84=20chore(libft):=20update=20s?= =?UTF-8?q?ubproject=20commit=20hash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libft | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libft b/libft index 22827c9..c123c9c 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 22827c93e86ead48470f22413e029d221b7b22ff +Subproject commit c123c9cb20e8b6612e7ff0ba4f5b54bc9812d56f From 9a0fc6a9419a248c71e1bf12e161586d8030d35d Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Fri, 21 Jul 2023 03:55:41 +0200 Subject: [PATCH 45/91] =?UTF-8?q?=F0=9F=90=9B=20fix(server.ts):=20change?= =?UTF-8?q?=20port=20variable=20case=20from=20lowercase=20port=20to=20uppe?= =?UTF-8?q?rcase=20PORT=20=E2=9C=A8=20feat(server.ts):=20add=20support=20f?= =?UTF-8?q?or=20process.env.PORT=20environment=20variable=20=F0=9F=94=A7?= =?UTF-8?q?=20chore(.vscode/launch.json):=20add=20preLaunchTask=20"build"?= =?UTF-8?q?=20=F0=9F=94=A7=20chore(.vscode/tasks.json):=20add=20build=20ta?= =?UTF-8?q?sk=20=F0=9F=94=A7=20chore(Makefile):=20add=20structs/env=20to?= =?UTF-8?q?=20SRC=20variable=20=F0=9F=94=A7=20chore(includes/minishell.h):?= =?UTF-8?q?=20update=20init=20function=20signature=20=F0=9F=94=A7=20chore(?= =?UTF-8?q?libft):=20update=20submodule=20commit=20=F0=9F=94=A7=20chore(sr?= =?UTF-8?q?c/debug/print=5Fvector.c):=20update=20print=5Ftoken=5Fvector=20?= =?UTF-8?q?function=20name=20to=20print=5Fvector=20=F0=9F=94=A7=20chore(sr?= =?UTF-8?q?c/lexer/string.c):=20update=20ft=5Fvec=5Fpush=20calls=20to=20us?= =?UTF-8?q?e=20vec->push=20=F0=9F=94=A7=20chore(src/main.c):=20update=20ft?= =?UTF-8?q?=5Fvec=5Ffree=20calls=20to=20not=20pass=20clear=5Ftoken=20funct?= =?UTF-8?q?ion=20=F0=9F=94=A7=20chore(src/main.c):=20update=20init=20funct?= =?UTF-8?q?ion=20call=20to=20pass=20env=20parameter=20=F0=9F=94=A7=20chore?= =?UTF-8?q?(src/main.c):=20update=20ft=5Fvec?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🐛 fix(parser/index.c): update timestamp in file header comment 🐛 fix(parser/index.c): update token retrieval method in parse_loop function ✨ feat(structs/env.c): add implementation for create_env function ✨ feat(structs/env.c): add implementation for print_env function ✨ feat(structs/env.c): add implementation for clear_env function 🐛 fix(structs/token.c): update timestamp in file header comment ✨ feat(structs/token.c): add implementation for print_token function ✨ feat(structs/token.c): add implementation for clear_token function 🐛 fix(utils/init.c): update timestamp in file header comment ✨ feat(utils/init.c): add implementation for init_env function ✨ feat(utils/init.c): call init_env function in init function --- .vscode/launch.json | 1 + .vscode/tasks.json | 17 +++++++++++++ Makefile | 2 +- includes/minishell.h | 11 +++++---- libft | 2 +- src/debug/print_vector.c | 26 +++++++------------- src/lexer/string.c | 8 +++---- src/main.c | 51 +++++++++++++++++++++++---------------- src/parser/index.c | 4 ++-- src/structs/env.c | 52 ++++++++++++++++++++++++++++++++++++++++ src/structs/token.c | 20 +++++++++++++++- src/utils/init.c | 28 ++++++++++++++++++---- 12 files changed, 166 insertions(+), 56 deletions(-) create mode 100644 .vscode/tasks.json create mode 100644 src/structs/env.c diff --git a/.vscode/launch.json b/.vscode/launch.json index 262f486..5bacc57 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,6 +9,7 @@ "request": "launch", "name": "Debug", "program": "${workspaceFolder}/minishell", + "preLaunchTask": "build", "args": [ "city test 'test' > < >> << || | && () " ], diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..b0e9333 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,17 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "type": "shell", + "command": "make re DEBUG=1", + "group": { + "kind": "build", + "isDefault": true + }, + "problemMatcher": [ + "$tsc" + ] + }, + ] +} diff --git a/Makefile b/Makefile index 3eec93b..36c9dfb 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = minishell -SRC = main check_input utils/error structs/token parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string utils/init +SRC = main check_input utils/error structs/token parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string utils/init structs/env SRCS = $(addsuffix .c, $(addprefix src/, $(SRC))) OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a diff --git a/includes/minishell.h b/includes/minishell.h index 5127a3e..defcc51 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/07/20 20:28:56 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 03:21:24 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ # define DEBUG 0 # endif -bool init(void); +bool init(char **envp); /* input_check */ bool check_quotes_parantheses(char *input); @@ -37,7 +37,11 @@ bool create_paran_string(char *str, size_t *i, t_vector *vec); /* structs */ t_token *create_token(char *value, int type); +void print_token(void *data, size_t i); void clear_token(void *data); +t_env *create_env(char *key, char *value); +void print_env(void *data, size_t i); +void clear_env(void *data); /* utils */ void err(char *err, char *cmd, int exit_code); @@ -55,6 +59,5 @@ bool contains_env_var(char *str); bool is_or(char *str); bool is_and(char *str); /* debug */ -void print_token_vector(t_vector *tokens); - +void print_vector(t_vector *vec, void (*printer)(void *, size_t)); #endif diff --git a/libft b/libft index c123c9c..86da02c 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit c123c9cb20e8b6612e7ff0ba4f5b54bc9812d56f +Subproject commit 86da02c973abd1b84224871b22494ba56cd45dcd diff --git a/src/debug/print_vector.c b/src/debug/print_vector.c index b89d0c2..711153b 100644 --- a/src/debug/print_vector.c +++ b/src/debug/print_vector.c @@ -6,36 +6,26 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 13:51:45 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 13:52:28 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 03:47:23 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include /** - * @brief Prints a t_token vector in a pretty way + * @brief Prints data in a vector in a pretty way * - * @param tokens t_vector of t_token structs + * @param vec The vector to print + * @param printer The function to print the data */ -void print_token_vector(t_vector *tokens) +void print_vector(t_vector *vec, void (*printer)(void *, size_t)) { - t_token *token; size_t i; i = 0; - while (i < tokens->lenght) + while (i < vec->lenght) { - token = (t_token *)ft_vec_get(tokens, i); - if (token != NULL) - { - printf("\033[1;32m●\n"); - printf("\033[1;34m│\n"); - printf("├── Token %zu:\n", i++); - printf("│ ├── Value: %s\n", token->value); - printf("│ ├── Type: %i\n", token->type); - printf("│ └── Adress: %p\n", token); - printf("\033[1;34m│\n"); - printf("\033[0m"); - } + printer(vec->get(vec, i), i); + i++; } } diff --git a/src/lexer/string.c b/src/lexer/string.c index 497e4aa..4e761c4 100644 --- a/src/lexer/string.c +++ b/src/lexer/string.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 13:41:35 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 21:12:29 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 03:03:11 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ static bool build_string(char *str, t_vector *vec, t_local x) value = ft_substr(str, x.left, x.right - x.left + 1); if (!value) return (false); - if (!ft_vec_push(vec, (void *)create_token(value, 0))) + if (!vec->push(vec, (void *)create_token(value, 0))) return (false); return (true); } @@ -72,7 +72,7 @@ bool create_quote_string(char *str, size_t *i, t_vector *vec) value = ft_substr(str, *i, occur_right - (*i)); if (!value) return (false); - if (!ft_vec_push(vec, (void *)create_token(value, 0))) + if (!vec->push(vec, (void *)create_token(value, 0))) return (false); *i = occur_right; while (str[*i] == ' ' && str[*i]) @@ -101,7 +101,7 @@ bool create_paran_string(char *str, size_t *i, t_vector *vec) value = ft_substr(str, *i, occur_right - (*i)); if (!value) return (false); - if (!ft_vec_push(vec, (void *)create_token(value, 0))) + if (!vec->push(vec, (void *)create_token(value, 0))) return (false); *i = occur_right; while (str[*i] == ' ' && str[*i]) diff --git a/src/main.c b/src/main.c index 57036d7..5891b77 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 02:49:36 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 03:40:06 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -29,13 +29,13 @@ static void debug(void) printf("\033[0m"); } -static bool token_is_pipe(void *data) -{ - t_token *token; +// static bool token_is_pipe(void *data) +// { +// t_token *token; - token = (t_token *)data; - return (token->type == PIPE); -} +// token = (t_token *)data; +// return (token->type == PIPE); +// } /** * @brief The main loop of the program @@ -58,37 +58,46 @@ static void loop(t_vector *vec) } add_history(input); if (!ft_strcmp(input, "exit")) - return (free(input), ft_vec_free(vec, clear_token)); + return (free(input), ft_vec_free(vec)); else { if (!lexer(input, vec)) - return (free(input), ft_vec_free(vec, clear_token)); + return (free(input), ft_vec_free(vec)); parser(vec); - print_token_vector(vec); + print_vector(vec, print_token); } free(input); - printf("\n\n\n\amount of pipes: %zu\n\n\n", ft_vec_count(vec, - token_is_pipe)); - ft_vec_free(vec, clear_token); + ft_vec_free(vec); ft_vec_init(vec, 5, sizeof(t_token), clear_token); } } +// static bool is_path(void *data) +// { +// char *str; + +// str = ((t_env *)data)->key; +// if (ft_strcmp(str, "PATH") == 0) +// return (true); +// return (false); +// } + +// print_env((void *)g_data.env.find(&g_data.env, is_path), 0); + int main(int ac, char **av, char **env) { - (void)env; if (DEBUG) debug(); - init(); + init(env); if (ac == 2) { if (!lexer(av[1], &g_data.tokens)) - return (ft_vec_free(&g_data.tokens, clear_token), 1); + return (ft_vec_free(&g_data.tokens), 1); parser(&g_data.tokens); - print_token_vector(&g_data.tokens); - printf("\n\n\n\amount of pipes: %zu\n\n\n", ft_vec_count(&g_data.tokens, - token_is_pipe)); - ft_vec_free(&g_data.tokens, clear_token); + print_vector(&g_data.tokens, print_token); + print_vector(&g_data.env, print_env); + ft_vec_free(&g_data.env); + ft_vec_free(&g_data.tokens); return (0); } else if (ac == 1) @@ -96,7 +105,7 @@ int main(int ac, char **av, char **env) else { printf("Too many arguments"); - ft_vec_free(&g_data.tokens, clear_token); + ft_vec_free(&g_data.tokens); } return (0); } diff --git a/src/parser/index.c b/src/parser/index.c index 95e360a..0ac89f5 100644 --- a/src/parser/index.c +++ b/src/parser/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 11:12:20 by mdekker #+# #+# */ -/* Updated: 2023/07/20 18:15:40 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 02:59:56 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -52,7 +52,7 @@ void parse_loop(t_vector *vec, t_func_map *func_map) j = 0; while (i < vec->lenght) { - token = (t_token *)ft_vec_get(vec, i); + token = (t_token *)vec->get(vec, i); if (token->type == 0) { j = 0; diff --git a/src/structs/env.c b/src/structs/env.c new file mode 100644 index 0000000..9ade3d5 --- /dev/null +++ b/src/structs/env.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* env.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/07/21 02:54:34 by mdekker/jde #+# #+# */ +/* Updated: 2023/07/21 03:54:58 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +t_env *create_env(char *key, char *value) +{ + t_env *env; + + env = malloc(sizeof(t_env)); + if (!env) + return (NULL); + env->key = key; + env->value = value; + return (env); +} + +void print_env(void *data, size_t i) +{ + t_env *env; + + env = (t_env *)data; + if (i == 0) + printf("\033[1;36m├── Enviroment Vector 👇\n"); + else + printf("\033[1;38m●\n"); + printf("\033[1;36m│\n"); + printf("├── Env %zu:\n", i); + printf("│ ├── Key: %s\n", env->key); + printf("│ ├── Value: %s\n", env->value); + printf("│ └── Adress: %p\n", env); + printf("\033[1;36m│\n"); + printf("\033[0m"); +} + +void clear_env(void *data) +{ + t_env *env; + + env = (t_env *)data; + free(env->key); + free(env->value); +} diff --git a/src/structs/token.c b/src/structs/token.c index 6ad0a0a..3d7bced 100644 --- a/src/structs/token.c +++ b/src/structs/token.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 22:36:40 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 14:06:29 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 03:54:56 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -24,6 +24,24 @@ t_token *create_token(char *value, int type) return (token); } +void print_token(void *data, size_t i) +{ + t_token *token; + + token = (t_token *)data; + if (i == 0) + printf("\033[1;34m├── Token Vector 👇\n"); + else + printf("\033[1;32m●\n"); + printf("\033[1;34m│\n"); + printf("├── Token %zu:\n", i); + printf("│ ├── Value: %s\n", token->value); + printf("│ ├── Type: %i\n", token->type); + printf("│ └── Adress: %p\n", token); + printf("\033[1;34m│\n"); + printf("\033[0m"); +} + void clear_token(void *data) { t_token *token; diff --git a/src/utils/init.c b/src/utils/init.c index 86b148e..6823b42 100644 --- a/src/utils/init.c +++ b/src/utils/init.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 20:09:52 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 20:35:00 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 03:32:39 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,6 +14,25 @@ extern t_global g_data; +static void init_env(char **envp) +{ + size_t i; + char *key; + char *value; + + i = 0; + while (envp[i] != NULL) + { + key = ft_substr(envp[i], 0, ft_strchr(envp[i], '=') - envp[i]); + value = ft_strdup(ft_strchr(envp[i], '=') + 1); + if (!key || !value) + exit(1); + if (!g_data.env.push(&g_data.env, create_env(key, value))) + exit(1); + i++; + } +} + static t_signal create_signal_struct(void) { t_signal signal; @@ -24,13 +43,14 @@ static t_signal create_signal_struct(void) return (signal); } -bool init(void) +bool init(char **env) { g_data.signal = create_signal_struct(); - ft_vec_init(&g_data.tokens, 3, sizeof(t_token)); - ft_vec_init(&g_data.env, 50, sizeof(t_env)); + ft_vec_init(&g_data.tokens, 3, sizeof(t_token), clear_token); + ft_vec_init(&g_data.env, 50, sizeof(t_env), clear_env); g_data.exit_status = ft_strdup("0"); if (!g_data.exit_status) return (false); + init_env(env); return (true); } From 97137362fe7c58795f9032e8c3b4243fbbb5301e Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Fri, 21 Jul 2023 04:12:33 +0200 Subject: [PATCH 46/91] =?UTF-8?q?=F0=9F=94=A7=20fix(main.c):=20update=20ch?= =?UTF-8?q?eck=5Fleaks=20function=20to=20include=20visual=20formatting=20a?= =?UTF-8?q?nd=20message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main.c b/src/main.c index 5891b77..8762d86 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 03:40:06 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 04:11:47 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -16,6 +16,11 @@ t_global g_data; void check_leaks(void) { + printf("\033[1;10m●\n"); + printf("\033[1;12m│\n"); + printf("├── Checking for leaks 💦\n"); + printf("\033[1;12m│\n"); + printf("\033[0m\n"); system("leaks minishell"); } From 45df33fc18c08609ae6c38931e5dd4894f3fee05 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Fri, 21 Jul 2023 04:16:45 +0200 Subject: [PATCH 47/91] =?UTF-8?q?=F0=9F=94=A5=20refactor(main.c):=20remove?= =?UTF-8?q?=20unused=20code=20and=20commented=20out=20function=20?= =?UTF-8?q?=F0=9F=94=92=20chore(main.c):=20free=20g=5Fdata.env=20before=20?= =?UTF-8?q?exiting=20loop?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main.c | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/main.c b/src/main.c index 8762d86..e04bdfc 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 04:11:47 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 04:14:54 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -63,7 +63,7 @@ static void loop(t_vector *vec) } add_history(input); if (!ft_strcmp(input, "exit")) - return (free(input), ft_vec_free(vec)); + return (free(input), ft_vec_free(vec), ft_vec_free(&g_data.env)); else { if (!lexer(input, vec)) @@ -73,22 +73,11 @@ static void loop(t_vector *vec) } free(input); ft_vec_free(vec); + ft_vec_free(&g_data.env); ft_vec_init(vec, 5, sizeof(t_token), clear_token); } } -// static bool is_path(void *data) -// { -// char *str; - -// str = ((t_env *)data)->key; -// if (ft_strcmp(str, "PATH") == 0) -// return (true); -// return (false); -// } - -// print_env((void *)g_data.env.find(&g_data.env, is_path), 0); - int main(int ac, char **av, char **env) { if (DEBUG) From 306131d2f253a133ff4ceb437e934e0e190c8cc7 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Fri, 21 Jul 2023 04:42:04 +0200 Subject: [PATCH 48/91] =?UTF-8?q?=F0=9F=90=9B=20fix(Makefile):=20add=20mis?= =?UTF-8?q?sing=20file=20'utils/global'=20to=20SRC=20variable=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(includes/minishell.h):=20update=20timestamp?= =?UTF-8?q?=20in=20file=20header=20=F0=9F=90=9B=20fix(includes/structs.h):?= =?UTF-8?q?=20update=20timestamp=20in=20file=20header=20=F0=9F=90=9B=20fix?= =?UTF-8?q?(src/lexer/index.c):=20update=20timestamp=20in=20file=20header?= =?UTF-8?q?=20=F0=9F=90=9B=20fix(src/lexer/string.c):=20update=20timestamp?= =?UTF-8?q?=20in=20file=20header=20=E2=9C=A8=20feat(includes/minishell.h):?= =?UTF-8?q?=20add=20declaration=20for=20free=5Fglobal=20function=20?= =?UTF-8?q?=E2=9C=A8=20feat(src/lexer/index.c):=20add=20check=5Fnext=5Fquo?= =?UTF-8?q?te=20function=20=E2=9C=A8=20feat(src/lexer/index.c):=20add=20ch?= =?UTF-8?q?eck=5Fparantheses=20function=20=E2=9C=A8=20feat(src/lexer/index?= =?UTF-8?q?.c):=20add=20check=5Fdelimiters=20function=20=E2=9C=A8=20feat(s?= =?UTF-8?q?rc/lexer/index.c):=20add=20make=5Fstring=20function=20=E2=9C=A8?= =?UTF-8?q?=20feat(src/lexer/index.c):=20add=20lexer=20function=20?= =?UTF-8?q?=E2=9C=A8=20feat(src/lexer/string.c):=20add=20build=5Fstring=20?= =?UTF-8?q?function=20=E2=9C=A8=20feat(src/lexer/string?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🐛 fix(main.c): update last modified timestamp in main.c 🐛 fix(main.c): update free_global function calls in loop and main functions 🐛 fix(parser/index.c): fix size of func_map array in return_map function 🐛 fix(parser/quotes.c): add missing function descriptions 🐛 fix(parser/tokens.c): add missing function descriptions 🐛 fix(parser/tokens2.c): add missing function descriptions 🐛 fix(env.c): fix typo in comment 🐛 fix(token.c): fix typo in comment 🐛 fix(error.c): fix typo in comment ✨ feat(global.c): add function to free global variables ✨ feat(init.c): add function to initialize global variables and environment --- Makefile | 2 +- includes/minishell.h | 5 ++++- includes/structs.h | 20 +++++++++++++++----- src/lexer/index.c | 44 +++++++++++++++++++++++++++++++++++++++++++- src/lexer/string.c | 42 +++++++++++++++++++++++++++++++++++++++--- src/main.c | 12 +++++------- src/parser/index.c | 11 ++++++++--- src/parser/quotes.c | 23 ++++++++++++++++++++++- src/parser/tokens.c | 37 ++++++++++++++++++++++++++++++++++++- src/parser/tokens2.c | 23 ++++++++++++++++++++++- src/structs/env.c | 20 +++++++++++++++++++- src/structs/token.c | 20 +++++++++++++++++++- src/utils/error.c | 9 ++++++++- src/utils/global.c | 28 ++++++++++++++++++++++++++++ src/utils/init.c | 33 ++++++++++++++++++++++++++------- 15 files changed, 295 insertions(+), 34 deletions(-) create mode 100644 src/utils/global.c diff --git a/Makefile b/Makefile index 36c9dfb..4830d12 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = minishell -SRC = main check_input utils/error structs/token parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string utils/init structs/env +SRC = main check_input utils/error structs/token parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string utils/init structs/env utils/global SRCS = $(addsuffix .c, $(addprefix src/, $(SRC))) OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a diff --git a/includes/minishell.h b/includes/minishell.h index defcc51..8d56b1d 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/07/21 03:21:24 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 04:20:30 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -60,4 +60,7 @@ bool is_or(char *str); bool is_and(char *str); /* debug */ void print_vector(t_vector *vec, void (*printer)(void *, size_t)); + +/* global */ +void free_global(bool exit); #endif diff --git a/includes/structs.h b/includes/structs.h index 742e725..4a1430e 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/07/20 20:30:18 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 04:24:55 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,6 +21,8 @@ * @param value The value of the token * @param prev The previous token * @param next The next token + * + * @note The value is malloced */ typedef struct s_token { @@ -28,6 +30,14 @@ typedef struct s_token char *value; } t_token; +/** + * @brief The struct for the environment variables + * + * @param key The key of the variable + * @param value The value of the variable + * + * @note The key and value are both malloced + */ typedef struct env { char *key; @@ -35,11 +45,11 @@ typedef struct env } t_env; /** - * @brief THe strt for signals + * @brief The struct for signals * - * @param quit The quit signal - * @param interrupt The interrupt signal - * @param pipe The pipe signal + * @param quit The quit signal (ctrl + D) + * @param inte The interrupt signal (ctrl + C) + * @param pipe The pipe signal (ctrl + \) */ typedef struct s_signal { diff --git a/src/lexer/index.c b/src/lexer/index.c index 98c9fa5..d4beefb 100644 --- a/src/lexer/index.c +++ b/src/lexer/index.c @@ -6,12 +6,20 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 13:32:55 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 21:13:35 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 04:40:38 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include +/** + * @brief Checks if the next quote is valid + * + * @param str The string to check + * @param i The index of the string to check + * @return true The next quote is valid + * @return false The next quote is not valid + */ static bool check_next_quote(char *str, size_t *i) { char quote; @@ -26,6 +34,14 @@ static bool check_next_quote(char *str, size_t *i) return (true); } +/** + * @brief Checks if the parantheses are valid + * + * @param str The string to check + * @param i The index of the string to check + * @return true The parantheses are valid + * @return false The parantheses are not valid + */ static bool check_parantheses(char *str, size_t *i) { (*i)++; @@ -50,6 +66,13 @@ static bool check_parantheses(char *str, size_t *i) return (true); } +/** + * @brief Checks if the delimiters are valid + * + * @param str The string to check + * @return true The delimiters are valid + * @return false The delimiters are not valid + */ static bool check_delimiters(char *str) { size_t i; @@ -70,6 +93,15 @@ static bool check_delimiters(char *str) return (true); } +/** + * @brief Creates a string from another string + * + * @param str The string to create a string from + * @param i The index of the string to create + * @param vec The vector to store the string in + * @return true The string was succesfully stored + * @return false The string could not be stored + */ bool make_string(char *str, size_t *i, t_vector *vec) { if (str[*i] == '\"' || str[*i] == '\'') @@ -85,6 +117,16 @@ bool make_string(char *str, size_t *i, t_vector *vec) return (true); } +/** + * @brief Splits a string into tokens + * + * @param input The string to split + * @param vec The vector to store the tokens in + * @return true The string was succesfully split + * @return false The string could not be split + * + * @note The vector must be initialized before calling this function + */ bool lexer(char *input, t_vector *vec) { size_t i; diff --git a/src/lexer/string.c b/src/lexer/string.c index 4e761c4..216a9f7 100644 --- a/src/lexer/string.c +++ b/src/lexer/string.c @@ -6,18 +6,30 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 13:41:35 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 03:03:11 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 04:40:27 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include +/** + * @brief Struct to store the left and right index of a string + */ typedef struct s_local { int left; int right; } t_local; +/** + * @brief Builds a string from a given string + * + * @param str The string to build from + * @param vec The vector to push the string to + * @param x The left and right index of the string + * @return true The string was built and pushed to the vector + * @return false The string was not built and not pushed to the vector + */ static bool build_string(char *str, t_vector *vec, t_local x) { char *value; @@ -34,6 +46,15 @@ static bool build_string(char *str, t_vector *vec, t_local x) return (true); } +/** + * @brief Create a string from different characters + * + * @param str The string to create from + * @param i The index of the string to create from + * @param vec The vector to push the string to + * @return true The string was created and pushed to the vector + * @return false The string was not created and not pushed to the vector + */ bool create_string(char *str, size_t *i, t_vector *vec) { t_local x; @@ -54,8 +75,14 @@ bool create_string(char *str, size_t *i, t_vector *vec) } /** - * @todo " hello'gmarmopg grnrga' 'hey'"; check spaces runback -*/ + * @brief Create a quote string object + * + * @param str The string to create from + * @param i The index of the string to create from + * @param vec The vector to push the string to + * @return true The string was created and pushed to the vector + * @return false The string was not created and not pushed to the vector + */ bool create_quote_string(char *str, size_t *i, t_vector *vec) { size_t occur_right; @@ -80,6 +107,15 @@ bool create_quote_string(char *str, size_t *i, t_vector *vec) return (true); } +/** + * @brief Create a paran string object + * + * @param str The string to create from + * @param i The index of the string to create from + * @param vec The vector to push the string to + * @return true The string was created and pushed to the vector + * @return false The string was not created and not pushed to the vector + */ bool create_paran_string(char *str, size_t *i, t_vector *vec) { size_t occur_right; diff --git a/src/main.c b/src/main.c index e04bdfc..a56f0e4 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 04:14:54 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 04:21:16 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -63,7 +63,7 @@ static void loop(t_vector *vec) } add_history(input); if (!ft_strcmp(input, "exit")) - return (free(input), ft_vec_free(vec), ft_vec_free(&g_data.env)); + return (free(input), free_global(true)); else { if (!lexer(input, vec)) @@ -72,8 +72,7 @@ static void loop(t_vector *vec) print_vector(vec, print_token); } free(input); - ft_vec_free(vec); - ft_vec_free(&g_data.env); + free_global(false); ft_vec_init(vec, 5, sizeof(t_token), clear_token); } } @@ -90,8 +89,7 @@ int main(int ac, char **av, char **env) parser(&g_data.tokens); print_vector(&g_data.tokens, print_token); print_vector(&g_data.env, print_env); - ft_vec_free(&g_data.env); - ft_vec_free(&g_data.tokens); + free_global(false); return (0); } else if (ac == 1) @@ -99,7 +97,7 @@ int main(int ac, char **av, char **env) else { printf("Too many arguments"); - ft_vec_free(&g_data.tokens); + free_global(true); } return (0); } diff --git a/src/parser/index.c b/src/parser/index.c index 0ac89f5..76f5b50 100644 --- a/src/parser/index.c +++ b/src/parser/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 11:12:20 by mdekker #+# #+# */ -/* Updated: 2023/07/21 02:59:56 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 04:35:10 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ t_func_map *return_map(void) { t_func_map *func_map; - func_map = (t_func_map *)malloc(sizeof(t_func_map) * 13); + func_map = (t_func_map *)malloc(sizeof(t_func_map) * 12); if (func_map == NULL) return (NULL); func_map[0] = (t_func_map){is_encased_dq, DOUBLE_QUOTE}; @@ -38,10 +38,15 @@ t_func_map *return_map(void) func_map[9] = (t_func_map){is_r_hd, O_HEREDOC}; func_map[10] = (t_func_map){is_l_hd, I_HEREDOC}; func_map[11] = (t_func_map){NULL, STRING}; - func_map[12] = (t_func_map){NULL, 0}; return (func_map); } +/** + * @brief Loops over the tokens in the vector and parses them + * + * @param vec The vector containing the tokens + * @param func_map The array of structs with function pointers and types + */ void parse_loop(t_vector *vec, t_func_map *func_map) { t_token *token; diff --git a/src/parser/quotes.c b/src/parser/quotes.c index 226cb14..5852f01 100644 --- a/src/parser/quotes.c +++ b/src/parser/quotes.c @@ -6,12 +6,19 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 11:37:28 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 16:46:11 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 04:34:01 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include +/** + * @brief Checks if a string is encased in double quotes + * + * @param str The string to check + * @return true The string is encased in double quotes + * @return false The string is not encased in double quotes + */ bool is_encased_dq(char *str) { if (str[0] == '\"' && str[ft_strlen(str) - 1] == '\"') @@ -19,6 +26,13 @@ bool is_encased_dq(char *str) return (false); } +/** + * @brief Checks if a string is encased in single quotes + * + * @param str The string to check + * @return true The string is encased in single quotes + * @return false The string is not encased in single quotes + */ bool is_encased_sq(char *str) { if (str[0] == '\'' && str[ft_strlen(str) - 1] == '\'') @@ -26,6 +40,13 @@ bool is_encased_sq(char *str) return (false); } +/** + * @brief Checks if a string is encased in parentheses + * + * @param str The string to check + * @return true The string is encased in parentheses + * @return false The string is not encased in parentheses + */ bool is_encased_parentheses(char *str) { if (str[0] == '(' && str[ft_strlen(str) - 1] == ')') diff --git a/src/parser/tokens.c b/src/parser/tokens.c index 0d03238..a0fd622 100644 --- a/src/parser/tokens.c +++ b/src/parser/tokens.c @@ -6,12 +6,19 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 14:50:46 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 16:28:50 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 04:33:03 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include +/** + * @brief Checks if a string contains a PIPE + * + * @param str The string to check + * @return true The string contains a PIPE + * @return false The string does not contain a PIPE + */ bool is_pipe(char *str) { if (ft_strcmp(str, "|") == 0) @@ -19,6 +26,13 @@ bool is_pipe(char *str) return (false); } +/** + * @brief Checks if a string contains a REDIRECT + * + * @param str The string to check + * @return true The string contains a REDIRECT + * @return false The string does not contain a REDIRECT + */ bool is_r_redirect(char *str) { if (ft_strcmp(str, ">") == 0) @@ -26,6 +40,13 @@ bool is_r_redirect(char *str) return (false); } +/** + * @brief Checks if a string contains a REDIRECT + * + * @param str The string to check + * @return true The string contains a REDIRECT + * @return false The string does not contain a REDIRECT + */ bool is_l_redirect(char *str) { if (ft_strcmp(str, "<") == 0) @@ -33,6 +54,13 @@ bool is_l_redirect(char *str) return (false); } +/** + * @brief Checks if a string contains a HEREDOC + * + * @param str The string to check + * @return true The string contains a HEREDOC + * @return false The string does not contain a HEREDOC + */ bool is_r_hd(char *str) { if (ft_strcmp(str, ">>") == 0) @@ -40,6 +68,13 @@ bool is_r_hd(char *str) return (false); } +/** + * @brief Checks if a string contains a HEREDOC + * + * @param str The string to check + * @return true The string contains a HEREDOC + * @return false The string does not contain a HEREDOC + */ bool is_l_hd(char *str) { if (ft_strcmp(str, "<<") == 0) diff --git a/src/parser/tokens2.c b/src/parser/tokens2.c index 1ed3493..4898265 100644 --- a/src/parser/tokens2.c +++ b/src/parser/tokens2.c @@ -6,12 +6,19 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 16:29:02 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 16:29:52 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 04:31:43 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include +/** + * @brief Checks if a string contains an enviroment variable + * + * @param str The string to check + * @return true The string contains an enviroment variable + * @return false The string does not contain an enviroment variable + */ bool contains_env_var(char *str) { if (ft_strchr(str, '$')) @@ -19,6 +26,13 @@ bool contains_env_var(char *str) return (false); } +/** + * @brief Checks if a string contains an AND + * + * @param str The string to check + * @return true The string contains an AND + * @return false The string does not contain an AND + */ bool is_and(char *str) { if (ft_strcmp(str, "&&") == 0) @@ -26,6 +40,13 @@ bool is_and(char *str) return (false); } +/** + * @brief Checks if a string contains an OR + * + * @param str The string to check + * @return true The string contains an OR + * @return false The string does not contain an OR + */ bool is_or(char *str) { if (ft_strcmp(str, "||") == 0) diff --git a/src/structs/env.c b/src/structs/env.c index 9ade3d5..92c44f4 100644 --- a/src/structs/env.c +++ b/src/structs/env.c @@ -6,12 +6,19 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/21 02:54:34 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 03:54:58 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 04:30:06 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include +/** + * @brief Create a env object + * + * @param key The key of the env + * @param value The value of the env + * @return t_env* The created env object + */ t_env *create_env(char *key, char *value) { t_env *env; @@ -24,6 +31,12 @@ t_env *create_env(char *key, char *value) return (env); } +/** + * @brief Prints a env struct + * + * @param data The env struct + * @param i The index of the env struct + */ void print_env(void *data, size_t i) { t_env *env; @@ -42,6 +55,11 @@ void print_env(void *data, size_t i) printf("\033[0m"); } +/** + * @brief Clears a env struct + * + * @param data The env struct + */ void clear_env(void *data) { t_env *env; diff --git a/src/structs/token.c b/src/structs/token.c index 3d7bced..a12e5bb 100644 --- a/src/structs/token.c +++ b/src/structs/token.c @@ -6,12 +6,19 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 22:36:40 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 03:54:56 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 04:29:02 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include +/** + * @brief Create a token object + * + * @param value A string containing the value of the token + * @param type The type of the token + * @return t_token* The created token + */ t_token *create_token(char *value, int type) { t_token *token; @@ -24,6 +31,12 @@ t_token *create_token(char *value, int type) return (token); } +/** + * @brief Prints a token struct + * + * @param data The token struct + * @param i The index of the token + */ void print_token(void *data, size_t i) { t_token *token; @@ -42,6 +55,11 @@ void print_token(void *data, size_t i) printf("\033[0m"); } +/** + * @brief Clears a token struct + * + * @param data The token struct + */ void clear_token(void *data) { t_token *token; diff --git a/src/utils/error.c b/src/utils/error.c index 8e9fb6a..eabd13c 100644 --- a/src/utils/error.c +++ b/src/utils/error.c @@ -6,12 +6,19 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 21:42:24 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/19 21:42:31 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 04:41:35 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include +/** + * @brief Prints an error message + * + * @param err The error message + * @param cmd The command that caused the error + * @param exit_code The exit code of the error + */ void err(char *err, char *cmd, int exit_code) { printf("\033[1;31m"); diff --git a/src/utils/global.c b/src/utils/global.c new file mode 100644 index 0000000..3de0faa --- /dev/null +++ b/src/utils/global.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* global.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/07/21 04:17:23 by mdekker/jde #+# #+# */ +/* Updated: 2023/07/21 04:25:46 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +extern t_global g_data; + +/** + * @brief Frees the global variables + * + * @param exit If is true, the exit status will be freed + */ +void free_global(bool exit) +{ + ft_vec_free(&g_data.tokens); + ft_vec_free(&g_data.env); + if (exit) + free(g_data.exit_status); +} diff --git a/src/utils/init.c b/src/utils/init.c index 6823b42..cae9617 100644 --- a/src/utils/init.c +++ b/src/utils/init.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 20:09:52 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 03:32:39 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 04:27:48 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,17 +14,22 @@ extern t_global g_data; -static void init_env(char **envp) +/** + * @brief Initializes the enviroment variables + * + * @param env The enviroment variables + */ +static void init_env(char **env) { size_t i; char *key; char *value; i = 0; - while (envp[i] != NULL) + while (env[i] != NULL) { - key = ft_substr(envp[i], 0, ft_strchr(envp[i], '=') - envp[i]); - value = ft_strdup(ft_strchr(envp[i], '=') + 1); + key = ft_substr(env[i], 0, ft_strchr(env[i], '=') - env[i]); + value = ft_strdup(ft_strchr(env[i], '=') + 1); if (!key || !value) exit(1); if (!g_data.env.push(&g_data.env, create_env(key, value))) @@ -33,6 +38,11 @@ static void init_env(char **envp) } } +/** + * @brief Create a signal struct object + * + * @return t_signal The created signal struct + */ static t_signal create_signal_struct(void) { t_signal signal; @@ -43,11 +53,20 @@ static t_signal create_signal_struct(void) return (signal); } +/** + * @brief Initializes the global variables + * + * @param env The enviroment variables + * @return true When the initialization was succesfull + * @return false When the initialization failed + */ bool init(char **env) { g_data.signal = create_signal_struct(); - ft_vec_init(&g_data.tokens, 3, sizeof(t_token), clear_token); - ft_vec_init(&g_data.env, 50, sizeof(t_env), clear_env); + if (!ft_vec_init(&g_data.tokens, 3, sizeof(t_token), clear_token)) + return (false); + if (!ft_vec_init(&g_data.env, 50, sizeof(t_env), clear_env)) + return (false); g_data.exit_status = ft_strdup("0"); if (!g_data.exit_status) return (false); From decfc2c932d747e7ef6cfa0397ff4db5c438666e Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Fri, 21 Jul 2023 12:58:26 +0200 Subject: [PATCH 49/91] =?UTF-8?q?=F0=9F=90=9B=20fix(global.c):=20free=20en?= =?UTF-8?q?v=20vector=20when=20exit=20is=20true=20in=20free=5Fglobal=20fun?= =?UTF-8?q?ction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/utils/global.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/utils/global.c b/src/utils/global.c index 3de0faa..1eae36b 100644 --- a/src/utils/global.c +++ b/src/utils/global.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/21 04:17:23 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 04:25:46 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 12:51:05 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,12 +17,14 @@ extern t_global g_data; /** * @brief Frees the global variables * - * @param exit If is true, the exit status will be freed + * @param exit If is true, the exit status and env vector will be freed */ void free_global(bool exit) { ft_vec_free(&g_data.tokens); - ft_vec_free(&g_data.env); if (exit) + { + ft_vec_free(&g_data.env); free(g_data.exit_status); + } } From ba6ea2c0039ddd4808e755897b72dac49f335f60 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Fri, 21 Jul 2023 13:04:32 +0200 Subject: [PATCH 50/91] =?UTF-8?q?=F0=9F=94=84=20chore(libft):=20update=20s?= =?UTF-8?q?ubproject=20commit=20hash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libft | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libft b/libft index 86da02c..67ed497 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 86da02c973abd1b84224871b22494ba56cd45dcd +Subproject commit 67ed4977de18057be68c921b082f0aa669f3fd09 From 3c1311f783c8536a8fedf45d319e3f1b37202bee Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Fri, 21 Jul 2023 17:24:42 +0200 Subject: [PATCH 51/91] =?UTF-8?q?=F0=9F=94=84=20chore(libft):=20update=20s?= =?UTF-8?q?ubproject=20commit=20hash?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libft | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libft b/libft index 67ed497..aa403d8 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 67ed4977de18057be68c921b082f0aa669f3fd09 +Subproject commit aa403d8e7e17ba0053fdcbba4d111e6e5f42a1d3 From fef0dbdaa20036651aa74fd1f550c90e0ede8790 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Fri, 21 Jul 2023 17:50:12 +0200 Subject: [PATCH 52/91] =?UTF-8?q?=F0=9F=90=9B=20fix(minishell.h):=20change?= =?UTF-8?q?=20function=20signature=20of=20check=5Fquotes=5Fparantheses=20?= =?UTF-8?q?=E2=9C=A8=20feat(minishell.h):=20add=20new=20function=20parse?= =?UTF-8?q?=5Fone=20=F0=9F=90=9B=20fix(print=5Fvector.c):=20change=20funct?= =?UTF-8?q?ion=20signature=20of=20print=5Ftype=20=F0=9F=90=9B=20fix(main.c?= =?UTF-8?q?):=20change=20condition=20for=20printing=20env=20vector=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(index.c):=20change=20condition=20for=20check?= =?UTF-8?q?ing=20unknown=20token=20type=20=F0=9F=90=9B=20fix(token.c):=20c?= =?UTF-8?q?hange=20function=20signature=20of=20create=5Ftoken=20and=20prin?= =?UTF-8?q?t=5Ftoken?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/minishell.h | 14 +++++++----- libft | 2 +- src/debug/print_vector.c | 48 +++++++++++++++++++++++++++++++++++++++- src/main.c | 5 +++-- src/parser/index.c | 23 +++++++++++++++++-- src/structs/token.c | 6 ++--- 6 files changed, 84 insertions(+), 14 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index 8d56b1d..6aa418a 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/07/21 04:20:30 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 17:49:32 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -29,24 +29,24 @@ bool init(char **envp); /* input_check */ -bool check_quotes_parantheses(char *input); bool lexer(char *input, t_vector *vec); +bool check_quotes_parantheses(char *input); bool create_string(char *str, size_t *i, t_vector *vec); bool create_quote_string(char *str, size_t *i, t_vector *vec); bool create_paran_string(char *str, size_t *i, t_vector *vec); /* structs */ -t_token *create_token(char *value, int type); -void print_token(void *data, size_t i); +t_token *create_token(char *value, t_types type); void clear_token(void *data); t_env *create_env(char *key, char *value); -void print_env(void *data, size_t i); void clear_env(void *data); + /* utils */ void err(char *err, char *cmd, int exit_code); /* parser */ void parser(t_vector *vec); +void parse_one(t_token *token); bool is_encased_dq(char *str); bool is_encased_sq(char *str); bool is_encased_parentheses(char *str); @@ -58,8 +58,12 @@ bool is_l_hd(char *str); bool contains_env_var(char *str); bool is_or(char *str); bool is_and(char *str); + /* debug */ void print_vector(t_vector *vec, void (*printer)(void *, size_t)); +void print_token(void *data, size_t i); +char *print_type(t_types type); +void print_env(void *data, size_t i); /* global */ void free_global(bool exit); diff --git a/libft b/libft index aa403d8..b804781 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit aa403d8e7e17ba0053fdcbba4d111e6e5f42a1d3 +Subproject commit b8047815b1484156db39153b5e2c77e62427cf2a diff --git a/src/debug/print_vector.c b/src/debug/print_vector.c index 711153b..b2e8dfe 100644 --- a/src/debug/print_vector.c +++ b/src/debug/print_vector.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 13:51:45 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 03:47:23 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 17:43:28 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -29,3 +29,49 @@ void print_vector(t_vector *vec, void (*printer)(void *, size_t)) i++; } } + +static char *extra_types(t_types type) +{ + if (type == AND) + return ("AND"); + if (type == ENV) + return ("ENV"); + if (type == DQ_ENV) + return ("DQ_ENV"); + if (type == STRING) + return ("STRING"); + if (type == O_REDIRECT) + return ("O_REDIRECT"); + if (type == I_REDIRECT) + return ("I_REDIRECT"); + if (type == O_HEREDOC) + return ("O_HEREDOC"); + if (type == I_HEREDOC) + return ("I_HEREDOC"); + return (""); +} + +/** + * @brief Prints type names when provided with a token type + * + * @param type The t_types enum + * @return char* The name of the type + */ +char *print_type(t_types type) +{ + if (type == DOUBLE_QUOTE) + return ("DOUBLE_QUOTE"); + if (type == SINGLE_QUOTE) + return ("SINGLE_QUOTE"); + if (type == PIPE) + return ("PIPE"); + if (type == PARENTHESES) + return ("PARENTHESES"); + if (type == SEMICOLON) + return ("SEMICOLON"); + if (type == OR) + return ("OR"); + if (extra_types(type)[0] != '\0') + return (extra_types(type)); + return ("UNKNOWN"); +} diff --git a/src/main.c b/src/main.c index a56f0e4..b3187ea 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 04:21:16 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 17:45:26 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -88,7 +88,8 @@ int main(int ac, char **av, char **env) return (ft_vec_free(&g_data.tokens), 1); parser(&g_data.tokens); print_vector(&g_data.tokens, print_token); - print_vector(&g_data.env, print_env); + if (DEBUG) + print_vector(&g_data.env, print_env); free_global(false); return (0); } diff --git a/src/parser/index.c b/src/parser/index.c index 76f5b50..67b12e9 100644 --- a/src/parser/index.c +++ b/src/parser/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 11:12:20 by mdekker #+# #+# */ -/* Updated: 2023/07/21 04:35:10 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 17:48:58 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -58,7 +58,7 @@ void parse_loop(t_vector *vec, t_func_map *func_map) while (i < vec->lenght) { token = (t_token *)vec->get(vec, i); - if (token->type == 0) + if (token->type == UNKNOWN) { j = 0; while (func_map[j].func != NULL) @@ -91,3 +91,22 @@ void parser(t_vector *vec) parse_loop(vec, func_map); free(func_map); } + +void parse_one(t_token *token) +{ + t_func_map *func_map; + + func_map = return_map(); + if (func_map == NULL) + return (err("Malloc failed", "parser", 1)); + while (func_map->func != NULL) + { + if (func_map->func(token->value)) + { + token->type = func_map->type; + break ; + } + func_map++; + } + free(func_map); +} diff --git a/src/structs/token.c b/src/structs/token.c index a12e5bb..5238dfe 100644 --- a/src/structs/token.c +++ b/src/structs/token.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 22:36:40 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 04:29:02 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 17:44:48 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ * @param type The type of the token * @return t_token* The created token */ -t_token *create_token(char *value, int type) +t_token *create_token(char *value, t_types type) { t_token *token; @@ -49,7 +49,7 @@ void print_token(void *data, size_t i) printf("\033[1;34m│\n"); printf("├── Token %zu:\n", i); printf("│ ├── Value: %s\n", token->value); - printf("│ ├── Type: %i\n", token->type); + printf("│ ├── Type: %s\n", print_type(token->type)); printf("│ └── Adress: %p\n", token); printf("\033[1;34m│\n"); printf("\033[0m"); From 8d9b48445a051ea0e6698b22c83d40d7da1f3ef8 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Fri, 21 Jul 2023 18:30:55 +0200 Subject: [PATCH 53/91] =?UTF-8?q?=F0=9F=94=A7=20fix(enum.h):=20add=20ENV?= =?UTF-8?q?=5FQUESTION=20enum=20value=20to=20the=20list=20of=20types=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(minishell.h):=20add=20is=5Fenv=5Fquestionmar?= =?UTF-8?q?k=20function=20declaration=20=F0=9F=94=A7=20fix(print=5Fvector.?= =?UTF-8?q?c):=20add=20ENV=5FQUESTION=20case=20to=20print=5Ftype=20functio?= =?UTF-8?q?n=20=F0=9F=94=A7=20fix(index.c):=20add=20ENV=5FQUESTION=20to=20?= =?UTF-8?q?the=20func=5Fmap=20array=20=F0=9F=94=A7=20fix(tokens2.c):=20add?= =?UTF-8?q?=20is=5Fenv=5Fquestionmark=20function=20implementation?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/enum.h | 4 +++- includes/minishell.h | 4 ++-- src/debug/print_vector.c | 4 +++- src/parser/index.c | 23 ++++++++++++----------- src/parser/tokens2.c | 9 ++++++++- 5 files changed, 28 insertions(+), 16 deletions(-) diff --git a/includes/enum.h b/includes/enum.h index 2a7f6c3..ca2f048 100644 --- a/includes/enum.h +++ b/includes/enum.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/13 17:41:00 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/20 17:15:53 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 18:23:49 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -25,6 +25,7 @@ * @param AND (&&) * @param ENV ($) * @param DQ_ENV An environment variable in double quotes ("$") + * @param ENV_QUESTION An environment variable with a question mark ("$?") * @param STRING A string * @param O_REDIRECT (>) * @param I_REDIRECT (<) @@ -44,6 +45,7 @@ typedef enum e_types AND, ENV, DQ_ENV, + ENV_QUESTION, O_REDIRECT, I_REDIRECT, O_HEREDOC, diff --git a/includes/minishell.h b/includes/minishell.h index 6aa418a..c45517e 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/07/21 17:49:32 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 18:24:37 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -58,7 +58,7 @@ bool is_l_hd(char *str); bool contains_env_var(char *str); bool is_or(char *str); bool is_and(char *str); - +bool is_env_questionmark(char *str); /* debug */ void print_vector(t_vector *vec, void (*printer)(void *, size_t)); void print_token(void *data, size_t i); diff --git a/src/debug/print_vector.c b/src/debug/print_vector.c index b2e8dfe..145f735 100644 --- a/src/debug/print_vector.c +++ b/src/debug/print_vector.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 13:51:45 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 17:43:28 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 18:25:14 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -71,6 +71,8 @@ char *print_type(t_types type) return ("SEMICOLON"); if (type == OR) return ("OR"); + if (type == ENV_QUESTION) + return ("ENV_QUESTION"); if (extra_types(type)[0] != '\0') return (extra_types(type)); return ("UNKNOWN"); diff --git a/src/parser/index.c b/src/parser/index.c index 67b12e9..7627134 100644 --- a/src/parser/index.c +++ b/src/parser/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 11:12:20 by mdekker #+# #+# */ -/* Updated: 2023/07/21 17:48:58 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 18:26:19 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,21 +23,22 @@ t_func_map *return_map(void) { t_func_map *func_map; - func_map = (t_func_map *)malloc(sizeof(t_func_map) * 12); + func_map = (t_func_map *)malloc(sizeof(t_func_map) * 13); if (func_map == NULL) return (NULL); func_map[0] = (t_func_map){is_encased_dq, DOUBLE_QUOTE}; func_map[1] = (t_func_map){is_encased_sq, SINGLE_QUOTE}; func_map[2] = (t_func_map){is_encased_parentheses, PARENTHESES}; - func_map[3] = (t_func_map){contains_env_var, ENV}; - func_map[4] = (t_func_map){is_and, AND}; - func_map[5] = (t_func_map){is_or, OR}; - func_map[6] = (t_func_map){is_pipe, PIPE}; - func_map[7] = (t_func_map){is_r_redirect, O_REDIRECT}; - func_map[8] = (t_func_map){is_l_redirect, I_REDIRECT}; - func_map[9] = (t_func_map){is_r_hd, O_HEREDOC}; - func_map[10] = (t_func_map){is_l_hd, I_HEREDOC}; - func_map[11] = (t_func_map){NULL, STRING}; + func_map[3] = (t_func_map){is_env_questionmark, ENV_QUESTION}; + func_map[4] = (t_func_map){contains_env_var, ENV}; + func_map[5] = (t_func_map){is_and, AND}; + func_map[6] = (t_func_map){is_or, OR}; + func_map[7] = (t_func_map){is_pipe, PIPE}; + func_map[8] = (t_func_map){is_r_redirect, O_REDIRECT}; + func_map[9] = (t_func_map){is_l_redirect, I_REDIRECT}; + func_map[10] = (t_func_map){is_r_hd, O_HEREDOC}; + func_map[11] = (t_func_map){is_l_hd, I_HEREDOC}; + func_map[12] = (t_func_map){NULL, STRING}; return (func_map); } diff --git a/src/parser/tokens2.c b/src/parser/tokens2.c index 4898265..7a9236a 100644 --- a/src/parser/tokens2.c +++ b/src/parser/tokens2.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 16:29:02 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 04:31:43 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 18:23:15 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -53,3 +53,10 @@ bool is_or(char *str) return (true); return (false); } + +bool is_env_questionmark(char *str) +{ + if (ft_strncmp(str, "$?", 2) == 0) + return (true); + return (false); +} From 4f8fcb479eadb6a723dda5fada4ba6e2faa01ab1 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Fri, 21 Jul 2023 19:52:43 +0200 Subject: [PATCH 54/91] =?UTF-8?q?=F0=9F=94=A7=20fix(minishell.h):=20remove?= =?UTF-8?q?=20unused=20is=5Fenv=5Fquestionmark=20function=20=F0=9F=94=A7?= =?UTF-8?q?=20fix(parser/index.c):=20reduce=20size=20of=20func=5Fmap=20arr?= =?UTF-8?q?ay=20from=2013=20to=2011=20=F0=9F=94=A7=20fix(parser/tokens2.c)?= =?UTF-8?q?:=20remove=20is=5Fenv=5Fquestionmark=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/minishell.h | 3 +-- src/parser/index.c | 22 ++++++++++------------ src/parser/tokens2.c | 11 ++--------- 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index c45517e..1c09007 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/07/21 18:24:37 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 19:48:33 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -58,7 +58,6 @@ bool is_l_hd(char *str); bool contains_env_var(char *str); bool is_or(char *str); bool is_and(char *str); -bool is_env_questionmark(char *str); /* debug */ void print_vector(t_vector *vec, void (*printer)(void *, size_t)); void print_token(void *data, size_t i); diff --git a/src/parser/index.c b/src/parser/index.c index 7627134..805481c 100644 --- a/src/parser/index.c +++ b/src/parser/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 11:12:20 by mdekker #+# #+# */ -/* Updated: 2023/07/21 18:26:19 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 19:51:10 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,22 +23,20 @@ t_func_map *return_map(void) { t_func_map *func_map; - func_map = (t_func_map *)malloc(sizeof(t_func_map) * 13); + func_map = (t_func_map *)malloc(sizeof(t_func_map) * 11); if (func_map == NULL) return (NULL); func_map[0] = (t_func_map){is_encased_dq, DOUBLE_QUOTE}; func_map[1] = (t_func_map){is_encased_sq, SINGLE_QUOTE}; func_map[2] = (t_func_map){is_encased_parentheses, PARENTHESES}; - func_map[3] = (t_func_map){is_env_questionmark, ENV_QUESTION}; - func_map[4] = (t_func_map){contains_env_var, ENV}; - func_map[5] = (t_func_map){is_and, AND}; - func_map[6] = (t_func_map){is_or, OR}; - func_map[7] = (t_func_map){is_pipe, PIPE}; - func_map[8] = (t_func_map){is_r_redirect, O_REDIRECT}; - func_map[9] = (t_func_map){is_l_redirect, I_REDIRECT}; - func_map[10] = (t_func_map){is_r_hd, O_HEREDOC}; - func_map[11] = (t_func_map){is_l_hd, I_HEREDOC}; - func_map[12] = (t_func_map){NULL, STRING}; + func_map[3] = (t_func_map){is_and, AND}; + func_map[4] = (t_func_map){is_or, OR}; + func_map[5] = (t_func_map){is_pipe, PIPE}; + func_map[6] = (t_func_map){is_r_redirect, O_REDIRECT}; + func_map[7] = (t_func_map){is_l_redirect, I_REDIRECT}; + func_map[8] = (t_func_map){is_r_hd, O_HEREDOC}; + func_map[9] = (t_func_map){is_l_hd, I_HEREDOC}; + func_map[10] = (t_func_map){NULL, STRING}; return (func_map); } diff --git a/src/parser/tokens2.c b/src/parser/tokens2.c index 7a9236a..48ba742 100644 --- a/src/parser/tokens2.c +++ b/src/parser/tokens2.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 16:29:02 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 18:23:15 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 19:48:27 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ */ bool contains_env_var(char *str) { - if (ft_strchr(str, '$')) + if (str[0] == '$') return (true); return (false); } @@ -53,10 +53,3 @@ bool is_or(char *str) return (true); return (false); } - -bool is_env_questionmark(char *str) -{ - if (ft_strncmp(str, "$?", 2) == 0) - return (true); - return (false); -} From 2d6f4217272dc13ea7687ac0bf26029b0ee96069 Mon Sep 17 00:00:00 2001 From: Julius de Baaij Date: Fri, 21 Jul 2023 19:58:41 +0200 Subject: [PATCH 55/91] =?UTF-8?q?=F0=9F=90=9B=20fix(launch.json):=20update?= =?UTF-8?q?=20program=20path=20to=20include=20current=20directory=20for=20?= =?UTF-8?q?minishell=20=F0=9F=90=9B=20fix(Makefile):=20add=20missing=20lex?= =?UTF-8?q?er/token.c=20file=20to=20the=20source=20files=20list=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(minishell.h):=20add=20missing=20declaration?= =?UTF-8?q?=20for=20operator=5Fsplit=20function=20=E2=9C=A8=20feat(lexer/i?= =?UTF-8?q?ndex.c):=20update=20timestamp=20in=20file=20header=20comment=20?= =?UTF-8?q?=E2=9C=A8=20feat(lexer/token.c):=20add=20implementation=20for?= =?UTF-8?q?=20operator=5Fsplit=20function=20to=20split=20tokens=20with=20o?= =?UTF-8?q?perators=20=E2=9C=A8=20feat(main.c):=20update=20timestamp=20in?= =?UTF-8?q?=20file=20header=20comment=20and=20add=20call=20to=20operator?= =?UTF-8?q?=5Fsplit=20function=20after=20parsing=20tokens?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 6 +- Makefile | 2 +- includes/minishell.h | 3 +- src/.DS_Store | Bin 0 -> 6148 bytes src/lexer/index.c | 2 +- src/lexer/token.c | 186 +++++++++++++++++++++++++++++++++++++++++++ src/main.c | 10 ++- 7 files changed, 199 insertions(+), 10 deletions(-) create mode 100644 src/.DS_Store create mode 100644 src/lexer/token.c diff --git a/.vscode/launch.json b/.vscode/launch.json index 5bacc57..1d67ad8 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,12 +8,12 @@ "type": "lldb", "request": "launch", "name": "Debug", - "program": "${workspaceFolder}/minishell", + "program": "${workspaceFolder}/./minishell", "preLaunchTask": "build", "args": [ - "city test 'test' > < >> << || | && () " + "hello|test|split +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/07/21 19:48:33 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 19:58:27 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,6 +34,7 @@ bool check_quotes_parantheses(char *input); bool create_string(char *str, size_t *i, t_vector *vec); bool create_quote_string(char *str, size_t *i, t_vector *vec); bool create_paran_string(char *str, size_t *i, t_vector *vec); +bool operator_split(t_vector *vec); /* structs */ t_token *create_token(char *value, t_types type); diff --git a/src/.DS_Store b/src/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..dab7208db9fdf6d6af7ad048fdb67a149018acfd GIT binary patch literal 6148 zcmeHK%Wl&^6upz?;ZRk|0;Fz`EU}GBNTE{IjY-RbEk>{a6zn)^t-7Ab4}ug$^1A#3 zzrdC+;a^z6xieGAsp%FXG*_BA^ElVzIoC7eAtEs;qbEcIB65%z+xL+D!MLAO!&;_= z{e*f(K{2J2zf9z;!L|)n0jt2aDZp#jp^!?f+=OcDH%w%RKHeCfBOUxwh7!>p&FF&W z$S-NCR{R3~1s%|gSANa&+bz!yMeyU%&NrFjVS|}~`57s@#r$$NGhF5iGy3;%k;Hj1 z8vPKhopz_&b9&CM^TEH6OTP$;GM@z5JFdN!G6`?WLHH(~FQ~kz7t>nMS$t^>v5Scly)8{%Uo2{A}oskH+hvyE-0^hVIkDqxHJ)Jb3i@t zxk}|V-X12fUn<)(cmq7U4kBUZ{>jhMM5gCxWsfdhQJ@rWXf1xCzaFJ7Ex^qvZ)(`$ z=heAAunKTX#I(@+cP)w>|F5<9cT}|bdr>=mjmhz@Gf}m+wF;PT(AnMEH?%Ki6|f5Y zD+PFeaFG~2gOx_Lbs$q$0AL%<%24KC2KI3cdIl?v=z$6C3e>K`Trq@pN4slyp213^ zb|+yjAHs|*%ne1T(eZp&(Mfn3ZEh8?3T!H{qpJfx|6kmF|KD`7HLHMC;J;Epw9ovr z38rMu*2d)ctaXt-BC#>A(x_aJnd4X$_$b~-QieX43qa3cr4cPK`y-%au$fihuPX2x DbU(2> literal 0 HcmV?d00001 diff --git a/src/lexer/index.c b/src/lexer/index.c index d4beefb..9b55ac5 100644 --- a/src/lexer/index.c +++ b/src/lexer/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 13:32:55 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 04:40:38 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 18:16:06 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/lexer/token.c b/src/lexer/token.c new file mode 100644 index 0000000..5dab888 --- /dev/null +++ b/src/lexer/token.c @@ -0,0 +1,186 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* token.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/07/21 13:06:18 by mdekker/jde #+# #+# */ +/* Updated: 2023/07/21 19:55:58 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +static bool find_operator(t_token *token) +{ + int i; + + i = 0; + while(token->value[i] && !checkchar(token->value[i], "<>|&")) + i++; + if (token->value[i] == '\0') + return (false); + return (true); +} + +/* +static bool operator_err(char *str) +{ + // & -> fail + // aan het einde | of || of && -> vec->index == i, "unfinished sign" + // aan het begin | of || of && -> vec->index == i, "syntax error near unexpected token" + // < > vec->index == i, "syntax error near unexpected token `newline'" + // << >> vec->index == i, syntax error near unexpected token `newline' + if (str[0] == '&' && str[1] != '&') + err("nice try, out of scope", "`&'", 2); + else if (checkchar(str[0], "<>")) + err("syntax error near unexpected token `newline'", str, 2); + else if (str[0] == '|' && str[1] != '||') + err("syntax error near unexpected token", "`|'", 2); + else if (str[0] == '|' && str[1] == '||') + err("syntax error near unexpected token", "`|'", 2); + else if (Str[0] == ) + return (false); +} +*/ + + + +static size_t split_size(char *str) +{ + size_t split; + size_t i; + + split = 0; + i = 0; + while (str[i]) + { + if (checkchar(str[i], "<>|&")) + { + split++; + if (str[i - 1] && !checkchar(str[i - 1], "<>|&")) + split++; + if (str[i] == str[i + 1] && str[i] != '&') + i++; + } + i++; + } + return (split); +} + +static bool split_left(char *str, char **array, size_t i, size_t *split) +{ + size_t left; + + left = i; + if (left > 0 && !checkchar(str[left - 1], "<>|&")) + { + left--; + while(left > 0 && !checkchar(str[left], "<>|&")) + left--; + if (checkchar(str[left], "<>|&")) + left++; + array[(*split)] = ft_substr(str, left, i - left); + if (!array[(*split)]) + return (false); + (*split)++; + } + return (true); +} + +static bool split_string(char *str, char **array, size_t *i, size_t *split) +{ + size_t len; + + if (!split_left(str, array, (*i), split)) + return (false); + len = 1; + if (str[(*i)] == str[(*i) + 1] && str[(*i)] != '&') + len++; + array[(*split)] = ft_substr(str, (*i), len); + if (!array[(*split)]) + return (false); + (*split)++; + (*i) += len; + return (true); +} + +/** + * + * +*/ +static char **split(t_token *token) +{ + size_t i; + size_t split; + char **array; + + split = split_size(token->value) + 1; + array = malloc(sizeof(char *) * split + 1); + if (!array) + return (NULL); + array[split] = NULL; + split = 0; + i = 0; + while (token->value[i]) + { + if (checkchar(token->value[i], "<>|&")) + { + if (!split_string(token->value, array, &i, &split)) + return (ft_free(array), NULL); + } + else + i++; + } + if (!split_left(token->value, array, i, &split)) + return (ft_free(array), NULL); + return (array); +} + +size_t array_lenght(char **array) +{ + size_t i; + + i = 0; + while (array[i]) + i++; + return (i); +} + +bool operator_split(t_vector *vec) +{ + size_t i; + size_t j; + t_token *token; + char **array; + t_token **token_a; + + i = 0; + while (i < vec->lenght) + { + token = (t_token *)vec->get(vec, i); + if (token->type==STRING && find_operator(token)) + { + array = split(token); + if (!array) + return (err("malloc", NULL, 1), false); + j = 0; + token_a = malloc(sizeof(t_token *) * array_lenght(array) + 1); + while (array[j]) + { + token_a[j] = create_token(array[j], UNKNOWN); + j++; + } + token_a[j] = NULL; + if (!vec->replace_multiple(vec, i, (void **)token_a)) + return (err("malloc", NULL, 1), false); + i += array_lenght(array); + free(array); + free(token_a); + } + else + i++; + } + return (true); +} diff --git a/src/main.c b/src/main.c index b3187ea..4da7bb9 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 17:45:26 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/21 18:18:07 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -85,11 +85,13 @@ int main(int ac, char **av, char **env) if (ac == 2) { if (!lexer(av[1], &g_data.tokens)) - return (ft_vec_free(&g_data.tokens), 1); + return (free_global(true), 1); parser(&g_data.tokens); + if (!operator_split(&g_data.tokens)) + return (free_global(true), 1); print_vector(&g_data.tokens, print_token); - if (DEBUG) - print_vector(&g_data.env, print_env); + // if (DEBUG) + // print_vector(&g_data.env, print_env); free_global(false); return (0); } From 170f558605c8cbf6e067e07c95c69bfff7c26dd6 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Fri, 21 Jul 2023 20:06:47 +0200 Subject: [PATCH 56/91] =?UTF-8?q?=F0=9F=94=80=20chore(libft):=20update=20l?= =?UTF-8?q?ibft=20submodule=20to=20commit=209cce6eca4ccf2b425ce0e600f499e1?= =?UTF-8?q?5b3f2b8ece=20=F0=9F=97=91=EF=B8=8F=20chore(src):=20delete=20.DS?= =?UTF-8?q?=5FStore=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libft | 2 +- src/.DS_Store | Bin 6148 -> 0 bytes 2 files changed, 1 insertion(+), 1 deletion(-) delete mode 100644 src/.DS_Store diff --git a/libft b/libft index b804781..9cce6ec 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit b8047815b1484156db39153b5e2c77e62427cf2a +Subproject commit 9cce6eca4ccf2b425ce0e600f499e15b3f2b8ece diff --git a/src/.DS_Store b/src/.DS_Store deleted file mode 100644 index dab7208db9fdf6d6af7ad048fdb67a149018acfd..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%Wl&^6upz?;ZRk|0;Fz`EU}GBNTE{IjY-RbEk>{a6zn)^t-7Ab4}ug$^1A#3 zzrdC+;a^z6xieGAsp%FXG*_BA^ElVzIoC7eAtEs;qbEcIB65%z+xL+D!MLAO!&;_= z{e*f(K{2J2zf9z;!L|)n0jt2aDZp#jp^!?f+=OcDH%w%RKHeCfBOUxwh7!>p&FF&W z$S-NCR{R3~1s%|gSANa&+bz!yMeyU%&NrFjVS|}~`57s@#r$$NGhF5iGy3;%k;Hj1 z8vPKhopz_&b9&CM^TEH6OTP$;GM@z5JFdN!G6`?WLHH(~FQ~kz7t>nMS$t^>v5Scly)8{%Uo2{A}oskH+hvyE-0^hVIkDqxHJ)Jb3i@t zxk}|V-X12fUn<)(cmq7U4kBUZ{>jhMM5gCxWsfdhQJ@rWXf1xCzaFJ7Ex^qvZ)(`$ z=heAAunKTX#I(@+cP)w>|F5<9cT}|bdr>=mjmhz@Gf}m+wF;PT(AnMEH?%Ki6|f5Y zD+PFeaFG~2gOx_Lbs$q$0AL%<%24KC2KI3cdIl?v=z$6C3e>K`Trq@pN4slyp213^ zb|+yjAHs|*%ne1T(eZp&(Mfn3ZEh8?3T!H{qpJfx|6kmF|KD`7HLHMC;J;Epw9ovr z38rMu*2d)ctaXt-BC#>A(x_aJnd4X$_$b~-QieX43qa3cr4cPK`y-%au$fihuPX2x DbU(2> From 70687a600f243940f858874c086fc3f059222490 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Mon, 24 Jul 2023 10:47:21 +0200 Subject: [PATCH 57/91] =?UTF-8?q?=F0=9F=94=A7=20chore(Makefile):=20add=20-?= =?UTF-8?q?fsanitize=3Daddress=20flag=20to=20DEBUG=5FFLAGS=20=F0=9F=94=A7?= =?UTF-8?q?=20chore(token.c):=20update=20timestamp=20in=20file=20header=20?= =?UTF-8?q?=F0=9F=94=A7=20chore(string.c):=20update=20timestamp=20in=20fil?= =?UTF-8?q?e=20header=20=F0=9F=94=A7=20chore(main.c):=20update=20timestamp?= =?UTF-8?q?=20in=20file=20header=20=F0=9F=94=A7=20chore(token.c):=20refact?= =?UTF-8?q?or=20operator=5Fsplit=20function=20=F0=9F=94=A7=20chore(token.c?= =?UTF-8?q?):=20refactor=20split=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- libft | 2 +- src/lexer/string.c | 2 +- src/lexer/token.c | 43 +++++++++++++++++++------------------------ src/main.c | 2 +- 5 files changed, 23 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index 2621b31..823ed72 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ LIBFT = libft/libft.a READLINE = readline/libreadline.a DEBUG ?= 0 -DEBUG_FLAGS = -g +DEBUG_FLAGS = -g -fsanitize=address G_FLAGS = -DREADLINE_LIBRARY CODAM_FLAGS = -Wall -Wextra -Werror LIBS = libft/libft.a readline/libreadline.a readline/libhistory.a diff --git a/libft b/libft index 9cce6ec..70218ab 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 9cce6eca4ccf2b425ce0e600f499e15b3f2b8ece +Subproject commit 70218ab69bb5ed65a061be8115b077164f5e34bd diff --git a/src/lexer/string.c b/src/lexer/string.c index 216a9f7..d46191b 100644 --- a/src/lexer/string.c +++ b/src/lexer/string.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 13:41:35 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 04:40:27 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/24 10:28:29 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/lexer/token.c b/src/lexer/token.c index 5dab888..87bc231 100644 --- a/src/lexer/token.c +++ b/src/lexer/token.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/21 13:06:18 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 19:55:58 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/24 10:43:36 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ static bool find_operator(t_token *token) int i; i = 0; - while(token->value[i] && !checkchar(token->value[i], "<>|&")) + while (token->value[i] && !checkchar(token->value[i], "<>|&")) i++; if (token->value[i] == '\0') return (false); @@ -29,7 +29,8 @@ static bool operator_err(char *str) { // & -> fail // aan het einde | of || of && -> vec->index == i, "unfinished sign" - // aan het begin | of || of && -> vec->index == i, "syntax error near unexpected token" + // aan het begin | of || of && -> vec->index == i, + "syntax error near unexpected token" // < > vec->index == i, "syntax error near unexpected token `newline'" // << >> vec->index == i, syntax error near unexpected token `newline' if (str[0] == '&' && str[1] != '&') @@ -45,8 +46,6 @@ static bool operator_err(char *str) } */ - - static size_t split_size(char *str) { size_t split; @@ -59,7 +58,7 @@ static size_t split_size(char *str) if (checkchar(str[i], "<>|&")) { split++; - if (str[i - 1] && !checkchar(str[i - 1], "<>|&")) + if (i > 0 && str[i - 1] && !checkchar(str[i - 1], "<>|&")) split++; if (str[i] == str[i + 1] && str[i] != '&') i++; @@ -77,7 +76,7 @@ static bool split_left(char *str, char **array, size_t i, size_t *split) if (left > 0 && !checkchar(str[left - 1], "<>|&")) { left--; - while(left > 0 && !checkchar(str[left], "<>|&")) + while (left > 0 && !checkchar(str[left], "<>|&")) left--; if (checkchar(str[left], "<>|&")) left++; @@ -107,17 +106,17 @@ static bool split_string(char *str, char **array, size_t *i, size_t *split) } /** - * - * + * + * */ static char **split(t_token *token) { - size_t i; - size_t split; - char **array; + size_t i; + size_t split; + char **array; split = split_size(token->value) + 1; - array = malloc(sizeof(char *) * split + 1); + array = malloc(sizeof(char *) * (split + 1)); if (!array) return (NULL); array[split] = NULL; @@ -154,33 +153,29 @@ bool operator_split(t_vector *vec) size_t j; t_token *token; char **array; - t_token **token_a; i = 0; while (i < vec->lenght) { token = (t_token *)vec->get(vec, i); - if (token->type==STRING && find_operator(token)) + if (token->type == STRING && find_operator(token)) { array = split(token); - if (!array) + if (!array || !vec->set(vec, i, array[0])) return (err("malloc", NULL, 1), false); j = 0; - token_a = malloc(sizeof(t_token *) * array_lenght(array) + 1); while (array[j]) { - token_a[j] = create_token(array[j], UNKNOWN); + token = create_token(array[j], UNKNOWN); + if (!token || !vec->insert(vec, i + j, token)) + return (err("malloc", NULL, 1), false); j++; } - token_a[j] = NULL; - if (!vec->replace_multiple(vec, i, (void **)token_a)) - return (err("malloc", NULL, 1), false); - i += array_lenght(array); - free(array); - free(token_a); } else i++; } + parser(vec); + free(array); return (true); } diff --git a/src/main.c b/src/main.c index 4da7bb9..62b1e78 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 18:18:07 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/24 10:21:28 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ From aaa048528472ce08b7ed9f60340cbc18b9154b50 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Mon, 24 Jul 2023 11:42:55 +0200 Subject: [PATCH 58/91] =?UTF-8?q?=F0=9F=94=80=20chore(libft):=20update=20s?= =?UTF-8?q?ubproject=20commit=20hash=20=F0=9F=90=9B=20fix(token.c):=20remo?= =?UTF-8?q?ve=20unused=20array=20element=20and=20print=20array=20elements?= =?UTF-8?q?=20=F0=9F=90=9B=20fix(error.c):=20update=20timestamp=20in=20err?= =?UTF-8?q?or=20message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libft | 2 +- src/lexer/token.c | 6 ++++-- src/utils/error.c | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/libft b/libft index 70218ab..c2237a9 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 70218ab69bb5ed65a061be8115b077164f5e34bd +Subproject commit c2237a91c43d302c8293ee20325c4729dee3ae83 diff --git a/src/lexer/token.c b/src/lexer/token.c index 87bc231..63850e0 100644 --- a/src/lexer/token.c +++ b/src/lexer/token.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/21 13:06:18 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/24 10:43:36 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/24 11:39:00 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -161,11 +161,13 @@ bool operator_split(t_vector *vec) if (token->type == STRING && find_operator(token)) { array = split(token); - if (!array || !vec->set(vec, i, array[0])) + printf("array: %s\n", array[0]); + if (!array || !vec->remove(vec, i)) return (err("malloc", NULL, 1), false); j = 0; while (array[j]) { + printf("array: %s\n", array[j]); token = create_token(array[j], UNKNOWN); if (!token || !vec->insert(vec, i + j, token)) return (err("malloc", NULL, 1), false); diff --git a/src/utils/error.c b/src/utils/error.c index eabd13c..482e0e6 100644 --- a/src/utils/error.c +++ b/src/utils/error.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 21:42:24 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 04:41:35 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/24 11:38:21 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ From 826c91c8fbb9ea5e983a5b93e15a5994a1e6098a Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Mon, 24 Jul 2023 11:43:52 +0200 Subject: [PATCH 59/91] =?UTF-8?q?=F0=9F=94=A5=20refactor(token.c):=20remov?= =?UTF-8?q?e=20debug=20print=20statements?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/lexer/token.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lexer/token.c b/src/lexer/token.c index 63850e0..abc36f0 100644 --- a/src/lexer/token.c +++ b/src/lexer/token.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/21 13:06:18 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/24 11:39:00 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/24 11:43:31 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -161,13 +161,11 @@ bool operator_split(t_vector *vec) if (token->type == STRING && find_operator(token)) { array = split(token); - printf("array: %s\n", array[0]); if (!array || !vec->remove(vec, i)) return (err("malloc", NULL, 1), false); j = 0; while (array[j]) { - printf("array: %s\n", array[j]); token = create_token(array[j], UNKNOWN); if (!token || !vec->insert(vec, i + j, token)) return (err("malloc", NULL, 1), false); From 9c277d70cc1ca4d823f8921c3ac5829c1a025585 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Mon, 24 Jul 2023 20:47:19 +0200 Subject: [PATCH 60/91] =?UTF-8?q?=F0=9F=90=9B=20fix(launch.json):=20update?= =?UTF-8?q?=20args=20value=20to=20include=20escaped=20quotes=20and=20speci?= =?UTF-8?q?al=20characters=20=F0=9F=94=A8=20refactor(main.c):=20change=20c?= =?UTF-8?q?heck=5Fleaks=20function=20to=20static=20=F0=9F=94=A8=20refactor?= =?UTF-8?q?(main.c):=20change=20token=5Fis=5Fpipe=20function=20to=20a=20co?= =?UTF-8?q?mment=20=F0=9F=94=A8=20refactor(main.c):=20remove=20unused=20co?= =?UTF-8?q?de=20=F0=9F=94=A8=20refactor(main.c):=20remove=20commented=20ou?= =?UTF-8?q?t=20code=20=F0=9F=94=A8=20refactor(main.c):=20remove=20todo=20c?= =?UTF-8?q?omment=20=F0=9F=94=A8=20refactor(main.c):=20remove=20unused=20f?= =?UTF-8?q?unction=20parameter=20=F0=9F=94=A8=20refactor(main.c):=20remove?= =?UTF-8?q?=20unused=20function=20parameter=20=F0=9F=94=A8=20refactor(main?= =?UTF-8?q?.c):=20remove=20unused=20function=20parameter=20=F0=9F=94=A8=20?= =?UTF-8?q?refactor(main.c):=20remove=20unused=20function=20parameter=20?= =?UTF-8?q?=F0=9F=94=A8=20refactor(main.c):=20remove=20unused=20function?= =?UTF-8?q?=20parameter=20=F0=9F=94=A8=20refactor(main.c):=20remove=20unus?= =?UTF-8?q?ed=20function=20parameter=20=F0=9F=94=A8=20refactor(main.c):=20?= =?UTF-8?q?remove=20unused=20function=20parameter=20=F0=9F=94=A8=20refacto?= =?UTF-8?q?r(main.c):=20remove=20unused=20function=20parameter=20?= =?UTF-8?q?=F0=9F=94=A8=20refactor(main.c):=20remove=20unused=20function?= =?UTF-8?q?=20parameter=20=F0=9F=94=A8=20refactor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/launch.json | 4 ++-- src/lexer/token.c | 5 +++-- src/main.c | 23 +++++++---------------- src/parser/quotes.c | 8 +++++++- src/parser/tokens.c | 12 +++++++++++- src/parser/tokens2.c | 8 +++++++- 6 files changed, 37 insertions(+), 23 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 1d67ad8..30f1375 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -11,9 +11,9 @@ "program": "${workspaceFolder}/./minishell", "preLaunchTask": "build", "args": [ - "hello|test|split < >> << || | && () )\"" ], "cwd": "${workspaceFolder}" } ] -} \ No newline at end of file +} diff --git a/src/lexer/token.c b/src/lexer/token.c index abc36f0..0e34524 100644 --- a/src/lexer/token.c +++ b/src/lexer/token.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/21 13:06:18 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/24 11:43:31 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/24 19:52:11 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -176,6 +176,7 @@ bool operator_split(t_vector *vec) i++; } parser(vec); - free(array); + if (!array) + free(array); return (true); } diff --git a/src/main.c b/src/main.c index 62b1e78..729ad92 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/24 10:21:28 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/24 20:46:53 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ t_global g_data; -void check_leaks(void) +static void check_leaks(void) { printf("\033[1;10m●\n"); printf("\033[1;12m│\n"); @@ -34,20 +34,10 @@ static void debug(void) printf("\033[0m"); } -// static bool token_is_pipe(void *data) -// { -// t_token *token; - -// token = (t_token *)data; -// return (token->type == PIPE); -// } - /** * @brief The main loop of the program * * @param vec The vector to store the tokens in - * - * @todo Add a way to exit the program but not the shell */ static void loop(t_vector *vec) { @@ -67,7 +57,7 @@ static void loop(t_vector *vec) else { if (!lexer(input, vec)) - return (free(input), ft_vec_free(vec)); + return (free(input), free_global(true)); parser(vec); print_vector(vec, print_token); } @@ -81,7 +71,8 @@ int main(int ac, char **av, char **env) { if (DEBUG) debug(); - init(env); + if (!init(env)) + return (1); if (ac == 2) { if (!lexer(av[1], &g_data.tokens)) @@ -90,8 +81,8 @@ int main(int ac, char **av, char **env) if (!operator_split(&g_data.tokens)) return (free_global(true), 1); print_vector(&g_data.tokens, print_token); - // if (DEBUG) - // print_vector(&g_data.env, print_env); + if (DEBUG) + print_vector(&g_data.env, print_env); free_global(false); return (0); } diff --git a/src/parser/quotes.c b/src/parser/quotes.c index 5852f01..15e2db8 100644 --- a/src/parser/quotes.c +++ b/src/parser/quotes.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 11:37:28 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 04:34:01 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/24 11:58:04 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,6 +21,8 @@ */ bool is_encased_dq(char *str) { + if (str[0] == '\0') + return (false); if (str[0] == '\"' && str[ft_strlen(str) - 1] == '\"') return (true); return (false); @@ -35,6 +37,8 @@ bool is_encased_dq(char *str) */ bool is_encased_sq(char *str) { + if (str[0] == '\0') + return (false); if (str[0] == '\'' && str[ft_strlen(str) - 1] == '\'') return (true); return (false); @@ -49,6 +53,8 @@ bool is_encased_sq(char *str) */ bool is_encased_parentheses(char *str) { + if (str[0] == '\0') + return (false); if (str[0] == '(' && str[ft_strlen(str) - 1] == ')') return (true); return (false); diff --git a/src/parser/tokens.c b/src/parser/tokens.c index a0fd622..ada5e27 100644 --- a/src/parser/tokens.c +++ b/src/parser/tokens.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 14:50:46 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 04:33:03 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/24 11:58:25 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,6 +21,8 @@ */ bool is_pipe(char *str) { + if (str[0] == '\0') + return (false); if (ft_strcmp(str, "|") == 0) return (true); return (false); @@ -35,6 +37,8 @@ bool is_pipe(char *str) */ bool is_r_redirect(char *str) { + if (str[0] == '\0') + return (false); if (ft_strcmp(str, ">") == 0) return (true); return (false); @@ -49,6 +53,8 @@ bool is_r_redirect(char *str) */ bool is_l_redirect(char *str) { + if (str[0] == '\0') + return (false); if (ft_strcmp(str, "<") == 0) return (true); return (false); @@ -63,6 +69,8 @@ bool is_l_redirect(char *str) */ bool is_r_hd(char *str) { + if (str[0] == '\0') + return (false); if (ft_strcmp(str, ">>") == 0) return (true); return (false); @@ -77,6 +85,8 @@ bool is_r_hd(char *str) */ bool is_l_hd(char *str) { + if (str[0] == '\0') + return (false); if (ft_strcmp(str, "<<") == 0) return (true); return (false); diff --git a/src/parser/tokens2.c b/src/parser/tokens2.c index 48ba742..bb3b3d8 100644 --- a/src/parser/tokens2.c +++ b/src/parser/tokens2.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 16:29:02 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 19:48:27 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/24 11:58:33 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,6 +21,8 @@ */ bool contains_env_var(char *str) { + if (str[0] == '\0') + return (false); if (str[0] == '$') return (true); return (false); @@ -35,6 +37,8 @@ bool contains_env_var(char *str) */ bool is_and(char *str) { + if (str[0] == '\0') + return (false); if (ft_strcmp(str, "&&") == 0) return (true); return (false); @@ -49,6 +53,8 @@ bool is_and(char *str) */ bool is_or(char *str) { + if (str[0] == '\0') + return (false); if (ft_strcmp(str, "||") == 0) return (true); return (false); From 31132d88b79a452b7c068e0fbc26b960e8b3ded5 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Sat, 29 Jul 2023 16:02:38 +0200 Subject: [PATCH 61/91] =?UTF-8?q?=F0=9F=90=9B=20fix(Makefile):=20remove=20?= =?UTF-8?q?address=20sanitizer=20flag=20from=20DEBUG=5FFLAGS=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(parser/index.c):=20update=20timestamp=20in?= =?UTF-8?q?=20file=20header=20comment=20=F0=9F=90=9B=20fix(parser/quotes.c?= =?UTF-8?q?):=20remove=20redundant=20check=20for=20empty=20string=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(parser/tokens.c):=20remove=20redundant=20che?= =?UTF-8?q?ck=20for=20empty=20string=20=F0=9F=90=9B=20fix(parser/tokens2.c?= =?UTF-8?q?):=20remove=20redundant=20check=20for=20empty=20string=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(utils/init.c):=20update=20timestamp=20in=20f?= =?UTF-8?q?ile=20header=20comment=20=E2=9C=A8=20feat(server.ts):=20add=20s?= =?UTF-8?q?upport=20for=20process.env.PORT=20environment=20variable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- libft | 2 +- src/main.c | 37 ++++++++++++++++++++++++++++++++++--- src/parser/index.c | 2 +- src/parser/quotes.c | 8 +------- src/parser/tokens.c | 12 +----------- src/parser/tokens2.c | 8 +------- src/utils/init.c | 4 ++-- 8 files changed, 42 insertions(+), 33 deletions(-) diff --git a/Makefile b/Makefile index 823ed72..2621b31 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ LIBFT = libft/libft.a READLINE = readline/libreadline.a DEBUG ?= 0 -DEBUG_FLAGS = -g -fsanitize=address +DEBUG_FLAGS = -g G_FLAGS = -DREADLINE_LIBRARY CODAM_FLAGS = -Wall -Wextra -Werror LIBS = libft/libft.a readline/libreadline.a readline/libhistory.a diff --git a/libft b/libft index c2237a9..7cd6635 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit c2237a91c43d302c8293ee20325c4729dee3ae83 +Subproject commit 7cd6635d2ed0acd43bc50ffdacae207e28988488 diff --git a/src/main.c b/src/main.c index 729ad92..7b59aa8 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/24 20:46:53 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/29 14:32:28 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -67,8 +67,20 @@ static void loop(t_vector *vec) } } +static bool find_strings(void *data) +{ + t_token *token; + + token = (t_token *)data; + if (token->type == STRING) + return (true); + return (false); +} + int main(int ac, char **av, char **env) { + t_found **found; + if (DEBUG) debug(); if (!init(env)) @@ -78,11 +90,30 @@ int main(int ac, char **av, char **env) if (!lexer(av[1], &g_data.tokens)) return (free_global(true), 1); parser(&g_data.tokens); + printf("Parsed!\n"); if (!operator_split(&g_data.tokens)) return (free_global(true), 1); + found = g_data.tokens.find(&g_data.tokens, find_strings); + if (!found) + { + printf("%zu counted\n", g_data.tokens.count(&g_data.tokens, + find_strings)); + printf("No matches found\n"); + } + else + { + printf("Printing matches:\n"); + while (*found) + { + print_token((*found)->item, (*found)->index); + free(*found); + found++; + } + free(found); + } print_vector(&g_data.tokens, print_token); - if (DEBUG) - print_vector(&g_data.env, print_env); + // if (DEBUG) + // print_vector(&g_data.env, print_env); free_global(false); return (0); } diff --git a/src/parser/index.c b/src/parser/index.c index 805481c..8e19f74 100644 --- a/src/parser/index.c +++ b/src/parser/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 11:12:20 by mdekker #+# #+# */ -/* Updated: 2023/07/21 19:51:10 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/28 18:01:38 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/parser/quotes.c b/src/parser/quotes.c index 15e2db8..67d4824 100644 --- a/src/parser/quotes.c +++ b/src/parser/quotes.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 11:37:28 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/24 11:58:04 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/24 20:48:19 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,8 +21,6 @@ */ bool is_encased_dq(char *str) { - if (str[0] == '\0') - return (false); if (str[0] == '\"' && str[ft_strlen(str) - 1] == '\"') return (true); return (false); @@ -37,8 +35,6 @@ bool is_encased_dq(char *str) */ bool is_encased_sq(char *str) { - if (str[0] == '\0') - return (false); if (str[0] == '\'' && str[ft_strlen(str) - 1] == '\'') return (true); return (false); @@ -53,8 +49,6 @@ bool is_encased_sq(char *str) */ bool is_encased_parentheses(char *str) { - if (str[0] == '\0') - return (false); if (str[0] == '(' && str[ft_strlen(str) - 1] == ')') return (true); return (false); diff --git a/src/parser/tokens.c b/src/parser/tokens.c index ada5e27..7c8617b 100644 --- a/src/parser/tokens.c +++ b/src/parser/tokens.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 14:50:46 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/24 11:58:25 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/24 20:48:05 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,8 +21,6 @@ */ bool is_pipe(char *str) { - if (str[0] == '\0') - return (false); if (ft_strcmp(str, "|") == 0) return (true); return (false); @@ -37,8 +35,6 @@ bool is_pipe(char *str) */ bool is_r_redirect(char *str) { - if (str[0] == '\0') - return (false); if (ft_strcmp(str, ">") == 0) return (true); return (false); @@ -53,8 +49,6 @@ bool is_r_redirect(char *str) */ bool is_l_redirect(char *str) { - if (str[0] == '\0') - return (false); if (ft_strcmp(str, "<") == 0) return (true); return (false); @@ -69,8 +63,6 @@ bool is_l_redirect(char *str) */ bool is_r_hd(char *str) { - if (str[0] == '\0') - return (false); if (ft_strcmp(str, ">>") == 0) return (true); return (false); @@ -85,8 +77,6 @@ bool is_r_hd(char *str) */ bool is_l_hd(char *str) { - if (str[0] == '\0') - return (false); if (ft_strcmp(str, "<<") == 0) return (true); return (false); diff --git a/src/parser/tokens2.c b/src/parser/tokens2.c index bb3b3d8..5f83046 100644 --- a/src/parser/tokens2.c +++ b/src/parser/tokens2.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 16:29:02 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/24 11:58:33 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/24 20:47:49 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,8 +21,6 @@ */ bool contains_env_var(char *str) { - if (str[0] == '\0') - return (false); if (str[0] == '$') return (true); return (false); @@ -37,8 +35,6 @@ bool contains_env_var(char *str) */ bool is_and(char *str) { - if (str[0] == '\0') - return (false); if (ft_strcmp(str, "&&") == 0) return (true); return (false); @@ -53,8 +49,6 @@ bool is_and(char *str) */ bool is_or(char *str) { - if (str[0] == '\0') - return (false); if (ft_strcmp(str, "||") == 0) return (true); return (false); diff --git a/src/utils/init.c b/src/utils/init.c index cae9617..a96ae73 100644 --- a/src/utils/init.c +++ b/src/utils/init.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 20:09:52 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 04:27:48 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/27 17:26:16 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -31,7 +31,7 @@ static void init_env(char **env) key = ft_substr(env[i], 0, ft_strchr(env[i], '=') - env[i]); value = ft_strdup(ft_strchr(env[i], '=') + 1); if (!key || !value) - exit(1); + exit(1); // !TODO: error message if (!g_data.env.push(&g_data.env, create_env(key, value))) exit(1); i++; From a8e47f1df23b691b320596f7ae526c32f1994301 Mon Sep 17 00:00:00 2001 From: Julius De Baaij Date: Mon, 31 Jul 2023 17:31:12 +0200 Subject: [PATCH 62/91] bugfix: lexer, fixed ignoring first character --- libft | 2 +- src/lexer/string.c | 5 ++--- src/main.c | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/libft b/libft index 7cd6635..c2237a9 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 7cd6635d2ed0acd43bc50ffdacae207e28988488 +Subproject commit c2237a91c43d302c8293ee20325c4729dee3ae83 diff --git a/src/lexer/string.c b/src/lexer/string.c index d46191b..82c57e6 100644 --- a/src/lexer/string.c +++ b/src/lexer/string.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 13:41:35 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/24 10:28:29 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/31 17:26:44 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -67,8 +67,7 @@ bool create_string(char *str, size_t *i, t_vector *vec) x.left = x.right; if (x.left > 0) x.left--; - if (x.left != 0) - build_string(str, vec, x); + build_string(str, vec, x); while (str[*i] == ' ' && str[*i]) (*i)++; return (true); diff --git a/src/main.c b/src/main.c index 7b59aa8..089b9dd 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/29 14:32:28 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/31 17:29:59 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ From b74405b9f7c8d52a13cc214368a847a2da5fa288 Mon Sep 17 00:00:00 2001 From: Julius De Baaij Date: Mon, 31 Jul 2023 20:08:41 +0200 Subject: [PATCH 63/91] setup, no code --- includes/structs.h | 10 +++++++- src/exec/exec.c | 51 +++++++++++++++++++++++++++++++++++++++ src/exec/group.c | 20 +++++++++++++++ src/exec/left_process.c | 0 src/exec/middle_process.c | 0 src/exec/right_process.c | 0 src/main.c | 2 +- 7 files changed, 81 insertions(+), 2 deletions(-) create mode 100644 src/exec/exec.c create mode 100644 src/exec/group.c create mode 100644 src/exec/left_process.c create mode 100644 src/exec/middle_process.c create mode 100644 src/exec/right_process.c diff --git a/includes/structs.h b/includes/structs.h index 4a1430e..54bbacb 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/07/21 04:24:55 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/31 20:08:18 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -58,6 +58,14 @@ typedef struct s_signal bool pipe; } t_signal; +typedef struct s_exec +{ + char **cmd; + pid_t pd; + int right_pipe[2]; + int left_pipe[2]; +} t_exec; + /** * @brief The global struct * diff --git a/src/exec/exec.c b/src/exec/exec.c new file mode 100644 index 0000000..1823599 --- /dev/null +++ b/src/exec/exec.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* exec.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/07/31 19:55:50 by mdekker/jde #+# #+# */ +/* Updated: 2023/07/31 20:02:35 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +/* + +1. begin indirect +2. end outdirect +3. count pipes + +pipe_count + +put all structs in vector + +each process pipes and forks +left_process 1 pipe +middle_process a pipe from left_process and a pipe_right + + +if (pipe_count > 0) +{ + left_process; + while (pipe_count > 1) + { + middle_process.c + pipe_count--; + } + right_process; +} +vec->length +waitpid's +close_pipes + +*/ + +#include + + + + + + + diff --git a/src/exec/group.c b/src/exec/group.c new file mode 100644 index 0000000..9ce7c1e --- /dev/null +++ b/src/exec/group.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* group.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */ +/* Updated: 2023/07/31 19:55:46 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +/* + +group the tokens into their executable groups + +*/ + +#include + diff --git a/src/exec/left_process.c b/src/exec/left_process.c new file mode 100644 index 0000000..e69de29 diff --git a/src/exec/middle_process.c b/src/exec/middle_process.c new file mode 100644 index 0000000..e69de29 diff --git a/src/exec/right_process.c b/src/exec/right_process.c new file mode 100644 index 0000000..e69de29 diff --git a/src/main.c b/src/main.c index 089b9dd..143d572 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/31 17:29:59 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/31 18:56:38 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ From 103c1ba61174541c33a273d3a3a1972332ee4f41 Mon Sep 17 00:00:00 2001 From: Julius De Baaij Date: Mon, 31 Jul 2023 20:14:57 +0200 Subject: [PATCH 64/91] only setup+explanation --- src/exec/group.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/exec/group.c b/src/exec/group.c index 9ce7c1e..1492542 100644 --- a/src/exec/group.c +++ b/src/exec/group.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/31 19:55:46 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/07/31 20:14:30 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,6 +14,29 @@ group the tokens into their executable groups +< infile cat | cat -e | wc > outfile + +Groups: +< infile == start_I_redirect +wc > outfile == end_O_redirect + +cat == left_process (pipe on the right side, no pipe on the left side) +cat -e == middle_process (pipe on the left side and pipe on the right side) +wc == right_process (pipe on the right side, no pipe on the left side) + + +< infile cat | cat -e > outfile | echo "hello" | wc + +redirects are only handled by main process if they are on the outside, if redirects are not fully on the outside they are handled by the child_processes + +groups: +< infile == start_I_redirect + +cat == left_process +cat -e > outfile == middle_process +echo "hello" = middle_process +wc = right_process + */ #include From 3f031e212143f81ca596ee122ab3cbf6383645e8 Mon Sep 17 00:00:00 2001 From: Julius De Baaij Date: Wed, 2 Aug 2023 21:32:07 +0200 Subject: [PATCH 65/91] inbetween commit --- .vscode/settings.json | 3 ++ Makefile | 2 +- includes/enum.h | 6 +-- includes/minishell.h | 9 +++- includes/structs.h | 6 +-- src/exec/group.c | 89 +++++++++++++++++++++++++++++++++++++-- src/main.c | 61 ++++++++++++++------------- src/parser/verify_token.c | 36 ++++++++++++++++ src/structs/exec.c | 47 +++++++++++++++++++++ src/utils/init.c | 4 +- 10 files changed, 219 insertions(+), 44 deletions(-) create mode 100644 src/parser/verify_token.c create mode 100644 src/structs/exec.c diff --git a/.vscode/settings.json b/.vscode/settings.json index 5185980..8632430 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -11,5 +11,8 @@ "comments": false, "strings": false }, + }, + "files.associations": { + "sstream": "c" } } diff --git a/Makefile b/Makefile index 2621b31..c2569df 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = minishell -SRC = main check_input utils/error structs/token parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string lexer/token utils/init structs/env utils/global +SRC = main check_input utils/error structs/token structs/exec parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string lexer/token utils/init structs/env utils/global exec/group SRCS = $(addsuffix .c, $(addprefix src/, $(SRC))) OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a diff --git a/includes/enum.h b/includes/enum.h index ca2f048..496d5ea 100644 --- a/includes/enum.h +++ b/includes/enum.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/13 17:41:00 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 18:23:49 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/02 20:08:56 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ * @param SINGLE_QUOTE (') * @param PIPE (|) * @param PARENTHESES (()) - * @param SEMICOLON (;) + * @param SEMICOLON (;) // semicolon weghalen? * @param OR (||) * @param AND (&&) * @param ENV ($) @@ -40,7 +40,7 @@ typedef enum e_types SINGLE_QUOTE, PARENTHESES, PIPE, - SEMICOLON, + SEMICOLON, // remove semi? OR, AND, ENV, diff --git a/includes/minishell.h b/includes/minishell.h index a0fc863..eda987b 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/07/21 19:58:27 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/02 20:55:18 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -41,6 +41,8 @@ t_token *create_token(char *value, t_types type); void clear_token(void *data); t_env *create_env(char *key, char *value); void clear_env(void *data); +t_exec *create_exec(char **cmd); +void clear_exec(void *data); /* utils */ void err(char *err, char *cmd, int exit_code); @@ -59,6 +61,10 @@ bool is_l_hd(char *str); bool contains_env_var(char *str); bool is_or(char *str); bool is_and(char *str); + +/* executor */ +bool group_tokens(t_vector *token, t_vector *exec); + /* debug */ void print_vector(t_vector *vec, void (*printer)(void *, size_t)); void print_token(void *data, size_t i); @@ -67,4 +73,5 @@ void print_env(void *data, size_t i); /* global */ void free_global(bool exit); + #endif diff --git a/includes/structs.h b/includes/structs.h index 54bbacb..eee4d30 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/07/31 20:08:18 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/02 19:32:58 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,9 +19,6 @@ * * @param type The type of token * @param value The value of the token - * @param prev The previous token - * @param next The next token - * * @note The value is malloced */ typedef struct s_token @@ -76,6 +73,7 @@ typedef struct s_global { t_vector tokens; t_vector env; + t_vector exec; t_signal signal; char *exit_status; diff --git a/src/exec/group.c b/src/exec/group.c index 1492542..bd4bd4b 100644 --- a/src/exec/group.c +++ b/src/exec/group.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/31 20:14:30 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/02 21:31:51 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,8 +17,8 @@ group the tokens into their executable groups < infile cat | cat -e | wc > outfile Groups: -< infile == start_I_redirect -wc > outfile == end_O_redirect +"< infile" == start_I_redirect +"wc > outfile" == end_O_redirect cat == left_process (pipe on the right side, no pipe on the left side) cat -e == middle_process (pipe on the left side and pipe on the right side) @@ -37,7 +37,88 @@ cat -e > outfile == middle_process echo "hello" = middle_process wc = right_process -*/ +cmd [] + + +*/ #include +typedef struct s_local +{ + size_t left; + size_t right; +} t_local; + + + +static t_local group_range(t_vector *token_vec, int i) +{ + t_token *token; + t_local range; + + range.left = i; + token = (t_token *)ft_vec_get(token_vec, i); + if (token->type == I_REDIRECT)// && i == 0; + range.right = 2; + return (range); +} + +bool create_group(t_vector *token_vec, t_vector *exec_vec, int *i, t_local range) +{ + t_token *token; + t_exec *exec; + + + + +} + +/** + * +*/ +bool group_tokens(t_vector *token_vec, t_vector *exec_vec) +{ + size_t i; + t_token *token; + t_exec *exec; + t_local range; + char **cmd; + + i = 0; + while (i < token_vec->lenght) + { + token = (t_token *)ft_vec_get(token_vec, i); + if (token->type != STRING) + { + printf("found non string--%zu\n", i); + range = group_range(token_vec, i); + //create_group(t_vector *token_vec, t_vector *exec_vec, int *i) + cmd = malloc(sizeof(char *) * (range.right + 1)); + if (!cmd) + return (false); + while (i < i + range.left + range.right) + { + token = (t_token *)ft_vec_get(token_vec, i); + cmd[i - range.left] = ft_strdup(token->value); + if (!exec->cmd[i - range.left]) + return (false); + exec = create_exec(cmd); + exec_vec->push(exec_vec, (t_exec *) exec); + i++; + } + } + else + i++; + } + + + + + if (exec) + return (true); + + //pos = &cat -> &vec->data + return (true); +} + diff --git a/src/main.c b/src/main.c index 143d572..b94fc0d 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/31 18:56:38 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/02 19:31:07 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -67,19 +67,19 @@ static void loop(t_vector *vec) } } -static bool find_strings(void *data) -{ - t_token *token; +// static bool find_strings(void *data) +// { +// t_token *token; - token = (t_token *)data; - if (token->type == STRING) - return (true); - return (false); -} +// token = (t_token *)data; +// if (token->type == STRING) +// return (true); +// return (false); +// } int main(int ac, char **av, char **env) { - t_found **found; + // t_found **found; if (DEBUG) debug(); @@ -93,27 +93,28 @@ int main(int ac, char **av, char **env) printf("Parsed!\n"); if (!operator_split(&g_data.tokens)) return (free_global(true), 1); - found = g_data.tokens.find(&g_data.tokens, find_strings); - if (!found) - { - printf("%zu counted\n", g_data.tokens.count(&g_data.tokens, - find_strings)); - printf("No matches found\n"); - } - else - { - printf("Printing matches:\n"); - while (*found) - { - print_token((*found)->item, (*found)->index); - free(*found); - found++; - } - free(found); - } - print_vector(&g_data.tokens, print_token); + group_tokens(&g_data.tokens, &g_data.exec); + // found = g_data.tokens.find(&g_data.tokens, find_strings); + // if (!found) + // { + // printf("%zu counted\n", g_data.tokens.count(&g_data.tokens, + // find_strings)); + // printf("No matches found\n"); + // } + // else + // { + // printf("Printing matches:\n"); + // while (*found) + // { + // print_token((*found)->item, (*found)->index); + // free(*found); + // found++; + // } + // free(found); + // } // if (DEBUG) - // print_vector(&g_data.env, print_env); + print_vector(&g_data.tokens, print_token); + //print_vector(&g_data.env, print_env); free_global(false); return (0); } diff --git a/src/parser/verify_token.c b/src/parser/verify_token.c new file mode 100644 index 0000000..ae17a09 --- /dev/null +++ b/src/parser/verify_token.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* verify_token.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/08/02 16:57:32 by mdekker/jde #+# #+# */ +/* Updated: 2023/08/02 20:08:01 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +/* +check for double operators +check for starting with anything other token than redirects / heredoc + +geen I_INDIRECT aan het einde + +Na een operator mag je alleen STRING/DOUBLE_QUOTE/SINGLE_QUOTE + +na een pipe mag je wel < > << >> + +bash-3.2$ <|hello +bash: syntax error near unexpected token `|' +bash-3.2$ cat infile |< bruh +bash: bruh: No such file or directory + +*/ + + +bool verify_token(t_vector *vec) +{ + +} diff --git a/src/structs/exec.c b/src/structs/exec.c new file mode 100644 index 0000000..e0fab20 --- /dev/null +++ b/src/structs/exec.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* exec.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/08/02 20:42:59 by mdekker/jde #+# #+# */ +/* Updated: 2023/08/02 20:55:02 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +/** + * @brief Create a t_exec object + * @param cmd is assigned to exec->cmd + * @return t_exec* The created exec struct + * @note left/right pipe values are set to -1 + * @note pid_t is set to -2 by default, as fork() can return -1 + */ +t_exec *create_exec(char **cmd) +{ + t_exec *exec; + + exec = malloc(sizeof(exec)); + if (!exec) + return (NULL); + exec->cmd = cmd; + exec->pd = -2; + exec->left_pipe[0] = -1; + exec->left_pipe[1] = -1; + exec->right_pipe[0] = -1; + exec->right_pipe[1] = -1; + return (exec); +} + +/** + * @brief clears data of t_exec +*/ +void clear_exec(void *data) +{ + t_exec *exec; + + exec = (t_exec *)data; + ft_free(exec->cmd); +} diff --git a/src/utils/init.c b/src/utils/init.c index a96ae73..21d0b37 100644 --- a/src/utils/init.c +++ b/src/utils/init.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 20:09:52 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/27 17:26:16 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/02 20:46:02 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -67,6 +67,8 @@ bool init(char **env) return (false); if (!ft_vec_init(&g_data.env, 50, sizeof(t_env), clear_env)) return (false); + if (!ft_vec_init(&g_data.exec, 3, sizeof(t_exec), clear_exec)) + return (false); g_data.exit_status = ft_strdup("0"); if (!g_data.exit_status) return (false); From 3f7500a2b6e85a1d24357d1559c532ba01f68ae8 Mon Sep 17 00:00:00 2001 From: JuliusdB Date: Fri, 4 Aug 2023 05:29:46 +0200 Subject: [PATCH 66/91] BRRRR heel veel text geschreven ter voorbereiding exec - removed ; from enum --- Makefile | 2 +- includes/enum.h | 8 +-- includes/minishell.h | 6 +- includes/structs.h | 10 ++- libft | 2 +- src/exec/exec.c | 60 ++++++++++++++++-- src/exec/find_cmd.c | 0 src/exec/group.c | 125 +++++++++++++++++++++++--------------- src/exec/redirect.c | 0 src/parser/index.c | 6 +- src/parser/tokens.c | 6 +- src/parser/verify_token.c | 28 +++++++-- src/structs/exec.c | 5 +- 13 files changed, 181 insertions(+), 77 deletions(-) create mode 100644 src/exec/find_cmd.c create mode 100644 src/exec/redirect.c diff --git a/Makefile b/Makefile index c2569df..4fc9602 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = minishell -SRC = main check_input utils/error structs/token structs/exec parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string lexer/token utils/init structs/env utils/global exec/group +SRC = main check_input utils/error structs/token structs/exec parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string lexer/token utils/init structs/env utils/global exec/group parser/verify_token SRCS = $(addsuffix .c, $(addprefix src/, $(SRC))) OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a diff --git a/includes/enum.h b/includes/enum.h index 496d5ea..7a237bf 100644 --- a/includes/enum.h +++ b/includes/enum.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/13 17:41:00 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/02 20:08:56 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/04 04:19:03 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -20,7 +20,6 @@ * @param SINGLE_QUOTE (') * @param PIPE (|) * @param PARENTHESES (()) - * @param SEMICOLON (;) // semicolon weghalen? * @param OR (||) * @param AND (&&) * @param ENV ($) @@ -29,7 +28,7 @@ * @param STRING A string * @param O_REDIRECT (>) * @param I_REDIRECT (<) - * @param O_HEREDOC (>>) + * @param A_REDIRECT (>>) * @param I_HEREDOC (<<) */ typedef enum e_types @@ -40,7 +39,6 @@ typedef enum e_types SINGLE_QUOTE, PARENTHESES, PIPE, - SEMICOLON, // remove semi? OR, AND, ENV, @@ -48,7 +46,7 @@ typedef enum e_types ENV_QUESTION, O_REDIRECT, I_REDIRECT, - O_HEREDOC, + A_REDIRECT, I_HEREDOC } t_types; diff --git a/includes/minishell.h b/includes/minishell.h index eda987b..9a2b2e7 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/08/02 20:55:18 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/04 04:14:01 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -56,8 +56,8 @@ bool is_encased_parentheses(char *str); bool is_pipe(char *str); bool is_r_redirect(char *str); bool is_l_redirect(char *str); -bool is_r_hd(char *str); -bool is_l_hd(char *str); +bool is_a_redirect(char *str); +bool is_heredoc(char *str); bool contains_env_var(char *str); bool is_or(char *str); bool is_and(char *str); diff --git a/includes/structs.h b/includes/structs.h index eee4d30..8cac7d0 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/08/02 19:32:58 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/04 05:28:01 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -55,13 +55,19 @@ typedef struct s_signal bool pipe; } t_signal; +/** + * @param locate 1 == left_process + * @param locate 2 == middle_process + * @param locate 3 == right_process + */ typedef struct s_exec { char **cmd; pid_t pd; + int locate; int right_pipe[2]; int left_pipe[2]; -} t_exec; +} t_exec; // /** * @brief The global struct diff --git a/libft b/libft index c2237a9..7cd6635 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit c2237a91c43d302c8293ee20325c4729dee3ae83 +Subproject commit 7cd6635d2ed0acd43bc50ffdacae207e28988488 diff --git a/src/exec/exec.c b/src/exec/exec.c index 1823599..a7b307a 100644 --- a/src/exec/exec.c +++ b/src/exec/exec.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:50 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/31 20:02:35 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/04 05:26:51 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -36,16 +36,68 @@ if (pipe_count > 0) right_process; } vec->length -waitpid's +waitpid's close_pipes -*/ -#include +for each struct in commands t_exec{ + // Create a pipe + int pipefd[2]; + pipe(pipefd); + + // Fork a new child process + pid_t pid = fork(); + if (pid == 0) { // Child process + if (is_middle_process) { + // Close the unused write end of the input pipe + close(input_pipe[1]); + // Redirect standard input to the read end of the input pipe + dup2(input_pipe[0], STDIN_FILENO); + close(input_pipe[0]); + // Close the unused read end of the output pipe + close(output_pipe[0]); + + // Redirect standard output to the write end of the output pipe + dup2(output_pipe[1], STDOUT_FILENO); + close(output_pipe[1]); + } + // Execute the command + execvp(command, command_args); + if (pid == 0) { // Child process + iif left_pipe[0] == -1 && right_pipe[0] != -1 + left_process + iif left_pipe[0] != -1 && right_pipe[0] == -1 + right_process + if left_pipe[0] != -1 && right_pipe[0] != -1 + middle_process + close(pipefd[0]); + + // Redirect standard output to the write end of the pipe + dup2(pipefd[1], STDOUT_FILENO); + close(pipefd[1]); + + // Execute the command + execvp(command, command_args); + } else { // Parent process + // Close the write end of the pipe + close(pipefd[1]); + + // Redirect standard input to the read end of the pipe + dup2(pipefd[0], STDIN_FILENO); + close(pipefd[0]); + } +} + + + + +*/ + +#include diff --git a/src/exec/find_cmd.c b/src/exec/find_cmd.c new file mode 100644 index 0000000..e69de29 diff --git a/src/exec/group.c b/src/exec/group.c index bd4bd4b..1ea8f11 100644 --- a/src/exec/group.c +++ b/src/exec/group.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/02 21:31:51 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/04 05:00:22 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,8 +17,8 @@ group the tokens into their executable groups < infile cat | cat -e | wc > outfile Groups: -"< infile" == start_I_redirect -"wc > outfile" == end_O_redirect +"< infile" == start_I_redirect --> function +"wc > outfile" == end_O_redirect --> function cat == left_process (pipe on the right side, no pipe on the left side) cat -e == middle_process (pipe on the left side and pipe on the right side) @@ -27,30 +27,67 @@ wc == right_process (pipe on the right side, no pipe on the left side) < infile cat | cat -e > outfile | echo "hello" | wc -redirects are only handled by main process if they are on the outside, if redirects are not fully on the outside they are handled by the child_processes +redirects are only handled by main process if they are on the outside, + if redirects are not fully on the outside they are handled by the child_processes groups: -< infile == start_I_redirect +< infile == start_I_redirect --> function -cat == left_process -cat -e > outfile == middle_process -echo "hello" = middle_process -wc = right_process +cat == left_process -->function +cat -e > outfile == middle_process --> function +echo "hello" = middle_process --> function +wc = right_process --> function +redirects not on the outside should be handled by childprocesses -cmd [] +I_Heredocs should always be handled in the main process ( before forking ) +all redirect should be handled before the child process is forked if on the outside + otherwise it should be handled by the child process +quote expansion handled by mees + +pipes are handled by the child process(left, right and middle process) + +parantheses should be handled beforehand? + +AND OR are their own childprocesses? idk still need to look into that + + * @param DOUBLE_QUOTE (") + * @param SINGLE_QUOTE (') + * @param PIPE (|) + * @param PARENTHESES (()) + * @param OR (||) + * @param AND (&&) + * @param ENV ($) + * @param DQ_ENV An environment variable in double quotes ("$") + * @param ENV_QUESTION An environment variable with a question mark ("$?") + * @param STRING A string + + + +------------------------ +edge case: + meaning no fork should be started for left_pipe +the right process will be cat + -e --> meaning a fork should be started for right_pipe + + +to do this keep track of which tokens have already been read so that when a pipe is found +the left process can be checked if it is NULL or not */ + +* / #include -typedef struct s_local + typedef struct s_local { - size_t left; - size_t right; -} t_local; - - + size_t left; + size_t right; +} t_local; static t_local group_range(t_vector *token_vec, int i) { @@ -59,66 +96,58 @@ static t_local group_range(t_vector *token_vec, int i) range.left = i; token = (t_token *)ft_vec_get(token_vec, i); - if (token->type == I_REDIRECT)// && i == 0; + if (token->type == I_REDIRECT) // && i == 0; range.right = 2; return (range); } -bool create_group(t_vector *token_vec, t_vector *exec_vec, int *i, t_local range) +bool create_group(t_vector *token_vec, t_vector *exec_vec, int *i, + t_local range) { t_token *token; t_exec *exec; - - - - } /** - * -*/ + * + */ bool group_tokens(t_vector *token_vec, t_vector *exec_vec) { size_t i; t_token *token; t_exec *exec; - t_local range; + t_local range; char **cmd; i = 0; while (i < token_vec->lenght) { token = (t_token *)ft_vec_get(token_vec, i); - if (token->type != STRING) - { - printf("found non string--%zu\n", i); - range = group_range(token_vec, i); - //create_group(t_vector *token_vec, t_vector *exec_vec, int *i) - cmd = malloc(sizeof(char *) * (range.right + 1)); - if (!cmd) - return (false); - while (i < i + range.left + range.right) + if (token->type != STRING) // or double quote / single quote + / parentheses { - token = (t_token *)ft_vec_get(token_vec, i); - cmd[i - range.left] = ft_strdup(token->value); - if (!exec->cmd[i - range.left]) + printf("found non string--%zu\n", i); + range = group_range(token_vec, i); + // create_group(t_vector *token_vec, t_vector *exec_vec, int *i) + cmd = malloc(sizeof(char *) * (range.right + 1)); + if (!cmd) return (false); - exec = create_exec(cmd); - exec_vec->push(exec_vec, (t_exec *) exec); - i++; + while (i < i + range.left + range.right) + { + token = (t_token *)ft_vec_get(token_vec, i); + cmd[i - range.left] = ft_strdup(token->value); + if (!exec->cmd[i - range.left]) + return (false); + exec = create_exec(cmd); + exec_vec->push(exec_vec, (t_exec *)exec); + i++; + } } - } else i++; } - - - - if (exec) return (true); - - //pos = &cat -> &vec->data + // pos = &cat -> &vec->data return (true); } - diff --git a/src/exec/redirect.c b/src/exec/redirect.c new file mode 100644 index 0000000..e69de29 diff --git a/src/parser/index.c b/src/parser/index.c index 8e19f74..c82b586 100644 --- a/src/parser/index.c +++ b/src/parser/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 11:12:20 by mdekker #+# #+# */ -/* Updated: 2023/07/28 18:01:38 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/04 04:14:29 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,8 +34,8 @@ t_func_map *return_map(void) func_map[5] = (t_func_map){is_pipe, PIPE}; func_map[6] = (t_func_map){is_r_redirect, O_REDIRECT}; func_map[7] = (t_func_map){is_l_redirect, I_REDIRECT}; - func_map[8] = (t_func_map){is_r_hd, O_HEREDOC}; - func_map[9] = (t_func_map){is_l_hd, I_HEREDOC}; + func_map[8] = (t_func_map){is_a_redirect, A_REDIRECT}; + func_map[9] = (t_func_map){is_heredoc, I_HEREDOC}; func_map[10] = (t_func_map){NULL, STRING}; return (func_map); } diff --git a/src/parser/tokens.c b/src/parser/tokens.c index 7c8617b..2a33a30 100644 --- a/src/parser/tokens.c +++ b/src/parser/tokens.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 14:50:46 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/24 20:48:05 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/04 04:14:11 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -61,7 +61,7 @@ bool is_l_redirect(char *str) * @return true The string contains a HEREDOC * @return false The string does not contain a HEREDOC */ -bool is_r_hd(char *str) +bool is_a_redirect(char *str) { if (ft_strcmp(str, ">>") == 0) return (true); @@ -75,7 +75,7 @@ bool is_r_hd(char *str) * @return true The string contains a HEREDOC * @return false The string does not contain a HEREDOC */ -bool is_l_hd(char *str) +bool is_heredoc(char *str) { if (ft_strcmp(str, "<<") == 0) return (true); diff --git a/src/parser/verify_token.c b/src/parser/verify_token.c index ae17a09..fbb49dd 100644 --- a/src/parser/verify_token.c +++ b/src/parser/verify_token.c @@ -6,13 +6,13 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/02 16:57:32 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/02 20:08:01 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/03 12:46:53 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -/* +/* check for double operators check for starting with anything other token than redirects / heredoc @@ -28,9 +28,27 @@ bash-3.2$ cat infile |< bruh bash: bruh: No such file or directory */ - - bool verify_token(t_vector *vec) { - + int i; + t_token *token; + + if (vec->len == 0) + return (false); + token = vec->data[0]; + if (token->type != I_REDIRECT && token->type != I_HEREDOC) + return (false); + token = vec->data[vec->len - 1]; + if (token->type != I_REDIRECT && token->type != I_HEREDOC) + return (false); + i = 0; + while (i < vec->len - 1) + { + token = vec->data[i]; + if (token->type == I_PIPE && ((t_token *)vec->data[i + + 1])->type == I_PIPE) + return (false); + i++; + } + return (true); } diff --git a/src/structs/exec.c b/src/structs/exec.c index e0fab20..b212317 100644 --- a/src/structs/exec.c +++ b/src/structs/exec.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/02 20:42:59 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/02 20:55:02 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/04 05:28:20 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -28,6 +28,7 @@ t_exec *create_exec(char **cmd) return (NULL); exec->cmd = cmd; exec->pd = -2; + exec->locate = 0; exec->left_pipe[0] = -1; exec->left_pipe[1] = -1; exec->right_pipe[0] = -1; @@ -37,7 +38,7 @@ t_exec *create_exec(char **cmd) /** * @brief clears data of t_exec -*/ + */ void clear_exec(void *data) { t_exec *exec; From e503fc212446d8f8b305aa113bae426a73ee00ed Mon Sep 17 00:00:00 2001 From: JuliusdB Date: Sat, 12 Aug 2023 20:26:42 +0200 Subject: [PATCH 67/91] =?UTF-8?q?=F0=9F=94=A7=20fix(settings.json):=20add?= =?UTF-8?q?=20file=20associations=20for=20"=5F=5Flocale"=20and=20"=5F=5Fst?= =?UTF-8?q?ring"=20to=20be=20recognized=20as=20C=20files=20=F0=9F=94=A7=20?= =?UTF-8?q?fix(enum.h):=20rename=20"I=5FHEREDOC"=20enum=20value=20to=20"HE?= =?UTF-8?q?REDOC"=20for=20consistency=20and=20clarity=20=F0=9F=94=A7=20fix?= =?UTF-8?q?(structs.h):=20change=20"t=5Fvector=20exec"=20to=20"t=5Fpipe=20?= =?UTF-8?q?exec"=20to=20accurately=20represent=20the=20type=20of=20the=20v?= =?UTF-8?q?ariable=20=F0=9F=94=A7=20fix(print=5Fvector.c):=20change=20"I?= =?UTF-8?q?=5FHEREDOC"=20to=20"HEREDOC"=20in=20the=20extra=5Ftypes=20funct?= =?UTF-8?q?ion=20for=20consistency=20=F0=9F=94=A7=20fix(exec.c):=20update?= =?UTF-8?q?=20comments=20to=20reflect=20the=20plan=20of=20executing=20the?= =?UTF-8?q?=20entire=20process=20in=20a=20child=20process=20=F0=9F=94=A7?= =?UTF-8?q?=20fix(group.c):=20update=20comments=20to=20reflect=20the=20pla?= =?UTF-8?q?n=20of=20handling=20HEREDOCs=20in=20the=20main=20process=20and?= =?UTF-8?q?=20redirects=20in=20child=20processes=20=F0=9F=94=A7=20fix(grou?= =?UTF-8?q?p.c):=20remove=20unused=20group=5Frange=20function=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(group.c):=20update=20comments=20to=20reflect?= =?UTF-8?q?=20the=20step-by-step=20process=20of=20grouping=20tokens=20and?= =?UTF-8?q?=20creating=20t=5Fpipe=20=F0=9F=94=A7=20fix(group.c):=20update?= =?UTF-8?q?=20comments=20to=20reflect=20the=20plan=20of=20handling=20redir?= =?UTF-8?q?ects=20and=20pipes=20in=20the=20group=5Ftokens=20function=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(index.c):=20rename=20"I=5FHEREDOC"=20to=20"H?= =?UTF-8?q?EREDOC"=20in=20the=20func=5Fmap=20array=20for=20consistency=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(verify=5Ftoken.c):=20rename=20"I=5FHEREDOC"?= =?UTF-8?q?=20to=20"HEREDOC"=20in=20the=20verify=5Ftoken=20function=20for?= =?UTF-8?q?=20consistency?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 6 ++- includes/enum.h | 6 +-- includes/structs.h | 25 ++++++++++-- src/debug/print_vector.c | 6 +-- src/exec/exec.c | 5 ++- src/exec/group.c | 84 +++++++++++++++++++++------------------ src/parser/index.c | 4 +- src/parser/verify_token.c | 6 +-- 8 files changed, 85 insertions(+), 57 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 8632430..2d929a2 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -13,6 +13,8 @@ }, }, "files.associations": { - "sstream": "c" + "sstream": "c", + "__locale": "c", + "__string": "c" } -} +} \ No newline at end of file diff --git a/includes/enum.h b/includes/enum.h index 7a237bf..962a0fa 100644 --- a/includes/enum.h +++ b/includes/enum.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/13 17:41:00 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/04 04:19:03 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/12 20:25:03 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ * @param O_REDIRECT (>) * @param I_REDIRECT (<) * @param A_REDIRECT (>>) - * @param I_HEREDOC (<<) + * @param HEREDOC (<<) */ typedef enum e_types { @@ -47,7 +47,7 @@ typedef enum e_types O_REDIRECT, I_REDIRECT, A_REDIRECT, - I_HEREDOC + HEREDOC } t_types; #endif diff --git a/includes/structs.h b/includes/structs.h index 8cac7d0..695f466 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/08/04 05:28:01 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/11 12:24:08 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -56,18 +56,35 @@ typedef struct s_signal } t_signal; /** + * @brief The struct for the execution + * + * @param pipes The vector of pipes + * @param here_doc The here_doc stop-word token + * @param input_redirect The input_redirect file to read from + * @param output_redirect The output_redirect file to write to + */ +typedef struct s_exec +{ + t_vector pipes; + t_token here_doc; + t_token input_redirect; + t_token output_redirect; +} t_exec; + +/** + * @param locate 0 == no pipe * @param locate 1 == left_process * @param locate 2 == middle_process * @param locate 3 == right_process */ -typedef struct s_exec +typedef struct s_pipe { char **cmd; pid_t pd; int locate; int right_pipe[2]; int left_pipe[2]; -} t_exec; // +} t_pipe; // /** * @brief The global struct @@ -79,7 +96,7 @@ typedef struct s_global { t_vector tokens; t_vector env; - t_vector exec; + t_pipe exec; t_signal signal; char *exit_status; diff --git a/src/debug/print_vector.c b/src/debug/print_vector.c index 145f735..185b7f1 100644 --- a/src/debug/print_vector.c +++ b/src/debug/print_vector.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 13:51:45 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 18:25:14 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/12 20:25:03 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -46,8 +46,8 @@ static char *extra_types(t_types type) return ("I_REDIRECT"); if (type == O_HEREDOC) return ("O_HEREDOC"); - if (type == I_HEREDOC) - return ("I_HEREDOC"); + if (type == HEREDOC) + return ("HEREDOC"); return (""); } diff --git a/src/exec/exec.c b/src/exec/exec.c index a7b307a..9593b0e 100644 --- a/src/exec/exec.c +++ b/src/exec/exec.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:50 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/04 05:26:51 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/11 12:12:25 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -96,7 +96,8 @@ for each struct in commands t_exec{ } - +make the entire executor happen in a childprocess, that way for example setting +the redirects in main process wont affect the nexg call */ diff --git a/src/exec/group.c b/src/exec/group.c index 1ea8f11..a01f968 100644 --- a/src/exec/group.c +++ b/src/exec/group.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/04 05:00:22 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/12 20:25:43 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -40,7 +40,7 @@ wc = right_process --> function redirects not on the outside should be handled by childprocesses -I_Heredocs should always be handled in the main process ( before forking ) +HEREDOCs should always be handled in the main process ( before forking ) all redirect should be handled before the child process is forked if on the outside otherwise it should be handled by the child process @@ -89,17 +89,22 @@ the left process can be checked if it is NULL or not size_t right; } t_local; -static t_local group_range(t_vector *token_vec, int i) -{ - t_token *token; - t_local range; - - range.left = i; - token = (t_token *)ft_vec_get(token_vec, i); - if (token->type == I_REDIRECT) // && i == 0; - range.right = 2; - return (range); -} +// static t_local group_range(t_vector *token_vec, int i) +// { +// t_token *token; +// t_local range; + +// range.left = i; +// token = (t_token *)ft_vec_get(token_vec, i); +// while (token->type != PIPE && token->type != I_REDIRECT +// && token->type != O_REDIRECT && token->type != A_REDIRECT +// && i < token_vec->lenght) +// { +// i++; +// token = (t_token *)ft_vec_get(token_vec, i); +// } +// return (range); +// } bool create_group(t_vector *token_vec, t_vector *exec_vec, int *i, t_local range) @@ -109,6 +114,20 @@ bool create_group(t_vector *token_vec, t_vector *exec_vec, int *i, } /** + *step by step: + * 1. check if there is a redirect at the start or end of the token_vec + + * 2. If there is a redirect at the start or end of the token_vec store it in t_exec and remove the associated tokens from the token_vec + * 3. Check if there is a heredoc in the token_vec + + * 4. If there is a heredoc in the token_vec store it in t_exec and remove the associated tokens from the token_vec + * 5. read through the token_vec and check if there is a pipe, + if there is store the tokens before the pipe in t_pipe + * 6. if there is no pipes store all of the remaining tokens in t_pipe + * 7. if there is a pipe check if its a left_pipe, + middle_pipe or right_pipe and store it in t_pipe correctly + * 8. once all is complete call the exector + * * */ bool group_tokens(t_vector *token_vec, t_vector *exec_vec) @@ -123,31 +142,20 @@ bool group_tokens(t_vector *token_vec, t_vector *exec_vec) while (i < token_vec->lenght) { token = (t_token *)ft_vec_get(token_vec, i); - if (token->type != STRING) // or double quote / single quote - / parentheses - { - printf("found non string--%zu\n", i); - range = group_range(token_vec, i); - // create_group(t_vector *token_vec, t_vector *exec_vec, int *i) - cmd = malloc(sizeof(char *) * (range.right + 1)); - if (!cmd) - return (false); - while (i < i + range.left + range.right) - { - token = (t_token *)ft_vec_get(token_vec, i); - cmd[i - range.left] = ft_strdup(token->value); - if (!exec->cmd[i - range.left]) - return (false); - exec = create_exec(cmd); - exec_vec->push(exec_vec, (t_exec *)exec); - i++; - } - } - else - i++; + if (i == 0 && (token->type == I_REDIRECT)) + redirect_token(token_vec, &i); + if (i == token->vec->length - 1 || i == token->vec->length - 2) + && token->type == O_REDIRECT || token->type == A_REDIRECT)) + redirect_token(token_vec, &i); } - if (exec) - return (true); - // pos = &cat -> &vec->data return (true); } + +/* + < infile cat -e + + +#+ */ /* +#+ */ /* Created: 2023/07/20 11:12:20 by mdekker #+# #+# */ -/* Updated: 2023/08/04 04:14:29 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/12 20:25:03 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ t_func_map *return_map(void) func_map[6] = (t_func_map){is_r_redirect, O_REDIRECT}; func_map[7] = (t_func_map){is_l_redirect, I_REDIRECT}; func_map[8] = (t_func_map){is_a_redirect, A_REDIRECT}; - func_map[9] = (t_func_map){is_heredoc, I_HEREDOC}; + func_map[9] = (t_func_map){is_heredoc, HEREDOC}; func_map[10] = (t_func_map){NULL, STRING}; return (func_map); } diff --git a/src/parser/verify_token.c b/src/parser/verify_token.c index fbb49dd..7731d6d 100644 --- a/src/parser/verify_token.c +++ b/src/parser/verify_token.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/02 16:57:32 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/03 12:46:53 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/12 20:25:03 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -36,10 +36,10 @@ bool verify_token(t_vector *vec) if (vec->len == 0) return (false); token = vec->data[0]; - if (token->type != I_REDIRECT && token->type != I_HEREDOC) + if (token->type != I_REDIRECT && token->type != HEREDOC) return (false); token = vec->data[vec->len - 1]; - if (token->type != I_REDIRECT && token->type != I_HEREDOC) + if (token->type != I_REDIRECT && token->type != HEREDOC) return (false); i = 0; while (i < vec->len - 1) From 2bcbf3af9cf64c6dbffc8ef43278821089c8097b Mon Sep 17 00:00:00 2001 From: JuliusdB Date: Sat, 12 Aug 2023 21:21:22 +0200 Subject: [PATCH 68/91] =?UTF-8?q?=F0=9F=94=A7=20fix(structs.h):=20update?= =?UTF-8?q?=20struct=20field=20names=20for=20better=20clarity=20and=20sema?= =?UTF-8?q?ntics=20=F0=9F=94=A7=20fix(exec.c):=20update=20comments=20and?= =?UTF-8?q?=20remove=20unnecessary=20code=20=F0=9F=94=A7=20fix(group.c):?= =?UTF-8?q?=20update=20comments=20and=20remove=20unnecessary=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/structs.h | 19 +++++---- src/exec/exec.c | 96 ++++++---------------------------------------- src/exec/group.c | 19 ++++++--- 3 files changed, 35 insertions(+), 99 deletions(-) diff --git a/includes/structs.h b/includes/structs.h index 695f466..215ab99 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/08/11 12:24:08 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/12 21:20:51 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -59,16 +59,15 @@ typedef struct s_signal * @brief The struct for the execution * * @param pipes The vector of pipes - * @param here_doc The here_doc stop-word token - * @param input_redirect The input_redirect file to read from - * @param output_redirect The output_redirect file to write to + * @param start_redirect can be any type of redirect or HEREDOC, + value is filename or stopword + * @param end_redirect can be any type of redirect, the value is the filename */ typedef struct s_exec { t_vector pipes; - t_token here_doc; - t_token input_redirect; - t_token output_redirect; + t_token start_redirect; + t_token end_redirect; } t_exec; /** @@ -80,11 +79,11 @@ typedef struct s_exec typedef struct s_pipe { char **cmd; - pid_t pd; int locate; - int right_pipe[2]; + pid_t pd; int left_pipe[2]; -} t_pipe; // + int right_pipe[2]; +} t_pipe; /** * @brief The global struct diff --git a/src/exec/exec.c b/src/exec/exec.c index 9593b0e..8b69b4d 100644 --- a/src/exec/exec.c +++ b/src/exec/exec.c @@ -6,95 +6,23 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:50 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/11 12:12:25 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/12 21:05:48 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ /* -1. begin indirect -2. end outdirect -3. count pipes - -pipe_count - -put all structs in vector - -each process pipes and forks -left_process 1 pipe -middle_process a pipe from left_process and a pipe_right - - -if (pipe_count > 0) -{ - left_process; - while (pipe_count > 1) - { - middle_process.c - pipe_count--; - } - right_process; -} -vec->length -waitpid's -close_pipes - - -for each struct in commands t_exec{ - // Create a pipe - int pipefd[2]; - pipe(pipefd); - - // Fork a new child process - pid_t pid = fork(); - - - if (pid == 0) { // Child process - if (is_middle_process) { - // Close the unused write end of the input pipe - close(input_pipe[1]); - - // Redirect standard input to the read end of the input pipe - dup2(input_pipe[0], STDIN_FILENO); - close(input_pipe[0]); - - // Close the unused read end of the output pipe - close(output_pipe[0]); - - // Redirect standard output to the write end of the output pipe - dup2(output_pipe[1], STDOUT_FILENO); - close(output_pipe[1]); - } - - // Execute the command - execvp(command, command_args); - - if (pid == 0) { // Child process - iif left_pipe[0] == -1 && right_pipe[0] != -1 - left_process - iif left_pipe[0] != -1 && right_pipe[0] == -1 - right_process - if left_pipe[0] != -1 && right_pipe[0] != -1 - middle_process - - close(pipefd[0]); - - // Redirect standard output to the write end of the pipe - dup2(pipefd[1], STDOUT_FILENO); - close(pipefd[1]); - - // Execute the command - execvp(command, command_args); - } else { // Parent process - // Close the write end of the pipe - close(pipefd[1]); - - // Redirect standard input to the read end of the pipe - dup2(pipefd[0], STDIN_FILENO); - close(pipefd[0]); - } -} - +0. start cprocess +1. check for start_heredoc +2. check for start_redirect + if start_redirect execute start_red function +3. check for out_redirect +4. check for size of t_pipe + if size==1 + dont pipe and just execute +5. if size of t_pipe size==2 + create 1 pipe, 2 cprocesses + for each size greater than 2 add 1 pipe and 1 cprocess make the entire executor happen in a childprocess, that way for example setting the redirects in main process wont affect the nexg call diff --git a/src/exec/group.c b/src/exec/group.c index a01f968..badfbcd 100644 --- a/src/exec/group.c +++ b/src/exec/group.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/12 20:25:43 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/12 21:19:32 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -115,12 +115,21 @@ bool create_group(t_vector *token_vec, t_vector *exec_vec, int *i, /** *step by step: - * 1. check if there is a redirect at the start or end of the token_vec + * 1. check if there is a redirect at the start + * + * + * if there is a heredoc or redirect at the start - * 2. If there is a redirect at the start or end of the token_vec store it in t_exec and remove the associated tokens from the token_vec - * 3. Check if there is a heredoc in the token_vec + * create a token for this with the value relating to the type and the value being the stopword/fi;e + * the relating tokens are then removed from the main vector + * + * if there is a redirect at the end (heredoc doesnt matter + - should be handled by verify) + * store the redirect as token, + the value is the filename and then token is the type of redirect + * then remove the corresponding tokens from the main vector - * 4. If there is a heredoc in the token_vec store it in t_exec and remove the associated tokens from the token_vec + store this heredoc stopword as t_token with value set to the stopword * 5. read through the token_vec and check if there is a pipe, if there is store the tokens before the pipe in t_pipe * 6. if there is no pipes store all of the remaining tokens in t_pipe From 2d3d6d2cfed2282cb6af82e960c872c7811c4840 Mon Sep 17 00:00:00 2001 From: JuliusdB Date: Wed, 16 Aug 2023 10:54:01 +0200 Subject: [PATCH 69/91] temp --- includes/structs.h | 19 +++++++++++-------- src/exec/exec.c | 16 +++++++++++++--- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/includes/structs.h b/includes/structs.h index 215ab99..81868fd 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/08/12 21:20:51 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/12 21:30:09 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -58,28 +58,31 @@ typedef struct s_signal /** * @brief The struct for the execution * - * @param pipes The vector of pipes + * @param pipe_groups The vector of vectors, grouping seperate processes, + these vectors contain t_tokens * @param start_redirect can be any type of redirect or HEREDOC, value is filename or stopword * @param end_redirect can be any type of redirect, the value is the filename */ typedef struct s_exec { - t_vector pipes; + t_vector pipe_groups; t_token start_redirect; t_token end_redirect; } t_exec; /** - * @param locate 0 == no pipe - * @param locate 1 == left_process - * @param locate 2 == middle_process - * @param locate 3 == right_process + * @param group a vector of t_tokens, + all of the tokens belonging to 1 child_process + @param cmd an array of commands containing the commands and parameters to be send to the executing function + @param pd the pd of this specific process + @param left_pipe the pipe related to the process happening on the left + @param right_pipe the pipe related to the process happening on the right of this one */ typedef struct s_pipe { + t_vector group; char **cmd; - int locate; pid_t pd; int left_pipe[2]; int right_pipe[2]; diff --git a/src/exec/exec.c b/src/exec/exec.c index 8b69b4d..7126ea9 100644 --- a/src/exec/exec.c +++ b/src/exec/exec.c @@ -6,17 +6,17 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:50 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/12 21:05:48 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/12 21:31:35 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ /* 0. start cprocess -1. check for start_heredoc -2. check for start_redirect +1. check for start_redirect if start_redirect execute start_red function 3. check for out_redirect + if end_redirect exists end_red function 4. check for size of t_pipe if size==1 dont pipe and just execute @@ -27,6 +27,16 @@ make the entire executor happen in a childprocess, that way for example setting the redirects in main process wont affect the nexg call +HEREDOCS are handled as grandchildprocesses, example: + newfile cat << stop +1. Process < newfile in the main process. +2. Fork a child process for cat << stop. +3. In the child process, create a pipe. +4. Fork a grandchild process. +5. In the grandchild process, + write the heredoc content to the pipe and then exit. +6. In the child process, read from the pipe and pass this as input to cat. + */ #include From 9cdba878fbc4e0eaddf29c4243632e151cfad876 Mon Sep 17 00:00:00 2001 From: Julius De Baaij Date: Wed, 16 Aug 2023 17:26:41 +0200 Subject: [PATCH 70/91] heredoc setup + structs --- Makefile | 2 +- includes/minishell.h | 4 +-- includes/structs.h | 29 ++++++++--------- libft | 2 +- src/exec/group.c | 30 ++++++++++------- src/exec/heredoc.c | 31 ++++++++++++++++++ src/main.c | 4 +-- src/structs/exec.c | 77 +++++++++++++++++++++++++++++++------------- src/utils/init.c | 4 +-- 9 files changed, 125 insertions(+), 58 deletions(-) create mode 100644 src/exec/heredoc.c diff --git a/Makefile b/Makefile index 4fc9602..84dd618 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = minishell -SRC = main check_input utils/error structs/token structs/exec parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string lexer/token utils/init structs/env utils/global exec/group parser/verify_token +SRC = main check_input utils/error structs/token structs/exec parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string lexer/token utils/init structs/env utils/global exec/group exec/heredoc parser/verify_token SRCS = $(addsuffix .c, $(addprefix src/, $(SRC))) OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a diff --git a/includes/minishell.h b/includes/minishell.h index 9a2b2e7..65c4807 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/08/04 04:14:01 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/16 16:21:36 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -41,7 +41,7 @@ t_token *create_token(char *value, t_types type); void clear_token(void *data); t_env *create_env(char *key, char *value); void clear_env(void *data); -t_exec *create_exec(char **cmd); +t_exec *create_exec(void); void clear_exec(void *data); /* utils */ diff --git a/includes/structs.h b/includes/structs.h index 81868fd..b18ee8c 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/08/12 21:30:09 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/16 17:22:54 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -58,35 +58,34 @@ typedef struct s_signal /** * @brief The struct for the execution * - * @param pipe_groups The vector of vectors, grouping seperate processes, - these vectors contain t_tokens - * @param start_redirect can be any type of redirect or HEREDOC, - value is filename or stopword - * @param end_redirect can be any type of redirect, the value is the filename + * @param groups A vevctor containing each of the child processes; + * @param start_redirect can be any type of redirect, value is filename + * @param end_redirect can be any type of redirect, value is the filename */ typedef struct s_exec { - t_vector pipe_groups; - t_token start_redirect; - t_token end_redirect; + t_vector process; + t_token *start_redirect; + t_token *end_redirect; } t_exec; /** - * @param group a vector of t_tokens, + * @param input a vector of t_tokens, all of the tokens belonging to 1 child_process @param cmd an array of commands containing the commands and parameters to be send to the executing function @param pd the pd of this specific process @param left_pipe the pipe related to the process happening on the left @param right_pipe the pipe related to the process happening on the right of this one */ -typedef struct s_pipe +typedef struct s_process { - t_vector group; + t_vector input; + int hdoc_fd; char **cmd; pid_t pd; int left_pipe[2]; int right_pipe[2]; -} t_pipe; +} t_process; /** * @brief The global struct @@ -98,10 +97,8 @@ typedef struct s_global { t_vector tokens; t_vector env; - t_pipe exec; t_signal signal; - char *exit_status; - + int exit_status; } t_global; /** diff --git a/libft b/libft index 7cd6635..c2237a9 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 7cd6635d2ed0acd43bc50ffdacae207e28988488 +Subproject commit c2237a91c43d302c8293ee20325c4729dee3ae83 diff --git a/src/exec/group.c b/src/exec/group.c index badfbcd..9b05f56 100644 --- a/src/exec/group.c +++ b/src/exec/group.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/12 21:19:32 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/16 17:25:28 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -117,19 +117,27 @@ bool create_group(t_vector *token_vec, t_vector *exec_vec, int *i, *step by step: * 1. check if there is a redirect at the start * - * - * if there is a heredoc or redirect at the start - + * + * if there is a redirect at the start / end * create a token for this with the value relating to the type and the value being the stopword/fi;e - * the relating tokens are then removed from the main vector + * + * the relating tokens are then removed from the main vector * - * if there is a redirect at the end (heredoc doesnt matter - - should be handled by verify) - * store the redirect as token, - the value is the filename and then token is the type of redirect - * then remove the corresponding tokens from the main vector - store this heredoc stopword as t_token with value set to the stopword + +heredocs call the function heredoc and are stored in each related t_process +heredocs are called before anything else +the heredoc token will contain the filename which needs to be destroyed + + +ls | cat < +#+ */ +/* +#+ */ +/* Created: 2023/08/16 12:15:45 by mdekker/jde #+# #+# */ +/* Updated: 2023/08/16 17:21:14 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +/* + + +int ft_heredoc(char *stop, t_process, int quotes) + +Create newfile with open(); 0_CREATE + +use readline --> interpet based on quotes +write(heredoc_fd) + + +dup2(fd_replace, heredoc_Fd) + +close(herredoc_fd); +remove the file? --> or remove later in child_process; + +*/ \ No newline at end of file diff --git a/src/main.c b/src/main.c index b94fc0d..62343d5 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/02 19:31:07 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/16 13:48:52 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -93,7 +93,7 @@ int main(int ac, char **av, char **env) printf("Parsed!\n"); if (!operator_split(&g_data.tokens)) return (free_global(true), 1); - group_tokens(&g_data.tokens, &g_data.exec); + // group_tokens(&g_data.tokens, &g_data.exec); // found = g_data.tokens.find(&g_data.tokens, find_strings); // if (!found) // { diff --git a/src/structs/exec.c b/src/structs/exec.c index b212317..734b1c7 100644 --- a/src/structs/exec.c +++ b/src/structs/exec.c @@ -6,43 +6,76 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/02 20:42:59 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/04 05:28:20 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/16 17:26:23 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include +/** + * @brief clears data of t_exec + */ +void clear_exec(void *data) +{ + t_exec *exec; + t_vector *p_vec; + + exec = (t_exec *)data; + p_vec = &exec->process; + ft_vec_free(p_vec); +} + +/** + * clears the data inside t_process; +*/ +void clear_process(void *data) +{ + t_process *p; + + p = (t_process *)data; + ft_vec_free(&p->input); + if (p->cmd) + ft_free(p->cmd); +} + /** * @brief Create a t_exec object - * @param cmd is assigned to exec->cmd - * @return t_exec* The created exec struct - * @note left/right pipe values are set to -1 - * @note pid_t is set to -2 by default, as fork() can return -1 + * @return t_exec* The created exec struct, NULL on failure */ -t_exec *create_exec(char **cmd) +t_exec *create_exec(void) { - t_exec *exec; + t_exec *exec; - exec = malloc(sizeof(exec)); + exec = malloc(sizeof(t_exec)); if (!exec) return (NULL); - exec->cmd = cmd; - exec->pd = -2; - exec->locate = 0; - exec->left_pipe[0] = -1; - exec->left_pipe[1] = -1; - exec->right_pipe[0] = -1; - exec->right_pipe[1] = -1; + if (!ft_vec_init(&exec->process, 2, sizeof(t_process), clear_process)) + return (free(exec), NULL); + exec->start_redirect = NULL; + exec->end_redirect = NULL; return (exec); } /** - * @brief clears data of t_exec - */ -void clear_exec(void *data) + * @brief inits a process + * @note left/right pipe values are set to -1 + * @note pid_t is set to -2 by default, as fork() can return -1 + * @return t_process initialised, NULL on malloc failure +*/ +t_process *create_process(void) { - t_exec *exec; - - exec = (t_exec *)data; - ft_free(exec->cmd); + t_process *p; + + p = malloc(sizeof(t_process)); + if (!p) + return (NULL); + if (!ft_vec_init(&p->input, 2, sizeof(t_token), clear_token)) + return (free(p), NULL); + p->hdoc_fd = -1; + p->cmd = NULL; + p->pd = -2; + p->left_pipe[0] = -1; + p->left_pipe[1] = -1; + p->right_pipe[0] = -1; + p->right_pipe[1] = -1; } diff --git a/src/utils/init.c b/src/utils/init.c index 21d0b37..76bce68 100644 --- a/src/utils/init.c +++ b/src/utils/init.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 20:09:52 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/02 20:46:02 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/16 13:56:02 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -67,8 +67,6 @@ bool init(char **env) return (false); if (!ft_vec_init(&g_data.env, 50, sizeof(t_env), clear_env)) return (false); - if (!ft_vec_init(&g_data.exec, 3, sizeof(t_exec), clear_exec)) - return (false); g_data.exit_status = ft_strdup("0"); if (!g_data.exit_status) return (false); From 28ee1bbef9cff1ec189388fccb40d0be6547b25a Mon Sep 17 00:00:00 2001 From: Julius De Baaij Date: Thu, 17 Aug 2023 16:18:11 +0200 Subject: [PATCH 71/91] moving computer --- Makefile | 2 +- includes/minishell.h | 9 ++-- includes/structs.h | 31 ++++++------ src/exec/group.c | 87 ++++++++++++++++---------------- src/exec/heredoc.c | 6 +-- src/main.c | 5 +- src/structs/{exec.c => group.c} | 88 +++++++++++++++++++-------------- 7 files changed, 118 insertions(+), 110 deletions(-) rename src/structs/{exec.c => group.c} (56%) diff --git a/Makefile b/Makefile index 84dd618..42b42df 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = minishell -SRC = main check_input utils/error structs/token structs/exec parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string lexer/token utils/init structs/env utils/global exec/group exec/heredoc parser/verify_token +SRC = main check_input utils/error structs/token structs/group parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string lexer/token utils/init structs/env utils/global exec/group exec/heredoc parser/verify_token SRCS = $(addsuffix .c, $(addprefix src/, $(SRC))) OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a diff --git a/includes/minishell.h b/includes/minishell.h index 65c4807..ad33e9e 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/08/16 16:21:36 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/17 16:18:01 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -41,8 +41,11 @@ t_token *create_token(char *value, t_types type); void clear_token(void *data); t_env *create_env(char *key, char *value); void clear_env(void *data); +t_group *create_group(void); +void clear_group(void *data); +void clear_fname(void *data); t_exec *create_exec(void); -void clear_exec(void *data); +void clear_exec(t_exec *exec); /* utils */ void err(char *err, char *cmd, int exit_code); @@ -63,7 +66,7 @@ bool is_or(char *str); bool is_and(char *str); /* executor */ -bool group_tokens(t_vector *token, t_vector *exec); +t_vector *group_tokens(t_vector *token); /* debug */ void print_vector(t_vector *vec, void (*printer)(void *, size_t)); diff --git a/includes/structs.h b/includes/structs.h index b18ee8c..5032538 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/08/16 17:22:54 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/17 13:28:21 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -56,36 +56,35 @@ typedef struct s_signal } t_signal; /** - * @brief The struct for the execution - * - * @param groups A vevctor containing each of the child processes; - * @param start_redirect can be any type of redirect, value is filename - * @param end_redirect can be any type of redirect, value is the filename - */ + * @param group_vec vec of all of groups to be executed + * @param fname_vec vec of all of the hererdoc filenames + * @note heredoc files are to be deleted after execution is completed +*/ typedef struct s_exec { - t_vector process; - t_token *start_redirect; - t_token *end_redirect; + t_vector group_vec; + t_vector fname_vec; } t_exec; /** - * @param input a vector of t_tokens, - all of the tokens belonging to 1 child_process - @param cmd an array of commands containing the commands and parameters to be send to the executing function + * @brief a group to be individually executed. + * + * @param input a vector of t_tokens belonging to 1 child_process + @param cmd an array containing the commands and parameters to be executed + @param hdoc_vec vector with heredoc filedescriptors @param pd the pd of this specific process @param left_pipe the pipe related to the process happening on the left @param right_pipe the pipe related to the process happening on the right of this one + @warning the heredoc file is to be deleted after usage */ -typedef struct s_process +typedef struct s_group { t_vector input; - int hdoc_fd; char **cmd; pid_t pd; int left_pipe[2]; int right_pipe[2]; -} t_process; +} t_group; /** * @brief The global struct diff --git a/src/exec/group.c b/src/exec/group.c index 9b05f56..dce0776 100644 --- a/src/exec/group.c +++ b/src/exec/group.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/16 17:25:28 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/17 16:13:03 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -83,12 +83,6 @@ the left process can be checked if it is NULL or not * / #include - typedef struct s_local -{ - size_t left; - size_t right; -} t_local; - // static t_local group_range(t_vector *token_vec, int i) // { // t_token *token; @@ -106,29 +100,25 @@ the left process can be checked if it is NULL or not // return (range); // } -bool create_group(t_vector *token_vec, t_vector *exec_vec, int *i, - t_local range) +/** + * @param token_vec the token vector + * @param group the group to add the new tokens to + * @param i to step over the heredoc and the quotes / filenames + * @param +*/ +bool hdoc_found(t_vector *token_vec, t_group group, int *i) { t_token *token; t_exec *exec; -} + char *filename; + t_global g; -/** - *step by step: - * 1. check if there is a redirect at the start - * - * - * if there is a redirect at the start / end - * create a token for this with the value relating to the type and the value being the stopword/fi;e - * - * the relating tokens are then removed from the main vector - * + filename = ft_heredoc(t_types, stopword); -heredocs call the function heredoc and are stored in each related t_process -heredocs are called before anything else -the heredoc token will contain the filename which needs to be destroyed +} +/** ls | cat <lenght) { + group = create_group(); + if (!group) + return (ft_vec_free(&group_vec), NULL); token = (t_token *)ft_vec_get(token_vec, i); - if (i == 0 && (token->type == I_REDIRECT)) - redirect_token(token_vec, &i); - if (i == token->vec->length - 1 || i == token->vec->length - 2) - && token->type == O_REDIRECT || token->type == A_REDIRECT)) - redirect_token(token_vec, &i); + while (token->type != PIPE) + { + if (token->type == HEREDOC) + hdoc_found(token_vec, group, &i); + else + ft_vec_push(&group->input, (void *)dup_token(&token_vec, token)); + if (i >= token_vec->lenght ) + break ; + i++; + token = (t_token *)ft_vec_get(token_vec, i); + } + ft_vec_push(&group_vec, group); // push the created group + if (i >= token_vec->lenght ) + break ; + i++; } - return (true); + return (exec); } - -/* - < infile cat -e - - +#+ */ /* +#+ */ /* Created: 2023/08/16 12:15:45 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/16 17:21:14 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/17 13:18:01 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,10 +21,6 @@ Create newfile with open(); 0_CREATE use readline --> interpet based on quotes write(heredoc_fd) - - -dup2(fd_replace, heredoc_Fd) - close(herredoc_fd); remove the file? --> or remove later in child_process; diff --git a/src/main.c b/src/main.c index 62343d5..5873fe4 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/16 13:48:52 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/17 13:29:31 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -80,6 +80,7 @@ static void loop(t_vector *vec) int main(int ac, char **av, char **env) { // t_found **found; + t_exec *exec; if (DEBUG) debug(); @@ -93,7 +94,7 @@ int main(int ac, char **av, char **env) printf("Parsed!\n"); if (!operator_split(&g_data.tokens)) return (free_global(true), 1); - // group_tokens(&g_data.tokens, &g_data.exec); + exec = datagroup_tokens(&g_data.tokens); // found = g_data.tokens.find(&g_data.tokens, find_strings); // if (!found) // { diff --git a/src/structs/exec.c b/src/structs/group.c similarity index 56% rename from src/structs/exec.c rename to src/structs/group.c index 734b1c7..87456bb 100644 --- a/src/structs/exec.c +++ b/src/structs/group.c @@ -1,77 +1,68 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* exec.c :+: :+: */ +/* group.c :+: :+: */ /* +:+ */ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/02 20:42:59 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/16 17:26:23 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/17 16:17:56 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -/** - * @brief clears data of t_exec - */ -void clear_exec(void *data) -{ - t_exec *exec; - t_vector *p_vec; - - exec = (t_exec *)data; - p_vec = &exec->process; - ft_vec_free(p_vec); -} - /** * clears the data inside t_process; */ -void clear_process(void *data) +void clear_group(void *data) { - t_process *p; + t_group *p; - p = (t_process *)data; + if (!data) + return ; + p = (t_group *)data; ft_vec_free(&p->input); if (p->cmd) ft_free(p->cmd); } -/** - * @brief Create a t_exec object - * @return t_exec* The created exec struct, NULL on failure - */ -t_exec *create_exec(void) +void clear_fname(void *data) { - t_exec *exec; + char *filename; - exec = malloc(sizeof(t_exec)); + if (!data) + return ; + filename = (char *)data; + if (-1 == unlink(filename)) + perror("minishell:"); + free(filename); +} + +void clear_exec(t_exec *exec) +{ if (!exec) - return (NULL); - if (!ft_vec_init(&exec->process, 2, sizeof(t_process), clear_process)) - return (free(exec), NULL); - exec->start_redirect = NULL; - exec->end_redirect = NULL; - return (exec); + return ; + ft_vec_free(&exec->group_vec); + ft_vec_free(&exec->fname_vec); + free(exec); } /** - * @brief inits a process + * @brief init a t_group object * @note left/right pipe values are set to -1 - * @note pid_t is set to -2 by default, as fork() can return -1 - * @return t_process initialised, NULL on malloc failure + * @note pid_t is set to -2 by default + * @return t_group initialised, NULL on malloc failure */ -t_process *create_process(void) +t_group *create_group(void) { - t_process *p; + t_group *p; - p = malloc(sizeof(t_process)); + p = malloc(sizeof(t_group)); if (!p) return (NULL); if (!ft_vec_init(&p->input, 2, sizeof(t_token), clear_token)) return (free(p), NULL); - p->hdoc_fd = -1; p->cmd = NULL; p->pd = -2; p->left_pipe[0] = -1; @@ -79,3 +70,24 @@ t_process *create_process(void) p->right_pipe[0] = -1; p->right_pipe[1] = -1; } + +/** + * @brief creates t_exec +*/ +t_exec *create_exec(void) +{ + t_exec *exec; + t_vector group_vec; + t_vector fname_vec; + + exec = malloc(sizeof(t_exec)); + if (!exec) + return (NULL); + if (!ft_vec_init(&group_vec, 2, sizeof(t_group), clear_group)) + return (free(exec), NULL); + if (!ft_vec_init(&fname_vec, 1, sizeof(char *), clear_fname)) + return (free(exec), NULL); + exec->group_vec = group_vec; + exec->fname_vec = fname_vec; + return (exec); +} From 55ba96da542435dba4aa727f010ab50bd86f57b7 Mon Sep 17 00:00:00 2001 From: Julius De Baaij Date: Thu, 17 Aug 2023 19:46:39 +0200 Subject: [PATCH 72/91] switching pc --- includes/minishell.h | 4 +-- src/exec/group.c | 60 +++++++++++++++++++++++++++++++++++++++++--- src/exec/heredoc.c | 7 ++++-- 3 files changed, 63 insertions(+), 8 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index ad33e9e..5a459f9 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/08/17 16:18:01 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/17 19:44:33 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -66,7 +66,7 @@ bool is_or(char *str); bool is_and(char *str); /* executor */ -t_vector *group_tokens(t_vector *token); +t_vector *group_tokens(t_vector *token_vec); /* debug */ void print_vector(t_vector *vec, void (*printer)(void *, size_t)); diff --git a/src/exec/group.c b/src/exec/group.c index dce0776..06871ae 100644 --- a/src/exec/group.c +++ b/src/exec/group.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/17 16:13:03 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/17 19:43:22 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -100,23 +100,75 @@ the left process can be checked if it is NULL or not // return (range); // } +static char *rm_quotes(t_token *token) +{ + if (token->type == STRING) + return (token->value); + if (token->type == SINGLE_QUOTE) + return (ft_strtrim(token->value, "\'")); + if (token->type == DOUBLE_QUOTE) + return (ft_strtrim(token->value, "\"")); +} + /** * @param token_vec the token vector * @param group the group to add the new tokens to * @param i to step over the heredoc and the quotes / filenames * @param */ -bool hdoc_found(t_vector *token_vec, t_group group, int *i) +bool hdoc_found(t_vector *token_vec, t_group *group, int *i) { t_token *token; + t_token *token_next; t_exec *exec; + char *end; char *filename; t_global g; - filename = ft_heredoc(t_types, stopword); + filename = ft_strjoin("./src/.heredoc/", ft_itoa((int *)(*i))); + if (!filename) + return (false); // strerror malloc + set exitstatus? + (*i) =+ 1; + token = ft_vec_get(token_vec, (*i)); + if ((token->type == SINGLE_QUOTE || token->type == DOUBLE_QUOTE) && ft_strlen(token->value) == 2) + { + token_next = ft_vec_get(token_vec, (*i) + 1); + if (token_next->type == STRING || token->type == SINGLE_QUOTE || token->type == DOUBLE_QUOTE) + { + end = rm_quotes(token); + if (!end) + return (free(filename), false); // sterror malloc + set exitstatus + if (heredoc(filename, end, token->type)) + return (false); // // strerror hdoc + set exitstatus? + free(end); + token = create_token(filename, STRING); + ft_vec_push(&group->input, (void *)token); + (*i) =+ 2; + return (true); + } + else + { + if (heredoc(filename, "\n", STRING)) + return (false); /// strerror hdoc + set exitstatus? + token = create_token(filename, STRING); + ft_vec_push(&group->input, (void *)token); + (*i) =+ 1; + return (true); + } + } + else + { + if (heredoc(filename, token->value, token->type)) + return (false); + token = create_token(filename, STRING); + ft_vec_push(&group->input, (void *)token); + (*i) =+ 1; + return (true); + } +} + -} /** diff --git a/src/exec/heredoc.c b/src/exec/heredoc.c index 940972a..71eba33 100644 --- a/src/exec/heredoc.c +++ b/src/exec/heredoc.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/16 12:15:45 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/17 13:18:01 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/17 18:42:23 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,7 +15,10 @@ /* -int ft_heredoc(char *stop, t_process, int quotes) +int ft_heredoc(char *filename, char *stop, t_type quotes) + + +if t_type quotes == NULL Create newfile with open(); 0_CREATE From 95bed6ef9a422a0bf285f9a6dbb79ecdac9f3582 Mon Sep 17 00:00:00 2001 From: JuliusdB Date: Fri, 18 Aug 2023 14:37:49 +0200 Subject: [PATCH 73/91] =?UTF-8?q?=F0=9F=94=A7=20chore(.gitignore):=20add?= =?UTF-8?q?=20.heredoc=20file=20to=20the=20repository=20to=20track=20hered?= =?UTF-8?q?oc=20files=20=F0=9F=94=A7=20chore(Makefile):=20add=20exec/exec.?= =?UTF-8?q?c=20to=20the=20source=20files=20list=20to=20include=20it=20in?= =?UTF-8?q?=20the=20build=20process=20=F0=9F=94=A7=20chore(includes/minish?= =?UTF-8?q?ell.h):=20add=20exec=20function=20declaration=20to=20the=20head?= =?UTF-8?q?er=20file=20for=20proper=20function=20prototype=20=F0=9F=94=A7?= =?UTF-8?q?=20chore(libft):=20update=20libft=20submodule=20to=20commit=207?= =?UTF-8?q?cd6635d2ed0acd43bc50ffdacae207e28988488=20=F0=9F=94=A7=20chore(?= =?UTF-8?q?src/exec/exec.c):=20refactor=20exec=20function=20to=20improve?= =?UTF-8?q?=20code=20readability=20and=20remove=20unnecessary=20comments?= =?UTF-8?q?=20=F0=9F=94=A7=20chore(src/exec/group.c):=20refactor=20group?= =?UTF-8?q?=5Frange=20function=20to=20improve=20code=20readability=20and?= =?UTF-8?q?=20remove=20commented=20code=20=F0=9F=94=A7=20chore(src/exec/he?= =?UTF-8?q?redoc.c):=20refactor=20heredoc=20function=20to=20improve=20code?= =?UTF-8?q?=20readability=20and=20remove=20unnecessary=20comments=20?= =?UTF-8?q?=F0=9F=94=A7=20chore(src/main.c):=20refactor=20main=20function?= =?UTF-8?q?=20to=20improve=20code=20readability=20and=20remove=20commented?= =?UTF-8?q?=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + Makefile | 2 +- includes/minishell.h | 3 +- libft | 2 +- src/exec/exec.c | 72 +++++++++++++++++++++++++++--- src/exec/group.c | 103 ++++++++++++++++++------------------------- src/exec/heredoc.c | 44 +++++++++++++++++- src/main.c | 10 +++-- 8 files changed, 163 insertions(+), 74 deletions(-) diff --git a/.gitignore b/.gitignore index 49bf532..e97d8e7 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ !includes/* !libft/* !readline/* +!.heredoc diff --git a/Makefile b/Makefile index 42b42df..d40cfc0 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = minishell -SRC = main check_input utils/error structs/token structs/group parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string lexer/token utils/init structs/env utils/global exec/group exec/heredoc parser/verify_token +SRC = main check_input utils/error structs/token structs/group parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string lexer/token utils/init structs/env utils/global exec/group exec/heredoc parser/verify_token exec/exec SRCS = $(addsuffix .c, $(addprefix src/, $(SRC))) OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a diff --git a/includes/minishell.h b/includes/minishell.h index 5a459f9..cddf510 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/08/17 19:44:33 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/18 14:25:55 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -67,6 +67,7 @@ bool is_and(char *str); /* executor */ t_vector *group_tokens(t_vector *token_vec); +bool exec(t_exec *exec); /* debug */ void print_vector(t_vector *vec, void (*printer)(void *, size_t)); diff --git a/libft b/libft index c2237a9..7cd6635 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit c2237a91c43d302c8293ee20325c4729dee3ae83 +Subproject commit 7cd6635d2ed0acd43bc50ffdacae207e28988488 diff --git a/src/exec/exec.c b/src/exec/exec.c index 7126ea9..eef57a9 100644 --- a/src/exec/exec.c +++ b/src/exec/exec.c @@ -6,20 +6,16 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:50 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/12 21:31:35 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/18 14:36:30 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ /* 0. start cprocess -1. check for start_redirect - if start_redirect execute start_red function -3. check for out_redirect - if end_redirect exists end_red function 4. check for size of t_pipe if size==1 - dont pipe and just execute + dont pipe and call single_process 5. if size of t_pipe size==2 create 1 pipe, 2 cprocesses for each size greater than 2 add 1 pipe and 1 cprocess @@ -40,3 +36,67 @@ HEREDOCS are handled as grandchildprocesses, example: */ #include + +bool exec(t_exec *exec) +{ + { + size_t len = exec->group_vec->length; + size_t i = 0; + t_group *group; + + while (i < len) + { + group = vector_get(exec->group_vec, i); + group->pd = fork(); + if (group->pd == 0) + { + if (len == 1) + { + single_process(group); + } + else if (len == 2) + { + if (i == 0) + { + pipe(group->right_pipe); + left_process(group); + } + else + { + pipe(group->left_pipe); + right_process(group); + } + } + else + { + if (i == 0) + { + pipe(group->right_pipe); + left_process(group); + } + else if (i == len - 1) + { + pipe(group->left_pipe); + right_process(group); + } + else + { + pipe(group->left_pipe); + pipe(group->right_pipe); + middle_process(group); + } + } + exit(0); + } + i++; + } + + i = 0; + while (i < len) + { + group = vector_get(exec->group_vec, i); + waitpid(group->pd, NULL, 0); + i++; + } + return (true); + } diff --git a/src/exec/group.c b/src/exec/group.c index 06871ae..13e07df 100644 --- a/src/exec/group.c +++ b/src/exec/group.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/17 19:43:22 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/18 14:11:12 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -83,24 +83,24 @@ the left process can be checked if it is NULL or not * / #include -// static t_local group_range(t_vector *token_vec, int i) -// { -// t_token *token; -// t_local range; - -// range.left = i; -// token = (t_token *)ft_vec_get(token_vec, i); -// while (token->type != PIPE && token->type != I_REDIRECT -// && token->type != O_REDIRECT && token->type != A_REDIRECT -// && i < token_vec->lenght) -// { -// i++; -// token = (t_token *)ft_vec_get(token_vec, i); -// } -// return (range); -// } - -static char *rm_quotes(t_token *token) + // static t_local group_range(t_vector *token_vec, int i) + // { + // t_token *token; + // t_local range; + + // range.left = i; + // token = (t_token *)ft_vec_get(token_vec, i); + // while (token->type != PIPE && token->type != I_REDIRECT + // && token->type != O_REDIRECT && token->type != A_REDIRECT + // && i < token_vec->lenght) + // { + // i++; + // token = (t_token *)ft_vec_get(token_vec, i); + // } + // return (range); + // } + + static char *rm_quotes(t_token *token) { if (token->type == STRING) return (token->value); @@ -115,61 +115,42 @@ static char *rm_quotes(t_token *token) * @param group the group to add the new tokens to * @param i to step over the heredoc and the quotes / filenames * @param -*/ + */ bool hdoc_found(t_vector *token_vec, t_group *group, int *i) { - t_token *token; - t_token *token_next; - t_exec *exec; - char *end; - char *filename; + t_token *token; + t_exec *exec; + char *stop; + char *filename; t_global g; filename = ft_strjoin("./src/.heredoc/", ft_itoa((int *)(*i))); if (!filename) return (false); // strerror malloc + set exitstatus? - (*i) =+ 1; + (*i) = +1; token = ft_vec_get(token_vec, (*i)); - if ((token->type == SINGLE_QUOTE || token->type == DOUBLE_QUOTE) && ft_strlen(token->value) == 2) + if ((token->type == SINGLE_QUOTE || token->type == DOUBLE_QUOTE) + && ft_strlen(token->value) == 2) { - token_next = ft_vec_get(token_vec, (*i) + 1); - if (token_next->type == STRING || token->type == SINGLE_QUOTE || token->type == DOUBLE_QUOTE) - { - end = rm_quotes(token); - if (!end) - return (free(filename), false); // sterror malloc + set exitstatus - if (heredoc(filename, end, token->type)) - return (false); // // strerror hdoc + set exitstatus? - free(end); - token = create_token(filename, STRING); - ft_vec_push(&group->input, (void *)token); - (*i) =+ 2; - return (true); - } - else - { - if (heredoc(filename, "\n", STRING)) - return (false); /// strerror hdoc + set exitstatus? - token = create_token(filename, STRING); - ft_vec_push(&group->input, (void *)token); - (*i) =+ 1; - return (true); - } + if (heredoc(filename, "", token->type)) + return (false); // strerror hdoc + set exitstatus? + token = create_token(filename, STRING); + ft_vec_push(&group->input, (void *)token); + (*i) += 1; + return (true); } else { - if (heredoc(filename, token->value, token->type)) + stop = rm_quotes(token); + if (heredoc(filename, stop, token->type)) return (false); token = create_token(filename, STRING); ft_vec_push(&group->input, (void *)token); - (*i) =+ 1; + (*i) += 1; return (true); } } - - - /** ls | cat <type == HEREDOC) hdoc_found(token_vec, group, &i); else - ft_vec_push(&group->input, (void *)dup_token(&token_vec, token)); - if (i >= token_vec->lenght ) + ft_vec_push(&group->input, (void *)dup_token(&token_vec, + token)); + if (i >= token_vec->lenght) break ; i++; token = (t_token *)ft_vec_get(token_vec, i); } ft_vec_push(&group_vec, group); // push the created group - if (i >= token_vec->lenght ) - break ; + if (i >= token_vec->lenght) + break ; i++; } return (exec); diff --git a/src/exec/heredoc.c b/src/exec/heredoc.c index 71eba33..dd5b551 100644 --- a/src/exec/heredoc.c +++ b/src/exec/heredoc.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/16 12:15:45 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/17 18:42:23 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/18 14:09:44 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,6 +14,19 @@ /* +Expansion na heredoc +bash-3.2$ cat << $USER +> hello +> juliusdebaaij +> $USER +hello +juliusdebaaij +bash-3.2$ cat $USER +cat: juliusdebaaij: No such file or directory + +*/ +/* + int ft_heredoc(char *filename, char *stop, t_type quotes) @@ -27,4 +40,31 @@ write(heredoc_fd) close(herredoc_fd); remove the file? --> or remove later in child_process; -*/ \ No newline at end of file +*/ + +bool heredoc(char *filename, char *stop, t_type type) +{ + int heredoc_fd; + char *line; + + heredoc_fd = open(filename, O_CREAT | O_WRONLY, 0644); + if (heredoc_fd < 0) + return (false); + while (1) + { + line = readline(">"); + if (!line || ft_strcmp(line, stop) == 0) + break ; + if (type == STRING) + { + line = expand_line(line); + if (!line) + return (false); + } + write(heredoc_fd, line, ft_strlen(line)); + write(heredoc_fd, "\n", 1); + free(line); + } + close(heredoc_fd); + return (true); +} diff --git a/src/main.c b/src/main.c index 5873fe4..3180daf 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/17 13:29:31 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/18 14:36:43 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -79,9 +79,9 @@ static void loop(t_vector *vec) int main(int ac, char **av, char **env) { - // t_found **found; t_exec *exec; + // t_found **found; if (DEBUG) debug(); if (!init(env)) @@ -95,6 +95,10 @@ int main(int ac, char **av, char **env) if (!operator_split(&g_data.tokens)) return (free_global(true), 1); exec = datagroup_tokens(&g_data.tokens); + if (!exec) + return (free_global(true), 1); + executor(exec); + clear_exec(exec); // found = g_data.tokens.find(&g_data.tokens, find_strings); // if (!found) // { @@ -115,7 +119,7 @@ int main(int ac, char **av, char **env) // } // if (DEBUG) print_vector(&g_data.tokens, print_token); - //print_vector(&g_data.env, print_env); + // print_vector(&g_data.env, print_env); free_global(false); return (0); } From 35b3dfad13a25e4fc858ea9708bde0469c5b3133 Mon Sep 17 00:00:00 2001 From: Julius De Baaij Date: Fri, 18 Aug 2023 17:07:28 +0200 Subject: [PATCH 74/91] temp --- libft | 2 +- src/exec/group.c | 35 +++++++++++++++++++++-------------- src/exec/heredoc.c | 4 +++- src/exec/single_process.c | 0 src/main.c | 2 +- src/structs/token.c | 4 ++-- 6 files changed, 28 insertions(+), 19 deletions(-) create mode 100644 src/exec/single_process.c diff --git a/libft b/libft index 7cd6635..c2237a9 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 7cd6635d2ed0acd43bc50ffdacae207e28988488 +Subproject commit c2237a91c43d302c8293ee20325c4729dee3ae83 diff --git a/src/exec/group.c b/src/exec/group.c index 13e07df..245580a 100644 --- a/src/exec/group.c +++ b/src/exec/group.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/18 14:11:12 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/18 17:00:19 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -80,7 +80,7 @@ the left process can be checked if it is NULL or not */ -* / + #include // static t_local group_range(t_vector *token_vec, int i) @@ -116,39 +116,46 @@ the left process can be checked if it is NULL or not * @param i to step over the heredoc and the quotes / filenames * @param */ -bool hdoc_found(t_vector *token_vec, t_group *group, int *i) +bool hdoc_found(t_vector token_vec, t_group *group, int *i, t_vector fname_vec) { t_token *token; t_exec *exec; char *stop; char *filename; - t_global g; + char *fname; filename = ft_strjoin("./src/.heredoc/", ft_itoa((int *)(*i))); if (!filename) return (false); // strerror malloc + set exitstatus? (*i) = +1; - token = ft_vec_get(token_vec, (*i)); + token = ft_vec_get(&token_vec, (*i)); if ((token->type == SINGLE_QUOTE || token->type == DOUBLE_QUOTE) && ft_strlen(token->value) == 2) { if (heredoc(filename, "", token->type)) - return (false); // strerror hdoc + set exitstatus? - token = create_token(filename, STRING); - ft_vec_push(&group->input, (void *)token); - (*i) += 1; - return (true); + return (false); // hdoc error -> error code written in hdoc } else { stop = rm_quotes(token); + if (!stop) + return (false); // malloc error if (heredoc(filename, stop, token->type)) - return (false); + return (false); // hdoc error -> error code written in hdoc + free(stop); + } + fname = strdup(filename); + if (!fname) + return (false); //malloc serror token = create_token(filename, STRING); - ft_vec_push(&group->input, (void *)token); + if (!token) + return (false); // malloc error + if (!ft_vec_push(&group->input, (void *)token)) + return (false); // malloc error + if (!ft_vec_push(&fname_vec, fname)) + return (false); // malloc error (*i) += 1; return (true); - } } /** @@ -192,7 +199,7 @@ t_exec *group_tokens(t_vector *token_vec) while (token->type != PIPE) { if (token->type == HEREDOC) - hdoc_found(token_vec, group, &i); + hdoc_found(*token_vec, group, &i, exec->fname_vec); else ft_vec_push(&group->input, (void *)dup_token(&token_vec, token)); diff --git a/src/exec/heredoc.c b/src/exec/heredoc.c index dd5b551..02aea87 100644 --- a/src/exec/heredoc.c +++ b/src/exec/heredoc.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/16 12:15:45 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/18 14:09:44 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/18 16:25:35 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -45,6 +45,7 @@ remove the file? --> or remove later in child_process; bool heredoc(char *filename, char *stop, t_type type) { int heredoc_fd; + char *filename_dup; char *line; heredoc_fd = open(filename, O_CREAT | O_WRONLY, 0644); @@ -66,5 +67,6 @@ bool heredoc(char *filename, char *stop, t_type type) free(line); } close(heredoc_fd); + filename_dup = ft_strdup(filename); return (true); } diff --git a/src/exec/single_process.c b/src/exec/single_process.c new file mode 100644 index 0000000..e69de29 diff --git a/src/main.c b/src/main.c index 3180daf..277fcf1 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/18 14:36:43 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/18 16:26:55 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/structs/token.c b/src/structs/token.c index 5238dfe..8cb5d2a 100644 --- a/src/structs/token.c +++ b/src/structs/token.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 22:36:40 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 17:44:48 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/18 16:33:25 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ * * @param value A string containing the value of the token * @param type The type of the token - * @return t_token* The created token + * @return t_token* The created token, NULL if malloc error */ t_token *create_token(char *value, t_types type) { From 2dc96ba3662afdd16d0f87eaeaa9c138b2f636a9 Mon Sep 17 00:00:00 2001 From: Julius De Baaij Date: Fri, 18 Aug 2023 18:56:11 +0200 Subject: [PATCH 75/91] tmp --- src/exec/heredoc.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/exec/heredoc.c b/src/exec/heredoc.c index 02aea87..278f926 100644 --- a/src/exec/heredoc.c +++ b/src/exec/heredoc.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/16 12:15:45 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/18 16:25:35 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/18 18:33:27 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -42,6 +42,9 @@ remove the file? --> or remove later in child_process; */ +/** + * +*/ bool heredoc(char *filename, char *stop, t_type type) { int heredoc_fd; From 68579d4cafffd00176218ee405ac902c795fbc25 Mon Sep 17 00:00:00 2001 From: JuliusdB Date: Sat, 19 Aug 2023 17:55:00 +0200 Subject: [PATCH 76/91] =?UTF-8?q?=F0=9F=90=9B=20fix(server.ts):=20change?= =?UTF-8?q?=20port=20variable=20case=20from=20lowercase=20port=20to=20uppe?= =?UTF-8?q?rcase=20PORT=20to=20improve=20semantics=20=E2=9C=A8=20feat(serv?= =?UTF-8?q?er.ts):=20add=20support=20for=20process.env.PORT=20environment?= =?UTF-8?q?=20variable=20to=20be=20able=20to=20run=20app=20on=20a=20config?= =?UTF-8?q?urable=20port=20=F0=9F=90=9B=20fix(Makefile):=20add=20missing?= =?UTF-8?q?=20source=20files=20to=20SRC=20variable=20to=20fix=20compilatio?= =?UTF-8?q?n=20errors=20=E2=9C=A8=20feat(enum.h):=20add=20enum=20for=20dif?= =?UTF-8?q?ferent=20types=20of=20errors=20and=20processes=20for=20better?= =?UTF-8?q?=20error=20handling=20and=20process=20identification=20?= =?UTF-8?q?=E2=9C=A8=20feat(minishell.h):=20add=20errno.h=20include=20for?= =?UTF-8?q?=20better=20error=20handling=20=E2=9C=A8=20feat(minishell.h):?= =?UTF-8?q?=20add=20rm=5Fquotes=20function=20to=20remove=20quotes=20from?= =?UTF-8?q?=20tokens=20=E2=9C=A8=20feat(minishell.h):=20add=20t=5Fexit=20e?= =?UTF-8?q?num=20for=20different=20types=20of=20errors=20=E2=9C=A8=20feat(?= =?UTF-8?q?minishell.h):=20add=20t=5Fprocess=20enum=20for=20different=20ty?= =?UTF-8?q?pes=20of=20processes=20=E2=9C=A8=20feat(minishell.h):=20add=20e?= =?UTF-8?q?nvp=20parameter=20to=20t=5Fexec=20struct=20to=20pass=20environm?= =?UTF-8?q?ent=20variables=20to=20executor=20=E2=9C=A8=20feat(minishell.h)?= =?UTF-8?q?:=20add=20create=5Fprocesses=20function=20to=20create=20child?= =?UTF-8?q?=20processes=20for=20execution=20=E2=9C=A8=20feat(exec.c):=20ad?= =?UTF-8?q?d=20close=5Fpipes=20function=20to=20close=20pipe=20file=20descr?= =?UTF-8?q?iptors=20in=20groups=20=E2=9C=A8=20feat(exec.c):=20add=20wait?= =?UTF-8?q?=5Fprocesses=20function=20to=20wait=20for=20child=20processes?= =?UTF-8?q?=20to=20finish=20execution=20=E2=9C=A8=20feat(executor.c):=20ad?= =?UTF-8?q?d=20executor=20function=20to=20handle=20execution=20of=20comman?= =?UTF-8?q?ds=20and=20processes=20=E2=9C=A8=20feat(executor.c):=20add=20ex?= =?UTF-8?q?it=5Fmini=20function=20to=20handle=20program=20exit=20with=20er?= =?UTF-8?q?ror=20message=20and=20exit=20code=20=E2=9C=A8=20feat(executor.c?= =?UTF-8?q?):=20add=20err=20function=20to=20handle=20error=20messages=20an?= =?UTF-8?q?d=20exit=20codes=20=E2=9C=A8=20feat(executor.c):=20add=20handle?= =?UTF-8?q?=5Fredirects=20function=20to=20handle=20input/output=20redirect?= =?UTF-8?q?ions=20in=20groups=20=E2=9C=A8=20feat(exec=5Fprocess.c):=20add?= =?UTF-8?q?=20exec=5Fprocess=20function=20to=20execute=20individual=20proc?= =?UTF-8?q?esses=20in=20groups=20=E2=9C=A8=20feat(group.c):=20add=20group?= =?UTF-8?q?=5Ftokens=20function=20to=20group=20tokens=20into=20executable?= =?UTF-8?q?=20groups=20=E2=9C=A8=20feat(miscellaneous.c):=20add=20rm=5Fquo?= =?UTF-8?q?tes=20function=20to=20remove=20quotes=20from=20tokens?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- includes/enum.h | 35 +++++++- includes/minishell.h | 16 +++- includes/structs.h | 7 +- libft | 2 +- src/exec/create_processes.c | 88 +++++++++++++++++++ src/exec/exec.c | 150 +++++++++++++++------------------ src/exec/exec_process.c | 96 +++++++++++++++++++++ src/exec/group.c | 163 +++++++----------------------------- src/exec/left_process.c | 0 src/exec/middle_process.c | 0 src/exec/redirect.c | 46 ++++++++++ src/exec/right_process.c | 0 src/exec/single_process.c | 0 src/main.c | 36 +++++--- src/utils/error.c | 82 +++++++++++++++++- src/utils/init.c | 2 +- src/utils/miscellaneous.c | 23 +++++ 18 files changed, 506 insertions(+), 242 deletions(-) create mode 100644 src/exec/create_processes.c create mode 100644 src/exec/exec_process.c delete mode 100644 src/exec/left_process.c delete mode 100644 src/exec/middle_process.c delete mode 100644 src/exec/right_process.c delete mode 100644 src/exec/single_process.c create mode 100644 src/utils/miscellaneous.c diff --git a/Makefile b/Makefile index d40cfc0..b644baa 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = minishell -SRC = main check_input utils/error structs/token structs/group parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string lexer/token utils/init structs/env utils/global exec/group exec/heredoc parser/verify_token exec/exec +SRC = main check_input utils/error structs/token structs/group parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string lexer/token utils/init structs/env utils/global exec/group exec/heredoc parser/verify_token exec/exec exec/create_processes exec/redirect exec/exec_process utils/miscellaneous SRCS = $(addsuffix .c, $(addprefix src/, $(SRC))) OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a diff --git a/includes/enum.h b/includes/enum.h index 962a0fa..d4adc1a 100644 --- a/includes/enum.h +++ b/includes/enum.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/13 17:41:00 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/12 20:25:03 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/19 17:23:57 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -50,4 +50,37 @@ typedef enum e_types HEREDOC } t_types; +/** + * @brief The enum for the different types of errors + * + * @param PERROR A perror + * @param NOT_FOUND A not found error + * @param PERMISSION A permission error + * @param SYNTAX A syntax error + * @param SYNTAX_MINI A syntax error for the minishell + * @param SIGNAL_C A signal interrupt + * + */ +typedef enum e_exit +{ + PERROR, + MALLOC, + NOT_FOUND, + PERMISSION, + SYNTAX, + SYNTAX_MINI, + SIGNAL_C +} t_exit; + +/** + * @brief defines which process is being executed + */ +typedef enum e_process +{ + SINGLE, + LEFT, + RIGHT, + MIDDLE, +} t_process; + #endif diff --git a/includes/minishell.h b/includes/minishell.h index cddf510..bd276a6 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,13 +6,14 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/08/18 14:25:55 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/19 17:26:56 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #ifndef MINISHELL_H # define MINISHELL_H +# include # include # include # include @@ -48,7 +49,9 @@ t_exec *create_exec(void); void clear_exec(t_exec *exec); /* utils */ -void err(char *err, char *cmd, int exit_code); +void exit_mini(char *str, int exit_code); +void err(t_exit type, char *name, t_exec *exec); +char *rm_quotes(t_token *token); /* parser */ void parser(t_vector *vec); @@ -65,9 +68,14 @@ bool contains_env_var(char *str); bool is_or(char *str); bool is_and(char *str); +t_exec *group_tokens(t_vector *token_vec, char **envp); + /* executor */ -t_vector *group_tokens(t_vector *token_vec); -bool exec(t_exec *exec); + +bool executor(t_exec *exec); +bool create_processes(t_exec *exec); +void exec_process(t_process type, t_group *group, char **envp); +void redirect_input(t_group *group, size_t i); /* debug */ void print_vector(t_vector *vec, void (*printer)(void *, size_t)); diff --git a/includes/structs.h b/includes/structs.h index 5032538..a36060d 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/08/17 13:28:21 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/19 16:43:50 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -59,16 +59,17 @@ typedef struct s_signal * @param group_vec vec of all of groups to be executed * @param fname_vec vec of all of the hererdoc filenames * @note heredoc files are to be deleted after execution is completed -*/ + */ typedef struct s_exec { t_vector group_vec; t_vector fname_vec; + char **envp; } t_exec; /** * @brief a group to be individually executed. - * + * * @param input a vector of t_tokens belonging to 1 child_process @param cmd an array containing the commands and parameters to be executed @param hdoc_vec vector with heredoc filedescriptors diff --git a/libft b/libft index c2237a9..7cd6635 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit c2237a91c43d302c8293ee20325c4729dee3ae83 +Subproject commit 7cd6635d2ed0acd43bc50ffdacae207e28988488 diff --git a/src/exec/create_processes.c b/src/exec/create_processes.c new file mode 100644 index 0000000..dedfd6f --- /dev/null +++ b/src/exec/create_processes.c @@ -0,0 +1,88 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* create_processes.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/08/19 12:40:07 by mdekker/jde #+# #+# */ +/* Updated: 2023/08/19 17:30:39 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +static bool add_pipes(t_vector *group_vec, size_t process_count) +{ + size_t i; + t_group *group; + t_group *next_group; + + i = 0; + while (i < process_count - 1) + { + group = ft_vector_get(group_vec, i); + if (pipe(group->right_pipe) != 0) + return (false); + next_group = ft_vector_get(group_vec, i + 1); + next_group->left_pipe[0] = group->right_pipe[0]; + next_group->left_pipe[1] = group->right_pipe[1]; + i++; + } + return (true); +} + +static bool fork_processes(t_exec *exec, size_t process_count) +{ + size_t i; + t_group *group; + t_group *next_group; + t_vector *group_vec; + + group_vec = &exec->group_vec; + i = 0; + while (i < process_count) + { + group = ft_vector_get(group_vec, i); + group->pd = fork(); + if (group->pd == -1) + return (false); + if (group->pd == 0) + { + if (i == 0) + exec_process(LEFT, group, exec->envp); + else if (i == process_count - 1) + exec_process(RIGHT, group, exec->envp); + else + exec_process(MIDDLE, group, exec->envp); + } + i++; + } + return (true); +} + +bool create_processes(t_exec *exec) +{ + t_vector *group_vec; + size_t process_count; + t_group *group; + + group_vec = &exec->group_vec; + process_count = group_vec->lenght; + if (process_count == 1) + { + group = ft_vector_get(group_vec, 0); + group->pd = fork(); + if (group->pd == -1) + return (false); + exec_process(SINGLE, group, exec->envp); + } + else + { + if (!add_pipes(group_vec, process_count)) + return (false); + if (!fork_processes(group_vec, process_count)) + return (false); + } + return (true); +} diff --git a/src/exec/exec.c b/src/exec/exec.c index eef57a9..697ee50 100644 --- a/src/exec/exec.c +++ b/src/exec/exec.c @@ -6,97 +6,81 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:50 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/18 14:36:30 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/19 17:53:08 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ -/* - -0. start cprocess -4. check for size of t_pipe - if size==1 - dont pipe and call single_process -5. if size of t_pipe size==2 - create 1 pipe, 2 cprocesses - for each size greater than 2 add 1 pipe and 1 cprocess - -make the entire executor happen in a childprocess, that way for example setting -the redirects in main process wont affect the nexg call - -HEREDOCS are handled as grandchildprocesses, example: - newfile cat << stop -1. Process < newfile in the main process. -2. Fork a child process for cat << stop. -3. In the child process, create a pipe. -4. Fork a grandchild process. -5. In the grandchild process, - write the heredoc content to the pipe and then exit. -6. In the child process, read from the pipe and pass this as input to cat. +#include -*/ +bool close_pipes(t_vector *group_vec) +{ + size_t i; + t_group *group; + bool status; -#include + i = 0; + status = true; + while (i < group_vec->lenght) + { + group = ft_vector_get(group_vec, i); + if (group->left_pipe[0] >= 0) + if (close(group->left_pipe[0]) == -1) + status = false; + if (group->left_pipe[1] >= 0) + if (close(group->left_pipe[1]) == -1) + status = false; + if (group->right_pipe[0] >= 0) + if (close(group->right_pipe[0]) == -1) + status = false; + if (group->right_pipe[1] >= 0) + if (close(group->right_pipe[1]) == -1) + status = false; + i++; + } + return (status); +} -bool exec(t_exec *exec) +static int wait_processes(t_vector *group_vec) { + size_t i; + t_group *group; + int status; + int temp; + + i = 0; + status = 0; + while (i < group_vec->lenght) { - size_t len = exec->group_vec->length; - size_t i = 0; - t_group *group; + group = ft_vector_get(group_vec, i); + if (group->pd >= 0) + if (waitpid(group->pd, &temp, 0) == -1) + status = -1; + i++; + } + if (status == 0) + status = WEXITSTATUS(temp); + return (status); +} - while (i < len) - { - group = vector_get(exec->group_vec, i); - group->pd = fork(); - if (group->pd == 0) - { - if (len == 1) - { - single_process(group); - } - else if (len == 2) - { - if (i == 0) - { - pipe(group->right_pipe); - left_process(group); - } - else - { - pipe(group->left_pipe); - right_process(group); - } - } - else - { - if (i == 0) - { - pipe(group->right_pipe); - left_process(group); - } - else if (i == len - 1) - { - pipe(group->left_pipe); - right_process(group); - } - else - { - pipe(group->left_pipe); - pipe(group->right_pipe); - middle_process(group); - } - } - exit(0); - } - i++; - } +bool executor(t_exec *exec) +{ + int temp; + int status; - i = 0; - while (i < len) - { - group = vector_get(exec->group_vec, i); - waitpid(group->pd, NULL, 0); - i++; - } - return (true); + if (!create_processes(exec)) + { + close_pipes(&exec->group_vec); + wait_process(&exec->group_vec); + err(PERROR, NULL, exec); + } + if (!close_pipes(&exec->group_vec)) + { + wait_processes(&exec->group_vec); + err(PERROR, NULL, exec); } + status = wait_processes(&exec->group_vec); + if (!status) + err(PERROR, NULL, exec); + clear_exec(exec); + exit(status); +} diff --git a/src/exec/exec_process.c b/src/exec/exec_process.c new file mode 100644 index 0000000..6f036c1 --- /dev/null +++ b/src/exec/exec_process.c @@ -0,0 +1,96 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* exec_process.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/08/19 16:08:08 by mdekker/jde #+# #+# */ +/* Updated: 2023/08/19 17:54:16 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +extern t_global g_data; + +void exec_process(t_process type, t_group *group, char **envp) +{ + size_t i; + char *str; + t_token *token; + + close_start(type, group); + token = ft_vector_get(&group->input, 0); + handle_redirects(group); + group->cmd = combine_tokens(&group->input); + if (!group->cmd) + err("malloc error", 1, NULL); + str = rm_quotes(token->value); + if (!str) + err("malloc error", 1, NULL); + check_cmd(str); + dup_fd(type, group); + built_in(str, group->cmd, &g_data.env); + execve(str, group->cmd, envp); +} + +static void close_start(t_process type, t_group *group) +{ + if (type == LEFT) + { + if (close(group->right_pipe[0]) == -1) + err(PERROR, 1, NULL); + } + else if (type == RIGHT) + { + if (close(group->left_pipe[1]) == -1) + err(PERROR, 1, NULL); + } + else if (type == MIDDLE) + { + if (close(group->left_pipe[1]) == -1) + err(PERROR, 1, NULL); + if (close(group->right_pipe[0]) == -1) + err(PERROR, 1, NULL); + } +} + +static void dup_fd(t_process type, t_group *group) +{ + if (type == LEFT) + { + if (dup2(group->right_pipe[1], STDOUT_FILENO) == -1) + err(PERROR, 1, NULL); + } + else if (type == RIGHT) + { + if (dup2(group->left_pipe[0], STDIN_FILENO) == -1) + err(PERROR, 1, NULL); + } + else if (type == MIDDLE) + { + if (dup2(group->left_pipe[0], STDIN_FILENO) == -1) + err(PERROR, 1, NULL); + if (dup2(group->right_pipe[1], STDOUT_FILENO) == -1) + err(PERROR, 1, NULL); + } +} + +static void built_in(char *str, char **cmd, t_vector *env) +{ + if (ft_strcmp(str, "echo") == 0) + ft_echo(str, cmd, env); + else if (ft_strcmp(str, "cd") == 0) + ft_cd(str, cmd, env); + else if (ft_strcmp(str, "pwd") == 0) + ft_pwd(str, cmd, env); + else if (ft_strcmp(str, "export") == 0) + ft_export(str, cmd, env); + else if (ft_strcmp(str, "unset") == 0) + ft_unset(str, cmd, env); + else if (ft_strcmp(str, "env") == 0) + ft_env(str, cmd, env); + else if (ft_strcmp(str, "exit") == 0) + ft_exit(str, cmd, env); +} diff --git a/src/exec/group.c b/src/exec/group.c index 245580a..0f79f08 100644 --- a/src/exec/group.c +++ b/src/exec/group.c @@ -6,123 +6,27 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/18 17:00:19 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/19 16:52:22 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ -/* - -group the tokens into their executable groups - -< infile cat | cat -e | wc > outfile - -Groups: -"< infile" == start_I_redirect --> function -"wc > outfile" == end_O_redirect --> function - -cat == left_process (pipe on the right side, no pipe on the left side) -cat -e == middle_process (pipe on the left side and pipe on the right side) -wc == right_process (pipe on the right side, no pipe on the left side) - - -< infile cat | cat -e > outfile | echo "hello" | wc - -redirects are only handled by main process if they are on the outside, - if redirects are not fully on the outside they are handled by the child_processes - -groups: -< infile == start_I_redirect --> function - -cat == left_process -->function -cat -e > outfile == middle_process --> function -echo "hello" = middle_process --> function -wc = right_process --> function - -redirects not on the outside should be handled by childprocesses - -HEREDOCs should always be handled in the main process ( before forking ) -all redirect should be handled before the child process is forked if on the outside - otherwise it should be handled by the child process - -quote expansion handled by mees - -pipes are handled by the child process(left, right and middle process) - -parantheses should be handled beforehand? - -AND OR are their own childprocesses? idk still need to look into that - - * @param DOUBLE_QUOTE (") - * @param SINGLE_QUOTE (') - * @param PIPE (|) - * @param PARENTHESES (()) - * @param OR (||) - * @param AND (&&) - * @param ENV ($) - * @param DQ_ENV An environment variable in double quotes ("$") - * @param ENV_QUESTION An environment variable with a question mark ("$?") - * @param STRING A string - - - ------------------------- -edge case: - meaning no fork should be started for left_pipe -the right process will be cat - -e --> meaning a fork should be started for right_pipe - - -to do this keep track of which tokens have already been read so that when a pipe is found -the left process can be checked if it is NULL or not - -*/ - - #include - // static t_local group_range(t_vector *token_vec, int i) - // { - // t_token *token; - // t_local range; - - // range.left = i; - // token = (t_token *)ft_vec_get(token_vec, i); - // while (token->type != PIPE && token->type != I_REDIRECT - // && token->type != O_REDIRECT && token->type != A_REDIRECT - // && i < token_vec->lenght) - // { - // i++; - // token = (t_token *)ft_vec_get(token_vec, i); - // } - // return (range); - // } - - static char *rm_quotes(t_token *token) -{ - if (token->type == STRING) - return (token->value); - if (token->type == SINGLE_QUOTE) - return (ft_strtrim(token->value, "\'")); - if (token->type == DOUBLE_QUOTE) - return (ft_strtrim(token->value, "\"")); -} - /** + * @brief Creates a token for the hdoc file, and adds the filename to the f_name * @param token_vec the token vector * @param group the group to add the new tokens to * @param i to step over the heredoc and the quotes / filenames * @param */ -bool hdoc_found(t_vector token_vec, t_group *group, int *i, t_vector fname_vec) +bool hdoc_found(t_vector token_vec, t_group *group, int *i, + t_vector fname_vec) { - t_token *token; - t_exec *exec; - char *stop; - char *filename; - char *fname; + t_token *token; + t_exec *exec; + char *stop; + char *filename; + char *fname; filename = ft_strjoin("./src/.heredoc/", ft_itoa((int *)(*i))); if (!filename) @@ -144,41 +48,30 @@ bool hdoc_found(t_vector token_vec, t_group *group, int *i, t_vector fname_vec) return (false); // hdoc error -> error code written in hdoc free(stop); } - fname = strdup(filename); - if (!fname) - return (false); //malloc serror + fname = ft_strdup(filename); + if (!fname) + return (false); // malloc serror + if ((&group->input)->lenght == 0) + token = create_token(filename, I_REDIRECT); + else token = create_token(filename, STRING); - if (!token) - return (false); // malloc error - if (!ft_vec_push(&group->input, (void *)token)) - return (false); // malloc error - if (!ft_vec_push(&fname_vec, fname)) - return (false); // malloc error - (*i) += 1; - return (true); + if (!token) + return (false); // malloc error + if (!ft_vec_push(&group->input, (void *)token)) + return (false); // malloc error + if (!ft_vec_push(&fname_vec, fname)) + return (false); // malloc error + (*i) += 1; + return (true); } /** - -ls | cat <envp = envp; + // verify that groups only have redirects / heredocs at ends return (exec); } diff --git a/src/exec/left_process.c b/src/exec/left_process.c deleted file mode 100644 index e69de29..0000000 diff --git a/src/exec/middle_process.c b/src/exec/middle_process.c deleted file mode 100644 index e69de29..0000000 diff --git a/src/exec/redirect.c b/src/exec/redirect.c index e69de29..f57e2e6 100644 --- a/src/exec/redirect.c +++ b/src/exec/redirect.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* redirect.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/08/19 16:32:48 by mdekker/jde #+# #+# */ +/* Updated: 2023/08/19 17:07:40 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +/* + redirects need to remove the tokens related to the redirect + +*/ + +#include + +void handle_redirects(t_vector *group) +{ + size_t i; + t_token *token; + + i = 0; + while (i < (&group->input)->lenght) + { + token = ft_vector_get(&group->input, i); + if (token->type == I_REDIRECT || token->type == O_REDIRECT + || token->type == A_REDIRECT) + { + redirect_input(group, i); + i = 0; + } + else + i++; + } +} + +//redirect_input() + +//in_rediect + +//out_redirect + +//append_redirect \ No newline at end of file diff --git a/src/exec/right_process.c b/src/exec/right_process.c deleted file mode 100644 index e69de29..0000000 diff --git a/src/exec/single_process.c b/src/exec/single_process.c deleted file mode 100644 index e69de29..0000000 diff --git a/src/main.c b/src/main.c index 277fcf1..19a00b2 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/18 16:26:55 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/19 16:44:40 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -80,6 +80,8 @@ static void loop(t_vector *vec) int main(int ac, char **av, char **env) { t_exec *exec; + pid_t pid; + int status; // t_found **found; if (DEBUG) @@ -88,17 +90,27 @@ int main(int ac, char **av, char **env) return (1); if (ac == 2) { - if (!lexer(av[1], &g_data.tokens)) - return (free_global(true), 1); - parser(&g_data.tokens); - printf("Parsed!\n"); - if (!operator_split(&g_data.tokens)) - return (free_global(true), 1); - exec = datagroup_tokens(&g_data.tokens); - if (!exec) - return (free_global(true), 1); - executor(exec); - clear_exec(exec); + pid = fork(); + if (pid == -1) + exit_mini("initial fork failed", errno); + if (pid == 0) + { + if (!lexer(av[1], &g_data.tokens)) + mini_exit("lexer failed", 1); + parser(&g_data.tokens); + printf("Parsed!\n"); + if (!operator_split(&g_data.tokens)) + mini_exit("operator split failed", 1); + // verify token order -> no pipes start/end, + // no double pipes / redirects + exec = group_tokens(&g_data.tokens, env); + if (!exec) + exit_mini("malloc error"); + exit(executor(exec)); + } + if (waitpid(pid, &status, 0) == -1) + exit_mini("waitpid failed", errno); + g_data.exit_status = WEXITSTATUS(status); // found = g_data.tokens.find(&g_data.tokens, find_strings); // if (!found) // { diff --git a/src/utils/error.c b/src/utils/error.c index 482e0e6..92ce43c 100644 --- a/src/utils/error.c +++ b/src/utils/error.c @@ -6,19 +6,20 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 21:42:24 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/24 11:38:21 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/19 16:38:45 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include +/* /** * @brief Prints an error message * * @param err The error message * @param cmd The command that caused the error * @param exit_code The exit code of the error - */ + void err(char *err, char *cmd, int exit_code) { printf("\033[1;31m"); @@ -28,3 +29,80 @@ void err(char *err, char *cmd, int exit_code) printf("Exit code: %d\n", exit_code); printf("\033[0m"); } +*/ + +/** + * @brief called if there is an error in minihsell that should close the program + * + * @param str The error message + */ +void exit_mini(char *str, int exit_code) +{ + if (str) + { + write(STDERR_FILENO, "minishell: ", 11); + write(STDERR_FILENO, str, ft_strlen(str)); + write(STDERR_FILENO, "\n", 1); + } + else + write(STDERR_FILENO, "minishell: error\n", 16); + free_global(true); + exit(exit_code); +} + +/** + * @brief Prints an error message + * + * @param type The type of error + * @param name The name of the file that caused the error + * @param func The function to call before exiting + */ +void err(t_exit type, char *name, t_exec *exec) +{ + int status; + + if (exec) + clear_exec(exec); // free function for example: clear_exec(); + if (type == PERROR) + { + perror("minishell:"); + status = errno; // related exit code? + } + if (type == MALLOC) + { + write(STDERR_FILENO, "minishell: malloc error\n", 25); + status = 1; + } + if (type == NOT_FOUND) + { + write(STDERR_FILENO, "minishell: ", 11); + write(STDERR_FILENO, name, ft_strlen(name)); + write(STDERR_FILENO, ": No such file or directory\n", 28); + status = 127; + } + if (type == PERMISSION) + { + write(STDERR_FILENO, "minishell: ", 11); + write(STDERR_FILENO, name, ft_strlen(name)); + write(STDERR_FILENO, ": Permission denied\n", 20); + status = 126; + } + if (type == SYNTAX) + { + write(STDERR_FILENO, "minishell: syntax error near unexpected token : ", + 48); + write(STDERR_FILENO, name, ft_strlen(name)); + status = 258; + } + if (type == SYNTAX_MINI) + { + write(STDERR_FILENO, "minishell: unfinished operator", 48); + status = 2; + } + if (type == SIGNAL_C) + { + write(STDERR_FILENO, "^C\n", 3); + status = 130; + } + exit(status); +} diff --git a/src/utils/init.c b/src/utils/init.c index 76bce68..724d1f2 100644 --- a/src/utils/init.c +++ b/src/utils/init.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 20:09:52 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/16 13:56:02 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/19 16:53:26 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/utils/miscellaneous.c b/src/utils/miscellaneous.c new file mode 100644 index 0000000..41992d5 --- /dev/null +++ b/src/utils/miscellaneous.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* miscellaneous.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/08/19 16:53:28 by mdekker/jde #+# #+# */ +/* Updated: 2023/08/19 16:53:41 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +char *rm_quotes(t_token *token) +{ + if (token->type == STRING) + return (token->value); + if (token->type == SINGLE_QUOTE) + return (ft_strtrim(token->value, "\'")); + if (token->type == DOUBLE_QUOTE) + return (ft_strtrim(token->value, "\"")); +} From 7d42904113d05a5f89a350100c356f552ae6d6c2 Mon Sep 17 00:00:00 2001 From: JuliusdB Date: Sat, 19 Aug 2023 20:26:54 +0200 Subject: [PATCH 77/91] temp --- src/exec/exec_process.c | 4 ++-- src/utils/error.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/exec/exec_process.c b/src/exec/exec_process.c index 6f036c1..7a1775e 100644 --- a/src/exec/exec_process.c +++ b/src/exec/exec_process.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/19 16:08:08 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/19 17:54:16 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/19 18:01:27 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -29,9 +29,9 @@ void exec_process(t_process type, t_group *group, char **envp) str = rm_quotes(token->value); if (!str) err("malloc error", 1, NULL); - check_cmd(str); dup_fd(type, group); built_in(str, group->cmd, &g_data.env); + check_cmd(str); execve(str, group->cmd, envp); } diff --git a/src/utils/error.c b/src/utils/error.c index 92ce43c..4b10f1f 100644 --- a/src/utils/error.c +++ b/src/utils/error.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 21:42:24 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/19 16:38:45 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/19 19:33:03 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -62,7 +62,7 @@ void err(t_exit type, char *name, t_exec *exec) int status; if (exec) - clear_exec(exec); // free function for example: clear_exec(); + clear_exec(exec); // free exec maybe replace with general free function if (type == PERROR) { perror("minishell:"); From 682e11d7c35f0efc5e66378a358849f22fe9600a Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Mon, 28 Aug 2023 11:29:11 +0200 Subject: [PATCH 78/91] =?UTF-8?q?=F0=9F=94=80=20chore(libft):=20update=20s?= =?UTF-8?q?ubmodule=20commit=20reference=20=F0=9F=94=A7=20fix(print=5Fvect?= =?UTF-8?q?or.c):=20fix=20typo=20in=20variable=20name=20'lenght'=20to=20'l?= =?UTF-8?q?ength'=20=F0=9F=94=A7=20fix(create=5Fprocesses.c):=20fix=20typo?= =?UTF-8?q?=20in=20variable=20name=20'lenght'=20to=20'length'=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(exec.c):=20fix=20typo=20in=20variable=20name?= =?UTF-8?q?=20'lenght'=20to=20'length'=20=F0=9F=94=A7=20fix(group.c):=20fi?= =?UTF-8?q?x=20typo=20in=20variable=20name=20'lenght'=20to=20'length'=20?= =?UTF-8?q?=F0=9F=94=A7=20fix(redirect.c):=20fix=20typo=20in=20variable=20?= =?UTF-8?q?name=20'lenght'=20to=20'length'=20=F0=9F=94=A7=20fix(token.c):?= =?UTF-8?q?=20fix=20typo=20in=20function=20name=20'array=5Flenght'=20to=20?= =?UTF-8?q?'array=5Flength'=20=F0=9F=94=A7=20fix(index.c):=20fix=20typo=20?= =?UTF-8?q?in=20variable=20name=20'lenght'=20to=20'length'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- libft | 2 +- src/debug/print_vector.c | 4 ++-- src/exec/create_processes.c | 4 ++-- src/exec/exec.c | 6 +++--- src/exec/group.c | 10 +++++----- src/exec/redirect.c | 6 +++--- src/lexer/token.c | 6 +++--- src/parser/index.c | 4 ++-- 8 files changed, 21 insertions(+), 21 deletions(-) diff --git a/libft b/libft index 7cd6635..d93dc57 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 7cd6635d2ed0acd43bc50ffdacae207e28988488 +Subproject commit d93dc5782b1ceb4adfbe09a99e098803819e08eb diff --git a/src/debug/print_vector.c b/src/debug/print_vector.c index 185b7f1..85a85b2 100644 --- a/src/debug/print_vector.c +++ b/src/debug/print_vector.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 13:51:45 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/12 20:25:03 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/28 11:25:54 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ void print_vector(t_vector *vec, void (*printer)(void *, size_t)) size_t i; i = 0; - while (i < vec->lenght) + while (i < vec->length) { printer(vec->get(vec, i), i); i++; diff --git a/src/exec/create_processes.c b/src/exec/create_processes.c index dedfd6f..ef35afc 100644 --- a/src/exec/create_processes.c +++ b/src/exec/create_processes.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/19 12:40:07 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/19 17:30:39 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/28 11:25:54 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -68,7 +68,7 @@ bool create_processes(t_exec *exec) t_group *group; group_vec = &exec->group_vec; - process_count = group_vec->lenght; + process_count = group_vec->length; if (process_count == 1) { group = ft_vector_get(group_vec, 0); diff --git a/src/exec/exec.c b/src/exec/exec.c index 697ee50..7409eb3 100644 --- a/src/exec/exec.c +++ b/src/exec/exec.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:50 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/19 17:53:08 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/28 11:25:54 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ bool close_pipes(t_vector *group_vec) i = 0; status = true; - while (i < group_vec->lenght) + while (i < group_vec->length) { group = ft_vector_get(group_vec, i); if (group->left_pipe[0] >= 0) @@ -49,7 +49,7 @@ static int wait_processes(t_vector *group_vec) i = 0; status = 0; - while (i < group_vec->lenght) + while (i < group_vec->length) { group = ft_vector_get(group_vec, i); if (group->pd >= 0) diff --git a/src/exec/group.c b/src/exec/group.c index 0f79f08..70b31e8 100644 --- a/src/exec/group.c +++ b/src/exec/group.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/19 16:52:22 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/28 11:25:54 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -51,7 +51,7 @@ bool hdoc_found(t_vector token_vec, t_group *group, int *i, fname = ft_strdup(filename); if (!fname) return (false); // malloc serror - if ((&group->input)->lenght == 0) + if ((&group->input)->length == 0) token = create_token(filename, I_REDIRECT); else token = create_token(filename, STRING); @@ -83,7 +83,7 @@ t_exec *group_tokens(t_vector *token_vec, char **envp) if (!exec) return (NULL); i = 0; - while (i < token_vec->lenght) + while (i < token_vec->length) { group = create_group(); if (!group) @@ -96,13 +96,13 @@ t_exec *group_tokens(t_vector *token_vec, char **envp) else ft_vec_push(&group->input, (void *)dup_token(&token_vec, token)); - if (i >= token_vec->lenght) + if (i >= token_vec->length) break ; i++; token = (t_token *)ft_vec_get(token_vec, i); } ft_vec_push(&group_vec, group); // push the created group - if (i >= token_vec->lenght) + if (i >= token_vec->length) break ; i++; } diff --git a/src/exec/redirect.c b/src/exec/redirect.c index f57e2e6..d0e018c 100644 --- a/src/exec/redirect.c +++ b/src/exec/redirect.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/19 16:32:48 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/19 17:07:40 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/28 11:25:54 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ void handle_redirects(t_vector *group) t_token *token; i = 0; - while (i < (&group->input)->lenght) + while (i < (&group->input)->length) { token = ft_vector_get(&group->input, i); if (token->type == I_REDIRECT || token->type == O_REDIRECT @@ -43,4 +43,4 @@ void handle_redirects(t_vector *group) //out_redirect -//append_redirect \ No newline at end of file +//append_redirect diff --git a/src/lexer/token.c b/src/lexer/token.c index 0e34524..1b80b6e 100644 --- a/src/lexer/token.c +++ b/src/lexer/token.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/21 13:06:18 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/24 19:52:11 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/28 11:25:54 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -137,7 +137,7 @@ static char **split(t_token *token) return (array); } -size_t array_lenght(char **array) +size_t array_length(char **array) { size_t i; @@ -155,7 +155,7 @@ bool operator_split(t_vector *vec) char **array; i = 0; - while (i < vec->lenght) + while (i < vec->length) { token = (t_token *)vec->get(vec, i); if (token->type == STRING && find_operator(token)) diff --git a/src/parser/index.c b/src/parser/index.c index 289acde..7bc97e6 100644 --- a/src/parser/index.c +++ b/src/parser/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 11:12:20 by mdekker #+# #+# */ -/* Updated: 2023/08/12 20:25:03 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/28 11:25:54 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -54,7 +54,7 @@ void parse_loop(t_vector *vec, t_func_map *func_map) i = 0; j = 0; - while (i < vec->lenght) + while (i < vec->length) { token = (t_token *)vec->get(vec, i); if (token->type == UNKNOWN) From 35008c93ff7492a89cea017959b37d9b9ee7b108 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Mon, 28 Aug 2023 11:32:59 +0200 Subject: [PATCH 79/91] =?UTF-8?q?=F0=9F=90=9B=20fix(create=5Fprocesses.c):?= =?UTF-8?q?=20change=20ft=5Fvector=5Fget=20to=20vector=5Fget=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(create=5Fprocesses.c):=20change=20ft=5Fvecto?= =?UTF-8?q?r=5Fget=20to=20vector=5Fget=20=F0=9F=90=9B=20fix(create=5Fproce?= =?UTF-8?q?sses.c):=20change=20ft=5Fvector=5Fget=20to=20vector=5Fget=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(create=5Fprocesses.c):=20change=20ft=5Fvecto?= =?UTF-8?q?r=5Fget=20to=20vector=5Fget=20=F0=9F=90=9B=20fix(exec.c):=20cha?= =?UTF-8?q?nge=20ft=5Fvector=5Fget=20to=20vector=5Fget=20=F0=9F=90=9B=20fi?= =?UTF-8?q?x(exec.c):=20change=20ft=5Fvector=5Fget=20to=20vector=5Fget=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(exec=5Fprocess.c):=20change=20ft=5Fvector=5F?= =?UTF-8?q?get=20to=20vector=5Fget=20=F0=9F=90=9B=20fix(group.c):=20change?= =?UTF-8?q?=20ft=5Fvec=5Fget=20to=20vec=5Fget=20=F0=9F=90=9B=20fix(group.c?= =?UTF-8?q?):=20change=20ft=5Fvec=5Fpush=20to=20vec=5Fpush=20=F0=9F=90=9B?= =?UTF-8?q?=20fix(group.c):=20change=20ft=5Fvec=5Fpush=20to=20vec=5Fpush?= =?UTF-8?q?=20=F0=9F=90=9B=20fix(group.c):=20change=20ft=5Fvec=5Fpush=20to?= =?UTF-8?q?=20vec=5Fpush=20=F0=9F=90=9B=20fix(group.c):=20change=20ft=5Fve?= =?UTF-8?q?c=5Fpush=20to=20vec=5Fpush=20=F0=9F=90=9B=20fix(group.c):=20cha?= =?UTF-8?q?nge=20ft=5Fvec=5Fpush?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🔧 fix(group.c): change function name from ft_vec_free to vec_free 🔧 fix(group.c): change function name from ft_vec_init to vec_init 🔧 fix(global.c): change function name from ft_vec_free to vec_free 🔧 fix(init.c): change function name from ft_vec_init to vec_init --- src/exec/create_processes.c | 10 +++++----- src/exec/exec.c | 6 +++--- src/exec/exec_process.c | 4 ++-- src/exec/group.c | 18 +++++++++--------- src/exec/redirect.c | 4 ++-- src/main.c | 2 +- src/structs/group.c | 16 ++++++++-------- src/utils/global.c | 6 +++--- src/utils/init.c | 6 +++--- 9 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/exec/create_processes.c b/src/exec/create_processes.c index ef35afc..3fd5600 100644 --- a/src/exec/create_processes.c +++ b/src/exec/create_processes.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/19 12:40:07 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/28 11:25:54 by mdekker ######## odam.nl */ +/* Updated: 2023/08/28 11:29:57 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,10 +21,10 @@ static bool add_pipes(t_vector *group_vec, size_t process_count) i = 0; while (i < process_count - 1) { - group = ft_vector_get(group_vec, i); + group = vector_get(group_vec, i); if (pipe(group->right_pipe) != 0) return (false); - next_group = ft_vector_get(group_vec, i + 1); + next_group = vector_get(group_vec, i + 1); next_group->left_pipe[0] = group->right_pipe[0]; next_group->left_pipe[1] = group->right_pipe[1]; i++; @@ -43,7 +43,7 @@ static bool fork_processes(t_exec *exec, size_t process_count) i = 0; while (i < process_count) { - group = ft_vector_get(group_vec, i); + group = vector_get(group_vec, i); group->pd = fork(); if (group->pd == -1) return (false); @@ -71,7 +71,7 @@ bool create_processes(t_exec *exec) process_count = group_vec->length; if (process_count == 1) { - group = ft_vector_get(group_vec, 0); + group = vector_get(group_vec, 0); group->pd = fork(); if (group->pd == -1) return (false); diff --git a/src/exec/exec.c b/src/exec/exec.c index 7409eb3..d2b6b7b 100644 --- a/src/exec/exec.c +++ b/src/exec/exec.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:50 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/28 11:25:54 by mdekker ######## odam.nl */ +/* Updated: 2023/08/28 11:29:56 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ bool close_pipes(t_vector *group_vec) status = true; while (i < group_vec->length) { - group = ft_vector_get(group_vec, i); + group = vector_get(group_vec, i); if (group->left_pipe[0] >= 0) if (close(group->left_pipe[0]) == -1) status = false; @@ -51,7 +51,7 @@ static int wait_processes(t_vector *group_vec) status = 0; while (i < group_vec->length) { - group = ft_vector_get(group_vec, i); + group = vector_get(group_vec, i); if (group->pd >= 0) if (waitpid(group->pd, &temp, 0) == -1) status = -1; diff --git a/src/exec/exec_process.c b/src/exec/exec_process.c index 7a1775e..0675156 100644 --- a/src/exec/exec_process.c +++ b/src/exec/exec_process.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/19 16:08:08 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/19 18:01:27 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/28 11:29:57 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ void exec_process(t_process type, t_group *group, char **envp) t_token *token; close_start(type, group); - token = ft_vector_get(&group->input, 0); + token = vector_get(&group->input, 0); handle_redirects(group); group->cmd = combine_tokens(&group->input); if (!group->cmd) diff --git a/src/exec/group.c b/src/exec/group.c index 70b31e8..f75f779 100644 --- a/src/exec/group.c +++ b/src/exec/group.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/28 11:25:54 by mdekker ######## odam.nl */ +/* Updated: 2023/08/28 11:29:57 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ bool hdoc_found(t_vector token_vec, t_group *group, int *i, if (!filename) return (false); // strerror malloc + set exitstatus? (*i) = +1; - token = ft_vec_get(&token_vec, (*i)); + token = vec_get(&token_vec, (*i)); if ((token->type == SINGLE_QUOTE || token->type == DOUBLE_QUOTE) && ft_strlen(token->value) == 2) { @@ -57,9 +57,9 @@ bool hdoc_found(t_vector token_vec, t_group *group, int *i, token = create_token(filename, STRING); if (!token) return (false); // malloc error - if (!ft_vec_push(&group->input, (void *)token)) + if (!vec_push(&group->input, (void *)token)) return (false); // malloc error - if (!ft_vec_push(&fname_vec, fname)) + if (!vec_push(&fname_vec, fname)) return (false); // malloc error (*i) += 1; return (true); @@ -87,21 +87,21 @@ t_exec *group_tokens(t_vector *token_vec, char **envp) { group = create_group(); if (!group) - return (ft_vec_free(&group_vec), NULL); - token = (t_token *)ft_vec_get(token_vec, i); + return (vec_free(&group_vec), NULL); + token = (t_token *)vec_get(token_vec, i); while (token->type != PIPE) { if (token->type == HEREDOC) hdoc_found(*token_vec, group, &i, exec->fname_vec); else - ft_vec_push(&group->input, (void *)dup_token(&token_vec, + vec_push(&group->input, (void *)dup_token(&token_vec, token)); if (i >= token_vec->length) break ; i++; - token = (t_token *)ft_vec_get(token_vec, i); + token = (t_token *)vec_get(token_vec, i); } - ft_vec_push(&group_vec, group); // push the created group + vec_push(&group_vec, group); // push the created group if (i >= token_vec->length) break ; i++; diff --git a/src/exec/redirect.c b/src/exec/redirect.c index d0e018c..b32f5c9 100644 --- a/src/exec/redirect.c +++ b/src/exec/redirect.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/19 16:32:48 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/28 11:25:54 by mdekker ######## odam.nl */ +/* Updated: 2023/08/28 11:29:57 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ void handle_redirects(t_vector *group) i = 0; while (i < (&group->input)->length) { - token = ft_vector_get(&group->input, i); + token = vector_get(&group->input, i); if (token->type == I_REDIRECT || token->type == O_REDIRECT || token->type == A_REDIRECT) { diff --git a/src/main.c b/src/main.c index 19a00b2..89e3b15 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/19 16:44:40 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/28 11:32:46 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/structs/group.c b/src/structs/group.c index 87456bb..f813d1f 100644 --- a/src/structs/group.c +++ b/src/structs/group.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/02 20:42:59 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/17 16:17:56 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/28 11:29:57 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ void clear_group(void *data) if (!data) return ; p = (t_group *)data; - ft_vec_free(&p->input); + vec_free(&p->input); if (p->cmd) ft_free(p->cmd); } @@ -43,8 +43,8 @@ void clear_exec(t_exec *exec) { if (!exec) return ; - ft_vec_free(&exec->group_vec); - ft_vec_free(&exec->fname_vec); + vec_free(&exec->group_vec); + vec_free(&exec->fname_vec); free(exec); } @@ -57,11 +57,11 @@ void clear_exec(t_exec *exec) t_group *create_group(void) { t_group *p; - + p = malloc(sizeof(t_group)); if (!p) return (NULL); - if (!ft_vec_init(&p->input, 2, sizeof(t_token), clear_token)) + if (!vec_init(&p->input, 2, sizeof(t_token), clear_token)) return (free(p), NULL); p->cmd = NULL; p->pd = -2; @@ -83,9 +83,9 @@ t_exec *create_exec(void) exec = malloc(sizeof(t_exec)); if (!exec) return (NULL); - if (!ft_vec_init(&group_vec, 2, sizeof(t_group), clear_group)) + if (!vec_init(&group_vec, 2, sizeof(t_group), clear_group)) return (free(exec), NULL); - if (!ft_vec_init(&fname_vec, 1, sizeof(char *), clear_fname)) + if (!vec_init(&fname_vec, 1, sizeof(char *), clear_fname)) return (free(exec), NULL); exec->group_vec = group_vec; exec->fname_vec = fname_vec; diff --git a/src/utils/global.c b/src/utils/global.c index 1eae36b..3f598a5 100644 --- a/src/utils/global.c +++ b/src/utils/global.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/21 04:17:23 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 12:51:05 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/28 11:29:57 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,10 +21,10 @@ extern t_global g_data; */ void free_global(bool exit) { - ft_vec_free(&g_data.tokens); + vec_free(&g_data.tokens); if (exit) { - ft_vec_free(&g_data.env); + vec_free(&g_data.env); free(g_data.exit_status); } } diff --git a/src/utils/init.c b/src/utils/init.c index 724d1f2..f27d131 100644 --- a/src/utils/init.c +++ b/src/utils/init.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 20:09:52 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/19 16:53:26 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/28 11:29:57 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -63,9 +63,9 @@ static t_signal create_signal_struct(void) bool init(char **env) { g_data.signal = create_signal_struct(); - if (!ft_vec_init(&g_data.tokens, 3, sizeof(t_token), clear_token)) + if (!vec_init(&g_data.tokens, 3, sizeof(t_token), clear_token)) return (false); - if (!ft_vec_init(&g_data.env, 50, sizeof(t_env), clear_env)) + if (!vec_init(&g_data.env, 50, sizeof(t_env), clear_env)) return (false); g_data.exit_status = ft_strdup("0"); if (!g_data.exit_status) From 61ef2e39982660cc7cbd892d1a5cdaee84bbcc81 Mon Sep 17 00:00:00 2001 From: "meesdekker.xyz" Date: Mon, 28 Aug 2023 11:35:42 +0200 Subject: [PATCH 80/91] =?UTF-8?q?=F0=9F=90=9B=20fix(print=5Fvector.c):=20c?= =?UTF-8?q?hange=20function=20call=20from=20vec->get=20to=20vec=5Fget=20?= =?UTF-8?q?=F0=9F=90=9B=20fix(token.c):=20change=20function=20call=20from?= =?UTF-8?q?=20vec->get=20to=20vec=5Fget=20=F0=9F=90=9B=20fix(main.c):=20ch?= =?UTF-8?q?ange=20function=20call=20from=20ft=5Fvec=5Finit=20to=20vec=5Fin?= =?UTF-8?q?it=20=F0=9F=90=9B=20fix(index.c):=20change=20function=20call=20?= =?UTF-8?q?from=20vec->get=20to=20vec=5Fget=20=F0=9F=90=9B=20fix(init.c):?= =?UTF-8?q?=20change=20function=20call=20from=20g=5Fdata.env.push=20to=20v?= =?UTF-8?q?ec=5Fpush?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/debug/print_vector.c | 4 ++-- src/lexer/token.c | 4 ++-- src/main.c | 4 ++-- src/parser/index.c | 4 ++-- src/utils/init.c | 4 ++-- 5 files changed, 10 insertions(+), 10 deletions(-) diff --git a/src/debug/print_vector.c b/src/debug/print_vector.c index 85a85b2..66cec46 100644 --- a/src/debug/print_vector.c +++ b/src/debug/print_vector.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 13:51:45 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/28 11:25:54 by mdekker ######## odam.nl */ +/* Updated: 2023/08/28 11:33:48 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ void print_vector(t_vector *vec, void (*printer)(void *, size_t)) i = 0; while (i < vec->length) { - printer(vec->get(vec, i), i); + printer(vec_get(vec, i), i); i++; } } diff --git a/src/lexer/token.c b/src/lexer/token.c index 1b80b6e..dfdace8 100644 --- a/src/lexer/token.c +++ b/src/lexer/token.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/21 13:06:18 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/28 11:25:54 by mdekker ######## odam.nl */ +/* Updated: 2023/08/28 11:33:48 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -157,7 +157,7 @@ bool operator_split(t_vector *vec) i = 0; while (i < vec->length) { - token = (t_token *)vec->get(vec, i); + token = (t_token *)vec_get(vec, i); if (token->type == STRING && find_operator(token)) { array = split(token); diff --git a/src/main.c b/src/main.c index 89e3b15..a811347 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/28 11:32:46 by mdekker ######## odam.nl */ +/* Updated: 2023/08/28 11:34:13 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -63,7 +63,7 @@ static void loop(t_vector *vec) } free(input); free_global(false); - ft_vec_init(vec, 5, sizeof(t_token), clear_token); + vec_init(vec, 5, sizeof(t_token), clear_token); } } diff --git a/src/parser/index.c b/src/parser/index.c index 7bc97e6..b459911 100644 --- a/src/parser/index.c +++ b/src/parser/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 11:12:20 by mdekker #+# #+# */ -/* Updated: 2023/08/28 11:25:54 by mdekker ######## odam.nl */ +/* Updated: 2023/08/28 11:33:16 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -56,7 +56,7 @@ void parse_loop(t_vector *vec, t_func_map *func_map) j = 0; while (i < vec->length) { - token = (t_token *)vec->get(vec, i); + token = (t_token *)vec_get(vec, i); if (token->type == UNKNOWN) { j = 0; diff --git a/src/utils/init.c b/src/utils/init.c index f27d131..989e8b4 100644 --- a/src/utils/init.c +++ b/src/utils/init.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 20:09:52 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/28 11:29:57 by mdekker ######## odam.nl */ +/* Updated: 2023/08/28 11:34:44 by mdekker ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ static void init_env(char **env) value = ft_strdup(ft_strchr(env[i], '=') + 1); if (!key || !value) exit(1); // !TODO: error message - if (!g_data.env.push(&g_data.env, create_env(key, value))) + if (!vec_push(&g_data.env, create_env(key, value))) exit(1); i++; } From c6e178f0d0cb3b91cffde0e9b7bb551648ddc176 Mon Sep 17 00:00:00 2001 From: Julius De Baaij Date: Wed, 30 Aug 2023 23:48:32 +0200 Subject: [PATCH 81/91] added verify_token function, started on shortening group.c function --- includes/minishell.h | 5 ++- src/exec/group.c | 73 +++++++++++++++++----------------- src/lexer/index.c | 10 ++--- src/lexer/string.c | 8 ++-- src/lexer/token.c | 10 ++--- src/main.c | 4 +- src/parser/verify_token.c | 83 +++++++++++++++++++++++++-------------- src/structs/token.c | 21 +++++++++- src/utils/error.c | 26 +++++++----- 9 files changed, 146 insertions(+), 94 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index bd276a6..670c325 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/08/19 17:26:56 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/30 21:44:28 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -40,6 +40,7 @@ bool operator_split(t_vector *vec); /* structs */ t_token *create_token(char *value, t_types type); void clear_token(void *data); +t_token *dup_token(t_token *input); t_env *create_env(char *key, char *value); void clear_env(void *data); t_group *create_group(void); @@ -50,7 +51,7 @@ void clear_exec(t_exec *exec); /* utils */ void exit_mini(char *str, int exit_code); -void err(t_exit type, char *name, t_exec *exec); +void err(t_exit type, char *name, void (*func)(void *), void *data); char *rm_quotes(t_token *token); /* parser */ diff --git a/src/exec/group.c b/src/exec/group.c index f75f779..1eba1af 100644 --- a/src/exec/group.c +++ b/src/exec/group.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/28 11:29:57 by mdekker ######## odam.nl */ +/* Updated: 2023/08/30 23:47:46 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -28,43 +28,65 @@ bool hdoc_found(t_vector token_vec, t_group *group, int *i, char *filename; char *fname; - filename = ft_strjoin("./src/.heredoc/", ft_itoa((int *)(*i))); + filename = ft_strjoin("./src/.heredoc/", ft_itoa((*i))); if (!filename) return (false); // strerror malloc + set exitstatus? (*i) = +1; token = vec_get(&token_vec, (*i)); if ((token->type == SINGLE_QUOTE || token->type == DOUBLE_QUOTE) && ft_strlen(token->value) == 2) - { - if (heredoc(filename, "", token->type)) - return (false); // hdoc error -> error code written in hdoc - } + heredoc(filename, "", token->type); else { stop = rm_quotes(token); if (!stop) return (false); // malloc error - if (heredoc(filename, stop, token->type)) - return (false); // hdoc error -> error code written in hdoc + heredoc(filename, stop, token->type); free(stop); } fname = ft_strdup(filename); if (!fname) return (false); // malloc serror - if ((&group->input)->length == 0) - token = create_token(filename, I_REDIRECT); - else - token = create_token(filename, STRING); + token = create_token(filename, HEREDOC); if (!token) return (false); // malloc error if (!vec_push(&group->input, (void *)token)) return (false); // malloc error - if (!vec_push(&fname_vec, fname)) + if (!vec_push(&fname_vec, (void *)fname)) return (false); // malloc error (*i) += 1; return (true); } +t_group *make_group(t_vector *token_vec, int *i, t_exec *exec) +{ + t_token *dup; + t_group *group; + t_token *token; + + group = create_group(); + if (!group) + err(MALLOC, NULL, clear_exec, exec); + token = (t_token *)vec_get(token_vec, (*i)); + while (token->type != PIPE) + { + if (token->type == HEREDOC) + hdoc_found(*token_vec, group, i, exec->fname_vec); + else + { + dup = dup_token(token); + if (!dup) + err(MALLOC, NULL, clear_exec, exec); + vec_push(&group->input, (void *)dup); + } + if ((*i) >= token_vec->length) + break ; + (*i)++; + token = (t_token *)vec_get(token_vec, (*i)); + } + return (group); +} + /** * @brief Group the tokens into groups * @@ -77,36 +99,17 @@ t_exec *group_tokens(t_vector *token_vec, char **envp) t_exec *exec; t_vector group_vec; t_group *group; - t_token *token; exec = create_exec(); if (!exec) - return (NULL); + err(MALLOC, 0, 0, 0); i = 0; while (i < token_vec->length) { - group = create_group(); - if (!group) - return (vec_free(&group_vec), NULL); - token = (t_token *)vec_get(token_vec, i); - while (token->type != PIPE) - { - if (token->type == HEREDOC) - hdoc_found(*token_vec, group, &i, exec->fname_vec); - else - vec_push(&group->input, (void *)dup_token(&token_vec, - token)); - if (i >= token_vec->length) - break ; - i++; - token = (t_token *)vec_get(token_vec, i); - } - vec_push(&group_vec, group); // push the created group - if (i >= token_vec->length) - break ; + group = make_group(token_vec, &i, exec); + vec_push(&group_vec, group); i++; } exec->envp = envp; - // verify that groups only have redirects / heredocs at ends return (exec); } diff --git a/src/lexer/index.c b/src/lexer/index.c index 9b55ac5..47b6688 100644 --- a/src/lexer/index.c +++ b/src/lexer/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 13:32:55 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/21 18:16:06 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/30 22:02:44 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -137,20 +137,20 @@ bool lexer(char *input, t_vector *vec) if (checkchar(input[i], "\"\'()") == 1) { if (!check_delimiters(&input[i])) - return (err("bad quote(s) or parantheses", NULL, 1), false); + return (err(SYNTAX, "bad quote(s) or parantheses", NULL, NULL), false); if (!make_string(input, &i, vec)) - return (err("malloc", NULL, 1), false); + return (err(MALLOC, "malloc", NULL, NULL), false); } else if (input[i] == ' ') { if (!create_string(input, &i, vec)) - return (err("malloc", NULL, 1), false); + return (err(MALLOC, "malloc", NULL, NULL), false); } else i++; } if (i > 0 && checkchar(input[i - 1], "\"\') ") == 0) if (!create_string(input, &i, vec)) - return (err("malloc", NULL, 1), false); + return (err(MALLOC, "malloc", NULL, NULL), false); return (true); } diff --git a/src/lexer/string.c b/src/lexer/string.c index 82c57e6..80c42a6 100644 --- a/src/lexer/string.c +++ b/src/lexer/string.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 13:41:35 by mdekker/jde #+# #+# */ -/* Updated: 2023/07/31 17:26:44 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/30 22:00:47 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -41,7 +41,7 @@ static bool build_string(char *str, t_vector *vec, t_local x) value = ft_substr(str, x.left, x.right - x.left + 1); if (!value) return (false); - if (!vec->push(vec, (void *)create_token(value, 0))) + if (!vec_push(vec, (void *)create_token(value, 0))) return (false); return (true); } @@ -98,7 +98,7 @@ bool create_quote_string(char *str, size_t *i, t_vector *vec) value = ft_substr(str, *i, occur_right - (*i)); if (!value) return (false); - if (!vec->push(vec, (void *)create_token(value, 0))) + if (!vec_push(vec, (void *)create_token(value, 0))) return (false); *i = occur_right; while (str[*i] == ' ' && str[*i]) @@ -136,7 +136,7 @@ bool create_paran_string(char *str, size_t *i, t_vector *vec) value = ft_substr(str, *i, occur_right - (*i)); if (!value) return (false); - if (!vec->push(vec, (void *)create_token(value, 0))) + if (!vec_push(vec, (void *)create_token(value, 0))) return (false); *i = occur_right; while (str[*i] == ' ' && str[*i]) diff --git a/src/lexer/token.c b/src/lexer/token.c index dfdace8..ecabde1 100644 --- a/src/lexer/token.c +++ b/src/lexer/token.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/21 13:06:18 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/28 11:33:48 by mdekker ######## odam.nl */ +/* Updated: 2023/08/30 22:03:16 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -161,14 +161,14 @@ bool operator_split(t_vector *vec) if (token->type == STRING && find_operator(token)) { array = split(token); - if (!array || !vec->remove(vec, i)) - return (err("malloc", NULL, 1), false); + if (!array || !vec_remove(vec, i)) + return (err(MALLOC, "malloc", NULL, NULL), false); j = 0; while (array[j]) { token = create_token(array[j], UNKNOWN); - if (!token || !vec->insert(vec, i + j, token)) - return (err("malloc", NULL, 1), false); + if (!token || !vec_insert(vec, i + j, token)) + return (err(MALLOC, "malloc", NULL, NULL), false); j++; } } diff --git a/src/main.c b/src/main.c index a811347..2661849 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/28 11:34:13 by mdekker ######## odam.nl */ +/* Updated: 2023/08/30 21:08:41 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -104,8 +104,6 @@ int main(int ac, char **av, char **env) // verify token order -> no pipes start/end, // no double pipes / redirects exec = group_tokens(&g_data.tokens, env); - if (!exec) - exit_mini("malloc error"); exit(executor(exec)); } if (waitpid(pid, &status, 0) == -1) diff --git a/src/parser/verify_token.c b/src/parser/verify_token.c index 7731d6d..418caf6 100644 --- a/src/parser/verify_token.c +++ b/src/parser/verify_token.c @@ -6,49 +6,72 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/02 16:57:32 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/12 20:25:03 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/30 23:46:39 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -/* -check for double operators -check for starting with anything other token than redirects / heredoc - -geen I_INDIRECT aan het einde +/** + * @brief loop cause it didnt fit in verify_token xd +*/ +static void loop(t_vector *token_vec) +{ + int i; + t_token *token; + t_token *next; -Na een operator mag je alleen STRING/DOUBLE_QUOTE/SINGLE_QUOTE + i = 0; + while (i < token_vec->length) + { + token = vec_get(&token_vec, i); + if (token->type == O_REDIRECT || token->type == I_REDIRECT || token->type == A_REDIRECT || token->type == HEREDOC) // check redirect/HERERDOC + check_redirect(token_vec, i); + else if (i == token_vec->length - 1) // check if last token is not a pipe + { + if (token->type == PIPE) + err(SYNTAX_MINI, token->value, NULL, NULL); + } + else if (i + 1 >= token_vec->length) // checking if there isnt two PIPES + { + next = vec_get(&token_vec, i + 1); + if (token->type == PIPE && next->type == PIPE) + err(SYNTAX, next->value, NULL, NULL); + } + if (i + 1 < token_vec->length) + i++; + } +} -na een pipe mag je wel < > << >> +/** + * @brief checks if redirect is followed by STRING/D_QUOTE/S_QUOTE +*/ +static void check_redirect(t_vector *token_vec, int i) +{ + t_token *token; + t_token *next; -bash-3.2$ <|hello -bash: syntax error near unexpected token `|' -bash-3.2$ cat infile |< bruh -bash: bruh: No such file or directory + token = vec_get(&token_vec, i); + if (i + 1 >= token_vec->length) + err(SYNTAX, "`newline'", NULL, NULL); + next = vec_get(&token_vec, i + 1); + if (next->type != STRING || next->type != DOUBLE_QUOTE || next->type != SINGLE_QUOTE) + err(SYNTAX, next->value, NULL, NULL); +} +/** + * @brief verifies that the input doesnt start with a pipe, checks for double pipes + * @brief checks if redirects/heredocs are followe by a string/quoted string + * @param vec the vector of t_tokens */ -bool verify_token(t_vector *vec) +void verify_token(t_vector *token_vec) { int i; t_token *token; - if (vec->len == 0) - return (false); - token = vec->data[0]; - if (token->type != I_REDIRECT && token->type != HEREDOC) - return (false); - token = vec->data[vec->len - 1]; - if (token->type != I_REDIRECT && token->type != HEREDOC) - return (false); i = 0; - while (i < vec->len - 1) - { - token = vec->data[i]; - if (token->type == I_PIPE && ((t_token *)vec->data[i - + 1])->type == I_PIPE) - return (false); - i++; - } - return (true); + token = vec_get(&token_vec, i); + if (token->type == PIPE) + err(SYNTAX, token->value, NULL, NULL); + loop(token_vec); } diff --git a/src/structs/token.c b/src/structs/token.c index 8cb5d2a..9e3df2d 100644 --- a/src/structs/token.c +++ b/src/structs/token.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 22:36:40 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/18 16:33:25 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/30 21:44:06 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -67,3 +67,22 @@ void clear_token(void *data) token = (t_token *)data; free(token->value); } + +t_token *dup_token(t_token *input) +{ + t_token *dup; + + dup = malloc(sizeof(t_token)); + if (!dup) + return (NULL); + if (!input->value) + dup->value = NULL; + else + { + dup->value = ft_strdup(input->value); + if (!dup->value) + return (free(dup), NULL); + } + dup->type = input->type; + return (dup); +} diff --git a/src/utils/error.c b/src/utils/error.c index 4b10f1f..398b833 100644 --- a/src/utils/error.c +++ b/src/utils/error.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 21:42:24 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/19 19:33:03 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/30 23:18:33 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -55,14 +55,17 @@ void exit_mini(char *str, int exit_code) * * @param type The type of error * @param name The name of the file that caused the error - * @param func The function to call before exiting + * @param func function to be called before exiting + * @param data data to be passed to func + * @warning func is executed with data regardless of data is NULL or not + * @note free global.tokens before returning? */ -void err(t_exit type, char *name, t_exec *exec) +void err(t_exit type, char *name, void (*func)(void *), void *data) { int status; - if (exec) - clear_exec(exec); // free exec maybe replace with general free function + if (func) + func(data); if (type == PERROR) { perror("minishell:"); @@ -70,7 +73,9 @@ void err(t_exit type, char *name, t_exec *exec) } if (type == MALLOC) { - write(STDERR_FILENO, "minishell: malloc error\n", 25); + write(STDERR_FILENO, "minishell: malloc error in : ", 28); + write(STDERR_FILENO, name, ft_strlen(name)); + write(STDERR_FILENO, "\n", 1); status = 1; } if (type == NOT_FOUND) @@ -89,14 +94,17 @@ void err(t_exit type, char *name, t_exec *exec) } if (type == SYNTAX) { - write(STDERR_FILENO, "minishell: syntax error near unexpected token : ", - 48); + write(STDERR_FILENO, "minishell: syntax error near unexpected token `", + 47); write(STDERR_FILENO, name, ft_strlen(name)); + write(STDERR_FILENO, "'\n", 2); status = 258; } if (type == SYNTAX_MINI) { - write(STDERR_FILENO, "minishell: unfinished operator", 48); + write(STDERR_FILENO, "minishell: unfinished operator : `", 34); + write(STDERR_FILENO, name, ft_strlen(name)); + write(STDERR_FILENO, "'\n", 2); status = 2; } if (type == SIGNAL_C) From bed018044db589ef694439e046fe59474a608504 Mon Sep 17 00:00:00 2001 From: JuliusdB Date: Thu, 31 Aug 2023 00:43:33 +0200 Subject: [PATCH 82/91] small fix on verify_token --- src/exec/exec.c | 8 ++++---- src/parser/verify_token.c | 41 +++++++++++++++++++++++---------------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/exec/exec.c b/src/exec/exec.c index d2b6b7b..a5672dd 100644 --- a/src/exec/exec.c +++ b/src/exec/exec.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:50 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/28 11:29:56 by mdekker ######## odam.nl */ +/* Updated: 2023/08/31 00:37:39 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -71,16 +71,16 @@ bool executor(t_exec *exec) { close_pipes(&exec->group_vec); wait_process(&exec->group_vec); - err(PERROR, NULL, exec); + err(PERROR, NULL, clear_exec, exec); } if (!close_pipes(&exec->group_vec)) { wait_processes(&exec->group_vec); - err(PERROR, NULL, exec); + err(PERROR, NULL, clear_exec, exec); } status = wait_processes(&exec->group_vec); if (!status) - err(PERROR, NULL, exec); + err(PERROR, NULL, clear_exec, exec); clear_exec(exec); exit(status); } diff --git a/src/parser/verify_token.c b/src/parser/verify_token.c index 418caf6..a873f71 100644 --- a/src/parser/verify_token.c +++ b/src/parser/verify_token.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/02 16:57:32 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/30 23:46:39 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/31 00:43:21 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ /** * @brief loop cause it didnt fit in verify_token xd -*/ + */ static void loop(t_vector *token_vec) { int i; @@ -25,45 +25,45 @@ static void loop(t_vector *token_vec) while (i < token_vec->length) { token = vec_get(&token_vec, i); - if (token->type == O_REDIRECT || token->type == I_REDIRECT || token->type == A_REDIRECT || token->type == HEREDOC) // check redirect/HERERDOC - check_redirect(token_vec, i); - else if (i == token_vec->length - 1) // check if last token is not a pipe + if (token->type == O_REDIRECT || token->type == I_REDIRECT + || token->type == A_REDIRECT || token->type == HEREDOC) { - if (token->type == PIPE) - err(SYNTAX_MINI, token->value, NULL, NULL); + check_redirect(token_vec, i); + i++; } - else if (i + 1 >= token_vec->length) // checking if there isnt two PIPES + else if (i + 1 < token_vec->length) { next = vec_get(&token_vec, i + 1); - if (token->type == PIPE && next->type == PIPE) + if (token->type == PIPE && next->type == PIPE) err(SYNTAX, next->value, NULL, NULL); } - if (i + 1 < token_vec->length) - i++; + i++; } } /** - * @brief checks if redirect is followed by STRING/D_QUOTE/S_QUOTE -*/ + * @brief checks if redirect is followed by STRING/D_QUOTE/S_QUOTE + */ static void check_redirect(t_vector *token_vec, int i) { t_token *token; - t_token *next; + t_token *next; token = vec_get(&token_vec, i); if (i + 1 >= token_vec->length) err(SYNTAX, "`newline'", NULL, NULL); next = vec_get(&token_vec, i + 1); - if (next->type != STRING || next->type != DOUBLE_QUOTE || next->type != SINGLE_QUOTE) + if (next->type != STRING || next->type != DOUBLE_QUOTE + || next->type != SINGLE_QUOTE) err(SYNTAX, next->value, NULL, NULL); } /** - * @brief verifies that the input doesnt start with a pipe, checks for double pipes + * @brief verifies that the input doesnt start with a pipe, + checks for double pipes * @brief checks if redirects/heredocs are followe by a string/quoted string * @param vec the vector of t_tokens -*/ + */ void verify_token(t_vector *token_vec) { int i; @@ -74,4 +74,11 @@ void verify_token(t_vector *token_vec) if (token->type == PIPE) err(SYNTAX, token->value, NULL, NULL); loop(token_vec); + if (token_vec->length > 1) + { + i = token_vec->length - 1; + token = vec_get(&token_vec, i); + if (token->type == PIPE) + err(SYNTAX_MINI, token->value, NULL, NULL); + } } From 3007a2e81c14930ad80e5472c4f0ab603d541fdf Mon Sep 17 00:00:00 2001 From: JuliusdB Date: Thu, 31 Aug 2023 00:56:58 +0200 Subject: [PATCH 83/91] split group.c, exec is next to be completed --- src/exec/group.c | 69 +++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/src/exec/group.c b/src/exec/group.c index 1eba1af..d259279 100644 --- a/src/exec/group.c +++ b/src/exec/group.c @@ -6,12 +6,29 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/30 23:47:46 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/08/31 00:56:33 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include +static void push_hdoc(char *filename, t_group *group, t_exec *exec) +{ + char *fname; + t_token *token; + + fname = ft_strdup(filename); + if (!fname) + err(MALLOC, "push_hdoc", clear_exec, exec); + token = create_token(filename, HEREDOC); + if (!token) + err(MALLOC, "push_hdoc", clear_exec, exec); + if (!vec_push(&group->input, (void *)token)) + err(MALLOC, "push_hdoc", clear_exec, exec); + if (!vec_push(&exec->fname_vec, (void *)fname)) + err(MALLOC, "push_hdoc", clear_exec, exec); +} + /** * @brief Creates a token for the hdoc file, and adds the filename to the f_name * @param token_vec the token vector @@ -19,18 +36,15 @@ * @param i to step over the heredoc and the quotes / filenames * @param */ -bool hdoc_found(t_vector token_vec, t_group *group, int *i, - t_vector fname_vec) +static void hdoc_found(t_vector token_vec, t_group *group, int *i, t_exec *exec) { t_token *token; - t_exec *exec; char *stop; char *filename; - char *fname; filename = ft_strjoin("./src/.heredoc/", ft_itoa((*i))); if (!filename) - return (false); // strerror malloc + set exitstatus? + err(MALLOC, "hdoc_found", clear_exec, exec); (*i) = +1; token = vec_get(&token_vec, (*i)); if ((token->type == SINGLE_QUOTE || token->type == DOUBLE_QUOTE) @@ -40,49 +54,39 @@ bool hdoc_found(t_vector token_vec, t_group *group, int *i, { stop = rm_quotes(token); if (!stop) - return (false); // malloc error - heredoc(filename, stop, token->type); + err(MALLOC, "hdoc_found", clear_exec, exec); + heredoc(filename, stop, token->type); free(stop); } - fname = ft_strdup(filename); - if (!fname) - return (false); // malloc serror - token = create_token(filename, HEREDOC); - if (!token) - return (false); // malloc error - if (!vec_push(&group->input, (void *)token)) - return (false); // malloc error - if (!vec_push(&fname_vec, (void *)fname)) - return (false); // malloc error + push_hdoc(filename, group, exec); (*i) += 1; - return (true); } -t_group *make_group(t_vector *token_vec, int *i, t_exec *exec) +t_group *make_group(t_vector *token_vec, int *i, t_exec *exec) { - t_token *dup; - t_group *group; - t_token *token; + t_token *dup; + t_group *group; + t_token *token; group = create_group(); if (!group) - err(MALLOC, NULL, clear_exec, exec); + err(MALLOC, "make_group", clear_exec, exec); token = (t_token *)vec_get(token_vec, (*i)); while (token->type != PIPE) { if (token->type == HEREDOC) - hdoc_found(*token_vec, group, i, exec->fname_vec); + hdoc_found(*token_vec, group, i, exec); else { dup = dup_token(token); if (!dup) - err(MALLOC, NULL, clear_exec, exec); + err(MALLOC, "make_group", clear_exec, exec); vec_push(&group->input, (void *)dup); } if ((*i) >= token_vec->length) break ; (*i)++; - token = (t_token *)vec_get(token_vec, (*i)); + token = (t_token *)vec_get(token_vec, (*i)); } return (group); } @@ -95,19 +99,18 @@ t_group *make_group(t_vector *token_vec, int *i, t_exec *exec) */ t_exec *group_tokens(t_vector *token_vec, char **envp) { - size_t i; - t_exec *exec; - t_vector group_vec; - t_group *group; + size_t i; + t_exec *exec; + t_group *group; exec = create_exec(); if (!exec) - err(MALLOC, 0, 0, 0); + err(MALLOC, "group_tokens", NULL, NULL); i = 0; while (i < token_vec->length) { group = make_group(token_vec, &i, exec); - vec_push(&group_vec, group); + vec_push(&exec->group_vec, group); i++; } exec->envp = envp; From 7063fbd694b4a40b0601739ef241a43685373ccd Mon Sep 17 00:00:00 2001 From: Julius De Baaij Date: Fri, 1 Sep 2023 22:53:46 +0200 Subject: [PATCH 84/91] brrrrrrrrrr --- .vscode/settings.json | 7 +++- includes/minishell.h | 18 ++++++-- includes/structs.h | 3 +- src/exec/check_cmd.c | 33 +++++++++++++++ src/exec/combine_cmd.c | 84 +++++++++++++++++++++++++++++++++++++ src/exec/create_processes.c | 10 ++--- src/exec/exec.c | 15 ++++--- src/exec/exec_process.c | 79 ++++++++++++++++++---------------- src/exec/find_cmd.c | 0 src/exec/group.c | 21 ++++++---- src/exec/heredoc.c | 65 +++++++++++----------------- src/exec/utils.c | 36 ++++++++++++++++ src/main.c | 12 +++--- src/parser/verify_token.c | 4 +- src/structs/group.c | 5 ++- src/utils/error.c | 3 +- src/utils/miscellaneous.c | 10 ++++- 17 files changed, 292 insertions(+), 113 deletions(-) create mode 100644 src/exec/check_cmd.c create mode 100644 src/exec/combine_cmd.c delete mode 100644 src/exec/find_cmd.c create mode 100644 src/exec/utils.c diff --git a/.vscode/settings.json b/.vscode/settings.json index 2d929a2..abd26e5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -15,6 +15,11 @@ "files.associations": { "sstream": "c", "__locale": "c", - "__string": "c" + "__string": "c", + "array": "c", + "iterator": "c", + "string": "c", + "string_view": "c", + "vector": "c" } } \ No newline at end of file diff --git a/includes/minishell.h b/includes/minishell.h index 670c325..1085dff 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/08/30 21:44:28 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/01 19:56:19 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ # include # include # include +# include # ifndef DEBUG # define DEBUG 0 @@ -69,15 +70,24 @@ bool contains_env_var(char *str); bool is_or(char *str); bool is_and(char *str); -t_exec *group_tokens(t_vector *token_vec, char **envp); +void verify_tokens(t_vector *token_vec); + /* executor */ +t_exec *group_tokens(t_vector *token_vec, char **envp); +void heredoc(char *filename, char *stop, t_types type, t_exec *exec); -bool executor(t_exec *exec); +int executor(t_exec *exec); bool create_processes(t_exec *exec); -void exec_process(t_process type, t_group *group, char **envp); +void exec_process(t_group *group, t_process type); void redirect_input(t_group *group, size_t i); +//utils +bool is_built_in(char *str); +char **combine_env(t_vector *env_vec); +char **create_cmd(t_vector *input); + + /* debug */ void print_vector(t_vector *vec, void (*printer)(void *, size_t)); void print_token(void *data, size_t i); diff --git a/includes/structs.h b/includes/structs.h index a36060d..73bb11c 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/08/19 16:43:50 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/01 20:35:31 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -64,7 +64,6 @@ typedef struct s_exec { t_vector group_vec; t_vector fname_vec; - char **envp; } t_exec; /** diff --git a/src/exec/check_cmd.c b/src/exec/check_cmd.c new file mode 100644 index 0000000..a64a86c --- /dev/null +++ b/src/exec/check_cmd.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* check_cmd.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/09/01 17:36:19 by mdekker/jde #+# #+# */ +/* Updated: 2023/09/01 19:44:26 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +// static void check_cmd(char *str) +// { +// if (cmd[0] == '\0') +// cmd_error("", data); +// if (cmd[0] == ' ') +// cmd_error(cmd, data); +// if (0 == access(cmd, F_OK) && 0 != access(cmd, X_OK)) +// permission_error(cmd, 126, data); +// if (0 == access(cmd, F_OK | X_OK)) +// absolute_cmd(data, cmd); +// else +// { +// if (0 != find_path(data, envp)) +// path_error(cmd, data); +// if (0 != find_cmd(data, cmd)) +// cmd_error(cmd, data); +// } +// } + diff --git a/src/exec/combine_cmd.c b/src/exec/combine_cmd.c new file mode 100644 index 0000000..6aa1dfa --- /dev/null +++ b/src/exec/combine_cmd.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* combine_cmd.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/09/01 18:02:02 by mdekker/jde #+# #+# */ +/* Updated: 2023/09/01 22:29:30 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +char **combine_cmd(t_group *group) +{ + char **cmd; + size_t count; + + count = cmd_count(&group->input, false); + if (count == 0) + return (NULL); + cmd = malloc(sizeof(char *) * count + 1); + if (!cmd) + err(MALLOC, "create_cmd", NULL, NULL); + cmd[count] = NULL; + cmd = assign_tokens(cmd, cmd_count(&group->input, true), &group->input); + return (cmd); +} + +/** + * @brief assigns the tokens to the cmd; +*/ +char **assign_tokens(char **cmd, size_t start, t_vector *input) +{ + t_token *token; + size_t i; + + i = 0; + while (start < input->length && !is_redirect(token)) + { + token = vec_get(input, start); + if (i == 0) + cmd[i] = ft_strdup(rm_quotes(token->value)); + else + cmd[i] = ft_strdup(token->value); + if (!cmd[i]) + err(MALLOC, "create_cmd", NULL, NULL); + start++; + i++; + } +} + +/** + * @brief counts the amount of tokens in the vector skipping redirects + * @param start if true it will return the index after the starting redirects + * @param start if false it will return the amount of non-redirect tokens +*/ +static size_t cmd_count(t_vector *input, bool start) +{ + char **cmd; + t_token *token; + size_t i; + size_t count; + + i = 0; + count = 0; + token = (t_token *)vec_get(input, i); + while (i < input->length && is_redirect(token)) + { + i++; + if (i < input->length) + token = (t_token *)vec_get(input, i); + } + if (start == true) + return (i); + while (i < input->length && !is_rediect(token)) + { + count++; + i++; + token = (t_token *)vec_get(input, i); + } + return (count); +} diff --git a/src/exec/create_processes.c b/src/exec/create_processes.c index 3fd5600..9a4373e 100644 --- a/src/exec/create_processes.c +++ b/src/exec/create_processes.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/19 12:40:07 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/28 11:29:57 by mdekker ######## odam.nl */ +/* Updated: 2023/09/01 19:47:27 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -50,11 +50,11 @@ static bool fork_processes(t_exec *exec, size_t process_count) if (group->pd == 0) { if (i == 0) - exec_process(LEFT, group, exec->envp); + exec_process(LEFT, group); else if (i == process_count - 1) - exec_process(RIGHT, group, exec->envp); + exec_process(RIGHT, group); else - exec_process(MIDDLE, group, exec->envp); + exec_process(MIDDLE, group); } i++; } @@ -75,7 +75,7 @@ bool create_processes(t_exec *exec) group->pd = fork(); if (group->pd == -1) return (false); - exec_process(SINGLE, group, exec->envp); + exec_process(SINGLE, group); } else { diff --git a/src/exec/exec.c b/src/exec/exec.c index a5672dd..a5022c8 100644 --- a/src/exec/exec.c +++ b/src/exec/exec.c @@ -6,13 +6,13 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:50 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/31 00:37:39 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/01 16:53:31 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -bool close_pipes(t_vector *group_vec) +static bool close_pipes(t_vector *group_vec) { size_t i; t_group *group; @@ -62,7 +62,12 @@ static int wait_processes(t_vector *group_vec) return (status); } -bool executor(t_exec *exec) +/** + * @brief creates pipes and starts processes, closes pipes and waits for child_processes + * @return return value is the EXITSTATUS of the last childprocess to complete + * @note if all testing is succesful make it a void function and exit status instead of returning it +*/ +int executor(t_exec *exec) { int temp; int status; @@ -79,8 +84,8 @@ bool executor(t_exec *exec) err(PERROR, NULL, clear_exec, exec); } status = wait_processes(&exec->group_vec); - if (!status) + if (status == -1) err(PERROR, NULL, clear_exec, exec); clear_exec(exec); - exit(status); + return (status); } diff --git a/src/exec/exec_process.c b/src/exec/exec_process.c index 0675156..c53c5b8 100644 --- a/src/exec/exec_process.c +++ b/src/exec/exec_process.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/19 16:08:08 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/28 11:29:57 by mdekker ######## odam.nl */ +/* Updated: 2023/09/01 22:26:59 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,83 +14,88 @@ extern t_global g_data; -void exec_process(t_process type, t_group *group, char **envp) +void exec_process(t_group *group, t_process type) { size_t i; char *str; - t_token *token; + char **env; close_start(type, group); - token = vector_get(&group->input, 0); + group->cmd = combine_cmd(group); //@note for first command remove quotes + if (is_built_in(group->cmd[0])) + exec_built_in(group, type, &g_data.env); + check_cmd(group->cmd[0], &g_data.env); handle_redirects(group); - group->cmd = combine_tokens(&group->input); - if (!group->cmd) - err("malloc error", 1, NULL); - str = rm_quotes(token->value); - if (!str) - err("malloc error", 1, NULL); dup_fd(type, group); - built_in(str, group->cmd, &g_data.env); - check_cmd(str); - execve(str, group->cmd, envp); + exec_built_in(str, group->cmd, &g_data.env); + env = combine_env(&g_data.env); + execve(str, group->cmd, env); } + + static void close_start(t_process type, t_group *group) { if (type == LEFT) { if (close(group->right_pipe[0]) == -1) - err(PERROR, 1, NULL); + err(PERROR, 1, NULL, NULL); } else if (type == RIGHT) { if (close(group->left_pipe[1]) == -1) - err(PERROR, 1, NULL); + err(PERROR, 1, NULL, NULL); } else if (type == MIDDLE) { if (close(group->left_pipe[1]) == -1) - err(PERROR, 1, NULL); + err(PERROR, 1, NULL, NULL); if (close(group->right_pipe[0]) == -1) - err(PERROR, 1, NULL); + err(PERROR, 1, NULL, NULL); } } static void dup_fd(t_process type, t_group *group) { - if (type == LEFT) + if (type == SINGLE) + return ; + else if (type == LEFT) { if (dup2(group->right_pipe[1], STDOUT_FILENO) == -1) - err(PERROR, 1, NULL); + err(PERROR, 1, NULL, NULL); } else if (type == RIGHT) { if (dup2(group->left_pipe[0], STDIN_FILENO) == -1) - err(PERROR, 1, NULL); + err(PERROR, 1, NULL, NULL); } else if (type == MIDDLE) { if (dup2(group->left_pipe[0], STDIN_FILENO) == -1) - err(PERROR, 1, NULL); + err(PERROR, 1, NULL, NULL); if (dup2(group->right_pipe[1], STDOUT_FILENO) == -1) - err(PERROR, 1, NULL); + err(PERROR, 1, NULL, NULL); } } -static void built_in(char *str, char **cmd, t_vector *env) +static void exec_built_in(t_group *group, t_process type, t_vector *env) { - if (ft_strcmp(str, "echo") == 0) - ft_echo(str, cmd, env); - else if (ft_strcmp(str, "cd") == 0) - ft_cd(str, cmd, env); - else if (ft_strcmp(str, "pwd") == 0) - ft_pwd(str, cmd, env); - else if (ft_strcmp(str, "export") == 0) - ft_export(str, cmd, env); - else if (ft_strcmp(str, "unset") == 0) - ft_unset(str, cmd, env); - else if (ft_strcmp(str, "env") == 0) - ft_env(str, cmd, env); - else if (ft_strcmp(str, "exit") == 0) - ft_exit(str, cmd, env); + handle_redirects(group); + dup_fd(type, group); + if (ft_strcmp(group->cmd[0], "exit") == 0) + ft_exec_exit(group->cmd, type); //@note create built in for exit + if (ft_strcmp(group->cmd[0], "echo") == 0) + ft_echo(group->cmd, env); + else if (ft_strcmp(group->cmd[0], "cd") == 0) + ft_cd(group->cmd[0], group->cmd, env); + else if (ft_strcmp(group->cmd[0], "pwd") == 0) + ft_pwd(group->cmd, env); + else if (ft_strcmp(group->cmd[0], "export") == 0) + ft_export(group->cmd, env); + else if (ft_strcmp(group->cmd[0], "unset") == 0) + ft_unset(group->cmd, env); + else if (ft_strcmp(group->cmd[0], "env") == 0) + ft_env(group->cmd, env); + else if (ft_strcmp(group->cmd[0], "exit") == 0) + ft_exit(group->cmd, env); } diff --git a/src/exec/find_cmd.c b/src/exec/find_cmd.c deleted file mode 100644 index e69de29..0000000 diff --git a/src/exec/group.c b/src/exec/group.c index d259279..d06597b 100644 --- a/src/exec/group.c +++ b/src/exec/group.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/31 00:56:33 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/01 20:49:26 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,15 +15,21 @@ static void push_hdoc(char *filename, t_group *group, t_exec *exec) { char *fname; - t_token *token; + t_token *red_token; + t_token *fname_token; fname = ft_strdup(filename); if (!fname) err(MALLOC, "push_hdoc", clear_exec, exec); - token = create_token(filename, HEREDOC); - if (!token) + fname_token = create_token(filename, STRING); + if (!fname_token) + err(MALLOC, "push_hdoc", clear_exec, exec); + red_token = create_token("<", I_REDIRECT); + if (!red_token) + err(MALLOC, "push_hdoc", clear_exec, exec); + if (!vec_push(&group->input, (void *)red_token)) err(MALLOC, "push_hdoc", clear_exec, exec); - if (!vec_push(&group->input, (void *)token)) + if (!vec_push(&group->input, (void *)fname_token)) err(MALLOC, "push_hdoc", clear_exec, exec); if (!vec_push(&exec->fname_vec, (void *)fname)) err(MALLOC, "push_hdoc", clear_exec, exec); @@ -49,13 +55,13 @@ static void hdoc_found(t_vector token_vec, t_group *group, int *i, t_exec *exec) token = vec_get(&token_vec, (*i)); if ((token->type == SINGLE_QUOTE || token->type == DOUBLE_QUOTE) && ft_strlen(token->value) == 2) - heredoc(filename, "", token->type); + heredoc(filename, "", token->type, exec); else { stop = rm_quotes(token); if (!stop) err(MALLOC, "hdoc_found", clear_exec, exec); - heredoc(filename, stop, token->type); + heredoc(filename, stop, token->type, exec); free(stop); } push_hdoc(filename, group, exec); @@ -113,6 +119,5 @@ t_exec *group_tokens(t_vector *token_vec, char **envp) vec_push(&exec->group_vec, group); i++; } - exec->envp = envp; return (exec); } diff --git a/src/exec/heredoc.c b/src/exec/heredoc.c index 278f926..b596266 100644 --- a/src/exec/heredoc.c +++ b/src/exec/heredoc.c @@ -6,54 +6,17 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/16 12:15:45 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/18 18:33:27 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/01 17:24:09 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -/* -Expansion na heredoc -bash-3.2$ cat << $USER -> hello -> juliusdebaaij -> $USER -hello -juliusdebaaij -bash-3.2$ cat $USER -cat: juliusdebaaij: No such file or directory - -*/ -/* - - -int ft_heredoc(char *filename, char *stop, t_type quotes) - - -if t_type quotes == NULL - -Create newfile with open(); 0_CREATE - -use readline --> interpet based on quotes -write(heredoc_fd) -close(herredoc_fd); -remove the file? --> or remove later in child_process; - -*/ - -/** - * -*/ -bool heredoc(char *filename, char *stop, t_type type) +static void loop(int heredoc_fd, char *stop, t_types type, t_exec *exec) { - int heredoc_fd; - char *filename_dup; char *line; - heredoc_fd = open(filename, O_CREAT | O_WRONLY, 0644); - if (heredoc_fd < 0) - return (false); while (1) { line = readline(">"); @@ -63,13 +26,33 @@ bool heredoc(char *filename, char *stop, t_type type) { line = expand_line(line); if (!line) - return (false); + { + free(line); + err(MALLOC, "hdoc_loop", clear_exec, exec); + } } write(heredoc_fd, line, ft_strlen(line)); write(heredoc_fd, "\n", 1); free(line); } + free(line); +} + +/** + * @param filename the filename to be given to the new file + * @param stop the string to stop readline + * @param type string or quoted, if quoted there will be no expansion. + * @param exec to be freed when an error occurs +*/ +void heredoc(char *filename, char *stop, t_types type, t_exec *exec) +{ + int heredoc_fd; + + heredoc_fd = open(filename, O_CREAT | O_WRONLY, 0644); + if (heredoc_fd == -1) + err(PERROR, filename, clear_exec, exec); + if (heredoc_fd < 0) + return (false); close(heredoc_fd); - filename_dup = ft_strdup(filename); return (true); } diff --git a/src/exec/utils.c b/src/exec/utils.c new file mode 100644 index 0000000..f867492 --- /dev/null +++ b/src/exec/utils.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* utils.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/09/01 19:44:05 by mdekker/jde #+# #+# */ +/* Updated: 2023/09/01 21:37:27 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +bool is_built_in(char *str) +{ + if (ft_strcmp(str, "exit") == 0) //@note or any of the other strings + return (true); +} + +char **combine_env(t_vector *env_vec) +{ + +} + + + +bool is_redirect(t_token *token) +{ + if (token->type == O_REDIRECT || token->type == I_REDIRECT + || token->type == A_REDIRECT || token->type == HEREDOC) + { + return (true); + } + return (false); +} diff --git a/src/main.c b/src/main.c index 2661849..372cbf9 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/30 21:08:41 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/01 21:07:36 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -101,10 +101,12 @@ int main(int ac, char **av, char **env) printf("Parsed!\n"); if (!operator_split(&g_data.tokens)) mini_exit("operator split failed", 1); - // verify token order -> no pipes start/end, - // no double pipes / redirects + + verify_tokens(&g_data.tokens); exec = group_tokens(&g_data.tokens, env); - exit(executor(exec)); + status = executor(exec); + printf("succes, status=%i\n", status); + exit(status); } if (waitpid(pid, &status, 0) == -1) exit_mini("waitpid failed", errno); @@ -130,7 +132,7 @@ int main(int ac, char **av, char **env) // if (DEBUG) print_vector(&g_data.tokens, print_token); // print_vector(&g_data.env, print_env); - free_global(false); + free_global(true); return (0); } else if (ac == 1) diff --git a/src/parser/verify_token.c b/src/parser/verify_token.c index a873f71..3b1fc33 100644 --- a/src/parser/verify_token.c +++ b/src/parser/verify_token.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/02 16:57:32 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/31 00:43:21 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/01 15:32:01 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -64,7 +64,7 @@ static void check_redirect(t_vector *token_vec, int i) * @brief checks if redirects/heredocs are followe by a string/quoted string * @param vec the vector of t_tokens */ -void verify_token(t_vector *token_vec) +void verify_tokens(t_vector *token_vec) { int i; t_token *token; diff --git a/src/structs/group.c b/src/structs/group.c index f813d1f..a52381f 100644 --- a/src/structs/group.c +++ b/src/structs/group.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/02 20:42:59 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/28 11:29:57 by mdekker ######## odam.nl */ +/* Updated: 2023/09/01 21:52:28 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -25,6 +25,7 @@ void clear_group(void *data) vec_free(&p->input); if (p->cmd) ft_free(p->cmd); + p = NULL; } void clear_fname(void *data) @@ -37,6 +38,7 @@ void clear_fname(void *data) if (-1 == unlink(filename)) perror("minishell:"); free(filename); + filename = NULL; } void clear_exec(t_exec *exec) @@ -46,6 +48,7 @@ void clear_exec(t_exec *exec) vec_free(&exec->group_vec); vec_free(&exec->fname_vec); free(exec); + exec = NULL; } /** diff --git a/src/utils/error.c b/src/utils/error.c index 398b833..6ddf057 100644 --- a/src/utils/error.c +++ b/src/utils/error.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 21:42:24 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/30 23:18:33 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/01 20:12:37 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -112,5 +112,6 @@ void err(t_exit type, char *name, void (*func)(void *), void *data) write(STDERR_FILENO, "^C\n", 3); status = 130; } + free_global(true); exit(status); } diff --git a/src/utils/miscellaneous.c b/src/utils/miscellaneous.c index 41992d5..6428080 100644 --- a/src/utils/miscellaneous.c +++ b/src/utils/miscellaneous.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/19 16:53:28 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/19 16:53:41 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/01 17:52:14 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,7 +17,15 @@ char *rm_quotes(t_token *token) if (token->type == STRING) return (token->value); if (token->type == SINGLE_QUOTE) + { + if (strlen(token->value) == 2) + return (""); return (ft_strtrim(token->value, "\'")); + } if (token->type == DOUBLE_QUOTE) + { + if (strlen(token->value) == 2) + return (""); return (ft_strtrim(token->value, "\"")); + } } From 0a1dad092b2612deba58f3036b373b55d80f5480 Mon Sep 17 00:00:00 2001 From: Julius De Baaij Date: Sun, 3 Sep 2023 20:24:22 +0200 Subject: [PATCH 85/91] replacing global wiht t_shell data, passing data everywhere so it can be freed on exit) --- .vscode/settings.json | 3 +- Makefile | 2 +- includes/enum.h | 4 +- includes/minishell.h | 25 ++--- includes/structs.h | 20 ++-- src/exec/combine_cmd.c | 12 +-- src/exec/group.c | 12 +-- src/exec/redirect.c | 4 +- src/exec/utils.c | 13 +-- src/lexer/index.c | 27 ++--- src/lexer/op_split.c | 100 +++++++++++++++++ src/lexer/token.c | 150 ++++---------------------- src/main.c | 42 ++++---- src/parser/index.c | 20 ++-- src/parser/tokens.c | 2 +- src/parser/tokens2.c | 2 +- src/parser/verify_token.c | 54 +++++----- src/{utils/init.c => structs/shell.c} | 72 +++++++++---- src/utils/error.c | 17 ++- src/utils/global.c | 30 ------ src/utils/miscellaneous.c | 34 +++++- 21 files changed, 323 insertions(+), 322 deletions(-) create mode 100644 src/lexer/op_split.c rename src/{utils/init.c => structs/shell.c} (50%) delete mode 100644 src/utils/global.c diff --git a/.vscode/settings.json b/.vscode/settings.json index abd26e5..5c1f543 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -20,6 +20,7 @@ "iterator": "c", "string": "c", "string_view": "c", - "vector": "c" + "vector": "c", + "minishell.h": "c" } } \ No newline at end of file diff --git a/Makefile b/Makefile index b644baa..9ffa8ca 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = minishell -SRC = main check_input utils/error structs/token structs/group parser/index parser/tokens2 parser/quotes parser/tokens debug/print_vector lexer/index lexer/string lexer/token utils/init structs/env utils/global exec/group exec/heredoc parser/verify_token exec/exec exec/create_processes exec/redirect exec/exec_process utils/miscellaneous +SRC = main check_input utils/error structs/token structs/group parser/index parser/token_vec2 parser/quotes parser/token_vec debug/print_vector lexer/index lexer/string lexer/token lexer/op_split structs/env structs/shell exec/group exec/heredoc parser/verify_token exec/exec exec/create_processes exec/redirect exec/exec_process utils/miscellaneous SRCS = $(addsuffix .c, $(addprefix src/, $(SRC))) OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a diff --git a/includes/enum.h b/includes/enum.h index d4adc1a..21dfb1e 100644 --- a/includes/enum.h +++ b/includes/enum.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/13 17:41:00 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/19 17:23:57 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/03 17:12:24 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ # define ENUM_H /** - * @brief The enum for the different types of tokens + * @brief The enum for the different types of token_vec * * @param DOUBLE_QUOTE (") * @param SINGLE_QUOTE (') diff --git a/includes/minishell.h b/includes/minishell.h index 1085dff..4e0b70e 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/09/01 19:56:19 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/03 20:21:55 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -28,15 +28,18 @@ # define DEBUG 0 # endif -bool init(char **envp); +/* init shell */ +t_shell *init_shell(char **env, bool first_init); +void free_shell(t_shell *data, bool close_shell); /* input_check */ -bool lexer(char *input, t_vector *vec); +void lexer(char *input, t_shell *data); bool check_quotes_parantheses(char *input); bool create_string(char *str, size_t *i, t_vector *vec); bool create_quote_string(char *str, size_t *i, t_vector *vec); bool create_paran_string(char *str, size_t *i, t_vector *vec); -bool operator_split(t_vector *vec); +void operator_split(t_shell *data); +char **split(t_token *token); /* structs */ t_token *create_token(char *value, t_types type); @@ -52,11 +55,11 @@ void clear_exec(t_exec *exec); /* utils */ void exit_mini(char *str, int exit_code); -void err(t_exit type, char *name, void (*func)(void *), void *data); +void err(t_exit type, char *name, t_shell *data, bool free_struct); char *rm_quotes(t_token *token); /* parser */ -void parser(t_vector *vec); +void parser(t_shell *data); void parse_one(t_token *token); bool is_encased_dq(char *str); bool is_encased_sq(char *str); @@ -69,12 +72,11 @@ bool is_heredoc(char *str); bool contains_env_var(char *str); bool is_or(char *str); bool is_and(char *str); - -void verify_tokens(t_vector *token_vec); +void verify_token_vec(t_shell *data); /* executor */ -t_exec *group_tokens(t_vector *token_vec, char **envp); +t_exec *group_token_vec(t_vector *token_vec, char **envp); void heredoc(char *filename, char *stop, t_types type, t_exec *exec); int executor(t_exec *exec); @@ -86,6 +88,8 @@ void redirect_input(t_group *group, size_t i); bool is_built_in(char *str); char **combine_env(t_vector *env_vec); char **create_cmd(t_vector *input); +bool is_redirect(t_token *token); +bool is_string_type(t_token *token); /* debug */ @@ -94,7 +98,4 @@ void print_token(void *data, size_t i); char *print_type(t_types type); void print_env(void *data, size_t i); -/* global */ -void free_global(bool exit); - #endif diff --git a/includes/structs.h b/includes/structs.h index 73bb11c..20dd2e5 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/09/01 20:35:31 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/03 18:53:43 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ # include "enum.h" /** - * @brief The struct for the tokens + * @brief The struct for the token_vec * * @param type The type of token * @param value The value of the token @@ -69,7 +69,7 @@ typedef struct s_exec /** * @brief a group to be individually executed. * - * @param input a vector of t_tokens belonging to 1 child_process + * @param input a vector of t_token_vec belonging to 1 child_process @param cmd an array containing the commands and parameters to be executed @param hdoc_vec vector with heredoc filedescriptors @param pd the pd of this specific process @@ -89,16 +89,18 @@ typedef struct s_group /** * @brief The global struct * - * @param tokens The tokens vector from the lexer and parser - * @param env The environment variables + * @param token_vec The token_vec vector from the lexer and parser; + * @param env The environment variables; + * @param data to be passed to the executor; */ -typedef struct s_global +typedef struct s_shell { - t_vector tokens; + t_vector token_vec; t_vector env; - t_signal signal; + t_exec *exec; int exit_status; -} t_global; +} t_shell; + /** * @brief The struct for the parser functions. diff --git a/src/exec/combine_cmd.c b/src/exec/combine_cmd.c index 6aa1dfa..2b7243b 100644 --- a/src/exec/combine_cmd.c +++ b/src/exec/combine_cmd.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/09/01 18:02:02 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/01 22:29:30 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/03 17:12:24 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -24,14 +24,14 @@ char **combine_cmd(t_group *group) if (!cmd) err(MALLOC, "create_cmd", NULL, NULL); cmd[count] = NULL; - cmd = assign_tokens(cmd, cmd_count(&group->input, true), &group->input); + cmd = assign_token_vec(cmd, cmd_count(&group->input, true), &group->input); return (cmd); } /** - * @brief assigns the tokens to the cmd; + * @brief assigns the token_vec to the cmd; */ -char **assign_tokens(char **cmd, size_t start, t_vector *input) +char **assign_token_vec(char **cmd, size_t start, t_vector *input) { t_token *token; size_t i; @@ -52,9 +52,9 @@ char **assign_tokens(char **cmd, size_t start, t_vector *input) } /** - * @brief counts the amount of tokens in the vector skipping redirects + * @brief counts the amount of token_vec in the vector skipping redirects * @param start if true it will return the index after the starting redirects - * @param start if false it will return the amount of non-redirect tokens + * @param start if false it will return the amount of non-redirect token_vec */ static size_t cmd_count(t_vector *input, bool start) { diff --git a/src/exec/group.c b/src/exec/group.c index d06597b..e2347b3 100644 --- a/src/exec/group.c +++ b/src/exec/group.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/01 20:49:26 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/03 17:12:24 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -38,7 +38,7 @@ static void push_hdoc(char *filename, t_group *group, t_exec *exec) /** * @brief Creates a token for the hdoc file, and adds the filename to the f_name * @param token_vec the token vector - * @param group the group to add the new tokens to + * @param group the group to add the new token_vec to * @param i to step over the heredoc and the quotes / filenames * @param */ @@ -98,12 +98,12 @@ t_group *make_group(t_vector *token_vec, int *i, t_exec *exec) } /** - * @brief Group the tokens into groups + * @brief Group the token_vec into groups * - * @param token_vec The vector containing the tokens + * @param token_vec The vector containing the token_vec * @return t_exec* The struct containing the groups */ -t_exec *group_tokens(t_vector *token_vec, char **envp) +t_exec *group_token_vec(t_vector *token_vec, char **envp) { size_t i; t_exec *exec; @@ -111,7 +111,7 @@ t_exec *group_tokens(t_vector *token_vec, char **envp) exec = create_exec(); if (!exec) - err(MALLOC, "group_tokens", NULL, NULL); + err(MALLOC, "group_token_vec", NULL, NULL); i = 0; while (i < token_vec->length) { diff --git a/src/exec/redirect.c b/src/exec/redirect.c index b32f5c9..c11fd71 100644 --- a/src/exec/redirect.c +++ b/src/exec/redirect.c @@ -6,12 +6,12 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/19 16:32:48 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/28 11:29:57 by mdekker ######## odam.nl */ +/* Updated: 2023/09/03 17:12:24 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ /* - redirects need to remove the tokens related to the redirect + redirects need to remove the token_vec related to the redirect */ diff --git a/src/exec/utils.c b/src/exec/utils.c index f867492..0a328e1 100644 --- a/src/exec/utils.c +++ b/src/exec/utils.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/09/01 19:44:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/01 21:37:27 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/03 20:16:02 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,14 +23,3 @@ char **combine_env(t_vector *env_vec) } - - -bool is_redirect(t_token *token) -{ - if (token->type == O_REDIRECT || token->type == I_REDIRECT - || token->type == A_REDIRECT || token->type == HEREDOC) - { - return (true); - } - return (false); -} diff --git a/src/lexer/index.c b/src/lexer/index.c index 47b6688..b81f357 100644 --- a/src/lexer/index.c +++ b/src/lexer/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 13:32:55 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/30 22:02:44 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/03 19:25:37 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -118,16 +118,12 @@ bool make_string(char *str, size_t *i, t_vector *vec) } /** - * @brief Splits a string into tokens + * @brief Splits a string into token_vec * * @param input The string to split - * @param vec The vector to store the tokens in - * @return true The string was succesfully split - * @return false The string could not be split - * - * @note The vector must be initialized before calling this function + * @param data t_shell data, of which token_vec will be used */ -bool lexer(char *input, t_vector *vec) +void lexer(char *input, t_shell *data) { size_t i; @@ -137,20 +133,19 @@ bool lexer(char *input, t_vector *vec) if (checkchar(input[i], "\"\'()") == 1) { if (!check_delimiters(&input[i])) - return (err(SYNTAX, "bad quote(s) or parantheses", NULL, NULL), false); - if (!make_string(input, &i, vec)) - return (err(MALLOC, "malloc", NULL, NULL), false); + err(SYNTAX_MINI, input[i], data, false); + if (!make_string(input, &i, &data->token_vec)) + err(MALLOC, "malloc", data, false); } else if (input[i] == ' ') { - if (!create_string(input, &i, vec)) - return (err(MALLOC, "malloc", NULL, NULL), false); + if (!create_string(input, &i, &data->token_vec)) + err(MALLOC, "malloc", data, false); } else i++; } if (i > 0 && checkchar(input[i - 1], "\"\') ") == 0) - if (!create_string(input, &i, vec)) - return (err(MALLOC, "malloc", NULL, NULL), false); - return (true); + if (!create_string(input, &i, &data->token_vec)) + err(MALLOC, "malloc", data, false); } diff --git a/src/lexer/op_split.c b/src/lexer/op_split.c new file mode 100644 index 0000000..a6c9bea --- /dev/null +++ b/src/lexer/op_split.c @@ -0,0 +1,100 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* op_split.c :+: :+: */ +/* +:+ */ +/* By: mdekker/jde-baai +#+ */ +/* +#+ */ +/* Created: 2023/09/03 19:55:34 by mdekker/jde #+# #+# */ +/* Updated: 2023/09/03 20:00:10 by mdekker/jde ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +static size_t split_size(char *str) +{ + size_t split; + size_t i; + + split = 0; + i = 0; + while (str[i]) + { + if (checkchar(str[i], "<>|&")) + { + split++; + if (i > 0 && str[i - 1] && !checkchar(str[i - 1], "<>|&")) + split++; + if (str[i] == str[i + 1] && str[i] != '&') + i++; + } + i++; + } + return (split); +} + +static bool split_left(char *str, char **array, size_t i, size_t *split) +{ + size_t left; + + left = i; + if (left > 0 && !checkchar(str[left - 1], "<>|&")) + { + left--; + while (left > 0 && !checkchar(str[left], "<>|&")) + left--; + if (checkchar(str[left], "<>|&")) + left++; + array[(*split)] = ft_substr(str, left, i - left); + if (!array[(*split)]) + return (false); + (*split)++; + } + return (true); +} + +static bool split_string(char *str, char **array, size_t *i, size_t *split) +{ + size_t len; + + if (!split_left(str, array, (*i), split)) + return (false); + len = 1; + if (str[(*i)] == str[(*i) + 1] && str[(*i)] != '&') + len++; + array[(*split)] = ft_substr(str, (*i), len); + if (!array[(*split)]) + return (false); + (*split)++; + (*i) += len; + return (true); +} + +char **split(t_token *token) +{ + size_t i; + size_t split; + char **array; + + split = split_size(token->value) + 1; + array = malloc(sizeof(char *) * (split + 1)); + if (!array) + return (NULL); + array[split] = NULL; + split = 0; + i = 0; + while (token->value[i]) + { + if (checkchar(token->value[i], "<>|&")) + { + if (!split_string(token->value, array, &i, &split)) + return (ft_free(array), NULL); + } + else + i++; + } + if (!split_left(token->value, array, i, &split)) + return (ft_free(array), NULL); + return (array); +} diff --git a/src/lexer/token.c b/src/lexer/token.c index ecabde1..8c038ab 100644 --- a/src/lexer/token.c +++ b/src/lexer/token.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/21 13:06:18 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/30 22:03:16 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/03 20:06:17 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -24,130 +24,25 @@ static bool find_operator(t_token *token) return (true); } -/* -static bool operator_err(char *str) +static void insert_array(t_shell *data, char **array, int i) { - // & -> fail - // aan het einde | of || of && -> vec->index == i, "unfinished sign" - // aan het begin | of || of && -> vec->index == i, - "syntax error near unexpected token" - // < > vec->index == i, "syntax error near unexpected token `newline'" - // << >> vec->index == i, syntax error near unexpected token `newline' - if (str[0] == '&' && str[1] != '&') - err("nice try, out of scope", "`&'", 2); - else if (checkchar(str[0], "<>")) - err("syntax error near unexpected token `newline'", str, 2); - else if (str[0] == '|' && str[1] != '||') - err("syntax error near unexpected token", "`|'", 2); - else if (str[0] == '|' && str[1] == '||') - err("syntax error near unexpected token", "`|'", 2); - else if (Str[0] == ) - return (false); -} -*/ - -static size_t split_size(char *str) -{ - size_t split; - size_t i; - - split = 0; - i = 0; - while (str[i]) - { - if (checkchar(str[i], "<>|&")) - { - split++; - if (i > 0 && str[i - 1] && !checkchar(str[i - 1], "<>|&")) - split++; - if (str[i] == str[i + 1] && str[i] != '&') - i++; - } - i++; - } - return (split); -} - -static bool split_left(char *str, char **array, size_t i, size_t *split) -{ - size_t left; - - left = i; - if (left > 0 && !checkchar(str[left - 1], "<>|&")) - { - left--; - while (left > 0 && !checkchar(str[left], "<>|&")) - left--; - if (checkchar(str[left], "<>|&")) - left++; - array[(*split)] = ft_substr(str, left, i - left); - if (!array[(*split)]) - return (false); - (*split)++; - } - return (true); -} - -static bool split_string(char *str, char **array, size_t *i, size_t *split) -{ - size_t len; - - if (!split_left(str, array, (*i), split)) - return (false); - len = 1; - if (str[(*i)] == str[(*i) + 1] && str[(*i)] != '&') - len++; - array[(*split)] = ft_substr(str, (*i), len); - if (!array[(*split)]) - return (false); - (*split)++; - (*i) += len; - return (true); -} - -/** - * - * -*/ -static char **split(t_token *token) -{ - size_t i; - size_t split; - char **array; + int j; + t_token *token; - split = split_size(token->value) + 1; - array = malloc(sizeof(char *) * (split + 1)); - if (!array) - return (NULL); - array[split] = NULL; - split = 0; - i = 0; - while (token->value[i]) + j = 0; + while (array[j]) { - if (checkchar(token->value[i], "<>|&")) + token = create_token(array[j], UNKNOWN); + if (!token || !vec_insert(&data->token_vec, i + j, token)) { - if (!split_string(token->value, array, &i, &split)) - return (ft_free(array), NULL); + ft_free(array); + err(MALLOC, "malloc", data, false); } - else - i++; + j++; } - if (!split_left(token->value, array, i, &split)) - return (ft_free(array), NULL); - return (array); -} - -size_t array_length(char **array) -{ - size_t i; - - i = 0; - while (array[i]) - i++; - return (i); } -bool operator_split(t_vector *vec) +void operator_split(t_shell *data) { size_t i; size_t j; @@ -155,28 +50,23 @@ bool operator_split(t_vector *vec) char **array; i = 0; - while (i < vec->length) + while (i < (&data->token_vec)->length) { - token = (t_token *)vec_get(vec, i); + token = (t_token *)vec_get(&data->token_vec, i); if (token->type == STRING && find_operator(token)) { array = split(token); - if (!array || !vec_remove(vec, i)) - return (err(MALLOC, "malloc", NULL, NULL), false); - j = 0; - while (array[j]) + if (!array || !vec_remove(&data->token_vec, i)) { - token = create_token(array[j], UNKNOWN); - if (!token || !vec_insert(vec, i + j, token)) - return (err(MALLOC, "malloc", NULL, NULL), false); - j++; + ft_free(array); + err(MALLOC, "malloc", data, false); } + insert_array(data, array, i); } else i++; } - parser(vec); - if (!array) + parser(&data->token_vec); + if (array) free(array); - return (true); } diff --git a/src/main.c b/src/main.c index 372cbf9..6cb3df9 100644 --- a/src/main.c +++ b/src/main.c @@ -6,13 +6,13 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/01 21:07:36 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/03 20:06:47 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -t_global g_data; +t_signal signal; static void check_leaks(void) { @@ -37,7 +37,7 @@ static void debug(void) /** * @brief The main loop of the program * - * @param vec The vector to store the tokens in + * @param vec The vector to store the token_vec in */ static void loop(t_vector *vec) { @@ -79,15 +79,14 @@ static void loop(t_vector *vec) int main(int ac, char **av, char **env) { - t_exec *exec; + t_shell *data; pid_t pid; int status; // t_found **found; if (DEBUG) debug(); - if (!init(env)) - return (1); + data = init_shell(env, true); if (ac == 2) { pid = fork(); @@ -95,26 +94,25 @@ int main(int ac, char **av, char **env) exit_mini("initial fork failed", errno); if (pid == 0) { - if (!lexer(av[1], &g_data.tokens)) - mini_exit("lexer failed", 1); - parser(&g_data.tokens); - printf("Parsed!\n"); - if (!operator_split(&g_data.tokens)) - mini_exit("operator split failed", 1); - - verify_tokens(&g_data.tokens); - exec = group_tokens(&g_data.tokens, env); - status = executor(exec); - printf("succes, status=%i\n", status); + lexer(av[1], data); + //lexer retester + parser(data); + //parser retesting + operator_split(data); + // operator split retesting + verify_token_vec(&data->token_vec); + // check if it really catches all doubles + data->exec = group_token_vec(&data->token_vec, env); + status = executor(data->exec); exit(status); } if (waitpid(pid, &status, 0) == -1) exit_mini("waitpid failed", errno); - g_data.exit_status = WEXITSTATUS(status); - // found = g_data.tokens.find(&g_data.tokens, find_strings); + data->exit_status = WEXITSTATUS(status); + // found = g_data.token_vec.find(&g_data.token_vec, find_strings); // if (!found) // { - // printf("%zu counted\n", g_data.tokens.count(&g_data.tokens, + // printf("%zu counted\n", g_data.token_vec.count(&g_data.token_vec, // find_strings)); // printf("No matches found\n"); // } @@ -130,13 +128,13 @@ int main(int ac, char **av, char **env) // free(found); // } // if (DEBUG) - print_vector(&g_data.tokens, print_token); + print_vector(&g_data.token_vec, print_token); // print_vector(&g_data.env, print_env); free_global(true); return (0); } else if (ac == 1) - loop(&g_data.tokens); + loop(&g_data.token_vec); else { printf("Too many arguments"); diff --git a/src/parser/index.c b/src/parser/index.c index b459911..7e54930 100644 --- a/src/parser/index.c +++ b/src/parser/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 11:12:20 by mdekker #+# #+# */ -/* Updated: 2023/08/28 11:33:16 by mdekker ######## odam.nl */ +/* Updated: 2023/09/03 19:30:07 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -41,9 +41,9 @@ t_func_map *return_map(void) } /** - * @brief Loops over the tokens in the vector and parses them + * @brief Loops over the token_vec in the vector and parses them * - * @param vec The vector containing the tokens + * @param vec The vector containing the token_vec * @param func_map The array of structs with function pointers and types */ void parse_loop(t_vector *vec, t_func_map *func_map) @@ -74,20 +74,20 @@ void parse_loop(t_vector *vec, t_func_map *func_map) } /** - * @brief Parses the tokens in the vector + * @brief Parses the token_vec in the vector * - * @note The parser will only parse tokens that have the type 0 so make sure - * to set the type of the tokens to 0 before calling this function - * @param vec The vector containing the tokens + * @note The parser will only parse token_vec that have the type 0 so make sure + * to set the type of the token_vec to 0 before calling this function + * @param vec The vector containing the token_vec */ -void parser(t_vector *vec) +void parser(t_shell *data) { t_func_map *func_map; func_map = return_map(); if (func_map == NULL) - return (err("Malloc failed", "parser", 1)); - parse_loop(vec, func_map); + return (err(MALLOC, "parser", data, false)); + parse_loop(&data->token_vec, func_map); free(func_map); } diff --git a/src/parser/tokens.c b/src/parser/tokens.c index 2a33a30..9f241e3 100644 --- a/src/parser/tokens.c +++ b/src/parser/tokens.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* tokens.c :+: :+: */ +/* token_vec.c :+: :+: */ /* +:+ */ /* By: mdekker/jde-baai +#+ */ /* +#+ */ diff --git a/src/parser/tokens2.c b/src/parser/tokens2.c index 5f83046..9667f03 100644 --- a/src/parser/tokens2.c +++ b/src/parser/tokens2.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* tokens2.c :+: :+: */ +/* token_vec2.c :+: :+: */ /* +:+ */ /* By: mdekker/jde-baai +#+ */ /* +#+ */ diff --git a/src/parser/verify_token.c b/src/parser/verify_token.c index 3b1fc33..7f76ea6 100644 --- a/src/parser/verify_token.c +++ b/src/parser/verify_token.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/02 16:57:32 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/01 15:32:01 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/03 20:23:18 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,27 +15,26 @@ /** * @brief loop cause it didnt fit in verify_token xd */ -static void loop(t_vector *token_vec) +static void loop(t_shell *data) { int i; t_token *token; t_token *next; i = 0; - while (i < token_vec->length) + while (i < (&data->token_vec)->length) { - token = vec_get(&token_vec, i); - if (token->type == O_REDIRECT || token->type == I_REDIRECT - || token->type == A_REDIRECT || token->type == HEREDOC) + token = vec_get(&data->token_vec, i); + if (is_redirect(token)) { - check_redirect(token_vec, i); + check_redirect(&data->token_vec, i); i++; } - else if (i + 1 < token_vec->length) + else if (i + 1 < (&data->token_vec)->length) { - next = vec_get(&token_vec, i + 1); - if (token->type == PIPE && next->type == PIPE) - err(SYNTAX, next->value, NULL, NULL); + next = vec_get(&data->token_vec, i + 1); + if (token->type == next->type) + err(SYNTAX, next->value, data, false); } i++; } @@ -44,41 +43,38 @@ static void loop(t_vector *token_vec) /** * @brief checks if redirect is followed by STRING/D_QUOTE/S_QUOTE */ -static void check_redirect(t_vector *token_vec, int i) +static void check_redirect(t_shell *data, int i) { - t_token *token; t_token *next; - token = vec_get(&token_vec, i); - if (i + 1 >= token_vec->length) - err(SYNTAX, "`newline'", NULL, NULL); - next = vec_get(&token_vec, i + 1); - if (next->type != STRING || next->type != DOUBLE_QUOTE - || next->type != SINGLE_QUOTE) - err(SYNTAX, next->value, NULL, NULL); + if (i + 1 >= (&data->token_vec)->length) + err(SYNTAX, "newline", data, false); + next = vec_get(&data->token_vec, i + 1); + if (!is_string_type(next)) + err(SYNTAX, next->value, data, false); } /** * @brief verifies that the input doesnt start with a pipe, checks for double pipes * @brief checks if redirects/heredocs are followe by a string/quoted string - * @param vec the vector of t_tokens + * @param vec the vector of t_token_vec */ -void verify_tokens(t_vector *token_vec) +void verify_token_vec(t_shell *data) { int i; t_token *token; i = 0; - token = vec_get(&token_vec, i); + token = vec_get(&data->token_vec, i); if (token->type == PIPE) - err(SYNTAX, token->value, NULL, NULL); - loop(token_vec); - if (token_vec->length > 1) + err(SYNTAX, token->value, data, false); + loop(data); + if ((&data->token_vec)->length > 1) { - i = token_vec->length - 1; - token = vec_get(&token_vec, i); + i = (&data->token_vec)->length - 1; + token = vec_get(&data->token_vec, i); if (token->type == PIPE) - err(SYNTAX_MINI, token->value, NULL, NULL); + err(SYNTAX_MINI, token->value, data, false); } } diff --git a/src/utils/init.c b/src/structs/shell.c similarity index 50% rename from src/utils/init.c rename to src/structs/shell.c index 989e8b4..d28dce4 100644 --- a/src/utils/init.c +++ b/src/structs/shell.c @@ -1,25 +1,44 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* init.c :+: :+: */ +/* shell.c :+: :+: */ /* +:+ */ /* By: mdekker/jde-baai +#+ */ /* +#+ */ -/* Created: 2023/07/20 20:09:52 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/28 11:34:44 by mdekker ######## odam.nl */ +/* Created: 2023/09/03 18:11:09 by mdekker/jde #+# #+# */ +/* Updated: 2023/09/03 18:59:20 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -extern t_global g_data; +extern t_signal signal; + + +/** + * @brief frees the main data struct + * @param data t_shell data + * @param close_shell true to free env and the struct + * false to keep data and env + * +*/ +void free_shell(t_shell *data, bool close_shell) +{ + vec_free(&data->token_vec); + clear_exec(data->exec); + if (close_shell) + { + vec_free(&data->env); + free(data); + } +} /** * @brief Initializes the enviroment variables * * @param env The enviroment variables */ -static void init_env(char **env) +static void init_env(char **env, t_vector *env_vec) { size_t i; char *key; @@ -32,7 +51,7 @@ static void init_env(char **env) value = ft_strdup(ft_strchr(env[i], '=') + 1); if (!key || !value) exit(1); // !TODO: error message - if (!vec_push(&g_data.env, create_env(key, value))) + if (!vec_push(env_vec, create_env(key, value))) exit(1); i++; } @@ -43,33 +62,44 @@ static void init_env(char **env) * * @return t_signal The created signal struct */ -static t_signal create_signal_struct(void) +void create_signal_struct(void) { - t_signal signal; - signal.inte = false; signal.quit = false; signal.pipe = false; - return (signal); } /** * @brief Initializes the global variables * + * @param bool if its the first init of minishell = true * @param env The enviroment variables * @return true When the initialization was succesfull * @return false When the initialization failed */ -bool init(char **env) +t_shell *init_shell(char **env, bool first_init) { - g_data.signal = create_signal_struct(); - if (!vec_init(&g_data.tokens, 3, sizeof(t_token), clear_token)) - return (false); - if (!vec_init(&g_data.env, 50, sizeof(t_env), clear_env)) - return (false); - g_data.exit_status = ft_strdup("0"); - if (!g_data.exit_status) - return (false); - init_env(env); - return (true); + t_shell *data; + + if (first_init) + { + data = malloc(sizeof(t_shell)); + if (!data) + exit_mini("failed to init data", 1); + if (!vec_init(&data->token_vec, 3, sizeof(t_token), clear_token)) + exit_mini("failed to vec_init token_vec", 1); + if (!vec_init(&data->env, 50, sizeof(t_env), clear_env)) + exit_mini("failed to vec_init env", 1); + init_env(env, &data->env); + data->exec = NULL; + data->exit_status = 0; + create_signal_struct(); + } + else + { + if (!vec_init(&data->token_vec, 3, sizeof(t_token), clear_token)) + exit_mini("failed to vec_init env", 1); + data->exec = NULL; + data->exit_status = 0; + } } diff --git a/src/utils/error.c b/src/utils/error.c index 6ddf057..4866c27 100644 --- a/src/utils/error.c +++ b/src/utils/error.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 21:42:24 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/01 20:12:37 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/03 19:01:37 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -35,6 +35,8 @@ void err(char *err, char *cmd, int exit_code) * @brief called if there is an error in minihsell that should close the program * * @param str The error message + * @param int the exit code to be set + * @param data the t_shell data to freed */ void exit_mini(char *str, int exit_code) { @@ -46,7 +48,6 @@ void exit_mini(char *str, int exit_code) } else write(STDERR_FILENO, "minishell: error\n", 16); - free_global(true); exit(exit_code); } @@ -55,17 +56,13 @@ void exit_mini(char *str, int exit_code) * * @param type The type of error * @param name The name of the file that caused the error - * @param func function to be called before exiting - * @param data data to be passed to func - * @warning func is executed with data regardless of data is NULL or not - * @note free global.tokens before returning? + * @param data t_shell data to be freed + * @param free_struct if t_shell should be freed including env */ -void err(t_exit type, char *name, void (*func)(void *), void *data) +void err(t_exit type, char *name, t_shell *data, bool free_struct) { int status; - if (func) - func(data); if (type == PERROR) { perror("minishell:"); @@ -112,6 +109,6 @@ void err(t_exit type, char *name, void (*func)(void *), void *data) write(STDERR_FILENO, "^C\n", 3); status = 130; } - free_global(true); + free_shell(data, free_struct); exit(status); } diff --git a/src/utils/global.c b/src/utils/global.c deleted file mode 100644 index 3f598a5..0000000 --- a/src/utils/global.c +++ /dev/null @@ -1,30 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* global.c :+: :+: */ -/* +:+ */ -/* By: mdekker/jde-baai +#+ */ -/* +#+ */ -/* Created: 2023/07/21 04:17:23 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/28 11:29:57 by mdekker ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include - -extern t_global g_data; - -/** - * @brief Frees the global variables - * - * @param exit If is true, the exit status and env vector will be freed - */ -void free_global(bool exit) -{ - vec_free(&g_data.tokens); - if (exit) - { - vec_free(&g_data.env); - free(g_data.exit_status); - } -} diff --git a/src/utils/miscellaneous.c b/src/utils/miscellaneous.c index 6428080..37caa9f 100644 --- a/src/utils/miscellaneous.c +++ b/src/utils/miscellaneous.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/19 16:53:28 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/01 17:52:14 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/03 20:19:39 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -29,3 +29,35 @@ char *rm_quotes(t_token *token) return (ft_strtrim(token->value, "\"")); } } + +/** + * @brief checks if token->type is a redirect + * @note redirects include O_REDIRECT I_REDIRECT A_REDIRECT HEREDOC + * @return true if its a redirect type + * false if not +*/ +bool is_redirect(t_token *token) +{ + if (token->type == O_REDIRECT || token->type == I_REDIRECT + || token->type == A_REDIRECT || HEREDOC) + { + return (true); + } + return (false); +} + +/** + * @brief checks if token->type is a string, single-or double_quote + * @note includes STRING SINGLE_QUOTE DOUBLE_QUOTE + * @return true is its a string type + * false if not +*/ +bool is_string_type(t_token *token) +{ + if (token->type == STRING || token->type == SINGLE_QUOTE + || token->type == DOUBLE_QUOTE) + { + return (true); + } + return (false); +} \ No newline at end of file From 502c59469b18aa777718f853850e419c6d127200 Mon Sep 17 00:00:00 2001 From: JuliusdB Date: Wed, 6 Sep 2023 13:00:55 +0200 Subject: [PATCH 86/91] . --- src/structs/shell.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/structs/shell.c b/src/structs/shell.c index d28dce4..0be9a9e 100644 --- a/src/structs/shell.c +++ b/src/structs/shell.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/09/03 18:11:09 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/03 18:59:20 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/06 13:00:01 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,14 +14,13 @@ extern t_signal signal; - /** * @brief frees the main data struct * @param data t_shell data * @param close_shell true to free env and the struct * false to keep data and env - * -*/ + * + */ void free_shell(t_shell *data, bool close_shell) { vec_free(&data->token_vec); @@ -100,6 +99,5 @@ t_shell *init_shell(char **env, bool first_init) if (!vec_init(&data->token_vec, 3, sizeof(t_token), clear_token)) exit_mini("failed to vec_init env", 1); data->exec = NULL; - data->exit_status = 0; } } From 86ad7e55c6d4dce565fc305731e67bc60d426f94 Mon Sep 17 00:00:00 2001 From: Julius De Baaij Date: Wed, 6 Sep 2023 20:51:41 +0200 Subject: [PATCH 87/91] New plan: put type in struct to define the error + str, if a function returns false then write err, turn all voids into bool, red + find_cmd still need to be done --- .vscode/c_cpp_properties.json | 16 +++- .vscode/settings.json | 3 +- Makefile | 2 +- includes/minishell.h | 51 ++++++------ includes/structs.h | 5 +- src/exec/create_processes.c | 24 +++--- src/exec/exec.c | 45 +++++++---- src/exec/exec_process.c | 10 +-- src/exec/utils.c | 6 +- src/{exec => group}/group.c | 68 ++++++++-------- src/{exec => group}/heredoc.c | 15 ++-- src/lexer/index.c | 10 +-- src/lexer/token.c | 8 +- src/main.c | 147 +++++++++++++++++----------------- src/parser/index.c | 4 +- src/parser/verify_token.c | 12 +-- src/structs/group.c | 33 ++++---- src/structs/shell.c | 8 +- src/utils/error.c | 39 +++------ 19 files changed, 252 insertions(+), 254 deletions(-) rename src/{exec => group}/group.c (60%) rename src/{exec => group}/heredoc.c (82%) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index cec6ed1..5c350dd 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -2,6 +2,7 @@ "configurations": [ { "name": "Mac", + "os": "mac", "includePath": [ "${workspaceFolder}/includes/**", "${workspaceFolder}/libft/includes/**" @@ -14,7 +15,20 @@ "cStandard": "c17", "cppStandard": "c++17", "configurationProvider": "ms-vscode.makefile-tools" + }, + { + "name": "Ubuntu", + "os": "linux", + "includePath": [ + "${workspaceFolder}/includes/**", + "${workspaceFolder}//libft/includes/**" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "c17", + "cppStandard": "c++17", + "configurationProvider": "ms-vscode.makefile-tools" } ], "version": 4 -} +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 5c1f543..e85bc30 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -21,6 +21,7 @@ "string": "c", "string_view": "c", "vector": "c", - "minishell.h": "c" + "minishell.h": "c", + "random": "c" } } \ No newline at end of file diff --git a/Makefile b/Makefile index 9ffa8ca..3cc7f4c 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = minishell -SRC = main check_input utils/error structs/token structs/group parser/index parser/token_vec2 parser/quotes parser/token_vec debug/print_vector lexer/index lexer/string lexer/token lexer/op_split structs/env structs/shell exec/group exec/heredoc parser/verify_token exec/exec exec/create_processes exec/redirect exec/exec_process utils/miscellaneous +SRC = main check_input utils/error structs/token structs/group parser/index parser/token_vec2 parser/quotes parser/token_vec debug/print_vector lexer/index lexer/string lexer/token lexer/op_split structs/env structs/shell group/group group/heredoc parser/verify_token exec/exec exec/create_processes exec/redirect exec/exec_process utils/miscellaneous exec/utils SRCS = $(addsuffix .c, $(addprefix src/, $(SRC))) OBJS = $(patsubst src/%.c, build/%.o, $(SRCS)) LIBFT = libft/libft.a diff --git a/includes/minishell.h b/includes/minishell.h index 4e0b70e..50560cc 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/09 21:25:59 by mdekker #+# #+# */ -/* Updated: 2023/09/03 20:21:55 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/06 20:29:47 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ # define MINISHELL_H # include +# include # include # include # include @@ -22,7 +23,6 @@ # include # include # include -# include # ifndef DEBUG # define DEBUG 0 @@ -32,7 +32,7 @@ t_shell *init_shell(char **env, bool first_init); void free_shell(t_shell *data, bool close_shell); -/* input_check */ +/* lexer */ void lexer(char *input, t_shell *data); bool check_quotes_parantheses(char *input); bool create_string(char *str, size_t *i, t_vector *vec); @@ -41,23 +41,6 @@ bool create_paran_string(char *str, size_t *i, t_vector *vec); void operator_split(t_shell *data); char **split(t_token *token); -/* structs */ -t_token *create_token(char *value, t_types type); -void clear_token(void *data); -t_token *dup_token(t_token *input); -t_env *create_env(char *key, char *value); -void clear_env(void *data); -t_group *create_group(void); -void clear_group(void *data); -void clear_fname(void *data); -t_exec *create_exec(void); -void clear_exec(t_exec *exec); - -/* utils */ -void exit_mini(char *str, int exit_code); -void err(t_exit type, char *name, t_shell *data, bool free_struct); -char *rm_quotes(t_token *token); - /* parser */ void parser(t_shell *data); void parse_one(t_token *token); @@ -74,23 +57,39 @@ bool is_or(char *str); bool is_and(char *str); void verify_token_vec(t_shell *data); +/* group */ +void group_token_vec(t_shell *data); +void heredoc(char *filename, char *stop, t_types type, t_shell *data); /* executor */ -t_exec *group_token_vec(t_vector *token_vec, char **envp); -void heredoc(char *filename, char *stop, t_types type, t_exec *exec); - -int executor(t_exec *exec); -bool create_processes(t_exec *exec); +int executor(t_shell *data); +bool create_processes(t_shell *data); void exec_process(t_group *group, t_process type); void redirect_input(t_group *group, size_t i); -//utils +/* exec_utils */ bool is_built_in(char *str); char **combine_env(t_vector *env_vec); char **create_cmd(t_vector *input); bool is_redirect(t_token *token); bool is_string_type(t_token *token); +/* structs */ +t_token *create_token(char *value, t_types type); +void clear_token(void *data); +t_token *dup_token(t_token *input); +t_env *create_env(char *key, char *value); +void clear_env(void *data); +t_group *create_group(void); +void clear_group(void *data); +void clear_fname(void *data); +t_exec *create_exec(void); +void clear_exec(t_exec *exec); + +/* general utils */ +void exit_mini(char *str, int exit_code); +void err(t_exit type, char *name, t_shell *data, bool free_struct); +char *rm_quotes(t_token *token); /* debug */ void print_vector(t_vector *vec, void (*printer)(void *, size_t)); diff --git a/includes/structs.h b/includes/structs.h index 20dd2e5..207f3d2 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/09/03 18:53:43 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/06 15:56:16 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -53,6 +53,7 @@ typedef struct s_signal bool quit; bool inte; bool pipe; + int exit_status; } t_signal; /** @@ -98,10 +99,8 @@ typedef struct s_shell t_vector token_vec; t_vector env; t_exec *exec; - int exit_status; } t_shell; - /** * @brief The struct for the parser functions. * diff --git a/src/exec/create_processes.c b/src/exec/create_processes.c index 9a4373e..abe6c86 100644 --- a/src/exec/create_processes.c +++ b/src/exec/create_processes.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/19 12:40:07 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/01 19:47:27 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/06 20:31:39 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -32,14 +32,14 @@ static bool add_pipes(t_vector *group_vec, size_t process_count) return (true); } -static bool fork_processes(t_exec *exec, size_t process_count) +static bool fork_processes(t_shell *data, size_t process_count) { size_t i; t_group *group; t_group *next_group; t_vector *group_vec; - group_vec = &exec->group_vec; + group_vec = &data->exec->group_vec; i = 0; while (i < process_count) { @@ -50,26 +50,24 @@ static bool fork_processes(t_exec *exec, size_t process_count) if (group->pd == 0) { if (i == 0) - exec_process(LEFT, group); + exec_process(LEFT, data); else if (i == process_count - 1) - exec_process(RIGHT, group); + exec_process(RIGHT, data); else - exec_process(MIDDLE, group); + exec_process(MIDDLE, data); } i++; } return (true); } -bool create_processes(t_exec *exec) +bool create_processes(t_shell *data) { t_vector *group_vec; - size_t process_count; t_group *group; - group_vec = &exec->group_vec; - process_count = group_vec->length; - if (process_count == 1) + group_vec = &data->exec->group_vec; + if (group_vec->length == 1) { group = vector_get(group_vec, 0); group->pd = fork(); @@ -79,9 +77,9 @@ bool create_processes(t_exec *exec) } else { - if (!add_pipes(group_vec, process_count)) + if (!add_pipes(group_vec, group_vec->length)) return (false); - if (!fork_processes(group_vec, process_count)) + if (!fork_processes(group_vec, group_vec->length)) return (false); } return (true); diff --git a/src/exec/exec.c b/src/exec/exec.c index a5022c8..44417af 100644 --- a/src/exec/exec.c +++ b/src/exec/exec.c @@ -6,13 +6,13 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:50 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/01 16:53:31 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/06 20:49:39 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -static bool close_pipes(t_vector *group_vec) +close_pipes(t_vector *group_vec) { size_t i; t_group *group; @@ -62,30 +62,43 @@ static int wait_processes(t_vector *group_vec) return (status); } +static void check_for_exit(t_shell *data) +{ + printf("exit was called\n"); + // int long long atoi + free_shell(data, true); + exit(10); +} + /** - * @brief creates pipes and starts processes, closes pipes and waits for child_processes + * @brief creates pipes and starts processes, + closes pipes and waits for child_processes * @return return value is the EXITSTATUS of the last childprocess to complete - * @note if all testing is succesful make it a void function and exit status instead of returning it -*/ -int executor(t_exec *exec) + + * @note if all testing is succesful make it a void function and exit status instead of returning it + */ +int executor(t_shell *data) { int temp; int status; - if (!create_processes(exec)) + if ((&data->exec->group_vec)->length == 1) + { + check_for_exit(data); // write function to exit() + } + if (!create_processes(data)) { - close_pipes(&exec->group_vec); - wait_process(&exec->group_vec); - err(PERROR, NULL, clear_exec, exec); + close_pipes(&data->exec->group_vec); + wait_process(&data->exec->group_vec); + err(PERROR, NULL, data, true); } - if (!close_pipes(&exec->group_vec)) + if (!close_pipes(&data->exec->group_vec)) { - wait_processes(&exec->group_vec); - err(PERROR, NULL, clear_exec, exec); + wait_processes(&data->exec->group_vec); + err(PERROR, NULL, data, true); } - status = wait_processes(&exec->group_vec); + status = wait_processes(&data->exec->group_vec); if (status == -1) - err(PERROR, NULL, clear_exec, exec); - clear_exec(exec); + err(PERROR, NULL, data, true); return (status); } diff --git a/src/exec/exec_process.c b/src/exec/exec_process.c index c53c5b8..7836ba9 100644 --- a/src/exec/exec_process.c +++ b/src/exec/exec_process.c @@ -6,14 +6,12 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/19 16:08:08 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/01 22:26:59 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/06 17:20:33 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -extern t_global g_data; - void exec_process(t_group *group, t_process type) { size_t i; @@ -32,8 +30,6 @@ void exec_process(t_group *group, t_process type) execve(str, group->cmd, env); } - - static void close_start(t_process type, t_group *group) { if (type == LEFT) @@ -82,8 +78,8 @@ static void exec_built_in(t_group *group, t_process type, t_vector *env) { handle_redirects(group); dup_fd(type, group); - if (ft_strcmp(group->cmd[0], "exit") == 0) - ft_exec_exit(group->cmd, type); //@note create built in for exit + if (ft_strcmp(group->cmd[0], "exit")) + ft_exec_exit(group->cmd, type); if (ft_strcmp(group->cmd[0], "echo") == 0) ft_echo(group->cmd, env); else if (ft_strcmp(group->cmd[0], "cd") == 0) diff --git a/src/exec/utils.c b/src/exec/utils.c index 0a328e1..2e00b93 100644 --- a/src/exec/utils.c +++ b/src/exec/utils.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/09/01 19:44:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/03 20:16:02 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/06 17:07:42 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,12 +14,10 @@ bool is_built_in(char *str) { - if (ft_strcmp(str, "exit") == 0) //@note or any of the other strings + if (ft_strcmp(str, "exit") == 0 || ft_strcmp(str, "cd") == 0) return (true); } char **combine_env(t_vector *env_vec) { - } - diff --git a/src/exec/group.c b/src/group/group.c similarity index 60% rename from src/exec/group.c rename to src/group/group.c index e2347b3..f416c45 100644 --- a/src/exec/group.c +++ b/src/group/group.c @@ -6,13 +6,13 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:05 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/03 17:12:24 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/06 20:36:35 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -static void push_hdoc(char *filename, t_group *group, t_exec *exec) +static void push_hdoc(char *filename, t_group *group, t_shell *data) { char *fname; t_token *red_token; @@ -20,29 +20,29 @@ static void push_hdoc(char *filename, t_group *group, t_exec *exec) fname = ft_strdup(filename); if (!fname) - err(MALLOC, "push_hdoc", clear_exec, exec); + err(MALLOC, "push_hdoc", data, true); fname_token = create_token(filename, STRING); if (!fname_token) - err(MALLOC, "push_hdoc", clear_exec, exec); + err(MALLOC, "push_hdoc", data, true); red_token = create_token("<", I_REDIRECT); if (!red_token) - err(MALLOC, "push_hdoc", clear_exec, exec); + err(MALLOC, "push_hdoc", data, true); if (!vec_push(&group->input, (void *)red_token)) - err(MALLOC, "push_hdoc", clear_exec, exec); + err(MALLOC, "push_hdoc", data, true); if (!vec_push(&group->input, (void *)fname_token)) - err(MALLOC, "push_hdoc", clear_exec, exec); - if (!vec_push(&exec->fname_vec, (void *)fname)) - err(MALLOC, "push_hdoc", clear_exec, exec); + err(MALLOC, "push_hdoc", data, true); + if (!vec_push(&data->exec->fname_vec, (void *)fname)) + err(MALLOC, "push_hdoc", data, true); } /** * @brief Creates a token for the hdoc file, and adds the filename to the f_name - * @param token_vec the token vector * @param group the group to add the new token_vec to * @param i to step over the heredoc and the quotes / filenames + * @param data general data struct * @param */ -static void hdoc_found(t_vector token_vec, t_group *group, int *i, t_exec *exec) +static void hdoc_found(t_group *group, int *i, t_shell *data) { t_token *token; char *stop; @@ -50,49 +50,45 @@ static void hdoc_found(t_vector token_vec, t_group *group, int *i, t_exec *exec) filename = ft_strjoin("./src/.heredoc/", ft_itoa((*i))); if (!filename) - err(MALLOC, "hdoc_found", clear_exec, exec); + err(MALLOC, "hdoc_found", data, true); (*i) = +1; - token = vec_get(&token_vec, (*i)); + token = vec_get(&data->token_vec, (*i)); if ((token->type == SINGLE_QUOTE || token->type == DOUBLE_QUOTE) && ft_strlen(token->value) == 2) - heredoc(filename, "", token->type, exec); + heredoc(filename, "", token->type, data); else { stop = rm_quotes(token); if (!stop) - err(MALLOC, "hdoc_found", clear_exec, exec); - heredoc(filename, stop, token->type, exec); + err(MALLOC, "hdoc_found", data, true); + heredoc(filename, stop, token->type, data); free(stop); } - push_hdoc(filename, group, exec); + push_hdoc(filename, group, data); (*i) += 1; } -t_group *make_group(t_vector *token_vec, int *i, t_exec *exec) +static void group_tokens(t_group *group, int *i, t_shell *data) { t_token *dup; - t_group *group; t_token *token; - group = create_group(); - if (!group) - err(MALLOC, "make_group", clear_exec, exec); - token = (t_token *)vec_get(token_vec, (*i)); + token = (t_token *)vec_get(&data->token_vec, (*i)); while (token->type != PIPE) { if (token->type == HEREDOC) - hdoc_found(*token_vec, group, i, exec); + hdoc_found(group, &i, data); else { dup = dup_token(token); if (!dup) - err(MALLOC, "make_group", clear_exec, exec); + err(MALLOC, "group_tokens", data, true); vec_push(&group->input, (void *)dup); } - if ((*i) >= token_vec->length) + if ((*i) >= (&data->token_vec)->length) break ; (*i)++; - token = (t_token *)vec_get(token_vec, (*i)); + token = (t_token *)vec_get(&data->exec->group_vec, (*i)); } return (group); } @@ -103,20 +99,24 @@ t_group *make_group(t_vector *token_vec, int *i, t_exec *exec) * @param token_vec The vector containing the token_vec * @return t_exec* The struct containing the groups */ -t_exec *group_token_vec(t_vector *token_vec, char **envp) +void group_token_vec(t_shell *data) { size_t i; t_exec *exec; t_group *group; - exec = create_exec(); - if (!exec) - err(MALLOC, "group_token_vec", NULL, NULL); + data->exec = create_exec(); + if (!data->exec) + err(MALLOC, "group_token_vec", data, true); i = 0; - while (i < token_vec->length) + while (i < (&data->token_vec)->length) { - group = make_group(token_vec, &i, exec); - vec_push(&exec->group_vec, group); + group = create_group(); + if (!group) + err(MALLOC, "group_token_vec", clear_exec, exec); + group_tokens(group, &i, data); + if (!vec_push(&exec->group_vec, group)) + err(MALLOC, "group_token_vec", data, true); i++; } return (exec); diff --git a/src/exec/heredoc.c b/src/group/heredoc.c similarity index 82% rename from src/exec/heredoc.c rename to src/group/heredoc.c index b596266..d941a4b 100644 --- a/src/exec/heredoc.c +++ b/src/group/heredoc.c @@ -6,14 +6,13 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/16 12:15:45 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/01 17:24:09 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/06 20:36:35 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include - -static void loop(int heredoc_fd, char *stop, t_types type, t_exec *exec) +static void loop(int heredoc_fd, char *stop, t_types type, t_shell *data) { char *line; @@ -28,7 +27,7 @@ static void loop(int heredoc_fd, char *stop, t_types type, t_exec *exec) if (!line) { free(line); - err(MALLOC, "hdoc_loop", clear_exec, exec); + err(MALLOC, "hdoc_loop", data, true); } } write(heredoc_fd, line, ft_strlen(line)); @@ -43,14 +42,14 @@ static void loop(int heredoc_fd, char *stop, t_types type, t_exec *exec) * @param stop the string to stop readline * @param type string or quoted, if quoted there will be no expansion. * @param exec to be freed when an error occurs -*/ -void heredoc(char *filename, char *stop, t_types type, t_exec *exec) + */ +void heredoc(char *filename, char *stop, t_types type, t_shell *data) { - int heredoc_fd; + int heredoc_fd; heredoc_fd = open(filename, O_CREAT | O_WRONLY, 0644); if (heredoc_fd == -1) - err(PERROR, filename, clear_exec, exec); + err(PERROR, filename, data, true); if (heredoc_fd < 0) return (false); close(heredoc_fd); diff --git a/src/lexer/index.c b/src/lexer/index.c index b81f357..33b6e26 100644 --- a/src/lexer/index.c +++ b/src/lexer/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 13:32:55 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/03 19:25:37 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/06 20:36:35 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -133,19 +133,19 @@ void lexer(char *input, t_shell *data) if (checkchar(input[i], "\"\'()") == 1) { if (!check_delimiters(&input[i])) - err(SYNTAX_MINI, input[i], data, false); + err(SYNTAX_MINI, input[i], data, true); if (!make_string(input, &i, &data->token_vec)) - err(MALLOC, "malloc", data, false); + err(MALLOC, "malloc", data, true); } else if (input[i] == ' ') { if (!create_string(input, &i, &data->token_vec)) - err(MALLOC, "malloc", data, false); + err(MALLOC, "malloc", data, true); } else i++; } if (i > 0 && checkchar(input[i - 1], "\"\') ") == 0) if (!create_string(input, &i, &data->token_vec)) - err(MALLOC, "malloc", data, false); + err(MALLOC, "malloc", data, true); } diff --git a/src/lexer/token.c b/src/lexer/token.c index 8c038ab..6e2ba75 100644 --- a/src/lexer/token.c +++ b/src/lexer/token.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/21 13:06:18 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/03 20:06:17 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/06 20:36:35 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -24,7 +24,7 @@ static bool find_operator(t_token *token) return (true); } -static void insert_array(t_shell *data, char **array, int i) +static void insert_array(t_shell *data, char **array, int i) { int j; t_token *token; @@ -36,7 +36,7 @@ static void insert_array(t_shell *data, char **array, int i) if (!token || !vec_insert(&data->token_vec, i + j, token)) { ft_free(array); - err(MALLOC, "malloc", data, false); + err(MALLOC, "malloc", data, true); } j++; } @@ -59,7 +59,7 @@ void operator_split(t_shell *data) if (!array || !vec_remove(&data->token_vec, i)) { ft_free(array); - err(MALLOC, "malloc", data, false); + err(MALLOC, "malloc", data, true); } insert_array(data, array, i); } diff --git a/src/main.c b/src/main.c index 6cb3df9..9b71610 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/03 20:06:47 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/06 20:50:26 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -39,49 +39,39 @@ static void debug(void) * * @param vec The vector to store the token_vec in */ -static void loop(t_vector *vec) -{ - char *input; - - while (1) - { - input = readline("\n\033[1;32mminishell$ \033[0m"); - if (!input) - { - free(input); - break ; - } - add_history(input); - if (!ft_strcmp(input, "exit")) - return (free(input), free_global(true)); - else - { - if (!lexer(input, vec)) - return (free(input), free_global(true)); - parser(vec); - print_vector(vec, print_token); - } - free(input); - free_global(false); - vec_init(vec, 5, sizeof(t_token), clear_token); - } -} - -// static bool find_strings(void *data) +// static void loop(t_vector *vec) // { -// t_token *token; +// char *input; -// token = (t_token *)data; -// if (token->type == STRING) -// return (true); -// return (false); +// while (1) +// { +// input = readline("\n\033[1;32mminishell$ \033[0m"); +// if (!input) +// { +// free(input); +// break ; +// } +// add_history(input); +// if (!ft_strcmp(input, "exit")) +// return (free(input), free_global(true)); +// else +// { +// if (!lexer(input, vec)) +// return (free(input), free_global(true)); +// parser(vec); +// print_vector(vec, print_token); +// } +// free(input); +// free_global(false); +// vec_init(vec, 5, sizeof(t_token), clear_token); +// } // } int main(int ac, char **av, char **env) { - t_shell *data; - pid_t pid; - int status; + t_shell *data; + pid_t pid; + int status; // t_found **found; if (DEBUG) @@ -94,51 +84,58 @@ int main(int ac, char **av, char **env) exit_mini("initial fork failed", errno); if (pid == 0) { - lexer(av[1], data); - //lexer retester + if (!lexer(av[1], data)) + err(); + // lexer retester parser(data); - //parser retesting + // parser retesting operator_split(data); // operator split retesting - verify_token_vec(&data->token_vec); + verify_token_vec(data); // check if it really catches all doubles - data->exec = group_token_vec(&data->token_vec, env); + group_token_vec(data); + // check if all groups are properly cerated status = executor(data->exec); + free_shell(data, true); exit(status); } if (waitpid(pid, &status, 0) == -1) exit_mini("waitpid failed", errno); - data->exit_status = WEXITSTATUS(status); - // found = g_data.token_vec.find(&g_data.token_vec, find_strings); - // if (!found) - // { - // printf("%zu counted\n", g_data.token_vec.count(&g_data.token_vec, - // find_strings)); - // printf("No matches found\n"); - // } - // else - // { - // printf("Printing matches:\n"); - // while (*found) - // { - // print_token((*found)->item, (*found)->index); - // free(*found); - // found++; - // } - // free(found); - // } - // if (DEBUG) - print_vector(&g_data.token_vec, print_token); - // print_vector(&g_data.env, print_env); - free_global(true); + if (WEXITSTATUS(status) == 10) + { + return (10); // pipe workaround + signal.exit_status = WEXITSTATUS(status); + // found = g_data.token_vec.find(&g_data.token_vec, find_strings); + // if (!found) + // { + // printf("%zu counted\n", + g_data.token_vec.count(&g_data.token_vec, + // find_strings)); + // printf("No matches found\n"); + // } + // else + // { + // printf("Printing matches:\n"); + // while (*found) + // { + // print_token((*found)->item, (*found)->index); + // free(*found); + // found++; + // } + // free(found); + // } + // if (DEBUG) + print_vector(&data->token_vec, print_token); + // print_vector(&g_data.env, print_env); + free_shell(data, true); + return (0); + } + // else if (ac == 1) + // loop(data); + else + { + printf("Too many arguments"); + free_shell(data, true); + } return (0); } - else if (ac == 1) - loop(&g_data.token_vec); - else - { - printf("Too many arguments"); - free_global(true); - } - return (0); -} diff --git a/src/parser/index.c b/src/parser/index.c index 7e54930..8e3b080 100644 --- a/src/parser/index.c +++ b/src/parser/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 11:12:20 by mdekker #+# #+# */ -/* Updated: 2023/09/03 19:30:07 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/06 20:36:35 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -86,7 +86,7 @@ void parser(t_shell *data) func_map = return_map(); if (func_map == NULL) - return (err(MALLOC, "parser", data, false)); + return (err(MALLOC, "parser", data, true)); parse_loop(&data->token_vec, func_map); free(func_map); } diff --git a/src/parser/verify_token.c b/src/parser/verify_token.c index 7f76ea6..dad904b 100644 --- a/src/parser/verify_token.c +++ b/src/parser/verify_token.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/02 16:57:32 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/03 20:23:18 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/06 20:36:35 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ static void loop(t_shell *data) { next = vec_get(&data->token_vec, i + 1); if (token->type == next->type) - err(SYNTAX, next->value, data, false); + err(SYNTAX, next->value, data, true); } i++; } @@ -48,10 +48,10 @@ static void check_redirect(t_shell *data, int i) t_token *next; if (i + 1 >= (&data->token_vec)->length) - err(SYNTAX, "newline", data, false); + err(SYNTAX, "newline", data, true); next = vec_get(&data->token_vec, i + 1); if (!is_string_type(next)) - err(SYNTAX, next->value, data, false); + err(SYNTAX, next->value, data, true); } /** @@ -68,13 +68,13 @@ void verify_token_vec(t_shell *data) i = 0; token = vec_get(&data->token_vec, i); if (token->type == PIPE) - err(SYNTAX, token->value, data, false); + err(SYNTAX, token->value, data, true); loop(data); if ((&data->token_vec)->length > 1) { i = (&data->token_vec)->length - 1; token = vec_get(&data->token_vec, i); if (token->type == PIPE) - err(SYNTAX_MINI, token->value, data, false); + err(SYNTAX_MINI, token->value, data, true); } } diff --git a/src/structs/group.c b/src/structs/group.c index a52381f..a747c1c 100644 --- a/src/structs/group.c +++ b/src/structs/group.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/02 20:42:59 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/01 21:52:28 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/06 18:49:48 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,10 +14,10 @@ /** * clears the data inside t_process; -*/ + */ void clear_group(void *data) { - t_group *p; + t_group *p; if (!data) return ; @@ -56,7 +56,7 @@ void clear_exec(t_exec *exec) * @note left/right pipe values are set to -1 * @note pid_t is set to -2 by default * @return t_group initialised, NULL on malloc failure -*/ + */ t_group *create_group(void) { t_group *p; @@ -76,21 +76,24 @@ t_group *create_group(void) /** * @brief creates t_exec -*/ -t_exec *create_exec(void) + */ +t_exec *create_exec(void) { - t_exec *exec; - t_vector group_vec; - t_vector fname_vec; + t_exec *exec; exec = malloc(sizeof(t_exec)); if (!exec) return (NULL); - if (!vec_init(&group_vec, 2, sizeof(t_group), clear_group)) - return (free(exec), NULL); - if (!vec_init(&fname_vec, 1, sizeof(char *), clear_fname)) - return (free(exec), NULL); - exec->group_vec = group_vec; - exec->fname_vec = fname_vec; + if (!vec_init(&exec->group_vec, 2, sizeof(t_group), clear_group)) + { + free(exec); + return (NULL); + } + if (!vec_init(&exec->fname_vec, 1, sizeof(char *), clear_fname)) + { + vec_free(&exec->group_vec); + free(exec); + return (NULL); + } return (exec); } diff --git a/src/structs/shell.c b/src/structs/shell.c index 0be9a9e..95d4bc6 100644 --- a/src/structs/shell.c +++ b/src/structs/shell.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/09/03 18:11:09 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/06 13:00:01 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/06 19:44:47 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -49,9 +49,9 @@ static void init_env(char **env, t_vector *env_vec) key = ft_substr(env[i], 0, ft_strchr(env[i], '=') - env[i]); value = ft_strdup(ft_strchr(env[i], '=') + 1); if (!key || !value) - exit(1); // !TODO: error message + exit_mini("init_env", 1); if (!vec_push(env_vec, create_env(key, value))) - exit(1); + exit_mini("init_env", 1); i++; } } @@ -66,6 +66,7 @@ void create_signal_struct(void) signal.inte = false; signal.quit = false; signal.pipe = false; + signal.exit_status = 0; } /** @@ -91,7 +92,6 @@ t_shell *init_shell(char **env, bool first_init) exit_mini("failed to vec_init env", 1); init_env(env, &data->env); data->exec = NULL; - data->exit_status = 0; create_signal_struct(); } else diff --git a/src/utils/error.c b/src/utils/error.c index 4866c27..3dd50da 100644 --- a/src/utils/error.c +++ b/src/utils/error.c @@ -6,30 +6,13 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 21:42:24 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/03 19:01:37 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/06 18:36:43 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ #include -/* -/** - * @brief Prints an error message - * - * @param err The error message - * @param cmd The command that caused the error - * @param exit_code The exit code of the error - -void err(char *err, char *cmd, int exit_code) -{ - printf("\033[1;31m"); - printf("❗️ Error: %s\n", err); - if (cmd) - printf("In command: %s\n", cmd); - printf("Exit code: %d\n", exit_code); - printf("\033[0m"); -} -*/ +extern t_signal signal; /** * @brief called if there is an error in minihsell that should close the program @@ -61,33 +44,31 @@ void exit_mini(char *str, int exit_code) */ void err(t_exit type, char *name, t_shell *data, bool free_struct) { - int status; - if (type == PERROR) { perror("minishell:"); - status = errno; // related exit code? + signal.exit_status = errno; } if (type == MALLOC) { write(STDERR_FILENO, "minishell: malloc error in : ", 28); write(STDERR_FILENO, name, ft_strlen(name)); write(STDERR_FILENO, "\n", 1); - status = 1; + signal.exit_status = 1; } if (type == NOT_FOUND) { write(STDERR_FILENO, "minishell: ", 11); write(STDERR_FILENO, name, ft_strlen(name)); write(STDERR_FILENO, ": No such file or directory\n", 28); - status = 127; + signal.exit_status = 127; } if (type == PERMISSION) { write(STDERR_FILENO, "minishell: ", 11); write(STDERR_FILENO, name, ft_strlen(name)); write(STDERR_FILENO, ": Permission denied\n", 20); - status = 126; + signal.exit_status = 126; } if (type == SYNTAX) { @@ -95,20 +76,20 @@ void err(t_exit type, char *name, t_shell *data, bool free_struct) 47); write(STDERR_FILENO, name, ft_strlen(name)); write(STDERR_FILENO, "'\n", 2); - status = 258; + signal.exit_status = 258; } if (type == SYNTAX_MINI) { write(STDERR_FILENO, "minishell: unfinished operator : `", 34); write(STDERR_FILENO, name, ft_strlen(name)); write(STDERR_FILENO, "'\n", 2); - status = 2; + signal.exit_status = 2; } if (type == SIGNAL_C) { write(STDERR_FILENO, "^C\n", 3); - status = 130; + signal.exit_status = 130; } free_shell(data, free_struct); - exit(status); + exit(signal.exit_status); } From 17b902830989604a70becee6a6c2dea292f5b38d Mon Sep 17 00:00:00 2001 From: JuliusdB Date: Thu, 7 Sep 2023 02:39:34 +0200 Subject: [PATCH 88/91] comment on how to proceed in todo.txt --- includes/structs.h | 4 +++- src/exec/exec.c | 5 +++-- src/exec/exec_process.c | 6 +++--- src/main.c | 3 ++- src/structs/group.c | 8 +++++++- 5 files changed, 18 insertions(+), 8 deletions(-) diff --git a/includes/structs.h b/includes/structs.h index 207f3d2..dce0650 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/09/06 15:56:16 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/07 02:33:46 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -81,6 +81,8 @@ typedef struct s_exec typedef struct s_group { t_vector input; + t_vector in_red; + t_vector out_red; char **cmd; pid_t pd; int left_pipe[2]; diff --git a/src/exec/exec.c b/src/exec/exec.c index 44417af..aa8850f 100644 --- a/src/exec/exec.c +++ b/src/exec/exec.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/31 19:55:50 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/06 20:49:39 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/07 02:39:16 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -84,7 +84,8 @@ int executor(t_shell *data) if ((&data->exec->group_vec)->length == 1) { - check_for_exit(data); // write function to exit() + check_for_exit(data); // check for cd / exit / unset + // export; in which case dont pipe } if (!create_processes(data)) { diff --git a/src/exec/exec_process.c b/src/exec/exec_process.c index 7836ba9..e3a6735 100644 --- a/src/exec/exec_process.c +++ b/src/exec/exec_process.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/19 16:08:08 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/06 17:20:33 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/07 02:38:25 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -76,8 +76,8 @@ static void dup_fd(t_process type, t_group *group) static void exec_built_in(t_group *group, t_process type, t_vector *env) { - handle_redirects(group); - dup_fd(type, group); + dup_fd(type, group);//@note check if it should maybe only be done for outfiles? + // for the calls of the builtins just pass the entire group as paramater so they can read their own indirects if (ft_strcmp(group->cmd[0], "exit")) ft_exec_exit(group->cmd, type); if (ft_strcmp(group->cmd[0], "echo") == 0) diff --git a/src/main.c b/src/main.c index 9b71610..99ee80a 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/06 20:50:26 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/07 02:36:16 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -93,6 +93,7 @@ int main(int ac, char **av, char **env) // operator split retesting verify_token_vec(data); // check if it really catches all doubles + // build function that combines a redirects into 1 singular token group_token_vec(data); // check if all groups are properly cerated status = executor(data->exec); diff --git a/src/structs/group.c b/src/structs/group.c index a747c1c..213de62 100644 --- a/src/structs/group.c +++ b/src/structs/group.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/02 20:42:59 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/06 18:49:48 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/07 02:34:50 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,6 +23,8 @@ void clear_group(void *data) return ; p = (t_group *)data; vec_free(&p->input); + vec_free(&p->in_red); + vec_free(&p->out_red); if (p->cmd) ft_free(p->cmd); p = NULL; @@ -66,6 +68,10 @@ t_group *create_group(void) return (NULL); if (!vec_init(&p->input, 2, sizeof(t_token), clear_token)) return (free(p), NULL); + if (!vec_init(&p->in_red, 2, sizeof(t_token), clear_token)) + return (vec_free(&p->input), free(p), NULL); + if (!vec_init(&p->out_red, 2, sizeof(t_token), clear_token)) + return (vec_free(&p->input), vec_free(&p->in_red), free(p), NULL); p->cmd = NULL; p->pd = -2; p->left_pipe[0] = -1; From 90887f1bf12caddfa250f80d5422a1b3ddbdcf72 Mon Sep 17 00:00:00 2001 From: JuliusdB Date: Thu, 7 Sep 2023 03:50:52 +0200 Subject: [PATCH 89/91] more todo --- includes/enum.h | 3 ++- includes/structs.h | 13 +++++++++---- src/utils/miscellaneous.c | 12 ++++++------ 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/includes/enum.h b/includes/enum.h index 21dfb1e..a043f9a 100644 --- a/includes/enum.h +++ b/includes/enum.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/13 17:41:00 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/03 17:12:24 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/07 03:46:17 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -63,6 +63,7 @@ typedef enum e_types */ typedef enum e_exit { + GOOD, PERROR, MALLOC, NOT_FOUND, diff --git a/includes/structs.h b/includes/structs.h index dce0650..b78df3f 100644 --- a/includes/structs.h +++ b/includes/structs.h @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/10 11:15:16 by mdekker #+# #+# */ -/* Updated: 2023/09/07 02:33:46 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/07 03:48:26 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -80,10 +80,10 @@ typedef struct s_exec */ typedef struct s_group { - t_vector input; - t_vector in_red; + char *cmd; + char **args; + t_vector in_red; t_vector out_red; - char **cmd; pid_t pd; int left_pipe[2]; int right_pipe[2]; @@ -95,12 +95,17 @@ typedef struct s_group * @param token_vec The token_vec vector from the lexer and parser; * @param env The environment variables; * @param data to be passed to the executor; + * @param exit_type The type of exit; + * @param exit_msg The message to be printed as part of err() + * @warning exit_msg will not be freed by err() */ typedef struct s_shell { t_vector token_vec; t_vector env; t_exec *exec; + t_exit exit_type; + char *exit_msg; } t_shell; /** diff --git a/src/utils/miscellaneous.c b/src/utils/miscellaneous.c index 37caa9f..c8e37ff 100644 --- a/src/utils/miscellaneous.c +++ b/src/utils/miscellaneous.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/08/19 16:53:28 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/03 20:19:39 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/07 03:42:54 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,7 +15,7 @@ char *rm_quotes(t_token *token) { if (token->type == STRING) - return (token->value); + return (ft_strdup(token->value)); if (token->type == SINGLE_QUOTE) { if (strlen(token->value) == 2) @@ -35,11 +35,11 @@ char *rm_quotes(t_token *token) * @note redirects include O_REDIRECT I_REDIRECT A_REDIRECT HEREDOC * @return true if its a redirect type * false if not -*/ + */ bool is_redirect(t_token *token) { if (token->type == O_REDIRECT || token->type == I_REDIRECT - || token->type == A_REDIRECT || HEREDOC) + || token->type == A_REDIRECT || token->type == HEREDOC) { return (true); } @@ -51,7 +51,7 @@ bool is_redirect(t_token *token) * @note includes STRING SINGLE_QUOTE DOUBLE_QUOTE * @return true is its a string type * false if not -*/ + */ bool is_string_type(t_token *token) { if (token->type == STRING || token->type == SINGLE_QUOTE @@ -60,4 +60,4 @@ bool is_string_type(t_token *token) return (true); } return (false); -} \ No newline at end of file +} From edabb67093291341138f08d1ab56e6acd07266d0 Mon Sep 17 00:00:00 2001 From: JuliusdB Date: Thu, 7 Sep 2023 13:18:27 +0200 Subject: [PATCH 90/91] =?UTF-8?q?=F0=9F=94=A7=20fix(shell.c):=20initialize?= =?UTF-8?q?=20exit=5Ftype=20and=20exit=5Fmsg=20variables=20in=20init=5Fshe?= =?UTF-8?q?ll=20function=20to=20prevent=20potential=20uninitialized=20vari?= =?UTF-8?q?able=20usage=20=F0=9F=94=A7=20fix(error.c):=20refactor=20err=20?= =?UTF-8?q?function=20to=20use=20the=20exit=5Ftype=20and=20exit=5Fmsg=20va?= =?UTF-8?q?riables=20from=20the=20t=5Fshell=20struct=20instead=20of=20pass?= =?UTF-8?q?ing=20them=20as=20arguments=20=E2=9C=A8=20feat(error.c):=20add?= =?UTF-8?q?=20set=5Ferr=20function=20to=20set=20the=20exit=5Ftype=20and=20?= =?UTF-8?q?exit=5Fmsg=20variables=20in=20the=20t=5Fshell=20struct=20based?= =?UTF-8?q?=20on=20the=20type=20of=20error=20=E2=9C=A8=20feat(error.c):=20?= =?UTF-8?q?add=20write=5Ferr=20function=20to=20print=20error=20messages=20?= =?UTF-8?q?based=20on=20the=20exit=5Ftype=20and=20exit=5Fmsg=20variables?= =?UTF-8?q?=20in=20the=20t=5Fshell=20struct?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/structs/shell.c | 6 +++++- src/utils/error.c | 51 ++++++++++++++++++++++++++------------------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/src/structs/shell.c b/src/structs/shell.c index 95d4bc6..7765bd7 100644 --- a/src/structs/shell.c +++ b/src/structs/shell.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/09/03 18:11:09 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/06 19:44:47 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/07 13:17:21 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -92,6 +92,8 @@ t_shell *init_shell(char **env, bool first_init) exit_mini("failed to vec_init env", 1); init_env(env, &data->env); data->exec = NULL; + data->exit_type = GOOD; + data->exit_msg = NULL; create_signal_struct(); } else @@ -99,5 +101,7 @@ t_shell *init_shell(char **env, bool first_init) if (!vec_init(&data->token_vec, 3, sizeof(t_token), clear_token)) exit_mini("failed to vec_init env", 1); data->exec = NULL; + data->exit_type = GOOD; + data->exit_msg = NULL; } } diff --git a/src/utils/error.c b/src/utils/error.c index 3dd50da..fff143e 100644 --- a/src/utils/error.c +++ b/src/utils/error.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 21:42:24 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/06 18:36:43 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/07 13:16:30 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,62 +34,71 @@ void exit_mini(char *str, int exit_code) exit(exit_code); } +bool set_err(t_exit type, char *msg, t_shell *data) +{ + if (type == GOOD) + return (true); + data->exit_type = type; + data->exit_msg = msg; + return (false); +} + /** - * @brief Prints an error message + * @brief Prints an error message based on the type of error + * @details If the type is GOOD, nothing is printed + * @details the t_Signal exit_status is set to the correct value * - * @param type The type of error - * @param name The name of the file that caused the error - * @param data t_shell data to be freed - * @param free_struct if t_shell should be freed including env + * @param data general data struct */ -void err(t_exit type, char *name, t_shell *data, bool free_struct) +void write_err(t_shell *data) { - if (type == PERROR) + if (data->exit_type == GOOD) + return ; + if (data->exit_type == PERROR) { perror("minishell:"); signal.exit_status = errno; } - if (type == MALLOC) + if (data->exit_type == MALLOC) { write(STDERR_FILENO, "minishell: malloc error in : ", 28); - write(STDERR_FILENO, name, ft_strlen(name)); + write(STDERR_FILENO, data->exit_msg, ft_strlen(data->exit_msg)); write(STDERR_FILENO, "\n", 1); signal.exit_status = 1; } - if (type == NOT_FOUND) + if (data->exit_type == NOT_FOUND) { write(STDERR_FILENO, "minishell: ", 11); - write(STDERR_FILENO, name, ft_strlen(name)); + write(STDERR_FILENO, data->exit_msg, ft_strlen(data->exit_msg)); write(STDERR_FILENO, ": No such file or directory\n", 28); signal.exit_status = 127; } - if (type == PERMISSION) + if (data->exit_type == PERMISSION) { write(STDERR_FILENO, "minishell: ", 11); - write(STDERR_FILENO, name, ft_strlen(name)); + write(STDERR_FILENO, data->exit_msg, ft_strlen(data->exit_msg)); write(STDERR_FILENO, ": Permission denied\n", 20); signal.exit_status = 126; } - if (type == SYNTAX) + if (data->exit_type == SYNTAX) { write(STDERR_FILENO, "minishell: syntax error near unexpected token `", 47); - write(STDERR_FILENO, name, ft_strlen(name)); + write(STDERR_FILENO, data->exit_msg, ft_strlen(data->exit_msg)); write(STDERR_FILENO, "'\n", 2); signal.exit_status = 258; } - if (type == SYNTAX_MINI) + if (data->exit_type == SYNTAX_MINI) { write(STDERR_FILENO, "minishell: unfinished operator : `", 34); - write(STDERR_FILENO, name, ft_strlen(name)); + write(STDERR_FILENO, data->exit_msg, ft_strlen(data->exit_msg)); write(STDERR_FILENO, "'\n", 2); signal.exit_status = 2; } - if (type == SIGNAL_C) + if (data->exit_type == SIGNAL_C) { write(STDERR_FILENO, "^C\n", 3); signal.exit_status = 130; } - free_shell(data, free_struct); - exit(signal.exit_status); + free_shell(data, false); } From bf94b4f9472e8d51991be0458941983af1ee564d Mon Sep 17 00:00:00 2001 From: JuliusdB Date: Thu, 7 Sep 2023 14:57:26 +0200 Subject: [PATCH 91/91] lexer changed to bool --- src/lexer/index.c | 57 ++++++++++++---------------- src/lexer/string.c | 26 ++++++++++++- src/main.c | 92 ++++++++++++++-------------------------------- 3 files changed, 77 insertions(+), 98 deletions(-) diff --git a/src/lexer/index.c b/src/lexer/index.c index 33b6e26..c746500 100644 --- a/src/lexer/index.c +++ b/src/lexer/index.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/19 13:32:55 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/06 20:36:35 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/07 14:55:59 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -93,27 +93,29 @@ static bool check_delimiters(char *str) return (true); } -/** - * @brief Creates a string from another string - * - * @param str The string to create a string from - * @param i The index of the string to create - * @param vec The vector to store the string in - * @return true The string was succesfully stored - * @return false The string could not be stored - */ -bool make_string(char *str, size_t *i, t_vector *vec) +bool lexer_check_char(char *input, int *i, t_shell *data) { - if (str[*i] == '\"' || str[*i] == '\'') + char temp[2]; + + temp[0] = '\0'; + temp[1] = '\0'; + if (checkchar(input[*i], "\"\'()") == 1) { - if (!create_quote_string(str, i, vec)) - return (false); + if (!check_delimiters(&input[*i])) + { + temp[0] = input[*i]; + return (set_err(SYNTAX_MINI, temp, data)); + } + if (!make_string(input, i, &data->token_vec)) + return (set_err(MALLOC, "lexer", data)); } - else if (str[*i] == '(') + else if (input[*i] == ' ') { - if (!create_paran_string(str, i, vec)) - return (false); + if (!create_string(input, i, &data->token_vec)) + return (set_err(MALLOC, "lexer", data)); } + else + (*i)++; return (true); } @@ -123,29 +125,18 @@ bool make_string(char *str, size_t *i, t_vector *vec) * @param input The string to split * @param data t_shell data, of which token_vec will be used */ -void lexer(char *input, t_shell *data) +bool lexer(char *input, t_shell *data) { size_t i; i = 0; while (input[i]) { - if (checkchar(input[i], "\"\'()") == 1) - { - if (!check_delimiters(&input[i])) - err(SYNTAX_MINI, input[i], data, true); - if (!make_string(input, &i, &data->token_vec)) - err(MALLOC, "malloc", data, true); - } - else if (input[i] == ' ') - { - if (!create_string(input, &i, &data->token_vec)) - err(MALLOC, "malloc", data, true); - } - else - i++; + if (!lexer_check_char(input, &i, data)) + return (false); } if (i > 0 && checkchar(input[i - 1], "\"\') ") == 0) if (!create_string(input, &i, &data->token_vec)) - err(MALLOC, "malloc", data, true); + return (set_err(MALLOC, "lexer", data)); + return (true); } diff --git a/src/lexer/string.c b/src/lexer/string.c index 80c42a6..e3c4e2a 100644 --- a/src/lexer/string.c +++ b/src/lexer/string.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/20 13:41:35 by mdekker/jde #+# #+# */ -/* Updated: 2023/08/30 22:00:47 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/07 14:50:40 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -143,3 +143,27 @@ bool create_paran_string(char *str, size_t *i, t_vector *vec) (*i)++; return (true); } + +/** + * @brief Creates a string from another string + * + * @param str The string to create a string from + * @param i The index of the string to create + * @param vec The vector to store the string in + * @return true The string was succesfully stored + * @return false The string could not be stored + */ +bool make_string(char *str, size_t *i, t_vector *vec) +{ + if (str[*i] == '\"' || str[*i] == '\'') + { + if (!create_quote_string(str, i, vec)) + return (false); + } + else if (str[*i] == '(') + { + if (!create_paran_string(str, i, vec)) + return (false); + } + return (true); +} diff --git a/src/main.c b/src/main.c index 99ee80a..4cdcb4d 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mdekker/jde-baai +#+ */ /* +#+ */ /* Created: 2023/07/12 14:11:01 by mdekker/jde #+# #+# */ -/* Updated: 2023/09/07 02:36:16 by mdekker/jde ######## odam.nl */ +/* Updated: 2023/09/07 14:40:28 by mdekker/jde ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -69,9 +69,9 @@ static void debug(void) int main(int ac, char **av, char **env) { - t_shell *data; - pid_t pid; - int status; + t_shell *data; + pid_t pid; + int status; // t_found **found; if (DEBUG) @@ -79,64 +79,28 @@ int main(int ac, char **av, char **env) data = init_shell(env, true); if (ac == 2) { - pid = fork(); - if (pid == -1) - exit_mini("initial fork failed", errno); - if (pid == 0) - { - if (!lexer(av[1], data)) - err(); - // lexer retester - parser(data); - // parser retesting - operator_split(data); - // operator split retesting - verify_token_vec(data); - // check if it really catches all doubles - // build function that combines a redirects into 1 singular token - group_token_vec(data); - // check if all groups are properly cerated - status = executor(data->exec); - free_shell(data, true); - exit(status); - } - if (waitpid(pid, &status, 0) == -1) - exit_mini("waitpid failed", errno); - if (WEXITSTATUS(status) == 10) - { - return (10); // pipe workaround - signal.exit_status = WEXITSTATUS(status); - // found = g_data.token_vec.find(&g_data.token_vec, find_strings); - // if (!found) - // { - // printf("%zu counted\n", - g_data.token_vec.count(&g_data.token_vec, - // find_strings)); - // printf("No matches found\n"); - // } - // else - // { - // printf("Printing matches:\n"); - // while (*found) - // { - // print_token((*found)->item, (*found)->index); - // free(*found); - // found++; - // } - // free(found); - // } - // if (DEBUG) - print_vector(&data->token_vec, print_token); - // print_vector(&g_data.env, print_env); - free_shell(data, true); - return (0); - } - // else if (ac == 1) - // loop(data); - else - { - printf("Too many arguments"); - free_shell(data, true); - } - return (0); + if (!lexer(av[1], data)) + write_err(data); + // lexer retester + parser(data); + // parser retesting + operator_split(data); + // operator split retesting + verify_token_vec(data); + // check if it really catches all doubles + // build function that combines a redirects+heredocs into 1 singular token + group_token_vec(data); + // check if all groups are properly cerated + status = executor(data->exec); + free_shell(data, true); + return (0); // change this to return built_in_exit } + // else if (ac == 1) + // loop(data); + else + { + printf("Too many arguments"); + free_shell(data, true); + } + return (0); +}