Exec work du feu de dieu

This commit is contained in:
kinou-p 2022-04-01 17:37:21 +02:00
parent af438b8db4
commit 7bb7b4afb2
5 changed files with 176 additions and 76 deletions

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/09 22:33:49 by apommier #+# #+# */
/* Updated: 2022/03/11 17:04:09 by apommier ### ########.fr */
/* Updated: 2022/04/01 15:37:03 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -31,6 +31,11 @@
// Describes a simple command and arguments
typedef struct s_simple {
int fd[2];
int pipe[2];
int last;
int last_pipe[2];
int child;
int nb_args;
char *infile;
int in_type;

View File

@ -6,61 +6,101 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/06 13:27:11 by apommier #+# #+# */
/* Updated: 2022/03/10 11:07:08 by apommier ### ########.fr */
/* Updated: 2022/04/01 17:30:02 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/minishell.h"
void red()
{
printf("\033[1;31m");
}
void normal()
{
printf("\033[0m");
}
void print_prompt(char **path)
{
char *input;
t_cmd *cmd;
int i;
int tmpout = dup(1);
int tmpin = dup(0);
input = 0;
i = 0;
cmd = 0;
while (1)
{
red();
printf("~$");
normal();
input = readline(" ");
if (!ft_strcmp("exit", input))
i = 0;
//ft_putstr_fd("\033[1;31m~$ \033[0m", 2);
/*input = get_next_line(0);
while (input[i] != '\n' && input[i])
i++;
if (input[i] == '\n')
input[i] = 0;*/
//input = ft_strjoin(readline("\033[1;31m~$ \033[0m"), 0);
input = readline("\033[1;31m~$ \033[0m");
//if (!ft_strlen(input))
// printf("\n");
add_history(input);
//l_replace_line(0, 1);
//rl_replace_line("\n", 1);
//_on_new_line();
//rl_redisplay();
if (!ft_strcmp("exit", input) && input)
{
dprintf(2, "exit\n");
rl_clear_history();
free(input);
//free(input);
exit_shell(cmd);
}
if (ft_strlen(input) && next_space(input, 0))
if (ft_strlen(input) && next_space(input, 0) && input)
{
add_history(input);
//printf("hre\n");
//add_history(input);
cmd = set_cmd(input, path);
free(input);
//free(input);
if (cmd)
{
execute(cmd, path);
free_cmd(cmd);
dup2(tmpout, 1);
dup2(tmpin, 0);
//waitpid(-1, 0, 0);
//free_cmd(cmd);
cmd = 0;
}
//free_cmd(cmd);
//cmd = 0;
//rl_replace_line("test", 0);
}
//else
// dprintf(2, "no exec\n");
//rl_replace_line("\n", 1);
free(input);
}
}
void test(int num)
{
num = 0;
printf("CRTL-C ? try exit\n");
//ft_putstr_fd("\n\033[1;31m~$ \033[0m", 1);
printf("\n");
rl_on_new_line();
rl_replace_line("", 0);
rl_redisplay();
//num = 0;
//if (num)
// printf("num = %d\n", num);
//ft_putstr_fd("\b\b ", 2);
//ft_putstr_fd("\n", 2);
//ft_putstr_fd("\033[1;31m~$ \033[0m", 1);
//red();
//normal();
//printf("CRTL-C ? try exit\n");
}
void just_exit(int num)
{
num = 0;
printf("crtl d\n");
exit(0);
}
int main(int ac, char **av, char **path)
@ -71,8 +111,10 @@ int main(int ac, char **av, char **path)
printf("too much arguments\n");
return (0);
}
//ft_putstr_fd("\033[1;31m~$ \033[0m", 1);
printf("---MINISHELL START---\n");
signal(SIGINT, test);
signal(SIGQUIT, just_exit);
print_prompt(path);
return (0);
}

View File

