diff --git a/.gitignore b/.gitignore index 26f0cdb..6b4ca9f 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ minishell ast.xml .$ast.xml.bkp .TEST_objs +.mmoat_history file file2 diff --git a/includes/exec/expander.h b/includes/exec/expander.h index 3dda2ac..8980987 100644 --- a/includes/exec/expander.h +++ b/includes/exec/expander.h @@ -6,7 +6,7 @@ /* By: lderidde +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/07 10:21:00 by lderidde #+# #+# */ -/* Updated: 2025/02/08 10:52:24 by lderidde ### ########.fr */ +/* Updated: 2025/02/10 10:40:51 by lderidde ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,14 @@ # define EXPANDER_H # include "../minishell.h" +# include +# include +int in_dquote(char *str, char *ch); +int in_squote(char *str, char *ch); int expand_var(t_ast_n *node, int j); int expand_tilde(t_ast_n *node, int j); +int expand_star(t_ast_n *node, int j); t_ast_n *expand_node(t_ast_n *node); #endif diff --git a/srcs/execution/exec.c b/srcs/execution/exec.c index 10749ea..fe8edfa 100644 --- a/srcs/execution/exec.c +++ b/srcs/execution/exec.c @@ -6,7 +6,7 @@ /* By: lderidde +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/27 11:22:33 by lderidde #+# #+# */ -/* Updated: 2025/02/08 14:09:45 by lderidde ### ########.fr */ +/* Updated: 2025/02/10 10:31:33 by lderidde ### ########.fr */ /* */ /* ************************************************************************** */ @@ -191,7 +191,7 @@ void handle_redir(t_ast_n *node) int is_builtin(char *str) { - if (ft_strncmp(str, "exit", 4) == 0) + if (ft_strncmp(str, "exit", -1) == 0) return (1); else if (ft_strncmp(str, "pwd", 3) == 0) return (1); diff --git a/srcs/expander/expand_star.c b/srcs/expander/expand_star.c new file mode 100644 index 0000000..9b038b7 --- /dev/null +++ b/srcs/expander/expand_star.c @@ -0,0 +1,110 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* expand_star.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lderidde +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/10 09:09:37 by lderidde #+# #+# */ +/* Updated: 2025/02/10 13:29:09 by lderidde ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../includes/exec/expander.h" +#include +#include + +static char *_strfjoin(char *s1, char *s2) +{ + char *out; + unsigned int i; + unsigned int j; + + if (!s1 && !s2) + return (NULL); + if (!s1) + return (ft_strdup(s2)); + if (!s2) + return (ft_strdup(s1)); + out = ft_calloc(ft_strlen(s1) + ft_strlen(s2) + 1, sizeof(char)); + if (!out) + return (NULL); + i = 0; + j = 0; + while (s1[j]) + out[i++] = s1[j++]; + j = 0; + while (s2[j]) + out[i++] = s2[j++]; + free(s1); + return (out); +} + +bool ft_fnmatch(const char *str, const char *pat) +{ + if (*pat == '\0' && *str == '\0') + return (true); + if (*pat == '*' && *(pat + 1) != '\0' && *str == '\0') + return (false); + if (*pat == *str) + return (ft_fnmatch(str + 1, pat + 1)); + if (*pat == '*') + return (ft_fnmatch(str, pat + 1) || ft_fnmatch(str + 1, pat)); + return (false); +} +void handle_new(t_ast_n *node, int j, char *new) +{ + if (new) + { + ft_free(&node->args[j]); + node->args[j] = new; + } +} + +void expander_star(t_ast_n *node, int j, char *pat) +{ + DIR *dir; + struct dirent *entry; + char *new; + + new = NULL; + dir = opendir("."); + entry = readdir(dir); + while (entry != NULL) + { + if (ft_strncmp(entry->d_name, ".", 1) == 0) + entry = readdir(dir); + else if (ft_fnmatch(entry->d_name, pat) == true) + { + if (new != NULL) + new = _strfjoin(new, " "); + new = _strfjoin(new, entry->d_name); + entry = readdir(dir); + } + else + entry = readdir(dir); + } + handle_new(node, j, new); + closedir(dir); +} + +static int in_quote(char *str, char *ch) +{ + if (in_squote(str, ch) || in_dquote(str, ch)) + return (1); + return (0); +} + +int expand_star(t_ast_n *node, int j) +{ + int i; + + i = 0; + while (node->args[j][i]) + { + if (node->args[j][i] == '*' && !in_quote(node->args[j], &node->args[j][i])) + return (expander_star(node, j, node->args[j]), 1); + i++; + } + return (0); +} diff --git a/srcs/expander/expand_var.c b/srcs/expander/expand_var.c index 16f7c6d..5bfab95 100644 --- a/srcs/expander/expand_var.c +++ b/srcs/expander/expand_var.c @@ -6,7 +6,7 @@ /* By: lderidde +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/07 12:58:25 by lderidde #+# #+# */ -/* Updated: 2025/02/08 10:53:48 by lderidde ### ########.fr */ +/* Updated: 2025/02/10 09:12:39 by lderidde ### ########.fr */ /* */ /* ************************************************************************** */ @@ -130,6 +130,29 @@ int in_squote(char *str, char *ch) } } +int in_dquote(char *str, char *ch) +{ + if (!ft_strchr(str, '\'') && !ft_strchr(str, '\"')) + return (0); + else if (ft_strchr(str, '\"') && !ft_strchr(str, '\'')) + { + if ((ch > ft_strchr(str, '\"')) && ch < ft_strrchr(str, '\"')) + return (1); + return (0); + } + else if (!ft_strchr(str, '\"') && ft_strchr(str, '\'')) + return (0); + else + { + if (ft_strchr(str, '\"') < ft_strchr(str, '\'')) + { + if ((ch > ft_strchr(str, '\"')) && ch < ft_strrchr(str, '\"')) + return (1); + return (0); + } + return (0); + } +} int expand_var(t_ast_n *node, int j) { int i; diff --git a/srcs/expander/expander.c b/srcs/expander/expander.c index 382dc34..85c508b 100644 --- a/srcs/expander/expander.c +++ b/srcs/expander/expander.c @@ -6,7 +6,7 @@ /* By: lderidde +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/07 10:23:02 by lderidde #+# #+# */ -/* Updated: 2025/02/08 15:56:26 by lderidde ### ########.fr */ +/* Updated: 2025/02/10 10:40:34 by lderidde ### ########.fr */ /* */ /* ************************************************************************** */ @@ -114,6 +114,12 @@ t_ast_n *expand_node(t_ast_n *node) if (expand_var(node, i)) check = 1; if (!ifremove_quote(node, i) && check) + { + split_tab(node, i); + check = 0; + } + check = expand_star(node, i); + if (check) split_tab(node, i); ft_free(&node->cmd); node->cmd = ft_strdup(node->args[0]); diff --git a/srcs/msh_struct.c b/srcs/msh_struct.c index 797ed74..3bed85c 100644 --- a/srcs/msh_struct.c +++ b/srcs/msh_struct.c @@ -6,7 +6,7 @@ /* By: lderidde +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/06 15:32:03 by lderidde #+# #+# */ -/* Updated: 2025/02/07 09:24:52 by lderidde ### ########.fr */ +/* Updated: 2025/02/10 13:31:17 by lderidde ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ t_msh *init_msh(char **envp) t_msh *msh; msh = malloc(sizeof(t_msh) * 1); - msh->hist = open(".mmoat_hisotry", O_RDWR | O_CREAT | O_APPEND, 0666); + msh->hist = open(".mmoat_history", O_RDWR | O_CREAT | O_APPEND, 0666); msh->ex_code = 0; msh->input = NULL; if (!msh)