This commit is contained in:
kinou-p 2022-04-19 12:41:00 +02:00
parent 10abbb572d
commit 954e8c47fb
9 changed files with 97 additions and 118 deletions

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/09 22:33:49 by apommier #+# #+# */ /* Created: 2022/03/09 22:33:49 by apommier #+# #+# */
/* Updated: 2022/04/19 08:37:43 by apommier ### ########.fr */ /* Updated: 2022/04/19 12:18:26 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,12 +31,12 @@
// Command Data Structure // Command Data Structure
// Describes a simple command and arguments // Describes a simple command and arguments
typedef struct s_simple { typedef struct s_simple {
struct s_command *big_cmd; struct s_command *big_cmd;
int fd[2]; int fd[2];
int pipe[2]; int pipe[2];
int last; int last;
int last_pipe[2]; int last_pipe[2];
int child; int child;
int nb_args; int nb_args;
char *infile; char *infile;
@ -49,23 +49,21 @@ typedef struct s_simple {
// Describes a complete command with the multiple pipes if any // Describes a complete command with the multiple pipes if any
// and input/output redirection if any. // and input/output redirection if any.
typedef struct s_command { typedef struct s_command {
int tmpin; int tmpin;
int tmpout; int tmpout;
char **env; char **env;
int nb_s_cmd; int nb_s_cmd;
struct s_simple **s_cmds; struct s_simple **s_cmds;
//char *outfile;
//char *infile;
int err_var; int err_var;
struct s_simple *current_s_cmd; struct s_simple *current_s_cmd;
char **path; char **path;
} t_cmd; } t_cmd;
char *error_parsing(void); char *error_parsing(void);
//main.c //main.c
int main();//int ac, char **av, char **path); int main(int ac, char **av, char **path);
char **ft_dup_double(char **env); char **ft_dup_double(char **env);
//pipe.c //pipe.c
@ -79,8 +77,8 @@ char **get_path(char **env);
char *get_command(char **exec, char **env); char *get_command(char **exec, char **env);
//free_cmd //free_cmd
void free_cmd(t_cmd *cmd); void free_cmd(t_cmd *cmd);
void exit_shell(t_cmd *cmd, int ret); void exit_shell(t_cmd *cmd, int ret);
//set_quote.c //set_quote.c
char *set_var(t_cmd *big_cmd, char *cmd); char *set_var(t_cmd *big_cmd, char *cmd);
@ -89,7 +87,7 @@ int is_quote_good(char *str);
char **ft_split_with_quote(char const *s, char c); char **ft_split_with_quote(char const *s, char c);
//signals //signals
void crtl_c(int num); void crtl_c(int num);
void sig_heredoc(int num); void sig_heredoc(int num);
//redirection.c set redirection and input good //redirection.c set redirection and input good
@ -102,7 +100,7 @@ char *get_str(char *str, int start, int end);
char *cut_str(char *str, int start, int end); char *cut_str(char *str, int start, int end);
int double_size(char **tab); int double_size(char **tab);
void print_double_fd(char **tab, int fd); void print_double_fd(char **tab, int fd);
void free_double(char **tab); void free_double(char **tab);
//builtins utils //builtins utils
void ft_shlvl(char **env); void ft_shlvl(char **env);
@ -117,7 +115,6 @@ int find_variable(char *variable, t_s_cmd *cmd);
int find_it(char **str, char *s); int find_it(char **str, char *s);
int check_variable(char *variable); int check_variable(char *variable);
int cd_error(t_s_cmd *cmd, char *str, int i); int cd_error(t_s_cmd *cmd, char *str, int i);
int msg_error(t_s_cmd *cmd, char *cm, char *entry, char *error, int i);
int size_path(char **str); int size_path(char **str);
int check_return(t_s_cmd *cmd, int var); int check_return(t_s_cmd *cmd, int var);
@ -133,7 +130,7 @@ int ft_pwd(t_s_cmd *cmd);
int open_directory(t_s_cmd *cmd);//cd int open_directory(t_s_cmd *cmd);//cd
//parse builtin //parse builtin
int is_builtin(char *cmd); int is_builtin(char *cmd);
void call_builtin(t_cmd *cmd); void call_builtin(t_cmd *cmd);
#endif #endif

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/29 00:09:17 by apommier #+# #+# */ /* Created: 2020/11/29 00:09:17 by apommier #+# #+# */
/* Updated: 2022/04/19 08:44:19 by apommier ### ########.fr */ /* Updated: 2022/04/19 12:11:56 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
long long ft_atoi(const char *nptr) long long ft_atoi(const char *nptr)
{ {
int i; int i;
long long nbr; long long nbr;
long long minus; long long minus;

View File

@ -70,10 +70,11 @@ char *next_line(char *save, int *end, int fd)
char *get_next_line(int fd) char *get_next_line(int fd)
{ {
char *save = NULL; char *save;
int *end; int *end;
char *line; char *line;
save = 0;
line = 0; line = 0;
if (fd < 0) if (fd < 0)
return (0); return (0);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/21 18:30:26 by sadjigui #+# #+# */ /* Created: 2022/03/21 18:30:26 by sadjigui #+# #+# */
/* Updated: 2022/04/17 10:05:05 by apommier ### ########.fr */ /* Updated: 2022/04/19 12:32:13 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -159,11 +159,10 @@ int open_directory(t_s_cmd *cmd)
} }
else else
{ {
return (msg_error(cmd, "Minishell: cd: ", cmd->args[1], ": No such directory\n", 1)); ft_putstr_fd("Minishell: cd: ", 2);
// ft_putstr_fd("Minishell: cd: ", 2); ft_putstr_fd(cmd->args[1], 2);
// ft_putstr_fd(cmd->args[1], 2); ft_putstr_fd(": No such directory\n", 2);
// ft_putstr_fd(": No such directory\n", 2); return (check_return(cmd, 1));
// return ;//(1);
} }
} }
return (check_return(cmd, 0)); return (check_return(cmd, 0));

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/16 15:04:12 by sadjigui #+# #+# */ /* Created: 2022/03/16 15:04:12 by sadjigui #+# #+# */
/* Updated: 2022/04/18 03:15:44 by apommier ### ########.fr */ /* Updated: 2022/04/19 12:18:18 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -42,14 +42,6 @@ int cd_error(t_s_cmd *cmd, char *str, int i)
return (check_return(cmd, i)); return (check_return(cmd, i));
} }
int msg_error(t_s_cmd *cmd, char *cm, char *entry, char *error, int i)
{
ft_putstr_fd(cm, 2);
ft_putstr_fd(entry, 2);
ft_putstr_fd(error, 2);
return (check_return(cmd, i));
}
int size_path(char **str) int size_path(char **str)
{ {
int i; int i;

View File

@ -6,13 +6,13 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/06 13:27:11 by apommier #+# #+# */ /* Created: 2022/03/06 13:27:11 by apommier #+# #+# */
/* Updated: 2022/04/19 12:05:20 by apommier ### ########.fr */ /* Updated: 2022/04/19 12:22:04 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../includes/minishell.h" #include "../includes/minishell.h"
void crtl_c(int num) void crtl_c(int num)
{ {
num = 0; num = 0;
printf("\n"); printf("\n");
@ -30,30 +30,30 @@ void sig_quit(int num)
char **ft_dup_double(char **env) char **ft_dup_double(char **env)
{ {
char **new_tab; char **new_tab;
int i; int i;
i = 0; i = 0;
if (!env) if (!env)
return (0); return (0);
while (env[i]) while (env[i])
i++; i++;
new_tab = ft_calloc(sizeof(char *), i + 1); new_tab = ft_calloc(sizeof(char *), i + 1);
i = 0; i = 0;
while (env[i]) while (env[i])
{ {
new_tab[i] = ft_strjoin(env[i], 0); new_tab[i] = ft_strjoin(env[i], 0);
i++; i++;
} }
new_tab[i] = NULL; new_tab[i] = NULL;
return (new_tab); return (new_tab);
} }
void print_prompt(char **path) void print_prompt(char **path)
{ {
char *input; char *input;
t_cmd *cmd; t_cmd *cmd;
int err_var; int err_var;
struct sigaction test; struct sigaction test;
memset(&test, 0, sizeof(test)); memset(&test, 0, sizeof(test));
test.sa_handler = &sig_quit; test.sa_handler = &sig_quit;
@ -83,7 +83,6 @@ void print_prompt(char **path)
free_double(path); free_double(path);
execute(cmd, cmd->env); execute(cmd, cmd->env);
err_var = cmd->err_var; err_var = cmd->err_var;
path = ft_dup_double(cmd->env); path = ft_dup_double(cmd->env);
free_cmd(cmd); free_cmd(cmd);
cmd = 0; cmd = 0;
@ -95,7 +94,7 @@ void print_prompt(char **path)
int main(int ac, char **av, char **path) int main(int ac, char **av, char **path)
{ {
char **env; char **env;
if (!isatty(0)) if (!isatty(0))
{ {

View File

@ -6,16 +6,15 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/02 18:51:31 by apommier #+# #+# */ /* Created: 2022/04/02 18:51:31 by apommier #+# #+# */
/* Updated: 2022/04/19 12:00:46 by apommier ### ########.fr */ /* Updated: 2022/04/19 12:30:01 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../../includes/minishell.h" #include "../../includes/minishell.h"
void close_pipe(t_cmd *cmd) void close_pipe(t_cmd *cmd)
{ {
int i; int i;
i = 0; i = 0;
close(0); close(0);
@ -29,7 +28,7 @@ void close_pipe(t_cmd *cmd)
int wait_exit(t_cmd *cmd) int wait_exit(t_cmd *cmd)
{ {
int i; int i;
int ret; int ret;
int exit_pid; int exit_pid;
int status; int status;
@ -38,33 +37,33 @@ int wait_exit(t_cmd *cmd)
ret = 0; ret = 0;
exit_pid = 1; exit_pid = 1;
while (exit_pid > 0) while (exit_pid > 0)
{ {
i = 0; i = 0;
exit_pid = wait(&status); exit_pid = wait(&status);
while (exit_pid != -1 && cmd->s_cmds[i]) while (exit_pid != -1 && cmd->s_cmds[i])
{ {
if (exit_pid == cmd->s_cmds[i]->child) if (exit_pid == cmd->s_cmds[i]->child)
{ {
close(cmd->s_cmds[i]->fd[0]); close(cmd->s_cmds[i]->fd[0]);
close(cmd->s_cmds[i]->fd[1]); close(cmd->s_cmds[i]->fd[1]);
if (WIFEXITED(status)) if (WIFEXITED(status))
cmd->err_var = WEXITSTATUS(status); cmd->err_var = WEXITSTATUS(status);
else if (WIFSIGNALED(status)) else if (WIFSIGNALED(status))
{ {
if (WTERMSIG(status) != 13) if (WTERMSIG(status) != 13)
cmd->err_var = WTERMSIG(status); cmd->err_var = WTERMSIG(status);
if (WTERMSIG(status) == 3) if (WTERMSIG(status) == 3)
ft_putstr_fd("^\\Quit", 1); ft_putstr_fd("^\\Quit", 1);
ft_putstr_fd("\b\b\b\b\b", 1); ft_putstr_fd("\b\b\b\b\b", 1);
} }
} }
i++; i++;
} }
} }
return (ret); return (ret);
} }
void exec_cmd(t_cmd *cmd, char **env, int *fdpipe) void exec_cmd(t_cmd *cmd, char **env, int *fdpipe)
{ {
cmd->current_s_cmd->child = fork(); cmd->current_s_cmd->child = fork();
if (cmd->current_s_cmd->child == 0) if (cmd->current_s_cmd->child == 0)
@ -92,10 +91,10 @@ void exec_cmd(t_cmd *cmd, char **env, int *fdpipe)
free_cmd(cmd); free_cmd(cmd);
exit(127); exit(127);
} }
if (-1 == execve(cmd->current_s_cmd->cmd, cmd->current_s_cmd->args, env)) if (-1 == execve(cmd->current_s_cmd->cmd, cmd->current_s_cmd->args, env))
ft_putstr_fd("Minishell: exec error\n", 2); ft_putstr_fd("Minishell: exec error\n", 2);
exit(126); exit(126);
} }
else if (!cmd->current_s_cmd->cmd || access(cmd->current_s_cmd->cmd, F_OK)) else if (!cmd->current_s_cmd->cmd || access(cmd->current_s_cmd->cmd, F_OK))
cmd->err_var = 127; cmd->err_var = 127;
else else
@ -104,21 +103,15 @@ void exec_cmd(t_cmd *cmd, char **env, int *fdpipe)
void execute(t_cmd *cmd, char **env) void execute(t_cmd *cmd, char **env)
{ {
int fdpipe[2]; int fdpipe[2];
int fdout; int fdout;
int fdin; int fdin;
int tmpin = -1; int i;
int tmpout = -1;
int i;
tmpin = -1;
tmpout = -1;
i = 0; i = 0;
fdpipe[1] = -1; fdpipe[1] = -1;
tmpin = dup(0); cmd->tmpin = dup(0);
tmpout = dup(1); cmd->tmpout = dup(1);
cmd->tmpin = tmpin;
cmd->tmpout = tmpout;
if (cmd->current_s_cmd->infile) if (cmd->current_s_cmd->infile)
{ {
fdin = open(cmd->current_s_cmd->infile, O_RDWR); fdin = open(cmd->current_s_cmd->infile, O_RDWR);
@ -126,8 +119,8 @@ void execute(t_cmd *cmd, char **env)
printf("Minishell: open : bad file descriptor\n"); printf("Minishell: open : bad file descriptor\n");
} }
else else
fdin = dup(tmpin); fdin = dup(cmd->tmpin);
while(cmd->current_s_cmd) while (cmd->current_s_cmd)
{ {
cmd->current_s_cmd->child = 1; cmd->current_s_cmd->child = 1;
fdout = -1; fdout = -1;
@ -144,7 +137,7 @@ void execute(t_cmd *cmd, char **env)
if (cmd->current_s_cmd->outfile) if (cmd->current_s_cmd->outfile)
fdout = open(cmd->current_s_cmd->outfile, O_RDWR | O_CREAT | O_APPEND, 0666); fdout = open(cmd->current_s_cmd->outfile, O_RDWR | O_CREAT | O_APPEND, 0666);
else else
fdout = dup(tmpout); fdout = dup(cmd->tmpout);
cmd->current_s_cmd->fd[0] = fdin; cmd->current_s_cmd->fd[0] = fdin;
cmd->current_s_cmd->fd[1] = fdout; cmd->current_s_cmd->fd[1] = fdout;
if (i == 0 && is_builtin(cmd->current_s_cmd->cmd)) if (i == 0 && is_builtin(cmd->current_s_cmd->cmd))
@ -164,7 +157,7 @@ void execute(t_cmd *cmd, char **env)
} }
else else
cmd->current_s_cmd->fd[1] = fdpipe[1]; cmd->current_s_cmd->fd[1] = fdpipe[1];
fdin=fdpipe[0]; fdin = fdpipe[0];
exec_cmd(cmd, env, fdpipe); exec_cmd(cmd, env, fdpipe);
close(cmd->current_s_cmd->fd[0]); close(cmd->current_s_cmd->fd[0]);
} }
@ -174,9 +167,9 @@ void execute(t_cmd *cmd, char **env)
cmd->current_s_cmd = cmd->s_cmds[i]; cmd->current_s_cmd = cmd->s_cmds[i];
} }
close_pipe(cmd); close_pipe(cmd);
dup2(tmpin, 0); dup2(cmd->tmpin, 0);
dup2(tmpout, 1); dup2(cmd->tmpout, 1);
close(tmpin); close(cmd->tmpin);
close(tmpout); close(cmd->tmpout);
wait_exit(cmd); wait_exit(cmd);
} }

View File

@ -6,14 +6,12 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/08 16:58:40 by apommier #+# #+# */ /* Created: 2022/03/08 16:58:40 by apommier #+# #+# */
/* Updated: 2022/04/19 12:01:06 by apommier ### ########.fr */ /* Updated: 2022/04/19 12:34:06 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../../includes/minishell.h" #include "../../includes/minishell.h"
char **get_path(char **env) char **get_path(char **env)
{ {
int i; int i;
@ -79,7 +77,7 @@ char *get_command(char **exec, char **path)
if ((exec[0][0] == '/' || exec[0][0] == '.') && !access(exec[0], F_OK)) if ((exec[0][0] == '/' || exec[0][0] == '.') && !access(exec[0], F_OK))
return (exec[0]); return (exec[0]);
else if (exec[0][0] == '/') else if (exec[0][0] == '/')
return(0); return (0);
swap = does_access(path, exec); swap = does_access(path, exec);
return (swap); return (swap);
} }

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/08 15:19:42 by apommier #+# #+# */ /* Created: 2022/03/08 15:19:42 by apommier #+# #+# */
/* Updated: 2022/04/19 12:01:46 by apommier ### ########.fr */ /* Updated: 2022/04/19 12:35:26 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,7 +22,7 @@ t_s_cmd *set_s_cmd(char *line, int index)
{ {
t_s_cmd *s_cmd; t_s_cmd *s_cmd;
char **split_line; char **split_line;
int i; int i;
i = 0; i = 0;
s_cmd = malloc(sizeof(t_s_cmd)); s_cmd = malloc(sizeof(t_s_cmd));
@ -46,7 +46,7 @@ t_s_cmd *set_s_cmd(char *line, int index)
return (s_cmd); return (s_cmd);
} }
t_cmd *split_cmd(t_cmd *cmd, char **cmds) t_cmd *split_cmd(t_cmd *cmd, char **cmds)
{ {
int i; int i;
@ -68,8 +68,8 @@ t_cmd *split_cmd(t_cmd *cmd, char **cmds)
int is_pipe_good(char *str) int is_pipe_good(char *str)
{ {
int i; int i;
char next; char next;
i = 0; i = 0;
while (str[i]) while (str[i])