@ -1,21 +1,79 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipe.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/07 11:13:32 by apommier #+# #+# */
/* Updated: 2022/03/10 11:38:53 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/minishell.h"
void close_pipe(t_cmd *cmd)
{
int i;
i = 0;
close(0);
while (cmd->s_cmds[i])// && !cmd->s_cmds[i]->last)
{
close(cmd->s_cmds[i]->last_pipe[0]);
close(cmd->s_cmds[i]->last_pipe[1]);
close(cmd->s_cmds[i]->fd[0]);
close(cmd->s_cmds[i]->fd[1]);
close(cmd->s_cmds[i]->pipe[0]);
close(cmd->s_cmds[i]->pipe[1]);
i++;
}
}
int wait_exit(t_cmd *cmd)
{
int i;
int ret;
int exit_pid;
int status;
i = 0;
ret = 0;
exit_pid = 1;
while (exit_pid > 0)
{
i = 0;
exit_pid = wait(&status);
//dprintf(2, "exit_pid= %d\n", exit_pid);
while (exit_pid != -1 && cmd->s_cmds[i])
{
if (exit_pid == cmd->s_cmds[i]->child)
{
close(cmd->s_cmds[i]->fd[0]);
close(cmd->s_cmds[i]->fd[1]);
}
i++;
}
}
//dprintf(2, "return wait_exit\n");
return (ret);
}
void exec_cmd(t_cmd *cmd, char **env, int *fdpipe)
{
// int ret;
cmd->current_s_cmd->child = fork();
if (cmd->current_s_cmd->child == 0)
{
if (fdpipe)
close(fdpipe[0]);
dup2(cmd->current_s_cmd->fd[0], 0);
dup2(cmd->current_s_cmd->fd[1], 1);
close(cmd->current_s_cmd->fd[0]);
close(cmd->current_s_cmd->fd[1]);
if (-1 == execve(cmd->current_s_cmd->cmd, cmd->current_s_cmd->args, env))
dprintf(2, "exec error\n");
exit(0);
}
}
void execute(t_cmd *cmd, char **env)
{
//save in/out
int ret;
int fdpipe[2];
//int ret;
int fdout;
int tmpin = dup(0);
int tmpout= dup(1);
@ -23,67 +81,61 @@ void execute(t_cmd *cmd, char **env)
int i;
i = 0;
if (cmd->current_s_cmd->infile)//set the initial input
fdin = open(cmd->current_s_cmd->infile, O_APPEND);
//close(0);
//close(1);
//if (cmd->current_s_cmd->infile)//set the initial input
// fdin = open(cmd->current_s_cmd->infile, O_APPEND);
//fdin = open(cmd->current_s_cmd->infile, O_RDONLY);
//else if (cmd->infile)
// fdin = open(cmd->infile, O_RDONLY);
else
fdin=dup(tmpin);
while( i < cmd->nb_s_cmd)
//else
fdin=dup(tmpin);
while(cmd->current_s_cmd)
{
//if (i)
// cmd->current_s_cmd++;
if (i != 0 && cmd->current_s_cmd->infile)
fdin = open(cmd->current_s_cmd->infile, O_CREAT | O_APPEND);
//fdin = open(cmd->current_s_cmd->infile, O_RDONLY);
//redirect input
dup2(fdin, 0);
close(fdin);
fdin = -1;
//setup output
if (i == cmd->nb_s_cmd - 1)
{
// Last simple command
cmd->current_s_cmd->last = 1;
if (cmd->current_s_cmd->outfile)
fdout = open(cmd->current_s_cmd->outfile, O_RDWR | O_CREAT | O_APPEND, 0666);
else if(cmd->outfile)
fdout=open(cmd->outfile, O_RDWR | O_CREAT | O_TRUNC, 0666);
else// Use default output
{
fdout=dup(tmpout);
}
cmd->current_s_cmd->fd[0] = fdin;
cmd->current_s_cmd->fd[1] = fdout;
exec_cmd(cmd, env, 0);
}
else
{
//not last
//simple command
//create pipe
int fdpipe[2];
cmd->current_s_cmd->last = 0;
pipe(fdpipe);
fdout=fdpipe[1];
//cmd->current_s_cmd->pipe[0] = fdpipe[0];
//cmd->current_s_cmd->pipe[1] = fdpipe[1];
cmd->current_s_cmd->fd[0] = fdin;//entree current
cmd->current_s_cmd->fd[1] = fdpipe[1];//sortie current
cmd->current_s_cmd->pipe[0] = fdpipe[0];//entree prochaine cmd
cmd->current_s_cmd->pipe[1] = fdpipe[1];//sortie current
cmd->s_cmds[i + 1]->last_pipe[0] = fdpipe[0];
cmd->s_cmds[i + 1]->last_pipe[1] = fdpipe[1];
//fdout=fdpipe[1];
fdin=fdpipe[0];
exec_cmd(cmd, env, fdpipe);
close(cmd->current_s_cmd->fd[0]);
}
// redirect output
dup2(fdout, 1);
close(fdout);
// create child process
ret=fork();
if(ret==0)
{
execve(cmd->current_s_cmd->cmd, cmd->current_s_cmd->args, env);//cmd->path);
//_exit(1);
}
close(cmd->current_s_cmd->pipe[1]);
i++;
cmd->current_s_cmd = cmd->s_cmds[i];
} //while
//restore in/out defaults
close_pipe(cmd);
wait_exit(cmd);
//wait(0);
dup2(tmpin,0);
dup2(tmpout,1);
close(tmpin);
close(tmpout);
// Wait for last command
waitpid(ret, NULL, 0);
} // execute

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/08 15:19:42 by apommier #+# #+# */
/* Updated: 2022/03/11 16:39:15 by apommier ### ########.fr */
/* Updated: 2022/03/31 16:31:40 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -63,6 +63,7 @@ t_cmd *split_cmd(t_cmd *cmd, char **cmds)
}
i++;
}
cmd->s_cmds[i] = 0;
return (cmd);
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/09 15:18:58 by apommier #+# #+# */
/* Updated: 2022/03/12 00:37:11 by apommier ### ########.fr */
/* Updated: 2022/04/01 17:36:46 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
@ -40,7 +40,7 @@ char *cut_str(char *str, int start, int end)
char *get_word(char *str, int start, int end)
{
char *new;
char *swap;
//char *swap;
int i;
i = 0;