From 5539ca7b9445fe18e633dc34843b76fddd877992 Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Mon, 14 Feb 2022 11:59:37 +0100 Subject: [PATCH 001/163] Create main.c --- main.c | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 main.c diff --git a/main.c b/main.c new file mode 100644 index 0000000..74de8a8 --- /dev/null +++ b/main.c @@ -0,0 +1,3 @@ +int main(void) +{ +} From 60b812239003a0a9fb9fc79c0107e9f9624a40f6 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Mon, 14 Feb 2022 12:08:31 +0100 Subject: [PATCH 002/163] Maia's libft and pipex --- libraries/libft/Makefile | 92 ++++++++++++++++ libraries/libft/ft_atoi.c | 42 ++++++++ libraries/libft/ft_bzero.c | 26 +++++ libraries/libft/ft_calloc.c | 24 +++++ libraries/libft/ft_isalnum.c | 18 ++++ libraries/libft/ft_isalpha.c | 18 ++++ libraries/libft/ft_isascii.c | 18 ++++ libraries/libft/ft_isdigit.c | 18 ++++ libraries/libft/ft_isprint.c | 18 ++++ libraries/libft/ft_itoa.c | 76 +++++++++++++ libraries/libft/ft_lstadd_back.c | 28 +++++ libraries/libft/ft_lstadd_front.c | 19 ++++ libraries/libft/ft_lstclear.c | 29 +++++ libraries/libft/ft_lstdelone.c | 19 ++++ libraries/libft/ft_lstiter.c | 24 +++++ libraries/libft/ft_lstlast.c | 28 +++++ libraries/libft/ft_lstmap.c | 36 +++++++ libraries/libft/ft_lstnew.c | 25 +++++ libraries/libft/ft_lstsize.c | 26 +++++ libraries/libft/ft_memchr.c | 28 +++++ libraries/libft/ft_memcmp.c | 27 +++++ libraries/libft/ft_memcpy.c | 26 +++++ libraries/libft/ft_memmove.c | 40 +++++++ libraries/libft/ft_memset.c | 26 +++++ libraries/libft/ft_printf/ft_convert_hex.c | 41 +++++++ libraries/libft/ft_printf/ft_determine_type.c | 29 +++++ libraries/libft/ft_printf/ft_if_char.c | 25 +++++ libraries/libft/ft_printf/ft_if_hex.c | 32 ++++++ libraries/libft/ft_printf/ft_if_num.c | 71 ++++++++++++ libraries/libft/ft_printf/ft_printf.c | 38 +++++++ libraries/libft/ft_printf/ft_putstr_size.c | 31 ++++++ .../libft/ft_printf/ft_putunsignedint_fd.c | 24 +++++ libraries/libft/ft_putchar_fd.c | 18 ++++ libraries/libft/ft_putendl_fd.c | 19 ++++ libraries/libft/ft_putnbr_fd.c | 32 ++++++ libraries/libft/ft_putstr_fd.c | 27 +++++ libraries/libft/ft_split.c | 102 ++++++++++++++++++ libraries/libft/ft_strchr.c | 26 +++++ libraries/libft/ft_strdup.c | 33 ++++++ libraries/libft/ft_striteri.c | 26 +++++ libraries/libft/ft_strjoin.c | 42 ++++++++ libraries/libft/ft_strlcat.c | 36 +++++++ libraries/libft/ft_strlcpy.c | 28 +++++ libraries/libft/ft_strlen.c | 26 +++++ libraries/libft/ft_strmapi.c | 33 ++++++ libraries/libft/ft_strncmp.c | 33 ++++++ libraries/libft/ft_strnstr.c | 34 ++++++ libraries/libft/ft_strrchr.c | 38 +++++++ libraries/libft/ft_strtrim.c | 94 ++++++++++++++++ libraries/libft/ft_substr.c | 39 +++++++ libraries/libft/ft_tolower.c | 18 ++++ libraries/libft/ft_toupper.c | 18 ++++ libraries/libft/get_next_line/get_next_line.c | 72 +++++++++++++ .../libft/get_next_line/get_next_line_utils.c | 89 +++++++++++++++ libraries/libft/libft.h | 92 ++++++++++++++++ main.c | 3 - srcs/main.c | 16 +++ 57 files changed, 2013 insertions(+), 3 deletions(-) create mode 100644 libraries/libft/Makefile create mode 100644 libraries/libft/ft_atoi.c create mode 100644 libraries/libft/ft_bzero.c create mode 100644 libraries/libft/ft_calloc.c create mode 100644 libraries/libft/ft_isalnum.c create mode 100644 libraries/libft/ft_isalpha.c create mode 100644 libraries/libft/ft_isascii.c create mode 100644 libraries/libft/ft_isdigit.c create mode 100644 libraries/libft/ft_isprint.c create mode 100644 libraries/libft/ft_itoa.c create mode 100644 libraries/libft/ft_lstadd_back.c create mode 100644 libraries/libft/ft_lstadd_front.c create mode 100644 libraries/libft/ft_lstclear.c create mode 100644 libraries/libft/ft_lstdelone.c create mode 100644 libraries/libft/ft_lstiter.c create mode 100644 libraries/libft/ft_lstlast.c create mode 100644 libraries/libft/ft_lstmap.c create mode 100644 libraries/libft/ft_lstnew.c create mode 100644 libraries/libft/ft_lstsize.c create mode 100644 libraries/libft/ft_memchr.c create mode 100644 libraries/libft/ft_memcmp.c create mode 100644 libraries/libft/ft_memcpy.c create mode 100644 libraries/libft/ft_memmove.c create mode 100644 libraries/libft/ft_memset.c create mode 100644 libraries/libft/ft_printf/ft_convert_hex.c create mode 100644 libraries/libft/ft_printf/ft_determine_type.c create mode 100644 libraries/libft/ft_printf/ft_if_char.c create mode 100644 libraries/libft/ft_printf/ft_if_hex.c create mode 100644 libraries/libft/ft_printf/ft_if_num.c create mode 100644 libraries/libft/ft_printf/ft_printf.c create mode 100644 libraries/libft/ft_printf/ft_putstr_size.c create mode 100644 libraries/libft/ft_printf/ft_putunsignedint_fd.c create mode 100644 libraries/libft/ft_putchar_fd.c create mode 100644 libraries/libft/ft_putendl_fd.c create mode 100644 libraries/libft/ft_putnbr_fd.c create mode 100644 libraries/libft/ft_putstr_fd.c create mode 100644 libraries/libft/ft_split.c create mode 100644 libraries/libft/ft_strchr.c create mode 100644 libraries/libft/ft_strdup.c create mode 100644 libraries/libft/ft_striteri.c create mode 100644 libraries/libft/ft_strjoin.c create mode 100644 libraries/libft/ft_strlcat.c create mode 100644 libraries/libft/ft_strlcpy.c create mode 100644 libraries/libft/ft_strlen.c create mode 100644 libraries/libft/ft_strmapi.c create mode 100644 libraries/libft/ft_strncmp.c create mode 100644 libraries/libft/ft_strnstr.c create mode 100644 libraries/libft/ft_strrchr.c create mode 100644 libraries/libft/ft_strtrim.c create mode 100644 libraries/libft/ft_substr.c create mode 100644 libraries/libft/ft_tolower.c create mode 100644 libraries/libft/ft_toupper.c create mode 100644 libraries/libft/get_next_line/get_next_line.c create mode 100644 libraries/libft/get_next_line/get_next_line_utils.c create mode 100644 libraries/libft/libft.h delete mode 100644 main.c create mode 100644 srcs/main.c diff --git a/libraries/libft/Makefile b/libraries/libft/Makefile new file mode 100644 index 0000000..ad6751c --- /dev/null +++ b/libraries/libft/Makefile @@ -0,0 +1,92 @@ +NAME := libft.a +SRCFILES := \ + ft_atoi.c \ + ft_bzero.c \ + ft_isalnum.c \ + ft_isalpha.c \ + ft_isascii.c \ + ft_isdigit.c \ + ft_isprint.c \ + ft_memchr.c \ + ft_memcmp.c \ + ft_memcpy.c \ + ft_memmove.c \ + ft_memset.c \ + ft_strchr.c \ + ft_strlcat.c \ + ft_strlcpy.c \ + ft_strlen.c \ + ft_strncmp.c \ + ft_strnstr.c \ + ft_strrchr.c \ + ft_tolower.c \ + ft_toupper.c \ + ft_strdup.c \ + ft_calloc.c \ + ft_substr.c \ + ft_strjoin.c \ + ft_strtrim.c \ + ft_split.c \ + ft_itoa.c \ + ft_strmapi.c \ + ft_striteri.c \ + ft_putchar_fd.c \ + ft_putstr_fd.c \ + ft_putendl_fd.c \ + ft_putnbr_fd.c \ + get_next_line/get_next_line_utils.c \ + get_next_line/get_next_line.c \ + ft_printf/ft_convert_hex.c \ + ft_printf/ft_determine_type.c \ + ft_printf/ft_if_char.c \ + ft_printf/ft_if_hex.c \ + ft_printf/ft_if_num.c \ + ft_printf/ft_printf.c \ + ft_printf/ft_putstr_size.c \ + ft_printf/ft_putunsignedint_fd.c + +OBJFILES = ${SRCFILES:%.c=%.o} + +BONUS_SRCFILES := \ + ft_lstadd_front.c \ + ft_lstnew.c \ + ft_lstsize.c \ + ft_lstlast.c \ + ft_lstadd_back.c \ + ft_lstdelone.c \ + ft_lstclear.c \ + ft_lstiter.c \ + ft_lstmap.c + +BONUS_OBJFILES = ${BONUS_SRCFILES:%.c=%.o} + +HEADERFILES := libft.h + +CFLAGS = -Wall -Wextra -Werror + +ifdef WITH_BONUS +ALL_OBJS = $(OBJFILES) $(BONUS_OBJFILES) +else +ALL_OBJS = $(OBJFILES) +endif + +all: libft.a + +%.o: %.c $(HEADERFILES) + $(CC) -c $(CFLAGS) -o $@ $< + +$(NAME): $(ALL_OBJS) + ar -ru $(NAME) $^ + +bonus: + $(MAKE) WITH_BONUS=1 + +clean: + rm -f $(OBJFILES) $(BONUS_OBJFILES) + +fclean: clean + rm -f $(NAME) $^ + +re: fclean all + +.PHONY: clean fclean re bonus \ No newline at end of file diff --git a/libraries/libft/ft_atoi.c b/libraries/libft/ft_atoi.c new file mode 100644 index 0000000..52f3b1f --- /dev/null +++ b/libraries/libft/ft_atoi.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_atoi.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:18:44 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:18:46 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_atoi(const char *str) +{ + int ret; + int p_n; + int i; + + i = 0; + ret = 0; + p_n = 1; + while (*str == ' ' || (*str > 8 && *str < 14)) + str++; + if (*str == '-' || *str == '+') + { + if (*str == '-') + p_n *= -1; + str++; + } + while (ft_isdigit(str[i]) == 1) + { + ret = (ret * 10) + (str[i] - '0'); + i++; + if (i == 10 && str[i] && p_n == -1) + return (0); + else if (i == 10 && str[i]) + return (-1); + } + return (ret * p_n); +} diff --git a/libraries/libft/ft_bzero.c b/libraries/libft/ft_bzero.c new file mode 100644 index 0000000..17b77d8 --- /dev/null +++ b/libraries/libft/ft_bzero.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_bzero.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:18:49 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:18:51 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_bzero(void *str, size_t n) +{ + size_t i; + + i = 0; + while (i < n) + { + ((char *)str)[i] = '\0'; + i++; + } + return (str); +} diff --git a/libraries/libft/ft_calloc.c b/libraries/libft/ft_calloc.c new file mode 100644 index 0000000..46b7d2b --- /dev/null +++ b/libraries/libft/ft_calloc.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_calloc.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:18:54 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:21:22 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_calloc(size_t count, size_t size) +{ + int *ret; + + ret = malloc(count * size); + if (!ret) + return (0); + ft_bzero(ret, count * size); + return (ret); +} diff --git a/libraries/libft/ft_isalnum.c b/libraries/libft/ft_isalnum.c new file mode 100644 index 0000000..5279a1d --- /dev/null +++ b/libraries/libft/ft_isalnum.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_isalnum.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:21:27 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:21:29 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +int ft_isalnum(int c) +{ + if ((c > 64 && c < 91) || (c > 96 && c < 123) || (c > 47 && c < 58)) + return (1); + return (0); +} diff --git a/libraries/libft/ft_isalpha.c b/libraries/libft/ft_isalpha.c new file mode 100644 index 0000000..850a05b --- /dev/null +++ b/libraries/libft/ft_isalpha.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_isalpha.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:21:33 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:21:35 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +int ft_isalpha(int c) +{ + if ((c > 64 && c < 91) || (c > 96 && c < 123)) + return (1); + return (0); +} diff --git a/libraries/libft/ft_isascii.c b/libraries/libft/ft_isascii.c new file mode 100644 index 0000000..57df49f --- /dev/null +++ b/libraries/libft/ft_isascii.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_isascii.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:21:38 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:21:40 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +int ft_isascii(int c) +{ + if (c >= 0 && c <= 127) + return (1); + return (0); +} diff --git a/libraries/libft/ft_isdigit.c b/libraries/libft/ft_isdigit.c new file mode 100644 index 0000000..45865b3 --- /dev/null +++ b/libraries/libft/ft_isdigit.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_isdigit.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:21:44 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:21:47 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +int ft_isdigit(int c) +{ + if (c > 47 && c < 58) + return (1); + return (0); +} diff --git a/libraries/libft/ft_isprint.c b/libraries/libft/ft_isprint.c new file mode 100644 index 0000000..9a7b422 --- /dev/null +++ b/libraries/libft/ft_isprint.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_isprint.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:21:50 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:21:53 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +int ft_isprint(int c) +{ + if (c >= 32 && c < 127) + return (1); + return (0); +} diff --git a/libraries/libft/ft_itoa.c b/libraries/libft/ft_itoa.c new file mode 100644 index 0000000..4d725f2 --- /dev/null +++ b/libraries/libft/ft_itoa.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_itoa.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:21:55 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:21:59 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int determine_length(long long n, int neg_or_pos) +{ + int i; + + i = 0; + if (n == 0) + i++; + while (n > 0) + { + n = n / 10; + i++; + } + if (neg_or_pos) + i++; + return (i); +} + +char *fill_arr(long long n, char *arr, int i, int neg_or_pos) +{ + arr[i] = '\0'; + i--; + if (n == 0) + { + arr[i] = '0'; + i--; + } + while (n > 0) + { + arr[i] = (n % 10) + '0'; + i--; + n = n / 10; + } + if (neg_or_pos) + { + arr[i] = '-'; + i++; + } + return (arr); +} + +char *ft_itoa(int n) +{ + int i; + int neg_or_pos; + char *arr; + long long j; + + neg_or_pos = 0; + if (n < 0) + { + neg_or_pos = 1; + j = (long long)n * -1; + } + else + j = (long long)n; + i = determine_length(j, neg_or_pos); + arr = malloc(i * sizeof(char) + 1); + if (!arr) + return (0); + arr = fill_arr(j, arr, i, neg_or_pos); + return (arr); +} diff --git a/libraries/libft/ft_lstadd_back.c b/libraries/libft/ft_lstadd_back.c new file mode 100644 index 0000000..41566c1 --- /dev/null +++ b/libraries/libft/ft_lstadd_back.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_lstadd_back.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:22:02 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:22:04 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_back(t_list **lst, t_list *new) +{ + t_list *tmp; + + tmp = *lst; + if (!(*lst)) + { + *lst = new; + return ; + } + while (tmp->next != NULL) + tmp = tmp->next; + tmp->next = new; +} diff --git a/libraries/libft/ft_lstadd_front.c b/libraries/libft/ft_lstadd_front.c new file mode 100644 index 0000000..2ad59f0 --- /dev/null +++ b/libraries/libft/ft_lstadd_front.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_lstadd_front.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:22:07 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:22:09 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstadd_front(t_list **lst, t_list *new) +{ + new->next = *lst; + *lst = new; +} diff --git a/libraries/libft/ft_lstclear.c b/libraries/libft/ft_lstclear.c new file mode 100644 index 0000000..626b8b8 --- /dev/null +++ b/libraries/libft/ft_lstclear.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_lstclear.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:22:13 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:22:16 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstclear(t_list **lst, void (*del)(void*)) +{ + t_list *tmp; + + if (!*lst || !del) + return ; + while (*lst) + { + tmp = (*lst)->next; + del((*lst)->content); + free(*lst); + *lst = tmp; + } + *lst = NULL; +} diff --git a/libraries/libft/ft_lstdelone.c b/libraries/libft/ft_lstdelone.c new file mode 100644 index 0000000..13dd4f0 --- /dev/null +++ b/libraries/libft/ft_lstdelone.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_lstdelone.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:22:18 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:22:20 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstdelone(t_list *lst, void (*del)(void*)) +{ + del(lst->content); + free(lst); +} diff --git a/libraries/libft/ft_lstiter.c b/libraries/libft/ft_lstiter.c new file mode 100644 index 0000000..b215811 --- /dev/null +++ b/libraries/libft/ft_lstiter.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_lstiter.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:22:22 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:22:24 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstiter(t_list *lst, void (*f)(void *)) +{ + if (!lst) + return ; + while (lst) + { + f(lst->content); + lst = lst->next; + } +} diff --git a/libraries/libft/ft_lstlast.c b/libraries/libft/ft_lstlast.c new file mode 100644 index 0000000..aac4bc4 --- /dev/null +++ b/libraries/libft/ft_lstlast.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_lstlast.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:22:27 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:22:29 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstlast(t_list *lst) +{ + int i; + + i = 0; + if (!lst) + return (NULL); + while (lst->next != NULL) + { + lst = lst->next; + i++; + } + return (lst); +} diff --git a/libraries/libft/ft_lstmap.c b/libraries/libft/ft_lstmap.c new file mode 100644 index 0000000..9103be3 --- /dev/null +++ b/libraries/libft/ft_lstmap.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_lstmap.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:22:33 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:22:35 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) +{ + t_list *new_list; + t_list *head; + + head = NULL; + if (!lst || !f) + return (head); + while (lst) + { + new_list = ft_lstnew(f(lst->content)); + if (new_list) + ft_lstadd_back(&head, new_list); + else + { + ft_lstclear(&head, del); + return (NULL); + } + lst = lst->next; + } + return (head); +} diff --git a/libraries/libft/ft_lstnew.c b/libraries/libft/ft_lstnew.c new file mode 100644 index 0000000..2978ad8 --- /dev/null +++ b/libraries/libft/ft_lstnew.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_lstnew.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:22:38 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:22:40 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *ft_lstnew(void *content) +{ + struct s_list *new_element; + + new_element = (struct s_list *)malloc(sizeof(struct s_list)); + if (!new_element) + return (0); + new_element->content = content; + new_element->next = NULL; + return (new_element); +} diff --git a/libraries/libft/ft_lstsize.c b/libraries/libft/ft_lstsize.c new file mode 100644 index 0000000..eaf9e69 --- /dev/null +++ b/libraries/libft/ft_lstsize.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_lstsize.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:22:43 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:22:45 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_lstsize(t_list *lst) +{ + int i; + + i = 0; + while (lst != NULL) + { + lst = lst->next; + i++; + } + return (i); +} diff --git a/libraries/libft/ft_memchr.c b/libraries/libft/ft_memchr.c new file mode 100644 index 0000000..a09fcf5 --- /dev/null +++ b/libraries/libft/ft_memchr.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_memchr.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:22:48 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:22:50 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memchr(const void *s, int c, size_t n) +{ + int i; + + i = 0; + while (n > 0) + { + if (((unsigned char *)s)[i] == (unsigned char)c) + return (&(((unsigned char *)s)[i])); + n--; + i++; + } + return (0); +} diff --git a/libraries/libft/ft_memcmp.c b/libraries/libft/ft_memcmp.c new file mode 100644 index 0000000..9f52978 --- /dev/null +++ b/libraries/libft/ft_memcmp.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_memcmp.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:22:53 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:22:56 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_memcmp(const void *s1, const void *s2, size_t n) +{ + size_t i; + + i = 0; + while (i < n) + { + if ((((unsigned char *)s1)[i] != ((unsigned char *)s2)[i])) + return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]); + i++; + } + return (0); +} diff --git a/libraries/libft/ft_memcpy.c b/libraries/libft/ft_memcpy.c new file mode 100644 index 0000000..3ab646d --- /dev/null +++ b/libraries/libft/ft_memcpy.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_memcpy.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:22:59 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:23:03 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memcpy(void *dst, const void *src, size_t n) +{ + size_t i; + + i = 0; + while (i < n && (((char *)src) || ((char *)dst))) + { + ((char *)dst)[i] = ((char *)src)[i]; + i++; + } + return ((void *)dst); +} diff --git a/libraries/libft/ft_memmove.c b/libraries/libft/ft_memmove.c new file mode 100644 index 0000000..da62737 --- /dev/null +++ b/libraries/libft/ft_memmove.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_memmove.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:23:07 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:23:08 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memmove(void *dst, const void *src, size_t n) +{ + size_t i; + + i = 0; + if (src == 0 && dst == 0) + return (0); + if (dst < src) + { + while (n) + { + ((char *)dst)[i] = ((char *)src)[i]; + i++; + n--; + } + } + else + { + while (n) + { + ((char *)dst)[n - 1] = ((char *)src)[n - 1]; + n--; + } + } + return (dst); +} diff --git a/libraries/libft/ft_memset.c b/libraries/libft/ft_memset.c new file mode 100644 index 0000000..075fd82 --- /dev/null +++ b/libraries/libft/ft_memset.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_memset.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:23:22 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:23:23 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void *ft_memset(void *str, int c, size_t n) +{ + size_t i; + + i = 0; + while (i < n) + { + ((char *)str)[i] = c; + i++; + } + return ((void *)str); +} diff --git a/libraries/libft/ft_printf/ft_convert_hex.c b/libraries/libft/ft_printf/ft_convert_hex.c new file mode 100644 index 0000000..1dde12c --- /dev/null +++ b/libraries/libft/ft_printf/ft_convert_hex.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_convert_hex.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:17:31 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:17:33 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void ft_print_hex(unsigned long n, char *up_or_low) +{ + if (n != 0) + { + ft_print_hex(n / 16, up_or_low); + ft_putchar_fd(up_or_low[n % 16], 1); + } +} + +int ft_convert_hex(unsigned long n, char *up_or_low) +{ + int i; + + i = 0; + if (n == 0) + { + ft_putchar_fd(up_or_low[n], 1); + return (1); + } + ft_print_hex(n, up_or_low); + while (n != 0) + { + n /= 16; + i++; + } + return (i); +} diff --git a/libraries/libft/ft_printf/ft_determine_type.c b/libraries/libft/ft_printf/ft_determine_type.c new file mode 100644 index 0000000..aa1a54b --- /dev/null +++ b/libraries/libft/ft_printf/ft_determine_type.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_determine_type.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:17:36 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:17:39 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_determine_type(char c, va_list ap) +{ + if (c == '%') + { + ft_putchar_fd('%', 1); + return (1); + } + if (c == 'c' || c == 's') + return (ft_if_char(c, ap)); + if (c == 'd' || c == 'i' || c == 'u') + return (ft_if_num(c, ap)); + if (c == 'x' || c == 'X' || c == 'p') + return (ft_if_hex(c, ap)); + return (0); +} diff --git a/libraries/libft/ft_printf/ft_if_char.c b/libraries/libft/ft_printf/ft_if_char.c new file mode 100644 index 0000000..dff4921 --- /dev/null +++ b/libraries/libft/ft_printf/ft_if_char.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_if_char.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:17:46 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:17:49 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_if_char(char c, va_list ap) +{ + if (c == 'c') + { + ft_putchar_fd(va_arg(ap, int), 1); + return (1); + } + if (c == 's') + return (ft_putstr_size(va_arg(ap, char *), 1)); + return (0); +} diff --git a/libraries/libft/ft_printf/ft_if_hex.c b/libraries/libft/ft_printf/ft_if_hex.c new file mode 100644 index 0000000..e8e684d --- /dev/null +++ b/libraries/libft/ft_printf/ft_if_hex.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_if_hex.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:17:54 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:17:56 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_if_hex(char c, va_list ap) +{ + char *up; + char *low; + + up = "0123456789ABCDEF"; + low = "0123456789abcdef"; + if (c == 'x') + return (ft_convert_hex(va_arg(ap, unsigned int), low)); + if (c == 'X') + return (ft_convert_hex(va_arg(ap, unsigned int), up)); + if (c == 'p') + { + ft_putstr_fd("0x", 1); + return (2 + ft_convert_hex(va_arg(ap, unsigned long), low)); + } + return (0); +} diff --git a/libraries/libft/ft_printf/ft_if_num.c b/libraries/libft/ft_printf/ft_if_num.c new file mode 100644 index 0000000..5c59a32 --- /dev/null +++ b/libraries/libft/ft_printf/ft_if_num.c @@ -0,0 +1,71 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_if_num.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:18:00 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:18:02 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_determine_unsigned_nbr_size(unsigned int nbr) +{ + int i; + + i = 0; + if (nbr == 0) + i++; + while (nbr > 0) + { + nbr = nbr / 10; + i++; + } + return (i); +} + +int ft_determine_nbr_size(long long nbr) +{ + int i; + int neg_or_pos; + + neg_or_pos = 0; + i = 0; + if (nbr == 0) + i++; + if (nbr < 0) + { + neg_or_pos = 1; + nbr *= -1; + } + while (nbr > 0) + { + nbr = nbr / 10; + i++; + } + if (neg_or_pos) + i++; + return (i); +} + +int ft_if_num(char c, va_list ap) +{ + long long nbr; + + if (c == 'i' || c == 'd') + { + nbr = va_arg(ap, int); + ft_putnbr_fd(nbr, 1); + return (ft_determine_nbr_size(nbr)); + } + if (c == 'u') + { + nbr = va_arg(ap, unsigned int); + ft_putunsignedint_fd(nbr, 1); + return (ft_determine_unsigned_nbr_size(nbr)); + } + return (0); +} diff --git a/libraries/libft/ft_printf/ft_printf.c b/libraries/libft/ft_printf/ft_printf.c new file mode 100644 index 0000000..9b483a5 --- /dev/null +++ b/libraries/libft/ft_printf/ft_printf.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_printf.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:18:06 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:18:08 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_printf(const char *format, ...) +{ + va_list ap; + int len; + + len = 0; + va_start(ap, format); + while (*format != '\0') + { + if (*format == '%') + { + len += ft_determine_type(format[1], ap); + format++; + } + else + { + ft_putchar_fd(*format, 1); + len++; + } + format++; + } + va_end(ap); + return (len); +} diff --git a/libraries/libft/ft_printf/ft_putstr_size.c b/libraries/libft/ft_printf/ft_putstr_size.c new file mode 100644 index 0000000..6f36b23 --- /dev/null +++ b/libraries/libft/ft_printf/ft_putstr_size.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_putstr_size.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:18:10 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:18:13 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_putstr_size(char *s, int fd) +{ + int i; + + if (!s) + { + ft_putstr_fd("(null)", fd); + return (6); + } + i = 0; + while (s[i]) + { + ft_putchar_fd(s[i], fd); + i++; + } + return (i); +} diff --git a/libraries/libft/ft_printf/ft_putunsignedint_fd.c b/libraries/libft/ft_printf/ft_putunsignedint_fd.c new file mode 100644 index 0000000..185d04a --- /dev/null +++ b/libraries/libft/ft_printf/ft_putunsignedint_fd.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_putunsignedint_fd.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:18:16 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:18:20 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +void ft_putunsignedint_fd(unsigned int n, int fd) +{ + if (n < 10) + ft_putchar_fd(n + '0', fd); + if (n >= 10) + { + ft_putnbr_fd(n / 10, fd); + ft_putchar_fd((n % 10) + '0', fd); + } +} diff --git a/libraries/libft/ft_putchar_fd.c b/libraries/libft/ft_putchar_fd.c new file mode 100644 index 0000000..6b03899 --- /dev/null +++ b/libraries/libft/ft_putchar_fd.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_putchar_fd.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:23:26 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:23:28 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putchar_fd(char c, int fd) +{ + write(fd, &c, 1); +} diff --git a/libraries/libft/ft_putendl_fd.c b/libraries/libft/ft_putendl_fd.c new file mode 100644 index 0000000..4345a79 --- /dev/null +++ b/libraries/libft/ft_putendl_fd.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_putendl_fd.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:23:31 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:23:33 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putendl_fd(char *s, int fd) +{ + ft_putstr_fd(s, fd); + ft_putchar_fd('\n', fd); +} diff --git a/libraries/libft/ft_putnbr_fd.c b/libraries/libft/ft_putnbr_fd.c new file mode 100644 index 0000000..439c554 --- /dev/null +++ b/libraries/libft/ft_putnbr_fd.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_putnbr_fd.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:23:40 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:23:42 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putnbr_fd(int n, int fd) +{ + long long j; + + j = (long long)n; + if (j < 0) + { + ft_putchar_fd('-', fd); + j *= -1; + } + if (j < 10) + ft_putchar_fd(j + '0', fd); + if (j >= 10) + { + ft_putnbr_fd(j / 10, fd); + ft_putchar_fd((j % 10) + '0', fd); + } +} diff --git a/libraries/libft/ft_putstr_fd.c b/libraries/libft/ft_putstr_fd.c new file mode 100644 index 0000000..8982b1e --- /dev/null +++ b/libraries/libft/ft_putstr_fd.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_putstr_fd.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:23:46 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:23:47 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_putstr_fd(char *s, int fd) +{ + int i; + + if (!s) + return ; + i = 0; + while (s[i]) + { + ft_putchar_fd(s[i], fd); + i++; + } +} diff --git a/libraries/libft/ft_split.c b/libraries/libft/ft_split.c new file mode 100644 index 0000000..e19705d --- /dev/null +++ b/libraries/libft/ft_split.c @@ -0,0 +1,102 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_split.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:23:50 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:23:52 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int count_c(char const *s, char c) +{ + size_t i; + int c_count; + + i = 0; + c_count = 0; + while (i < ft_strlen(s)) + { + if (s[i] != c) + c_count++; + while (s[i] != c && i < ft_strlen(s)) + i++; + i++; + } + return (c_count); +} + +int strlen_c(char const *s, char c, int s_i) +{ + int i; + + i = 0; + while (s[s_i] != c && s[s_i] != '\0') + { + s_i++; + i++; + } + return (i); +} + +char **create_arr(char const *s, char c, int c_count, char **split_arr) +{ + int s_i; + int s_a_i; + int num_of_char; + + s_i = 0; + s_a_i = 0; + num_of_char = 0; + while (s_a_i < c_count) + { + while (s[s_i] == c) + s_i++; + num_of_char = strlen_c(s, c, s_i); + split_arr[s_a_i] = (char *)ft_calloc(num_of_char + 1, sizeof(char)); + if (!split_arr) + return (NULL); + ft_strlcpy(split_arr[s_a_i], &s[s_i], num_of_char + 1); + s_i += num_of_char; + s_a_i++; + } + return (split_arr); +} + +void free_arr(char **split_arr) +{ + int i; + + i = 0; + while (split_arr[i]) + { + free (split_arr[i]); + i++; + } +} + +char **ft_split(char const *s, char c) +{ + int c_count; + char **split_arr; + + if (!s) + return (NULL); + c_count = count_c(s, c); + split_arr = ft_calloc(c_count + 1, sizeof(char *)); + if (!split_arr) + return (NULL); + if (!s || c_count == 0) + return (split_arr); + if (!create_arr(s, c, c_count, split_arr)) + { + free_arr(split_arr); + free (split_arr); + return (NULL); + } + return (split_arr); +} diff --git a/libraries/libft/ft_strchr.c b/libraries/libft/ft_strchr.c new file mode 100644 index 0000000..9260341 --- /dev/null +++ b/libraries/libft/ft_strchr.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_strchr.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:23:55 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:23:57 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strchr(const char *s, int c) +{ + while (*s) + { + if (*s == (unsigned char)c) + return ((char *)s); + s++; + } + if (*s == (unsigned char)c) + return ((char *)s); + return (0); +} diff --git a/libraries/libft/ft_strdup.c b/libraries/libft/ft_strdup.c new file mode 100644 index 0000000..5b93802 --- /dev/null +++ b/libraries/libft/ft_strdup.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_strdup.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:24:00 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:24:02 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strdup(const char *s1) +{ + char *s2; + size_t size; + size_t i; + + i = 0; + size = ft_strlen(s1); + s2 = (char *)malloc((size * sizeof(char)) + 1); + if (!s2) + return (0); + while (i < size) + { + s2[i] = s1[i]; + i++; + } + s2[i] = '\0'; + return (s2); +} diff --git a/libraries/libft/ft_striteri.c b/libraries/libft/ft_striteri.c new file mode 100644 index 0000000..57dc61f --- /dev/null +++ b/libraries/libft/ft_striteri.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_striteri.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:24:06 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:24:09 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +void ft_striteri(char *s, void (*f)(unsigned int, char*)) +{ + int i; + + i = 0; + if ((!s || !f) || (!s && !f)) + return ; + while (s[i]) + { + f(i, &s[i]); + i++; + } + s[i] = '\0'; +} diff --git a/libraries/libft/ft_strjoin.c b/libraries/libft/ft_strjoin.c new file mode 100644 index 0000000..da4e92f --- /dev/null +++ b/libraries/libft/ft_strjoin.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_strjoin.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:24:11 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:24:13 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strjoin(char const *s1, char const *s2) +{ + int len; + char *s1_2; + int i; + + if ((!s1 || !s2) || (!s1 && !s2)) + return (0); + len = ft_strlen(s1) + ft_strlen(s2); + s1_2 = (char *)malloc(len * sizeof(char) + 1); + i = 0; + if (!s1_2) + return (0); + while (*s1) + { + s1_2[i] = *s1; + s1++; + i++; + } + while (*s2) + { + s1_2[i] = *s2; + s2++; + i++; + } + s1_2[i] = '\0'; + return (s1_2); +} diff --git a/libraries/libft/ft_strlcat.c b/libraries/libft/ft_strlcat.c new file mode 100644 index 0000000..802d69e --- /dev/null +++ b/libraries/libft/ft_strlcat.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_strlcat.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:24:16 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:24:18 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcat(char *dst, const char *src, size_t dstsize) +{ + size_t i; + size_t j; + size_t len_dst; + + i = 0; + j = 0; + len_dst = ft_strlen(dst); + i = len_dst; + if (len_dst > dstsize) + return (dstsize + ft_strlen(src)); + while (src[j] && i < (dstsize - 1) && dstsize > 0) + { + dst[i] = src[j]; + i++; + j++; + } + if (dstsize > 0) + dst[i] = '\0'; + return (len_dst + ft_strlen(src)); +} diff --git a/libraries/libft/ft_strlcpy.c b/libraries/libft/ft_strlcpy.c new file mode 100644 index 0000000..9e4a381 --- /dev/null +++ b/libraries/libft/ft_strlcpy.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_strlcpy.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:24:21 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:24:23 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlcpy(char *dst, const char *src, size_t dstsize) +{ + size_t i; + + i = 0; + while ((i < (dstsize - 1)) && src[i] && dstsize > 0) + { + dst[i] = src[i]; + i++; + } + if (dstsize != 0) + dst[i] = '\0'; + return (ft_strlen(src)); +} diff --git a/libraries/libft/ft_strlen.c b/libraries/libft/ft_strlen.c new file mode 100644 index 0000000..c6b4e26 --- /dev/null +++ b/libraries/libft/ft_strlen.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_strlen.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:24:28 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:24:29 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +size_t ft_strlen(const char *str) +{ + int i; + + i = 0; + while (*str) + { + str++; + i++; + } + return (i); +} diff --git a/libraries/libft/ft_strmapi.c b/libraries/libft/ft_strmapi.c new file mode 100644 index 0000000..8d64314 --- /dev/null +++ b/libraries/libft/ft_strmapi.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_strmapi.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:24:33 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:24:45 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + int i; + char *ret; + + if (!s) + return (0); + ret = (char *)malloc((ft_strlen(s) + 1) * sizeof(char)); + if (!ret) + return (0); + i = 0; + while (s[i]) + { + ret[i] = f(i, s[i]); + i++; + } + ret[i] = '\0'; + return (ret); +} diff --git a/libraries/libft/ft_strncmp.c b/libraries/libft/ft_strncmp.c new file mode 100644 index 0000000..56ebae0 --- /dev/null +++ b/libraries/libft/ft_strncmp.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_strncmp.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:24:48 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:24:50 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_strncmp(const char *s1, const char *s2, size_t n) +{ + size_t i; + + i = 0; + while (i < n && s2[i] != '\0') + { + if ((((unsigned char *)s1)[i] != ((unsigned char *)s2)[i])) + return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]); + i++; + } + while (i < n && s1[i] != '\0') + { + if ((((unsigned char *)s1)[i] != ((unsigned char *)s2)[i])) + return (((unsigned char *)s1)[i] - ((unsigned char *)s2)[i]); + i++; + } + return (0); +} diff --git a/libraries/libft/ft_strnstr.c b/libraries/libft/ft_strnstr.c new file mode 100644 index 0000000..d71f062 --- /dev/null +++ b/libraries/libft/ft_strnstr.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_strnstr.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:24:54 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:24:55 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strnstr(const char *haystack, const char *needle, size_t len) +{ + int i; + + if (needle[0] == '\0') + return ((char *)haystack); + while (*haystack && len > 0) + { + i = 0; + while (haystack[i] == needle[i] && (len - i) > 0) + { + i++; + if (needle[i] == '\0') + return ((char *)haystack); + } + haystack++; + len--; + } + return (0); +} diff --git a/libraries/libft/ft_strrchr.c b/libraries/libft/ft_strrchr.c new file mode 100644 index 0000000..f3ef142 --- /dev/null +++ b/libraries/libft/ft_strrchr.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_strrchr.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:24:59 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:25:00 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + int i; + + i = ft_strlen(s) - 1; + while (i >= 0) + { + if (s[i] == (unsigned char)c) + { + while (i > 0) + { + s++; + i--; + } + return ((char *)s); + } + i--; + } + while (*s) + s++; + if (*s == (unsigned char)c) + return ((char *)s); + return (0); +} diff --git a/libraries/libft/ft_strtrim.c b/libraries/libft/ft_strtrim.c new file mode 100644 index 0000000..649ac05 --- /dev/null +++ b/libraries/libft/ft_strtrim.c @@ -0,0 +1,94 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_strtrim.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:25:05 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:25:11 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int begin_chars(char const *s1, char const *set) +{ + int i; + int j; + int char_at_begin; + + j = 0; + char_at_begin = 0; + while (s1[j]) + { + i = 0; + while (set[i]) + { + if (s1[j] == set[i]) + { + char_at_begin++; + break ; + } + i++; + } + if (set[i] == '\0') + return (char_at_begin); + j++; + } + return (char_at_begin); +} + +int end_chars(char const *s1, char const *set, int len) +{ + int i; + int char_at_end; + + len -= 1; + char_at_end = 0; + while (len >= 0) + { + i = 0; + while (set[i]) + { + if (s1[len] == set[i]) + { + char_at_end++; + break ; + } + i++; + } + if (set[i] == '\0') + return (char_at_end); + len--; + } + return (char_at_end); +} + +char *ft_strtrim(char const *s1, char const *set) +{ + int char_at_begin; + int new_len; + int len_s1; + char *trimmed; + int i; + + if ((!s1 || !set) || (!s1 && !set)) + return (0); + i = 0; + char_at_begin = begin_chars(s1, set); + len_s1 = ft_strlen(s1); + new_len = len_s1 - (char_at_begin + end_chars(s1, set, len_s1)); + if (new_len <= 0) + new_len = 1; + trimmed = malloc((new_len * sizeof(char)) + 1); + if (!trimmed) + return (0); + while (i < new_len) + { + trimmed[i] = s1[char_at_begin + i]; + i++; + } + trimmed[i] = '\0'; + return (trimmed); +} diff --git a/libraries/libft/ft_substr.c b/libraries/libft/ft_substr.c new file mode 100644 index 0000000..9e18787 --- /dev/null +++ b/libraries/libft/ft_substr.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_substr.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:25:16 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:25:18 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + unsigned int i; + char *substr; + size_t strln; + + strln = ft_strlen(s); + if (!s) + return (0); + if (start > strln) + return (ft_strdup("")); + if (len > strln - start) + return (ft_strdup(s + start)); + i = 0; + substr = (char *)malloc((len * sizeof(char)) + 1); + if (!substr) + return (0); + while (i < len) + { + substr[i] = s[start + i]; + i++; + } + substr[i] = '\0'; + return (substr); +} diff --git a/libraries/libft/ft_tolower.c b/libraries/libft/ft_tolower.c new file mode 100644 index 0000000..0279f06 --- /dev/null +++ b/libraries/libft/ft_tolower.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_tolower.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:25:21 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:25:22 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +int ft_tolower(int c) +{ + if (c > 64 && c < 91) + return (c + 32); + return (c); +} diff --git a/libraries/libft/ft_toupper.c b/libraries/libft/ft_toupper.c new file mode 100644 index 0000000..5ccf7f5 --- /dev/null +++ b/libraries/libft/ft_toupper.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_toupper.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:25:25 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:25:27 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +int ft_toupper(int c) +{ + if (c > 96 && c < 123) + return (c - 32); + return (c); +} diff --git a/libraries/libft/get_next_line/get_next_line.c b/libraries/libft/get_next_line/get_next_line.c new file mode 100644 index 0000000..45019e0 --- /dev/null +++ b/libraries/libft/get_next_line/get_next_line.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* get_next_line.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:18:35 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:18:37 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_check_nl(char *input) +{ + int i; + + i = 0; + if (!input) + return (0); + while (input[i]) + { + if (input[i] == '\n') + return (++i); + i++; + } + return (0); +} + +char *ft_send_join(char *s1, char *s2) +{ + if (!s1) + s1 = ft_strdup(""); + if (!s1 && !s2) + return (NULL); + return (ft_g_strjoin(s1, s2)); +} + +char *read_buff(int fd, char *saved) +{ + char buff[11]; + int i; + + i = 1; + while (i > 0) + { + i = read(fd, buff, 10); + if (i >= 0) + buff[i] = '\0'; + if (i < 0) + return (NULL); + saved = ft_send_join(saved, buff); + if (ft_check_nl(saved) > 0) + break ; + } + return (saved); +} + +char *get_next_line(int fd) +{ + static char *saved; + char *ret; + + if (ft_check_nl(saved) == 0) + saved = read_buff(fd, saved); + if (!saved) + return (NULL); + ret = ft_create_line(saved); + saved = ft_remove_line(saved); + return (ret); +} diff --git a/libraries/libft/get_next_line/get_next_line_utils.c b/libraries/libft/get_next_line/get_next_line_utils.c new file mode 100644 index 0000000..9293c45 --- /dev/null +++ b/libraries/libft/get_next_line/get_next_line_utils.c @@ -0,0 +1,89 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* get_next_line_utils.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:18:30 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:18:32 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +char *ft_g_strjoin(char *s1, char *s2) +{ + char *s1_2; + int i; + int j; + + s1_2 = (char *)malloc((ft_strlen(s1) + ft_strlen(s2)) * sizeof(char) + 1); + if (!s1_2) + return (0); + i = 0; + j = 0; + while (s1[i]) + { + s1_2[i] = s1[i]; + i++; + } + while (s2[j]) + { + s1_2[i + j] = s2[j]; + j++; + } + s1_2[i + j] = '\0'; + free(s1); + return (s1_2); +} + +char *ft_create_line(char *saved) +{ + int line_len; + int i; + char *temp; + + i = 0; + if (!saved[i]) + return (NULL); + line_len = ft_check_nl(saved); + if (line_len == 0) + line_len = ft_strlen(saved); + temp = malloc (line_len * sizeof(char) + 1); + if (!temp) + return (NULL); + while (i < line_len && saved[i]) + { + temp[i] = saved[i]; + i++; + } + temp[i] = '\0'; + return (temp); +} + +char *ft_remove_line(char *saved) +{ + int line_len; + int i; + char *temp; + + i = 0; + line_len = ft_check_nl(saved); + if (!line_len) + { + free(saved); + return (NULL); + } + temp = malloc(sizeof(char) * (ft_strlen(saved) - line_len + 1)); + if (!temp) + return (NULL); + while (saved[line_len + i]) + { + temp[i] = saved[line_len + i]; + i++; + } + temp[i] = '\0'; + free(saved); + return (temp); +} diff --git a/libraries/libft/libft.h b/libraries/libft/libft.h new file mode 100644 index 0000000..fd5d06c --- /dev/null +++ b/libraries/libft/libft.h @@ -0,0 +1,92 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* libft.h :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:25:29 by mgraaf #+# #+# */ +/* Updated: 2021/12/16 14:25:31 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#ifndef LIBFT_H +# define LIBFT_H +# include +# include +# include + +typedef struct s_list +{ + void *content; + struct s_list *next; +} t_list; + +int ft_isalpha(int c); +int ft_isdigit(int c); +int ft_isalnum(int c); +int ft_isascii(int c); +int ft_isprint(int c); +size_t ft_strlen(const char *str); +void *ft_memset(void *str, int c, size_t n); +void *ft_bzero(void *str, size_t n); +int ft_toupper(int c); +int ft_tolower(int c); +void *ft_memcpy(void *dst, const void *src, size_t n); +void *ft_memmove(void *dst, const void *src, size_t n); +char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); +int ft_strncmp(const char *s1, const char *s2, size_t n); +int ft_memcmp(const void *s1, const void *s2, size_t n); +void *ft_memchr(const void *s, int c, size_t n); +char *ft_strnstr(const char *haystack, const char *needle, size_t len); +int ft_atoi(const char *str); +size_t ft_strlcpy(char *dst, const char *src, size_t dstsize); +size_t ft_strlcat(char *dst, const char *src, size_t dstsize); +void *ft_calloc(size_t count, size_t size); +char *ft_strdup(const char *s1); +char *ft_substr(char const *s, unsigned int start, size_t len); +char *ft_strjoin(char const *s1, char const *s2); +char *ft_strtrim(char const *s1, char const *set); +char **ft_split(char const *s, char c); +void free_arr(char **split_arr); +char *ft_itoa(int n); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); +void ft_striteri(char *s, void (*f)(unsigned int, char*)); +void ft_putchar_fd(char c, int fd); +void ft_putstr_fd(char *s, int fd); +void ft_putendl_fd(char *s, int fd); +void ft_putnbr_fd(int n, int fd); +t_list *ft_lstnew(void *content); +void ft_lstadd_front(t_list **lst, t_list *new); +int ft_lstsize(t_list *lst); +t_list *ft_lstlast(t_list *lst); +void ft_lstadd_back(t_list **lst, t_list *new); +void ft_lstdelone(t_list *lst, void (*del)(void*)); +void ft_lstclear(t_list **lst, void (*del)(void*)); +void ft_lstiter(t_list *lst, void (*f)(void *)); +t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)); + +//gnl +char *get_next_line(int fd); +int ft_check_nl(char *input); +char *ft_send_join(char *s1, char *s2); +char *ft_g_strjoin(char *s1, char *s2); +char *ft_create_line(char *saved); +char *ft_remove_line(char *saved); +char *read_buff(int fd, char *saved); + +//printf +int ft_printf(const char *format, ...); +void ft_putunsignedint_fd(unsigned int n, int fd); +int ft_convert_hex(unsigned long n, char *up_or_low); +void ft_print_hex(unsigned long n, char *up_or_low); +int ft_putstr_size(char *s, int fd); +int ft_determine_type(char c, va_list ap); +int ft_determine_nbr_size(long long nbr); +int ft_determine_unsigned_nbr_size(unsigned int nbr); +int ft_if_num(char c, va_list ap); +int ft_if_char(char c, va_list ap); +int ft_if_hex(char c, va_list ap); + +#endif \ No newline at end of file diff --git a/main.c b/main.c deleted file mode 100644 index 74de8a8..0000000 --- a/main.c +++ /dev/null @@ -1,3 +0,0 @@ -int main(void) -{ -} diff --git a/srcs/main.c b/srcs/main.c new file mode 100644 index 0000000..229fbb4 --- /dev/null +++ b/srcs/main.c @@ -0,0 +1,16 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* main.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ +/* Updated: 2022/02/14 12:04:47 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +int main(int argc, char **argv) +{ + +} From 892aa951d0dcbd67017cc5e2215aab677ed10df6 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Mon, 14 Feb 2022 12:09:03 +0100 Subject: [PATCH 003/163] +makefile --- Makefile | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..4cb6628 --- /dev/null +++ b/Makefile @@ -0,0 +1,26 @@ +NAME = pipex + +SRCS = pipex.c parse_envp.c + +FLAGS = -Wall -Werror -Wextra + +LIBFT = libft/libft.a + +HEADER = pipex.h + +all: $(NAME) + +$(NAME): $(LIBFT) $(SRCS) $(HEADER) + $(CC) $(LIBFT) $(SRCS) -o $(NAME) + +$(LIBFT): + $(MAKE) -C libft + +clean: + make fclean -C libft + +fclean: clean + rm -f $(LIBFT) + rm -f $(NAME) + +re: fclean all From e25963a0021a1956eb243f7c9c83fcc73537df65 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Mon, 14 Feb 2022 12:10:47 +0100 Subject: [PATCH 004/163] ++ pipex --- srcs/pipex+/parse_envp.c | 52 +++++++++++++++++++ srcs/pipex+/pipex.c | 109 +++++++++++++++++++++++++++++++++++++++ srcs/pipex+/pipex.h | 32 ++++++++++++ 3 files changed, 193 insertions(+) create mode 100644 srcs/pipex+/parse_envp.c create mode 100644 srcs/pipex+/pipex.c create mode 100644 srcs/pipex+/pipex.h diff --git a/srcs/pipex+/parse_envp.c b/srcs/pipex+/parse_envp.c new file mode 100644 index 0000000..6e85bd6 --- /dev/null +++ b/srcs/pipex+/parse_envp.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* parse_envp.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/17 16:16:57 by mgraaf #+# #+# */ +/* Updated: 2021/12/17 16:17:02 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "pipex.h" + +char *find_path(char **envp) +{ + char *path_from_envp; + int i; + + i = 0; + while (envp[i]) + { + if (!ft_strncmp(envp[i], "PATH=", 5)) + path_from_envp = ft_substr(envp[i], 5, ft_strlen(envp[i]) - 5); + i++; + } + return (path_from_envp); +} + +char **parse_envp(char **argv, char **envp) +{ + char *path_from_envp; + char **mypaths; + int i; + char *tmp; + + path_from_envp = find_path(envp); + mypaths = ft_split(path_from_envp, ':'); + free(path_from_envp); + i = 0; + while (mypaths[i]) + { + if (ft_strncmp(&mypaths[i][ft_strlen(mypaths[i]) - 1], "/", 1) != 0) + { + tmp = ft_strjoin(mypaths[i], "/"); + free(mypaths[i]); + mypaths[i] = tmp; + } + i++; + } + return (mypaths); +} diff --git a/srcs/pipex+/pipex.c b/srcs/pipex+/pipex.c new file mode 100644 index 0000000..5ed896f --- /dev/null +++ b/srcs/pipex+/pipex.c @@ -0,0 +1,109 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* pipex.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/16 14:58:07 by mgraaf #+# #+# */ +/* Updated: 2021/12/23 15:26:26 by maiadegraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "pipex.h" + +void child1_process(t_pipex *info, int *end) +{ + int i; + char *mycmd; + + if (dup2(info->f1, STDIN_FILENO) < 0) + return (perror("Dup ")); + if (dup2(end[1], STDOUT_FILENO) < 0) + return (perror("Dup ")); + close(end[0]); + close(info->f1); + i = 0; + while ((info->paths)[i]) + { + mycmd = ft_strjoin((info->paths)[i], (info->cmds1)[0]); + if (!access(mycmd, F_OK && X_OK)) + execve(mycmd, info->cmds1, info->envp); + free(mycmd); + i++; + } + perror("Cmd not found "); + exit(EXIT_FAILURE); +} + +void child2_process(t_pipex *info, int *end) +{ + int i; + char *mycmd; + + if (dup2(info->f2, STDOUT_FILENO) < 0) + return (perror("Dup ")); + if (dup2(end[0], STDIN_FILENO) < 0) + return (perror("Dup ")); + close(end[1]); + close(info->f2); + i = 0; + while ((info->paths)[i]) + { + mycmd = ft_strjoin((info->paths)[i], (info->cmds2)[0]); + if (!access(mycmd, F_OK && X_OK)) + execve(mycmd, info->cmds2, info->envp); + free(mycmd); + i++; + } + perror("Cmd not found "); + exit(EXIT_FAILURE); +} + +void pipex(t_pipex *info) +{ + int end[2]; + int status; + pid_t child1; + pid_t child2; + + pipe(end); + child1 = fork(); + if (child1 < 0) + return (perror("Fork: ")); + if (child1 == 0) + child1_process(info, end); + child2 = fork(); + if (child2 < 0) + return (perror("Fork: ")); + if (child2 == 0) + child2_process(info, end); + close(end[0]); + close(end[1]); + waitpid(child1, &status, 0); + waitpid(child2, &status, 0); +} + +int main(int argc, char **argv, char **envp) +{ + t_pipex info; + + if (argc != 5) + { + perror("Invalid number of arguments"); + exit(EXIT_FAILURE); + } + info.f1 = open(argv[1], O_RDONLY); + info.f2 = open(argv[4], O_CREAT | O_RDWR | O_TRUNC, 0644); + if (info.f1 < 0 || info.f2 < 0) + { + perror("Open "); + exit(EXIT_FAILURE); + } + info.envp = envp; + info.cmds1 = ft_split(argv[2], ' '); + info.cmds2 = ft_split(argv[3], ' '); + info.paths = parse_envp(argv, envp); + pipex(&info); + return (0); +} diff --git a/srcs/pipex+/pipex.h b/srcs/pipex+/pipex.h new file mode 100644 index 0000000..d85bba8 --- /dev/null +++ b/srcs/pipex+/pipex.h @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* pipex.h :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2021/12/17 10:59:55 by mgraaf #+# #+# */ +/* Updated: 2021/12/17 16:11:49 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#ifndef PIPEX_H +# define PIPEX_H +# include "libft/libft.h" +# include +# include +# include + +typedef struct s_pipex +{ + char **paths; + int f1; + int f2; + char **envp; + char **cmds1; + char **cmds2; +} t_pipex; + +char **parse_envp(char **argv, char **envp); + +#endif \ No newline at end of file From faac4101b714e6c362635bfc92d8733d04d4eb4d Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Mon, 14 Feb 2022 12:12:25 +0100 Subject: [PATCH 005/163] minitalk --- srcs/minitalk | 1 + 1 file changed, 1 insertion(+) create mode 160000 srcs/minitalk diff --git a/srcs/minitalk b/srcs/minitalk new file mode 160000 index 0000000..cdc7cf5 --- /dev/null +++ b/srcs/minitalk @@ -0,0 +1 @@ +Subproject commit cdc7cf5842bcb3f56f828a52fd0ee2795f0960be From d85a45c0c8348415b8b2bb446a05a987cd2a62d4 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Mon, 14 Feb 2022 12:13:30 +0100 Subject: [PATCH 006/163] minitalk --- srcs/minitalk | 1 - 1 file changed, 1 deletion(-) delete mode 160000 srcs/minitalk diff --git a/srcs/minitalk b/srcs/minitalk deleted file mode 160000 index cdc7cf5..0000000 --- a/srcs/minitalk +++ /dev/null @@ -1 +0,0 @@ -Subproject commit cdc7cf5842bcb3f56f828a52fd0ee2795f0960be From 771ecf46b4e02328d42885c355da153504d2c6c4 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Mon, 14 Feb 2022 12:15:41 +0100 Subject: [PATCH 007/163] minitalk --- srcs/minitalk/Makefile | 45 ++++++++++++++++++++++++ srcs/minitalk/client.c | 75 ++++++++++++++++++++++++++++++++++++++++ srcs/minitalk/minitalk.h | 26 ++++++++++++++ srcs/minitalk/server.c | 59 +++++++++++++++++++++++++++++++ 4 files changed, 205 insertions(+) create mode 100644 srcs/minitalk/Makefile create mode 100644 srcs/minitalk/client.c create mode 100644 srcs/minitalk/minitalk.h create mode 100644 srcs/minitalk/server.c diff --git a/srcs/minitalk/Makefile b/srcs/minitalk/Makefile new file mode 100644 index 0000000..5757550 --- /dev/null +++ b/srcs/minitalk/Makefile @@ -0,0 +1,45 @@ +# **************************************************************************** # +# # +# :::::::: # +# Makefile :+: :+: # +# +:+ # +# By: fpolycar +#+ # +# +#+ # +# Created: 2021/12/15 10:49:26 by fpolycar #+# #+# # +# Updated: 2022/02/04 14:06:57 by fpolycar ######## odam.nl # +# # +# **************************************************************************** # + +SERVER = server +CLIENT = client +CC = gcc +CFLAGS = -Wall -Werror -Wextra +AR = ar ru +RM = rm -f +LIBFT = ./libft/libft.a + +all: $(LIBFT) $(SERVER) $(CLIENT) + +$(LIBFT): + @make bonus -C libft + +$(SERVER): server.o minitalk.h + $(CC) server.o $(LIBFT) -o $@ + @printf "\e[38;5;226m./$@ successfully build\e[0m\n" + +$(CLIENT): client.o minitalk.h + $(CC) client.o $(LIBFT) -o $@ + @printf "\e[38;5;46m./$@ successfully build\e[0m\n" + +%.o: %.c + $(CC) -c $(CFLAGS) -o $@ $< + +clean: + @make clean -C libft + $(RM) *.o + +fclean: clean + @make fclean -C libft + rm -f $(CLIENT) $(SERVER) + +re: fclean all diff --git a/srcs/minitalk/client.c b/srcs/minitalk/client.c new file mode 100644 index 0000000..0ffc357 --- /dev/null +++ b/srcs/minitalk/client.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* client.c :+: :+: */ +/* +:+ */ +/* By: fpolycar +#+ */ +/* +#+ */ +/* Created: 2021/12/15 10:49:04 by fpolycar #+# #+# */ +/* Updated: 2021/12/17 15:17:43 by fpolycar ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minitalk.h" + +void send_binary(int pid, int *bin) +{ + int i; + + i = 7; + while (i >= 0) + { + if (bin[i] == 1) + { + kill(pid, SIGUSR2); + } + if (bin[i] == 0) + { + kill(pid, SIGUSR1); + } + usleep(100); + i--; + } +} + +int *char_to_bin(char c) +{ + int *binary; + int i; + int n; + + binary = ft_calloc(8, sizeof(int)); + if (!binary) + return (NULL); + n = (unsigned int)c; + i = 7; + while (i >= 0) + { + binary[i] = n & 1; + i--; + n >>= 1; + } + return (binary); +} + +int main(int argc, char **argv) +{ + int pid; + int i; + int *bin; + + if (argc == 3) + { + i = 0; + pid = ft_atoi(argv[1]); + while (argv[2][i] != 0) + { + bin = char_to_bin(argv[2][i]); + send_binary(pid, bin); + i++; + free(bin); + } + } + else + write(1, "\nERROR expected: ./execute [pid] [str]\n\n", 43); +} diff --git a/srcs/minitalk/minitalk.h b/srcs/minitalk/minitalk.h new file mode 100644 index 0000000..a75cb66 --- /dev/null +++ b/srcs/minitalk/minitalk.h @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* minitalk.h :+: :+: */ +/* +:+ */ +/* By: fpolycar +#+ */ +/* +#+ */ +/* Created: 2021/12/15 10:52:13 by fpolycar #+# #+# */ +/* Updated: 2021/12/15 13:43:39 by fpolycar ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#ifndef MINITALK_H +# define MINITALK_H +# include +# include +# include +# include "libft/libft.h" + +typedef struct s_msg +{ + char c; + size_t size; +} t_msg; + +#endif \ No newline at end of file diff --git a/srcs/minitalk/server.c b/srcs/minitalk/server.c new file mode 100644 index 0000000..3ae787b --- /dev/null +++ b/srcs/minitalk/server.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* server.c :+: :+: */ +/* +:+ */ +/* By: fpolycar +#+ */ +/* +#+ */ +/* Created: 2021/12/15 10:49:07 by fpolycar #+# #+# */ +/* Updated: 2021/12/15 13:44:26 by fpolycar ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minitalk.h" + +static t_msg g_msg; + +static void handler_msg(int signum) +{ + g_msg.c += ((signum & 1) << (g_msg.size)); + g_msg.size++; + if (g_msg.size == 8) + { + ft_putchar_fd(g_msg.c, 1); + if (!g_msg.c) + ft_putchar_fd('\n', 1); + g_msg.c = 0; + g_msg.size = 0; + } +} + +void display_pid(void) +{ + int pid; + char *str_pid; + + pid = getpid(); + str_pid = ft_itoa(pid); + if (!str_pid) + { + ft_putendl_fd("Error at ft_itoa()", 1); + return ; + } + ft_putstr_fd("PID: ", 1); + ft_putendl_fd(str_pid, 1); + free(str_pid); +} + +int main(void) +{ + g_msg.c = 0; + g_msg.size = 0; + display_pid(); + while (1) + { + signal(SIGUSR2, handler_msg); + signal(SIGUSR1, handler_msg); + pause(); + } +} From 9ec693fdb76aa5dccbf33eada27ecc081ab8350d Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Mon, 14 Feb 2022 12:18:03 +0100 Subject: [PATCH 008/163] makefile_edit --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 4cb6628..e36d90c 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -NAME = pipex +NAME = minishell SRCS = pipex.c parse_envp.c @@ -6,7 +6,7 @@ FLAGS = -Wall -Werror -Wextra LIBFT = libft/libft.a -HEADER = pipex.h +HEADER = minishell.h all: $(NAME) From c90939747307b82624f39525029e46e2482757d5 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Mon, 14 Feb 2022 13:50:26 +0100 Subject: [PATCH 009/163] Updated makefile and added header --- Makefile | 22 +++++++++++++++------- includes/minishell.h | 18 ++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) create mode 100644 includes/minishell.h diff --git a/Makefile b/Makefile index 4cb6628..f7fa7fe 100644 --- a/Makefile +++ b/Makefile @@ -1,26 +1,34 @@ NAME = pipex -SRCS = pipex.c parse_envp.c +SRCS = srcs/pipex.c + srcs/parse_envp.c + srcs/main.c FLAGS = -Wall -Werror -Wextra LIBFT = libft/libft.a -HEADER = pipex.h +HEADER = includes/minishell.h all: $(NAME) -$(NAME): $(LIBFT) $(SRCS) $(HEADER) - $(CC) $(LIBFT) $(SRCS) -o $(NAME) +%.o: %.c $(HEADER) + @echo "Compiling ${notdir $<}" + @$(CC) -c $(CFLAGS) -I includes -o $@ $< + +$(NAME): $(LIBFT) $(OBJS) $(HEADER) + @$(CC) $(LIBFT) $(OBJS) -o $(NAME) + @echo "Success" $(LIBFT): $(MAKE) -C libft clean: - make fclean -C libft + @echo "Cleaning" + @rm -f $(OBJS) fclean: clean - rm -f $(LIBFT) - rm -f $(NAME) + @rm -f $(NAME) + @rm -f $(LIBFT) re: fclean all diff --git a/includes/minishell.h b/includes/minishell.h new file mode 100644 index 0000000..1e8dda7 --- /dev/null +++ b/includes/minishell.h @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* minishell.h :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ +/* Updated: 2022/02/14 13:46:43 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#ifndef MINISHELL_H +# define MINISHELL_H +# include +# include + +#endif \ No newline at end of file From 9de5efd8ad8ddef958f04b556e06e4c289db5e39 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Mon, 14 Feb 2022 17:54:30 +0100 Subject: [PATCH 010/163] .gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1530978 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.o \ No newline at end of file From cda53103b7635b093c1aa70061c6102b1e8e2e96 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Mon, 14 Feb 2022 17:55:41 +0100 Subject: [PATCH 011/163] .gitignore_edit --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 1530978..7eb60c8 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -*.o \ No newline at end of file +*.o +libft.a +minishell \ No newline at end of file From 167085ad927857396b6cce96e2dba7e291204f69 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Mon, 14 Feb 2022 18:13:28 +0100 Subject: [PATCH 012/163] fucked around a bit; made a very basic cmd reader --- .vscode/settings.json | 8 ++++++ Makefile | 23 +++++++++------- includes/minishell.h | 16 ++++++++++- libraries/libft/Makefile | 12 ++++---- srcs/main.c | 50 ++++++++++++++++++++++++++++++++-- srcs/{pipex+ => }/parse_envp.c | 6 ++-- srcs/{pipex+ => pipex}/pipex.c | 46 +++++++++++++++---------------- srcs/{pipex+ => pipex}/pipex.h | 6 ++-- 8 files changed, 119 insertions(+), 48 deletions(-) create mode 100644 .vscode/settings.json rename srcs/{pipex+ => }/parse_envp.c (91%) rename srcs/{pipex+ => pipex}/pipex.c (78%) rename srcs/{pipex+ => pipex}/pipex.h (88%) diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f023251 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "files.autoSave": "onFocusChange", + "files.associations": { + "stdio.h": "c", + "readline.h": "c", + "libft.h": "c" + } +} \ No newline at end of file diff --git a/Makefile b/Makefile index f7fa7fe..5336875 100644 --- a/Makefile +++ b/Makefile @@ -1,31 +1,34 @@ -NAME = pipex +NAME = minishell -SRCS = srcs/pipex.c - srcs/parse_envp.c - srcs/main.c +SRCS = ./srcs/main.c \ + ./srcs/parse_envp.c \ + # ./srcs/pipex/pipex.c \ -FLAGS = -Wall -Werror -Wextra +OBJS = ${SRCS:%.c=%.o} -LIBFT = libft/libft.a +FLAGS = -Wall -Werror -Wextra -HEADER = includes/minishell.h +LIBFT = ./libraries/libft/libft.a + +HEADER = ./includes/minishell.h all: $(NAME) %.o: %.c $(HEADER) @echo "Compiling ${notdir $<}" - @$(CC) -c $(CFLAGS) -I includes -o $@ $< + @$(CC) -c $(FLAGS) -I includes -o $@ $< $(NAME): $(LIBFT) $(OBJS) $(HEADER) - @$(CC) $(LIBFT) $(OBJS) -o $(NAME) + @$(CC) $(FLAGS) $(LIBFT) $(OBJS) -lreadline -o $(NAME) @echo "Success" $(LIBFT): - $(MAKE) -C libft + $(MAKE) -C ./libraries/libft clean: @echo "Cleaning" @rm -f $(OBJS) + @make fclean -C libraries/libft fclean: clean @rm -f $(NAME) diff --git a/includes/minishell.h b/includes/minishell.h index 1e8dda7..d9c9d66 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,13 +6,27 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/02/14 13:46:43 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/14 18:06:02 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #ifndef MINISHELL_H # define MINISHELL_H +# include "../libraries/libft/libft.h" # include # include +# include +# include +# include +# include "../srcs/pipex/pipex.h" + +typedef struct s_tools +{ + char **args; + char **paths; + char **envp; +} t_tools; + +char **parse_envp(char **envp); #endif \ No newline at end of file diff --git a/libraries/libft/Makefile b/libraries/libft/Makefile index ad6751c..c042847 100644 --- a/libraries/libft/Makefile +++ b/libraries/libft/Makefile @@ -73,19 +73,21 @@ endif all: libft.a %.o: %.c $(HEADERFILES) - $(CC) -c $(CFLAGS) -o $@ $< + @echo "Compiling ${notdir $<}" + @$(CC) -c $(CFLAGS) -o $@ $< $(NAME): $(ALL_OBJS) - ar -ru $(NAME) $^ + @ar -ru $(NAME) $^ + @echo "Libft succesfully created" bonus: - $(MAKE) WITH_BONUS=1 + @$(MAKE) WITH_BONUS=1 clean: - rm -f $(OBJFILES) $(BONUS_OBJFILES) + @rm -f $(OBJFILES) $(BONUS_OBJFILES) fclean: clean - rm -f $(NAME) $^ + @rm -f $(NAME) $^ re: fclean all diff --git a/srcs/main.c b/srcs/main.c index 229fbb4..732fbc8 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,11 +6,55 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/02/14 12:04:47 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/14 18:10:58 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ -int main(int argc, char **argv) +#include "minishell.h" + +int find_cmd(t_tools *tools) +{ + int i; + char *mycmd; + + i = 0; + while ((tools->paths)[i]) + { + mycmd = ft_strjoin((tools->paths)[i], (tools->args)[0]); + if (!access(mycmd, F_OK && X_OK)) + execve(mycmd, tools->args, tools->envp); + free(mycmd); + i++; + } + return (1); +} + +int main(int argc, char **argv, char **envp) { - + char *line; + t_tools tools; + pid_t process; + int status; + + if (argc != 1 || argv[1]) + { + printf("Please do not enter any arguments\n"); + exit(0); + } + tools.envp = envp; + tools.paths = parse_envp(envp); + while (1) + { + line = readline(">> "); + add_history(line); + if (!ft_strncmp(line, "exit", ft_strlen(line))) + exit (0); + tools.args = ft_split(line, ' '); + process = fork(); + if (process == 0) + find_cmd(&tools); + waitpid(process, &status, 0); + free(line); + } + return (0); } diff --git a/srcs/pipex+/parse_envp.c b/srcs/parse_envp.c similarity index 91% rename from srcs/pipex+/parse_envp.c rename to srcs/parse_envp.c index 6e85bd6..1f414ee 100644 --- a/srcs/pipex+/parse_envp.c +++ b/srcs/parse_envp.c @@ -6,11 +6,11 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2021/12/17 16:16:57 by mgraaf #+# #+# */ -/* Updated: 2021/12/17 16:17:02 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/14 17:48:27 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ -#include "pipex.h" +#include "minishell.h" char *find_path(char **envp) { @@ -27,7 +27,7 @@ char *find_path(char **envp) return (path_from_envp); } -char **parse_envp(char **argv, char **envp) +char **parse_envp(char **envp) { char *path_from_envp; char **mypaths; diff --git a/srcs/pipex+/pipex.c b/srcs/pipex/pipex.c similarity index 78% rename from srcs/pipex+/pipex.c rename to srcs/pipex/pipex.c index 5ed896f..69f7686 100644 --- a/srcs/pipex+/pipex.c +++ b/srcs/pipex/pipex.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2021/12/16 14:58:07 by mgraaf #+# #+# */ -/* Updated: 2021/12/23 15:26:26 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/02/14 14:20:37 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -84,26 +84,26 @@ void pipex(t_pipex *info) waitpid(child2, &status, 0); } -int main(int argc, char **argv, char **envp) -{ - t_pipex info; +// int main(int argc, char **argv, char **envp) +// { +// t_pipex info; - if (argc != 5) - { - perror("Invalid number of arguments"); - exit(EXIT_FAILURE); - } - info.f1 = open(argv[1], O_RDONLY); - info.f2 = open(argv[4], O_CREAT | O_RDWR | O_TRUNC, 0644); - if (info.f1 < 0 || info.f2 < 0) - { - perror("Open "); - exit(EXIT_FAILURE); - } - info.envp = envp; - info.cmds1 = ft_split(argv[2], ' '); - info.cmds2 = ft_split(argv[3], ' '); - info.paths = parse_envp(argv, envp); - pipex(&info); - return (0); -} +// if (argc != 5) +// { +// perror("Invalid number of arguments"); +// exit(EXIT_FAILURE); +// } +// info.f1 = open(argv[1], O_RDONLY); +// info.f2 = open(argv[4], O_CREAT | O_RDWR | O_TRUNC, 0644); +// if (info.f1 < 0 || info.f2 < 0) +// { +// perror("Open "); +// exit(EXIT_FAILURE); +// } +// info.envp = envp; +// info.cmds1 = ft_split(argv[2], ' '); +// info.cmds2 = ft_split(argv[3], ' '); +// info.paths = parse_envp(argv, envp); +// pipex(&info); +// return (0); +// } diff --git a/srcs/pipex+/pipex.h b/srcs/pipex/pipex.h similarity index 88% rename from srcs/pipex+/pipex.h rename to srcs/pipex/pipex.h index d85bba8..850d25a 100644 --- a/srcs/pipex+/pipex.h +++ b/srcs/pipex/pipex.h @@ -6,13 +6,13 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2021/12/17 10:59:55 by mgraaf #+# #+# */ -/* Updated: 2021/12/17 16:11:49 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/14 17:50:42 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #ifndef PIPEX_H # define PIPEX_H -# include "libft/libft.h" +# include "../../libraries/libft/libft.h" # include # include # include @@ -27,6 +27,6 @@ typedef struct s_pipex char **cmds2; } t_pipex; -char **parse_envp(char **argv, char **envp); +char **parse_envp(char **envp); #endif \ No newline at end of file From fee8d98708dadf772cb158914d593fa056de342c Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Tue, 15 Feb 2022 18:13:15 +0100 Subject: [PATCH 013/163] who knows --- Makefile | 12 ++++++++++- a.out | Bin 0 -> 8528 bytes includes/builtins.h | 36 +++++++++++++++++++++++++++++++ includes/minishell.h | 12 +++++++++-- srcs/builtins/builtins.c | 42 ++++++++++++++++++++++++++++++++++++ srcs/builtins/mini_cd.c | 20 +++++++++++++++++ srcs/builtins/mini_echo.c | 7 ++++++ srcs/builtins/mini_env.c | 7 ++++++ srcs/builtins/mini_exit.c | 7 ++++++ srcs/builtins/mini_export.c | 7 ++++++ srcs/builtins/mini_pwd.c | 7 ++++++ srcs/builtins/mini_unset.c | 7 ++++++ srcs/main.c | 4 ++-- srcs/parse_envp.c | 41 +++++++++++++++++++++++++---------- srcs/pipex/pipex.h | 4 ++-- 15 files changed, 195 insertions(+), 18 deletions(-) create mode 100755 a.out create mode 100644 includes/builtins.h create mode 100644 srcs/builtins/builtins.c create mode 100644 srcs/builtins/mini_cd.c create mode 100644 srcs/builtins/mini_echo.c create mode 100644 srcs/builtins/mini_env.c create mode 100644 srcs/builtins/mini_exit.c create mode 100644 srcs/builtins/mini_export.c create mode 100644 srcs/builtins/mini_pwd.c create mode 100644 srcs/builtins/mini_unset.c diff --git a/Makefile b/Makefile index 5336875..69db428 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,16 @@ NAME = minishell SRCS = ./srcs/main.c \ ./srcs/parse_envp.c \ + ./srcs/builtins/builtins.c \ + ./srcs/builtins/mini_cd.c \ + ./srcs/builtins/mini_echo.c \ + ./srcs/builtins/mini_exit.c \ + ./srcs/builtins/mini_export.c \ + ./srcs/builtins/mini_pwd.c \ + ./srcs/builtins/mini_unset.c \ + ./srcs/builtins/mini_env.c \ # ./srcs/pipex/pipex.c \ + OBJS = ${SRCS:%.c=%.o} @@ -10,7 +19,8 @@ FLAGS = -Wall -Werror -Wextra LIBFT = ./libraries/libft/libft.a -HEADER = ./includes/minishell.h +HEADER = ./includes/minishell.h \ + ./includes/builtins.h all: $(NAME) diff --git a/a.out b/a.out new file mode 100755 index 0000000000000000000000000000000000000000..e88938c1961ef01eb9a254d2a6390dcbe6cb1b56 GIT binary patch literal 8528 zcmeHNO=uHA7@gFrL90zYi2tlV5r3fd@6e0VN`nzaqpctX8JcEo0%=;34K@cQP(*_i za`qraJc|dxgCK|^dJ@Eg2ahTdDtInpd~bH9*=&nHZ)e_QVY}1!i$8v^ z6(X`qh-!xrq8lDtEyQi%AQECLJPDUFHFzd5nz($C*>;Px!xa&EKA}*`k;K)J_7XKd z)}9j@V=BTpE2Q+?8Lw3v=5w$B@}1Kq#GS2jf@%;-S@O#1(o!nSm)CrobcMlYKveUs z(FvbVX5IX>TSP9*SJiyIx}KQ2FWIvBjhDP)t}vks!hDZ4pIIYu(Av1sA!WI6BUi}C zT;ckZ!tMLTuT(KnFFA3fZ3nY zE%%cBcY(Ra=x+b{UXM5h4;pLfK^=nqj2n)B-*A2-66g|L@V;`X*q6_x`!bb$2AH-T z&Ut)%xo@ufS--45zw_zTP;crOat=HK7aP|pXRNgB6di{UL)s-ffc2Ay&w0>z-+nmz zxQ58$7c3*vnhWMLTq$|(Wbf%-%qy2h5o|x4{8h(a*FlY`mo=+^Rlq7>6|f3e1*`&A zf&X8DE6Li|-=DiMGNwi|+X@uYmpaCf|GsV?={8tnW2=Bw zz$#!BunJfOtO8a6tAJI&Dqt0`3RngHEd_e^#D+QJo&g@tsl#xY#`!P~uP+s&by1A< z49M|pCRY>yTL30pZ~R7v$N`$h*|c{Z$XG<;oU=ntW~Dphj+Z?bXW?U{+dVm!D~LEh zQmhj5yL7z?nC_1FCwu( +#+ */ +/* +#+ */ +/* Created: 2022/02/15 15:20:00 by mgraaf #+# #+# */ +/* Updated: 2022/02/15 18:02:46 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#ifndef BUILTINS_H +# define BUILTINS_H +# include +# include "minishell.h" + +//builtins +void change_path(t_tools *tools); +void *init_builtin_arr(char *str); + +int mini_echo(t_tools *tools); + +int mini_cd(t_tools *tools); + +int mini_pwd(t_tools *tools); + +int mini_export(t_tools *tools); + +int mini_unset(t_tools *tools); + +int mini_env(t_tools *tools); + +int mini_exit(t_tools *tools); + +#endif \ No newline at end of file diff --git a/includes/minishell.h b/includes/minishell.h index d9c9d66..dfbee16 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/02/14 18:06:02 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/15 17:42:00 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,14 +19,22 @@ # include # include # include "../srcs/pipex/pipex.h" +//# include "builtins.h" typedef struct s_tools { char **args; char **paths; char **envp; + int in; + int out; + int err; + char *pwd; + char *old_pwd; + } t_tools; -char **parse_envp(char **envp); +int parse_envp(t_tools *tools); +int find_pwd(t_tools *tools); #endif \ No newline at end of file diff --git a/srcs/builtins/builtins.c b/srcs/builtins/builtins.c new file mode 100644 index 0000000..c759e62 --- /dev/null +++ b/srcs/builtins/builtins.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* builtins.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/02/15 11:42:32 by mgraaf #+# #+# */ +/* Updated: 2022/02/15 18:02:20 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "builtins.h" + +void change_path(t_tools *tools) +{ + tools->old_pwd = tools->pwd; + tools->pwd = getcwd(NULL, 200); +} + +void *init_builtin_arr(char *str) +{ + static void *builtins[7][2] = { + {"echo", mini_echo}, + {"cd", mini_cd}, + {"pwd", mini_pwd}, + {"export", mini_export}, + {"unset", mini_unset}, + {"env", mini_env}, + {"exit", mini_exit} + }; + int i; + + i = 0; + while (i < 7) + { + if (ft_strncmp(builtins[i][0], str, strlen(builtins[i][0]))) + return (builtins[i][1]); + i++; + } + return (NULL); +} diff --git a/srcs/builtins/mini_cd.c b/srcs/builtins/mini_cd.c new file mode 100644 index 0000000..2a2c54a --- /dev/null +++ b/srcs/builtins/mini_cd.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* mini_cd.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/02/15 15:17:04 by mgraaf #+# #+# */ +/* Updated: 2022/02/15 18:03:56 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "builtins.h" + +int mini_cd(t_tools *tools) +{ + chdir(tools->args[1]); + change_path(tools); + return (0); +} diff --git a/srcs/builtins/mini_echo.c b/srcs/builtins/mini_echo.c new file mode 100644 index 0000000..938dccd --- /dev/null +++ b/srcs/builtins/mini_echo.c @@ -0,0 +1,7 @@ +#include "builtins.h" + +int mini_echo(t_tools *tools) +{ + printf("%s\n", tools->pwd); + return (1); +} \ No newline at end of file diff --git a/srcs/builtins/mini_env.c b/srcs/builtins/mini_env.c new file mode 100644 index 0000000..e165cb8 --- /dev/null +++ b/srcs/builtins/mini_env.c @@ -0,0 +1,7 @@ +#include "builtins.h" + +int mini_env(t_tools *tools) +{ + printf("%s\n", tools->pwd); + return (1); +} \ No newline at end of file diff --git a/srcs/builtins/mini_exit.c b/srcs/builtins/mini_exit.c new file mode 100644 index 0000000..60d86ae --- /dev/null +++ b/srcs/builtins/mini_exit.c @@ -0,0 +1,7 @@ +#include "builtins.h" + +int mini_exit(t_tools *tools) +{ + printf("%s\n", tools->pwd); + return (1); +} \ No newline at end of file diff --git a/srcs/builtins/mini_export.c b/srcs/builtins/mini_export.c new file mode 100644 index 0000000..a9d9973 --- /dev/null +++ b/srcs/builtins/mini_export.c @@ -0,0 +1,7 @@ +#include "builtins.h" + +int mini_export(t_tools *tools) +{ + printf("%s\n", tools->pwd); + return (1); +} \ No newline at end of file diff --git a/srcs/builtins/mini_pwd.c b/srcs/builtins/mini_pwd.c new file mode 100644 index 0000000..189c70d --- /dev/null +++ b/srcs/builtins/mini_pwd.c @@ -0,0 +1,7 @@ +#include "builtins.h" + +int mini_pwd(t_tools *tools) +{ + printf("%s\n", tools->pwd); + return (1); +} \ No newline at end of file diff --git a/srcs/builtins/mini_unset.c b/srcs/builtins/mini_unset.c new file mode 100644 index 0000000..63fe8c9 --- /dev/null +++ b/srcs/builtins/mini_unset.c @@ -0,0 +1,7 @@ +#include "builtins.h" + +int mini_unset(t_tools *tools) +{ + printf("%s\n", tools->pwd); + return (1); +} \ No newline at end of file diff --git a/srcs/main.c b/srcs/main.c index 732fbc8..263bada 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/02/14 18:10:58 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/15 17:30:17 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -42,7 +42,7 @@ int main(int argc, char **argv, char **envp) exit(0); } tools.envp = envp; - tools.paths = parse_envp(envp); + parse_envp(&tools); while (1) { line = readline(">> "); diff --git a/srcs/parse_envp.c b/srcs/parse_envp.c index 1f414ee..466be6c 100644 --- a/srcs/parse_envp.c +++ b/srcs/parse_envp.c @@ -6,12 +6,30 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2021/12/17 16:16:57 by mgraaf #+# #+# */ -/* Updated: 2022/02/14 17:48:27 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/15 16:19:13 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" +int find_pwd(t_tools *tools) +{ + int i; + + i = 0; + while (tools->envp[i]) + { + if (!ft_strncmp(tools->envp[i], "PWD=", 4)) + tools->pwd = ft_substr(tools->envp[i], + 4, ft_strlen(tools->envp[i]) - 4); + if (!ft_strncmp(tools->envp[i], "OLDPWD=", 7)) + tools->pwd = ft_substr(tools->envp[i], + 7, ft_strlen(tools->envp[i]) - 7); + i++; + } + return (1); +} + char *find_path(char **envp) { char *path_from_envp; @@ -27,26 +45,27 @@ char *find_path(char **envp) return (path_from_envp); } -char **parse_envp(char **envp) +int parse_envp(t_tools *tools) { char *path_from_envp; - char **mypaths; int i; char *tmp; - path_from_envp = find_path(envp); - mypaths = ft_split(path_from_envp, ':'); + path_from_envp = find_path(tools->envp); + tools->paths = ft_split(path_from_envp, ':'); free(path_from_envp); i = 0; - while (mypaths[i]) + while (tools->paths[i]) { - if (ft_strncmp(&mypaths[i][ft_strlen(mypaths[i]) - 1], "/", 1) != 0) + if (ft_strncmp(&tools->paths[i][ft_strlen(tools->paths[i]) - 1], + "/", 1) != 0) { - tmp = ft_strjoin(mypaths[i], "/"); - free(mypaths[i]); - mypaths[i] = tmp; + tmp = ft_strjoin(tools->paths[i], "/"); + free(tools->paths[i]); + tools->paths[i] = tmp; } i++; } - return (mypaths); + find_pwd(tools); + return (1); } diff --git a/srcs/pipex/pipex.h b/srcs/pipex/pipex.h index 850d25a..78b5eac 100644 --- a/srcs/pipex/pipex.h +++ b/srcs/pipex/pipex.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2021/12/17 10:59:55 by mgraaf #+# #+# */ -/* Updated: 2022/02/14 17:50:42 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/15 17:07:32 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -27,6 +27,6 @@ typedef struct s_pipex char **cmds2; } t_pipex; -char **parse_envp(char **envp); +//char **parse_envp(char **envp); #endif \ No newline at end of file From 1ebdc50d732e1e0f7f396494165359d8cb3d6337 Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Thu, 17 Feb 2022 11:22:04 +0100 Subject: [PATCH 014/163] added some builtins --- includes/builtins.h | 4 ++-- srcs/builtins/builtins.c | 4 ++-- srcs/builtins/mini_echo.c | 21 ++++++++++++++++++--- srcs/builtins/mini_env.c | 30 +++++++++++++++++++++++++++--- srcs/builtins/mini_exit.c | 21 ++++++++++++++++++--- srcs/builtins/mini_export.c | 16 ++++++++++++++-- srcs/builtins/mini_pwd.c | 16 ++++++++++++++-- srcs/builtins/mini_unset.c | 16 ++++++++++++++-- srcs/main.c | 7 +++---- 9 files changed, 112 insertions(+), 23 deletions(-) diff --git a/includes/builtins.h b/includes/builtins.h index 4d54213..135df57 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:20:00 by mgraaf #+# #+# */ -/* Updated: 2022/02/15 18:02:46 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/17 10:23:37 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ //builtins void change_path(t_tools *tools); -void *init_builtin_arr(char *str); +void *builtin_arr(char *str); int mini_echo(t_tools *tools); diff --git a/srcs/builtins/builtins.c b/srcs/builtins/builtins.c index c759e62..5a37c31 100644 --- a/srcs/builtins/builtins.c +++ b/srcs/builtins/builtins.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 11:42:32 by mgraaf #+# #+# */ -/* Updated: 2022/02/15 18:02:20 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/17 10:23:26 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ void change_path(t_tools *tools) tools->pwd = getcwd(NULL, 200); } -void *init_builtin_arr(char *str) +void *builtin_arr(char *str) { static void *builtins[7][2] = { {"echo", mini_echo}, diff --git a/srcs/builtins/mini_echo.c b/srcs/builtins/mini_echo.c index 938dccd..ab48f3a 100644 --- a/srcs/builtins/mini_echo.c +++ b/srcs/builtins/mini_echo.c @@ -1,7 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* mini_echo.c :+: :+: */ +/* +:+ */ +/* By: maiadegraaf pwd); + if (ft_strncmp(tools->args[1], "-n", ft_strlen(tools->args[1]))) + printf("%s", tools->args[2]); + else + printf("%s\n", tools->args[2]); return (1); -} \ No newline at end of file +} diff --git a/srcs/builtins/mini_env.c b/srcs/builtins/mini_env.c index e165cb8..baeeda6 100644 --- a/srcs/builtins/mini_env.c +++ b/srcs/builtins/mini_env.c @@ -1,7 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* mini_env.c :+: :+: */ +/* +:+ */ +/* By: maiadegraaf pwd); + int i; + + i = 0; + while (tools->envp[i]) + { + if (!ft_strncmp(tools->envp[i], "PWD=", 4)) + printf("%s", tools->pwd); + else if (!ft_strncmp(tools->envp[i], "OLDPWD=", 7)) + printf("%s", tools->old_pwd); + else + printf("%s", tools->envp[i]); + i++; + } return (1); -} \ No newline at end of file +} diff --git a/srcs/builtins/mini_exit.c b/srcs/builtins/mini_exit.c index 60d86ae..82b0fab 100644 --- a/srcs/builtins/mini_exit.c +++ b/srcs/builtins/mini_exit.c @@ -1,7 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* mini_exit.c :+: :+: */ +/* +:+ */ +/* By: maiadegraaf pwd); + free(tools->args); + exit(0); return (1); -} \ No newline at end of file +} diff --git a/srcs/builtins/mini_export.c b/srcs/builtins/mini_export.c index a9d9973..b3f8a27 100644 --- a/srcs/builtins/mini_export.c +++ b/srcs/builtins/mini_export.c @@ -1,7 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* mini_export.c :+: :+: */ +/* +:+ */ +/* By: maiadegraaf pwd); return (1); -} \ No newline at end of file +} diff --git a/srcs/builtins/mini_pwd.c b/srcs/builtins/mini_pwd.c index 189c70d..3afe5cc 100644 --- a/srcs/builtins/mini_pwd.c +++ b/srcs/builtins/mini_pwd.c @@ -1,7 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* mini_pwd.c :+: :+: */ +/* +:+ */ +/* By: maiadegraaf pwd); return (1); -} \ No newline at end of file +} diff --git a/srcs/builtins/mini_unset.c b/srcs/builtins/mini_unset.c index 63fe8c9..14ee0fb 100644 --- a/srcs/builtins/mini_unset.c +++ b/srcs/builtins/mini_unset.c @@ -1,7 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* mini_unset.c :+: :+: */ +/* +:+ */ +/* By: maiadegraaf pwd); return (1); -} \ No newline at end of file +} diff --git a/srcs/main.c b/srcs/main.c index 263bada..3ffb823 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/02/15 17:30:17 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/17 10:23:49 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ int find_cmd(t_tools *tools) while ((tools->paths)[i]) { mycmd = ft_strjoin((tools->paths)[i], (tools->args)[0]); - if (!access(mycmd, F_OK && X_OK)) + if (!access(mycmd, F_OK)) execve(mycmd, tools->args, tools->envp); free(mycmd); i++; @@ -47,8 +47,7 @@ int main(int argc, char **argv, char **envp) { line = readline(">> "); add_history(line); - if (!ft_strncmp(line, "exit", ft_strlen(line))) - exit (0); + if (builtins_arr) tools.args = ft_split(line, ' '); process = fork(); if (process == 0) From 8aa2192a16fdcd93f9f79ed3ff34e993607365df Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Fri, 18 Feb 2022 11:06:04 +0100 Subject: [PATCH 015/163] built a lexor --- .vscode/launch.json | 29 ++++++++++ .vscode/tasks.json | 12 ++++ Makefile | 28 +++++---- a.out | Bin 8528 -> 0 bytes includes/builtins.h | 3 +- includes/lexor.h | 27 +++++++++ includes/minishell.h | 26 ++++----- includes/parser.h | 51 ++++++++++++++++ includes/utils.h | 43 ++++++++++++++ libraries/libft/Makefile | 2 +- srcs/builtins/builtins.c | 8 +-- srcs/builtins/mini_cd.c | 7 ++- srcs/builtins/mini_echo.c | 21 +++++-- srcs/builtins/mini_env.c | 9 +-- srcs/lexor/handle_token.c | 60 +++++++++++++++++++ srcs/lexor/token_reader.c | 92 +++++++++++++++++++++++++++++ srcs/main.c | 54 +++++++++-------- srcs/parse_envp.c | 4 +- srcs/parser/parse_args.c | 13 +++++ srcs/utils/t_lexor_utils.c | 95 ++++++++++++++++++++++++++++++ srcs/utils/t_simple_cmds_utils.c | 96 +++++++++++++++++++++++++++++++ 21 files changed, 611 insertions(+), 69 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json delete mode 100755 a.out create mode 100644 includes/lexor.h create mode 100644 includes/parser.h create mode 100644 includes/utils.h create mode 100644 srcs/lexor/handle_token.c create mode 100644 srcs/lexor/token_reader.c create mode 100644 srcs/parser/parse_args.c create mode 100644 srcs/utils/t_lexor_utils.c create mode 100644 srcs/utils/t_simple_cmds_utils.c diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..fe2c156 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,29 @@ +{ + // 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": [ + { + "name": "(lldb) Launch", + "type": "lldb", + "request": "launch", + "program": "${workspaceFolder}/minishell", + "args": [], + "stopAtEntry": true, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": true, + "MIMode": "lldb", + "setupCommands": [ + { + "description": "Enable pretty-printing for lldb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ], + "preLaunchTask": "build", + "disableOptimisticBPs": true + } + ] +} diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..5f75ac2 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,12 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "type": "shell", + "command": "make; export SHLVL=1", + "group": "build", + "problemMatcher": "$gcc" + }, + ] +} \ No newline at end of file diff --git a/Makefile b/Makefile index 69db428..6f32838 100644 --- a/Makefile +++ b/Makefile @@ -2,25 +2,33 @@ NAME = minishell SRCS = ./srcs/main.c \ ./srcs/parse_envp.c \ + ./srcs/utils/t_simple_cmds_utils.c \ + ./srcs/utils/t_lexor_utils.c \ + ./srcs/lexor/token_reader.c \ + ./srcs/lexor/handle_token.c \ + # ./srcs/builtins/mini_cd.c \ ./srcs/builtins/builtins.c \ - ./srcs/builtins/mini_cd.c \ - ./srcs/builtins/mini_echo.c \ - ./srcs/builtins/mini_exit.c \ - ./srcs/builtins/mini_export.c \ - ./srcs/builtins/mini_pwd.c \ - ./srcs/builtins/mini_unset.c \ - ./srcs/builtins/mini_env.c \ - # ./srcs/pipex/pipex.c \ + # ./srcs/builtins/mini_echo.c \ + # ./srcs/builtins/mini_exit.c \ + # ./srcs/builtins/mini_export.c \ + # ./srcs/builtins/mini_pwd.c \ + # ./srcs/builtins/mini_unset.c \ + # ./srcs/builtins/mini_env.c \ + # ./srcs/parser/parse_args.c \ + # ./srcs/pipex/pipex.c OBJS = ${SRCS:%.c=%.o} -FLAGS = -Wall -Werror -Wextra +FLAGS = -Wall -Werror -Wextra -g LIBFT = ./libraries/libft/libft.a HEADER = ./includes/minishell.h \ - ./includes/builtins.h + ./includes/lexor.h \ + ./includes/builtins.h \ + ./includes/utils.h \ + ./includes/parser.h all: $(NAME) diff --git a/a.out b/a.out deleted file mode 100755 index e88938c1961ef01eb9a254d2a6390dcbe6cb1b56..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8528 zcmeHNO=uHA7@gFrL90zYi2tlV5r3fd@6e0VN`nzaqpctX8JcEo0%=;34K@cQP(*_i za`qraJc|dxgCK|^dJ@Eg2ahTdDtInpd~bH9*=&nHZ)e_QVY}1!i$8v^ z6(X`qh-!xrq8lDtEyQi%AQECLJPDUFHFzd5nz($C*>;Px!xa&EKA}*`k;K)J_7XKd z)}9j@V=BTpE2Q+?8Lw3v=5w$B@}1Kq#GS2jf@%;-S@O#1(o!nSm)CrobcMlYKveUs z(FvbVX5IX>TSP9*SJiyIx}KQ2FWIvBjhDP)t}vks!hDZ4pIIYu(Av1sA!WI6BUi}C zT;ckZ!tMLTuT(KnFFA3fZ3nY zE%%cBcY(Ra=x+b{UXM5h4;pLfK^=nqj2n)B-*A2-66g|L@V;`X*q6_x`!bb$2AH-T z&Ut)%xo@ufS--45zw_zTP;crOat=HK7aP|pXRNgB6di{UL)s-ffc2Ay&w0>z-+nmz zxQ58$7c3*vnhWMLTq$|(Wbf%-%qy2h5o|x4{8h(a*FlY`mo=+^Rlq7>6|f3e1*`&A zf&X8DE6Li|-=DiMGNwi|+X@uYmpaCf|GsV?={8tnW2=Bw zz$#!BunJfOtO8a6tAJI&Dqt0`3RngHEd_e^#D+QJo&g@tsl#xY#`!P~uP+s&by1A< z49M|pCRY>yTL30pZ~R7v$N`$h*|c{Z$XG<;oU=ntW~Dphj+Z?bXW?U{+dVm!D~LEh zQmhj5yL7z?nC_1FCwu( +#+ */ /* +#+ */ /* Created: 2022/02/15 15:20:00 by mgraaf #+# #+# */ -/* Updated: 2022/02/17 10:23:37 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/02/17 11:25:57 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,7 +17,6 @@ //builtins void change_path(t_tools *tools); -void *builtin_arr(char *str); int mini_echo(t_tools *tools); diff --git a/includes/lexor.h b/includes/lexor.h new file mode 100644 index 0000000..e12be7d --- /dev/null +++ b/includes/lexor.h @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* lexor.h :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/02/17 17:55:06 by mgraaf #+# #+# */ +/* Updated: 2022/02/18 10:42:48 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#ifndef LEXOR_H +# define LEXOR_H +# include "minishell.h" + +// typedef struct s_lexor +// { +// char *str; +// t_tokens token; +// struct s_lexor *next; +// } t_lexor; + +//handle_tokens + + +#endif \ No newline at end of file diff --git a/includes/minishell.h b/includes/minishell.h index dfbee16..c402672 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/02/15 17:42:00 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/17 18:12:59 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,22 +19,18 @@ # include # include # include "../srcs/pipex/pipex.h" -//# include "builtins.h" - -typedef struct s_tools -{ - char **args; - char **paths; - char **envp; - int in; - int out; - int err; - char *pwd; - char *old_pwd; - -} t_tools; +# include "parser.h" +# include "lexor.h" +# include "utils.h" +# include "builtins.h" int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); +//builtins +int (*builtin_arr(char *str))(t_tools *tools); + +int token_reader(t_tools *tools); + + #endif \ No newline at end of file diff --git a/includes/parser.h b/includes/parser.h new file mode 100644 index 0000000..535b66f --- /dev/null +++ b/includes/parser.h @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* parser.h :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ +/* Updated: 2022/02/18 11:00:17 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#ifndef PARSER_H +# define PARSER_H +# include "minishell.h" + +typedef enum s_tokens +{ + PIPE = 1, + NEW_LINE, + GREAT, + GREAT_GREAT, + LESS, + LESS_LESS, +} t_tokens; + +typedef struct s_tools +{ + char *args; + char **paths; + char **envp; + int in; + int out; + int err; + char *pwd; + char *old_pwd; + int pipes; +} t_tools; + +typedef struct s_simple_cmds +{ + char *str; + int token; + int (*f)(t_tools *); + struct s_simple_cmds *next; +} t_simple_cmds; + +int parse_envp(t_tools *tools); +int find_pwd(t_tools *tools); + +#endif \ No newline at end of file diff --git a/includes/utils.h b/includes/utils.h new file mode 100644 index 0000000..96fc887 --- /dev/null +++ b/includes/utils.h @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* utils.h :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/02/17 15:36:23 by mgraaf #+# #+# */ +/* Updated: 2022/02/18 10:56:44 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#ifndef UTILS_H +# define UTILS_H +# include "minishell.h" + +typedef struct s_lexor +{ + char *str; + t_tokens token; + struct s_lexor *next; +} t_lexor; + +//t_simple_cmds_utils +t_simple_cmds *ft_simple_cmdsnew(char *str, int token, int (*f)(t_tools *)); +void ft_simple_cmdsadd_back(t_simple_cmds **lst, t_simple_cmds *new); +void ft_simple_cmdsdelone(t_simple_cmds **lst, char key); +void ft_simple_cmdsclear(t_simple_cmds **lst); +t_simple_cmds *ft_simple_cmdslast(t_simple_cmds *map); + +//t_lexor_utils +t_lexor *ft_lexornew(char *str, int token); +void ft_lexoradd_back(t_lexor **lst, t_lexor *new); +void ft_lexordelone(t_lexor **lst, char key); +void ft_lexorclear(t_lexor **lst); +t_lexor *ft_lexorlast(t_lexor *map); + +// +int add_node(char *str, t_tokens token, t_lexor **lexor_list); +t_tokens check_token(int c); +int handle_token(char *str, int i, t_lexor **lexor_list); + +#endif \ No newline at end of file diff --git a/libraries/libft/Makefile b/libraries/libft/Makefile index c042847..0272d93 100644 --- a/libraries/libft/Makefile +++ b/libraries/libft/Makefile @@ -62,7 +62,7 @@ BONUS_OBJFILES = ${BONUS_SRCFILES:%.c=%.o} HEADERFILES := libft.h -CFLAGS = -Wall -Wextra -Werror +CFLAGS = -Wall -Wextra -Werror -g ifdef WITH_BONUS ALL_OBJS = $(OBJFILES) $(BONUS_OBJFILES) diff --git a/srcs/builtins/builtins.c b/srcs/builtins/builtins.c index 5a37c31..61ad615 100644 --- a/srcs/builtins/builtins.c +++ b/srcs/builtins/builtins.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 11:42:32 by mgraaf #+# #+# */ -/* Updated: 2022/02/17 10:23:26 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/02/17 14:13:18 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,10 +15,10 @@ void change_path(t_tools *tools) { tools->old_pwd = tools->pwd; - tools->pwd = getcwd(NULL, 200); + tools->pwd = getcwd(NULL, sizeof(NULL)); } -void *builtin_arr(char *str) +int (*builtin_arr(char *str))(t_tools *tools) { static void *builtins[7][2] = { {"echo", mini_echo}, @@ -34,7 +34,7 @@ void *builtin_arr(char *str) i = 0; while (i < 7) { - if (ft_strncmp(builtins[i][0], str, strlen(builtins[i][0]))) + if (!ft_strncmp(builtins[i][0], str, strlen(builtins[i][0]))) return (builtins[i][1]); i++; } diff --git a/srcs/builtins/mini_cd.c b/srcs/builtins/mini_cd.c index 2a2c54a..88c4379 100644 --- a/srcs/builtins/mini_cd.c +++ b/srcs/builtins/mini_cd.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:17:04 by mgraaf #+# #+# */ -/* Updated: 2022/02/15 18:03:56 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/17 12:48:37 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,7 +14,8 @@ int mini_cd(t_tools *tools) { - chdir(tools->args[1]); + if (chdir(tools->args[1])) + perror("cd"); change_path(tools); - return (0); + return (1); } diff --git a/srcs/builtins/mini_echo.c b/srcs/builtins/mini_echo.c index ab48f3a..ed1fbb1 100644 --- a/srcs/builtins/mini_echo.c +++ b/srcs/builtins/mini_echo.c @@ -6,17 +6,30 @@ /* By: maiadegraaf args[1], "-n", ft_strlen(tools->args[1]))) - printf("%s", tools->args[2]); + if (!ft_strncmp(tools->args[1], "-n", ft_strlen(tools->args[1]))) + print_lines(2, tools->args); else - printf("%s\n", tools->args[2]); + { + print_lines(1, tools->args); + printf("\n"); + } return (1); } diff --git a/srcs/builtins/mini_env.c b/srcs/builtins/mini_env.c index baeeda6..fc9ed68 100644 --- a/srcs/builtins/mini_env.c +++ b/srcs/builtins/mini_env.c @@ -6,7 +6,7 @@ /* By: maiadegraaf envp[i]) { if (!ft_strncmp(tools->envp[i], "PWD=", 4)) - printf("%s", tools->pwd); + printf("PWD=%s\n", tools->pwd); else if (!ft_strncmp(tools->envp[i], "OLDPWD=", 7)) - printf("%s", tools->old_pwd); + printf("OLDPWD=%s\n", tools->old_pwd); else - printf("%s", tools->envp[i]); + printf("%s\n", tools->envp[i]); i++; } return (1); diff --git a/srcs/lexor/handle_token.c b/srcs/lexor/handle_token.c new file mode 100644 index 0000000..7570d3d --- /dev/null +++ b/srcs/lexor/handle_token.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* handle_token.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/02/18 10:27:43 by mgraaf #+# #+# */ +/* Updated: 2022/02/18 11:00:39 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "utils.h" +#include "lexor.h" + +t_tokens check_token(int c) +{ + static int token_arr[4][2] = { + {'|', PIPE}, + {'\n', NEW_LINE}, + {'>', GREAT}, + {'<', LESS}, + }; + int i; + + i = 0; + while (i < 4) + { + if (token_arr[i][0] == c) + return (token_arr[i][1]); + i++; + } + return (0); +} + +int handle_token(char *str, int i, t_lexor **lexor_list) +{ + t_tokens token; + + token = check_token(str[i]); + if (token == GREAT && check_token(str[i + 1]) == GREAT) + { + if (!add_node(NULL, GREAT_GREAT, lexor_list)) + printf("EMERGENCY!\n"); + return (2); + } + if (token == LESS && check_token(str[i + 1]) == LESS) + { + if (!add_node(NULL, LESS_LESS, lexor_list)) + printf("EMERGENCY!\n"); + return (2); + } + else if (token) + { + if (!add_node(NULL, token, lexor_list)) + printf("EMERGENCY!\n"); + return (1); + } + return (0); +} diff --git a/srcs/lexor/token_reader.c b/srcs/lexor/token_reader.c new file mode 100644 index 0000000..d017e3f --- /dev/null +++ b/srcs/lexor/token_reader.c @@ -0,0 +1,92 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* token_reader.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/02/17 17:11:20 by mgraaf #+# #+# */ +/* Updated: 2022/02/18 11:05:42 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "lexor.h" + +int skip_spaces(char *str, int i) +{ + int j; + + j = 0; + while (str[i + j] == ' ') + j++; + return (j); +} + +int add_node(char *str, t_tokens token, t_lexor **lexor_list) +{ + t_lexor *node; + + node = ft_lexornew(str, token); + if (!node) + { + printf("EMERGENCY!\n"); + return (0); + } + ft_lexoradd_back(lexor_list, node); + return (1); +} + +int handle_quotes(int i, char *str, char del, t_lexor **lexor_list) +{ + int j; + + j = 0; + if (str[i + j] == del || del == ' ') + { + j++; + while (str[i + j] != del && str[i + j]) + j++; + if (!add_node(ft_substr(str, i, j - 1), 0, lexor_list)) + printf("EMERGENCY!\n"); + } + return (j); +} + +int read_words(int i, char *str, t_lexor **lexor_list) +{ + int j; + + j = 0; + while (str[i + j] != ' ' && str[i + j] && !check_token(str[i + j])) + j++; + if (!add_node(ft_substr(str, i, j), 0, lexor_list)) + printf("EMERGENCY!\n"); + return (j); +} + +int token_reader(t_tools *tools) +{ + int i; + t_lexor *lexor_list; + + i = 0; + lexor_list = NULL; + while (tools->args[i]) + { + if (tools->args[i] == 34) + i += handle_quotes(i, tools->args, 34, &lexor_list); + else if (tools->args[i] == 39) + i += handle_quotes(i, tools->args, 39, &lexor_list); + else if (check_token(tools->args[i])) + i += handle_token(tools->args, i, &lexor_list); + else + i += read_words(i, tools->args, &lexor_list); + i += skip_spaces(tools->args, i); + } + while (lexor_list) + { + printf("str = %s \t token = %d\n", lexor_list->str, lexor_list->token); + lexor_list = lexor_list->next; + } + return (0); +} diff --git a/srcs/main.c b/srcs/main.c index 3ffb823..eeabbe7 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,35 +6,36 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/02/17 10:23:49 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/02/17 18:00:06 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -int find_cmd(t_tools *tools) -{ - int i; - char *mycmd; +// int find_cmd(t_tools *tools) +// { +// int i; +// char *mycmd; - i = 0; - while ((tools->paths)[i]) - { - mycmd = ft_strjoin((tools->paths)[i], (tools->args)[0]); - if (!access(mycmd, F_OK)) - execve(mycmd, tools->args, tools->envp); - free(mycmd); - i++; - } - return (1); -} +// i = 0; +// while ((tools->paths)[i]) +// { +// mycmd = ft_strjoin((tools->paths)[i], (tools->args)[0]); +// if (!access(mycmd, F_OK)) +// execve(mycmd, tools->args, tools->envp); +// free(mycmd); +// i++; +// } +// return (1); +// } int main(int argc, char **argv, char **envp) { char *line; t_tools tools; - pid_t process; - int status; + // int (*f)(t_tools *); + // pid_t process; + // int status; if (argc != 1 || argv[1]) { @@ -47,12 +48,17 @@ int main(int argc, char **argv, char **envp) { line = readline(">> "); add_history(line); - if (builtins_arr) - tools.args = ft_split(line, ' '); - process = fork(); - if (process == 0) - find_cmd(&tools); - waitpid(process, &status, 0); + tools.args = line; + token_reader(&tools); + // f = builtin_arr(tools.args[0]); + // if (!f(&tools)) + // { + // printf("HELLO\n"); + // process = fork(); + // if (process == 0) + // find_cmd(&tools); + // waitpid(process, &status, 0); + // } free(line); } return (0); diff --git a/srcs/parse_envp.c b/srcs/parse_envp.c index 466be6c..46dd856 100644 --- a/srcs/parse_envp.c +++ b/srcs/parse_envp.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2021/12/17 16:16:57 by mgraaf #+# #+# */ -/* Updated: 2022/02/15 16:19:13 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/17 12:19:04 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ int find_pwd(t_tools *tools) tools->pwd = ft_substr(tools->envp[i], 4, ft_strlen(tools->envp[i]) - 4); if (!ft_strncmp(tools->envp[i], "OLDPWD=", 7)) - tools->pwd = ft_substr(tools->envp[i], + tools->old_pwd = ft_substr(tools->envp[i], 7, ft_strlen(tools->envp[i]) - 7); i++; } diff --git a/srcs/parser/parse_args.c b/srcs/parser/parse_args.c new file mode 100644 index 0000000..4ccb8df --- /dev/null +++ b/srcs/parser/parse_args.c @@ -0,0 +1,13 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* parse_args.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ +/* Updated: 2022/02/17 17:11:13 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" diff --git a/srcs/utils/t_lexor_utils.c b/srcs/utils/t_lexor_utils.c new file mode 100644 index 0000000..659fb81 --- /dev/null +++ b/srcs/utils/t_lexor_utils.c @@ -0,0 +1,95 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* t_lexor_utils.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ +/* Updated: 2022/02/17 15:36:36 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "utils.h" + +t_lexor *ft_lexornew(char *str, int token) +{ + t_lexor *new_element; + + new_element = (t_lexor *)malloc(sizeof(t_lexor)); + if (!new_element) + return (0); + new_element->str = str; + new_element->token = token; + new_element->next = NULL; + return (new_element); +} + +void ft_lexoradd_back(t_lexor **lst, t_lexor *new) +{ + t_lexor *tmp; + + tmp = *lst; + if (!(*lst)) + { + *lst = new; + return ; + } + while (tmp->next != NULL) + tmp = tmp->next; + tmp->next = new; +} + +void ft_lexordelone(t_lexor **lst, char key) +{ + t_lexor *node; + t_lexor *prev; + t_lexor *start; + + start = *lst; + node = start; + if ((*lst)->str[1] == key) + { + *lst = node->next; + free(node); + return ; + } + while (node && node->str[1] != key) + { + prev = node; + node = node->next; + } + prev->next = node->next; + free(node); + *lst = start; +} + +void ft_lexorclear(t_lexor **lst) +{ + t_lexor *tmp; + + if (!*lst) + return ; + while (*lst) + { + tmp = (*lst)->next; + free(*lst); + *lst = tmp; + } + *lst = NULL; +} + +t_lexor *ft_lexorlast(t_lexor *map) +{ + int i; + + i = 0; + if (!map) + return (NULL); + while (map->next != NULL) + { + map = map->next; + i++; + } + return (map); +} diff --git a/srcs/utils/t_simple_cmds_utils.c b/srcs/utils/t_simple_cmds_utils.c new file mode 100644 index 0000000..54fefce --- /dev/null +++ b/srcs/utils/t_simple_cmds_utils.c @@ -0,0 +1,96 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* t_simple_cmds_utils.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ +/* Updated: 2022/02/17 18:03:31 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "utils.h" + +t_simple_cmds *ft_simple_cmdsnew(char *str, int token, int (*f)(t_tools *)) +{ + t_simple_cmds *new_element; + + new_element = (t_simple_cmds *)malloc(sizeof(t_simple_cmds)); + if (!new_element) + return (0); + new_element->str = str; + new_element->token = token; + new_element->f = f; + new_element->next = NULL; + return (new_element); +} + +void ft_simple_cmdsadd_back(t_simple_cmds **lst, t_simple_cmds *new) +{ + t_simple_cmds *tmp; + + tmp = *lst; + if (!(*lst)) + { + *lst = new; + return ; + } + while (tmp->next != NULL) + tmp = tmp->next; + tmp->next = new; +} + +void ft_simple_cmdsdelone(t_simple_cmds **lst, char key) +{ + t_simple_cmds *node; + t_simple_cmds *prev; + t_simple_cmds *start; + + start = *lst; + node = start; + if ((*lst)->str[1] == key) + { + *lst = node->next; + free(node); + return ; + } + while (node && node->str[1] != key) + { + prev = node; + node = node->next; + } + prev->next = node->next; + free(node); + *lst = start; +} + +void ft_simple_cmdsclear(t_simple_cmds **lst) +{ + t_simple_cmds *tmp; + + if (!*lst) + return ; + while (*lst) + { + tmp = (*lst)->next; + free(*lst); + *lst = tmp; + } + *lst = NULL; +} + +t_simple_cmds *ft_simple_cmdslast(t_simple_cmds *map) +{ + int i; + + i = 0; + if (!map) + return (NULL); + while (map->next != NULL) + { + map = map->next; + i++; + } + return (map); +} From 243152f4c921ef3ec509bf15e9ad23fb70d38506 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Fri, 18 Feb 2022 11:38:14 +0100 Subject: [PATCH 016/163] quote edit --- includes/minishell.h | 7 +++---- srcs/lexor/token_reader.c | 12 +++++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index c402672..04a327e 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/02/17 18:12:59 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/18 11:06:35 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -28,9 +28,8 @@ int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); //builtins -int (*builtin_arr(char *str))(t_tools *tools); - -int token_reader(t_tools *tools); +int (*builtin_arr(char *str))(t_tools *tools); +int token_reader(t_tools *tools); #endif \ No newline at end of file diff --git a/srcs/lexor/token_reader.c b/srcs/lexor/token_reader.c index d017e3f..75166e9 100644 --- a/srcs/lexor/token_reader.c +++ b/srcs/lexor/token_reader.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:11:20 by mgraaf #+# #+# */ -/* Updated: 2022/02/18 11:05:42 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/18 11:34:49 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -41,13 +41,14 @@ int handle_quotes(int i, char *str, char del, t_lexor **lexor_list) int j; j = 0; - if (str[i + j] == del || del == ' ') + if (str[i + j] == del) { j++; while (str[i + j] != del && str[i + j]) j++; - if (!add_node(ft_substr(str, i, j - 1), 0, lexor_list)) + if (!add_node(ft_substr(str, i, j + 1), 0, lexor_list)) printf("EMERGENCY!\n"); + j++; } return (j); } @@ -57,7 +58,8 @@ int read_words(int i, char *str, t_lexor **lexor_list) int j; j = 0; - while (str[i + j] != ' ' && str[i + j] && !check_token(str[i + j])) + while (str[i + j] != ' ' && str[i + j] + && !(check_token(str[i + j]) && str[i + j] == 34 && str[i + j] == 39)) j++; if (!add_node(ft_substr(str, i, j), 0, lexor_list)) printf("EMERGENCY!\n"); @@ -73,6 +75,7 @@ int token_reader(t_tools *tools) lexor_list = NULL; while (tools->args[i]) { + i += skip_spaces(tools->args, i); if (tools->args[i] == 34) i += handle_quotes(i, tools->args, 34, &lexor_list); else if (tools->args[i] == 39) @@ -81,7 +84,6 @@ int token_reader(t_tools *tools) i += handle_token(tools->args, i, &lexor_list); else i += read_words(i, tools->args, &lexor_list); - i += skip_spaces(tools->args, i); } while (lexor_list) { From b11314decc4ee82ed58405736a05e52bad7d763b Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Fri, 18 Feb 2022 14:09:23 +0100 Subject: [PATCH 017/163] fix quotes --- srcs/lexor/token_reader.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/srcs/lexor/token_reader.c b/srcs/lexor/token_reader.c index d017e3f..ac21900 100644 --- a/srcs/lexor/token_reader.c +++ b/srcs/lexor/token_reader.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:11:20 by mgraaf #+# #+# */ -/* Updated: 2022/02/18 11:05:42 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/18 14:07:47 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -46,10 +46,10 @@ int handle_quotes(int i, char *str, char del, t_lexor **lexor_list) j++; while (str[i + j] != del && str[i + j]) j++; - if (!add_node(ft_substr(str, i, j - 1), 0, lexor_list)) + if (!add_node(ft_substr(str, i, j + 1), 0, lexor_list)) printf("EMERGENCY!\n"); } - return (j); + return (j + 1); } int read_words(int i, char *str, t_lexor **lexor_list) @@ -73,6 +73,7 @@ int token_reader(t_tools *tools) lexor_list = NULL; while (tools->args[i]) { + printf("%d\n", i); if (tools->args[i] == 34) i += handle_quotes(i, tools->args, 34, &lexor_list); else if (tools->args[i] == 39) From 379a55ccedef045600830c0467ed29f0074f36be Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Fri, 18 Feb 2022 14:31:04 +0100 Subject: [PATCH 018/163] parser begin --- includes/minishell.h | 4 ++-- includes/utils.h | 3 ++- srcs/lexor/token_reader.c | 12 +++--------- srcs/main.c | 20 +++++++++----------- srcs/parser/parse_args.c | 7 ++++++- 5 files changed, 22 insertions(+), 24 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index c402672..df95efb 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/02/17 18:12:59 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/18 14:18:25 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -30,7 +30,7 @@ int find_pwd(t_tools *tools); //builtins int (*builtin_arr(char *str))(t_tools *tools); -int token_reader(t_tools *tools); + #endif \ No newline at end of file diff --git a/includes/utils.h b/includes/utils.h index 96fc887..8ad99f7 100644 --- a/includes/utils.h +++ b/includes/utils.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:36:23 by mgraaf #+# #+# */ -/* Updated: 2022/02/18 10:56:44 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/18 14:18:33 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -36,6 +36,7 @@ void ft_lexorclear(t_lexor **lst); t_lexor *ft_lexorlast(t_lexor *map); // +t_lexor *token_reader(t_tools *tools); int add_node(char *str, t_tokens token, t_lexor **lexor_list); t_tokens check_token(int c); int handle_token(char *str, int i, t_lexor **lexor_list); diff --git a/srcs/lexor/token_reader.c b/srcs/lexor/token_reader.c index ac21900..db78618 100644 --- a/srcs/lexor/token_reader.c +++ b/srcs/lexor/token_reader.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:11:20 by mgraaf #+# #+# */ -/* Updated: 2022/02/18 14:07:47 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/18 14:18:59 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -64,7 +64,7 @@ int read_words(int i, char *str, t_lexor **lexor_list) return (j); } -int token_reader(t_tools *tools) +t_lexor *token_reader(t_tools *tools) { int i; t_lexor *lexor_list; @@ -73,7 +73,6 @@ int token_reader(t_tools *tools) lexor_list = NULL; while (tools->args[i]) { - printf("%d\n", i); if (tools->args[i] == 34) i += handle_quotes(i, tools->args, 34, &lexor_list); else if (tools->args[i] == 39) @@ -84,10 +83,5 @@ int token_reader(t_tools *tools) i += read_words(i, tools->args, &lexor_list); i += skip_spaces(tools->args, i); } - while (lexor_list) - { - printf("str = %s \t token = %d\n", lexor_list->str, lexor_list->token); - lexor_list = lexor_list->next; - } - return (0); + return (lexor_list); } diff --git a/srcs/main.c b/srcs/main.c index eeabbe7..549a0ec 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/02/17 18:00:06 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/18 14:21:02 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -33,6 +33,7 @@ int main(int argc, char **argv, char **envp) { char *line; t_tools tools; + t_lexor *lexor_list = NULL; // int (*f)(t_tools *); // pid_t process; // int status; @@ -49,16 +50,13 @@ int main(int argc, char **argv, char **envp) line = readline(">> "); add_history(line); tools.args = line; - token_reader(&tools); - // f = builtin_arr(tools.args[0]); - // if (!f(&tools)) - // { - // printf("HELLO\n"); - // process = fork(); - // if (process == 0) - // find_cmd(&tools); - // waitpid(process, &status, 0); - // } + lexor_list = token_reader(&tools); + + while (lexor_list) + { + printf("str = %s \t token = %d\n", lexor_list->str, lexor_list->token); + lexor_list = lexor_list->next; + } free(line); } return (0); diff --git a/srcs/parser/parse_args.c b/srcs/parser/parse_args.c index 4ccb8df..85867f1 100644 --- a/srcs/parser/parse_args.c +++ b/srcs/parser/parse_args.c @@ -6,8 +6,13 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/02/17 17:11:13 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/18 14:24:41 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" + +void parser(t_lexor *lexer) +{ + +} \ No newline at end of file From 25ef466c8b644ac16997be77d60065d000b73983 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Mon, 21 Feb 2022 11:44:55 +0100 Subject: [PATCH 019/163] PS2 newline --- Makefile | 1 + includes/minishell.h | 6 ++---- includes/parser.h | 3 ++- includes/utils.h | 2 +- srcs/lexor/handle_token.c | 7 ++++--- srcs/lexor/token_reader.c | 2 +- srcs/main.c | 8 +++++--- srcs/parser/{parse_args.c => parser.c} | 11 +++++++---- srcs/utils/utils.c | 27 ++++++++++++++++++++++++++ 9 files changed, 50 insertions(+), 17 deletions(-) rename srcs/parser/{parse_args.c => parser.c} (79%) create mode 100644 srcs/utils/utils.c diff --git a/Makefile b/Makefile index 6f32838..c4a9a8d 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,7 @@ SRCS = ./srcs/main.c \ ./srcs/parse_envp.c \ ./srcs/utils/t_simple_cmds_utils.c \ ./srcs/utils/t_lexor_utils.c \ + ./srcs/utils/utils.c \ ./srcs/lexor/token_reader.c \ ./srcs/lexor/handle_token.c \ # ./srcs/builtins/mini_cd.c \ diff --git a/includes/minishell.h b/includes/minishell.h index df95efb..07e62e4 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/02/18 14:18:25 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/21 11:17:54 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -26,11 +26,9 @@ int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); +int count_quotes(char *line); //builtins int (*builtin_arr(char *str))(t_tools *tools); - - - #endif \ No newline at end of file diff --git a/includes/parser.h b/includes/parser.h index 535b66f..62df96e 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/02/18 11:00:17 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/21 09:59:29 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ typedef enum s_tokens GREAT_GREAT, LESS, LESS_LESS, + PS2, } t_tokens; typedef struct s_tools diff --git a/includes/utils.h b/includes/utils.h index 8ad99f7..1fabf93 100644 --- a/includes/utils.h +++ b/includes/utils.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:36:23 by mgraaf #+# #+# */ -/* Updated: 2022/02/18 14:18:33 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/21 11:04:02 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/srcs/lexor/handle_token.c b/srcs/lexor/handle_token.c index 7570d3d..82a08cd 100644 --- a/srcs/lexor/handle_token.c +++ b/srcs/lexor/handle_token.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/18 10:27:43 by mgraaf #+# #+# */ -/* Updated: 2022/02/18 11:00:39 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/21 10:06:44 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,16 +15,17 @@ t_tokens check_token(int c) { - static int token_arr[4][2] = { + static int token_arr[5][2] = { {'|', PIPE}, {'\n', NEW_LINE}, {'>', GREAT}, {'<', LESS}, + {92, PS2}, }; int i; i = 0; - while (i < 4) + while (i < 5) { if (token_arr[i][0] == c) return (token_arr[i][1]); diff --git a/srcs/lexor/token_reader.c b/srcs/lexor/token_reader.c index db78618..f10dc4e 100644 --- a/srcs/lexor/token_reader.c +++ b/srcs/lexor/token_reader.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:11:20 by mgraaf #+# #+# */ -/* Updated: 2022/02/18 14:18:59 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/21 11:07:23 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/srcs/main.c b/srcs/main.c index 549a0ec..2048fd3 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/02/18 14:21:02 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/21 11:44:14 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -47,11 +47,13 @@ int main(int argc, char **argv, char **envp) parse_envp(&tools); while (1) { - line = readline(">> "); + line = readline("minishell>> "); add_history(line); + while (count_quotes(line) % 2 != 0) + line = ft_strjoin(line, readline("> ")); tools.args = line; lexor_list = token_reader(&tools); - + // parser(lexor_list) while (lexor_list) { printf("str = %s \t token = %d\n", lexor_list->str, lexor_list->token); diff --git a/srcs/parser/parse_args.c b/srcs/parser/parser.c similarity index 79% rename from srcs/parser/parse_args.c rename to srcs/parser/parser.c index 85867f1..8f48026 100644 --- a/srcs/parser/parse_args.c +++ b/srcs/parser/parser.c @@ -1,18 +1,21 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* parse_args.c :+: :+: */ +/* parser.c :+: :+: */ /* +:+ */ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/02/18 14:24:41 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/21 10:15:24 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -void parser(t_lexor *lexer) +void parser(t_lexor *lexor_list) { - + while (lexor_list->token == 0) + { + + } } \ No newline at end of file diff --git a/srcs/utils/utils.c b/srcs/utils/utils.c new file mode 100644 index 0000000..0119d67 --- /dev/null +++ b/srcs/utils/utils.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* utils.c :+: :+: */ +/* +:+ */ +/* By: fpolycar +#+ */ +/* +#+ */ +/* Created: 2022/02/21 11:17:26 by fpolycar #+# #+# */ +/* Updated: 2022/02/21 11:17:31 by fpolycar ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +int count_quotes(char *line) +{ + int i; + int nb; + + i = 0; + nb = 0; + while (line[i]) + { + if (line[i] == 34 || line[i] == 39) + nb++; + i++; + } + return (nb); +} From b1726c41da82053977afa0a8fdb5dbe9f82340d5 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Mon, 21 Feb 2022 15:19:01 +0100 Subject: [PATCH 020/163] parser does pipes but > < are still WIP --- Makefile | 15 +++---- dirlist | 11 +++++ includes/minishell.h | 4 +- includes/parser.h | 15 +++++-- includes/utils.h | 13 ++---- infile | 7 ++++ jkhds | 0 outfile | 0 srcs/builtins/mini_cd.c | 4 +- srcs/builtins/mini_echo.c | 8 ++-- srcs/lexor/token_reader.c | 5 ++- srcs/main.c | 15 ++++--- srcs/parser/parser.c | 69 ++++++++++++++++++++++++++++++-- srcs/utils/t_simple_cmds_utils.c | 11 +++-- test | 2 + test2 | 0 16 files changed, 131 insertions(+), 48 deletions(-) create mode 100644 dirlist create mode 100644 infile create mode 100644 jkhds create mode 100644 outfile create mode 100644 test create mode 100644 test2 diff --git a/Makefile b/Makefile index c4a9a8d..8813a78 100644 --- a/Makefile +++ b/Makefile @@ -7,14 +7,15 @@ SRCS = ./srcs/main.c \ ./srcs/utils/utils.c \ ./srcs/lexor/token_reader.c \ ./srcs/lexor/handle_token.c \ - # ./srcs/builtins/mini_cd.c \ + ./srcs/parser/parser.c \ ./srcs/builtins/builtins.c \ - # ./srcs/builtins/mini_echo.c \ - # ./srcs/builtins/mini_exit.c \ - # ./srcs/builtins/mini_export.c \ - # ./srcs/builtins/mini_pwd.c \ - # ./srcs/builtins/mini_unset.c \ - # ./srcs/builtins/mini_env.c \ + ./srcs/builtins/mini_cd.c \ + ./srcs/builtins/mini_echo.c \ + ./srcs/builtins/mini_exit.c \ + ./srcs/builtins/mini_export.c \ + ./srcs/builtins/mini_pwd.c \ + ./srcs/builtins/mini_unset.c \ + ./srcs/builtins/mini_env.c \ # ./srcs/parser/parse_args.c \ # ./srcs/pipex/pipex.c diff --git a/dirlist b/dirlist new file mode 100644 index 0000000..9cc08b8 --- /dev/null +++ b/dirlist @@ -0,0 +1,11 @@ +Makefile +dirlist +includes +infile +jkhds +libraries +minishell +outfile +srcs +test +test2 diff --git a/includes/minishell.h b/includes/minishell.h index f28497e..622ceb3 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 11:47:30 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/21 13:52:10 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -20,8 +20,8 @@ # include # include "../srcs/pipex/pipex.h" # include "parser.h" -# include "lexor.h" # include "utils.h" +# include "lexor.h" # include "builtins.h" int parse_envp(t_tools *tools); diff --git a/includes/parser.h b/includes/parser.h index 62df96e..9409591 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 09:59:29 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/21 13:53:33 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -25,6 +25,13 @@ typedef enum s_tokens PS2, } t_tokens; +typedef struct s_lexor +{ + char *str; + t_tokens token; + struct s_lexor *next; +} t_lexor; + typedef struct s_tools { char *args; @@ -40,13 +47,13 @@ typedef struct s_tools typedef struct s_simple_cmds { - char *str; - int token; - int (*f)(t_tools *); + char **str; + int (*builtin)(t_tools *); struct s_simple_cmds *next; } t_simple_cmds; int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); +void parser(t_lexor *lexor_list); #endif \ No newline at end of file diff --git a/includes/utils.h b/includes/utils.h index 1fabf93..aeee436 100644 --- a/includes/utils.h +++ b/includes/utils.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:36:23 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 11:04:02 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/21 13:52:37 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,15 +14,10 @@ # define UTILS_H # include "minishell.h" -typedef struct s_lexor -{ - char *str; - t_tokens token; - struct s_lexor *next; -} t_lexor; + //t_simple_cmds_utils -t_simple_cmds *ft_simple_cmdsnew(char *str, int token, int (*f)(t_tools *)); +t_simple_cmds *ft_simple_cmdsnew(char **str, int (*builtin)(t_tools *)); void ft_simple_cmdsadd_back(t_simple_cmds **lst, t_simple_cmds *new); void ft_simple_cmdsdelone(t_simple_cmds **lst, char key); void ft_simple_cmdsclear(t_simple_cmds **lst); @@ -36,7 +31,7 @@ void ft_lexorclear(t_lexor **lst); t_lexor *ft_lexorlast(t_lexor *map); // -t_lexor *token_reader(t_tools *tools); +t_lexor *token_reader(t_tools *tools); int add_node(char *str, t_tokens token, t_lexor **lexor_list); t_tokens check_token(int c); int handle_token(char *str, int i, t_lexor **lexor_list); diff --git a/infile b/infile new file mode 100644 index 0000000..ea0879e --- /dev/null +++ b/infile @@ -0,0 +1,7 @@ +efje +afjkjls +skvvjkfss +djcnsdfjasf +sfhsa +qjshfas +dsdkscjkf diff --git a/jkhds b/jkhds new file mode 100644 index 0000000..e69de29 diff --git a/outfile b/outfile new file mode 100644 index 0000000..e69de29 diff --git a/srcs/builtins/mini_cd.c b/srcs/builtins/mini_cd.c index 88c4379..e75edb4 100644 --- a/srcs/builtins/mini_cd.c +++ b/srcs/builtins/mini_cd.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:17:04 by mgraaf #+# #+# */ -/* Updated: 2022/02/17 12:48:37 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/21 13:46:52 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ int mini_cd(t_tools *tools) { - if (chdir(tools->args[1])) + if (chdir(&tools->args[1])) perror("cd"); change_path(tools); return (1); diff --git a/srcs/builtins/mini_echo.c b/srcs/builtins/mini_echo.c index ed1fbb1..a82ca30 100644 --- a/srcs/builtins/mini_echo.c +++ b/srcs/builtins/mini_echo.c @@ -6,7 +6,7 @@ /* By: maiadegraaf args[1], "-n", ft_strlen(tools->args[1]))) - print_lines(2, tools->args); + if (!ft_strncmp(&tools->args[1], "-n", ft_strlen(&tools->args[1]))) + print_lines(2, &tools->args); else { - print_lines(1, tools->args); + print_lines(1, &tools->args); printf("\n"); } return (1); diff --git a/srcs/lexor/token_reader.c b/srcs/lexor/token_reader.c index 97182e0..0c771ee 100644 --- a/srcs/lexor/token_reader.c +++ b/srcs/lexor/token_reader.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:11:20 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 11:46:33 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/21 14:56:30 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -59,7 +59,8 @@ int read_words(int i, char *str, t_lexor **lexor_list) j = 0; while (str[i + j] != ' ' && str[i + j] - && !(check_token(str[i + j]) && str[i + j] == 34 && str[i + j] == 39)) + && !(check_token(str[i + j])) + && !(str[i + j] == 34 && str[i + j] == 39)) j++; if (!add_node(ft_substr(str, i, j), 0, lexor_list)) printf("EMERGENCY!\n"); diff --git a/srcs/main.c b/srcs/main.c index 2048fd3..df6ec66 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 11:44:14 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/21 13:51:50 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,7 +34,6 @@ int main(int argc, char **argv, char **envp) char *line; t_tools tools; t_lexor *lexor_list = NULL; - // int (*f)(t_tools *); // pid_t process; // int status; @@ -53,12 +52,12 @@ int main(int argc, char **argv, char **envp) line = ft_strjoin(line, readline("> ")); tools.args = line; lexor_list = token_reader(&tools); - // parser(lexor_list) - while (lexor_list) - { - printf("str = %s \t token = %d\n", lexor_list->str, lexor_list->token); - lexor_list = lexor_list->next; - } + parser(lexor_list); + // while (lexor_list) + // { + // printf("str = %s \t token = %d\n", lexor_list->str, lexor_list->token); + // lexor_list = lexor_list->next; + // } free(line); } return (0); diff --git a/srcs/parser/parser.c b/srcs/parser/parser.c index 8f48026..b24c669 100644 --- a/srcs/parser/parser.c +++ b/srcs/parser/parser.c @@ -6,16 +6,77 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 10:15:24 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/21 14:50:02 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" +int count_args(t_lexor *lexor_list) +{ + t_lexor *tmp; + int i; + + i = 0; + tmp = lexor_list; + while (tmp && tmp->token == 0) + { + i++; + tmp = tmp->next; + } + return (i); +} + +t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) +{ + char **str; + char *o_str; + char *tmp; + int i; + + i = 0; + o_str = ft_strdup(lexor_list->str); + while (i++ < arg_size - 1) + { + lexor_list = lexor_list->next; + tmp = ft_strjoin(o_str, ft_strjoin(" ", ft_strdup(lexor_list->str))); + free(o_str); + o_str = ft_strdup(tmp); + free(tmp); + } + str = ft_split(o_str, ' '); + return (ft_simple_cmdsnew(str, builtin_arr(str[0]))); +} + void parser(t_lexor *lexor_list) { - while (lexor_list->token == 0) + t_simple_cmds *simple_cmds; + t_simple_cmds *node; + int arg_size; + + simple_cmds = NULL; + while (lexor_list) + { + if (lexor_list->token == PIPE) + lexor_list = lexor_list->next; + arg_size = count_args(lexor_list); + node = initialize_cmd(lexor_list, arg_size); + if (!simple_cmds) + simple_cmds = node; + else + ft_simple_cmdsadd_back(&simple_cmds, node); + while (arg_size--) + lexor_list = lexor_list->next; + } + int i = 0; + while (simple_cmds) { - + printf("\n%d\n", i); + i++; + while(*simple_cmds->str) + printf("%s\n", *simple_cmds->str++); + if (simple_cmds->builtin) + printf("builtin👍\n"); + simple_cmds = simple_cmds->next; } -} \ No newline at end of file +} diff --git a/srcs/utils/t_simple_cmds_utils.c b/srcs/utils/t_simple_cmds_utils.c index 54fefce..02ecad0 100644 --- a/srcs/utils/t_simple_cmds_utils.c +++ b/srcs/utils/t_simple_cmds_utils.c @@ -6,13 +6,13 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/02/17 18:03:31 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/21 13:43:59 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "utils.h" -t_simple_cmds *ft_simple_cmdsnew(char *str, int token, int (*f)(t_tools *)) +t_simple_cmds *ft_simple_cmdsnew(char **str, int (*builtin)(t_tools *)) { t_simple_cmds *new_element; @@ -20,8 +20,7 @@ t_simple_cmds *ft_simple_cmdsnew(char *str, int token, int (*f)(t_tools *)) if (!new_element) return (0); new_element->str = str; - new_element->token = token; - new_element->f = f; + new_element->builtin = builtin; new_element->next = NULL; return (new_element); } @@ -49,13 +48,13 @@ void ft_simple_cmdsdelone(t_simple_cmds **lst, char key) start = *lst; node = start; - if ((*lst)->str[1] == key) + if ((*lst)->str[1][1] == key) { *lst = node->next; free(node); return ; } - while (node && node->str[1] != key) + while (node && node->str[1][1] != key) { prev = node; node = node->next; diff --git a/test b/test new file mode 100644 index 0000000..44ef198 --- /dev/null +++ b/test @@ -0,0 +1,2 @@ +Makefile +libraries diff --git a/test2 b/test2 new file mode 100644 index 0000000..e69de29 From f106e3a78446b2cad730b7718973b04ef4ebd641 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Mon, 21 Feb 2022 15:19:43 +0100 Subject: [PATCH 021/163] rm tester files --- dirlist | 11 ----------- infile | 7 ------- jkhds | 0 outfile | 0 test | 2 -- test2 | 0 6 files changed, 20 deletions(-) delete mode 100644 dirlist delete mode 100644 infile delete mode 100644 jkhds delete mode 100644 outfile delete mode 100644 test delete mode 100644 test2 diff --git a/dirlist b/dirlist deleted file mode 100644 index 9cc08b8..0000000 --- a/dirlist +++ /dev/null @@ -1,11 +0,0 @@ -Makefile -dirlist -includes -infile -jkhds -libraries -minishell -outfile -srcs -test -test2 diff --git a/infile b/infile deleted file mode 100644 index ea0879e..0000000 --- a/infile +++ /dev/null @@ -1,7 +0,0 @@ -efje -afjkjls -skvvjkfss -djcnsdfjasf -sfhsa -qjshfas -dsdkscjkf diff --git a/jkhds b/jkhds deleted file mode 100644 index e69de29..0000000 diff --git a/outfile b/outfile deleted file mode 100644 index e69de29..0000000 diff --git a/test b/test deleted file mode 100644 index 44ef198..0000000 --- a/test +++ /dev/null @@ -1,2 +0,0 @@ -Makefile -libraries diff --git a/test2 b/test2 deleted file mode 100644 index e69de29..0000000 From cf81f2e77b1f0f29897c52704d40cce762ebb708 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Mon, 21 Feb 2022 18:11:30 +0100 Subject: [PATCH 022/163] did I do it? unlikely --- includes/parser.h | 6 ++- includes/utils.h | 7 ++-- srcs/main.c | 4 +- srcs/parser/handle_redirections.c | 14 +++++++ srcs/parser/parser.c | 65 ++++++++++++++++++++++--------- srcs/utils/t_simple_cmds_utils.c | 7 +++- 6 files changed, 75 insertions(+), 28 deletions(-) create mode 100644 srcs/parser/handle_redirections.c diff --git a/includes/parser.h b/includes/parser.h index 9409591..4ea318b 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 13:53:33 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/21 17:02:46 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -49,6 +49,8 @@ typedef struct s_simple_cmds { char **str; int (*builtin)(t_tools *); + int num_redirections; + t_lexor *redirections; struct s_simple_cmds *next; } t_simple_cmds; @@ -56,4 +58,4 @@ int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); void parser(t_lexor *lexor_list); -#endif \ No newline at end of file +#endif diff --git a/includes/utils.h b/includes/utils.h index aeee436..12b5a78 100644 --- a/includes/utils.h +++ b/includes/utils.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:36:23 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 13:52:37 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/21 17:11:45 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,10 +14,9 @@ # define UTILS_H # include "minishell.h" - - //t_simple_cmds_utils -t_simple_cmds *ft_simple_cmdsnew(char **str, int (*builtin)(t_tools *)); +t_simple_cmds *ft_simple_cmdsnew(char **str, int (*builtin)(t_tools *), + int num_redirections, t_lexor *redirections); void ft_simple_cmdsadd_back(t_simple_cmds **lst, t_simple_cmds *new); void ft_simple_cmdsdelone(t_simple_cmds **lst, char key); void ft_simple_cmdsclear(t_simple_cmds **lst); diff --git a/srcs/main.c b/srcs/main.c index df6ec66..0b4f48a 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 13:51:50 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/21 17:28:38 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -46,7 +46,7 @@ int main(int argc, char **argv, char **envp) parse_envp(&tools); while (1) { - line = readline("minishell>> "); + line = readline("minishell$ "); add_history(line); while (count_quotes(line) % 2 != 0) line = ft_strjoin(line, readline("> ")); diff --git a/srcs/parser/handle_redirections.c b/srcs/parser/handle_redirections.c new file mode 100644 index 0000000..388ba83 --- /dev/null +++ b/srcs/parser/handle_redirections.c @@ -0,0 +1,14 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* handle_redirections.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/02/21 16:00:27 by mgraaf #+# #+# */ +/* Updated: 2022/02/21 18:10:59 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" + diff --git a/srcs/parser/parser.c b/srcs/parser/parser.c index b24c669..c237258 100644 --- a/srcs/parser/parser.c +++ b/srcs/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 14:50:02 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/21 18:10:10 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ int count_args(t_lexor *lexor_list) i = 0; tmp = lexor_list; - while (tmp && tmp->token == 0) + while (tmp && tmp->token != PIPE) { i++; tmp = tmp->next; @@ -27,27 +27,51 @@ int count_args(t_lexor *lexor_list) return (i); } +int add_redirection(t_lexor **redirections, t_lexor **lexor_list, int *num_redirections) +{ + t_lexor *node; + + if (!*lexor_list || !(*lexor_list)->token) + return (0); + node = ft_lexornew(ft_strdup((*lexor_list)->next->str), (*lexor_list)->token); + if (!*redirections) + *redirections = node; + else + ft_lexoradd_back(redirections, node); + *lexor_list = (*lexor_list)->next; + num_redirections++; + return (1); +} + t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) { char **str; - char *o_str; - char *tmp; int i; + t_lexor *redirections; + int num_redirections; i = 0; - o_str = ft_strdup(lexor_list->str); - while (i++ < arg_size - 1) + num_redirections = 0; + redirections = NULL; + str = malloc(sizeof(char **) * arg_size); + if (!str) + return (NULL); + str[i] = ft_strdup(lexor_list->str); + while (--arg_size >= 1) { lexor_list = lexor_list->next; - tmp = ft_strjoin(o_str, ft_strjoin(" ", ft_strdup(lexor_list->str))); - free(o_str); - o_str = ft_strdup(tmp); - free(tmp); + if (add_redirection(&redirections, &lexor_list, &num_redirections)) + arg_size--; + else + str[++i] = ft_strdup(lexor_list->str); } - str = ft_split(o_str, ' '); - return (ft_simple_cmdsnew(str, builtin_arr(str[0]))); + str[++i] = NULL; + return (ft_simple_cmdsnew(str, builtin_arr(str[0]), + num_redirections, redirections)); } +//free lexor_list +//handle malloc errors void parser(t_lexor *lexor_list) { t_simple_cmds *simple_cmds; @@ -69,14 +93,19 @@ void parser(t_lexor *lexor_list) lexor_list = lexor_list->next; } int i = 0; - while (simple_cmds) + + while(simple_cmds) { - printf("\n%d\n", i); - i++; - while(*simple_cmds->str) + printf("\n%i\n", i++); + while (*simple_cmds->str) printf("%s\n", *simple_cmds->str++); - if (simple_cmds->builtin) - printf("builtin👍\n"); + if (simple_cmds->redirections) + printf("\tredirections:\n"); + while (simple_cmds->redirections) + { + printf("\t%s\t%d\n", simple_cmds->redirections->str, simple_cmds->redirections->token); + simple_cmds->redirections = simple_cmds->redirections->next; + } simple_cmds = simple_cmds->next; } } diff --git a/srcs/utils/t_simple_cmds_utils.c b/srcs/utils/t_simple_cmds_utils.c index 02ecad0..b70d408 100644 --- a/srcs/utils/t_simple_cmds_utils.c +++ b/srcs/utils/t_simple_cmds_utils.c @@ -6,13 +6,14 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 13:43:59 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/21 17:11:19 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "utils.h" -t_simple_cmds *ft_simple_cmdsnew(char **str, int (*builtin)(t_tools *)) +t_simple_cmds *ft_simple_cmdsnew(char **str, int (*builtin)(t_tools *), + int num_redirections, t_lexor *redirections) { t_simple_cmds *new_element; @@ -21,6 +22,8 @@ t_simple_cmds *ft_simple_cmdsnew(char **str, int (*builtin)(t_tools *)) return (0); new_element->str = str; new_element->builtin = builtin; + new_element->num_redirections = num_redirections; + new_element->redirections = redirections; new_element->next = NULL; return (new_element); } From cff75c04ed10703057327fadb1596a0f23fb25fa Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Tue, 22 Feb 2022 11:44:02 +0100 Subject: [PATCH 023/163] not working --- srcs/parser/parser.c | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/srcs/parser/parser.c b/srcs/parser/parser.c index c237258..931b641 100644 --- a/srcs/parser/parser.c +++ b/srcs/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 18:10:10 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/22 11:28:46 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -33,7 +33,8 @@ int add_redirection(t_lexor **redirections, t_lexor **lexor_list, int *num_redir if (!*lexor_list || !(*lexor_list)->token) return (0); - node = ft_lexornew(ft_strdup((*lexor_list)->next->str), (*lexor_list)->token); + node = ft_lexornew(ft_strdup((*lexor_list)->next->str), + (*lexor_list)->token); if (!*redirections) *redirections = node; else @@ -56,16 +57,25 @@ t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) str = malloc(sizeof(char **) * arg_size); if (!str) return (NULL); - str[i] = ft_strdup(lexor_list->str); - while (--arg_size >= 1) + while (arg_size > 0) { - lexor_list = lexor_list->next; if (add_redirection(&redirections, &lexor_list, &num_redirections)) arg_size--; else - str[++i] = ft_strdup(lexor_list->str); + { + str[i] = ft_strdup(lexor_list->str); + printf("i = %i\t%s\n", i, str[i]); + i++; + } + lexor_list = lexor_list->next; + arg_size--; } - str[++i] = NULL; + str[i] = NULL; + printf("i = %i\t%s\n", i, str[i]); + i = 0; + while (i < 2) + printf("%s\n", str[i++]); + return (ft_simple_cmdsnew(str, builtin_arr(str[0]), num_redirections, redirections)); } From 3a2e028fc0767dcd1d709379b8a9890a927cdcfa Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Tue, 22 Feb 2022 15:58:48 +0100 Subject: [PATCH 024/163] parser merge + fix echo + trying " --- srcs/main.c | 4 +- srcs/parser/parser.c | 106 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 105 insertions(+), 5 deletions(-) diff --git a/srcs/main.c b/srcs/main.c index 2048fd3..fc67a3b 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 11:44:14 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/22 15:57:24 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -49,7 +49,7 @@ int main(int argc, char **argv, char **envp) { line = readline("minishell>> "); add_history(line); - while (count_quotes(line) % 2 != 0) + while (count_quotes(line) % 2 != 0 || line[ft_strlen(line) - 1] == 92) line = ft_strjoin(line, readline("> ")); tools.args = line; lexor_list = token_reader(&tools); diff --git a/srcs/parser/parser.c b/srcs/parser/parser.c index 8f48026..c238439 100644 --- a/srcs/parser/parser.c +++ b/srcs/parser/parser.c @@ -6,16 +6,116 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 10:15:24 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/22 15:57:26 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" +int count_args(t_lexor *lexor_list) +{ + t_lexor *tmp; + int i; + + i = 0; + tmp = lexor_list; + while (tmp && tmp->token != PIPE) + { + i++; + tmp = tmp->next; + } + return (i); +} + +int add_redirection(t_lexor **redirections, t_lexor **lexor_list, int *num_redirections) +{ + t_lexor *node; + + if (!*lexor_list || ((*lexor_list)->token != LESS + && (*lexor_list)->token != GREAT)) + return (0); + node = ft_lexornew(ft_strdup((*lexor_list)->next->str), + (*lexor_list)->token); + if (!*redirections) + *redirections = node; + else + ft_lexoradd_back(redirections, node); + *lexor_list = (*lexor_list)->next; + num_redirections++; + return (1); +} + +t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) +{ + char **str; + int i; + t_lexor *redirections; + int num_redirections; + + i = 0; + num_redirections = 0; + redirections = NULL; + str = malloc(sizeof(char **) * arg_size + 1); + if (!str) + return (NULL); + while (arg_size > 0) + { + if (add_redirection(&redirections, &lexor_list, &num_redirections)) + arg_size--; + else + { + str[i] = ft_strdup(lexor_list->str); + i++; + } + lexor_list = lexor_list->next; + arg_size--; + } + printf("\t\t%d\n", i); + str[i] = NULL; + i = 0; + return (ft_simple_cmdsnew(str, builtin_arr(str[0]), + num_redirections, redirections)); +} + +//free lexor_list +//handle malloc errors void parser(t_lexor *lexor_list) { - while (lexor_list->token == 0) + t_simple_cmds *simple_cmds; + t_simple_cmds *node; + int arg_size; + + simple_cmds = NULL; + while (lexor_list) + { + if (lexor_list->token == PIPE) + lexor_list = lexor_list->next; + // if (lexor_list->token == PS2) + // lexor_list = lexor_list->next; + arg_size = count_args(lexor_list); + node = initialize_cmd(lexor_list, arg_size); + if (!simple_cmds) + simple_cmds = node; + else + ft_simple_cmdsadd_back(&simple_cmds, node); + while (arg_size--) + lexor_list = lexor_list->next; + } + int i = 0; + while(simple_cmds) { - + printf("\n%i\n", i++); + while (*simple_cmds->str) + { + printf("%s\n", *simple_cmds->str++); + } + if (simple_cmds->redirections) + printf("\tredirections:\n"); + while (simple_cmds->redirections) + { + printf("\t%s\t%d\n", simple_cmds->redirections->str, simple_cmds->redirections->token); + simple_cmds->redirections = simple_cmds->redirections->next; + } + simple_cmds = simple_cmds->next; } } \ No newline at end of file From 5dba795babc3409548b1e0adb41c9a5f6daca020 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Tue, 22 Feb 2022 16:18:03 +0100 Subject: [PATCH 025/163] added PS2 --- srcs/parser/parser.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/srcs/parser/parser.c b/srcs/parser/parser.c index 0d3eda6..aa79149 100644 --- a/srcs/parser/parser.c +++ b/srcs/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/02/22 16:08:52 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/22 16:17:36 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -31,8 +31,7 @@ int add_redirection(t_lexor **redirections, t_lexor **lexor_list, int *num_redir { t_lexor *node; - if (!*lexor_list || ((*lexor_list)->token != LESS - && (*lexor_list)->token != GREAT)) + if (!*lexor_list || !(*lexor_list)->token || (*lexor_list)->token == PS2) return (0); node = ft_lexornew(ft_strdup((*lexor_list)->next->str), (*lexor_list)->token); @@ -62,15 +61,11 @@ t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) { if (add_redirection(&redirections, &lexor_list, &num_redirections)) arg_size--; - else - { - str[i] = ft_strdup(lexor_list->str); - i++; - } + else if (lexor_list->token != PS2) + str[i++] = ft_strdup(lexor_list->str); lexor_list = lexor_list->next; arg_size--; } - printf("\t\t%d\n", i); str[i] = NULL; i = 0; return (ft_simple_cmdsnew(str, builtin_arr(str[0]), From 214ca5e5d870f4438a2801c97dbb1cd8daaf6ab3 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Tue, 22 Feb 2022 16:41:06 +0100 Subject: [PATCH 026/163] cleaned up a bit --- srcs/parser/parser.c | 51 ++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/srcs/parser/parser.c b/srcs/parser/parser.c index aa79149..d6c6486 100644 --- a/srcs/parser/parser.c +++ b/srcs/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/02/22 16:17:36 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/22 16:40:49 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -27,7 +27,8 @@ int count_args(t_lexor *lexor_list) return (i); } -int add_redirection(t_lexor **redirections, t_lexor **lexor_list, int *num_redirections) +int add_redirection(t_lexor **redirections, t_lexor **lexor_list, + int *num_redirections) { t_lexor *node; @@ -69,24 +70,23 @@ t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) str[i] = NULL; i = 0; return (ft_simple_cmdsnew(str, builtin_arr(str[0]), - num_redirections, redirections)); } + num_redirections, redirections)); //free lexor_list //handle malloc errors + void parser(t_lexor *lexor_list) { t_simple_cmds *simple_cmds; t_simple_cmds *node; - int arg_size; + int arg_size; simple_cmds = NULL; while (lexor_list) { if (lexor_list->token == PIPE) lexor_list = lexor_list->next; - // if (lexor_list->token == PS2) - // lexor_list = lexor_list->next; arg_size = count_args(lexor_list); node = initialize_cmd(lexor_list, arg_size); if (!simple_cmds) @@ -96,21 +96,26 @@ void parser(t_lexor *lexor_list) while (arg_size--) lexor_list = lexor_list->next; } - int i = 0; - while(simple_cmds) - { - printf("\n%i\n", i++); - while (*simple_cmds->str) - { - printf("%s\n", *simple_cmds->str++); - } - if (simple_cmds->redirections) - printf("\tredirections:\n"); - while (simple_cmds->redirections) - { - printf("\t%s\t%d\n", simple_cmds->redirections->str, simple_cmds->redirections->token); - simple_cmds->redirections = simple_cmds->redirections->next; - } - simple_cmds = simple_cmds->next; - } } + +// >> means write over file + +// int i = 0; +// while(simple_cmds) +// { +// printf("\n%i\n", i++); +// while (*simple_cmds->str) +// { +// printf("%s\n", *simple_cmds->str++); +// } +// if (simple_cmds->redirections) +// printf("\tredirections:\n"); +// while (simple_cmds->redirections) +// { +// printf("\t%s\t%d\n", simple_cmds->redirections->str, simple_cmds->redirections->token); +// simple_cmds->redirections = simple_cmds->redirections->next; +// } +// if (simple_cmds->builtin) +// printf("BUILTIN :)\n"); +// simple_cmds = simple_cmds->next; +// } \ No newline at end of file From 657873799598f9aa65fc0d61d3e5eea8a556b5ad Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Tue, 22 Feb 2022 16:41:27 +0100 Subject: [PATCH 027/163] fixed a stupid thing --- srcs/parser/parser.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/srcs/parser/parser.c b/srcs/parser/parser.c index d6c6486..7593bb4 100644 --- a/srcs/parser/parser.c +++ b/srcs/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/02/22 16:40:49 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/22 16:41:16 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -70,8 +70,8 @@ t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) str[i] = NULL; i = 0; return (ft_simple_cmdsnew(str, builtin_arr(str[0]), -} num_redirections, redirections)); +} //free lexor_list //handle malloc errors From ec8d85f60678eab7af9bd16b8f15f3edc55e2979 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 23 Feb 2022 15:57:34 +0100 Subject: [PATCH 028/163] backslash --- includes/minishell.h | 3 ++- includes/parser.h | 2 +- srcs/lexor/handle_token.c | 2 +- srcs/lexor/token_reader.c | 4 ++- srcs/main.c | 7 ++++- srcs/parser/parser.c | 56 ++++++++++++++++++++++++--------------- srcs/utils/utils.c | 29 +++++++++++++++++++- 7 files changed, 75 insertions(+), 28 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index 622ceb3..c8d6cf3 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 13:52:10 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/23 12:16:34 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -27,6 +27,7 @@ int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); int count_quotes(char *line); +void delete_char(char *str, char c); //builtins int (*builtin_arr(char *str))(t_tools *tools); diff --git a/includes/parser.h b/includes/parser.h index 4ea318b..f02f92d 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 17:02:46 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/23 10:19:40 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/srcs/lexor/handle_token.c b/srcs/lexor/handle_token.c index 82a08cd..5188088 100644 --- a/srcs/lexor/handle_token.c +++ b/srcs/lexor/handle_token.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/18 10:27:43 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 10:06:44 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/23 12:00:01 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/srcs/lexor/token_reader.c b/srcs/lexor/token_reader.c index 0c771ee..3c63c25 100644 --- a/srcs/lexor/token_reader.c +++ b/srcs/lexor/token_reader.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:11:20 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 14:56:30 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/23 15:39:26 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -74,6 +74,7 @@ t_lexor *token_reader(t_tools *tools) i = 0; lexor_list = NULL; + i += skip_spaces(tools->args, i); while (tools->args[i]) { i += skip_spaces(tools->args, i); @@ -85,6 +86,7 @@ t_lexor *token_reader(t_tools *tools) i += handle_token(tools->args, i, &lexor_list); else i += read_words(i, tools->args, &lexor_list); + i += skip_spaces(tools->args, i); } return (lexor_list); } diff --git a/srcs/main.c b/srcs/main.c index 63a69be..2a610a8 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/02/22 16:08:47 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/23 15:39:03 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -49,7 +49,12 @@ int main(int argc, char **argv, char **envp) line = readline("minishell$ "); add_history(line); while (count_quotes(line) % 2 != 0 || line[ft_strlen(line) - 1] == 92) + { + if (line[ft_strlen(line) - 1] == 92) + line = ft_substr(line, 0, ft_strlen(line) - 1); line = ft_strjoin(line, readline("> ")); + } + delete_char(line, 92); tools.args = line; lexor_list = token_reader(&tools); parser(lexor_list); diff --git a/srcs/parser/parser.c b/srcs/parser/parser.c index 7593bb4..58f5a44 100644 --- a/srcs/parser/parser.c +++ b/srcs/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/02/22 16:41:16 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/23 15:46:19 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -45,6 +45,14 @@ int add_redirection(t_lexor **redirections, t_lexor **lexor_list, return (1); } +// int ps2_token(t_lexor **lexor_list) +// { +// if (*lexor_list && lexor_list->token == PS2) +// { + +// } +// } + t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) { char **str; @@ -62,8 +70,11 @@ t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) { if (add_redirection(&redirections, &lexor_list, &num_redirections)) arg_size--; + // if (ps2_token) else if (lexor_list->token != PS2) str[i++] = ft_strdup(lexor_list->str); + // printf("%u\n", lexor_list->token); + // lexor_list = lexor_list->next; lexor_list = lexor_list->next; arg_size--; } @@ -87,6 +98,8 @@ void parser(t_lexor *lexor_list) { if (lexor_list->token == PIPE) lexor_list = lexor_list->next; + else if (lexor_list->token == PS2) + lexor_list = lexor_list->next; arg_size = count_args(lexor_list); node = initialize_cmd(lexor_list, arg_size); if (!simple_cmds) @@ -96,26 +109,25 @@ void parser(t_lexor *lexor_list) while (arg_size--) lexor_list = lexor_list->next; } +int i = 0; +while(simple_cmds) +{ + printf("\n%i\n", i++); + while (*simple_cmds->str) + { + printf("%s\n", *simple_cmds->str++); + } + if (simple_cmds->redirections) + printf("\tredirections:\n"); + while (simple_cmds->redirections) + { + printf("\t%s\t%d\n", simple_cmds->redirections->str, simple_cmds->redirections->token); + simple_cmds->redirections = simple_cmds->redirections->next; + } + if (simple_cmds->builtin) + printf("BUILTIN :)\n"); + simple_cmds = simple_cmds->next; +} } -// >> means write over file - -// int i = 0; -// while(simple_cmds) -// { -// printf("\n%i\n", i++); -// while (*simple_cmds->str) -// { -// printf("%s\n", *simple_cmds->str++); -// } -// if (simple_cmds->redirections) -// printf("\tredirections:\n"); -// while (simple_cmds->redirections) -// { -// printf("\t%s\t%d\n", simple_cmds->redirections->str, simple_cmds->redirections->token); -// simple_cmds->redirections = simple_cmds->redirections->next; -// } -// if (simple_cmds->builtin) -// printf("BUILTIN :)\n"); -// simple_cmds = simple_cmds->next; -// } \ No newline at end of file +// >> means write over file \ No newline at end of file diff --git a/srcs/utils/utils.c b/srcs/utils/utils.c index 0119d67..fc7a534 100644 --- a/srcs/utils/utils.c +++ b/srcs/utils/utils.c @@ -6,10 +6,12 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/21 11:17:26 by fpolycar #+# #+# */ -/* Updated: 2022/02/21 11:17:31 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/23 12:19:51 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ +#include "minishell.h" + int count_quotes(char *line) { int i; @@ -25,3 +27,28 @@ int count_quotes(char *line) } return (nb); } + +void delete_char(char *str, char c) +{ + int len; + int i; + int j; + + len = ft_strlen(str); + i = 0; + while (i < len) + { + if (str[i] == c) + { + j = i; + while (j < len) + { + str[i] = str[j + 1]; + j++; + } + len--; + i--; + } + i++; + } +} From 3fe7ff24935f3ff7498be995fd29b122158f8eb3 Mon Sep 17 00:00:00 2001 From: Alfred Polycarpe Date: Thu, 24 Feb 2022 11:03:18 +0100 Subject: [PATCH 029/163] \ ignoring pb --- .vscode/launch.json | 3 ++- srcs/parser/parser.c | 59 ++++++++++++++++++++++---------------------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index fe2c156..4e701e8 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,11 +4,12 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { "name": "(lldb) Launch", "type": "lldb", "request": "launch", - "program": "${workspaceFolder}/minishell", + "program": "${workspaceFolder}", "args": [], "stopAtEntry": true, "cwd": "${workspaceFolder}", diff --git a/srcs/parser/parser.c b/srcs/parser/parser.c index 58f5a44..26211cf 100644 --- a/srcs/parser/parser.c +++ b/srcs/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/02/23 15:46:19 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/24 10:44:04 by alfred ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ int count_args(t_lexor *lexor_list) i = 0; tmp = lexor_list; - while (tmp && tmp->token != PIPE) + while (tmp && tmp->token != PIPE && tmp->token != PS2) { i++; tmp = tmp->next; @@ -45,13 +45,14 @@ int add_redirection(t_lexor **redirections, t_lexor **lexor_list, return (1); } -// int ps2_token(t_lexor **lexor_list) -// { -// if (*lexor_list && lexor_list->token == PS2) -// { - -// } -// } +int ps2_token(t_lexor **lexor_list) +{ + if (*lexor_list && (*lexor_list)->token == PS2) + { + *lexor_list = (*lexor_list)->next; + } + return (0); +} t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) { @@ -70,11 +71,11 @@ t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) { if (add_redirection(&redirections, &lexor_list, &num_redirections)) arg_size--; - // if (ps2_token) + // else if (ps2_token(&lexor_list)) + // printf("test"); else if (lexor_list->token != PS2) str[i++] = ft_strdup(lexor_list->str); - // printf("%u\n", lexor_list->token); - // lexor_list = lexor_list->next; + printf("%u\n", lexor_list->token); lexor_list = lexor_list->next; arg_size--; } @@ -109,25 +110,25 @@ void parser(t_lexor *lexor_list) while (arg_size--) lexor_list = lexor_list->next; } -int i = 0; -while(simple_cmds) -{ - printf("\n%i\n", i++); - while (*simple_cmds->str) - { - printf("%s\n", *simple_cmds->str++); - } - if (simple_cmds->redirections) - printf("\tredirections:\n"); - while (simple_cmds->redirections) + int i = 0; + while(simple_cmds) { - printf("\t%s\t%d\n", simple_cmds->redirections->str, simple_cmds->redirections->token); - simple_cmds->redirections = simple_cmds->redirections->next; + printf("\n%i\n", i++); + while (*simple_cmds->str) + { + printf("%s\n", *simple_cmds->str++); + } + if (simple_cmds->redirections) + printf("\tredirections:\n"); + while (simple_cmds->redirections) + { + printf("\t%s\t%d\n", simple_cmds->redirections->str, simple_cmds->redirections->token); + simple_cmds->redirections = simple_cmds->redirections->next; + } + if (simple_cmds->builtin) + printf("BUILTIN :)\n"); + simple_cmds = simple_cmds->next; } - if (simple_cmds->builtin) - printf("BUILTIN :)\n"); - simple_cmds = simple_cmds->next; -} } // >> means write over file \ No newline at end of file From 06ae641fef4a07f1c65acda0f052e3fdefff68cb Mon Sep 17 00:00:00 2001 From: Alfred Polycarpe Date: Thu, 24 Feb 2022 15:58:33 +0100 Subject: [PATCH 030/163] first step for backslash --- includes/minishell.h | 4 ++-- srcs/builtins/builtins.c | 4 ++-- srcs/main.c | 4 ++-- srcs/parser/parser.c | 37 +++++++++++++++++++------------------ srcs/utils/utils.c | 6 ++++-- 5 files changed, 29 insertions(+), 26 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index c8d6cf3..dbacb29 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/02/23 12:16:34 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/24 15:33:33 by alfred ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -27,7 +27,7 @@ int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); int count_quotes(char *line); -void delete_char(char *str, char c); +char *delete_char(char *str, char c); //builtins int (*builtin_arr(char *str))(t_tools *tools); diff --git a/srcs/builtins/builtins.c b/srcs/builtins/builtins.c index 61ad615..4205541 100644 --- a/srcs/builtins/builtins.c +++ b/srcs/builtins/builtins.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 11:42:32 by mgraaf #+# #+# */ -/* Updated: 2022/02/17 14:13:18 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/24 15:09:05 by alfred ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ int (*builtin_arr(char *str))(t_tools *tools) i = 0; while (i < 7) { - if (!ft_strncmp(builtins[i][0], str, strlen(builtins[i][0]))) + if (!ft_strncmp(builtins[i][0], str, ft_strlen(str))) return (builtins[i][1]); i++; } diff --git a/srcs/main.c b/srcs/main.c index 2a610a8..a032ad5 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/02/23 15:39:03 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/24 15:40:34 by alfred ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -54,7 +54,7 @@ int main(int argc, char **argv, char **envp) line = ft_substr(line, 0, ft_strlen(line) - 1); line = ft_strjoin(line, readline("> ")); } - delete_char(line, 92); + line = delete_char(line, '\\'); tools.args = line; lexor_list = token_reader(&tools); parser(lexor_list); diff --git a/srcs/parser/parser.c b/srcs/parser/parser.c index 26211cf..c477dd1 100644 --- a/srcs/parser/parser.c +++ b/srcs/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/02/24 10:44:04 by alfred ######## odam.nl */ +/* Updated: 2022/02/24 15:34:29 by alfred ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -32,17 +32,19 @@ int add_redirection(t_lexor **redirections, t_lexor **lexor_list, { t_lexor *node; - if (!*lexor_list || !(*lexor_list)->token || (*lexor_list)->token == PS2) - return (0); - node = ft_lexornew(ft_strdup((*lexor_list)->next->str), - (*lexor_list)->token); - if (!*redirections) - *redirections = node; - else - ft_lexoradd_back(redirections, node); - *lexor_list = (*lexor_list)->next; - num_redirections++; - return (1); + if (*lexor_list && ((*lexor_list)->token == LESS || (*lexor_list)->token == GREAT)) + { + node = ft_lexornew(ft_strdup((*lexor_list)->next->str), + (*lexor_list)->token); + if (!*redirections) + *redirections = node; + else + ft_lexoradd_back(redirections, node); + *lexor_list = (*lexor_list)->next; + num_redirections++; + return (1); + } + return (0); } int ps2_token(t_lexor **lexor_list) @@ -50,6 +52,7 @@ int ps2_token(t_lexor **lexor_list) if (*lexor_list && (*lexor_list)->token == PS2) { *lexor_list = (*lexor_list)->next; + return (1); } return (0); } @@ -71,11 +74,9 @@ t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) { if (add_redirection(&redirections, &lexor_list, &num_redirections)) arg_size--; - // else if (ps2_token(&lexor_list)) - // printf("test"); - else if (lexor_list->token != PS2) - str[i++] = ft_strdup(lexor_list->str); - printf("%u\n", lexor_list->token); + // else if (lexor_list->token != PS2) + str[i++] = ft_strdup(lexor_list->str); + // printf("%u\n", lexor_list->token); lexor_list = lexor_list->next; arg_size--; } @@ -100,7 +101,7 @@ void parser(t_lexor *lexor_list) if (lexor_list->token == PIPE) lexor_list = lexor_list->next; else if (lexor_list->token == PS2) - lexor_list = lexor_list->next; + ps2_token(&lexor_list); arg_size = count_args(lexor_list); node = initialize_cmd(lexor_list, arg_size); if (!simple_cmds) diff --git a/srcs/utils/utils.c b/srcs/utils/utils.c index fc7a534..4e0fc64 100644 --- a/srcs/utils/utils.c +++ b/srcs/utils/utils.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/21 11:17:26 by fpolycar #+# #+# */ -/* Updated: 2022/02/23 12:19:51 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/24 15:48:19 by alfred ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ int count_quotes(char *line) return (nb); } -void delete_char(char *str, char c) +char *delete_char(char *str, char c) { int len; int i; @@ -45,10 +45,12 @@ void delete_char(char *str, char c) { str[i] = str[j + 1]; j++; + i++; } len--; i--; } i++; } + return (str); } From 4373cb50815377cee4d21483f3b9197ddee43c51 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Thu, 24 Feb 2022 17:54:41 +0100 Subject: [PATCH 031/163] started the executor --- .vscode/launch.json | 2 +- Makefile | 4 +- includes/executor.h | 15 ++++ includes/minishell.h | 3 +- includes/parser.h | 24 +++--- includes/utils.h | 6 +- infile | 0 outfile | 0 srcs/builtins/builtins.c | 4 +- srcs/executor/executor.c | 128 ++++++++++++++++++++++++++++++ srcs/main.c | 36 +++------ srcs/parser/handle_redirections.c | 4 +- srcs/parser/parser.c | 54 +++++++------ srcs/utils/t_lexor_utils.c | 14 +++- srcs/utils/t_simple_cmds_utils.c | 28 ++----- 15 files changed, 227 insertions(+), 95 deletions(-) create mode 100644 includes/executor.h create mode 100644 infile create mode 100644 outfile create mode 100644 srcs/executor/executor.c diff --git a/.vscode/launch.json b/.vscode/launch.json index 4e701e8..65242c9 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,7 +9,7 @@ "name": "(lldb) Launch", "type": "lldb", "request": "launch", - "program": "${workspaceFolder}", + "program": "${workspaceFolder}/minishell", "args": [], "stopAtEntry": true, "cwd": "${workspaceFolder}", diff --git a/Makefile b/Makefile index 8813a78..e7a94e7 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,7 @@ SRCS = ./srcs/main.c \ ./srcs/builtins/mini_pwd.c \ ./srcs/builtins/mini_unset.c \ ./srcs/builtins/mini_env.c \ + ./srcs/executor/executor.c \ # ./srcs/parser/parse_args.c \ # ./srcs/pipex/pipex.c @@ -30,7 +31,8 @@ HEADER = ./includes/minishell.h \ ./includes/lexor.h \ ./includes/builtins.h \ ./includes/utils.h \ - ./includes/parser.h + ./includes/parser.h \ + ./includes/executor.h all: $(NAME) diff --git a/includes/executor.h b/includes/executor.h new file mode 100644 index 0000000..4e30e61 --- /dev/null +++ b/includes/executor.h @@ -0,0 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* executor.h :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/02/24 15:17:39 by mgraaf #+# #+# */ +/* Updated: 2022/02/24 15:19:56 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int executor(t_tools *tools); \ No newline at end of file diff --git a/includes/minishell.h b/includes/minishell.h index c8d6cf3..2132503 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/02/23 12:16:34 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/24 15:19:43 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,6 +23,7 @@ # include "utils.h" # include "lexor.h" # include "builtins.h" +# include "executor.h" int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); diff --git a/includes/parser.h b/includes/parser.h index f02f92d..2af8bec 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/02/23 10:19:40 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/24 15:20:42 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,15 +34,16 @@ typedef struct s_lexor typedef struct s_tools { - char *args; - char **paths; - char **envp; - int in; - int out; - int err; - char *pwd; - char *old_pwd; - int pipes; + char *args; + char **paths; + char **envp; + struct s_simple_cmds *simple_cmds; + int in; + int out; + int err; + char *pwd; + char *old_pwd; + int pipes; } t_tools; typedef struct s_simple_cmds @@ -54,8 +55,9 @@ typedef struct s_simple_cmds struct s_simple_cmds *next; } t_simple_cmds; + int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); -void parser(t_lexor *lexor_list); +void parser(t_lexor *lexor_list, t_tools *tools); #endif diff --git a/includes/utils.h b/includes/utils.h index 12b5a78..077c27a 100644 --- a/includes/utils.h +++ b/includes/utils.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:36:23 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 17:11:45 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/24 16:58:36 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,14 +18,14 @@ t_simple_cmds *ft_simple_cmdsnew(char **str, int (*builtin)(t_tools *), int num_redirections, t_lexor *redirections); void ft_simple_cmdsadd_back(t_simple_cmds **lst, t_simple_cmds *new); -void ft_simple_cmdsdelone(t_simple_cmds **lst, char key); +void ft_simple_cmds_rm_first(t_simple_cmds **lst); void ft_simple_cmdsclear(t_simple_cmds **lst); t_simple_cmds *ft_simple_cmdslast(t_simple_cmds *map); //t_lexor_utils t_lexor *ft_lexornew(char *str, int token); void ft_lexoradd_back(t_lexor **lst, t_lexor *new); -void ft_lexordelone(t_lexor **lst, char key); +void ft_lexordelone(t_lexor **lst, int i); void ft_lexorclear(t_lexor **lst); t_lexor *ft_lexorlast(t_lexor *map); diff --git a/infile b/infile new file mode 100644 index 0000000..e69de29 diff --git a/outfile b/outfile new file mode 100644 index 0000000..e69de29 diff --git a/srcs/builtins/builtins.c b/srcs/builtins/builtins.c index 61ad615..48d6c3e 100644 --- a/srcs/builtins/builtins.c +++ b/srcs/builtins/builtins.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 11:42:32 by mgraaf #+# #+# */ -/* Updated: 2022/02/17 14:13:18 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/24 15:04:55 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ int (*builtin_arr(char *str))(t_tools *tools) i = 0; while (i < 7) { - if (!ft_strncmp(builtins[i][0], str, strlen(builtins[i][0]))) + if (!ft_strncmp(builtins[i][0], str, ft_strlen(str))) return (builtins[i][1]); i++; } diff --git a/srcs/executor/executor.c b/srcs/executor/executor.c new file mode 100644 index 0000000..2079b2c --- /dev/null +++ b/srcs/executor/executor.c @@ -0,0 +1,128 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* executor.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ +/* Updated: 2022/02/24 17:51:05 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "executor.h" + +int find_cmd(char **args, char **paths, char **envp) +{ + int i; + char *mycmd; + + i = 0; + printf("HELLO\n"); + while (paths[i]) + { + mycmd = ft_strjoin(paths[i], args[0]); + if (!access(mycmd, F_OK)) + execve(mycmd, args, envp); + free(mycmd); + i++; + } + return (1); +} + +int check_outfile(t_tools *tools) +{ + t_lexor *start; + + start = tools->simple_cmds->redirections; + while (tools->simple_cmds->redirections) + { + if (tools->simple_cmds->redirections->token == GREAT + || tools->simple_cmds->redirections->token == GREAT_GREAT) + { + if (tools->simple_cmds->redirections->token == GREAT) + tools->out = open(tools->simple_cmds->redirections->str, + O_CREAT | O_RDWR | O_TRUNC | O_APPEND, 0644); + else + tools->out = open(tools->simple_cmds->redirections->str, + O_CREAT | O_RDWR | O_TRUNC, 0644); + if (tools->out < 0) + { + perror("Open "); + // do more stuff (ie lots of freeing) + } + } + tools->simple_cmds->redirections + = tools->simple_cmds->redirections->next; + } + tools->simple_cmds->redirections = start; + return (tools->out); +} + +int check_infile(t_tools *tools) +{ + t_lexor *start; + + start = tools->simple_cmds->redirections; + while (tools->simple_cmds->redirections) + { + if (tools->simple_cmds->redirections->token == LESS) + { + tools->in = open(tools->simple_cmds->redirections->str, O_RDONLY); + if (tools->in < 0) + { + perror("Open "); + // do more stuff (ie lots of freeing) + } + } + tools->simple_cmds->redirections + = tools->simple_cmds->redirections->next; + } + tools->simple_cmds->redirections = start; + return (tools->in); +} + +int executor(t_tools *tools) +{ + int i; + int end[2]; + pid_t ret; + int status; + + i = 0; + while (i < tools->pipes + 1) + { + check_infile(tools); + dup2(tools->in, 0); + close(tools->in); + if (i == tools->pipes) + { + tools->out = dup(1); + check_outfile(tools); + } + else + { + pipe(end); + tools->out = end[1]; + tools->in = end[0]; + } + printf("HELo\n"); + dup2(tools->out, 1); + close(tools->out); + ret = fork(); + if (ret < 0) + { + perror("Fork: "); + exit(0); + } + if (ret == 0) + { + printf("b4 find_cmd\n"); + find_cmd(tools->simple_cmds->str, tools->paths, tools->envp); + } + i++; + ft_simple_cmds_rm_first(&tools->simple_cmds); + waitpid(ret, &status, 0); + } + return (0); +} diff --git a/srcs/main.c b/srcs/main.c index 2a610a8..dd29010 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,36 +6,26 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/02/23 15:39:03 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/24 17:33:48 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -// int find_cmd(t_tools *tools) -// { -// int i; -// char *mycmd; +int implement_tools(t_tools *tools) +{ + tools->in = dup(0); + tools->out = dup(1); + tools->err = dup(2); -// i = 0; -// while ((tools->paths)[i]) -// { -// mycmd = ft_strjoin((tools->paths)[i], (tools->args)[0]); -// if (!access(mycmd, F_OK)) -// execve(mycmd, tools->args, tools->envp); -// free(mycmd); -// i++; -// } -// return (1); -// } + return (1); +} int main(int argc, char **argv, char **envp) { char *line; t_tools tools; t_lexor *lexor_list = NULL; - // pid_t process; - // int status; if (argc != 1 || argv[1]) { @@ -44,6 +34,7 @@ int main(int argc, char **argv, char **envp) } tools.envp = envp; parse_envp(&tools); + implement_tools(&tools); while (1) { line = readline("minishell$ "); @@ -54,15 +45,10 @@ int main(int argc, char **argv, char **envp) line = ft_substr(line, 0, ft_strlen(line) - 1); line = ft_strjoin(line, readline("> ")); } - delete_char(line, 92); tools.args = line; lexor_list = token_reader(&tools); - parser(lexor_list); - // while (lexor_list) - // { - // printf("str = %s \t token = %d\n", lexor_list->str, lexor_list->token); - // lexor_list = lexor_list->next; - // } + parser(lexor_list, &tools); + executor(&tools); free(line); } return (0); diff --git a/srcs/parser/handle_redirections.c b/srcs/parser/handle_redirections.c index 388ba83..b0caa76 100644 --- a/srcs/parser/handle_redirections.c +++ b/srcs/parser/handle_redirections.c @@ -6,9 +6,9 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/21 16:00:27 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 18:10:59 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/24 10:22:09 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ -#include "parser.h" +#include "parser.h" diff --git a/srcs/parser/parser.c b/srcs/parser/parser.c index 26211cf..eacdfc4 100644 --- a/srcs/parser/parser.c +++ b/srcs/parser/parser.c @@ -6,13 +6,13 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/02/24 10:44:04 by alfred ######## odam.nl */ +/* Updated: 2022/02/24 15:29:45 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -int count_args(t_lexor *lexor_list) +int count_args(t_lexor *lexor_list, t_tools *tools) { t_lexor *tmp; int i; @@ -24,6 +24,11 @@ int count_args(t_lexor *lexor_list) i++; tmp = tmp->next; } + if (tmp) + { + if (tmp->token == PIPE) + tools->pipes++; + } return (i); } @@ -75,12 +80,10 @@ t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) // printf("test"); else if (lexor_list->token != PS2) str[i++] = ft_strdup(lexor_list->str); - printf("%u\n", lexor_list->token); lexor_list = lexor_list->next; arg_size--; } str[i] = NULL; - i = 0; return (ft_simple_cmdsnew(str, builtin_arr(str[0]), num_redirections, redirections)); } @@ -88,7 +91,7 @@ t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) //free lexor_list //handle malloc errors -void parser(t_lexor *lexor_list) +void parser(t_lexor *lexor_list, t_tools *tools) { t_simple_cmds *simple_cmds; t_simple_cmds *node; @@ -101,7 +104,7 @@ void parser(t_lexor *lexor_list) lexor_list = lexor_list->next; else if (lexor_list->token == PS2) lexor_list = lexor_list->next; - arg_size = count_args(lexor_list); + arg_size = count_args(lexor_list, tools); node = initialize_cmd(lexor_list, arg_size); if (!simple_cmds) simple_cmds = node; @@ -110,25 +113,26 @@ void parser(t_lexor *lexor_list) while (arg_size--) lexor_list = lexor_list->next; } - int i = 0; - while(simple_cmds) - { - printf("\n%i\n", i++); - while (*simple_cmds->str) - { - printf("%s\n", *simple_cmds->str++); - } - if (simple_cmds->redirections) - printf("\tredirections:\n"); - while (simple_cmds->redirections) - { - printf("\t%s\t%d\n", simple_cmds->redirections->str, simple_cmds->redirections->token); - simple_cmds->redirections = simple_cmds->redirections->next; - } - if (simple_cmds->builtin) - printf("BUILTIN :)\n"); - simple_cmds = simple_cmds->next; - } + tools->simple_cmds = simple_cmds; } + // int i = 0; + // while(simple_cmds) + // { + // printf("\n%i\n", i++); + // while (*simple_cmds->str) + // { + // printf("%s\n", *simple_cmds->str++); + // } + // if (simple_cmds->redirections) + // printf("\tredirections:\n"); + // while (simple_cmds->redirections) + // { + // printf("\t%s\t%d\n", simple_cmds->redirections->str, simple_cmds->redirections->token); + // simple_cmds->redirections = simple_cmds->redirections->next; + // } + // if (simple_cmds->builtin) + // printf("BUILTIN :)\n"); + // simple_cmds = simple_cmds->next; + // } // >> means write over file \ No newline at end of file diff --git a/srcs/utils/t_lexor_utils.c b/srcs/utils/t_lexor_utils.c index 659fb81..7ecded8 100644 --- a/srcs/utils/t_lexor_utils.c +++ b/srcs/utils/t_lexor_utils.c @@ -40,26 +40,32 @@ void ft_lexoradd_back(t_lexor **lst, t_lexor *new) tmp->next = new; } -void ft_lexordelone(t_lexor **lst, char key) +void ft_lexordelone(t_lexor **lst, int i) { t_lexor *node; t_lexor *prev; t_lexor *start; + int j; + j = 0; start = *lst; node = start; - if ((*lst)->str[1] == key) + if (j == i) { *lst = node->next; free(node); return ; } - while (node && node->str[1] != key) + while (node && j < i) { prev = node; node = node->next; + j++; } - prev->next = node->next; + if (node->next) + prev->next = node->next; + else + prev->next = NULL; free(node); *lst = start; } diff --git a/srcs/utils/t_simple_cmds_utils.c b/srcs/utils/t_simple_cmds_utils.c index b70d408..582c939 100644 --- a/srcs/utils/t_simple_cmds_utils.c +++ b/srcs/utils/t_simple_cmds_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 17:11:19 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/24 16:59:22 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -43,28 +43,16 @@ void ft_simple_cmdsadd_back(t_simple_cmds **lst, t_simple_cmds *new) tmp->next = new; } -void ft_simple_cmdsdelone(t_simple_cmds **lst, char key) +void ft_simple_cmds_rm_first(t_simple_cmds **lst) { - t_simple_cmds *node; - t_simple_cmds *prev; - t_simple_cmds *start; + t_simple_cmds *tmp; - start = *lst; - node = start; - if ((*lst)->str[1][1] == key) - { - *lst = node->next; - free(node); + if (!*lst) return ; - } - while (node && node->str[1][1] != key) - { - prev = node; - node = node->next; - } - prev->next = node->next; - free(node); - *lst = start; + tmp = (*lst)->next; + ft_lexorclear(&(*lst)->redirections); + free(*lst); + *lst = tmp; } void ft_simple_cmdsclear(t_simple_cmds **lst) From 2da869028f0711dd00f63351a9a8178f1cfa0042 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Mon, 28 Feb 2022 16:11:15 +0100 Subject: [PATCH 032/163] tester makefile --- Makefile | 140 +++++++++++++-------- Makefile_no_test | 57 +++++++++ build/results/Testmain.txt | 6 + includes/minishell.h | 7 +- includes/parser.h | 4 +- {srcs => src}/builtins/builtins.c | 0 {srcs => src}/builtins/mini_cd.c | 0 {srcs => src}/builtins/mini_echo.c | 0 {srcs => src}/builtins/mini_env.c | 0 {srcs => src}/builtins/mini_exit.c | 0 {srcs => src}/builtins/mini_export.c | 0 {srcs => src}/builtins/mini_pwd.c | 0 {srcs => src}/builtins/mini_unset.c | 0 {srcs => src}/lexor/handle_token.c | 4 +- {srcs => src}/lexor/token_reader.c | 0 {srcs => src}/main.c | 11 +- {srcs => src}/parse_envp.c | 0 {srcs => src}/parser/handle_redirections.c | 0 {srcs => src}/parser/parser.c | 10 +- {srcs => src}/pipex/pipex.c | 0 {srcs => src}/pipex/pipex.h | 0 {srcs => src}/utils/t_lexor_utils.c | 0 {srcs => src}/utils/t_simple_cmds_utils.c | 0 {srcs => src}/utils/utils.c | 42 +------ test/Testmain.c | 43 +++++++ unity | 1 + {srcs => utilities}/minitalk/Makefile | 0 {srcs => utilities}/minitalk/client.c | 0 {srcs => utilities}/minitalk/minitalk.h | 0 {srcs => utilities}/minitalk/server.c | 0 30 files changed, 208 insertions(+), 117 deletions(-) create mode 100644 Makefile_no_test create mode 100644 build/results/Testmain.txt rename {srcs => src}/builtins/builtins.c (100%) rename {srcs => src}/builtins/mini_cd.c (100%) rename {srcs => src}/builtins/mini_echo.c (100%) rename {srcs => src}/builtins/mini_env.c (100%) rename {srcs => src}/builtins/mini_exit.c (100%) rename {srcs => src}/builtins/mini_export.c (100%) rename {srcs => src}/builtins/mini_pwd.c (100%) rename {srcs => src}/builtins/mini_unset.c (100%) rename {srcs => src}/lexor/handle_token.c (95%) rename {srcs => src}/lexor/token_reader.c (100%) rename {srcs => src}/main.c (84%) rename {srcs => src}/parse_envp.c (100%) rename {srcs => src}/parser/handle_redirections.c (100%) rename {srcs => src}/parser/parser.c (97%) rename {srcs => src}/pipex/pipex.c (100%) rename {srcs => src}/pipex/pipex.h (100%) rename {srcs => src}/utils/t_lexor_utils.c (100%) rename {srcs => src}/utils/t_simple_cmds_utils.c (100%) rename {srcs => src}/utils/utils.c (64%) create mode 100644 test/Testmain.c create mode 160000 unity rename {srcs => utilities}/minitalk/Makefile (100%) rename {srcs => utilities}/minitalk/client.c (100%) rename {srcs => utilities}/minitalk/minitalk.h (100%) rename {srcs => utilities}/minitalk/server.c (100%) diff --git a/Makefile b/Makefile index 8813a78..836c38a 100644 --- a/Makefile +++ b/Makefile @@ -1,57 +1,89 @@ -NAME = minishell - -SRCS = ./srcs/main.c \ - ./srcs/parse_envp.c \ - ./srcs/utils/t_simple_cmds_utils.c \ - ./srcs/utils/t_lexor_utils.c \ - ./srcs/utils/utils.c \ - ./srcs/lexor/token_reader.c \ - ./srcs/lexor/handle_token.c \ - ./srcs/parser/parser.c \ - ./srcs/builtins/builtins.c \ - ./srcs/builtins/mini_cd.c \ - ./srcs/builtins/mini_echo.c \ - ./srcs/builtins/mini_exit.c \ - ./srcs/builtins/mini_export.c \ - ./srcs/builtins/mini_pwd.c \ - ./srcs/builtins/mini_unset.c \ - ./srcs/builtins/mini_env.c \ - # ./srcs/parser/parse_args.c \ - # ./srcs/pipex/pipex.c - - -OBJS = ${SRCS:%.c=%.o} - -FLAGS = -Wall -Werror -Wextra -g - -LIBFT = ./libraries/libft/libft.a - -HEADER = ./includes/minishell.h \ - ./includes/lexor.h \ - ./includes/builtins.h \ - ./includes/utils.h \ - ./includes/parser.h - -all: $(NAME) - -%.o: %.c $(HEADER) - @echo "Compiling ${notdir $<}" - @$(CC) -c $(FLAGS) -I includes -o $@ $< - -$(NAME): $(LIBFT) $(OBJS) $(HEADER) - @$(CC) $(FLAGS) $(LIBFT) $(OBJS) -lreadline -o $(NAME) - @echo "Success" - -$(LIBFT): - $(MAKE) -C ./libraries/libft +NAME = testexe +CLEANUP=rm -f +MKDIR=mkdir -p +TARGET_EXTENSION=out -clean: - @echo "Cleaning" - @rm -f $(OBJS) - @make fclean -C libraries/libft +.PHONY: clean +.PHONY: test + +PATHU = unity/src/ +PATHI = includes/ +PATHS = src/ +PATHSL = src/lexor +PATHSP = src/parser +PATHP = src/pipex +PATHT = test/ +PATHB = build/ +PATHD = build/depends/ +PATHO = build/objs/ +PATHR = build/results/ + +BUILD_PATHS = $(PATHB) $(PATHD) $(PATHO) $(PATHR) + +SRCT = $(wildcard $(PATHT)*.c) + +COMPILE=gcc -c +LINK=gcc +DEPEND=gcc -MM -MG -MF +CFLAGS=-Wall -Werror -Wextra -I. -I$(PATHU) -I$(PATHS) -I$(PATHP) -I$(PATHI) -DTEST + +RESULTS = $(patsubst $(PATHT)Test%.c,$(PATHR)Test%.txt,$(SRCT)) + +PASSED = `grep -s PASS $(PATHR)*.txt` +FAIL = `grep -s FAIL $(PATHR)*.txt` +IGNORE = `grep -s IGNORE $(PATHR)*.txt` + +test: $(BUILD_PATHS) $(RESULTS) + @echo "-----------------------\nIGNORES:\n-----------------------" + @echo "$(IGNORE)" + @echo "-----------------------\nFAILURES:\n-----------------------" + @echo "$(FAIL)" + @echo "-----------------------\nPASSED:\n-----------------------" + @echo "$(PASSED)" + @echo "\nDONE" + +$(PATHR)%.txt: $(PATHB)%.$(TARGET_EXTENSION) + -./$< > $@ 2>&1 + +$(PATHB)Test%.$(TARGET_EXTENSION): $(PATHO)Test%.o $(PATHO)%.o $(PATHU)unity.o #$(PATHD)Test%.d + $(LINK) -o $@ $^ + +$(PATHO)%.o:: $(PATHT)%.c + $(COMPILE) $(CFLAGS) $< -o $@ -fclean: clean - @rm -f $(NAME) - @rm -f $(LIBFT) +$(PATHO)%.o:: $(PATHS)%.c + $(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHO)%.o:: $(PATHSL)%.c + $(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHO)%.o:: $(PATHSP)%.c + $(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHO)%.o:: $(PATHU)%.c $(PATHU)%.h + $(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHD)%.d:: $(PATHT)%.c + $(DEPEND) $@ $< + +$(PATHB): + $(MKDIR) $(PATHB) + +$(PATHD): + $(MKDIR) $(PATHD) + +$(PATHO): + $(MKDIR) $(PATHO) + +$(PATHR): + $(MKDIR) $(PATHR) + +clean: + $(CLEANUP) $(PATHO)*.o + $(CLEANUP) $(PATHB)*.$(TARGET_EXTENSION) + $(CLEANUP) $(PATHR)*.txt -re: fclean all +.PRECIOUS: $(PATHB)Test%.$(TARGET_EXTENSION) +.PRECIOUS: $(PATHD)%.d +.PRECIOUS: $(PATHO)%.o +.PRECIOUS: $(PATHR)%.txt diff --git a/Makefile_no_test b/Makefile_no_test new file mode 100644 index 0000000..8813a78 --- /dev/null +++ b/Makefile_no_test @@ -0,0 +1,57 @@ +NAME = minishell + +SRCS = ./srcs/main.c \ + ./srcs/parse_envp.c \ + ./srcs/utils/t_simple_cmds_utils.c \ + ./srcs/utils/t_lexor_utils.c \ + ./srcs/utils/utils.c \ + ./srcs/lexor/token_reader.c \ + ./srcs/lexor/handle_token.c \ + ./srcs/parser/parser.c \ + ./srcs/builtins/builtins.c \ + ./srcs/builtins/mini_cd.c \ + ./srcs/builtins/mini_echo.c \ + ./srcs/builtins/mini_exit.c \ + ./srcs/builtins/mini_export.c \ + ./srcs/builtins/mini_pwd.c \ + ./srcs/builtins/mini_unset.c \ + ./srcs/builtins/mini_env.c \ + # ./srcs/parser/parse_args.c \ + # ./srcs/pipex/pipex.c + + +OBJS = ${SRCS:%.c=%.o} + +FLAGS = -Wall -Werror -Wextra -g + +LIBFT = ./libraries/libft/libft.a + +HEADER = ./includes/minishell.h \ + ./includes/lexor.h \ + ./includes/builtins.h \ + ./includes/utils.h \ + ./includes/parser.h + +all: $(NAME) + +%.o: %.c $(HEADER) + @echo "Compiling ${notdir $<}" + @$(CC) -c $(FLAGS) -I includes -o $@ $< + +$(NAME): $(LIBFT) $(OBJS) $(HEADER) + @$(CC) $(FLAGS) $(LIBFT) $(OBJS) -lreadline -o $(NAME) + @echo "Success" + +$(LIBFT): + $(MAKE) -C ./libraries/libft + +clean: + @echo "Cleaning" + @rm -f $(OBJS) + @make fclean -C libraries/libft + +fclean: clean + @rm -f $(NAME) + @rm -f $(LIBFT) + +re: fclean all diff --git a/build/results/Testmain.txt b/build/results/Testmain.txt new file mode 100644 index 0000000..ccfa9d5 --- /dev/null +++ b/build/results/Testmain.txt @@ -0,0 +1,6 @@ +test/Testmain.c:41:test_AverageThreeBytes_should_AverageMidRangeValues:PASS +test/Testmain.c:42:test_AverageThreeBytes_should_AverageHighValues:PASS + +----------------------- +2 Tests 0 Failures 0 Ignored +OK diff --git a/includes/minishell.h b/includes/minishell.h index c8d6cf3..d86528b 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/02/23 12:16:34 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/28 15:39:20 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ # include # include # include -# include "../srcs/pipex/pipex.h" +# include "pipex.h" # include "parser.h" # include "utils.h" # include "lexor.h" @@ -26,8 +26,7 @@ int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); -int count_quotes(char *line); -void delete_char(char *str, char c); +int8_t AverageThreeBytes(int8_t a, int8_t b, int8_t c); //builtins int (*builtin_arr(char *str))(t_tools *tools); diff --git a/includes/parser.h b/includes/parser.h index f02f92d..b826539 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/02/23 10:19:40 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/28 09:41:55 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ typedef enum s_tokens GREAT_GREAT, LESS, LESS_LESS, - PS2, + DOLLAR, } t_tokens; typedef struct s_lexor diff --git a/srcs/builtins/builtins.c b/src/builtins/builtins.c similarity index 100% rename from srcs/builtins/builtins.c rename to src/builtins/builtins.c diff --git a/srcs/builtins/mini_cd.c b/src/builtins/mini_cd.c similarity index 100% rename from srcs/builtins/mini_cd.c rename to src/builtins/mini_cd.c diff --git a/srcs/builtins/mini_echo.c b/src/builtins/mini_echo.c similarity index 100% rename from srcs/builtins/mini_echo.c rename to src/builtins/mini_echo.c diff --git a/srcs/builtins/mini_env.c b/src/builtins/mini_env.c similarity index 100% rename from srcs/builtins/mini_env.c rename to src/builtins/mini_env.c diff --git a/srcs/builtins/mini_exit.c b/src/builtins/mini_exit.c similarity index 100% rename from srcs/builtins/mini_exit.c rename to src/builtins/mini_exit.c diff --git a/srcs/builtins/mini_export.c b/src/builtins/mini_export.c similarity index 100% rename from srcs/builtins/mini_export.c rename to src/builtins/mini_export.c diff --git a/srcs/builtins/mini_pwd.c b/src/builtins/mini_pwd.c similarity index 100% rename from srcs/builtins/mini_pwd.c rename to src/builtins/mini_pwd.c diff --git a/srcs/builtins/mini_unset.c b/src/builtins/mini_unset.c similarity index 100% rename from srcs/builtins/mini_unset.c rename to src/builtins/mini_unset.c diff --git a/srcs/lexor/handle_token.c b/src/lexor/handle_token.c similarity index 95% rename from srcs/lexor/handle_token.c rename to src/lexor/handle_token.c index 5188088..f9953c1 100644 --- a/srcs/lexor/handle_token.c +++ b/src/lexor/handle_token.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/18 10:27:43 by mgraaf #+# #+# */ -/* Updated: 2022/02/23 12:00:01 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/28 10:27:41 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -20,7 +20,7 @@ t_tokens check_token(int c) {'\n', NEW_LINE}, {'>', GREAT}, {'<', LESS}, - {92, PS2}, + {'$', DOLLAR}, }; int i; diff --git a/srcs/lexor/token_reader.c b/src/lexor/token_reader.c similarity index 100% rename from srcs/lexor/token_reader.c rename to src/lexor/token_reader.c diff --git a/srcs/main.c b/src/main.c similarity index 84% rename from srcs/main.c rename to src/main.c index 2a610a8..56f19ad 100644 --- a/srcs/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/02/23 15:39:03 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/28 15:42:50 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -48,16 +48,9 @@ int main(int argc, char **argv, char **envp) { line = readline("minishell$ "); add_history(line); - while (count_quotes(line) % 2 != 0 || line[ft_strlen(line) - 1] == 92) - { - if (line[ft_strlen(line) - 1] == 92) - line = ft_substr(line, 0, ft_strlen(line) - 1); - line = ft_strjoin(line, readline("> ")); - } - delete_char(line, 92); tools.args = line; lexor_list = token_reader(&tools); - parser(lexor_list); + // parser(lexor_list); // while (lexor_list) // { // printf("str = %s \t token = %d\n", lexor_list->str, lexor_list->token); diff --git a/srcs/parse_envp.c b/src/parse_envp.c similarity index 100% rename from srcs/parse_envp.c rename to src/parse_envp.c diff --git a/srcs/parser/handle_redirections.c b/src/parser/handle_redirections.c similarity index 100% rename from srcs/parser/handle_redirections.c rename to src/parser/handle_redirections.c diff --git a/srcs/parser/parser.c b/src/parser/parser.c similarity index 97% rename from srcs/parser/parser.c rename to src/parser/parser.c index 58f5a44..532408a 100644 --- a/srcs/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/02/23 15:46:19 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/28 15:48:27 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -109,6 +109,9 @@ void parser(t_lexor *lexor_list) while (arg_size--) lexor_list = lexor_list->next; } +} + +// >> means write over file int i = 0; while(simple_cmds) { @@ -127,7 +130,4 @@ while(simple_cmds) if (simple_cmds->builtin) printf("BUILTIN :)\n"); simple_cmds = simple_cmds->next; -} -} - -// >> means write over file \ No newline at end of file +} \ No newline at end of file diff --git a/srcs/pipex/pipex.c b/src/pipex/pipex.c similarity index 100% rename from srcs/pipex/pipex.c rename to src/pipex/pipex.c diff --git a/srcs/pipex/pipex.h b/src/pipex/pipex.h similarity index 100% rename from srcs/pipex/pipex.h rename to src/pipex/pipex.h diff --git a/srcs/utils/t_lexor_utils.c b/src/utils/t_lexor_utils.c similarity index 100% rename from srcs/utils/t_lexor_utils.c rename to src/utils/t_lexor_utils.c diff --git a/srcs/utils/t_simple_cmds_utils.c b/src/utils/t_simple_cmds_utils.c similarity index 100% rename from srcs/utils/t_simple_cmds_utils.c rename to src/utils/t_simple_cmds_utils.c diff --git a/srcs/utils/utils.c b/src/utils/utils.c similarity index 64% rename from srcs/utils/utils.c rename to src/utils/utils.c index fc7a534..eddbce3 100644 --- a/srcs/utils/utils.c +++ b/src/utils/utils.c @@ -6,49 +6,9 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/21 11:17:26 by fpolycar #+# #+# */ -/* Updated: 2022/02/23 12:19:51 by fpolycar ######## odam.nl */ +/* Updated: 2022/02/28 09:28:22 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -int count_quotes(char *line) -{ - int i; - int nb; - - i = 0; - nb = 0; - while (line[i]) - { - if (line[i] == 34 || line[i] == 39) - nb++; - i++; - } - return (nb); -} - -void delete_char(char *str, char c) -{ - int len; - int i; - int j; - - len = ft_strlen(str); - i = 0; - while (i < len) - { - if (str[i] == c) - { - j = i; - while (j < len) - { - str[i] = str[j + 1]; - j++; - } - len--; - i--; - } - i++; - } -} diff --git a/test/Testmain.c b/test/Testmain.c new file mode 100644 index 0000000..25513c5 --- /dev/null +++ b/test/Testmain.c @@ -0,0 +1,43 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* Testmain.c :+: :+: */ +/* +:+ */ +/* By: fpolycar +#+ */ +/* +#+ */ +/* Created: 2022/02/28 11:12:08 by fpolycar #+# #+# */ +/* Updated: 2022/02/28 16:02:46 by fpolycar ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "unity.h" +#include "minishell.h" + + + +void setUp(void) { + // set stuff up here +} + +void tearDown(void) { + // clean stuff up here +} + +void test_parser_should_word(void) +{ +// t_lexor *lexor_list; +// char *str = "echo test | cd nice"; + + +TEST_ASSERT_EQUAL_HEX8(0, main()); +// TEST_ASSERT_EQUAL_HEX8(40, AverageThreeBytes(10, 70, 40)); +// TEST_ASSERT_EQUAL_HEX8(33, AverageThreeBytes(33, 33, 33)); +} + + +int main(void) +{ +UNITY_BEGIN(); +RUN_TEST(test_parser_should_word); +return UNITY_END(); +} diff --git a/unity b/unity new file mode 160000 index 0000000..db878cc --- /dev/null +++ b/unity @@ -0,0 +1 @@ +Subproject commit db878ccaedaea3d07b3c5443a00d131ecacd30b3 diff --git a/srcs/minitalk/Makefile b/utilities/minitalk/Makefile similarity index 100% rename from srcs/minitalk/Makefile rename to utilities/minitalk/Makefile diff --git a/srcs/minitalk/client.c b/utilities/minitalk/client.c similarity index 100% rename from srcs/minitalk/client.c rename to utilities/minitalk/client.c diff --git a/srcs/minitalk/minitalk.h b/utilities/minitalk/minitalk.h similarity index 100% rename from srcs/minitalk/minitalk.h rename to utilities/minitalk/minitalk.h diff --git a/srcs/minitalk/server.c b/utilities/minitalk/server.c similarity index 100% rename from srcs/minitalk/server.c rename to utilities/minitalk/server.c From 732a160ce4bb2a99977ecc68b9cc0686ffc83fe0 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Mon, 28 Feb 2022 16:15:40 +0100 Subject: [PATCH 033/163] tester makefile --- build/results/Testmain.txt | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 build/results/Testmain.txt diff --git a/build/results/Testmain.txt b/build/results/Testmain.txt deleted file mode 100644 index ccfa9d5..0000000 --- a/build/results/Testmain.txt +++ /dev/null @@ -1,6 +0,0 @@ -test/Testmain.c:41:test_AverageThreeBytes_should_AverageMidRangeValues:PASS -test/Testmain.c:42:test_AverageThreeBytes_should_AverageHighValues:PASS - ------------------------ -2 Tests 0 Failures 0 Ignored -OK From edd85476443241d892708ef4a407753275639a8d Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Tue, 1 Mar 2022 13:38:45 +0100 Subject: [PATCH 034/163] no worky --- Makefile | 1 + includes/executor.h | 12 +++-- includes/parser.h | 3 +- srcs/builtins/mini_cd.c | 2 +- srcs/executor/check_redirections.c | 75 ++++++++++++++++++++++++++++++ srcs/executor/executor.c | 68 ++++----------------------- srcs/main.c | 19 ++++---- 7 files changed, 103 insertions(+), 77 deletions(-) create mode 100644 srcs/executor/check_redirections.c diff --git a/Makefile b/Makefile index e7a94e7..6b35fd9 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,7 @@ SRCS = ./srcs/main.c \ ./srcs/builtins/mini_unset.c \ ./srcs/builtins/mini_env.c \ ./srcs/executor/executor.c \ + ./srcs/executor/check_redirections.c \ # ./srcs/parser/parse_args.c \ # ./srcs/pipex/pipex.c diff --git a/includes/executor.h b/includes/executor.h index 4e30e61..eb7616a 100644 --- a/includes/executor.h +++ b/includes/executor.h @@ -6,10 +6,16 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:17:39 by mgraaf #+# #+# */ -/* Updated: 2022/02/24 15:19:56 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/25 11:53:28 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ -#include "minishell.h" +#ifndef EXECUTOR_H +# define EXECUTOR_H +# include "minishell.h" -int executor(t_tools *tools); \ No newline at end of file +int check_outfile(t_tools *tools); +int check_infile(t_tools *tools); +int executor(t_tools *tools); + +#endif \ No newline at end of file diff --git a/includes/parser.h b/includes/parser.h index 2af8bec..0e23ebd 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/02/24 15:20:42 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/25 14:39:29 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -55,7 +55,6 @@ typedef struct s_simple_cmds struct s_simple_cmds *next; } t_simple_cmds; - int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); void parser(t_lexor *lexor_list, t_tools *tools); diff --git a/srcs/builtins/mini_cd.c b/srcs/builtins/mini_cd.c index e75edb4..8b60622 100644 --- a/srcs/builtins/mini_cd.c +++ b/srcs/builtins/mini_cd.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:17:04 by mgraaf #+# #+# */ -/* Updated: 2022/02/21 13:46:52 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/25 15:02:13 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/srcs/executor/check_redirections.c b/srcs/executor/check_redirections.c new file mode 100644 index 0000000..2db7acb --- /dev/null +++ b/srcs/executor/check_redirections.c @@ -0,0 +1,75 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* check_redirections.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/02/25 11:39:57 by mgraaf #+# #+# */ +/* Updated: 2022/02/25 12:10:51 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "executor.h" + +void check_append_outfile(t_tools *tools) +{ + if (tools->simple_cmds->redirections->token == GREAT) + tools->out = open(tools->simple_cmds->redirections->str, + O_CREAT | O_RDWR | O_TRUNC | O_APPEND, 0644); + else + tools->out = open(tools->simple_cmds->redirections->str, + O_CREAT | O_RDWR | O_TRUNC, 0644); +} + +int check_outfile(t_tools *tools) +{ + t_lexor *start; + int i; + + i = 0; + start = tools->simple_cmds->redirections; + while (tools->simple_cmds->redirections) + { + if (tools->simple_cmds->redirections->token == GREAT + || tools->simple_cmds->redirections->token == GREAT_GREAT) + { + check_append_outfile(tools); + if (tools->out < 0) + { + perror("Open "); + // do more stuff (ie lots of freeing) + } + i++; + } + tools->simple_cmds->redirections + = tools->simple_cmds->redirections->next; + } + if (i == 0) + tools->out = dup(1); + tools->simple_cmds->redirections = start; + return (tools->out); +} + +int check_infile(t_tools *tools) +{ + t_lexor *start; + + start = tools->simple_cmds->redirections; + while (tools->simple_cmds->redirections) + { + if (tools->simple_cmds->redirections->token == LESS) + { + tools->in = open(tools->simple_cmds->redirections->str, O_RDONLY); + if (tools->in < 0) + { + perror("Open "); + // do more stuff (ie lots of freeing) + } + } + tools->simple_cmds->redirections + = tools->simple_cmds->redirections->next; + } + tools->simple_cmds->redirections = start; + return (tools->in); +} diff --git a/srcs/executor/executor.c b/srcs/executor/executor.c index 2079b2c..396701a 100644 --- a/srcs/executor/executor.c +++ b/srcs/executor/executor.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/02/24 17:51:05 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/25 14:41:17 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,70 +18,20 @@ int find_cmd(char **args, char **paths, char **envp) char *mycmd; i = 0; - printf("HELLO\n"); + fprintf(stderr, "in find_cmd\n"); while (paths[i]) { mycmd = ft_strjoin(paths[i], args[0]); + fprintf(stderr, "%i\n", i); if (!access(mycmd, F_OK)) execve(mycmd, args, envp); free(mycmd); i++; } + printf("HELO\n"); return (1); } -int check_outfile(t_tools *tools) -{ - t_lexor *start; - - start = tools->simple_cmds->redirections; - while (tools->simple_cmds->redirections) - { - if (tools->simple_cmds->redirections->token == GREAT - || tools->simple_cmds->redirections->token == GREAT_GREAT) - { - if (tools->simple_cmds->redirections->token == GREAT) - tools->out = open(tools->simple_cmds->redirections->str, - O_CREAT | O_RDWR | O_TRUNC | O_APPEND, 0644); - else - tools->out = open(tools->simple_cmds->redirections->str, - O_CREAT | O_RDWR | O_TRUNC, 0644); - if (tools->out < 0) - { - perror("Open "); - // do more stuff (ie lots of freeing) - } - } - tools->simple_cmds->redirections - = tools->simple_cmds->redirections->next; - } - tools->simple_cmds->redirections = start; - return (tools->out); -} - -int check_infile(t_tools *tools) -{ - t_lexor *start; - - start = tools->simple_cmds->redirections; - while (tools->simple_cmds->redirections) - { - if (tools->simple_cmds->redirections->token == LESS) - { - tools->in = open(tools->simple_cmds->redirections->str, O_RDONLY); - if (tools->in < 0) - { - perror("Open "); - // do more stuff (ie lots of freeing) - } - } - tools->simple_cmds->redirections - = tools->simple_cmds->redirections->next; - } - tools->simple_cmds->redirections = start; - return (tools->in); -} - int executor(t_tools *tools) { int i; @@ -96,17 +46,14 @@ int executor(t_tools *tools) dup2(tools->in, 0); close(tools->in); if (i == tools->pipes) - { - tools->out = dup(1); check_outfile(tools); - } else { pipe(end); tools->out = end[1]; tools->in = end[0]; } - printf("HELo\n"); + fprintf(stderr, "\nb4 dup out: in = %d out = %d\nend[0] = %d, end[1] = %d\n", tools->in, tools->out, end[0], end[1]); dup2(tools->out, 1); close(tools->out); ret = fork(); @@ -117,12 +64,13 @@ int executor(t_tools *tools) } if (ret == 0) { - printf("b4 find_cmd\n"); + fprintf(stderr, "\nb4 find_cmd: %s\nin = %d out = %d\n", tools->simple_cmds->str[0], tools->in, tools->out); find_cmd(tools->simple_cmds->str, tools->paths, tools->envp); } i++; + fprintf(stderr, "\nguess who's back\n"); ft_simple_cmds_rm_first(&tools->simple_cmds); - waitpid(ret, &status, 0); } + waitpid(ret, &status, 0); return (0); } diff --git a/srcs/main.c b/srcs/main.c index dd29010..fad39d1 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/02/24 17:33:48 by mgraaf ######## odam.nl */ +/* Updated: 2022/02/25 12:20:04 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,13 +17,11 @@ int implement_tools(t_tools *tools) tools->in = dup(0); tools->out = dup(1); tools->err = dup(2); - return (1); } int main(int argc, char **argv, char **envp) { - char *line; t_tools tools; t_lexor *lexor_list = NULL; @@ -37,19 +35,18 @@ int main(int argc, char **argv, char **envp) implement_tools(&tools); while (1) { - line = readline("minishell$ "); - add_history(line); - while (count_quotes(line) % 2 != 0 || line[ft_strlen(line) - 1] == 92) + tools.args = readline("minishell$ "); + add_history(tools.args); + while (count_quotes(tools.args) % 2 != 0 || tools.args[ft_strlen(tools.args) - 1] == 92) { - if (line[ft_strlen(line) - 1] == 92) - line = ft_substr(line, 0, ft_strlen(line) - 1); - line = ft_strjoin(line, readline("> ")); + if (tools.args[ft_strlen(tools.args) - 1] == 92) + tools.args = ft_substr(tools.args, 0, ft_strlen(tools.args) - 1); + tools.args = ft_strjoin(tools.args, readline("> ")); } - tools.args = line; lexor_list = token_reader(&tools); parser(lexor_list, &tools); executor(&tools); - free(line); + free(tools.args); } return (0); } From 2a9935c24ef60b17b2c3cff8a36d05e16af4620b Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Tue, 1 Mar 2022 13:43:17 +0100 Subject: [PATCH 035/163] 2 makefile + starting tester --- test/Makefile | 90 +++++++++++++++++++++++++ test/{Testmain.c => Testtoken_reader.c} | 25 ++++--- 2 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 test/Makefile rename test/{Testmain.c => Testtoken_reader.c} (74%) diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 0000000..af2b909 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,90 @@ +CLEANUP=rm -f +MKDIR=mkdir -p +TARGET_EXTENSION=out + +.PHONY: clean +.PHONY: test + +PATHU = ../unity/src/ +PATHI = ../includes/ +PATHS = ../src/ +PATHSL = ../src/lexor/ +PATHSP = ../src/parser/ +PATHP = ../src/pipex/ +PATHT = ../test/ +PATHB = ../build/ +PATHD = ../build/depends/ +PATHO = ../build/objs/ +PATHR = ../build/results/ + +BUILD_PATHS = $(PATHB) $(PATHD) $(PATHO) $(PATHR) + +HEADER = $(wildcard ../includes/*.c) + +SRCT = $(wildcard $(PATHT)*.c) + +COMPILE=gcc -c +LINK=gcc +DEPEND=gcc -MM -MG -MF +CFLAGS=-Wall -Werror -Wextra -I. -I$(PATHU) -I$(PATHS) -I$(PATHP) -I$(PATHI) -DTEST + +RESULTS = $(patsubst $(PATHT)Test%.c,$(PATHR)Test%.txt,$(SRCT)) + +PASSED = `grep -s PASS $(PATHR)*.txt` +FAIL = `grep -s FAIL $(PATHR)*.txt` +IGNORE = `grep -s IGNORE $(PATHR)*.txt` + +test: $(BUILD_PATHS) $(RESULTS) + @echo "-----------------------\nIGNORES:\n-----------------------" + @echo "$(IGNORE)" + @echo "-----------------------\nFAILURES:\n-----------------------" + @echo "$(FAIL)" + @echo "-----------------------\nPASSED:\n-----------------------" + @echo "$(PASSED)" + @echo "\nDONE" + +$(PATHR)%.txt: $(PATHB)%.$(TARGET_EXTENSION) + -./$< > $@ 2>&1 + +$(PATHB)Test%.$(TARGET_EXTENSION): $(PATHO)Test%.o $(PATHO)%.o $(PATHU)unity.o #$(PATHD)Test%.d + $(LINK) $(CFLAGS) -o $@ $^ + +$(PATHO)%.o:: $(PATHT)%.c $(HEADERS) + $(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHO)%.o:: $(PATHS)%.c $(HEADERS) + $(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHO)%.o:: $(PATHSL)%.c $(HEADERS) + $(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHO)%.o:: $(PATHSP)%.c $(HEADERS) + $(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHO)%.o:: $(PATHU)%.c $(PATHU)%.h + $(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHD)%.d:: $(PATHT)%.c + $(DEPEND) $@ $< + +$(PATHB): + $(MKDIR) $(PATHB) + +$(PATHD): + $(MKDIR) $(PATHD) + +$(PATHO): + $(MKDIR) $(PATHO) + +$(PATHR): + $(MKDIR) $(PATHR) + +clean: + $(CLEANUP) $(PATHO)*.o + $(CLEANUP) $(PATHB)*.$(TARGET_EXTENSION) + $(CLEANUP) $(PATHR)*.txt + +.PRECIOUS: $(PATHB)Test%.$(TARGET_EXTENSION) +.PRECIOUS: $(PATHD)%.d +.PRECIOUS: $(PATHO)%.o +.PRECIOUS: $(PATHR)%.txt diff --git a/test/Testmain.c b/test/Testtoken_reader.c similarity index 74% rename from test/Testmain.c rename to test/Testtoken_reader.c index 25513c5..fdbe12b 100644 --- a/test/Testmain.c +++ b/test/Testtoken_reader.c @@ -6,14 +6,15 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/28 11:12:08 by fpolycar #+# #+# */ -/* Updated: 2022/02/28 16:02:46 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/01 13:38:49 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "unity.h" #include "minishell.h" - +t_tools tools; +t_lexor *lexor; void setUp(void) { // set stuff up here @@ -23,21 +24,27 @@ void tearDown(void) { // clean stuff up here } -void test_parser_should_word(void) +void init_test(char *line) +{ + tools.args = line; + +} + +void test_parser_1(void) { -// t_lexor *lexor_list; -// char *str = "echo test | cd nice"; + init_test(" test "); + TEST_ASSERT_EQUAL_STRING("echo", token_reader(&tools)->str); +} -TEST_ASSERT_EQUAL_HEX8(0, main()); -// TEST_ASSERT_EQUAL_HEX8(40, AverageThreeBytes(10, 70, 40)); +// TEST_ASSERT_EQUAL_STRING("echo", token_reader(&tools)->str); // TEST_ASSERT_EQUAL_HEX8(33, AverageThreeBytes(33, 33, 33)); -} +// } int main(void) { UNITY_BEGIN(); -RUN_TEST(test_parser_should_word); +RUN_TEST(test_parser_1); return UNITY_END(); } From ec80b4635e54aebb5ae03470ca1266f46f300db7 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Tue, 1 Mar 2022 13:50:41 +0100 Subject: [PATCH 036/163] 2 commit makefile --- .gitignore | 3 +- Makefile | 116 ++++++++---------- Makefile_no_test | 57 --------- src/main.c | 7 +- ...le_redirections.c => handle_redirections.} | 0 src/parser/parser.c | 82 +++++-------- test/Makefile | 3 + 7 files changed, 99 insertions(+), 169 deletions(-) delete mode 100644 Makefile_no_test rename src/parser/{handle_redirections.c => handle_redirections.} (100%) diff --git a/.gitignore b/.gitignore index 7eb60c8..adf21f6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.o libft.a -minishell \ No newline at end of file +minishell +build \ No newline at end of file diff --git a/Makefile b/Makefile index 836c38a..78b0183 100644 --- a/Makefile +++ b/Makefile @@ -1,89 +1,81 @@ -NAME = testexe -CLEANUP=rm -f -MKDIR=mkdir -p -TARGET_EXTENSION=out +NAME = minishell -.PHONY: clean -.PHONY: test +CC = gcc -PATHU = unity/src/ -PATHI = includes/ -PATHS = src/ -PATHSL = src/lexor -PATHSP = src/parser -PATHP = src/pipex -PATHT = test/ PATHB = build/ -PATHD = build/depends/ PATHO = build/objs/ -PATHR = build/results/ +PATHS = src/ +PATHSL = src/lexor/ +PATHSP = src/parser/ +PATHSB = src/builtins/ +PATHSU = src/utils/ +PATHP = src/pipex/ + BUILD_PATHS = $(PATHB) $(PATHD) $(PATHO) $(PATHR) -SRCT = $(wildcard $(PATHT)*.c) +src = $(wildcard $(PATHS)*.c) \ + $(wildcard $(PATHSL)*.c) \ + $(wildcard $(PATHSP)*.c) \ + $(wildcard $(PATHSB)*.c) \ + $(wildcard $(PATHSU)*.c) -COMPILE=gcc -c -LINK=gcc -DEPEND=gcc -MM -MG -MF -CFLAGS=-Wall -Werror -Wextra -I. -I$(PATHU) -I$(PATHS) -I$(PATHP) -I$(PATHI) -DTEST +OBJS = $(addprefix $(PATHO), $(notdir $(patsubst %.c, %.o, $(src)))) -RESULTS = $(patsubst $(PATHT)Test%.c,$(PATHR)Test%.txt,$(SRCT)) +FLAGS = -Wall -Werror -Wextra -g -PASSED = `grep -s PASS $(PATHR)*.txt` -FAIL = `grep -s FAIL $(PATHR)*.txt` -IGNORE = `grep -s IGNORE $(PATHR)*.txt` +LIBFT = ./libraries/libft/libft.a -test: $(BUILD_PATHS) $(RESULTS) - @echo "-----------------------\nIGNORES:\n-----------------------" - @echo "$(IGNORE)" - @echo "-----------------------\nFAILURES:\n-----------------------" - @echo "$(FAIL)" - @echo "-----------------------\nPASSED:\n-----------------------" - @echo "$(PASSED)" - @echo "\nDONE" +HEADER = $(wildcard ./includes/*.c) + +INCLUDES =-Iincludes -Isrc/pipex -$(PATHR)%.txt: $(PATHB)%.$(TARGET_EXTENSION) - -./$< > $@ 2>&1 -$(PATHB)Test%.$(TARGET_EXTENSION): $(PATHO)Test%.o $(PATHO)%.o $(PATHU)unity.o #$(PATHD)Test%.d - $(LINK) -o $@ $^ +all: $(BUILD_PATHS) $(NAME) -$(PATHO)%.o:: $(PATHT)%.c - $(COMPILE) $(CFLAGS) $< -o $@ +$(PATHO)%.o:: $(PATHS)%.c + @echo $(OBJS) + @echo "Compiling ${notdir $<} in $(PATHS)" + @$(CC) -c $(FLAGS) $(INCLUDES) $< -o $@ -$(PATHO)%.o:: $(PATHS)%.c - $(COMPILE) $(CFLAGS) $< -o $@ +$(PATHO)%.o:: $(PATHSL)%.c $(HEADERS) + @echo "Compiling ${notdir $<} in $(PATHSL)" + @$(CC) -c $(FLAGS) $(INCLUDES) $< -o $@ -$(PATHO)%.o:: $(PATHSL)%.c - $(COMPILE) $(CFLAGS) $< -o $@ +$(PATHO)%.o:: $(PATHSP)%.c $(HEADERS) + @echo "Compiling ${notdir $<} in $(PATHSP)" + @$(CC) -c $(FLAGS) $(INCLUDES) $< -o $@ -$(PATHO)%.o:: $(PATHSP)%.c - $(COMPILE) $(CFLAGS) $< -o $@ +$(PATHO)%.o:: $(PATHSB)%.c $(HEADERS) + @echo "Compiling ${notdir $<} in $(PATHSB)" + @$(CC) -c $(FLAGS) $(INCLUDES) $< -o $@ -$(PATHO)%.o:: $(PATHU)%.c $(PATHU)%.h - $(COMPILE) $(CFLAGS) $< -o $@ +$(PATHO)%.o:: $(PATHSU)%.c $(HEADERS) + @echo "Compiling ${notdir $<} in $(PATHSU)" + @$(CC) -c $(FLAGS) $(INCLUDES) $< -o $@ -$(PATHD)%.d:: $(PATHT)%.c - $(DEPEND) $@ $< +$(NAME): $(LIBFT) $(OBJS) $(HEADERS) + @$(CC) $(FLAGS) $(LIBFT) $(OBJS) -lreadline -o $(NAME) + @echo "Success" + +$(LIBFT): + $(MAKE) -C ./libraries/libft $(PATHB): $(MKDIR) $(PATHB) -$(PATHD): - $(MKDIR) $(PATHD) - $(PATHO): $(MKDIR) $(PATHO) -$(PATHR): - $(MKDIR) $(PATHR) - clean: - $(CLEANUP) $(PATHO)*.o - $(CLEANUP) $(PATHB)*.$(TARGET_EXTENSION) - $(CLEANUP) $(PATHR)*.txt - -.PRECIOUS: $(PATHB)Test%.$(TARGET_EXTENSION) -.PRECIOUS: $(PATHD)%.d -.PRECIOUS: $(PATHO)%.o -.PRECIOUS: $(PATHR)%.txt + @echo "Cleaning" + @rm -f $(OBJS) + @make fclean -C libraries/libft + +fclean: clean + @rm -f $(NAME) + @rm -f $(LIBFT) + +re: fclean all + +# .PRECIOUS: $(PATHO)%.o \ No newline at end of file diff --git a/Makefile_no_test b/Makefile_no_test deleted file mode 100644 index 8813a78..0000000 --- a/Makefile_no_test +++ /dev/null @@ -1,57 +0,0 @@ -NAME = minishell - -SRCS = ./srcs/main.c \ - ./srcs/parse_envp.c \ - ./srcs/utils/t_simple_cmds_utils.c \ - ./srcs/utils/t_lexor_utils.c \ - ./srcs/utils/utils.c \ - ./srcs/lexor/token_reader.c \ - ./srcs/lexor/handle_token.c \ - ./srcs/parser/parser.c \ - ./srcs/builtins/builtins.c \ - ./srcs/builtins/mini_cd.c \ - ./srcs/builtins/mini_echo.c \ - ./srcs/builtins/mini_exit.c \ - ./srcs/builtins/mini_export.c \ - ./srcs/builtins/mini_pwd.c \ - ./srcs/builtins/mini_unset.c \ - ./srcs/builtins/mini_env.c \ - # ./srcs/parser/parse_args.c \ - # ./srcs/pipex/pipex.c - - -OBJS = ${SRCS:%.c=%.o} - -FLAGS = -Wall -Werror -Wextra -g - -LIBFT = ./libraries/libft/libft.a - -HEADER = ./includes/minishell.h \ - ./includes/lexor.h \ - ./includes/builtins.h \ - ./includes/utils.h \ - ./includes/parser.h - -all: $(NAME) - -%.o: %.c $(HEADER) - @echo "Compiling ${notdir $<}" - @$(CC) -c $(FLAGS) -I includes -o $@ $< - -$(NAME): $(LIBFT) $(OBJS) $(HEADER) - @$(CC) $(FLAGS) $(LIBFT) $(OBJS) -lreadline -o $(NAME) - @echo "Success" - -$(LIBFT): - $(MAKE) -C ./libraries/libft - -clean: - @echo "Cleaning" - @rm -f $(OBJS) - @make fclean -C libraries/libft - -fclean: clean - @rm -f $(NAME) - @rm -f $(LIBFT) - -re: fclean all diff --git a/src/main.c b/src/main.c index a3ff88f..42d51e0 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/02/28 16:13:20 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/01 09:49:07 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -60,3 +60,8 @@ int main(int argc, char **argv, char **envp) } return (0); } + +// int8_t AverageThreeBytes(int8_t a, int8_t b, int8_t c) +// { +// return (int8_t)(((int16_t)a + (int16_t)b + (int16_t)c) / 3); +// } \ No newline at end of file diff --git a/src/parser/handle_redirections.c b/src/parser/handle_redirections. similarity index 100% rename from src/parser/handle_redirections.c rename to src/parser/handle_redirections. diff --git a/src/parser/parser.c b/src/parser/parser.c index 3a78ebb..9b1fecf 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/02/28 16:13:34 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/01 09:55:26 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ int count_args(t_lexor *lexor_list) i = 0; tmp = lexor_list; - while (tmp && tmp->token != PIPE && tmp->token != PS2) + while (tmp && tmp->token != PIPE) { i++; tmp = tmp->next; @@ -47,16 +47,6 @@ int add_redirection(t_lexor **redirections, t_lexor **lexor_list, return (0); } -int ps2_token(t_lexor **lexor_list) -{ - if (*lexor_list && (*lexor_list)->token == PS2) - { - *lexor_list = (*lexor_list)->next; - return (1); - } - return (0); -} - t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) { char **str; @@ -74,9 +64,7 @@ t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) { if (add_redirection(&redirections, &lexor_list, &num_redirections)) arg_size--; - // else if (lexor_list->token != PS2) str[i++] = ft_strdup(lexor_list->str); - // printf("%u\n", lexor_list->token); lexor_list = lexor_list->next; arg_size--; } @@ -100,8 +88,6 @@ void parser(t_lexor *lexor_list) { if (lexor_list->token == PIPE) lexor_list = lexor_list->next; - else if (lexor_list->token == PS2) - ps2_token(&lexor_list); arg_size = count_args(lexor_list); node = initialize_cmd(lexor_list, arg_size); if (!simple_cmds) @@ -114,35 +100,35 @@ void parser(t_lexor *lexor_list) } // >> means write over file -int i = 0; -while(simple_cmds) -{ - printf("\n%i\n", i++); - while (*simple_cmds->str) - { - printf("%s\n", *simple_cmds->str++); - } - if (simple_cmds->redirections) - printf("\tredirections:\n"); - while (simple_cmds->redirections) - { - printf("\n%i\n", i++); - while (*simple_cmds->str) - { - printf("%s\n", *simple_cmds->str++); - } - if (simple_cmds->redirections) - printf("\tredirections:\n"); - while (simple_cmds->redirections) - { - printf("\t%s\t%d\n", simple_cmds->redirections->str, simple_cmds->redirections->token); - simple_cmds->redirections = simple_cmds->redirections->next; - } - if (simple_cmds->builtin) - printf("BUILTIN :)\n"); - simple_cmds = simple_cmds->next; - } - if (simple_cmds->builtin) - printf("BUILTIN :)\n"); - simple_cmds = simple_cmds->next; -} +// int i = 0; +// while(simple_cmds) +// { +// printf("\n%i\n", i++); +// while (*simple_cmds->str) +// { +// printf("%s\n", *simple_cmds->str++); +// } +// if (simple_cmds->redirections) +// printf("\tredirections:\n"); +// while (simple_cmds->redirections) +// { +// printf("\n%i\n", i++); +// while (*simple_cmds->str) +// { +// printf("%s\n", *simple_cmds->str++); +// } +// if (simple_cmds->redirections) +// printf("\tredirections:\n"); +// while (simple_cmds->redirections) +// { +// printf("\t%s\t%d\n", simple_cmds->redirections->str, simple_cmds->redirections->token); +// simple_cmds->redirections = simple_cmds->redirections->next; +// } +// if (simple_cmds->builtin) +// printf("BUILTIN :)\n"); +// simple_cmds = simple_cmds->next; +// } +// if (simple_cmds->builtin) +// printf("BUILTIN :)\n"); +// simple_cmds = simple_cmds->next; +// } diff --git a/test/Makefile b/test/Makefile index af2b909..8c5a4d3 100644 --- a/test/Makefile +++ b/test/Makefile @@ -84,6 +84,9 @@ clean: $(CLEANUP) $(PATHB)*.$(TARGET_EXTENSION) $(CLEANUP) $(PATHR)*.txt +fclean: + clean + .PRECIOUS: $(PATHB)Test%.$(TARGET_EXTENSION) .PRECIOUS: $(PATHD)%.d .PRECIOUS: $(PATHO)%.o From ef063c55ec310485a498c35a44682b68c380b27b Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Tue, 1 Mar 2022 17:36:11 +0100 Subject: [PATCH 037/163] fix makefile --- Makefile | 8 +++---- includes/builtins.h | 2 +- includes/minishell.h | 5 ++-- src/lexor/token_reader.c | 2 +- test/Makefile | 50 +++++++++++++++++++++++++++++----------- test/Testtoken_reader.c | 41 +++++++++++++++++++------------- 6 files changed, 69 insertions(+), 39 deletions(-) diff --git a/Makefile b/Makefile index 78b0183..0d3848f 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,7 @@ NAME = minishell CC = gcc +LIBFTP = libraries/libft PATHB = build/ PATHO = build/objs/ PATHS = src/ @@ -26,15 +27,14 @@ FLAGS = -Wall -Werror -Wextra -g LIBFT = ./libraries/libft/libft.a -HEADER = $(wildcard ./includes/*.c) +HEADER = $(wildcard ./includes/*.h) -INCLUDES =-Iincludes -Isrc/pipex +INCLUDES =-Iincludes -I$(PATHP) -I$(LIBFTP) all: $(BUILD_PATHS) $(NAME) -$(PATHO)%.o:: $(PATHS)%.c - @echo $(OBJS) +$(PATHO)%.o:: $(PATHS)%.c @echo "Compiling ${notdir $<} in $(PATHS)" @$(CC) -c $(FLAGS) $(INCLUDES) $< -o $@ diff --git a/includes/builtins.h b/includes/builtins.h index 0346f0e..6a53e3f 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:20:00 by mgraaf #+# #+# */ -/* Updated: 2022/02/17 11:25:57 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/01 17:00:19 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/includes/minishell.h b/includes/minishell.h index 685dbe2..9cab065 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,13 +6,13 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/02/28 16:13:44 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/01 16:28:18 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #ifndef MINISHELL_H # define MINISHELL_H -# include "../libraries/libft/libft.h" +# include "libft.h" # include # include # include @@ -26,7 +26,6 @@ int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); -int8_t AverageThreeBytes(int8_t a, int8_t b, int8_t c); //builtins int (*builtin_arr(char *str))(t_tools *tools); diff --git a/src/lexor/token_reader.c b/src/lexor/token_reader.c index 3c63c25..0e4c594 100644 --- a/src/lexor/token_reader.c +++ b/src/lexor/token_reader.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:11:20 by mgraaf #+# #+# */ -/* Updated: 2022/02/23 15:39:26 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/01 17:18:16 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/test/Makefile b/test/Makefile index 8c5a4d3..5669180 100644 --- a/test/Makefile +++ b/test/Makefile @@ -5,36 +5,52 @@ TARGET_EXTENSION=out .PHONY: clean .PHONY: test +PATHLIBFT = ../libraries/libft PATHU = ../unity/src/ PATHI = ../includes/ PATHS = ../src/ PATHSL = ../src/lexor/ PATHSP = ../src/parser/ PATHP = ../src/pipex/ +PATHSU = ../src/utils/ +PATHSB = ../src/builtins/ PATHT = ../test/ PATHB = ../build/ PATHD = ../build/depends/ PATHO = ../build/objs/ PATHR = ../build/results/ +LIBFT = ../libraries/libft/libft.a + BUILD_PATHS = $(PATHB) $(PATHD) $(PATHO) $(PATHR) -HEADER = $(wildcard ../includes/*.c) +HEADER = $(wildcard $(PATHI)*.h) SRCT = $(wildcard $(PATHT)*.c) +src = $(wildcard $(PATHSL)*.c) \ + $(wildcard $(PATHSP)*.c) \ + $(wildcard $(PATHSU)*.c) \ + $(wildcard $(PATHSB)*.c) + COMPILE=gcc -c + LINK=gcc -DEPEND=gcc -MM -MG -MF -CFLAGS=-Wall -Werror -Wextra -I. -I$(PATHU) -I$(PATHS) -I$(PATHP) -I$(PATHI) -DTEST -RESULTS = $(patsubst $(PATHT)Test%.c,$(PATHR)Test%.txt,$(SRCT)) +CFLAGS=-Wall -Werror -Wextra -I. -I$(PATHU) -I$(PATHLIBFT) -I$(PATHS) -I$(PATHP) -I$(PATHI) + +RESULTS = $(patsubst $(PATHT)Test%.c,$(PATHR)Test%.txt, $(SRCT)) + +OBJS = $(addprefix $(PATHO), $(notdir $(patsubst %.c, %.o, $(src)))) PASSED = `grep -s PASS $(PATHR)*.txt` + FAIL = `grep -s FAIL $(PATHR)*.txt` + IGNORE = `grep -s IGNORE $(PATHR)*.txt` test: $(BUILD_PATHS) $(RESULTS) + @echo $(PATHO) @echo "-----------------------\nIGNORES:\n-----------------------" @echo "$(IGNORE)" @echo "-----------------------\nFAILURES:\n-----------------------" @@ -43,17 +59,17 @@ test: $(BUILD_PATHS) $(RESULTS) @echo "$(PASSED)" @echo "\nDONE" -$(PATHR)%.txt: $(PATHB)%.$(TARGET_EXTENSION) +$(PATHR)%.txt: $(PATHO)%.$(TARGET_EXTENSION) -./$< > $@ 2>&1 -$(PATHB)Test%.$(TARGET_EXTENSION): $(PATHO)Test%.o $(PATHO)%.o $(PATHU)unity.o #$(PATHD)Test%.d - $(LINK) $(CFLAGS) -o $@ $^ +$(PATHO)Test%.$(TARGET_EXTENSION): $(LIBFT) $(OBJS) $(PATHO)Test%.o $(PATHO)%.o $(PATHU)unity.o #$(PATHD)Test%.d + $(LINK) -lreadline -o $@ $^ $(PATHO)%.o:: $(PATHT)%.c $(HEADERS) $(COMPILE) $(CFLAGS) $< -o $@ -$(PATHO)%.o:: $(PATHS)%.c $(HEADERS) - $(COMPILE) $(CFLAGS) $< -o $@ +# $(PATHO)%.o:: $(PATHS)%.c $(HEADERS) +# $(COMPILE) $(CFLAGS) $< -o $@ $(PATHO)%.o:: $(PATHSL)%.c $(HEADERS) $(COMPILE) $(CFLAGS) $< -o $@ @@ -61,11 +77,14 @@ $(PATHO)%.o:: $(PATHSL)%.c $(HEADERS) $(PATHO)%.o:: $(PATHSP)%.c $(HEADERS) $(COMPILE) $(CFLAGS) $< -o $@ -$(PATHO)%.o:: $(PATHU)%.c $(PATHU)%.h +$(PATHO)%.o:: $(PATHSU)%.c $(HEADERS) $(COMPILE) $(CFLAGS) $< -o $@ -$(PATHD)%.d:: $(PATHT)%.c - $(DEPEND) $@ $< +$(PATHO)%.o:: $(PATHSB)%.c $(HEADERS) + $(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHO)%.o:: $(PATHU)%.c $(PATHU)%.h + $(COMPILE) $(CFLAGS) $< -o $@ $(PATHB): $(MKDIR) $(PATHB) @@ -79,13 +98,16 @@ $(PATHO): $(PATHR): $(MKDIR) $(PATHR) +$(LIBFT): + $(MAKE) -C $(PATHLIBFT) + clean: $(CLEANUP) $(PATHO)*.o $(CLEANUP) $(PATHB)*.$(TARGET_EXTENSION) $(CLEANUP) $(PATHR)*.txt + make fclean -C $(PATHLIBFT) -fclean: - clean +re: clean test .PRECIOUS: $(PATHB)Test%.$(TARGET_EXTENSION) .PRECIOUS: $(PATHD)%.d diff --git a/test/Testtoken_reader.c b/test/Testtoken_reader.c index fdbe12b..d118c35 100644 --- a/test/Testtoken_reader.c +++ b/test/Testtoken_reader.c @@ -1,20 +1,20 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* Testmain.c :+: :+: */ +/* Testtoken_reader.c :+: :+: */ /* +:+ */ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/28 11:12:08 by fpolycar #+# #+# */ -/* Updated: 2022/03/01 13:38:49 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/01 17:16:25 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "unity.h" -#include "minishell.h" +#include "lexor.h" -t_tools tools; -t_lexor *lexor; +t_tools test_tools; +t_lexor *test_lexor; void setUp(void) { // set stuff up here @@ -26,25 +26,34 @@ void tearDown(void) { void init_test(char *line) { - tools.args = line; - + test_tools.args = line; + test_lexor = token_reader(&test_tools); +} + +void assert_token(int token, char *expected) +{ + TEST_ASSERT_EQUAL_STRING(expected, test_lexor->str); + TEST_ASSERT_EQUAL_INT(token, test_lexor->token); + test_lexor = test_lexor->next; } void test_parser_1(void) { init_test(" test "); - TEST_ASSERT_EQUAL_STRING("echo", token_reader(&tools)->str); + assert_token( 0, "test"); } - -// TEST_ASSERT_EQUAL_STRING("echo", token_reader(&tools)->str); -// TEST_ASSERT_EQUAL_HEX8(33, AverageThreeBytes(33, 33, 33)); -// } - +void test_parser_2(void) +{ + init_test(" test | test "); + assert_token(0, "test"); + assert_token(PIPE, NULL); + assert_token(0, "test"); +} int main(void) { -UNITY_BEGIN(); -RUN_TEST(test_parser_1); -return UNITY_END(); + UNITY_BEGIN(); + RUN_TEST(test_parser_1); + return UNITY_END(); } From 4df09e792867d22dab303f29e06820564868d95a Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Tue, 1 Mar 2022 18:45:42 +0100 Subject: [PATCH 038/163] trying to rewrite parser --- Makefile | 19 ++-- infile => file1 | 0 file2 | 1 + includes/parser.h | 4 +- libraries/libft/Makefile | 1 - outfile | 0 src/builtins/builtins.c | 6 +- src/main.c | 15 ++-- src/parser/handle_redirections. | 14 --- src/parser/parser.c | 154 +++++++++++++++++++------------- src/{ => utils}/parse_envp.c | 0 src/utils/t_lexor_utils.c | 38 ++++---- 12 files changed, 137 insertions(+), 115 deletions(-) rename infile => file1 (100%) create mode 100644 file2 delete mode 100644 outfile delete mode 100644 src/parser/handle_redirections. rename src/{ => utils}/parse_envp.c (100%) diff --git a/Makefile b/Makefile index 78b0183..6dd508d 100644 --- a/Makefile +++ b/Makefile @@ -10,15 +10,17 @@ PATHSP = src/parser/ PATHSB = src/builtins/ PATHSU = src/utils/ PATHP = src/pipex/ +PATHEX = src/executor/ -BUILD_PATHS = $(PATHB) $(PATHD) $(PATHO) $(PATHR) +BUILD_PATHS = $(PATHB) $(PATHD) $(PATHO) $(PATHR) $(PATHEX) src = $(wildcard $(PATHS)*.c) \ $(wildcard $(PATHSL)*.c) \ $(wildcard $(PATHSP)*.c) \ $(wildcard $(PATHSB)*.c) \ - $(wildcard $(PATHSU)*.c) + $(wildcard $(PATHSU)*.c) \ + $(wildcard $(PATHEX)*.c) OBJS = $(addprefix $(PATHO), $(notdir $(patsubst %.c, %.o, $(src)))) @@ -33,8 +35,7 @@ INCLUDES =-Iincludes -Isrc/pipex all: $(BUILD_PATHS) $(NAME) -$(PATHO)%.o:: $(PATHS)%.c - @echo $(OBJS) +$(PATHO)%.o:: $(PATHS)%.c @echo "Compiling ${notdir $<} in $(PATHS)" @$(CC) -c $(FLAGS) $(INCLUDES) $< -o $@ @@ -54,18 +55,22 @@ $(PATHO)%.o:: $(PATHSU)%.c $(HEADERS) @echo "Compiling ${notdir $<} in $(PATHSU)" @$(CC) -c $(FLAGS) $(INCLUDES) $< -o $@ +$(PATHO)%.o:: $(PATHEX)%.c $(HEADERS) + @echo "Compiling ${notdir $<} in $(PATHEX)" + @$(CC) -c $(FLAGS) $(INCLUDES) $< -o $@ + $(NAME): $(LIBFT) $(OBJS) $(HEADERS) @$(CC) $(FLAGS) $(LIBFT) $(OBJS) -lreadline -o $(NAME) @echo "Success" $(LIBFT): - $(MAKE) -C ./libraries/libft + @$(MAKE) -C ./libraries/libft $(PATHB): - $(MKDIR) $(PATHB) + @$(MKDIR) $(PATHB) $(PATHO): - $(MKDIR) $(PATHO) + @$(MKDIR) $(PATHO) clean: @echo "Cleaning" diff --git a/infile b/file1 similarity index 100% rename from infile rename to file1 diff --git a/file2 b/file2 new file mode 100644 index 0000000..af01f32 --- /dev/null +++ b/file2 @@ -0,0 +1 @@ +skd akljf ksd diff --git a/includes/parser.h b/includes/parser.h index 4966b0e..9adb4d2 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/03/01 13:43:49 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/01 18:18:38 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -29,7 +29,9 @@ typedef struct s_lexor { char *str; t_tokens token; + int i; struct s_lexor *next; + struct s_lexor *prev; } t_lexor; typedef struct s_tools diff --git a/libraries/libft/Makefile b/libraries/libft/Makefile index 0272d93..18693f4 100644 --- a/libraries/libft/Makefile +++ b/libraries/libft/Makefile @@ -73,7 +73,6 @@ endif all: libft.a %.o: %.c $(HEADERFILES) - @echo "Compiling ${notdir $<}" @$(CC) -c $(CFLAGS) -o $@ $< $(NAME): $(ALL_OBJS) diff --git a/outfile b/outfile deleted file mode 100644 index e69de29..0000000 diff --git a/src/builtins/builtins.c b/src/builtins/builtins.c index a07ac17..dab11cd 100644 --- a/src/builtins/builtins.c +++ b/src/builtins/builtins.c @@ -6,11 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 11:42:32 by mgraaf #+# #+# */ -<<<<<<< HEAD:srcs/builtins/builtins.c -/* Updated: 2022/02/24 15:04:55 by mgraaf ######## odam.nl */ -======= -/* Updated: 2022/02/24 15:09:05 by alfred ######## odam.nl */ ->>>>>>> 13faa8f204671af4932f2f2f0432effe154044f3:src/builtins/builtins.c +/* Updated: 2022/03/01 14:07:44 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/main.c b/src/main.c index b22a949..91b1f7b 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/01 13:51:34 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/01 16:35:49 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,11 +23,12 @@ int implement_tools(t_tools *tools) int main(int argc, char **argv, char **envp) { t_tools tools; - t_lexor *lexor_list = NULL; + t_lexor *lexor_list; + lexor_list = NULL; if (argc != 1 || argv[1]) { - printf("Please do not enter any arguments\n"); + printf("This program does not accept arguments\n"); exit(0); } tools.envp = envp; @@ -37,15 +38,9 @@ int main(int argc, char **argv, char **envp) { tools.args = readline("minishell$ "); add_history(tools.args); - while (count_quotes(tools.args) % 2 != 0 || tools.args[ft_strlen(tools.args) - 1] == 92) - { - if (tools.args[ft_strlen(tools.args) - 1] == 92) - tools.args = ft_substr(tools.args, 0, ft_strlen(tools.args) - 1); - tools.args = ft_strjoin(tools.args, readline("> ")); - } lexor_list = token_reader(&tools); parser(lexor_list, &tools); - executor(&tools); + // executor(&tools); free(tools.args); } return (0); diff --git a/src/parser/handle_redirections. b/src/parser/handle_redirections. deleted file mode 100644 index b0caa76..0000000 --- a/src/parser/handle_redirections. +++ /dev/null @@ -1,14 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* handle_redirections.c :+: :+: */ -/* +:+ */ -/* By: mgraaf +#+ */ -/* +#+ */ -/* Created: 2022/02/21 16:00:27 by mgraaf #+# #+# */ -/* Updated: 2022/02/24 10:22:09 by mgraaf ######## odam.nl */ -/* */ -/* ************************************************************************** */ - - -#include "parser.h" diff --git a/src/parser/parser.c b/src/parser/parser.c index 47c4475..a1b430a 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,13 +6,14 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/01 13:55:42 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/01 18:39:17 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" +void print_parser(t_simple_cmds *simple_cmds); -int count_args(t_lexor *lexor_list) +int count_args(t_lexor *lexor_list, t_tools *tools) { t_lexor *tmp; int i; @@ -24,28 +25,66 @@ int count_args(t_lexor *lexor_list) i++; tmp = tmp->next; } + if (tmp) + { + if (tmp->token == PIPE) + tools->pipes++; + } return (i); } -int add_redirection(t_lexor **redirections, t_lexor **lexor_list, +int handle_heredoc(t_lexor **lexor_list, t_lexor **redirections) +{ + t_lexor *node; + + if ((*lexor_list)->prev->str) + { + node = ft_lexornew(ft_strdup((*lexor_list)->prev->str), GREAT_GREAT); + if (!node) + printf("EMERGENCY!!\n"); + ft_lexoradd_back(redirections, node); + ft_lexordelone(lexor_list, (*lexor_list)->prev->i); + } + node = ft_lexornew(ft_strdup((*lexor_list)->next->str), GREAT_GREAT); + if (!node) + printf("EMERGENCY!!\n"); + ft_lexoradd_back(redirections, node); + ft_lexordelone(lexor_list, (*lexor_list)->next->i); + ft_lexordelone(lexor_list, (*lexor_list)->i); + return (1); +} + +t_lexor *rm_redirections(t_lexor **lexor_list, int arg_size, int *num_redirections) { + t_lexor *redirections; t_lexor *node; - if (*lexor_list && ((*lexor_list)->token == LESS - || (*lexor_list)->token == GREAT)) + redirections = NULL; + while (arg_size > 0) { - node = ft_lexornew(ft_strdup((*lexor_list)->next->str), - (*lexor_list)->token); - if (!*redirections) - *redirections = node; - else - ft_lexoradd_back(redirections, node); - *lexor_list = (*lexor_list)->next; - num_redirections++; - return (1); + if (*lexor_list && (*lexor_list)->token == GREAT_GREAT) + { + handle_heredoc(lexor_list, &redirections); + num_redirections++; + } + else if (*lexor_list && ((*lexor_list)->token >= GREAT + && (*lexor_list)->token <= LESS_LESS)) + { + node = ft_lexornew(ft_strdup((*lexor_list)->next->str), + (*lexor_list)->token); + if (!node) + printf("EMERGENCY!!\n"); + ft_lexoradd_back(&redirections, node); + ft_lexordelone(lexor_list, (*lexor_list)->next->i); + ft_lexordelone(lexor_list, (*lexor_list)->i); + num_redirections++; + } + if (*lexor_list) + *lexor_list = (*lexor_list)->next; + arg_size--; } - return (0); + return (redirections); } t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) @@ -57,16 +96,13 @@ t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) i = 0; num_redirections = 0; - redirections = NULL; + redirections = rm_redirections(&lexor_list, arg_size, &num_redirections); str = malloc(sizeof(char **) * arg_size + 1); if (!str) return (NULL); while (arg_size > 0) { - if (add_redirection(&redirections, &lexor_list, &num_redirections)) - arg_size--; - else - str[i++] = ft_strdup(lexor_list->str); + str[i++] = ft_strdup(lexor_list->str); lexor_list = lexor_list->next; arg_size--; } @@ -79,58 +115,56 @@ t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) //free lexor_list //handle malloc errors -void parser(t_lexor *lexor_list) +void parser(t_lexor *lexor_list, t_tools *tools) { - t_simple_cmds *simple_cmds; t_simple_cmds *node; int arg_size; - simple_cmds = NULL; + tools->simple_cmds = NULL; while (lexor_list) { if (lexor_list->token == PIPE) lexor_list = lexor_list->next; - arg_size = count_args(lexor_list); + arg_size = count_args(lexor_list, tools); node = initialize_cmd(lexor_list, arg_size); - if (!simple_cmds) - simple_cmds = node; + if (!tools->simple_cmds) + tools->simple_cmds = node; else - ft_simple_cmdsadd_back(&simple_cmds, node); + ft_simple_cmdsadd_back(&tools->simple_cmds, node); while (arg_size--) lexor_list = lexor_list->next; } + print_parser(tools->simple_cmds); } -// >> means write over file -// int i = 0; -// while(simple_cmds) -// { -// printf("\n%i\n", i++); -// while (*simple_cmds->str) -// { -// printf("%s\n", *simple_cmds->str++); -// } -// if (simple_cmds->redirections) -// printf("\tredirections:\n"); -// while (simple_cmds->redirections) -// { -// printf("\n%i\n", i++); -// while (*simple_cmds->str) -// { -// printf("%s\n", *simple_cmds->str++); -// } -// if (simple_cmds->redirections) -// printf("\tredirections:\n"); -// while (simple_cmds->redirections) -// { -// printf("\t%s\t%d\n", simple_cmds->redirections->str, simple_cmds->redirections->token); -// simple_cmds->redirections = simple_cmds->redirections->next; -// } -// if (simple_cmds->builtin) -// printf("BUILTIN :)\n"); -// simple_cmds = simple_cmds->next; -// } -// if (simple_cmds->builtin) -// printf("BUILTIN :)\n"); -// simple_cmds = simple_cmds->next; -// } \ No newline at end of file +void print_parser(t_simple_cmds *simple_cmds) +{ + int i = 0; + + while (simple_cmds) + { + printf("\n>>>%i<<<\n", i++); + while (*simple_cmds->str) + { + printf("%s\n", *simple_cmds->str++); + } + if (simple_cmds->redirections) + printf("\nredirections:\n"); + while (simple_cmds->redirections) + { + printf("%s\t", simple_cmds->redirections->str); + if (simple_cmds->redirections->token == 3) + printf("GREAT\n"); + else if (simple_cmds->redirections->token == 4) + printf("GREAT_GREAT\n"); + else if (simple_cmds->redirections->token == 5) + printf("LESS\n"); + else if (simple_cmds->redirections->token == 6) + printf("LESS_LESS\n"); + simple_cmds->redirections = simple_cmds->redirections->next; + } + if (simple_cmds->builtin) + printf("BUILTIN :)\n"); + simple_cmds = simple_cmds->next; + } +} diff --git a/src/parse_envp.c b/src/utils/parse_envp.c similarity index 100% rename from src/parse_envp.c rename to src/utils/parse_envp.c diff --git a/src/utils/t_lexor_utils.c b/src/utils/t_lexor_utils.c index 7ecded8..84fdd68 100644 --- a/src/utils/t_lexor_utils.c +++ b/src/utils/t_lexor_utils.c @@ -14,58 +14,62 @@ t_lexor *ft_lexornew(char *str, int token) { - t_lexor *new_element; + t_lexor *new_element; + static int i = 0; new_element = (t_lexor *)malloc(sizeof(t_lexor)); if (!new_element) return (0); new_element->str = str; new_element->token = token; + new_element->i = i++; new_element->next = NULL; + new_element->prev = NULL; return (new_element); } void ft_lexoradd_back(t_lexor **lst, t_lexor *new) { t_lexor *tmp; + t_lexor *prev; tmp = *lst; - if (!(*lst)) + if (*lst == NULL) { *lst = new; return ; } while (tmp->next != NULL) + { + prev = tmp; tmp = tmp->next; + } tmp->next = new; + tmp->prev = prev; } -void ft_lexordelone(t_lexor **lst, int i) +void ft_lexordelone(t_lexor **lst, int key) { t_lexor *node; t_lexor *prev; + t_lexor *tmp; t_lexor *start; - int j; - j = 0; start = *lst; node = start; - if (j == i) + if ((*lst)->i == key) { *lst = node->next; free(node); return ; } - while (node && j < i) + while (node && node->i != key) { prev = node; node = node->next; - j++; } - if (node->next) - prev->next = node->next; - else - prev->next = NULL; + prev->next = node->next; + node->next->prev = prev; free(node); *lst = start; } @@ -85,17 +89,17 @@ void ft_lexorclear(t_lexor **lst) *lst = NULL; } -t_lexor *ft_lexorlast(t_lexor *map) +t_lexor *ft_lexorlast(t_lexor *lst) { int i; i = 0; - if (!map) + if (!lst) return (NULL); - while (map->next != NULL) + while (lst->next != NULL) { - map = map->next; + lst = lst->next; i++; } - return (map); + return (lst); } From 6e5f23ee3472e32b540e77e836e1d3c72c153ba9 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 2 Mar 2022 13:56:20 +0100 Subject: [PATCH 039/163] tester for lexer done --- includes/parser.h | 8 ++-- src/main.c | 11 ++--- src/parser/parser.c | 69 ++++++++++++++++--------------- test/Makefile | 7 +--- test/Testparser.c | 41 ++++++++++++++++++ test/Testtoken_reader.c | 92 +++++++++++++++++++++++++++++++++++++++-- 6 files changed, 173 insertions(+), 55 deletions(-) create mode 100644 test/Testparser.c diff --git a/includes/parser.h b/includes/parser.h index b826539..3a8bf51 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/02/28 09:41:55 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/02 11:10:27 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -54,8 +54,8 @@ typedef struct s_simple_cmds struct s_simple_cmds *next; } t_simple_cmds; -int parse_envp(t_tools *tools); -int find_pwd(t_tools *tools); -void parser(t_lexor *lexor_list); +int parse_envp(t_tools *tools); +int find_pwd(t_tools *tools); +t_simple_cmds *parser(t_lexor *lexor_list); #endif diff --git a/src/main.c b/src/main.c index 42d51e0..dbe550c 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/01 09:49:07 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/02 11:59:16 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -50,7 +50,7 @@ int main(int argc, char **argv, char **envp) add_history(line); tools.args = line; lexor_list = token_reader(&tools); - // parser(lexor_list); + parser(lexor_list); // while (lexor_list) // { // printf("str = %s \t token = %d\n", lexor_list->str, lexor_list->token); @@ -59,9 +59,4 @@ int main(int argc, char **argv, char **envp) free(line); } return (0); -} - -// int8_t AverageThreeBytes(int8_t a, int8_t b, int8_t c) -// { -// return (int8_t)(((int16_t)a + (int16_t)b + (int16_t)c) / 3); -// } \ No newline at end of file +} \ No newline at end of file diff --git a/src/parser/parser.c b/src/parser/parser.c index 9b1fecf..65f211c 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/01 09:55:26 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/02 12:10:13 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -77,7 +77,7 @@ t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) //free lexor_list //handle malloc errors -void parser(t_lexor *lexor_list) +t_simple_cmds *parser(t_lexor *lexor_list) { t_simple_cmds *simple_cmds; t_simple_cmds *node; @@ -97,38 +97,39 @@ void parser(t_lexor *lexor_list) while (arg_size--) lexor_list = lexor_list->next; } +int i = 0; +while(simple_cmds) +{ + printf("\n%i\n", i++); + while (*simple_cmds->str) + { + printf("%s\n", *simple_cmds->str++); + } + if (simple_cmds->redirections) + printf("\tredirections:\n"); + while (simple_cmds->redirections) + { + printf("\n%i\n", i++); + while (*simple_cmds->str) + { + printf("%s\n", *simple_cmds->str++); + } + if (simple_cmds->redirections) + printf("\tredirections:\n"); + while (simple_cmds->redirections) + { + printf("\t%s\t%d\n", simple_cmds->redirections->str, simple_cmds->redirections->token); + simple_cmds->redirections = simple_cmds->redirections->next; + } + if (simple_cmds->builtin) + printf("BUILTIN :)\n"); + simple_cmds = simple_cmds->next; + } + if (simple_cmds->builtin) + printf("BUILTIN :)\n"); + simple_cmds = simple_cmds->next; +} + return (simple_cmds); } // >> means write over file -// int i = 0; -// while(simple_cmds) -// { -// printf("\n%i\n", i++); -// while (*simple_cmds->str) -// { -// printf("%s\n", *simple_cmds->str++); -// } -// if (simple_cmds->redirections) -// printf("\tredirections:\n"); -// while (simple_cmds->redirections) -// { -// printf("\n%i\n", i++); -// while (*simple_cmds->str) -// { -// printf("%s\n", *simple_cmds->str++); -// } -// if (simple_cmds->redirections) -// printf("\tredirections:\n"); -// while (simple_cmds->redirections) -// { -// printf("\t%s\t%d\n", simple_cmds->redirections->str, simple_cmds->redirections->token); -// simple_cmds->redirections = simple_cmds->redirections->next; -// } -// if (simple_cmds->builtin) -// printf("BUILTIN :)\n"); -// simple_cmds = simple_cmds->next; -// } -// if (simple_cmds->builtin) -// printf("BUILTIN :)\n"); -// simple_cmds = simple_cmds->next; -// } diff --git a/test/Makefile b/test/Makefile index 5669180..4ee16c5 100644 --- a/test/Makefile +++ b/test/Makefile @@ -16,7 +16,6 @@ PATHSU = ../src/utils/ PATHSB = ../src/builtins/ PATHT = ../test/ PATHB = ../build/ -PATHD = ../build/depends/ PATHO = ../build/objs/ PATHR = ../build/results/ @@ -62,7 +61,7 @@ test: $(BUILD_PATHS) $(RESULTS) $(PATHR)%.txt: $(PATHO)%.$(TARGET_EXTENSION) -./$< > $@ 2>&1 -$(PATHO)Test%.$(TARGET_EXTENSION): $(LIBFT) $(OBJS) $(PATHO)Test%.o $(PATHO)%.o $(PATHU)unity.o #$(PATHD)Test%.d +$(PATHO)Test%.$(TARGET_EXTENSION): $(LIBFT) $(OBJS) $(PATHO)Test%.o $(PATHO)%.o $(PATHU)unity.o $(LINK) -lreadline -o $@ $^ $(PATHO)%.o:: $(PATHT)%.c $(HEADERS) @@ -89,9 +88,6 @@ $(PATHO)%.o:: $(PATHU)%.c $(PATHU)%.h $(PATHB): $(MKDIR) $(PATHB) -$(PATHD): - $(MKDIR) $(PATHD) - $(PATHO): $(MKDIR) $(PATHO) @@ -105,6 +101,7 @@ clean: $(CLEANUP) $(PATHO)*.o $(CLEANUP) $(PATHB)*.$(TARGET_EXTENSION) $(CLEANUP) $(PATHR)*.txt + rm -rdf $(PATHB) make fclean -C $(PATHLIBFT) re: clean test diff --git a/test/Testparser.c b/test/Testparser.c new file mode 100644 index 0000000..70d7528 --- /dev/null +++ b/test/Testparser.c @@ -0,0 +1,41 @@ +#include "unity.h" +#include "lexor.h" + +t_tools test_tools; +t_lexor *test_lexor; +t_simple_cmds *test_simple_cmds; + +void setUp(void) +{ + // set stuff up here +} + +void tearDown(void) +{ + // clean stuff up here +} + +void init_test(char *line) +{ + test_tools.args = line; + test_lexor = token_reader(&test_tools); + test_simple_cmds = parser(test_lexor); +} + +void assert_parser(int num_direction, char **expected, int (*builtin)(t_tools *)) +{ + while (*expected) + { + TEST_ASSERT_EQUAL_STRING(*expected++, test_simple_cmds->str++); + } + test_simple_cmds = test_simple_cmds->next; +} + +void test_parser_1() + +int main(void) +{ + UNITY_BEGIN(); + RUN_TEST(test_lexer_1); + return UNITY_END(); +} \ No newline at end of file diff --git a/test/Testtoken_reader.c b/test/Testtoken_reader.c index d118c35..b796bcc 100644 --- a/test/Testtoken_reader.c +++ b/test/Testtoken_reader.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/28 11:12:08 by fpolycar #+# #+# */ -/* Updated: 2022/03/01 17:16:25 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/02 10:53:47 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -37,13 +37,13 @@ void assert_token(int token, char *expected) test_lexor = test_lexor->next; } -void test_parser_1(void) +void test_lexer_1(void) { init_test(" test "); assert_token( 0, "test"); } -void test_parser_2(void) +void test_lexer_2(void) { init_test(" test | test "); assert_token(0, "test"); @@ -51,9 +51,93 @@ void test_parser_2(void) assert_token(0, "test"); } +void test_lexer_3(void) +{ + init_test(" test test test test || test test"); + assert_token(0, "test"); + assert_token(0, "test"); + assert_token(0, "test"); + assert_token(0, "test"); + assert_token(PIPE, NULL); + assert_token(PIPE, NULL); + assert_token(0, "test"); + assert_token(0, "test"); +} + +void test_lexer_4(void) +{ + init_test(" test \n\n test\n \ntest"); + assert_token(0, "test"); + assert_token(NEW_LINE, NULL); + assert_token(NEW_LINE, NULL); + assert_token(0, "test"); + assert_token(NEW_LINE, NULL); + assert_token(NEW_LINE, NULL); + assert_token(0, "test"); +} + +void test_lexer_5(void) +{ + init_test("test ' test | test' ' "); + assert_token(0, "test"); + assert_token(0, "' test | test'"); +} + +void test_lexer_6(void) +{ + init_test("test \" | test\""); + assert_token(0, "test"); + assert_token(0, "\" | test\""); +} + +void test_lexer_7(void) +{ + init_test("test < | test > | test << | test >> |"); + assert_token(0, "test"); + assert_token(LESS, NULL); + assert_token(PIPE, NULL); + assert_token(0, "test"); + assert_token(GREAT, NULL); + assert_token(PIPE, NULL); + assert_token(0, "test"); + assert_token(LESS_LESS, NULL); + assert_token(PIPE, NULL); + assert_token(0, "test"); + assert_token(GREAT_GREAT, NULL); + assert_token(PIPE, NULL); +} + +void test_lexer_8(void) +{ + init_test("test -nBa -n"); + assert_token(0, "test"); + assert_token(0, "-nBa"); + assert_token(0, "-n"); +} + +void test_lexer_9(void) +{ + init_test("test $BLA$BLA=10$BLA"); + assert_token(0, "test"); + assert_token(DOLLAR, NULL); + assert_token(0, "BLA"); + assert_token(DOLLAR, NULL); + assert_token(0, "BLA=10"); + assert_token(DOLLAR, NULL); + assert_token(0, "BLA"); +} + int main(void) { UNITY_BEGIN(); - RUN_TEST(test_parser_1); + RUN_TEST(test_lexer_1); + RUN_TEST(test_lexer_2); + RUN_TEST(test_lexer_3); + RUN_TEST(test_lexer_4); + RUN_TEST(test_lexer_5); + RUN_TEST(test_lexer_6); + RUN_TEST(test_lexer_7); + RUN_TEST(test_lexer_8); + RUN_TEST(test_lexer_9); return UNITY_END(); } From ecf8572ede15c9050fd4c7cf15a9e76569cabe77 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 2 Mar 2022 16:43:30 +0100 Subject: [PATCH 040/163] tester for parser nearly done just need to test it --- includes/parser.h | 4 +-- src/main.c | 6 +--- src/parser/parser.c | 5 +-- src/utils/t_lexor_utils.c | 2 +- test/Testparser.c | 65 ++++++++++++++++++++++++++++++++++++--- 5 files changed, 67 insertions(+), 15 deletions(-) diff --git a/includes/parser.h b/includes/parser.h index 15de93a..baeb9c8 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/03/02 13:57:48 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/02 14:27:29 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -59,6 +59,6 @@ typedef struct s_simple_cmds int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); -t_simple_cmds *parser(t_lexor *lexor_list); +t_simple_cmds *parser(t_lexor *lexor_list, t_tools *tools); #endif diff --git a/src/main.c b/src/main.c index 42bc0df..61f0222 100644 --- a/src/main.c +++ b/src/main.c @@ -6,11 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -<<<<<<< HEAD -/* Updated: 2022/03/02 11:59:16 by fpolycar ######## odam.nl */ -======= -/* Updated: 2022/03/01 16:35:49 by mgraaf ######## odam.nl */ ->>>>>>> maia +/* Updated: 2022/03/02 14:18:35 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/parser/parser.c b/src/parser/parser.c index 4d6c352..7a0431b 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/02 13:59:29 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/02 14:28:17 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -115,7 +115,7 @@ t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) //free lexor_list //handle malloc errors -t_simple_cmds *parser(t_lexor *lexor_list) +t_simple_cmds *parser(t_lexor *lexor_list, t_tools *tools) { t_simple_cmds *node; int arg_size; @@ -135,6 +135,7 @@ t_simple_cmds *parser(t_lexor *lexor_list) lexor_list = lexor_list->next; } print_parser(tools->simple_cmds); + return (node); } void print_parser(t_simple_cmds *simple_cmds) diff --git a/src/utils/t_lexor_utils.c b/src/utils/t_lexor_utils.c index 84fdd68..84d88e9 100644 --- a/src/utils/t_lexor_utils.c +++ b/src/utils/t_lexor_utils.c @@ -52,7 +52,7 @@ void ft_lexordelone(t_lexor **lst, int key) { t_lexor *node; t_lexor *prev; - t_lexor *tmp; + // t_lexor *tmp; t_lexor *start; start = *lst; diff --git a/test/Testparser.c b/test/Testparser.c index 70d7528..9acdded 100644 --- a/test/Testparser.c +++ b/test/Testparser.c @@ -1,5 +1,6 @@ #include "unity.h" #include "lexor.h" +#include t_tools test_tools; t_lexor *test_lexor; @@ -19,19 +20,73 @@ void init_test(char *line) { test_tools.args = line; test_lexor = token_reader(&test_tools); - test_simple_cmds = parser(test_lexor); + test_simple_cmds = parser(test_lexor, &test_tools); } -void assert_parser(int num_direction, char **expected, int (*builtin)(t_tools *)) +void assert_parser(char **expected, char *builtin, int num_directions, t_lexor *expected_redirection) { - while (*expected) + int i; + + i = 0; + while (expected[i]) { - TEST_ASSERT_EQUAL_STRING(*expected++, test_simple_cmds->str++); + TEST_ASSERT_EQUAL_STRING(expected[i], test_simple_cmds->str[i]); + i++; } + if (builtin) + TEST_ASSERT_NOT_NULL(test_simple_cmds->builtin); + else + TEST_ASSERT_NULL(test_simple_cmds->builtin); + TEST_ASSERT_EQUAL_INT(num_directions, test_simple_cmds->num_redirections); + TEST_ASSERT_EQUAL_STRING(expected_redirection->str, test_simple_cmds->redirections->str); + TEST_ASSERT_EQUAL_INT(expected_redirection->token, test_simple_cmds->redirections->token); + TEST_ASSERT_EQUAL_INT(expected_redirection->i, test_simple_cmds->redirections->i); test_simple_cmds = test_simple_cmds->next; } -void test_parser_1() +t_lexor *make_expected_redirection(char *str, int token, int i) +{ + t_lexor *redirection; + + redirection = malloc(sizeof(t_lexor)); + redirection->str = str; + redirection->token = token; + redirection->i = i; + return (redirection); +} + +char **make_array(char *str, ...) +{ + va_list arg; + char **arr; + char *test = "test"; + int i; + + i = 0; + va_start(arg, *str); + while (test) + { + test = va_arg(arg, char*); + *arr[i] = test; + i++; + } + va_end(arg); + return (arr); +} + +void test_lexer_1(void) +{ + init_test("test test"); + assert_parser(make_array("test", "test"), NULL, 0, make_expected_redirection(NULL, 0, 0)); +} + +void test_lexer_2(void) +{ + init_test("test test | test"); + + assert_parser(make_array("test", "test"), NULL, 0, make_expected_redirection(NULL, 0, 0)); + assert_parser(make_array("test"), NULL, 0, make_expected_redirection(NULL, 0, 0)); +} int main(void) { From 09c6c4ba180039bd36b103ee757a50ba6ce115f9 Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Thu, 3 Mar 2022 10:55:51 +0100 Subject: [PATCH 041/163] Still doesn't work --- Makefile | 4 ++-- src/parser/parser.c | 39 ++++++++++++++++++++++----------------- src/utils/t_lexor_utils.c | 6 +++--- 3 files changed, 27 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index e475287..b8fd80c 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ PATHP = src/pipex/ PATHEX = src/executor/ -BUILD_PATHS = $(PATHB) $(PATHD) $(PATHO) $(PATHR) $(PATHEX) +BUILD_PATHS = $(PATHB) $(PATHO) $(PATHR) $(PATHEX) src = $(wildcard $(PATHS)*.c) \ $(wildcard $(PATHSL)*.c) \ @@ -84,4 +84,4 @@ fclean: clean re: fclean all -# .PRECIOUS: $(PATHO)%.o \ No newline at end of file +.PRECIOUS: $(PATHO)%.o \ No newline at end of file diff --git a/src/parser/parser.c b/src/parser/parser.c index a1b430a..599e352 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/01 18:39:17 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/02 15:49:12 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -33,7 +33,8 @@ int count_args(t_lexor *lexor_list, t_tools *tools) return (i); } -int handle_heredoc(t_lexor **lexor_list, t_lexor **redirections) +int handle_heredoc(t_lexor **lexor_list, t_lexor **redirections, + int *arg_size, int *num_redirections) { t_lexor *node; @@ -44,6 +45,7 @@ int handle_heredoc(t_lexor **lexor_list, t_lexor **redirections) printf("EMERGENCY!!\n"); ft_lexoradd_back(redirections, node); ft_lexordelone(lexor_list, (*lexor_list)->prev->i); + arg_size--; } node = ft_lexornew(ft_strdup((*lexor_list)->next->str), GREAT_GREAT); if (!node) @@ -51,10 +53,12 @@ int handle_heredoc(t_lexor **lexor_list, t_lexor **redirections) ft_lexoradd_back(redirections, node); ft_lexordelone(lexor_list, (*lexor_list)->next->i); ft_lexordelone(lexor_list, (*lexor_list)->i); + arg_size -= 2; + num_redirections++; return (1); } -t_lexor *rm_redirections(t_lexor **lexor_list, int arg_size, +t_lexor *find_redirections(t_lexor *lexor_list, int *arg_size, int *num_redirections) { t_lexor *redirections; @@ -63,25 +67,24 @@ t_lexor *rm_redirections(t_lexor **lexor_list, int arg_size, redirections = NULL; while (arg_size > 0) { - if (*lexor_list && (*lexor_list)->token == GREAT_GREAT) + if (lexor_list && lexor_list->token == GREAT_GREAT) + handle_heredoc(&lexor_list, &redirections, + arg_size, num_redirections); + else if (lexor_list && (lexor_list->token >= GREAT + && lexor_list->token <= LESS_LESS)) { - handle_heredoc(lexor_list, &redirections); - num_redirections++; - } - else if (*lexor_list && ((*lexor_list)->token >= GREAT - && (*lexor_list)->token <= LESS_LESS)) - { - node = ft_lexornew(ft_strdup((*lexor_list)->next->str), - (*lexor_list)->token); + node = ft_lexornew(ft_strdup(lexor_list->next->str), + lexor_list->token); if (!node) printf("EMERGENCY!!\n"); ft_lexoradd_back(&redirections, node); - ft_lexordelone(lexor_list, (*lexor_list)->next->i); - ft_lexordelone(lexor_list, (*lexor_list)->i); + ft_lexordelone(&lexor_list, lexor_list->i); + ft_lexordelone(&lexor_list, lexor_list->i); + arg_size -= 2; num_redirections++; } - if (*lexor_list) - *lexor_list = (*lexor_list)->next; + if (lexor_list) + lexor_list = lexor_list->next; arg_size--; } return (redirections); @@ -96,7 +99,9 @@ t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) i = 0; num_redirections = 0; - redirections = rm_redirections(&lexor_list, arg_size, &num_redirections); + redirections = find_redirections(lexor_list, &arg_size, &num_redirections); + printf("HELLO\n"); + printf("%d\n", arg_size); str = malloc(sizeof(char **) * arg_size + 1); if (!str) return (NULL); diff --git a/src/utils/t_lexor_utils.c b/src/utils/t_lexor_utils.c index 84fdd68..54b5626 100644 --- a/src/utils/t_lexor_utils.c +++ b/src/utils/t_lexor_utils.c @@ -22,7 +22,7 @@ t_lexor *ft_lexornew(char *str, int token) return (0); new_element->str = str; new_element->token = token; - new_element->i = i++; + new_element->i = i++; new_element->next = NULL; new_element->prev = NULL; return (new_element); @@ -52,7 +52,6 @@ void ft_lexordelone(t_lexor **lst, int key) { t_lexor *node; t_lexor *prev; - t_lexor *tmp; t_lexor *start; start = *lst; @@ -69,7 +68,8 @@ void ft_lexordelone(t_lexor **lst, int key) node = node->next; } prev->next = node->next; - node->next->prev = prev; + if (prev->next) + prev->next->prev = prev; free(node); *lst = start; } From 7e9feeec9d312cc8ca6eb4162a1172ae3298878b Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Thu, 3 Mar 2022 12:05:29 +0100 Subject: [PATCH 042/163] lexer $ fixed --- test/Testparser.c | 7 ++++--- test/Testtoken_reader.c | 11 ++++------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/test/Testparser.c b/test/Testparser.c index 9acdded..cfc7e9f 100644 --- a/test/Testparser.c +++ b/test/Testparser.c @@ -58,16 +58,16 @@ t_lexor *make_expected_redirection(char *str, int token, int i) char **make_array(char *str, ...) { va_list arg; - char **arr; + char **arr = NULL; char *test = "test"; int i; i = 0; - va_start(arg, *str); + va_start(arg, str); while (test) { test = va_arg(arg, char*); - *arr[i] = test; + arr[i] = test; i++; } va_end(arg); @@ -85,6 +85,7 @@ void test_lexer_2(void) init_test("test test | test"); assert_parser(make_array("test", "test"), NULL, 0, make_expected_redirection(NULL, 0, 0)); + assert_parser(NULL, NULL, 0, make_expected_redirection(NULL, 0, 0)); assert_parser(make_array("test"), NULL, 0, make_expected_redirection(NULL, 0, 0)); } diff --git a/test/Testtoken_reader.c b/test/Testtoken_reader.c index b796bcc..03399f2 100644 --- a/test/Testtoken_reader.c +++ b/test/Testtoken_reader.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/28 11:12:08 by fpolycar #+# #+# */ -/* Updated: 2022/03/02 10:53:47 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/03 11:03:25 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -119,12 +119,9 @@ void test_lexer_9(void) { init_test("test $BLA$BLA=10$BLA"); assert_token(0, "test"); - assert_token(DOLLAR, NULL); - assert_token(0, "BLA"); - assert_token(DOLLAR, NULL); - assert_token(0, "BLA=10"); - assert_token(DOLLAR, NULL); - assert_token(0, "BLA"); + assert_token(DOLLAR, "BLA"); + assert_token(DOLLAR, "BLA=10"); + assert_token(DOLLAR, "BLA"); } int main(void) From 5255b1e97b587244a6e9a6814f2c0f9c22b361c0 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Thu, 3 Mar 2022 12:06:54 +0100 Subject: [PATCH 043/163] lexer $ fixed --- .vscode/launch.json | 30 ------------------------------ .vscode/settings.json | 8 -------- .vscode/tasks.json | 12 ------------ file1 | 0 file2 | 1 - src/builtins/builtins.c | 2 +- src/lexor/handle_token.c | 17 ++++++++++++++--- src/main.c | 2 +- src/parser/parser.c | 13 ++++++++----- 9 files changed, 24 insertions(+), 61 deletions(-) delete mode 100644 .vscode/launch.json delete mode 100644 .vscode/settings.json delete mode 100644 .vscode/tasks.json delete mode 100644 file1 delete mode 100644 file2 diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 65242c9..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,30 +0,0 @@ -{ - // 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": [ - - { - "name": "(lldb) Launch", - "type": "lldb", - "request": "launch", - "program": "${workspaceFolder}/minishell", - "args": [], - "stopAtEntry": true, - "cwd": "${workspaceFolder}", - "environment": [], - "externalConsole": true, - "MIMode": "lldb", - "setupCommands": [ - { - "description": "Enable pretty-printing for lldb", - "text": "-enable-pretty-printing", - "ignoreFailures": true - } - ], - "preLaunchTask": "build", - "disableOptimisticBPs": true - } - ] -} diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index f023251..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "files.autoSave": "onFocusChange", - "files.associations": { - "stdio.h": "c", - "readline.h": "c", - "libft.h": "c" - } -} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json deleted file mode 100644 index 5f75ac2..0000000 --- a/.vscode/tasks.json +++ /dev/null @@ -1,12 +0,0 @@ -{ - "version": "2.0.0", - "tasks": [ - { - "label": "build", - "type": "shell", - "command": "make; export SHLVL=1", - "group": "build", - "problemMatcher": "$gcc" - }, - ] -} \ No newline at end of file diff --git a/file1 b/file1 deleted file mode 100644 index e69de29..0000000 diff --git a/file2 b/file2 deleted file mode 100644 index af01f32..0000000 --- a/file2 +++ /dev/null @@ -1 +0,0 @@ -skd akljf ksd diff --git a/src/builtins/builtins.c b/src/builtins/builtins.c index dab11cd..a0cba3d 100644 --- a/src/builtins/builtins.c +++ b/src/builtins/builtins.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 11:42:32 by mgraaf #+# #+# */ -/* Updated: 2022/03/01 14:07:44 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/03 10:29:38 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/lexor/handle_token.c b/src/lexor/handle_token.c index f9953c1..c49f234 100644 --- a/src/lexor/handle_token.c +++ b/src/lexor/handle_token.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/18 10:27:43 by mgraaf #+# #+# */ -/* Updated: 2022/02/28 10:27:41 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/03 12:02:27 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -37,15 +37,26 @@ t_tokens check_token(int c) int handle_token(char *str, int i, t_lexor **lexor_list) { t_tokens token; + int j; + j = 0; + while (str[i + j + 1] != ' ' && str[i + j + 1] && !(check_token(str[i + j + 1])) + && !(str[i + j + 1] == 34 && str[i + j + 1] == 39)) + j++; token = check_token(str[i]); - if (token == GREAT && check_token(str[i + 1]) == GREAT) + if (token == DOLLAR) + { + if (!add_node(ft_substr(str, i + 1, j), DOLLAR, lexor_list)) + printf("EMERGENCY!\n"); + return (j + 1); + } + else if (token == GREAT && check_token(str[i + 1]) == GREAT) { if (!add_node(NULL, GREAT_GREAT, lexor_list)) printf("EMERGENCY!\n"); return (2); } - if (token == LESS && check_token(str[i + 1]) == LESS) + else if (token == LESS && check_token(str[i + 1]) == LESS) { if (!add_node(NULL, LESS_LESS, lexor_list)) printf("EMERGENCY!\n"); diff --git a/src/main.c b/src/main.c index 61f0222..6df1652 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/02 14:18:35 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/03 09:35:28 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/parser/parser.c b/src/parser/parser.c index 7a0431b..8ca61c5 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/02 14:28:17 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/03 11:05:24 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -59,7 +59,9 @@ t_lexor *rm_redirections(t_lexor **lexor_list, int arg_size, { t_lexor *redirections; t_lexor *node; + // t_lexor *tmp; + // tmp = lexor_list; redirections = NULL; while (arg_size > 0) { @@ -91,12 +93,12 @@ t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) { char **str; int i; - t_lexor *redirections; + t_lexor *redirections = NULL; int num_redirections; i = 0; num_redirections = 0; - redirections = rm_redirections(&lexor_list, arg_size, &num_redirections); + // redirections = rm_redirections(&lexor_list, arg_size, &num_redirections); str = malloc(sizeof(char **) * arg_size + 1); if (!str) return (NULL); @@ -120,11 +122,12 @@ t_simple_cmds *parser(t_lexor *lexor_list, t_tools *tools) t_simple_cmds *node; int arg_size; + tools->simple_cmds = NULL; while (lexor_list) { - if (lexor_list->token == PIPE) - lexor_list = lexor_list->next; + // if (lexor_list->token == PIPE) + // lexor_list = lexor_list->next; arg_size = count_args(lexor_list, tools); node = initialize_cmd(lexor_list, arg_size); if (!tools->simple_cmds) From da40a134d13fd4e9d9204d416b74eab509388660 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Thu, 3 Mar 2022 16:17:25 +0100 Subject: [PATCH 044/163] parser not fcking working --- src/main.c | 2 +- src/parser/parser.c | 123 +++++++++++++++++++++++--------------------- test/Makefile | 2 +- 3 files changed, 65 insertions(+), 62 deletions(-) diff --git a/src/main.c b/src/main.c index 6df1652..624d7b9 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/03 09:35:28 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/03 13:14:51 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/parser/parser.c b/src/parser/parser.c index 3b3d3c0..568dc7e 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/03 12:17:12 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/03 15:48:37 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -33,63 +33,68 @@ int count_args(t_lexor *lexor_list, t_tools *tools) return (i); } -int handle_heredoc(t_lexor **lexor_list, t_lexor **redirections, - int *arg_size, int *num_redirections) -{ - t_lexor *node; +// int handle_heredoc(t_lexor **lexor_list, t_lexor **redirections, +// int *arg_size, int *num_redirections) +// { +// t_lexor *node; - if ((*lexor_list)->prev->str) - { - node = ft_lexornew(ft_strdup((*lexor_list)->prev->str), GREAT_GREAT); - if (!node) - printf("EMERGENCY!!\n"); - ft_lexoradd_back(redirections, node); - ft_lexordelone(lexor_list, (*lexor_list)->prev->i); - arg_size--; - } - node = ft_lexornew(ft_strdup((*lexor_list)->next->str), GREAT_GREAT); - if (!node) - printf("EMERGENCY!!\n"); - ft_lexoradd_back(redirections, node); - ft_lexordelone(lexor_list, (*lexor_list)->next->i); - ft_lexordelone(lexor_list, (*lexor_list)->i); - arg_size -= 2; - num_redirections++; - return (1); -} +// if ((*lexor_list)->prev->str) +// { +// node = ft_lexornew(ft_strdup((*lexor_list)->prev->str), LESS_LESS); +// if (!node) +// printf("EMERGENCY!!\n"); +// ft_lexoradd_back(redirections, node); +// // ft_lexordelone(lexor_list, (*lexor_list)->prev->i); +// arg_size--; +// } +// node = ft_lexornew(ft_strdup((*lexor_list)->next->str), LESS_LESS); +// if (!node) +// printf("EMERGENCY!!\n"); +// ft_lexoradd_back(redirections, node); +// ft_lexordelone(lexor_list, (*lexor_list)->next->i); +// ft_lexordelone(lexor_list, (*lexor_list)->i); +// arg_size -= 2; +// num_redirections++; +// return (1); +// } -t_lexor *find_redirections(t_lexor *lexor_list, int *arg_size, - int *num_redirections) -{ - t_lexor *redirections; - t_lexor *node; - // t_lexor *tmp; +// t_lexor *find_redirections(t_lexor *lexor_list, int *arg_size, +// int *num_redirections) +// { +// t_lexor *redirections; +// // t_lexor *node = NULL; - // tmp = lexor_list; - redirections = NULL; - while (arg_size > 0) - { - if (lexor_list && lexor_list->token == GREAT_GREAT) - handle_heredoc(&lexor_list, &redirections, - arg_size, num_redirections); - else if (lexor_list && (lexor_list->token >= GREAT - && lexor_list->token <= LESS_LESS)) - { - node = ft_lexornew(ft_strdup(lexor_list->next->str), - lexor_list->token); - if (!node) - printf("EMERGENCY!!\n"); - ft_lexoradd_back(&redirections, node); - ft_lexordelone(&lexor_list, lexor_list->i); - ft_lexordelone(&lexor_list, lexor_list->i); - arg_size -= 2; - num_redirections++; - } - if (lexor_list) - lexor_list = lexor_list->next; - arg_size--; - } - return (redirections); +// redirections = NULL; +// // while (*arg_size > 0) +// // { +// // // printf("%d\n", *arg_size); +// // // if (lexor_list && lexor_list->token == LESS_LESS) // if it is LESS_LESS -> heredoc +// printf("%d\n", *arg_size); +// // // handle_heredoc(&lexor_list, &redirections, +// // // arg_size, num_redirections); +// // // else if (lexor_list && (lexor_list->token >= GREAT +// // // && lexor_list->token <= LESS_LESS)) +// // // { +// // // node = ft_lexornew(ft_strdup(lexor_list->next->str), +// // // lexor_list->token); +// // // if (!node) +// // // printf("EMERGENCY!!\n"); +// // // ft_lexoradd_back(&redirections, node); +// // // ft_lexordelone(&lexor_list, lexor_list->i); +// // // ft_lexordelone(&lexor_list, lexor_list->i); +// // // arg_size -= 2; +// num_redirections++; +// // // } +// if (lexor_list) +// lexor_list = lexor_list->next; +// // arg_size--; +// // } +// return (redirections); +// } + +t_lexor find_redirections() +{ + } t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) @@ -101,9 +106,7 @@ t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) i = 0; num_redirections = 0; - redirections = find_redirections(lexor_list, &arg_size, &num_redirections); - printf("HELLO\n"); - printf("%d\n", arg_size); + redirections = find_redirections(lexor_list, &arg_size, &num_redirections); //current lexor, arg size, 0 str = malloc(sizeof(char **) * arg_size + 1); if (!str) return (NULL); @@ -131,8 +134,8 @@ t_simple_cmds *parser(t_lexor *lexor_list, t_tools *tools) tools->simple_cmds = NULL; while (lexor_list) { - // if (lexor_list->token == PIPE) - // lexor_list = lexor_list->next; + if (lexor_list->token == PIPE) + lexor_list = lexor_list->next; arg_size = count_args(lexor_list, tools); node = initialize_cmd(lexor_list, arg_size); if (!tools->simple_cmds) diff --git a/test/Makefile b/test/Makefile index 4ee16c5..06b2350 100644 --- a/test/Makefile +++ b/test/Makefile @@ -36,7 +36,7 @@ COMPILE=gcc -c LINK=gcc -CFLAGS=-Wall -Werror -Wextra -I. -I$(PATHU) -I$(PATHLIBFT) -I$(PATHS) -I$(PATHP) -I$(PATHI) +CFLAGS= -I. -I$(PATHU) -I$(PATHLIBFT) -I$(PATHS) -I$(PATHP) -I$(PATHI) RESULTS = $(patsubst $(PATHT)Test%.c,$(PATHR)Test%.txt, $(SRCT)) From 236ef6f68154b989d9afac8428adabf53ce59f17 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Fri, 4 Mar 2022 10:04:47 +0100 Subject: [PATCH 045/163] added a parser struct but parser still not working --- .vscode/launch.json | 16 +++++ includes/parser.h | 10 ++- src/parser/parser.c | 157 ++++++++++++++++++++++---------------------- 3 files changed, 102 insertions(+), 81 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..1cb55e7 --- /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}/", + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/includes/parser.h b/includes/parser.h index baeb9c8..842c7db 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/03/02 14:27:29 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/04 09:36:47 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,6 +34,14 @@ typedef struct s_lexor struct s_lexor *prev; } t_lexor; +typedef struct s_parser_tools +{ + t_lexor *lexor_list; + t_lexor *redirections; + int arg_size; + int num_redirections; +} t_parser_tools; + typedef struct s_tools { char *args; diff --git a/src/parser/parser.c b/src/parser/parser.c index 568dc7e..6d5be39 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,11 +6,12 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/03 15:48:37 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/04 10:04:11 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" + void print_parser(t_simple_cmds *simple_cmds); int count_args(t_lexor *lexor_list, t_tools *tools) @@ -33,93 +34,89 @@ int count_args(t_lexor *lexor_list, t_tools *tools) return (i); } -// int handle_heredoc(t_lexor **lexor_list, t_lexor **redirections, -// int *arg_size, int *num_redirections) -// { -// t_lexor *node; - -// if ((*lexor_list)->prev->str) -// { -// node = ft_lexornew(ft_strdup((*lexor_list)->prev->str), LESS_LESS); -// if (!node) -// printf("EMERGENCY!!\n"); -// ft_lexoradd_back(redirections, node); -// // ft_lexordelone(lexor_list, (*lexor_list)->prev->i); -// arg_size--; -// } -// node = ft_lexornew(ft_strdup((*lexor_list)->next->str), LESS_LESS); -// if (!node) -// printf("EMERGENCY!!\n"); -// ft_lexoradd_back(redirections, node); -// ft_lexordelone(lexor_list, (*lexor_list)->next->i); -// ft_lexordelone(lexor_list, (*lexor_list)->i); -// arg_size -= 2; -// num_redirections++; -// return (1); -// } - -// t_lexor *find_redirections(t_lexor *lexor_list, int *arg_size, -// int *num_redirections) -// { -// t_lexor *redirections; -// // t_lexor *node = NULL; - -// redirections = NULL; -// // while (*arg_size > 0) -// // { -// // // printf("%d\n", *arg_size); -// // // if (lexor_list && lexor_list->token == LESS_LESS) // if it is LESS_LESS -> heredoc -// printf("%d\n", *arg_size); -// // // handle_heredoc(&lexor_list, &redirections, -// // // arg_size, num_redirections); -// // // else if (lexor_list && (lexor_list->token >= GREAT -// // // && lexor_list->token <= LESS_LESS)) -// // // { -// // // node = ft_lexornew(ft_strdup(lexor_list->next->str), -// // // lexor_list->token); -// // // if (!node) -// // // printf("EMERGENCY!!\n"); -// // // ft_lexoradd_back(&redirections, node); -// // // ft_lexordelone(&lexor_list, lexor_list->i); -// // // ft_lexordelone(&lexor_list, lexor_list->i); -// // // arg_size -= 2; -// num_redirections++; -// // // } -// if (lexor_list) -// lexor_list = lexor_list->next; -// // arg_size--; -// // } -// return (redirections); -// } - -t_lexor find_redirections() +int handle_heredoc(t_parser_tools *parser_tools) { - + t_lexor *node; + + if (parser_tools->lexor_list->prev->str) + { + node = ft_lexornew(ft_strdup(parser_tools->lexor_list->prev->str), LESS_LESS); + if (!node) + printf("EMERGENCY!!\n"); + ft_lexoradd_back(&parser_tools->redirections, node); + ft_lexordelone(&parser_tools->lexor_list, parser_tools->lexor_list->prev->i); + parser_tools->arg_size--; + } + node = ft_lexornew(ft_strdup(parser_tools->lexor_list->next->str), LESS_LESS); + if (!node) + printf("EMERGENCY!!\n"); + ft_lexoradd_back(&parser_tools->redirections, node); + ft_lexordelone(&parser_tools->lexor_list, parser_tools->lexor_list->i); + ft_lexordelone(&parser_tools->lexor_list, parser_tools->lexor_list->i); + parser_tools->arg_size -= 2; + parser_tools->num_redirections++; + return (1); } -t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) +void find_redirections(t_parser_tools *parser_tools) +{ + t_lexor *node; + t_lexor *tmp; + + tmp = parser_tools->lexor_list; + while (parser_tools->arg_size > 0) + { + printf("%d\n", parser_tools->arg_size); + if (tmp && tmp->token == LESS_LESS) + handle_heredoc(parser_tools); + else if (tmp && (tmp->token >= GREAT + && tmp->token <= LESS_LESS)) + { + node = ft_lexornew(ft_strdup(tmp->next->str), tmp->token); + if (!node) + printf("EMERGENCY!!\n"); + ft_lexoradd_back(&parser_tools->redirections, node); + ft_lexordelone(&parser_tools->lexor_list, tmp->i); + ft_lexordelone(&parser_tools->lexor_list, tmp->i); + parser_tools->arg_size -= 2; + parser_tools->num_redirections++; + } + if (parser_tools->lexor_list) + tmp = tmp->next; + parser_tools->arg_size--; + } +} + +t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) { char **str; int i; - t_lexor *redirections = NULL; - int num_redirections; i = 0; - num_redirections = 0; - redirections = find_redirections(lexor_list, &arg_size, &num_redirections); //current lexor, arg size, 0 - str = malloc(sizeof(char **) * arg_size + 1); + find_redirections(parser_tools); + str = malloc(sizeof(char **) * parser_tools->arg_size + 1); if (!str) return (NULL); - while (arg_size > 0) + while (parser_tools->arg_size > 0) { - str[i++] = ft_strdup(lexor_list->str); - lexor_list = lexor_list->next; - arg_size--; + str[i++] = ft_strdup(parser_tools->lexor_list->str); + parser_tools->lexor_list = parser_tools->lexor_list->next; + parser_tools->arg_size--; } str[i] = NULL; - i = 0; return (ft_simple_cmdsnew(str, builtin_arr(str[0]), - num_redirections, redirections)); + parser_tools->num_redirections, parser_tools->redirections)); +} + +t_parser_tools init_parser_tools(t_lexor *lexor_list, int arg_size) +{ + t_parser_tools parser_tools; + + parser_tools.lexor_list = lexor_list; + parser_tools.redirections = NULL; + parser_tools.arg_size = arg_size; + parser_tools.num_redirections = 0; + return (parser_tools); } //free lexor_list @@ -128,21 +125,21 @@ t_simple_cmds *initialize_cmd(t_lexor *lexor_list, int arg_size) t_simple_cmds *parser(t_lexor *lexor_list, t_tools *tools) { t_simple_cmds *node; - int arg_size; - + t_parser_tools parser_tools; tools->simple_cmds = NULL; while (lexor_list) { if (lexor_list->token == PIPE) lexor_list = lexor_list->next; - arg_size = count_args(lexor_list, tools); - node = initialize_cmd(lexor_list, arg_size); + parser_tools = init_parser_tools(lexor_list, + count_args(lexor_list, tools)); + node = initialize_cmd(&parser_tools); if (!tools->simple_cmds) tools->simple_cmds = node; else ft_simple_cmdsadd_back(&tools->simple_cmds, node); - while (arg_size--) + while (parser_tools.arg_size--) lexor_list = lexor_list->next; } print_parser(tools->simple_cmds); @@ -151,7 +148,7 @@ t_simple_cmds *parser(t_lexor *lexor_list, t_tools *tools) void print_parser(t_simple_cmds *simple_cmds) { - int i = 0; + int i = 0; while (simple_cmds) { From 55d9381baefae9594ea1161abac37ca07ab82832 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Fri, 4 Mar 2022 10:06:14 +0100 Subject: [PATCH 046/163] .vscode --- .vscode/launch.json | 44 ++++++++++++++++++++++++++++--------------- .vscode/settings.json | 8 ++++++++ .vscode/tasks.json | 12 ++++++++++++ 3 files changed, 49 insertions(+), 15 deletions(-) create mode 100644 .vscode/settings.json create mode 100644 .vscode/tasks.json diff --git a/.vscode/launch.json b/.vscode/launch.json index 1cb55e7..65242c9 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -1,16 +1,30 @@ { - // 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}/", - "args": [], - "cwd": "${workspaceFolder}" - } - ] -} \ No newline at end of file + // 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": [ + + { + "name": "(lldb) Launch", + "type": "lldb", + "request": "launch", + "program": "${workspaceFolder}/minishell", + "args": [], + "stopAtEntry": true, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": true, + "MIMode": "lldb", + "setupCommands": [ + { + "description": "Enable pretty-printing for lldb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ], + "preLaunchTask": "build", + "disableOptimisticBPs": true + } + ] +} diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f023251 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "files.autoSave": "onFocusChange", + "files.associations": { + "stdio.h": "c", + "readline.h": "c", + "libft.h": "c" + } +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..5f75ac2 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,12 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "type": "shell", + "command": "make; export SHLVL=1", + "group": "build", + "problemMatcher": "$gcc" + }, + ] +} \ No newline at end of file From 0d795ab18fd35496e0d6033cee8c8c740a270556 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Fri, 4 Mar 2022 10:26:37 +0100 Subject: [PATCH 047/163] PARSER WORKS {but not erreting} --- asdf | 0 src/parser/parser.c | 20 ++++++++++++++------ src/utils/t_lexor_utils.c | 5 ++++- 3 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 asdf diff --git a/asdf b/asdf new file mode 100644 index 0000000..e69de29 diff --git a/src/parser/parser.c b/src/parser/parser.c index 6d5be39..fe94524 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/04 10:04:11 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/04 10:25:37 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -53,7 +53,7 @@ int handle_heredoc(t_parser_tools *parser_tools) ft_lexoradd_back(&parser_tools->redirections, node); ft_lexordelone(&parser_tools->lexor_list, parser_tools->lexor_list->i); ft_lexordelone(&parser_tools->lexor_list, parser_tools->lexor_list->i); - parser_tools->arg_size -= 2; + parser_tools->arg_size--; parser_tools->num_redirections++; return (1); } @@ -70,15 +70,15 @@ void find_redirections(t_parser_tools *parser_tools) if (tmp && tmp->token == LESS_LESS) handle_heredoc(parser_tools); else if (tmp && (tmp->token >= GREAT - && tmp->token <= LESS_LESS)) + && tmp->token <= LESS)) { node = ft_lexornew(ft_strdup(tmp->next->str), tmp->token); if (!node) printf("EMERGENCY!!\n"); ft_lexoradd_back(&parser_tools->redirections, node); ft_lexordelone(&parser_tools->lexor_list, tmp->i); - ft_lexordelone(&parser_tools->lexor_list, tmp->i); - parser_tools->arg_size -= 2; + ft_lexordelone(&parser_tools->lexor_list, tmp->next->i); + parser_tools->arg_size--; parser_tools->num_redirections++; } if (parser_tools->lexor_list) @@ -95,6 +95,13 @@ t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) i = 0; find_redirections(parser_tools); str = malloc(sizeof(char **) * parser_tools->arg_size + 1); + t_lexor *tmp; + tmp = parser_tools->lexor_list; + while (tmp && tmp->token != PIPE) + { + parser_tools->arg_size++; + tmp = tmp->next; + } if (!str) return (NULL); while (parser_tools->arg_size > 0) @@ -139,7 +146,8 @@ t_simple_cmds *parser(t_lexor *lexor_list, t_tools *tools) tools->simple_cmds = node; else ft_simple_cmdsadd_back(&tools->simple_cmds, node); - while (parser_tools.arg_size--) + lexor_list = parser_tools.lexor_list; + if (lexor_list && lexor_list->token == PIPE) lexor_list = lexor_list->next; } print_parser(tools->simple_cmds); diff --git a/src/utils/t_lexor_utils.c b/src/utils/t_lexor_utils.c index 54b5626..c2a7321 100644 --- a/src/utils/t_lexor_utils.c +++ b/src/utils/t_lexor_utils.c @@ -67,7 +67,10 @@ void ft_lexordelone(t_lexor **lst, int key) prev = node; node = node->next; } - prev->next = node->next; + if (node) + prev->next = node->next; + else + prev->next = NULL; if (prev->next) prev->next->prev = prev; free(node); From 5bf88f5e84808a4fbebe5ca563920a04958ee62f Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Fri, 4 Mar 2022 11:09:51 +0100 Subject: [PATCH 048/163] kinda fix heredoc --- src/parser/parser.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/parser/parser.c b/src/parser/parser.c index fe94524..21eb6f3 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/04 10:25:37 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/04 10:57:36 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,10 +34,11 @@ int count_args(t_lexor *lexor_list, t_tools *tools) return (i); } -int handle_heredoc(t_parser_tools *parser_tools) +int handle_heredoc(t_parser_tools *parser_tools, t_lexor *tmp) { t_lexor *node; + // printf("%s\n", parser_tools->lexor_list->prev->str); if (parser_tools->lexor_list->prev->str) { node = ft_lexornew(ft_strdup(parser_tools->lexor_list->prev->str), LESS_LESS); @@ -47,12 +48,12 @@ int handle_heredoc(t_parser_tools *parser_tools) ft_lexordelone(&parser_tools->lexor_list, parser_tools->lexor_list->prev->i); parser_tools->arg_size--; } - node = ft_lexornew(ft_strdup(parser_tools->lexor_list->next->str), LESS_LESS); + node = ft_lexornew(ft_strdup(tmp->next->str), tmp->token); if (!node) printf("EMERGENCY!!\n"); ft_lexoradd_back(&parser_tools->redirections, node); - ft_lexordelone(&parser_tools->lexor_list, parser_tools->lexor_list->i); - ft_lexordelone(&parser_tools->lexor_list, parser_tools->lexor_list->i); + ft_lexordelone(&parser_tools->lexor_list, tmp->i); + ft_lexordelone(&parser_tools->lexor_list, tmp->next->i); parser_tools->arg_size--; parser_tools->num_redirections++; return (1); @@ -68,7 +69,7 @@ void find_redirections(t_parser_tools *parser_tools) { printf("%d\n", parser_tools->arg_size); if (tmp && tmp->token == LESS_LESS) - handle_heredoc(parser_tools); + handle_heredoc(parser_tools, tmp); else if (tmp && (tmp->token >= GREAT && tmp->token <= LESS)) { From 7f4c57d8ed3d3137dd1126beb5c9dfa53d467c89 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Fri, 4 Mar 2022 11:29:01 +0100 Subject: [PATCH 049/163] heredoc first word --- src/parser/parser.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/parser/parser.c b/src/parser/parser.c index 21eb6f3..b054d38 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/04 10:57:36 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/04 11:28:30 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -38,14 +38,14 @@ int handle_heredoc(t_parser_tools *parser_tools, t_lexor *tmp) { t_lexor *node; - // printf("%s\n", parser_tools->lexor_list->prev->str); - if (parser_tools->lexor_list->prev->str) + printf("%s %d\n", tmp->prev->str, tmp->token); + if (tmp->prev->str && tmp->token == LESS_LESS) { - node = ft_lexornew(ft_strdup(parser_tools->lexor_list->prev->str), LESS_LESS); + node = ft_lexornew(ft_strdup(tmp->prev->str), LESS_LESS); if (!node) printf("EMERGENCY!!\n"); ft_lexoradd_back(&parser_tools->redirections, node); - ft_lexordelone(&parser_tools->lexor_list, parser_tools->lexor_list->prev->i); + ft_lexordelone(&parser_tools->lexor_list, tmp->prev->i); parser_tools->arg_size--; } node = ft_lexornew(ft_strdup(tmp->next->str), tmp->token); From 5a29be4089ddb8d60fd5750cd44fe2bb6c6f9cdd Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Mon, 7 Mar 2022 14:24:48 +0100 Subject: [PATCH 050/163] changed some minor stuff in the parser --- Makefile | 1 - asdf | 0 includes/minishell.h | 2 +- includes/parser.h | 7 +++++- includes/utils.h | 2 +- src/parser/parser.c | 47 +++--------------------------------- src/parser/parser_utils.c | 51 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 63 insertions(+), 47 deletions(-) delete mode 100644 asdf create mode 100644 src/parser/parser_utils.c diff --git a/Makefile b/Makefile index b8fd80c..c04b785 100644 --- a/Makefile +++ b/Makefile @@ -33,7 +33,6 @@ HEADER = $(wildcard ./includes/*.h) INCLUDES =-Iincludes -I$(PATHP) -I$(LIBFTP) - all: $(BUILD_PATHS) $(NAME) $(PATHO)%.o:: $(PATHS)%.c diff --git a/asdf b/asdf deleted file mode 100644 index e69de29..0000000 diff --git a/includes/minishell.h b/includes/minishell.h index 240ad7e..4dc5114 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/03/03 12:17:45 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/04 11:54:42 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/includes/parser.h b/includes/parser.h index 842c7db..a0d03b4 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/03/04 09:36:47 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/04 12:02:11 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -69,4 +69,9 @@ int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); t_simple_cmds *parser(t_lexor *lexor_list, t_tools *tools); +//parser_utils +t_parser_tools init_parser_tools(t_lexor *lexor_list); +void count_pipes(t_lexor *lexor_list, t_tools *tools); +int count_args(t_lexor *lexor_list); + #endif diff --git a/includes/utils.h b/includes/utils.h index 077c27a..020f372 100644 --- a/includes/utils.h +++ b/includes/utils.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:36:23 by mgraaf #+# #+# */ -/* Updated: 2022/02/24 16:58:36 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/04 12:01:37 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/parser/parser.c b/src/parser/parser.c index b054d38..8bf1327 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/04 11:28:30 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/04 12:01:30 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,31 +14,10 @@ void print_parser(t_simple_cmds *simple_cmds); -int count_args(t_lexor *lexor_list, t_tools *tools) -{ - t_lexor *tmp; - int i; - - i = 0; - tmp = lexor_list; - while (tmp && tmp->token != PIPE) - { - i++; - tmp = tmp->next; - } - if (tmp) - { - if (tmp->token == PIPE) - tools->pipes++; - } - return (i); -} - int handle_heredoc(t_parser_tools *parser_tools, t_lexor *tmp) { t_lexor *node; - printf("%s %d\n", tmp->prev->str, tmp->token); if (tmp->prev->str && tmp->token == LESS_LESS) { node = ft_lexornew(ft_strdup(tmp->prev->str), LESS_LESS); @@ -67,7 +46,6 @@ void find_redirections(t_parser_tools *parser_tools) tmp = parser_tools->lexor_list; while (parser_tools->arg_size > 0) { - printf("%d\n", parser_tools->arg_size); if (tmp && tmp->token == LESS_LESS) handle_heredoc(parser_tools, tmp); else if (tmp && (tmp->token >= GREAT @@ -96,13 +74,7 @@ t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) i = 0; find_redirections(parser_tools); str = malloc(sizeof(char **) * parser_tools->arg_size + 1); - t_lexor *tmp; - tmp = parser_tools->lexor_list; - while (tmp && tmp->token != PIPE) - { - parser_tools->arg_size++; - tmp = tmp->next; - } + parser_tools->arg_size = count_args(parser_tools->lexor_list); if (!str) return (NULL); while (parser_tools->arg_size > 0) @@ -116,17 +88,6 @@ t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) parser_tools->num_redirections, parser_tools->redirections)); } -t_parser_tools init_parser_tools(t_lexor *lexor_list, int arg_size) -{ - t_parser_tools parser_tools; - - parser_tools.lexor_list = lexor_list; - parser_tools.redirections = NULL; - parser_tools.arg_size = arg_size; - parser_tools.num_redirections = 0; - return (parser_tools); -} - //free lexor_list //handle malloc errors @@ -136,12 +97,12 @@ t_simple_cmds *parser(t_lexor *lexor_list, t_tools *tools) t_parser_tools parser_tools; tools->simple_cmds = NULL; + count_pipes(lexor_list, tools); while (lexor_list) { if (lexor_list->token == PIPE) lexor_list = lexor_list->next; - parser_tools = init_parser_tools(lexor_list, - count_args(lexor_list, tools)); + parser_tools = init_parser_tools(lexor_list); node = initialize_cmd(&parser_tools); if (!tools->simple_cmds) tools->simple_cmds = node; diff --git a/src/parser/parser_utils.c b/src/parser/parser_utils.c new file mode 100644 index 0000000..c2f4d37 --- /dev/null +++ b/src/parser/parser_utils.c @@ -0,0 +1,51 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* parser_utils.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/03/04 11:52:02 by mgraaf #+# #+# */ +/* Updated: 2022/03/04 12:01:59 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_parser_tools init_parser_tools(t_lexor *lexor_list) +{ + t_parser_tools parser_tools; + + parser_tools.lexor_list = lexor_list; + parser_tools.redirections = NULL; + parser_tools.arg_size = count_args(lexor_list); + parser_tools.num_redirections = 0; + return (parser_tools); +} + +void count_pipes(t_lexor *lexor_list, t_tools *tools) +{ + t_lexor *tmp; + + tmp = lexor_list; + while (tmp && tmp->token != PIPE) + { + tools->pipes++; + tmp = tmp->next; + } +} + +int count_args(t_lexor *lexor_list) +{ + t_lexor *tmp; + int i; + + i = 0; + tmp = lexor_list; + while (tmp && tmp->token != PIPE) + { + i++; + tmp = tmp->next; + } + return (i); +} From 946ce1c256522f92b38699397fc9d5fd8e199854 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Tue, 8 Mar 2022 16:25:31 +0100 Subject: [PATCH 051/163] parser seg fault --- includes/parser.h | 4 +++- src/builtins/builtins.c | 2 +- src/main.c | 2 +- src/parser/parser.c | 30 ++++++++++++++---------------- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/includes/parser.h b/includes/parser.h index a0d03b4..1125f05 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/03/04 12:02:11 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/07 15:10:47 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,6 +34,8 @@ typedef struct s_lexor struct s_lexor *prev; } t_lexor; + + typedef struct s_parser_tools { t_lexor *lexor_list; diff --git a/src/builtins/builtins.c b/src/builtins/builtins.c index a0cba3d..8f8ffe1 100644 --- a/src/builtins/builtins.c +++ b/src/builtins/builtins.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 11:42:32 by mgraaf #+# #+# */ -/* Updated: 2022/03/03 10:29:38 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/08 12:09:57 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/main.c b/src/main.c index 624d7b9..a358360 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/03 13:14:51 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/08 12:03:43 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/parser/parser.c b/src/parser/parser.c index 8bf1327..2e60428 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/04 12:01:30 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/08 16:14:49 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,22 +18,22 @@ int handle_heredoc(t_parser_tools *parser_tools, t_lexor *tmp) { t_lexor *node; - if (tmp->prev->str && tmp->token == LESS_LESS) - { - node = ft_lexornew(ft_strdup(tmp->prev->str), LESS_LESS); - if (!node) - printf("EMERGENCY!!\n"); - ft_lexoradd_back(&parser_tools->redirections, node); - ft_lexordelone(&parser_tools->lexor_list, tmp->prev->i); - parser_tools->arg_size--; - } - node = ft_lexornew(ft_strdup(tmp->next->str), tmp->token); + node = ft_lexornew(ft_strjoin(ft_strdup(tmp->prev->str), + ft_strjoin("|", ft_strdup(tmp->next->str))), tmp->token); if (!node) printf("EMERGENCY!!\n"); ft_lexoradd_back(&parser_tools->redirections, node); + printf("%d\n\n", tmp->i); ft_lexordelone(&parser_tools->lexor_list, tmp->i); - ft_lexordelone(&parser_tools->lexor_list, tmp->next->i); + printf("%d\n\n", tmp->i); + ft_lexordelone(&parser_tools->lexor_list, 0); + printf("%d\n\n", tmp->i); parser_tools->arg_size--; + // if (tmp->prev->str && tmp->token == LESS_LESS) + // { + // ft_lexordelone(&parser_tools->lexor_list, tmp->i + 1); + // parser_tools->arg_size--; + // } parser_tools->num_redirections++; return (1); } @@ -73,8 +73,8 @@ t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) i = 0; find_redirections(parser_tools); - str = malloc(sizeof(char **) * parser_tools->arg_size + 1); parser_tools->arg_size = count_args(parser_tools->lexor_list); + str = malloc(sizeof(char **) * parser_tools->arg_size + 1); if (!str) return (NULL); while (parser_tools->arg_size > 0) @@ -100,7 +100,7 @@ t_simple_cmds *parser(t_lexor *lexor_list, t_tools *tools) count_pipes(lexor_list, tools); while (lexor_list) { - if (lexor_list->token == PIPE) + if (lexor_list && lexor_list->token == PIPE) lexor_list = lexor_list->next; parser_tools = init_parser_tools(lexor_list); node = initialize_cmd(&parser_tools); @@ -109,8 +109,6 @@ t_simple_cmds *parser(t_lexor *lexor_list, t_tools *tools) else ft_simple_cmdsadd_back(&tools->simple_cmds, node); lexor_list = parser_tools.lexor_list; - if (lexor_list && lexor_list->token == PIPE) - lexor_list = lexor_list->next; } print_parser(tools->simple_cmds); return (node); From a47bbf6b01cf5f3a9c71d82bcb6d09cee66a4c5a Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Tue, 8 Mar 2022 16:38:37 +0100 Subject: [PATCH 052/163] parser no front arg < > << >> --- src/builtins/builtins.c | 9 ++++++--- src/parser/parser.c | 28 +++++++++++++++------------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/builtins/builtins.c b/src/builtins/builtins.c index 8f8ffe1..ec1472e 100644 --- a/src/builtins/builtins.c +++ b/src/builtins/builtins.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 11:42:32 by mgraaf #+# #+# */ -/* Updated: 2022/03/08 12:09:57 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/08 16:30:35 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,8 +34,11 @@ int (*builtin_arr(char *str))(t_tools *tools) i = 0; while (i < 7) { - if (!ft_strncmp(builtins[i][0], str, ft_strlen(str))) - return (builtins[i][1]); + if (str) + { + if (!ft_strncmp(builtins[i][0], str, ft_strlen(str))) + return (builtins[i][1]); + } i++; } return (NULL); diff --git a/src/parser/parser.c b/src/parser/parser.c index 2e60428..1abeef7 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/08 16:14:49 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/08 16:37:29 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,23 +17,22 @@ void print_parser(t_simple_cmds *simple_cmds); int handle_heredoc(t_parser_tools *parser_tools, t_lexor *tmp) { t_lexor *node; + int itmp; + itmp = tmp->i; + if (tmp->prev->str && tmp->token == LESS_LESS) + { + ft_lexordelone(&parser_tools->lexor_list, itmp - 1); + parser_tools->arg_size--; + } node = ft_lexornew(ft_strjoin(ft_strdup(tmp->prev->str), ft_strjoin("|", ft_strdup(tmp->next->str))), tmp->token); if (!node) printf("EMERGENCY!!\n"); ft_lexoradd_back(&parser_tools->redirections, node); - printf("%d\n\n", tmp->i); - ft_lexordelone(&parser_tools->lexor_list, tmp->i); - printf("%d\n\n", tmp->i); - ft_lexordelone(&parser_tools->lexor_list, 0); - printf("%d\n\n", tmp->i); + ft_lexordelone(&parser_tools->lexor_list, itmp); + ft_lexordelone(&parser_tools->lexor_list, itmp + 1); parser_tools->arg_size--; - // if (tmp->prev->str && tmp->token == LESS_LESS) - // { - // ft_lexordelone(&parser_tools->lexor_list, tmp->i + 1); - // parser_tools->arg_size--; - // } parser_tools->num_redirections++; return (1); } @@ -121,9 +120,12 @@ void print_parser(t_simple_cmds *simple_cmds) while (simple_cmds) { printf("\n>>>%i<<<\n", i++); - while (*simple_cmds->str) + if (*simple_cmds->str) { - printf("%s\n", *simple_cmds->str++); + while (*simple_cmds->str) + { + printf("%s\n", *simple_cmds->str++); + } } if (simple_cmds->redirections) printf("\nredirections:\n"); From f9fc025f426ea8135e44ce9ef9b63f557d5ab5f9 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 9 Mar 2022 14:34:59 +0100 Subject: [PATCH 053/163] tmp->i problem --- src/parser/parser.c | 18 +++++++++++------- src/utils/t_lexor_utils.c | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/parser/parser.c b/src/parser/parser.c index 1abeef7..cebb138 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/08 16:37:29 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/09 14:18:42 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -43,8 +43,9 @@ void find_redirections(t_parser_tools *parser_tools) t_lexor *tmp; tmp = parser_tools->lexor_list; - while (parser_tools->arg_size > 0) + while (tmp) { + printf("in =%d \n", parser_tools->arg_size); if (tmp && tmp->token == LESS_LESS) handle_heredoc(parser_tools, tmp); else if (tmp && (tmp->token >= GREAT @@ -55,14 +56,14 @@ void find_redirections(t_parser_tools *parser_tools) printf("EMERGENCY!!\n"); ft_lexoradd_back(&parser_tools->redirections, node); ft_lexordelone(&parser_tools->lexor_list, tmp->i); - ft_lexordelone(&parser_tools->lexor_list, tmp->next->i); - parser_tools->arg_size--; + // parser_tools->arg_size--; parser_tools->num_redirections++; } + parser_tools->arg_size--; if (parser_tools->lexor_list) tmp = tmp->next; - parser_tools->arg_size--; } + // ft_lexordelone(&parser_tools->lexor_list, 0); } t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) @@ -71,14 +72,17 @@ t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) int i; i = 0; - find_redirections(parser_tools); parser_tools->arg_size = count_args(parser_tools->lexor_list); str = malloc(sizeof(char **) * parser_tools->arg_size + 1); + find_redirections(parser_tools); + parser_tools->arg_size = count_args(parser_tools->lexor_list); + printf("%d", parser_tools->arg_size); if (!str) return (NULL); while (parser_tools->arg_size > 0) { - str[i++] = ft_strdup(parser_tools->lexor_list->str); + if (parser_tools->lexor_list->str) + str[i++] = ft_strdup(parser_tools->lexor_list->str); parser_tools->lexor_list = parser_tools->lexor_list->next; parser_tools->arg_size--; } diff --git a/src/utils/t_lexor_utils.c b/src/utils/t_lexor_utils.c index c2a7321..c9801e2 100644 --- a/src/utils/t_lexor_utils.c +++ b/src/utils/t_lexor_utils.c @@ -77,6 +77,28 @@ void ft_lexordelone(t_lexor **lst, int key) *lst = start; } +void deleteNode(t_lexor **head_ref, int key) +{ + + t_lexor *temp; + t_lexor *prev; + + temp = *head_ref; + if (temp != NULL && temp->i == key) { + *head_ref = temp->next; + free(temp); + return; + } + while (temp != NULL && temp->i != key) { + prev = temp; + temp = temp->next; + } + if (temp == NULL) + return; + prev->next = temp->next; + free(temp); +} + void ft_lexorclear(t_lexor **lst) { t_lexor *tmp; From acf8642e3e934f811a91f93ed53edb8b1600f101 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Thu, 10 Mar 2022 16:09:45 +0100 Subject: [PATCH 054/163] < > >> still << --- src/builtins/builtins.c | 4 ++-- src/parser/parser.c | 25 ++++++++++++++----------- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/builtins/builtins.c b/src/builtins/builtins.c index ec1472e..0d04a4d 100644 --- a/src/builtins/builtins.c +++ b/src/builtins/builtins.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 11:42:32 by mgraaf #+# #+# */ -/* Updated: 2022/03/08 16:30:35 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/10 11:18:04 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -36,7 +36,7 @@ int (*builtin_arr(char *str))(t_tools *tools) { if (str) { - if (!ft_strncmp(builtins[i][0], str, ft_strlen(str))) + if (!ft_strncmp(builtins[i][0], str, ft_strlen(builtins[i][0]))) return (builtins[i][1]); } i++; diff --git a/src/parser/parser.c b/src/parser/parser.c index cebb138..32c5ca1 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/09 14:18:42 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/10 16:06:24 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,21 +17,22 @@ void print_parser(t_simple_cmds *simple_cmds); int handle_heredoc(t_parser_tools *parser_tools, t_lexor *tmp) { t_lexor *node; - int itmp; - itmp = tmp->i; - if (tmp->prev->str && tmp->token == LESS_LESS) + tmp = tmp->prev; + if (tmp->str && tmp->token == LESS_LESS) { - ft_lexordelone(&parser_tools->lexor_list, itmp - 1); + ft_lexordelone(&parser_tools->lexor_list, tmp->prev->i); parser_tools->arg_size--; } + tmp = tmp->next; node = ft_lexornew(ft_strjoin(ft_strdup(tmp->prev->str), ft_strjoin("|", ft_strdup(tmp->next->str))), tmp->token); if (!node) printf("EMERGENCY!!\n"); ft_lexoradd_back(&parser_tools->redirections, node); - ft_lexordelone(&parser_tools->lexor_list, itmp); - ft_lexordelone(&parser_tools->lexor_list, itmp + 1); + ft_lexordelone(&parser_tools->lexor_list, tmp->i); + tmp = tmp->next; + ft_lexordelone(&parser_tools->lexor_list, tmp->i); parser_tools->arg_size--; parser_tools->num_redirections++; return (1); @@ -43,9 +44,9 @@ void find_redirections(t_parser_tools *parser_tools) t_lexor *tmp; tmp = parser_tools->lexor_list; - while (tmp) + while (parser_tools->arg_size) { - printf("in =%d \n", parser_tools->arg_size); + printf("arg =%d \n", parser_tools->arg_size); if (tmp && tmp->token == LESS_LESS) handle_heredoc(parser_tools, tmp); else if (tmp && (tmp->token >= GREAT @@ -56,12 +57,14 @@ void find_redirections(t_parser_tools *parser_tools) printf("EMERGENCY!!\n"); ft_lexoradd_back(&parser_tools->redirections, node); ft_lexordelone(&parser_tools->lexor_list, tmp->i); - // parser_tools->arg_size--; + tmp = tmp->next; + ft_lexordelone(&parser_tools->lexor_list, tmp->i); + parser_tools->arg_size--; parser_tools->num_redirections++; } - parser_tools->arg_size--; if (parser_tools->lexor_list) tmp = tmp->next; + parser_tools->arg_size--; } // ft_lexordelone(&parser_tools->lexor_list, 0); } From 9bae9a015b4127a4bf811125a1dbf017bde6f1c5 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Mon, 14 Mar 2022 18:48:55 +0100 Subject: [PATCH 055/163] chage lexer $ --- src/executor/executor.c | 4 +++- src/lexor/handle_token.c | 5 ++--- src/parser/parser.c | 10 ++++++---- test/Testtoken_reader.c | 6 ++---- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/executor/executor.c b/src/executor/executor.c index 396701a..3344c14 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/02/25 14:41:17 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/14 18:17:52 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -74,3 +74,5 @@ int executor(t_tools *tools) waitpid(ret, &status, 0); return (0); } + +lexer $ \ No newline at end of file diff --git a/src/lexor/handle_token.c b/src/lexor/handle_token.c index c49f234..54fa5a4 100644 --- a/src/lexor/handle_token.c +++ b/src/lexor/handle_token.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/18 10:27:43 by mgraaf #+# #+# */ -/* Updated: 2022/03/03 12:02:27 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/14 18:41:33 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -20,12 +20,11 @@ t_tokens check_token(int c) {'\n', NEW_LINE}, {'>', GREAT}, {'<', LESS}, - {'$', DOLLAR}, }; int i; i = 0; - while (i < 5) + while (i < 4) { if (token_arr[i][0] == c) return (token_arr[i][1]); diff --git a/src/parser/parser.c b/src/parser/parser.c index 32c5ca1..244c5bd 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/10 16:06:24 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/11 14:20:13 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,10 +23,12 @@ int handle_heredoc(t_parser_tools *parser_tools, t_lexor *tmp) { ft_lexordelone(&parser_tools->lexor_list, tmp->prev->i); parser_tools->arg_size--; + tmp = tmp->next; + node = ft_lexornew(ft_strjoin(ft_strdup(tmp->prev->str), + ft_strjoin("|", ft_strdup(tmp->next->str))), tmp->token); } - tmp = tmp->next; - node = ft_lexornew(ft_strjoin(ft_strdup(tmp->prev->str), - ft_strjoin("|", ft_strdup(tmp->next->str))), tmp->token); + node = ft_lexornew(ft_strdup(tmp->next->str), + tmp->token); if (!node) printf("EMERGENCY!!\n"); ft_lexoradd_back(&parser_tools->redirections, node); diff --git a/test/Testtoken_reader.c b/test/Testtoken_reader.c index 03399f2..5655a12 100644 --- a/test/Testtoken_reader.c +++ b/test/Testtoken_reader.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/28 11:12:08 by fpolycar #+# #+# */ -/* Updated: 2022/03/03 11:03:25 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/14 18:43:14 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -119,9 +119,7 @@ void test_lexer_9(void) { init_test("test $BLA$BLA=10$BLA"); assert_token(0, "test"); - assert_token(DOLLAR, "BLA"); - assert_token(DOLLAR, "BLA=10"); - assert_token(DOLLAR, "BLA"); + assert_token(0, "$BLA$BLA=10$BLA"); } int main(void) From 5b38e760799d8d76aba541978ff234fb62a30c0c Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Tue, 15 Mar 2022 11:23:44 +0100 Subject: [PATCH 056/163] lexer perfect --- src/executor/executor.c | 4 +--- src/expander/expander.c | 0 src/lexor/token_reader.c | 31 ++++++++++++++++++++++++++----- src/main.c | 4 ++-- test/Testtoken_reader.c | 11 ++++++++++- 5 files changed, 39 insertions(+), 11 deletions(-) create mode 100644 src/expander/expander.c diff --git a/src/executor/executor.c b/src/executor/executor.c index 3344c14..5fdbd88 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/03/14 18:17:52 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/15 10:05:42 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -74,5 +74,3 @@ int executor(t_tools *tools) waitpid(ret, &status, 0); return (0); } - -lexer $ \ No newline at end of file diff --git a/src/expander/expander.c b/src/expander/expander.c new file mode 100644 index 0000000..e69de29 diff --git a/src/lexor/token_reader.c b/src/lexor/token_reader.c index 0e4c594..491d490 100644 --- a/src/lexor/token_reader.c +++ b/src/lexor/token_reader.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:11:20 by mgraaf #+# #+# */ -/* Updated: 2022/03/01 17:18:16 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/15 11:03:38 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -36,6 +36,21 @@ int add_node(char *str, t_tokens token, t_lexor **lexor_list) return (1); } +int handle_quotes_inside_word(int i, char *str, char del) +{ + int j; + + j = 0; + if (str[i + j] == del) + { + j++; + while (str[i + j] != del && str[i + j]) + j++; + j++; + } + return (j); +} + int handle_quotes(int i, char *str, char del, t_lexor **lexor_list) { int j; @@ -58,10 +73,16 @@ int read_words(int i, char *str, t_lexor **lexor_list) int j; j = 0; - while (str[i + j] != ' ' && str[i + j] - && !(check_token(str[i + j])) - && !(str[i + j] == 34 && str[i + j] == 39)) - j++; + while (str[i + j] && !(check_token(str[i + j]))) + { + j += handle_quotes_inside_word(i + j, str, 34); + j += handle_quotes_inside_word(i + j, str, 39); + if (str[i + j] == ' ') + break ; + else + j++; + } + printf("%d %d\n", i, j); if (!add_node(ft_substr(str, i, j), 0, lexor_list)) printf("EMERGENCY!\n"); return (j); diff --git a/src/main.c b/src/main.c index a358360..f077dfe 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/08 12:03:43 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/15 10:05:21 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -39,7 +39,7 @@ int main(int argc, char **argv, char **envp) tools.args = readline("minishell$ "); add_history(tools.args); lexor_list = token_reader(&tools); - parser(lexor_list, &tools); + // parser(lexor_list, &tools); // executor(&tools); free(tools.args); } diff --git a/test/Testtoken_reader.c b/test/Testtoken_reader.c index 5655a12..9ad3d11 100644 --- a/test/Testtoken_reader.c +++ b/test/Testtoken_reader.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/28 11:12:08 by fpolycar #+# #+# */ -/* Updated: 2022/03/14 18:43:14 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/15 10:52:59 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -122,6 +122,14 @@ void test_lexer_9(void) assert_token(0, "$BLA$BLA=10$BLA"); } +void test_lexer_10(void) +{ + init_test("test bla=\"test\" bla=\"t est\""); + assert_token(0, "test"); + assert_token(0, "bla=\"test\""); + assert_token(0, "bla=\"t est\""); +} + int main(void) { UNITY_BEGIN(); @@ -134,5 +142,6 @@ int main(void) RUN_TEST(test_lexer_7); RUN_TEST(test_lexer_8); RUN_TEST(test_lexer_9); + RUN_TEST(test_lexer_10); return UNITY_END(); } From 3d43623630c6f06dbfdea4e74dfbc807a52e923f Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Tue, 15 Mar 2022 12:54:46 +0100 Subject: [PATCH 057/163] altered builtin to accept simple_cmds --- includes/parser.h | 6 ++---- src/builtins/builtins.c | 4 ++-- src/builtins/mini_cd.c | 4 ++-- src/builtins/mini_echo.c | 4 ++-- src/builtins/mini_env.c | 4 ++-- src/builtins/mini_exit.c | 4 ++-- src/builtins/mini_export.c | 4 ++-- src/builtins/mini_pwd.c | 4 ++-- src/builtins/mini_unset.c | 4 ++-- src/parser/parser.c | 6 +++--- 10 files changed, 21 insertions(+), 23 deletions(-) diff --git a/includes/parser.h b/includes/parser.h index 1125f05..e053fd6 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/03/07 15:10:47 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/15 12:54:11 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,8 +34,6 @@ typedef struct s_lexor struct s_lexor *prev; } t_lexor; - - typedef struct s_parser_tools { t_lexor *lexor_list; @@ -61,7 +59,7 @@ typedef struct s_tools typedef struct s_simple_cmds { char **str; - int (*builtin)(t_tools *); + int (*builtin)(t_tools *, t_simple_cmds *)); int num_redirections; t_lexor *redirections; struct s_simple_cmds *next; diff --git a/src/builtins/builtins.c b/src/builtins/builtins.c index 0d04a4d..ae2f8d3 100644 --- a/src/builtins/builtins.c +++ b/src/builtins/builtins.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 11:42:32 by mgraaf #+# #+# */ -/* Updated: 2022/03/10 11:18:04 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/15 12:53:14 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ void change_path(t_tools *tools) tools->pwd = getcwd(NULL, sizeof(NULL)); } -int (*builtin_arr(char *str))(t_tools *tools) +int (*builtin_arr(char *str))(t_tools *tools, t_simple_cmds *simple_cmd) { static void *builtins[7][2] = { {"echo", mini_echo}, diff --git a/src/builtins/mini_cd.c b/src/builtins/mini_cd.c index 8b60622..a65a1e4 100644 --- a/src/builtins/mini_cd.c +++ b/src/builtins/mini_cd.c @@ -6,13 +6,13 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:17:04 by mgraaf #+# #+# */ -/* Updated: 2022/02/25 15:02:13 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/15 12:53:17 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "builtins.h" -int mini_cd(t_tools *tools) +int mini_cd(t_tools *tools, t_simple_cmds *simple_cmd) { if (chdir(&tools->args[1])) perror("cd"); diff --git a/src/builtins/mini_echo.c b/src/builtins/mini_echo.c index a82ca30..617f599 100644 --- a/src/builtins/mini_echo.c +++ b/src/builtins/mini_echo.c @@ -6,7 +6,7 @@ /* By: maiadegraaf args[1], "-n", ft_strlen(&tools->args[1]))) print_lines(2, &tools->args); diff --git a/src/builtins/mini_env.c b/src/builtins/mini_env.c index fc9ed68..540f274 100644 --- a/src/builtins/mini_env.c +++ b/src/builtins/mini_env.c @@ -6,13 +6,13 @@ /* By: maiadegraaf args); exit(0); diff --git a/src/builtins/mini_export.c b/src/builtins/mini_export.c index b3f8a27..365243d 100644 --- a/src/builtins/mini_export.c +++ b/src/builtins/mini_export.c @@ -6,13 +6,13 @@ /* By: maiadegraaf pwd); return (1); diff --git a/src/builtins/mini_pwd.c b/src/builtins/mini_pwd.c index 3afe5cc..4f6ed11 100644 --- a/src/builtins/mini_pwd.c +++ b/src/builtins/mini_pwd.c @@ -6,13 +6,13 @@ /* By: maiadegraaf pwd); return (1); diff --git a/src/builtins/mini_unset.c b/src/builtins/mini_unset.c index 14ee0fb..b72f3a2 100644 --- a/src/builtins/mini_unset.c +++ b/src/builtins/mini_unset.c @@ -6,13 +6,13 @@ /* By: maiadegraaf pwd); return (1); diff --git a/src/parser/parser.c b/src/parser/parser.c index 244c5bd..f7fdc68 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/11 14:20:13 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/15 12:24:13 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -25,9 +25,9 @@ int handle_heredoc(t_parser_tools *parser_tools, t_lexor *tmp) parser_tools->arg_size--; tmp = tmp->next; node = ft_lexornew(ft_strjoin(ft_strdup(tmp->prev->str), - ft_strjoin("|", ft_strdup(tmp->next->str))), tmp->token); + ft_strjoin("|", ft_strdup(tmp->next->str))), tmp->token); } - node = ft_lexornew(ft_strdup(tmp->next->str), + node = ft_lexornew(ft_strdup(tmp->next->str), tmp->token); if (!node) printf("EMERGENCY!!\n"); From e8d6dedb668d22ce70de152ea326345af0eff995 Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Tue, 15 Mar 2022 16:56:22 +0100 Subject: [PATCH 058/163] Parser works?!! --- includes/builtins.h | 16 ++++---- includes/lexor.h | 4 +- includes/minishell.h | 4 +- includes/parser.h | 11 +++-- includes/utils.h | 7 ++-- src/builtins/mini_cd.c | 3 +- src/builtins/mini_echo.c | 3 +- src/builtins/mini_env.c | 3 +- src/builtins/mini_exit.c | 3 +- src/builtins/mini_export.c | 3 +- src/builtins/mini_pwd.c | 3 +- src/builtins/mini_unset.c | 3 +- src/lexor/handle_quotes.c | 45 ++++++++++++++++++++ src/lexor/handle_token.c | 20 ++------- src/lexor/token_reader.c | 35 +--------------- src/main.c | 4 +- src/parser/handle_redirections.c | 67 ++++++++++++++++++++++++++++++ src/parser/parser.c | 70 +++----------------------------- src/utils/t_lexor_utils.c | 47 +++++++++------------ src/utils/t_simple_cmds_utils.c | 5 ++- 20 files changed, 186 insertions(+), 170 deletions(-) create mode 100644 src/lexor/handle_quotes.c create mode 100644 src/parser/handle_redirections.c diff --git a/includes/builtins.h b/includes/builtins.h index 6a53e3f..91febde 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:20:00 by mgraaf #+# #+# */ -/* Updated: 2022/03/01 17:00:19 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/15 13:25:05 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,18 +18,18 @@ //builtins void change_path(t_tools *tools); -int mini_echo(t_tools *tools); +int mini_echo(t_tools *tools, t_simple_cmds *simple_cmd); -int mini_cd(t_tools *tools); +int mini_cd(t_tools *tools, t_simple_cmds *simple_cmd); -int mini_pwd(t_tools *tools); +int mini_pwd(t_tools *tools, t_simple_cmds *simple_cmd); -int mini_export(t_tools *tools); +int mini_export(t_tools *tools, t_simple_cmds *simple_cmd); -int mini_unset(t_tools *tools); +int mini_unset(t_tools *tools, t_simple_cmds *simple_cmd); -int mini_env(t_tools *tools); +int mini_env(t_tools *tools, t_simple_cmds *simple_cmd); -int mini_exit(t_tools *tools); +int mini_exit(t_tools *tools, t_simple_cmds *simple_cmd); #endif \ No newline at end of file diff --git a/includes/lexor.h b/includes/lexor.h index e12be7d..2964a84 100644 --- a/includes/lexor.h +++ b/includes/lexor.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:55:06 by mgraaf #+# #+# */ -/* Updated: 2022/02/18 10:42:48 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/15 14:05:16 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,5 +23,7 @@ //handle_tokens +int handle_quotes_inside_word(int i, char *str, char del); +int handle_quotes(int i, char *str, char del, t_lexor **lexor_list); #endif \ No newline at end of file diff --git a/includes/minishell.h b/includes/minishell.h index 4dc5114..b558494 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/03/04 11:54:42 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/15 13:24:45 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -29,6 +29,6 @@ int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); //builtins -int (*builtin_arr(char *str))(t_tools *tools); +int (*builtin_arr(char *str))(t_tools *tools, t_simple_cmds *simple_cmd); #endif \ No newline at end of file diff --git a/includes/parser.h b/includes/parser.h index e053fd6..6aa4f58 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/03/15 12:54:11 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/15 16:18:04 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,12 +17,10 @@ typedef enum s_tokens { PIPE = 1, - NEW_LINE, GREAT, GREAT_GREAT, LESS, LESS_LESS, - DOLLAR, } t_tokens; typedef struct s_lexor @@ -59,7 +57,7 @@ typedef struct s_tools typedef struct s_simple_cmds { char **str; - int (*builtin)(t_tools *, t_simple_cmds *)); + int (*builtin)(t_tools *, struct s_simple_cmds *); int num_redirections; t_lexor *redirections; struct s_simple_cmds *next; @@ -74,4 +72,9 @@ t_parser_tools init_parser_tools(t_lexor *lexor_list); void count_pipes(t_lexor *lexor_list, t_tools *tools); int count_args(t_lexor *lexor_list); +//handle_redirections +int add_new_redirection(t_lexor *tmp, t_parser_tools *parser_tools); +int handle_heredoc(t_parser_tools *parser_tools, t_lexor *tmp); +void rm_redirections(t_parser_tools *parser_tools); + #endif diff --git a/includes/utils.h b/includes/utils.h index 020f372..35169d6 100644 --- a/includes/utils.h +++ b/includes/utils.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:36:23 by mgraaf #+# #+# */ -/* Updated: 2022/03/04 12:01:37 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/15 13:36:34 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,7 +15,8 @@ # include "minishell.h" //t_simple_cmds_utils -t_simple_cmds *ft_simple_cmdsnew(char **str, int (*builtin)(t_tools *), +t_simple_cmds *ft_simple_cmdsnew(char **str, + int (*builtin)(t_tools *, t_simple_cmds *), int num_redirections, t_lexor *redirections); void ft_simple_cmdsadd_back(t_simple_cmds **lst, t_simple_cmds *new); void ft_simple_cmds_rm_first(t_simple_cmds **lst); @@ -27,7 +28,7 @@ t_lexor *ft_lexornew(char *str, int token); void ft_lexoradd_back(t_lexor **lst, t_lexor *new); void ft_lexordelone(t_lexor **lst, int i); void ft_lexorclear(t_lexor **lst); -t_lexor *ft_lexorlast(t_lexor *map); +t_lexor *ft_lexorlast(t_lexor *lst); // t_lexor *token_reader(t_tools *tools); diff --git a/src/builtins/mini_cd.c b/src/builtins/mini_cd.c index a65a1e4..d472962 100644 --- a/src/builtins/mini_cd.c +++ b/src/builtins/mini_cd.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:17:04 by mgraaf #+# #+# */ -/* Updated: 2022/03/15 12:53:17 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/15 13:25:38 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ int mini_cd(t_tools *tools, t_simple_cmds *simple_cmd) { + (void) simple_cmd; if (chdir(&tools->args[1])) perror("cd"); change_path(tools); diff --git a/src/builtins/mini_echo.c b/src/builtins/mini_echo.c index 617f599..2726c4f 100644 --- a/src/builtins/mini_echo.c +++ b/src/builtins/mini_echo.c @@ -6,7 +6,7 @@ /* By: maiadegraaf args[1], "-n", ft_strlen(&tools->args[1]))) print_lines(2, &tools->args); else diff --git a/src/builtins/mini_env.c b/src/builtins/mini_env.c index 540f274..66990a3 100644 --- a/src/builtins/mini_env.c +++ b/src/builtins/mini_env.c @@ -6,7 +6,7 @@ /* By: maiadegraaf envp[i]) { if (!ft_strncmp(tools->envp[i], "PWD=", 4)) diff --git a/src/builtins/mini_exit.c b/src/builtins/mini_exit.c index 3538467..9244aac 100644 --- a/src/builtins/mini_exit.c +++ b/src/builtins/mini_exit.c @@ -6,7 +6,7 @@ /* By: maiadegraaf args); exit(0); return (1); diff --git a/src/builtins/mini_export.c b/src/builtins/mini_export.c index 365243d..576f2b6 100644 --- a/src/builtins/mini_export.c +++ b/src/builtins/mini_export.c @@ -6,7 +6,7 @@ /* By: maiadegraaf pwd); return (1); } diff --git a/src/builtins/mini_pwd.c b/src/builtins/mini_pwd.c index 4f6ed11..b3dbc07 100644 --- a/src/builtins/mini_pwd.c +++ b/src/builtins/mini_pwd.c @@ -6,7 +6,7 @@ /* By: maiadegraaf pwd); return (1); } diff --git a/src/builtins/mini_unset.c b/src/builtins/mini_unset.c index b72f3a2..bde7faa 100644 --- a/src/builtins/mini_unset.c +++ b/src/builtins/mini_unset.c @@ -6,7 +6,7 @@ /* By: maiadegraaf pwd); return (1); } diff --git a/src/lexor/handle_quotes.c b/src/lexor/handle_quotes.c new file mode 100644 index 0000000..c593287 --- /dev/null +++ b/src/lexor/handle_quotes.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* handle_quotes.c :+: :+: */ +/* +:+ */ +/* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/02/18 10:27:43 by mgraaf #+# #+# */ -/* Updated: 2022/03/14 18:41:33 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/15 13:51:31 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,16 +15,15 @@ t_tokens check_token(int c) { - static int token_arr[5][2] = { + static int token_arr[3][2] = { {'|', PIPE}, - {'\n', NEW_LINE}, {'>', GREAT}, {'<', LESS}, }; int i; i = 0; - while (i < 4) + while (i < 3) { if (token_arr[i][0] == c) return (token_arr[i][1]); @@ -36,20 +35,9 @@ t_tokens check_token(int c) int handle_token(char *str, int i, t_lexor **lexor_list) { t_tokens token; - int j; - j = 0; - while (str[i + j + 1] != ' ' && str[i + j + 1] && !(check_token(str[i + j + 1])) - && !(str[i + j + 1] == 34 && str[i + j + 1] == 39)) - j++; token = check_token(str[i]); - if (token == DOLLAR) - { - if (!add_node(ft_substr(str, i + 1, j), DOLLAR, lexor_list)) - printf("EMERGENCY!\n"); - return (j + 1); - } - else if (token == GREAT && check_token(str[i + 1]) == GREAT) + if (token == GREAT && check_token(str[i + 1]) == GREAT) { if (!add_node(NULL, GREAT_GREAT, lexor_list)) printf("EMERGENCY!\n"); diff --git a/src/lexor/token_reader.c b/src/lexor/token_reader.c index 491d490..7878d66 100644 --- a/src/lexor/token_reader.c +++ b/src/lexor/token_reader.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:11:20 by mgraaf #+# #+# */ -/* Updated: 2022/03/15 11:03:38 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/15 14:04:49 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -36,38 +36,6 @@ int add_node(char *str, t_tokens token, t_lexor **lexor_list) return (1); } -int handle_quotes_inside_word(int i, char *str, char del) -{ - int j; - - j = 0; - if (str[i + j] == del) - { - j++; - while (str[i + j] != del && str[i + j]) - j++; - j++; - } - return (j); -} - -int handle_quotes(int i, char *str, char del, t_lexor **lexor_list) -{ - int j; - - j = 0; - if (str[i + j] == del) - { - j++; - while (str[i + j] != del && str[i + j]) - j++; - if (!add_node(ft_substr(str, i, j + 1), 0, lexor_list)) - printf("EMERGENCY!\n"); - j++; - } - return (j + 1); -} - int read_words(int i, char *str, t_lexor **lexor_list) { int j; @@ -82,7 +50,6 @@ int read_words(int i, char *str, t_lexor **lexor_list) else j++; } - printf("%d %d\n", i, j); if (!add_node(ft_substr(str, i, j), 0, lexor_list)) printf("EMERGENCY!\n"); return (j); diff --git a/src/main.c b/src/main.c index f077dfe..7f400d3 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/15 10:05:21 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/15 13:27:45 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -39,7 +39,7 @@ int main(int argc, char **argv, char **envp) tools.args = readline("minishell$ "); add_history(tools.args); lexor_list = token_reader(&tools); - // parser(lexor_list, &tools); + parser(lexor_list, &tools); // executor(&tools); free(tools.args); } diff --git a/src/parser/handle_redirections.c b/src/parser/handle_redirections.c new file mode 100644 index 0000000..30ec303 --- /dev/null +++ b/src/parser/handle_redirections.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* handle_redirections.c :+: :+: */ +/* +:+ */ +/* By: maiadegraaf next->str), tmp->token); + if (!node) + printf("EMERGENCY!!\n"); + ft_lexoradd_back(&parser_tools->redirections, node); + ft_lexordelone(&parser_tools->lexor_list, tmp->i); + tmp = tmp->next; + ft_lexordelone(&parser_tools->lexor_list, tmp->i); + parser_tools->num_redirections++; + return (0); +} + +int handle_heredoc(t_parser_tools *parser_tools, t_lexor *tmp) +{ + t_lexor *node; + + if (tmp->token == LESS_LESS && tmp->prev != NULL && tmp->prev->str) + { + node = ft_lexornew(ft_strjoin(ft_strdup(tmp->prev->str), + ft_strjoin("|", ft_strdup(tmp->next->str))), tmp->token); + if (!node) + printf("EMERGENCY!!\n"); + ft_lexoradd_back(&parser_tools->redirections, node); + ft_lexordelone(&parser_tools->lexor_list, tmp->prev->i); + ft_lexordelone(&parser_tools->lexor_list, tmp->i); + tmp = tmp->next; + ft_lexordelone(&parser_tools->lexor_list, tmp->i); + parser_tools->num_redirections++; + } + else + add_new_redirection(tmp, parser_tools); + return (1); +} + +void rm_redirections(t_parser_tools *parser_tools) +{ + t_lexor *tmp; + + tmp = parser_tools->lexor_list; + while (tmp && tmp->token == 0) + tmp = tmp->next; + if (!tmp || tmp->token == PIPE) + return ; + if (tmp->token == LESS_LESS) + handle_heredoc(parser_tools, tmp); + else if ((tmp->token >= GREAT + && tmp->token <= LESS)) + add_new_redirection(tmp, parser_tools); + rm_redirections(parser_tools); +} diff --git a/src/parser/parser.c b/src/parser/parser.c index f7fdc68..cf4e057 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/15 12:24:13 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/15 16:16:46 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,63 +14,6 @@ void print_parser(t_simple_cmds *simple_cmds); -int handle_heredoc(t_parser_tools *parser_tools, t_lexor *tmp) -{ - t_lexor *node; - - tmp = tmp->prev; - if (tmp->str && tmp->token == LESS_LESS) - { - ft_lexordelone(&parser_tools->lexor_list, tmp->prev->i); - parser_tools->arg_size--; - tmp = tmp->next; - node = ft_lexornew(ft_strjoin(ft_strdup(tmp->prev->str), - ft_strjoin("|", ft_strdup(tmp->next->str))), tmp->token); - } - node = ft_lexornew(ft_strdup(tmp->next->str), - tmp->token); - if (!node) - printf("EMERGENCY!!\n"); - ft_lexoradd_back(&parser_tools->redirections, node); - ft_lexordelone(&parser_tools->lexor_list, tmp->i); - tmp = tmp->next; - ft_lexordelone(&parser_tools->lexor_list, tmp->i); - parser_tools->arg_size--; - parser_tools->num_redirections++; - return (1); -} - -void find_redirections(t_parser_tools *parser_tools) -{ - t_lexor *node; - t_lexor *tmp; - - tmp = parser_tools->lexor_list; - while (parser_tools->arg_size) - { - printf("arg =%d \n", parser_tools->arg_size); - if (tmp && tmp->token == LESS_LESS) - handle_heredoc(parser_tools, tmp); - else if (tmp && (tmp->token >= GREAT - && tmp->token <= LESS)) - { - node = ft_lexornew(ft_strdup(tmp->next->str), tmp->token); - if (!node) - printf("EMERGENCY!!\n"); - ft_lexoradd_back(&parser_tools->redirections, node); - ft_lexordelone(&parser_tools->lexor_list, tmp->i); - tmp = tmp->next; - ft_lexordelone(&parser_tools->lexor_list, tmp->i); - parser_tools->arg_size--; - parser_tools->num_redirections++; - } - if (parser_tools->lexor_list) - tmp = tmp->next; - parser_tools->arg_size--; - } - // ft_lexordelone(&parser_tools->lexor_list, 0); -} - t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) { char **str; @@ -79,9 +22,8 @@ t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) i = 0; parser_tools->arg_size = count_args(parser_tools->lexor_list); str = malloc(sizeof(char **) * parser_tools->arg_size + 1); - find_redirections(parser_tools); + rm_redirections(parser_tools); parser_tools->arg_size = count_args(parser_tools->lexor_list); - printf("%d", parser_tools->arg_size); if (!str) return (NULL); while (parser_tools->arg_size > 0) @@ -141,13 +83,13 @@ void print_parser(t_simple_cmds *simple_cmds) while (simple_cmds->redirections) { printf("%s\t", simple_cmds->redirections->str); - if (simple_cmds->redirections->token == 3) + if (simple_cmds->redirections->token == GREAT) printf("GREAT\n"); - else if (simple_cmds->redirections->token == 4) + else if (simple_cmds->redirections->token == GREAT_GREAT) printf("GREAT_GREAT\n"); - else if (simple_cmds->redirections->token == 5) + else if (simple_cmds->redirections->token == LESS) printf("LESS\n"); - else if (simple_cmds->redirections->token == 6) + else if (simple_cmds->redirections->token == LESS_LESS) printf("LESS_LESS\n"); simple_cmds->redirections = simple_cmds->redirections->next; } diff --git a/src/utils/t_lexor_utils.c b/src/utils/t_lexor_utils.c index c9801e2..f5491e7 100644 --- a/src/utils/t_lexor_utils.c +++ b/src/utils/t_lexor_utils.c @@ -45,7 +45,7 @@ void ft_lexoradd_back(t_lexor **lst, t_lexor *new) tmp = tmp->next; } tmp->next = new; - tmp->prev = prev; + new->prev = tmp; } void ft_lexordelone(t_lexor **lst, int key) @@ -77,27 +77,26 @@ void ft_lexordelone(t_lexor **lst, int key) *lst = start; } -void deleteNode(t_lexor **head_ref, int key) -{ +// void deleteNode(t_lexor **head_ref, int key) +// { +// t_lexor *temp; +// t_lexor *prev; - t_lexor *temp; - t_lexor *prev; - - temp = *head_ref; - if (temp != NULL && temp->i == key) { - *head_ref = temp->next; - free(temp); - return; - } - while (temp != NULL && temp->i != key) { - prev = temp; - temp = temp->next; - } - if (temp == NULL) - return; - prev->next = temp->next; - free(temp); -} +// temp = *head_ref; +// if (temp != NULL && temp->i == key) { +// *head_ref = temp->next; +// free(temp); +// return; +// } +// while (temp != NULL && temp->i != key) { +// prev = temp; +// temp = temp->next; +// } +// if (temp == NULL) +// return; +// prev->next = temp->next; +// free(temp); +// } void ft_lexorclear(t_lexor **lst) { @@ -116,15 +115,9 @@ void ft_lexorclear(t_lexor **lst) t_lexor *ft_lexorlast(t_lexor *lst) { - int i; - - i = 0; if (!lst) return (NULL); while (lst->next != NULL) - { lst = lst->next; - i++; - } return (lst); } diff --git a/src/utils/t_simple_cmds_utils.c b/src/utils/t_simple_cmds_utils.c index 582c939..87e26d0 100644 --- a/src/utils/t_simple_cmds_utils.c +++ b/src/utils/t_simple_cmds_utils.c @@ -6,13 +6,14 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/02/24 16:59:22 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/15 13:36:11 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "utils.h" -t_simple_cmds *ft_simple_cmdsnew(char **str, int (*builtin)(t_tools *), +t_simple_cmds *ft_simple_cmdsnew(char **str, + int (*builtin)(t_tools *, t_simple_cmds *), int num_redirections, t_lexor *redirections) { t_simple_cmds *new_element; From 65dd38f9c37d79a0243ef99f47a9f82eacf11ff9 Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Wed, 16 Mar 2022 09:56:15 +0100 Subject: [PATCH 059/163] Small cleanup --- .DS_Store | Bin 6148 -> 0 bytes .gitignore | 3 ++- src/lexor/handle_quotes.c | 4 ++-- src/main.c | 4 ++-- 4 files changed, 6 insertions(+), 5 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 7d41e9693408fc630ebed4a3b73d7bd6f015772f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHK%}T>S5T0$T-B!dN1of7SH<1>l#e)#49=r(=J*a46iVf7PG^s^vCC{O6-kXvSe8JJ`sZ@Lva{||nC|4$Zij~QSFW{Lq(*!T9ExFmbFPA!hkS_kzCm4xy# mgX0o3^ihntbQG_mYC*q42BK>*Gl(7(z6fXf8Q diff --git a/.gitignore b/.gitignore index adf21f6..a77d767 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.o libft.a minishell -build \ No newline at end of file +build +.DS_Store \ No newline at end of file diff --git a/src/lexor/handle_quotes.c b/src/lexor/handle_quotes.c index c593287..ee6dd81 100644 --- a/src/lexor/handle_quotes.c +++ b/src/lexor/handle_quotes.c @@ -6,7 +6,7 @@ /* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/15 13:27:45 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/15 16:56:50 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -44,4 +44,4 @@ int main(int argc, char **argv, char **envp) free(tools.args); } return (0); -} \ No newline at end of file +} From 944cd40776b2bcc01211471049a374f29096059a Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 16 Mar 2022 10:01:54 +0100 Subject: [PATCH 060/163] builtin change --- src/builtins/mini_echo.c | 14 +++++++------- src/builtins/mini_env.c | 15 ++++++++++----- src/builtins/mini_exit.c | 4 ++-- src/expander/expander.c | 18 ++++++++++++++++++ 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/builtins/mini_echo.c b/src/builtins/mini_echo.c index 617f599..86ec8db 100644 --- a/src/builtins/mini_echo.c +++ b/src/builtins/mini_echo.c @@ -6,30 +6,30 @@ /* By: maiadegraaf args[1], "-n", ft_strlen(&tools->args[1]))) - print_lines(2, &tools->args); + print_lines(2, &tools->args, 1); else { - print_lines(1, &tools->args); - printf("\n"); + print_lines(1, &tools->args, 1); + ft_putchar_fd('\n', STDOUT_FILENO); } return (1); } diff --git a/src/builtins/mini_env.c b/src/builtins/mini_env.c index 540f274..61ad214 100644 --- a/src/builtins/mini_env.c +++ b/src/builtins/mini_env.c @@ -6,7 +6,7 @@ /* By: maiadegraaf envp[i]) { if (!ft_strncmp(tools->envp[i], "PWD=", 4)) - printf("PWD=%s\n", tools->pwd); + { + ft_putstr_fd("PWD=", STDOUT_FILENO); + ft_putendl_fd(tools->pwd, STDOUT_FILENO); + } else if (!ft_strncmp(tools->envp[i], "OLDPWD=", 7)) - printf("OLDPWD=%s\n", tools->old_pwd); + { + ft_putstr_fd("OLDPWD=", STDOUT_FILENO); + ft_putendl_fd(tools->old_pwd, STDOUT_FILENO); + } else - printf("%s\n", tools->envp[i]); + ft_putendl_fd(tools->envp[i], STDOUT_FILENO); i++; } return (1); diff --git a/src/builtins/mini_exit.c b/src/builtins/mini_exit.c index 3538467..974b683 100644 --- a/src/builtins/mini_exit.c +++ b/src/builtins/mini_exit.c @@ -6,7 +6,7 @@ /* By: maiadegraaf args); - exit(0); + exit(STDERR_FILENO); return (1); } diff --git a/src/expander/expander.c b/src/expander/expander.c index e69de29..b9bafb2 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* expander.c :+: :+: */ +/* +:+ */ +/* By: fpolycar +#+ */ +/* +#+ */ +/* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ +/* Updated: 2022/03/15 13:44:22 by fpolycar ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include + +void expander(void) +{ + +} \ No newline at end of file From e682daa097ef659b8b8ec45addd5570779c3555e Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 16 Mar 2022 10:38:07 +0100 Subject: [PATCH 061/163] remove sub --- unity | 1 - 1 file changed, 1 deletion(-) delete mode 160000 unity diff --git a/unity b/unity deleted file mode 160000 index db878cc..0000000 --- a/unity +++ /dev/null @@ -1 +0,0 @@ -Subproject commit db878ccaedaea3d07b3c5443a00d131ecacd30b3 From 3a6c78bbf6ca5abb82c089dfa8b9472532c94bc4 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 16 Mar 2022 10:39:13 +0100 Subject: [PATCH 062/163] add unity --- test/Testtoken_reader.c | 8 ++------ unity | 1 + 2 files changed, 3 insertions(+), 6 deletions(-) create mode 160000 unity diff --git a/test/Testtoken_reader.c b/test/Testtoken_reader.c index 9ad3d11..ca17e49 100644 --- a/test/Testtoken_reader.c +++ b/test/Testtoken_reader.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/28 11:12:08 by fpolycar #+# #+# */ -/* Updated: 2022/03/15 10:52:59 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/16 10:15:44 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -66,13 +66,9 @@ void test_lexer_3(void) void test_lexer_4(void) { - init_test(" test \n\n test\n \ntest"); + init_test(" test test test"); assert_token(0, "test"); - assert_token(NEW_LINE, NULL); - assert_token(NEW_LINE, NULL); assert_token(0, "test"); - assert_token(NEW_LINE, NULL); - assert_token(NEW_LINE, NULL); assert_token(0, "test"); } diff --git a/unity b/unity new file mode 160000 index 0000000..db878cc --- /dev/null +++ b/unity @@ -0,0 +1 @@ +Subproject commit db878ccaedaea3d07b3c5443a00d131ecacd30b3 From 69f0b96d62b8a4154382c2b435040058bf5954a8 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 16 Mar 2022 10:51:58 +0100 Subject: [PATCH 063/163] submodule --- .gitmodules | 3 +++ Unity | 1 + unity | 1 - 3 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 .gitmodules create mode 160000 Unity delete mode 160000 unity diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..cbe9089 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "Unity"] + path = Unity + url = https://github.com/ThrowTheSwitch/Unity.git diff --git a/Unity b/Unity new file mode 160000 index 0000000..8286aaf --- /dev/null +++ b/Unity @@ -0,0 +1 @@ +Subproject commit 8286aaf32cfc2362b4355f809599a482a9b32578 diff --git a/unity b/unity deleted file mode 160000 index db878cc..0000000 --- a/unity +++ /dev/null @@ -1 +0,0 @@ -Subproject commit db878ccaedaea3d07b3c5443a00d131ecacd30b3 From 8847f4b09f2dde6786ed814537939912078d6414 Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Wed, 16 Mar 2022 11:06:46 +0100 Subject: [PATCH 064/163] Changed main a bit and started error handeling --- includes/error.h | 14 +++++++++++ src/error/error_handeling.c | 46 +++++++++++++++++++++++++++++++++++++ src/lexor/token_reader.c | 13 +++++++++-- src/main.c | 29 +++++++++++++---------- test/Makefile | 7 +++++- 5 files changed, 94 insertions(+), 15 deletions(-) create mode 100644 includes/error.h create mode 100644 src/error/error_handeling.c diff --git a/includes/error.h b/includes/error.h new file mode 100644 index 0000000..685ce3d --- /dev/null +++ b/includes/error.h @@ -0,0 +1,14 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* error.h :+: :+: */ +/* +:+ */ +/* By: maiadegraaf Incorrect input handleing + -> Parser + -> No str after token +-> Malloc error handeling + -> Lexor + -> Parser + -> Redirections +*/ + +void lexor_error(t_lexor **lexor_list) +{ + +} + +/** + * @brief + * Finds corresponding error and frees args; + * @param error + * Number of related error: + * 0 = If there is no string following a redirection or a pipe. + * @param tools + */ +void ft_error(int error, t_tools *tools) +{ + if (error == 0) + printf("syntax error: unexpected 'newline'\n"); + else if (error == 1) + printf("memory error: unable to assign memory\n"); +} diff --git a/src/lexor/token_reader.c b/src/lexor/token_reader.c index 7878d66..f8d5fc0 100644 --- a/src/lexor/token_reader.c +++ b/src/lexor/token_reader.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:11:20 by mgraaf #+# #+# */ -/* Updated: 2022/03/15 14:04:49 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/16 10:34:45 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -58,9 +58,11 @@ int read_words(int i, char *str, t_lexor **lexor_list) t_lexor *token_reader(t_tools *tools) { int i; + int j; t_lexor *lexor_list; i = 0; + j = 0; lexor_list = NULL; i += skip_spaces(tools->args, i); while (tools->args[i]) @@ -73,7 +75,14 @@ t_lexor *token_reader(t_tools *tools) else if (check_token(tools->args[i])) i += handle_token(tools->args, i, &lexor_list); else - i += read_words(i, tools->args, &lexor_list); + { + j = read_words(i, tools->args, &lexor_list); + if (j < 0) + { + lexor_error + + } + } i += skip_spaces(tools->args, i); } return (lexor_list); diff --git a/src/main.c b/src/main.c index d0dc09f..f232d3a 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/15 16:56:50 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/16 10:28:20 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -20,12 +20,25 @@ int implement_tools(t_tools *tools) return (1); } -int main(int argc, char **argv, char **envp) +int minishell_loop(t_tools *tools) { - t_tools tools; t_lexor *lexor_list; lexor_list = NULL; + tools->args = readline("minishell$ "); + add_history(tools->args); + lexor_list = token_reader(tools); + parser(lexor_list, tools); + // executor(&tools); + free(tools->args); + minishell_loop(tools); + return (0); +} + +int main(int argc, char **argv, char **envp) +{ + t_tools tools; + if (argc != 1 || argv[1]) { printf("This program does not accept arguments\n"); @@ -34,14 +47,6 @@ int main(int argc, char **argv, char **envp) tools.envp = envp; parse_envp(&tools); implement_tools(&tools); - while (1) - { - tools.args = readline("minishell$ "); - add_history(tools.args); - lexor_list = token_reader(&tools); - parser(lexor_list, &tools); - // executor(&tools); - free(tools.args); - } + minishell_loop(&tools); return (0); } diff --git a/test/Makefile b/test/Makefile index 06b2350..5c28446 100644 --- a/test/Makefile +++ b/test/Makefile @@ -14,6 +14,7 @@ PATHSP = ../src/parser/ PATHP = ../src/pipex/ PATHSU = ../src/utils/ PATHSB = ../src/builtins/ +PATHSE = ../src/error/ PATHT = ../test/ PATHB = ../build/ PATHO = ../build/objs/ @@ -30,7 +31,8 @@ SRCT = $(wildcard $(PATHT)*.c) src = $(wildcard $(PATHSL)*.c) \ $(wildcard $(PATHSP)*.c) \ $(wildcard $(PATHSU)*.c) \ - $(wildcard $(PATHSB)*.c) + $(wildcard $(PATHSB)*.c) \ + $(wildcard $(PATHSE)*.c) COMPILE=gcc -c @@ -82,6 +84,9 @@ $(PATHO)%.o:: $(PATHSU)%.c $(HEADERS) $(PATHO)%.o:: $(PATHSB)%.c $(HEADERS) $(COMPILE) $(CFLAGS) $< -o $@ +$(PATHO)%.o:: $(PATHSE)%.c $(HEADERS) + $(COMPILE) $(CFLAGS) $< -o $@ + $(PATHO)%.o:: $(PATHU)%.c $(PATHU)%.h $(COMPILE) $(CFLAGS) $< -o $@ From a5b06235a167e056f3c3ad7c0b561da89a19bc03 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 16 Mar 2022 16:29:07 +0100 Subject: [PATCH 065/163] builtin nearly done --- includes/builtins.h | 4 ++- src/builtins/builtins.c | 7 +++--- src/builtins/mini_cd.c | 49 ++++++++++++++++++++++++++++++++---- src/builtins/mini_continue.c | 20 +++++++++++++++ src/builtins/mini_echo.c | 20 ++++++++++----- src/builtins/mini_env.c | 6 ++--- src/builtins/mini_exit.c | 4 +-- src/builtins/mini_pwd.c | 6 ++--- src/main.c | 5 ++-- src/parser/parser.c | 4 +-- src/utils/parse_envp.c | 2 +- 11 files changed, 99 insertions(+), 28 deletions(-) create mode 100644 src/builtins/mini_continue.c diff --git a/includes/builtins.h b/includes/builtins.h index 91febde..e147cec 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:20:00 by mgraaf #+# #+# */ -/* Updated: 2022/03/15 13:25:05 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/16 13:45:08 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -32,4 +32,6 @@ int mini_env(t_tools *tools, t_simple_cmds *simple_cmd); int mini_exit(t_tools *tools, t_simple_cmds *simple_cmd); +int mini_continue(t_tools *tools, t_simple_cmds *simple_cmd); + #endif \ No newline at end of file diff --git a/src/builtins/builtins.c b/src/builtins/builtins.c index ae2f8d3..ba7a438 100644 --- a/src/builtins/builtins.c +++ b/src/builtins/builtins.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 11:42:32 by mgraaf #+# #+# */ -/* Updated: 2022/03/15 12:53:14 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/16 15:16:31 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ void change_path(t_tools *tools) { tools->old_pwd = tools->pwd; tools->pwd = getcwd(NULL, sizeof(NULL)); + } int (*builtin_arr(char *str))(t_tools *tools, t_simple_cmds *simple_cmd) @@ -36,10 +37,10 @@ int (*builtin_arr(char *str))(t_tools *tools, t_simple_cmds *simple_cmd) { if (str) { - if (!ft_strncmp(builtins[i][0], str, ft_strlen(builtins[i][0]))) + if (!ft_strncmp(builtins[i][0], str, ft_strlen((builtins[i][0])))) return (builtins[i][1]); } i++; } - return (NULL); + return (mini_continue); } diff --git a/src/builtins/mini_cd.c b/src/builtins/mini_cd.c index d472962..5a114b0 100644 --- a/src/builtins/mini_cd.c +++ b/src/builtins/mini_cd.c @@ -6,17 +6,56 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:17:04 by mgraaf #+# #+# */ -/* Updated: 2022/03/15 13:25:38 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/16 16:25:52 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ +// #include "minishell.h" #include "builtins.h" +char *find_path_ret(char *str, t_tools *tools) +{ + int i; + + i = 0; + while (tools->envp[i]) + { + if (!ft_strncmp(tools->envp[i], str, ft_strlen(str))) + return (ft_substr(tools->envp[i], ft_strlen(str), ft_strlen(tools->envp[i]) - ft_strlen(str))); + i++; + } + return (NULL); +} + int mini_cd(t_tools *tools, t_simple_cmds *simple_cmd) { - (void) simple_cmd; - if (chdir(&tools->args[1])) - perror("cd"); + int ret; + + if (!simple_cmd->str[1]) + { + ret = chdir(find_path_ret("HOME=", tools)); + if (ret != 0) + { + ft_putendl_fd("HOME not set", STDERR_FILENO); + return (EXIT_FAILURE); + } + } + else if (ft_strncmp(simple_cmd->str[1], "-", 1) == 0) + { + ret = chdir(find_path_ret("OLDPWD=", tools)); + if (ret != 0) + { + ft_putendl_fd("OLDPWD not set", STDERR_FILENO); + return (EXIT_FAILURE); + } + } + else + ret = chdir(simple_cmd->str[1]); + if (ret != 0) + { + ft_putendl_fd("Path do not exist", STDERR_FILENO); + return (EXIT_FAILURE); + } change_path(tools); - return (1); + return (EXIT_SUCCESS); } diff --git a/src/builtins/mini_continue.c b/src/builtins/mini_continue.c new file mode 100644 index 0000000..26269ed --- /dev/null +++ b/src/builtins/mini_continue.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* mini_continue.c :+: :+: */ +/* +:+ */ +/* By: fpolycar +#+ */ +/* +#+ */ +/* Created: 2022/03/16 13:43:06 by fpolycar #+# #+# */ +/* Updated: 2022/03/16 13:56:08 by fpolycar ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int mini_continue(t_tools *tools, t_simple_cmds *simple_cmd) +{ + (void) simple_cmd; + (void) tools; + return (EXIT_SUCCESS); +} \ No newline at end of file diff --git a/src/builtins/mini_echo.c b/src/builtins/mini_echo.c index c3d26e6..b33ecb7 100644 --- a/src/builtins/mini_echo.c +++ b/src/builtins/mini_echo.c @@ -6,7 +6,7 @@ /* By: maiadegraaf args[1], "-n", ft_strlen(&tools->args[1]))) - print_lines(2, &tools->args); + int i; + + i = 1; + (void) tools; + if (!simple_cmd->str[i]) + { + ft_putchar_fd('\n', STDOUT_FILENO); + return (EXIT_SUCCESS); + } + if (!ft_strncmp(simple_cmd->str[1], "-n", ft_strlen(simple_cmd->str[1]))) + print_lines(2, simple_cmd->str); else { - print_lines(1, &tools->args); + print_lines(1, simple_cmd->str); ft_putchar_fd('\n', STDOUT_FILENO); } - return (1); + return (EXIT_SUCCESS); } diff --git a/src/builtins/mini_env.c b/src/builtins/mini_env.c index 96f2676..9893ebd 100644 --- a/src/builtins/mini_env.c +++ b/src/builtins/mini_env.c @@ -6,7 +6,7 @@ /* By: maiadegraaf pwd); - return (1); + ft_putendl_fd(tools->pwd, STDOUT_FILENO); + return (EXIT_SUCCESS); } diff --git a/src/main.c b/src/main.c index d0dc09f..3018c27 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/15 16:56:50 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/16 14:13:53 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -39,8 +39,9 @@ int main(int argc, char **argv, char **envp) tools.args = readline("minishell$ "); add_history(tools.args); lexor_list = token_reader(&tools); - parser(lexor_list, &tools); + tools.simple_cmds = parser(lexor_list, &tools); // executor(&tools); + builtin_arr(tools.args)(&tools, tools.simple_cmds); free(tools.args); } return (0); diff --git a/src/parser/parser.c b/src/parser/parser.c index cf4e057..197745f 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/15 16:16:46 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/16 14:13:47 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -60,7 +60,7 @@ t_simple_cmds *parser(t_lexor *lexor_list, t_tools *tools) ft_simple_cmdsadd_back(&tools->simple_cmds, node); lexor_list = parser_tools.lexor_list; } - print_parser(tools->simple_cmds); + // print_parser(tools->simple_cmds); return (node); } diff --git a/src/utils/parse_envp.c b/src/utils/parse_envp.c index 46dd856..2af782d 100644 --- a/src/utils/parse_envp.c +++ b/src/utils/parse_envp.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2021/12/17 16:16:57 by mgraaf #+# #+# */ -/* Updated: 2022/02/17 12:19:04 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/16 15:23:06 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ From 83e4de3c0a0fba714ab2924e519ae2bca6b8e60f Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 16 Mar 2022 17:02:35 +0100 Subject: [PATCH 066/163] export builtin begining --- src/builtins/mini_export.c | 49 +++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/src/builtins/mini_export.c b/src/builtins/mini_export.c index 576f2b6..74f0917 100644 --- a/src/builtins/mini_export.c +++ b/src/builtins/mini_export.c @@ -6,15 +6,58 @@ /* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/02/18 10:27:43 by mgraaf #+# #+# */ -/* Updated: 2022/03/15 13:51:31 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/16 11:48:36 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -40,19 +40,19 @@ int handle_token(char *str, int i, t_lexor **lexor_list) if (token == GREAT && check_token(str[i + 1]) == GREAT) { if (!add_node(NULL, GREAT_GREAT, lexor_list)) - printf("EMERGENCY!\n"); + return (-1); return (2); } else if (token == LESS && check_token(str[i + 1]) == LESS) { if (!add_node(NULL, LESS_LESS, lexor_list)) - printf("EMERGENCY!\n"); + return (-1); return (2); } else if (token) { if (!add_node(NULL, token, lexor_list)) - printf("EMERGENCY!\n"); + return (-1); return (1); } return (0); diff --git a/src/lexor/token_reader.c b/src/lexor/token_reader.c index f8d5fc0..ba22a12 100644 --- a/src/lexor/token_reader.c +++ b/src/lexor/token_reader.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:11:20 by mgraaf #+# #+# */ -/* Updated: 2022/03/16 10:34:45 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/16 15:03:44 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -28,10 +28,7 @@ int add_node(char *str, t_tokens token, t_lexor **lexor_list) node = ft_lexornew(str, token); if (!node) - { - printf("EMERGENCY!\n"); return (0); - } ft_lexoradd_back(lexor_list, node); return (1); } @@ -51,39 +48,31 @@ int read_words(int i, char *str, t_lexor **lexor_list) j++; } if (!add_node(ft_substr(str, i, j), 0, lexor_list)) - printf("EMERGENCY!\n"); + return (-1); return (j); } -t_lexor *token_reader(t_tools *tools) +int token_reader(t_tools *tools) { int i; int j; - t_lexor *lexor_list; i = 0; - j = 0; - lexor_list = NULL; - i += skip_spaces(tools->args, i); while (tools->args[i]) { + j = 0; i += skip_spaces(tools->args, i); if (tools->args[i] == 34) - i += handle_quotes(i, tools->args, 34, &lexor_list); + j = handle_quotes(i, tools->args, 34, &tools->lexor_list); else if (tools->args[i] == 39) - i += handle_quotes(i, tools->args, 39, &lexor_list); + j = handle_quotes(i, tools->args, 39, &tools->lexor_list); else if (check_token(tools->args[i])) - i += handle_token(tools->args, i, &lexor_list); + j = handle_token(tools->args, i, &tools->lexor_list); else - { - j = read_words(i, tools->args, &lexor_list); - if (j < 0) - { - lexor_error - - } - } - i += skip_spaces(tools->args, i); + j = read_words(i, tools->args, &tools->lexor_list); + if (j < 0) + return (0); + i += j; } - return (lexor_list); + return (1); } diff --git a/src/main.c b/src/main.c index f232d3a..1f04d7c 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/16 10:28:20 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/16 16:15:32 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -20,19 +20,32 @@ int implement_tools(t_tools *tools) return (1); } -int minishell_loop(t_tools *tools) +int reset_tools(t_tools *tools) { - t_lexor *lexor_list; + ft_simple_cmdsclear(&tools->simple_cmds); + free(tools->args); + implement_tools(tools); + tools->pipes = 0; + system ("leaks minishell"); + exit (EXIT_SUCCESS); + return (1); +} - lexor_list = NULL; +int minishell_loop(t_tools *tools) +{ + tools->lexor_list = NULL; tools->args = readline("minishell$ "); add_history(tools->args); - lexor_list = token_reader(tools); - parser(lexor_list, tools); + if (!count_quotes(tools->args)) + ft_error(2, tools); + if (!token_reader(tools)) + ft_error(1, tools); + parser(tools); + // ft_lexorclear(&lexor_list); // executor(&tools); - free(tools->args); - minishell_loop(tools); - return (0); + if (reset_tools(tools)) + minishell_loop(tools); + return (1); } int main(int argc, char **argv, char **envp) diff --git a/src/parser/handle_redirections.c b/src/parser/handle_redirections.c index 30ec303..aa1a760 100644 --- a/src/parser/handle_redirections.c +++ b/src/parser/handle_redirections.c @@ -6,7 +6,7 @@ /* By: maiadegraaf next->str, tmp->token); node = ft_lexornew(ft_strdup(tmp->next->str), tmp->token); if (!node) printf("EMERGENCY!!\n"); @@ -33,6 +34,8 @@ int handle_heredoc(t_parser_tools *parser_tools, t_lexor *tmp) if (tmp->token == LESS_LESS && tmp->prev != NULL && tmp->prev->str) { + // node = ft_lexornew(ft_strjoin(tmp->prev->str, + // ft_strjoin("|", tmp->next->str)), tmp->token); node = ft_lexornew(ft_strjoin(ft_strdup(tmp->prev->str), ft_strjoin("|", ft_strdup(tmp->next->str))), tmp->token); if (!node) diff --git a/src/parser/parser.c b/src/parser/parser.c index cf4e057..592ed5a 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/15 16:16:46 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/16 20:36:01 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -30,6 +30,7 @@ t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) { if (parser_tools->lexor_list->str) str[i++] = ft_strdup(parser_tools->lexor_list->str); + // str[i++] = parser_tools->lexor_list->str; parser_tools->lexor_list = parser_tools->lexor_list->next; parser_tools->arg_size--; } @@ -41,25 +42,29 @@ t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) //free lexor_list //handle malloc errors -t_simple_cmds *parser(t_lexor *lexor_list, t_tools *tools) +t_simple_cmds *parser(t_tools *tools) { t_simple_cmds *node; t_parser_tools parser_tools; + t_lexor *start; tools->simple_cmds = NULL; - count_pipes(lexor_list, tools); - while (lexor_list) + start = tools->lexor_list; + count_pipes(tools->lexor_list, tools); + while (tools->lexor_list) { - if (lexor_list && lexor_list->token == PIPE) - lexor_list = lexor_list->next; - parser_tools = init_parser_tools(lexor_list); + if (tools->lexor_list && tools->lexor_list->token == PIPE) + tools->lexor_list = tools->lexor_list->next; + parser_tools = init_parser_tools(tools->lexor_list); node = initialize_cmd(&parser_tools); if (!tools->simple_cmds) tools->simple_cmds = node; else ft_simple_cmdsadd_back(&tools->simple_cmds, node); - lexor_list = parser_tools.lexor_list; + tools->lexor_list = parser_tools.lexor_list; } + ft_lexorclear(&start); + reset_tools(tools); print_parser(tools->simple_cmds); return (node); } diff --git a/src/utils/t_lexor_utils.c b/src/utils/t_lexor_utils.c index f5491e7..f442056 100644 --- a/src/utils/t_lexor_utils.c +++ b/src/utils/t_lexor_utils.c @@ -98,6 +98,7 @@ void ft_lexordelone(t_lexor **lst, int key) // free(temp); // } + void ft_lexorclear(t_lexor **lst) { t_lexor *tmp; @@ -106,18 +107,23 @@ void ft_lexorclear(t_lexor **lst) return ; while (*lst) { + printf("HOWDIE\n"); tmp = (*lst)->next; + if ((*lst)->str) + { + free((*lst)->str); + } free(*lst); *lst = tmp; } *lst = NULL; } -t_lexor *ft_lexorlast(t_lexor *lst) -{ - if (!lst) - return (NULL); - while (lst->next != NULL) - lst = lst->next; - return (lst); -} +// t_lexor *ft_lexorlast(t_lexor *lst) +// { +// if (!lst) +// return (NULL); +// while (lst->next != NULL) +// lst = lst->next; +// return (lst); +// } diff --git a/src/utils/t_simple_cmds_utils.c b/src/utils/t_simple_cmds_utils.c index 87e26d0..a6d75a4 100644 --- a/src/utils/t_simple_cmds_utils.c +++ b/src/utils/t_simple_cmds_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/03/15 13:36:11 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/16 20:24:55 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -64,7 +64,10 @@ void ft_simple_cmdsclear(t_simple_cmds **lst) return ; while (*lst) { + printf("HELLO\n"); tmp = (*lst)->next; + ft_lexorclear(&(*lst)->redirections); + free_arr((*lst)->str); free(*lst); *lst = tmp; } diff --git a/src/utils/utils.c b/src/utils/utils.c index fc73a48..151304d 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -6,9 +6,42 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/21 11:17:26 by fpolycar #+# #+# */ -/* Updated: 2022/02/28 16:13:40 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/16 15:07:23 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" +int find_matching_quote(char *line, int i, int *num_del, int del) +{ + int j; + + j = i + 1; + *num_del += 1; + while (line[j] && line[j] != del) + j++; + if (line[j] == del) + *num_del += 1; + return (j - i); +} + +int count_quotes(char *line) +{ + int i; + int s; + int d; + + s = 0; + d = 0; + i = -1; + while (line[++i]) + { + if (line[i] == 34) + i += find_matching_quote(line, i, &d, 34); + if (line[i] == 39) + i += find_matching_quote(line, i, &s, 39); + } + if ((d > 0 && d % 2 != 0) || (s > 0 && s % 2 != 0)) + return (0); + return (1); +} From 190654bdff3eae3e056880b1311225210a3c1196 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Thu, 17 Mar 2022 11:16:48 +0100 Subject: [PATCH 068/163] builtin done -> need to free --- includes/builtins.h | 4 ++- src/builtins/mini_cd.c | 4 ++- src/builtins/mini_exit.c | 4 +-- src/builtins/mini_export.c | 51 +++++++++++++++++++++++++++++--------- src/builtins/mini_unset.c | 37 ++++++++++++++++++++++++--- 5 files changed, 80 insertions(+), 20 deletions(-) diff --git a/includes/builtins.h b/includes/builtins.h index e147cec..9452761 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:20:00 by mgraaf #+# #+# */ -/* Updated: 2022/03/16 13:45:08 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/17 10:14:05 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,4 +34,6 @@ int mini_exit(t_tools *tools, t_simple_cmds *simple_cmd); int mini_continue(t_tools *tools, t_simple_cmds *simple_cmd); +int equal_sign(char *str); + #endif \ No newline at end of file diff --git a/src/builtins/mini_cd.c b/src/builtins/mini_cd.c index 5a114b0..753b3e1 100644 --- a/src/builtins/mini_cd.c +++ b/src/builtins/mini_cd.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:17:04 by mgraaf #+# #+# */ -/* Updated: 2022/03/16 16:25:52 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/17 11:16:13 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -50,12 +50,14 @@ int mini_cd(t_tools *tools, t_simple_cmds *simple_cmd) } } else + { ret = chdir(simple_cmd->str[1]); if (ret != 0) { ft_putendl_fd("Path do not exist", STDERR_FILENO); return (EXIT_FAILURE); } + } change_path(tools); return (EXIT_SUCCESS); } diff --git a/src/builtins/mini_exit.c b/src/builtins/mini_exit.c index cd93cb6..2c93d0f 100644 --- a/src/builtins/mini_exit.c +++ b/src/builtins/mini_exit.c @@ -6,7 +6,7 @@ /* By: maiadegraaf args); - exit(STDERR_FILENO); + exit(EXIT_SUCCESS); return (EXIT_SUCCESS); } diff --git a/src/builtins/mini_export.c b/src/builtins/mini_export.c index 74f0917..8a393d5 100644 --- a/src/builtins/mini_export.c +++ b/src/builtins/mini_export.c @@ -6,7 +6,7 @@ /* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/17 13:21:33 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/17 15:25:30 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" +int minishell_loop(t_tools *tools); + int implement_tools(t_tools *tools) { tools->in = dup(0); tools->out = dup(1); tools->err = dup(2); + tools->simple_cmds = NULL; + tools->lexor_list = NULL; return (1); } @@ -27,13 +31,13 @@ int reset_tools(t_tools *tools) implement_tools(tools); tools->pipes = 0; system("leaks minishell"); + minishell_loop(tools); // exit (EXIT_SUCCESS); return (1); } int minishell_loop(t_tools *tools) { - tools->lexor_list = NULL; tools->args = readline("minishell$ "); add_history(tools->args); if (!count_quotes(tools->args)) @@ -43,8 +47,7 @@ int minishell_loop(t_tools *tools) parser(tools); // ft_lexorclear(&lexor_list); // executor(&tools); - if (reset_tools(tools)) - minishell_loop(tools); + reset_tools(tools); return (1); } diff --git a/src/parser/handle_redirections.c b/src/parser/handle_redirections.c index 089d008..af67ea3 100644 --- a/src/parser/handle_redirections.c +++ b/src/parser/handle_redirections.c @@ -6,7 +6,7 @@ /* By: maiadegraaf token == LESS_LESS && tmp->prev != NULL && tmp->prev->str) { - // node = ft_lexornew(ft_strjoin(tmp->prev->str, - // ft_strjoin("|", tmp->next->str)), tmp->token); - node = ft_lexornew(ft_strjoin(ft_strdup(tmp->prev->str), - ft_strjoin("|", ft_strdup(tmp->next->str))), tmp->token); + node = ft_lexornew(join_heredoc(tmp->prev->str, tmp->next->str), + tmp->token); if (!node) printf("EMERGENCY!!\n"); ft_lexoradd_back(&parser_tools->redirections, node); @@ -61,6 +70,8 @@ void rm_redirections(t_parser_tools *parser_tools) tmp = tmp->next; if (!tmp || tmp->token == PIPE) return ; + if (tmp->token && !tmp->next) + lexor_error(0, parser_tools->tools); if (tmp->token == LESS_LESS) handle_heredoc(parser_tools, tmp); else if ((tmp->token >= GREAT diff --git a/src/parser/parser.c b/src/parser/parser.c index fa164f2..ccc59ad 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/17 13:24:20 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/17 15:28:51 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,21 +18,20 @@ t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) { char **str; int i; + int arg_size; i = 0; - parser_tools->arg_size = count_args(parser_tools->lexor_list); - str = malloc(sizeof(char **) * parser_tools->arg_size + 1); rm_redirections(parser_tools); - parser_tools->arg_size = count_args(parser_tools->lexor_list); + arg_size = count_args(parser_tools->lexor_list); + str = malloc(sizeof(char **) * arg_size + 1); if (!str) return (NULL); - while (parser_tools->arg_size > 0) + while (arg_size > 0) { - if (parser_tools->lexor_list->str && parser_tools->lexor_list->i > 0) + if (parser_tools->lexor_list->str && parser_tools->lexor_list->i >= 0) str[i++] = ft_strdup(parser_tools->lexor_list->str); - // str[i++] = parser_tools->lexor_list->str; parser_tools->lexor_list = parser_tools->lexor_list->next; - parser_tools->arg_size--; + arg_size--; } str[i] = NULL; return (ft_simple_cmdsnew(str, builtin_arr(str[0]), @@ -46,16 +45,17 @@ t_simple_cmds *parser(t_tools *tools) { t_simple_cmds *node; t_parser_tools parser_tools; - t_lexor *start; + t_lexor *lexor_start; - tools->simple_cmds = NULL; - start = tools->lexor_list; + lexor_start = tools->lexor_list; count_pipes(tools->lexor_list, tools); while (tools->lexor_list) { if (tools->lexor_list && tools->lexor_list->token == PIPE) tools->lexor_list = tools->lexor_list->next; - parser_tools = init_parser_tools(tools->lexor_list); + if (!tools->lexor_list) + lexor_error(0, tools); + parser_tools = init_parser_tools(tools->lexor_list, tools); node = initialize_cmd(&parser_tools); if (!tools->simple_cmds) tools->simple_cmds = node; @@ -63,8 +63,7 @@ t_simple_cmds *parser(t_tools *tools) ft_simple_cmdsadd_back(&tools->simple_cmds, node); tools->lexor_list = parser_tools.lexor_list; } - ft_lexorclear(&start); - reset_tools(tools); + ft_lexorclear(&lexor_start); print_parser(tools->simple_cmds); return (node); } diff --git a/src/parser/parser_utils.c b/src/parser/parser_utils.c index c2f4d37..5811689 100644 --- a/src/parser/parser_utils.c +++ b/src/parser/parser_utils.c @@ -6,20 +6,20 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/03/04 11:52:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/04 12:01:59 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/17 15:23:44 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_parser_tools init_parser_tools(t_lexor *lexor_list) +t_parser_tools init_parser_tools(t_lexor *lexor_list, t_tools *tools) { t_parser_tools parser_tools; parser_tools.lexor_list = lexor_list; parser_tools.redirections = NULL; - parser_tools.arg_size = count_args(lexor_list); parser_tools.num_redirections = 0; + parser_tools.tools = tools; return (parser_tools); } diff --git a/src/utils/t_simple_cmds_utils.c b/src/utils/t_simple_cmds_utils.c index e5afee3..a920dc8 100644 --- a/src/utils/t_simple_cmds_utils.c +++ b/src/utils/t_simple_cmds_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/03/17 13:23:01 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/17 13:53:38 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ From 1023870bfaadb0714778392305433d520a0778ff Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Fri, 18 Mar 2022 09:38:16 +0100 Subject: [PATCH 074/163] Some other small changes for the better --- Makefile | 2 +- src/parser/parser.c | 5 ++--- src/parser/parser_utils.c | 5 +++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 97a9e35..29d084e 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ src = $(wildcard $(PATHS)*.c) \ OBJS = $(addprefix $(PATHO), $(notdir $(patsubst %.c, %.o, $(src)))) -FLAGS = -Wall -Werror -Wextra -g +FLAGS = -Wall -Werror -Wextra -g -fsanitize=address LIBFT = ./libraries/libft/libft.a diff --git a/src/parser/parser.c b/src/parser/parser.c index ccc59ad..32093b9 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/17 15:28:51 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/17 16:07:38 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) i = 0; rm_redirections(parser_tools); arg_size = count_args(parser_tools->lexor_list); - str = malloc(sizeof(char **) * arg_size + 1); + str = ft_calloc(arg_size + 1, sizeof(char *)); if (!str) return (NULL); while (arg_size > 0) @@ -33,7 +33,6 @@ t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) parser_tools->lexor_list = parser_tools->lexor_list->next; arg_size--; } - str[i] = NULL; return (ft_simple_cmdsnew(str, builtin_arr(str[0]), parser_tools->num_redirections, parser_tools->redirections)); } diff --git a/src/parser/parser_utils.c b/src/parser/parser_utils.c index 5811689..c6930e9 100644 --- a/src/parser/parser_utils.c +++ b/src/parser/parser_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/03/04 11:52:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/17 15:23:44 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/17 16:04:42 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -44,7 +44,8 @@ int count_args(t_lexor *lexor_list) tmp = lexor_list; while (tmp && tmp->token != PIPE) { - i++; + if (tmp->i >= 0) + i++; tmp = tmp->next; } return (i); From 50afe612e23acd2d5a4fd109a047d1ce8f61bc7d Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Fri, 18 Mar 2022 09:52:30 +0100 Subject: [PATCH 075/163] Fix parser when redirections --- Makefile | 3 ++- includes/parser.h | 3 ++- src/parser/parser.c | 4 ++-- src/parser/parser_utils.c | 9 ++++++++- src/utils/t_simple_cmds_utils.c | 2 +- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 29d084e..f299b71 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,8 @@ src = $(wildcard $(PATHS)*.c) \ OBJS = $(addprefix $(PATHO), $(notdir $(patsubst %.c, %.o, $(src)))) -FLAGS = -Wall -Werror -Wextra -g -fsanitize=address +FLAGS = -Wall -Werror -Wextra -g +#-fsanitize=address LIBFT = ./libraries/libft/libft.a diff --git a/includes/parser.h b/includes/parser.h index ffd46ab..9004e3f 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/03/17 15:23:51 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/18 09:46:52 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -72,6 +72,7 @@ t_simple_cmds *parser(t_tools *tools); t_parser_tools init_parser_tools(t_lexor *lexor_list, t_tools *tools); void count_pipes(t_lexor *lexor_list, t_tools *tools); int count_args(t_lexor *lexor_list); +t_lexor *find_next_cmd(t_lexor *lexor_lst); //handle_redirections int add_new_redirection(t_lexor *tmp, t_parser_tools *parser_tools); diff --git a/src/parser/parser.c b/src/parser/parser.c index 32093b9..126b19b 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/17 16:07:38 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/18 09:47:23 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -60,7 +60,7 @@ t_simple_cmds *parser(t_tools *tools) tools->simple_cmds = node; else ft_simple_cmdsadd_back(&tools->simple_cmds, node); - tools->lexor_list = parser_tools.lexor_list; + tools->lexor_list = find_next_cmd(parser_tools.lexor_list); } ft_lexorclear(&lexor_start); print_parser(tools->simple_cmds); diff --git a/src/parser/parser_utils.c b/src/parser/parser_utils.c index c6930e9..67fc26b 100644 --- a/src/parser/parser_utils.c +++ b/src/parser/parser_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/03/04 11:52:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/17 16:04:42 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/18 09:47:34 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -50,3 +50,10 @@ int count_args(t_lexor *lexor_list) } return (i); } + +t_lexor *find_next_cmd(t_lexor *lexor_lst) +{ + while (lexor_lst && lexor_lst->token != PIPE) + lexor_lst = lexor_lst->next; + return (lexor_lst); +} diff --git a/src/utils/t_simple_cmds_utils.c b/src/utils/t_simple_cmds_utils.c index a920dc8..6a7d89a 100644 --- a/src/utils/t_simple_cmds_utils.c +++ b/src/utils/t_simple_cmds_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/03/17 13:53:38 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/18 09:51:10 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ From 13c16124e245bd78864fc9456b667254921786b7 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Fri, 18 Mar 2022 13:27:23 +0100 Subject: [PATCH 076/163] tester + reset leak --- Makefile | 3 +-- includes/parser.h | 2 +- includes/utils.h | 4 ++- src/builtins/builtins.c | 3 +-- src/builtins/mini_cd.c | 2 +- src/builtins/mini_echo.c | 2 +- src/builtins/mini_export.c | 2 +- src/error/error_handeling.c | 5 ++-- src/main.c | 41 +--------------------------- src/parser/parser.c | 4 +-- src/utils/t_simple_cmds_utils.c | 4 +-- src/utils/utils.c | 47 ++++++++++++++++++++++++++++++++- test/Makefile | 2 +- test/Testparser.c | 4 +-- test/Testtoken_reader.c | 19 +++++++------ 15 files changed, 77 insertions(+), 67 deletions(-) diff --git a/Makefile b/Makefile index f299b71..3dcbabc 100644 --- a/Makefile +++ b/Makefile @@ -27,8 +27,7 @@ src = $(wildcard $(PATHS)*.c) \ OBJS = $(addprefix $(PATHO), $(notdir $(patsubst %.c, %.o, $(src)))) -FLAGS = -Wall -Werror -Wextra -g -#-fsanitize=address +FLAGS = -Wall -Werror -Wextra -g #-fsanitize=address LIBFT = ./libraries/libft/libft.a diff --git a/includes/parser.h b/includes/parser.h index 9004e3f..3f8ab9a 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/03/18 09:46:52 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/18 11:13:40 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/includes/utils.h b/includes/utils.h index 1fb5a86..fd115f9 100644 --- a/includes/utils.h +++ b/includes/utils.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:36:23 by mgraaf #+# #+# */ -/* Updated: 2022/03/16 20:25:00 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/18 13:18:01 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -38,6 +38,8 @@ int token_reader(t_tools *tools); int add_node(char *str, t_tokens token, t_lexor **lexor_list); t_tokens check_token(int c); int handle_token(char *str, int i, t_lexor **lexor_list); +int implement_tools(t_tools *tools); +int minishell_loop(t_tools *tools); #endif \ No newline at end of file diff --git a/src/builtins/builtins.c b/src/builtins/builtins.c index 7ec2b36..13b4106 100644 --- a/src/builtins/builtins.c +++ b/src/builtins/builtins.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 11:42:32 by mgraaf #+# #+# */ -/* Updated: 2022/03/17 13:34:25 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/18 10:12:16 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -16,7 +16,6 @@ void change_path(t_tools *tools) { tools->old_pwd = tools->pwd; tools->pwd = getcwd(NULL, sizeof(NULL)); - } int (*builtin_arr(char *str))(t_tools *tools, t_simple_cmds *simple_cmd) diff --git a/src/builtins/mini_cd.c b/src/builtins/mini_cd.c index 753b3e1..ad8fdf2 100644 --- a/src/builtins/mini_cd.c +++ b/src/builtins/mini_cd.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:17:04 by mgraaf #+# #+# */ -/* Updated: 2022/03/17 11:16:13 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/18 10:47:00 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/builtins/mini_echo.c b/src/builtins/mini_echo.c index b33ecb7..5560353 100644 --- a/src/builtins/mini_echo.c +++ b/src/builtins/mini_echo.c @@ -6,7 +6,7 @@ /* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/18 09:40:24 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/18 13:18:10 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -int minishell_loop(t_tools *tools); - -int implement_tools(t_tools *tools) -{ - tools->in = dup(0); - tools->out = dup(1); - tools->err = dup(2); - tools->simple_cmds = NULL; - tools->lexor_list = NULL; - return (1); -} - -int reset_tools(t_tools *tools) -{ - ft_simple_cmdsclear(&tools->simple_cmds); - free(tools->args); - implement_tools(tools); - tools->pipes = 0; - system("leaks minishell"); - minishell_loop(tools); - // exit (EXIT_SUCCESS); - return (1); -} - -int minishell_loop(t_tools *tools) -{ - tools->args = readline("minishell$ "); - add_history(tools->args); - if (!count_quotes(tools->args)) - ft_error(2, tools); - if (!token_reader(tools)) - ft_error(1, tools); - tools->simple_cmds = parser(tools); - // ft_lexorclear(&lexor_list); - // executor(&tools); - reset_tools(tools); - return (1); -} - int main(int argc, char **argv, char **envp) { t_tools tools; diff --git a/src/parser/parser.c b/src/parser/parser.c index 59a82a6..8c71668 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/18 09:53:28 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/18 10:01:47 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -63,7 +63,7 @@ t_simple_cmds *parser(t_tools *tools) tools->lexor_list = find_next_cmd(parser_tools.lexor_list); } ft_lexorclear(&lexor_start); - print_parser(tools->simple_cmds); + // print_parser(tools->simple_cmds); return (node); } diff --git a/src/utils/t_simple_cmds_utils.c b/src/utils/t_simple_cmds_utils.c index bd34bf6..6cb2150 100644 --- a/src/utils/t_simple_cmds_utils.c +++ b/src/utils/t_simple_cmds_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/03/18 09:53:34 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/18 11:09:45 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -67,7 +67,7 @@ void ft_simple_cmdsclear(t_simple_cmds **lst) tmp = (*lst)->next; ft_lexorclear(&(*lst)->redirections); free_arr((*lst)->str); - // free(*lst); + free(*lst); *lst = tmp; } *lst = NULL; diff --git a/src/utils/utils.c b/src/utils/utils.c index 151304d..c676da2 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/21 11:17:26 by fpolycar #+# #+# */ -/* Updated: 2022/03/16 15:07:23 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/18 13:24:27 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -45,3 +45,48 @@ int count_quotes(char *line) return (0); return (1); } + +int reset_tools(t_tools *tools) +{ + ft_simple_cmdsclear(&tools->simple_cmds); + free(tools->args); + implement_tools(tools); + tools->pipes = 0; + // minishell_loop(tools); + // exit (EXIT_SUCCESS); + return (1); +} + +int implement_tools(t_tools *tools) +{ + tools->in = dup(0); + tools->out = dup(1); + tools->err = dup(2); + tools->simple_cmds = NULL; + tools->lexor_list = NULL; + return (1); +} + +int minishell_loop(t_tools *tools) +{ + tools->args = readline("minishell$ "); + add_history(tools->args); + if (!count_quotes(tools->args)) + ft_error(2, tools); + if (!token_reader(tools)) + ft_error(1, tools); + // while (tools->lexor_list) + // { + // printf("%s\n\n", tools->lexor_list->str); + // printf("%d\n\n", tools->lexor_list->token); + // tools->lexor_list = tools->lexor_list->next; + // } + tools->simple_cmds = parser(tools); + // printf("str= %s\n", *tools->simple_cmds->str); + // builtin_arr(*tools->simple_cmds->str)(tools, tools->simple_cmds); + system("leaks minishell"); + // ft_lexorclear(&lexor_list); + // executor(&tools); + reset_tools(tools); + return (1); +} \ No newline at end of file diff --git a/test/Makefile b/test/Makefile index 5c28446..3111aca 100644 --- a/test/Makefile +++ b/test/Makefile @@ -32,7 +32,7 @@ src = $(wildcard $(PATHSL)*.c) \ $(wildcard $(PATHSP)*.c) \ $(wildcard $(PATHSU)*.c) \ $(wildcard $(PATHSB)*.c) \ - $(wildcard $(PATHSE)*.c) + $(wildcard $(PATHSE)*.c) \ COMPILE=gcc -c diff --git a/test/Testparser.c b/test/Testparser.c index cfc7e9f..b5300a7 100644 --- a/test/Testparser.c +++ b/test/Testparser.c @@ -19,8 +19,8 @@ void tearDown(void) void init_test(char *line) { test_tools.args = line; - test_lexor = token_reader(&test_tools); - test_simple_cmds = parser(test_lexor, &test_tools); + token_reader(&test_tools); + // test_simple_cmds = parser(test_lexor, &test_tools); } void assert_parser(char **expected, char *builtin, int num_directions, t_lexor *expected_redirection) diff --git a/test/Testtoken_reader.c b/test/Testtoken_reader.c index ca17e49..436a858 100644 --- a/test/Testtoken_reader.c +++ b/test/Testtoken_reader.c @@ -6,15 +6,16 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/28 11:12:08 by fpolycar #+# #+# */ -/* Updated: 2022/03/16 10:15:44 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/18 13:25:17 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "unity.h" #include "lexor.h" +#include "minishell.h" -t_tools test_tools; -t_lexor *test_lexor; + +t_tools *test_tools; void setUp(void) { // set stuff up here @@ -26,15 +27,17 @@ void tearDown(void) { void init_test(char *line) { - test_tools.args = line; - test_lexor = token_reader(&test_tools); + if (!test_tools->args) + reset_tools(test_tools); + test_tools->args = line; + token_reader(test_tools); } void assert_token(int token, char *expected) { - TEST_ASSERT_EQUAL_STRING(expected, test_lexor->str); - TEST_ASSERT_EQUAL_INT(token, test_lexor->token); - test_lexor = test_lexor->next; + TEST_ASSERT_EQUAL_STRING(expected, test_tools->lexor_list->str); + TEST_ASSERT_EQUAL_INT(token, test_tools->lexor_list->token); + test_tools->lexor_list = test_tools->lexor_list->next; } void test_lexer_1(void) From f4442b0199cca5fdccfea81d6ae7165207f7ae48 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Mon, 21 Mar 2022 12:27:47 +0100 Subject: [PATCH 077/163] little mod --- src/builtins/builtins.c | 3 +- src/builtins/mini_echo.c | 2 +- src/builtins/mini_export.c | 23 ++++++++------- src/minishell_loop.c | 59 ++++++++++++++++++++++++++++++++++++++ src/utils/utils.c | 47 +----------------------------- test/Testtoken_reader.c | 2 +- 6 files changed, 76 insertions(+), 60 deletions(-) create mode 100644 src/minishell_loop.c diff --git a/src/builtins/builtins.c b/src/builtins/builtins.c index 13b4106..1e0f702 100644 --- a/src/builtins/builtins.c +++ b/src/builtins/builtins.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 11:42:32 by mgraaf #+# #+# */ -/* Updated: 2022/03/18 10:12:16 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/21 12:00:40 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -41,5 +41,6 @@ int (*builtin_arr(char *str))(t_tools *tools, t_simple_cmds *simple_cmd) } i++; } + printf("tedfdgst\n"); return (NULL); } diff --git a/src/builtins/mini_echo.c b/src/builtins/mini_echo.c index 5560353..0137208 100644 --- a/src/builtins/mini_echo.c +++ b/src/builtins/mini_echo.c @@ -6,7 +6,7 @@ /* By: maiadegraaf envp[i], simple_cmd->str[1], equal_sign(tools->envp[i])) == 0) { - tools->envp[i] = simple_cmd->str[1]; + tools->envp[i] = ft_strdup(simple_cmd->str[1]); return (1); } i++; @@ -70,18 +70,19 @@ int mini_export(t_tools *tools, t_simple_cmds *simple_cmd) mini_env(tools, simple_cmd); else { - if (check_parameter(simple_cmd->str[1]) == 0) + if (simple_cmd->str[1]) { - if (variable_exist(tools, simple_cmd) == 0) + if (check_parameter(simple_cmd->str[1]) == 0) { - if (simple_cmd->str[1]) + if (variable_exist(tools, simple_cmd) == 0 + && equal_sign(simple_cmd->str[1]) != 0) { - i = 0; - while(tools->envp[i]) - i++; - tools->envp[i] = tools->envp[i - 1]; - tools->envp[i - 1] = simple_cmd->str[1]; - tools->envp[i + 1] = NULL; + i = 0; + while(tools->envp[i]) + i++; + tools->envp[i] = tools->envp[i - 1]; + tools->envp[i - 1] = ft_strdup(simple_cmd->str[1]); + tools->envp[i + 1] = NULL; } } } diff --git a/src/minishell_loop.c b/src/minishell_loop.c new file mode 100644 index 0000000..f13f26d --- /dev/null +++ b/src/minishell_loop.c @@ -0,0 +1,59 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* minishell_loop.c :+: :+: */ +/* +:+ */ +/* By: fpolycar +#+ */ +/* +#+ */ +/* Created: 2022/03/21 12:04:00 by fpolycar #+# #+# */ +/* Updated: 2022/03/21 12:04:46 by fpolycar ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int reset_tools(t_tools *tools) +{ + ft_simple_cmdsclear(&tools->simple_cmds); + free(tools->args); + implement_tools(tools); + tools->pipes = 0; + minishell_loop(tools); + // exit (EXIT_SUCCESS); + return (1); +} + +int implement_tools(t_tools *tools) +{ + tools->in = dup(0); + tools->out = dup(1); + tools->err = dup(2); + tools->simple_cmds = NULL; + tools->lexor_list = NULL; + return (1); +} + +int minishell_loop(t_tools *tools) +{ + // int (*builtin)(t_tools *, struct s_simple_cmds *); + + tools->args = readline("minishell$ "); + add_history(tools->args); + if (!count_quotes(tools->args)) + ft_error(2, tools); + if (!token_reader(tools)) + ft_error(1, tools); + tools->simple_cmds = parser(tools); + // printf("str= %s\n", *tools->simple_cmds->str); + builtin_arr(*tools->simple_cmds->str)(tools, tools->simple_cmds); + // if (builtin) + // { + // printf("BUITLIN") + // builtin + // } + // system("leaks minishell"); + // ft_lexorclear(&lexor_list); + // executor(&tools); + reset_tools(tools); + return (1); +} \ No newline at end of file diff --git a/src/utils/utils.c b/src/utils/utils.c index c676da2..fffcac1 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/21 11:17:26 by fpolycar #+# #+# */ -/* Updated: 2022/03/18 13:24:27 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/21 12:03:56 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -45,48 +45,3 @@ int count_quotes(char *line) return (0); return (1); } - -int reset_tools(t_tools *tools) -{ - ft_simple_cmdsclear(&tools->simple_cmds); - free(tools->args); - implement_tools(tools); - tools->pipes = 0; - // minishell_loop(tools); - // exit (EXIT_SUCCESS); - return (1); -} - -int implement_tools(t_tools *tools) -{ - tools->in = dup(0); - tools->out = dup(1); - tools->err = dup(2); - tools->simple_cmds = NULL; - tools->lexor_list = NULL; - return (1); -} - -int minishell_loop(t_tools *tools) -{ - tools->args = readline("minishell$ "); - add_history(tools->args); - if (!count_quotes(tools->args)) - ft_error(2, tools); - if (!token_reader(tools)) - ft_error(1, tools); - // while (tools->lexor_list) - // { - // printf("%s\n\n", tools->lexor_list->str); - // printf("%d\n\n", tools->lexor_list->token); - // tools->lexor_list = tools->lexor_list->next; - // } - tools->simple_cmds = parser(tools); - // printf("str= %s\n", *tools->simple_cmds->str); - // builtin_arr(*tools->simple_cmds->str)(tools, tools->simple_cmds); - system("leaks minishell"); - // ft_lexorclear(&lexor_list); - // executor(&tools); - reset_tools(tools); - return (1); -} \ No newline at end of file diff --git a/test/Testtoken_reader.c b/test/Testtoken_reader.c index 436a858..b4feaa2 100644 --- a/test/Testtoken_reader.c +++ b/test/Testtoken_reader.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/28 11:12:08 by fpolycar #+# #+# */ -/* Updated: 2022/03/18 13:25:17 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/21 10:57:39 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ From d961f74cc7d7404e740e336b8d74f284dea8c357 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Mon, 21 Mar 2022 12:37:38 +0100 Subject: [PATCH 078/163] little mod --- src/error/error_handeling.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/error/error_handeling.c b/src/error/error_handeling.c index a20ea2a..b67a13c 100755 --- a/src/error/error_handeling.c +++ b/src/error/error_handeling.c @@ -6,7 +6,7 @@ /* By: maiadegraaf Date: Mon, 21 Mar 2022 15:20:46 +0100 Subject: [PATCH 079/163] changed parser back still mf leaks --- includes/parser.h | 6 +- includes/utils.h | 3 +- src/parser/handle_redirections.c | 99 ++++++++++++++++++------ src/parser/parser.c | 127 ++++++++++++++++++++++--------- src/parser/parser_utils.c | 5 +- src/utils/t_lexor_utils.c | 3 +- src/utils/t_simple_cmds_utils.c | 11 ++- 7 files changed, 182 insertions(+), 72 deletions(-) diff --git a/includes/parser.h b/includes/parser.h index 9004e3f..9c95b2e 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/03/18 09:46:52 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/21 14:41:43 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -66,10 +66,10 @@ typedef struct s_simple_cmds int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); -t_simple_cmds *parser(t_tools *tools); +int *parser(t_tools *tools); //parser_utils -t_parser_tools init_parser_tools(t_lexor *lexor_list, t_tools *tools); +t_parser_tools init_parser_tools(t_lexor *lexor_list); void count_pipes(t_lexor *lexor_list, t_tools *tools); int count_args(t_lexor *lexor_list); t_lexor *find_next_cmd(t_lexor *lexor_lst); diff --git a/includes/utils.h b/includes/utils.h index 1fb5a86..2c36015 100644 --- a/includes/utils.h +++ b/includes/utils.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:36:23 by mgraaf #+# #+# */ -/* Updated: 2022/03/16 20:25:00 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/21 12:15:28 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -39,5 +39,4 @@ int add_node(char *str, t_tokens token, t_lexor **lexor_list); t_tokens check_token(int c); int handle_token(char *str, int i, t_lexor **lexor_list); - #endif \ No newline at end of file diff --git a/src/parser/handle_redirections.c b/src/parser/handle_redirections.c index af67ea3..63ab671 100644 --- a/src/parser/handle_redirections.c +++ b/src/parser/handle_redirections.c @@ -6,27 +6,27 @@ /* By: maiadegraaf next->str, tmp->token); - node = ft_lexornew(ft_strdup(tmp->next->str), tmp->token); - if (!node) - printf("EMERGENCY!!\n"); - ft_lexoradd_back(&parser_tools->redirections, node); - tmp->i = -1; - tmp = tmp->next; - tmp->i = -1; - parser_tools->num_redirections++; - return (0); -} +// // node = ft_lexornew(tmp->next->str, tmp->token); +// node = ft_lexornew(ft_strdup(tmp->next->str), tmp->token); +// if (!node) +// printf("EMERGENCY!!\n"); +// ft_lexoradd_back(&parser_tools->redirections, node); +// tmp->i = -1; +// tmp = tmp->next; +// tmp->i = -1; +// parser_tools->num_redirections++; +// return (0); +// } char *join_heredoc(char *str1, char *str2) { @@ -39,6 +39,21 @@ char *join_heredoc(char *str1, char *str2) return (ret); } +int add_new_redirection(t_lexor *tmp, t_parser_tools *parser_tools) +{ + t_lexor *node; + + node = ft_lexornew(ft_strdup(tmp->next->str), tmp->token); + if (!node) + printf("EMERGENCY!!\n"); + ft_lexoradd_back(&parser_tools->redirections, node); + ft_lexordelone(&parser_tools->lexor_list, tmp->i); + tmp = tmp->next; + ft_lexordelone(&parser_tools->lexor_list, tmp->i); + parser_tools->num_redirections++; + return (0); +} + int handle_heredoc(t_parser_tools *parser_tools, t_lexor *tmp) { t_lexor *node; @@ -46,14 +61,14 @@ int handle_heredoc(t_parser_tools *parser_tools, t_lexor *tmp) if (tmp->token == LESS_LESS && tmp->prev != NULL && tmp->prev->str) { node = ft_lexornew(join_heredoc(tmp->prev->str, tmp->next->str), - tmp->token); + tmp->token); if (!node) printf("EMERGENCY!!\n"); ft_lexoradd_back(&parser_tools->redirections, node); - tmp->prev->i = -1; - tmp->i = -1; + ft_lexordelone(&parser_tools->lexor_list, tmp->prev->i); + ft_lexordelone(&parser_tools->lexor_list, tmp->i); tmp = tmp->next; - tmp->i = -1; + ft_lexordelone(&parser_tools->lexor_list, tmp->i); parser_tools->num_redirections++; } else @@ -66,12 +81,10 @@ void rm_redirections(t_parser_tools *parser_tools) t_lexor *tmp; tmp = parser_tools->lexor_list; - while ((tmp && tmp->token == 0) || (tmp && tmp->token > 0 && tmp->i < 0)) + while (tmp && tmp->token == 0) tmp = tmp->next; if (!tmp || tmp->token == PIPE) return ; - if (tmp->token && !tmp->next) - lexor_error(0, parser_tools->tools); if (tmp->token == LESS_LESS) handle_heredoc(parser_tools, tmp); else if ((tmp->token >= GREAT @@ -79,3 +92,45 @@ void rm_redirections(t_parser_tools *parser_tools) add_new_redirection(tmp, parser_tools); rm_redirections(parser_tools); } + +// int handle_heredoc(t_parser_tools *parser_tools, t_lexor *tmp) +// { +// t_lexor *node; + +// if (tmp->token == LESS_LESS && tmp->prev != NULL +// && tmp->prev->str && tmp->prev->i > 0) +// { +// node = ft_lexornew(join_heredoc(tmp->prev->str, tmp->next->str), +// tmp->token); +// if (!node) +// printf("EMERGENCY!!\n"); +// ft_lexoradd_back(&parser_tools->redirections, node); +// tmp->prev->i = -1; +// tmp->i = -1; +// tmp = tmp->next; +// tmp->i = -1; +// parser_tools->num_redirections++; +// } +// else +// add_new_redirection(tmp, parser_tools); +// return (1); +// } + +// void rm_redirections(t_parser_tools *parser_tools) +// { +// t_lexor *tmp; + +// tmp = parser_tools->lexor_list; +// while ((tmp && tmp->token == 0) || (tmp && tmp->token > 0 && tmp->i < 0)) +// tmp = tmp->next; +// if (!tmp || tmp->token == PIPE) +// return ; +// if (tmp->token && !tmp->next) +// lexor_error(0, parser_tools->tools); +// if (tmp->token == LESS_LESS) +// handle_heredoc(parser_tools, tmp); +// else if ((tmp->token >= GREAT +// && tmp->token <= LESS)) +// add_new_redirection(tmp, parser_tools); +// rm_redirections(parser_tools); +// } diff --git a/src/parser/parser.c b/src/parser/parser.c index 126b19b..0cf122f 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/18 09:47:23 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/21 15:18:34 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,90 +14,145 @@ void print_parser(t_simple_cmds *simple_cmds); +// t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) +// { +// char **str; +// int i; +// int arg_size; + +// i = 0; +// rm_redirections(parser_tools); +// arg_size = count_args(parser_tools->lexor_list); +// str = ft_calloc(arg_size + 1, sizeof(char *)); +// if (!str) +// return (NULL); +// while (arg_size > 0) +// { +// if (parser_tools->lexor_list->str && parser_tools->lexor_list->i >= 0) +// str[i++] = ft_strdup(parser_tools->lexor_list->str); +// parser_tools->lexor_list = parser_tools->lexor_list->next; +// arg_size--; +// } +// return (ft_simple_cmdsnew(str, builtin_arr(str[0]), +// parser_tools->num_redirections, parser_tools->redirections)); +// } + +//free lexor_list +//handle malloc errors + t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) { char **str; int i; int arg_size; + t_lexor *tmp; i = 0; rm_redirections(parser_tools); arg_size = count_args(parser_tools->lexor_list); str = ft_calloc(arg_size + 1, sizeof(char *)); + tmp = parser_tools->lexor_list; if (!str) return (NULL); while (arg_size > 0) { - if (parser_tools->lexor_list->str && parser_tools->lexor_list->i >= 0) - str[i++] = ft_strdup(parser_tools->lexor_list->str); - parser_tools->lexor_list = parser_tools->lexor_list->next; + if (tmp->str) + { + str[i++] = ft_strdup(tmp->str); + ft_lexordelone(&parser_tools->lexor_list, tmp->i); + } + tmp = tmp->next; arg_size--; } + str[i] = NULL; return (ft_simple_cmdsnew(str, builtin_arr(str[0]), parser_tools->num_redirections, parser_tools->redirections)); } -//free lexor_list -//handle malloc errors - -t_simple_cmds *parser(t_tools *tools) +int *parser(t_tools *tools) { t_simple_cmds *node; t_parser_tools parser_tools; - t_lexor *lexor_start; + t_lexor *tmp; - lexor_start = tools->lexor_list; - count_pipes(tools->lexor_list, tools); - while (tools->lexor_list) + tmp = tools->lexor_list; + tools->simple_cmds = NULL; + count_pipes(tmp, tools); + while (tmp) { - if (tools->lexor_list && tools->lexor_list->token == PIPE) - tools->lexor_list = tools->lexor_list->next; - if (!tools->lexor_list) - lexor_error(0, tools); - parser_tools = init_parser_tools(tools->lexor_list, tools); + if (tmp && tmp->token == PIPE) + tmp = tmp->next; + parser_tools = init_parser_tools(tmp); node = initialize_cmd(&parser_tools); if (!tools->simple_cmds) tools->simple_cmds = node; else ft_simple_cmdsadd_back(&tools->simple_cmds, node); - tools->lexor_list = find_next_cmd(parser_tools.lexor_list); + tmp = find_next_cmd(tmp); } - ft_lexorclear(&lexor_start); - print_parser(tools->simple_cmds); - return (node); + // ft_lexorclear(&tools->lexor_list); + // print_parser(tools->simple_cmds); + return (0); } +// t_simple_cmds *parser(t_tools *tools) +// { +// t_simple_cmds *node; +// t_parser_tools parser_tools; +// t_lexor *lexor_start; + +// lexor_start = tools->lexor_list; +// count_pipes(tools->lexor_list, tools); +// while (tools->lexor_list) +// { +// if (tools->lexor_list && tools->lexor_list->token == PIPE) +// tools->lexor_list = tools->lexor_list->next; +// if (!tools->lexor_list) +// lexor_error(0, tools); +// parser_tools = init_parser_tools(tools->lexor_list, tools); +// node = initialize_cmd(&parser_tools); +// if (!tools->simple_cmds) +// tools->simple_cmds = node; +// else +// ft_simple_cmdsadd_back(&tools->simple_cmds, node); +// tools->lexor_list = find_next_cmd(parser_tools.lexor_list); +// } +// ft_lexorclear(&lexor_start); +// // print_parser(tools->simple_cmds); +// return (node); +// } + void print_parser(t_simple_cmds *simple_cmds) { int i = 0; - - while (simple_cmds) + t_simple_cmds *tmp = simple_cmds; + while (tmp) { printf("\n>>>%i<<<\n", i++); - if (*simple_cmds->str) + if (*tmp->str) { - while (*simple_cmds->str) + while (*tmp->str) { - printf("%s\n", *simple_cmds->str++); + printf("%s\n", *tmp->str++); } } - if (simple_cmds->redirections) + if (tmp->redirections) printf("\nredirections:\n"); - while (simple_cmds->redirections) + while (tmp->redirections) { - printf("%s\t", simple_cmds->redirections->str); - if (simple_cmds->redirections->token == GREAT) + printf("%s\t", tmp->redirections->str); + if (tmp->redirections->token == GREAT) printf("GREAT\n"); - else if (simple_cmds->redirections->token == GREAT_GREAT) + else if (tmp->redirections->token == GREAT_GREAT) printf("GREAT_GREAT\n"); - else if (simple_cmds->redirections->token == LESS) + else if (tmp->redirections->token == LESS) printf("LESS\n"); - else if (simple_cmds->redirections->token == LESS_LESS) + else if (tmp->redirections->token == LESS_LESS) printf("LESS_LESS\n"); - simple_cmds->redirections = simple_cmds->redirections->next; + tmp->redirections = tmp->redirections->next; } - if (simple_cmds->builtin) + if (tmp->builtin) printf("BUILTIN :)\n"); - simple_cmds = simple_cmds->next; + tmp = tmp->next; } } diff --git a/src/parser/parser_utils.c b/src/parser/parser_utils.c index 67fc26b..c9c78f3 100644 --- a/src/parser/parser_utils.c +++ b/src/parser/parser_utils.c @@ -6,20 +6,19 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/03/04 11:52:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/18 09:47:34 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/21 14:28:44 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_parser_tools init_parser_tools(t_lexor *lexor_list, t_tools *tools) +t_parser_tools init_parser_tools(t_lexor *lexor_list) { t_parser_tools parser_tools; parser_tools.lexor_list = lexor_list; parser_tools.redirections = NULL; parser_tools.num_redirections = 0; - parser_tools.tools = tools; return (parser_tools); } diff --git a/src/utils/t_lexor_utils.c b/src/utils/t_lexor_utils.c index f1cbbb1..a41b8a3 100644 --- a/src/utils/t_lexor_utils.c +++ b/src/utils/t_lexor_utils.c @@ -55,6 +55,7 @@ void ft_lexorclear_one(t_lexor **lst) free((*lst)->str); } free(*lst); + *lst = NULL; } void ft_lexordelone(t_lexor **lst, int key) @@ -117,9 +118,7 @@ void ft_lexorclear(t_lexor **lst) { tmp = (*lst)->next; if ((*lst)->str) - { free((*lst)->str); - } free(*lst); *lst = tmp; } diff --git a/src/utils/t_simple_cmds_utils.c b/src/utils/t_simple_cmds_utils.c index 6a7d89a..00491c9 100644 --- a/src/utils/t_simple_cmds_utils.c +++ b/src/utils/t_simple_cmds_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/03/18 09:51:10 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/21 15:03:54 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -59,6 +59,7 @@ void ft_simple_cmds_rm_first(t_simple_cmds **lst) void ft_simple_cmdsclear(t_simple_cmds **lst) { t_simple_cmds *tmp; + t_lexor *redirections_tmp; if (!*lst) return ; @@ -66,9 +67,11 @@ void ft_simple_cmdsclear(t_simple_cmds **lst) { printf("HELLO\n"); tmp = (*lst)->next; - ft_lexorclear(&(*lst)->redirections); - free_arr((*lst)->str); - // free(*lst); + redirections_tmp = (*lst)->redirections; + ft_lexorclear(&redirections_tmp); + if ((*lst)->str) + free_arr((*lst)->str); + free(*lst); *lst = tmp; } *lst = NULL; From 789a891bfd88e18bd97d08d8fc87b75dd211c909 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Mon, 21 Mar 2022 15:48:55 +0100 Subject: [PATCH 080/163] test merge --- includes/utils.h | 4 +-- src/builtins/builtins.c | 4 +-- src/main.c | 43 ++++++++++++++++++++++-- src/minishell_loop.c | 59 --------------------------------- src/parser/parser.c | 2 +- src/utils/t_simple_cmds_utils.c | 3 +- 6 files changed, 47 insertions(+), 68 deletions(-) delete mode 100644 src/minishell_loop.c diff --git a/includes/utils.h b/includes/utils.h index 1b7fbb1..2c36015 100644 --- a/includes/utils.h +++ b/includes/utils.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:36:23 by mgraaf #+# #+# */ -/* Updated: 2022/03/21 15:23:52 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/21 12:15:28 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -38,7 +38,5 @@ int token_reader(t_tools *tools); int add_node(char *str, t_tokens token, t_lexor **lexor_list); t_tokens check_token(int c); int handle_token(char *str, int i, t_lexor **lexor_list); -int implement_tools(t_tools *tools); -int minishell_loop(t_tools *tools); #endif \ No newline at end of file diff --git a/src/builtins/builtins.c b/src/builtins/builtins.c index 1e0f702..97d4b7e 100644 --- a/src/builtins/builtins.c +++ b/src/builtins/builtins.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 11:42:32 by mgraaf #+# #+# */ -/* Updated: 2022/03/21 12:00:40 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/21 15:26:36 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -41,6 +41,6 @@ int (*builtin_arr(char *str))(t_tools *tools, t_simple_cmds *simple_cmd) } i++; } - printf("tedfdgst\n"); + printf("return null from builtin\n"); return (NULL); } diff --git a/src/main.c b/src/main.c index 0d3a4c3..c1c912e 100644 --- a/src/main.c +++ b/src/main.c @@ -6,12 +6,51 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/18 13:18:10 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/21 15:33:16 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" +int minishell_loop(t_tools *tools); + +int implement_tools(t_tools *tools) +{ + tools->in = dup(0); + tools->out = dup(1); + tools->err = dup(2); + tools->simple_cmds = NULL; + tools->lexor_list = NULL; + return (1); +} + +int reset_tools(t_tools *tools) +{ + ft_simple_cmdsclear(&tools->simple_cmds); + free(tools->args); + implement_tools(tools); + tools->pipes = 0; + system("leaks minishell"); + minishell_loop(tools); + // exit (EXIT_SUCCESS); + return (1); +} + +int minishell_loop(t_tools *tools) +{ + tools->args = readline("minishell$ "); + add_history(tools->args); + if (!count_quotes(tools->args)) + ft_error(2, tools); + if (!token_reader(tools)) + ft_error(1, tools); + parser(tools); + // ft_lexorclear(&lexor_list); + // executor(&tools); + reset_tools(tools); + return (1); +} + int main(int argc, char **argv, char **envp) { t_tools tools; @@ -26,4 +65,4 @@ int main(int argc, char **argv, char **envp) implement_tools(&tools); minishell_loop(&tools); return (0); -} +} \ No newline at end of file diff --git a/src/minishell_loop.c b/src/minishell_loop.c deleted file mode 100644 index f13f26d..0000000 --- a/src/minishell_loop.c +++ /dev/null @@ -1,59 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* minishell_loop.c :+: :+: */ -/* +:+ */ -/* By: fpolycar +#+ */ -/* +#+ */ -/* Created: 2022/03/21 12:04:00 by fpolycar #+# #+# */ -/* Updated: 2022/03/21 12:04:46 by fpolycar ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -int reset_tools(t_tools *tools) -{ - ft_simple_cmdsclear(&tools->simple_cmds); - free(tools->args); - implement_tools(tools); - tools->pipes = 0; - minishell_loop(tools); - // exit (EXIT_SUCCESS); - return (1); -} - -int implement_tools(t_tools *tools) -{ - tools->in = dup(0); - tools->out = dup(1); - tools->err = dup(2); - tools->simple_cmds = NULL; - tools->lexor_list = NULL; - return (1); -} - -int minishell_loop(t_tools *tools) -{ - // int (*builtin)(t_tools *, struct s_simple_cmds *); - - tools->args = readline("minishell$ "); - add_history(tools->args); - if (!count_quotes(tools->args)) - ft_error(2, tools); - if (!token_reader(tools)) - ft_error(1, tools); - tools->simple_cmds = parser(tools); - // printf("str= %s\n", *tools->simple_cmds->str); - builtin_arr(*tools->simple_cmds->str)(tools, tools->simple_cmds); - // if (builtin) - // { - // printf("BUITLIN") - // builtin - // } - // system("leaks minishell"); - // ft_lexorclear(&lexor_list); - // executor(&tools); - reset_tools(tools); - return (1); -} \ No newline at end of file diff --git a/src/parser/parser.c b/src/parser/parser.c index d913595..ff20d1a 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/21 15:24:10 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/21 15:29:19 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/utils/t_simple_cmds_utils.c b/src/utils/t_simple_cmds_utils.c index 503695f..00491c9 100644 --- a/src/utils/t_simple_cmds_utils.c +++ b/src/utils/t_simple_cmds_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/03/21 15:24:22 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/21 15:03:54 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -65,6 +65,7 @@ void ft_simple_cmdsclear(t_simple_cmds **lst) return ; while (*lst) { + printf("HELLO\n"); tmp = (*lst)->next; redirections_tmp = (*lst)->redirections; ft_lexorclear(&redirections_tmp); From e2c373ac033e25f4340b45ea14472ccced1ad071 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Tue, 22 Mar 2022 15:11:42 +0100 Subject: [PATCH 081/163] leaks fixed hopefully --- minishell.dSYM/Contents/Info.plist | 20 ++++++++++++++++++++ src/parser/handle_redirections.c | 20 +++++++++++++------- src/parser/parser.c | 6 +++--- 3 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 minishell.dSYM/Contents/Info.plist diff --git a/minishell.dSYM/Contents/Info.plist b/minishell.dSYM/Contents/Info.plist new file mode 100644 index 0000000..e2f3007 --- /dev/null +++ b/minishell.dSYM/Contents/Info.plist @@ -0,0 +1,20 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.minishell + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/src/parser/handle_redirections.c b/src/parser/handle_redirections.c index 63ab671..7974b5d 100644 --- a/src/parser/handle_redirections.c +++ b/src/parser/handle_redirections.c @@ -6,7 +6,7 @@ /* By: maiadegraaf next->str), tmp->token); if (!node) printf("EMERGENCY!!\n"); ft_lexoradd_back(&parser_tools->redirections, node); - ft_lexordelone(&parser_tools->lexor_list, tmp->i); - tmp = tmp->next; - ft_lexordelone(&parser_tools->lexor_list, tmp->i); + index_1 = tmp->i; + index_2 = tmp->next->i; + ft_lexordelone(&parser_tools->lexor_list, index_1); + ft_lexordelone(&parser_tools->lexor_list, index_2); parser_tools->num_redirections++; return (0); } @@ -57,6 +60,8 @@ int add_new_redirection(t_lexor *tmp, t_parser_tools *parser_tools) int handle_heredoc(t_parser_tools *parser_tools, t_lexor *tmp) { t_lexor *node; + int index_1; + int index_2; if (tmp->token == LESS_LESS && tmp->prev != NULL && tmp->prev->str) { @@ -66,9 +71,10 @@ int handle_heredoc(t_parser_tools *parser_tools, t_lexor *tmp) printf("EMERGENCY!!\n"); ft_lexoradd_back(&parser_tools->redirections, node); ft_lexordelone(&parser_tools->lexor_list, tmp->prev->i); - ft_lexordelone(&parser_tools->lexor_list, tmp->i); - tmp = tmp->next; - ft_lexordelone(&parser_tools->lexor_list, tmp->i); + index_1 = tmp->i; + index_2 = tmp->next->i; + ft_lexordelone(&parser_tools->lexor_list, index_1); + ft_lexordelone(&parser_tools->lexor_list, index_2); parser_tools->num_redirections++; } else diff --git a/src/parser/parser.c b/src/parser/parser.c index 0cf122f..17e5c12 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/21 15:18:34 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/22 11:56:40 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -60,8 +60,8 @@ t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) { str[i++] = ft_strdup(tmp->str); ft_lexordelone(&parser_tools->lexor_list, tmp->i); + tmp = parser_tools->lexor_list; } - tmp = tmp->next; arg_size--; } str[i] = NULL; @@ -88,7 +88,7 @@ int *parser(t_tools *tools) tools->simple_cmds = node; else ft_simple_cmdsadd_back(&tools->simple_cmds, node); - tmp = find_next_cmd(tmp); + tmp = parser_tools.lexor_list; } // ft_lexorclear(&tools->lexor_list); // print_parser(tools->simple_cmds); From 65a713e69f9f13010b688f236ceea282e8f93985 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Tue, 22 Mar 2022 16:34:11 +0100 Subject: [PATCH 082/163] leak still here --- Makefile | 2 +- src/main.c | 3 ++- src/parser/parser.c | 17 +++++++---------- src/utils/t_simple_cmds_utils.c | 5 ++++- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 3dcbabc..7bddf01 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ src = $(wildcard $(PATHS)*.c) \ OBJS = $(addprefix $(PATHO), $(notdir $(patsubst %.c, %.o, $(src)))) -FLAGS = -Wall -Werror -Wextra -g #-fsanitize=address +FLAGS = -Wall -Werror -Wextra #-g3 -fsanitize=address LIBFT = ./libraries/libft/libft.a diff --git a/src/main.c b/src/main.c index c1c912e..7529baa 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/21 15:33:16 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/22 16:15:42 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -27,6 +27,7 @@ int implement_tools(t_tools *tools) int reset_tools(t_tools *tools) { ft_simple_cmdsclear(&tools->simple_cmds); + ft_lexorclear(&tools->lexor_list); free(tools->args); implement_tools(tools); tools->pipes = 0; diff --git a/src/parser/parser.c b/src/parser/parser.c index 471f980..f7c8cb2 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/22 15:12:50 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/22 16:17:23 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -64,7 +64,6 @@ t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) } arg_size--; } - str[i] = NULL; return (ft_simple_cmdsnew(str, builtin_arr(str[0]), parser_tools->num_redirections, parser_tools->redirections)); } @@ -73,22 +72,20 @@ int *parser(t_tools *tools) { t_simple_cmds *node; t_parser_tools parser_tools; - t_lexor *tmp; - tmp = tools->lexor_list; tools->simple_cmds = NULL; - count_pipes(tmp, tools); - while (tmp) + count_pipes(tools->lexor_list, tools); + while (tools->lexor_list) { - if (tmp && tmp->token == PIPE) - tmp = tmp->next; - parser_tools = init_parser_tools(tmp); + if (tools->lexor_list && tools->lexor_list->token == PIPE) + ft_lexordelone(&tools->lexor_list, tools->lexor_list->i); + parser_tools = init_parser_tools(tools->lexor_list); node = initialize_cmd(&parser_tools); if (!tools->simple_cmds) tools->simple_cmds = node; else ft_simple_cmdsadd_back(&tools->simple_cmds, node); - tmp = parser_tools.lexor_list; + tools->lexor_list = parser_tools.lexor_list; } // ft_lexorclear(&tools->lexor_list); // print_parser(tools->simple_cmds); diff --git a/src/utils/t_simple_cmds_utils.c b/src/utils/t_simple_cmds_utils.c index 00491c9..43b9fa9 100644 --- a/src/utils/t_simple_cmds_utils.c +++ b/src/utils/t_simple_cmds_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/03/21 15:03:54 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/22 16:33:40 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -70,7 +70,10 @@ void ft_simple_cmdsclear(t_simple_cmds **lst) redirections_tmp = (*lst)->redirections; ft_lexorclear(&redirections_tmp); if ((*lst)->str) + { free_arr((*lst)->str); + // free(*(*lst)->str); + } free(*lst); *lst = tmp; } From 0b88d5b40c5a3f19f705d498ef9be6d7560fd0a2 Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Wed, 23 Mar 2022 10:35:50 +0100 Subject: [PATCH 083/163] The leak is no more!!!!!!! also finished error handeling in the parser and started on the tester --- includes/error.h | 3 +- includes/parser.h | 4 +- libraries/libft/ft_split.c | 4 +- src/error/error_handeling.c | 18 ++-- src/main.c | 40 +-------- src/parser/handle_redirections.c | 68 ++------------- src/parser/parser.c | 138 +++++++++---------------------- src/parser/parser_utils.c | 5 +- src/utils/minishell_loop.c | 40 +++++++++ src/utils/t_simple_cmds_utils.c | 3 +- test/Testparser.c | 110 ++++++++++++------------ test/Testtoken_reader.c | 21 +++-- 12 files changed, 175 insertions(+), 279 deletions(-) create mode 100644 src/utils/minishell_loop.c diff --git a/includes/error.h b/includes/error.h index 39948cb..e4fe11b 100644 --- a/includes/error.h +++ b/includes/error.h @@ -6,7 +6,7 @@ /* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/03/21 14:41:43 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/22 17:06:19 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -69,7 +69,7 @@ int find_pwd(t_tools *tools); int *parser(t_tools *tools); //parser_utils -t_parser_tools init_parser_tools(t_lexor *lexor_list); +t_parser_tools init_parser_tools(t_lexor *lexor_list, t_tools *tools); void count_pipes(t_lexor *lexor_list, t_tools *tools); int count_args(t_lexor *lexor_list); t_lexor *find_next_cmd(t_lexor *lexor_lst); diff --git a/libraries/libft/ft_split.c b/libraries/libft/ft_split.c index 78a2fe4..88a5bee 100644 --- a/libraries/libft/ft_split.c +++ b/libraries/libft/ft_split.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2021/12/16 14:23:50 by mgraaf #+# #+# */ -/* Updated: 2022/03/17 11:50:57 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/22 16:55:51 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -77,6 +77,7 @@ void free_arr(char **split_arr) free(split_arr[i]); i++; } + free(split_arr); } char **ft_split(char const *s, char c) @@ -95,7 +96,6 @@ char **ft_split(char const *s, char c) if (!create_arr(s, c, c_count, split_arr)) { free_arr(split_arr); - free (split_arr); return (NULL); } return (split_arr); diff --git a/src/error/error_handeling.c b/src/error/error_handeling.c index a67c848..b6a1242 100755 --- a/src/error/error_handeling.c +++ b/src/error/error_handeling.c @@ -6,23 +6,17 @@ /* By: maiadegraaf Incorrect input handleing - -> Parser - -> No str after token --> Malloc error handeling - -> Lexor - -> Parser - -> Redirections -*/ +void parser_error(int error, t_tools *tools, t_lexor *lexor_list) +{ + ft_lexorclear(&lexor_list); + ft_error(error, tools); +} void lexor_error(int error, t_tools *tools) { diff --git a/src/main.c b/src/main.c index 717d437..1cdb498 100644 --- a/src/main.c +++ b/src/main.c @@ -6,50 +6,14 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/17 15:25:30 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/22 18:08:51 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" int minishell_loop(t_tools *tools); - -int implement_tools(t_tools *tools) -{ - tools->in = dup(0); - tools->out = dup(1); - tools->err = dup(2); - tools->simple_cmds = NULL; - tools->lexor_list = NULL; - return (1); -} - -int reset_tools(t_tools *tools) -{ - ft_simple_cmdsclear(&tools->simple_cmds); - free(tools->args); - implement_tools(tools); - tools->pipes = 0; - system("leaks minishell"); - minishell_loop(tools); - // exit (EXIT_SUCCESS); - return (1); -} - -int minishell_loop(t_tools *tools) -{ - tools->args = readline("minishell$ "); - add_history(tools->args); - if (!count_quotes(tools->args)) - ft_error(2, tools); - if (!token_reader(tools)) - ft_error(1, tools); - parser(tools); - // ft_lexorclear(&lexor_list); - // executor(&tools); - reset_tools(tools); - return (1); -} +int implement_tools(t_tools *tools); int main(int argc, char **argv, char **envp) { diff --git a/src/parser/handle_redirections.c b/src/parser/handle_redirections.c index 7974b5d..7439c71 100644 --- a/src/parser/handle_redirections.c +++ b/src/parser/handle_redirections.c @@ -6,28 +6,12 @@ /* By: maiadegraaf next->str, tmp->token); -// node = ft_lexornew(ft_strdup(tmp->next->str), tmp->token); -// if (!node) -// printf("EMERGENCY!!\n"); -// ft_lexoradd_back(&parser_tools->redirections, node); -// tmp->i = -1; -// tmp = tmp->next; -// tmp->i = -1; -// parser_tools->num_redirections++; -// return (0); -// } - char *join_heredoc(char *str1, char *str2) { char *tmp_str; @@ -47,7 +31,7 @@ int add_new_redirection(t_lexor *tmp, t_parser_tools *parser_tools) node = ft_lexornew(ft_strdup(tmp->next->str), tmp->token); if (!node) - printf("EMERGENCY!!\n"); + parser_error(1, parser_tools->tools, parser_tools->lexor_list); ft_lexoradd_back(&parser_tools->redirections, node); index_1 = tmp->i; index_2 = tmp->next->i; @@ -66,9 +50,9 @@ int handle_heredoc(t_parser_tools *parser_tools, t_lexor *tmp) if (tmp->token == LESS_LESS && tmp->prev != NULL && tmp->prev->str) { node = ft_lexornew(join_heredoc(tmp->prev->str, tmp->next->str), - tmp->token); + tmp->token); if (!node) - printf("EMERGENCY!!\n"); + parser_error(1, parser_tools->tools, parser_tools->lexor_list); ft_lexoradd_back(&parser_tools->redirections, node); ft_lexordelone(&parser_tools->lexor_list, tmp->prev->i); index_1 = tmp->i; @@ -91,6 +75,8 @@ void rm_redirections(t_parser_tools *parser_tools) tmp = tmp->next; if (!tmp || tmp->token == PIPE) return ; + if (!tmp->next || !tmp->next->str) + parser_error(0, parser_tools->tools, parser_tools->lexor_list); if (tmp->token == LESS_LESS) handle_heredoc(parser_tools, tmp); else if ((tmp->token >= GREAT @@ -98,45 +84,3 @@ void rm_redirections(t_parser_tools *parser_tools) add_new_redirection(tmp, parser_tools); rm_redirections(parser_tools); } - -// int handle_heredoc(t_parser_tools *parser_tools, t_lexor *tmp) -// { -// t_lexor *node; - -// if (tmp->token == LESS_LESS && tmp->prev != NULL -// && tmp->prev->str && tmp->prev->i > 0) -// { -// node = ft_lexornew(join_heredoc(tmp->prev->str, tmp->next->str), -// tmp->token); -// if (!node) -// printf("EMERGENCY!!\n"); -// ft_lexoradd_back(&parser_tools->redirections, node); -// tmp->prev->i = -1; -// tmp->i = -1; -// tmp = tmp->next; -// tmp->i = -1; -// parser_tools->num_redirections++; -// } -// else -// add_new_redirection(tmp, parser_tools); -// return (1); -// } - -// void rm_redirections(t_parser_tools *parser_tools) -// { -// t_lexor *tmp; - -// tmp = parser_tools->lexor_list; -// while ((tmp && tmp->token == 0) || (tmp && tmp->token > 0 && tmp->i < 0)) -// tmp = tmp->next; -// if (!tmp || tmp->token == PIPE) -// return ; -// if (tmp->token && !tmp->next) -// lexor_error(0, parser_tools->tools); -// if (tmp->token == LESS_LESS) -// handle_heredoc(parser_tools, tmp); -// else if ((tmp->token >= GREAT -// && tmp->token <= LESS)) -// add_new_redirection(tmp, parser_tools); -// rm_redirections(parser_tools); -// } diff --git a/src/parser/parser.c b/src/parser/parser.c index 17e5c12..7cb4037 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/22 11:56:40 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/22 17:51:13 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,32 +14,6 @@ void print_parser(t_simple_cmds *simple_cmds); -// t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) -// { -// char **str; -// int i; -// int arg_size; - -// i = 0; -// rm_redirections(parser_tools); -// arg_size = count_args(parser_tools->lexor_list); -// str = ft_calloc(arg_size + 1, sizeof(char *)); -// if (!str) -// return (NULL); -// while (arg_size > 0) -// { -// if (parser_tools->lexor_list->str && parser_tools->lexor_list->i >= 0) -// str[i++] = ft_strdup(parser_tools->lexor_list->str); -// parser_tools->lexor_list = parser_tools->lexor_list->next; -// arg_size--; -// } -// return (ft_simple_cmdsnew(str, builtin_arr(str[0]), -// parser_tools->num_redirections, parser_tools->redirections)); -// } - -//free lexor_list -//handle malloc errors - t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) { char **str; @@ -51,9 +25,9 @@ t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) rm_redirections(parser_tools); arg_size = count_args(parser_tools->lexor_list); str = ft_calloc(arg_size + 1, sizeof(char *)); - tmp = parser_tools->lexor_list; if (!str) - return (NULL); + parser_error(1, parser_tools->tools, parser_tools->lexor_list); + tmp = parser_tools->lexor_list; while (arg_size > 0) { if (tmp->str) @@ -64,7 +38,6 @@ t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) } arg_size--; } - str[i] = NULL; return (ft_simple_cmdsnew(str, builtin_arr(str[0]), parser_tools->num_redirections, parser_tools->redirections)); } @@ -73,86 +46,57 @@ int *parser(t_tools *tools) { t_simple_cmds *node; t_parser_tools parser_tools; - t_lexor *tmp; - tmp = tools->lexor_list; tools->simple_cmds = NULL; - count_pipes(tmp, tools); - while (tmp) + count_pipes(tools->lexor_list, tools); + while (tools->lexor_list) { - if (tmp && tmp->token == PIPE) - tmp = tmp->next; - parser_tools = init_parser_tools(tmp); + if (tools->lexor_list && tools->lexor_list->token == PIPE) + ft_lexordelone(&tools->lexor_list, tools->lexor_list->i); + parser_tools = init_parser_tools(tools->lexor_list, tools); node = initialize_cmd(&parser_tools); + if (!node) + parser_error(0, tools, parser_tools.lexor_list); if (!tools->simple_cmds) tools->simple_cmds = node; else ft_simple_cmdsadd_back(&tools->simple_cmds, node); - tmp = parser_tools.lexor_list; + tools->lexor_list = parser_tools.lexor_list; } - // ft_lexorclear(&tools->lexor_list); - // print_parser(tools->simple_cmds); return (0); } -// t_simple_cmds *parser(t_tools *tools) +// void print_parser(t_simple_cmds *simple_cmds) // { -// t_simple_cmds *node; -// t_parser_tools parser_tools; -// t_lexor *lexor_start; - -// lexor_start = tools->lexor_list; -// count_pipes(tools->lexor_list, tools); -// while (tools->lexor_list) +// int i = 0; +// t_simple_cmds *tmp = simple_cmds; +// while (tmp) // { -// if (tools->lexor_list && tools->lexor_list->token == PIPE) -// tools->lexor_list = tools->lexor_list->next; -// if (!tools->lexor_list) -// lexor_error(0, tools); -// parser_tools = init_parser_tools(tools->lexor_list, tools); -// node = initialize_cmd(&parser_tools); -// if (!tools->simple_cmds) -// tools->simple_cmds = node; -// else -// ft_simple_cmdsadd_back(&tools->simple_cmds, node); -// tools->lexor_list = find_next_cmd(parser_tools.lexor_list); +// printf("\n>>>%i<<<\n", i++); +// if (*tmp->str) +// { +// while (*tmp->str) +// { +// printf("%s\n", *tmp->str++); +// } +// } +// if (tmp->redirections) +// printf("\nredirections:\n"); +// while (tmp->redirections) +// { +// printf("%s\t", tmp->redirections->str); +// if (tmp->redirections->token == GREAT) +// printf("GREAT\n"); +// else if (tmp->redirections->token == GREAT_GREAT) +// printf("GREAT_GREAT\n"); +// else if (tmp->redirections->token == LESS) +// printf("LESS\n"); +// else if (tmp->redirections->token == LESS_LESS) +// printf("LESS_LESS\n"); +// tmp->redirections = tmp->redirections->next; +// } +// if (tmp->builtin) +// printf("BUILTIN :)\n"); +// tmp = tmp->next; // } -// ft_lexorclear(&lexor_start); -// // print_parser(tools->simple_cmds); -// return (node); // } - -void print_parser(t_simple_cmds *simple_cmds) -{ - int i = 0; - t_simple_cmds *tmp = simple_cmds; - while (tmp) - { - printf("\n>>>%i<<<\n", i++); - if (*tmp->str) - { - while (*tmp->str) - { - printf("%s\n", *tmp->str++); - } - } - if (tmp->redirections) - printf("\nredirections:\n"); - while (tmp->redirections) - { - printf("%s\t", tmp->redirections->str); - if (tmp->redirections->token == GREAT) - printf("GREAT\n"); - else if (tmp->redirections->token == GREAT_GREAT) - printf("GREAT_GREAT\n"); - else if (tmp->redirections->token == LESS) - printf("LESS\n"); - else if (tmp->redirections->token == LESS_LESS) - printf("LESS_LESS\n"); - tmp->redirections = tmp->redirections->next; - } - if (tmp->builtin) - printf("BUILTIN :)\n"); - tmp = tmp->next; - } -} diff --git a/src/parser/parser_utils.c b/src/parser/parser_utils.c index c9c78f3..e71bd5c 100644 --- a/src/parser/parser_utils.c +++ b/src/parser/parser_utils.c @@ -6,19 +6,20 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/03/04 11:52:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/21 14:28:44 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/22 17:06:00 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_parser_tools init_parser_tools(t_lexor *lexor_list) +t_parser_tools init_parser_tools(t_lexor *lexor_list, t_tools *tools) { t_parser_tools parser_tools; parser_tools.lexor_list = lexor_list; parser_tools.redirections = NULL; parser_tools.num_redirections = 0; + parser_tools.tools = tools; return (parser_tools); } diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c new file mode 100644 index 0000000..dd198bc --- /dev/null +++ b/src/utils/minishell_loop.c @@ -0,0 +1,40 @@ +#include "minishell.h" + +int minishell_loop(t_tools *tools); + +int implement_tools(t_tools *tools) +{ + tools->in = dup(0); + tools->out = dup(1); + tools->err = dup(2); + tools->simple_cmds = NULL; + tools->lexor_list = NULL; + return (1); +} + +int reset_tools(t_tools *tools) +{ + ft_simple_cmdsclear(&tools->simple_cmds); + free(tools->args); + implement_tools(tools); + tools->pipes = 0; + // system("leaks minishell"); + minishell_loop(tools); + // exit (EXIT_SUCCESS); + return (1); +} + +int minishell_loop(t_tools *tools) +{ + tools->args = readline("minishell$ "); + add_history(tools->args); + if (!count_quotes(tools->args)) + ft_error(2, tools); + if (!token_reader(tools)) + ft_error(1, tools); + parser(tools); + // ft_lexorclear(&lexor_list); + // executor(&tools); + reset_tools(tools); + return (1); +} \ No newline at end of file diff --git a/src/utils/t_simple_cmds_utils.c b/src/utils/t_simple_cmds_utils.c index 00491c9..0b3bbda 100644 --- a/src/utils/t_simple_cmds_utils.c +++ b/src/utils/t_simple_cmds_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/03/21 15:03:54 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/22 17:11:04 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -65,7 +65,6 @@ void ft_simple_cmdsclear(t_simple_cmds **lst) return ; while (*lst) { - printf("HELLO\n"); tmp = (*lst)->next; redirections_tmp = (*lst)->redirections; ft_lexorclear(&redirections_tmp); diff --git a/test/Testparser.c b/test/Testparser.c index cfc7e9f..f002164 100644 --- a/test/Testparser.c +++ b/test/Testparser.c @@ -6,92 +6,92 @@ t_tools test_tools; t_lexor *test_lexor; t_simple_cmds *test_simple_cmds; -void setUp(void) +void setUp(void) { - // set stuff up here + // set stuff up here } -void tearDown(void) +void tearDown(void) { - // clean stuff up here + // clean stuff up here } -void init_test(char *line) +void init_test(char *line) { - test_tools.args = line; - test_lexor = token_reader(&test_tools); - test_simple_cmds = parser(test_lexor, &test_tools); + test_tools.args = line; + token_reader(&test_tools); + parser(&test_tools); } -void assert_parser(char **expected, char *builtin, int num_directions, t_lexor *expected_redirection) +void assert_parser(char **expected, char *builtin, int num_directions, t_lexor *expected_redirection) { - int i; + int i; i = 0; while (expected[i]) { - TEST_ASSERT_EQUAL_STRING(expected[i], test_simple_cmds->str[i]); + TEST_ASSERT_EQUAL_STRING_MESSAGE(expected[i], test_tools.simple_cmds->str[i], "STR CHECK\t"); i++; } if (builtin) - TEST_ASSERT_NOT_NULL(test_simple_cmds->builtin); + TEST_ASSERT_NOT_NULL_MESSAGE(test_tools.simple_cmds->builtin, "BUILTIN CHECK\t"); else - TEST_ASSERT_NULL(test_simple_cmds->builtin); - TEST_ASSERT_EQUAL_INT(num_directions, test_simple_cmds->num_redirections); - TEST_ASSERT_EQUAL_STRING(expected_redirection->str, test_simple_cmds->redirections->str); - TEST_ASSERT_EQUAL_INT(expected_redirection->token, test_simple_cmds->redirections->token); - TEST_ASSERT_EQUAL_INT(expected_redirection->i, test_simple_cmds->redirections->i); - test_simple_cmds = test_simple_cmds->next; + TEST_ASSERT_NULL(test_tools.simple_cmds->builtin); + TEST_ASSERT_EQUAL_INT_MESSAGE(num_directions, test_tools.simple_cmds->num_redirections, "NUM REDIRECTIONS CHECK\t"); + // if (expected_redirection) + // { + // printf("%s\n", test_tools.simple_cmds->redirections->str); + // // TEST_ASSERT_EQUAL_STRING(expected_redirection->str, test_tools.simple_cmds->redirections->str); + // // TEST_ASSERT_EQUAL_INT(expected_redirection->token, test_tools.simple_cmds->redirections->token); + // } + test_tools.simple_cmds = test_tools.simple_cmds->next; } -t_lexor *make_expected_redirection(char *str, int token, int i) +void test_parser_1(void) { - t_lexor *redirection; - - redirection = malloc(sizeof(t_lexor)); - redirection->str = str; - redirection->token = token; - redirection->i = i; - return (redirection); + init_test("test test"); + assert_parser(ft_split("test test", ' '), NULL, 0, NULL); + ft_simple_cmdsclear(&test_tools.simple_cmds); } -char **make_array(char *str, ...) +void test_parser_2(void) { - va_list arg; - char **arr = NULL; - char *test = "test"; - int i; - - i = 0; - va_start(arg, str); - while (test) - { - test = va_arg(arg, char*); - arr[i] = test; - i++; - } - va_end(arg); - return (arr); + init_test("test test | test"); + assert_parser(ft_split("test test", ' '), NULL, 0, NULL); + assert_parser(ft_split("test", ' '), NULL, 0, NULL); + ft_simple_cmdsclear(&test_tools.simple_cmds); } -void test_lexer_1(void) +void test_parser_3(void) { - init_test("test test"); - assert_parser(make_array("test", "test"), NULL, 0, make_expected_redirection(NULL, 0, 0)); + init_test("test test | test | 'test'"); + assert_parser(ft_split("test test", ' '), NULL, 0, NULL); + assert_parser(ft_split("test", ' '), NULL, 0, NULL); + assert_parser(ft_split("'test'", ' '), NULL, 0, NULL); + ft_simple_cmdsclear(&test_tools.simple_cmds); } -void test_lexer_2(void) +void test_parser_4(void) { - init_test("test test | test"); + init_test("< redirection"); + assert_parser(NULL, NULL, 1, ft_lexornew("redirection", LESS)); + ft_simple_cmdsclear(&test_tools.simple_cmds); +} - assert_parser(make_array("test", "test"), NULL, 0, make_expected_redirection(NULL, 0, 0)); - assert_parser(NULL, NULL, 0, make_expected_redirection(NULL, 0, 0)); - assert_parser(make_array("test"), NULL, 0, make_expected_redirection(NULL, 0, 0)); +void test_parser_5(void) +{ + init_test("test > redirection"); + assert_parser(ft_split("test", ' '), NULL, 1, ft_lexornew("redirection", GREAT)); + ft_simple_cmdsclear(&test_tools.simple_cmds); } -int main(void) +int main(void) { - UNITY_BEGIN(); - RUN_TEST(test_lexer_1); - return UNITY_END(); -} \ No newline at end of file + UNITY_BEGIN(); + RUN_TEST(test_parser_1); + RUN_TEST(test_parser_2); + RUN_TEST(test_parser_3); + // RUN_TEST(test_parser_4); + // RUN_TEST(test_parser_5); + return UNITY_END(); +} diff --git a/test/Testtoken_reader.c b/test/Testtoken_reader.c index ca17e49..7142932 100644 --- a/test/Testtoken_reader.c +++ b/test/Testtoken_reader.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/28 11:12:08 by fpolycar #+# #+# */ -/* Updated: 2022/03/16 10:15:44 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/22 18:53:44 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,7 +14,6 @@ #include "lexor.h" t_tools test_tools; -t_lexor *test_lexor; void setUp(void) { // set stuff up here @@ -27,14 +26,14 @@ void tearDown(void) { void init_test(char *line) { test_tools.args = line; - test_lexor = token_reader(&test_tools); + token_reader(&test_tools); } void assert_token(int token, char *expected) { - TEST_ASSERT_EQUAL_STRING(expected, test_lexor->str); - TEST_ASSERT_EQUAL_INT(token, test_lexor->token); - test_lexor = test_lexor->next; + TEST_ASSERT_EQUAL_STRING(expected, test_tools.lexor_list->str); + TEST_ASSERT_EQUAL_INT(token, test_tools.lexor_list->token); + test_tools.lexor_list = test_tools.lexor_list->next; } void test_lexer_1(void) @@ -130,14 +129,24 @@ int main(void) { UNITY_BEGIN(); RUN_TEST(test_lexer_1); + ft_lexorclear(&test_tools.lexor_list); RUN_TEST(test_lexer_2); + ft_lexorclear(&test_tools.lexor_list); RUN_TEST(test_lexer_3); + ft_lexorclear(&test_tools.lexor_list); RUN_TEST(test_lexer_4); + ft_lexorclear(&test_tools.lexor_list); RUN_TEST(test_lexer_5); + ft_lexorclear(&test_tools.lexor_list); RUN_TEST(test_lexer_6); + ft_lexorclear(&test_tools.lexor_list); RUN_TEST(test_lexer_7); + ft_lexorclear(&test_tools.lexor_list); RUN_TEST(test_lexer_8); + ft_lexorclear(&test_tools.lexor_list); RUN_TEST(test_lexer_9); + ft_lexorclear(&test_tools.lexor_list); RUN_TEST(test_lexer_10); + ft_lexorclear(&test_tools.lexor_list); return UNITY_END(); } From 6ea760c298b52318cce119297df8bc479a5d1dfe Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 23 Mar 2022 11:07:50 +0100 Subject: [PATCH 084/163] merge maia_2 Alfred --- Makefile | 5 +++-- src/utils/minishell_loop.c | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index f299b71..8959ab8 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,6 @@ NAME = minishell +MKDIR = mkdir + CC = gcc @@ -14,8 +16,7 @@ PATHSE = src/error/ PATHP = src/pipex/ PATHEX = src/executor/ - -BUILD_PATHS = $(PATHB) $(PATHO) $(PATHR) $(PATHEX) +BUILD_PATHS = $(PATHB) $(PATHO) src = $(wildcard $(PATHS)*.c) \ $(wildcard $(PATHSL)*.c) \ diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index dd198bc..e4a1c7c 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -18,7 +18,7 @@ int reset_tools(t_tools *tools) free(tools->args); implement_tools(tools); tools->pipes = 0; - // system("leaks minishell"); + system("leaks minishell"); minishell_loop(tools); // exit (EXIT_SUCCESS); return (1); From e1cb908eb8f1e8ff56a8d4b21d99c6428fef6a9e Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 23 Mar 2022 16:26:32 +0100 Subject: [PATCH 085/163] env no leak but export & unset to review --- src/builtins/mini_export.c | 43 +++++++++++++++++++++++++++++--------- src/builtins/mini_unset.c | 3 ++- src/main.c | 8 +++++-- src/utils/minishell_loop.c | 3 +++ src/utils/parse_envp.c | 2 +- src/utils/utils.c | 27 +++++++++++++++++++++++- 6 files changed, 71 insertions(+), 15 deletions(-) diff --git a/src/builtins/mini_export.c b/src/builtins/mini_export.c index 8a393d5..2d61162 100644 --- a/src/builtins/mini_export.c +++ b/src/builtins/mini_export.c @@ -6,7 +6,7 @@ /* By: maiadegraaf envp[i], simple_cmd->str[1], equal_sign(tools->envp[i])) == 0) { - tools->envp[i] = simple_cmd->str[1]; + free(tools->envp[i]); + tools->envp[i] = ft_strdup(simple_cmd->str[1]); return (1); } i++; @@ -61,11 +62,36 @@ int check_parameter(char *str) return (0); } -int mini_export(t_tools *tools, t_simple_cmds *simple_cmd) +char **add_var(char **arr, char *str) { - int i; + char **rtn; + size_t i; i = 0; + while (arr[i] != NULL) + i++; + rtn = ft_calloc(sizeof(char *), i + 2); + if (!rtn) + return (NULL); + i = 0; + while (arr[i + 1] != NULL) + { + rtn[i] = ft_strdup(arr[i]); + if (rtn[i] == NULL) + { + free_arr(rtn); + return (rtn); + } + i++; + } + rtn[i] = ft_strdup(str); + return (rtn); +} + +int mini_export(t_tools *tools, t_simple_cmds *simple_cmd) +{ + char **tmp; + if (!simple_cmd->str[1]) mini_env(tools, simple_cmd); else @@ -76,12 +102,9 @@ int mini_export(t_tools *tools, t_simple_cmds *simple_cmd) { if (simple_cmd->str[1]) { - i = 0; - while(tools->envp[i]) - i++; - tools->envp[i] = tools->envp[i - 1]; - tools->envp[i - 1] = simple_cmd->str[1]; - tools->envp[i + 1] = NULL; + tmp = add_var(tools->envp, simple_cmd->str[1]); + free_arr(tools->envp); + tools->envp = tmp; } } } diff --git a/src/builtins/mini_unset.c b/src/builtins/mini_unset.c index 9554e27..5bec2a5 100644 --- a/src/builtins/mini_unset.c +++ b/src/builtins/mini_unset.c @@ -6,7 +6,7 @@ /* By: maiadegraaf envp[i + j] = tools->envp[i + j + 1]; j++; } + tools->envp[i + j] = NULL; return (1); } i++; diff --git a/src/main.c b/src/main.c index 1cdb498..146e6df 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/22 18:08:51 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/23 15:17:36 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,17 +14,21 @@ int minishell_loop(t_tools *tools); int implement_tools(t_tools *tools); +char **ft_arrdup(char **arr); int main(int argc, char **argv, char **envp) { t_tools tools; + int i; + i = 0; if (argc != 1 || argv[1]) { printf("This program does not accept arguments\n"); exit(0); } - tools.envp = envp; + tools.envp = ft_arrdup(envp); + // printf("%s\n\n", tools.envp[0]) parse_envp(&tools); implement_tools(&tools); minishell_loop(&tools); diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index e4a1c7c..21f21f5 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -14,12 +14,15 @@ int implement_tools(t_tools *tools) int reset_tools(t_tools *tools) { + builtin_arr(tools->simple_cmds->str[0])(tools, tools->simple_cmds); ft_simple_cmdsclear(&tools->simple_cmds); free(tools->args); + free_arr(tools->envp); implement_tools(tools); tools->pipes = 0; system("leaks minishell"); minishell_loop(tools); + // exit (EXIT_SUCCESS); return (1); } diff --git a/src/utils/parse_envp.c b/src/utils/parse_envp.c index ac1a2d4..80b2da6 100644 --- a/src/utils/parse_envp.c +++ b/src/utils/parse_envp.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2021/12/17 16:16:57 by mgraaf #+# #+# */ -/* Updated: 2022/03/17 11:41:00 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/23 15:01:24 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/utils/utils.c b/src/utils/utils.c index 151304d..987b95e 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/21 11:17:26 by fpolycar #+# #+# */ -/* Updated: 2022/03/16 15:07:23 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/23 16:03:06 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -45,3 +45,28 @@ int count_quotes(char *line) return (0); return (1); } + +char **ft_arrdup(char **arr) +{ + char **rtn; + size_t i; + + i = 0; + while (arr[i] != NULL) + i++; + rtn = ft_calloc(sizeof(char *), i + 1); + if (!rtn) + return (NULL); + i = 0; + while (arr[i] != NULL) + { + rtn[i] = ft_strdup(arr[i]); + if (rtn[i] == NULL) + { + free_arr(rtn); + return (rtn); + } + i++; + } + return (rtn); +} \ No newline at end of file From 55dafa77d076b7ec9625f3e2952dc116a27a6afd Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Thu, 24 Mar 2022 16:10:24 +0100 Subject: [PATCH 086/163] builtins done -> leaks and norm --- includes/builtins.h | 4 +-- src/builtins/builtins.c | 10 ++++-- src/builtins/mini_cd.c | 52 ++++++++++++++------------- src/builtins/mini_env.c | 18 +++++----- src/builtins/mini_export.c | 67 ++++++++++++++++++----------------- src/builtins/mini_unset.c | 53 +++++++++++++++++++-------- src/builtins/utils_builtins.c | 27 ++++++++++++++ src/main.c | 7 ++-- src/utils/minishell_loop.c | 16 +++++++-- src/utils/utils.c | 4 +-- 10 files changed, 164 insertions(+), 94 deletions(-) create mode 100644 src/builtins/utils_builtins.c diff --git a/includes/builtins.h b/includes/builtins.h index 9452761..ed72a70 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:20:00 by mgraaf #+# #+# */ -/* Updated: 2022/03/17 10:14:05 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/24 13:08:38 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,6 +34,6 @@ int mini_exit(t_tools *tools, t_simple_cmds *simple_cmd); int mini_continue(t_tools *tools, t_simple_cmds *simple_cmd); -int equal_sign(char *str); +size_t equal_sign(char *str); #endif \ No newline at end of file diff --git a/src/builtins/builtins.c b/src/builtins/builtins.c index 7ec2b36..ba0040e 100644 --- a/src/builtins/builtins.c +++ b/src/builtins/builtins.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 11:42:32 by mgraaf #+# #+# */ -/* Updated: 2022/03/17 13:34:25 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/24 16:09:37 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,9 +14,13 @@ void change_path(t_tools *tools) { - tools->old_pwd = tools->pwd; + char *tmp; + + tmp = ft_strdup(tools->pwd); + free(tools->old_pwd); + tools->old_pwd = tmp; + free(tools->pwd); tools->pwd = getcwd(NULL, sizeof(NULL)); - } int (*builtin_arr(char *str))(t_tools *tools, t_simple_cmds *simple_cmd) diff --git a/src/builtins/mini_cd.c b/src/builtins/mini_cd.c index 753b3e1..58a4e5d 100644 --- a/src/builtins/mini_cd.c +++ b/src/builtins/mini_cd.c @@ -6,58 +6,62 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:17:04 by mgraaf #+# #+# */ -/* Updated: 2022/03/17 11:16:13 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/24 15:43:08 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ // #include "minishell.h" #include "builtins.h" -char *find_path_ret(char *str, t_tools *tools) +char *find_path_ret(char *str, t_tools *tools) { - int i; + int i; i = 0; while (tools->envp[i]) { if (!ft_strncmp(tools->envp[i], str, ft_strlen(str))) - return (ft_substr(tools->envp[i], ft_strlen(str), ft_strlen(tools->envp[i]) - ft_strlen(str))); + return (ft_substr(tools->envp[i], ft_strlen(str), + ft_strlen(tools->envp[i]) - ft_strlen(str))); i++; } return (NULL); } -int mini_cd(t_tools *tools, t_simple_cmds *simple_cmd) +int specific_path(t_tools *tools, char *str) { - int ret; + char *tmp; + int ret; - if (!simple_cmd->str[1]) + tmp = find_path_ret(str, tools); + ret = chdir(tmp); + free(tmp); + if (ret != 0) { - ret = chdir(find_path_ret("HOME=", tools)); - if (ret != 0) - { - ft_putendl_fd("HOME not set", STDERR_FILENO); - return (EXIT_FAILURE); - } + str = ft_substr(str, 0, ft_strlen(str) - 1); + ft_putstr_fd(str, STDERR_FILENO); + free(str); + ft_putendl_fd(" not set", STDERR_FILENO); } + return (ret); +} + +int mini_cd(t_tools *tools, t_simple_cmds *simple_cmd) +{ + int ret; + + if (!simple_cmd->str[1]) + ret = specific_path(tools, "HOME="); else if (ft_strncmp(simple_cmd->str[1], "-", 1) == 0) - { - ret = chdir(find_path_ret("OLDPWD=", tools)); - if (ret != 0) - { - ft_putendl_fd("OLDPWD not set", STDERR_FILENO); - return (EXIT_FAILURE); - } - } + ret = specific_path(tools, "OLDPWD="); else { ret = chdir(simple_cmd->str[1]); if (ret != 0) - { ft_putendl_fd("Path do not exist", STDERR_FILENO); - return (EXIT_FAILURE); - } } + if (ret != 0) + return (EXIT_FAILURE); change_path(tools); return (EXIT_SUCCESS); } diff --git a/src/builtins/mini_env.c b/src/builtins/mini_env.c index 9893ebd..4dfa102 100644 --- a/src/builtins/mini_env.c +++ b/src/builtins/mini_env.c @@ -6,7 +6,7 @@ /* By: maiadegraaf envp[i], "PWD=", 4)) { - ft_putstr_fd("PWD=", STDOUT_FILENO); - ft_putendl_fd(tools->pwd, STDOUT_FILENO); + tmp = ft_strjoin("PWD=", tools->pwd); + free(tools->envp[i]); + tools->envp[i] = tmp; } else if (!ft_strncmp(tools->envp[i], "OLDPWD=", 7) && tools->old_pwd) { - ft_putstr_fd("OLDPWD=", STDOUT_FILENO); - ft_putendl_fd(tools->old_pwd, STDOUT_FILENO); + tmp = ft_strjoin("OLDPWD=", tools->old_pwd); + free(tools->envp[i]); + tools->envp[i] = tmp; } - else - ft_putendl_fd(tools->envp[i], STDOUT_FILENO); + ft_putendl_fd(tools->envp[i], STDOUT_FILENO); i++; } return (EXIT_SUCCESS); diff --git a/src/builtins/mini_export.c b/src/builtins/mini_export.c index 2d61162..61d8239 100644 --- a/src/builtins/mini_export.c +++ b/src/builtins/mini_export.c @@ -3,37 +3,24 @@ /* :::::::: */ /* mini_export.c :+: :+: */ /* +:+ */ -/* By: maiadegraaf +#+ */ /* +#+ */ -/* Created: 2022/02/17 10:11:56 by maiadegraaf #+# #+# */ -/* Updated: 2022/03/23 16:15:26 by fpolycar ######## odam.nl */ +/* Created: 2022/03/24 16:07:21 by fpolycar #+# #+# */ +/* Updated: 2022/03/24 16:07:25 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "builtins.h" -int equal_sign(char *str) -{ - int i; - - i = 0; - while (str[i]) - { - if (str[i] == '=') - return (i); - i++; - } - return (0); -} - -int variable_exist(t_tools *tools, t_simple_cmds *simple_cmd) +int variable_exist(t_tools *tools, t_simple_cmds *simple_cmd) { int i; i = 0; while (tools->envp[i]) { - if (ft_strncmp(tools->envp[i], simple_cmd->str[1], equal_sign(tools->envp[i])) == 0) + if (ft_strncmp(tools->envp[i], + simple_cmd->str[1], equal_sign(tools->envp[i])) == 0) { free(tools->envp[i]); tools->envp[i] = ft_strdup(simple_cmd->str[1]); @@ -44,9 +31,9 @@ int variable_exist(t_tools *tools, t_simple_cmds *simple_cmd) return (0); } -int check_parameter(char *str) +int check_parameter(char *str) { - int i; + int i; i = 0; if (ft_isdigit(str[0])) @@ -62,21 +49,20 @@ int check_parameter(char *str) return (0); } -char **add_var(char **arr, char *str) +char **whileloop_add_var(char **arr, char **rtn, char *str) { - char **rtn; - size_t i; + int i; i = 0; while (arr[i] != NULL) - i++; - rtn = ft_calloc(sizeof(char *), i + 2); - if (!rtn) - return (NULL); - i = 0; - while (arr[i + 1] != NULL) { - rtn[i] = ft_strdup(arr[i]); + if (arr[i + 1] == NULL) + { + rtn[i] = ft_strdup(str); + rtn[i + 1] = ft_strdup(arr[i]); + } + else + rtn[i] = ft_strdup(arr[i]); if (rtn[i] == NULL) { free_arr(rtn); @@ -84,7 +70,22 @@ char **add_var(char **arr, char *str) } i++; } - rtn[i] = ft_strdup(str); + return (rtn); +} + +char **add_var(char **arr, char *str) +{ + char **rtn; + size_t i; + + i = 0; + while (arr[i] != NULL) + i++; + rtn = ft_calloc(sizeof(char *), i + 2); + if (!rtn) + return (NULL); + i = 0; + whileloop_add_var(arr, rtn, str); return (rtn); } @@ -94,7 +95,7 @@ int mini_export(t_tools *tools, t_simple_cmds *simple_cmd) if (!simple_cmd->str[1]) mini_env(tools, simple_cmd); - else + else { if (check_parameter(simple_cmd->str[1]) == 0) { diff --git a/src/builtins/mini_unset.c b/src/builtins/mini_unset.c index 5bec2a5..1004d48 100644 --- a/src/builtins/mini_unset.c +++ b/src/builtins/mini_unset.c @@ -6,45 +6,68 @@ /* By: maiadegraaf envp[i]) + j = 0; + while (arr[i] != NULL) { - if (ft_strncmp(tools->envp[i], simple_cmd->str[1], equal_sign(tools->envp[i])) == 0) + if (!(ft_strncmp(arr[i], str, equal_sign(arr[i]) - 1) == 0 + && str[equal_sign(arr[i])] == '\0' + && arr[i][ft_strlen(str)] == '=')) { - j = 0; - while (tools->envp[i + j]) + rtn[j] = ft_strdup(arr[i]); + if (rtn[j] == NULL) { - tools->envp[i + j] = tools->envp[i + j + 1]; - j++; + free_arr(rtn); + return (rtn); } - tools->envp[i + j] = NULL; - return (1); - } + j++; + } i++; } - return (0); + return (rtn); +} + +char **del_var(char **arr, char *str) +{ + char **rtn; + size_t i; + size_t j; + + i = 0; + j = 0; + while (arr[i] != NULL) + i++; + rtn = ft_calloc(sizeof(char *), i + 1); + if (!rtn) + return (NULL); + rtn = whileloop_del_var(arr, rtn, str); + return (rtn); } int mini_unset(t_tools *tools, t_simple_cmds *simple_cmd) { - int exist; + char **tmp; if (!simple_cmd->str[1]) ft_putendl_fd("unset: not enough arguments", STDERR_FILENO); if (equal_sign(simple_cmd->str[1]) != 0) ft_putendl_fd("unset: invalid parameter name", STDERR_FILENO); else - exist = variable_exist_del(tools, simple_cmd); + { + tmp = del_var(tools->envp, simple_cmd->str[1]); + free_arr(tools->envp); + tools->envp = tmp; + } return (EXIT_SUCCESS); } diff --git a/src/builtins/utils_builtins.c b/src/builtins/utils_builtins.c new file mode 100644 index 0000000..35801d9 --- /dev/null +++ b/src/builtins/utils_builtins.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* utils.c :+: :+: */ +/* +:+ */ +/* By: fpolycar +#+ */ +/* +#+ */ +/* Created: 2022/03/24 16:04:47 by fpolycar #+# #+# */ +/* Updated: 2022/03/24 16:06:09 by fpolycar ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "builtins.h" + +size_t equal_sign(char *str) +{ + size_t i; + + i = 0; + while (str[i]) + { + if (str[i] == '=') + return (i + 1); + i++; + } + return (0); +} diff --git a/src/main.c b/src/main.c index 146e6df..02d8836 100644 --- a/src/main.c +++ b/src/main.c @@ -6,14 +6,14 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/23 15:17:36 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/24 15:44:21 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -int minishell_loop(t_tools *tools); -int implement_tools(t_tools *tools); +int minishell_loop(t_tools *tools); +int implement_tools(t_tools *tools); char **ft_arrdup(char **arr); int main(int argc, char **argv, char **envp) @@ -28,7 +28,6 @@ int main(int argc, char **argv, char **envp) exit(0); } tools.envp = ft_arrdup(envp); - // printf("%s\n\n", tools.envp[0]) parse_envp(&tools); implement_tools(&tools); minishell_loop(&tools); diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 21f21f5..409404b 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -1,3 +1,15 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* minishell_loop.c :+: :+: */ +/* +:+ */ +/* By: fpolycar +#+ */ +/* +#+ */ +/* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ +/* Updated: 2022/03/24 16:06:59 by fpolycar ######## odam.nl */ +/* */ +/* ************************************************************************** */ + #include "minishell.h" int minishell_loop(t_tools *tools); @@ -14,15 +26,12 @@ int implement_tools(t_tools *tools) int reset_tools(t_tools *tools) { - builtin_arr(tools->simple_cmds->str[0])(tools, tools->simple_cmds); ft_simple_cmdsclear(&tools->simple_cmds); free(tools->args); - free_arr(tools->envp); implement_tools(tools); tools->pipes = 0; system("leaks minishell"); minishell_loop(tools); - // exit (EXIT_SUCCESS); return (1); } @@ -36,6 +45,7 @@ int minishell_loop(t_tools *tools) if (!token_reader(tools)) ft_error(1, tools); parser(tools); + builtin_arr(tools->simple_cmds->str[0])(tools, tools->simple_cmds); // ft_lexorclear(&lexor_list); // executor(&tools); reset_tools(tools); diff --git a/src/utils/utils.c b/src/utils/utils.c index 987b95e..7292362 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/21 11:17:26 by fpolycar #+# #+# */ -/* Updated: 2022/03/23 16:03:06 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/24 16:08:31 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -69,4 +69,4 @@ char **ft_arrdup(char **arr) i++; } return (rtn); -} \ No newline at end of file +} From 1b78020493fd9019eadd0d5e70ea25d0513b6bf8 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Fri, 25 Mar 2022 12:49:21 +0100 Subject: [PATCH 087/163] start expander --- Makefile | 8 +++- includes/minishell.h | 3 +- src/error/error_handeling.c | 4 +- src/expander/expander.c | 48 ++++++++++++++++++--- src/parser/parser.c | 74 +++++++++++++++++---------------- src/utils/minishell_loop.c | 5 ++- src/utils/t_simple_cmds_utils.c | 2 +- 7 files changed, 97 insertions(+), 47 deletions(-) diff --git a/Makefile b/Makefile index 8959ab8..ad248cd 100644 --- a/Makefile +++ b/Makefile @@ -11,6 +11,7 @@ PATHS = src/ PATHSL = src/lexor/ PATHSP = src/parser/ PATHSB = src/builtins/ +PATHSEX = src/expander/ PATHSU = src/utils/ PATHSE = src/error/ PATHP = src/pipex/ @@ -24,7 +25,8 @@ src = $(wildcard $(PATHS)*.c) \ $(wildcard $(PATHSB)*.c) \ $(wildcard $(PATHSU)*.c) \ $(wildcard $(PATHSE)*.c) \ - $(wildcard $(PATHEX)*.c) + $(wildcard $(PATHEX)*.c) \ + $(wildcard $(PATHSEX)*.c) OBJS = $(addprefix $(PATHO), $(notdir $(patsubst %.c, %.o, $(src)))) @@ -55,6 +57,10 @@ $(PATHO)%.o:: $(PATHSB)%.c $(HEADERS) @echo "Compiling ${notdir $<} in $(PATHSB)" @$(CC) -c $(FLAGS) $(INCLUDES) $< -o $@ +$(PATHO)%.o:: $(PATHSEX)%.c $(HEADERS) + @echo "Compiling ${notdir $<} in $(PATHSEX)" + @$(CC) -c $(FLAGS) $(INCLUDES) $< -o $@ + $(PATHO)%.o:: $(PATHSU)%.c $(HEADERS) @echo "Compiling ${notdir $<} in $(PATHSU)" @$(CC) -c $(FLAGS) $(INCLUDES) $< -o $@ diff --git a/includes/minishell.h b/includes/minishell.h index 68fc12a..d50ea91 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/03/16 15:25:04 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/25 11:41:03 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -29,6 +29,7 @@ int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); int reset_tools(t_tools *tools); +void expander(t_tools *tools, t_simple_cmds *simple_cmds); //builtins int (*builtin_arr(char *str))(t_tools *tools, t_simple_cmds *simple_cmd); diff --git a/src/error/error_handeling.c b/src/error/error_handeling.c index b6a1242..5444ea8 100755 --- a/src/error/error_handeling.c +++ b/src/error/error_handeling.c @@ -6,7 +6,7 @@ /* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/03/15 13:44:22 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/25 12:46:50 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ -#include +#include "minishell.h" +#include "builtins.h" -void expander(void) +void expander_loop(t_tools *tools, t_simple_cmds *tmp, int i) { - -} \ No newline at end of file + int j; + + j = 0; + while (tools->envp[j]) + { + if (ft_strncmp(tmp->str[i] + 1, + tools->envp[j], equal_sign(tools->envp[j]) - 1) == 0 + && equal_sign(tools->envp[j]) + == ft_strlen(tmp->str[i])) + { + free(tmp->str[i]); + tmp->str[i] = ft_substr(tools->envp[j], + equal_sign(tools->envp[j]), + ft_strlen(tools->envp[j])); + } + j++; + } +} + +void expander(t_tools *tools, t_simple_cmds *simple_cmds) +{ + t_simple_cmds *tmp; + int i; + + i = 0; + tmp = simple_cmds; + while (tmp) + { + while (tmp->str[i]) + { + if (tmp->str[i][0] == '$') + { + expander_loop(tools, tmp, i); + } + i++; + } + tmp = tmp->next; + } +} diff --git a/src/parser/parser.c b/src/parser/parser.c index 7cb4037..bcb770d 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,13 +6,13 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/22 17:51:13 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/25 10:38:09 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -void print_parser(t_simple_cmds *simple_cmds); +void print_parser(t_simple_cmds simple_cmds); t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) { @@ -63,40 +63,42 @@ int *parser(t_tools *tools) ft_simple_cmdsadd_back(&tools->simple_cmds, node); tools->lexor_list = parser_tools.lexor_list; } + print_parser(*tools->simple_cmds); return (0); } -// void print_parser(t_simple_cmds *simple_cmds) -// { -// int i = 0; -// t_simple_cmds *tmp = simple_cmds; -// while (tmp) -// { -// printf("\n>>>%i<<<\n", i++); -// if (*tmp->str) -// { -// while (*tmp->str) -// { -// printf("%s\n", *tmp->str++); -// } -// } -// if (tmp->redirections) -// printf("\nredirections:\n"); -// while (tmp->redirections) -// { -// printf("%s\t", tmp->redirections->str); -// if (tmp->redirections->token == GREAT) -// printf("GREAT\n"); -// else if (tmp->redirections->token == GREAT_GREAT) -// printf("GREAT_GREAT\n"); -// else if (tmp->redirections->token == LESS) -// printf("LESS\n"); -// else if (tmp->redirections->token == LESS_LESS) -// printf("LESS_LESS\n"); -// tmp->redirections = tmp->redirections->next; -// } -// if (tmp->builtin) -// printf("BUILTIN :)\n"); -// tmp = tmp->next; -// } -// } +void print_parser(t_simple_cmds simple_cmds) +{ + int i = 0; + + t_simple_cmds *tmp = &simple_cmds; + while (tmp) + { + printf("\n>>>%i<<<\n", i++); + if (*tmp->str) + { + while (*tmp->str) + { + printf("%s\n", *tmp->str++); + } + } + if (tmp->redirections) + printf("\nredirections:\n"); + while (tmp->redirections) + { + printf("%s\t", tmp->redirections->str); + if (tmp->redirections->token == GREAT) + printf("GREAT\n"); + else if (tmp->redirections->token == GREAT_GREAT) + printf("GREAT_GREAT\n"); + else if (tmp->redirections->token == LESS) + printf("LESS\n"); + else if (tmp->redirections->token == LESS_LESS) + printf("LESS_LESS\n"); + tmp->redirections = tmp->redirections->next; + } + if (tmp->builtin) + printf("BUILTIN :)\n"); + tmp = tmp->next; + } +} diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 409404b..81ca930 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/03/24 16:06:59 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/25 12:03:41 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -45,7 +45,8 @@ int minishell_loop(t_tools *tools) if (!token_reader(tools)) ft_error(1, tools); parser(tools); - builtin_arr(tools->simple_cmds->str[0])(tools, tools->simple_cmds); + expander(tools, tools->simple_cmds); + printf("%s\n", *tools->simple_cmds->str); // ft_lexorclear(&lexor_list); // executor(&tools); reset_tools(tools); diff --git a/src/utils/t_simple_cmds_utils.c b/src/utils/t_simple_cmds_utils.c index 0b3bbda..28d272f 100644 --- a/src/utils/t_simple_cmds_utils.c +++ b/src/utils/t_simple_cmds_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/03/22 17:11:04 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/25 10:10:24 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ From fdf3a3ec48cbc2cbb6e749d98082a8d9a3e07c35 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Fri, 25 Mar 2022 15:58:08 +0100 Subject: [PATCH 088/163] expander problem with multiple var --- Makefile | 2 +- src/expander/expander.c | 69 +++++++++++++++++++++++++++++--------- src/utils/minishell_loop.c | 5 ++- 3 files changed, 57 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index ad248cd..611abe3 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ src = $(wildcard $(PATHS)*.c) \ OBJS = $(addprefix $(PATHO), $(notdir $(patsubst %.c, %.o, $(src)))) -FLAGS = -Wall -Werror -Wextra -g +FLAGS = #-Wall -Werror -Wextra -g #-fsanitize=address LIBFT = ./libraries/libft/libft.a diff --git a/src/expander/expander.c b/src/expander/expander.c index 3743f11..b36b583 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,29 +6,71 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/03/25 12:46:50 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/25 15:57:37 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" #include "builtins.h" +size_t dollar_sign(char *str) +{ + size_t i; + + i = 0; + while (str[i]) + { + if (str[i] == '$') + return (i + 1); + i++; + } + return (0); +} + void expander_loop(t_tools *tools, t_simple_cmds *tmp, int i) { - int j; + int j; + int k; + char *arr_tmp; + char *before; + char *after; j = 0; - while (tools->envp[j]) + k = 0; + arr_tmp = tmp->str[i]; + while (arr_tmp[j]) { - if (ft_strncmp(tmp->str[i] + 1, - tools->envp[j], equal_sign(tools->envp[j]) - 1) == 0 - && equal_sign(tools->envp[j]) - == ft_strlen(tmp->str[i])) + if (arr_tmp[j] == '$') { - free(tmp->str[i]); - tmp->str[i] = ft_substr(tools->envp[j], - equal_sign(tools->envp[j]), - ft_strlen(tools->envp[j])); + while (tools->envp[k]) + { + if (ft_strncmp(arr_tmp + j + 1, + tools->envp[k], equal_sign(tools->envp[k]) - 1) == 0 + && (arr_tmp[equal_sign(tools->envp[k])] == '\0' + || arr_tmp[equal_sign(tools->envp[k])] == '$')) + { + + // if (j == 0) + // { + // free(tmp->str[i]); + // tmp->str[i] = ft_substr(tools->envp[k], + // equal_sign(tools->envp[k]), + // ft_strlen(tools->envp[k])); + // } + // // // printf("test\n"); + // // // before = ft_substr(arr_tmp, 0, dollar_sign(arr_tmp)); + // else + // { + // before = tmp->str[i]; + // free(tmp->str[i]); + // tmp->str[i] = ft_strjoin(before, ft_substr(tools->envp[j], + // equal_sign(tools->envp[j]), + // ft_strlen(tools->envp[j]))); + // } + printf("%s\n\n", arr_tmp); + } + k++; + } } j++; } @@ -45,10 +87,7 @@ void expander(t_tools *tools, t_simple_cmds *simple_cmds) { while (tmp->str[i]) { - if (tmp->str[i][0] == '$') - { - expander_loop(tools, tmp, i); - } + expander_loop(tools, tmp, i); i++; } tmp = tmp->next; diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 81ca930..6769c41 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/03/25 12:03:41 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/25 14:51:48 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -30,7 +30,7 @@ int reset_tools(t_tools *tools) free(tools->args); implement_tools(tools); tools->pipes = 0; - system("leaks minishell"); + // system("leaks minishell"); minishell_loop(tools); // exit (EXIT_SUCCESS); return (1); @@ -47,7 +47,6 @@ int minishell_loop(t_tools *tools) parser(tools); expander(tools, tools->simple_cmds); printf("%s\n", *tools->simple_cmds->str); - // ft_lexorclear(&lexor_list); // executor(&tools); reset_tools(tools); return (1); From adb8672d85d6165ce520297c95425f7af4f0aa52 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Mon, 28 Mar 2022 12:50:36 +0200 Subject: [PATCH 089/163] fixed whitespace check --- src/lexor/token_reader.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/lexor/token_reader.c b/src/lexor/token_reader.c index ba22a12..456a7d7 100644 --- a/src/lexor/token_reader.c +++ b/src/lexor/token_reader.c @@ -6,18 +6,23 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:11:20 by mgraaf #+# #+# */ -/* Updated: 2022/03/16 15:03:44 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/28 11:47:14 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "lexor.h" +int is_whitespace(char c) +{ + return (c == ' ' || (c > 8 && c < 14)); +} + int skip_spaces(char *str, int i) { int j; j = 0; - while (str[i + j] == ' ') + while (is_whitespace(str[i + j])) j++; return (j); } From cca7e7a633d562d686855e68176a7ec73ac0e258 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Mon, 28 Mar 2022 12:52:40 +0200 Subject: [PATCH 090/163] +++ --- src/parser/parser.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/parser/parser.c b/src/parser/parser.c index bcb770d..3dfe22f 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/25 10:38:09 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/28 12:51:16 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ From bfae42674a9a4747543cbdef96abcd0a56211c30 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Mon, 28 Mar 2022 17:54:36 +0200 Subject: [PATCH 091/163] tester stuff --- src/expander/expander.c | 4 +- src/lexor/token_reader.c | 4 +- src/parser/parser.c | 5 +- src/utils/minishell_loop.c | 6 +- test/Makefile | 19 +++--- test/Testparser.c | 130 ++++++++++++++++++++++++++++++++----- test/Testtoken_reader.c | 14 +++- test/read_tester_output.sh | 22 +++++++ 8 files changed, 169 insertions(+), 35 deletions(-) create mode 100755 test/read_tester_output.sh diff --git a/src/expander/expander.c b/src/expander/expander.c index b36b583..605917f 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/03/25 15:57:37 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/28 15:35:09 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ void expander_loop(t_tools *tools, t_simple_cmds *tmp, int i) int j; int k; char *arr_tmp; - char *before; + char *before; char *after; j = 0; diff --git a/src/lexor/token_reader.c b/src/lexor/token_reader.c index 456a7d7..976f79d 100644 --- a/src/lexor/token_reader.c +++ b/src/lexor/token_reader.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:11:20 by mgraaf #+# #+# */ -/* Updated: 2022/03/28 11:47:14 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/28 12:57:33 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -47,7 +47,7 @@ int read_words(int i, char *str, t_lexor **lexor_list) { j += handle_quotes_inside_word(i + j, str, 34); j += handle_quotes_inside_word(i + j, str, 39); - if (str[i + j] == ' ') + if (is_whitespace(str[i + j])) break ; else j++; diff --git a/src/parser/parser.c b/src/parser/parser.c index 3dfe22f..ff261db 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/28 12:51:16 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/28 15:35:53 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -63,7 +63,7 @@ int *parser(t_tools *tools) ft_simple_cmdsadd_back(&tools->simple_cmds, node); tools->lexor_list = parser_tools.lexor_list; } - print_parser(*tools->simple_cmds); + // print_parser(*tools->simple_cmds); return (0); } @@ -82,6 +82,7 @@ void print_parser(t_simple_cmds simple_cmds) printf("%s\n", *tmp->str++); } } + printf("num redirections = %d\n", simple_cmds.num_redirections); if (tmp->redirections) printf("\nredirections:\n"); while (tmp->redirections) diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 6769c41..29e03ed 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/03/25 14:51:48 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/28 12:56:13 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -45,8 +45,8 @@ int minishell_loop(t_tools *tools) if (!token_reader(tools)) ft_error(1, tools); parser(tools); - expander(tools, tools->simple_cmds); - printf("%s\n", *tools->simple_cmds->str); + // expander(tools, tools->simple_cmds); + // printf("%s\n", *tools->simple_cmds->str); // executor(&tools); reset_tools(tools); return (1); diff --git a/test/Makefile b/test/Makefile index 5c28446..cf67fab 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,8 +1,11 @@ CLEANUP=rm -f MKDIR=mkdir -p TARGET_EXTENSION=out +UNITY_CONFIG_DEFINES = -D UNITY_OUTPUT_COLOR \ + -D UNITY_FIXTURE_NO_EXTRAS .PHONY: clean +.PHONY: re .PHONY: test PATHLIBFT = ../libraries/libft @@ -67,28 +70,28 @@ $(PATHO)Test%.$(TARGET_EXTENSION): $(LIBFT) $(OBJS) $(PATHO)Test%.o $(PATHO)%.o $(LINK) -lreadline -o $@ $^ $(PATHO)%.o:: $(PATHT)%.c $(HEADERS) - $(COMPILE) $(CFLAGS) $< -o $@ + $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ # $(PATHO)%.o:: $(PATHS)%.c $(HEADERS) -# $(COMPILE) $(CFLAGS) $< -o $@ +# $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ $(PATHO)%.o:: $(PATHSL)%.c $(HEADERS) - $(COMPILE) $(CFLAGS) $< -o $@ + $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ $(PATHO)%.o:: $(PATHSP)%.c $(HEADERS) - $(COMPILE) $(CFLAGS) $< -o $@ + $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ $(PATHO)%.o:: $(PATHSU)%.c $(HEADERS) - $(COMPILE) $(CFLAGS) $< -o $@ + $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ $(PATHO)%.o:: $(PATHSB)%.c $(HEADERS) - $(COMPILE) $(CFLAGS) $< -o $@ + $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ $(PATHO)%.o:: $(PATHSE)%.c $(HEADERS) - $(COMPILE) $(CFLAGS) $< -o $@ + $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ $(PATHO)%.o:: $(PATHU)%.c $(PATHU)%.h - $(COMPILE) $(CFLAGS) $< -o $@ + $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ $(PATHB): $(MKDIR) $(PATHB) diff --git a/test/Testparser.c b/test/Testparser.c index f002164..aef4d4f 100644 --- a/test/Testparser.c +++ b/test/Testparser.c @@ -3,8 +3,6 @@ #include t_tools test_tools; -t_lexor *test_lexor; -t_simple_cmds *test_simple_cmds; void setUp(void) { @@ -28,27 +26,49 @@ void assert_parser(char **expected, char *builtin, int num_directions, t_lexor * int i; i = 0; - while (expected[i]) + if (expected) { - TEST_ASSERT_EQUAL_STRING_MESSAGE(expected[i], test_tools.simple_cmds->str[i], "STR CHECK\t"); - i++; + while (expected[i]) + { + TEST_ASSERT_EQUAL_STRING_MESSAGE(expected[i], test_tools.simple_cmds->str[i], "Incorrect: STR"); + i++; + } } if (builtin) - TEST_ASSERT_NOT_NULL_MESSAGE(test_tools.simple_cmds->builtin, "BUILTIN CHECK\t"); + TEST_ASSERT_NOT_NULL_MESSAGE(test_tools.simple_cmds->builtin, "Incorrect: BUILTIN"); else TEST_ASSERT_NULL(test_tools.simple_cmds->builtin); - TEST_ASSERT_EQUAL_INT_MESSAGE(num_directions, test_tools.simple_cmds->num_redirections, "NUM REDIRECTIONS CHECK\t"); - // if (expected_redirection) - // { - // printf("%s\n", test_tools.simple_cmds->redirections->str); - // // TEST_ASSERT_EQUAL_STRING(expected_redirection->str, test_tools.simple_cmds->redirections->str); - // // TEST_ASSERT_EQUAL_INT(expected_redirection->token, test_tools.simple_cmds->redirections->token); - // } + // printf("num redirections = >%d<\t|\t", num_directions); + // printf(">%d<\n", test_tools.simple_cmds->num_redirections); + TEST_ASSERT_EQUAL_INT_MESSAGE(num_directions, test_tools.simple_cmds->num_redirections, "Incorrect: NUM REDIRECTIONS"); + if (expected_redirection) + { + t_lexor *start = expected_redirection; + while (expected_redirection) + { + // printf("%s\t", expected_redirection->str); + // if (expected_redirection->token == GREAT) + // printf("GREAT\n"); + // else if (expected_redirection->token == GREAT_GREAT) + // printf("GREAT_GREAT\n"); + // else if (expected_redirection->token == LESS) + // printf("LESS\n"); + // else if (expected_redirection->token == LESS_LESS) + // printf("LESS_LESS\n"); + TEST_ASSERT_EQUAL_STRING(expected_redirection->str, test_tools.simple_cmds->redirections->str); + TEST_ASSERT_EQUAL_INT(expected_redirection->token, test_tools.simple_cmds->redirections->token); + test_tools.simple_cmds->redirections = test_tools.simple_cmds->redirections->next; + expected_redirection = expected_redirection->next; + } + // printf("%s\n", start->str); + // ft_lexorclear(&start); + } test_tools.simple_cmds = test_tools.simple_cmds->next; } void test_parser_1(void) { + TEST_MESSAGE("1: {test test}"); init_test("test test"); assert_parser(ft_split("test test", ' '), NULL, 0, NULL); ft_simple_cmdsclear(&test_tools.simple_cmds); @@ -56,6 +76,7 @@ void test_parser_1(void) void test_parser_2(void) { + TEST_MESSAGE("2: {test test | test}"); init_test("test test | test"); assert_parser(ft_split("test test", ' '), NULL, 0, NULL); assert_parser(ft_split("test", ' '), NULL, 0, NULL); @@ -64,15 +85,17 @@ void test_parser_2(void) void test_parser_3(void) { - init_test("test test | test | 'test'"); + TEST_MESSAGE("3: {test test | test | \'test\'}"); + init_test("test test | test | \'test\'"); assert_parser(ft_split("test test", ' '), NULL, 0, NULL); assert_parser(ft_split("test", ' '), NULL, 0, NULL); - assert_parser(ft_split("'test'", ' '), NULL, 0, NULL); + assert_parser(ft_split("\'test\'", ' '), NULL, 0, NULL); ft_simple_cmdsclear(&test_tools.simple_cmds); } void test_parser_4(void) { + TEST_MESSAGE("4: {< redirection}"); init_test("< redirection"); assert_parser(NULL, NULL, 1, ft_lexornew("redirection", LESS)); ft_simple_cmdsclear(&test_tools.simple_cmds); @@ -80,18 +103,91 @@ void test_parser_4(void) void test_parser_5(void) { + TEST_MESSAGE("5: {test > redirection}"); init_test("test > redirection"); assert_parser(ft_split("test", ' '), NULL, 1, ft_lexornew("redirection", GREAT)); ft_simple_cmdsclear(&test_tools.simple_cmds); } +void test_parser_6(void) +{ + TEST_MESSAGE("6: {test > redirection < redirection >> redirection}"); + init_test("test > redirection < redirection >> redirection"); + t_lexor *lst= ft_lexornew("redirection", GREAT); + ft_lexoradd_back(&lst, ft_lexornew("redirection", LESS)); + ft_lexoradd_back(&lst, ft_lexornew("redirection", GREAT_GREAT)); + assert_parser(ft_split("test", ' '), NULL, 3, lst); + ft_simple_cmdsclear(&test_tools.simple_cmds); +} + +void test_parser_7(void) +{ + TEST_MESSAGE("7: {test > redirection < redirection | heredoc << EOF}"); + init_test("test > redirection < redirection | heredoc << EOF"); + t_lexor *lst = ft_lexornew("redirection", GREAT); + ft_lexoradd_back(&lst, ft_lexornew("redirection", LESS)); + assert_parser(ft_split("test", ' '), NULL, 2, lst); + assert_parser(NULL, NULL, 1, ft_lexornew("heredoc|EOF", LESS_LESS)); + ft_simple_cmdsclear(&test_tools.simple_cmds); +} + +void test_parser_8(void) +{ + TEST_MESSAGE("8: {test > redirection < redirection | heredoc < redirection >> redirection << EOF}"); + init_test("test > redirection < redirection | heredoc < redirection >> redirection << EOF"); + t_lexor *lst = ft_lexornew("redirection", GREAT); + ft_lexoradd_back(&lst, ft_lexornew("redirection", LESS)); + assert_parser(ft_split("test", ' '), NULL, 2, lst); + t_lexor *lst2 = ft_lexornew("redirection", LESS); + ft_lexoradd_back(&lst2, ft_lexornew("redirection", GREAT_GREAT)); + ft_lexoradd_back(&lst2, ft_lexornew("heredoc|EOF", LESS_LESS)); + assert_parser(NULL, NULL, 3, lst2); + ft_simple_cmdsclear(&test_tools.simple_cmds); +} + +void test_parser_9(void) +{ + TEST_MESSAGE("9: {cat test | test < redirection | echo test}"); + init_test("cat test | test < redirection | echo test"); + assert_parser(ft_split("cat test", ' '), NULL, 0, NULL); + assert_parser(ft_split("test", ' '), NULL, 1, ft_lexornew("redirection", LESS)); + assert_parser(ft_split("echo test", ' '), "YES", 0, NULL); + ft_simple_cmdsclear(&test_tools.simple_cmds); +} + +void test_parser_10(void) +{ + TEST_MESSAGE("10: {cd test | env < redirection | exit test}"); + init_test("cd test | env < redirection | exit test"); + assert_parser(ft_split("cd test", ' '), "YES", 0, NULL); + assert_parser(ft_split("env", ' '), "YES", 1, ft_lexornew("redirection", LESS)); + assert_parser(ft_split("exit test", ' '), "YES", 0, NULL); + ft_simple_cmdsclear(&test_tools.simple_cmds); +} + +void test_parser_11(void) +{ + TEST_MESSAGE("11: {export test | pwd < redirection | unset test}"); + init_test("export test | pwd < redirection | unset test"); + assert_parser(ft_split("export test", ' '), "YES", 0, NULL); + assert_parser(ft_split("pwd", ' '), "YES", 1, ft_lexornew("redirection", LESS)); + assert_parser(ft_split("unset test", ' '), "YES", 0, NULL); + ft_simple_cmdsclear(&test_tools.simple_cmds); +} + int main(void) { UNITY_BEGIN(); RUN_TEST(test_parser_1); RUN_TEST(test_parser_2); RUN_TEST(test_parser_3); - // RUN_TEST(test_parser_4); - // RUN_TEST(test_parser_5); + RUN_TEST(test_parser_4); + RUN_TEST(test_parser_5); + RUN_TEST(test_parser_6); + RUN_TEST(test_parser_7); + RUN_TEST(test_parser_8); + RUN_TEST(test_parser_9); + RUN_TEST(test_parser_10); + RUN_TEST(test_parser_11); return UNITY_END(); } diff --git a/test/Testtoken_reader.c b/test/Testtoken_reader.c index 7142932..0c93940 100644 --- a/test/Testtoken_reader.c +++ b/test/Testtoken_reader.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/28 11:12:08 by fpolycar #+# #+# */ -/* Updated: 2022/03/22 18:53:44 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/28 12:55:35 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -125,6 +125,16 @@ void test_lexer_10(void) assert_token(0, "bla=\"t est\""); } +void test_lexer_11(void) +{ + init_test("test\n\ntest\ftest\rtest\ttest"); + assert_token(0, "test"); + assert_token(0, "test"); + assert_token(0, "test"); + assert_token(0, "test"); + assert_token(0, "test"); +} + int main(void) { UNITY_BEGIN(); @@ -148,5 +158,7 @@ int main(void) ft_lexorclear(&test_tools.lexor_list); RUN_TEST(test_lexer_10); ft_lexorclear(&test_tools.lexor_list); + RUN_TEST(test_lexer_11); + ft_lexorclear(&test_tools.lexor_list); return UNITY_END(); } diff --git a/test/read_tester_output.sh b/test/read_tester_output.sh new file mode 100755 index 0000000..e43c61e --- /dev/null +++ b/test/read_tester_output.sh @@ -0,0 +1,22 @@ +GREEN='\033[0;32m' +RED='\033[0;31m' +NC='\033[0m' +FILE='../build/results/Testparser.txt' + +I=0 +cat $FILE | while read line +do + if [ $I -le 0 ] + then + awk -F 'INFO:' '{ print $2 }' + I=1 + else + if [`grep -s PASS ../build/results/*.txt`] + then + printf "${GREEN}[OK]${NC}" + else + printf "${RED}[KO]${NC}" + fi + I=0 + fi +done From 4defa06b6467e4f69e9a107d18c07d4b696cf919 Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Tue, 29 Mar 2022 12:46:37 +0200 Subject: [PATCH 092/163] Finished tester parser --- test/Testparser.c | 22 ++++++++-------- test/read_tester_output.sh | 37 ++++++++++++++++---------- test/test_builtins.c | 54 ++++++++++++++++++++++++++++++++++++++ test/test_executor.c | 0 test/test_expander.c | 0 5 files changed, 88 insertions(+), 25 deletions(-) create mode 100644 test/test_builtins.c create mode 100644 test/test_executor.c create mode 100644 test/test_expander.c diff --git a/test/Testparser.c b/test/Testparser.c index aef4d4f..d249097 100644 --- a/test/Testparser.c +++ b/test/Testparser.c @@ -68,7 +68,6 @@ void assert_parser(char **expected, char *builtin, int num_directions, t_lexor * void test_parser_1(void) { - TEST_MESSAGE("1: {test test}"); init_test("test test"); assert_parser(ft_split("test test", ' '), NULL, 0, NULL); ft_simple_cmdsclear(&test_tools.simple_cmds); @@ -76,7 +75,6 @@ void test_parser_1(void) void test_parser_2(void) { - TEST_MESSAGE("2: {test test | test}"); init_test("test test | test"); assert_parser(ft_split("test test", ' '), NULL, 0, NULL); assert_parser(ft_split("test", ' '), NULL, 0, NULL); @@ -85,7 +83,6 @@ void test_parser_2(void) void test_parser_3(void) { - TEST_MESSAGE("3: {test test | test | \'test\'}"); init_test("test test | test | \'test\'"); assert_parser(ft_split("test test", ' '), NULL, 0, NULL); assert_parser(ft_split("test", ' '), NULL, 0, NULL); @@ -95,7 +92,6 @@ void test_parser_3(void) void test_parser_4(void) { - TEST_MESSAGE("4: {< redirection}"); init_test("< redirection"); assert_parser(NULL, NULL, 1, ft_lexornew("redirection", LESS)); ft_simple_cmdsclear(&test_tools.simple_cmds); @@ -103,7 +99,6 @@ void test_parser_4(void) void test_parser_5(void) { - TEST_MESSAGE("5: {test > redirection}"); init_test("test > redirection"); assert_parser(ft_split("test", ' '), NULL, 1, ft_lexornew("redirection", GREAT)); ft_simple_cmdsclear(&test_tools.simple_cmds); @@ -111,7 +106,6 @@ void test_parser_5(void) void test_parser_6(void) { - TEST_MESSAGE("6: {test > redirection < redirection >> redirection}"); init_test("test > redirection < redirection >> redirection"); t_lexor *lst= ft_lexornew("redirection", GREAT); ft_lexoradd_back(&lst, ft_lexornew("redirection", LESS)); @@ -122,7 +116,6 @@ void test_parser_6(void) void test_parser_7(void) { - TEST_MESSAGE("7: {test > redirection < redirection | heredoc << EOF}"); init_test("test > redirection < redirection | heredoc << EOF"); t_lexor *lst = ft_lexornew("redirection", GREAT); ft_lexoradd_back(&lst, ft_lexornew("redirection", LESS)); @@ -133,7 +126,6 @@ void test_parser_7(void) void test_parser_8(void) { - TEST_MESSAGE("8: {test > redirection < redirection | heredoc < redirection >> redirection << EOF}"); init_test("test > redirection < redirection | heredoc < redirection >> redirection << EOF"); t_lexor *lst = ft_lexornew("redirection", GREAT); ft_lexoradd_back(&lst, ft_lexornew("redirection", LESS)); @@ -147,7 +139,6 @@ void test_parser_8(void) void test_parser_9(void) { - TEST_MESSAGE("9: {cat test | test < redirection | echo test}"); init_test("cat test | test < redirection | echo test"); assert_parser(ft_split("cat test", ' '), NULL, 0, NULL); assert_parser(ft_split("test", ' '), NULL, 1, ft_lexornew("redirection", LESS)); @@ -157,7 +148,6 @@ void test_parser_9(void) void test_parser_10(void) { - TEST_MESSAGE("10: {cd test | env < redirection | exit test}"); init_test("cd test | env < redirection | exit test"); assert_parser(ft_split("cd test", ' '), "YES", 0, NULL); assert_parser(ft_split("env", ' '), "YES", 1, ft_lexornew("redirection", LESS)); @@ -167,7 +157,6 @@ void test_parser_10(void) void test_parser_11(void) { - TEST_MESSAGE("11: {export test | pwd < redirection | unset test}"); init_test("export test | pwd < redirection | unset test"); assert_parser(ft_split("export test", ' '), "YES", 0, NULL); assert_parser(ft_split("pwd", ' '), "YES", 1, ft_lexornew("redirection", LESS)); @@ -179,15 +168,26 @@ int main(void) { UNITY_BEGIN(); RUN_TEST(test_parser_1); + TEST_MESSAGE("1: {test test}"); RUN_TEST(test_parser_2); + TEST_MESSAGE("2: {test test | test}"); RUN_TEST(test_parser_3); + TEST_MESSAGE("3: {test test | test | \'test\'}"); RUN_TEST(test_parser_4); + TEST_MESSAGE("4: {< redirection}"); RUN_TEST(test_parser_5); + TEST_MESSAGE("5: {test > redirection}"); RUN_TEST(test_parser_6); + TEST_MESSAGE("6: {test > redirection < redirection >> redirection}"); RUN_TEST(test_parser_7); + TEST_MESSAGE("7: {test > redirection < redirection | heredoc << EOF}"); RUN_TEST(test_parser_8); + TEST_MESSAGE("8: {test > redirection < redirection | heredoc < redirection >> redirection << EOF}"); RUN_TEST(test_parser_9); + TEST_MESSAGE("9: {cat test | test < redirection | echo test}"); RUN_TEST(test_parser_10); + TEST_MESSAGE("10: {cd test | env < redirection | exit test}"); RUN_TEST(test_parser_11); + TEST_MESSAGE("11: {export test | pwd < redirection | unset test}"); return UNITY_END(); } diff --git a/test/read_tester_output.sh b/test/read_tester_output.sh index e43c61e..f4286f2 100755 --- a/test/read_tester_output.sh +++ b/test/read_tester_output.sh @@ -1,22 +1,31 @@ GREEN='\033[0;32m' RED='\033[0;31m' NC='\033[0m' -FILE='../build/results/Testparser.txt' +FILE=$1 I=0 -cat $FILE | while read line +echo "$FILE\n" | awk -F '/results/' '{ print $2 }' +while read line do - if [ $I -le 0 ] + if [ $I -eq 1 ] then - awk -F 'INFO:' '{ print $2 }' - I=1 - else - if [`grep -s PASS ../build/results/*.txt`] - then - printf "${GREEN}[OK]${NC}" - else - printf "${RED}[KO]${NC}" - fi - I=0 + INFO_INFO=$(echo $line | awk -F 'INFO:' '{ print $2 }') + printf "%s >>" "$INFO_INFO" + printf "${RED}%s${NC}\n" "$FAIL_INFO" + I=0; fi -done + if [[ "$line" == *"PASS"* ]] + then + printf "${GREEN}[OK]${NC}" + elif [[ "$line" == *"FAIL"* ]] + then + printf "\n${RED}[KO]${NC}" + FAIL_INFO=$(echo $line | awk -F 'FAIL:' '{ print $2 }') + I=1; + fi + if [[ "$line" == *"Tests"*"Failures"* ]] + then + echo "\n\n~~~~~~~~Final Results:~~~~~~~~\n$line" + break + fi +done < "$FILE" diff --git a/test/test_builtins.c b/test/test_builtins.c new file mode 100644 index 0000000..a4defb1 --- /dev/null +++ b/test/test_builtins.c @@ -0,0 +1,54 @@ +#include "unity.h" +#include "minishell.h" +#include + +t_tools test_tools; + +void setUp(void) +{ + // set stuff up here +} + +void tearDown(void) +{ + // clean stuff up here +} + +void init_test(char *line) +{ + parse_envp(test_t) +} + +void test_builtin_1(void) +{ + init_test +} + +int main(int argc, char **argv, char **envp) +{ + UNITY_BEGIN(); + test_tools.envp = ft_arrdup(envp); + RUN_TEST(test_builtin_1); + TEST_MESSAGE("1: {test test}"); + RUN_TEST(test_builtin_2); + TEST_MESSAGE("2: {test test | test}"); + RUN_TEST(test_builtin_3); + TEST_MESSAGE("3: {test test | test | \'test\'}"); + RUN_TEST(test_builtin_4); + TEST_MESSAGE("4: {< redirection}"); + RUN_TEST(test_builtin_5); + TEST_MESSAGE("5: {test > redirection}"); + RUN_TEST(test_builtin_6); + TEST_MESSAGE("6: {test > redirection < redirection >> redirection}"); + RUN_TEST(test_builtin_7); + TEST_MESSAGE("7: {test > redirection < redirection | heredoc << EOF}"); + RUN_TEST(test_builtin_8); + TEST_MESSAGE("8: {test > redirection < redirection | heredoc < redirection >> redirection << EOF}"); + RUN_TEST(test_builtin_9); + TEST_MESSAGE("9: {cat test | test < redirection | echo test}"); + RUN_TEST(test_builtin_10); + TEST_MESSAGE("10: {cd test | env < redirection | exit test}"); + RUN_TEST(test_builtin_11); + TEST_MESSAGE("11: {export test | pwd < redirection | unset test}"); + return UNITY_END(); +} \ No newline at end of file diff --git a/test/test_executor.c b/test/test_executor.c new file mode 100644 index 0000000..e69de29 diff --git a/test/test_expander.c b/test/test_expander.c new file mode 100644 index 0000000..e69de29 From c719ba9688bd6b054c1192166f6b9e0396e54203 Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Tue, 29 Mar 2022 13:44:32 +0200 Subject: [PATCH 093/163] Makefile calls tester parser --- src/error/error_handeling.c | 10 +++++----- test/Makefile | 35 ++--------------------------------- test/Testparser.c | 13 ------------- test/call_read_tester.sh | 4 ++++ test/read_tester_output.sh | 2 +- 5 files changed, 12 insertions(+), 52 deletions(-) create mode 100755 test/call_read_tester.sh diff --git a/src/error/error_handeling.c b/src/error/error_handeling.c index 5444ea8..3c14ee4 100755 --- a/src/error/error_handeling.c +++ b/src/error/error_handeling.c @@ -6,7 +6,7 @@ /* By: maiadegraaf $@ 2>&1 @@ -69,25 +56,7 @@ $(PATHR)%.txt: $(PATHO)%.$(TARGET_EXTENSION) $(PATHO)Test%.$(TARGET_EXTENSION): $(LIBFT) $(OBJS) $(PATHO)Test%.o $(PATHO)%.o $(PATHU)unity.o $(LINK) -lreadline -o $@ $^ -$(PATHO)%.o:: $(PATHT)%.c $(HEADERS) - $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ - -# $(PATHO)%.o:: $(PATHS)%.c $(HEADERS) -# $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ - -$(PATHO)%.o:: $(PATHSL)%.c $(HEADERS) - $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ - -$(PATHO)%.o:: $(PATHSP)%.c $(HEADERS) - $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ - -$(PATHO)%.o:: $(PATHSU)%.c $(HEADERS) - $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ - -$(PATHO)%.o:: $(PATHSB)%.c $(HEADERS) - $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ - -$(PATHO)%.o:: $(PATHSE)%.c $(HEADERS) +$(PATHO)%.o:: $(PATHT)%.c $(PATHSL)%.c $(PATHSP)%.c $(PATHSU)%.c $(PATHSB)%.c $(PATHSE)%.c $(HEADERS) $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ $(PATHO)%.o:: $(PATHU)%.c $(PATHU)%.h diff --git a/test/Testparser.c b/test/Testparser.c index d249097..771f585 100644 --- a/test/Testparser.c +++ b/test/Testparser.c @@ -38,30 +38,17 @@ void assert_parser(char **expected, char *builtin, int num_directions, t_lexor * TEST_ASSERT_NOT_NULL_MESSAGE(test_tools.simple_cmds->builtin, "Incorrect: BUILTIN"); else TEST_ASSERT_NULL(test_tools.simple_cmds->builtin); - // printf("num redirections = >%d<\t|\t", num_directions); - // printf(">%d<\n", test_tools.simple_cmds->num_redirections); TEST_ASSERT_EQUAL_INT_MESSAGE(num_directions, test_tools.simple_cmds->num_redirections, "Incorrect: NUM REDIRECTIONS"); if (expected_redirection) { t_lexor *start = expected_redirection; while (expected_redirection) { - // printf("%s\t", expected_redirection->str); - // if (expected_redirection->token == GREAT) - // printf("GREAT\n"); - // else if (expected_redirection->token == GREAT_GREAT) - // printf("GREAT_GREAT\n"); - // else if (expected_redirection->token == LESS) - // printf("LESS\n"); - // else if (expected_redirection->token == LESS_LESS) - // printf("LESS_LESS\n"); TEST_ASSERT_EQUAL_STRING(expected_redirection->str, test_tools.simple_cmds->redirections->str); TEST_ASSERT_EQUAL_INT(expected_redirection->token, test_tools.simple_cmds->redirections->token); test_tools.simple_cmds->redirections = test_tools.simple_cmds->redirections->next; expected_redirection = expected_redirection->next; } - // printf("%s\n", start->str); - // ft_lexorclear(&start); } test_tools.simple_cmds = test_tools.simple_cmds->next; } diff --git a/test/call_read_tester.sh b/test/call_read_tester.sh new file mode 100755 index 0000000..9ba52d3 --- /dev/null +++ b/test/call_read_tester.sh @@ -0,0 +1,4 @@ +for f in ../build/results/Test*.txt + do + ./read_tester_output.sh $f + done \ No newline at end of file diff --git a/test/read_tester_output.sh b/test/read_tester_output.sh index f4286f2..f2334b9 100755 --- a/test/read_tester_output.sh +++ b/test/read_tester_output.sh @@ -25,7 +25,7 @@ do fi if [[ "$line" == *"Tests"*"Failures"* ]] then - echo "\n\n~~~~~~~~Final Results:~~~~~~~~\n$line" + echo "\n\n~~~~~~~~~~~Results:~~~~~~~~~~~\n$line\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" break fi done < "$FILE" From f8fe5f0b463ffe6e22e32ecffc800b6752a4d29c Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Tue, 29 Mar 2022 14:06:07 +0200 Subject: [PATCH 094/163] desole --- test/Makefile | 6 +-- test/call_read_tester.sh | 2 +- test/test_builtins.c | 59 +++++++++++++---------- test/{Testtoken_reader.c => test_lexor.c} | 0 test/{Testparser.c => test_parser.c} | 2 +- 5 files changed, 39 insertions(+), 30 deletions(-) rename test/{Testtoken_reader.c => test_lexor.c} (100%) rename test/{Testparser.c => test_parser.c} (99%) diff --git a/test/Makefile b/test/Makefile index 195cebd..116a50b 100644 --- a/test/Makefile +++ b/test/Makefile @@ -43,7 +43,7 @@ LINK=gcc CFLAGS= -I. -I$(PATHU) -I$(PATHLIBFT) -I$(PATHS) -I$(PATHP) -I$(PATHI) -RESULTS = $(patsubst $(PATHT)Test%.c,$(PATHR)Test%.txt, $(SRCT)) +RESULTS = $(patsubst $(PATHT)test_%.c,$(PATHR)test_%.txt, $(SRCT)) OBJS = $(addprefix $(PATHO), $(notdir $(patsubst %.c, %.o, $(src)))) @@ -53,7 +53,7 @@ test: $(BUILD_PATHS) $(RESULTS) $(PATHR)%.txt: $(PATHO)%.$(TARGET_EXTENSION) -./$< > $@ 2>&1 -$(PATHO)Test%.$(TARGET_EXTENSION): $(LIBFT) $(OBJS) $(PATHO)Test%.o $(PATHO)%.o $(PATHU)unity.o +$(PATHO)test_%.$(TARGET_EXTENSION): $(LIBFT) $(OBJS) $(PATHO)test_%.o $(PATHO)%.o $(PATHU)unity.o $(LINK) -lreadline -o $@ $^ $(PATHO)%.o:: $(PATHT)%.c $(PATHSL)%.c $(PATHSP)%.c $(PATHSU)%.c $(PATHSB)%.c $(PATHSE)%.c $(HEADERS) @@ -83,7 +83,7 @@ clean: re: clean test -.PRECIOUS: $(PATHB)Test%.$(TARGET_EXTENSION) +.PRECIOUS: $(PATHB)test_%.$(TARGET_EXTENSION) .PRECIOUS: $(PATHD)%.d .PRECIOUS: $(PATHO)%.o .PRECIOUS: $(PATHR)%.txt diff --git a/test/call_read_tester.sh b/test/call_read_tester.sh index 9ba52d3..c78d5f9 100755 --- a/test/call_read_tester.sh +++ b/test/call_read_tester.sh @@ -1,4 +1,4 @@ -for f in ../build/results/Test*.txt +for f in ../build/results/test_*.txt do ./read_tester_output.sh $f done \ No newline at end of file diff --git a/test/test_builtins.c b/test/test_builtins.c index a4defb1..ea75b1c 100644 --- a/test/test_builtins.c +++ b/test/test_builtins.c @@ -3,6 +3,7 @@ #include t_tools test_tools; +t_simple_cmds test_simple_cmd; void setUp(void) { @@ -16,39 +17,47 @@ void tearDown(void) void init_test(char *line) { - parse_envp(test_t) + parse_envp(&test_tools); + implement_tools(&test_tools); + test_simple_cmd.str = ft_split(line, ' '); + test_simple_cmd.builtin = builtin_arr(test_simple_cmd.str[0]); + test_simple_cmd.num_redirections = 0; + test_simple_cmd.redirections = NULL; } +void assert_mini_echo(); + void test_builtin_1(void) { - init_test + init_test("echo hello"); + } int main(int argc, char **argv, char **envp) { UNITY_BEGIN(); - test_tools.envp = ft_arrdup(envp); - RUN_TEST(test_builtin_1); - TEST_MESSAGE("1: {test test}"); - RUN_TEST(test_builtin_2); - TEST_MESSAGE("2: {test test | test}"); - RUN_TEST(test_builtin_3); - TEST_MESSAGE("3: {test test | test | \'test\'}"); - RUN_TEST(test_builtin_4); - TEST_MESSAGE("4: {< redirection}"); - RUN_TEST(test_builtin_5); - TEST_MESSAGE("5: {test > redirection}"); - RUN_TEST(test_builtin_6); - TEST_MESSAGE("6: {test > redirection < redirection >> redirection}"); - RUN_TEST(test_builtin_7); - TEST_MESSAGE("7: {test > redirection < redirection | heredoc << EOF}"); - RUN_TEST(test_builtin_8); - TEST_MESSAGE("8: {test > redirection < redirection | heredoc < redirection >> redirection << EOF}"); - RUN_TEST(test_builtin_9); - TEST_MESSAGE("9: {cat test | test < redirection | echo test}"); - RUN_TEST(test_builtin_10); - TEST_MESSAGE("10: {cd test | env < redirection | exit test}"); - RUN_TEST(test_builtin_11); - TEST_MESSAGE("11: {export test | pwd < redirection | unset test}"); + // test_tools.envp = ft_arrdup(envp); + // RUN_TEST(test_builtin_1); + // TEST_MESSAGE("1: {test test}"); + // RUN_TEST(test_builtin_2); + // TEST_MESSAGE("2: {test test | test}"); + // RUN_TEST(test_builtin_3); + // TEST_MESSAGE("3: {test test | test | \'test\'}"); + // RUN_TEST(test_builtin_4); + // TEST_MESSAGE("4: {< redirection}"); + // RUN_TEST(test_builtin_5); + // TEST_MESSAGE("5: {test > redirection}"); + // RUN_TEST(test_builtin_6); + // TEST_MESSAGE("6: {test > redirection < redirection >> redirection}"); + // RUN_TEST(test_builtin_7); + // TEST_MESSAGE("7: {test > redirection < redirection | heredoc << EOF}"); + // RUN_TEST(test_builtin_8); + // TEST_MESSAGE("8: {test > redirection < redirection | heredoc < redirection >> redirection << EOF}"); + // RUN_TEST(test_builtin_9); + // TEST_MESSAGE("9: {cat test | test < redirection | echo test}"); + // RUN_TEST(test_builtin_10); + // TEST_MESSAGE("10: {cd test | env < redirection | exit test}"); + // RUN_TEST(test_builtin_11); + // TEST_MESSAGE("11: {export test | pwd < redirection | unset test}"); return UNITY_END(); } \ No newline at end of file diff --git a/test/Testtoken_reader.c b/test/test_lexor.c similarity index 100% rename from test/Testtoken_reader.c rename to test/test_lexor.c diff --git a/test/Testparser.c b/test/test_parser.c similarity index 99% rename from test/Testparser.c rename to test/test_parser.c index 771f585..5f7aa00 100644 --- a/test/Testparser.c +++ b/test/test_parser.c @@ -177,4 +177,4 @@ int main(void) RUN_TEST(test_parser_11); TEST_MESSAGE("11: {export test | pwd < redirection | unset test}"); return UNITY_END(); -} +} \ No newline at end of file From e5d7fa6d5813a45a3d8cdebfbe16890c02a4b0ed Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Tue, 29 Mar 2022 14:09:47 +0200 Subject: [PATCH 095/163] still no worky --- test/Makefile | 6 +++--- test/{test_builtins.c => Testbuiltins.c} | 0 test/{test_executor.c => Testexecutor.c} | 0 test/{test_expander.c => Testexpander.c} | 0 test/{test_parser.c => Testparser.c} | 0 test/{test_lexor.c => Testtoken_reader.c} | 0 test/call_read_tester.sh | 2 +- 7 files changed, 4 insertions(+), 4 deletions(-) rename test/{test_builtins.c => Testbuiltins.c} (100%) rename test/{test_executor.c => Testexecutor.c} (100%) rename test/{test_expander.c => Testexpander.c} (100%) rename test/{test_parser.c => Testparser.c} (100%) rename test/{test_lexor.c => Testtoken_reader.c} (100%) diff --git a/test/Makefile b/test/Makefile index 116a50b..195cebd 100644 --- a/test/Makefile +++ b/test/Makefile @@ -43,7 +43,7 @@ LINK=gcc CFLAGS= -I. -I$(PATHU) -I$(PATHLIBFT) -I$(PATHS) -I$(PATHP) -I$(PATHI) -RESULTS = $(patsubst $(PATHT)test_%.c,$(PATHR)test_%.txt, $(SRCT)) +RESULTS = $(patsubst $(PATHT)Test%.c,$(PATHR)Test%.txt, $(SRCT)) OBJS = $(addprefix $(PATHO), $(notdir $(patsubst %.c, %.o, $(src)))) @@ -53,7 +53,7 @@ test: $(BUILD_PATHS) $(RESULTS) $(PATHR)%.txt: $(PATHO)%.$(TARGET_EXTENSION) -./$< > $@ 2>&1 -$(PATHO)test_%.$(TARGET_EXTENSION): $(LIBFT) $(OBJS) $(PATHO)test_%.o $(PATHO)%.o $(PATHU)unity.o +$(PATHO)Test%.$(TARGET_EXTENSION): $(LIBFT) $(OBJS) $(PATHO)Test%.o $(PATHO)%.o $(PATHU)unity.o $(LINK) -lreadline -o $@ $^ $(PATHO)%.o:: $(PATHT)%.c $(PATHSL)%.c $(PATHSP)%.c $(PATHSU)%.c $(PATHSB)%.c $(PATHSE)%.c $(HEADERS) @@ -83,7 +83,7 @@ clean: re: clean test -.PRECIOUS: $(PATHB)test_%.$(TARGET_EXTENSION) +.PRECIOUS: $(PATHB)Test%.$(TARGET_EXTENSION) .PRECIOUS: $(PATHD)%.d .PRECIOUS: $(PATHO)%.o .PRECIOUS: $(PATHR)%.txt diff --git a/test/test_builtins.c b/test/Testbuiltins.c similarity index 100% rename from test/test_builtins.c rename to test/Testbuiltins.c diff --git a/test/test_executor.c b/test/Testexecutor.c similarity index 100% rename from test/test_executor.c rename to test/Testexecutor.c diff --git a/test/test_expander.c b/test/Testexpander.c similarity index 100% rename from test/test_expander.c rename to test/Testexpander.c diff --git a/test/test_parser.c b/test/Testparser.c similarity index 100% rename from test/test_parser.c rename to test/Testparser.c diff --git a/test/test_lexor.c b/test/Testtoken_reader.c similarity index 100% rename from test/test_lexor.c rename to test/Testtoken_reader.c diff --git a/test/call_read_tester.sh b/test/call_read_tester.sh index c78d5f9..9ba52d3 100755 --- a/test/call_read_tester.sh +++ b/test/call_read_tester.sh @@ -1,4 +1,4 @@ -for f in ../build/results/test_*.txt +for f in ../build/results/Test*.txt do ./read_tester_output.sh $f done \ No newline at end of file From a84b6d2f5487215dda027c53b4d56d9dc4387c4a Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Tue, 29 Mar 2022 15:05:28 +0200 Subject: [PATCH 096/163] It works now????? --- src/builtins/builtins.c | 13 +---- src/builtins/utils_builtins.c | 15 +++++- test/Makefile | 31 ++++++++++-- test/Testbuiltins.c | 90 +++++++++++++++++++++++++---------- 4 files changed, 107 insertions(+), 42 deletions(-) diff --git a/src/builtins/builtins.c b/src/builtins/builtins.c index ba0040e..188ab53 100644 --- a/src/builtins/builtins.c +++ b/src/builtins/builtins.c @@ -6,23 +6,12 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 11:42:32 by mgraaf #+# #+# */ -/* Updated: 2022/03/24 16:09:37 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/29 14:25:04 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "builtins.h" -void change_path(t_tools *tools) -{ - char *tmp; - - tmp = ft_strdup(tools->pwd); - free(tools->old_pwd); - tools->old_pwd = tmp; - free(tools->pwd); - tools->pwd = getcwd(NULL, sizeof(NULL)); -} - int (*builtin_arr(char *str))(t_tools *tools, t_simple_cmds *simple_cmd) { static void *builtins[7][2] = { diff --git a/src/builtins/utils_builtins.c b/src/builtins/utils_builtins.c index 35801d9..af4674b 100644 --- a/src/builtins/utils_builtins.c +++ b/src/builtins/utils_builtins.c @@ -1,17 +1,28 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* utils.c :+: :+: */ +/* utils_builtins.c :+: :+: */ /* +:+ */ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:04:47 by fpolycar #+# #+# */ -/* Updated: 2022/03/24 16:06:09 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/29 14:25:16 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "builtins.h" +void change_path(t_tools *tools) +{ + char *tmp; + + tmp = ft_strdup(tools->pwd); + free(tools->old_pwd); + tools->old_pwd = tmp; + free(tools->pwd); + tools->pwd = getcwd(NULL, sizeof(NULL)); +} + size_t equal_sign(char *str) { size_t i; diff --git a/test/Makefile b/test/Makefile index 195cebd..c63d205 100644 --- a/test/Makefile +++ b/test/Makefile @@ -56,11 +56,36 @@ $(PATHR)%.txt: $(PATHO)%.$(TARGET_EXTENSION) $(PATHO)Test%.$(TARGET_EXTENSION): $(LIBFT) $(OBJS) $(PATHO)Test%.o $(PATHO)%.o $(PATHU)unity.o $(LINK) -lreadline -o $@ $^ -$(PATHO)%.o:: $(PATHT)%.c $(PATHSL)%.c $(PATHSP)%.c $(PATHSU)%.c $(PATHSB)%.c $(PATHSE)%.c $(HEADERS) - $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ +# $(PATHO)%.o:: $(PATHT)%.c $(PATHSL)%.c $(PATHSP)%.c $(PATHSU)%.c $(PATHSB)%.c $(PATHSE)%.c $(HEADERS) +# $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ + + +$(PATHO)%.o:: $(PATHT)%.c $(HEADERS) + @$(COMPILE) $(CFLAGS) $< -o $@ + +# $(PATHO)%.o:: $(PATHS)%.c $(HEADERS) +# $(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHO)%.o:: $(PATHSL)%.c $(HEADERS) + @$(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHO)%.o:: $(PATHSP)%.c $(HEADERS) + @$(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHO)%.o:: $(PATHSU)%.c $(HEADERS) + @$(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHO)%.o:: $(PATHSB)%.c $(HEADERS) + @$(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHO)%.o:: $(PATHSE)%.c $(HEADERS) + @$(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHO)%.o:: $(PATHU)%.c $(PATHU)%.h + @$(COMPILE) $(CFLAGS) $< -o $@ $(PATHO)%.o:: $(PATHU)%.c $(PATHU)%.h - $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ + @$(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ $(PATHB): $(MKDIR) $(PATHB) diff --git a/test/Testbuiltins.c b/test/Testbuiltins.c index ea75b1c..b2e3d83 100644 --- a/test/Testbuiltins.c +++ b/test/Testbuiltins.c @@ -25,39 +25,79 @@ void init_test(char *line) test_simple_cmd.redirections = NULL; } -void assert_mini_echo(); +void assert_mini_echo(void) +{ + TEST_ASSERT_NOT_NULL_MESSAGE(test_simple_cmd.builtin, "Builtin function not connected."); +} + +void assert_mini_cd(char *expected_new_path, char *expected_old_path) +{ + TEST_ASSERT_NOT_NULL_MESSAGE(test_simple_cmd.builtin, "Builtin function not connected."); + TEST_ASSERT_EQUAL_STRING_MESSAGE(expected_new_path, tools->ne) +} + +void assert_mini_env(void) +{ + TEST_ASSERT_NOT_NULL_MESSAGE(test_simple_cmd.builtin, "Builtin function not connected."); +} + +void assert_mini_exit(void) +{ + TEST_ASSERT_NOT_NULL_MESSAGE(test_simple_cmd.builtin, "Builtin function not connected."); +} + +void assert_mini_export(void) +{ + TEST_ASSERT_NOT_NULL_MESSAGE(test_simple_cmd.builtin, "Builtin function not connected."); +} + +void assert_mini_pwd(void) +{ + TEST_ASSERT_NOT_NULL_MESSAGE(test_simple_cmd.builtin, "Builtin function not connected."); +} + +void assert_mini_unset(void) +{ + TEST_ASSERT_NOT_NULL_MESSAGE(test_simple_cmd.builtin, "Builtin function not connected."); +} void test_builtin_1(void) { init_test("echo hello"); - + assert_mini_echo(); +} + +void test_builtin_2(void) +{ + init_test("echo hello"); + assert_mini_echo(); } int main(int argc, char **argv, char **envp) { UNITY_BEGIN(); - // test_tools.envp = ft_arrdup(envp); - // RUN_TEST(test_builtin_1); - // TEST_MESSAGE("1: {test test}"); - // RUN_TEST(test_builtin_2); - // TEST_MESSAGE("2: {test test | test}"); - // RUN_TEST(test_builtin_3); - // TEST_MESSAGE("3: {test test | test | \'test\'}"); - // RUN_TEST(test_builtin_4); - // TEST_MESSAGE("4: {< redirection}"); - // RUN_TEST(test_builtin_5); - // TEST_MESSAGE("5: {test > redirection}"); - // RUN_TEST(test_builtin_6); - // TEST_MESSAGE("6: {test > redirection < redirection >> redirection}"); - // RUN_TEST(test_builtin_7); - // TEST_MESSAGE("7: {test > redirection < redirection | heredoc << EOF}"); - // RUN_TEST(test_builtin_8); - // TEST_MESSAGE("8: {test > redirection < redirection | heredoc < redirection >> redirection << EOF}"); - // RUN_TEST(test_builtin_9); - // TEST_MESSAGE("9: {cat test | test < redirection | echo test}"); - // RUN_TEST(test_builtin_10); - // TEST_MESSAGE("10: {cd test | env < redirection | exit test}"); - // RUN_TEST(test_builtin_11); - // TEST_MESSAGE("11: {export test | pwd < redirection | unset test}"); + test_tools.envp = ft_arrdup(envp); + RUN_TEST(test_builtin_1); + TEST_MESSAGE("1: {echo hello}"); + RUN_TEST(test_builtin_2); + TEST_MESSAGE("2: {test test | test}"); + RUN_TEST(test_builtin_3); + TEST_MESSAGE("3: {test test | test | \'test\'}"); + RUN_TEST(test_builtin_4); + TEST_MESSAGE("4: {< redirection}"); + RUN_TEST(test_builtin_5); + TEST_MESSAGE("5: {test > redirection}"); + RUN_TEST(test_builtin_6); + TEST_MESSAGE("6: {test > redirection < redirection >> redirection}"); + RUN_TEST(test_builtin_7); + TEST_MESSAGE("7: {test > redirection < redirection | heredoc << EOF}"); + RUN_TEST(test_builtin_8); + TEST_MESSAGE("8: {test > redirection < redirection | heredoc < redirection >> redirection << EOF}"); + RUN_TEST(test_builtin_9); + TEST_MESSAGE("9: {cat test | test < redirection | echo test}"); + RUN_TEST(test_builtin_10); + TEST_MESSAGE("10: {cd test | env < redirection | exit test}"); + RUN_TEST(test_builtin_11); + TEST_MESSAGE("11: {export test | pwd < redirection | unset test}"); return UNITY_END(); } \ No newline at end of file From b5d1573c7df66f83399aa0bbfda0d48ba170a25d Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Tue, 29 Mar 2022 15:10:58 +0200 Subject: [PATCH 097/163] Try it now? --- includes/utils.h | 6 ++- src/main.c | 6 +-- test/Testbuiltins.c | 44 ++++++++++--------- test/{Testexecutor.c => Testexecutor.ctttttt} | 0 test/{Testexpander.c => Testexpander.ctttttt} | 0 5 files changed, 29 insertions(+), 27 deletions(-) rename test/{Testexecutor.c => Testexecutor.ctttttt} (100%) rename test/{Testexpander.c => Testexpander.ctttttt} (100%) diff --git a/includes/utils.h b/includes/utils.h index 2c36015..57ffd10 100644 --- a/includes/utils.h +++ b/includes/utils.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:36:23 by mgraaf #+# #+# */ -/* Updated: 2022/03/21 12:15:28 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/29 15:09:02 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -16,6 +16,10 @@ // utils int count_quotes(char *line); +int implement_tools(t_tools *tools); +char **ft_arrdup(char **arr); + +int minishell_loop(t_tools *tools); //t_simple_cmds_utils t_simple_cmds *ft_simple_cmdsnew(char **str, diff --git a/src/main.c b/src/main.c index 02d8836..91726e4 100644 --- a/src/main.c +++ b/src/main.c @@ -6,16 +6,12 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/24 15:44:21 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/29 15:08:50 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -int minishell_loop(t_tools *tools); -int implement_tools(t_tools *tools); -char **ft_arrdup(char **arr); - int main(int argc, char **argv, char **envp) { t_tools tools; diff --git a/test/Testbuiltins.c b/test/Testbuiltins.c index b2e3d83..e867078 100644 --- a/test/Testbuiltins.c +++ b/test/Testbuiltins.c @@ -1,5 +1,6 @@ #include "unity.h" #include "minishell.h" +#include "parser.h" #include t_tools test_tools; @@ -33,7 +34,8 @@ void assert_mini_echo(void) void assert_mini_cd(char *expected_new_path, char *expected_old_path) { TEST_ASSERT_NOT_NULL_MESSAGE(test_simple_cmd.builtin, "Builtin function not connected."); - TEST_ASSERT_EQUAL_STRING_MESSAGE(expected_new_path, tools->ne) + TEST_ASSERT_EQUAL_STRING_MESSAGE(expected_new_path, test_tools.pwd, "New path incorrectly assigned"); + TEST_ASSERT_EQUAL_STRING_MESSAGE(expected_old_path, test_tools.old_pwd, "Old path incorectly assigned"); } void assert_mini_env(void) @@ -79,25 +81,25 @@ int main(int argc, char **argv, char **envp) test_tools.envp = ft_arrdup(envp); RUN_TEST(test_builtin_1); TEST_MESSAGE("1: {echo hello}"); - RUN_TEST(test_builtin_2); - TEST_MESSAGE("2: {test test | test}"); - RUN_TEST(test_builtin_3); - TEST_MESSAGE("3: {test test | test | \'test\'}"); - RUN_TEST(test_builtin_4); - TEST_MESSAGE("4: {< redirection}"); - RUN_TEST(test_builtin_5); - TEST_MESSAGE("5: {test > redirection}"); - RUN_TEST(test_builtin_6); - TEST_MESSAGE("6: {test > redirection < redirection >> redirection}"); - RUN_TEST(test_builtin_7); - TEST_MESSAGE("7: {test > redirection < redirection | heredoc << EOF}"); - RUN_TEST(test_builtin_8); - TEST_MESSAGE("8: {test > redirection < redirection | heredoc < redirection >> redirection << EOF}"); - RUN_TEST(test_builtin_9); - TEST_MESSAGE("9: {cat test | test < redirection | echo test}"); - RUN_TEST(test_builtin_10); - TEST_MESSAGE("10: {cd test | env < redirection | exit test}"); - RUN_TEST(test_builtin_11); - TEST_MESSAGE("11: {export test | pwd < redirection | unset test}"); + // RUN_TEST(test_builtin_2); + // TEST_MESSAGE("2: {test test | test}"); + // RUN_TEST(test_builtin_3); + // TEST_MESSAGE("3: {test test | test | \'test\'}"); + // RUN_TEST(test_builtin_4); + // TEST_MESSAGE("4: {< redirection}"); + // RUN_TEST(test_builtin_5); + // TEST_MESSAGE("5: {test > redirection}"); + // RUN_TEST(test_builtin_6); + // TEST_MESSAGE("6: {test > redirection < redirection >> redirection}"); + // RUN_TEST(test_builtin_7); + // TEST_MESSAGE("7: {test > redirection < redirection | heredoc << EOF}"); + // RUN_TEST(test_builtin_8); + // TEST_MESSAGE("8: {test > redirection < redirection | heredoc < redirection >> redirection << EOF}"); + // RUN_TEST(test_builtin_9); + // TEST_MESSAGE("9: {cat test | test < redirection | echo test}"); + // RUN_TEST(test_builtin_10); + // TEST_MESSAGE("10: {cd test | env < redirection | exit test}"); + // RUN_TEST(test_builtin_11); + // TEST_MESSAGE("11: {export test | pwd < redirection | unset test}"); return UNITY_END(); } \ No newline at end of file diff --git a/test/Testexecutor.c b/test/Testexecutor.ctttttt similarity index 100% rename from test/Testexecutor.c rename to test/Testexecutor.ctttttt diff --git a/test/Testexpander.c b/test/Testexpander.ctttttt similarity index 100% rename from test/Testexpander.c rename to test/Testexpander.ctttttt From eda34fee41c5d1319738378074d09d85e7f0c1d7 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Tue, 29 Mar 2022 18:14:22 +0200 Subject: [PATCH 098/163] builtin tester --- src/builtins/mini_echo.c | 16 ++++----- src/builtins/mini_env.c | 4 +-- test/Makefile | 14 ++++---- test/Testbuiltins.c | 69 +++++++++++++++++++++++++++++--------- test/read_tester_output.sh | 8 +++-- 5 files changed, 76 insertions(+), 35 deletions(-) diff --git a/src/builtins/mini_echo.c b/src/builtins/mini_echo.c index b33ecb7..7ef3f6a 100644 --- a/src/builtins/mini_echo.c +++ b/src/builtins/mini_echo.c @@ -6,19 +6,19 @@ /* By: maiadegraaf str[i]) { - ft_putchar_fd('\n', STDOUT_FILENO); + ft_putchar_fd('\n', tools->out); return (EXIT_SUCCESS); } if (!ft_strncmp(simple_cmd->str[1], "-n", ft_strlen(simple_cmd->str[1]))) - print_lines(2, simple_cmd->str); + print_lines(2, simple_cmd->str, tools->out); else { - print_lines(1, simple_cmd->str); - ft_putchar_fd('\n', STDOUT_FILENO); + print_lines(1, simple_cmd->str, tools->out); + ft_putchar_fd('\n', tools->out); } return (EXIT_SUCCESS); } diff --git a/src/builtins/mini_env.c b/src/builtins/mini_env.c index 4dfa102..2feeffc 100644 --- a/src/builtins/mini_env.c +++ b/src/builtins/mini_env.c @@ -6,7 +6,7 @@ /* By: maiadegraaf envp[i]); tools->envp[i] = tmp; } - ft_putendl_fd(tools->envp[i], STDOUT_FILENO); + ft_putendl_fd(tools->envp[i], tools->out); i++; } return (EXIT_SUCCESS); diff --git a/test/Makefile b/test/Makefile index c63d205..24499b5 100644 --- a/test/Makefile +++ b/test/Makefile @@ -54,7 +54,7 @@ $(PATHR)%.txt: $(PATHO)%.$(TARGET_EXTENSION) -./$< > $@ 2>&1 $(PATHO)Test%.$(TARGET_EXTENSION): $(LIBFT) $(OBJS) $(PATHO)Test%.o $(PATHO)%.o $(PATHU)unity.o - $(LINK) -lreadline -o $@ $^ + @$(LINK) -lreadline -o $@ $^ # $(PATHO)%.o:: $(PATHT)%.c $(PATHSL)%.c $(PATHSP)%.c $(PATHSU)%.c $(PATHSB)%.c $(PATHSE)%.c $(HEADERS) # $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ @@ -84,20 +84,20 @@ $(PATHO)%.o:: $(PATHSE)%.c $(HEADERS) $(PATHO)%.o:: $(PATHU)%.c $(PATHU)%.h @$(COMPILE) $(CFLAGS) $< -o $@ -$(PATHO)%.o:: $(PATHU)%.c $(PATHU)%.h - @$(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ +# $(PATHO)%.o:: $(PATHU)%.c $(PATHU)%.h +# @$(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ $(PATHB): - $(MKDIR) $(PATHB) + @$(MKDIR) $(PATHB) $(PATHO): - $(MKDIR) $(PATHO) + @$(MKDIR) $(PATHO) $(PATHR): - $(MKDIR) $(PATHR) + @$(MKDIR) $(PATHR) $(LIBFT): - $(MAKE) -C $(PATHLIBFT) + @$(MAKE) -C $(PATHLIBFT) clean: $(CLEANUP) $(PATHO)*.o diff --git a/test/Testbuiltins.c b/test/Testbuiltins.c index e867078..2b8d731 100644 --- a/test/Testbuiltins.c +++ b/test/Testbuiltins.c @@ -5,6 +5,9 @@ t_tools test_tools; t_simple_cmds test_simple_cmd; +char *path_to_test; +char *path_to_minishell; +char **test_envp; void setUp(void) { @@ -20,10 +23,12 @@ void init_test(char *line) { parse_envp(&test_tools); implement_tools(&test_tools); + test_tools.out = open("tmp.text", O_CREAT | O_RDWR | O_TRUNC, 0644); test_simple_cmd.str = ft_split(line, ' '); test_simple_cmd.builtin = builtin_arr(test_simple_cmd.str[0]); test_simple_cmd.num_redirections = 0; test_simple_cmd.redirections = NULL; + test_simple_cmd.builtin(&test_tools, &test_simple_cmd); } void assert_mini_echo(void) @@ -38,19 +43,23 @@ void assert_mini_cd(char *expected_new_path, char *expected_old_path) TEST_ASSERT_EQUAL_STRING_MESSAGE(expected_old_path, test_tools.old_pwd, "Old path incorectly assigned"); } -void assert_mini_env(void) +void assert_mini_env(char **expected_envp) { TEST_ASSERT_NOT_NULL_MESSAGE(test_simple_cmd.builtin, "Builtin function not connected."); + TEST_ASSERT_EQUAL_STRING_ARRAY_MESSAGE(expected_envp, test_tools.envp, ft_strlen(*expected_envp), "Incorrect ENV"); } -void assert_mini_exit(void) -{ - TEST_ASSERT_NOT_NULL_MESSAGE(test_simple_cmd.builtin, "Builtin function not connected."); -} +// void assert_mini_exit(void) +// { +// TEST_ASSERT_NOT_NULL_MESSAGE(test_simple_cmd.builtin, "Builtin function not connected."); +// } -void assert_mini_export(void) +void assert_mini_export(char *expected) { TEST_ASSERT_NOT_NULL_MESSAGE(test_simple_cmd.builtin, "Builtin function not connected."); + char *found_export; + while (test_tools.envp = ) + TEST_ASSERT_EQUAL_STRING_MESSAGE(expect,) } void assert_mini_pwd(void) @@ -71,24 +80,52 @@ void test_builtin_1(void) void test_builtin_2(void) { - init_test("echo hello"); - assert_mini_echo(); + init_test("env"); + assert_mini_env(test_envp); +} + +void test_builtin_3(void) +{ + init_test("cd .."); + assert_mini_cd( path_to_minishell, path_to_test); +} + +void test_builtin_4(void) +{ + init_test("cd ~/Documents"); + assert_mini_cd(ft_strjoin( "/Users/", ft_strjoin(getenv("USER"), "/Documents")), path_to_minishell); +} + + +void test_builtin_5(void) +{ + init_test("cd minishell/src"); + assert_mini_cd(ft_strjoin(path_to_minishell, "/src"), ft_strjoin( "/Users/", ft_strjoin(getenv("USER"), "/Documents"))); +} + +void test_builtin_6(void) +{ + init_test("export test=\"bla\""); + assert_mini_export(); } int main(int argc, char **argv, char **envp) { UNITY_BEGIN(); + test_envp = envp; test_tools.envp = ft_arrdup(envp); + path_to_test = getenv("PWD"); + path_to_minishell = ft_substr(path_to_test, 0, ft_strlen(path_to_test) - ft_strlen("/test")); RUN_TEST(test_builtin_1); TEST_MESSAGE("1: {echo hello}"); - // RUN_TEST(test_builtin_2); - // TEST_MESSAGE("2: {test test | test}"); - // RUN_TEST(test_builtin_3); - // TEST_MESSAGE("3: {test test | test | \'test\'}"); - // RUN_TEST(test_builtin_4); - // TEST_MESSAGE("4: {< redirection}"); - // RUN_TEST(test_builtin_5); - // TEST_MESSAGE("5: {test > redirection}"); + RUN_TEST(test_builtin_2); + TEST_MESSAGE("2: env"); + RUN_TEST(test_builtin_3); + TEST_MESSAGE("3: {cd ..}"); + RUN_TEST(test_builtin_4); + TEST_MESSAGE("4: {cd ~/Documents'}"); + RUN_TEST(test_builtin_5); + TEST_MESSAGE("5: {cd minishell/src}"); // RUN_TEST(test_builtin_6); // TEST_MESSAGE("6: {test > redirection < redirection >> redirection}"); // RUN_TEST(test_builtin_7); diff --git a/test/read_tester_output.sh b/test/read_tester_output.sh index f2334b9..53dde7b 100755 --- a/test/read_tester_output.sh +++ b/test/read_tester_output.sh @@ -1,10 +1,13 @@ +BLUE='\033[0;34m' +BOLDBLUE="\e[1;34m" GREEN='\033[0;32m' RED='\033[0;31m' NC='\033[0m' FILE=$1 I=0 -echo "$FILE\n" | awk -F '/results/' '{ print $2 }' +FILE_NAME=$(echo "$FILE\n" | awk -F '/results/' '{ print $2 }') +printf "${BOLDBLUE}%s\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n${NC}" "$FILE_NAME" while read line do if [ $I -eq 1 ] @@ -25,7 +28,8 @@ do fi if [[ "$line" == *"Tests"*"Failures"* ]] then - echo "\n\n~~~~~~~~~~~Results:~~~~~~~~~~~\n$line\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" + echo "\n\n-----------Results:-----------\n$line\n------------------------------" break fi done < "$FILE" +printf "${BOLDBLUE}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~${NC}\n\n\n" \ No newline at end of file From 546eaec3659670ad838749714703bde363e2252b Mon Sep 17 00:00:00 2001 From: Alfred Polycarpe Date: Tue, 29 Mar 2022 18:58:01 +0200 Subject: [PATCH 099/163] expander evol --- src/expander/expander.c | 56 ++++++++++++++++++++------------------ src/parser/parser.c | 2 +- src/utils/minishell_loop.c | 6 ++-- 3 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/expander/expander.c b/src/expander/expander.c index 605917f..f72d646 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/03/28 15:35:09 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/29 18:32:49 by alfred ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -32,48 +32,51 @@ void expander_loop(t_tools *tools, t_simple_cmds *tmp, int i) int j; int k; char *arr_tmp; - char *before; - char *after; + char *tmp_transformed; j = 0; - k = 0; arr_tmp = tmp->str[i]; while (arr_tmp[j]) { if (arr_tmp[j] == '$') { + k = 0; while (tools->envp[k]) { if (ft_strncmp(arr_tmp + j + 1, tools->envp[k], equal_sign(tools->envp[k]) - 1) == 0 - && (arr_tmp[equal_sign(tools->envp[k])] == '\0' - || arr_tmp[equal_sign(tools->envp[k])] == '$')) + && (arr_tmp[equal_sign(tools->envp[k]) + j] == '\0' + || arr_tmp[equal_sign(tools->envp[k]) + j] == '$' + || arr_tmp[equal_sign(tools->envp[k]) + j] == ' ' + || arr_tmp[equal_sign(tools->envp[k]) + j] == '\"')) { - - // if (j == 0) - // { - // free(tmp->str[i]); - // tmp->str[i] = ft_substr(tools->envp[k], - // equal_sign(tools->envp[k]), - // ft_strlen(tools->envp[k])); - // } - // // // printf("test\n"); - // // // before = ft_substr(arr_tmp, 0, dollar_sign(arr_tmp)); - // else - // { - // before = tmp->str[i]; - // free(tmp->str[i]); - // tmp->str[i] = ft_strjoin(before, ft_substr(tools->envp[j], - // equal_sign(tools->envp[j]), - // ft_strlen(tools->envp[j]))); - // } - printf("%s\n\n", arr_tmp); + if (j == 0) + { + free(tmp->str[i]); + tmp->str[i] = ft_substr(tools->envp[k], + equal_sign(tools->envp[k]), + ft_strlen(tools->envp[k])); + } + // // printf("test\n"); + // // before = ft_substr(arr_tmp, 0, dollar_sign(arr_tmp)); + else + { + before = tmp->str[i]; + free(tmp->str[i]); + tmp->str[i] = ft_strjoin(before, ft_substr(tools->envp[j], + equal_sign(tools->envp[j]), + ft_strlen(tools->envp[j]))); + } + printf("arr = %s\n\n", arr_tmp); } + k++; } } j++; } + free(tmp->str[i]); + tmp->str[i] = tmp_transformed; } void expander(t_tools *tools, t_simple_cmds *simple_cmds) @@ -87,7 +90,8 @@ void expander(t_tools *tools, t_simple_cmds *simple_cmds) { while (tmp->str[i]) { - expander_loop(tools, tmp, i); + if (tmp->str[i][0] != '\'' && tmp->str[i][ft_strlen(tmp->str[i])] != '\'') + expander_loop(tools, tmp, i); i++; } tmp = tmp->next; diff --git a/src/parser/parser.c b/src/parser/parser.c index ff261db..5d29920 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/28 15:35:53 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/29 15:31:00 by alfred ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 29e03ed..27dde55 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/03/28 12:56:13 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/29 15:34:16 by alfred ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -45,8 +45,8 @@ int minishell_loop(t_tools *tools) if (!token_reader(tools)) ft_error(1, tools); parser(tools); - // expander(tools, tools->simple_cmds); - // printf("%s\n", *tools->simple_cmds->str); + expander(tools, tools->simple_cmds); + printf("%s\n", *tools->simple_cmds->str); // executor(&tools); reset_tools(tools); return (1); From dbf104cd73227b85ca30abd31e896cfd80d7a411 Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Wed, 30 Mar 2022 13:15:32 +0200 Subject: [PATCH 100/163] tester is even more colorful; --- test/Testexpander.ctttttt => src/tmp.text | 0 test/Testbuiltins.c | 112 ++++++++++++++----- test/Testexpander.cx | 129 ++++++++++++++++++++++ test/read_tester_output.sh | 27 ++++- test/tmp.text | 0 tmp.text | 0 6 files changed, 236 insertions(+), 32 deletions(-) rename test/Testexpander.ctttttt => src/tmp.text (100%) create mode 100644 test/Testexpander.cx create mode 100644 test/tmp.text create mode 100644 tmp.text diff --git a/test/Testexpander.ctttttt b/src/tmp.text similarity index 100% rename from test/Testexpander.ctttttt rename to src/tmp.text diff --git a/test/Testbuiltins.c b/test/Testbuiltins.c index 2b8d731..642f02d 100644 --- a/test/Testbuiltins.c +++ b/test/Testbuiltins.c @@ -21,7 +21,6 @@ void tearDown(void) void init_test(char *line) { - parse_envp(&test_tools); implement_tools(&test_tools); test_tools.out = open("tmp.text", O_CREAT | O_RDWR | O_TRUNC, 0644); test_simple_cmd.str = ft_split(line, ' '); @@ -54,22 +53,41 @@ void assert_mini_env(char **expected_envp) // TEST_ASSERT_NOT_NULL_MESSAGE(test_simple_cmd.builtin, "Builtin function not connected."); // } -void assert_mini_export(char *expected) +void assert_mini_export(char *variable_name, char *expected) { TEST_ASSERT_NOT_NULL_MESSAGE(test_simple_cmd.builtin, "Builtin function not connected."); - char *found_export; - while (test_tools.envp = ) - TEST_ASSERT_EQUAL_STRING_MESSAGE(expect,) + int i = 0; + int x = 0; + while (test_tools.envp[i]) + { + if (!ft_strncmp(variable_name, test_tools.envp[i], ft_strlen(variable_name))) + { + TEST_ASSERT_EQUAL_STRING(expected, test_tools.envp[i]); + x++; + } + i++; + } + TEST_ASSERT_FALSE_MESSAGE(x != 1, "Variable exists more than once or is not present in ENVP"); } -void assert_mini_pwd(void) +void assert_mini_pwd(char *pwd) { TEST_ASSERT_NOT_NULL_MESSAGE(test_simple_cmd.builtin, "Builtin function not connected."); + TEST_ASSERT_EQUAL_STRING(pwd, test_tools.pwd); } -void assert_mini_unset(void) +void assert_mini_unset(char *variable_name) { TEST_ASSERT_NOT_NULL_MESSAGE(test_simple_cmd.builtin, "Builtin function not connected."); + int i = 0; + while (test_tools.envp[i]) + { + if (!ft_strncmp(variable_name, test_tools.envp[i], ft_strlen(variable_name))) + { + TEST_FAIL_MESSAGE("Variable still exists in ENVP"); + } + i++; + } } void test_builtin_1(void) @@ -87,26 +105,61 @@ void test_builtin_2(void) void test_builtin_3(void) { init_test("cd .."); - assert_mini_cd( path_to_minishell, path_to_test); + assert_mini_cd(path_to_minishell, path_to_test); } void test_builtin_4(void) { - init_test("cd ~/Documents"); - assert_mini_cd(ft_strjoin( "/Users/", ft_strjoin(getenv("USER"), "/Documents")), path_to_minishell); + init_test(ft_strjoin("cd /Users/", ft_strjoin(getenv("USER"), "/Documents"))); + assert_mini_cd(ft_strjoin("/Users/", ft_strjoin(getenv("USER"), "/Documents")), path_to_minishell); } - void test_builtin_5(void) { - init_test("cd minishell/src"); - assert_mini_cd(ft_strjoin(path_to_minishell, "/src"), ft_strjoin( "/Users/", ft_strjoin(getenv("USER"), "/Documents"))); + init_test(ft_strjoin(ft_strjoin("cd ", path_to_minishell), "/src")); + assert_mini_cd(ft_strjoin(path_to_minishell, "/src"), ft_strjoin("/Users/", ft_strjoin(getenv("USER"), "/Documents"))); } void test_builtin_6(void) { - init_test("export test=\"bla\""); - assert_mini_export(); + init_test("export test=\"a\""); + assert_mini_export("test", "test=a"); +} + +void test_builtin_7(void) +{ + init_test("export test=\'b\'"); + assert_mini_export("test", "test=b"); +} + +void test_builtin_8(void) +{ + init_test("unset test"); + assert_mini_unset("test"); +} + +void test_builtin_9(void) +{ + init_test("export test2=c"); + assert_mini_export("test2", "test2=c"); +} + +void test_builtin_10(void) +{ + init_test("env"); + assert_mini_env(test_envp); +} + +void test_builtin_11(void) +{ + init_test("pwd"); + assert_mini_pwd(getcwd(NULL, sizeof(NULL))); +} + +void test_builtin_12(void) +{ + init_test("unset test2"); + assert_mini_unset("test2"); } int main(int argc, char **argv, char **envp) @@ -114,6 +167,7 @@ int main(int argc, char **argv, char **envp) UNITY_BEGIN(); test_envp = envp; test_tools.envp = ft_arrdup(envp); + parse_envp(&test_tools); path_to_test = getenv("PWD"); path_to_minishell = ft_substr(path_to_test, 0, ft_strlen(path_to_test) - ft_strlen("/test")); RUN_TEST(test_builtin_1); @@ -123,20 +177,22 @@ int main(int argc, char **argv, char **envp) RUN_TEST(test_builtin_3); TEST_MESSAGE("3: {cd ..}"); RUN_TEST(test_builtin_4); - TEST_MESSAGE("4: {cd ~/Documents'}"); + TEST_MESSAGE("4: {cd {path to} Documents'}"); RUN_TEST(test_builtin_5); - TEST_MESSAGE("5: {cd minishell/src}"); - // RUN_TEST(test_builtin_6); - // TEST_MESSAGE("6: {test > redirection < redirection >> redirection}"); - // RUN_TEST(test_builtin_7); - // TEST_MESSAGE("7: {test > redirection < redirection | heredoc << EOF}"); - // RUN_TEST(test_builtin_8); - // TEST_MESSAGE("8: {test > redirection < redirection | heredoc < redirection >> redirection << EOF}"); - // RUN_TEST(test_builtin_9); - // TEST_MESSAGE("9: {cat test | test < redirection | echo test}"); + TEST_MESSAGE("5: {cd {path from Documents to} minishell/src}"); + RUN_TEST(test_builtin_6); + TEST_MESSAGE("6: {export test=\"a\"}"); + RUN_TEST(test_builtin_7); + TEST_MESSAGE("7: {export test=\"b\"}"); + RUN_TEST(test_builtin_8); + TEST_MESSAGE("8: {unset test}"); + RUN_TEST(test_builtin_9); + TEST_MESSAGE("9: {export test2=c}"); // RUN_TEST(test_builtin_10); - // TEST_MESSAGE("10: {cd test | env < redirection | exit test}"); - // RUN_TEST(test_builtin_11); - // TEST_MESSAGE("11: {export test | pwd < redirection | unset test}"); + // TEST_MESSAGE("10: {env}"); + RUN_TEST(test_builtin_11); + TEST_MESSAGE("11: {pwd}"); + RUN_TEST(test_builtin_12); + TEST_MESSAGE("12: {unset test2}"); return UNITY_END(); } \ No newline at end of file diff --git a/test/Testexpander.cx b/test/Testexpander.cx new file mode 100644 index 0000000..0e2b941 --- /dev/null +++ b/test/Testexpander.cx @@ -0,0 +1,129 @@ +#include "unity.h" +#include "minishell.h" +#include "parser.h" +#include + +t_tools test_tools; +t_simple_cmds test_simple_cmd; + +void setUp(void) +{ + // set stuff up here +} + +void tearDown(void) +{ + // clean stuff up here +} + +void init_test(char *line) +{ + +} + +void assert_expander(void) +{ + TEST_ASSERT_NOT_NULL_MESSAGE(test_simple_cmd.expander, "expander function not connected."); +} + +void test_expander_1(void) +{ + init_test("echo hello"); + assert_mini_echo(); +} + +void test_expander_2(void) +{ + init_test("env"); + assert_mini_env(test_envp); +} + +void test_expander_3(void) +{ + init_test("cd .."); + assert_mini_cd(path_to_minishell, path_to_test); +} + +void test_expander_4(void) +{ + init_test(ft_strjoin("cd /Users/", ft_strjoin(getenv("USER"), "/Documents"))); + assert_mini_cd(ft_strjoin("/Users/", ft_strjoin(getenv("USER"), "/Documents")), path_to_minishell); +} + +void test_expander_5(void) +{ + init_test(ft_strjoin(ft_strjoin("cd ", path_to_minishell), "/src")); + assert_mini_cd(ft_strjoin(path_to_minishell, "/src"), ft_strjoin("/Users/", ft_strjoin(getenv("USER"), "/Documents"))); +} + +void test_expander_6(void) +{ + init_test("export test=\"a\""); + assert_mini_export("test", "test=a"); +} + +void test_expander_7(void) +{ + init_test("export test=\"b\""); + assert_mini_export("test", "test=b"); +} + +void test_expander_8(void) +{ + init_test("unset test"); + assert_mini_unset("test"); +} + +void test_expander_9(void) +{ + init_test("export test2=c"); + assert_mini_export("test2", "test2=c"); +} + +void test_expander_10(void) +{ + init_test("env"); + assert_mini_env(test_envp); +} + +void test_expander_11(void) +{ + init_test("pwd"); + assert_mini_pwd(getcwd(NULL, sizeof(NULL))); +} + +void test_expander_12(void) +{ + init_test("unset test2"); + assert_mini_unset("test2"); +} + +int main(int argc, char **argv, char **envp) +{ + UNITY_BEGIN(); + RUN_TEST(test_expander_1); + TEST_MESSAGE("1: {echo hello}"); + RUN_TEST(test_expander_2); + TEST_MESSAGE("2: env"); + RUN_TEST(test_expander_3); + TEST_MESSAGE("3: {cd ..}"); + RUN_TEST(test_expander_4); + TEST_MESSAGE("4: {cd {path to} Documents'}"); + RUN_TEST(test_expander_5); + TEST_MESSAGE("5: {cd {path from Documents to} minishell/src}"); + RUN_TEST(test_expander_6); + TEST_MESSAGE("6: {export test=\"a\"}"); + RUN_TEST(test_expander_7); + TEST_MESSAGE("7: {export test=\"b\"}"); + RUN_TEST(test_expander_8); + TEST_MESSAGE("8: {unset test}"); + RUN_TEST(test_expander_9); + TEST_MESSAGE("9: {export test2=c}"); + // RUN_TEST(test_expander_10); + // TEST_MESSAGE("10: {env}"); + RUN_TEST(test_expander_11); + TEST_MESSAGE("11: {pwd}"); + RUN_TEST(test_expander_12); + TEST_MESSAGE("12: {unset test2}"); + return UNITY_END(); +} \ No newline at end of file diff --git a/test/read_tester_output.sh b/test/read_tester_output.sh index 53dde7b..8434ff5 100755 --- a/test/read_tester_output.sh +++ b/test/read_tester_output.sh @@ -6,8 +6,17 @@ NC='\033[0m' FILE=$1 I=0 -FILE_NAME=$(echo "$FILE\n" | awk -F '/results/' '{ print $2 }') -printf "${BOLDBLUE}%s\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n${NC}" "$FILE_NAME" +FILE_NAME=$(echo "$FILE\n" | awk -F '/results/' '{ print $2 }' | awk -F 'Test' '{ print $2 }' | awk -F '.txt' '{ print $1 }' | tr 'a-z' 'A-Z') +center() { + termwidth="$(tput cols)" + padding="$(printf '%0.1s' ={1..500})" + printf "\n\n%*.*s${BOLDBLUE}%s${NC}%*.*s\n\n" 0 "$(((termwidth-2-${#1})/2))" "$padding" "$1" 0 "$(((termwidth-1-${#1})/2))" "$padding" +} +# printf "${BOLDBLUE}" +center " $FILE_NAME " +# printf "${NC}\n" + +# printf "\n${BOLDBLUE}* %s *\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n${NC}" "$FILE_NAME" while read line do if [ $I -eq 1 ] @@ -28,8 +37,18 @@ do fi if [[ "$line" == *"Tests"*"Failures"* ]] then - echo "\n\n-----------Results:-----------\n$line\n------------------------------" + FAILURES=$(echo $line | awk -F 'Tests' '{ print $2 }' | awk -F 'Failures' '{ print $1 }') + if [ $FAILURES -gt 0 ] + then + printf "\n\n${RED}Failures: $FAILURES\nyeah no......${NC}" + else + printf "\n\n🎉🥳${GREEN}you never cease to amaze me${NC}👏🎉" + fi break fi done < "$FILE" -printf "${BOLDBLUE}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~${NC}\n\n\n" \ No newline at end of file + +# printf "\n\n${BOLDBLUE}" +center "" +# printf "${NC}\n\n" +# printf "${BOLDBLUE}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~${NC}\n\n" \ No newline at end of file diff --git a/test/tmp.text b/test/tmp.text new file mode 100644 index 0000000..e69de29 diff --git a/tmp.text b/tmp.text new file mode 100644 index 0000000..e69de29 From 8d4e34ead2cbb3f33a9c56e54bd11bb330c8ffcb Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Wed, 30 Mar 2022 15:06:27 +0200 Subject: [PATCH 101/163] Expander tester finished --- includes/parser.h | 3 +- src/executor/executor.c | 39 +-------- src/utils/t_simple_cmds_utils.c | 10 ++- test/Makefile | 9 +- test/Testexecutor.c | 145 ++++++++++++++++++++++++++++++++ test/Testexecutor.ctttttt | 0 test/Testexpander.c | 145 ++++++++++++++++++++++++++++++++ test/Testexpander.cx | 129 ---------------------------- test/read_tester_output.sh | 21 +++-- 9 files changed, 319 insertions(+), 182 deletions(-) create mode 100644 test/Testexecutor.c delete mode 100644 test/Testexecutor.ctttttt create mode 100644 test/Testexpander.c delete mode 100644 test/Testexpander.cx diff --git a/includes/parser.h b/includes/parser.h index 497efa6..ead7d82 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/03/22 17:06:19 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/30 14:59:35 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -62,6 +62,7 @@ typedef struct s_simple_cmds int num_redirections; t_lexor *redirections; struct s_simple_cmds *next; + struct s_simple_cmds *prev; } t_simple_cmds; int parse_envp(t_tools *tools); diff --git a/src/executor/executor.c b/src/executor/executor.c index 5fdbd88..7357841 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/03/15 10:05:42 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/30 14:57:22 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,43 +34,6 @@ int find_cmd(char **args, char **paths, char **envp) int executor(t_tools *tools) { - int i; - int end[2]; - pid_t ret; - int status; - i = 0; - while (i < tools->pipes + 1) - { - check_infile(tools); - dup2(tools->in, 0); - close(tools->in); - if (i == tools->pipes) - check_outfile(tools); - else - { - pipe(end); - tools->out = end[1]; - tools->in = end[0]; - } - fprintf(stderr, "\nb4 dup out: in = %d out = %d\nend[0] = %d, end[1] = %d\n", tools->in, tools->out, end[0], end[1]); - dup2(tools->out, 1); - close(tools->out); - ret = fork(); - if (ret < 0) - { - perror("Fork: "); - exit(0); - } - if (ret == 0) - { - fprintf(stderr, "\nb4 find_cmd: %s\nin = %d out = %d\n", tools->simple_cmds->str[0], tools->in, tools->out); - find_cmd(tools->simple_cmds->str, tools->paths, tools->envp); - } - i++; - fprintf(stderr, "\nguess who's back\n"); - ft_simple_cmds_rm_first(&tools->simple_cmds); - } - waitpid(ret, &status, 0); return (0); } diff --git a/src/utils/t_simple_cmds_utils.c b/src/utils/t_simple_cmds_utils.c index 28d272f..3d42227 100644 --- a/src/utils/t_simple_cmds_utils.c +++ b/src/utils/t_simple_cmds_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/03/25 10:10:24 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/30 15:00:17 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -26,22 +26,28 @@ t_simple_cmds *ft_simple_cmdsnew(char **str, new_element->num_redirections = num_redirections; new_element->redirections = redirections; new_element->next = NULL; + new_element->prev = NULL; return (new_element); } void ft_simple_cmdsadd_back(t_simple_cmds **lst, t_simple_cmds *new) { t_simple_cmds *tmp; + t_simple_cmds *prev; tmp = *lst; - if (!(*lst)) + if (*lst == NULL) { *lst = new; return ; } while (tmp->next != NULL) + { + prev = tmp; tmp = tmp->next; + } tmp->next = new; + new->prev = tmp; } void ft_simple_cmds_rm_first(t_simple_cmds **lst) diff --git a/test/Makefile b/test/Makefile index 24499b5..80fa25d 100644 --- a/test/Makefile +++ b/test/Makefile @@ -18,6 +18,8 @@ PATHP = ../src/pipex/ PATHSU = ../src/utils/ PATHSB = ../src/builtins/ PATHSE = ../src/error/ +PATHSEXP = ../src/expander/ +PATHSEXC = ../src/executor/ PATHT = ../test/ PATHB = ../build/ PATHO = ../build/objs/ @@ -59,7 +61,6 @@ $(PATHO)Test%.$(TARGET_EXTENSION): $(LIBFT) $(OBJS) $(PATHO)Test%.o $(PATHO)%.o # $(PATHO)%.o:: $(PATHT)%.c $(PATHSL)%.c $(PATHSP)%.c $(PATHSU)%.c $(PATHSB)%.c $(PATHSE)%.c $(HEADERS) # $(COMPILE) $(CFLAGS) $(UNITY_CONFIG_DEFINES) $< -o $@ - $(PATHO)%.o:: $(PATHT)%.c $(HEADERS) @$(COMPILE) $(CFLAGS) $< -o $@ @@ -81,6 +82,12 @@ $(PATHO)%.o:: $(PATHSB)%.c $(HEADERS) $(PATHO)%.o:: $(PATHSE)%.c $(HEADERS) @$(COMPILE) $(CFLAGS) $< -o $@ +$(PATHO)%.o:: $(PATHSEXP)%.c $(HEADERS) + @$(COMPILE) $(CFLAGS) $< -o $@ + +$(PATHO)%.o:: $(PATHSEXC)%.c $(HEADERS) + @$(COMPILE) $(CFLAGS) $< -o $@ + $(PATHO)%.o:: $(PATHU)%.c $(PATHU)%.h @$(COMPILE) $(CFLAGS) $< -o $@ diff --git a/test/Testexecutor.c b/test/Testexecutor.c new file mode 100644 index 0000000..5b25464 --- /dev/null +++ b/test/Testexecutor.c @@ -0,0 +1,145 @@ +#include "unity.h" +#include "minishell.h" +#include "parser.h" +#include + +t_tools test_tools; +t_simple_cmds test_simple_cmd; + +void setUp(void) +{ + // set stuff up here +} + +void tearDown(void) +{ + // clean stuff up here +} + +void init_test(char *line) +{ + test_tools.args = line; + token_reader(&test_tools); + parser(&test_tools); + expander(&test_tools, test_tools.simple_cmds); +} + +void assert_executor(char **expected) +{ + TEST_ASSERT_EQUAL_STRING_ARRAY_MESSAGE(expected, test_tools.simple_cmds->str, (ft_strlen(*expected) - 2), "Incorrect variable replacement"); +} + +void test_executor_1(void) +{ + init_test("echo $USER"); + assert_executor(ft_split(ft_strjoin("echo ", getenv("USER")), ' ')); + ft_simple_cmdsclear(&test_tools.simple_cmds); +} + +void test_executor_2(void) +{ + init_test("echo \"$HOME\""); + assert_executor(ft_split(ft_strjoin("echo ", getenv("HOME")), ' ')); + ft_simple_cmdsclear(&test_tools.simple_cmds); +} + +void test_executor_3(void) +{ + init_test("echo \'$HOME\'"); + assert_executor(ft_split("echo \'$HOME\'", ' ')); + ft_simple_cmdsclear(&test_tools.simple_cmds); +} + +// void test_executor_4(void) +// { +// init_test(ft_strjoin("cd /Users/", ft_strjoin(getenv("USER"), "/Documents"))); +// assert_executor(ft_strjoin("/Users/", ft_strjoin(getenv("USER"), "/Documents")), path_to_minishell); +// ft_simple_cmdsclear(&test_tools.simple_cmds); +// } + +// void test_executor_5(void) +// { +// init_test(ft_strjoin(ft_strjoin("cd ", path_to_minishell), "/src")); +// assert_executor(ft_strjoin(path_to_minishell, "/src"), ft_strjoin("/Users/", ft_strjoin(getenv("USER"), "/Documents"))); +// ft_simple_cmdsclear(&test_tools.simple_cmds); +// } + +// void test_executor_6(void) +// { +// init_test("export test=\"a\""); +// assert_executor("test", "test=a"); +// ft_simple_cmdsclear(&test_tools.simple_cmds); +// } + +// void test_executor_7(void) +// { +// init_test("export test=\"b\""); +// assert_executor("test", "test=b"); +// ft_simple_cmdsclear(&test_tools.simple_cmds); +// } + +// void test_executor_8(void) +// { +// init_test("unset test"); +// assert_executor("test"); +// ft_simple_cmdsclear(&test_tools.simple_cmds); +// } + +// void test_executor_9(void) +// { +// init_test("export test2=c"); +// assert_executor("test2", "test2=c"); +// ft_simple_cmdsclear(&test_tools.simple_cmds); +// } + +// void test_executor_10(void) +// { +// init_test("env"); +// assert_executor(test_envp); +// ft_simple_cmdsclear(&test_tools.simple_cmds); +// } + +// void test_executor_11(void) +// { +// init_test("pwd"); +// assert_executor(getcwd(NULL, sizeof(NULL))); +// ft_simple_cmdsclear(&test_tools.simple_cmds); +// } + +// void test_executor_12(void) +// { +// init_test("unset test2"); +// assert_executor("test2"); +// ft_simple_cmdsclear(&test_tools.simple_cmds); +// } + +int main(int argc, char **argv, char **envp) +{ + test_tools.envp = ft_arrdup(envp); + UNITY_BEGIN(); + // RUN_TEST(test_executor_1); + // TEST_MESSAGE("1: {echo $USER}"); + // RUN_TEST(test_executor_2); + // TEST_MESSAGE("2: {echo \"$HOME\"}"); + // RUN_TEST(test_executor_3); + // TEST_MESSAGE("3: {echo \'$HOME\'}"); + // RUN_TEST(test_executor_4); + // TEST_MESSAGE("4: {cd {path to} Documents'}"); + // RUN_TEST(test_executor_5); + // TEST_MESSAGE("5: {cd {path from Documents to} minishell/src}"); + // RUN_TEST(test_executor_6); + // TEST_MESSAGE("6: {export test=\"a\"}"); + // RUN_TEST(test_executor_7); + // TEST_MESSAGE("7: {export test=\"b\"}"); + // RUN_TEST(test_executor_8); + // TEST_MESSAGE("8: {unset test}"); + // RUN_TEST(test_executor_9); + // TEST_MESSAGE("9: {export test2=c}"); + // // RUN_TEST(test_executor_10); + // // TEST_MESSAGE("10: {env}"); + // RUN_TEST(test_executor_11); + // TEST_MESSAGE("11: {pwd}"); + // RUN_TEST(test_executor_12); + // TEST_MESSAGE("12: {unset test2}"); + return UNITY_END(); +} \ No newline at end of file diff --git a/test/Testexecutor.ctttttt b/test/Testexecutor.ctttttt deleted file mode 100644 index e69de29..0000000 diff --git a/test/Testexpander.c b/test/Testexpander.c new file mode 100644 index 0000000..f94b1f1 --- /dev/null +++ b/test/Testexpander.c @@ -0,0 +1,145 @@ +#include "unity.h" +#include "minishell.h" +#include "parser.h" +#include + +t_tools test_tools; +t_simple_cmds test_simple_cmd; + +void setUp(void) +{ + // set stuff up here +} + +void tearDown(void) +{ + // clean stuff up here +} + +void init_test(char *line) +{ + test_tools.args = line; + token_reader(&test_tools); + parser(&test_tools); + expander(&test_tools, test_tools.simple_cmds); +} + +void assert_expander(char **expected) +{ + TEST_ASSERT_EQUAL_STRING_ARRAY_MESSAGE(expected, test_tools.simple_cmds->str, (ft_strlen(*expected) - 2), "Incorrect variable replacement"); +} + +void test_expander_1(void) +{ + init_test("echo $USER"); + assert_expander(ft_split(ft_strjoin("echo ", getenv("USER")), ' ')); + ft_simple_cmdsclear(&test_tools.simple_cmds); +} + +void test_expander_2(void) +{ + init_test("echo \"$HOME\""); + assert_expander(ft_split(ft_strjoin("echo ", getenv("HOME")), ' ')); + ft_simple_cmdsclear(&test_tools.simple_cmds); +} + +void test_expander_3(void) +{ + init_test("echo \'$HOME\'"); + assert_expander(ft_split("echo \'$HOME\'", ' ')); + ft_simple_cmdsclear(&test_tools.simple_cmds); +} + +// void test_expander_4(void) +// { +// init_test(ft_strjoin("cd /Users/", ft_strjoin(getenv("USER"), "/Documents"))); +// assert_expander(ft_strjoin("/Users/", ft_strjoin(getenv("USER"), "/Documents")), path_to_minishell); +// ft_simple_cmdsclear(&test_tools.simple_cmds); +// } + +// void test_expander_5(void) +// { +// init_test(ft_strjoin(ft_strjoin("cd ", path_to_minishell), "/src")); +// assert_expander(ft_strjoin(path_to_minishell, "/src"), ft_strjoin("/Users/", ft_strjoin(getenv("USER"), "/Documents"))); +// ft_simple_cmdsclear(&test_tools.simple_cmds); +// } + +// void test_expander_6(void) +// { +// init_test("export test=\"a\""); +// assert_expander("test", "test=a"); +// ft_simple_cmdsclear(&test_tools.simple_cmds); +// } + +// void test_expander_7(void) +// { +// init_test("export test=\"b\""); +// assert_expander("test", "test=b"); +// ft_simple_cmdsclear(&test_tools.simple_cmds); +// } + +// void test_expander_8(void) +// { +// init_test("unset test"); +// assert_expander("test"); +// ft_simple_cmdsclear(&test_tools.simple_cmds); +// } + +// void test_expander_9(void) +// { +// init_test("export test2=c"); +// assert_expander("test2", "test2=c"); +// ft_simple_cmdsclear(&test_tools.simple_cmds); +// } + +// void test_expander_10(void) +// { +// init_test("env"); +// assert_expander(test_envp); +// ft_simple_cmdsclear(&test_tools.simple_cmds); +// } + +// void test_expander_11(void) +// { +// init_test("pwd"); +// assert_expander(getcwd(NULL, sizeof(NULL))); +// ft_simple_cmdsclear(&test_tools.simple_cmds); +// } + +// void test_expander_12(void) +// { +// init_test("unset test2"); +// assert_expander("test2"); +// ft_simple_cmdsclear(&test_tools.simple_cmds); +// } + +int main(int argc, char **argv, char **envp) +{ + test_tools.envp = ft_arrdup(envp); + UNITY_BEGIN(); + RUN_TEST(test_expander_1); + TEST_MESSAGE("1: {echo $USER}"); + RUN_TEST(test_expander_2); + TEST_MESSAGE("2: {echo \"$HOME\"}"); + RUN_TEST(test_expander_3); + TEST_MESSAGE("3: {echo \'$HOME\'}"); + // RUN_TEST(test_expander_4); + // TEST_MESSAGE("4: {cd {path to} Documents'}"); + // RUN_TEST(test_expander_5); + // TEST_MESSAGE("5: {cd {path from Documents to} minishell/src}"); + // RUN_TEST(test_expander_6); + // TEST_MESSAGE("6: {export test=\"a\"}"); + // RUN_TEST(test_expander_7); + // TEST_MESSAGE("7: {export test=\"b\"}"); + // RUN_TEST(test_expander_8); + // TEST_MESSAGE("8: {unset test}"); + // RUN_TEST(test_expander_9); + // TEST_MESSAGE("9: {export test2=c}"); + // // RUN_TEST(test_expander_10); + // // TEST_MESSAGE("10: {env}"); + // RUN_TEST(test_expander_11); + // TEST_MESSAGE("11: {pwd}"); + // RUN_TEST(test_expander_12); + // TEST_MESSAGE("12: {unset test2}"); + return UNITY_END(); +} \ No newline at end of file diff --git a/test/Testexpander.cx b/test/Testexpander.cx deleted file mode 100644 index 0e2b941..0000000 --- a/test/Testexpander.cx +++ /dev/null @@ -1,129 +0,0 @@ -#include "unity.h" -#include "minishell.h" -#include "parser.h" -#include - -t_tools test_tools; -t_simple_cmds test_simple_cmd; - -void setUp(void) -{ - // set stuff up here -} - -void tearDown(void) -{ - // clean stuff up here -} - -void init_test(char *line) -{ - -} - -void assert_expander(void) -{ - TEST_ASSERT_NOT_NULL_MESSAGE(test_simple_cmd.expander, "expander function not connected."); -} - -void test_expander_1(void) -{ - init_test("echo hello"); - assert_mini_echo(); -} - -void test_expander_2(void) -{ - init_test("env"); - assert_mini_env(test_envp); -} - -void test_expander_3(void) -{ - init_test("cd .."); - assert_mini_cd(path_to_minishell, path_to_test); -} - -void test_expander_4(void) -{ - init_test(ft_strjoin("cd /Users/", ft_strjoin(getenv("USER"), "/Documents"))); - assert_mini_cd(ft_strjoin("/Users/", ft_strjoin(getenv("USER"), "/Documents")), path_to_minishell); -} - -void test_expander_5(void) -{ - init_test(ft_strjoin(ft_strjoin("cd ", path_to_minishell), "/src")); - assert_mini_cd(ft_strjoin(path_to_minishell, "/src"), ft_strjoin("/Users/", ft_strjoin(getenv("USER"), "/Documents"))); -} - -void test_expander_6(void) -{ - init_test("export test=\"a\""); - assert_mini_export("test", "test=a"); -} - -void test_expander_7(void) -{ - init_test("export test=\"b\""); - assert_mini_export("test", "test=b"); -} - -void test_expander_8(void) -{ - init_test("unset test"); - assert_mini_unset("test"); -} - -void test_expander_9(void) -{ - init_test("export test2=c"); - assert_mini_export("test2", "test2=c"); -} - -void test_expander_10(void) -{ - init_test("env"); - assert_mini_env(test_envp); -} - -void test_expander_11(void) -{ - init_test("pwd"); - assert_mini_pwd(getcwd(NULL, sizeof(NULL))); -} - -void test_expander_12(void) -{ - init_test("unset test2"); - assert_mini_unset("test2"); -} - -int main(int argc, char **argv, char **envp) -{ - UNITY_BEGIN(); - RUN_TEST(test_expander_1); - TEST_MESSAGE("1: {echo hello}"); - RUN_TEST(test_expander_2); - TEST_MESSAGE("2: env"); - RUN_TEST(test_expander_3); - TEST_MESSAGE("3: {cd ..}"); - RUN_TEST(test_expander_4); - TEST_MESSAGE("4: {cd {path to} Documents'}"); - RUN_TEST(test_expander_5); - TEST_MESSAGE("5: {cd {path from Documents to} minishell/src}"); - RUN_TEST(test_expander_6); - TEST_MESSAGE("6: {export test=\"a\"}"); - RUN_TEST(test_expander_7); - TEST_MESSAGE("7: {export test=\"b\"}"); - RUN_TEST(test_expander_8); - TEST_MESSAGE("8: {unset test}"); - RUN_TEST(test_expander_9); - TEST_MESSAGE("9: {export test2=c}"); - // RUN_TEST(test_expander_10); - // TEST_MESSAGE("10: {env}"); - RUN_TEST(test_expander_11); - TEST_MESSAGE("11: {pwd}"); - RUN_TEST(test_expander_12); - TEST_MESSAGE("12: {unset test2}"); - return UNITY_END(); -} \ No newline at end of file diff --git a/test/read_tester_output.sh b/test/read_tester_output.sh index 8434ff5..b9bfddb 100755 --- a/test/read_tester_output.sh +++ b/test/read_tester_output.sh @@ -10,13 +10,10 @@ FILE_NAME=$(echo "$FILE\n" | awk -F '/results/' '{ print $2 }' | awk -F 'Test' ' center() { termwidth="$(tput cols)" padding="$(printf '%0.1s' ={1..500})" - printf "\n\n%*.*s${BOLDBLUE}%s${NC}%*.*s\n\n" 0 "$(((termwidth-2-${#1})/2))" "$padding" "$1" 0 "$(((termwidth-1-${#1})/2))" "$padding" + printf "\n%*.*s${BOLDBLUE}%s${NC}%*.*s\n" 0 "$(((termwidth-2-${#1})/2))" "$padding" "$1" 0 "$(((termwidth-1-${#1})/2))" "$padding" } -# printf "${BOLDBLUE}" center " $FILE_NAME " -# printf "${NC}\n" - -# printf "\n${BOLDBLUE}* %s *\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n${NC}" "$FILE_NAME" +echo '' while read line do if [ $I -eq 1 ] @@ -37,18 +34,20 @@ do fi if [[ "$line" == *"Tests"*"Failures"* ]] then - FAILURES=$(echo $line | awk -F 'Tests' '{ print $2 }' | awk -F 'Failures' '{ print $1 }') + FAILURES=$(echo $line | awk -F 'Tests ' '{ print $2 }' | awk -F ' Failures' '{ print $1 }') + TESTS=$(echo $line | awk -F 'Tests' '{ print $1 }') + PASSED=$(expr $TESTS - $FAILURES) + printf "\n\n$PASSED/$TESTS\n\n" if [ $FAILURES -gt 0 ] then - printf "\n\n${RED}Failures: $FAILURES\nyeah no......${NC}" + printf "${RED}yeah no......${NC}" else - printf "\n\n🎉🥳${GREEN}you never cease to amaze me${NC}👏🎉" + printf "🎉🥳${GREEN}you never cease to amaze me${NC}👏🎉" fi break fi done < "$FILE" -# printf "\n\n${BOLDBLUE}" +echo '' center "" -# printf "${NC}\n\n" -# printf "${BOLDBLUE}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~${NC}\n\n" \ No newline at end of file +echo '\n' From 1fa5704a3e0236df90b78c6ab3ccb6b182b50a1e Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 30 Mar 2022 16:27:06 +0200 Subject: [PATCH 102/163] expander is a mess --- src/builtins/mini_export.c | 5 +- src/expander/expander.c | 110 ++++++++++++++++++++++++++++++------- 2 files changed, 94 insertions(+), 21 deletions(-) diff --git a/src/builtins/mini_export.c b/src/builtins/mini_export.c index 61d8239..ddd8269 100644 --- a/src/builtins/mini_export.c +++ b/src/builtins/mini_export.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:07:21 by fpolycar #+# #+# */ -/* Updated: 2022/03/24 16:07:25 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/30 14:53:22 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -56,6 +56,9 @@ char **whileloop_add_var(char **arr, char **rtn, char *str) i = 0; while (arr[i] != NULL) { + if (str[0] == '\'' && str[ft_strlen(str)] == '\'' + && str[0] == '\"' && str[ft_strlen(str)] == '\"') + ft_strtrim(str, "\'\""); if (arr[i + 1] == NULL) { rtn[i] = ft_strdup(str); diff --git a/src/expander/expander.c b/src/expander/expander.c index f72d646..57036e0 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/03/29 18:32:49 by alfred ######## odam.nl */ +/* Updated: 2022/03/30 16:20:30 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -27,12 +27,70 @@ size_t dollar_sign(char *str) return (0); } +int arg_length(char **envp) +{ + int i; + + i = 0; + while (envp[i]) + i++; + return (i); +} + + + +// } + +// size_t str_replace_with_var_lenght(char *str, t_tools *tools, t_simple_cmds *tmp) +// { +// size_t len; +// int i; +// int k; +// int process; + +// len = 0; +// i = 0; +// process = 0; +// while (str[i]) +// { +// if (str[i] == '$') +// { +// k = 0; +// while (tools->envp[k]) +// { +// if (ft_strncmp(str + i + 1, +// tools->envp[k], equal_sign(tools->envp[k]) - 1) == 0 +// && (str[equal_sign(tools->envp[k]) + i] == '\0' +// || str[equal_sign(tools->envp[k]) + i] == '$' +// || str[equal_sign(tools->envp[k]) + i] == ' ' +// || str[equal_sign(tools->envp[k]) + i] == '\"')) +// { +// // len += ft_strlen(tools->envp[k] + equal_sign(tools->envp[k])); +// i += equal_sign(tools->envp[k]); +// process = 1; +// } +// if (process == 0 && k == arg_length(tools->envp) - 1) +// i += ft_strlen(str + i); +// k++; +// } +// } +// else +// { +// len++; +// i++; +// } +// } +// return(len); +// } + void expander_loop(t_tools *tools, t_simple_cmds *tmp, int i) { int j; int k; + int len_non_var; char *arr_tmp; - char *tmp_transformed; + char *tmp_transformed = NULL; + char *tmp_tmp; j = 0; arr_tmp = tmp->str[i]; @@ -50,33 +108,42 @@ void expander_loop(t_tools *tools, t_simple_cmds *tmp, int i) || arr_tmp[equal_sign(tools->envp[k]) + j] == ' ' || arr_tmp[equal_sign(tools->envp[k]) + j] == '\"')) { - if (j == 0) - { - free(tmp->str[i]); - tmp->str[i] = ft_substr(tools->envp[k], + if (!tmp_transformed) + tmp_transformed = ft_substr(tools->envp[k], equal_sign(tools->envp[k]), ft_strlen(tools->envp[k])); - } - // // printf("test\n"); - // // before = ft_substr(arr_tmp, 0, dollar_sign(arr_tmp)); else { - before = tmp->str[i]; - free(tmp->str[i]); - tmp->str[i] = ft_strjoin(before, ft_substr(tools->envp[j], - equal_sign(tools->envp[j]), - ft_strlen(tools->envp[j]))); + tmp_tmp = tmp_transformed; + free(tmp_transformed); + tmp_transformed = ft_strjoin(tmp_tmp, ft_substr(tools->envp[k], + equal_sign(tools->envp[k]), + ft_strlen(tools->envp[k]))); } - printf("arr = %s\n\n", arr_tmp); + j += equal_sign(tools->envp[k]); } - k++; } } - j++; + else + { + if (!tmp_transformed) + { + tmp_transformed = ft_strdup(arr_tmp); + ft_strlcpy(tmp_transformed, tmp_transformed, 2); + } + else + { + tmp_tmp = tmp_transformed; + free(tmp_transformed); + tmp_transformed = ft_strjoin(tmp_transformed, tmp_tmp); + } + j++; + printf("tmp_transformed = %s\n\n", tmp_transformed); + } } - free(tmp->str[i]); - tmp->str[i] = tmp_transformed; + // free(tmp->str[i]); + // tmp->str[i] = tmp_transformed; } void expander(t_tools *tools, t_simple_cmds *simple_cmds) @@ -88,10 +155,13 @@ void expander(t_tools *tools, t_simple_cmds *simple_cmds) tmp = simple_cmds; while (tmp) { - while (tmp->str[i]) + while (tmp->str[i] && dollar_sign(tmp->str[i]) != 0) { if (tmp->str[i][0] != '\'' && tmp->str[i][ft_strlen(tmp->str[i])] != '\'') + { + // printf("%zu\n", str_replace_with_var_lenght(tmp->str[i], tools, tmp)); expander_loop(tools, tmp, i); + } i++; } tmp = tmp->next; From ecc86a6e469f24d6509e4972d1a0bd03ac875399 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Thu, 31 Mar 2022 16:23:39 +0200 Subject: [PATCH 103/163] expander done --- includes/minishell.h | 8 +- src/expander/expander.c | 164 +++++++++------------------------ src/expander/expanders_utils.c | 63 +++++++++++++ src/utils/minishell_loop.c | 6 +- 4 files changed, 115 insertions(+), 126 deletions(-) create mode 100644 src/expander/expanders_utils.c diff --git a/includes/minishell.h b/includes/minishell.h index d50ea91..1fcce6e 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/03/25 11:41:03 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/31 16:19:25 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -29,7 +29,11 @@ int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); int reset_tools(t_tools *tools); -void expander(t_tools *tools, t_simple_cmds *simple_cmds); +void expander(t_tools *tools); +size_t dollar_sign(char *str); +char *char_to_str(char c); +int after_dollar_lenght(char *str, int j); +void free_things(char *tmp2, t_tools *tools, int i); //builtins int (*builtin_arr(char *str))(t_tools *tools, t_simple_cmds *simple_cmd); diff --git a/src/expander/expander.c b/src/expander/expander.c index 57036e0..cda3659 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,162 +6,84 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/03/30 16:20:30 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/31 16:22:13 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" #include "builtins.h" -size_t dollar_sign(char *str) +int loop_if_dollar_sign(t_tools *tools, char *arr_tmp, int j, int i) { - size_t i; + int k; + int ret; + char *tmp; + char *tmp2; - i = 0; - while (str[i]) + k = 0; + ret = 0; + while (tools->envp[k]) { - if (str[i] == '$') - return (i + 1); - i++; + if (ft_strncmp(arr_tmp + j + 1, + tools->envp[k], equal_sign(tools->envp[k]) - 1) == 0 + && after_dollar_lenght(arr_tmp, j) - j + == equal_sign(tools->envp[k])) + { + tmp2 = ft_strdup(tools->envp[k] + equal_sign(tools->envp[k])); + tmp = ft_strjoin(tools->simple_cmds->str[i], tmp2); + free_things(tmp2, tools, i); + tools->simple_cmds->str[i] = tmp; + ret = equal_sign(tools->envp[k]); + } + k++; } - return (0); -} - -int arg_length(char **envp) -{ - int i; - - i = 0; - while (envp[i]) - i++; - return (i); + if (ret == 0) + ret = after_dollar_lenght(arr_tmp, j) - j; + return (ret); } - - -// } - -// size_t str_replace_with_var_lenght(char *str, t_tools *tools, t_simple_cmds *tmp) -// { -// size_t len; -// int i; -// int k; -// int process; - -// len = 0; -// i = 0; -// process = 0; -// while (str[i]) -// { -// if (str[i] == '$') -// { -// k = 0; -// while (tools->envp[k]) -// { -// if (ft_strncmp(str + i + 1, -// tools->envp[k], equal_sign(tools->envp[k]) - 1) == 0 -// && (str[equal_sign(tools->envp[k]) + i] == '\0' -// || str[equal_sign(tools->envp[k]) + i] == '$' -// || str[equal_sign(tools->envp[k]) + i] == ' ' -// || str[equal_sign(tools->envp[k]) + i] == '\"')) -// { -// // len += ft_strlen(tools->envp[k] + equal_sign(tools->envp[k])); -// i += equal_sign(tools->envp[k]); -// process = 1; -// } -// if (process == 0 && k == arg_length(tools->envp) - 1) -// i += ft_strlen(str + i); -// k++; -// } -// } -// else -// { -// len++; -// i++; -// } -// } -// return(len); -// } - -void expander_loop(t_tools *tools, t_simple_cmds *tmp, int i) +char *detect_dollar_sign(t_tools *tools, char *str, int i) { int j; - int k; - int len_non_var; char *arr_tmp; - char *tmp_transformed = NULL; - char *tmp_tmp; + char *tmp; + char *tmp2; j = 0; - arr_tmp = tmp->str[i]; + arr_tmp = ft_strdup(str); while (arr_tmp[j]) { + init_stri(i, j, tools); if (arr_tmp[j] == '$') - { - k = 0; - while (tools->envp[k]) - { - if (ft_strncmp(arr_tmp + j + 1, - tools->envp[k], equal_sign(tools->envp[k]) - 1) == 0 - && (arr_tmp[equal_sign(tools->envp[k]) + j] == '\0' - || arr_tmp[equal_sign(tools->envp[k]) + j] == '$' - || arr_tmp[equal_sign(tools->envp[k]) + j] == ' ' - || arr_tmp[equal_sign(tools->envp[k]) + j] == '\"')) - { - if (!tmp_transformed) - tmp_transformed = ft_substr(tools->envp[k], - equal_sign(tools->envp[k]), - ft_strlen(tools->envp[k])); - else - { - tmp_tmp = tmp_transformed; - free(tmp_transformed); - tmp_transformed = ft_strjoin(tmp_tmp, ft_substr(tools->envp[k], - equal_sign(tools->envp[k]), - ft_strlen(tools->envp[k]))); - } - j += equal_sign(tools->envp[k]); - } - k++; - } - } + j += loop_if_dollar_sign(tools, arr_tmp, j, i); else { - if (!tmp_transformed) - { - tmp_transformed = ft_strdup(arr_tmp); - ft_strlcpy(tmp_transformed, tmp_transformed, 2); - } - else - { - tmp_tmp = tmp_transformed; - free(tmp_transformed); - tmp_transformed = ft_strjoin(tmp_transformed, tmp_tmp); - } + tmp2 = char_to_str(arr_tmp[j]); + tmp = ft_strjoin(tools->simple_cmds->str[i], tmp2); + free_things(tmp2, tools, i); + tools->simple_cmds->str[i] = tmp; + str = tmp; j++; - printf("tmp_transformed = %s\n\n", tmp_transformed); } } - // free(tmp->str[i]); - // tmp->str[i] = tmp_transformed; + free(arr_tmp); + return (tools->simple_cmds->str[i]); } -void expander(t_tools *tools, t_simple_cmds *simple_cmds) +void expander(t_tools *tools) { t_simple_cmds *tmp; int i; i = 0; - tmp = simple_cmds; + tmp = tools->simple_cmds; while (tmp) { - while (tmp->str[i] && dollar_sign(tmp->str[i]) != 0) + while (tmp->str[i]) { - if (tmp->str[i][0] != '\'' && tmp->str[i][ft_strlen(tmp->str[i])] != '\'') - { - // printf("%zu\n", str_replace_with_var_lenght(tmp->str[i], tools, tmp)); - expander_loop(tools, tmp, i); - } + if (tmp->str[i][0] != '\'' + && tmp->str[i][ft_strlen(tmp->str[i])] != '\'') + detect_dollar_sign(tools, tmp->str[i], i); i++; } tmp = tmp->next; diff --git a/src/expander/expanders_utils.c b/src/expander/expanders_utils.c new file mode 100644 index 0000000..a595e12 --- /dev/null +++ b/src/expander/expanders_utils.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* expanders_utils.c :+: :+: */ +/* +:+ */ +/* By: fpolycar +#+ */ +/* +#+ */ +/* Created: 2022/03/31 16:08:47 by fpolycar #+# #+# */ +/* Updated: 2022/03/31 16:22:46 by fpolycar ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" +#include "builtins.h" + +void init_stri(int i, int j, t_tools *tools) +{ + if (j == 0) + { + free(tools->simple_cmds->str[i]); + tools->simple_cmds->str[i] = ft_strdup("\0"); + } +} + +void free_things(char *tmp2, t_tools *tools, int i) +{ + free(tmp2); + free(tools->simple_cmds->str[i]); +} + +size_t dollar_sign(char *str) +{ + size_t i; + + i = 0; + while (str[i]) + { + if (str[i] == '$') + return (i + 1); + i++; + } + return (0); +} + +char *char_to_str(char c) +{ + char *str; + + str = ft_calloc(sizeof(char), 2); + str[0] = c; + return (str); +} + +int after_dollar_lenght(char *str, int j) +{ + int i; + + i = j + 1; + while (str[i] != '\0' && str[i] != '$' && str[i] != ' ' + && str[i] != '\"') + i++; + return (i); +} diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 27dde55..85686b5 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/03/29 15:34:16 by alfred ######## odam.nl */ +/* Updated: 2022/03/31 16:04:08 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -45,8 +45,8 @@ int minishell_loop(t_tools *tools) if (!token_reader(tools)) ft_error(1, tools); parser(tools); - expander(tools, tools->simple_cmds); - printf("%s\n", *tools->simple_cmds->str); + expander(tools); + builtin_arr(tools->simple_cmds->str[0])(tools, tools->simple_cmds); // executor(&tools); reset_tools(tools); return (1); From 1f179b60d31eee46331e29b5b566417e5148e665 Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Thu, 31 Mar 2022 16:59:53 +0200 Subject: [PATCH 104/163] Executor start!! --- includes/utils.h | 6 +-- src/error/error_handeling.c | 6 ++- src/executor/check_redirections.c | 4 +- src/executor/executor.c | 47 ++++++++++++++++++++---- src/utils/minishell_loop.c | 7 +--- src/utils/t_simple_cmds_utils.c | 8 ++-- test/{Testexecutor.c => Testexecutor.cx} | 0 test/Testtoken_reader.c | 18 ++++++++- 8 files changed, 72 insertions(+), 24 deletions(-) rename test/{Testexecutor.c => Testexecutor.cx} (100%) diff --git a/includes/utils.h b/includes/utils.h index 57ffd10..1a336b5 100644 --- a/includes/utils.h +++ b/includes/utils.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:36:23 by mgraaf #+# #+# */ -/* Updated: 2022/03/29 15:09:02 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/31 16:15:49 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ t_simple_cmds *ft_simple_cmdsnew(char **str, void ft_simple_cmdsadd_back(t_simple_cmds **lst, t_simple_cmds *new); void ft_simple_cmds_rm_first(t_simple_cmds **lst); void ft_simple_cmdsclear(t_simple_cmds **lst); -t_simple_cmds *ft_simple_cmdslast(t_simple_cmds *map); +t_simple_cmds *ft_simple_cmdsfirst(t_simple_cmds *map); //t_lexor_utils t_lexor *ft_lexornew(char *str, int token); @@ -43,4 +43,4 @@ int add_node(char *str, t_tokens token, t_lexor **lexor_list); t_tokens check_token(int c); int handle_token(char *str, int i, t_lexor **lexor_list); -#endif \ No newline at end of file +#endif diff --git a/src/error/error_handeling.c b/src/error/error_handeling.c index 3c14ee4..2905417 100755 --- a/src/error/error_handeling.c +++ b/src/error/error_handeling.c @@ -6,7 +6,7 @@ /* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/02/25 11:39:57 by mgraaf #+# #+# */ -/* Updated: 2022/02/25 12:10:51 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/31 13:26:49 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -46,7 +46,7 @@ int check_outfile(t_tools *tools) = tools->simple_cmds->redirections->next; } if (i == 0) - tools->out = dup(1); + tools->out = dup(STDOUT_FILENO); tools->simple_cmds->redirections = start; return (tools->out); } diff --git a/src/executor/executor.c b/src/executor/executor.c index 7357841..619e4f8 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,34 +6,65 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/03/30 14:57:22 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/31 16:43:13 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "executor.h" -int find_cmd(char **args, char **paths, char **envp) +int find_cmd(t_simple_cmds *cmd, t_tools *tools, int end[2], int fd_in) { int i; char *mycmd; + if (cmd->prev && dup2(fd_in, STDIN_FILENO) < 0) + ft_error(4, tools); + if (cmd->next && dup2(end[1], STDOUT_FILENO) < 0) + ft_error(4, tools); + close(end[0]); + close(end[1]); + if (cmd->prev) + close(fd_in); i = 0; - fprintf(stderr, "in find_cmd\n"); - while (paths[i]) + while (tools->paths[i]) { - mycmd = ft_strjoin(paths[i], args[0]); - fprintf(stderr, "%i\n", i); + mycmd = ft_strjoin(tools->paths[i], cmd->str[0]); if (!access(mycmd, F_OK)) - execve(mycmd, args, envp); + execve(mycmd, cmd->str, tools->envp); free(mycmd); i++; } - printf("HELO\n"); + exit(EXIT_FAILURE); return (1); } int executor(t_tools *tools) { + int end[2]; + pid_t ret; + int status; + int fd_in; + fd_in = STDIN_FILENO; + while (tools->simple_cmds) + { + if (tools->simple_cmds->next) + pipe(end); + ret = fork(); + if (ret < 0) + ft_error(5, tools); + if (ret == 0) + find_cmd(tools->simple_cmds, tools, end, fd_in); + close(end[1]); + if (tools->simple_cmds->prev) + close(fd_in); + fd_in = end[0]; + if (tools->simple_cmds->next) + tools->simple_cmds = tools->simple_cmds->next; + else + break ; + } + waitpid(ret, &status, 0); + tools->simple_cmds = ft_simple_cmdsfirst(tools->simple_cmds); return (0); } diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 29e03ed..4f7237a 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/03/28 12:56:13 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/31 16:43:58 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -16,9 +16,6 @@ int minishell_loop(t_tools *tools); int implement_tools(t_tools *tools) { - tools->in = dup(0); - tools->out = dup(1); - tools->err = dup(2); tools->simple_cmds = NULL; tools->lexor_list = NULL; return (1); @@ -47,7 +44,7 @@ int minishell_loop(t_tools *tools) parser(tools); // expander(tools, tools->simple_cmds); // printf("%s\n", *tools->simple_cmds->str); - // executor(&tools); + executor(tools); reset_tools(tools); return (1); } \ No newline at end of file diff --git a/src/utils/t_simple_cmds_utils.c b/src/utils/t_simple_cmds_utils.c index 3d42227..88c0aee 100644 --- a/src/utils/t_simple_cmds_utils.c +++ b/src/utils/t_simple_cmds_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/03/30 15:00:17 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/31 16:14:40 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -82,16 +82,16 @@ void ft_simple_cmdsclear(t_simple_cmds **lst) *lst = NULL; } -t_simple_cmds *ft_simple_cmdslast(t_simple_cmds *map) +t_simple_cmds *ft_simple_cmdsfirst(t_simple_cmds *map) { int i; i = 0; if (!map) return (NULL); - while (map->next != NULL) + while (map->prev != NULL) { - map = map->next; + map = map->prev; i++; } return (map); diff --git a/test/Testexecutor.c b/test/Testexecutor.cx similarity index 100% rename from test/Testexecutor.c rename to test/Testexecutor.cx diff --git a/test/Testtoken_reader.c b/test/Testtoken_reader.c index 0c93940..5ecfe27 100644 --- a/test/Testtoken_reader.c +++ b/test/Testtoken_reader.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/28 11:12:08 by fpolycar #+# #+# */ -/* Updated: 2022/03/28 12:55:35 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/31 12:22:21 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -135,6 +135,20 @@ void test_lexer_11(void) assert_token(0, "test"); } +void test_lexer_12(void) +{ + init_test("test|test|test>test Date: Fri, 1 Apr 2022 09:30:43 +0200 Subject: [PATCH 105/163] Expander builtins and redirections work ! --- tmp.text => a | 0 b | 0 c | 21 +++++++ d | 0 includes/executor.h | 10 +++- includes/minishell.h | 4 +- includes/parser.h | 6 +- src/builtins/mini_exit.c | 7 ++- src/error/error_handeling.c | 6 +- src/executor/check_redirections.c | 96 ++++++++++++++++++------------- src/executor/executor.c | 35 +++++++---- src/utils/minishell_loop.c | 11 ++-- test/Testexpander.c | 2 +- 13 files changed, 130 insertions(+), 68 deletions(-) rename tmp.text => a (100%) create mode 100644 b create mode 100644 c create mode 100644 d diff --git a/tmp.text b/a similarity index 100% rename from tmp.text rename to a diff --git a/b b/b new file mode 100644 index 0000000..e69de29 diff --git a/c b/c new file mode 100644 index 0000000..ff4a1f5 --- /dev/null +++ b/c @@ -0,0 +1,21 @@ +total 176 +drwxr-xr-x@ 20 maiadegraaf staff 640 31 Mar 17:53 . +drwxr-xr-x@ 16 maiadegraaf staff 512 15 Mar 14:50 .. +drwxr-xr-x 17 maiadegraaf staff 544 31 Mar 17:02 .git +-rw-r--r-- 1 maiadegraaf staff 37 16 Mar 09:56 .gitignore +-rw-r--r-- 1 maiadegraaf staff 85 16 Mar 11:21 .gitmodules +drwxr-xr-x 5 maiadegraaf staff 160 1 Mar 19:41 .vscode +-rw-r--r-- 1 maiadegraaf staff 2210 29 Mar 09:46 Makefile +drwxr-xr-x 18 maiadegraaf staff 576 16 Mar 11:21 Unity +-rw-r--r-- 1 maiadegraaf staff 0 31 Mar 17:53 a +-rw-r--r-- 1 maiadegraaf staff 0 31 Mar 17:53 b +drwxr-xr-x 4 maiadegraaf staff 128 31 Mar 17:08 build +-rw-r--r-- 1 maiadegraaf staff 0 31 Mar 17:53 c +-rw-r--r-- 1 maiadegraaf staff 0 31 Mar 17:53 d +drwxr-xr-x 9 maiadegraaf staff 288 31 Mar 17:02 includes +drwxr-xr-x 3 maiadegraaf staff 96 16 Feb 10:44 libraries +-rwxr-xr-x 1 maiadegraaf staff 76262 31 Mar 17:46 minishell +drwxr-xr-x 3 maiadegraaf staff 96 22 Mar 15:13 minishell.dSYM +drwxr-xr-x 12 maiadegraaf staff 384 30 Mar 12:29 src +drwxr-xr-x 11 maiadegraaf staff 352 31 Mar 12:22 test +drwxr-xr-x 3 maiadegraaf staff 96 1 Mar 18:46 utilities diff --git a/d b/d new file mode 100644 index 0000000..e69de29 diff --git a/includes/executor.h b/includes/executor.h index eb7616a..009bba1 100644 --- a/includes/executor.h +++ b/includes/executor.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:17:39 by mgraaf #+# #+# */ -/* Updated: 2022/02/25 11:53:28 by mgraaf ######## odam.nl */ +/* Updated: 2022/03/31 17:45:11 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,8 +14,12 @@ # define EXECUTOR_H # include "minishell.h" -int check_outfile(t_tools *tools); -int check_infile(t_tools *tools); +// check_redirectoins +int check_outfile(t_lexor *redirections); +int check_infile(t_lexor *redirections); +int handle_redirections(t_simple_cmds *cmd, t_tools *tools); + +// executor int executor(t_tools *tools); #endif \ No newline at end of file diff --git a/includes/minishell.h b/includes/minishell.h index 1fcce6e..74eaf5f 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/03/31 16:19:25 by fpolycar ######## odam.nl */ +/* Updated: 2022/03/31 17:21:59 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ # include # include # include +# include # include "pipex.h" # include "parser.h" # include "utils.h" @@ -29,6 +30,7 @@ int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); int reset_tools(t_tools *tools); +void init_stri(int i, int j, t_tools *tools); void expander(t_tools *tools); size_t dollar_sign(char *str); char *char_to_str(char c); diff --git a/includes/parser.h b/includes/parser.h index ead7d82..7ac189a 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/03/30 14:59:35 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/31 17:20:12 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -47,12 +47,10 @@ typedef struct s_tools char **envp; struct s_simple_cmds *simple_cmds; t_lexor *lexor_list; - int in; - int out; - int err; char *pwd; char *old_pwd; int pipes; + bool end; } t_tools; typedef struct s_simple_cmds diff --git a/src/builtins/mini_exit.c b/src/builtins/mini_exit.c index 2c93d0f..9ffed3c 100644 --- a/src/builtins/mini_exit.c +++ b/src/builtins/mini_exit.c @@ -6,7 +6,7 @@ /* By: maiadegraaf args); - exit(EXIT_SUCCESS); + printf("EXIT HERE\n"); + tools->end = true; + exit (EXIT_SUCCESS); return (EXIT_SUCCESS); } diff --git a/src/error/error_handeling.c b/src/error/error_handeling.c index 2905417..9b8f537 100755 --- a/src/error/error_handeling.c +++ b/src/error/error_handeling.c @@ -6,7 +6,7 @@ /* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/02/25 11:39:57 by mgraaf #+# #+# */ -/* Updated: 2022/03/31 13:26:49 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/31 17:46:29 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "executor.h" -void check_append_outfile(t_tools *tools) +int check_append_outfile(t_lexor *redirections) { - if (tools->simple_cmds->redirections->token == GREAT) - tools->out = open(tools->simple_cmds->redirections->str, + int fd; + + if (redirections->token == GREAT) + fd = open(redirections->str, O_CREAT | O_RDWR | O_TRUNC | O_APPEND, 0644); else - tools->out = open(tools->simple_cmds->redirections->str, + fd = open(redirections->str, O_CREAT | O_RDWR | O_TRUNC, 0644); + return (fd); } -int check_outfile(t_tools *tools) +int check_outfile(t_lexor *redirections) { t_lexor *start; - int i; + int fd; - i = 0; - start = tools->simple_cmds->redirections; - while (tools->simple_cmds->redirections) + start = redirections; + fd = 0; + while (redirections) { - if (tools->simple_cmds->redirections->token == GREAT - || tools->simple_cmds->redirections->token == GREAT_GREAT) + if (redirections->token == GREAT + || redirections->token == GREAT_GREAT) { - check_append_outfile(tools); - if (tools->out < 0) - { - perror("Open "); - // do more stuff (ie lots of freeing) - } - i++; + fd = check_append_outfile(redirections); + if (fd < 0) + return (-1); } - tools->simple_cmds->redirections - = tools->simple_cmds->redirections->next; + redirections + = redirections->next; } - if (i == 0) - tools->out = dup(STDOUT_FILENO); - tools->simple_cmds->redirections = start; - return (tools->out); + redirections = start; + return (fd); } -int check_infile(t_tools *tools) +int check_infile(t_lexor *redirections) { t_lexor *start; + int fd; - start = tools->simple_cmds->redirections; - while (tools->simple_cmds->redirections) + start = redirections; + fd = 0; + while (redirections) { - if (tools->simple_cmds->redirections->token == LESS) + if (redirections->token == LESS) { - tools->in = open(tools->simple_cmds->redirections->str, O_RDONLY); - if (tools->in < 0) - { - perror("Open "); - // do more stuff (ie lots of freeing) - } + fd = open(redirections->str, O_RDONLY); + if (fd < 0) + return (-1); } - tools->simple_cmds->redirections - = tools->simple_cmds->redirections->next; + redirections + = redirections->next; } - tools->simple_cmds->redirections = start; - return (tools->in); + redirections = start; + return (fd); +} + +int handle_redirections(t_simple_cmds *cmd, t_tools *tools) +{ + int fd_in; + int fd_out; + + fd_in = check_infile(cmd->redirections); + if (fd_in < 0) + ft_error(7, tools); + if (fd_in > 0 && dup2(fd_in, STDIN_FILENO) < 0) + ft_error(4, tools); + if (fd_in > 0) + close(fd_in); + fd_out = check_outfile(cmd->redirections); + if (fd_out < 0) + ft_error (6, tools); + if (fd_out > 0 && dup2(fd_out, STDOUT_FILENO) < 0) + ft_error(4, tools); + if (fd_out > 0) + close(fd_out); + return (EXIT_SUCCESS); } diff --git a/src/executor/executor.c b/src/executor/executor.c index 619e4f8..c8534f6 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,25 +6,17 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/03/31 16:43:13 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/31 17:45:44 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "executor.h" -int find_cmd(t_simple_cmds *cmd, t_tools *tools, int end[2], int fd_in) +int find_cmd(t_simple_cmds *cmd, t_tools *tools) { int i; char *mycmd; - if (cmd->prev && dup2(fd_in, STDIN_FILENO) < 0) - ft_error(4, tools); - if (cmd->next && dup2(end[1], STDOUT_FILENO) < 0) - ft_error(4, tools); - close(end[0]); - close(end[1]); - if (cmd->prev) - close(fd_in); i = 0; while (tools->paths[i]) { @@ -38,6 +30,27 @@ int find_cmd(t_simple_cmds *cmd, t_tools *tools, int end[2], int fd_in) return (1); } +void fork_cmd(t_simple_cmds *cmd, t_tools *tools, int end[2], int fd_in) +{ + if (cmd->prev && dup2(fd_in, STDIN_FILENO) < 0) + ft_error(4, tools); + if (cmd->next && dup2(end[1], STDOUT_FILENO) < 0) + ft_error(4, tools); + close(end[0]); + close(end[1]); + if (cmd->prev) + close(fd_in); + if (cmd->redirections) + handle_redirections(cmd, tools); + if (cmd->builtin) + { + cmd->builtin(tools, cmd); + exit(EXIT_SUCCESS); + } + else + find_cmd(cmd, tools); +} + int executor(t_tools *tools) { int end[2]; @@ -54,7 +67,7 @@ int executor(t_tools *tools) if (ret < 0) ft_error(5, tools); if (ret == 0) - find_cmd(tools->simple_cmds, tools, end, fd_in); + fork_cmd(tools->simple_cmds, tools, end, fd_in); close(end[1]); if (tools->simple_cmds->prev) close(fd_in); diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 41379a5..afbbe21 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/03/31 16:43:58 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/03/31 17:24:49 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,6 +18,7 @@ int implement_tools(t_tools *tools) { tools->simple_cmds = NULL; tools->lexor_list = NULL; + tools->end = false; return (1); } @@ -28,8 +29,9 @@ int reset_tools(t_tools *tools) implement_tools(tools); tools->pipes = 0; // system("leaks minishell"); + if (tools->end == true) + exit (EXIT_SUCCESS); minishell_loop(tools); - // exit (EXIT_SUCCESS); return (1); } @@ -42,9 +44,8 @@ int minishell_loop(t_tools *tools) if (!token_reader(tools)) ft_error(1, tools); parser(tools); - expander(tools); - builtin_arr(tools->simple_cmds->str[0])(tools, tools->simple_cmds); - // executor(&tools); + // expander(tools); + executor(tools); reset_tools(tools); return (1); } \ No newline at end of file diff --git a/test/Testexpander.c b/test/Testexpander.c index f94b1f1..54f83ed 100644 --- a/test/Testexpander.c +++ b/test/Testexpander.c @@ -21,7 +21,7 @@ void init_test(char *line) test_tools.args = line; token_reader(&test_tools); parser(&test_tools); - expander(&test_tools, test_tools.simple_cmds); + expander(&test_tools); } void assert_expander(char **expected) From 2370aa683bd387c8a226736be36f767058b32999 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Fri, 1 Apr 2022 16:09:33 +0200 Subject: [PATCH 106/163] seg fault + quotes export --- Makefile | 3 +-- includes/minishell.h | 4 +++- includes/parser.h | 4 +++- src/builtins/mini_echo.c | 6 +++--- src/builtins/mini_env.c | 2 +- src/builtins/mini_exit.c | 11 +++++++---- src/builtins/mini_export.c | 17 +++++++++-------- src/builtins/utils_builtins.c | 18 +++++++++++++++++- src/executor/executor.c | 25 ++++++++++++++++++++++++- src/expander/expander.c | 4 ++-- src/utils/minishell_loop.c | 7 ++++--- test.c | 1 + 12 files changed, 75 insertions(+), 27 deletions(-) create mode 100644 test.c diff --git a/Makefile b/Makefile index 611abe3..2ff647f 100644 --- a/Makefile +++ b/Makefile @@ -30,8 +30,7 @@ src = $(wildcard $(PATHS)*.c) \ OBJS = $(addprefix $(PATHO), $(notdir $(patsubst %.c, %.o, $(src)))) -FLAGS = #-Wall -Werror -Wextra -g -#-fsanitize=address +FLAGS = -Wall -Werror -Wextra -g #-fsanitize=address LIBFT = ./libraries/libft/libft.a diff --git a/includes/minishell.h b/includes/minishell.h index 1fcce6e..f954d86 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/03/31 16:19:25 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/01 16:07:47 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,6 +34,8 @@ size_t dollar_sign(char *str); char *char_to_str(char c); int after_dollar_lenght(char *str, int j); void free_things(char *tmp2, t_tools *tools, int i); +void print_parser(t_simple_cmds simple_cmds); +char *delete_quotes_value(char *str); //builtins int (*builtin_arr(char *str))(t_tools *tools, t_simple_cmds *simple_cmd); diff --git a/includes/parser.h b/includes/parser.h index 497efa6..e22e21d 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/03/22 17:06:19 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/01 16:07:58 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -53,6 +53,8 @@ typedef struct s_tools char *pwd; char *old_pwd; int pipes; + bool end; + int end_pid; } t_tools; typedef struct s_simple_cmds diff --git a/src/builtins/mini_echo.c b/src/builtins/mini_echo.c index b33ecb7..1e166ba 100644 --- a/src/builtins/mini_echo.c +++ b/src/builtins/mini_echo.c @@ -6,7 +6,7 @@ /* By: maiadegraaf str[1], "-n", ft_strlen(simple_cmd->str[1]))) - print_lines(2, simple_cmd->str); + print_lines(2, simple_cmd->str, STDOUT_FILENO); else { - print_lines(1, simple_cmd->str); + print_lines(1, simple_cmd->str, STDOUT_FILENO); ft_putchar_fd('\n', STDOUT_FILENO); } return (EXIT_SUCCESS); diff --git a/src/builtins/mini_env.c b/src/builtins/mini_env.c index 4dfa102..e83518b 100644 --- a/src/builtins/mini_env.c +++ b/src/builtins/mini_env.c @@ -6,7 +6,7 @@ /* By: maiadegraaf //probably have to add some frees in here. int mini_exit(t_tools *tools, t_simple_cmds *simple_cmd) { (void) simple_cmd; - free(tools->args); - exit(EXIT_SUCCESS); + printf("%d\n", tools->end_pid); + ft_putstr_fd("exit ", STDERR_FILENO); + tools->end = true; + kill(0, SIGINT); + exit (EXIT_SUCCESS); return (EXIT_SUCCESS); } diff --git a/src/builtins/mini_export.c b/src/builtins/mini_export.c index ddd8269..0a0e321 100644 --- a/src/builtins/mini_export.c +++ b/src/builtins/mini_export.c @@ -6,24 +6,26 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:07:21 by fpolycar #+# #+# */ -/* Updated: 2022/03/30 14:53:22 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/01 16:01:31 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "builtins.h" -int variable_exist(t_tools *tools, t_simple_cmds *simple_cmd) +int variable_exist(t_tools *tools, char *str) { int i; i = 0; + if (str[equal_sign(str)] == '"' || str[equal_sign(str)] == '\'') + delete_quotes_value(str); while (tools->envp[i]) { if (ft_strncmp(tools->envp[i], - simple_cmd->str[1], equal_sign(tools->envp[i])) == 0) + str, equal_sign(tools->envp[i])) == 0) { free(tools->envp[i]); - tools->envp[i] = ft_strdup(simple_cmd->str[1]); + tools->envp[i] = ft_strdup(str); return (1); } i++; @@ -56,9 +58,6 @@ char **whileloop_add_var(char **arr, char **rtn, char *str) i = 0; while (arr[i] != NULL) { - if (str[0] == '\'' && str[ft_strlen(str)] == '\'' - && str[0] == '\"' && str[ft_strlen(str)] == '\"') - ft_strtrim(str, "\'\""); if (arr[i + 1] == NULL) { rtn[i] = ft_strdup(str); @@ -82,6 +81,8 @@ char **add_var(char **arr, char *str) size_t i; i = 0; + if (str[equal_sign(str)] == '"' || str[equal_sign(str)] == '\'') + delete_quotes_value(str); while (arr[i] != NULL) i++; rtn = ft_calloc(sizeof(char *), i + 2); @@ -102,7 +103,7 @@ int mini_export(t_tools *tools, t_simple_cmds *simple_cmd) { if (check_parameter(simple_cmd->str[1]) == 0) { - if (variable_exist(tools, simple_cmd) == 0) + if (variable_exist(tools, simple_cmd->str[1]) == 0) { if (simple_cmd->str[1]) { diff --git a/src/builtins/utils_builtins.c b/src/builtins/utils_builtins.c index af4674b..87f3675 100644 --- a/src/builtins/utils_builtins.c +++ b/src/builtins/utils_builtins.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:04:47 by fpolycar #+# #+# */ -/* Updated: 2022/03/29 14:25:16 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/01 16:01:46 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -36,3 +36,19 @@ size_t equal_sign(char *str) } return (0); } + +char *delete_quotes_value(char *str) +{ + char **split_quotes; + + split_quotes = ft_split(str, '"'); + if (!split_quotes[1]) + { + free_arr(split_quotes); + split_quotes = ft_split(str, '\''); + } + free(str); + str = ft_strjoin(split_quotes[0], split_quotes[1]); + free_arr(split_quotes); + return (str); +} diff --git a/src/executor/executor.c b/src/executor/executor.c index 5fdbd88..04db00b 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/03/15 10:05:42 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/01 16:08:27 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -32,6 +32,28 @@ int find_cmd(char **args, char **paths, char **envp) return (1); } +void fork_cmd(t_simple_cmds *cmd, t_tools *tools, int end[2], int fd_in) +{ + if (cmd->prev && dup2(fd_in, STDIN_FILENO) < 0) + ft_error(4, tools); + if (cmd->next && dup2(end[1], STDOUT_FILENO) < 0) + ft_error(4, tools); + close(end[0]); + close(end[1]); + if (cmd->prev) + close(fd_in); + if (cmd->redirections) + handle_redirections(cmd, tools); + if (cmd->builtin) + { + printf("BUILTIN\n"); + cmd->builtin(tools, cmd); + exit(EXIT_SUCCESS); + } + else + find_cmd(cmd, tools); +} + int executor(t_tools *tools) { int i; @@ -71,6 +93,7 @@ int executor(t_tools *tools) fprintf(stderr, "\nguess who's back\n"); ft_simple_cmds_rm_first(&tools->simple_cmds); } + tools->end_pid = ret; waitpid(ret, &status, 0); return (0); } diff --git a/src/expander/expander.c b/src/expander/expander.c index cda3659..3ea17f5 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/03/31 16:22:13 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/01 10:28:49 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -27,7 +27,7 @@ int loop_if_dollar_sign(t_tools *tools, char *arr_tmp, int j, int i) if (ft_strncmp(arr_tmp + j + 1, tools->envp[k], equal_sign(tools->envp[k]) - 1) == 0 && after_dollar_lenght(arr_tmp, j) - j - == equal_sign(tools->envp[k])) + == (int) equal_sign(tools->envp[k])) { tmp2 = ft_strdup(tools->envp[k] + equal_sign(tools->envp[k])); tmp = ft_strjoin(tools->simple_cmds->str[i], tmp2); diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 85686b5..340512c 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/03/31 16:04:08 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/01 16:08:32 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -45,9 +45,10 @@ int minishell_loop(t_tools *tools) if (!token_reader(tools)) ft_error(1, tools); parser(tools); - expander(tools); + if (dollar_sign(tools->args) != 0) + expander(tools); builtin_arr(tools->simple_cmds->str[0])(tools, tools->simple_cmds); - // executor(&tools); + // executor(tools); reset_tools(tools); return (1); } \ No newline at end of file diff --git a/test.c b/test.c new file mode 100644 index 0000000..03402b7 --- /dev/null +++ b/test.c @@ -0,0 +1 @@ +/Users/fpolycar/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Users/fpolycar/.brew/bin:/Applications/Visual Studio Code.app/Contents/Resources/app/bin:/Applications/Visual Studio Code.app/Contents/Resources/app/bin From 59c1a3c6be8486c356621c08ca60d2aefc4570c3 Mon Sep 17 00:00:00 2001 From: Alfred Polycarpe Date: Tue, 5 Apr 2022 22:05:52 +0200 Subject: [PATCH 107/163] segfault - --- includes/minishell.h | 2 +- src/builtins/mini_echo.c | 4 ++-- src/builtins/mini_env.c | 4 ++-- src/expander/expander.c | 26 ++++++++++++++------------ src/utils/minishell_loop.c | 17 ++++++++--------- 5 files changed, 27 insertions(+), 26 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index 46cc299..83f490a 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/04/04 16:09:24 by alfred ######## odam.nl */ +/* Updated: 2022/04/05 15:24:44 by alfred ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/builtins/mini_echo.c b/src/builtins/mini_echo.c index ed220a2..d5dbd3b 100644 --- a/src/builtins/mini_echo.c +++ b/src/builtins/mini_echo.c @@ -6,7 +6,7 @@ /* By: maiadegraaf str[i]) { - ft_putchar_fd('\n', tools->out); + ft_putchar_fd('\n', STDOUT_FILENO); return (EXIT_SUCCESS); } if (!ft_strncmp(simple_cmd->str[1], "-n", ft_strlen(simple_cmd->str[1]))) diff --git a/src/builtins/mini_env.c b/src/builtins/mini_env.c index 430de54..12c22ee 100644 --- a/src/builtins/mini_env.c +++ b/src/builtins/mini_env.c @@ -6,7 +6,7 @@ /* By: maiadegraaf envp[i]); tools->envp[i] = tmp; } - ft_putendl_fd(tools->envp[i], tools->out); + ft_putendl_fd(tools->envp[i], STDOUT_FILENO); i++; } return (EXIT_SUCCESS); diff --git a/src/expander/expander.c b/src/expander/expander.c index 3ea17f5..406527e 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/01 10:28:49 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/05 17:12:35 by alfred ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -42,7 +42,7 @@ int loop_if_dollar_sign(t_tools *tools, char *arr_tmp, int j, int i) return (ret); } -char *detect_dollar_sign(t_tools *tools, char *str, int i) +char *detect_dollar_sign(t_tools *tools, int i) { int j; char *arr_tmp; @@ -50,7 +50,7 @@ char *detect_dollar_sign(t_tools *tools, char *str, int i) char *tmp2; j = 0; - arr_tmp = ft_strdup(str); + arr_tmp = ft_strdup(tools->simple_cmds->str[i]); while (arr_tmp[j]) { init_stri(i, j, tools); @@ -62,7 +62,6 @@ char *detect_dollar_sign(t_tools *tools, char *str, int i) tmp = ft_strjoin(tools->simple_cmds->str[i], tmp2); free_things(tmp2, tools, i); tools->simple_cmds->str[i] = tmp; - str = tmp; j++; } } @@ -72,20 +71,23 @@ char *detect_dollar_sign(t_tools *tools, char *str, int i) void expander(t_tools *tools) { - t_simple_cmds *tmp; int i; i = 0; - tmp = tools->simple_cmds; - while (tmp) + while (tools->simple_cmds->next) { - while (tmp->str[i]) + while (tools->simple_cmds->str[i]) { - if (tmp->str[i][0] != '\'' - && tmp->str[i][ft_strlen(tmp->str[i])] != '\'') - detect_dollar_sign(tools, tmp->str[i], i); + if (tools->simple_cmds->str[i][0] != '\'' + && tools->simple_cmds->str[i][ft_strlen(tools->simple_cmds->str[i])] != '\'') + detect_dollar_sign(tools, i); i++; } - tmp = tmp->next; + if (tools->simple_cmds->next) + tools->simple_cmds = tools->simple_cmds->next; } + while (i-- > 0) + tools->simple_cmds = tools->simple_cmds->prev; + // printf("%s\n", tools->simple_cmds->str[0]); + // tools->simple_cmds = ft_simple_cmdsfirst(tools->simple_cmds); } diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index e1da2c9..01e4731 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,11 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -<<<<<<< HEAD -/* Updated: 2022/04/01 16:08:32 by fpolycar ######## odam.nl */ -======= -/* Updated: 2022/03/31 17:24:49 by maiadegraaf ######## odam.nl */ ->>>>>>> maia_2 +/* Updated: 2022/04/05 16:07:24 by alfred ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -42,16 +38,19 @@ int reset_tools(t_tools *tools) int minishell_loop(t_tools *tools) { tools->args = readline("minishell$ "); + if (!tools->args) + { + tools->args = readline("minishell$ "); + } add_history(tools->args); if (!count_quotes(tools->args)) ft_error(2, tools); if (!token_reader(tools)) ft_error(1, tools); parser(tools); - if (dollar_sign(tools->args) != 0) - expander(tools); - builtin_arr(tools->simple_cmds->str[0])(tools, tools->simple_cmds); - // executor(tools); + expander(tools); + // builtin_arr(tools->simple_cmds->str[0])(tools, tools->simple_cmds); + executor(tools); reset_tools(tools); return (1); } \ No newline at end of file From 7ba8e089f91509478d2ffce9fb7df6c69f203e6c Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 6 Apr 2022 09:55:44 +0200 Subject: [PATCH 108/163] segfault done --- src/expander/expander.c | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/src/expander/expander.c b/src/expander/expander.c index 406527e..4d2557e 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/05 17:12:35 by alfred ######## odam.nl */ +/* Updated: 2022/04/06 09:55:27 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -72,22 +72,20 @@ char *detect_dollar_sign(t_tools *tools, int i) void expander(t_tools *tools) { int i; + t_simple_cmds *tmp; - i = 0; - while (tools->simple_cmds->next) + tmp = tools->simple_cmds; + while (tmp->next) { - while (tools->simple_cmds->str[i]) + i = 0; + while (tmp->str[i]) { - if (tools->simple_cmds->str[i][0] != '\'' - && tools->simple_cmds->str[i][ft_strlen(tools->simple_cmds->str[i])] != '\'') + if (tmp->str[i][ft_strlen(tmp->str[i])] != '\'') detect_dollar_sign(tools, i); + printf("%s\n", tmp->str[i]); i++; } - if (tools->simple_cmds->next) - tools->simple_cmds = tools->simple_cmds->next; + if (tmp->next) + tmp = tmp->next; } - while (i-- > 0) - tools->simple_cmds = tools->simple_cmds->prev; - // printf("%s\n", tools->simple_cmds->str[0]); - // tools->simple_cmds = ft_simple_cmdsfirst(tools->simple_cmds); } From d96ccf1bb464ee57702ca92b09984d0f9187af0c Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 6 Apr 2022 10:02:08 +0200 Subject: [PATCH 109/163] segfault done --- a | 1 + src/expander/expander.c | 8 +++----- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/a b/a index e69de29..03402b7 100644 --- a/a +++ b/a @@ -0,0 +1 @@ +/Users/fpolycar/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Users/fpolycar/.brew/bin:/Applications/Visual Studio Code.app/Contents/Resources/app/bin:/Applications/Visual Studio Code.app/Contents/Resources/app/bin diff --git a/src/expander/expander.c b/src/expander/expander.c index 4d2557e..6663fb4 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/06 09:55:27 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/06 10:01:47 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -75,17 +75,15 @@ void expander(t_tools *tools) t_simple_cmds *tmp; tmp = tools->simple_cmds; - while (tmp->next) + while (tmp != NULL) { i = 0; while (tmp->str[i]) { if (tmp->str[i][ft_strlen(tmp->str[i])] != '\'') detect_dollar_sign(tools, i); - printf("%s\n", tmp->str[i]); i++; } - if (tmp->next) - tmp = tmp->next; + tmp = tmp->next; } } From f151b52270565a4d1e4ba673b8b35b7640af71fc Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Thu, 7 Apr 2022 13:27:11 +0200 Subject: [PATCH 110/163] seg fault and expander modify --- Makefile | 2 +- a | 1 - includes/minishell.h | 4 +-- src/builtins/mini_cd.c | 2 +- src/builtins/mini_env.c | 2 +- src/builtins/mini_exit.c | 7 ++-- src/error/error_handeling.c | 4 ++- src/executor/executor.c | 18 ++++++---- src/expander/expander.c | 63 ++++++++++++++++------------------ src/expander/expanders_utils.c | 8 +---- src/utils/minishell_loop.c | 11 +++--- 11 files changed, 58 insertions(+), 64 deletions(-) diff --git a/Makefile b/Makefile index 2ff647f..423ca91 100644 --- a/Makefile +++ b/Makefile @@ -30,7 +30,7 @@ src = $(wildcard $(PATHS)*.c) \ OBJS = $(addprefix $(PATHO), $(notdir $(patsubst %.c, %.o, $(src)))) -FLAGS = -Wall -Werror -Wextra -g #-fsanitize=address +FLAGS = -Wall -Werror -Wextra #-g3 -fsanitize=address LIBFT = ./libraries/libft/libft.a diff --git a/a b/a index 03402b7..e69de29 100644 --- a/a +++ b/a @@ -1 +0,0 @@ -/Users/fpolycar/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Users/fpolycar/.brew/bin:/Applications/Visual Studio Code.app/Contents/Resources/app/bin:/Applications/Visual Studio Code.app/Contents/Resources/app/bin diff --git a/includes/minishell.h b/includes/minishell.h index 83f490a..9a0cf32 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/04/05 15:24:44 by alfred ######## odam.nl */ +/* Updated: 2022/04/07 11:39:35 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -31,7 +31,7 @@ int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); int reset_tools(t_tools *tools); void init_stri(int i, int j, t_tools *tools); -void expander(t_tools *tools); +char **expander(t_tools *tools, char **str); size_t dollar_sign(char *str); char *char_to_str(char c); int after_dollar_lenght(char *str, int j); diff --git a/src/builtins/mini_cd.c b/src/builtins/mini_cd.c index 58a4e5d..ccc701e 100644 --- a/src/builtins/mini_cd.c +++ b/src/builtins/mini_cd.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:17:04 by mgraaf #+# #+# */ -/* Updated: 2022/03/24 15:43:08 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/06 10:44:11 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/builtins/mini_env.c b/src/builtins/mini_env.c index 12c22ee..b95a60f 100644 --- a/src/builtins/mini_env.c +++ b/src/builtins/mini_env.c @@ -6,7 +6,7 @@ /* By: maiadegraaf end_pid); - ft_putstr_fd("exit ", STDERR_FILENO); + ft_putstr_fd("exit", STDERR_FILENO); + tools->end = true; kill(0, SIGINT); - exit (EXIT_SUCCESS); + // exit (EXIT_SUCCESS); return (EXIT_SUCCESS); } diff --git a/src/error/error_handeling.c b/src/error/error_handeling.c index 9b8f537..35a77ac 100755 --- a/src/error/error_handeling.c +++ b/src/error/error_handeling.c @@ -6,7 +6,7 @@ /* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/04/04 16:11:45 by alfred ######## odam.nl */ +/* Updated: 2022/04/07 13:26:06 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,6 +18,12 @@ int find_cmd(t_simple_cmds *cmd, t_tools *tools) char *mycmd; i = 0; + cmd->str = expander(tools, cmd->str); + if (cmd->builtin) + { + cmd->builtin(tools, cmd); + exit(EXIT_SUCCESS); + } while (tools->paths[i]) { mycmd = ft_strjoin(tools->paths[i], cmd->str[0]); @@ -42,11 +48,11 @@ void fork_cmd(t_simple_cmds *cmd, t_tools *tools, int end[2], int fd_in) close(fd_in); if (cmd->redirections) handle_redirections(cmd, tools); - if (cmd->builtin) - { - cmd->builtin(tools, cmd); - exit(EXIT_SUCCESS); - } + // if (cmd->builtin) + // { + // cmd->builtin(tools, cmd); + // exit(EXIT_SUCCESS); + // } else find_cmd(cmd, tools); } diff --git a/src/expander/expander.c b/src/expander/expander.c index 6663fb4..85c9db5 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,14 +6,14 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/06 10:01:47 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/07 13:24:33 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" #include "builtins.h" -int loop_if_dollar_sign(t_tools *tools, char *arr_tmp, int j, int i) +int loop_if_dollar_sign(t_tools *tools, char **str, int j, int i) { int k; int ret; @@ -24,66 +24,61 @@ int loop_if_dollar_sign(t_tools *tools, char *arr_tmp, int j, int i) ret = 0; while (tools->envp[k]) { - if (ft_strncmp(arr_tmp + j + 1, + if (ft_strncmp(str[i] + j + 1, tools->envp[k], equal_sign(tools->envp[k]) - 1) == 0 - && after_dollar_lenght(arr_tmp, j) - j + && after_dollar_lenght(str[i], j) - j == (int) equal_sign(tools->envp[k])) { tmp2 = ft_strdup(tools->envp[k] + equal_sign(tools->envp[k])); - tmp = ft_strjoin(tools->simple_cmds->str[i], tmp2); - free_things(tmp2, tools, i); - tools->simple_cmds->str[i] = tmp; + tmp = ft_strjoin(str[i], tmp2); + free(tmp2); + str[i] = tmp; ret = equal_sign(tools->envp[k]); } k++; } if (ret == 0) - ret = after_dollar_lenght(arr_tmp, j) - j; + ret = after_dollar_lenght(str[i], j) - j; return (ret); } -char *detect_dollar_sign(t_tools *tools, int i) +char *detect_dollar_sign(t_tools *tools, char **str, int i) { int j; - char *arr_tmp; char *tmp; char *tmp2; j = 0; - arr_tmp = ft_strdup(tools->simple_cmds->str[i]); - while (arr_tmp[j]) + tmp = NULL; + while (str[i][j]) { - init_stri(i, j, tools); - if (arr_tmp[j] == '$') - j += loop_if_dollar_sign(tools, arr_tmp, j, i); + if (str[i][j] == '$') + j += loop_if_dollar_sign(tools, str, j, i); else { - tmp2 = char_to_str(arr_tmp[j]); - tmp = ft_strjoin(tools->simple_cmds->str[i], tmp2); - free_things(tmp2, tools, i); - tools->simple_cmds->str[i] = tmp; + tmp2 = char_to_str(str[i][j]); + if (!tmp) + tmp = ft_strdup(tmp2); + else + tmp = ft_strjoin(tmp, tmp2); + free(tmp2); j++; } } - free(arr_tmp); - return (tools->simple_cmds->str[i]); + return (tmp); } -void expander(t_tools *tools) +char **expander(t_tools *tools, char **str) { int i; - t_simple_cmds *tmp; - tmp = tools->simple_cmds; - while (tmp != NULL) - { - i = 0; - while (tmp->str[i]) - { - if (tmp->str[i][ft_strlen(tmp->str[i])] != '\'') - detect_dollar_sign(tools, i); - i++; - } - tmp = tmp->next; + i = 0; + while (str[i] != NULL) + { + if (str[i][ft_strlen(str[i])] != '\'' + && dollar_sign(str[i]) != 0) + str[i] = detect_dollar_sign(tools, str, i); + i++; } + return (str); } diff --git a/src/expander/expanders_utils.c b/src/expander/expanders_utils.c index a595e12..9cd1ef9 100644 --- a/src/expander/expanders_utils.c +++ b/src/expander/expanders_utils.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/31 16:08:47 by fpolycar #+# #+# */ -/* Updated: 2022/03/31 16:22:46 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/07 11:40:57 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -22,12 +22,6 @@ void init_stri(int i, int j, t_tools *tools) } } -void free_things(char *tmp2, t_tools *tools, int i) -{ - free(tmp2); - free(tools->simple_cmds->str[i]); -} - size_t dollar_sign(char *str) { size_t i; diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 01e4731..ac8562d 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/05 16:07:24 by alfred ######## odam.nl */ +/* Updated: 2022/04/07 13:26:18 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -37,19 +37,16 @@ int reset_tools(t_tools *tools) int minishell_loop(t_tools *tools) { + int i; + + i =0; tools->args = readline("minishell$ "); - if (!tools->args) - { - tools->args = readline("minishell$ "); - } add_history(tools->args); if (!count_quotes(tools->args)) ft_error(2, tools); if (!token_reader(tools)) ft_error(1, tools); parser(tools); - expander(tools); - // builtin_arr(tools->simple_cmds->str[0])(tools, tools->simple_cmds); executor(tools); reset_tools(tools); return (1); From c663f8587abb87debfa62996baa9d08eb41ccfc0 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Thu, 7 Apr 2022 14:04:21 +0200 Subject: [PATCH 111/163] expander in executor done --- a | 1 + f | 1 + src/executor/executor.c | 20 +++++++------------- src/expander/expander.c | 6 +++--- w | 1 + 5 files changed, 13 insertions(+), 16 deletions(-) create mode 100644 f create mode 100644 w diff --git a/a b/a index e69de29..14fcd0e 100644 --- a/a +++ b/a @@ -0,0 +1 @@ +rer diff --git a/f b/f new file mode 100644 index 0000000..44a1a41 --- /dev/null +++ b/f @@ -0,0 +1 @@ +minishell$ çç \ No newline at end of file diff --git a/src/executor/executor.c b/src/executor/executor.c index 7a02101..706739e 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/04/07 13:26:06 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/07 13:56:57 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,12 +18,6 @@ int find_cmd(t_simple_cmds *cmd, t_tools *tools) char *mycmd; i = 0; - cmd->str = expander(tools, cmd->str); - if (cmd->builtin) - { - cmd->builtin(tools, cmd); - exit(EXIT_SUCCESS); - } while (tools->paths[i]) { mycmd = ft_strjoin(tools->paths[i], cmd->str[0]); @@ -48,11 +42,11 @@ void fork_cmd(t_simple_cmds *cmd, t_tools *tools, int end[2], int fd_in) close(fd_in); if (cmd->redirections) handle_redirections(cmd, tools); - // if (cmd->builtin) - // { - // cmd->builtin(tools, cmd); - // exit(EXIT_SUCCESS); - // } + if (cmd->builtin) + { + cmd->builtin(tools, cmd); + exit(EXIT_SUCCESS); + } else find_cmd(cmd, tools); } @@ -67,6 +61,7 @@ int executor(t_tools *tools) fd_in = STDIN_FILENO; while (tools->simple_cmds) { + tools->simple_cmds->str = expander(tools, tools->simple_cmds->str); if (tools->simple_cmds->next) pipe(end); ret = fork(); @@ -83,7 +78,6 @@ int executor(t_tools *tools) else break ; } - tools->end_pid = ret; waitpid(ret, &status, 0); tools->simple_cmds = ft_simple_cmdsfirst(tools->simple_cmds); return (0); diff --git a/src/expander/expander.c b/src/expander/expander.c index 85c9db5..1a94827 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/07 13:24:33 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/07 14:03:03 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -73,10 +73,10 @@ char **expander(t_tools *tools, char **str) int i; i = 0; + (void) tools; while (str[i] != NULL) { - if (str[i][ft_strlen(str[i])] != '\'' - && dollar_sign(str[i]) != 0) + if (str[i][ft_strlen(str[i]) - 1] != '\'' && dollar_sign(str[i]) != 0) str[i] = detect_dollar_sign(tools, str, i); i++; } diff --git a/w b/w new file mode 100644 index 0000000..0d99181 --- /dev/null +++ b/w @@ -0,0 +1 @@ +minishell$ \ No newline at end of file From 6773964b7e77e0f3e8db9624dc00c78fe5947a28 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Thu, 7 Apr 2022 15:50:22 +0200 Subject: [PATCH 112/163] expander fully fonctional --- "\020" | 1 + a => ahhhhhysssrer | 0 b | 0 c | 21 ----------- d | 0 f | 1 - includes/minishell.h | 3 +- src/executor/executor.c | 4 +- src/expander/expander.c | 15 +++----- src/expander/expander_str.c | 74 +++++++++++++++++++++++++++++++++++++ test.c | 1 - w | 1 - 12 files changed, 86 insertions(+), 35 deletions(-) create mode 100644 "\020" rename a => ahhhhhysssrer (100%) delete mode 100644 b delete mode 100644 c delete mode 100644 d delete mode 100644 f create mode 100644 src/expander/expander_str.c delete mode 100644 test.c delete mode 100644 w diff --git "a/\020" "b/\020" new file mode 100644 index 0000000..06e7df9 --- /dev/null +++ "b/\020" @@ -0,0 +1 @@ +tets diff --git a/a b/ahhhhhysssrer similarity index 100% rename from a rename to ahhhhhysssrer diff --git a/b b/b deleted file mode 100644 index e69de29..0000000 diff --git a/c b/c deleted file mode 100644 index ff4a1f5..0000000 --- a/c +++ /dev/null @@ -1,21 +0,0 @@ -total 176 -drwxr-xr-x@ 20 maiadegraaf staff 640 31 Mar 17:53 . -drwxr-xr-x@ 16 maiadegraaf staff 512 15 Mar 14:50 .. -drwxr-xr-x 17 maiadegraaf staff 544 31 Mar 17:02 .git --rw-r--r-- 1 maiadegraaf staff 37 16 Mar 09:56 .gitignore --rw-r--r-- 1 maiadegraaf staff 85 16 Mar 11:21 .gitmodules -drwxr-xr-x 5 maiadegraaf staff 160 1 Mar 19:41 .vscode --rw-r--r-- 1 maiadegraaf staff 2210 29 Mar 09:46 Makefile -drwxr-xr-x 18 maiadegraaf staff 576 16 Mar 11:21 Unity --rw-r--r-- 1 maiadegraaf staff 0 31 Mar 17:53 a --rw-r--r-- 1 maiadegraaf staff 0 31 Mar 17:53 b -drwxr-xr-x 4 maiadegraaf staff 128 31 Mar 17:08 build --rw-r--r-- 1 maiadegraaf staff 0 31 Mar 17:53 c --rw-r--r-- 1 maiadegraaf staff 0 31 Mar 17:53 d -drwxr-xr-x 9 maiadegraaf staff 288 31 Mar 17:02 includes -drwxr-xr-x 3 maiadegraaf staff 96 16 Feb 10:44 libraries --rwxr-xr-x 1 maiadegraaf staff 76262 31 Mar 17:46 minishell -drwxr-xr-x 3 maiadegraaf staff 96 22 Mar 15:13 minishell.dSYM -drwxr-xr-x 12 maiadegraaf staff 384 30 Mar 12:29 src -drwxr-xr-x 11 maiadegraaf staff 352 31 Mar 12:22 test -drwxr-xr-x 3 maiadegraaf staff 96 1 Mar 18:46 utilities diff --git a/d b/d deleted file mode 100644 index e69de29..0000000 diff --git a/f b/f deleted file mode 100644 index 44a1a41..0000000 --- a/f +++ /dev/null @@ -1 +0,0 @@ -minishell$ çç \ No newline at end of file diff --git a/includes/minishell.h b/includes/minishell.h index 9a0cf32..0b651ae 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/04/07 11:39:35 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/07 14:19:34 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -32,6 +32,7 @@ int find_pwd(t_tools *tools); int reset_tools(t_tools *tools); void init_stri(int i, int j, t_tools *tools); char **expander(t_tools *tools, char **str); +char *expander_str(t_tools *tools, char *str); size_t dollar_sign(char *str); char *char_to_str(char c); int after_dollar_lenght(char *str, int j); diff --git a/src/executor/executor.c b/src/executor/executor.c index 706739e..8b44fa9 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/04/07 13:56:57 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/07 15:46:28 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -62,6 +62,8 @@ int executor(t_tools *tools) while (tools->simple_cmds) { tools->simple_cmds->str = expander(tools, tools->simple_cmds->str); + if (tools->simple_cmds->redirections->str) + tools->simple_cmds->redirections->str = expander_str(tools, tools->simple_cmds->redirections->str); if (tools->simple_cmds->next) pipe(end); ret = fork(); diff --git a/src/expander/expander.c b/src/expander/expander.c index 1a94827..d4d035a 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,18 +6,17 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/07 14:03:03 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/07 15:49:18 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" #include "builtins.h" -int loop_if_dollar_sign(t_tools *tools, char **str, int j, int i) +int loop_if_dollar_sign(t_tools *tools, char **str, char **tmp, int j, int i) { int k; int ret; - char *tmp; char *tmp2; k = 0; @@ -30,9 +29,8 @@ int loop_if_dollar_sign(t_tools *tools, char **str, int j, int i) == (int) equal_sign(tools->envp[k])) { tmp2 = ft_strdup(tools->envp[k] + equal_sign(tools->envp[k])); - tmp = ft_strjoin(str[i], tmp2); + *tmp = ft_strjoin(*tmp, tmp2); free(tmp2); - str[i] = tmp; ret = equal_sign(tools->envp[k]); } k++; @@ -49,11 +47,11 @@ char *detect_dollar_sign(t_tools *tools, char **str, int i) char *tmp2; j = 0; - tmp = NULL; + tmp = ft_strdup("\0"); while (str[i][j]) { if (str[i][j] == '$') - j += loop_if_dollar_sign(tools, str, j, i); + j += loop_if_dollar_sign(tools, str, &tmp, j, i); else { tmp2 = char_to_str(str[i][j]); @@ -62,7 +60,7 @@ char *detect_dollar_sign(t_tools *tools, char **str, int i) else tmp = ft_strjoin(tmp, tmp2); free(tmp2); - j++; + j++; } } return (tmp); @@ -73,7 +71,6 @@ char **expander(t_tools *tools, char **str) int i; i = 0; - (void) tools; while (str[i] != NULL) { if (str[i][ft_strlen(str[i]) - 1] != '\'' && dollar_sign(str[i]) != 0) diff --git a/src/expander/expander_str.c b/src/expander/expander_str.c new file mode 100644 index 0000000..f79ff71 --- /dev/null +++ b/src/expander/expander_str.c @@ -0,0 +1,74 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* expander_str.c :+: :+: */ +/* +:+ */ +/* By: fpolycar +#+ */ +/* +#+ */ +/* Created: 2022/04/07 15:49:23 by fpolycar #+# #+# */ +/* Updated: 2022/04/07 15:49:35 by fpolycar ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" +#include "builtins.h" + +int loop_if_dollar_sign_str(t_tools *tools, char *str, char **tmp, int j) +{ + int k; + int ret; + char *tmp2; + + k = 0; + ret = 0; + while (tools->envp[k]) + { + if (ft_strncmp(str + j + 1, + tools->envp[k], equal_sign(tools->envp[k]) - 1) == 0 + && after_dollar_lenght(str, j) - j + == (int) equal_sign(tools->envp[k])) + { + tmp2 = ft_strdup(tools->envp[k] + equal_sign(tools->envp[k])); + *tmp = ft_strjoin(*tmp, tmp2); + free(tmp2); + ret = equal_sign(tools->envp[k]); + } + k++; + } + if (ret == 0) + ret = after_dollar_lenght(str, j) - j; + return (ret); +} + +char *detect_dollar_sign_str(t_tools *tools, char *str) +{ + int j; + char *tmp; + char *tmp2; + + j = 0; + tmp = ft_strdup("\0"); + while (str[j]) + { + if (str[j] == '$') + j += loop_if_dollar_sign_str(tools, str, &tmp, j); + else + { + tmp2 = char_to_str(str[j]); + if (!tmp) + tmp = ft_strdup(tmp2); + else + tmp = ft_strjoin(tmp, tmp2); + free(tmp2); + j++; + } + } + return (tmp); +} + +char *expander_str(t_tools *tools, char *str) +{ + if (str[ft_strlen(str) - 1] != '\'' && dollar_sign(str) != 0) + str = detect_dollar_sign_str(tools, str); + return (str); +} diff --git a/test.c b/test.c deleted file mode 100644 index 03402b7..0000000 --- a/test.c +++ /dev/null @@ -1 +0,0 @@ -/Users/fpolycar/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Users/fpolycar/.brew/bin:/Applications/Visual Studio Code.app/Contents/Resources/app/bin:/Applications/Visual Studio Code.app/Contents/Resources/app/bin diff --git a/w b/w deleted file mode 100644 index 0d99181..0000000 --- a/w +++ /dev/null @@ -1 +0,0 @@ -minishell$ \ No newline at end of file From 681dec3263a1e6bddd3a1d6c6cf6990cad8652ec Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Thu, 7 Apr 2022 15:52:30 +0200 Subject: [PATCH 113/163] expander fully fonctional --- ahhhhhysssrerrer | 1 + src/executor/executor.c | 4 ++-- src/utils/minishell_loop.c | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) create mode 100644 ahhhhhysssrerrer diff --git a/ahhhhhysssrerrer b/ahhhhhysssrerrer new file mode 100644 index 0000000..14fcd0e --- /dev/null +++ b/ahhhhhysssrerrer @@ -0,0 +1 @@ +rer diff --git a/src/executor/executor.c b/src/executor/executor.c index 8b44fa9..59cb69d 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/04/07 15:46:28 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/07 15:51:50 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -62,7 +62,7 @@ int executor(t_tools *tools) while (tools->simple_cmds) { tools->simple_cmds->str = expander(tools, tools->simple_cmds->str); - if (tools->simple_cmds->redirections->str) + if (tools->simple_cmds->redirections) tools->simple_cmds->redirections->str = expander_str(tools, tools->simple_cmds->redirections->str); if (tools->simple_cmds->next) pipe(end); diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index ac8562d..5e8c441 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/07 13:26:18 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/07 15:51:10 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ From f77e3655d1675a8139a6b4eec011cc88ced99566 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Thu, 7 Apr 2022 17:04:11 +0200 Subject: [PATCH 114/163] expander fully done leaks + norminette --- includes/minishell.h | 4 +- rer | 1 + src/expander/expander.c | 61 +++++++++++++++++++--------- src/expander/expander_str.c | 74 ---------------------------------- src/expander/expanders_utils.c | 13 +----- src/utils/minishell_loop.c | 4 +- 6 files changed, 48 insertions(+), 109 deletions(-) create mode 100644 rer delete mode 100644 src/expander/expander_str.c diff --git a/includes/minishell.h b/includes/minishell.h index 0b651ae..4f101fb 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/04/07 14:19:34 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/07 17:02:43 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ char **expander(t_tools *tools, char **str); char *expander_str(t_tools *tools, char *str); size_t dollar_sign(char *str); char *char_to_str(char c); -int after_dollar_lenght(char *str, int j); +int after_dol_lenght(char *str, int j); void free_things(char *tmp2, t_tools *tools, int i); void print_parser(t_simple_cmds simple_cmds); char *delete_quotes_value(char *str); diff --git a/rer b/rer new file mode 100644 index 0000000..14fcd0e --- /dev/null +++ b/rer @@ -0,0 +1 @@ +rer diff --git a/src/expander/expander.c b/src/expander/expander.c index d4d035a..e54ab1b 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,61 +6,62 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/07 15:49:18 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/07 17:03:10 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" #include "builtins.h" -int loop_if_dollar_sign(t_tools *tools, char **str, char **tmp, int j, int i) +int loop_if_dollar_sign(t_tools *tools, char *str, char **tmp, int j) { int k; int ret; char *tmp2; + char *tmp3; k = 0; ret = 0; while (tools->envp[k]) { - if (ft_strncmp(str[i] + j + 1, - tools->envp[k], equal_sign(tools->envp[k]) - 1) == 0 - && after_dollar_lenght(str[i], j) - j - == (int) equal_sign(tools->envp[k])) + if (ft_strncmp(str + j + 1, tools->envp[k], + equal_sign(tools->envp[k]) - 1) == 0 + && after_dol_lenght(str, j) - j == (int)equal_sign(tools->envp[k])) { tmp2 = ft_strdup(tools->envp[k] + equal_sign(tools->envp[k])); - *tmp = ft_strjoin(*tmp, tmp2); + tmp3 = ft_strjoin(*tmp, tmp2); + free(*tmp); + *tmp = tmp3; free(tmp2); ret = equal_sign(tools->envp[k]); } k++; } if (ret == 0) - ret = after_dollar_lenght(str[i], j) - j; + ret = after_dol_lenght(str, j) - j; return (ret); } -char *detect_dollar_sign(t_tools *tools, char **str, int i) +char *detect_dollar_sign(t_tools *tools, char *str) { int j; char *tmp; char *tmp2; + char *tmp3; j = 0; tmp = ft_strdup("\0"); - while (str[i][j]) + while (str[j]) { - if (str[i][j] == '$') - j += loop_if_dollar_sign(tools, str, &tmp, j, i); + if (str[j] == '$') + j += loop_if_dollar_sign(tools, str, &tmp, j); else { - tmp2 = char_to_str(str[i][j]); - if (!tmp) - tmp = ft_strdup(tmp2); - else - tmp = ft_strjoin(tmp, tmp2); + tmp2 = char_to_str(str[j++]); + tmp3 = ft_strjoin(tmp, tmp2); + free(tmp); + tmp = tmp3; free(tmp2); - j++; } } return (tmp); @@ -68,14 +69,34 @@ char *detect_dollar_sign(t_tools *tools, char **str, int i) char **expander(t_tools *tools, char **str) { - int i; + int i; + char *tmp; i = 0; + tmp = NULL; while (str[i] != NULL) { if (str[i][ft_strlen(str[i]) - 1] != '\'' && dollar_sign(str[i]) != 0) - str[i] = detect_dollar_sign(tools, str, i); + { + tmp = detect_dollar_sign(tools, str[i]); + free(str[i]); + str[i] = tmp; + } i++; } return (str); } + +char *expander_str(t_tools *tools, char *str) +{ + char *tmp; + + tmp = NULL; + if (str[ft_strlen(str) - 1] != '\'' && dollar_sign(str) != 0) + { + tmp = detect_dollar_sign(tools, str); + free(str); + str = tmp; + } + return (str); +} diff --git a/src/expander/expander_str.c b/src/expander/expander_str.c deleted file mode 100644 index f79ff71..0000000 --- a/src/expander/expander_str.c +++ /dev/null @@ -1,74 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* expander_str.c :+: :+: */ -/* +:+ */ -/* By: fpolycar +#+ */ -/* +#+ */ -/* Created: 2022/04/07 15:49:23 by fpolycar #+# #+# */ -/* Updated: 2022/04/07 15:49:35 by fpolycar ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" -#include "builtins.h" - -int loop_if_dollar_sign_str(t_tools *tools, char *str, char **tmp, int j) -{ - int k; - int ret; - char *tmp2; - - k = 0; - ret = 0; - while (tools->envp[k]) - { - if (ft_strncmp(str + j + 1, - tools->envp[k], equal_sign(tools->envp[k]) - 1) == 0 - && after_dollar_lenght(str, j) - j - == (int) equal_sign(tools->envp[k])) - { - tmp2 = ft_strdup(tools->envp[k] + equal_sign(tools->envp[k])); - *tmp = ft_strjoin(*tmp, tmp2); - free(tmp2); - ret = equal_sign(tools->envp[k]); - } - k++; - } - if (ret == 0) - ret = after_dollar_lenght(str, j) - j; - return (ret); -} - -char *detect_dollar_sign_str(t_tools *tools, char *str) -{ - int j; - char *tmp; - char *tmp2; - - j = 0; - tmp = ft_strdup("\0"); - while (str[j]) - { - if (str[j] == '$') - j += loop_if_dollar_sign_str(tools, str, &tmp, j); - else - { - tmp2 = char_to_str(str[j]); - if (!tmp) - tmp = ft_strdup(tmp2); - else - tmp = ft_strjoin(tmp, tmp2); - free(tmp2); - j++; - } - } - return (tmp); -} - -char *expander_str(t_tools *tools, char *str) -{ - if (str[ft_strlen(str) - 1] != '\'' && dollar_sign(str) != 0) - str = detect_dollar_sign_str(tools, str); - return (str); -} diff --git a/src/expander/expanders_utils.c b/src/expander/expanders_utils.c index 9cd1ef9..e9b5068 100644 --- a/src/expander/expanders_utils.c +++ b/src/expander/expanders_utils.c @@ -6,22 +6,13 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/31 16:08:47 by fpolycar #+# #+# */ -/* Updated: 2022/04/07 11:40:57 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/07 17:03:21 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" #include "builtins.h" -void init_stri(int i, int j, t_tools *tools) -{ - if (j == 0) - { - free(tools->simple_cmds->str[i]); - tools->simple_cmds->str[i] = ft_strdup("\0"); - } -} - size_t dollar_sign(char *str) { size_t i; @@ -45,7 +36,7 @@ char *char_to_str(char c) return (str); } -int after_dollar_lenght(char *str, int j) +int after_dol_lenght(char *str, int j) { int i; diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 5e8c441..3aa4c19 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/07 15:51:10 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/07 16:30:30 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ int reset_tools(t_tools *tools) free(tools->args); implement_tools(tools); tools->pipes = 0; - // system("leaks minishell"); + system("leaks minishell"); if (tools->end == true) exit (EXIT_SUCCESS); minishell_loop(tools); From 1a9b253921dcc729b6ccb63a7f028fd92dc338fb Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Fri, 8 Apr 2022 17:48:58 +0200 Subject: [PATCH 115/163] begin signal and fix for lexer --- Makefile | 6 ++-- ahhhhhysssrer | 1 - ahhhhhysssrerrer | 1 - includes/lexor.h | 14 ++------ includes/minishell.h | 5 ++- rer | 1 - src/builtins/mini_exit.c | 18 +++++----- src/executor/executor.c | 3 +- src/lexor/handle_quotes.c | 20 ++--------- src/lexor/token_reader.c | 12 +++---- src/main.c | 5 ++- src/parser/parser.c | 3 +- src/signals.c | 35 ++++++++++++++++++++ src/utils/minishell_loop.c | 11 +++--- test/Makefile | 2 ++ test/{Testbuiltins.c => Testbuiltins.cttttt} | 0 test/{Testexpander.c => Testexpander.cttttt} | 0 test/Testtoken_reader.c | 11 +++++- 18 files changed, 85 insertions(+), 63 deletions(-) delete mode 100644 ahhhhhysssrer delete mode 100644 ahhhhhysssrerrer delete mode 100644 rer create mode 100644 src/signals.c rename test/{Testbuiltins.c => Testbuiltins.cttttt} (100%) rename test/{Testexpander.c => Testexpander.cttttt} (100%) diff --git a/Makefile b/Makefile index 423ca91..be706a1 100644 --- a/Makefile +++ b/Makefile @@ -35,8 +35,10 @@ FLAGS = -Wall -Werror -Wextra #-g3 -fsanitize=address LIBFT = ./libraries/libft/libft.a HEADER = $(wildcard ./includes/*.h) + +READLINE_DIR = /Users/fpolycar/.brew/opt/readline -INCLUDES =-Iincludes -I$(PATHP) -I$(LIBFTP) +INCLUDES =-Iincludes -I$(PATHP) -I$(LIBFTP) -I$(READLINE_DIR)/include all: $(BUILD_PATHS) $(NAME) @@ -73,7 +75,7 @@ $(PATHO)%.o:: $(PATHEX)%.c $(HEADERS) @$(CC) -c $(FLAGS) $(INCLUDES) $< -o $@ $(NAME): $(LIBFT) $(OBJS) $(HEADERS) - @$(CC) $(FLAGS) $(LIBFT) $(OBJS) -lreadline -o $(NAME) + @$(CC) $(FLAGS) $(LIBFT) $(OBJS) -lreadline -I$(READLINE_DIR)/include -L$(READLINE_DIR)/lib -o $(NAME) @echo "Success" $(LIBFT): diff --git a/ahhhhhysssrer b/ahhhhhysssrer deleted file mode 100644 index 14fcd0e..0000000 --- a/ahhhhhysssrer +++ /dev/null @@ -1 +0,0 @@ -rer diff --git a/ahhhhhysssrerrer b/ahhhhhysssrerrer deleted file mode 100644 index 14fcd0e..0000000 --- a/ahhhhhysssrerrer +++ /dev/null @@ -1 +0,0 @@ -rer diff --git a/includes/lexor.h b/includes/lexor.h index da50fd9..3676dbf 100644 --- a/includes/lexor.h +++ b/includes/lexor.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:55:06 by mgraaf #+# #+# */ -/* Updated: 2022/03/16 12:50:20 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/08 10:55:02 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,16 +14,6 @@ # define LEXOR_H # include "minishell.h" -// typedef struct s_lexor -// { -// char *str; -// t_tokens token; -// struct s_lexor *next; -// } t_lexor; - -//handle_tokens - -int handle_quotes_inside_word(int i, char *str, char del); -int handle_quotes(int i, char *str, char del, t_lexor **lexor_list); +int handle_quotes(int i, char *str, char del); #endif \ No newline at end of file diff --git a/includes/minishell.h b/includes/minishell.h index 4f101fb..3adb338 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/04/07 17:02:43 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/08 16:14:53 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -39,6 +39,9 @@ int after_dol_lenght(char *str, int j); void free_things(char *tmp2, t_tools *tools, int i); void print_parser(t_simple_cmds simple_cmds); char *delete_quotes_value(char *str); +void sigint_handler(int sig); +void sigquit_handler(int sig); +void init_signals(void); //builtins int (*builtin_arr(char *str))(t_tools *tools, t_simple_cmds *simple_cmd); diff --git a/rer b/rer deleted file mode 100644 index 14fcd0e..0000000 --- a/rer +++ /dev/null @@ -1 +0,0 @@ -rer diff --git a/src/builtins/mini_exit.c b/src/builtins/mini_exit.c index 83fd89d..312b776 100644 --- a/src/builtins/mini_exit.c +++ b/src/builtins/mini_exit.c @@ -6,7 +6,7 @@ /* By: maiadegraaf end_pid); - ft_putstr_fd("exit", STDERR_FILENO); - - tools->end = true; - kill(0, SIGINT); - // exit (EXIT_SUCCESS); + int status; + + status = tools->end_pid; + ft_putendl_fd("exit", STDERR_FILENO); + if (simple_cmd->str[1] != NULL) + status = ft_atoi(simple_cmd->str[1]); + free_arr(simple_cmd->str); + free_arr(tools->envp); + exit(status); return (EXIT_SUCCESS); } diff --git a/src/executor/executor.c b/src/executor/executor.c index 59cb69d..0725d13 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/04/07 15:51:50 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/08 17:10:46 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -80,6 +80,7 @@ int executor(t_tools *tools) else break ; } + tools->end_pid = ret; waitpid(ret, &status, 0); tools->simple_cmds = ft_simple_cmdsfirst(tools->simple_cmds); return (0); diff --git a/src/lexor/handle_quotes.c b/src/lexor/handle_quotes.c index d981e32..a333f38 100644 --- a/src/lexor/handle_quotes.c +++ b/src/lexor/handle_quotes.c @@ -6,13 +6,13 @@ /* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:11:20 by mgraaf #+# #+# */ -/* Updated: 2022/03/28 12:57:33 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/08 10:45:54 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -45,8 +45,8 @@ int read_words(int i, char *str, t_lexor **lexor_list) j = 0; while (str[i + j] && !(check_token(str[i + j]))) { - j += handle_quotes_inside_word(i + j, str, 34); - j += handle_quotes_inside_word(i + j, str, 39); + j += handle_quotes(i + j, str, 34); + j += handle_quotes(i + j, str, 39); if (is_whitespace(str[i + j])) break ; else @@ -67,11 +67,7 @@ int token_reader(t_tools *tools) { j = 0; i += skip_spaces(tools->args, i); - if (tools->args[i] == 34) - j = handle_quotes(i, tools->args, 34, &tools->lexor_list); - else if (tools->args[i] == 39) - j = handle_quotes(i, tools->args, 39, &tools->lexor_list); - else if (check_token(tools->args[i])) + if (check_token(tools->args[i])) j = handle_token(tools->args, i, &tools->lexor_list); else j = read_words(i, tools->args, &tools->lexor_list); diff --git a/src/main.c b/src/main.c index 91726e4..90ba01f 100644 --- a/src/main.c +++ b/src/main.c @@ -6,12 +6,14 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/29 15:08:50 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/08 17:32:46 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" +void INThandler(int); + int main(int argc, char **argv, char **envp) { t_tools tools; @@ -23,6 +25,7 @@ int main(int argc, char **argv, char **envp) printf("This program does not accept arguments\n"); exit(0); } + init_signals(); tools.envp = ft_arrdup(envp); parse_envp(&tools); implement_tools(&tools); diff --git a/src/parser/parser.c b/src/parser/parser.c index 5d29920..6d61e53 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/03/29 15:31:00 by alfred ######## odam.nl */ +/* Updated: 2022/04/08 10:57:24 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -63,7 +63,6 @@ int *parser(t_tools *tools) ft_simple_cmdsadd_back(&tools->simple_cmds, node); tools->lexor_list = parser_tools.lexor_list; } - // print_parser(*tools->simple_cmds); return (0); } diff --git a/src/signals.c b/src/signals.c new file mode 100644 index 0000000..fb0cf84 --- /dev/null +++ b/src/signals.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* signals.c :+: :+: */ +/* +:+ */ +/* By: fpolycar +#+ */ +/* +#+ */ +/* Created: 2022/04/08 14:35:54 by fpolycar #+# #+# */ +/* Updated: 2022/04/08 17:46:30 by fpolycar ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void sigint_handler(int sig) +{ + ft_putstr_fd("minishell$ \n", STDOUT_FILENO); + rl_on_new_line(); + rl_replace_line("", 0); + rl_redisplay(); + (void) sig; +} + +void sigquit_handler(int sig) +{ + ft_putstr_fd("Quit: ", 2); + ft_putnbr_fd(sig, 2); + ft_putchar_fd('\n', 2); +} + +void init_signals(void) +{ + signal(SIGINT, sigint_handler); + signal(SIGQUIT, SIG_IGN); +} diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 3aa4c19..fcebf82 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/07 16:30:30 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/08 17:32:43 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ int reset_tools(t_tools *tools) free(tools->args); implement_tools(tools); tools->pipes = 0; - system("leaks minishell"); + // system("leaks minishell"); if (tools->end == true) exit (EXIT_SUCCESS); minishell_loop(tools); @@ -37,9 +37,8 @@ int reset_tools(t_tools *tools) int minishell_loop(t_tools *tools) { - int i; - - i =0; + + // signal(SIGQUIT, sigint_handler); tools->args = readline("minishell$ "); add_history(tools->args); if (!count_quotes(tools->args)) @@ -50,4 +49,4 @@ int minishell_loop(t_tools *tools) executor(tools); reset_tools(tools); return (1); -} \ No newline at end of file +} diff --git a/test/Makefile b/test/Makefile index 80fa25d..66e8ccb 100644 --- a/test/Makefile +++ b/test/Makefile @@ -37,6 +37,8 @@ src = $(wildcard $(PATHSL)*.c) \ $(wildcard $(PATHSP)*.c) \ $(wildcard $(PATHSU)*.c) \ $(wildcard $(PATHSB)*.c) \ + $(wildcard $(PATHSEXC)*.c) \ + $(wildcard $(PATHSEXP)*.c) \ $(wildcard $(PATHSE)*.c) COMPILE=gcc -c diff --git a/test/Testbuiltins.c b/test/Testbuiltins.cttttt similarity index 100% rename from test/Testbuiltins.c rename to test/Testbuiltins.cttttt diff --git a/test/Testexpander.c b/test/Testexpander.cttttt similarity index 100% rename from test/Testexpander.c rename to test/Testexpander.cttttt diff --git a/test/Testtoken_reader.c b/test/Testtoken_reader.c index 5ecfe27..c59aab6 100644 --- a/test/Testtoken_reader.c +++ b/test/Testtoken_reader.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/28 11:12:08 by fpolycar #+# #+# */ -/* Updated: 2022/03/31 12:22:21 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/08 10:40:58 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -149,6 +149,13 @@ void test_lexer_12(void) assert_token(0, "test"); } +void test_lexer_13(void) +{ + init_test("test\"test\"test \"test\"test"); + assert_token(0, "test\"test\"test"); + assert_token(0, "\"test\"test"); +} + int main(void) { UNITY_BEGIN(); @@ -176,5 +183,7 @@ int main(void) ft_lexorclear(&test_tools.lexor_list); RUN_TEST(test_lexer_12); ft_lexorclear(&test_tools.lexor_list); + RUN_TEST(test_lexer_13); + ft_lexorclear(&test_tools.lexor_list); return UNITY_END(); } From 4d1249d9f9cb78196cadd249e5891f98cd43ba5c Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Tue, 12 Apr 2022 10:30:53 +0200 Subject: [PATCH 116/163] delete continue --- src/builtins/mini_continue.c | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 src/builtins/mini_continue.c diff --git a/src/builtins/mini_continue.c b/src/builtins/mini_continue.c deleted file mode 100644 index 26269ed..0000000 --- a/src/builtins/mini_continue.c +++ /dev/null @@ -1,20 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* :::::::: */ -/* mini_continue.c :+: :+: */ -/* +:+ */ -/* By: fpolycar +#+ */ -/* +#+ */ -/* Created: 2022/03/16 13:43:06 by fpolycar #+# #+# */ -/* Updated: 2022/03/16 13:56:08 by fpolycar ######## odam.nl */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -int mini_continue(t_tools *tools, t_simple_cmds *simple_cmd) -{ - (void) simple_cmd; - (void) tools; - return (EXIT_SUCCESS); -} \ No newline at end of file From 895cf47d056ba556bc7cf8d849c2847991ca67d5 Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Tue, 12 Apr 2022 10:45:32 +0200 Subject: [PATCH 117/163] fix cat | cat and single cmd, started heredoc --- "\020" | 1 - .vscode/settings.json | 3 +- Makefile | 8 ++- a.out | Bin 0 -> 37010 bytes a.out.dSYM/Contents/Info.plist | 20 ++++++ a.out.dSYM/Contents/Resources/DWARF/a.out | Bin 0 -> 22339 bytes includes/executor.h | 19 ++++-- includes/parser.h | 4 +- src/error/error_handeling.c | 5 +- src/executor/check_redirections.c | 25 +++++-- src/executor/executor.c | 79 +++++++++++----------- src/executor/handle_cmd.c | 77 +++++++++++++++++++++ src/executor/heredoc.c | 55 +++++++++++++++ src/main.c | 4 +- src/parser/parser_utils.c | 8 ++- src/signals.c | 40 +++++------ src/tmp.text | 0 src/utils/minishell_loop.c | 16 +++-- 18 files changed, 277 insertions(+), 87 deletions(-) delete mode 100644 "\020" create mode 100755 a.out create mode 100644 a.out.dSYM/Contents/Info.plist create mode 100644 a.out.dSYM/Contents/Resources/DWARF/a.out create mode 100644 src/executor/handle_cmd.c create mode 100644 src/executor/heredoc.c delete mode 100644 src/tmp.text diff --git "a/\020" "b/\020" deleted file mode 100644 index 06e7df9..0000000 --- "a/\020" +++ /dev/null @@ -1 +0,0 @@ -tets diff --git a/.vscode/settings.json b/.vscode/settings.json index f023251..988cd2a 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -3,6 +3,7 @@ "files.associations": { "stdio.h": "c", "readline.h": "c", - "libft.h": "c" + "libft.h": "c", + "executor.h": "c" } } \ No newline at end of file diff --git a/Makefile b/Makefile index be706a1..ade0cb2 100644 --- a/Makefile +++ b/Makefile @@ -30,13 +30,13 @@ src = $(wildcard $(PATHS)*.c) \ OBJS = $(addprefix $(PATHO), $(notdir $(patsubst %.c, %.o, $(src)))) -FLAGS = -Wall -Werror -Wextra #-g3 -fsanitize=address +FLAGS = -Wall -Werror -Wextra -g #-fsanitize=thread LIBFT = ./libraries/libft/libft.a HEADER = $(wildcard ./includes/*.h) -READLINE_DIR = /Users/fpolycar/.brew/opt/readline +READLINE_DIR = #/Users/fpolycar/.brew/opt/readline INCLUDES =-Iincludes -I$(PATHP) -I$(LIBFTP) -I$(READLINE_DIR)/include @@ -75,8 +75,10 @@ $(PATHO)%.o:: $(PATHEX)%.c $(HEADERS) @$(CC) -c $(FLAGS) $(INCLUDES) $< -o $@ $(NAME): $(LIBFT) $(OBJS) $(HEADERS) - @$(CC) $(FLAGS) $(LIBFT) $(OBJS) -lreadline -I$(READLINE_DIR)/include -L$(READLINE_DIR)/lib -o $(NAME) + @$(CC) $(FLAGS) $(LIBFT) $(OBJS) -lreadline -o $(NAME) @echo "Success" + +#-I$(READLINE_DIR)/include -L$(READLINE_DIR)/lib $(LIBFT): @$(MAKE) -C ./libraries/libft diff --git a/a.out b/a.out new file mode 100755 index 0000000000000000000000000000000000000000..a7ef01fdea0c10134c25d2bde6366727f6a9c269 GIT binary patch literal 37010 zcmeHQe{fXA9p86%m`g$>3JQvl$gd{C_ww55e||QUF^!?vP%lL7bTGDsm7vAgC8�b^WIK*DShi z(alS!H@k4=?5;B?lCu>Pb$!{Q<;${%Ilnu*j|qARx~ak>UUWTdtPcwUw_JHszhye} zDx9K1H2IY-4i-tb(a`l!xT!jn?#h+--+89IABq`7Tje>0JfRNqYC_?l*Iy^*=gPa| zJEpwpVw`AevC5`?U2pQQ^ZGry*IygpbT)oFg}g=~gy>uuVu;^p9a>Ozec}9N^YzLl zH!d|-!!3dUcV2Stc63suwJJi@^}2v%83vU{vBQho4-KazaL=~OG7!nJ%Q8ew zt6fCvdUYtXn$k}i5hY^Ev+5=pvgF2cCjPI#_QvZLExgu5i?Oa8yCSB9T?7sa$+619 zdPn$Bgt6B#Hcim4gIgB<2KSxY7^_F%7ovR=DwSJBr9E1YnvXhISq%OQ z1{gdaeI=+Audb$0(B<=1yF7RLJfNwJMkPBQxTW;f$4^iD{WFotcOPCp_awFv=QDkx zj4_n^XJYrV?Fx(w*U@6E6#kK%7C34xq_+Z<#>_Y^hL>8#7TB}&&QREBC|^(xyS&vV z_axj%{wUy6f_s^u2`?&RP`UmG8QWzV$@|qsA)pXY2q**;0tx|zfI>hapb$_9Cy)5Kssx1QY@a0fm4ha zpb$_9{J$cQI>e6lZ?JXrH?wGKb8)i0Sx+YNSYIpdI%B(!+u2j`Ja->{1$*b^jdPkEIT2kbNFxYg~4ly*T+;^Vno-^UCD$RO;dQ zXx5h)?d}8iJK2u8(H=XCmTIhTj2-gOk5?adC!jy!Vo-}Yd*(*xcSUZdd0VGP-k`a| zFt@dGY$tv_{pPoAXg7~ZwpXs~OiX66_U0AI#BlOuMRE#cbq{y^W;ycdRvcxbqA9H$pDxhOcBJa^lX^i9pgP957M!Zh1x1tAgd(kfQuEN~>)ou;^ zXBnrp!|(Qwu-g3&Y(2vkyRaoK%VG=0683Xr3*J*X!iOx$K;NBB6kuGw1| zn?b%NMzTJoA$D8^FoBRzz_o|_pMUZ(#6>%JdE99E+bMq2v7;4iFV)y=YfOAZwvsPA*7LU)(>UmM$fjJ> zzoD`tWfyx1IZMWNYB-Cfu^rAa*Ma5&Gj0>c%V-@C&u!*@ zF!vSk#mg)s5+mIv4SBT3xt%0aVvhBoIL|XfL`oK z&LZEqF0xf%n6_W&=k{a#to9EV_CsFUev&zm{T5qjpF;nc_E%DzI6C^V9uy~S$mz7! z3C8xdBe!E8>?0cY=ZTgvG6(q3T2V~3q5lEI73FTC4}X?=nCE)r*#z<_?H!)S5%+Tb zar8e29>fUWS7jP2q%}*72hVu;h}`AF`juionthQN#cQ*8K2iKiTksy(2=c9m^X0GZ z74h1E{N0Ow`xCNbw+l3veN)fj?`-^fj z`L+eV$L-jQi1$qbhqxViggLj9{YwY1|4M;nig1Dqsh{>)JDwA{eY7tJ^Wl_?=|aSW zh_eJeYiK`O`b-(_*h%EH?`nmGWe2Y?@c_;7fGr=S@o>lRtV!cY`6f?kgW_n!aHo_P4(=h_M9259(LSiEP;JIxu z43OL8d5m%{tuLLmX2MV8TvI>%q`p_sM_5dTA4GrLk3EDq&1cS&*aL5r!Eg4X{GT2o zUc^@dF(u=FEO^Yg&0Z&pAHMdGhuo62hCGKuMlWI_?Wbk!d2YeDj2ZHi>}rL6(w$jz z%Nem5xt#nY-!PZ#OBJ)&MQQsoYoD=8tVSGF-bDx$RDZZ%=yTQ zb2pu*pdT^xJU?sBnHQZy`%*mjsGpu!5VMiBekvCRw>~IE7uYGbB?e%ty(_R(NNuK*w0Si1c z`o1A-BN;D=XE)jUB-*fJ%?+4`v*(C+MO^Z;0iA0QBUU>*n)M`foTO(W<)i)$nOrsz zcHw!PIWJTecb39O2LHB057V$0D|OyS=M4@02hdL#V+?)+5A@XoSBhb>nfh=B!Z>u% z9tO5_&LH|K;ss5-l$SEGl*u1>UvzJg4RdV8$9TKI(mvnPzR=RX*wVh#(jIGRk2Blj zHjZQXKQUCms++t%k1O2JsMi}o!xN~{YrQ_BJd9V_#~C&C0Zd)Aha zpb$_9Cy)5Kssx1QY@a0fm4hapb$_9Cy)5Kssx1QY@a0fm4rrn* ztwmjnx(Rg`>LZx1>v||$RkK>Jsb8(vR(XBdJ#|f0K@Zbwe1VX`^xB|-YeSXK7pP%+ zpwaL%y)o$ZhijQ0G^#LhUC7R4y;WJv0rF)a4xti08RJVYS<22&}ay(HB2c_+3akGfiE?*)Zv>B;UIf~ ztLL-#*_guz?~ZAH_Lk;pYGfa4;h?vHZ35E~nqSk<$a*wLXCG*GwzVE|4zI%HfDf1V z8gO|cK&?hx_JqIycK`sl|4CcD0mS`2_AJC5Xu_1Pb-3)k6PNb8aM`f|m-jcCK<@S2 zvBNgieZZE~+O;nH>$J7nHrq~V>pgLbk2$!UPalJbU`=uR9xi|)U3TWsdqCG>W^&Q8 zYMMkm<+vQd--oC6hqz3z(9|~p)s0Gp=o@iy6M;gshc2Sz>OYM!t37#Gy@B*Q@gYAF zb34XKzoe;;^pl@d=tlaj^x5>+L7r8Aco6*yXPC0_-8`;e(o%ntYtQJn(r45E#~l6f zLG;hThk{7gG<@)b{Fk)U-=3r2N}o;tJjk>9e;w$7@PAL}e^ls~wA9~~qu)wX{5&zs zwEt5R9>)Ym7o8Xr;%_sg-s8l4s zl|GyQ&q1EYWaZa`pa=4QFFssDvPa=TL;g!z>R&HpNc~ot>@S^b@=p}}l9v3^2FY)w ziGPRSpCZ9&WRIRP5(k*mDWM`5k>P?JcH56_`5 z$)PXLp}(0!yK?9mIdnbr<9k|cH7dR-$Qn@nsDxu9Dvf(l&o&3ctOQ4)GtVx%^~9oQ z4lMYJlsJy)wP74$#1ozqmS;cFnNtZ-^29=I>-mJ}5Xt8!5>ADKbQBRyI-iK9pH3sP zO=l9(=0_Gi`|xBQy!dg=JY9V)C%|l_<)NyzhKIRs2^qnV3nxFE0_%cRRkf~#ftscU z!ygX0uJMK!H&weDynb(}-thTcK5uofD(E#r)T#};7~v5P!tL^!uPBNOEUzlfNDP!o z%m`MqpHMx=s<_CEF1~I;8C*Np%1ER|83l%^3{iBBb&ehkFMT*9MX;@XXE@H95J&nlZA3d_AVgMb64^mMoR|p)ETD zuxwH!1j@3Ra{8U2U3hZfC)4z^WCZ%~_|@)aHXK@P_{Z2v)a9sD@FVhrti*;-?R}x* zmb0{~@<3CVP5H^D%~RXg72N*g%Pw60*tW4RUi9SX%FjP<{9n6TJ{^Bz?%@;LMs1n2+5hXU zO*gzV>)HQ&Htmx?-@D~le`)zI?ScMVXN{^j{C)RQ$F2WpI + + + + CFBundleDevelopmentRegion + English + CFBundleIdentifier + com.apple.xcode.dsym.a.out + CFBundleInfoDictionaryVersion + 6.0 + CFBundlePackageType + dSYM + CFBundleSignature + ???? + CFBundleShortVersionString + 1.0 + CFBundleVersion + 1 + + diff --git a/a.out.dSYM/Contents/Resources/DWARF/a.out b/a.out.dSYM/Contents/Resources/DWARF/a.out new file mode 100644 index 0000000000000000000000000000000000000000..7d3a242d8f977046a19411d18c43444a27645b57 GIT binary patch literal 22339 zcmeHP32>anmF|E3>Cs3dk46W!j1LJcV;kEV$uh<^wvn)8%Oe{rJ_?L&eny&+G@tbl2euZeu8j4>QiHD%ZzJ%bP?=LmyO8NHL3Kq&_%YuC20jn<=jD;n( zP`-nTpY}Ia&zNavP>?1+UJ6}XR(so)Hf#09^Eb<)tx_(Ac%p}R>Oih(S^bHe)=tcC zzm1t)E*J0>wmi#;Nk}<1qDBe3xzWuFIKK&k1Q9D0d*CDu$w`+cT4=8@!f?EFA z`PtASNqXCt)tfDMx zY}R^>l<#EQNqURL5^>Ez`iX`Z+pqYY$PQ3Z-eXxkkSg~S@;mSu`Q`fa_3O^>>S$l5 z9sen12Xan&RD6R{9?4~wjrcqYgF~JR+BU7B67qRb(KRVp#jEE?xC9aTiAQByRw&#v z)Ne&2aq)(ph~K@sgVlUh`NBx^8yxD1|D?ypFTH1QJioQC zNq%2bozPnJcc1iVe!*lg-X9kJmZ!hhj!1qZO1{@xKZzUZU+PbFm*ebl@vkJmY3c>P z_`H+j7wqXthKI-7ch6gr-$#ngp7kfk59600EVRe)uO+{Bwd-jVzo6q`evPmZ5(mI>s0N%7mDI?I<%l3$|d(q5{$ zP`$P@WUcofXd&}`NBI~loo7qk@DCLnT_)j|6#TN{ zcSON&w8;E76`WS~d|$y2D%b%Br~vL5Hgpg%uY!F`Bz&rZYhhT*pQqqg8zt;l@X~W6 zjF|;(G&EurIfUs|Z#t38$Ako?k>+t0{WdnMc#(=*RJ<5-6uD_37iofeA{=oTPuU25 z6JWu80&K3X9_$%}llgVoaJIVa*j!z9Y_2XlR@7y0SjtL<`y(l=xx*o5^@r0|99vUj zCDWJ+4inOf4Orpb;ohNi*cu22AtAGRPzW131HmL_|6%NOsAM1QdW6DJX7#0k3-|7_ z;tEKHgJ2+*NR-)+J*(uUiAY?K4p99GtoV_W4960~Hp&$7&r+gPXlPInA{hbkqmF)G)&yV<;%LehqF`R51AR6`2u(4MjJ}-6X@7zv@Sz++% zviEq4E5qjnI5T5o+-+88xGTV`U0s~H%xD0Z8i6ST#(^B>I?Nqqj(v_AUb&CEfx%y; z&NMF$aEEz%hEI0A!^@FVmfcdsy!u5p>OGkmHmV{RWaX9vvf0fOrS zyc8S=xIACsY zA2nM<$qmOHPH5c}hIn>DL-^N6dXmB99)EjyI2=t3hLir)I2{~}ht}h`>y|_!n(}XM z?@IYM1ba7bzOXSB+T|ZgCH=~P{$Mi3J}-~do|NBGrxE2>U3~b3-SJ4YbO>1AIwNf<_- zd?CZQ%HG4fPrZ{#oeQ)|zFIxTCqsP!a~Ek6w&Ji3{J5HE0(@$K*Nfj-89rx>FAVVc z0p1ia2gbN*)=Fykh2_qt$Ohm7oI@Ywl5+ll`yfV_7;R|m$Y|@6Ft9#^ zal;)vfKYiQ>??keu7bq;@%3Ws)JI|OH5RuyUe{>2nPe8^MON87e0;JO%ipK&+HQY z4mv4_N*+v2Db0wMOvWqIl;Mj-pL7PgsxguXs&ToxTmkdY)frHmKVjf4%?wvZ?!;!M zR?vGjE5J_^l|jE`@&NaupF_oD>@pC**ptQ^dkIDzC&O8^35JinlU0o6f!0tu5>3Ge z;4I_=`uH9Bz^pQN8)gy#-XbP4c*WNW6PE-aj&@Y%Xn~hZf>ZcV2ttyJh$f&RvI)F& zqs^fzL0SV+IFBTi3PPJ<58dVx5TLx6zK||RedHcCcjxRq46z^Z5;?Ts#e!wXfK%mL zgyO;^ycRg}r}B)LTFpZeeDm6)UcAYeLygh22|h77V0KamxD<_o#AKxj8@Ca%3Hy|h zg0g-sWt%ExgDBem^!A{)1?ic4c|A(D1bB0RugLHj6N*LsC`L1Mtnzi#vdk1-`)LwQ zq&Lw`+#$LNoZ9@1o|E$#eWX+N8`qK!>E;o9NEi z{aV-u=ULSS*GahY5(1Px==gO4(l{6?4=~ollHzb7(BfOBgHe$?0pXCV>hkH-B0Vz@ z9PGQue;O$qs#W>oC?g<65Op&413+{uOO6@hd?OLi*H8zWkIS-;B9$YcBa-w`XXIf2 ziTs-&7)fA1xp@oV+~LjRfO10;0LD5H94vwRZUGcm>~kkKsZ}V^YdhL`Orq>(IDui? z5FG3=To~+8a^&@ZtPA<_xasvs5l?+~lp0P_WeTPV5TD zQ*0-Ip?E6N9}kD>Bk?o~p?W&2%61_^h|3VCL5^h%%OFifDLoq;Q&>>M=%Q9{ER&I1Q#Ko-btV*ptg-BirE!~h_+_m$Gq9owUqynNKo1>Z&AAfkAX`Sz-UtlQ-ho&k?m|LUeF(|IsviSx zeAPgz7HFTuNpZ=K-UG$&TSyl2`&;0~^LqqX-GcQ<=Un;o@dYbZC-`aCk1KOuj##um zN21+3TjhC*mPiPhbKDFdQF^T+xd^dkwQbtew$0kS?xGHB%eL+g%c7DDhvrd~9(hkz z?@qMhZJg+l^yI>lUN2r-Cn5$;(o^pt>GdLS1!S7&k@RFZNw3!)A5KIJoTR6|Mbhgj z^gW+QPYRdxdMf*zrE-RA z2jc%gTq&@OU5D6i6$f<}4O7k#c1EU6vX#O*PP4lOeW4?l4&YVXPl{mRBz5}2U zdmF!w3JV5sIli*{cUh>!akI|_tfP|h81F<|C~+G^H)3U!Q%fC^%N#vmoFc%a1os=$ z1o#I~B6pu5nAQ@T5_5E~QAfF_QrQyM=o`#6tNwJtOj86OGEwbx1-{GXQ0@$(XIx*S z=oV7=DW_3!`ZwYv1ZkoUvZQ2`FbAbD&D6CqH+7}X#rSa<&Si+W(@nIc)Qhe}FqV~d zAiB_)@7aKuL@*XI&nCpvsK!RO=R%$K`#d|5t|xp`t%okQ+(_x>dQTc@QFhT>&u*P= zY4VI9P0|{RmwNsZG4;I0St~p@X#A2^&n-xQK=`veJzv!{OS_qy7`my8FQchflW~sc zKEy(-mCSPp>0v6i%I%>$Fru1seV%_pT8PzJ>vI`jN5a7RF5%y4_&MOxH4vs~2lCgJORq9&29 zuVtmw%hQz_qt;EVoqt5yVYsQBn@m(n4dhgEHKXo1|7EB~MklcJGF*=$b%5<%j@J$t ziJio!VgST&Z8vGlsJIqTiQ_9ylGdoC;fR&eIMxqZv#ykeuQ-y9HO{LLmAKtE08>HZ zSH9n{Ckcf>f`b<;7lW;qj+95z**iEVo%C`WUOUc6d>2Tgg2p-sS^uzrZjn&!V|K{* zgn;fs9va}54IqeVSNHQ+ER&<&V}ENXzg$92=l zqIi4l8Ajq+AsbnjTJbXh?UzvPf7l`4>jEO%q7~(`6^{U7?%VNGK~Bd;GmO~#LMSEvpfm!Bk4{h;p_1~bOQe6IU{p!* zZ;P>qqOA_*#Ja*%Tvx-|!=cBaT92+FOI z$|XubzSD)|3My|PsG1<=)pzeC|3#E@W*+}D^Z0A1Mwn7i5`R(K0?mH~aakz%bA30C z{(p-R>!h6aA*3O()OEY!KOp265IBZ}jB6yvo9j1IxwQ(KzMY^>1^IdiT1Px#Bk5ED zQMZ0IXbz=3*G)Zd1tHiP!XnPS0+)i3w#5r>X$$)xpXWn5qaJLG*k6$h75P3SA>~6N zk9_DGM5#SWN?Sz}IOMs7m8uD1T;FkY3jPPF)I81qdszsYiatcpe8r z$}z5QK^_BO#C}9MZAwO;;3y!{q2Rb$OC>nAD~Jh>9fG5N8!v{4`I>wQ%(W&wITwI5S~bBT_*Gx*W{lCEPn zaY*1(5sX4*z`;isZH)>l1Es0Hhw>QL*Fi&6Dpo^ubwuZaxkPuMA-Cw01;n_%#Cb%N zI*m%rRi&m2Y3ItCZHW>sUf+~y-b$ki-G#$xtgAM?9?x>mInc8?mzmz_xPj|zKw_200~~Ke=_*%i zt?4DAIaAkFpF0(eFT}(pRS;mYAD6p1V$nyu z^j3l1CI}|`Q}`&I9!Oy)Wq1%v;M|6R*dD-*SNvzO!OKD{5e-=s4@U4Wcxc~6?7a*| z2ou7V2L9Jt!HxsnV(l4kU{gpsoQYmxZikThF%hb_0eChkU0 zw7=20xvi7>disxzRS4w>^bpQq=t%Kk*2w5TWf2Yi<0k5Rhz|eofO!!{=1r;i#r3Zb zKeNv`zPXAYU+CNP(eSz{70dTub^h{SCYIJ7Ts3mm-uAzrb<61KQ=V_wx^&3}zDuwE z`jz+XOS*rOSh?b@+p2mFKW7|&8N7}nyo*4idksPZ!eWGWge?dmgfzllgxe7AMtBh6 z9}r$fIEo6(M!SRSW0^&)Sy3ivQ7l@Byu%t)Ky%7IYHkXMx3*ZN+(Rjp)3Gum# zUd%NCpQ7k#vjEkK?PNrZwgCthGhoDGxJ7&(DG-ks5)mI#uozeY?^dvwa{zWKm=^TJ zubzJJh*<&RH&uJYj0LczU@=z(Jf!3k%Mif#Va%jQ+g!05XLGu)qiKj`B?5*b@em90 z3lmbjzkBG&8NYe*%Cml7vi+52@{NTMxE*0T z0xijDv3n5V5rpRvTv)HnK)43s0K)eW{t@A0ge9;Y?QM`rX +#+ */ /* +#+ */ /* Created: 2022/02/24 15:17:39 by mgraaf #+# #+# */ -/* Updated: 2022/03/31 17:45:11 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/11 17:27:42 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,12 +14,19 @@ # define EXECUTOR_H # include "minishell.h" -// check_redirectoins -int check_outfile(t_lexor *redirections); -int check_infile(t_lexor *redirections); -int handle_redirections(t_simple_cmds *cmd, t_tools *tools); +// check_redirections +int check_outfile(t_lexor *redirections); +int check_infile(t_lexor *redirections); +int handle_redirections(t_simple_cmds *cmd, t_tools *tools); // executor -int executor(t_tools *tools); +int executor(t_tools *tools); +t_simple_cmds *call_expander(t_tools *tools, t_simple_cmds *cmd); + +// handle_cmd +int find_cmd(t_simple_cmds *cmd, t_tools *tools); +void handle_cmd(t_simple_cmds *cmd, t_tools *tools); +void fork_cmd(t_simple_cmds *cmd, t_tools *tools, int end[2], int fd_in); +void single_cmd(t_simple_cmds *cmd, t_tools *tools); #endif \ No newline at end of file diff --git a/includes/parser.h b/includes/parser.h index 29c305c..709598e 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/04/04 16:10:41 by alfred ######## odam.nl */ +/* Updated: 2022/04/11 16:27:29 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -52,6 +52,8 @@ typedef struct s_tools int pipes; bool end; int end_pid; + int *pid; + bool reset; } t_tools; typedef struct s_simple_cmds diff --git a/src/error/error_handeling.c b/src/error/error_handeling.c index 35a77ac..2cfd687 100755 --- a/src/error/error_handeling.c +++ b/src/error/error_handeling.c @@ -6,7 +6,7 @@ /* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/02/25 11:39:57 by mgraaf #+# #+# */ -/* Updated: 2022/03/31 17:46:29 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/11 18:03:04 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -41,8 +41,7 @@ int check_outfile(t_lexor *redirections) if (fd < 0) return (-1); } - redirections - = redirections->next; + redirections = redirections->next; } redirections = start; return (fd); @@ -63,13 +62,29 @@ int check_infile(t_lexor *redirections) if (fd < 0) return (-1); } - redirections - = redirections->next; + redirections = redirections->next; } redirections = start; return (fd); } +// int check_heredoc(t_lexor *redirections) +// { +// t_lexor *start; + +// start = redirections; +// while (redirections) +// { +// if (redirections->token == LESS_LESS) +// { + +// } +// redirections = redirections->next; +// } +// redirections = start; +// return (fd); +// } + int handle_redirections(t_simple_cmds *cmd, t_tools *tools) { int fd_in; diff --git a/src/executor/executor.c b/src/executor/executor.c index 0725d13..5b656b6 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,71 +6,73 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/04/08 17:10:46 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/11 17:26:32 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "executor.h" -int find_cmd(t_simple_cmds *cmd, t_tools *tools) +t_simple_cmds *call_expander(t_tools *tools, t_simple_cmds *cmd) { - int i; - char *mycmd; + t_lexor *start; + + cmd->str = expander(tools, cmd->str); + start = cmd->redirections; + while (cmd->redirections) + { + cmd->redirections->str + = expander_str(tools, cmd->redirections->str); + cmd->redirections = cmd->redirections->next; + } + cmd->redirections = start; + return (cmd); +} + +int pipe_wait(t_tools *tools) +{ + int i; + int status; i = 0; - while (tools->paths[i]) + while (i < tools->pipes) { - mycmd = ft_strjoin(tools->paths[i], cmd->str[0]); - if (!access(mycmd, F_OK)) - execve(mycmd, cmd->str, tools->envp); - free(mycmd); + waitpid(tools->pid[i], &status, 0); i++; } - exit(EXIT_FAILURE); - return (1); + waitpid(tools->pid[i], &status, 0); + return (EXIT_SUCCESS); } -void fork_cmd(t_simple_cmds *cmd, t_tools *tools, int end[2], int fd_in) +int ft_fork(t_tools *tools, int end[2], int fd_in, t_simple_cmds *cmd) { - if (cmd->prev && dup2(fd_in, STDIN_FILENO) < 0) - ft_error(4, tools); - if (cmd->next && dup2(end[1], STDOUT_FILENO) < 0) - ft_error(4, tools); - close(end[0]); - close(end[1]); - if (cmd->prev) - close(fd_in); - if (cmd->redirections) - handle_redirections(cmd, tools); - if (cmd->builtin) + static int i = 0; + + if (tools->reset == true) { - cmd->builtin(tools, cmd); - exit(EXIT_SUCCESS); + i = 0; + tools->reset = false; } - else - find_cmd(cmd, tools); + tools->pid[i] = fork(); + if (tools->pid[i] < 0) + ft_error(5, tools); + if (tools->pid[i] == 0) + fork_cmd(cmd, tools, end, fd_in); + i++; + return (EXIT_SUCCESS); } int executor(t_tools *tools) { int end[2]; - pid_t ret; - int status; int fd_in; fd_in = STDIN_FILENO; while (tools->simple_cmds) { - tools->simple_cmds->str = expander(tools, tools->simple_cmds->str); - if (tools->simple_cmds->redirections) - tools->simple_cmds->redirections->str = expander_str(tools, tools->simple_cmds->redirections->str); + tools->simple_cmds = call_expander(tools, tools->simple_cmds); if (tools->simple_cmds->next) pipe(end); - ret = fork(); - if (ret < 0) - ft_error(5, tools); - if (ret == 0) - fork_cmd(tools->simple_cmds, tools, end, fd_in); + ft_fork(tools, end, fd_in, tools->simple_cmds); close(end[1]); if (tools->simple_cmds->prev) close(fd_in); @@ -80,8 +82,7 @@ int executor(t_tools *tools) else break ; } - tools->end_pid = ret; - waitpid(ret, &status, 0); + pipe_wait(tools); tools->simple_cmds = ft_simple_cmdsfirst(tools->simple_cmds); return (0); } diff --git a/src/executor/handle_cmd.c b/src/executor/handle_cmd.c new file mode 100644 index 0000000..c835e0f --- /dev/null +++ b/src/executor/handle_cmd.c @@ -0,0 +1,77 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* handle_cmd.c :+: :+: */ +/* +:+ */ +/* By: maiadegraaf paths[i]) + { + mycmd = ft_strjoin(tools->paths[i], cmd->str[0]); + if (!access(mycmd, F_OK)) + execve(mycmd, cmd->str, tools->envp); + free(mycmd); + i++; + } + exit(EXIT_FAILURE); + return (1); +} + +void handle_cmd(t_simple_cmds *cmd, t_tools *tools) +{ + if (cmd->redirections) + handle_redirections(cmd, tools); + + if (cmd->builtin) + { + cmd->builtin(tools, cmd); + exit(EXIT_SUCCESS); + } + else + find_cmd(cmd, tools); +} + +void fork_cmd(t_simple_cmds *cmd, t_tools *tools, int end[2], int fd_in) +{ + if (cmd->prev && dup2(fd_in, STDIN_FILENO) < 0) + ft_error(4, tools); + close(end[0]); + if (cmd->next && dup2(end[1], STDOUT_FILENO) < 0) + ft_error(4, tools); + close(end[1]); + if (cmd->prev) + close(fd_in); + handle_cmd(cmd, tools); +} + +void single_cmd(t_simple_cmds *cmd, t_tools *tools) +{ + int pid; + int status; + + tools->simple_cmds = call_expander(tools, tools->simple_cmds); + if (cmd->builtin) + { + cmd->builtin(tools, cmd); + return ; + } + pid = fork(); + if (pid < 0) + ft_error(5, tools); + if (pid == 0) + handle_cmd(cmd, tools); + waitpid(pid, &status, 0); +} diff --git a/src/executor/heredoc.c b/src/executor/heredoc.c new file mode 100644 index 0000000..6439c8d --- /dev/null +++ b/src/executor/heredoc.c @@ -0,0 +1,55 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* heredoc.c :+: :+: */ +/* +:+ */ +/* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/04/08 17:32:46 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/11 13:53:52 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -25,7 +25,7 @@ int main(int argc, char **argv, char **envp) printf("This program does not accept arguments\n"); exit(0); } - init_signals(); + // init_signals(); tools.envp = ft_arrdup(envp); parse_envp(&tools); implement_tools(&tools); diff --git a/src/parser/parser_utils.c b/src/parser/parser_utils.c index e71bd5c..97b803b 100644 --- a/src/parser/parser_utils.c +++ b/src/parser/parser_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/03/04 11:52:02 by mgraaf #+# #+# */ -/* Updated: 2022/03/22 17:06:00 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/11 16:42:32 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -28,9 +28,11 @@ void count_pipes(t_lexor *lexor_list, t_tools *tools) t_lexor *tmp; tmp = lexor_list; - while (tmp && tmp->token != PIPE) + tools->pipes = 0; + while (tmp) { - tools->pipes++; + if (tmp->token == PIPE) + tools->pipes++; tmp = tmp->next; } } diff --git a/src/signals.c b/src/signals.c index fb0cf84..5f53656 100644 --- a/src/signals.c +++ b/src/signals.c @@ -6,30 +6,30 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/04/08 14:35:54 by fpolycar #+# #+# */ -/* Updated: 2022/04/08 17:46:30 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/11 13:53:32 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -void sigint_handler(int sig) -{ - ft_putstr_fd("minishell$ \n", STDOUT_FILENO); - rl_on_new_line(); - rl_replace_line("", 0); - rl_redisplay(); - (void) sig; -} +// void sigint_handler(int sig) +// { +// ft_putstr_fd("minishell$ \n", STDOUT_FILENO); +// rl_on_new_line(); +// rl_replace_line("", 0); +// rl_redisplay(); +// (void) sig; +// } -void sigquit_handler(int sig) -{ - ft_putstr_fd("Quit: ", 2); - ft_putnbr_fd(sig, 2); - ft_putchar_fd('\n', 2); -} +// void sigquit_handler(int sig) +// { +// ft_putstr_fd("Quit: ", 2); +// ft_putnbr_fd(sig, 2); +// ft_putchar_fd('\n', 2); +// } -void init_signals(void) -{ - signal(SIGINT, sigint_handler); - signal(SIGQUIT, SIG_IGN); -} +// void init_signals(void) +// { +// signal(SIGINT, sigint_handler); +// signal(SIGQUIT, SIG_IGN); +// } diff --git a/src/tmp.text b/src/tmp.text deleted file mode 100644 index e69de29..0000000 diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index fcebf82..fbdedf6 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/08 17:32:43 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/11 17:35:27 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ int implement_tools(t_tools *tools) tools->simple_cmds = NULL; tools->lexor_list = NULL; tools->end = false; + tools->reset = false; return (1); } @@ -27,7 +28,7 @@ int reset_tools(t_tools *tools) ft_simple_cmdsclear(&tools->simple_cmds); free(tools->args); implement_tools(tools); - tools->pipes = 0; + tools->reset = true; // system("leaks minishell"); if (tools->end == true) exit (EXIT_SUCCESS); @@ -37,7 +38,6 @@ int reset_tools(t_tools *tools) int minishell_loop(t_tools *tools) { - // signal(SIGQUIT, sigint_handler); tools->args = readline("minishell$ "); add_history(tools->args); @@ -46,7 +46,15 @@ int minishell_loop(t_tools *tools) if (!token_reader(tools)) ft_error(1, tools); parser(tools); - executor(tools); + if (tools->pipes == 0) + single_cmd(tools->simple_cmds, tools); + else + { + tools->pid = ft_calloc(sizeof(int), tools->pipes + 2); + if (!tools->pid) + ft_error(1, tools); + executor(tools); + } reset_tools(tools); return (1); } From 8310effe355fee5dc936d78d8cef6110d46eaace Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Tue, 12 Apr 2022 11:29:06 +0200 Subject: [PATCH 118/163] some more stuff for the heredoc --- a.out | Bin 37010 -> 37570 bytes includes/parser.h | 12 ++++++++++-- src/executor/executor.c | 7 ++++--- src/executor/handle_cmd.c | 3 +-- src/executor/heredoc.c | 10 ++++++---- src/utils/minishell_loop.c | 3 ++- 6 files changed, 23 insertions(+), 12 deletions(-) diff --git a/a.out b/a.out index a7ef01fdea0c10134c25d2bde6366727f6a9c269..575821d02af4522c87ba1ba6bff998ecbfc5bcb5 100755 GIT binary patch delta 3192 zcmZ`*4Nz3q6+ZXvA}Am~Vn~K0{OA}>96%*2M1d8eg~Y~4Fi9JAL3WWhNVdd+RT(0ZNt(c{qF94k*wtI zp8MW+?z!JN=l;G^XO-*kDBT|=dR7rJA}WbRh2mMIjus?RVMtpZQW6#N)vO?)T&mYe zbvZxGK4V5+sid$-{;KkO<$5A7(`A(UV6cYB8IPyWNmK(D3f>=tlpG<#pDuP1Rm>xb z#{2USQL3dR;n@U<;(mQfL=7zC)UAv7gl>6!(@UTHd|sen$A$fOK7Z&>dk^sC`dmi5 zQom){a?qxs*9wU6EIf(w2t?p`mFN<4`}LzMdG_juTVEnNJ*uPLkw{|z0d}W9pRe{q zHxf;Ok!Y!_$X6Zgo^HCisAuK|sXm?Qk27s-Xu3I~XS$fFuC~<4v%1vDb8U}Ky4rS5 zj?T=yIkE^mi;RJMaE`&xSSooDMmN>ws~ddI*0OOOsmF4hA1`MV$kEYhSMj5spuupy znoiELemKS`mQ5=NAUQY3#%TTt(3bKvrnbs-l2u8q<;1y(3 zq%TmfBHLh~%_TiE#qR5C&ME9gx5e0{%!OiJ2-Px{TROn$d;uxX$T~V)K6(#f*6+jBozleZh^X(e>cC4ecwHl-^!lKXvrY_cQsmT%~_6pc2kX|-eRt+GF4k^Et!sk z4w8z3v^2`FRMpg>$Hpz2GKhB-E-Ml{Zaro}2*ZXFMZD`EF%(5BgFA)jAS0cJ5&IGi zr}e~=uvh-KfD}hG|4(6wdz0~Mp;{3Yn}%*;``95-Ji)nL*G0tlcNRt0TPn?DI#6$Q zSojCdWvzW|mu0urW~!>GtZ$&Z%wjXk_cle?nszl-SJN$Jx5Z(ySq?f(wN{&j{-$7( zv3sqsHd|`R<}gu%jyzRF^JJcMfi+M+vo|{EgtFVz;HckIXSGqEQbqk>-dDHZ0(TBJm8$_p$l4)7Lu9{I zYAjWIO}1M41a`hgnA|*oYTyv6z9v-N&8Q|?koop6S+?JtknOz+64D z_8E;%Sc(#dRza_ohk@r}vr9I+WV5f~iEW1a=vu<)I=+RjFDDe-r;@HOho72*Zaj?c z-`_%a{>czs2^PD^_571OvE5J_p6m-~o}Fw3@Lb7mHiYN~B;97Nwr3^8tPN?eNo@=t zYR^g>l-fb59bC&3UpH98T@RyqwriXubGp{&g`5=XLz!q;%;ZuC#w}O`ehyU*?>~}! zpSS1Gd$zY7z;h*9sKSg%RKMwiH-+3xusmm-KcR?ZTUj ztFLDz_?`-7d{SzCdO$acNd+WFR_k15a2<4s2mt=f870>Stnf*!?e;;5we31Av9{KPRL=1XlsVd+0HvUnjXX*rQHms zYKI9hbUpC99lvz@I=K=X(n=%Fj&ELZuTN&(3X2`k}8WXBh{5@XtDpZuNwf zt)IFT<3q-m5UG}S5_C8k*;}OOaWMry0d}JOWr!#Xh}8Hrg<|kQye!)o)S+UPnRU2z zr#JoOzZcFKt8Yvk>N;clNSD53)BLFyO8if(8{XDa+O?jaI%zcSDJr+~qU*k{_Z{Dv zyjoW>)fIRAv}ekEWxV9^xXy}KbX&6RAN+JrTk@TEAM;FZpZUiRPDP$@kL6u5*dxtn z47YeWF}qSs=7b>xYY3*UeC)A-*lPX6NY7bDicoBs6M(T4a# g9seAzx-#&i-aoXwRbP7OH>v{T~|t4-Qt|YybcN delta 2657 zcmZ`*3s6*582-=QMFA-RD{1fnYZ5-jMR}U+y7?Nd9Mj6I&=uJWtimoWi&j2%V^Psa zS&oe~!x-zMu$MGjArls*O-^Hzy|5-z)-*AlV$w0zTVxu$(Z6kqffl953$RG9MN?>pThJB1=B-xmO>Zs7ujhx zmlsOOESj&EzLOf_`688O-f=i&5;^1XJnkf#ix3jtlVEw(M9<<`zlEqYif9PlABBms zSg}MrOJOnGr%sNV3(o}i!eP8ul^(zCiS%Q8u4R7pj&s6>BTIA_dAd4}HS$9Bg0v(k zr=Zt3n20PQi82xNH@gQL{1{|?YK6|-4z7C$ z`MZZGoKxps#{-j-Vh*!!0&55D54iZKDZ?GfI6f3Ec!V_5Dyy9Xr1j?k{u}$WeMji5E6n%2MUf>1Tin+gO%9f z!~BARqYdp>;d`mSe|R&3`;`8HwR86OA7<`Om9eYeC1L6@`vWs2clWR4_Kl(8ZY*j2 zjA*_4!~XuAB4A+cy#4(y@A%B!64QFW%03`57hj)O%lBmu<*ier@7Q>we`mJ}nH_5I zS9?F7I+L-okP_v6Zp> z>FFclG;&d?72NCvb2NP0^t2gbC+8#2!UEX8qKJ1DERLdSVsHl%H8N8D$cV+MiQZQe z%Nm5C$CA_+%dbwK8yoM87CpfQH;T7ceJ=5i6kxGO?XVc0;4Fx0z)9G}EzViPD{N+q zy_k$PxzI$$H8!(dh8OWe&NRpCY=vB6wit^_3vHFu$7G91c@Hq*Z(LDTTukj!iEKAo zLi<7Ze1;hRNj1&lS<{HRYr@Wj1EAr zqY6HqYf!a5kE-PbRCQ}nU0NsRi8VGA)JYkJ4yli|uul9Pl$xa)sh2%x>J}fTd{lR0 znaCOX{wxGWGH#ao2;2l*jL%wX-|WX%2rpoogz6|hO5*)bRQCzYJ=M7nMao@6%Ru?T zSPmfzUHeqjWD9GCiyO;ZFw27v? z5Zk7(raZ3Mm^1G%;J{Gv>i^<2U=i9Mv1(0ILvk*(il8Lq zVe5Emtu~=dxk>d3TgD4(@0aFB@vXHvj&M%LAjD9}hqF0J!Qsmp=?u1qhcjEj;S@Zf z;Bcaf6kMlV?p6hdlj8_s2Zd+yvf>clGmQ!kpO-xf4)6E_3J!1Cj}#nU_%9S3o_&{s z8E)+#$qe6_kJqh(F( zGh0U_F4?_h)F*fEOFlF8od3`-tF2c~w5{a3-3G&74UJdFoYx&Wza?p7T8+h9TQ&bw z?ty=9WM27c)5fkq`lNSN*1+Q2q`V_f8Xi+G{;g_IqqVWTb@SIf&f>uf?rP(jhUF>k lvGHxE-J8ZAZ&Qzr={?b8+x1 +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/04/11 16:27:29 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/12 11:28:24 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,6 +23,14 @@ typedef enum s_tokens LESS_LESS, } t_tokens; +typedef struct s_heredoc +{ + char *cmd; + char *del; + struct s_heredoc *next; + struct s_heredoc *prev; +} t_heredoc; + typedef struct s_lexor { char *str; @@ -51,7 +59,6 @@ typedef struct s_tools char *old_pwd; int pipes; bool end; - int end_pid; int *pid; bool reset; } t_tools; @@ -61,6 +68,7 @@ typedef struct s_simple_cmds char **str; int (*builtin)(t_tools *, struct s_simple_cmds *); int num_redirections; + t_heredoc *heredoc; t_lexor *redirections; struct s_simple_cmds *next; struct s_simple_cmds *prev; diff --git a/src/executor/executor.c b/src/executor/executor.c index 5b656b6..28ed9e7 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/04/11 17:26:32 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/12 10:53:43 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -20,8 +20,9 @@ t_simple_cmds *call_expander(t_tools *tools, t_simple_cmds *cmd) start = cmd->redirections; while (cmd->redirections) { - cmd->redirections->str - = expander_str(tools, cmd->redirections->str); + if (cmd->redirections->token != LESS_LESS) + cmd->redirections->str + = expander_str(tools, cmd->redirections->str); cmd->redirections = cmd->redirections->next; } cmd->redirections = start; diff --git a/src/executor/handle_cmd.c b/src/executor/handle_cmd.c index c835e0f..b1779b7 100644 --- a/src/executor/handle_cmd.c +++ b/src/executor/handle_cmd.c @@ -6,7 +6,7 @@ /* By: maiadegraaf redirections) handle_redirections(cmd, tools); - if (cmd->builtin) { cmd->builtin(tools, cmd); diff --git a/src/executor/heredoc.c b/src/executor/heredoc.c index 6439c8d..d4e408e 100644 --- a/src/executor/heredoc.c +++ b/src/executor/heredoc.c @@ -6,7 +6,7 @@ /* By: maiadegraaf ", STDOUT_FILENO); line = get_next_line(STDIN_FILENO); - write(fd, line, ft_strlen(line)); while (ft_strncmp(del, line, del_len)) { + write(fd, line, ft_strlen(line)); free(line); + ft_putstr_fd("heredoc> ", STDOUT_FILENO); line = get_next_line(STDIN_FILENO); // line = expander(tools, line); - write(fd, line, ft_strlen(line)); } close(fd); return (EXIT_SUCCESS); diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index fbdedf6..02551ba 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/11 17:35:27 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/12 11:02:07 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -29,6 +29,7 @@ int reset_tools(t_tools *tools) free(tools->args); implement_tools(tools); tools->reset = true; + free(tools->pid); // system("leaks minishell"); if (tools->end == true) exit (EXIT_SUCCESS); From ec541013206a1325f5bd8516cd010191db2392be Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Tue, 12 Apr 2022 14:47:32 +0200 Subject: [PATCH 119/163] amalgamation of shit --- file2 | 0 includes/parser.h | 3 +- includes/utils.h | 12 +++- src/builtins/mini_exit.c | 4 +- src/executor/check_redirections.c | 19 +------ src/executor/executor.c | 7 +-- src/executor/heredoc.c | 41 ++++++-------- src/parser/handle_redirections.c | 37 +++++++------ src/parser/parser.c | 4 +- src/parser/parser_utils.c | 3 +- src/utils/minishell_loop.c | 9 ++- src/utils/t_heredoc_utils.c | 91 +++++++++++++++++++++++++++++++ src/utils/t_simple_cmds_utils.c | 9 +-- 13 files changed, 161 insertions(+), 78 deletions(-) create mode 100644 file2 create mode 100644 src/utils/t_heredoc_utils.c diff --git a/file2 b/file2 new file mode 100644 index 0000000..e69de29 diff --git a/includes/parser.h b/includes/parser.h index e0db68b..24bad55 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/04/12 11:28:24 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/12 11:35:25 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -45,6 +45,7 @@ typedef struct s_parser_tools t_lexor *lexor_list; t_lexor *redirections; int num_redirections; + t_heredoc *heredoc; struct s_tools *tools; } t_parser_tools; diff --git a/includes/utils.h b/includes/utils.h index 1a336b5..ea16510 100644 --- a/includes/utils.h +++ b/includes/utils.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:36:23 by mgraaf #+# #+# */ -/* Updated: 2022/03/31 16:15:49 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/12 13:36:48 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -22,8 +22,7 @@ char **ft_arrdup(char **arr); int minishell_loop(t_tools *tools); //t_simple_cmds_utils -t_simple_cmds *ft_simple_cmdsnew(char **str, - int (*builtin)(t_tools *, t_simple_cmds *), +t_simple_cmds *ft_simple_cmdsnew(char **str, t_heredoc *heredoc, int num_redirections, t_lexor *redirections); void ft_simple_cmdsadd_back(t_simple_cmds **lst, t_simple_cmds *new); void ft_simple_cmds_rm_first(t_simple_cmds **lst); @@ -37,6 +36,13 @@ void ft_lexordelone(t_lexor **lst, int i); void ft_lexorclear(t_lexor **lst); t_lexor *ft_lexorlast(t_lexor *lst); +//t_heredoc_utils +t_heredoc *ft_heredocnew(char *cmd, char *del); +void ft_heredocadd_back(t_heredoc **lst, t_heredoc *new); +void ft_heredoc_rm_first(t_heredoc **lst); +void ft_heredocclear(t_heredoc **lst); +t_heredoc *ft_heredocfirst(t_heredoc *map); + // int token_reader(t_tools *tools); int add_node(char *str, t_tokens token, t_lexor **lexor_list); diff --git a/src/builtins/mini_exit.c b/src/builtins/mini_exit.c index 312b776..94e5b49 100644 --- a/src/builtins/mini_exit.c +++ b/src/builtins/mini_exit.c @@ -6,7 +6,7 @@ /* By: maiadegraaf end_pid; + status = tools->pid[tools->pipes]; ft_putendl_fd("exit", STDERR_FILENO); if (simple_cmd->str[1] != NULL) status = ft_atoi(simple_cmd->str[1]); diff --git a/src/executor/check_redirections.c b/src/executor/check_redirections.c index 659cf62..799c310 100644 --- a/src/executor/check_redirections.c +++ b/src/executor/check_redirections.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/25 11:39:57 by mgraaf #+# #+# */ -/* Updated: 2022/04/11 18:03:04 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/12 14:10:31 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -68,23 +68,6 @@ int check_infile(t_lexor *redirections) return (fd); } -// int check_heredoc(t_lexor *redirections) -// { -// t_lexor *start; - -// start = redirections; -// while (redirections) -// { -// if (redirections->token == LESS_LESS) -// { - -// } -// redirections = redirections->next; -// } -// redirections = start; -// return (fd); -// } - int handle_redirections(t_simple_cmds *cmd, t_tools *tools) { int fd_in; diff --git a/src/executor/executor.c b/src/executor/executor.c index 28ed9e7..b659017 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/04/12 10:53:43 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/12 14:10:39 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -20,9 +20,8 @@ t_simple_cmds *call_expander(t_tools *tools, t_simple_cmds *cmd) start = cmd->redirections; while (cmd->redirections) { - if (cmd->redirections->token != LESS_LESS) - cmd->redirections->str - = expander_str(tools, cmd->redirections->str); + cmd->redirections->str + = expander_str(tools, cmd->redirections->str); cmd->redirections = cmd->redirections->next; } cmd->redirections = start; diff --git a/src/executor/heredoc.c b/src/executor/heredoc.c index d4e408e..704cb31 100644 --- a/src/executor/heredoc.c +++ b/src/executor/heredoc.c @@ -6,52 +6,43 @@ /* By: maiadegraaf del); fd = open("build/tmp_heredoc_file.txt", O_CREAT | O_RDWR | O_TRUNC, 0644); ft_putstr_fd("heredoc> ", STDOUT_FILENO); line = get_next_line(STDIN_FILENO); - while (ft_strncmp(del, line, del_len)) + while (ft_strncmp(heredoc->del, line, del_len)) { write(fd, line, ft_strlen(line)); free(line); ft_putstr_fd("heredoc> ", STDOUT_FILENO); line = get_next_line(STDIN_FILENO); - // line = expander(tools, line); } close(fd); - return (EXIT_SUCCESS); + return (fd); } -int main(void) +int read_heredoc(int fd, t_heredoc *heredoc) { - t_tools tools; - t_simple_cmds cmd; - ft_heredoc(&tools, &cmd, "\"echo\"|\"EOF\""); + +} + +int ft_heredoc(t_tools *tools, t_simple_cmds *cmd, t_heredoc *heredoc) +{ + int fd; + + fd = create_heredoc(heredoc); + read_heredoc(fd, heredoc); + return (EXIT_SUCCESS); } diff --git a/src/parser/handle_redirections.c b/src/parser/handle_redirections.c index 7439c71..c1aa741 100644 --- a/src/parser/handle_redirections.c +++ b/src/parser/handle_redirections.c @@ -6,7 +6,7 @@ /* By: maiadegraaf token == LESS_LESS && tmp->prev != NULL && tmp->prev->str) + cmd = NULL; + while (tmp->prev != NULL && tmp->prev->str && ft_strlen(tmp->prev->str) > 0) + { + cmd = join_heredoc(tmp->prev->str, cmd); + ft_lexordelone(&parser_tools->lexor_list, tmp->prev->i); + } + if (cmd) { - node = ft_lexornew(join_heredoc(tmp->prev->str, tmp->next->str), - tmp->token); + node = ft_heredocnew(cmd, ft_strdup(tmp->next->str)); if (!node) parser_error(1, parser_tools->tools, parser_tools->lexor_list); - ft_lexoradd_back(&parser_tools->redirections, node); - ft_lexordelone(&parser_tools->lexor_list, tmp->prev->i); + ft_heredocadd_back(&parser_tools->heredoc, node); index_1 = tmp->i; index_2 = tmp->next->i; ft_lexordelone(&parser_tools->lexor_list, index_1); ft_lexordelone(&parser_tools->lexor_list, index_2); - parser_tools->num_redirections++; } - else - add_new_redirection(tmp, parser_tools); return (1); } diff --git a/src/parser/parser.c b/src/parser/parser.c index 6d61e53..4d0b180 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/04/08 10:57:24 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/12 11:37:07 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -38,7 +38,7 @@ t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) } arg_size--; } - return (ft_simple_cmdsnew(str, builtin_arr(str[0]), + return (ft_simple_cmdsnew(str, parser_tools->heredoc, parser_tools->num_redirections, parser_tools->redirections)); } diff --git a/src/parser/parser_utils.c b/src/parser/parser_utils.c index 97b803b..9a2fb2b 100644 --- a/src/parser/parser_utils.c +++ b/src/parser/parser_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/03/04 11:52:02 by mgraaf #+# #+# */ -/* Updated: 2022/04/11 16:42:32 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/12 13:54:16 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ t_parser_tools init_parser_tools(t_lexor *lexor_list, t_tools *tools) t_parser_tools parser_tools; parser_tools.lexor_list = lexor_list; + parser_tools.heredoc = NULL; parser_tools.redirections = NULL; parser_tools.num_redirections = 0; parser_tools.tools = tools; diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 02551ba..e84eb0a 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/12 11:02:07 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/12 14:13:42 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -20,6 +20,7 @@ int implement_tools(t_tools *tools) tools->lexor_list = NULL; tools->end = false; tools->reset = false; + tools->pid = NULL; return (1); } @@ -29,7 +30,11 @@ int reset_tools(t_tools *tools) free(tools->args); implement_tools(tools); tools->reset = true; - free(tools->pid); + if (tools->pid) + { + free(tools->pid); + tools->pid = NULL; + } // system("leaks minishell"); if (tools->end == true) exit (EXIT_SUCCESS); diff --git a/src/utils/t_heredoc_utils.c b/src/utils/t_heredoc_utils.c new file mode 100644 index 0000000..f87e0aa --- /dev/null +++ b/src/utils/t_heredoc_utils.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* t_heredoc_utils.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ +/* Updated: 2022/04/12 11:34:17 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "utils.h" + +t_heredoc *ft_heredocnew(char *cmd, char *del) +{ + t_heredoc *new_element; + + new_element = (t_heredoc *)malloc(sizeof(t_heredoc)); + if (!new_element) + return (0); + new_element->cmd = cmd; + new_element->del = del; + new_element->next = NULL; + new_element->prev = NULL; + return (new_element); +} + +void ft_heredocadd_back(t_heredoc **lst, t_heredoc *new) +{ + t_heredoc *tmp; + t_heredoc *prev; + + tmp = *lst; + if (*lst == NULL) + { + *lst = new; + return ; + } + while (tmp->next != NULL) + { + prev = tmp; + tmp = tmp->next; + } + tmp->next = new; + new->prev = tmp; +} + +void ft_heredoc_rm_first(t_heredoc **lst) +{ + t_heredoc *tmp; + + if (!*lst) + return ; + tmp = (*lst)->next; + free(*lst); + *lst = tmp; +} + +void ft_heredocclear(t_heredoc **lst) +{ + t_heredoc *tmp; + + if (!*lst) + return ; + while (*lst) + { + tmp = (*lst)->next; + if ((*lst)->cmd) + free((*lst)->cmd); + free((*lst)->del); + free(*lst); + *lst = tmp; + } + *lst = NULL; +} + +t_heredoc *ft_heredocfirst(t_heredoc *map) +{ + int i; + + i = 0; + if (!map) + return (NULL); + while (map->prev != NULL) + { + map = map->prev; + i++; + } + return (map); +} diff --git a/src/utils/t_simple_cmds_utils.c b/src/utils/t_simple_cmds_utils.c index 88c0aee..7a1f5f4 100644 --- a/src/utils/t_simple_cmds_utils.c +++ b/src/utils/t_simple_cmds_utils.c @@ -6,14 +6,13 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/03/31 16:14:40 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/12 11:39:02 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "utils.h" -t_simple_cmds *ft_simple_cmdsnew(char **str, - int (*builtin)(t_tools *, t_simple_cmds *), +t_simple_cmds *ft_simple_cmdsnew(char **str, t_heredoc *heredoc, int num_redirections, t_lexor *redirections) { t_simple_cmds *new_element; @@ -22,7 +21,8 @@ t_simple_cmds *ft_simple_cmdsnew(char **str, if (!new_element) return (0); new_element->str = str; - new_element->builtin = builtin; + new_element->builtin = builtin_arr(str[0]); + new_element->heredoc = heredoc; new_element->num_redirections = num_redirections; new_element->redirections = redirections; new_element->next = NULL; @@ -72,6 +72,7 @@ void ft_simple_cmdsclear(t_simple_cmds **lst) while (*lst) { tmp = (*lst)->next; + ft_heredocclear(&(*lst)->heredoc); redirections_tmp = (*lst)->redirections; ft_lexorclear(&redirections_tmp); if ((*lst)->str) From 1d4d98902513eca336ac1ce64414e8a759433adc Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Tue, 12 Apr 2022 14:49:52 +0200 Subject: [PATCH 120/163] small changes signal ctrl-c ctrl-d --- src/main.c | 6 ++---- src/signals.c | 12 ++---------- src/utils/minishell_loop.c | 6 +++--- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/src/main.c b/src/main.c index 90ba01f..a006133 100644 --- a/src/main.c +++ b/src/main.c @@ -6,14 +6,12 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/04/08 17:32:46 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/12 14:20:31 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -void INThandler(int); - int main(int argc, char **argv, char **envp) { t_tools tools; @@ -25,8 +23,8 @@ int main(int argc, char **argv, char **envp) printf("This program does not accept arguments\n"); exit(0); } - init_signals(); tools.envp = ft_arrdup(envp); + init_signals(); parse_envp(&tools); implement_tools(&tools); minishell_loop(&tools); diff --git a/src/signals.c b/src/signals.c index fb0cf84..c789220 100644 --- a/src/signals.c +++ b/src/signals.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/04/08 14:35:54 by fpolycar #+# #+# */ -/* Updated: 2022/04/08 17:46:30 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/12 14:30:11 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,22 +14,14 @@ void sigint_handler(int sig) { - ft_putstr_fd("minishell$ \n", STDOUT_FILENO); + ft_putstr_fd("\n", STDERR_FILENO); rl_on_new_line(); rl_replace_line("", 0); rl_redisplay(); (void) sig; } -void sigquit_handler(int sig) -{ - ft_putstr_fd("Quit: ", 2); - ft_putnbr_fd(sig, 2); - ft_putchar_fd('\n', 2); -} - void init_signals(void) { signal(SIGINT, sigint_handler); - signal(SIGQUIT, SIG_IGN); } diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index fcebf82..9780cd4 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/08 17:32:43 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/12 14:48:28 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -37,9 +37,9 @@ int reset_tools(t_tools *tools) int minishell_loop(t_tools *tools) { - - // signal(SIGQUIT, sigint_handler); tools->args = readline("minishell$ "); + if (!tools->args) + mini_exit(tools, tools->simple_cmds); add_history(tools->args); if (!count_quotes(tools->args)) ft_error(2, tools); From 2c9437b7bba8b3dc1976755092eece75d8ef999b Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Tue, 12 Apr 2022 15:37:31 +0200 Subject: [PATCH 121/163] signal ctrl-c ctrl-d normally done --- Makefile | 5 ++-- src/builtins/mini_exit.c | 7 +++-- src/executor/heredoc.c | 60 +++++++++++++++++++------------------- src/utils/minishell_loop.c | 7 +++-- 4 files changed, 41 insertions(+), 38 deletions(-) diff --git a/Makefile b/Makefile index ade0cb2..9cb4495 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,7 @@ LIBFT = ./libraries/libft/libft.a HEADER = $(wildcard ./includes/*.h) -READLINE_DIR = #/Users/fpolycar/.brew/opt/readline +READLINE_DIR = /Users/fpolycar/.brew/opt/readline INCLUDES =-Iincludes -I$(PATHP) -I$(LIBFTP) -I$(READLINE_DIR)/include @@ -75,10 +75,9 @@ $(PATHO)%.o:: $(PATHEX)%.c $(HEADERS) @$(CC) -c $(FLAGS) $(INCLUDES) $< -o $@ $(NAME): $(LIBFT) $(OBJS) $(HEADERS) - @$(CC) $(FLAGS) $(LIBFT) $(OBJS) -lreadline -o $(NAME) + @$(CC) $(FLAGS) $(LIBFT) $(OBJS) -lreadline -L$(READLINE_DIR)/lib -o $(NAME) @echo "Success" -#-I$(READLINE_DIR)/include -L$(READLINE_DIR)/lib $(LIBFT): @$(MAKE) -C ./libraries/libft diff --git a/src/builtins/mini_exit.c b/src/builtins/mini_exit.c index 94e5b49..18fc9cd 100644 --- a/src/builtins/mini_exit.c +++ b/src/builtins/mini_exit.c @@ -6,19 +6,20 @@ /* By: maiadegraaf -//probably have to add some frees in here. int mini_exit(t_tools *tools, t_simple_cmds *simple_cmd) { int status; - status = tools->pid[tools->pipes]; + status = 0; + if (tools->pipes != 0) + status = tools->pid[tools->pipes]; ft_putendl_fd("exit", STDERR_FILENO); if (simple_cmd->str[1] != NULL) status = ft_atoi(simple_cmd->str[1]); diff --git a/src/executor/heredoc.c b/src/executor/heredoc.c index 704cb31..822080f 100644 --- a/src/executor/heredoc.c +++ b/src/executor/heredoc.c @@ -6,43 +6,43 @@ /* By: maiadegraaf del); - fd = open("build/tmp_heredoc_file.txt", O_CREAT | O_RDWR | O_TRUNC, 0644); - ft_putstr_fd("heredoc> ", STDOUT_FILENO); - line = get_next_line(STDIN_FILENO); - while (ft_strncmp(heredoc->del, line, del_len)) - { - write(fd, line, ft_strlen(line)); - free(line); - ft_putstr_fd("heredoc> ", STDOUT_FILENO); - line = get_next_line(STDIN_FILENO); - } - close(fd); - return (fd); -} +// del_len = ft_strlen(heredoc->del); +// fd = open("build/tmp_heredoc_file.txt", O_CREAT | O_RDWR | O_TRUNC, 0644); +// ft_putstr_fd("heredoc> ", STDOUT_FILENO); +// line = get_next_line(STDIN_FILENO); +// while (ft_strncmp(heredoc->del, line, del_len)) +// { +// write(fd, line, ft_strlen(line)); +// free(line); +// ft_putstr_fd("heredoc> ", STDOUT_FILENO); +// line = get_next_line(STDIN_FILENO); +// } +// close(fd); +// return (fd); +// } -int read_heredoc(int fd, t_heredoc *heredoc) -{ +// int read_heredoc(int fd, t_heredoc *heredoc) +// { -} +// } -int ft_heredoc(t_tools *tools, t_simple_cmds *cmd, t_heredoc *heredoc) -{ - int fd; +// int ft_heredoc(t_tools *tools, t_simple_cmds *cmd, t_heredoc *heredoc) +// { +// int fd; - fd = create_heredoc(heredoc); - read_heredoc(fd, heredoc); - return (EXIT_SUCCESS); -} +// fd = create_heredoc(heredoc); +// read_heredoc(fd, heredoc); +// return (EXIT_SUCCESS); +// } diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index ddbb8ab..c0d77f3 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/12 14:52:38 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/12 15:27:55 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -46,7 +46,10 @@ int minishell_loop(t_tools *tools) { tools->args = readline("minishell$ "); if (!tools->args) - mini_exit(tools, tools->simple_cmds); + { + ft_putstr_fd("exit", STDOUT_FILENO); + exit(EXIT_SUCCESS); + } add_history(tools->args); if (!count_quotes(tools->args)) ft_error(2, tools); From 7f6fa9d0ab541c5f4696c5ba94342c864896186a Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Tue, 12 Apr 2022 15:39:12 +0200 Subject: [PATCH 122/163] fix putstr -> putendl --- src/utils/minishell_loop.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index c0d77f3..a0e0548 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/12 15:27:55 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/12 15:38:10 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -47,7 +47,7 @@ int minishell_loop(t_tools *tools) tools->args = readline("minishell$ "); if (!tools->args) { - ft_putstr_fd("exit", STDOUT_FILENO); + ft_putendl_fd("exit", STDOUT_FILENO); exit(EXIT_SUCCESS); } add_history(tools->args); From 828559e4bbab9ea07bf8e8e0e85ada2a02f96d3f Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Tue, 12 Apr 2022 17:09:09 +0200 Subject: [PATCH 123/163] echo -n -> need to be normed --- file1 | 21 +++++++++++++++++++++ src/builtins/mini_cd.c | 4 ++-- src/builtins/mini_echo.c | 24 ++++++++++++++++++++---- 3 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 file1 diff --git a/file1 b/file1 new file mode 100644 index 0000000..f90c859 --- /dev/null +++ b/file1 @@ -0,0 +1,21 @@ +total 208 +drwxr-xr-x 20 fpolycar piscine 680 Apr 12 15:53 . +drwxr-xr-x 8 fpolycar piscine 272 Mar 23 14:00 .. +drwxr-xr-x 16 fpolycar piscine 544 Apr 12 15:39 .git +-rw-r--r-- 1 fpolycar piscine 37 Mar 23 11:08 .gitignore +-rw-r--r-- 1 fpolycar piscine 85 Mar 23 11:08 .gitmodules +drwxr-xr-x 5 fpolycar piscine 170 Apr 12 14:51 .vscode +-rw-r--r-- 1 fpolycar piscine 2309 Apr 12 14:57 Makefile +drwxr-xr-x 18 fpolycar piscine 612 Apr 8 10:17 Unity +-rwxr-xr-x 1 fpolycar piscine 37570 Apr 12 14:51 a.out +drwxr-xr-x 3 fpolycar piscine 102 Apr 12 14:51 a.out.dSYM +drwxr-xr-x 4 fpolycar piscine 136 Mar 23 13:59 build +-rw-r--r-- 1 fpolycar piscine 0 Apr 12 15:54 file1 +-rw-r--r-- 1 fpolycar piscine 0 Apr 12 14:51 file2 +drwxr-xr-x 9 fpolycar piscine 306 Apr 12 14:51 includes +drwxr-xr-x 3 fpolycar piscine 102 Mar 23 11:08 libraries +-rwxr-xr-x 1 fpolycar piscine 51420 Apr 12 15:37 minishell +drwxr-xr-x 3 fpolycar piscine 102 Mar 23 11:08 minishell.dSYM +drwxr-xr-x 12 fpolycar piscine 408 Apr 12 14:51 src +drwxr-xr-x 11 fpolycar piscine 374 Apr 12 10:48 test +drwxr-xr-x 3 fpolycar piscine 102 Mar 23 11:08 utilities diff --git a/src/builtins/mini_cd.c b/src/builtins/mini_cd.c index ccc701e..6405b9d 100644 --- a/src/builtins/mini_cd.c +++ b/src/builtins/mini_cd.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:17:04 by mgraaf #+# #+# */ -/* Updated: 2022/04/06 10:44:11 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/12 15:55:28 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -58,7 +58,7 @@ int mini_cd(t_tools *tools, t_simple_cmds *simple_cmd) { ret = chdir(simple_cmd->str[1]); if (ret != 0) - ft_putendl_fd("Path do not exist", STDERR_FILENO); + ft_putendl_fd("Path does not exist", STDERR_FILENO); } if (ret != 0) return (EXIT_FAILURE); diff --git a/src/builtins/mini_echo.c b/src/builtins/mini_echo.c index d5dbd3b..47d30b2 100644 --- a/src/builtins/mini_echo.c +++ b/src/builtins/mini_echo.c @@ -6,7 +6,7 @@ /* By: maiadegraaf str[i]) { ft_putchar_fd('\n', STDOUT_FILENO); return (EXIT_SUCCESS); } - if (!ft_strncmp(simple_cmd->str[1], "-n", ft_strlen(simple_cmd->str[1]))) - print_lines(2, simple_cmd->str, STDOUT_FILENO); + while (simple_cmd->str[i][0] == '-') + { + j = 1; + while (simple_cmd->str[i][j] == 'n') + j++; + printf("%d\n", j); + if (simple_cmd->str[i][j] == '\0') + n_option = true; + else + break ; + i++; + } + if (n_option == true) + print_lines(i, simple_cmd->str, STDOUT_FILENO); else { print_lines(1, simple_cmd->str, STDOUT_FILENO); From 854cf168b70626d7ce172e38b70dae0817a4a62e Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Tue, 12 Apr 2022 17:34:36 +0200 Subject: [PATCH 124/163] heredoc works --- Makefile | 4 +- a.out | Bin 37570 -> 0 bytes file2 | 0 includes/executor.h | 10 +++- src/executor/executor.c | 4 +- src/executor/handle_cmd.c | 6 ++- src/executor/heredoc.c | 81 +++++++++++++++++++++++++++---- src/parser/handle_redirections.c | 21 ++++---- src/utils/minishell_loop.c | 2 +- src/utils/t_lexor_utils.c | 1 + 10 files changed, 98 insertions(+), 31 deletions(-) delete mode 100755 a.out delete mode 100644 file2 diff --git a/Makefile b/Makefile index ade0cb2..d8f0113 100644 --- a/Makefile +++ b/Makefile @@ -30,13 +30,13 @@ src = $(wildcard $(PATHS)*.c) \ OBJS = $(addprefix $(PATHO), $(notdir $(patsubst %.c, %.o, $(src)))) -FLAGS = -Wall -Werror -Wextra -g #-fsanitize=thread +FLAGS = -Wall -Werror -Wextra -g #-fsanitize=address LIBFT = ./libraries/libft/libft.a HEADER = $(wildcard ./includes/*.h) -READLINE_DIR = #/Users/fpolycar/.brew/opt/readline +READLINE_DIR = #/Users/$(USER)/.brew/opt/readline INCLUDES =-Iincludes -I$(PATHP) -I$(LIBFTP) -I$(READLINE_DIR)/include diff --git a/a.out b/a.out deleted file mode 100755 index 575821d02af4522c87ba1ba6bff998ecbfc5bcb5..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 37570 zcmeHQeQ=b;8Q=HrVlD}iR032{5s1vis*=0;d`EH{6LcDyDMKZmbUozW7!m}ERDPY;8OBsz zYK$S8?20EF3&-oI=z1{JR1=IhrSfAxGWf++25shN7yN)BPrB|5h5}yynmCuVZJ%P=fI2gdT`Fy;%2U4lxwBv2;nW$Fm0=w<4ns#!0h zb-gB-SWRgs^@tQU_}L|ml6c6J>dOiLzkT7N+bR|;l#bAqqF319@H%ZEB;sS{Rl-=9 za3k;QLh(aEcY&_OKw7i!B3C}h82+;MM;TiUkg`x;f=tOHaxTiv$YvWrCZ$@;&rWJF zh_NX0k;z|+n}UI2pSPyiwaMoKO=%J`>9P6F_g=VUVD8F2>%aQs@(=1Z`~YqF$c9Z6 zF~;z^f!*&cGtkHFLrJLs_7NX4)SO6e88Y=5ewv5vttE7UKFc-*L+<*b`9;vnTVu?= z8U^t$066(53k6MhQMwA5`g2+tJ76eD%_=Db6aoqXg@8gpA)pXY2q**;0tx|zfI>ha zpb$_9Cy)5Kssx1QY@a0fm4iUDmw*-7S;*+geujpN_?z z>>dX`;~WEXz%vRt(L5FbAUl|EPV2m`@X}r@Yu#EBK2e~tff6g**;YQellPyT(>jHP zm+psL{CfD(K?~$hG=Kqx7|Eb^~jm0LmfuEg^gWTqd4%leeT5f@@_KrcTrL`OO_ds|2Uiyyt zwz;jZ!d9+lb7e=Y+#<%aXEM@yE{D` ziIod%v2KK4Pc|Dk4ZFZ+pcj1lV3!@36l$4gXx?eYs-oEr-e>prmO;k4X!^5PP#^Js z%fd>dSOc1C_j=2EF}4r&Lho9P&8Tx|m_I^)w3tQkJM_6`y-z{c3v{sxUE;h21k#Gqq$u_F@q%M|PgAG_8yYb#^dldVzs65dnDr8>1|0MFhm7HMZ#AHr8- zUqqh-hG+vFdCSI1W7(*aZQ0>S25=^xeW+)8`^v<6)Dh-Av>xC=0Pme>jf580`u8b=+<%?MG|JApElpdy#ym z&8qjWq`inaG#@WXwcbJg4!cBTA$cB7Kvj!j$_7URoYky>@)D0 zm}oD+p6Ba8V}Tjh3H^n%4)Et)#(psN74XGNBe{qT1`U3nNj-_D#2o8EevbEy$R~`k z(CaV8Sl&0Rf02#`|8?*y!xqq$w*DK)HjWADDlm-eFXVIm(LbX8CY|E?6VIXaH|awA z6!I_BznuKUhFFHU4L{k1m`!UPW$a)(VmJ1|L84I}B3k;$Y~Vv{MLx9)?c3p36tjtb z<9zH%8RMgQkeP-3M6nruFUOxo`+m%UkMMm}sIe?sv*9TR1}g zx>uY($TKoA zht7i@BMuBy0$aR?lpCYih&|J5P54NqI47u$VU2r$R}XNDB8GCkG3QI@og^n^@6)+K zxUa&xbXyT8uov$ZYt(H;jAQm~gzd5+u*Iv8e4S&ohTtKwp%~B(-BQ~qW(%1M<0A5Zl`JOdE7(4^ck{~^lF2AlATy{ z^ZgWeD6*4m!&uTUhCAr^xPFPXPw0guE z&-1evK6HrRKg;H}j;DPo&SYw*7$EVZGmC7)z8}b>7#lxBO}3e0xSg?=>}hmwF>vUH zK0V;g9+>3KR7JkX^Ep+FCkC^rBXwmy2;|!hG6psd*6LI!? z&(3?tt6X;M6@{pSvi8*VjeE-28kt)$Ocpz>JE;H&2BVX~4RPS_^e;k&#aEO;s2-41h7Jrn{80fm4hapb$_9Cy)5Kssx1QY@a0fm4< zKp~(IPzWdl6aoqXg@8gpA)pXY2q**;0tx|zfI>hapb$_9Cy)5V&Xr z^1)#VawD=G+4vR!DoDnA|3;nu&u*D`-GQ$GIAn`4Ps0ZUw5ySCN5+RJG!OE67Ht@FS%t+Q+2?_bf>Q&=w`Yn;KsAQ+UIL?f|>nLo>a8woj{ZukrdJusRTA=QX$ACF(5} zVy4$Lc|7ctWsN(e``sHuy3gx(vyUzOQwSC>M!VcT<`3yCXl0#F#x4<`Z(w`0Alt7s zHig*ZmNhUdu(pBZS)6P?IM+9~js=*;ksCo%MjvJ}(~6)!}i>hsR@nsx{D9*r%L9txnllOAnBjDgKSi5vNjrPht5@cY<%7}wQ=A#ZHJ8I0 zx4`auKGSA84qN)QcC8D4IxQu&ZI*{DXSI#4ZjJo}PBZortVbA2g2ID`3#ClYr_lHY zApSpljrCVl)zBE4Q-sG%{D1LOw*md>CYsvlI~WdRN<=Th!$AZR(Jp$3lqx?P{LK1X z33@2`jY57S`boZ|i8aZeEo4wZ^3C)}^0%bOe`FZ>`$`O3J`(aJE#>zM`=op`J(B$1 z6#4%eM*efx8}cjh0T8lZ(o%kVihMIYlKf@hXU2au=%Mhx9UmC4G|0MXC?ZhP2-{%i#SZn}NTi zrF^&tPg1^_9!dTp@H5L_33@2}%Y^)wg?vd%`L!wX&9qtmkra7jagdnhPX|r%2U7Nb zn~?ti`lmyvq^10PsvuE8_?u~3|E?R2z9+5=UJE%=%3e+~3d_8-KDe2Di1#8R?f(o+6LExfp^iSY4V=_KW6SUm#MS`YpB@wSOK}$R<1TFnzxuB)LIR%}IkH*n_9IlWL ze-Zw9pP=RbcudgJUw$QMiT_?f%Xms33?qK>yz3IQJa3K(TE_pAf|l{<6FFYs{iUFp zIKKu2E!U6y-1v(QqzNfBzS)@6KQo0cO`+$e&!E0i5K4Sbo0$lPh9Nqr7dx7(>)RZbxM%p zPDPaYdPj7Sg!2m>r$Pa`$%!gm??ly4mpNIdE1jtG8=szh12t~1{Jv{kQAhRaoGh#; zSiRouV#UjY?m(~@*JfOy*95AoJ;e(eoK5v^e<)adi#Jr+R8w5<^?QT0ZlAB%=dB4; z2fXee)jXkMMmU55Ft^Bgk*qkwqUwSK$A&_Q6~Sq=6PlweinC0};_D@Z!L-p9Ml4Oj z$RP}2@SahYMH~~N$U6;5yc*PKxfPFr4_& zJ~`B}WKu*vI%6oEJ2j>YC*c89af0!bi#A+8v}c&bh995thp|=2D~xWI$yjSnz6GCN z{8q^!M`_hX4fv;n!J9w)_nzGj&(X7+wm;?nqjk!(n={TWtL(YvrjyG%7H*&Y!u|UW ztY~<%*VASD;P~-6%iR&s*s`2OZU+>!fcIcM=R~stN zZO?veXXiQB;oiy_*^jQi+q!sW;~###wq@d1FMhwX|DIUix}T*#{&IBIfzrlw*HfjZ zzKQ?E_Aih8H0RK{^G|1Ve;t{B-=(iSc+C@I-@fgmoZpY1`00#0(q_LnWx=yqr8%1( gIdjr^_~0-0{%OlIfrXpiJhc2blUDw%=UvBt0j}Fku>b%7 diff --git a/file2 b/file2 deleted file mode 100644 index e69de29..0000000 diff --git a/includes/executor.h b/includes/executor.h index e83527c..be113be 100644 --- a/includes/executor.h +++ b/includes/executor.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:17:39 by mgraaf #+# #+# */ -/* Updated: 2022/04/11 17:27:42 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/12 17:32:42 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -26,7 +26,13 @@ t_simple_cmds *call_expander(t_tools *tools, t_simple_cmds *cmd); // handle_cmd int find_cmd(t_simple_cmds *cmd, t_tools *tools); void handle_cmd(t_simple_cmds *cmd, t_tools *tools); -void fork_cmd(t_simple_cmds *cmd, t_tools *tools, int end[2], int fd_in); +void dup_cmd(t_simple_cmds *cmd, t_tools *tools, int end[2], int fd_in); void single_cmd(t_simple_cmds *cmd, t_tools *tools); +// heredoc +int ft_heredoc(t_tools *tools, t_heredoc *heredoc); +int read_heredoc(t_heredoc *heredoc, t_tools *tools); +int create_heredoc(t_heredoc *heredoc, bool quotes, t_tools *tools); +int send_heredoc(t_tools *tools, t_simple_cmds *cmd); + #endif \ No newline at end of file diff --git a/src/executor/executor.c b/src/executor/executor.c index b659017..cc8e7c6 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/04/12 14:10:39 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/12 15:06:51 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -56,7 +56,7 @@ int ft_fork(t_tools *tools, int end[2], int fd_in, t_simple_cmds *cmd) if (tools->pid[i] < 0) ft_error(5, tools); if (tools->pid[i] == 0) - fork_cmd(cmd, tools, end, fd_in); + dup_cmd(cmd, tools, end, fd_in); i++; return (EXIT_SUCCESS); } diff --git a/src/executor/handle_cmd.c b/src/executor/handle_cmd.c index b1779b7..290fd0e 100644 --- a/src/executor/handle_cmd.c +++ b/src/executor/handle_cmd.c @@ -6,7 +6,7 @@ /* By: maiadegraaf heredoc) + send_heredoc(tools, cmd); if (cmd->redirections) handle_redirections(cmd, tools); if (cmd->builtin) @@ -43,7 +45,7 @@ void handle_cmd(t_simple_cmds *cmd, t_tools *tools) find_cmd(cmd, tools); } -void fork_cmd(t_simple_cmds *cmd, t_tools *tools, int end[2], int fd_in) +void dup_cmd(t_simple_cmds *cmd, t_tools *tools, int end[2], int fd_in) { if (cmd->prev && dup2(fd_in, STDIN_FILENO) < 0) ft_error(4, tools); diff --git a/src/executor/heredoc.c b/src/executor/heredoc.c index 704cb31..5e5a451 100644 --- a/src/executor/heredoc.c +++ b/src/executor/heredoc.c @@ -6,13 +6,25 @@ /* By: maiadegraaf del, line, del_len)) { + if (quotes == false) + line = send_expander(tools, line); write(fd, line, ft_strlen(line)); free(line); ft_putstr_fd("heredoc> ", STDOUT_FILENO); line = get_next_line(STDIN_FILENO); } close(fd); - return (fd); + return (EXIT_SUCCESS); } -int read_heredoc(int fd, t_heredoc *heredoc) +int read_heredoc(t_heredoc *heredoc, t_tools *tools) { - + int fd; + char **cmds; + int i; + char *mycmd; + + cmds = ft_split(heredoc->cmd, ' '); + cmds = expander(tools, cmds); + fd = open("build/tmp_heredoc_file.txt", O_RDONLY); + if (dup2(fd, STDIN_FILENO) < 0) + ft_error(4, tools); + i = 0; + while (tools->paths[i]) + { + mycmd = ft_strjoin(tools->paths[i], cmds[0]); + if (!access(mycmd, F_OK)) + execve(mycmd, cmds, tools->envp); + free(mycmd); + i++; + } + close(fd); + exit(EXIT_FAILURE); +} + +int ft_heredoc(t_tools *tools, t_heredoc *heredoc) +{ + bool quotes; + + if (heredoc->del[0] == '\"' + && heredoc->del[ft_strlen(heredoc->del) - 1] == '\"') + quotes = true; + else + quotes = false; + create_heredoc(heredoc, quotes, tools); + if (heredoc->cmd) + read_heredoc(heredoc, tools); + return (EXIT_SUCCESS); } -int ft_heredoc(t_tools *tools, t_simple_cmds *cmd, t_heredoc *heredoc) +int send_heredoc(t_tools *tools, t_simple_cmds *cmd) { - int fd; - - fd = create_heredoc(heredoc); - read_heredoc(fd, heredoc); + int pid; + int status; + t_heredoc *start; + + start = cmd->heredoc; + while (cmd->heredoc) + { + pid = fork(); + if (pid < 0) + ft_error(5, tools); + if (pid == 0) + ft_heredoc(tools, cmd->heredoc); + cmd->heredoc = cmd->heredoc->next; + waitpid(pid, &status, 0); + } + cmd->heredoc = start; return (EXIT_SUCCESS); } diff --git a/src/parser/handle_redirections.c b/src/parser/handle_redirections.c index c1aa741..a711bfa 100644 --- a/src/parser/handle_redirections.c +++ b/src/parser/handle_redirections.c @@ -6,7 +6,7 @@ /* By: maiadegraaf prev->str, cmd); ft_lexordelone(&parser_tools->lexor_list, tmp->prev->i); } - if (cmd) - { - node = ft_heredocnew(cmd, ft_strdup(tmp->next->str)); - if (!node) - parser_error(1, parser_tools->tools, parser_tools->lexor_list); - ft_heredocadd_back(&parser_tools->heredoc, node); - index_1 = tmp->i; - index_2 = tmp->next->i; - ft_lexordelone(&parser_tools->lexor_list, index_1); - ft_lexordelone(&parser_tools->lexor_list, index_2); - } + node = ft_heredocnew(cmd, ft_strdup(tmp->next->str)); + if (!node) + parser_error(1, parser_tools->tools, parser_tools->lexor_list); + ft_heredocadd_back(&parser_tools->heredoc, node); + index_1 = tmp->i; + index_2 = tmp->next->i; + ft_lexordelone(&parser_tools->lexor_list, index_1); + ft_lexordelone(&parser_tools->lexor_list, index_2); return (1); } diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index e84eb0a..6365b1d 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/12 14:13:42 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/12 16:24:22 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/utils/t_lexor_utils.c b/src/utils/t_lexor_utils.c index a41b8a3..367f9fc 100644 --- a/src/utils/t_lexor_utils.c +++ b/src/utils/t_lexor_utils.c @@ -53,6 +53,7 @@ void ft_lexorclear_one(t_lexor **lst) if ((*lst)->str) { free((*lst)->str); + (*lst)->str = NULL; } free(*lst); *lst = NULL; From 0fad20280d88913d7c88f07872c84eec9c49153f Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Tue, 12 Apr 2022 18:14:16 +0200 Subject: [PATCH 125/163] makefile readline edit --- Makefile | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index ade0cb2..7be33f6 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,9 @@ LIBFT = ./libraries/libft/libft.a HEADER = $(wildcard ./includes/*.h) -READLINE_DIR = #/Users/fpolycar/.brew/opt/readline +READLINE_DIR = $(shell brew --prefix readline) + +READLINE_LIB = -lreadline -lhistory - L $(READLINE_DIR)/lib INCLUDES =-Iincludes -I$(PATHP) -I$(LIBFTP) -I$(READLINE_DIR)/include @@ -75,7 +77,7 @@ $(PATHO)%.o:: $(PATHEX)%.c $(HEADERS) @$(CC) -c $(FLAGS) $(INCLUDES) $< -o $@ $(NAME): $(LIBFT) $(OBJS) $(HEADERS) - @$(CC) $(FLAGS) $(LIBFT) $(OBJS) -lreadline -o $(NAME) + @$(CC) $(FLAGS) $(LIBFT) $(OBJS) -o $(NAME) @echo "Success" #-I$(READLINE_DIR)/include -L$(READLINE_DIR)/lib From c5d4699a38873c5ff61dae4da55de2f423f23bea Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 13 Apr 2022 09:57:20 +0200 Subject: [PATCH 126/163] merge --- Makefile | 2 +- src/builtins/mini_echo.c | 19 ++++--------------- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index 76f7164..e4b95d1 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,7 @@ LIBFT = ./libraries/libft/libft.a HEADER = $(wildcard ./includes/*.h) -READLINE_DIR = #/Users/$(USER)/.brew/opt/readline +READLINE_DIR = /Users/$(USER)/.brew/opt/readline INCLUDES =-Iincludes -I$(PATHP) -I$(LIBFTP) -I$(READLINE_DIR)/include diff --git a/src/builtins/mini_echo.c b/src/builtins/mini_echo.c index 47d30b2..6ddb5c9 100644 --- a/src/builtins/mini_echo.c +++ b/src/builtins/mini_echo.c @@ -6,7 +6,7 @@ /* By: maiadegraaf str[i]) - { - ft_putchar_fd('\n', STDOUT_FILENO); - return (EXIT_SUCCESS); - } - while (simple_cmd->str[i][0] == '-') + while (simple_cmd->str[i] && simple_cmd->str[i][0] == '-') { j = 1; while (simple_cmd->str[i][j] == 'n') j++; - printf("%d\n", j); if (simple_cmd->str[i][j] == '\0') n_option = true; else break ; i++; } - if (n_option == true) - print_lines(i, simple_cmd->str, STDOUT_FILENO); - else - { - print_lines(1, simple_cmd->str, STDOUT_FILENO); + print_lines(i, simple_cmd->str, STDOUT_FILENO); + if (n_option == false) ft_putchar_fd('\n', STDOUT_FILENO); - } return (EXIT_SUCCESS); } From bcb8c52d2ad2001bca8e88c4f791584d06beb550 Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Wed, 13 Apr 2022 11:17:35 +0200 Subject: [PATCH 127/163] Heredoc works!! And Makefile change --- Makefile | 4 +- includes/utils.h | 3 +- libraries/libft/ft_strlen.c | 9 ++-- src/executor/heredoc.c | 2 +- src/utils/t_lexor_clear_utils.c | 81 ++++++++++++++++++++++++++++++ src/utils/t_lexor_utils.c | 87 --------------------------------- 6 files changed, 89 insertions(+), 97 deletions(-) create mode 100644 src/utils/t_lexor_clear_utils.c diff --git a/Makefile b/Makefile index d078a30..9b149cc 100644 --- a/Makefile +++ b/Makefile @@ -38,7 +38,7 @@ HEADER = $(wildcard ./includes/*.h) READLINE_DIR = $(shell brew --prefix readline) -READLINE_LIB = -lreadline -lhistory - L $(READLINE_DIR)/lib +READLINE_LIB = -lreadline -lhistory -L $(READLINE_DIR)/lib INCLUDES =-Iincludes -I$(PATHP) -I$(LIBFTP) -I$(READLINE_DIR)/include @@ -77,7 +77,7 @@ $(PATHO)%.o:: $(PATHEX)%.c $(HEADERS) @$(CC) -c $(FLAGS) $(INCLUDES) $< -o $@ $(NAME): $(LIBFT) $(OBJS) $(HEADERS) - @$(CC) $(FLAGS) $(LIBFT) $(OBJS) -o $(NAME) + @$(CC) $(FLAGS) $(LIBFT) $(OBJS) $(READLINE_LIB) -o $(NAME) @echo "Success" #-I$(READLINE_DIR)/include -L$(READLINE_DIR)/lib diff --git a/includes/utils.h b/includes/utils.h index ea16510..d6ac255 100644 --- a/includes/utils.h +++ b/includes/utils.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:36:23 by mgraaf #+# #+# */ -/* Updated: 2022/04/12 13:36:48 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/13 11:09:28 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,7 +34,6 @@ t_lexor *ft_lexornew(char *str, int token); void ft_lexoradd_back(t_lexor **lst, t_lexor *new); void ft_lexordelone(t_lexor **lst, int i); void ft_lexorclear(t_lexor **lst); -t_lexor *ft_lexorlast(t_lexor *lst); //t_heredoc_utils t_heredoc *ft_heredocnew(char *cmd, char *del); diff --git a/libraries/libft/ft_strlen.c b/libraries/libft/ft_strlen.c index c6b4e26..b25441e 100644 --- a/libraries/libft/ft_strlen.c +++ b/libraries/libft/ft_strlen.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2021/12/16 14:24:28 by mgraaf #+# #+# */ -/* Updated: 2021/12/16 14:24:29 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/13 11:02:53 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,10 +17,9 @@ size_t ft_strlen(const char *str) int i; i = 0; - while (*str) - { - str++; + if (!str[i]) + return (i); + while (str[i]) i++; - } return (i); } diff --git a/src/executor/heredoc.c b/src/executor/heredoc.c index 5e5a451..07af3ea 100644 --- a/src/executor/heredoc.c +++ b/src/executor/heredoc.c @@ -6,7 +6,7 @@ /* By: maiadegraaf str) + { + free((*lst)->str); + (*lst)->str = NULL; + } + free(*lst); + *lst = NULL; + return (NULL); +} + +void ft_lexordel_first(t_lexor **lst) +{ + t_lexor *node; + + node = *lst; + *lst = node->next; + ft_lexorclear_one(&node); + if (*lst) + (*lst)->prev = NULL; +} + +void ft_lexordelone(t_lexor **lst, int key) +{ + t_lexor *node; + t_lexor *prev; + t_lexor *start; + + start = *lst; + node = start; + if ((*lst)->i == key) + { + ft_lexordel_first(lst); + return ; + } + while (node && node->i != key) + { + prev = node; + node = node->next; + } + if (node) + prev->next = node->next; + else + prev->next = NULL; + if (prev->next) + prev->next->prev = prev; + ft_lexorclear_one(&node); + *lst = start; +} + +void ft_lexorclear(t_lexor **lst) +{ + t_lexor *tmp; + + if (!*lst) + return ; + while (*lst) + { + tmp = (*lst)->next; + if ((*lst)->str) + free((*lst)->str); + free(*lst); + *lst = tmp; + } + *lst = NULL; +} diff --git a/src/utils/t_lexor_utils.c b/src/utils/t_lexor_utils.c index 367f9fc..8f1a572 100644 --- a/src/utils/t_lexor_utils.c +++ b/src/utils/t_lexor_utils.c @@ -47,90 +47,3 @@ void ft_lexoradd_back(t_lexor **lst, t_lexor *new) tmp->next = new; new->prev = tmp; } - -void ft_lexorclear_one(t_lexor **lst) -{ - if ((*lst)->str) - { - free((*lst)->str); - (*lst)->str = NULL; - } - free(*lst); - *lst = NULL; -} - -void ft_lexordelone(t_lexor **lst, int key) -{ - t_lexor *node; - t_lexor *prev; - t_lexor *start; - - start = *lst; - node = start; - if ((*lst)->i == key) - { - *lst = node->next; - ft_lexorclear_one(&node); - return ; - } - while (node && node->i != key) - { - prev = node; - node = node->next; - } - if (node) - prev->next = node->next; - else - prev->next = NULL; - if (prev->next) - prev->next->prev = prev; - ft_lexorclear_one(&node); - *lst = start; -} - -// void deleteNode(t_lexor **head_ref, int key) -// { -// t_lexor *temp; -// t_lexor *prev; - -// temp = *head_ref; -// if (temp != NULL && temp->i == key) { -// *head_ref = temp->next; -// free(temp); -// return; -// } -// while (temp != NULL && temp->i != key) { -// prev = temp; -// temp = temp->next; -// } -// if (temp == NULL) -// return; -// prev->next = temp->next; -// free(temp); -// } - -void ft_lexorclear(t_lexor **lst) -{ - t_lexor *tmp; - - if (!*lst) - return ; - while (*lst) - { - tmp = (*lst)->next; - if ((*lst)->str) - free((*lst)->str); - free(*lst); - *lst = tmp; - } - *lst = NULL; -} - -// t_lexor *ft_lexorlast(t_lexor *lst) -// { -// if (!lst) -// return (NULL); -// while (lst->next != NULL) -// lst = lst->next; -// return (lst); -// } From 8c7f66e705fc6bd6b7cb8914fa77e11a8027ed47 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 13 Apr 2022 11:20:20 +0200 Subject: [PATCH 128/163] echo fix, + deletes quotes --- includes/minishell.h | 3 ++- src/expander/expander.c | 14 ++++++++++---- src/expander/expanders_utils.c | 9 ++++++++- src/utils/minishell_loop.c | 6 +++--- 4 files changed, 23 insertions(+), 9 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index 3adb338..7799290 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/04/08 16:14:53 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/13 11:04:53 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -42,6 +42,7 @@ char *delete_quotes_value(char *str); void sigint_handler(int sig); void sigquit_handler(int sig); void init_signals(void); +char *delete_quotes(char *str, char c); //builtins int (*builtin_arr(char *str))(t_tools *tools, t_simple_cmds *simple_cmd); diff --git a/src/expander/expander.c b/src/expander/expander.c index e54ab1b..4507ce0 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/07 17:03:10 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/13 11:16:29 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -75,13 +75,16 @@ char **expander(t_tools *tools, char **str) i = 0; tmp = NULL; while (str[i] != NULL) - { - if (str[i][ft_strlen(str[i]) - 1] != '\'' && dollar_sign(str[i]) != 0) + { + if (str[i][ft_strlen(str[i]) - 1] != '\'' && dollar_sign(str[i]) != 0 + && str[i][dollar_sign(str[i])] != '\0') { tmp = detect_dollar_sign(tools, str[i]); free(str[i]); str[i] = tmp; } + str[i] = delete_quotes(str[i], '\"'); + str[i] = delete_quotes(str[i], '\''); i++; } return (str); @@ -92,11 +95,14 @@ char *expander_str(t_tools *tools, char *str) char *tmp; tmp = NULL; - if (str[ft_strlen(str) - 1] != '\'' && dollar_sign(str) != 0) + if (str[ft_strlen(str) - 1] != '\'' && dollar_sign(str) != 0 + && str[dollar_sign(str)] != '\0') { tmp = detect_dollar_sign(tools, str); free(str); str = tmp; } + str = delete_quotes(str, '\"'); + str = delete_quotes(str, '\''); return (str); } diff --git a/src/expander/expanders_utils.c b/src/expander/expanders_utils.c index e9b5068..bfbc431 100644 --- a/src/expander/expanders_utils.c +++ b/src/expander/expanders_utils.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/31 16:08:47 by fpolycar #+# #+# */ -/* Updated: 2022/04/07 17:03:21 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/13 11:16:03 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -46,3 +46,10 @@ int after_dol_lenght(char *str, int j) i++; return (i); } + +char *delete_quotes(char *str, char c) +{ + while (str == ft_strchr(str, c)) + ft_strlcpy(str, str + 1, ft_strlen(str) - 1); + return (str); +} diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 3c6a726..bc9a99a 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,11 +6,11 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/12 15:38:10 by fpolycar ######## odam.nl */ -/* Updated: 2022/04/12 16:24:22 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/13 11:17:37 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ + #include "minishell.h" int minishell_loop(t_tools *tools); @@ -36,7 +36,7 @@ int reset_tools(t_tools *tools) free(tools->pid); tools->pid = NULL; } - // system("leaks minishell"); + system("leaks minishell"); if (tools->end == true) exit (EXIT_SUCCESS); minishell_loop(tools); From 4b0c67220b10b1cc47ed9bba0c0fd3a789979e2f Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Wed, 13 Apr 2022 12:51:58 +0200 Subject: [PATCH 129/163] fix empty readline and added some pretty things --- includes/color.h | 32 ++++++++++++++++++++++++++++++++ includes/minishell.h | 3 ++- src/main.c | 26 +++++++++++++++++++++++++- src/utils/minishell_loop.c | 20 +++++++++----------- 4 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 includes/color.h diff --git a/includes/color.h b/includes/color.h new file mode 100644 index 0000000..42c37ea --- /dev/null +++ b/includes/color.h @@ -0,0 +1,32 @@ +#ifndef _COLOR_H_ +#define _COLOR_H_ + + +# define RESET_COLOR "\033[0m" +# define BLACK "\033[30m" +# define RED "\033[31m" +# define LIGHT_RED "\033[91m" +# define GREEN "\033[32m" +# define LIGHT_GREEN "\033[92m" +# define YELLOW "\033[33m" +# define LIGHT_YELLOW "\033[93m" +# define BLUE "\033[34m" +# define LIGHT_BLUE "\033[94m" +# define MAGENTA "\033[35m" +# define LIGHT_MAGENTA "\033[95m" +# define CYAN "\033[36m" +# define LIGHT_CYAN "\033[96m" +# define WHITE "\033[37m" +# define GREY "\033[90m" +# define LIGHT_GREY "\033[37m" + +# define BLACK_BOLD "\033[1;30m" +# define RED_BOLD "\033[1;31m" +# define GREEN_BOLD "\033[1;32m" +# define YELLOW_BOLD "\033[1;33m" +# define BLUE_BOLD "\033[1;34m" +# define MAGENTA_BOLD "\033[1;35m" +# define CYAN_BOLD "\033[1;36m" +# define WHITE_BOLD "\033[1;37m" + +#endif \ No newline at end of file diff --git a/includes/minishell.h b/includes/minishell.h index 7799290..433ca74 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/04/13 11:04:53 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/13 11:55:41 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -24,6 +24,7 @@ # include "utils.h" # include "error.h" # include "lexor.h" +# include "color.h" # include "builtins.h" # include "executor.h" diff --git a/src/main.c b/src/main.c index 49231f9..efa1c70 100644 --- a/src/main.c +++ b/src/main.c @@ -6,12 +6,35 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/04/12 14:52:11 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/13 12:12:35 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" +void print_welcome(void) +{ + ft_printf( + "\n\n%s\t █▀█ █▀▀ █▀▀   █▀█ █▀█ █▀▀ █▀ █▀▀ █▄░█ ▀█▀ █▀ \n", + LIGHT_MAGENTA); + ft_printf( + "\t █▀▀ █▀░ █▄▄   █▀▀ █▀▄ ██▄ ▄█ ██▄ █░▀█ ░█░ ▄█ \n\n"); + ft_printf( + "%s███╗░░░███╗██╗███╗░░██╗██╗░██████╗██╗░░██╗███████╗██╗░░░░░██╗░░░░░\n", + MAGENTA); + ft_printf( + "████╗░████║██║████╗░██║██║██╔════╝██║░░██║██╔════╝██║░░░░░██║░░░░░\n"); + ft_printf( + "██╔████╔██║██║██╔██╗██║██║╚█████╗░███████║█████╗░░██║░░░░░██║░░░░░\n"); + ft_printf( + "██║╚██╔╝██║██║██║╚████║██║░╚═══██╗██╔══██║██╔══╝░░██║░░░░░██║░░░░░\n"); + ft_printf( + "██║░╚═╝░██║██║██║░╚███║██║██████╔╝██║░░██║███████╗███████╗███████╗\n"); + ft_printf( + "╚═╝░░░░░╚═╝╚═╝╚═╝░░╚══╝╚═╝╚═════╝░╚═╝░░╚═╝╚══════╝╚══════╝╚══════╝"); + ft_printf("\n\n%s", RESET_COLOR); +} + int main(int argc, char **argv, char **envp) { t_tools tools; @@ -27,6 +50,7 @@ int main(int argc, char **argv, char **envp) init_signals(); parse_envp(&tools); implement_tools(&tools); + print_welcome(); minishell_loop(&tools); return (0); } diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index bc9a99a..d6198f0 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/13 11:17:37 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/13 12:51:10 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -36,7 +36,7 @@ int reset_tools(t_tools *tools) free(tools->pid); tools->pid = NULL; } - system("leaks minishell"); + // system("leaks minishell"); if (tools->end == true) exit (EXIT_SUCCESS); minishell_loop(tools); @@ -45,17 +45,15 @@ int reset_tools(t_tools *tools) int minishell_loop(t_tools *tools) { - tools->args = readline("minishell$ "); - if (!tools->args) - { - ft_putendl_fd("exit", STDOUT_FILENO); - exit(EXIT_SUCCESS); - } + printf("%sminishell%s$%s", CYAN_BOLD, BLUE, RESET_COLOR); + tools->args = readline(" "); + if (tools->args[0] == '\0') + return (reset_tools(tools)); add_history(tools->args); if (!count_quotes(tools->args)) - ft_error(2, tools); + return (ft_error(2, tools)); if (!token_reader(tools)) - ft_error(1, tools); + return (ft_error(1, tools)); parser(tools); if (tools->pipes == 0) single_cmd(tools->simple_cmds, tools); @@ -63,7 +61,7 @@ int minishell_loop(t_tools *tools) { tools->pid = ft_calloc(sizeof(int), tools->pipes + 2); if (!tools->pid) - ft_error(1, tools); + return (ft_error(1, tools)); executor(tools); } reset_tools(tools); From ec381a3013aa77375703922de956dcf9783bf346 Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Wed, 13 Apr 2022 13:24:03 +0200 Subject: [PATCH 130/163] Made some more pretty changes --- includes/error.h | 4 ++-- includes/parser.h | 3 +-- src/error/error_handeling.c | 5 +++-- src/executor/heredoc.c | 8 +++++--- src/signals.c | 3 ++- src/utils/minishell_loop.c | 12 +++--------- 6 files changed, 16 insertions(+), 19 deletions(-) diff --git a/includes/error.h b/includes/error.h index e4fe11b..9012624 100644 --- a/includes/error.h +++ b/includes/error.h @@ -6,7 +6,7 @@ /* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/04/12 11:35:25 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/13 12:53:31 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -59,7 +59,6 @@ typedef struct s_tools char *pwd; char *old_pwd; int pipes; - bool end; int *pid; bool reset; } t_tools; diff --git a/src/error/error_handeling.c b/src/error/error_handeling.c index 2cfd687..b3dc6da 100755 --- a/src/error/error_handeling.c +++ b/src/error/error_handeling.c @@ -6,7 +6,7 @@ /* By: maiadegraaf del); fd = open("build/tmp_heredoc_file.txt", O_CREAT | O_RDWR | O_TRUNC, 0644); - ft_putstr_fd("heredoc> ", STDOUT_FILENO); + ft_printf("%s>", BLUE_BOLD); + ft_printf(" %s", RESET_COLOR); line = get_next_line(STDIN_FILENO); while (ft_strncmp(heredoc->del, line, del_len)) { @@ -40,7 +41,8 @@ int create_heredoc(t_heredoc *heredoc, bool quotes, t_tools *tools) line = send_expander(tools, line); write(fd, line, ft_strlen(line)); free(line); - ft_putstr_fd("heredoc> ", STDOUT_FILENO); + ft_printf("%s>", BLUE_BOLD); + ft_printf(" %s", RESET_COLOR); line = get_next_line(STDIN_FILENO); } close(fd); diff --git a/src/signals.c b/src/signals.c index 770dbc8..36c42e4 100644 --- a/src/signals.c +++ b/src/signals.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/04/08 14:35:54 by fpolycar #+# #+# */ -/* Updated: 2022/04/12 14:52:20 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/13 13:04:58 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ void sigint_handler(int sig) ft_putstr_fd("\n", STDERR_FILENO); rl_on_new_line(); rl_replace_line("", 0); + printf("%sminishell%s$%s", CYAN_BOLD, BLUE, RESET_COLOR); rl_redisplay(); (void) sig; } diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index d6198f0..e81aea1 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/13 12:51:10 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/13 13:04:51 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -19,7 +19,6 @@ int implement_tools(t_tools *tools) { tools->simple_cmds = NULL; tools->lexor_list = NULL; - tools->end = false; tools->reset = false; tools->pid = NULL; return (1); @@ -29,16 +28,11 @@ int reset_tools(t_tools *tools) { ft_simple_cmdsclear(&tools->simple_cmds); free(tools->args); - implement_tools(tools); - tools->reset = true; if (tools->pid) - { free(tools->pid); - tools->pid = NULL; - } + implement_tools(tools); + tools->reset = true; // system("leaks minishell"); - if (tools->end == true) - exit (EXIT_SUCCESS); minishell_loop(tools); return (1); } From a7e4b99050ef80c06eed6e5d49e96f8a783511db Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 13 Apr 2022 13:48:33 +0200 Subject: [PATCH 131/163] delete quotes --- src/expander/expander.c | 2 +- src/expander/expanders_utils.c | 20 +++++++++++++++++--- src/utils/minishell_loop.c | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/expander/expander.c b/src/expander/expander.c index 4507ce0..4f30d01 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/13 11:16:29 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/13 13:47:21 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/expander/expanders_utils.c b/src/expander/expanders_utils.c index bfbc431..5e69dd1 100644 --- a/src/expander/expanders_utils.c +++ b/src/expander/expanders_utils.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/31 16:08:47 by fpolycar #+# #+# */ -/* Updated: 2022/04/13 11:16:03 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/13 13:43:18 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -49,7 +49,21 @@ int after_dol_lenght(char *str, int j) char *delete_quotes(char *str, char c) { - while (str == ft_strchr(str, c)) - ft_strlcpy(str, str + 1, ft_strlen(str) - 1); + int i; + int j; + + i = 0; + j = 0; + while (str[i]) + { + if (str[i] == c && str[i - 1] != '\\') + { + j = 0; + while (str[i + j] == c) + j++; + ft_strlcpy(&str[i], &str[i + j], strlen(str) - i); + } + i++; + } return (str); } diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index bc9a99a..b38ea88 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/13 11:17:37 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/13 13:46:56 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ From cf35a7f2ec746c3b7f9630a8be7c192f6965927d Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Wed, 13 Apr 2022 14:01:17 +0200 Subject: [PATCH 132/163] Spruced up the welcome message --- src/main.c | 69 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 19 deletions(-) diff --git a/src/main.c b/src/main.c index efa1c70..03dac47 100644 --- a/src/main.c +++ b/src/main.c @@ -6,33 +6,64 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/04/13 12:12:35 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/13 13:59:47 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -void print_welcome(void) +void wrap_line(char *line, char *color) { - ft_printf( - "\n\n%s\t █▀█ █▀▀ █▀▀   █▀█ █▀█ █▀▀ █▀ █▀▀ █▄░█ ▀█▀ █▀ \n", + printf("\n%s║\t", LIGHT_CYAN); + printf("%s%s", color, line); + printf("%s\t ║", LIGHT_CYAN); +} + +void print_presents(void) +{ + printf("\n%s║\t\t\t\t\t\t\t\t\t\t ║", LIGHT_CYAN); + wrap_line( + "\t █▀█ █▀▀ █▀▀   █▀█ █▀█ █▀▀ █▀ █▀▀ █▄░█ ▀█▀ █▀\t\t", + LIGHT_MAGENTA); + wrap_line( + "\t █▀▀ █▀░ █▄▄   █▀▀ █▀▄ ██▄ ▄█ ██▄ █░▀█ ░█░ ▄█\t\t", LIGHT_MAGENTA); - ft_printf( - "\t █▀▀ █▀░ █▄▄   █▀▀ █▀▄ ██▄ ▄█ ██▄ █░▀█ ░█░ ▄█ \n\n"); - ft_printf( - "%s███╗░░░███╗██╗███╗░░██╗██╗░██████╗██╗░░██╗███████╗██╗░░░░░██╗░░░░░\n", + printf("\n%s║\t\t\t\t\t\t\t\t\t\t ║", LIGHT_CYAN); +} + +void print_minishell(void) +{ + wrap_line( + "███╗░░░███╗██╗███╗░░██╗██╗░██████╗██╗░░██╗███████╗██╗░░░░░██╗░░░░░", + MAGENTA); + wrap_line( + "████╗░████║██║████╗░██║██║██╔════╝██║░░██║██╔════╝██║░░░░░██║░░░░░", MAGENTA); - ft_printf( - "████╗░████║██║████╗░██║██║██╔════╝██║░░██║██╔════╝██║░░░░░██║░░░░░\n"); - ft_printf( - "██╔████╔██║██║██╔██╗██║██║╚█████╗░███████║█████╗░░██║░░░░░██║░░░░░\n"); - ft_printf( - "██║╚██╔╝██║██║██║╚████║██║░╚═══██╗██╔══██║██╔══╝░░██║░░░░░██║░░░░░\n"); - ft_printf( - "██║░╚═╝░██║██║██║░╚███║██║██████╔╝██║░░██║███████╗███████╗███████╗\n"); - ft_printf( - "╚═╝░░░░░╚═╝╚═╝╚═╝░░╚══╝╚═╝╚═════╝░╚═╝░░╚═╝╚══════╝╚══════╝╚══════╝"); - ft_printf("\n\n%s", RESET_COLOR); + wrap_line( + "██╔████╔██║██║██╔██╗██║██║╚█████╗░███████║█████╗░░██║░░░░░██║░░░░░", + MAGENTA); + wrap_line( + "██║╚██╔╝██║██║██║╚████║██║░╚═══██╗██╔══██║██╔══╝░░██║░░░░░██║░░░░░", + MAGENTA); + wrap_line( + "██║░╚═╝░██║██║██║░╚███║██║██████╔╝██║░░██║███████╗███████╗███████╗", + MAGENTA); + wrap_line( + "╚═╝░░░░░╚═╝╚═╝╚═╝░░╚══╝╚═╝╚═════╝░╚═╝░░╚═╝╚══════╝╚══════╝╚══════╝", + MAGENTA); +} + +void print_welcome(void) +{ + printf("\n%s╔═════════════════════════════════════════════", + LIGHT_CYAN); + printf("═══════════════════════════════════╗"); + print_presents(); + print_minishell(); + printf("\n%s║\t\t\t\t\t\t\t\t\t\t ║\n", LIGHT_CYAN); + printf("╚═══════════════════════════════════════"); + printf("═════════════════════════════════════════╝\n"); + printf("\n\n%s", RESET_COLOR); } int main(int argc, char **argv, char **envp) From 412e56edadae385340452ae6e3cfe0a368f7054c Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 13 Apr 2022 14:17:15 +0200 Subject: [PATCH 133/163] hotfix: expander single quote --- src/expander/expander.c | 6 +++--- src/expander/expanders_utils.c | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/expander/expander.c b/src/expander/expander.c index 4f30d01..cdf1703 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/13 13:47:21 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/13 14:16:23 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -76,7 +76,7 @@ char **expander(t_tools *tools, char **str) tmp = NULL; while (str[i] != NULL) { - if (str[i][ft_strlen(str[i]) - 1] != '\'' && dollar_sign(str[i]) != 0 + if (str[i][dollar_sign(str[i]) - 2] != '\'' && dollar_sign(str[i]) != 0 && str[i][dollar_sign(str[i])] != '\0') { tmp = detect_dollar_sign(tools, str[i]); @@ -95,7 +95,7 @@ char *expander_str(t_tools *tools, char *str) char *tmp; tmp = NULL; - if (str[ft_strlen(str) - 1] != '\'' && dollar_sign(str) != 0 + if (str[dollar_sign(str) - 2] != '\'' && dollar_sign(str) != 0 && str[dollar_sign(str)] != '\0') { tmp = detect_dollar_sign(tools, str); diff --git a/src/expander/expanders_utils.c b/src/expander/expanders_utils.c index 5e69dd1..04fb337 100644 --- a/src/expander/expanders_utils.c +++ b/src/expander/expanders_utils.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/31 16:08:47 by fpolycar #+# #+# */ -/* Updated: 2022/04/13 13:43:18 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/13 14:14:55 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -42,7 +42,7 @@ int after_dol_lenght(char *str, int j) i = j + 1; while (str[i] != '\0' && str[i] != '$' && str[i] != ' ' - && str[i] != '\"') + && str[i] != '\"' && str[i] != '\'') i++; return (i); } From 6771acbac100e98a535e8ab145ee582c59b9b988 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 13 Apr 2022 15:58:05 +0200 Subject: [PATCH 134/163] echo ds hanle --- src/expander/expander.c | 20 +++++++++++++++++++- src/expander/expanders_utils.c | 2 +- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/expander/expander.c b/src/expander/expander.c index cdf1703..412852f 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/13 14:16:23 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/13 15:57:06 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -42,6 +42,23 @@ int loop_if_dollar_sign(t_tools *tools, char *str, char **tmp, int j) return (ret); } +int handle_digit_after_dollar(int j, char *str) +{ + int i; + + i = j; + if (str[j] == '$') + { + if (ft_isdigit(str[j + 1]) == 1) + { + while (ft_isdigit(str[j + 1]) == 1) + j++; + j++; + } + } + return (j - i); +} + char *detect_dollar_sign(t_tools *tools, char *str) { int j; @@ -53,6 +70,7 @@ char *detect_dollar_sign(t_tools *tools, char *str) tmp = ft_strdup("\0"); while (str[j]) { + j += handle_digit_after_dollar(j, str); if (str[j] == '$') j += loop_if_dollar_sign(tools, str, &tmp, j); else diff --git a/src/expander/expanders_utils.c b/src/expander/expanders_utils.c index 04fb337..2ef2fe0 100644 --- a/src/expander/expanders_utils.c +++ b/src/expander/expanders_utils.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/31 16:08:47 by fpolycar #+# #+# */ -/* Updated: 2022/04/13 14:14:55 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/13 15:35:02 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ From 49a2f5a4eaeab88883d14bc792f1b73bb78c751d Mon Sep 17 00:00:00 2001 From: maiadegraaf <68693691+maiadegraaf@users.noreply.github.com> Date: Thu, 14 Apr 2022 10:53:44 +0200 Subject: [PATCH 135/163] Heredoc error :( --- file1 | 21 --------------------- includes/executor.h | 3 ++- includes/parser.h | 3 ++- includes/utils.h | 4 ++-- src/executor/executor.c | 12 ++++++------ src/executor/handle_cmd.c | 7 +++++-- src/executor/heredoc.c | 23 ++++++++++++----------- src/main.c | 2 +- src/signals.c | 3 +-- src/utils/minishell_loop.c | 29 +++++++++++++++++------------ src/utils/t_heredoc_utils.c | 18 ++++++++++-------- 11 files changed, 58 insertions(+), 67 deletions(-) delete mode 100644 file1 diff --git a/file1 b/file1 deleted file mode 100644 index f90c859..0000000 --- a/file1 +++ /dev/null @@ -1,21 +0,0 @@ -total 208 -drwxr-xr-x 20 fpolycar piscine 680 Apr 12 15:53 . -drwxr-xr-x 8 fpolycar piscine 272 Mar 23 14:00 .. -drwxr-xr-x 16 fpolycar piscine 544 Apr 12 15:39 .git --rw-r--r-- 1 fpolycar piscine 37 Mar 23 11:08 .gitignore --rw-r--r-- 1 fpolycar piscine 85 Mar 23 11:08 .gitmodules -drwxr-xr-x 5 fpolycar piscine 170 Apr 12 14:51 .vscode --rw-r--r-- 1 fpolycar piscine 2309 Apr 12 14:57 Makefile -drwxr-xr-x 18 fpolycar piscine 612 Apr 8 10:17 Unity --rwxr-xr-x 1 fpolycar piscine 37570 Apr 12 14:51 a.out -drwxr-xr-x 3 fpolycar piscine 102 Apr 12 14:51 a.out.dSYM -drwxr-xr-x 4 fpolycar piscine 136 Mar 23 13:59 build --rw-r--r-- 1 fpolycar piscine 0 Apr 12 15:54 file1 --rw-r--r-- 1 fpolycar piscine 0 Apr 12 14:51 file2 -drwxr-xr-x 9 fpolycar piscine 306 Apr 12 14:51 includes -drwxr-xr-x 3 fpolycar piscine 102 Mar 23 11:08 libraries --rwxr-xr-x 1 fpolycar piscine 51420 Apr 12 15:37 minishell -drwxr-xr-x 3 fpolycar piscine 102 Mar 23 11:08 minishell.dSYM -drwxr-xr-x 12 fpolycar piscine 408 Apr 12 14:51 src -drwxr-xr-x 11 fpolycar piscine 374 Apr 12 10:48 test -drwxr-xr-x 3 fpolycar piscine 102 Mar 23 11:08 utilities diff --git a/includes/executor.h b/includes/executor.h index be113be..e28f3bf 100644 --- a/includes/executor.h +++ b/includes/executor.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:17:39 by mgraaf #+# #+# */ -/* Updated: 2022/04/12 17:32:42 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/13 15:05:46 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ int handle_redirections(t_simple_cmds *cmd, t_tools *tools); // executor int executor(t_tools *tools); t_simple_cmds *call_expander(t_tools *tools, t_simple_cmds *cmd); +int pipe_wait(int *pid, int amount); // handle_cmd int find_cmd(t_simple_cmds *cmd, t_tools *tools); diff --git a/includes/parser.h b/includes/parser.h index 4e24cda..2bcd71f 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/04/13 12:53:31 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/13 14:54:02 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -69,6 +69,7 @@ typedef struct s_simple_cmds int (*builtin)(t_tools *, struct s_simple_cmds *); int num_redirections; t_heredoc *heredoc; + int *heredoc_pid; t_lexor *redirections; struct s_simple_cmds *next; struct s_simple_cmds *prev; diff --git a/includes/utils.h b/includes/utils.h index d6ac255..b291f7a 100644 --- a/includes/utils.h +++ b/includes/utils.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:36:23 by mgraaf #+# #+# */ -/* Updated: 2022/04/13 11:09:28 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/13 15:03:32 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -38,7 +38,7 @@ void ft_lexorclear(t_lexor **lst); //t_heredoc_utils t_heredoc *ft_heredocnew(char *cmd, char *del); void ft_heredocadd_back(t_heredoc **lst, t_heredoc *new); -void ft_heredoc_rm_first(t_heredoc **lst); +int ft_heredocsize(t_heredoc *lst); void ft_heredocclear(t_heredoc **lst); t_heredoc *ft_heredocfirst(t_heredoc *map); diff --git a/src/executor/executor.c b/src/executor/executor.c index cc8e7c6..5b1545f 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/04/12 15:06:51 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/13 16:02:19 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -28,18 +28,18 @@ t_simple_cmds *call_expander(t_tools *tools, t_simple_cmds *cmd) return (cmd); } -int pipe_wait(t_tools *tools) +int pipe_wait(int *pid, int amount) { int i; int status; i = 0; - while (i < tools->pipes) + while (i < amount) { - waitpid(tools->pid[i], &status, 0); + waitpid(pid[i], &status, 0); i++; } - waitpid(tools->pid[i], &status, 0); + waitpid(pid[i], &status, 0); return (EXIT_SUCCESS); } @@ -82,7 +82,7 @@ int executor(t_tools *tools) else break ; } - pipe_wait(tools); + pipe_wait(tools->pid, tools->pipes); tools->simple_cmds = ft_simple_cmdsfirst(tools->simple_cmds); return (0); } diff --git a/src/executor/handle_cmd.c b/src/executor/handle_cmd.c index 290fd0e..d86b98d 100644 --- a/src/executor/handle_cmd.c +++ b/src/executor/handle_cmd.c @@ -6,7 +6,7 @@ /* By: maiadegraaf str[0], STDERR_FILENO); + ft_putstr_fd(": command not found\n", STDERR_FILENO); exit(EXIT_FAILURE); return (1); } @@ -41,7 +44,7 @@ void handle_cmd(t_simple_cmds *cmd, t_tools *tools) cmd->builtin(tools, cmd); exit(EXIT_SUCCESS); } - else + else if (cmd->str[0][0] != '\0') find_cmd(cmd, tools); } diff --git a/src/executor/heredoc.c b/src/executor/heredoc.c index 506da0c..7cc8005 100644 --- a/src/executor/heredoc.c +++ b/src/executor/heredoc.c @@ -6,7 +6,7 @@ /* By: maiadegraaf del); fd = open("build/tmp_heredoc_file.txt", O_CREAT | O_RDWR | O_TRUNC, 0644); - ft_printf("%s>", BLUE_BOLD); - ft_printf(" %s", RESET_COLOR); + ft_putstr_fd("\033[1;34m> \033[0m", STDOUT_FILENO); line = get_next_line(STDIN_FILENO); while (ft_strncmp(heredoc->del, line, del_len)) { @@ -41,8 +40,7 @@ int create_heredoc(t_heredoc *heredoc, bool quotes, t_tools *tools) line = send_expander(tools, line); write(fd, line, ft_strlen(line)); free(line); - ft_printf("%s>", BLUE_BOLD); - ft_printf(" %s", RESET_COLOR); + ft_putstr_fd("\033[1;34m> \033[0m", STDOUT_FILENO); line = get_next_line(STDIN_FILENO); } close(fd); @@ -91,21 +89,24 @@ int ft_heredoc(t_tools *tools, t_heredoc *heredoc) int send_heredoc(t_tools *tools, t_simple_cmds *cmd) { - int pid; - int status; t_heredoc *start; + int i; + int num_heredocs; + i = 0; start = cmd->heredoc; + num_heredocs = ft_heredocsize(cmd->heredoc); + cmd->heredoc_pid = ft_calloc(num_heredocs, sizeof(int)); while (cmd->heredoc) { - pid = fork(); - if (pid < 0) + cmd->heredoc_pid[i] = fork(); + if (cmd->heredoc_pid[i] < 0) ft_error(5, tools); - if (pid == 0) + if (cmd->heredoc_pid[i] == 0) ft_heredoc(tools, cmd->heredoc); cmd->heredoc = cmd->heredoc->next; - waitpid(pid, &status, 0); } + pipe_wait(cmd->heredoc_pid, num_heredocs); cmd->heredoc = start; return (EXIT_SUCCESS); } diff --git a/src/main.c b/src/main.c index 03dac47..1c0f1b0 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/04/13 13:59:47 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/13 14:39:04 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/signals.c b/src/signals.c index 36c42e4..de64d39 100644 --- a/src/signals.c +++ b/src/signals.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/04/08 14:35:54 by fpolycar #+# #+# */ -/* Updated: 2022/04/13 13:04:58 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/13 14:41:54 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,7 +17,6 @@ void sigint_handler(int sig) ft_putstr_fd("\n", STDERR_FILENO); rl_on_new_line(); rl_replace_line("", 0); - printf("%sminishell%s$%s", CYAN_BOLD, BLUE, RESET_COLOR); rl_redisplay(); (void) sig; } diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index ab11278..ff33b6a 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/13 13:55:30 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/13 15:47:55 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -37,10 +37,23 @@ int reset_tools(t_tools *tools) return (1); } +int prepare_executor(t_tools *tools) +{ + if (tools->pipes == 0) + single_cmd(tools->simple_cmds, tools); + else + { + tools->pid = ft_calloc(sizeof(int), tools->pipes + 2); + if (!tools->pid) + return (ft_error(1, tools)); + executor(tools); + } + return (EXIT_SUCCESS); +} + int minishell_loop(t_tools *tools) { - printf("%sminishell%s$%s", CYAN_BOLD, BLUE, RESET_COLOR); - tools->args = readline(" "); + tools->args = readline("\033[1;36mminishell\033[34m$ \033[0m"); if (!tools->args) { ft_putendl_fd("exit", STDOUT_FILENO); @@ -54,15 +67,7 @@ int minishell_loop(t_tools *tools) if (!token_reader(tools)) return (ft_error(1, tools)); parser(tools); - if (tools->pipes == 0) - single_cmd(tools->simple_cmds, tools); - else - { - tools->pid = ft_calloc(sizeof(int), tools->pipes + 2); - if (!tools->pid) - return (ft_error(1, tools)); - executor(tools); - } + prepare_executor(tools); reset_tools(tools); return (1); } diff --git a/src/utils/t_heredoc_utils.c b/src/utils/t_heredoc_utils.c index f87e0aa..44669a7 100644 --- a/src/utils/t_heredoc_utils.c +++ b/src/utils/t_heredoc_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/04/12 11:34:17 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/13 15:07:55 by maiadegraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -46,15 +46,17 @@ void ft_heredocadd_back(t_heredoc **lst, t_heredoc *new) new->prev = tmp; } -void ft_heredoc_rm_first(t_heredoc **lst) +int ft_heredocsize(t_heredoc *lst) { - t_heredoc *tmp; + int i; - if (!*lst) - return ; - tmp = (*lst)->next; - free(*lst); - *lst = tmp; + i = 0; + while (lst != NULL) + { + lst = lst->next; + i++; + } + return (i); } void ft_heredocclear(t_heredoc **lst) From 790a128b9cec6cd33b4278280d6978572fe1c65b Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Thu, 14 Apr 2022 10:55:37 +0200 Subject: [PATCH 136/163] sd --- src/utils/minishell_loop.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index ab11278..35fe154 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,11 +6,10 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/13 13:55:30 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/14 10:55:04 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ - #include "minishell.h" int minishell_loop(t_tools *tools); From 3fcdff1b3e01004b7ab6fd812076c307eb8642a0 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Thu, 14 Apr 2022 13:37:37 +0200 Subject: [PATCH 137/163] heredoc works with two heredocs but pipe doesn't. --- includes/parser.h | 5 ++-- includes/utils.h | 4 +-- src/executor/check_redirections.c | 6 +++- src/executor/executor.c | 18 ++++++++++-- src/executor/handle_cmd.c | 9 +++++- src/executor/heredoc.c | 46 ++++--------------------------- src/parser/handle_redirections.c | 11 ++------ src/parser/parser.c | 4 ++- src/utils/minishell_loop.c | 3 +- src/utils/t_heredoc_utils.c | 7 ++--- 10 files changed, 47 insertions(+), 66 deletions(-) diff --git a/includes/parser.h b/includes/parser.h index 2bcd71f..de86528 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/04/13 14:54:02 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/14 11:44:17 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -25,7 +25,6 @@ typedef enum s_tokens typedef struct s_heredoc { - char *cmd; char *del; struct s_heredoc *next; struct s_heredoc *prev; @@ -60,6 +59,7 @@ typedef struct s_tools char *old_pwd; int pipes; int *pid; + bool heredoc; bool reset; } t_tools; @@ -69,7 +69,6 @@ typedef struct s_simple_cmds int (*builtin)(t_tools *, struct s_simple_cmds *); int num_redirections; t_heredoc *heredoc; - int *heredoc_pid; t_lexor *redirections; struct s_simple_cmds *next; struct s_simple_cmds *prev; diff --git a/includes/utils.h b/includes/utils.h index b291f7a..1dfcc16 100644 --- a/includes/utils.h +++ b/includes/utils.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:36:23 by mgraaf #+# #+# */ -/* Updated: 2022/04/13 15:03:32 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/14 11:31:53 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -36,7 +36,7 @@ void ft_lexordelone(t_lexor **lst, int i); void ft_lexorclear(t_lexor **lst); //t_heredoc_utils -t_heredoc *ft_heredocnew(char *cmd, char *del); +t_heredoc *ft_heredocnew(char *del); void ft_heredocadd_back(t_heredoc **lst, t_heredoc *new); int ft_heredocsize(t_heredoc *lst); void ft_heredocclear(t_heredoc **lst); diff --git a/src/executor/check_redirections.c b/src/executor/check_redirections.c index 799c310..ce20896 100644 --- a/src/executor/check_redirections.c +++ b/src/executor/check_redirections.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/25 11:39:57 by mgraaf #+# #+# */ -/* Updated: 2022/04/12 14:10:31 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/14 11:25:17 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -37,6 +37,8 @@ int check_outfile(t_lexor *redirections) if (redirections->token == GREAT || redirections->token == GREAT_GREAT) { + if (fd > 0) + close (fd); fd = check_append_outfile(redirections); if (fd < 0) return (-1); @@ -58,6 +60,8 @@ int check_infile(t_lexor *redirections) { if (redirections->token == LESS) { + if (fd > 0) + close (fd); fd = open(redirections->str, O_RDONLY); if (fd < 0) return (-1); diff --git a/src/executor/executor.c b/src/executor/executor.c index 5b1545f..fd49c7f 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/04/13 16:02:19 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/14 12:08:35 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -61,6 +61,20 @@ int ft_fork(t_tools *tools, int end[2], int fd_in, t_simple_cmds *cmd) return (EXIT_SUCCESS); } +int check_fd_heredoc(t_tools *tools, int end[2]) +{ + int fd_in; + + if (tools->heredoc) + { + close(end[0]); + fd_in = open("build/tmp_heredoc_file.txt", O_RDONLY); + } + else + fd_in = end[0]; + return (fd_in); +} + int executor(t_tools *tools) { int end[2]; @@ -76,7 +90,7 @@ int executor(t_tools *tools) close(end[1]); if (tools->simple_cmds->prev) close(fd_in); - fd_in = end[0]; + fd_in = check_fd_heredoc(tools, end); if (tools->simple_cmds->next) tools->simple_cmds = tools->simple_cmds->next; else diff --git a/src/executor/handle_cmd.c b/src/executor/handle_cmd.c index d86b98d..a5f462a 100644 --- a/src/executor/handle_cmd.c +++ b/src/executor/handle_cmd.c @@ -6,7 +6,7 @@ /* By: maiadegraaf heredoc) + { send_heredoc(tools, cmd); + fd = open("build/tmp_heredoc_file.txt", O_RDONLY); + dup2(fd, STDIN_FILENO); + close(fd); + } if (cmd->redirections) handle_redirections(cmd, tools); if (cmd->builtin) diff --git a/src/executor/heredoc.c b/src/executor/heredoc.c index 7cc8005..3fe1295 100644 --- a/src/executor/heredoc.c +++ b/src/executor/heredoc.c @@ -6,7 +6,7 @@ /* By: maiadegraaf del); fd = open("build/tmp_heredoc_file.txt", O_CREAT | O_RDWR | O_TRUNC, 0644); - ft_putstr_fd("\033[1;34m> \033[0m", STDOUT_FILENO); + ft_putstr_fd("\033[1;34m> \033[0m", STDERR_FILENO); line = get_next_line(STDIN_FILENO); while (ft_strncmp(heredoc->del, line, del_len)) { @@ -40,38 +40,13 @@ int create_heredoc(t_heredoc *heredoc, bool quotes, t_tools *tools) line = send_expander(tools, line); write(fd, line, ft_strlen(line)); free(line); - ft_putstr_fd("\033[1;34m> \033[0m", STDOUT_FILENO); + ft_putstr_fd("\033[1;34m> \033[0m", STDERR_FILENO); line = get_next_line(STDIN_FILENO); } close(fd); return (EXIT_SUCCESS); } -int read_heredoc(t_heredoc *heredoc, t_tools *tools) -{ - int fd; - char **cmds; - int i; - char *mycmd; - - cmds = ft_split(heredoc->cmd, ' '); - cmds = expander(tools, cmds); - fd = open("build/tmp_heredoc_file.txt", O_RDONLY); - if (dup2(fd, STDIN_FILENO) < 0) - ft_error(4, tools); - i = 0; - while (tools->paths[i]) - { - mycmd = ft_strjoin(tools->paths[i], cmds[0]); - if (!access(mycmd, F_OK)) - execve(mycmd, cmds, tools->envp); - free(mycmd); - i++; - } - close(fd); - exit(EXIT_FAILURE); -} - int ft_heredoc(t_tools *tools, t_heredoc *heredoc) { bool quotes; @@ -82,31 +57,20 @@ int ft_heredoc(t_tools *tools, t_heredoc *heredoc) else quotes = false; create_heredoc(heredoc, quotes, tools); - if (heredoc->cmd) - read_heredoc(heredoc, tools); + tools->heredoc = true; return (EXIT_SUCCESS); } int send_heredoc(t_tools *tools, t_simple_cmds *cmd) { t_heredoc *start; - int i; - int num_heredocs; - i = 0; start = cmd->heredoc; - num_heredocs = ft_heredocsize(cmd->heredoc); - cmd->heredoc_pid = ft_calloc(num_heredocs, sizeof(int)); while (cmd->heredoc) { - cmd->heredoc_pid[i] = fork(); - if (cmd->heredoc_pid[i] < 0) - ft_error(5, tools); - if (cmd->heredoc_pid[i] == 0) - ft_heredoc(tools, cmd->heredoc); + ft_heredoc(tools, cmd->heredoc); cmd->heredoc = cmd->heredoc->next; } - pipe_wait(cmd->heredoc_pid, num_heredocs); cmd->heredoc = start; return (EXIT_SUCCESS); } diff --git a/src/parser/handle_redirections.c b/src/parser/handle_redirections.c index a711bfa..9b13a44 100644 --- a/src/parser/handle_redirections.c +++ b/src/parser/handle_redirections.c @@ -6,7 +6,7 @@ /* By: maiadegraaf prev != NULL && tmp->prev->str && ft_strlen(tmp->prev->str) > 0) - { - cmd = join_heredoc(tmp->prev->str, cmd); - ft_lexordelone(&parser_tools->lexor_list, tmp->prev->i); - } - node = ft_heredocnew(cmd, ft_strdup(tmp->next->str)); + node = ft_heredocnew(ft_strdup(tmp->next->str)); if (!node) parser_error(1, parser_tools->tools, parser_tools->lexor_list); ft_heredocadd_back(&parser_tools->heredoc, node); diff --git a/src/parser/parser.c b/src/parser/parser.c index 4d0b180..7f59213 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/04/12 11:37:07 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/14 12:02:06 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -53,6 +53,8 @@ int *parser(t_tools *tools) { if (tools->lexor_list && tools->lexor_list->token == PIPE) ft_lexordelone(&tools->lexor_list, tools->lexor_list->i); + // if (!tools->lexor_list) + // parser_error(0, tools, tools->lexor_list); parser_tools = init_parser_tools(tools->lexor_list, tools); node = initialize_cmd(&parser_tools); if (!node) diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index ff33b6a..743f965 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/13 15:47:55 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/14 11:48:07 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,6 +21,7 @@ int implement_tools(t_tools *tools) tools->lexor_list = NULL; tools->reset = false; tools->pid = NULL; + tools->heredoc = false; return (1); } diff --git a/src/utils/t_heredoc_utils.c b/src/utils/t_heredoc_utils.c index 44669a7..a73f354 100644 --- a/src/utils/t_heredoc_utils.c +++ b/src/utils/t_heredoc_utils.c @@ -6,20 +6,19 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/04/13 15:07:55 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/14 11:31:36 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "utils.h" -t_heredoc *ft_heredocnew(char *cmd, char *del) +t_heredoc *ft_heredocnew(char *del) { t_heredoc *new_element; new_element = (t_heredoc *)malloc(sizeof(t_heredoc)); if (!new_element) return (0); - new_element->cmd = cmd; new_element->del = del; new_element->next = NULL; new_element->prev = NULL; @@ -68,8 +67,6 @@ void ft_heredocclear(t_heredoc **lst) while (*lst) { tmp = (*lst)->next; - if ((*lst)->cmd) - free((*lst)->cmd); free((*lst)->del); free(*lst); *lst = tmp; From d002c631a26132519be36f1c1710dd626f0ab731 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Thu, 14 Apr 2022 13:42:46 +0200 Subject: [PATCH 138/163] space fix --- src/builtins/mini_export.c | 11 ++++++++++- src/expander/expander.c | 3 ++- src/utils/minishell_loop.c | 3 ++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/builtins/mini_export.c b/src/builtins/mini_export.c index 0a0e321..f72b96d 100644 --- a/src/builtins/mini_export.c +++ b/src/builtins/mini_export.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:07:21 by fpolycar #+# #+# */ -/* Updated: 2022/04/01 16:01:31 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/14 11:45:26 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -48,6 +48,15 @@ int check_parameter(char *str) ft_putendl_fd("export: bad assignment", STDERR_FILENO); return (2); } + while (str[i] != '=') + { + if (str[i] == '|') + { + ft_putendl_fd("export: not valid in this context", STDERR_FILENO); + return (3); + } + i++; + } return (0); } diff --git a/src/expander/expander.c b/src/expander/expander.c index 412852f..bcfe13e 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/13 15:57:06 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/14 12:50:59 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -94,6 +94,7 @@ char **expander(t_tools *tools, char **str) tmp = NULL; while (str[i] != NULL) { + printf("exit = %d", WEXITSTATUS(tools->pid[tools->pipes])); if (str[i][dollar_sign(str[i]) - 2] != '\'' && dollar_sign(str[i]) != 0 && str[i][dollar_sign(str[i])] != '\0') { diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 9c94266..a3755c5 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/14 10:57:25 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/14 10:59:21 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -53,6 +53,7 @@ int prepare_executor(t_tools *tools) int minishell_loop(t_tools *tools) { tools->args = readline("\033[1;36mminishell\033[34m$ \033[0m"); + tools->args = ft_strtrim(tools->args, " "); if (!tools->args) { ft_putendl_fd("exit", STDOUT_FILENO); From bd4907a1ef18251e61ecccbc1c5db0a7501a25eb Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Thu, 14 Apr 2022 16:24:12 +0200 Subject: [PATCH 139/163] start of 0 --- src/builtins/mini_export.c | 12 +++++++----- src/builtins/mini_unset.c | 8 +++++++- src/expander/expander.c | 13 ++++++++++--- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/src/builtins/mini_export.c b/src/builtins/mini_export.c index f72b96d..e8e5cc6 100644 --- a/src/builtins/mini_export.c +++ b/src/builtins/mini_export.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:07:21 by fpolycar #+# #+# */ -/* Updated: 2022/04/14 11:45:26 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/14 15:03:22 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -41,23 +41,23 @@ int check_parameter(char *str) if (ft_isdigit(str[0])) { ft_putendl_fd("export: not an identifier", STDERR_FILENO); - return (1); + return (EXIT_FAILURE); } if (str[0] == '=') { ft_putendl_fd("export: bad assignment", STDERR_FILENO); - return (2); + return (EXIT_FAILURE); } while (str[i] != '=') { if (str[i] == '|') { ft_putendl_fd("export: not valid in this context", STDERR_FILENO); - return (3); + return (EXIT_FAILURE); } i++; } - return (0); + return (EXIT_SUCCESS); } char **whileloop_add_var(char **arr, char **rtn, char *str) @@ -122,6 +122,8 @@ int mini_export(t_tools *tools, t_simple_cmds *simple_cmd) } } } + else + return (EXIT_FAILURE); } return (EXIT_SUCCESS); } diff --git a/src/builtins/mini_unset.c b/src/builtins/mini_unset.c index 1004d48..0863397 100644 --- a/src/builtins/mini_unset.c +++ b/src/builtins/mini_unset.c @@ -6,7 +6,7 @@ /* By: maiadegraaf str[1]) + { ft_putendl_fd("unset: not enough arguments", STDERR_FILENO); + return (EXIT_FAILURE); + } if (equal_sign(simple_cmd->str[1]) != 0) + { ft_putendl_fd("unset: invalid parameter name", STDERR_FILENO); + return (EXIT_FAILURE); + } else { tmp = del_var(tools->envp, simple_cmd->str[1]); diff --git a/src/expander/expander.c b/src/expander/expander.c index bcfe13e..19c3ef2 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/14 12:50:59 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/14 14:48:46 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -71,8 +71,16 @@ char *detect_dollar_sign(t_tools *tools, char *str) while (str[j]) { j += handle_digit_after_dollar(j, str); - if (str[j] == '$') + if (str[j] == '$' && str[j + 1] != '\"' && str[j + 1] != '?') j += loop_if_dollar_sign(tools, str, &tmp, j); + if (str[j + 1] == '?') + { + if (tools->pipes != 0) + tmp = ft_itoa(tools->pid[tools->pipes]); + else + tmp = char_to_str('0'); + j += ft_strlen(tmp) + 1; + } else { tmp2 = char_to_str(str[j++]); @@ -94,7 +102,6 @@ char **expander(t_tools *tools, char **str) tmp = NULL; while (str[i] != NULL) { - printf("exit = %d", WEXITSTATUS(tools->pid[tools->pipes])); if (str[i][dollar_sign(str[i]) - 2] != '\'' && dollar_sign(str[i]) != 0 && str[i][dollar_sign(str[i])] != '\0') { From a1f0858ec368e12ce87d2d849d80ef4d1f5baa1b Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Thu, 14 Apr 2022 17:46:48 +0200 Subject: [PATCH 140/163] fixed bla="s -la" --- .vscode/settings.json | 3 +- Makefile | 3 +- includes/color.h | 7 ++-- includes/executor.h | 11 ++++--- includes/minishell.h | 11 ++++++- includes/parser.h | 3 +- src/error/error_handeling.c | 5 +-- src/executor/executor.c | 39 +++++++++++++++++++--- src/executor/handle_cmd.c | 26 ++++++++------- src/executor/heredoc.c | 39 +++++++++++++++++----- src/expander/expander.c | 3 +- src/expander/expanders_utils.c | 4 +-- src/main.c | 58 ++------------------------------- src/signals.c | 7 +++- src/utils/minishell_loop.c | 6 ++-- src/utils/t_simple_cmds_utils.c | 5 ++- 16 files changed, 129 insertions(+), 101 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 988cd2a..b643038 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -4,6 +4,7 @@ "stdio.h": "c", "readline.h": "c", "libft.h": "c", - "executor.h": "c" + "executor.h": "c", + "sstream": "c" } } \ No newline at end of file diff --git a/Makefile b/Makefile index 7a2e03f..33f1cbe 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ FLAGS = -Wall -Werror -Wextra -g #-fsanitize=address LIBFT = ./libraries/libft/libft.a -HEADER = $(wildcard ./includes/*.h) +HEADER = $(wildcard ./includes/*.h) READLINE_DIR = $(shell brew --prefix readline) @@ -94,6 +94,7 @@ $(PATHO): clean: @echo "Cleaning" @rm -f $(OBJS) + @rm -f $(PATHB).tmp* @make fclean -C libraries/libft fclean: clean diff --git a/includes/color.h b/includes/color.h index 42c37ea..23fb27f 100644 --- a/includes/color.h +++ b/includes/color.h @@ -1,7 +1,6 @@ #ifndef _COLOR_H_ #define _COLOR_H_ - # define RESET_COLOR "\033[0m" # define BLACK "\033[30m" # define RED "\033[31m" @@ -10,8 +9,8 @@ # define LIGHT_GREEN "\033[92m" # define YELLOW "\033[33m" # define LIGHT_YELLOW "\033[93m" -# define BLUE "\033[34m" -# define LIGHT_BLUE "\033[94m" +# define BLUE "\033[34m" +# define LIGHT_BLUE "\033[94m" # define MAGENTA "\033[35m" # define LIGHT_MAGENTA "\033[95m" # define CYAN "\033[36m" @@ -29,4 +28,6 @@ # define CYAN_BOLD "\033[1;36m" # define WHITE_BOLD "\033[1;37m" +# define WELCOME_MSG "\033[96m╔════════════════════════════════════════════════════════════════════════════════╗\n║ ║\n║ \033[95m█▀█ █▀▀ █▀▀   █▀█ █▀█ █▀▀ █▀ █▀▀ █▄░█ ▀█▀ █▀ \033[96m║\n║ \033[95m█▀▀ █▀░ █▄▄   █▀▀ █▀▄ ██▄ ▄█ ██▄ █░▀█ ░█░ ▄█ \033[96m║\n║ ║\n║ \033[35m ███╗░░░███╗██╗███╗░░██╗██╗░██████╗██╗░░██╗███████╗██╗░░░░░██╗░░░░░ \033[96m║\n║ \033[35m ████╗░████║██║████╗░██║██║██╔════╝██║░░██║██╔════╝██║░░░░░██║░░░░░ \033[96m║\n║ \033[35m ██╔████╔██║██║██╔██╗██║██║╚█████╗░███████║█████╗░░██║░░░░░██║░░░░░ \033[96m║\n║ \033[35m ██║╚██╔╝██║██║██║╚████║██║░╚═══██╗██╔══██║██╔══╝░░██║░░░░░██║░░░░░ \033[96m║\n║ \033[35m ██║░╚═╝░██║██║██║░╚███║██║██████╔╝██║░░██║███████╗███████╗███████╗ \033[96m║\n║ \033[35m ╚═╝░░░░░╚═╝╚═╝╚═╝░░╚══╝╚═╝╚═════╝░╚═╝░░╚═╝╚══════╝╚══════╝╚══════╝ \033[96m║\n║ ║\n╚════════════════════════════════════════════════════════════════════════════════╝" + #endif \ No newline at end of file diff --git a/includes/executor.h b/includes/executor.h index e28f3bf..1c094eb 100644 --- a/includes/executor.h +++ b/includes/executor.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:17:39 by mgraaf #+# #+# */ -/* Updated: 2022/04/13 15:05:46 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/14 14:58:26 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -27,13 +27,14 @@ int pipe_wait(int *pid, int amount); // handle_cmd int find_cmd(t_simple_cmds *cmd, t_tools *tools); void handle_cmd(t_simple_cmds *cmd, t_tools *tools); -void dup_cmd(t_simple_cmds *cmd, t_tools *tools, int end[2], int fd_in); +void dup_cmd(t_simple_cmds *cmd, t_tools *tools, + int end[2], int fd_in); void single_cmd(t_simple_cmds *cmd, t_tools *tools); // heredoc -int ft_heredoc(t_tools *tools, t_heredoc *heredoc); -int read_heredoc(t_heredoc *heredoc, t_tools *tools); -int create_heredoc(t_heredoc *heredoc, bool quotes, t_tools *tools); +int ft_heredoc(t_tools *tools, t_heredoc *heredoc, char *file_name); +int create_heredoc(t_heredoc *heredoc, bool quotes, + t_tools *tools, char *file_name); int send_heredoc(t_tools *tools, t_simple_cmds *cmd); #endif \ No newline at end of file diff --git a/includes/minishell.h b/includes/minishell.h index 433ca74..1776c83 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/04/13 11:55:41 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/14 16:56:49 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -48,4 +48,13 @@ char *delete_quotes(char *str, char c); //builtins int (*builtin_arr(char *str))(t_tools *tools, t_simple_cmds *simple_cmd); +typedef struct s_global +{ + int error_num; + int stop_heredoc; + int in_heredoc; +} t_global; + +t_global g_global; + #endif \ No newline at end of file diff --git a/includes/parser.h b/includes/parser.h index de86528..183261a 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/04/14 11:44:17 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/14 14:29:51 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -69,6 +69,7 @@ typedef struct s_simple_cmds int (*builtin)(t_tools *, struct s_simple_cmds *); int num_redirections; t_heredoc *heredoc; + char *hd_file_name; t_lexor *redirections; struct s_simple_cmds *next; struct s_simple_cmds *prev; diff --git a/src/error/error_handeling.c b/src/error/error_handeling.c index b3dc6da..5cfcba3 100755 --- a/src/error/error_handeling.c +++ b/src/error/error_handeling.c @@ -6,7 +6,7 @@ /* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/04/14 12:08:35 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/14 17:45:51 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "executor.h" +char *join_split_str(char **split_str, char *new_str) +{ + char *tmp; + char *add_space; + int i; + + tmp = ft_strdup(split_str[0]); + i = 1; + while (split_str[i]) + { + new_str = tmp; + add_space = ft_strjoin(new_str, " "); + free(new_str); + tmp = ft_strjoin(add_space, split_str[i]); + free(add_space); + i++; + printf("%d->%s\n", i, tmp); + } + new_str = tmp; + return (new_str); +} + t_simple_cmds *call_expander(t_tools *tools, t_simple_cmds *cmd) { t_lexor *start; + char *joined_str; cmd->str = expander(tools, cmd->str); + joined_str = join_split_str(cmd->str, NULL); + free_arr(cmd->str); + cmd->str = ft_split(joined_str, ' '); + printf(">%s<\n", joined_str); + free(joined_str); start = cmd->redirections; while (cmd->redirections) { @@ -40,6 +68,8 @@ int pipe_wait(int *pid, int amount) i++; } waitpid(pid[i], &status, 0); + if (WIFEXITED(status)) + g_global.error_num = WEXITSTATUS(status); return (EXIT_SUCCESS); } @@ -61,14 +91,14 @@ int ft_fork(t_tools *tools, int end[2], int fd_in, t_simple_cmds *cmd) return (EXIT_SUCCESS); } -int check_fd_heredoc(t_tools *tools, int end[2]) +int check_fd_heredoc(t_tools *tools, int end[2], t_simple_cmds *cmd) { int fd_in; if (tools->heredoc) { close(end[0]); - fd_in = open("build/tmp_heredoc_file.txt", O_RDONLY); + fd_in = open(cmd->hd_file_name, O_RDONLY); } else fd_in = end[0]; @@ -86,11 +116,12 @@ int executor(t_tools *tools) tools->simple_cmds = call_expander(tools, tools->simple_cmds); if (tools->simple_cmds->next) pipe(end); + send_heredoc(tools, tools->simple_cmds); ft_fork(tools, end, fd_in, tools->simple_cmds); close(end[1]); if (tools->simple_cmds->prev) close(fd_in); - fd_in = check_fd_heredoc(tools, end); + fd_in = check_fd_heredoc(tools, end, tools->simple_cmds); if (tools->simple_cmds->next) tools->simple_cmds = tools->simple_cmds->next; else diff --git a/src/executor/handle_cmd.c b/src/executor/handle_cmd.c index a5f462a..9eca65a 100644 --- a/src/executor/handle_cmd.c +++ b/src/executor/handle_cmd.c @@ -6,7 +6,7 @@ /* By: maiadegraaf str[0], STDERR_FILENO); ft_putstr_fd(": command not found\n", STDERR_FILENO); - exit(EXIT_FAILURE); - return (1); + return (EXIT_FAILURE); } void handle_cmd(t_simple_cmds *cmd, t_tools *tools) { int fd; + int exit_code; - if (cmd->heredoc) + exit_code = 0; + if (tools->heredoc) { - send_heredoc(tools, cmd); - fd = open("build/tmp_heredoc_file.txt", O_RDONLY); + fd = open(cmd->hd_file_name, O_RDONLY); dup2(fd, STDIN_FILENO); close(fd); } if (cmd->redirections) handle_redirections(cmd, tools); - if (cmd->builtin) + if (cmd->builtin != NULL) { - cmd->builtin(tools, cmd); - exit(EXIT_SUCCESS); + exit_code = cmd->builtin(tools, cmd); + exit(exit_code); } else if (cmd->str[0][0] != '\0') - find_cmd(cmd, tools); + exit_code = find_cmd(cmd, tools); + exit(exit_code); } void dup_cmd(t_simple_cmds *cmd, t_tools *tools, int end[2], int fd_in) @@ -76,13 +77,16 @@ void single_cmd(t_simple_cmds *cmd, t_tools *tools) tools->simple_cmds = call_expander(tools, tools->simple_cmds); if (cmd->builtin) { - cmd->builtin(tools, cmd); + g_global.error_num = cmd->builtin(tools, cmd); return ; } + send_heredoc(tools, cmd); pid = fork(); if (pid < 0) ft_error(5, tools); if (pid == 0) handle_cmd(cmd, tools); waitpid(pid, &status, 0); + if (WIFEXITED(status)) + g_global.error_num = WEXITSTATUS(status); } diff --git a/src/executor/heredoc.c b/src/executor/heredoc.c index 3fe1295..14c1f94 100644 --- a/src/executor/heredoc.c +++ b/src/executor/heredoc.c @@ -6,7 +6,7 @@ /* By: maiadegraaf del); - fd = open("build/tmp_heredoc_file.txt", O_CREAT | O_RDWR | O_TRUNC, 0644); + fd = open(file_name, O_CREAT | O_RDWR | O_TRUNC, 0644); ft_putstr_fd("\033[1;34m> \033[0m", STDERR_FILENO); line = get_next_line(STDIN_FILENO); - while (ft_strncmp(heredoc->del, line, del_len)) + while (line && (ft_strncmp(heredoc->del, line, ft_strlen(line) - 1) + || ft_strncmp(heredoc->del, line, del_len)) + && !g_global.stop_heredoc) { if (quotes == false) line = send_expander(tools, line); @@ -43,32 +46,52 @@ int create_heredoc(t_heredoc *heredoc, bool quotes, t_tools *tools) ft_putstr_fd("\033[1;34m> \033[0m", STDERR_FILENO); line = get_next_line(STDIN_FILENO); } + if (!line) + ft_putstr_fd("\n", STDERR_FILENO); + if (g_global.stop_heredoc || !line) + return (EXIT_FAILURE); close(fd); return (EXIT_SUCCESS); } -int ft_heredoc(t_tools *tools, t_heredoc *heredoc) +int ft_heredoc(t_tools *tools, t_heredoc *heredoc, char *file_name) { bool quotes; + int sl; + sl = EXIT_SUCCESS; if (heredoc->del[0] == '\"' && heredoc->del[ft_strlen(heredoc->del) - 1] == '\"') quotes = true; else quotes = false; - create_heredoc(heredoc, quotes, tools); + g_global.stop_heredoc = 0; + g_global.in_heredoc = 1; + sl = create_heredoc(heredoc, quotes, tools, file_name); + g_global.in_heredoc = 0; tools->heredoc = true; - return (EXIT_SUCCESS); + return (sl); } int send_heredoc(t_tools *tools, t_simple_cmds *cmd) { t_heredoc *start; + static int i = 0; + char *num; + int sl; start = cmd->heredoc; + sl = EXIT_SUCCESS; while (cmd->heredoc) { - ft_heredoc(tools, cmd->heredoc); + if (cmd->hd_file_name) + free(cmd->hd_file_name); + num = ft_itoa(i++); + cmd->hd_file_name = ft_strjoin("build/.tmp_heredoc_file", num); + free(num); + sl = ft_heredoc(tools, cmd->heredoc, cmd->hd_file_name); + if (sl) + return (reset_tools(tools)); cmd->heredoc = cmd->heredoc->next; } cmd->heredoc = start; diff --git a/src/expander/expander.c b/src/expander/expander.c index bcfe13e..7eff383 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/14 12:50:59 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/14 17:37:16 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -94,7 +94,6 @@ char **expander(t_tools *tools, char **str) tmp = NULL; while (str[i] != NULL) { - printf("exit = %d", WEXITSTATUS(tools->pid[tools->pipes])); if (str[i][dollar_sign(str[i]) - 2] != '\'' && dollar_sign(str[i]) != 0 && str[i][dollar_sign(str[i])] != '\0') { diff --git a/src/expander/expanders_utils.c b/src/expander/expanders_utils.c index 2ef2fe0..1c1c74f 100644 --- a/src/expander/expanders_utils.c +++ b/src/expander/expanders_utils.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/31 16:08:47 by fpolycar #+# #+# */ -/* Updated: 2022/04/13 15:35:02 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/14 17:37:47 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -49,7 +49,7 @@ int after_dol_lenght(char *str, int j) char *delete_quotes(char *str, char c) { - int i; + int i; int j; i = 0; diff --git a/src/main.c b/src/main.c index 1c0f1b0..5e405df 100644 --- a/src/main.c +++ b/src/main.c @@ -6,66 +6,12 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 12:04:02 by mgraaf #+# #+# */ -/* Updated: 2022/04/13 14:39:04 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/14 15:02:31 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -void wrap_line(char *line, char *color) -{ - printf("\n%s║\t", LIGHT_CYAN); - printf("%s%s", color, line); - printf("%s\t ║", LIGHT_CYAN); -} - -void print_presents(void) -{ - printf("\n%s║\t\t\t\t\t\t\t\t\t\t ║", LIGHT_CYAN); - wrap_line( - "\t █▀█ █▀▀ █▀▀   █▀█ █▀█ █▀▀ █▀ █▀▀ █▄░█ ▀█▀ █▀\t\t", - LIGHT_MAGENTA); - wrap_line( - "\t █▀▀ █▀░ █▄▄   █▀▀ █▀▄ ██▄ ▄█ ██▄ █░▀█ ░█░ ▄█\t\t", - LIGHT_MAGENTA); - printf("\n%s║\t\t\t\t\t\t\t\t\t\t ║", LIGHT_CYAN); -} - -void print_minishell(void) -{ - wrap_line( - "███╗░░░███╗██╗███╗░░██╗██╗░██████╗██╗░░██╗███████╗██╗░░░░░██╗░░░░░", - MAGENTA); - wrap_line( - "████╗░████║██║████╗░██║██║██╔════╝██║░░██║██╔════╝██║░░░░░██║░░░░░", - MAGENTA); - wrap_line( - "██╔████╔██║██║██╔██╗██║██║╚█████╗░███████║█████╗░░██║░░░░░██║░░░░░", - MAGENTA); - wrap_line( - "██║╚██╔╝██║██║██║╚████║██║░╚═══██╗██╔══██║██╔══╝░░██║░░░░░██║░░░░░", - MAGENTA); - wrap_line( - "██║░╚═╝░██║██║██║░╚███║██║██████╔╝██║░░██║███████╗███████╗███████╗", - MAGENTA); - wrap_line( - "╚═╝░░░░░╚═╝╚═╝╚═╝░░╚══╝╚═╝╚═════╝░╚═╝░░╚═╝╚══════╝╚══════╝╚══════╝", - MAGENTA); -} - -void print_welcome(void) -{ - printf("\n%s╔═════════════════════════════════════════════", - LIGHT_CYAN); - printf("═══════════════════════════════════╗"); - print_presents(); - print_minishell(); - printf("\n%s║\t\t\t\t\t\t\t\t\t\t ║\n", LIGHT_CYAN); - printf("╚═══════════════════════════════════════"); - printf("═════════════════════════════════════════╝\n"); - printf("\n\n%s", RESET_COLOR); -} - int main(int argc, char **argv, char **envp) { t_tools tools; @@ -81,7 +27,7 @@ int main(int argc, char **argv, char **envp) init_signals(); parse_envp(&tools); implement_tools(&tools); - print_welcome(); + printf("\n%s\n\n", WELCOME_MSG); minishell_loop(&tools); return (0); } diff --git a/src/signals.c b/src/signals.c index de64d39..3e7bc7c 100644 --- a/src/signals.c +++ b/src/signals.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/04/08 14:35:54 by fpolycar #+# #+# */ -/* Updated: 2022/04/13 14:41:54 by maiadegraaf ######## odam.nl */ +/* Updated: 2022/04/14 17:02:24 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,6 +15,11 @@ void sigint_handler(int sig) { ft_putstr_fd("\n", STDERR_FILENO); + if (g_global.in_heredoc) + { + g_global.stop_heredoc = 1; + return ; + } rl_on_new_line(); rl_replace_line("", 0); rl_redisplay(); diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index bfbf4a3..e98f77a 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/14 13:51:58 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/14 17:32:54 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -21,6 +21,8 @@ int implement_tools(t_tools *tools) tools->reset = false; tools->pid = NULL; tools->heredoc = false; + g_global.stop_heredoc = 0; + g_global.in_heredoc = 0; return (1); } @@ -32,7 +34,7 @@ int reset_tools(t_tools *tools) free(tools->pid); implement_tools(tools); tools->reset = true; - // system("leaks minishell"); + system("leaks minishell"); minishell_loop(tools); return (1); } diff --git a/src/utils/t_simple_cmds_utils.c b/src/utils/t_simple_cmds_utils.c index 7a1f5f4..7c84641 100644 --- a/src/utils/t_simple_cmds_utils.c +++ b/src/utils/t_simple_cmds_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/04/12 11:39:02 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/14 14:39:54 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,6 +23,7 @@ t_simple_cmds *ft_simple_cmdsnew(char **str, t_heredoc *heredoc, new_element->str = str; new_element->builtin = builtin_arr(str[0]); new_element->heredoc = heredoc; + new_element->hd_file_name = NULL; new_element->num_redirections = num_redirections; new_element->redirections = redirections; new_element->next = NULL; @@ -77,6 +78,8 @@ void ft_simple_cmdsclear(t_simple_cmds **lst) ft_lexorclear(&redirections_tmp); if ((*lst)->str) free_arr((*lst)->str); + if ((*lst)->hd_file_name) + free((*lst)->hd_file_name); free(*lst); *lst = tmp; } From ccf454c8fd87a7f53d86d7cbf269d65322b529ad Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Fri, 15 Apr 2022 15:01:45 +0200 Subject: [PATCH 141/163] hotfix leak trim --- src/expander/expander.c | 10 +++++++--- src/utils/minishell_loop.c | 10 +++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/expander/expander.c b/src/expander/expander.c index ce7990c..be3f696 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/14 17:49:32 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/15 11:59:24 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -109,8 +109,12 @@ char **expander(t_tools *tools, char **str) free(str[i]); str[i] = tmp; } - str[i] = delete_quotes(str[i], '\"'); - str[i] = delete_quotes(str[i], '\''); + if (ft_strncmp(str[0], "export", ft_strlen(str[0]) - 1) != 0) + { + printf("sdfs"); + str[i] = delete_quotes(str[i], '\"'); + str[i] = delete_quotes(str[i], '\''); + } i++; } return (str); diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index e98f77a..3f4c10f 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/14 17:32:54 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/15 11:32:34 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ int reset_tools(t_tools *tools) free(tools->pid); implement_tools(tools); tools->reset = true; - system("leaks minishell"); + // system("leaks minishell"); minishell_loop(tools); return (1); } @@ -55,8 +55,12 @@ int prepare_executor(t_tools *tools) int minishell_loop(t_tools *tools) { + char *tmp; + tools->args = readline("\033[1;36mminishell\033[34m$ \033[0m"); - tools->args = ft_strtrim(tools->args, " "); + tmp = ft_strtrim(tools->args, " "); + free(tools->args); + tools->args = tmp; if (!tools->args) { ft_putendl_fd("exit", STDOUT_FILENO); From 8ccdcb8f1534fbe19a39d489a591847fe96f90cb Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Fri, 15 Apr 2022 15:15:28 +0200 Subject: [PATCH 142/163] add exit codes --- src/builtins/mini_exit.c | 66 +++++++++++++++++++++++++++++++------ src/error/error_handeling.c | 2 +- src/executor/handle_cmd.c | 4 +-- src/executor/heredoc.c | 5 ++- src/lexor/handle_quotes.c | 3 +- 5 files changed, 64 insertions(+), 16 deletions(-) diff --git a/src/builtins/mini_exit.c b/src/builtins/mini_exit.c index 18fc9cd..8d6c1a7 100644 --- a/src/builtins/mini_exit.c +++ b/src/builtins/mini_exit.c @@ -6,25 +6,71 @@ /* By: maiadegraaf +void free_tools(t_tools *tools) +{ + free_arr(tools->paths); + free_arr(tools->envp); + free(tools->args); + ft_simple_cmdsclear(&tools->simple_cmds); + free(tools->pwd); + free(tools->old_pwd); + if (tools->pipes) + free(tools->pid); +} + +int is_str_digit(char *str) +{ + int i; + + i = 0; + while (str[i]) + { + if (!ft_isdigit(str[i])) + return (0); + i++; + } + return (1); +} + +void determine_exit_code(char **str) +{ + int exit_code; + + if (is_str_digit(str[1])) + { + exit_code = ft_atoi(str[1]); + free(str[1]); + exit(exit_code); + } + else + { + ft_putstr_fd("minishell: exit: ", STDERR_FILENO); + ft_putstr_fd(str[1], STDERR_FILENO); + ft_putstr_fd(": numeric argument required\n", STDERR_FILENO); + free_arr(str); + exit(255); + } +} + int mini_exit(t_tools *tools, t_simple_cmds *simple_cmd) { - int status; + char **str; - status = 0; - if (tools->pipes != 0) - status = tools->pid[tools->pipes]; ft_putendl_fd("exit", STDERR_FILENO); - if (simple_cmd->str[1] != NULL) - status = ft_atoi(simple_cmd->str[1]); - free_arr(simple_cmd->str); - free_arr(tools->envp); - exit(status); + if (simple_cmd->str[2]) + { + ft_putstr_fd("minishell: exit: too many arguments", STDERR_FILENO); + return (EXIT_FAILURE); + } + str = ft_arrdup(simple_cmd->str); + free_tools(tools); + determine_exit_code(str); return (EXIT_SUCCESS); } diff --git a/src/error/error_handeling.c b/src/error/error_handeling.c index 5cfcba3..e9f80ff 100755 --- a/src/error/error_handeling.c +++ b/src/error/error_handeling.c @@ -6,7 +6,7 @@ /* By: maiadegraaf str[0], STDERR_FILENO); ft_putstr_fd(": command not found\n", STDERR_FILENO); - return (EXIT_FAILURE); + return (127); } void handle_cmd(t_simple_cmds *cmd, t_tools *tools) diff --git a/src/executor/heredoc.c b/src/executor/heredoc.c index 14c1f94..4f9a86e 100644 --- a/src/executor/heredoc.c +++ b/src/executor/heredoc.c @@ -6,7 +6,7 @@ /* By: maiadegraaf heredoc, cmd->hd_file_name); if (sl) + { + g_global.error_num = 1; return (reset_tools(tools)); + } cmd->heredoc = cmd->heredoc->next; } cmd->heredoc = start; diff --git a/src/lexor/handle_quotes.c b/src/lexor/handle_quotes.c index a333f38..3ec3136 100644 --- a/src/lexor/handle_quotes.c +++ b/src/lexor/handle_quotes.c @@ -6,7 +6,7 @@ /* By: maiadegraaf Date: Fri, 15 Apr 2022 16:00:30 +0200 Subject: [PATCH 143/163] fixed mini_exit bug --- src/builtins/mini_exit.c | 21 ++++++++++----------- src/utils/minishell_loop.c | 4 ++-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/builtins/mini_exit.c b/src/builtins/mini_exit.c index 8d6c1a7..0fc9a5d 100644 --- a/src/builtins/mini_exit.c +++ b/src/builtins/mini_exit.c @@ -6,7 +6,7 @@ /* By: maiadegraaf str[2]) + if (simple_cmd->str[1] && simple_cmd->str[2]) { - ft_putstr_fd("minishell: exit: too many arguments", STDERR_FILENO); + ft_putstr_fd("minishell: exit: too many arguments\n", STDERR_FILENO); return (EXIT_FAILURE); } str = ft_arrdup(simple_cmd->str); diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index e98f77a..89529d3 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/14 17:32:54 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/15 15:24:58 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ int reset_tools(t_tools *tools) free(tools->pid); implement_tools(tools); tools->reset = true; - system("leaks minishell"); + // system("leaks minishell"); minishell_loop(tools); return (1); } From a679a4ba14ff4df782098f7fcf36794d6796dfa3 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Fri, 15 Apr 2022 16:05:54 +0200 Subject: [PATCH 144/163] export done --- src/builtins/mini_export.c | 27 +++++++++++++++++---------- src/builtins/utils_builtins.c | 2 +- src/executor/executor.c | 8 +------- src/executor/handle_cmd.c | 10 +++++++++- src/expander/expander.c | 4 ++-- src/expander/expanders_utils.c | 4 ++-- 6 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/builtins/mini_export.c b/src/builtins/mini_export.c index e8e5cc6..e6295ed 100644 --- a/src/builtins/mini_export.c +++ b/src/builtins/mini_export.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:07:21 by fpolycar #+# #+# */ -/* Updated: 2022/04/14 15:03:22 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/15 16:03:30 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,8 +17,10 @@ int variable_exist(t_tools *tools, char *str) int i; i = 0; - if (str[equal_sign(str)] == '"' || str[equal_sign(str)] == '\'') - delete_quotes_value(str); + if (str[equal_sign(str)] == '\"') + delete_quotes(str, '\"'); + if (str[equal_sign(str)] == '\'') + delete_quotes(str, '\''); while (tools->envp[i]) { if (ft_strncmp(tools->envp[i], @@ -43,6 +45,8 @@ int check_parameter(char *str) ft_putendl_fd("export: not an identifier", STDERR_FILENO); return (EXIT_FAILURE); } + if (equal_sign(str) == 0) + return (EXIT_FAILURE); if (str[0] == '=') { ft_putendl_fd("export: bad assignment", STDERR_FILENO); @@ -105,25 +109,28 @@ char **add_var(char **arr, char *str) int mini_export(t_tools *tools, t_simple_cmds *simple_cmd) { char **tmp; + int i; + i = 1; if (!simple_cmd->str[1]) mini_env(tools, simple_cmd); else { - if (check_parameter(simple_cmd->str[1]) == 0) - { - if (variable_exist(tools, simple_cmd->str[1]) == 0) + while (simple_cmd->str[i]) + { + printf("%s\n", simple_cmd->str[i]); + if (check_parameter(simple_cmd->str[i]) == 0 + && variable_exist(tools, simple_cmd->str[i]) == 0) { - if (simple_cmd->str[1]) + if (simple_cmd->str[i]) { - tmp = add_var(tools->envp, simple_cmd->str[1]); + tmp = add_var(tools->envp, simple_cmd->str[i]); free_arr(tools->envp); tools->envp = tmp; } } + i++; } - else - return (EXIT_FAILURE); } return (EXIT_SUCCESS); } diff --git a/src/builtins/utils_builtins.c b/src/builtins/utils_builtins.c index 87f3675..7516404 100644 --- a/src/builtins/utils_builtins.c +++ b/src/builtins/utils_builtins.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:04:47 by fpolycar #+# #+# */ -/* Updated: 2022/04/01 16:01:46 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/15 16:03:41 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/executor/executor.c b/src/executor/executor.c index 88d63e5..f46024c 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/04/14 17:45:51 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/15 15:41:35 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -37,14 +37,8 @@ char *join_split_str(char **split_str, char *new_str) t_simple_cmds *call_expander(t_tools *tools, t_simple_cmds *cmd) { t_lexor *start; - char *joined_str; cmd->str = expander(tools, cmd->str); - joined_str = join_split_str(cmd->str, NULL); - free_arr(cmd->str); - cmd->str = ft_split(joined_str, ' '); - printf(">%s<\n", joined_str); - free(joined_str); start = cmd->redirections; while (cmd->redirections) { diff --git a/src/executor/handle_cmd.c b/src/executor/handle_cmd.c index 9eca65a..c93f163 100644 --- a/src/executor/handle_cmd.c +++ b/src/executor/handle_cmd.c @@ -6,18 +6,26 @@ /* By: maiadegraaf str, NULL); + free_arr(cmd->str); + cmd->str = ft_split(joined_str, ' '); + printf(">%s<\n", joined_str); + free(joined_str); while (tools->paths[i]) { mycmd = ft_strjoin(tools->paths[i], cmd->str[0]); diff --git a/src/expander/expander.c b/src/expander/expander.c index be3f696..cb32f89 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/15 11:59:24 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/15 15:37:25 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -109,9 +109,9 @@ char **expander(t_tools *tools, char **str) free(str[i]); str[i] = tmp; } + printf("%d\n\n", ft_strncmp(str[0], "export", ft_strlen(str[0]) - 1)); if (ft_strncmp(str[0], "export", ft_strlen(str[0]) - 1) != 0) { - printf("sdfs"); str[i] = delete_quotes(str[i], '\"'); str[i] = delete_quotes(str[i], '\''); } diff --git a/src/expander/expanders_utils.c b/src/expander/expanders_utils.c index 1c1c74f..96e1579 100644 --- a/src/expander/expanders_utils.c +++ b/src/expander/expanders_utils.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/31 16:08:47 by fpolycar #+# #+# */ -/* Updated: 2022/04/14 17:37:47 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/15 16:01:44 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -56,7 +56,7 @@ char *delete_quotes(char *str, char c) j = 0; while (str[i]) { - if (str[i] == c && str[i - 1] != '\\') + if (str[i] == c) { j = 0; while (str[i + j] == c) From 8679ab9682fff91f6033f2ce5c2a69bd8e084d5a Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Fri, 15 Apr 2022 16:20:52 +0200 Subject: [PATCH 145/163] alter makefile --- Makefile | 49 +++++++++++++++++++++++++++++-------- src/builtins/mini_exit.c | 3 ++- src/executor/executor.c | 9 +------ src/executor/handle_cmd.c | 9 ++++++- src/utils/minishell_loop.c | 4 +-- src/utils/t_heredoc_utils.c | 3 ++- 6 files changed, 54 insertions(+), 23 deletions(-) diff --git a/Makefile b/Makefile index 33f1cbe..fb71346 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,6 @@ NAME = minishell MKDIR = mkdir - CC = gcc LIBFTP = libraries/libft @@ -19,14 +18,37 @@ PATHEX = src/executor/ BUILD_PATHS = $(PATHB) $(PATHO) -src = $(wildcard $(PATHS)*.c) \ - $(wildcard $(PATHSL)*.c) \ - $(wildcard $(PATHSP)*.c) \ - $(wildcard $(PATHSB)*.c) \ - $(wildcard $(PATHSU)*.c) \ - $(wildcard $(PATHSE)*.c) \ - $(wildcard $(PATHEX)*.c) \ - $(wildcard $(PATHSEX)*.c) +src = src/main.c \ + src/signals.c \ + src/lexor/handle_quotes.c \ + src/lexor/handle_token.c \ + src/lexor/token_reader.c \ + src/parser/handle_redirections.c \ + src/parser/parser.c \ + src/parser/parser_utils.c \ + src/builtins/builtins.c \ + src/builtins/mini_cd.c \ + src/builtins/mini_echo.c \ + src/builtins/mini_env.c \ + src/builtins/mini_exit.c \ + src/builtins/mini_export.c \ + src/builtins/mini_pwd.c \ + src/builtins/mini_unset.c \ + src/builtins/utils_builtins.c \ + src/utils/minishell_loop.c \ + src/utils/parse_envp.c \ + src/utils/t_heredoc_utils.c \ + src/utils/t_lexor_clear_utils.c \ + src/utils/t_lexor_utils.c \ + src/utils/t_simple_cmds_utils.c \ + src/utils/utils.c \ + src/error/error_handeling.c \ + src/executor/check_redirections.c \ + src/executor/executor.c \ + src/executor/handle_cmd.c \ + src/executor/heredoc.c \ + src/expander/expander.c \ + src/expander/expanders_utils.c OBJS = $(addprefix $(PATHO), $(notdir $(patsubst %.c, %.o, $(src)))) @@ -34,7 +56,14 @@ FLAGS = -Wall -Werror -Wextra -g #-fsanitize=address LIBFT = ./libraries/libft/libft.a -HEADER = $(wildcard ./includes/*.h) +HEADER = .includes/builtins.h \ + .includes/color.h \ + .includes/error.h \ + .includes/executor.h \ + .includes/lexor.h \ + .includes/minishell.h \ + .includes/parser.h \ + .includes/utils.h READLINE_DIR = $(shell brew --prefix readline) diff --git a/src/builtins/mini_exit.c b/src/builtins/mini_exit.c index 0fc9a5d..0b4257c 100644 --- a/src/builtins/mini_exit.c +++ b/src/builtins/mini_exit.c @@ -6,7 +6,7 @@ /* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/04/14 17:45:51 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/15 16:04:19 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -28,7 +28,6 @@ char *join_split_str(char **split_str, char *new_str) tmp = ft_strjoin(add_space, split_str[i]); free(add_space); i++; - printf("%d->%s\n", i, tmp); } new_str = tmp; return (new_str); @@ -37,14 +36,8 @@ char *join_split_str(char **split_str, char *new_str) t_simple_cmds *call_expander(t_tools *tools, t_simple_cmds *cmd) { t_lexor *start; - char *joined_str; cmd->str = expander(tools, cmd->str); - joined_str = join_split_str(cmd->str, NULL); - free_arr(cmd->str); - cmd->str = ft_split(joined_str, ' '); - printf(">%s<\n", joined_str); - free(joined_str); start = cmd->redirections; while (cmd->redirections) { diff --git a/src/executor/handle_cmd.c b/src/executor/handle_cmd.c index 3a31bcf..53b7e49 100644 --- a/src/executor/handle_cmd.c +++ b/src/executor/handle_cmd.c @@ -6,17 +6,24 @@ /* By: maiadegraaf str, NULL); + free_arr(cmd->str); + cmd->str = ft_split(joined_str, ' '); + free(joined_str); i = 0; while (tools->paths[i]) { diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index c44f581..4c399fe 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/15 16:01:16 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/15 16:01:51 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ int reset_tools(t_tools *tools) free(tools->pid); implement_tools(tools); tools->reset = true; - // system("leaks minishell"); + system("leaks minishell"); minishell_loop(tools); return (1); } diff --git a/src/utils/t_heredoc_utils.c b/src/utils/t_heredoc_utils.c index a73f354..a3eca21 100644 --- a/src/utils/t_heredoc_utils.c +++ b/src/utils/t_heredoc_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/04/14 11:31:36 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/15 16:06:32 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -68,6 +68,7 @@ void ft_heredocclear(t_heredoc **lst) { tmp = (*lst)->next; free((*lst)->del); + (*lst)->del = NULL; free(*lst); *lst = tmp; } From 801a9653e18d3c75fe486ed88d1364d783e542d5 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Fri, 15 Apr 2022 16:32:38 +0200 Subject: [PATCH 146/163] fix heredoc leak --- includes/color.h | 4 ++++ src/executor/heredoc.c | 12 ++++++------ src/utils/minishell_loop.c | 4 ++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/includes/color.h b/includes/color.h index 23fb27f..452d4e8 100644 --- a/includes/color.h +++ b/includes/color.h @@ -30,4 +30,8 @@ # define WELCOME_MSG "\033[96m╔════════════════════════════════════════════════════════════════════════════════╗\n║ ║\n║ \033[95m█▀█ █▀▀ █▀▀   █▀█ █▀█ █▀▀ █▀ █▀▀ █▄░█ ▀█▀ █▀ \033[96m║\n║ \033[95m█▀▀ █▀░ █▄▄   █▀▀ █▀▄ ██▄ ▄█ ██▄ █░▀█ ░█░ ▄█ \033[96m║\n║ ║\n║ \033[35m ███╗░░░███╗██╗███╗░░██╗██╗░██████╗██╗░░██╗███████╗██╗░░░░░██╗░░░░░ \033[96m║\n║ \033[35m ████╗░████║██║████╗░██║██║██╔════╝██║░░██║██╔════╝██║░░░░░██║░░░░░ \033[96m║\n║ \033[35m ██╔████╔██║██║██╔██╗██║██║╚█████╗░███████║█████╗░░██║░░░░░██║░░░░░ \033[96m║\n║ \033[35m ██║╚██╔╝██║██║██║╚████║██║░╚═══██╗██╔══██║██╔══╝░░██║░░░░░██║░░░░░ \033[96m║\n║ \033[35m ██║░╚═╝░██║██║██║░╚███║██║██████╔╝██║░░██║███████╗███████╗███████╗ \033[96m║\n║ \033[35m ╚═╝░░░░░╚═╝╚═╝╚═╝░░╚══╝╚═╝╚═════╝░╚═╝░░╚═╝╚══════╝╚══════╝╚══════╝ \033[96m║\n║ ║\n╚════════════════════════════════════════════════════════════════════════════════╝" +# define HEREDOC_MSG "\033[1;34m> \033[0m" + +# define READLINE_MSG "\033[1;36mminishell\033[34m$ \033[0m" + #endif \ No newline at end of file diff --git a/src/executor/heredoc.c b/src/executor/heredoc.c index 4f9a86e..c653c43 100644 --- a/src/executor/heredoc.c +++ b/src/executor/heredoc.c @@ -6,7 +6,7 @@ /* By: maiadegraaf del); fd = open(file_name, O_CREAT | O_RDWR | O_TRUNC, 0644); - ft_putstr_fd("\033[1;34m> \033[0m", STDERR_FILENO); + ft_putstr_fd(HEREDOC_MSG, STDERR_FILENO); line = get_next_line(STDIN_FILENO); while (line && (ft_strncmp(heredoc->del, line, ft_strlen(line) - 1) - || ft_strncmp(heredoc->del, line, del_len)) - && !g_global.stop_heredoc) + || line[0] == '\n') && !g_global.stop_heredoc) { if (quotes == false) line = send_expander(tools, line); write(fd, line, ft_strlen(line)); free(line); - ft_putstr_fd("\033[1;34m> \033[0m", STDERR_FILENO); + ft_putstr_fd(HEREDOC_MSG, STDERR_FILENO); line = get_next_line(STDIN_FILENO); } if (!line) ft_putstr_fd("\n", STDERR_FILENO); if (g_global.stop_heredoc || !line) return (EXIT_FAILURE); + free(line); close(fd); return (EXIT_SUCCESS); } @@ -87,7 +87,7 @@ int send_heredoc(t_tools *tools, t_simple_cmds *cmd) if (cmd->hd_file_name) free(cmd->hd_file_name); num = ft_itoa(i++); - cmd->hd_file_name = ft_strjoin("build/.tmp_heredoc_file", num); + cmd->hd_file_name = ft_strjoin("build/.tmp_heredoc_file_", num); free(num); sl = ft_heredoc(tools, cmd->heredoc, cmd->hd_file_name); if (sl) diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 4c399fe..6bf662c 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/15 16:01:51 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/15 16:26:08 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -57,7 +57,7 @@ int minishell_loop(t_tools *tools) { char *tmp; - tools->args = readline("\033[1;36mminishell\033[34m$ \033[0m"); + tools->args = readline(READLINE_MSG); tmp = ft_strtrim(tools->args, " "); free(tools->args); tools->args = tmp; From 0b32950a6ab1d158d13f7906a5d8bd3628e6accc Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Fri, 15 Apr 2022 17:42:02 +0200 Subject: [PATCH 147/163] need to check --- bash_outfile | 0 src/builtins/mini_exit.c | 2 +- src/executor/heredoc.c | 10 ++++------ src/expander/expander.c | 9 +++------ src/signals.c | 13 +++++++++++-- src/utils/minishell_loop.c | 2 +- 6 files changed, 20 insertions(+), 16 deletions(-) create mode 100644 bash_outfile diff --git a/bash_outfile b/bash_outfile new file mode 100644 index 0000000..e69de29 diff --git a/src/builtins/mini_exit.c b/src/builtins/mini_exit.c index 0b4257c..48987fd 100644 --- a/src/builtins/mini_exit.c +++ b/src/builtins/mini_exit.c @@ -6,7 +6,7 @@ /* By: maiadegraaf del); fd = open(file_name, O_CREAT | O_RDWR | O_TRUNC, 0644); - ft_putstr_fd(HEREDOC_MSG, STDERR_FILENO); - line = get_next_line(STDIN_FILENO); + line = readline(HEREDOC_MSG); while (line && (ft_strncmp(heredoc->del, line, ft_strlen(line) - 1) || line[0] == '\n') && !g_global.stop_heredoc) { @@ -42,14 +41,13 @@ int create_heredoc(t_heredoc *heredoc, bool quotes, line = send_expander(tools, line); write(fd, line, ft_strlen(line)); free(line); - ft_putstr_fd(HEREDOC_MSG, STDERR_FILENO); - line = get_next_line(STDIN_FILENO); + line = readline(HEREDOC_MSG); } + free(line); if (!line) ft_putstr_fd("\n", STDERR_FILENO); if (g_global.stop_heredoc || !line) return (EXIT_FAILURE); - free(line); close(fd); return (EXIT_SUCCESS); } diff --git a/src/expander/expander.c b/src/expander/expander.c index cb32f89..335d037 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/15 15:37:25 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/15 17:26:52 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -75,10 +75,8 @@ char *detect_dollar_sign(t_tools *tools, char *str) j += loop_if_dollar_sign(tools, str, &tmp, j); if (str[j + 1] == '?') { - if (tools->pipes != 0) - tmp = ft_itoa(tools->pid[tools->pipes]); - else - tmp = char_to_str('0'); + free(tmp); + tmp = ft_itoa(g_global.error_num); j += ft_strlen(tmp) + 1; } else @@ -109,7 +107,6 @@ char **expander(t_tools *tools, char **str) free(str[i]); str[i] = tmp; } - printf("%d\n\n", ft_strncmp(str[0], "export", ft_strlen(str[0]) - 1)); if (ft_strncmp(str[0], "export", ft_strlen(str[0]) - 1) != 0) { str[i] = delete_quotes(str[i], '\"'); diff --git a/src/signals.c b/src/signals.c index 3e7bc7c..0148fde 100644 --- a/src/signals.c +++ b/src/signals.c @@ -6,20 +6,28 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/04/08 14:35:54 by fpolycar #+# #+# */ -/* Updated: 2022/04/14 17:02:24 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/15 17:17:36 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" +int event(void) +{ + return (EXIT_SUCCESS); +} + void sigint_handler(int sig) { - ft_putstr_fd("\n", STDERR_FILENO); if (g_global.in_heredoc) { g_global.stop_heredoc = 1; + rl_replace_line("", 0); + rl_redisplay(); + rl_done = 1; return ; } + ft_putstr_fd("\n", STDERR_FILENO); rl_on_new_line(); rl_replace_line("", 0); rl_redisplay(); @@ -28,5 +36,6 @@ void sigint_handler(int sig) void init_signals(void) { + rl_event_hook = event; signal(SIGINT, sigint_handler); } diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 6bf662c..2bc0e98 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/15 16:26:08 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/15 17:23:15 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ From 2b466a6104476333912c9b799f32dddd1c3182c4 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Fri, 15 Apr 2022 18:03:38 +0200 Subject: [PATCH 148/163] cleaned up a bit | discovered << bug w/empty line --- a.out.dSYM/Contents/Info.plist | 20 -------------------- a.out.dSYM/Contents/Resources/DWARF/a.out | Bin 22339 -> 0 bytes bash_outfile | 0 minishell.dSYM/Contents/Info.plist | 20 -------------------- src/executor/handle_cmd.c | 3 +-- src/executor/heredoc.c | 6 +++--- src/utils/minishell_loop.c | 4 ++-- 7 files changed, 6 insertions(+), 47 deletions(-) delete mode 100644 a.out.dSYM/Contents/Info.plist delete mode 100644 a.out.dSYM/Contents/Resources/DWARF/a.out delete mode 100644 bash_outfile delete mode 100644 minishell.dSYM/Contents/Info.plist diff --git a/a.out.dSYM/Contents/Info.plist b/a.out.dSYM/Contents/Info.plist deleted file mode 100644 index 3679a65..0000000 --- a/a.out.dSYM/Contents/Info.plist +++ /dev/null @@ -1,20 +0,0 @@ - - - - - CFBundleDevelopmentRegion - English - CFBundleIdentifier - com.apple.xcode.dsym.a.out - CFBundleInfoDictionaryVersion - 6.0 - CFBundlePackageType - dSYM - CFBundleSignature - ???? - CFBundleShortVersionString - 1.0 - CFBundleVersion - 1 - - diff --git a/a.out.dSYM/Contents/Resources/DWARF/a.out b/a.out.dSYM/Contents/Resources/DWARF/a.out deleted file mode 100644 index 7d3a242d8f977046a19411d18c43444a27645b57..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 22339 zcmeHP32>anmF|E3>Cs3dk46W!j1LJcV;kEV$uh<^wvn)8%Oe{rJ_?L&eny&+G@tbl2euZeu8j4>QiHD%ZzJ%bP?=LmyO8NHL3Kq&_%YuC20jn<=jD;n( zP`-nTpY}Ia&zNavP>?1+UJ6}XR(so)Hf#09^Eb<)tx_(Ac%p}R>Oih(S^bHe)=tcC zzm1t)E*J0>wmi#;Nk}<1qDBe3xzWuFIKK&k1Q9D0d*CDu$w`+cT4=8@!f?EFA z`PtASNqXCt)tfDMx zY}R^>l<#EQNqURL5^>Ez`iX`Z+pqYY$PQ3Z-eXxkkSg~S@;mSu`Q`fa_3O^>>S$l5 z9sen12Xan&RD6R{9?4~wjrcqYgF~JR+BU7B67qRb(KRVp#jEE?xC9aTiAQByRw&#v z)Ne&2aq)(ph~K@sgVlUh`NBx^8yxD1|D?ypFTH1QJioQC zNq%2bozPnJcc1iVe!*lg-X9kJmZ!hhj!1qZO1{@xKZzUZU+PbFm*ebl@vkJmY3c>P z_`H+j7wqXthKI-7ch6gr-$#ngp7kfk59600EVRe)uO+{Bwd-jVzo6q`evPmZ5(mI>s0N%7mDI?I<%l3$|d(q5{$ zP`$P@WUcofXd&}`NBI~loo7qk@DCLnT_)j|6#TN{ zcSON&w8;E76`WS~d|$y2D%b%Br~vL5Hgpg%uY!F`Bz&rZYhhT*pQqqg8zt;l@X~W6 zjF|;(G&EurIfUs|Z#t38$Ako?k>+t0{WdnMc#(=*RJ<5-6uD_37iofeA{=oTPuU25 z6JWu80&K3X9_$%}llgVoaJIVa*j!z9Y_2XlR@7y0SjtL<`y(l=xx*o5^@r0|99vUj zCDWJ+4inOf4Orpb;ohNi*cu22AtAGRPzW131HmL_|6%NOsAM1QdW6DJX7#0k3-|7_ z;tEKHgJ2+*NR-)+J*(uUiAY?K4p99GtoV_W4960~Hp&$7&r+gPXlPInA{hbkqmF)G)&yV<;%LehqF`R51AR6`2u(4MjJ}-6X@7zv@Sz++% zviEq4E5qjnI5T5o+-+88xGTV`U0s~H%xD0Z8i6ST#(^B>I?Nqqj(v_AUb&CEfx%y; z&NMF$aEEz%hEI0A!^@FVmfcdsy!u5p>OGkmHmV{RWaX9vvf0fOrS zyc8S=xIACsY zA2nM<$qmOHPH5c}hIn>DL-^N6dXmB99)EjyI2=t3hLir)I2{~}ht}h`>y|_!n(}XM z?@IYM1ba7bzOXSB+T|ZgCH=~P{$Mi3J}-~do|NBGrxE2>U3~b3-SJ4YbO>1AIwNf<_- zd?CZQ%HG4fPrZ{#oeQ)|zFIxTCqsP!a~Ek6w&Ji3{J5HE0(@$K*Nfj-89rx>FAVVc z0p1ia2gbN*)=Fykh2_qt$Ohm7oI@Ywl5+ll`yfV_7;R|m$Y|@6Ft9#^ zal;)vfKYiQ>??keu7bq;@%3Ws)JI|OH5RuyUe{>2nPe8^MON87e0;JO%ipK&+HQY z4mv4_N*+v2Db0wMOvWqIl;Mj-pL7PgsxguXs&ToxTmkdY)frHmKVjf4%?wvZ?!;!M zR?vGjE5J_^l|jE`@&NaupF_oD>@pC**ptQ^dkIDzC&O8^35JinlU0o6f!0tu5>3Ge z;4I_=`uH9Bz^pQN8)gy#-XbP4c*WNW6PE-aj&@Y%Xn~hZf>ZcV2ttyJh$f&RvI)F& zqs^fzL0SV+IFBTi3PPJ<58dVx5TLx6zK||RedHcCcjxRq46z^Z5;?Ts#e!wXfK%mL zgyO;^ycRg}r}B)LTFpZeeDm6)UcAYeLygh22|h77V0KamxD<_o#AKxj8@Ca%3Hy|h zg0g-sWt%ExgDBem^!A{)1?ic4c|A(D1bB0RugLHj6N*LsC`L1Mtnzi#vdk1-`)LwQ zq&Lw`+#$LNoZ9@1o|E$#eWX+N8`qK!>E;o9NEi z{aV-u=ULSS*GahY5(1Px==gO4(l{6?4=~ollHzb7(BfOBgHe$?0pXCV>hkH-B0Vz@ z9PGQue;O$qs#W>oC?g<65Op&413+{uOO6@hd?OLi*H8zWkIS-;B9$YcBa-w`XXIf2 ziTs-&7)fA1xp@oV+~LjRfO10;0LD5H94vwRZUGcm>~kkKsZ}V^YdhL`Orq>(IDui? z5FG3=To~+8a^&@ZtPA<_xasvs5l?+~lp0P_WeTPV5TD zQ*0-Ip?E6N9}kD>Bk?o~p?W&2%61_^h|3VCL5^h%%OFifDLoq;Q&>>M=%Q9{ER&I1Q#Ko-btV*ptg-BirE!~h_+_m$Gq9owUqynNKo1>Z&AAfkAX`Sz-UtlQ-ho&k?m|LUeF(|IsviSx zeAPgz7HFTuNpZ=K-UG$&TSyl2`&;0~^LqqX-GcQ<=Un;o@dYbZC-`aCk1KOuj##um zN21+3TjhC*mPiPhbKDFdQF^T+xd^dkwQbtew$0kS?xGHB%eL+g%c7DDhvrd~9(hkz z?@qMhZJg+l^yI>lUN2r-Cn5$;(o^pt>GdLS1!S7&k@RFZNw3!)A5KIJoTR6|Mbhgj z^gW+QPYRdxdMf*zrE-RA z2jc%gTq&@OU5D6i6$f<}4O7k#c1EU6vX#O*PP4lOeW4?l4&YVXPl{mRBz5}2U zdmF!w3JV5sIli*{cUh>!akI|_tfP|h81F<|C~+G^H)3U!Q%fC^%N#vmoFc%a1os=$ z1o#I~B6pu5nAQ@T5_5E~QAfF_QrQyM=o`#6tNwJtOj86OGEwbx1-{GXQ0@$(XIx*S z=oV7=DW_3!`ZwYv1ZkoUvZQ2`FbAbD&D6CqH+7}X#rSa<&Si+W(@nIc)Qhe}FqV~d zAiB_)@7aKuL@*XI&nCpvsK!RO=R%$K`#d|5t|xp`t%okQ+(_x>dQTc@QFhT>&u*P= zY4VI9P0|{RmwNsZG4;I0St~p@X#A2^&n-xQK=`veJzv!{OS_qy7`my8FQchflW~sc zKEy(-mCSPp>0v6i%I%>$Fru1seV%_pT8PzJ>vI`jN5a7RF5%y4_&MOxH4vs~2lCgJORq9&29 zuVtmw%hQz_qt;EVoqt5yVYsQBn@m(n4dhgEHKXo1|7EB~MklcJGF*=$b%5<%j@J$t ziJio!VgST&Z8vGlsJIqTiQ_9ylGdoC;fR&eIMxqZv#ykeuQ-y9HO{LLmAKtE08>HZ zSH9n{Ckcf>f`b<;7lW;qj+95z**iEVo%C`WUOUc6d>2Tgg2p-sS^uzrZjn&!V|K{* zgn;fs9va}54IqeVSNHQ+ER&<&V}ENXzg$92=l zqIi4l8Ajq+AsbnjTJbXh?UzvPf7l`4>jEO%q7~(`6^{U7?%VNGK~Bd;GmO~#LMSEvpfm!Bk4{h;p_1~bOQe6IU{p!* zZ;P>qqOA_*#Ja*%Tvx-|!=cBaT92+FOI z$|XubzSD)|3My|PsG1<=)pzeC|3#E@W*+}D^Z0A1Mwn7i5`R(K0?mH~aakz%bA30C z{(p-R>!h6aA*3O()OEY!KOp265IBZ}jB6yvo9j1IxwQ(KzMY^>1^IdiT1Px#Bk5ED zQMZ0IXbz=3*G)Zd1tHiP!XnPS0+)i3w#5r>X$$)xpXWn5qaJLG*k6$h75P3SA>~6N zk9_DGM5#SWN?Sz}IOMs7m8uD1T;FkY3jPPF)I81qdszsYiatcpe8r z$}z5QK^_BO#C}9MZAwO;;3y!{q2Rb$OC>nAD~Jh>9fG5N8!v{4`I>wQ%(W&wITwI5S~bBT_*Gx*W{lCEPn zaY*1(5sX4*z`;isZH)>l1Es0Hhw>QL*Fi&6Dpo^ubwuZaxkPuMA-Cw01;n_%#Cb%N zI*m%rRi&m2Y3ItCZHW>sUf+~y-b$ki-G#$xtgAM?9?x>mInc8?mzmz_xPj|zKw_200~~Ke=_*%i zt?4DAIaAkFpF0(eFT}(pRS;mYAD6p1V$nyu z^j3l1CI}|`Q}`&I9!Oy)Wq1%v;M|6R*dD-*SNvzO!OKD{5e-=s4@U4Wcxc~6?7a*| z2ou7V2L9Jt!HxsnV(l4kU{gpsoQYmxZikThF%hb_0eChkU0 zw7=20xvi7>disxzRS4w>^bpQq=t%Kk*2w5TWf2Yi<0k5Rhz|eofO!!{=1r;i#r3Zb zKeNv`zPXAYU+CNP(eSz{70dTub^h{SCYIJ7Ts3mm-uAzrb<61KQ=V_wx^&3}zDuwE z`jz+XOS*rOSh?b@+p2mFKW7|&8N7}nyo*4idksPZ!eWGWge?dmgfzllgxe7AMtBh6 z9}r$fIEo6(M!SRSW0^&)Sy3ivQ7l@Byu%t)Ky%7IYHkXMx3*ZN+(Rjp)3Gum# zUd%NCpQ7k#vjEkK?PNrZwgCthGhoDGxJ7&(DG-ks5)mI#uozeY?^dvwa{zWKm=^TJ zubzJJh*<&RH&uJYj0LczU@=z(Jf!3k%Mif#Va%jQ+g!05XLGu)qiKj`B?5*b@em90 z3lmbjzkBG&8NYe*%Cml7vi+52@{NTMxE*0T z0xijDv3n5V5rpRvTv)HnK)43s0K)eW{t@A0ge9;Y?QM`rX - - - - CFBundleDevelopmentRegion - English - CFBundleIdentifier - com.apple.xcode.dsym.minishell - CFBundleInfoDictionaryVersion - 6.0 - CFBundlePackageType - dSYM - CFBundleSignature - ???? - CFBundleShortVersionString - 1.0 - CFBundleVersion - 1 - - diff --git a/src/executor/handle_cmd.c b/src/executor/handle_cmd.c index b7ee739..aab23ff 100644 --- a/src/executor/handle_cmd.c +++ b/src/executor/handle_cmd.c @@ -6,7 +6,7 @@ /* By: maiadegraaf str, NULL); free_arr(cmd->str); cmd->str = ft_split(joined_str, ' '); - printf(">%s<\n", joined_str); free(joined_str); while (tools->paths[i]) { diff --git a/src/executor/heredoc.c b/src/executor/heredoc.c index 4858285..ebf7c95 100644 --- a/src/executor/heredoc.c +++ b/src/executor/heredoc.c @@ -6,7 +6,7 @@ /* By: maiadegraaf del); fd = open(file_name, O_CREAT | O_RDWR | O_TRUNC, 0644); line = readline(HEREDOC_MSG); - while (line && (ft_strncmp(heredoc->del, line, ft_strlen(line) - 1) - || line[0] == '\n') && !g_global.stop_heredoc) + while (line && ft_strncmp(heredoc->del, line, ft_strlen(line)) + && !g_global.stop_heredoc) { if (quotes == false) line = send_expander(tools, line); diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 2bc0e98..f0ad757 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/15 17:23:15 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/15 17:50:24 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ int reset_tools(t_tools *tools) free(tools->pid); implement_tools(tools); tools->reset = true; - system("leaks minishell"); + // system("leaks minishell"); minishell_loop(tools); return (1); } From dc55b7694fa427a919c958966691bcaeac5fa5d0 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Mon, 18 Apr 2022 11:09:11 +0200 Subject: [PATCH 149/163] newline heredoc --- src/executor/heredoc.c | 6 +++--- src/utils/utils.c | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/executor/heredoc.c b/src/executor/heredoc.c index ebf7c95..435913b 100644 --- a/src/executor/heredoc.c +++ b/src/executor/heredoc.c @@ -6,7 +6,7 @@ /* By: maiadegraaf del); fd = open(file_name, O_CREAT | O_RDWR | O_TRUNC, 0644); line = readline(HEREDOC_MSG); - while (line && ft_strncmp(heredoc->del, line, ft_strlen(line)) - && !g_global.stop_heredoc) + while (line && (ft_strncmp(heredoc->del, line, ft_strlen(line) - 1) + || line[0] == '\n') && !g_global.stop_heredoc) { if (quotes == false) line = send_expander(tools, line); diff --git a/src/utils/utils.c b/src/utils/utils.c index 7292362..97ef6e9 100644 --- a/src/utils/utils.c +++ b/src/utils/utils.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/02/21 11:17:26 by fpolycar #+# #+# */ -/* Updated: 2022/03/24 16:08:31 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 10:21:28 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ From 6e75d9ce475560ea139349e468183265c321e77e Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Mon, 18 Apr 2022 12:30:36 +0200 Subject: [PATCH 150/163] garbage value end of export --- src/builtins/mini_env.c | 4 ++-- src/builtins/mini_export.c | 11 ++++++----- src/utils/minishell_loop.c | 4 ++-- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/builtins/mini_env.c b/src/builtins/mini_env.c index b95a60f..1fdf495 100644 --- a/src/builtins/mini_env.c +++ b/src/builtins/mini_env.c @@ -6,7 +6,7 @@ /* By: maiadegraaf envp[i]) { diff --git a/src/builtins/mini_export.c b/src/builtins/mini_export.c index e6295ed..11dc955 100644 --- a/src/builtins/mini_export.c +++ b/src/builtins/mini_export.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:07:21 by fpolycar #+# #+# */ -/* Updated: 2022/04/15 16:03:30 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 12:29:21 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -94,8 +94,10 @@ char **add_var(char **arr, char *str) size_t i; i = 0; - if (str[equal_sign(str)] == '"' || str[equal_sign(str)] == '\'') - delete_quotes_value(str); + if (str[equal_sign(str)] == '\"') + delete_quotes(str, '\"'); + if (str[equal_sign(str)] == '\'') + delete_quotes(str, '\''); while (arr[i] != NULL) i++; rtn = ft_calloc(sizeof(char *), i + 2); @@ -117,8 +119,7 @@ int mini_export(t_tools *tools, t_simple_cmds *simple_cmd) else { while (simple_cmd->str[i]) - { - printf("%s\n", simple_cmd->str[i]); + { if (check_parameter(simple_cmd->str[i]) == 0 && variable_exist(tools, simple_cmd->str[i]) == 0) { diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 2bc0e98..a85dd91 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -/* Updated: 2022/04/15 17:23:15 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 12:21:31 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -34,7 +34,7 @@ int reset_tools(t_tools *tools) free(tools->pid); implement_tools(tools); tools->reset = true; - system("leaks minishell"); + // system("leaks minishell"); minishell_loop(tools); return (1); } From 4c2d3241213155aa5c2a8d37f87e171f2da159dd Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Mon, 18 Apr 2022 13:03:02 +0200 Subject: [PATCH 151/163] added some error messages and cmd paths --- Makefile | 1 + includes/error.h | 5 +- includes/executor.h | 2 +- src/error/error_handeling.c | 27 ++- src/executor/executor.c | 23 +-- src/executor/executor_utils.c | 50 ++++++ src/executor/handle_cmd.c | 20 +-- src/parser/handle_redirections.c | 6 +- src/parser/parser.c | 43 +---- tester.sh | 281 +++++++++++++++++++++++++++++++ 10 files changed, 377 insertions(+), 81 deletions(-) create mode 100644 src/executor/executor_utils.c create mode 100755 tester.sh diff --git a/Makefile b/Makefile index fb71346..586a300 100644 --- a/Makefile +++ b/Makefile @@ -47,6 +47,7 @@ src = src/main.c \ src/executor/executor.c \ src/executor/handle_cmd.c \ src/executor/heredoc.c \ + src/executor/executor_utils.c \ src/expander/expander.c \ src/expander/expanders_utils.c diff --git a/includes/error.h b/includes/error.h index 9012624..64c6f42 100644 --- a/includes/error.h +++ b/includes/error.h @@ -6,7 +6,7 @@ /* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:17:39 by mgraaf #+# #+# */ -/* Updated: 2022/04/14 14:58:26 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/18 12:49:58 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/error/error_handeling.c b/src/error/error_handeling.c index e9f80ff..b620da3 100755 --- a/src/error/error_handeling.c +++ b/src/error/error_handeling.c @@ -6,7 +6,7 @@ /* By: maiadegraaf lexor_list); ft_error(error, tools); } +int cmd_not_found(char *str) +{ + ft_putstr_fd("minishell: ", STDERR_FILENO); + ft_putstr_fd(str, STDERR_FILENO); + ft_putstr_fd(": command not found\n", STDERR_FILENO); + return (127); +} + /** * @brief * Finds corresponding error and frees args; diff --git a/src/executor/executor.c b/src/executor/executor.c index ec25e6d..f0c5724 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,33 +6,12 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/04/15 16:21:45 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 12:50:16 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "executor.h" -char *join_split_str(char **split_str, char *new_str) -{ - char *tmp; - char *add_space; - int i; - - tmp = ft_strdup(split_str[0]); - i = 1; - while (split_str[i]) - { - new_str = tmp; - add_space = ft_strjoin(new_str, " "); - free(new_str); - tmp = ft_strjoin(add_space, split_str[i]); - free(add_space); - i++; - } - new_str = tmp; - return (new_str); -} - t_simple_cmds *call_expander(t_tools *tools, t_simple_cmds *cmd) { t_lexor *start; diff --git a/src/executor/executor_utils.c b/src/executor/executor_utils.c new file mode 100644 index 0000000..ccda270 --- /dev/null +++ b/src/executor/executor_utils.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* executor_utils.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/04/18 12:45:05 by mgraaf #+# #+# */ +/* Updated: 2022/04/18 12:50:20 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "executor.h" + +char *join_split_str(char **split_str, char *new_str) +{ + char *tmp; + char *add_space; + int i; + + tmp = ft_strdup(split_str[0]); + i = 1; + while (split_str[i]) + { + new_str = tmp; + add_space = ft_strjoin(new_str, " "); + free(new_str); + tmp = ft_strjoin(add_space, split_str[i]); + free(add_space); + i++; + } + new_str = tmp; + return (new_str); +} + +char **resplit_str(char **double_arr) +{ + char **ret; + char *joined_str; + + joined_str = join_split_str(double_arr, NULL); + free_arr(double_arr); + ret = ft_split(joined_str, ' '); + free(joined_str); + joined_str = join_split_str(ret, NULL); + free_arr(ret); + ret = ft_split(joined_str, ' '); + free(joined_str); + return (ret); +} diff --git a/src/executor/handle_cmd.c b/src/executor/handle_cmd.c index aab23ff..2f74c3a 100644 --- a/src/executor/handle_cmd.c +++ b/src/executor/handle_cmd.c @@ -6,29 +6,24 @@ /* By: maiadegraaf str, NULL); - free_arr(cmd->str); - cmd->str = ft_split(joined_str, ' '); - free(joined_str); i = 0; - joined_str = join_split_str(cmd->str, NULL); - free_arr(cmd->str); - cmd->str = ft_split(joined_str, ' '); - free(joined_str); + cmd->str = resplit_str(cmd->str); + if (!access(cmd->str[0], F_OK)) + execve(cmd->str[0], cmd->str, tools->envp); while (tools->paths[i]) { mycmd = ft_strjoin(tools->paths[i], cmd->str[0]); @@ -37,10 +32,7 @@ int find_cmd(t_simple_cmds *cmd, t_tools *tools) free(mycmd); i++; } - ft_putstr_fd("minishell: ", STDERR_FILENO); - ft_putstr_fd(cmd->str[0], STDERR_FILENO); - ft_putstr_fd(": command not found\n", STDERR_FILENO); - return (127); + return (cmd_not_found(cmd->str[0])); } void handle_cmd(t_simple_cmds *cmd, t_tools *tools) diff --git a/src/parser/handle_redirections.c b/src/parser/handle_redirections.c index 9b13a44..b9d1ef1 100644 --- a/src/parser/handle_redirections.c +++ b/src/parser/handle_redirections.c @@ -6,7 +6,7 @@ /* By: maiadegraaf next; if (!tmp || tmp->token == PIPE) return ; - if (!tmp->next || !tmp->next->str) + if (!tmp->next) parser_error(0, parser_tools->tools, parser_tools->lexor_list); + if (tmp->next->token) + parser_double_token_error(parser_tools->tools, parser_tools->lexor_list); if (tmp->token == LESS_LESS) handle_heredoc(parser_tools, tmp); else if ((tmp->token >= GREAT diff --git a/src/parser/parser.c b/src/parser/parser.c index 7f59213..86d0afd 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/04/14 12:02:06 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/18 12:54:30 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -53,8 +53,8 @@ int *parser(t_tools *tools) { if (tools->lexor_list && tools->lexor_list->token == PIPE) ft_lexordelone(&tools->lexor_list, tools->lexor_list->i); - // if (!tools->lexor_list) - // parser_error(0, tools, tools->lexor_list); + if (!tools->lexor_list) + parser_error(0, tools, tools->lexor_list); parser_tools = init_parser_tools(tools->lexor_list, tools); node = initialize_cmd(&parser_tools); if (!node) @@ -67,40 +67,3 @@ int *parser(t_tools *tools) } return (0); } - -void print_parser(t_simple_cmds simple_cmds) -{ - int i = 0; - - t_simple_cmds *tmp = &simple_cmds; - while (tmp) - { - printf("\n>>>%i<<<\n", i++); - if (*tmp->str) - { - while (*tmp->str) - { - printf("%s\n", *tmp->str++); - } - } - printf("num redirections = %d\n", simple_cmds.num_redirections); - if (tmp->redirections) - printf("\nredirections:\n"); - while (tmp->redirections) - { - printf("%s\t", tmp->redirections->str); - if (tmp->redirections->token == GREAT) - printf("GREAT\n"); - else if (tmp->redirections->token == GREAT_GREAT) - printf("GREAT_GREAT\n"); - else if (tmp->redirections->token == LESS) - printf("LESS\n"); - else if (tmp->redirections->token == LESS_LESS) - printf("LESS_LESS\n"); - tmp->redirections = tmp->redirections->next; - } - if (tmp->builtin) - printf("BUILTIN :)\n"); - tmp = tmp->next; - } -} diff --git a/tester.sh b/tester.sh new file mode 100755 index 0000000..62a4965 --- /dev/null +++ b/tester.sh @@ -0,0 +1,281 @@ +#!/bin/bash +#set -x +#set -e + +export ASAN_OPTIONS='detect_leaks=1' +export LSAN_OPTIONS='suppressions=leak_suppression.txt print_suppressions=0' + +function check() +{ + if [ $2 -ne $3 ]; then + echo "found exit status difference for: \"$1\""; + echo "bash exit status:" $3; + echo "minishell exit status:" $2; + fi + diff minishell_out bash_out || { echo "std output test failed for: \"$1\""; echo -e "\n\n\n";} + diff minishell_err bash_err || { echo "error output test failed for: \"$1\""; echo -e "\n\n\n";} + diff minishell_outfile bash_outfile || { echo "outfile of output test failed for: \"$1\""; echo -e "\n\n\n";} +} + +function shell_test() +{ + > minishell_outfile + > bash_outfile + export OUTFILE=minishell_outfile + echo -e $1 | minishell > minishell_out 2> minishell_err + export minishell_status=$? + export OUTFILE=bash_outfile + echo -e $1 | bash > bash_out 2> bash_err + export bash_status=$? + check "$1" $minishell_status $bash_status +} + +function environment_test() +{ + export minishell_status=0 + export bash_status=0 + > minishell_outfile + > bash_outfile + export OUTFILE=minishell_outfile + echo -e $1 | minishell 2> minishell_err | sort | grep -v -f environment_exceptions > minishell_out + export minishell_status=$? + export OUTFILE=bash_outfile + echo -e $1 | bash 2> bash_err | sort | grep -v -f environment_exceptions > bash_out + export bash_status=$? + check "$1" $minishell_status $bash_status +} + +#test by hand: +########## +# cat +# -\ +# signals in here_doc +# cd < minishell.c - +########## + +#make DEBUG=1 -C .. +make re DEBUG=1 -C .. + +#new tests +shell_test 'export bla="s -a"\nl$bla' +shell_test 'export bla="s -a"\nl"$bla"' + +# Generic tests +shell_test '' +shell_test '\n\n\n\n' +shell_test 'ls\nls' +shell_test '/bin/ls' + +# Echo tests +shell_test 'echo' +shell_test 'echo ""' +shell_test 'echo < |' +shell_test 'echo > |' +shell_test 'echo << |' +shell_test 'echo >> |' +shell_test 'echo -nABC' +shell_test 'echo $BLA$BLA=10$BLA' +shell_test 'echo a"bc"d' +shell_test 'echo a"bcd"e' +shell_test "echo 'hoi'"'"bla"' +shell_test 'echo -n' +shell_test 'echo bla -n' +shell_test 'echo -n -n -n' +shell_test 'echo -n -n -n bla' +shell_test 'ls | echo -n bla' +shell_test 'echo -nnnn' +shell_test 'echo -nnnn bla' + +# CD tests +shell_test 'cd\npwd' +shell_test 'cd\npwd\ncd -\npwd' +shell_test 'cd ..\ncd tests\npwd' +shell_test 'cd ..\ncd tests/\npwd' +shell_test 'cd ../../..\npwd' +shell_test 'cd banaan' +shell_test 'cd /usr\npwd' +shell_test 'cd /usr/bin\npwd' +shell_test 'cd /bla' +shell_test 'cd /\npwd' +shell_test 'cd ..\necho $OLDPWD\ncd tests\necho $OLDPWD' +shell_test 'echo $OLDPWDcd /\necho $OLDPWD\ncd tests\necho $OLDPWD' +shell_test 'cd\nunset OLDPWD\necho oldpwd after unset: $OLDPWD\ncd -\npwd' +shell_test 'ls | ls | unset OLDPWD\necho oldpwd after unset: $OLDPWD\ncd -\npwd' +#shell_test 'unset HOME\necho home after unset: $HOME\ncd\npwd' +shell_test 'mkdir TESTDIR\ncd TESTDIR\nrmdir ../TESTDIR\npwd' + +# PWD Tests +shell_test 'pwd' +shell_test 'pwd lalala' + +# Export tests +environment_test 'export' +environment_test 'export a\nexport a=42\nexport' +environment_test 'export a=\nexport a=42\nexport' +environment_test 'export a=\nexport a\nexport' +shell_test 'export =' +shell_test 'export =10' +shell_test 'export "|"=10' +shell_test 'export ">"=10' +shell_test 'export "$"=10' +shell_test 'export "["=10' +shell_test 'export "111"="222"' +environment_test 'export A=bla\nexport' +environment_test 'export 1A=bla\nexport' +environment_test 'export A1=bla\nexport' +environment_test 'export _A=bla\nexport' +environment_test 'export A_=bla\nexport' +environment_test 'export A=bla\nexport A=bloe\nexport' +environment_test 'export A=bla\nexport A=$A-bloe\nexport' +environment_test 'export PATH=$PATH:/banaan/konijn\nexport' +shell_test 'export A$B=10\necho $A$B' +shell_test 'export B=3\necho $A$B' +shell_test 'export B+=3\necho $A$B' +shell_test 'export HOME+=3\necho $HOME' +shell_test 'export HOME=+3\necho $HOME' +environment_test 'export +=3\nexport' +shell_test 'export HOME+=\necho $HOME' +shell_test 'export A$B=10\necho $A$B\nexport B=3\necho $A$B' +environment_test 'export PA=10\nexport' +environment_test 'export a1=10\nunset a1\nexport' +environment_test 'export a=5=5\nexport' + +# unset tests +shell_test 'unset PATH\necho $PATH' +#shell_test 'unset HOME\necho $HOME' +environment_test 'unset $PATH\nexport' +environment_test 'unset a1=10\nexport' +environment_test 'unset $PATH\nexport' +environment_test 'unset PATH\nexport' +environment_test 'unset PPATH\nexport' +environment_test 'unset PATHH\nexport' +environment_test 'unset "PATH "\nexport' +environment_test 'unset " PATH"\nexport' +environment_test 'unset PATH=\nexport' +environment_test 'unset 1a\nexport' +environment_test 'unset a-\nexport' +environment_test 'unset a-\nexport' +environment_test 'unset a_\nexport' + +# ENV tests +environment_test 'env' + +# Exit tests +shell_test "exit" +shell_test "exit 0" +shell_test "exit 1" +shell_test "exit 42" +shell_test "exit 1 1" +shell_test "exit banaan" +shell_test "exit 1000000000" + +# Redirection tests +shell_test 'pwd > $OUTFILE' +shell_test '> $OUTFILE pwd' +shell_test '> $OUTFILE ls -a' +shell_test 'cat test_shell_split.c > $OUTFILE -e' +shell_test 'cat > $OUTFILE < test_shell_split.c -e' +shell_test 'cat < test_shell_split.c > $OUTFILE -e' +shell_test '< test_shell_split.c << bla cat\nasdf\nbla\n' + +# Here doc tests +shell_test 'cat << bla -e\nasdf\n' +shell_test 'cat << bla -e\nasdf\nbla\n' +shell_test 'cat << bla\nasdf\nbla\n' +shell_test '<< bla cat\nasdf\nbla\n' +shell_test '<< bla\nasdf\nbla\n cat' +shell_test '<< bla cat\nasdf\nbla\necho banaan' +shell_test '<< bla cat < test_shell_split.c\nasdf\nbla\n' +shell_test '<< wildcard.c ls -la | wc -w >> $OUTFILE\nwildcard.c' + +# Pipe tests +shell_test 'ls | grep t' +shell_test 'ls|cat -e' +shell_test 'ls|cat -e|cat -e|cat -e|cat -e|cat -e|cat -e|cat -e|cat -e|cat -e|cat -e|cat -e|cat -e|cat -e|cat -e|cat -e|cat -e' +shell_test 'ls | < banaan | ls' +shell_test 'ls | cat > $OUTFILE' +shell_test 'ls < lol.c | grep "a"' +shell_test 'ls < test_shell_split.c | grep "a"' +shell_test '< test_shell_split.c grep a | cat -e' +shell_test '< test_shell_split.c | cat -e > $OUTFILE' +shell_test '< test_shell_split.c | ls | cat -e > $OUTFILE' +shell_test '< test_shell_split.c grep a | > $OUTFILE' +shell_test '< test_shell_split.c grep a | cat -e > $OUTFILE' +shell_test '< test_shell_split.c grep "a"|grep "m" | wc -l|cat -e > $OUTFILE' +shell_test 'ls | cat > $OUTFILE < test_shell_split.c -e' +shell_test 'cat > $OUTFILE < test_shell_split.c -e | ls' +shell_test 'ls | < integration_tests grep "a" | cat -e' +shell_test 'ls | head -n 10 | tail -n 9 | rev | sort | rev | cat -e' +shell_test 'cat | cat | ls\n' + +# Wildcard and dollar extension tests +shell_test "echo '$PATH'" +shell_test 'echo "$ a"' +shell_test 'echo $' +shell_test 'echo $9' +shell_test 'echo $9a' +shell_test 'echo $98a' +shell_test 'echo $PA' +shell_test 'echo $PA:' +shell_test 'echo $PATH:' +shell_test 'echo $HOME' +shell_test 'echo "$HOME"' +shell_test 'echo $HO"ME"' +shell_test 'echo $"HOME"' +shell_test 'echo $ "HOME"' +shell_test 'echo '\''$"HOME"'\' +shell_test 'echo "'\''$HOME'\'\" +shell_test 'echo bla $bla' +shell_test 'echo bla $bla | cat -e' + +# Quote tests +shell_test 'cat test_shell_split.c' +shell_test 'cat test_sh"ell_split.c"' +shell_test 'cat "test_sh"ell_split.c' +shell_test 'cat '"tes't_s'h"'ell_"spl"it.c' +shell_test 'exit "42"' + +# Combination tests +shell_test 'echo bla | export $BLA=bla\necho $BLA' +shell_test 'ls | > tmp_file | ls\nrm tmp_file' +shell_test 'ls | cat << bla\nasdf\nbla\n' +shell_test 'cat << bla | cat\nasdf\nbla\n' +shell_test '<< bla cat | << bloe cat\nfirst\nbla\nsecond\nbloe\n' +shell_test '<< bla cat | << bloe cat\nfirst\nbla\nsecond\nbloe\nls\n' +shell_test 'cat << bla | echo bla\nasdf\nbla\n' + +# Error tests +shell_test '|' +shell_test '<' +shell_test '>' +shell_test '< <' +shell_test '> <' +shell_test '<<' +shell_test '<< <' +shell_test '> > outfile' +shell_test 'ls | >' +shell_test 'ls | > >' +shell_test 'ls | > >>' +shell_test 'ls | <' +shell_test '< | ls' +shell_test 'ls | banaan' +shell_test 'ls | "|"' +shell_test '| ls' +shell_test 'ls |' +shell_test 'ls | | ls' +shell_test 'ls ||| cat' +shell_test 'ls\n\n\nls ||| cat' +shell_test 'ls\n\n\nexit banaan' +shell_test 'ls\n\n\nexport "$"=10' +shell_test 'ls\n\n\ncd banaan' + + +# For the next two tests with multiple output redirections, to the same file, minishell always does the same thing, but bash doesn't. +# Bash seems to open the file from different forks and they put in information at the same time, which causes race conditions. Minishell puts in information in order. + +# shell_test 'rm $OUTFILE | cat >> $OUTFILE << hd1 << hd2 -e | >> $OUTFILE << hd3 | cat < test_shell_split.c << hd4 >> $OUTFILE | >> $OUTFILE echo *$PATH * $PATH "*"$PATH -e\n1\nhd1\n2\nhd2\n3\nhd3\n4\nhd4\n' +# shell_test 'cat > $OUTFILE << hd1 << hd2 -e | >> $OUTFILE << hd3 | cat < test_shell_split.c >> $OUTFILE << hd4 -e\n1\nhd1\n2\nhd2\n3\nhd3\n4\nhd4\n' + +norminette src + +echo integration tests finished! \ No newline at end of file From b06993516bb23b63dfebe2bc3bffe4fc64150421 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Mon, 18 Apr 2022 13:03:27 +0200 Subject: [PATCH 152/163] empty quotes --- src/expander/expanders_utils.c | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/expander/expanders_utils.c b/src/expander/expanders_utils.c index 96e1579..5ab52a5 100644 --- a/src/expander/expanders_utils.c +++ b/src/expander/expanders_utils.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/31 16:08:47 by fpolycar #+# #+# */ -/* Updated: 2022/04/15 16:01:44 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 12:59:41 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -47,6 +47,24 @@ int after_dol_lenght(char *str, int j) return (i); } +size_t quotes_lenght(char *str) +{ + int i; + size_t ret; + + i = 0; + ret = 0; + while (str[i]) + { + if (str[i] == '\'' || str[i] == '\"') + { + ret++; + } + i++; + } + return (ret); +} + char *delete_quotes(char *str, char c) { int i; @@ -56,7 +74,13 @@ char *delete_quotes(char *str, char c) j = 0; while (str[i]) { - if (str[i] == c) + if (str[i] == c + && ft_strlen(str + equal_sign(str)) == quotes_lenght(str)) + { + ft_strlcpy(&str[i], "\'\'", 3); + break ; + } + else if (str[i] == c) { j = 0; while (str[i + j] == c) From 0f0dd971cd4346e169d4b6a6ddca0d59796c3c8a Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Mon, 18 Apr 2022 14:03:33 +0200 Subject: [PATCH 153/163] hotfix path --- src/builtins/mini_cd.c | 27 ++++++++++++++++++++++++++- src/builtins/mini_env.c | 15 +-------------- src/builtins/mini_export.c | 2 +- src/expander/expanders_utils.c | 16 ++++++++-------- src/parser/handle_redirections.c | 6 +++--- src/utils/minishell_loop.c | 6 +----- 6 files changed, 40 insertions(+), 32 deletions(-) diff --git a/src/builtins/mini_cd.c b/src/builtins/mini_cd.c index 6405b9d..6d128bb 100644 --- a/src/builtins/mini_cd.c +++ b/src/builtins/mini_cd.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:17:04 by mgraaf #+# #+# */ -/* Updated: 2022/04/12 15:55:28 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 14:00:54 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -46,6 +46,30 @@ int specific_path(t_tools *tools, char *str) return (ret); } +void add_path_to_env(t_tools *tools) +{ + int i; + char *tmp; + + i = 0; + while (tools->envp[i]) + { + if (!ft_strncmp(tools->envp[i], "PWD=", 4)) + { + tmp = ft_strjoin("PWD=", tools->pwd); + free(tools->envp[i]); + tools->envp[i] = tmp; + } + else if (!ft_strncmp(tools->envp[i], "OLDPWD=", 7) && tools->old_pwd) + { + tmp = ft_strjoin("OLDPWD=", tools->old_pwd); + free(tools->envp[i]); + tools->envp[i] = tmp; + } + i++; + } +} + int mini_cd(t_tools *tools, t_simple_cmds *simple_cmd) { int ret; @@ -63,5 +87,6 @@ int mini_cd(t_tools *tools, t_simple_cmds *simple_cmd) if (ret != 0) return (EXIT_FAILURE); change_path(tools); + add_path_to_env(tools); return (EXIT_SUCCESS); } diff --git a/src/builtins/mini_env.c b/src/builtins/mini_env.c index 1fdf495..c1c3e2e 100644 --- a/src/builtins/mini_env.c +++ b/src/builtins/mini_env.c @@ -6,7 +6,7 @@ /* By: maiadegraaf envp[i]) { - if (!ft_strncmp(tools->envp[i], "PWD=", 4)) - { - tmp = ft_strjoin("PWD=", tools->pwd); - free(tools->envp[i]); - tools->envp[i] = tmp; - } - else if (!ft_strncmp(tools->envp[i], "OLDPWD=", 7) && tools->old_pwd) - { - tmp = ft_strjoin("OLDPWD=", tools->old_pwd); - free(tools->envp[i]); - tools->envp[i] = tmp; - } ft_putendl_fd(tools->envp[i], STDOUT_FILENO); i++; } diff --git a/src/builtins/mini_export.c b/src/builtins/mini_export.c index 11dc955..4769c92 100644 --- a/src/builtins/mini_export.c +++ b/src/builtins/mini_export.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:07:21 by fpolycar #+# #+# */ -/* Updated: 2022/04/18 12:29:21 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 14:00:39 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/expander/expanders_utils.c b/src/expander/expanders_utils.c index 5ab52a5..5af11a9 100644 --- a/src/expander/expanders_utils.c +++ b/src/expander/expanders_utils.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/31 16:08:47 by fpolycar #+# #+# */ -/* Updated: 2022/04/18 12:59:41 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 13:19:00 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -74,13 +74,13 @@ char *delete_quotes(char *str, char c) j = 0; while (str[i]) { - if (str[i] == c - && ft_strlen(str + equal_sign(str)) == quotes_lenght(str)) - { - ft_strlcpy(&str[i], "\'\'", 3); - break ; - } - else if (str[i] == c) + // if (str[i] == c + // && ft_strlen(str + equal_sign(str)) == quotes_lenght(str)) + // { + // ft_strlcpy(&str[i], "\'\'", 3); + // break ; + // } + if (str[i] == c) { j = 0; while (str[i + j] == c) diff --git a/src/parser/handle_redirections.c b/src/parser/handle_redirections.c index b9d1ef1..c277062 100644 --- a/src/parser/handle_redirections.c +++ b/src/parser/handle_redirections.c @@ -6,7 +6,7 @@ /* By: maiadegraaf next) parser_error(0, parser_tools->tools, parser_tools->lexor_list); - if (tmp->next->token) - parser_double_token_error(parser_tools->tools, parser_tools->lexor_list); + // if (tmp->next->token) + // parser_double_token_error(parser_tools->tools, parser_tools->lexor_list); if (tmp->token == LESS_LESS) handle_heredoc(parser_tools, tmp); else if ((tmp->token >= GREAT diff --git a/src/utils/minishell_loop.c b/src/utils/minishell_loop.c index 7fda44f..e59a51b 100644 --- a/src/utils/minishell_loop.c +++ b/src/utils/minishell_loop.c @@ -6,11 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:06:58 by fpolycar #+# #+# */ -<<<<<<< HEAD -/* Updated: 2022/04/18 12:21:31 by fpolycar ######## odam.nl */ -======= -/* Updated: 2022/04/15 17:50:24 by mgraaf ######## odam.nl */ ->>>>>>> maia_3 +/* Updated: 2022/04/18 13:13:36 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ From 6c8e5a32ea3e9afcf013008c6ea4787f182e7ce9 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Mon, 18 Apr 2022 15:52:36 +0200 Subject: [PATCH 154/163] hotfix export --- includes/minishell.h | 3 ++- src/builtins/mini_export.c | 4 ++-- src/builtins/utils_builtins.c | 2 +- src/executor/executor.c | 3 ++- src/expander/expander.c | 4 ++-- src/expander/expanders_utils.c | 14 ++++---------- src/parser/handle_redirections.c | 6 +++--- 7 files changed, 16 insertions(+), 20 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index 1776c83..4112424 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/04/14 16:56:49 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/18 14:26:19 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -44,6 +44,7 @@ void sigint_handler(int sig); void sigquit_handler(int sig); void init_signals(void); char *delete_quotes(char *str, char c); +char *delete_quotes_export(char *str, char c); //builtins int (*builtin_arr(char *str))(t_tools *tools, t_simple_cmds *simple_cmd); diff --git a/src/builtins/mini_export.c b/src/builtins/mini_export.c index 4769c92..77bb52c 100644 --- a/src/builtins/mini_export.c +++ b/src/builtins/mini_export.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:07:21 by fpolycar #+# #+# */ -/* Updated: 2022/04/18 14:00:39 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 15:08:25 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -114,7 +114,7 @@ int mini_export(t_tools *tools, t_simple_cmds *simple_cmd) int i; i = 1; - if (!simple_cmd->str[1]) + if (!simple_cmd->str[1] || simple_cmd->str[1][0] == '\0') mini_env(tools, simple_cmd); else { diff --git a/src/builtins/utils_builtins.c b/src/builtins/utils_builtins.c index 7516404..c0e6a94 100644 --- a/src/builtins/utils_builtins.c +++ b/src/builtins/utils_builtins.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:04:47 by fpolycar #+# #+# */ -/* Updated: 2022/04/15 16:03:41 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 15:37:52 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/executor/executor.c b/src/executor/executor.c index f0c5724..494c8ed 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/04/18 12:50:16 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/18 15:12:03 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -85,6 +85,7 @@ int executor(t_tools *tools) fd_in = STDIN_FILENO; while (tools->simple_cmds) { + printf("%s\n", tools->simple_cmds->str[1]); tools->simple_cmds = call_expander(tools, tools->simple_cmds); if (tools->simple_cmds->next) pipe(end); diff --git a/src/expander/expander.c b/src/expander/expander.c index 335d037..f051dfc 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/15 17:26:52 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 15:51:49 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -73,7 +73,7 @@ char *detect_dollar_sign(t_tools *tools, char *str) j += handle_digit_after_dollar(j, str); if (str[j] == '$' && str[j + 1] != '\"' && str[j + 1] != '?') j += loop_if_dollar_sign(tools, str, &tmp, j); - if (str[j + 1] == '?') + else if (str[j] == '$' && str[j + 1] == '?') { free(tmp); tmp = ft_itoa(g_global.error_num); diff --git a/src/expander/expanders_utils.c b/src/expander/expanders_utils.c index 5af11a9..42fd86b 100644 --- a/src/expander/expanders_utils.c +++ b/src/expander/expanders_utils.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/31 16:08:47 by fpolycar #+# #+# */ -/* Updated: 2022/04/18 13:19:00 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 15:42:30 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -42,14 +42,14 @@ int after_dol_lenght(char *str, int j) i = j + 1; while (str[i] != '\0' && str[i] != '$' && str[i] != ' ' - && str[i] != '\"' && str[i] != '\'') + && str[i] != '\"' && str[i] != '\'' && str[i] != '=' && str[i] != '-') i++; return (i); } -size_t quotes_lenght(char *str) +size_t quotes_lenght(char *str) { - int i; + int i; size_t ret; i = 0; @@ -74,12 +74,6 @@ char *delete_quotes(char *str, char c) j = 0; while (str[i]) { - // if (str[i] == c - // && ft_strlen(str + equal_sign(str)) == quotes_lenght(str)) - // { - // ft_strlcpy(&str[i], "\'\'", 3); - // break ; - // } if (str[i] == c) { j = 0; diff --git a/src/parser/handle_redirections.c b/src/parser/handle_redirections.c index c277062..37c4411 100644 --- a/src/parser/handle_redirections.c +++ b/src/parser/handle_redirections.c @@ -6,7 +6,7 @@ /* By: maiadegraaf next) parser_error(0, parser_tools->tools, parser_tools->lexor_list); - // if (tmp->next->token) - // parser_double_token_error(parser_tools->tools, parser_tools->lexor_list); + if (tmp->next->token) + parser_double_token_error(parser_tools->tools, parser_tools->lexor_list, tmp->next->token); if (tmp->token == LESS_LESS) handle_heredoc(parser_tools, tmp); else if ((tmp->token >= GREAT From afe609fb0c7e6fb8ccbcbdf795d07d0377e96a96 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Mon, 18 Apr 2022 16:07:30 +0200 Subject: [PATCH 155/163] hotfix unset --- src/builtins/mini_unset.c | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/builtins/mini_unset.c b/src/builtins/mini_unset.c index 0863397..688c740 100644 --- a/src/builtins/mini_unset.c +++ b/src/builtins/mini_unset.c @@ -6,7 +6,7 @@ /* By: maiadegraaf str[1]) { - ft_putendl_fd("unset: not enough arguments", STDERR_FILENO); + ft_putendl_fd("minishell: unset: not enough arguments", STDERR_FILENO); return (EXIT_FAILURE); } + while (simple_cmd->str[1][i]) + { + if (simple_cmd->str[1][i++] == '/') + { + ft_putstr_fd("minishell: unset: `", STDERR_FILENO); + ft_putstr_fd(simple_cmd->str[1], STDERR_FILENO); + ft_putendl_fd("': not a valid identifier", STDERR_FILENO); + return (EXIT_FAILURE); + } + } if (equal_sign(simple_cmd->str[1]) != 0) { - ft_putendl_fd("unset: invalid parameter name", STDERR_FILENO); + ft_putendl_fd("minishell: unset: invalid parameter name", + STDERR_FILENO); return (EXIT_FAILURE); } + return (EXIT_SUCCESS); +} + +int mini_unset(t_tools *tools, t_simple_cmds *simple_cmd) +{ + char **tmp; + + if (unset_error(simple_cmd) == 1) + return (EXIT_FAILURE); else { tmp = del_var(tools->envp, simple_cmd->str[1]); From 9f6f74471e7172b3e6d4f407cbd8025cba9e1c78 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Mon, 18 Apr 2022 16:33:33 +0200 Subject: [PATCH 156/163] norminette expander --- Makefile | 3 ++- includes/minishell.h | 4 +++- src/expander/expander.c | 8 ++------ src/expander/expanders_utils2.c | 21 +++++++++++++++++++++ 4 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 src/expander/expanders_utils2.c diff --git a/Makefile b/Makefile index 586a300..692af3b 100644 --- a/Makefile +++ b/Makefile @@ -49,7 +49,8 @@ src = src/main.c \ src/executor/heredoc.c \ src/executor/executor_utils.c \ src/expander/expander.c \ - src/expander/expanders_utils.c + src/expander/expanders_utils.c \ + src/expander/expanders_utils2.c OBJS = $(addprefix $(PATHO), $(notdir $(patsubst %.c, %.o, $(src)))) diff --git a/includes/minishell.h b/includes/minishell.h index 4112424..0a0aba1 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/04/18 14:26:19 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 16:32:16 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -45,6 +45,8 @@ void sigquit_handler(int sig); void init_signals(void); char *delete_quotes(char *str, char c); char *delete_quotes_export(char *str, char c); +int question_mark(char **tmp); + //builtins int (*builtin_arr(char *str))(t_tools *tools, t_simple_cmds *simple_cmd); diff --git a/src/expander/expander.c b/src/expander/expander.c index f051dfc..ee5ba65 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/18 15:51:49 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 16:31:40 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -74,11 +74,7 @@ char *detect_dollar_sign(t_tools *tools, char *str) if (str[j] == '$' && str[j + 1] != '\"' && str[j + 1] != '?') j += loop_if_dollar_sign(tools, str, &tmp, j); else if (str[j] == '$' && str[j + 1] == '?') - { - free(tmp); - tmp = ft_itoa(g_global.error_num); - j += ft_strlen(tmp) + 1; - } + j += question_mark(&tmp); else { tmp2 = char_to_str(str[j++]); diff --git a/src/expander/expanders_utils2.c b/src/expander/expanders_utils2.c new file mode 100644 index 0000000..e10515d --- /dev/null +++ b/src/expander/expanders_utils2.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* expanders_utils2.c :+: :+: */ +/* +:+ */ +/* By: fpolycar +#+ */ +/* +#+ */ +/* Created: 2022/04/18 16:20:01 by fpolycar #+# #+# */ +/* Updated: 2022/04/18 16:32:01 by fpolycar ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" +#include "builtins.h" + +int question_mark(char **tmp) +{ + free(*tmp); + *tmp = ft_itoa(g_global.error_num); + return (ft_strlen(*tmp) + 1); +} From f721efd95e6bc28fd1bfab1e3ef40fd505a4c4ec Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Mon, 18 Apr 2022 16:41:47 +0200 Subject: [PATCH 157/163] rewrote redirections in executor and heredoc --- Makefile | 3 +- includes/builtins.h | 3 +- includes/error.h | 5 +- includes/executor.h | 12 ++-- includes/parser.h | 11 +--- includes/utils.h | 14 ++--- src/builtins/mini_export.c | 20 ++----- src/builtins/mini_unset.c | 7 ++- src/builtins/utils_builtins.c | 14 ++++- src/error/error_handeling.c | 61 ++++++++------------ src/error/ft_error.c | 47 ++++++++++++++++ src/executor/check_redirections.c | 93 ++++++++++++------------------- src/executor/handle_cmd.c | 14 ++--- src/executor/heredoc.c | 57 +++++++++++-------- src/parser/handle_redirections.c | 28 ++-------- src/parser/parser.c | 4 +- src/parser/parser_utils.c | 3 +- src/utils/t_simple_cmds_utils.c | 7 +-- 18 files changed, 197 insertions(+), 206 deletions(-) create mode 100644 src/error/ft_error.c diff --git a/Makefile b/Makefile index 586a300..c59c529 100644 --- a/Makefile +++ b/Makefile @@ -37,12 +37,12 @@ src = src/main.c \ src/builtins/utils_builtins.c \ src/utils/minishell_loop.c \ src/utils/parse_envp.c \ - src/utils/t_heredoc_utils.c \ src/utils/t_lexor_clear_utils.c \ src/utils/t_lexor_utils.c \ src/utils/t_simple_cmds_utils.c \ src/utils/utils.c \ src/error/error_handeling.c \ + src/error/ft_error.c \ src/executor/check_redirections.c \ src/executor/executor.c \ src/executor/handle_cmd.c \ @@ -50,6 +50,7 @@ src = src/main.c \ src/executor/executor_utils.c \ src/expander/expander.c \ src/expander/expanders_utils.c +# src/utils/t_heredoc_utils.c OBJS = $(addprefix $(PATHO), $(notdir $(patsubst %.c, %.o, $(src)))) diff --git a/includes/builtins.h b/includes/builtins.h index ed72a70..adbeaa4 100644 --- a/includes/builtins.h +++ b/includes/builtins.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:20:00 by mgraaf #+# #+# */ -/* Updated: 2022/03/24 13:08:38 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 14:17:35 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -35,5 +35,6 @@ int mini_exit(t_tools *tools, t_simple_cmds *simple_cmd); int mini_continue(t_tools *tools, t_simple_cmds *simple_cmd); size_t equal_sign(char *str); +int check_valid_identifier(char c); #endif \ No newline at end of file diff --git a/includes/error.h b/includes/error.h index 64c6f42..0a58565 100644 --- a/includes/error.h +++ b/includes/error.h @@ -6,7 +6,7 @@ /* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:17:39 by mgraaf #+# #+# */ -/* Updated: 2022/04/18 12:49:58 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/18 16:31:21 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -15,9 +15,10 @@ # include "minishell.h" // check_redirections -int check_outfile(t_lexor *redirections); -int check_infile(t_lexor *redirections); -int handle_redirections(t_simple_cmds *cmd, t_tools *tools); +// int check_outfile(t_lexor *redirections); +// int check_infile(t_lexor *redirections); +// int handle_redirections(t_simple_cmds *cmd, t_tools *tools); +int check_redirections(t_simple_cmds *cmd, t_tools *tools); // executor int executor(t_tools *tools); @@ -32,9 +33,6 @@ void dup_cmd(t_simple_cmds *cmd, t_tools *tools, void single_cmd(t_simple_cmds *cmd, t_tools *tools); // heredoc -int ft_heredoc(t_tools *tools, t_heredoc *heredoc, char *file_name); -int create_heredoc(t_heredoc *heredoc, bool quotes, - t_tools *tools, char *file_name); int send_heredoc(t_tools *tools, t_simple_cmds *cmd); #endif \ No newline at end of file diff --git a/includes/parser.h b/includes/parser.h index 183261a..0dd1606 100644 --- a/includes/parser.h +++ b/includes/parser.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/04/14 14:29:51 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/18 16:32:35 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -23,13 +23,6 @@ typedef enum s_tokens LESS_LESS, } t_tokens; -typedef struct s_heredoc -{ - char *del; - struct s_heredoc *next; - struct s_heredoc *prev; -} t_heredoc; - typedef struct s_lexor { char *str; @@ -44,7 +37,6 @@ typedef struct s_parser_tools t_lexor *lexor_list; t_lexor *redirections; int num_redirections; - t_heredoc *heredoc; struct s_tools *tools; } t_parser_tools; @@ -68,7 +60,6 @@ typedef struct s_simple_cmds char **str; int (*builtin)(t_tools *, struct s_simple_cmds *); int num_redirections; - t_heredoc *heredoc; char *hd_file_name; t_lexor *redirections; struct s_simple_cmds *next; diff --git a/includes/utils.h b/includes/utils.h index 1dfcc16..3692274 100644 --- a/includes/utils.h +++ b/includes/utils.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:36:23 by mgraaf #+# #+# */ -/* Updated: 2022/04/14 11:31:53 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/18 16:33:17 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ char **ft_arrdup(char **arr); int minishell_loop(t_tools *tools); //t_simple_cmds_utils -t_simple_cmds *ft_simple_cmdsnew(char **str, t_heredoc *heredoc, +t_simple_cmds *ft_simple_cmdsnew(char **str, int num_redirections, t_lexor *redirections); void ft_simple_cmdsadd_back(t_simple_cmds **lst, t_simple_cmds *new); void ft_simple_cmds_rm_first(t_simple_cmds **lst); @@ -36,11 +36,11 @@ void ft_lexordelone(t_lexor **lst, int i); void ft_lexorclear(t_lexor **lst); //t_heredoc_utils -t_heredoc *ft_heredocnew(char *del); -void ft_heredocadd_back(t_heredoc **lst, t_heredoc *new); -int ft_heredocsize(t_heredoc *lst); -void ft_heredocclear(t_heredoc **lst); -t_heredoc *ft_heredocfirst(t_heredoc *map); +// t_heredoc *ft_heredocnew(char *del); +// void ft_heredocadd_back(t_heredoc **lst, t_heredoc *new); +// int ft_heredocsize(t_heredoc *lst); +// void ft_heredocclear(t_heredoc **lst); +// t_heredoc *ft_heredocfirst(t_heredoc *map); // int token_reader(t_tools *tools); diff --git a/src/builtins/mini_export.c b/src/builtins/mini_export.c index e6295ed..a722118 100644 --- a/src/builtins/mini_export.c +++ b/src/builtins/mini_export.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:07:21 by fpolycar #+# #+# */ -/* Updated: 2022/04/15 16:03:30 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 14:27:29 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -41,24 +41,15 @@ int check_parameter(char *str) i = 0; if (ft_isdigit(str[0])) - { - ft_putendl_fd("export: not an identifier", STDERR_FILENO); - return (EXIT_FAILURE); - } + return (export_error(str)); if (equal_sign(str) == 0) return (EXIT_FAILURE); if (str[0] == '=') - { - ft_putendl_fd("export: bad assignment", STDERR_FILENO); - return (EXIT_FAILURE); - } + return (export_error(str)); while (str[i] != '=') { - if (str[i] == '|') - { - ft_putendl_fd("export: not valid in this context", STDERR_FILENO); - return (EXIT_FAILURE); - } + if (check_valid_identifier(str[i])) + return (export_error(str)); i++; } return (EXIT_SUCCESS); @@ -118,7 +109,6 @@ int mini_export(t_tools *tools, t_simple_cmds *simple_cmd) { while (simple_cmd->str[i]) { - printf("%s\n", simple_cmd->str[i]); if (check_parameter(simple_cmd->str[i]) == 0 && variable_exist(tools, simple_cmd->str[i]) == 0) { diff --git a/src/builtins/mini_unset.c b/src/builtins/mini_unset.c index 0863397..ee0a3fa 100644 --- a/src/builtins/mini_unset.c +++ b/src/builtins/mini_unset.c @@ -6,7 +6,7 @@ /* By: maiadegraaf str[1]) { - ft_putendl_fd("unset: not enough arguments", STDERR_FILENO); + ft_putendl_fd("minishell: unset: not enough arguments", STDERR_FILENO); return (EXIT_FAILURE); } if (equal_sign(simple_cmd->str[1]) != 0) { - ft_putendl_fd("unset: invalid parameter name", STDERR_FILENO); + ft_putendl_fd("minishell: unset: not a valid identifier", + STDERR_FILENO); return (EXIT_FAILURE); } else diff --git a/src/builtins/utils_builtins.c b/src/builtins/utils_builtins.c index 7516404..594b93d 100644 --- a/src/builtins/utils_builtins.c +++ b/src/builtins/utils_builtins.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/24 16:04:47 by fpolycar #+# #+# */ -/* Updated: 2022/04/15 16:03:41 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 14:25:09 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -52,3 +52,15 @@ char *delete_quotes_value(char *str) free_arr(split_quotes); return (str); } + +int check_valid_identifier(char c) +{ + return (c == '|' || c == '<' || c == '>' || c == '[' || c == ']' + || c == '\'' || c == '\"' || c == ' ' || c == ',' || c == '.' + || c == ':' || c == '/' || c == '{' || c == '}' || c == '+' + || c == '^' || c == '%' || c == '#' || c == '@' || c == '!' + || c == '~' + || c == '=' || c == '-' || c == '?' || c == '&' || c == '*'); +} + +// "\'\"\\$ ,.:/[{]}+=-?&*^%#@!~" \ No newline at end of file diff --git a/src/error/error_handeling.c b/src/error/error_handeling.c index b620da3..1f9379c 100755 --- a/src/error/error_handeling.c +++ b/src/error/error_handeling.c @@ -6,7 +6,7 @@ /* By: maiadegraaf '\n", STDERR_FILENO); else if (token == GREAT_GREAT) - ft_putstr_fd("GREAT_GREAT\n", STDERR_FILENO); + ft_putstr_fd("'>>'\n", STDERR_FILENO); else if (token == LESS) - ft_putstr_fd("LESS\n", STDERR_FILENO); + ft_putstr_fd("'<'\n", STDERR_FILENO); else if (token == LESS_LESS) - ft_putstr_fd("LESS_LESS\n", STDERR_FILENO); + ft_putstr_fd("'<<'\n", STDERR_FILENO); + else if (token == PIPE) + ft_putstr_fd("'|'\n", STDERR_FILENO); + else + ft_putstr_fd("\n", STDERR_FILENO); ft_lexorclear(&lexor_list); reset_tools(tools); } +int export_error(char *c) +{ + ft_putstr_fd("minishell: export: ", STDERR_FILENO); + if (c) + { + ft_putchar_fd('\'', STDERR_FILENO); + ft_putstr_fd(c, STDERR_FILENO); + ft_putstr_fd("\': is ", STDERR_FILENO); + } + ft_putendl_fd("not a valid identifier", STDERR_FILENO); + return (EXIT_FAILURE); +} + void lexor_error(int error, t_tools *tools) { ft_lexorclear(&tools->lexor_list); @@ -48,37 +65,3 @@ int cmd_not_found(char *str) ft_putstr_fd(": command not found\n", STDERR_FILENO); return (127); } - -/** - * @brief - * Finds corresponding error and frees args; - * @param error - * Number of related error: - * 0 = If there is no string following a redirection or a pipe. - * @param tools - */ -int ft_error(int error, t_tools *tools) -{ - ft_putstr_fd("minishell: ", STDERR_FILENO); - if (error == 0) - ft_putstr_fd("syntax error: unexpected 'newline'\n", STDERR_FILENO); - else if (error == 1) - ft_putstr_fd("memory error: unable to assign memory\n", STDERR_FILENO); - else if (error == 2) - ft_putstr_fd("syntax error: unable to locate closing quotation\n", - STDERR_FILENO); - else if (error == 3) - ft_putstr_fd("Parser problem\n", STDERR_FILENO); - else if (error == 4) - ft_putstr_fd("Failed to create pipe\n", STDERR_FILENO); - else if (error == 5) - ft_putstr_fd("Failed to fork\n", STDERR_FILENO); - else if (error == 6) - ft_putstr_fd("outfile: Error\n", STDERR_FILENO); - else if (error == 7) - ft_putstr_fd("infile: No such file or directory\n", STDERR_FILENO); - else if (error == 8) - ft_putendl_fd("Path does not exist", STDERR_FILENO); - reset_tools(tools); - return (EXIT_SUCCESS); -} diff --git a/src/error/ft_error.c b/src/error/ft_error.c new file mode 100644 index 0000000..1e2dc18 --- /dev/null +++ b/src/error/ft_error.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* ft_error.c :+: :+: */ +/* +:+ */ +/* By: mgraaf +#+ */ +/* +#+ */ +/* Created: 2022/04/18 14:12:23 by mgraaf #+# #+# */ +/* Updated: 2022/04/18 14:12:29 by mgraaf ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "error.h" + +/** + * @brief + * Finds corresponding error and frees args; + * @param error + * Number of related error: + * 0 = If there is no string following a redirection or a pipe. + * @param tools + */ +int ft_error(int error, t_tools *tools) +{ + ft_putstr_fd("minishell: ", STDERR_FILENO); + if (error == 0) + ft_putstr_fd("syntax error: unexpected 'newline'\n", STDERR_FILENO); + else if (error == 1) + ft_putstr_fd("memory error: unable to assign memory\n", STDERR_FILENO); + else if (error == 2) + ft_putstr_fd("syntax error: unable to locate closing quotation\n", + STDERR_FILENO); + else if (error == 3) + ft_putstr_fd("Parser problem\n", STDERR_FILENO); + else if (error == 4) + ft_putstr_fd("Failed to create pipe\n", STDERR_FILENO); + else if (error == 5) + ft_putstr_fd("Failed to fork\n", STDERR_FILENO); + else if (error == 6) + ft_putstr_fd("outfile: Error\n", STDERR_FILENO); + else if (error == 7) + ft_putstr_fd("infile: No such file or directory\n", STDERR_FILENO); + else if (error == 8) + ft_putendl_fd("Path does not exist", STDERR_FILENO); + reset_tools(tools); + return (EXIT_SUCCESS); +} \ No newline at end of file diff --git a/src/executor/check_redirections.c b/src/executor/check_redirections.c index ce20896..8d4ec96 100644 --- a/src/executor/check_redirections.c +++ b/src/executor/check_redirections.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/25 11:39:57 by mgraaf #+# #+# */ -/* Updated: 2022/04/14 11:25:17 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/18 16:41:00 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -25,71 +25,50 @@ int check_append_outfile(t_lexor *redirections) return (fd); } -int check_outfile(t_lexor *redirections) +int handle_infile(t_tools *tools, char *file) { - t_lexor *start; - int fd; + int fd; - start = redirections; - fd = 0; - while (redirections) - { - if (redirections->token == GREAT - || redirections->token == GREAT_GREAT) - { - if (fd > 0) - close (fd); - fd = check_append_outfile(redirections); - if (fd < 0) - return (-1); - } - redirections = redirections->next; - } - redirections = start; - return (fd); + fd = open(file, O_RDONLY); + if (fd < 0) + return (ft_error(7, tools)); + if (fd > 0 && dup2(fd, STDIN_FILENO) < 0) + return (ft_error(4, tools)); + if (fd > 0) + close(fd); + return (EXIT_SUCCESS); } -int check_infile(t_lexor *redirections) +int handle_outfile(t_lexor *redirection, t_tools *tools) { - t_lexor *start; - int fd; + int fd; - start = redirections; - fd = 0; - while (redirections) - { - if (redirections->token == LESS) - { - if (fd > 0) - close (fd); - fd = open(redirections->str, O_RDONLY); - if (fd < 0) - return (-1); - } - redirections = redirections->next; - } - redirections = start; - return (fd); + fd = check_append_outfile(redirection); + if (fd < 0) + return (ft_error(7, tools)); + if (fd > 0 && dup2(fd, STDOUT_FILENO) < 0) + return (ft_error(4, tools)); + if (fd > 0) + close(fd); + return (EXIT_SUCCESS); } -int handle_redirections(t_simple_cmds *cmd, t_tools *tools) +int check_redirections(t_simple_cmds *cmd, t_tools *tools) { - int fd_in; - int fd_out; + t_lexor *start; - fd_in = check_infile(cmd->redirections); - if (fd_in < 0) - ft_error(7, tools); - if (fd_in > 0 && dup2(fd_in, STDIN_FILENO) < 0) - ft_error(4, tools); - if (fd_in > 0) - close(fd_in); - fd_out = check_outfile(cmd->redirections); - if (fd_out < 0) - ft_error (6, tools); - if (fd_out > 0 && dup2(fd_out, STDOUT_FILENO) < 0) - ft_error(4, tools); - if (fd_out > 0) - close(fd_out); + start = cmd->redirections; + while (cmd->redirections) + { + if (cmd->redirections->token == LESS) + handle_infile(tools, cmd->redirections->str); + else if (cmd->redirections->token == GREAT + || cmd->redirections->token == GREAT_GREAT) + handle_outfile(cmd->redirections, tools); + else if (cmd->redirections->token == LESS_LESS) + handle_infile(tools, cmd->hd_file_name); + cmd->redirections = cmd->redirections->next; + } + cmd->redirections = start; return (EXIT_SUCCESS); } diff --git a/src/executor/handle_cmd.c b/src/executor/handle_cmd.c index 2f74c3a..cfbb13c 100644 --- a/src/executor/handle_cmd.c +++ b/src/executor/handle_cmd.c @@ -6,7 +6,7 @@ /* By: maiadegraaf heredoc) - { - fd = open(cmd->hd_file_name, O_RDONLY); - dup2(fd, STDIN_FILENO); - close(fd); - } if (cmd->redirections) - handle_redirections(cmd, tools); + check_redirections(cmd, tools); if (cmd->builtin != NULL) { exit_code = cmd->builtin(tools, cmd); @@ -78,7 +71,8 @@ void single_cmd(t_simple_cmds *cmd, t_tools *tools) int status; tools->simple_cmds = call_expander(tools, tools->simple_cmds); - if (cmd->builtin) + if (cmd->builtin == mini_cd || cmd->builtin == mini_exit + || cmd->builtin == mini_export || cmd->builtin == mini_unset) { g_global.error_num = cmd->builtin(tools, cmd); return ; diff --git a/src/executor/heredoc.c b/src/executor/heredoc.c index ebf7c95..4d40a01 100644 --- a/src/executor/heredoc.c +++ b/src/executor/heredoc.c @@ -6,7 +6,7 @@ /* By: maiadegraaf del); + del_len = ft_strlen(heredoc->str); fd = open(file_name, O_CREAT | O_RDWR | O_TRUNC, 0644); line = readline(HEREDOC_MSG); - while (line && ft_strncmp(heredoc->del, line, ft_strlen(line)) + while (line && ft_strncmp(heredoc->str, line, ft_strlen(line)) && !g_global.stop_heredoc) { if (quotes == false) @@ -52,14 +52,14 @@ int create_heredoc(t_heredoc *heredoc, bool quotes, return (EXIT_SUCCESS); } -int ft_heredoc(t_tools *tools, t_heredoc *heredoc, char *file_name) +int ft_heredoc(t_tools *tools, t_lexor *heredoc, char *file_name) { bool quotes; int sl; sl = EXIT_SUCCESS; - if (heredoc->del[0] == '\"' - && heredoc->del[ft_strlen(heredoc->del) - 1] == '\"') + if (heredoc->str[0] == '\"' + && heredoc->str[ft_strlen(heredoc->str) - 1] == '\"') quotes = true; else quotes = false; @@ -71,30 +71,41 @@ int ft_heredoc(t_tools *tools, t_heredoc *heredoc, char *file_name) return (sl); } -int send_heredoc(t_tools *tools, t_simple_cmds *cmd) +char *generate_heredoc_filename(void) { - t_heredoc *start; static int i = 0; char *num; - int sl; + char *file_name; + + num = ft_itoa(i++); + file_name = ft_strjoin("build/.tmp_heredoc_file_", num); + free(num); + return (file_name); +} + +int send_heredoc(t_tools *tools, t_simple_cmds *cmd) +{ + t_lexor *start; + int sl; - start = cmd->heredoc; + start = cmd->redirections; sl = EXIT_SUCCESS; - while (cmd->heredoc) + while (cmd->redirections) { - if (cmd->hd_file_name) - free(cmd->hd_file_name); - num = ft_itoa(i++); - cmd->hd_file_name = ft_strjoin("build/.tmp_heredoc_file_", num); - free(num); - sl = ft_heredoc(tools, cmd->heredoc, cmd->hd_file_name); - if (sl) + if (cmd->redirections->token == LESS_LESS) { - g_global.error_num = 1; - return (reset_tools(tools)); + if (cmd->hd_file_name) + free(cmd->hd_file_name); + cmd->hd_file_name = generate_heredoc_filename(); + sl = ft_heredoc(tools, cmd->redirections, cmd->hd_file_name); + if (sl) + { + g_global.error_num = 1; + return (reset_tools(tools)); + } } - cmd->heredoc = cmd->heredoc->next; + cmd->redirections = cmd->redirections->next; } - cmd->heredoc = start; + cmd->redirections = start; return (EXIT_SUCCESS); } diff --git a/src/parser/handle_redirections.c b/src/parser/handle_redirections.c index b9d1ef1..8e06941 100644 --- a/src/parser/handle_redirections.c +++ b/src/parser/handle_redirections.c @@ -6,7 +6,7 @@ /* By: maiadegraaf next->str)); - if (!node) - parser_error(1, parser_tools->tools, parser_tools->lexor_list); - ft_heredocadd_back(&parser_tools->heredoc, node); - index_1 = tmp->i; - index_2 = tmp->next->i; - ft_lexordelone(&parser_tools->lexor_list, index_1); - ft_lexordelone(&parser_tools->lexor_list, index_2); - return (1); -} - void rm_redirections(t_parser_tools *parser_tools) { t_lexor *tmp; @@ -73,11 +56,10 @@ void rm_redirections(t_parser_tools *parser_tools) if (!tmp->next) parser_error(0, parser_tools->tools, parser_tools->lexor_list); if (tmp->next->token) - parser_double_token_error(parser_tools->tools, parser_tools->lexor_list); - if (tmp->token == LESS_LESS) - handle_heredoc(parser_tools, tmp); - else if ((tmp->token >= GREAT - && tmp->token <= LESS)) + parser_double_token_error(parser_tools->tools, + parser_tools->lexor_list, tmp->next->token); + if ((tmp->token >= GREAT + && tmp->token <= LESS_LESS)) add_new_redirection(tmp, parser_tools); rm_redirections(parser_tools); } diff --git a/src/parser/parser.c b/src/parser/parser.c index 86d0afd..70ecfd0 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/04/18 12:54:30 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/18 16:33:07 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -38,7 +38,7 @@ t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) } arg_size--; } - return (ft_simple_cmdsnew(str, parser_tools->heredoc, + return (ft_simple_cmdsnew(str, parser_tools->num_redirections, parser_tools->redirections)); } diff --git a/src/parser/parser_utils.c b/src/parser/parser_utils.c index 9a2fb2b..6e3b71f 100644 --- a/src/parser/parser_utils.c +++ b/src/parser/parser_utils.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/03/04 11:52:02 by mgraaf #+# #+# */ -/* Updated: 2022/04/12 13:54:16 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/18 16:36:48 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -17,7 +17,6 @@ t_parser_tools init_parser_tools(t_lexor *lexor_list, t_tools *tools) t_parser_tools parser_tools; parser_tools.lexor_list = lexor_list; - parser_tools.heredoc = NULL; parser_tools.redirections = NULL; parser_tools.num_redirections = 0; parser_tools.tools = tools; diff --git a/src/utils/t_simple_cmds_utils.c b/src/utils/t_simple_cmds_utils.c index 7c84641..2e4e9a4 100644 --- a/src/utils/t_simple_cmds_utils.c +++ b/src/utils/t_simple_cmds_utils.c @@ -6,13 +6,13 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:31:53 by mgraaf #+# #+# */ -/* Updated: 2022/04/14 14:39:54 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/18 16:33:36 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ #include "utils.h" -t_simple_cmds *ft_simple_cmdsnew(char **str, t_heredoc *heredoc, +t_simple_cmds *ft_simple_cmdsnew(char **str, int num_redirections, t_lexor *redirections) { t_simple_cmds *new_element; @@ -22,7 +22,6 @@ t_simple_cmds *ft_simple_cmdsnew(char **str, t_heredoc *heredoc, return (0); new_element->str = str; new_element->builtin = builtin_arr(str[0]); - new_element->heredoc = heredoc; new_element->hd_file_name = NULL; new_element->num_redirections = num_redirections; new_element->redirections = redirections; @@ -73,7 +72,7 @@ void ft_simple_cmdsclear(t_simple_cmds **lst) while (*lst) { tmp = (*lst)->next; - ft_heredocclear(&(*lst)->heredoc); + // ft_heredocclear(&(*lst)->heredoc); redirections_tmp = (*lst)->redirections; ft_lexorclear(&redirections_tmp); if ((*lst)->str) From 3c47bf4c3a7f898874f1c00c15bf9eb92028dde1 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Mon, 18 Apr 2022 16:43:36 +0200 Subject: [PATCH 158/163] hotfix norminette --- includes/minishell.h | 4 ++-- src/parser/handle_redirections.c | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/includes/minishell.h b/includes/minishell.h index 0a0aba1..55ce54a 100644 --- a/includes/minishell.h +++ b/includes/minishell.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/14 13:46:41 by mgraaf #+# #+# */ -/* Updated: 2022/04/18 16:32:16 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 16:35:59 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -45,7 +45,7 @@ void sigquit_handler(int sig); void init_signals(void); char *delete_quotes(char *str, char c); char *delete_quotes_export(char *str, char c); -int question_mark(char **tmp); +int question_mark(char **tmp); //builtins diff --git a/src/parser/handle_redirections.c b/src/parser/handle_redirections.c index 37c4411..c62c9e0 100644 --- a/src/parser/handle_redirections.c +++ b/src/parser/handle_redirections.c @@ -6,7 +6,7 @@ /* By: maiadegraaf next) parser_error(0, parser_tools->tools, parser_tools->lexor_list); if (tmp->next->token) - parser_double_token_error(parser_tools->tools, parser_tools->lexor_list, tmp->next->token); + parser_double_token_error(parser_tools->tools, + parser_tools->lexor_list, tmp->next->token); if (tmp->token == LESS_LESS) handle_heredoc(parser_tools, tmp); else if ((tmp->token >= GREAT From eda5db2c63775a5e348f973d1487f80c924e7dc4 Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Mon, 18 Apr 2022 17:12:58 +0200 Subject: [PATCH 159/163] fix infile heredoc error --- includes/executor.h | 4 +-- src/error/ft_error.c | 4 +-- src/executor/check_redirections.c | 44 +++++++++++++++++++++++-------- src/executor/handle_cmd.c | 5 ++-- 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/includes/executor.h b/includes/executor.h index 055d7e9..e381949 100644 --- a/includes/executor.h +++ b/includes/executor.h @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:17:39 by mgraaf #+# #+# */ -/* Updated: 2022/04/18 16:31:21 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/18 17:09:10 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ // int check_outfile(t_lexor *redirections); // int check_infile(t_lexor *redirections); // int handle_redirections(t_simple_cmds *cmd, t_tools *tools); -int check_redirections(t_simple_cmds *cmd, t_tools *tools); +int check_redirections(t_simple_cmds *cmd); // executor int executor(t_tools *tools); diff --git a/src/error/ft_error.c b/src/error/ft_error.c index 1e2dc18..e390844 100644 --- a/src/error/ft_error.c +++ b/src/error/ft_error.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/04/18 14:12:23 by mgraaf #+# #+# */ -/* Updated: 2022/04/18 14:12:29 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/18 16:59:29 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -43,5 +43,5 @@ int ft_error(int error, t_tools *tools) else if (error == 8) ft_putendl_fd("Path does not exist", STDERR_FILENO); reset_tools(tools); - return (EXIT_SUCCESS); + return (EXIT_FAILURE); } \ No newline at end of file diff --git a/src/executor/check_redirections.c b/src/executor/check_redirections.c index 8d4ec96..2a0f8aa 100644 --- a/src/executor/check_redirections.c +++ b/src/executor/check_redirections.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/25 11:39:57 by mgraaf #+# #+# */ -/* Updated: 2022/04/18 16:41:00 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/18 17:08:32 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -25,35 +25,48 @@ int check_append_outfile(t_lexor *redirections) return (fd); } -int handle_infile(t_tools *tools, char *file) +int handle_infile(char *file) { int fd; fd = open(file, O_RDONLY); if (fd < 0) - return (ft_error(7, tools)); + { + ft_putstr_fd("minishell: infile: No such file or directory\n", + STDERR_FILENO); + return (EXIT_FAILURE); + } if (fd > 0 && dup2(fd, STDIN_FILENO) < 0) - return (ft_error(4, tools)); + { + ft_putstr_fd("minishell: pipe error\n", STDERR_FILENO); + return (EXIT_FAILURE); + } if (fd > 0) close(fd); return (EXIT_SUCCESS); } -int handle_outfile(t_lexor *redirection, t_tools *tools) +int handle_outfile(t_lexor *redirection) { int fd; fd = check_append_outfile(redirection); if (fd < 0) - return (ft_error(7, tools)); + { + ft_putstr_fd("minishell: outfile: Error\n", STDERR_FILENO); + return (EXIT_FAILURE); + } if (fd > 0 && dup2(fd, STDOUT_FILENO) < 0) - return (ft_error(4, tools)); + { + ft_putstr_fd("minishell: pipe error\n", STDERR_FILENO); + return (EXIT_FAILURE); + } if (fd > 0) close(fd); return (EXIT_SUCCESS); } -int check_redirections(t_simple_cmds *cmd, t_tools *tools) +int check_redirections(t_simple_cmds *cmd) { t_lexor *start; @@ -61,12 +74,21 @@ int check_redirections(t_simple_cmds *cmd, t_tools *tools) while (cmd->redirections) { if (cmd->redirections->token == LESS) - handle_infile(tools, cmd->redirections->str); + { + if (handle_infile(cmd->redirections->str)) + return (EXIT_FAILURE); + } else if (cmd->redirections->token == GREAT || cmd->redirections->token == GREAT_GREAT) - handle_outfile(cmd->redirections, tools); + { + if (handle_outfile(cmd->redirections)) + return (EXIT_FAILURE); + } else if (cmd->redirections->token == LESS_LESS) - handle_infile(tools, cmd->hd_file_name); + { + if (handle_infile(cmd->hd_file_name)) + return (EXIT_FAILURE); + } cmd->redirections = cmd->redirections->next; } cmd->redirections = start; diff --git a/src/executor/handle_cmd.c b/src/executor/handle_cmd.c index cfbb13c..566a994 100644 --- a/src/executor/handle_cmd.c +++ b/src/executor/handle_cmd.c @@ -6,7 +6,7 @@ /* By: maiadegraaf redirections) - check_redirections(cmd, tools); + if (check_redirections(cmd)) + exit(1); if (cmd->builtin != NULL) { exit_code = cmd->builtin(tools, cmd); From e6afa87dbd18482b51365e14d4439d51acc834ff Mon Sep 17 00:00:00 2001 From: Maia de Graaf Date: Mon, 18 Apr 2022 18:14:18 +0200 Subject: [PATCH 160/163] fix errors --- Makefile | 2 +- banaan | 18 ++++++++++++ includes/error.h | 4 +-- includes/parser.h | 4 +-- outfile | 18 ++++++++++++ src/builtins/mini_cd.c | 8 +++-- .../{error_handeling.c => error_handling.c} | 7 +++-- src/error/ft_error.c | 7 +++-- src/executor/executor.c | 3 +- src/parser/handle_redirections.c | 4 +-- src/parser/parser.c | 29 +++++++++++++++---- 11 files changed, 82 insertions(+), 22 deletions(-) create mode 100644 banaan create mode 100644 outfile rename src/error/{error_handeling.c => error_handling.c} (90%) diff --git a/Makefile b/Makefile index fd3c5fc..9c0b86b 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,7 @@ src = src/main.c \ src/utils/t_lexor_utils.c \ src/utils/t_simple_cmds_utils.c \ src/utils/utils.c \ - src/error/error_handeling.c \ + src/error/error_handling.c \ src/error/ft_error.c \ src/executor/check_redirections.c \ src/executor/executor.c \ diff --git a/banaan b/banaan new file mode 100644 index 0000000..c772f40 --- /dev/null +++ b/banaan @@ -0,0 +1,18 @@ +total 176 +drwxr-xr-x 17 mgraaf piscine 578 Apr 18 17:24 . +drwx------ 24 mgraaf piscine 816 Apr 12 15:35 .. +drwxr-xr-x 19 mgraaf piscine 646 Apr 18 17:15 .git +-rw-r--r-- 1 mgraaf piscine 37 Apr 15 17:47 .gitignore +-rw-r--r-- 1 mgraaf piscine 85 Apr 15 17:47 .gitmodules +drwxr-xr-x 5 mgraaf piscine 170 Apr 15 17:47 .vscode +-rw-r--r-- 1 mgraaf piscine 3274 Apr 18 17:13 Makefile +drwxr-xr-x 18 mgraaf piscine 612 Mar 28 12:53 Unity +-rw-r--r-- 1 mgraaf piscine 0 Apr 18 17:24 banaan +drwxr-xr-x 6 mgraaf piscine 204 Apr 18 17:15 build +drwxr-xr-x 10 mgraaf piscine 340 Apr 18 17:13 includes +drwxr-xr-x 3 mgraaf piscine 102 Feb 14 12:06 libraries +-rwxr-xr-x 1 mgraaf piscine 62860 Apr 18 17:15 minishell +drwxr-xr-x 12 mgraaf piscine 408 Apr 18 12:45 src +drwxr-xr-x 11 mgraaf piscine 374 Apr 15 17:47 test +-rwxrwxrwx 1 mgraaf piscine 8922 Apr 18 15:02 tester.sh +drwxr-xr-x 3 mgraaf piscine 102 Apr 15 17:47 utilities diff --git a/includes/error.h b/includes/error.h index 0a58565..db750cb 100644 --- a/includes/error.h +++ b/includes/error.h @@ -6,7 +6,7 @@ /* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 17:59:38 by mgraaf #+# #+# */ -/* Updated: 2022/04/18 16:32:35 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/18 17:53:55 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -68,7 +68,7 @@ typedef struct s_simple_cmds int parse_envp(t_tools *tools); int find_pwd(t_tools *tools); -int *parser(t_tools *tools); +int parser(t_tools *tools); //parser_utils t_parser_tools init_parser_tools(t_lexor *lexor_list, t_tools *tools); diff --git a/outfile b/outfile new file mode 100644 index 0000000..c772f40 --- /dev/null +++ b/outfile @@ -0,0 +1,18 @@ +total 176 +drwxr-xr-x 17 mgraaf piscine 578 Apr 18 17:24 . +drwx------ 24 mgraaf piscine 816 Apr 12 15:35 .. +drwxr-xr-x 19 mgraaf piscine 646 Apr 18 17:15 .git +-rw-r--r-- 1 mgraaf piscine 37 Apr 15 17:47 .gitignore +-rw-r--r-- 1 mgraaf piscine 85 Apr 15 17:47 .gitmodules +drwxr-xr-x 5 mgraaf piscine 170 Apr 15 17:47 .vscode +-rw-r--r-- 1 mgraaf piscine 3274 Apr 18 17:13 Makefile +drwxr-xr-x 18 mgraaf piscine 612 Mar 28 12:53 Unity +-rw-r--r-- 1 mgraaf piscine 0 Apr 18 17:24 banaan +drwxr-xr-x 6 mgraaf piscine 204 Apr 18 17:15 build +drwxr-xr-x 10 mgraaf piscine 340 Apr 18 17:13 includes +drwxr-xr-x 3 mgraaf piscine 102 Feb 14 12:06 libraries +-rwxr-xr-x 1 mgraaf piscine 62860 Apr 18 17:15 minishell +drwxr-xr-x 12 mgraaf piscine 408 Apr 18 12:45 src +drwxr-xr-x 11 mgraaf piscine 374 Apr 15 17:47 test +-rwxrwxrwx 1 mgraaf piscine 8922 Apr 18 15:02 tester.sh +drwxr-xr-x 3 mgraaf piscine 102 Apr 15 17:47 utilities diff --git a/src/builtins/mini_cd.c b/src/builtins/mini_cd.c index 6d128bb..a88f105 100644 --- a/src/builtins/mini_cd.c +++ b/src/builtins/mini_cd.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/15 15:17:04 by mgraaf #+# #+# */ -/* Updated: 2022/04/18 14:00:54 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 18:14:00 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -82,7 +82,11 @@ int mini_cd(t_tools *tools, t_simple_cmds *simple_cmd) { ret = chdir(simple_cmd->str[1]); if (ret != 0) - ft_putendl_fd("Path does not exist", STDERR_FILENO); + { + ft_putstr_fd("minishell: ", STDERR_FILENO); + ft_putstr_fd(simple_cmd->str[1], STDERR_FILENO); + perror(" "); + } } if (ret != 0) return (EXIT_FAILURE); diff --git a/src/error/error_handeling.c b/src/error/error_handling.c similarity index 90% rename from src/error/error_handeling.c rename to src/error/error_handling.c index 1f9379c..8a13e80 100755 --- a/src/error/error_handeling.c +++ b/src/error/error_handling.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* error_handeling.c :+: :+: */ +/* error_handling.c :+: :+: */ /* +:+ */ /* By: maiadegraaf +#+ */ /* +#+ */ /* Created: 2022/04/18 14:12:23 by mgraaf #+# #+# */ -/* Updated: 2022/04/18 16:59:29 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/18 18:10:33 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -24,7 +24,8 @@ int ft_error(int error, t_tools *tools) { ft_putstr_fd("minishell: ", STDERR_FILENO); if (error == 0) - ft_putstr_fd("syntax error: unexpected 'newline'\n", STDERR_FILENO); + ft_putstr_fd("syntax error near unexpected token 'newline'\n", + STDERR_FILENO); else if (error == 1) ft_putstr_fd("memory error: unable to assign memory\n", STDERR_FILENO); else if (error == 2) @@ -44,4 +45,4 @@ int ft_error(int error, t_tools *tools) ft_putendl_fd("Path does not exist", STDERR_FILENO); reset_tools(tools); return (EXIT_FAILURE); -} \ No newline at end of file +} diff --git a/src/executor/executor.c b/src/executor/executor.c index 494c8ed..1b1e5d9 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/24 15:09:50 by mgraaf #+# #+# */ -/* Updated: 2022/04/18 15:12:03 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/18 17:26:25 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -85,7 +85,6 @@ int executor(t_tools *tools) fd_in = STDIN_FILENO; while (tools->simple_cmds) { - printf("%s\n", tools->simple_cmds->str[1]); tools->simple_cmds = call_expander(tools, tools->simple_cmds); if (tools->simple_cmds->next) pipe(end); diff --git a/src/parser/handle_redirections.c b/src/parser/handle_redirections.c index ad19a8a..071177b 100644 --- a/src/parser/handle_redirections.c +++ b/src/parser/handle_redirections.c @@ -6,7 +6,7 @@ /* By: maiadegraaf next->token) parser_double_token_error(parser_tools->tools, parser_tools->lexor_list, tmp->next->token); - if ((tmp->token >= GREAT + if ((tmp->token >= GREAT && tmp->token <= LESS_LESS)) add_new_redirection(tmp, parser_tools); rm_redirections(parser_tools); diff --git a/src/parser/parser.c b/src/parser/parser.c index 70ecfd0..641ca89 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -6,7 +6,7 @@ /* By: mgraaf +#+ */ /* +#+ */ /* Created: 2022/02/17 15:28:22 by mgraaf #+# #+# */ -/* Updated: 2022/04/18 16:33:07 by mgraaf ######## odam.nl */ +/* Updated: 2022/04/18 18:00:53 by mgraaf ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -42,19 +42,38 @@ t_simple_cmds *initialize_cmd(t_parser_tools *parser_tools) parser_tools->num_redirections, parser_tools->redirections)); } -int *parser(t_tools *tools) +int handle_pipe_errors(t_tools *tools, t_tokens token) +{ + if (token == PIPE) + { + parser_double_token_error(tools, tools->lexor_list, + tools->lexor_list->token); + return (EXIT_FAILURE); + } + if (!tools->lexor_list) + { + parser_error(0, tools, tools->lexor_list); + return (EXIT_FAILURE); + } + return (EXIT_SUCCESS); +} + +int parser(t_tools *tools) { t_simple_cmds *node; t_parser_tools parser_tools; tools->simple_cmds = NULL; count_pipes(tools->lexor_list, tools); + if (tools->lexor_list->token == PIPE) + return (parser_double_token_error(tools, tools->lexor_list, + tools->lexor_list->token)); while (tools->lexor_list) { if (tools->lexor_list && tools->lexor_list->token == PIPE) ft_lexordelone(&tools->lexor_list, tools->lexor_list->i); - if (!tools->lexor_list) - parser_error(0, tools, tools->lexor_list); + if (handle_pipe_errors(tools, tools->lexor_list->token)) + return (EXIT_FAILURE); parser_tools = init_parser_tools(tools->lexor_list, tools); node = initialize_cmd(&parser_tools); if (!node) @@ -65,5 +84,5 @@ int *parser(t_tools *tools) ft_simple_cmdsadd_back(&tools->simple_cmds, node); tools->lexor_list = parser_tools.lexor_list; } - return (0); + return (EXIT_SUCCESS); } From afb78b81f0d928c297185cff05aac4780e9524d0 Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Tue, 19 Apr 2022 11:55:08 +0200 Subject: [PATCH 161/163] didn't fix $ and $ --- oufile | 1 + src/builtins/mini_echo.c | 5 +++-- src/executor/heredoc.c | 4 ++-- src/expander/expander.c | 8 +++----- src/expander/expanders_utils.c | 5 +++-- 5 files changed, 12 insertions(+), 11 deletions(-) create mode 100644 oufile diff --git a/oufile b/oufile new file mode 100644 index 0000000..fc7764b --- /dev/null +++ b/oufile @@ -0,0 +1 @@ +dsfdsf diff --git a/src/builtins/mini_echo.c b/src/builtins/mini_echo.c index 6ddb5c9..7eb9875 100644 --- a/src/builtins/mini_echo.c +++ b/src/builtins/mini_echo.c @@ -6,7 +6,7 @@ /* By: maiadegraaf str[i] && simple_cmd->str[i][0] == '-') + while (simple_cmd->str[i] && simple_cmd->str[i][0] == '-' + && simple_cmd->str[i][1] == 'n') { j = 1; while (simple_cmd->str[i][j] == 'n') diff --git a/src/executor/heredoc.c b/src/executor/heredoc.c index 4d40a01..1c6ea4b 100644 --- a/src/executor/heredoc.c +++ b/src/executor/heredoc.c @@ -6,7 +6,7 @@ /* By: maiadegraaf str); fd = open(file_name, O_CREAT | O_RDWR | O_TRUNC, 0644); line = readline(HEREDOC_MSG); - while (line && ft_strncmp(heredoc->str, line, ft_strlen(line)) + while (line && ft_strncmp(heredoc->str, line, ft_strlen(heredoc->str)) && !g_global.stop_heredoc) { if (quotes == false) diff --git a/src/expander/expander.c b/src/expander/expander.c index ee5ba65..1d3a045 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/18 16:31:40 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/19 11:19:08 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -51,9 +51,7 @@ int handle_digit_after_dollar(int j, char *str) { if (ft_isdigit(str[j + 1]) == 1) { - while (ft_isdigit(str[j + 1]) == 1) - j++; - j++; + j += 2; } } return (j - i); @@ -71,7 +69,7 @@ char *detect_dollar_sign(t_tools *tools, char *str) while (str[j]) { j += handle_digit_after_dollar(j, str); - if (str[j] == '$' && str[j + 1] != '\"' && str[j + 1] != '?') + if (str[j] == '$' && str[j + 1] != '?') j += loop_if_dollar_sign(tools, str, &tmp, j); else if (str[j] == '$' && str[j + 1] == '?') j += question_mark(&tmp); diff --git a/src/expander/expanders_utils.c b/src/expander/expanders_utils.c index 42fd86b..42908ad 100644 --- a/src/expander/expanders_utils.c +++ b/src/expander/expanders_utils.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/31 16:08:47 by fpolycar #+# #+# */ -/* Updated: 2022/04/18 15:42:30 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/19 10:34:35 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -42,7 +42,8 @@ int after_dol_lenght(char *str, int j) i = j + 1; while (str[i] != '\0' && str[i] != '$' && str[i] != ' ' - && str[i] != '\"' && str[i] != '\'' && str[i] != '=' && str[i] != '-') + && str[i] != '\"' && str[i] != '\'' && str[i] != '=' && str[i] != '-' + && str[i] != ':') i++; return (i); } From df0f2a2f14eca48c06ff04c945bc8ec9ba71fe1b Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Tue, 19 Apr 2022 15:00:12 +0200 Subject: [PATCH 162/163] final edge cases --- src/expander/expander.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/expander/expander.c b/src/expander/expander.c index 1d3a045..d8c0f08 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/19 11:19:08 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/19 14:59:02 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -69,7 +69,8 @@ char *detect_dollar_sign(t_tools *tools, char *str) while (str[j]) { j += handle_digit_after_dollar(j, str); - if (str[j] == '$' && str[j + 1] != '?') + if (str[j] == '$' && str[j + 1] != '?' && (str[j + 1] != ' ' + && (str[j + 1] != '"' || str[j + 2] != '\0'))) j += loop_if_dollar_sign(tools, str, &tmp, j); else if (str[j] == '$' && str[j + 1] == '?') j += question_mark(&tmp); From 88797f0136d6dabef306fd5f4dff71493ce1edfa Mon Sep 17 00:00:00 2001 From: alfredpoly <36860113+Alfredpoly@users.noreply.github.com> Date: Wed, 20 Apr 2022 10:16:28 +0200 Subject: [PATCH 163/163] final touches export --- src/expander/expander.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/expander/expander.c b/src/expander/expander.c index d8c0f08..3a9c8c9 100644 --- a/src/expander/expander.c +++ b/src/expander/expander.c @@ -6,7 +6,7 @@ /* By: fpolycar +#+ */ /* +#+ */ /* Created: 2022/03/15 13:35:26 by fpolycar #+# #+# */ -/* Updated: 2022/04/19 14:59:02 by fpolycar ######## odam.nl */ +/* Updated: 2022/04/20 10:13:37 by fpolycar ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -69,11 +69,11 @@ char *detect_dollar_sign(t_tools *tools, char *str) while (str[j]) { j += handle_digit_after_dollar(j, str); - if (str[j] == '$' && str[j + 1] != '?' && (str[j + 1] != ' ' - && (str[j + 1] != '"' || str[j + 2] != '\0'))) - j += loop_if_dollar_sign(tools, str, &tmp, j); - else if (str[j] == '$' && str[j + 1] == '?') + if (str[j] == '$' && str[j + 1] == '?') j += question_mark(&tmp); + else if (str[j] == '$' && (str[j + 1] != ' ' && (str[j + 1] != '"' + || str[j + 2] != '\0')) && str[j + 1] != '\0') + j += loop_if_dollar_sign(tools, str, &tmp, j); else { tmp2 = char_to_str(str[j++]);