implemented outfile redirections
This commit is contained in:
3
Makefile
3
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/21 21:48:25 by marde-vr ### ########.fr #
|
# Updated: 2024/02/26 20:40:19 by marde-vr ### ########.fr #
|
||||||
# #
|
# #
|
||||||
# **************************************************************************** #
|
# **************************************************************************** #
|
||||||
|
|
||||||
@ -29,6 +29,7 @@ SRCS_RAW = main.c\
|
|||||||
lst_alias.c\
|
lst_alias.c\
|
||||||
minishellrc.c\
|
minishellrc.c\
|
||||||
path.c\
|
path.c\
|
||||||
|
here_doc.c\
|
||||||
export.c
|
export.c
|
||||||
|
|
||||||
OBJS_DIR = objs/
|
OBJS_DIR = objs/
|
||||||
|
77
srcs/exec.c
77
srcs/exec.c
@ -6,7 +6,7 @@
|
|||||||
/* 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/02/26 16:33:16 by marde-vr ### ########.fr */
|
/* Updated: 2024/02/26 21:13:46 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -81,6 +81,7 @@ int get_cmd_count(t_cmd *cmds)
|
|||||||
{
|
{
|
||||||
if (cur_cmd->type != ARG && (cur_cmd->next
|
if (cur_cmd->type != ARG && (cur_cmd->next
|
||||||
&& cur_cmd->next->type == ARG))
|
&& cur_cmd->next->type == ARG))
|
||||||
|
if (cur_cmd->type == PIPE)
|
||||||
count++;
|
count++;
|
||||||
cur_cmd = cur_cmd->next;
|
cur_cmd = cur_cmd->next;
|
||||||
}
|
}
|
||||||
@ -138,7 +139,7 @@ int is_fd_open(int fd) // debug
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
ft_printf_fd(2, "%d:open\n", fd);
|
ft_printf_fd(2, "%d:open\n", fd);
|
||||||
return 1;
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
void redirect_input(t_msh *msh, int i)
|
void redirect_input(t_msh *msh, int i)
|
||||||
@ -196,8 +197,7 @@ void pipe_child(t_msh *msh, char **cmd_args, int i)
|
|||||||
if (msh->cmds->token && (!ft_strcmp(msh->cmds->token, "cd")
|
if (msh->cmds->token && (!ft_strcmp(msh->cmds->token, "cd")
|
||||||
|| !ft_strcmp(msh->cmds->token, "alias")
|
|| !ft_strcmp(msh->cmds->token, "alias")
|
||||||
|| !ft_strcmp(msh->cmds->token, "unalias")
|
|| !ft_strcmp(msh->cmds->token, "unalias")
|
||||||
|| !ft_strcmp(msh->cmds->token, "exit")
|
|| !ft_strcmp(msh->cmds->token, "exit") || exec_builtin(msh)))
|
||||||
|| exec_builtin(msh)))
|
|
||||||
ft_exit(msh, 1);
|
ft_exit(msh, 1);
|
||||||
if (msh->cmds->token)
|
if (msh->cmds->token)
|
||||||
execve(msh->cmds->token, cmd_args, env_to_char_tab(msh->env));
|
execve(msh->cmds->token, cmd_args, env_to_char_tab(msh->env));
|
||||||
@ -278,50 +278,65 @@ char **get_cmd_args(t_msh *msh)
|
|||||||
|
|
||||||
void remove_command_from_msh(t_msh *msh)
|
void remove_command_from_msh(t_msh *msh)
|
||||||
{
|
{
|
||||||
t_cmd *cmd_cur;
|
t_cmd *cur_cmd;
|
||||||
t_cmd *cmd_tmp;
|
t_cmd *cmd_tmp;
|
||||||
|
|
||||||
cmd_cur = msh->cmds;
|
cur_cmd = msh->cmds;
|
||||||
msh->out_type = ARG;
|
while (cur_cmd && cur_cmd->next)
|
||||||
while (cmd_cur && cmd_cur->next)
|
|
||||||
{
|
{
|
||||||
//if (cmd_cur->type != ARG)
|
// if (cur_cmd->type != ARG)
|
||||||
while (cmd_cur->type != ARG)
|
while (cur_cmd->type != ARG)
|
||||||
{
|
{
|
||||||
// remove cmd if not PIPE
|
// remove cmd if not PIPE
|
||||||
if (cmd_cur->type == PIPE)
|
if (cur_cmd->type == PIPE)
|
||||||
{
|
{
|
||||||
cmd_tmp = cmd_cur;
|
cmd_tmp = cur_cmd;
|
||||||
cmd_cur = cmd_cur->next;
|
cur_cmd = cur_cmd->next;
|
||||||
msh->in_type = cmd_tmp->type;
|
msh->in_type = cmd_tmp->type;
|
||||||
free(cmd_tmp->token);
|
free(cmd_tmp->token);
|
||||||
free(cmd_tmp);
|
free(cmd_tmp);
|
||||||
msh->cmds = cmd_cur;
|
msh->cmds = cur_cmd;
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
cmd_tmp = cmd_cur;
|
cmd_tmp = cur_cmd;
|
||||||
cmd_cur = cmd_cur->next;
|
cur_cmd = cur_cmd->next;
|
||||||
msh->out_type = cmd_cur->type;
|
msh->in_type = cur_cmd->type;
|
||||||
free(cmd_tmp->token);
|
free(cmd_tmp->token);
|
||||||
free(cmd_tmp);
|
free(cmd_tmp);
|
||||||
msh->cmds = cmd_cur;
|
msh->cmds = cur_cmd;
|
||||||
}
|
}
|
||||||
if (msh->out_type == RED_O)
|
if (msh->in_type == HERE_DOC)
|
||||||
msh->out_fd = open(cmd_cur->token, O_CREAT | O_RDWR | O_TRUNC, 0644);
|
{
|
||||||
if (msh->out_type == RED_O_APP)
|
handle_here_doc(msh, cur_cmd->token);
|
||||||
msh->out_fd = open(cmd_cur->token, O_CREAT | O_RDWR | O_APPEND, 0644);
|
ft_printf_fd(2, "eof: %s\n", cur_cmd->next->token);
|
||||||
cmd_tmp = cmd_cur;
|
ft_printf_fd(2, "cmd: %s\n", cur_cmd->next->next->token);
|
||||||
cmd_cur = cmd_cur->next;
|
}
|
||||||
|
if (msh->in_type == RED_I)
|
||||||
|
{
|
||||||
|
ft_printf_fd(2, "opening %s\n", cur_cmd->token);
|
||||||
|
msh->in_fd = open(cur_cmd->token, O_RDONLY);
|
||||||
|
if (msh->in_fd == -1)
|
||||||
|
{
|
||||||
|
ft_printf_fd(2, "minishell: %s", cur_cmd->token);
|
||||||
|
perror("");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cmd_tmp = cur_cmd;
|
||||||
|
cur_cmd = cur_cmd->next;
|
||||||
free(cmd_tmp->token);
|
free(cmd_tmp->token);
|
||||||
free(cmd_tmp);
|
free(cmd_tmp);
|
||||||
msh->cmds = cmd_cur;
|
msh->cmds = cur_cmd;
|
||||||
}
|
}
|
||||||
msh->in_type = ARG;
|
msh->in_type = ARG;
|
||||||
|
|
||||||
|
//todo: fix this function to not remove command and split into two functions: one for removing old commands, one for input redirections
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_out_type(t_msh *msh)
|
void get_out_type(t_msh *msh)
|
||||||
{
|
{
|
||||||
t_cmd *cur_cmd;
|
t_cmd *cur_cmd;
|
||||||
|
|
||||||
|
msh->out_type = ARG;
|
||||||
cur_cmd = msh->cmds;
|
cur_cmd = msh->cmds;
|
||||||
while (cur_cmd && cur_cmd->next && cur_cmd->type == ARG)
|
while (cur_cmd && cur_cmd->next && cur_cmd->type == ARG)
|
||||||
cur_cmd = cur_cmd->next;
|
cur_cmd = cur_cmd->next;
|
||||||
@ -329,10 +344,17 @@ void get_out_type(t_msh *msh)
|
|||||||
msh->out_type = ARG;
|
msh->out_type = ARG;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//cur_cmd->token
|
|
||||||
msh->out_type = cur_cmd->type;
|
msh->out_type = cur_cmd->type;
|
||||||
|
if (msh->out_type == RED_O)
|
||||||
|
msh->out_fd = open(cur_cmd->next->token, O_CREAT | O_RDWR | 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)
|
||||||
|
ft_exit(msh, 2);
|
||||||
}
|
}
|
||||||
ft_printf_fd(2, "type: %d\n", msh->out_type);
|
//ft_printf_fd(2, "type: %d\n", msh->out_type);
|
||||||
}
|
}
|
||||||
|
|
||||||
void exec_command(t_msh *msh)
|
void exec_command(t_msh *msh)
|
||||||
@ -344,6 +366,7 @@ void exec_command(t_msh *msh)
|
|||||||
if (!msh->cmds)
|
if (!msh->cmds)
|
||||||
return ;
|
return ;
|
||||||
cmd_count = get_cmd_count(msh->cmds);
|
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->fds = ft_calloc(cmd_count, sizeof(int **));
|
||||||
msh->pids = ft_calloc(cmd_count, sizeof(int *));
|
msh->pids = ft_calloc(cmd_count, sizeof(int *));
|
||||||
if (!msh->pids || !msh->fds)
|
if (!msh->pids || !msh->fds)
|
||||||
|
101
srcs/here_doc.c
Normal file
101
srcs/here_doc.c
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* here_doc.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/02/26 20:20:31 by marde-vr #+# #+# */
|
||||||
|
/* Updated: 2024/02/26 20:40:00 by marde-vr ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
char *get_tmp_file_name(t_msh *msh/*, int argc, char **argv*/)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char *tmp_file_name;
|
||||||
|
char *res;
|
||||||
|
char *i_char;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
tmp_file_name = ".tmp";
|
||||||
|
i_char = ft_itoa(i);
|
||||||
|
res = ft_strjoin(tmp_file_name, i_char);
|
||||||
|
if (!res)
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
free(i_char);
|
||||||
|
while (/*!ft_strncmp(res, argv[argc - 1], ft_strlen(res))
|
||||||
|
||*/ !access(res, F_OK))
|
||||||
|
{
|
||||||
|
free(res);
|
||||||
|
i_char = ft_itoa(i);
|
||||||
|
res = ft_strjoin(tmp_file_name, i_char);
|
||||||
|
if (!res)
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
free(i_char);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (res);
|
||||||
|
}
|
||||||
|
|
||||||
|
int contains_newline(char *str)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
if (!str)
|
||||||
|
return (0);
|
||||||
|
while (str[i])
|
||||||
|
{
|
||||||
|
if (str[i] == '\n')
|
||||||
|
return (1);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_here_doc_input(t_msh *msh, char *eof)
|
||||||
|
{
|
||||||
|
char *line;
|
||||||
|
int new_line;
|
||||||
|
|
||||||
|
line = NULL;
|
||||||
|
new_line = 1;
|
||||||
|
ft_printf_fd(1, "> ");
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
|
free(line);
|
||||||
|
line = get_next_line(0);
|
||||||
|
if (!line && new_line)
|
||||||
|
{
|
||||||
|
ft_printf_fd(2, "\npipex: here-document delimited by end-of-file\n");
|
||||||
|
break ;
|
||||||
|
}
|
||||||
|
if (line && new_line && !ft_strncmp(line, eof, ft_strlen(eof)))
|
||||||
|
break ;
|
||||||
|
new_line = contains_newline(line);
|
||||||
|
if (new_line)
|
||||||
|
ft_printf_fd(1, "> ");
|
||||||
|
write(msh->in_fd, line, ft_strlen(line));
|
||||||
|
}
|
||||||
|
free(eof);
|
||||||
|
free(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
void handle_here_doc(t_msh *msh, char *eof)
|
||||||
|
{
|
||||||
|
char *here_doc_file;
|
||||||
|
|
||||||
|
here_doc_file = get_tmp_file_name(msh);
|
||||||
|
msh->in_fd = open(here_doc_file, O_CREAT | O_RDWR, 0644);
|
||||||
|
if (msh->in_fd == -1)
|
||||||
|
ft_exit(msh, 2);
|
||||||
|
eof = ft_strjoin_free(eof, "\n", 1);
|
||||||
|
if (!eof)
|
||||||
|
ft_exit(msh, 1);
|
||||||
|
get_here_doc_input(msh, eof);
|
||||||
|
close(msh->in_fd);
|
||||||
|
msh->in_fd = open(here_doc_file, O_RDWR, 0644);
|
||||||
|
}
|
@ -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/26 15:59:49 by marde-vr ### ########.fr */
|
/* Updated: 2024/02/26 20:30:30 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -98,5 +98,6 @@ void free_msh(t_msh *msh);
|
|||||||
char **split_paths_from_env(t_env *env);
|
char **split_paths_from_env(t_env *env);
|
||||||
void find_cmd_path(t_msh *msh, char **paths, int *found);
|
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);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Reference in New Issue
Block a user