splited execution into multiple files and normed everything
This commit is contained in:
10
Makefile
10
Makefile
@ -6,7 +6,7 @@
|
|||||||
# By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ #
|
# By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ #
|
||||||
# +#+#+#+#+#+ +#+ #
|
# +#+#+#+#+#+ +#+ #
|
||||||
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
|
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
|
||||||
# Updated: 2024/02/26 20:40:19 by marde-vr ### ########.fr #
|
# Updated: 2024/03/05 19:11:56 by marde-vr ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
@ -30,7 +30,13 @@ SRCS_RAW = main.c\
|
|||||||
minishellrc.c\
|
minishellrc.c\
|
||||||
path.c\
|
path.c\
|
||||||
here_doc.c\
|
here_doc.c\
|
||||||
export.c
|
export.c\
|
||||||
|
input_redirections.c\
|
||||||
|
output_redirections.c\
|
||||||
|
builtins.c\
|
||||||
|
commands.c\
|
||||||
|
pipe.c\
|
||||||
|
utils.c
|
||||||
|
|
||||||
OBJS_DIR = objs/
|
OBJS_DIR = objs/
|
||||||
SRCS_DIR = srcs/
|
SRCS_DIR = srcs/
|
||||||
|
24
other/is_fd_open.c
Normal file
24
other/is_fd_open.c
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* is_fd_open.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/03/05 17:31:53 by marde-vr #+# #+# */
|
||||||
|
/* Updated: 2024/03/05 17:35:39 by marde-vr ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../srcs/minishell.h"
|
||||||
|
|
||||||
|
int is_fd_open(int fd)
|
||||||
|
{
|
||||||
|
if (fcntl(fd, F_GETFL) == -1)
|
||||||
|
{
|
||||||
|
ft_printf_fd(2, "%d:closed\n", fd);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
ft_printf_fd(2, "%d:open\n", fd);
|
||||||
|
return (1);
|
||||||
|
}
|
79
srcs/builtins.c
Normal file
79
srcs/builtins.c
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* builtins.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/03/05 18:20:21 by marde-vr #+# #+# */
|
||||||
|
/* Updated: 2024/03/05 18:51:41 by marde-vr ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
int cmd_is_forkable_builtin(char *cmd_token)
|
||||||
|
{
|
||||||
|
if (!ft_strcmp(cmd_token, "echo") || !ft_strcmp(cmd_token, "ret")
|
||||||
|
|| !ft_strcmp(cmd_token, "env") || !ft_strcmp(cmd_token, "exit")
|
||||||
|
|| !ft_strcmp(cmd_token, "pwd") || !ft_strcmp(cmd_token, "export")
|
||||||
|
|| !ft_strcmp(cmd_token, "unset"))
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int cmd_is_builtin(t_msh *msh, char *cmd_token)
|
||||||
|
{
|
||||||
|
if (!cmd_token)
|
||||||
|
return (0);
|
||||||
|
else if (!ft_strcmp(cmd_token, "cd"))
|
||||||
|
{
|
||||||
|
cd(msh->cmds);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
else if (!ft_strcmp(cmd_token, "unalias"))
|
||||||
|
{
|
||||||
|
unalias(msh);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
else if (!ft_strcmp(cmd_token, "alias"))
|
||||||
|
{
|
||||||
|
alias(msh);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
else if (!ft_strcmp(cmd_token, "exit"))
|
||||||
|
{
|
||||||
|
exit_bt(msh);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
return (cmd_is_forkable_builtin(cmd_token));
|
||||||
|
}
|
||||||
|
|
||||||
|
int exec_builtin(t_msh *msh)
|
||||||
|
{
|
||||||
|
if (!msh->cmds->token)
|
||||||
|
return (STDIN_FILENO);
|
||||||
|
if (!ft_strcmp(msh->cmds->token, "echo"))
|
||||||
|
g_return_code = echo(msh->cmds->next);
|
||||||
|
else if (!ft_strcmp(msh->cmds->token, "ret"))
|
||||||
|
g_return_code = ft_atoi(msh->cmds->next->token);
|
||||||
|
else if (!ft_strcmp(msh->cmds->token, "env"))
|
||||||
|
g_return_code = print_env(msh->env);
|
||||||
|
else if (!ft_strcmp(msh->cmds->token, "exit"))
|
||||||
|
exit_bt(msh);
|
||||||
|
else if (!ft_strcmp(msh->cmds->token, "pwd"))
|
||||||
|
g_return_code = pwd();
|
||||||
|
else if (!ft_strcmp(msh->cmds->token, "cd"))
|
||||||
|
g_return_code = cd(msh->cmds);
|
||||||
|
else if (!ft_strcmp(msh->cmds->token, "export"))
|
||||||
|
g_return_code = ft_export(msh);
|
||||||
|
else if (!ft_strcmp(msh->cmds->token, "unset"))
|
||||||
|
g_return_code = ft_unset(msh);
|
||||||
|
else if (!ft_strcmp(msh->cmds->token, "alias"))
|
||||||
|
g_return_code = alias(msh);
|
||||||
|
else if (!ft_strcmp(msh->cmds->token, "unalias"))
|
||||||
|
g_return_code = unalias(msh);
|
||||||
|
else
|
||||||
|
return (STDIN_FILENO);
|
||||||
|
return (STDOUT_FILENO);
|
||||||
|
}
|
109
srcs/commands.c
Normal file
109
srcs/commands.c
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* commands.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/03/05 18:22:15 by marde-vr #+# #+# */
|
||||||
|
/* Updated: 2024/03/05 18:24:19 by marde-vr ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
int get_cmd_count(t_cmd *cmds)
|
||||||
|
{
|
||||||
|
int count;
|
||||||
|
t_cmd *cur_cmd;
|
||||||
|
|
||||||
|
count = 0;
|
||||||
|
cur_cmd = cmds;
|
||||||
|
while (cur_cmd->next != 0)
|
||||||
|
{
|
||||||
|
if (cur_cmd->type != ARG)
|
||||||
|
if (cur_cmd->type == PIPE)
|
||||||
|
count++;
|
||||||
|
cur_cmd = cur_cmd->next;
|
||||||
|
}
|
||||||
|
if (cur_cmd->type == ARG)
|
||||||
|
count++;
|
||||||
|
return (count);
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_args_count(t_cmd *cmds)
|
||||||
|
{
|
||||||
|
int count;
|
||||||
|
t_cmd *cur_cmd;
|
||||||
|
|
||||||
|
count = 0;
|
||||||
|
cur_cmd = cmds;
|
||||||
|
if (cur_cmd->type == ARG)
|
||||||
|
count++;
|
||||||
|
while (cur_cmd->next)
|
||||||
|
{
|
||||||
|
if (cur_cmd->type == PIPE)
|
||||||
|
break ;
|
||||||
|
cur_cmd = cur_cmd->next;
|
||||||
|
if (cur_cmd->type == ARG)
|
||||||
|
count++;
|
||||||
|
else if (cur_cmd->type != PIPE)
|
||||||
|
cur_cmd = cur_cmd->next;
|
||||||
|
}
|
||||||
|
return (count);
|
||||||
|
}
|
||||||
|
|
||||||
|
char **get_cmd_args(t_msh *msh)
|
||||||
|
{
|
||||||
|
char **cmd_args;
|
||||||
|
t_cmd *cur_cmd;
|
||||||
|
int args_count;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
args_count = get_args_count(msh->cmds);
|
||||||
|
cmd_args = ft_calloc(args_count + 1, sizeof(char *));
|
||||||
|
if (!cmd_args || !msh->fds)
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
cur_cmd = msh->cmds;
|
||||||
|
i = 0;
|
||||||
|
while (i < args_count)
|
||||||
|
{
|
||||||
|
if (cur_cmd->type == ARG)
|
||||||
|
{
|
||||||
|
cmd_args[i] = cur_cmd->token;
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
cur_cmd = cur_cmd->next;
|
||||||
|
cur_cmd = cur_cmd->next;
|
||||||
|
}
|
||||||
|
return (cmd_args);
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove_command_from_msh(t_msh *msh)
|
||||||
|
{
|
||||||
|
t_cmd *cur_cmd;
|
||||||
|
t_cmd *cmd_tmp;
|
||||||
|
|
||||||
|
cur_cmd = msh->cmds;
|
||||||
|
while (cur_cmd && cur_cmd->next)
|
||||||
|
{
|
||||||
|
if (cur_cmd->type == PIPE)
|
||||||
|
{
|
||||||
|
cmd_tmp = cur_cmd;
|
||||||
|
cur_cmd = cur_cmd->next;
|
||||||
|
msh->in_type = cmd_tmp->type;
|
||||||
|
free(cmd_tmp->token);
|
||||||
|
free(cmd_tmp);
|
||||||
|
msh->cmds = cur_cmd;
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
cmd_tmp = cur_cmd;
|
||||||
|
cur_cmd = cur_cmd->next;
|
||||||
|
msh->in_type = cur_cmd->type;
|
||||||
|
free(cmd_tmp->token);
|
||||||
|
free(cmd_tmp);
|
||||||
|
msh->cmds = cur_cmd;
|
||||||
|
}
|
||||||
|
msh->in_type = msh->cmds->type;
|
||||||
|
}
|
28
srcs/echo.c
28
srcs/echo.c
@ -6,26 +6,19 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/07 15:30:37 by tomoron #+# #+# */
|
/* Created: 2024/02/07 15:30:37 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/03/04 13:02:28 by marde-vr ### ########.fr */
|
/* Updated: 2024/03/05 19:00:53 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
int echo(t_cmd *args)
|
void put_args(t_cmd *args)
|
||||||
{
|
{
|
||||||
int put_nl;
|
|
||||||
int first;
|
int first;
|
||||||
|
|
||||||
put_nl = 1;
|
|
||||||
first = 1;
|
first = 1;
|
||||||
while (args && args->token && !strcmp(args->token, "-n"))
|
while (args && args->type != PIPE)
|
||||||
{
|
|
||||||
put_nl = 0;
|
|
||||||
args = args->next;
|
|
||||||
}
|
|
||||||
while (args && args->type !=PIPE)
|
|
||||||
{
|
{
|
||||||
if (args->type != ARG)
|
if (args->type != ARG)
|
||||||
args = args->next;
|
args = args->next;
|
||||||
@ -38,7 +31,20 @@ int echo(t_cmd *args)
|
|||||||
}
|
}
|
||||||
args = args->next;
|
args = args->next;
|
||||||
}
|
}
|
||||||
if (put_nl && !first)
|
}
|
||||||
|
|
||||||
|
int echo(t_cmd *args)
|
||||||
|
{
|
||||||
|
int put_nl;
|
||||||
|
|
||||||
|
put_nl = 1;
|
||||||
|
while (args && args->token && !strcmp(args->token, "-n"))
|
||||||
|
{
|
||||||
|
put_nl = 0;
|
||||||
|
args = args->next;
|
||||||
|
}
|
||||||
|
put_args(args);
|
||||||
|
if (put_nl)
|
||||||
ft_putchar_fd('\n', STDOUT_FILENO);
|
ft_putchar_fd('\n', STDOUT_FILENO);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
455
srcs/exec.c
455
srcs/exec.c
@ -6,252 +6,12 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */
|
/* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/03/05 16:22:14 by marde-vr ### ########.fr */
|
/* Updated: 2024/03/05 19:05:20 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
int cmd_is_builtin(t_msh *msh, char *cmd_token)
|
|
||||||
{
|
|
||||||
if (!cmd_token)
|
|
||||||
return (0);
|
|
||||||
else if (!ft_strcmp(cmd_token, "cd"))
|
|
||||||
{
|
|
||||||
cd(msh->cmds);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
else if (!ft_strcmp(cmd_token, "unalias"))
|
|
||||||
{
|
|
||||||
unalias(msh);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
else if (!ft_strcmp(cmd_token, "alias"))
|
|
||||||
{
|
|
||||||
alias(msh);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
else if (!ft_strcmp(cmd_token, "exit"))
|
|
||||||
{
|
|
||||||
exit_bt(msh);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
else if (!ft_strcmp(cmd_token, "echo") || !ft_strcmp(cmd_token, "ret")
|
|
||||||
|| !ft_strcmp(cmd_token, "env") || !ft_strcmp(cmd_token, "exit")
|
|
||||||
|| !ft_strcmp(cmd_token, "pwd") || !ft_strcmp(cmd_token, "export")
|
|
||||||
|| !ft_strcmp(cmd_token, "unset"))
|
|
||||||
return (1);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int exec_builtin(t_msh *msh)
|
|
||||||
{
|
|
||||||
if (!msh->cmds->token)
|
|
||||||
return (STDIN_FILENO);
|
|
||||||
if (!ft_strcmp(msh->cmds->token, "echo"))
|
|
||||||
g_return_code = echo(msh->cmds->next);
|
|
||||||
else if (!ft_strcmp(msh->cmds->token, "ret"))
|
|
||||||
g_return_code = ft_atoi(msh->cmds->next->token);
|
|
||||||
else if (!ft_strcmp(msh->cmds->token, "env"))
|
|
||||||
g_return_code = print_env(msh->env);
|
|
||||||
else if (!ft_strcmp(msh->cmds->token, "exit"))
|
|
||||||
exit_bt(msh);
|
|
||||||
else if (!ft_strcmp(msh->cmds->token, "pwd"))
|
|
||||||
g_return_code = pwd();
|
|
||||||
else if (!ft_strcmp(msh->cmds->token, "cd"))
|
|
||||||
g_return_code = cd(msh->cmds);
|
|
||||||
else if (!ft_strcmp(msh->cmds->token, "export"))
|
|
||||||
g_return_code = ft_export(msh);
|
|
||||||
else if (!ft_strcmp(msh->cmds->token, "unset"))
|
|
||||||
g_return_code = ft_unset(msh);
|
|
||||||
else if (!ft_strcmp(msh->cmds->token, "alias"))
|
|
||||||
g_return_code = alias(msh);
|
|
||||||
else if (!ft_strcmp(msh->cmds->token, "unalias"))
|
|
||||||
g_return_code = unalias(msh);
|
|
||||||
else
|
|
||||||
return (STDIN_FILENO);
|
|
||||||
return (STDOUT_FILENO);
|
|
||||||
}
|
|
||||||
|
|
||||||
int get_cmd_count(t_cmd *cmds)
|
|
||||||
{
|
|
||||||
int count;
|
|
||||||
t_cmd *cur_cmd;
|
|
||||||
|
|
||||||
count = 0;
|
|
||||||
cur_cmd = cmds;
|
|
||||||
while (cur_cmd->next != 0)
|
|
||||||
{
|
|
||||||
if (cur_cmd->type != ARG)
|
|
||||||
if (cur_cmd->type == PIPE)
|
|
||||||
count++;
|
|
||||||
cur_cmd = cur_cmd->next;
|
|
||||||
}
|
|
||||||
if (cur_cmd->type == ARG)
|
|
||||||
count++;
|
|
||||||
return (count);
|
|
||||||
}
|
|
||||||
|
|
||||||
void free_msh(t_msh *msh)
|
|
||||||
{
|
|
||||||
if (msh)
|
|
||||||
{
|
|
||||||
if (msh->cmds)
|
|
||||||
free_cmd(msh->cmds);
|
|
||||||
if (msh->env)
|
|
||||||
free_env(msh->env);
|
|
||||||
if (msh->aliases)
|
|
||||||
free_alias(msh->aliases);
|
|
||||||
if (msh->pids)
|
|
||||||
free(msh->pids);
|
|
||||||
if (msh->fds)
|
|
||||||
free(msh->fds);
|
|
||||||
free(msh);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ft_exit(t_msh *msh, int exit_code)
|
|
||||||
{
|
|
||||||
free_msh(msh);
|
|
||||||
exit(exit_code);
|
|
||||||
}
|
|
||||||
|
|
||||||
int get_args_count(t_cmd *cmds)
|
|
||||||
{
|
|
||||||
int count;
|
|
||||||
t_cmd *cur_cmd;
|
|
||||||
|
|
||||||
count = 0;
|
|
||||||
cur_cmd = cmds;
|
|
||||||
if (cur_cmd->type == ARG)
|
|
||||||
count++;
|
|
||||||
while (cur_cmd->next)
|
|
||||||
{
|
|
||||||
if (cur_cmd->type == PIPE)
|
|
||||||
break;
|
|
||||||
cur_cmd = cur_cmd->next;
|
|
||||||
if (cur_cmd->type == ARG)
|
|
||||||
count++;
|
|
||||||
else if (cur_cmd->type != PIPE)
|
|
||||||
cur_cmd = cur_cmd->next;
|
|
||||||
}
|
|
||||||
return (count);
|
|
||||||
}
|
|
||||||
|
|
||||||
int is_fd_open(int fd) // debug
|
|
||||||
{
|
|
||||||
if (fcntl(fd, F_GETFL) == -1)
|
|
||||||
{
|
|
||||||
ft_printf_fd(2, "%d:closed\n", fd);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
ft_printf_fd(2, "%d:open\n", fd);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void redirect_input(t_msh *msh, int i)
|
|
||||||
{
|
|
||||||
if (msh->in_type != PIPE)
|
|
||||||
{
|
|
||||||
if (dup2(msh->in_fd, 0) < 0)
|
|
||||||
ft_exit(msh, 1);
|
|
||||||
close(msh->in_fd);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//ft_printf_fd(2, "input_fd: %d\n", msh->fds[i - 1][0]);
|
|
||||||
if (dup2(msh->fds[i - 1][0], 0) < 0)
|
|
||||||
ft_exit(msh, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void redirect_output(t_msh *msh, int i)
|
|
||||||
{
|
|
||||||
if (msh->out_type != PIPE)
|
|
||||||
{
|
|
||||||
//ft_printf_fd(2, "output_fd: %d\n", msh->out_fd);
|
|
||||||
if (dup2(msh->out_fd, 1) < 0)
|
|
||||||
ft_exit(msh, 1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (dup2(msh->fds[i][1], 1) < 0)
|
|
||||||
ft_exit(msh, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void pipe_child(t_msh *msh, char **cmd_args, int i)
|
|
||||||
{
|
|
||||||
if (msh->in_type != ARG)
|
|
||||||
{
|
|
||||||
//ft_printf_fd(2, "redirecting input of %s of type %d with fd %d\n", msh->cmds->token, msh->in_type, msh->in_fd);
|
|
||||||
redirect_input(msh, i);
|
|
||||||
}
|
|
||||||
if (msh->out_type == PIPE || msh->out_type == RED_O || msh->out_type == RED_O_APP)
|
|
||||||
{
|
|
||||||
//ft_printf_fd(2, "redirecting output of %s of type %d with fd %d\n", msh->cmds->token, msh->out_type, msh->out_fd);
|
|
||||||
redirect_output(msh, i);
|
|
||||||
}
|
|
||||||
if (i != 0)
|
|
||||||
{
|
|
||||||
if (msh->fds[i - 1][0] > 2)
|
|
||||||
close(msh->fds[i - 1][0]);
|
|
||||||
if (msh->fds[i - 1][1] > 2)
|
|
||||||
close(msh->fds[i - 1][1]);
|
|
||||||
}
|
|
||||||
if (msh->fds[i][0] > 2)
|
|
||||||
close(msh->fds[i][0]);
|
|
||||||
if (msh->fds[i][1] > 2)
|
|
||||||
close(msh->fds[i][1]);
|
|
||||||
if (msh->cmds->token && (!ft_strcmp(msh->cmds->token, "cd")
|
|
||||||
|| !ft_strcmp(msh->cmds->token, "alias")
|
|
||||||
|| !ft_strcmp(msh->cmds->token, "unalias")
|
|
||||||
|| !ft_strcmp(msh->cmds->token, "exit") || exec_builtin(msh)))
|
|
||||||
{
|
|
||||||
while(i >= 0)
|
|
||||||
{
|
|
||||||
free(msh->fds[i]);
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
free(cmd_args);
|
|
||||||
ft_exit(msh, 1);
|
|
||||||
}
|
|
||||||
if (msh->cmds->token)
|
|
||||||
execve(msh->cmds->token, cmd_args, env_to_char_tab(msh->env));
|
|
||||||
close(0);
|
|
||||||
close(1);
|
|
||||||
close(2);
|
|
||||||
while(i >= 0)
|
|
||||||
{
|
|
||||||
free(msh->fds[i]);
|
|
||||||
i--;
|
|
||||||
}
|
|
||||||
free(cmd_args);
|
|
||||||
ft_exit(msh, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void pipe_parent(t_msh *msh, int i, int cmd_count)
|
|
||||||
{
|
|
||||||
if (i != 0)
|
|
||||||
{
|
|
||||||
if (msh->fds[i - 1][0] > 2)
|
|
||||||
close(msh->fds[i - 1][0]);
|
|
||||||
if (msh->fds[i - 1][1] > 2)
|
|
||||||
close(msh->fds[i - 1][1]);
|
|
||||||
}
|
|
||||||
if (i == cmd_count - 1)
|
|
||||||
{
|
|
||||||
if (msh->fds[i][0] > 2)
|
|
||||||
close(msh->fds[i][0]);
|
|
||||||
if (msh->fds[i][1] > 2)
|
|
||||||
close(msh->fds[i][1]);
|
|
||||||
}
|
|
||||||
if (msh->in_fd > 2)
|
|
||||||
close(msh->in_fd);
|
|
||||||
if (msh->out_fd > 2)
|
|
||||||
close(msh->out_fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
|
int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
|
||||||
{
|
{
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
@ -271,223 +31,58 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
|
|||||||
ft_exit(msh, 1);
|
ft_exit(msh, 1);
|
||||||
}
|
}
|
||||||
if (pid == 0)
|
if (pid == 0)
|
||||||
pipe_child(msh, cmd_args, i);
|
child(msh, cmd_args, i);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
pipe_parent(msh, i, cmd_count);
|
parent(msh, i, cmd_count);
|
||||||
msh->pids[i] = pid;
|
msh->pids[i] = pid;
|
||||||
free(cmd_args);
|
free(cmd_args);
|
||||||
}
|
}
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
char **get_cmd_args(t_msh *msh)
|
void exec_command(t_msh *msh, int i, int cmd_count)
|
||||||
{
|
{
|
||||||
char **cmd_args;
|
|
||||||
t_cmd *cur_cmd;
|
|
||||||
int args_count;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
args_count = get_args_count(msh->cmds);
|
|
||||||
cmd_args = ft_calloc(args_count + 1, sizeof(char *));
|
|
||||||
if (!cmd_args || !msh->fds)
|
|
||||||
ft_exit(msh, 1);
|
|
||||||
cur_cmd = msh->cmds;
|
|
||||||
//ft_printf_fd(2, "cmd: %s: args_count: %d\n", cur_cmd->token, args_count);
|
|
||||||
i = 0;
|
|
||||||
while (i < args_count)
|
|
||||||
{
|
|
||||||
if (cur_cmd->type == ARG)
|
|
||||||
{
|
|
||||||
cmd_args[i] = cur_cmd->token;
|
|
||||||
//ft_printf_fd(2, "%s[%d] = %s\n", msh->cmds->token, i, cur_cmd->token);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
cur_cmd = cur_cmd->next;
|
|
||||||
cur_cmd = cur_cmd->next;
|
|
||||||
}
|
|
||||||
return (cmd_args);
|
|
||||||
}
|
|
||||||
|
|
||||||
void remove_command_from_msh(t_msh *msh)
|
|
||||||
{
|
|
||||||
t_cmd *cur_cmd;
|
|
||||||
t_cmd *cmd_tmp;
|
|
||||||
|
|
||||||
cur_cmd = msh->cmds;
|
|
||||||
while (cur_cmd && cur_cmd->next)
|
|
||||||
{
|
|
||||||
//while (cur_cmd->type != ARG)
|
|
||||||
//{
|
|
||||||
if (cur_cmd->type == PIPE)
|
|
||||||
{
|
|
||||||
cmd_tmp = cur_cmd;
|
|
||||||
cur_cmd = cur_cmd->next;
|
|
||||||
msh->in_type = cmd_tmp->type;
|
|
||||||
free(cmd_tmp->token);
|
|
||||||
free(cmd_tmp);
|
|
||||||
msh->cmds = cur_cmd;
|
|
||||||
return ;
|
|
||||||
}
|
|
||||||
cmd_tmp = cur_cmd;
|
|
||||||
cur_cmd = cur_cmd->next;
|
|
||||||
msh->in_type = cur_cmd->type;
|
|
||||||
free(cmd_tmp->token);
|
|
||||||
free(cmd_tmp);
|
|
||||||
msh->cmds = cur_cmd;
|
|
||||||
//}
|
|
||||||
//cmd_tmp = cur_cmd;
|
|
||||||
//cur_cmd = cur_cmd->next;
|
|
||||||
//free(cmd_tmp->token);
|
|
||||||
//free(cmd_tmp);
|
|
||||||
//msh->cmds = cur_cmd;
|
|
||||||
}
|
|
||||||
msh->in_type = msh->cmds->type;
|
|
||||||
}
|
|
||||||
|
|
||||||
void get_in_type(t_msh *msh, t_cmd *cmds)
|
|
||||||
{
|
|
||||||
t_cmd *cur_cmd;
|
|
||||||
|
|
||||||
cur_cmd = cmds;
|
|
||||||
while (cur_cmd && cur_cmd->next && cur_cmd->type == ARG)
|
|
||||||
cur_cmd = cur_cmd->next;
|
|
||||||
if (cur_cmd->type)
|
|
||||||
{
|
|
||||||
msh->in_type = cur_cmd->type;
|
|
||||||
if (cur_cmd->type == HERE_DOC || cur_cmd->type == RED_I)
|
|
||||||
{
|
|
||||||
if (cur_cmd->type == HERE_DOC)
|
|
||||||
handle_here_doc(msh, cur_cmd->next->token);
|
|
||||||
if (cur_cmd->type == RED_I)
|
|
||||||
{
|
|
||||||
if (msh->in_fd != 0)
|
|
||||||
close(msh->in_fd);
|
|
||||||
msh->in_fd = open(cur_cmd->next->token, O_RDONLY | O_CREAT);
|
|
||||||
//ft_printf_fd(2, "opened %s: %d\n", cur_cmd->next->token, msh->in_fd);
|
|
||||||
if (msh->in_fd == -1 && !g_return_code)
|
|
||||||
{
|
|
||||||
ft_printf_fd(2, "minishell: %s: ", cur_cmd->next->token);
|
|
||||||
perror("");
|
|
||||||
// todo: cancel execution of all commands????????????????? idk
|
|
||||||
g_return_code = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//ft_printf_fd(2, "cmd: %s\n", msh->cmds->token);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
while (cur_cmd && cur_cmd->next && cur_cmd->next->type == ARG)
|
|
||||||
cur_cmd = cur_cmd->next;
|
|
||||||
if (cur_cmd->next && (cur_cmd->next->type == HERE_DOC || cur_cmd->next->type == RED_I))
|
|
||||||
get_in_type(msh, cur_cmd);
|
|
||||||
//ft_printf_fd(2, "in_type: %d\n", msh->in_type);
|
|
||||||
}
|
|
||||||
|
|
||||||
void get_out_type(t_msh *msh, t_cmd *cmds)
|
|
||||||
{
|
|
||||||
t_cmd *cur_cmd;
|
|
||||||
|
|
||||||
msh->out_type = ARG;
|
|
||||||
msh->out_fd = 0;
|
|
||||||
//ft_printf_fd(2, "%s\n", cmds->token);
|
|
||||||
cur_cmd = cmds;
|
|
||||||
if (cmds->type && msh->cmds == cmds)
|
|
||||||
{
|
|
||||||
while (msh->cmds->type != ARG && msh->cmds->next->next)
|
|
||||||
msh->cmds = msh->cmds->next->next;
|
|
||||||
}
|
|
||||||
while (cur_cmd && cur_cmd->next && (cur_cmd->type == ARG || cur_cmd->type > 3))
|
|
||||||
cur_cmd = cur_cmd->next;
|
|
||||||
if (!cur_cmd->type)
|
|
||||||
msh->out_type = ARG;
|
|
||||||
else
|
|
||||||
{
|
|
||||||
msh->out_type = cur_cmd->type;
|
|
||||||
if (msh->out_type == RED_O)
|
|
||||||
msh->out_fd = open(cur_cmd->next->token, O_CREAT | O_WRONLY | O_TRUNC,
|
|
||||||
0644);
|
|
||||||
if (msh->out_type == RED_O_APP)
|
|
||||||
msh->out_fd = open(cur_cmd->next->token,
|
|
||||||
O_CREAT | O_RDWR | O_APPEND, 0644);
|
|
||||||
//if (msh->out_fd) // debug
|
|
||||||
// ft_printf_fd(2, "opened %s: %d\n", cur_cmd->next->token, msh->out_fd);
|
|
||||||
if (msh->out_fd == -1)
|
|
||||||
{
|
|
||||||
g_return_code = 1;
|
|
||||||
perror("open");
|
|
||||||
return ;
|
|
||||||
}
|
|
||||||
if (cur_cmd->type != PIPE)
|
|
||||||
{
|
|
||||||
while (cur_cmd->next && cur_cmd->next->type == ARG)
|
|
||||||
cur_cmd = cur_cmd->next;
|
|
||||||
if (cur_cmd->next && (cur_cmd->next->type == RED_O || cur_cmd->next->type == RED_O_APP))
|
|
||||||
get_out_type(msh, cur_cmd);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int first_is_in_type(t_msh *msh)
|
|
||||||
{
|
|
||||||
t_cmd *cur_cmd;
|
|
||||||
|
|
||||||
cur_cmd = msh->cmds;
|
|
||||||
while (cur_cmd && cur_cmd->type == ARG && cur_cmd->next)
|
|
||||||
cur_cmd = cur_cmd->next;
|
|
||||||
if (cur_cmd->type == PIPE || cur_cmd->type == RED_I || cur_cmd->type == HERE_DOC)
|
|
||||||
return (1);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void exec_command(t_msh *msh)
|
|
||||||
{
|
|
||||||
int cmd_count;
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = 0;
|
|
||||||
if (!msh->cmds)
|
|
||||||
return ;
|
|
||||||
cmd_count = get_cmd_count(msh->cmds);
|
|
||||||
//ft_printf_fd(2, "cmd_count: %d\n", cmd_count);
|
|
||||||
msh->fds = ft_calloc(cmd_count, sizeof(int **));
|
|
||||||
msh->pids = ft_calloc(cmd_count, sizeof(int *));
|
|
||||||
if (!msh->pids || !msh->fds)
|
|
||||||
ft_exit(msh, 1);
|
|
||||||
while (i < cmd_count)
|
|
||||||
{
|
|
||||||
g_return_code = 0;
|
g_return_code = 0;
|
||||||
msh->fds[i] = ft_calloc(2, sizeof(int *));
|
msh->fds[i] = ft_calloc(2, sizeof(int *));
|
||||||
if (!msh->fds[i])
|
if (!msh->fds[i])
|
||||||
ft_exit(msh, 1);
|
ft_exit(msh, 1);
|
||||||
|
|
||||||
if (first_is_in_type(msh))
|
if (first_is_in_type(msh))
|
||||||
{
|
{
|
||||||
get_in_type(msh, msh->cmds);
|
get_in_type(msh, msh->cmds);
|
||||||
if (!g_return_code) //maybe????
|
if (!g_return_code)
|
||||||
get_out_type(msh, msh->cmds);
|
get_out_type(msh, msh->cmds);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
get_out_type(msh, msh->cmds);
|
get_out_type(msh, msh->cmds);
|
||||||
if (!g_return_code) //maybe????
|
if (!g_return_code)
|
||||||
get_in_type(msh, msh->cmds);
|
get_in_type(msh, msh->cmds);
|
||||||
}
|
}
|
||||||
|
|
||||||
//ft_printf_fd(2, "%d\n", msh->out_fd);
|
|
||||||
//ft_printf_fd(2, "cmd: %s\n", msh->cmds->token);
|
|
||||||
if (!cmd_is_builtin(msh, msh->cmds->token))
|
if (!cmd_is_builtin(msh, msh->cmds->token))
|
||||||
get_cmd_path(msh);
|
get_cmd_path(msh);
|
||||||
exec(msh, get_cmd_args(msh), i, cmd_count);
|
exec(msh, get_cmd_args(msh), i, cmd_count);
|
||||||
remove_command_from_msh(msh);
|
remove_command_from_msh(msh);
|
||||||
i++;
|
}
|
||||||
}
|
|
||||||
i = cmd_count - 1;
|
void exec_commands(t_msh *msh)
|
||||||
while (i >= 0)
|
{
|
||||||
{
|
int cmd_count;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = -1;
|
||||||
|
if (!msh->cmds)
|
||||||
|
return ;
|
||||||
|
cmd_count = get_cmd_count(msh->cmds);
|
||||||
|
msh->fds = ft_calloc(cmd_count, sizeof(int **));
|
||||||
|
msh->pids = ft_calloc(cmd_count, sizeof(int *));
|
||||||
|
if (!msh->pids || !msh->fds)
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
while (++i < cmd_count)
|
||||||
|
exec_command(msh, i, cmd_count);
|
||||||
|
i = cmd_count;
|
||||||
|
while (--i >= 0)
|
||||||
waitpid(msh->pids[i], 0, 0);
|
waitpid(msh->pids[i], 0, 0);
|
||||||
i--;
|
|
||||||
}
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < cmd_count)
|
while (i < cmd_count)
|
||||||
{
|
{
|
||||||
|
@ -6,13 +6,13 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/26 20:20:31 by marde-vr #+# #+# */
|
/* Created: 2024/02/26 20:20:31 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/02/28 17:39:18 by marde-vr ### ########.fr */
|
/* Updated: 2024/03/05 17:45:51 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
char *get_tmp_file_name(t_msh *msh/*, int argc, char **argv*/)
|
char *get_tmp_file_name(t_msh *msh)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
char *tmp_file_name;
|
char *tmp_file_name;
|
||||||
@ -26,8 +26,7 @@ char *get_tmp_file_name(t_msh *msh/*, int argc, char **argv*/)
|
|||||||
if (!res)
|
if (!res)
|
||||||
ft_exit(msh, 1);
|
ft_exit(msh, 1);
|
||||||
free(i_char);
|
free(i_char);
|
||||||
while (/*!ft_strncmp(res, argv[argc - 1], ft_strlen(res))
|
while (!access(res, F_OK))
|
||||||
||*/ !access(res, F_OK))
|
|
||||||
{
|
{
|
||||||
free(res);
|
free(res);
|
||||||
i_char = ft_itoa(i);
|
i_char = ft_itoa(i);
|
||||||
@ -70,7 +69,8 @@ void get_here_doc_input(t_msh *msh, char *eof)
|
|||||||
line = get_next_line(0);
|
line = get_next_line(0);
|
||||||
if (!line && new_line)
|
if (!line && new_line)
|
||||||
{
|
{
|
||||||
ft_printf_fd(2, "\npipex: here-document delimited by end-of-file\n");
|
ft_printf_fd(2, "\nminishell: warning: here-document delimited by");
|
||||||
|
ft_printf_fd(2, " end-of-file, wanted %s", eof);
|
||||||
break ;
|
break ;
|
||||||
}
|
}
|
||||||
if (line && new_line && !ft_strncmp(line, eof, ft_strlen(eof)))
|
if (line && new_line && !ft_strncmp(line, eof, ft_strlen(eof)))
|
||||||
|
79
srcs/input_redirections.c
Normal file
79
srcs/input_redirections.c
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* input_redirections.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */
|
||||||
|
/* Updated: 2024/03/05 19:19:33 by marde-vr ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void redirect_input(t_msh *msh, int i)
|
||||||
|
{
|
||||||
|
if (msh->in_type != PIPE)
|
||||||
|
{
|
||||||
|
if (dup2(msh->in_fd, 0) < 0)
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
close(msh->in_fd);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (dup2(msh->fds[i - 1][0], 0) < 0)
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void open_input_file(t_msh *msh, t_cmd **cur_cmd)
|
||||||
|
{
|
||||||
|
if ((*cur_cmd)->type == HERE_DOC)
|
||||||
|
handle_here_doc(msh, (*cur_cmd)->next->token);
|
||||||
|
if ((*cur_cmd)->type == RED_I)
|
||||||
|
{
|
||||||
|
if (msh->in_fd != 0)
|
||||||
|
close(msh->in_fd);
|
||||||
|
msh->in_fd = open((*cur_cmd)->next->token, O_RDONLY | O_CREAT);
|
||||||
|
if (msh->in_fd == -1 && !g_return_code)
|
||||||
|
{
|
||||||
|
ft_printf_fd(2, "minishell: %s: ", (*cur_cmd)->next->token);
|
||||||
|
perror("");
|
||||||
|
g_return_code = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_in_type(t_msh *msh, t_cmd *cmds)
|
||||||
|
{
|
||||||
|
t_cmd *cur_cmd;
|
||||||
|
|
||||||
|
cur_cmd = cmds;
|
||||||
|
while (cur_cmd && cur_cmd->next && cur_cmd->type == ARG)
|
||||||
|
cur_cmd = cur_cmd->next;
|
||||||
|
if (cur_cmd->type)
|
||||||
|
{
|
||||||
|
msh->in_type = cur_cmd->type;
|
||||||
|
if (cur_cmd->type == HERE_DOC || cur_cmd->type == RED_I)
|
||||||
|
open_input_file(msh, &cur_cmd);
|
||||||
|
}
|
||||||
|
while (cur_cmd && cur_cmd->next && cur_cmd->next->type == ARG)
|
||||||
|
cur_cmd = cur_cmd->next;
|
||||||
|
if (cur_cmd->next && (cur_cmd->next->type == HERE_DOC
|
||||||
|
|| cur_cmd->next->type == RED_I))
|
||||||
|
get_in_type(msh, cur_cmd);
|
||||||
|
}
|
||||||
|
|
||||||
|
int first_is_in_type(t_msh *msh)
|
||||||
|
{
|
||||||
|
t_cmd *cur_cmd;
|
||||||
|
|
||||||
|
cur_cmd = msh->cmds;
|
||||||
|
while (cur_cmd && cur_cmd->type == ARG && cur_cmd->next)
|
||||||
|
cur_cmd = cur_cmd->next;
|
||||||
|
if (cur_cmd->type == PIPE || cur_cmd->type == RED_I
|
||||||
|
|| cur_cmd->type == HERE_DOC)
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
19
srcs/main.c
19
srcs/main.c
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */
|
/* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/03/05 13:20:39 by marde-vr ### ########.fr */
|
/* Updated: 2024/03/05 18:30:57 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -82,26 +82,25 @@ int init_minishell(t_msh **msh, int argc, char **argv, char **envp)
|
|||||||
|
|
||||||
int main(int argc, char **argv, char **envp)
|
int main(int argc, char **argv, char **envp)
|
||||||
{
|
{
|
||||||
char *command;
|
char *commands;
|
||||||
char *prompt;
|
char *prompt;
|
||||||
t_msh *msh;
|
t_msh *msh;
|
||||||
|
|
||||||
command = (char *)1;
|
commands = (char *)1;
|
||||||
init_minishell(&msh, argc, argv, envp);
|
init_minishell(&msh, argc, argv, envp);
|
||||||
handle_minishellrc(msh);
|
handle_minishellrc(msh);
|
||||||
while (msh->env && command)
|
while (msh->env && commands)
|
||||||
{
|
{
|
||||||
prompt = get_prompt(msh->env);
|
prompt = get_prompt(msh->env);
|
||||||
if (!prompt)
|
if (!prompt)
|
||||||
exit(STDIN_FILENO);
|
exit(STDIN_FILENO);
|
||||||
command = readline(prompt);
|
commands = readline(prompt);
|
||||||
free(prompt);
|
free(prompt);
|
||||||
add_history(command);
|
add_history(commands);
|
||||||
msh->cmds = parse_command(command, msh->env);
|
msh->cmds = parse_command(commands, msh->env);
|
||||||
free(command);
|
free(commands);
|
||||||
msh->cmds = handle_alias(msh);
|
msh->cmds = handle_alias(msh);
|
||||||
//print_parsed_cmd(msh->cmds); // debug
|
exec_commands(msh);
|
||||||
exec_command(msh);
|
|
||||||
free_cmd(msh->cmds);
|
free_cmd(msh->cmds);
|
||||||
}
|
}
|
||||||
rl_clear_history();
|
rl_clear_history();
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
|
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/28 12:46:27 by marde-vr ### ########.fr */
|
/* Updated: 2024/03/05 19:05:34 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -69,7 +69,7 @@ extern int g_return_code;
|
|||||||
|
|
||||||
t_cmd *cmd_add_back(t_cmd *res, char *token, t_token_type type);
|
t_cmd *cmd_add_back(t_cmd *res, char *token, t_token_type type);
|
||||||
void free_cmd(t_cmd *cmd);
|
void free_cmd(t_cmd *cmd);
|
||||||
void exec_command(t_msh *msh);
|
void exec_commands(t_msh *msh);
|
||||||
int echo(t_cmd *args);
|
int echo(t_cmd *args);
|
||||||
void exit_bt(t_msh *msh);
|
void exit_bt(t_msh *msh);
|
||||||
t_env *env_add_back(t_env *env, char *name, char *value);
|
t_env *env_add_back(t_env *env, char *name, char *value);
|
||||||
@ -100,5 +100,20 @@ void find_cmd_path(t_msh *msh, char **paths, int *found);
|
|||||||
void get_cmd_path(t_msh *msh);
|
void get_cmd_path(t_msh *msh);
|
||||||
void handle_here_doc(t_msh *msh, char *eof);
|
void handle_here_doc(t_msh *msh, char *eof);
|
||||||
int ft_unset(t_msh *msh);
|
int ft_unset(t_msh *msh);
|
||||||
|
void get_in_type(t_msh *msh, t_cmd *cmds);
|
||||||
|
void get_out_type(t_msh *msh, t_cmd *cmds);
|
||||||
|
int first_is_in_type(t_msh *msh);
|
||||||
|
void redirect_input(t_msh *msh, int i);
|
||||||
|
void redirect_output(t_msh *msh, int i);
|
||||||
|
void child(t_msh *msh, char **cmd_args, int i);
|
||||||
|
void parent(t_msh *msh, int i, int cmd_count);
|
||||||
|
void free_msh(t_msh *msh);
|
||||||
|
void ft_exit(t_msh *msh, int exit_code);
|
||||||
|
int cmd_is_builtin(t_msh *msh, char *cmd_token);
|
||||||
|
int exec_builtin(t_msh *msh);
|
||||||
|
int get_cmd_count(t_cmd *cmds);
|
||||||
|
int get_args_count(t_cmd *cmds);
|
||||||
|
char **get_cmd_args(t_msh *msh);
|
||||||
|
void remove_command_from_msh(t_msh *msh);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/16 17:40:16 by marde-vr #+# #+# */
|
/* Created: 2024/02/16 17:40:16 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/02/29 13:13:15 by marde-vr ### ########.fr */
|
/* Updated: 2024/03/05 18:33:30 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -22,9 +22,8 @@ void exec_rc_file(t_msh *msh, int fd)
|
|||||||
if (line[0] != '#')
|
if (line[0] != '#')
|
||||||
{
|
{
|
||||||
msh->cmds = parse_command(line, msh->env);
|
msh->cmds = parse_command(line, msh->env);
|
||||||
exec_command(msh);
|
exec_commands(msh);
|
||||||
free_cmd(msh->cmds);
|
free_cmd(msh->cmds);
|
||||||
//free(line);
|
|
||||||
}
|
}
|
||||||
free(line);
|
free(line);
|
||||||
line = get_next_line(fd);
|
line = get_next_line(fd);
|
||||||
|
73
srcs/output_redirections.c
Normal file
73
srcs/output_redirections.c
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* output_redirections.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/03/05 19:10:52 by marde-vr #+# #+# */
|
||||||
|
/* Updated: 2024/03/05 19:28:25 by marde-vr ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void redirect_output(t_msh *msh, int i)
|
||||||
|
{
|
||||||
|
if (msh->out_type != PIPE)
|
||||||
|
{
|
||||||
|
if (dup2(msh->out_fd, 1) < 0)
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (dup2(msh->fds[i][1], 1) < 0)
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void open_out_file(t_msh *msh, t_cmd **cur_cmd)
|
||||||
|
{
|
||||||
|
msh->out_type = (*cur_cmd)->type;
|
||||||
|
if (msh->out_type == RED_O)
|
||||||
|
msh->out_fd = open((*cur_cmd)->next->token,
|
||||||
|
O_CREAT | O_WRONLY | O_TRUNC, 0644);
|
||||||
|
if (msh->out_type == RED_O_APP)
|
||||||
|
msh->out_fd = open((*cur_cmd)->next->token,
|
||||||
|
O_CREAT | O_RDWR | O_APPEND, 0644);
|
||||||
|
if (msh->out_fd == -1)
|
||||||
|
{
|
||||||
|
g_return_code = 1;
|
||||||
|
perror("open");
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
if ((*cur_cmd)->type != PIPE)
|
||||||
|
{
|
||||||
|
while ((*cur_cmd)->next && (*cur_cmd)->next->type == ARG)
|
||||||
|
*cur_cmd = (*cur_cmd)->next;
|
||||||
|
if ((*cur_cmd)->next && ((*cur_cmd)->next->type == RED_O
|
||||||
|
|| (*cur_cmd)->next->type == RED_O_APP))
|
||||||
|
get_out_type(msh, *cur_cmd);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_out_type(t_msh *msh, t_cmd *cmds)
|
||||||
|
{
|
||||||
|
t_cmd *cur_cmd;
|
||||||
|
|
||||||
|
msh->out_type = ARG;
|
||||||
|
msh->out_fd = 0;
|
||||||
|
cur_cmd = cmds;
|
||||||
|
if (cmds->type && msh->cmds == cmds)
|
||||||
|
{
|
||||||
|
while (msh->cmds->type != ARG && msh->cmds->next->next)
|
||||||
|
msh->cmds = msh->cmds->next->next;
|
||||||
|
}
|
||||||
|
while (cur_cmd && cur_cmd->next && (cur_cmd->type == ARG
|
||||||
|
|| cur_cmd->type > 3))
|
||||||
|
cur_cmd = cur_cmd->next;
|
||||||
|
if (!cur_cmd->type)
|
||||||
|
msh->out_type = ARG;
|
||||||
|
else
|
||||||
|
open_out_file(msh, &cur_cmd);
|
||||||
|
}
|
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/09 15:24:36 by tomoron #+# #+# */
|
/* Created: 2024/02/09 15:24:36 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/29 15:31:25 by tomoron ### ########.fr */
|
/* Updated: 2024/03/05 17:29:06 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -28,8 +28,8 @@ int get_var_len(char **command, t_env *env)
|
|||||||
char *env_var;
|
char *env_var;
|
||||||
|
|
||||||
(*command)++;
|
(*command)++;
|
||||||
if(**command == '\'' || **command == '"')
|
if (**command == '\'' || **command == '"')
|
||||||
return(1);
|
return (1);
|
||||||
if (!ft_isalnum(**command) && **command != '_' && **command != '?')
|
if (!ft_isalnum(**command) && **command != '_' && **command != '?')
|
||||||
return (2);
|
return (2);
|
||||||
if (**command == '?')
|
if (**command == '?')
|
||||||
@ -64,8 +64,8 @@ int get_token_len(char *command, t_env *env)
|
|||||||
res += ft_strlen(getenv("HOME"));
|
res += ft_strlen(getenv("HOME"));
|
||||||
else if (*command != '\'' && *command != '"')
|
else if (*command != '\'' && *command != '"')
|
||||||
res++;
|
res++;
|
||||||
else if ((*command == '\'' && in_dquote)
|
else if ((*command == '\'' && in_dquote) || (*command == '"'
|
||||||
|| (*command == '"' && in_quote))
|
&& in_quote))
|
||||||
res++;
|
res++;
|
||||||
command++;
|
command++;
|
||||||
}
|
}
|
||||||
@ -95,11 +95,11 @@ int add_var_to_str(char *res, char **command, t_env *env)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
if(**command == '\'' || **command == '"')
|
if (**command == '\'' || **command == '"')
|
||||||
{
|
{
|
||||||
*res = '$';
|
*res = '$';
|
||||||
(*command)--;
|
(*command)--;
|
||||||
return(1);
|
return (1);
|
||||||
}
|
}
|
||||||
if (!ft_isalnum(**command) && **command != '_' && **command != '?')
|
if (!ft_isalnum(**command) && **command != '_' && **command != '?')
|
||||||
{
|
{
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/21 21:47:15 by marde-vr #+# #+# */
|
/* Created: 2024/02/21 21:47:15 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/02/26 14:49:52 by marde-vr ### ########.fr */
|
/* Updated: 2024/03/05 17:30:27 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -70,7 +70,7 @@ void free_paths(char **paths)
|
|||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
while(paths[i])
|
while (paths[i])
|
||||||
{
|
{
|
||||||
free(paths[i]);
|
free(paths[i]);
|
||||||
i++;
|
i++;
|
||||||
|
90
srcs/pipe.c
Normal file
90
srcs/pipe.c
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* pipe.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */
|
||||||
|
/* Updated: 2024/03/05 19:14:17 by marde-vr ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void close_pipe_fds(t_msh *msh, int i)
|
||||||
|
{
|
||||||
|
if (i != 0)
|
||||||
|
{
|
||||||
|
if (msh->fds[i - 1][0] > 2)
|
||||||
|
close(msh->fds[i - 1][0]);
|
||||||
|
if (msh->fds[i - 1][1] > 2)
|
||||||
|
close(msh->fds[i - 1][1]);
|
||||||
|
}
|
||||||
|
if (msh->fds[i][0] > 2)
|
||||||
|
close(msh->fds[i][0]);
|
||||||
|
if (msh->fds[i][1] > 2)
|
||||||
|
close(msh->fds[i][1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
void execute_command(t_msh *msh, char **cmd_args, int i)
|
||||||
|
{
|
||||||
|
if (msh->cmds->token && (!ft_strcmp(msh->cmds->token, "cd")
|
||||||
|
|| !ft_strcmp(msh->cmds->token, "alias")
|
||||||
|
|| !ft_strcmp(msh->cmds->token, "unalias")
|
||||||
|
|| !ft_strcmp(msh->cmds->token, "exit") || exec_builtin(msh)))
|
||||||
|
{
|
||||||
|
while (i >= 0)
|
||||||
|
{
|
||||||
|
free(msh->fds[i]);
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
free(cmd_args);
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
}
|
||||||
|
if (msh->cmds->token)
|
||||||
|
execve(msh->cmds->token, cmd_args, env_to_char_tab(msh->env));
|
||||||
|
}
|
||||||
|
|
||||||
|
void child(t_msh *msh, char **cmd_args, int i)
|
||||||
|
{
|
||||||
|
if (msh->in_type != ARG)
|
||||||
|
redirect_input(msh, i);
|
||||||
|
if (msh->out_type == PIPE || msh->out_type == RED_O
|
||||||
|
|| msh->out_type == RED_O_APP)
|
||||||
|
redirect_output(msh, i);
|
||||||
|
close_pipe_fds(msh, i);
|
||||||
|
execute_command(msh, cmd_args, i);
|
||||||
|
close(0);
|
||||||
|
close(1);
|
||||||
|
close(2);
|
||||||
|
while (i >= 0)
|
||||||
|
{
|
||||||
|
free(msh->fds[i]);
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
free(cmd_args);
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void parent(t_msh *msh, int i, int cmd_count)
|
||||||
|
{
|
||||||
|
if (i != 0)
|
||||||
|
{
|
||||||
|
if (msh->fds[i - 1][0] > 2)
|
||||||
|
close(msh->fds[i - 1][0]);
|
||||||
|
if (msh->fds[i - 1][1] > 2)
|
||||||
|
close(msh->fds[i - 1][1]);
|
||||||
|
}
|
||||||
|
if (i == cmd_count - 1)
|
||||||
|
{
|
||||||
|
if (msh->fds[i][0] > 2)
|
||||||
|
close(msh->fds[i][0]);
|
||||||
|
if (msh->fds[i][1] > 2)
|
||||||
|
close(msh->fds[i][1]);
|
||||||
|
}
|
||||||
|
if (msh->in_fd > 2)
|
||||||
|
close(msh->in_fd);
|
||||||
|
if (msh->out_fd > 2)
|
||||||
|
close(msh->out_fd);
|
||||||
|
}
|
37
srcs/utils.c
Normal file
37
srcs/utils.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/03/05 18:19:26 by marde-vr #+# #+# */
|
||||||
|
/* Updated: 2024/03/05 18:19:32 by marde-vr ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void free_msh(t_msh *msh)
|
||||||
|
{
|
||||||
|
if (msh)
|
||||||
|
{
|
||||||
|
if (msh->cmds)
|
||||||
|
free_cmd(msh->cmds);
|
||||||
|
if (msh->env)
|
||||||
|
free_env(msh->env);
|
||||||
|
if (msh->aliases)
|
||||||
|
free_alias(msh->aliases);
|
||||||
|
if (msh->pids)
|
||||||
|
free(msh->pids);
|
||||||
|
if (msh->fds)
|
||||||
|
free(msh->fds);
|
||||||
|
free(msh);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void ft_exit(t_msh *msh, int exit_code)
|
||||||
|
{
|
||||||
|
free_msh(msh);
|
||||||
|
exit(exit_code);
|
||||||
|
}
|
Reference in New Issue
Block a user