This commit is contained in:
kinou-p 2022-04-16 06:01:35 +02:00
parent 4495ac459c
commit af92795001
6 changed files with 61 additions and 32 deletions

View File

@ -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 04:08:28 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -72,14 +72,16 @@ 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);
if (cmd)
{
//cmd->err_var = 0;
execute(cmd, path);
err_var = cmd->err_var;
free_double(path);
path = ft_dup_double(cmd->env);
//cmd = 0;
free_cmd(cmd);
cmd = 0;
}
else
ft_putstr_fd("Minishell: error while parsing command\n", 2);

View File

@ -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 04:34:00 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -91,11 +91,11 @@ 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;
@ -112,6 +112,7 @@ void execute(t_cmd *cmd, char **env)
int tmpout;
int i;
//printf("exec\n");
i = 0;
fdpipe[1] = -1;
tmpin = dup(0);
@ -120,7 +121,7 @@ void execute(t_cmd *cmd, char **env)
{
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);
@ -132,7 +133,7 @@ void execute(t_cmd *cmd, char **env)
{
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)
{

View File

@ -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 04:43:03 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -33,7 +33,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)

View File

@ -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 04:33:32 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -104,34 +104,29 @@ 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);
if (!cmd->s_cmds)
//free(cmd);
return (0);
cmd->err_var = nb;
cmd->path = 0;
//cmd->s_cmds[double_size(cmds)] = NULL;
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)
{

View File

@ -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 04:43:06 by apommier ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -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 04:33:24 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -76,6 +76,22 @@ char *set_input(char *line, t_s_cmd *cmd, int index)
i++;
cmd->infile = get_word(line, word_index);
cmd->infile = set_var(cmd->big_cmd, cmd->infile);
i = open(cmd->infile, O_RDONLY);
if (i == -1)
{
ft_putstr_fd("Minishell: ", 2);
ft_putstr_fd(cmd->infile, 2);
ft_putstr_fd(": no such file\n", 2);
return (0);
}
if (access(cmd->infile, R_OK))
{
ft_putstr_fd("Minishell: ", 2);
ft_putstr_fd(cmd->infile, 2);
ft_putstr_fd(": Permission denied\n", 2);
return (0);
}
close(i);
line = cut_str(line, index, i);
return (line);
}
@ -96,7 +112,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 +133,10 @@ 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);
line = set_input(line, cmd, i);
if (!line)
return (0);
return (line);
}
@ -137,21 +155,28 @@ 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);
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)
@ -285,11 +310,14 @@ char *set_redirection(t_s_cmd *cmd, char *line, int index)
// printf("enter redirection\n");
while (line[i])
{
//printf("line[i] i= %d\n", i);
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 +332,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] == '>') && is_in_quote(line, i))
i++;
else if (line[i] != '<' && line[i] != '>')
i++;
}
return(line);