Merge branch 'master' into sadjigui
This commit is contained in:
commit
8c0116d52d
6
Makefile
6
Makefile
@ -6,7 +6,7 @@
|
||||
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2022/03/06 12:50:24 by apommier #+# #+# #
|
||||
# Updated: 2022/04/16 02:25:09 by apommier ### ########.fr #
|
||||
# Updated: 2022/04/16 09:58:10 by apommier ### ########.fr #
|
||||
# #
|
||||
# **************************************************************************** #
|
||||
|
||||
@ -40,6 +40,8 @@ LIB = -lreadline
|
||||
RM = rm -rf
|
||||
LIBFT = ./libft
|
||||
|
||||
#-include ./valgrind.mk
|
||||
|
||||
${NAME}: ${OBJS}
|
||||
@make bonus -C ${LIBFT}
|
||||
@${CC} ${LIB} ${OBJS} ${LIBFT}/libft.a -o ${NAME}
|
||||
@ -58,4 +60,4 @@ re: fclean all
|
||||
|
||||
.PHONY: all clean fclean re
|
||||
|
||||
-include ./valgrind.mk
|
||||
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/09 22:33:49 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/16 02:39:21 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/16 16:10:54 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -50,6 +50,8 @@ typedef struct s_simple {
|
||||
// Describes a complete command with the multiple pipes if any
|
||||
// and input/output redirection if any.
|
||||
typedef struct s_command {
|
||||
int tmpin;
|
||||
int tmpout;
|
||||
char **env;
|
||||
int nb_s_cmd;
|
||||
struct s_simple **s_cmds;
|
||||
|
||||
1
launch_minishell
Executable file
1
launch_minishell
Executable file
@ -0,0 +1 @@
|
||||
valgrind --leak-check=full --show-leak-kinds=all --suppressions=ignoreliberror --track-fds=yes ./minishell
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/06 13:27:11 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/16 02:50:22 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/16 13:54:16 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -72,14 +72,17 @@ void print_prompt(char **path)
|
||||
if (ft_strlen(input) && next_space(input, 0) && input)
|
||||
{
|
||||
cmd = set_cmd(input, path, err_var);
|
||||
//path = ft_dup_double(path);
|
||||
free_double(path);
|
||||
if (cmd)
|
||||
{
|
||||
//cmd->err_var = 0;
|
||||
execute(cmd, path);
|
||||
execute(cmd, cmd->env);
|
||||
err_var = cmd->err_var;
|
||||
|
||||
path = ft_dup_double(cmd->env);
|
||||
//cmd = 0;
|
||||
free_cmd(cmd);
|
||||
cmd = 0;
|
||||
}
|
||||
else
|
||||
ft_putstr_fd("Minishell: error while parsing command\n", 2);
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/02 18:51:31 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/15 06:29:09 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/16 16:14:43 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -91,16 +91,23 @@ void exec_cmd(t_cmd *cmd, char **env, int *fdpipe)
|
||||
ft_putstr_fd("Minishell: command not found: ", 2);
|
||||
ft_putstr_fd(cmd->current_s_cmd->cmd + 1, 2);
|
||||
ft_putstr_fd("\n", 2);
|
||||
exit(0);
|
||||
exit(127);
|
||||
}
|
||||
if (-1 == execve(cmd->current_s_cmd->cmd, cmd->current_s_cmd->args, env))
|
||||
ft_putstr_fd("Minishell: exec error\n", 2);
|
||||
exit(0);
|
||||
exit(126);
|
||||
}
|
||||
else if (!cmd->current_s_cmd->cmd || access(cmd->current_s_cmd->cmd, F_OK))
|
||||
cmd->err_var = 127;
|
||||
else
|
||||
cmd->err_var = 126;
|
||||
/*if (cmd->current_s_cmd->child)
|
||||
{
|
||||
if (cmd->current_s_cmd->infile)
|
||||
close(cmd->current_s_cmd->infile);
|
||||
if (cmd->current_s_cmd->outfile)
|
||||
close(cmd->current_s_cmd->outfile);
|
||||
}*/
|
||||
}
|
||||
|
||||
void execute(t_cmd *cmd, char **env)
|
||||
@ -108,31 +115,39 @@ void execute(t_cmd *cmd, char **env)
|
||||
int fdpipe[2];
|
||||
int fdout;
|
||||
int fdin;
|
||||
int tmpin;
|
||||
int tmpout;
|
||||
int tmpin = -1;
|
||||
int tmpout = -1;
|
||||
int i;
|
||||
|
||||
|
||||
|
||||
i = 0;
|
||||
fdpipe[1] = -1;
|
||||
tmpin = dup(0);
|
||||
tmpout = dup(1);
|
||||
cmd->tmpin = tmpin;
|
||||
cmd->tmpout = tmpout;
|
||||
//printf("infile= %s\n", cmd->current_s_cmd->infile);
|
||||
if (cmd->current_s_cmd->infile)
|
||||
{
|
||||
fdin = open(cmd->current_s_cmd->infile, O_RDWR);
|
||||
if (fdin < 0)
|
||||
printf("Minishell: open error\n");
|
||||
printf("Minishell: open : bad file descriptor\n");
|
||||
}
|
||||
else
|
||||
fdin=dup(tmpin);
|
||||
fdin = dup(tmpin);
|
||||
while(cmd->current_s_cmd)
|
||||
{
|
||||
{
|
||||
if (i > 0)
|
||||
close(fdout);
|
||||
cmd->current_s_cmd->child = 1;
|
||||
fdout = 0;
|
||||
fdout = -1;
|
||||
if (i > 0 && cmd->current_s_cmd->infile)
|
||||
{
|
||||
close(fdin);
|
||||
fdin = open(cmd->current_s_cmd->infile, O_RDWR);
|
||||
if (fdin < 0)
|
||||
printf("Minishell: open error\n");
|
||||
printf("Minishell: open : bad file descriptor\n");
|
||||
}
|
||||
if (i == cmd->nb_s_cmd - 1)
|
||||
{
|
||||
@ -140,7 +155,7 @@ void execute(t_cmd *cmd, char **env)
|
||||
if (cmd->current_s_cmd->outfile)
|
||||
fdout = open(cmd->current_s_cmd->outfile, O_RDWR | O_CREAT | O_APPEND, 0666);
|
||||
else
|
||||
fdout=dup(tmpout);
|
||||
fdout = dup(tmpout);
|
||||
cmd->current_s_cmd->fd[0] = fdin;
|
||||
cmd->current_s_cmd->fd[1] = fdout;
|
||||
if (i == 0 && is_builtin(cmd->current_s_cmd->cmd))
|
||||
@ -163,6 +178,7 @@ void execute(t_cmd *cmd, char **env)
|
||||
fdin=fdpipe[0];
|
||||
exec_cmd(cmd, env, fdpipe);
|
||||
close(cmd->current_s_cmd->fd[0]);
|
||||
close(fdin);
|
||||
}
|
||||
if (fdpipe[1] > 0)
|
||||
close(fdpipe[1]);
|
||||
@ -171,9 +187,13 @@ void execute(t_cmd *cmd, char **env)
|
||||
//printf("cmd->err_var= %d\n", cmd->err_var);
|
||||
}
|
||||
close_pipe(cmd);
|
||||
wait_exit(cmd);
|
||||
dup2(tmpin, 0);
|
||||
dup2(tmpout, 1);
|
||||
close(tmpin);
|
||||
dup2(tmpout, 1);
|
||||
close(tmpout);
|
||||
wait_exit(cmd);
|
||||
//close(tmpin);
|
||||
//tmpin = -1;
|
||||
//close(tmpout);
|
||||
//tmpout = -1;
|
||||
}
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/09 12:33:30 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/16 02:41:51 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/16 16:13:46 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -17,8 +17,19 @@ void exit_shell(t_cmd *cmd, int ret)
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (cmd)
|
||||
free_double(cmd->env);
|
||||
//if (cmd)
|
||||
// free_double(cmd->env);
|
||||
if (cmd->tmpin != -1)
|
||||
{
|
||||
dup2(cmd->tmpin, 0);
|
||||
close(cmd->tmpin);
|
||||
}
|
||||
if (cmd->tmpout != -1)
|
||||
{
|
||||
dup2(cmd->tmpout, 1);
|
||||
close(cmd->tmpout);
|
||||
}
|
||||
free_cmd(cmd);
|
||||
clear_history();
|
||||
//print_double_fd(cmd->s_cmds[0]->env, 1);
|
||||
ft_putstr_fd("exit\n", 1);
|
||||
@ -33,7 +44,7 @@ void free_cmd(t_cmd *cmd)
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (cmd->s_cmds[i])
|
||||
while (cmd->s_cmds && cmd->s_cmds[i])
|
||||
{
|
||||
free_double(cmd->s_cmds[i]->args);
|
||||
if (cmd->s_cmds[i]->cmd)
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/08 15:19:42 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/16 02:50:09 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/16 16:11:37 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -104,34 +104,31 @@ t_cmd *set_cmd(char *input, char **env, int nb)
|
||||
t_cmd *cmd;
|
||||
char **cmds;
|
||||
|
||||
if (!is_quote_good(input))
|
||||
return (0);
|
||||
if (!is_pipe_good(input))
|
||||
if (!is_quote_good(input) || !is_pipe_good(input))
|
||||
return (0);
|
||||
cmds = ft_split_with_quote(input, '|');
|
||||
//print_double_fd(cmds, 1);
|
||||
if (!cmds)
|
||||
return (0);
|
||||
cmd = malloc(sizeof(t_cmd));
|
||||
if (!cmd)
|
||||
return (0);
|
||||
//cmd->err_var = 0;
|
||||
cmd->s_cmds = calloc(sizeof(t_s_cmd), double_size(cmds) + 1);
|
||||
cmd->s_cmds = ft_calloc(sizeof(t_s_cmd), double_size(cmds) + 1);
|
||||
if (!cmd->s_cmds)
|
||||
//free(cmd);
|
||||
return (0);
|
||||
cmd->err_var = nb;
|
||||
cmd->path = 0;
|
||||
//cmd->s_cmds[double_size(cmds)] = NULL;
|
||||
cmd->tmpin = -1;
|
||||
cmd->tmpout = -1;
|
||||
cmd->err_var = nb;
|
||||
cmd->path = get_path(env);
|
||||
cmd->env = env;
|
||||
//set_quote_and_var;
|
||||
//cmd->err_var = 0;
|
||||
cmd->env = ft_dup_double(env);
|
||||
cmd->nb_s_cmd = double_size(cmds);
|
||||
//printf("call split cmd\n");
|
||||
cmd = split_cmd(cmd, cmds); //split each cmd into args in s_cmd
|
||||
//printf("end split cdm\n");
|
||||
if (!cmd)
|
||||
//free_cmd(cmd);
|
||||
return (0);
|
||||
parse_quote(cmd);
|
||||
//printf("after parse quote -%s-\n", cmd->s_cmds[0]->cmd);
|
||||
free(cmds);
|
||||
if (cmd)
|
||||
{
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/04/09 23:58:21 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/15 02:41:29 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/16 13:47:49 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -221,7 +221,7 @@ int parse_quote(t_cmd *cmd)
|
||||
}
|
||||
//printf("parse quote -%s-\n", cmd->s_cmds[i]->args[0]);
|
||||
if (!is_builtin(cmd->s_cmds[i]->args[0]))
|
||||
cmd->s_cmds[i]->cmd = ft_strdup(get_command(cmd->s_cmds[i]->args, cmd->path));
|
||||
cmd->s_cmds[i]->cmd = get_command(cmd->s_cmds[i]->args, cmd->path);
|
||||
else
|
||||
cmd->s_cmds[i]->cmd = ft_strdup(cmd->s_cmds[i]->args[0]);
|
||||
//free(cmd->s_cmds[i]->cmd);
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/03/09 15:18:58 by apommier #+# #+# */
|
||||
/* Updated: 2022/04/15 00:13:27 by apommier ### ########.fr */
|
||||
/* Updated: 2022/04/16 11:03:41 by apommier ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -25,8 +25,10 @@ char *cut_str(char *str, int start, int end)
|
||||
char *swap;
|
||||
char *del;
|
||||
|
||||
swap = 0;
|
||||
del = str;
|
||||
swap = ft_strjoin(&str[end], 0);
|
||||
if (str[end])
|
||||
swap = ft_strjoin(&str[end], 0);
|
||||
str[start] = 0;
|
||||
str = ft_strjoin(str, swap);
|
||||
free(del);
|
||||
@ -53,12 +55,6 @@ char *get_word(char *str, int start)
|
||||
return (new);
|
||||
}
|
||||
|
||||
void error_redirect(char *str)
|
||||
{
|
||||
printf("error : %s\n", str);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
char *set_input(char *line, t_s_cmd *cmd, int index)
|
||||
{
|
||||
int i;
|
||||
@ -74,8 +70,18 @@ char *set_input(char *line, t_s_cmd *cmd, int index)
|
||||
i++;
|
||||
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
|
||||
i++;
|
||||
cmd->infile = get_word(line, word_index);
|
||||
cmd->infile = get_word(line, index);
|
||||
cmd->infile = set_var(cmd->big_cmd, cmd->infile);
|
||||
if (access(cmd->infile, R_OK))
|
||||
{
|
||||
ft_putstr_fd("Minishell: ", 2);
|
||||
ft_putstr_fd(cmd->infile, 2);
|
||||
if (access(cmd->infile, F_OK))
|
||||
ft_putstr_fd(": no such file\n", 2);
|
||||
else
|
||||
ft_putstr_fd(": Permission denied\n", 2);
|
||||
return (0);
|
||||
}
|
||||
line = cut_str(line, index, i);
|
||||
return (line);
|
||||
}
|
||||
@ -96,7 +102,7 @@ char *set_output(char *line, t_s_cmd *cmd, int index)
|
||||
while ((line[i] != ' ' && line[i] != '<' && line[i] != '>') && line[i])
|
||||
i++;
|
||||
cmd->outfile = get_word(line, index);
|
||||
cmd->infile = set_var(cmd->big_cmd, cmd->infile);
|
||||
cmd->outfile = set_var(cmd->big_cmd, cmd->outfile);
|
||||
line = cut_str(line, index, i);
|
||||
return (line);
|
||||
}
|
||||
@ -117,8 +123,12 @@ char *ft_input(char *line, t_s_cmd *cmd, int index)
|
||||
else
|
||||
cmd->in_type = 0;
|
||||
if (next == '<' || next == '>' || !next)
|
||||
error_redirect("problem in ft_input");
|
||||
return (0);
|
||||
//printf("line bf set_input -%s-", line);
|
||||
line = set_input(line, cmd, i);
|
||||
if (!line)
|
||||
return (0);
|
||||
//printf("line -%s-\n", line);
|
||||
return (line);
|
||||
}
|
||||
|
||||
@ -137,21 +147,29 @@ char *ft_output(char *line, t_s_cmd *cmd, int index)
|
||||
else
|
||||
cmd->in_type = 0;
|
||||
if (next == '<' || next == '>' || !next)
|
||||
error_redirect("problem in ft_output");
|
||||
return (0);
|
||||
line = set_output(line, cmd, i);
|
||||
//printf("line -%s-\n", line);
|
||||
return (line);
|
||||
}
|
||||
|
||||
void set_file(char *file)
|
||||
int set_file(char *file)
|
||||
{
|
||||
int fd;
|
||||
|
||||
printf("setfile= %s\n", file);
|
||||
//printf("setfile= %s\n", file);
|
||||
fd = open(file, O_TRUNC | O_CREAT, 0644);
|
||||
if (fd == -1)
|
||||
error_redirect("can't set file");
|
||||
{
|
||||
ft_putstr_fd("Minishell: ", 2);
|
||||
ft_putstr_fd(file, 2);
|
||||
ft_putstr_fd(": Permission denied\n", 2);
|
||||
return (0);
|
||||
}
|
||||
//error_redirect("can't set file");
|
||||
if (fd)
|
||||
close(fd);
|
||||
return (1);
|
||||
}
|
||||
|
||||
char **add_line(char **tab, char *line)
|
||||
@ -217,6 +235,7 @@ void sig_heredoc(int num)
|
||||
|
||||
base.sa_handler = &crtl_c;
|
||||
base.sa_flags = 0;
|
||||
close(1);
|
||||
//printf("sig_heredoc\n");
|
||||
if (sigaction(SIGINT, &base, 0) == -1)
|
||||
{
|
||||
@ -280,16 +299,20 @@ int wait_prompt(t_s_cmd *cmd, int index)
|
||||
char *set_redirection(t_s_cmd *cmd, char *line, int index)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
|
||||
i= 0;
|
||||
// printf("enter redirection\n");
|
||||
while (line[i])
|
||||
{
|
||||
//printf("line[i] i= %d\n", i);
|
||||
//printf("-%s-\n", line);
|
||||
if(line[i] == '<')
|
||||
{
|
||||
if (!is_in_quote(line, i))
|
||||
{
|
||||
line = ft_input(line, cmd, i);
|
||||
if (!line)
|
||||
return (0);
|
||||
if (cmd->in_type == 1)
|
||||
{
|
||||
if (!wait_prompt(cmd, index))
|
||||
@ -304,11 +327,14 @@ char *set_redirection(t_s_cmd *cmd, char *line, int index)
|
||||
{
|
||||
line = ft_output(line, cmd, i);
|
||||
//if (cmd->in_type == 0)
|
||||
set_file(cmd->outfile);
|
||||
if (!set_file(cmd->outfile))
|
||||
return (0);
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
else
|
||||
if (line[i] && (line[i] == '<' || line[i] == '>') && is_in_quote(line, i))
|
||||
i++;
|
||||
else if (line[i] && line[i] != '<' && line[i] != '>')
|
||||
i++;
|
||||
}
|
||||
return(line);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user