mirror of
https://github.com/KeyZox71/ft_minipowershell.git
synced 2025-03-15 03:16:51 +01:00
「✨」 feat(exec_builtins / quote parsing): did builtins execution, wip quote parsing (don't work for the moment)
This commit is contained in:
2
Makefile
2
Makefile
@ -6,7 +6,7 @@
|
||||
# By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2024/04/24 10:49:52 by adjoly #+# #+# #
|
||||
# Updated: 2024/06/03 15:44:04 by mmoussou ### ########.fr #
|
||||
# Updated: 2024/07/04 09:35:32 by mmoussou ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/29 13:20:22 by mmoussou #+# #+# */
|
||||
/* Updated: 2024/06/13 14:12:31 by mmoussou ### ########.fr */
|
||||
/* Updated: 2024/07/03 11:04:53 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -15,6 +15,8 @@
|
||||
|
||||
# include "env.h"
|
||||
|
||||
int format_quotes(t_list *list_cmd);
|
||||
|
||||
char *get_path(char *path, char *cmd);
|
||||
int exec_split_cmd(t_list *list_cmd, t_env *env);
|
||||
|
||||
|
@ -6,13 +6,57 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/06/01 14:55:06 by mmoussou #+# #+# */
|
||||
/* Updated: 2024/06/27 15:06:45 by mmoussou ### ########.fr */
|
||||
/* Updated: 2024/07/04 11:39:00 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
#include "builtins.h"
|
||||
#include "error_msg.h"
|
||||
|
||||
int is_in_builtins(char *cmd)
|
||||
{
|
||||
int i;
|
||||
static const char *builtins[] = {"echo", "cd", "pwd", "export", "unset",
|
||||
"env", "exit", NULL};
|
||||
|
||||
i = 0;
|
||||
while (builtins[i])
|
||||
{
|
||||
if (!ft_strcmp(cmd, builtins[i]))
|
||||
return (i + 1);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void exec_cmd(char *cmd, char **argv, char **env, t_env *env_t)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = is_in_builtins(cmd);
|
||||
if (i)
|
||||
{
|
||||
if (i == 1)
|
||||
ft_echo(argv + 1);
|
||||
if (i == 2)
|
||||
ft_cd(env_t, argv[1]);
|
||||
if (i == 3)
|
||||
ft_pwd();
|
||||
if (i == 4)
|
||||
ft_export(argv + 1, env_t);
|
||||
if (i == 5)
|
||||
ft_unset(argv[1], env_t);
|
||||
if (i == 6)
|
||||
ft_env(env_t);
|
||||
if (i == 7)
|
||||
exit(ft_atoi(argv[1]));
|
||||
exit(0);
|
||||
}
|
||||
else
|
||||
execve(cmd, argv, env);
|
||||
}
|
||||
|
||||
char *get_cmd_local_path(char *cmd, t_env *env)
|
||||
{
|
||||
char *path;
|
||||
@ -29,6 +73,8 @@ char *get_cmd_local_path(char *cmd, t_env *env)
|
||||
|
||||
int switch_cmd_path(t_cmd *cmd, t_env *env)
|
||||
{
|
||||
if (is_in_builtins(cmd->cmd))
|
||||
return (0);
|
||||
if (cmd->cmd[0] == '.' && cmd->cmd[1] == '/')
|
||||
cmd->cmd = get_cmd_local_path(cmd->cmd, env);
|
||||
else if (cmd->cmd[0] != '/')
|
||||
@ -46,7 +92,7 @@ int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t, int pipe_fd[2])
|
||||
|
||||
input = ft_strdup(cmd->cmd);
|
||||
status = switch_cmd_path(cmd, env_t);
|
||||
if (status == -1 || !input || access(cmd->cmd, X_OK))
|
||||
if (status == -1 || !input || (access(cmd->cmd, X_OK) && !is_in_builtins(cmd->cmd)))
|
||||
{
|
||||
printf("minishell : command not found: %s\n", input);
|
||||
return (-1);
|
||||
@ -67,7 +113,7 @@ int exec_single_cmd(t_cmd *cmd, char **env, t_env *env_t, int pipe_fd[2])
|
||||
if (pipe_fd[0] != -1)
|
||||
close(pipe_fd[1]);
|
||||
if (status != -1)
|
||||
execve(cmd->cmd, cmd->argv, env);
|
||||
exec_cmd(cmd->cmd, cmd->argv, env, env_t);
|
||||
exit(-1);
|
||||
}
|
||||
return (fork_pid);
|
||||
@ -143,9 +189,10 @@ int exec_split_cmd(t_list *list_cmd, t_env *env)
|
||||
i--;
|
||||
if (i < 1)
|
||||
return (0);
|
||||
while (i)
|
||||
waitpid(status, &return_code, 0);
|
||||
while (i - 1)
|
||||
{
|
||||
waitpid(-1, &return_code, 0);
|
||||
waitpid(-1, NULL, 0);
|
||||
i--;
|
||||
}
|
||||
print_return_value(return_code);
|
||||
|
156
src/exec/format_quotes.c
Normal file
156
src/exec/format_quotes.c
Normal file
@ -0,0 +1,156 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* format_quotes.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/07/03 10:50:52 by mmoussou #+# #+# */
|
||||
/* Updated: 2024/07/04 10:09:03 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
uint formated_string_size(char *cmd)
|
||||
{
|
||||
uint i;
|
||||
uint c;
|
||||
|
||||
i = 0;
|
||||
c = 0;
|
||||
while (cmd[i])
|
||||
{
|
||||
if (cmd[i] == SINGLE)
|
||||
{
|
||||
c--;
|
||||
while (cmd[i] && cmd[i] != SINGLE)
|
||||
{
|
||||
i++;
|
||||
c++;
|
||||
}
|
||||
if (!cmd)
|
||||
return (c + 1);
|
||||
c--;
|
||||
}
|
||||
if (cmd[i] == DOUBLE)
|
||||
{
|
||||
c--;
|
||||
while (cmd[i] && cmd[i] != DOUBLE)
|
||||
{
|
||||
i++;
|
||||
c++;
|
||||
}
|
||||
if (!cmd)
|
||||
return (c + 1);
|
||||
c--;
|
||||
}
|
||||
i++;
|
||||
c++;
|
||||
}
|
||||
return (c);
|
||||
}
|
||||
|
||||
char *format_quotes_string(char *cmd)
|
||||
{
|
||||
//char *new_cmd;
|
||||
uint i;
|
||||
uint i_offset;
|
||||
|
||||
i = formated_string_size(cmd);
|
||||
if (i == ft_strlen(cmd))
|
||||
return (ft_strdup(cmd));
|
||||
/*new_cmd = ft_calloc(sizeof(char), i);
|
||||
if (!new_cmd)
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (cmd[i])
|
||||
{
|
||||
if (cmd[i] == SINGLE)
|
||||
{
|
||||
i++;
|
||||
last_i = i;
|
||||
while (cmd[i] && cmd[i] != SINGLE)
|
||||
i++;
|
||||
ft_strlcat(new_cmd, cmd + last_i, i);
|
||||
last_i = i;
|
||||
}
|
||||
else if (cmd[i] == DOUBLE)
|
||||
{
|
||||
i++;
|
||||
last_i = i;
|
||||
while (cmd[i] && cmd[i] != DOUBLE)
|
||||
i++;
|
||||
ft_strlcat(new_cmd, cmd + last_i, i);
|
||||
last_i = i;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
ft_strlcat(new_cmd, cmd + last_i, formated_string_size(cmd) + 1);*/
|
||||
i = 0;
|
||||
i_offset = 0;
|
||||
while (cmd[i])
|
||||
{
|
||||
if (cmd[i] == SINGLE)
|
||||
{
|
||||
i_offset++;
|
||||
while (cmd[i] && cmd[i] != SINGLE)
|
||||
{
|
||||
cmd[i - i_offset] = cmd[i];
|
||||
}
|
||||
if (!cmd[i])
|
||||
{
|
||||
// CANCEL THE OFFSET AND RETURN
|
||||
return (cmd);
|
||||
}
|
||||
}
|
||||
else if (cmd[i])
|
||||
{
|
||||
i_offset++;
|
||||
}
|
||||
else
|
||||
cmd[i - i_offset] = cmd[i];
|
||||
i++;
|
||||
}
|
||||
return (cmd);
|
||||
}
|
||||
|
||||
int format_quotes_cmd(t_cmd *cmd)
|
||||
{
|
||||
char *new_cmd;
|
||||
uint i;
|
||||
|
||||
printf("before : %s\n", cmd->cmd);
|
||||
new_cmd = format_quotes_string(cmd->cmd);
|
||||
if (!new_cmd)
|
||||
return (-1);
|
||||
free(cmd->cmd);
|
||||
cmd->cmd = new_cmd;
|
||||
printf("after : %s\n", cmd->cmd);
|
||||
i = 0;
|
||||
while (cmd->argv[i])
|
||||
{
|
||||
printf("before : %s\n", cmd->argv[i]);
|
||||
new_cmd = format_quotes_string(cmd->argv[i]);
|
||||
if (!new_cmd)
|
||||
return (-1);
|
||||
free(cmd->argv[i]);
|
||||
cmd->argv[i] = new_cmd;
|
||||
printf("after : %s\n", cmd->argv[i]);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int format_quotes(t_list *list_cmd)
|
||||
{
|
||||
printf("aled\n");
|
||||
if (!list_cmd)
|
||||
return (0);
|
||||
while (list_cmd)
|
||||
{
|
||||
format_quotes_cmd(list_cmd->content);
|
||||
list_cmd = list_cmd->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/05/20 09:19:39 by mmoussou #+# #+# */
|
||||
/* Updated: 2024/06/05 01:21:06 by mmoussou ### ########.fr */
|
||||
/* Updated: 2024/07/03 10:08:18 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -40,6 +40,7 @@ int fd_manager(int mode)
|
||||
index++;
|
||||
ft_strlcat(path, "/tmp/.minishell-heredoc-", 24);
|
||||
ft_strlcat(path, index_itoa, ft_strlen(index_itoa));
|
||||
free(index_itoa);
|
||||
if (mode > 0)
|
||||
fd = open(path, O_RDONLY);
|
||||
else
|
||||
|
29
src/main.c
29
src/main.c
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */
|
||||
/* Updated: 2024/06/30 17:28:28 by adjoly ### ########.fr */
|
||||
/* Updated: 2024/07/04 10:49:59 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -100,7 +100,7 @@ int main(int ac, char **av, char **env)
|
||||
piped = NULL;
|
||||
if (env_init(env, &env_l))
|
||||
return (EXIT_FAILURE);
|
||||
sigemptyset(&(sigset_t){SIGQUIT});
|
||||
//sigemptyset(&(sigset_t){ SIGQUIT });
|
||||
signal(SIGINT, &sigggg);
|
||||
signal(SIGQUIT, &siggg_backslash);
|
||||
//signal(SIGSEGV, &siggg_d);
|
||||
@ -108,6 +108,8 @@ int main(int ac, char **av, char **env)
|
||||
{
|
||||
prompt = get_prompt(env_l);
|
||||
test = readline(prompt);
|
||||
if (!test)
|
||||
exit(727);
|
||||
free(prompt);
|
||||
add_history(test);
|
||||
if (check_syntax(test))
|
||||
@ -115,28 +117,6 @@ int main(int ac, char **av, char **env)
|
||||
lll = ft_split(test, ' ');
|
||||
if (!*lll)
|
||||
continue ;
|
||||
else if (is_str(test, "pwd"))
|
||||
{
|
||||
ft_pwd();
|
||||
continue ;
|
||||
}
|
||||
else if (is_str(test, "export"))
|
||||
{
|
||||
ft_export(NULL, &env_l);
|
||||
continue ;
|
||||
}
|
||||
else if (is_str(test, "cd"))
|
||||
{
|
||||
ft_cd(&env_l, lll[1]);
|
||||
continue ;
|
||||
}
|
||||
else if (is_str(test, "echo"))
|
||||
{
|
||||
ft_echo(lll + 1);
|
||||
continue ;
|
||||
}
|
||||
else if (is_str(test, "exit"))
|
||||
exit(EXIT_SUCCESS);
|
||||
if (check_quote(test))
|
||||
continue ;
|
||||
if (check_pipe(test))
|
||||
@ -145,6 +125,7 @@ int main(int ac, char **av, char **env)
|
||||
if (check_redir(((t_token *)(piped->content))->redirection))
|
||||
continue ;
|
||||
cmd_list = get_cmd_list(piped);
|
||||
format_quotes(cmd_list);
|
||||
exec_split_cmd(cmd_list, &env_l);
|
||||
free(test);
|
||||
ft_lstclear(&piped, free_token);
|
||||
|
Reference in New Issue
Block a user