* expand
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
|
||||
110
srcs/expander/expand_star.c
Normal file
110
srcs/expander/expand_star.c
Normal file
@@ -0,0 +1,110 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* expand_star.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 <dirent.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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]);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
|
||||
Reference in New Issue
Block a user