norm complete

This commit is contained in:
PrStein 2022-04-20 00:37:55 +02:00
commit f9a2acb10b
21 changed files with 505 additions and 359 deletions

View File

@ -6,7 +6,7 @@
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ # # By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2022/03/06 12:50:24 by apommier #+# #+# # # Created: 2022/03/06 12:50:24 by apommier #+# #+# #
# Updated: 2022/04/19 15:05:18 by apommier ### ########.fr # # Updated: 2022/04/19 19:29:06 by apommier ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -14,11 +14,14 @@ NAME = minishell
SRCS = srcs/main.c\ SRCS = srcs/main.c\
srcs/pipe/pipe.c\ srcs/pipe/pipe.c\
srcs/pipe/pipex_utils.c\ srcs/pipe/pipex_utils.c\
srcs/pipe/exec_utils.c\
srcs/set_cmd/free_cmd.c\ srcs/set_cmd/free_cmd.c\
srcs/set_cmd/set_cmd.c\ srcs/set_cmd/set_cmd.c\
srcs/set_cmd/set_cmd_utils.c\
srcs/set_redirection/redirection.c\ srcs/set_redirection/redirection.c\
srcs/set_redirection/utils.c\ srcs/set_redirection/utils.c\
srcs/set_redirection/set_heredoc.c\ srcs/set_redirection/set_heredoc.c\
srcs/set_redirection/heredoc_utils.c\
srcs/set_redirection/set_input.c\ srcs/set_redirection/set_input.c\
srcs/set_redirection/set_output.c\ srcs/set_redirection/set_output.c\
srcs/built_in/unset.c\ srcs/built_in/unset.c\
@ -30,10 +33,12 @@ SRCS = srcs/main.c\
srcs/built_in/export2.c\ srcs/built_in/export2.c\
srcs/built_in/env.c\ srcs/built_in/env.c\
srcs/built_in/exit.c\ srcs/built_in/exit.c\
srcs/built_in/choose_builtin.c\
srcs/set_quote/split_with_quote.c\ srcs/set_quote/split_with_quote.c\
srcs/set_quote/set_quote.c\ srcs/set_quote/set_quote.c\
srcs/set_quote/set_var.c\ srcs/set_signals/set_signal.c\
srcs/built_in/choose_builtin.c srcs/set_quote/set_var.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}
CC = clang CC = clang

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 15:05:51 by apommier ### ########.fr */ /* Updated: 2022/04/19 19:33:31 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -60,8 +60,6 @@ typedef struct s_command {
char **path; char **path;
} t_cmd; } t_cmd;
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);
@ -70,9 +68,18 @@ char **ft_dup_double(char **env);
void execute(t_cmd *cmd, char **env); void execute(t_cmd *cmd, char **env);
//set_cmd.c //set_cmd.c
int error_parsing(void);
t_cmd *set_cmd(char *input, char **path, int nb); t_cmd *set_cmd(char *input, char **path, int nb);
//exec_utils.c
int wait_exit(t_cmd *cmd);
void exit_child(t_cmd *cmd, int exit_pid, int status, int i);
void check_access(t_cmd *cmd);
void close_pipe(t_cmd *cmd);
//pipex_utils.c //pipex_utils.c
void set_fdin(t_cmd *cmd, int *fdin);
void reset_fds(t_cmd *cmd);
char **get_path(char **env); char **get_path(char **env);
char *get_command(char **exec, char **env); char *get_command(char **exec, char **env);
@ -89,15 +96,23 @@ 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);
void sig_quit(int num);
//redirection.c set redirection and input good //redirection.c set redirection and input good
char *get_word(char *str, int start); char *get_word(char *str, int start);
char **add_line(char **tab, char *line); char **add_line(char **tab, char *line);
char *set_redirection(t_s_cmd *cmd, char *line, int index); char *set_redirection(t_s_cmd *cmd, char *line, int index, int i);
char next_space(char *str, int i); char next_space(char *str, int i);
//set_heredoc //set_heredoc
int wait_prompt(t_s_cmd *cmd, int index); int wait_prompt(t_s_cmd *cmd, int index, int i, char *input);
//heredoc_utils.c
void sig_heredoc(int num);
int free_wait_prompt(char *in, char**history);
void change_signal(void);
void sig_heredoc(int num);
char **fill_history(t_s_cmd *cmd, char *input, char *in, char **history);
//set_input.c //set_input.c
char *ft_input(char *line, t_s_cmd *cmd, int index); char *ft_input(char *line, t_s_cmd *cmd, int index);
@ -108,6 +123,8 @@ char *ft_output(char *line, t_s_cmd *cmd, int index);
//set_var.c //set_var.c
char *set_var(t_cmd *big_cmd, char *cmd); char *set_var(t_cmd *big_cmd, char *cmd);
//set_signals.c
//utils redirection //utils redirection
int parse_quote(t_cmd *cmd); int parse_quote(t_cmd *cmd);
char *get_str(char *str, int start, int end); char *get_str(char *str, int start, int end);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/15 11:23:32 by apommier #+# #+# */ /* Created: 2022/04/15 11:23:32 by apommier #+# #+# */
/* Updated: 2022/04/19 13:17:35 by apommier ### ########.fr */ /* Updated: 2022/04/19 16:09:57 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,6 +25,12 @@ int max_long(char *nbr)
return (0); return (0);
} }
void exit_error(t_cmd *cmd)
{
ft_putstr_fd("Minishell: exit: numeric argument required\n", 2);
exit_shell(cmd, 2);
}
void ft_exit(t_s_cmd *cmd) void ft_exit(t_s_cmd *cmd)
{ {
int i; int i;
@ -43,16 +49,11 @@ void ft_exit(t_s_cmd *cmd)
exit_shell(cmd->big_cmd, 0); exit_shell(cmd->big_cmd, 0);
while (cmd->args[1][++i]) while (cmd->args[1][++i])
{ {
if ((!ft_isdigit(cmd->args[1][i]) && !(cmd->args[1][i] == '-' && ft_isdigit(cmd->args[1][i + 1])))) if ((!ft_isdigit(cmd->args[1][i]) && !(cmd->args[1][i] == '-'
{ && ft_isdigit(cmd->args[1][i + 1]))))
ft_putstr_fd("Minishell: exit: numeric argument required\n", 2); exit_error(cmd->big_cmd);
exit_shell(cmd->big_cmd, 2);
}
} }
if (max_long(cmd->args[1])) if (max_long(cmd->args[1]))
{ exit_error(cmd->big_cmd);
ft_putstr_fd("Minishell: exit: numeric argument required\n", 2);
exit_shell(cmd->big_cmd, 2);
}
exit_shell(cmd->big_cmd, ft_atoi(cmd->args[1])); exit_shell(cmd->big_cmd, ft_atoi(cmd->args[1]));
} }

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* export.c :+: :+: :+: */ /* export.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: syd <syd@student.42.fr> +#+ +:+ +#+ */ /* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/28 23:26:59 by sadjigui #+# #+# */ /* Created: 2022/04/20 00:31:25 by sadjigui #+# #+# */
/* Updated: 2022/04/19 23:11:33 by syd ### ########.fr */ /* Updated: 2022/04/20 00:31:35 by sadjigui ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* init_builtin.c :+: :+: :+: */ /* init_builtin.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: syd <syd@student.42.fr> +#+ +:+ +#+ */ /* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/28 17:17:01 by sadjigui #+# #+# */ /* Created: 2022/02/28 17:17:01 by sadjigui #+# #+# */
/* Updated: 2022/04/19 23:27:08 by syd ### ########.fr */ /* Updated: 2022/04/20 00:31:45 by sadjigui ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* unset.c :+: :+: :+: */ /* unset.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: syd <syd@student.42.fr> +#+ +:+ +#+ */ /* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/11 18:26:29 by sadjigui #+# #+# */ /* Created: 2022/03/11 18:26:29 by sadjigui #+# #+# */
/* Updated: 2022/04/19 23:59:16 by syd ### ########.fr */ /* Updated: 2022/04/20 00:31:56 by sadjigui ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -3,15 +3,13 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* utils_builtin.c :+: :+: :+: */ /* utils_builtin.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: syd <syd@student.42.fr> +#+ +:+ +#+ */ /* By: sadjigui <sadjigui@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/16 15:04:12 by sadjigui #+# #+# */ /* Created: 2022/03/16 15:04:12 by sadjigui #+# #+# */
/* Updated: 2022/04/20 00:13:17 by syd ### ########.fr */ /* Updated: 2022/04/20 00:36:10 by sadjigui ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../../includes/minishell.h"
int find_len(char *input, int i, char c) int find_len(char *input, int i, char c)
{ {
int j; int j;
@ -25,13 +23,11 @@ int find_len(char *input, int i, char c)
int find_it(char **str, char *s) int find_it(char **str, char *s)
{ {
int i; int i;
int size;
i = 0; i = 0;
size = ft_strlen(s); while (str[i] && (ft_strncmp(str[i], s, ft_strlen(s))
while (str[i] && (ft_strncmp(str[i], s, size) || (!ft_strncmp(str[i], s, ft_strlen(s))
|| (!ft_strncmp(str[i], s, size) && (str[i][ft_strlen(s)] != '=' && str[i][ft_strlen(s)] != 0))))
&& (str[i][size] != '=' && str[i][size] != 0))))
i++; i++;
if (str[i] == NULL) if (str[i] == NULL)
return (-1); return (-1);

View File

@ -6,26 +6,26 @@
/* 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:22:04 by apommier ### ########.fr */ /* Updated: 2022/04/19 16:02:18 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");
rl_replace_line("", 0); // rl_replace_line("", 0);
rl_on_new_line(); // rl_on_new_line();
rl_redisplay(); // rl_redisplay();
} // }
void sig_quit(int num) // void sig_quit(int num)
{ // {
(void)num; // (void)num;
ft_putstr_fd("\b \b\b \b", 1); // ft_putstr_fd("\b \b\b \b", 1);
} // }
char **ft_dup_double(char **env) char **ft_dup_double(char **env)
{ {
@ -48,25 +48,7 @@ char **ft_dup_double(char **env)
return (new_tab); return (new_tab);
} }
void print_prompt(char **path) char **read_line(char **path, char *input, t_cmd *cmd, int *err_var)
{
char *input;
t_cmd *cmd;
int err_var;
struct sigaction test;
memset(&test, 0, sizeof(test));
test.sa_handler = &sig_quit;
test.sa_flags = 0;
input = 0;
err_var = 0;
cmd = 0;
if (sigaction(SIGQUIT, &test, 0) == -1)
{
printf("Minishell: sigaction error\n");
exit(1);
}
while (1)
{ {
input = readline("\033[1;31m~$ \033[0m"); input = readline("\033[1;31m~$ \033[0m");
if (!input) if (!input)
@ -77,19 +59,28 @@ void print_prompt(char **path)
add_history(input); add_history(input);
if (ft_strlen(input) && next_space(input, 0) && input && path) if (ft_strlen(input) && next_space(input, 0) && input && path)
{ {
cmd = set_cmd(input, path, err_var); cmd = set_cmd(input, path, *err_var);
if (cmd) if (cmd)
{ {
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;
} }
} }
free(input); free(input);
return (path);
} }
void print_prompt(char **path)
{
int err_var;
err_var = 0;
while (1)
path = read_line(path, 0, 0, &err_var);
} }
int main(int ac, char **av, char **path) int main(int ac, char **av, char **path)
@ -110,6 +101,7 @@ int main(int ac, char **av, char **path)
} }
printf("---MINISHELL START---\n"); printf("---MINISHELL START---\n");
signal(SIGINT, crtl_c); signal(SIGINT, crtl_c);
signal(SIGQUIT, sig_quit);
if (env) if (env)
ft_shlvl(env); ft_shlvl(env);
print_prompt(env); print_prompt(env);

85
srcs/pipe/exec_utils.c Normal file
View File

@ -0,0 +1,85 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* exec_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 17:36:53 by apommier #+# #+# */
/* Updated: 2022/04/19 18:13:29 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/minishell.h"
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);
while (exit_pid != -1 && cmd->s_cmds[i])
{
exit_child(cmd, exit_pid, status, i);
i++;
}
}
return (ret);
}
void exit_child(t_cmd *cmd, int exit_pid, int status, int i)
{
if (exit_pid == cmd->s_cmds[i]->child)
{
close(cmd->s_cmds[i]->fd[0]);
close(cmd->s_cmds[i]->fd[1]);
if (WIFEXITED(status))
cmd->err_var = WEXITSTATUS(status);
else if (WIFSIGNALED(status))
{
if (WTERMSIG(status) != 13)
cmd->err_var = WTERMSIG(status);
ft_putstr_fd("\b\b\b\b\b", 1);
if (WTERMSIG(status) == 3)
ft_putstr_fd("^\\Quit", 1);
}
}
}
void check_access(t_cmd *cmd)
{
if (!cmd->current_s_cmd->cmd || access(cmd->current_s_cmd->cmd, F_OK))
{
ft_putstr_fd("Minishell: command not found: ", 2);
ft_putstr_fd(cmd->current_s_cmd->cmd, 2);
ft_putstr_fd("\n", 2);
close(0);
close(1);
close(cmd->tmpin);
close(cmd->tmpout);
free_cmd(cmd);
exit(127);
}
}
void close_pipe(t_cmd *cmd)
{
int i;
i = 0;
close(0);
while (cmd->s_cmds[i])
{
close(cmd->s_cmds[i]->fd[0]);
close(cmd->s_cmds[i]->fd[1]);
i++;
}
}

View File

@ -6,63 +6,12 @@
/* 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:30:01 by apommier ### ########.fr */ /* Updated: 2022/04/19 18:12:19 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../../includes/minishell.h" #include "../../includes/minishell.h"
void close_pipe(t_cmd *cmd)
{
int i;
i = 0;
close(0);
while (cmd->s_cmds[i])
{
close(cmd->s_cmds[i]->fd[0]);
close(cmd->s_cmds[i]->fd[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);
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]);
if (WIFEXITED(status))
cmd->err_var = WEXITSTATUS(status);
else if (WIFSIGNALED(status))
{
if (WTERMSIG(status) != 13)
cmd->err_var = WTERMSIG(status);
if (WTERMSIG(status) == 3)
ft_putstr_fd("^\\Quit", 1);
ft_putstr_fd("\b\b\b\b\b", 1);
}
}
i++;
}
}
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();
@ -79,19 +28,9 @@ void exec_cmd(t_cmd *cmd, char **env, int *fdpipe)
call_builtin(cmd); call_builtin(cmd);
exit(0); exit(0);
} }
if (!cmd->current_s_cmd->cmd || access(cmd->current_s_cmd->cmd, F_OK)) check_access(cmd);
{ if (-1 == execve(cmd->current_s_cmd->cmd,
ft_putstr_fd("Minishell: command not found: ", 2); cmd->current_s_cmd->args, env))
ft_putstr_fd(cmd->current_s_cmd->cmd, 2);
ft_putstr_fd("\n", 2);
close(0);
close(1);
close(cmd->tmpin);
close(cmd->tmpout);
free_cmd(cmd);
exit(127);
}
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);
} }
@ -101,6 +40,48 @@ void exec_cmd(t_cmd *cmd, char **env, int *fdpipe)
cmd->err_var = 131; cmd->err_var = 131;
} }
void exec_last_cmd(t_cmd *cmd, int *fdin, int *fdout, int i)
{
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
*fdout = dup(cmd->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))
call_builtin(cmd);
else
exec_cmd(cmd, cmd->env, 0);
}
void exec_not_last_cmd(t_cmd *cmd, int *fdpipe, int *fdin, int *fdout)
{
cmd->current_s_cmd->last = 0;
pipe(fdpipe);
cmd->current_s_cmd->fd[0] = *fdin;
if (cmd->current_s_cmd->outfile)
{
*fdout = open(cmd->current_s_cmd->outfile,
O_RDWR | O_CREAT | O_APPEND, 0666);
cmd->current_s_cmd->fd[1] = *fdout;
}
else
cmd->current_s_cmd->fd[1] = fdpipe[1];
*fdin = fdpipe[0];
exec_cmd(cmd, cmd->env, fdpipe);
close(cmd->current_s_cmd->fd[0]);
}
void set_fdin_not_first(t_cmd *cmd, int *fdin)
{
close(*fdin);
*fdin = open(cmd->current_s_cmd->infile, O_RDWR);
if (*fdin < 0)
printf("Minishell: open : bad file descriptor\n");
}
void execute(t_cmd *cmd, char **env) void execute(t_cmd *cmd, char **env)
{ {
int fdpipe[2]; int fdpipe[2];
@ -112,64 +93,20 @@ void execute(t_cmd *cmd, char **env)
fdpipe[1] = -1; fdpipe[1] = -1;
cmd->tmpin = dup(0); cmd->tmpin = dup(0);
cmd->tmpout = dup(1); cmd->tmpout = dup(1);
if (cmd->current_s_cmd->infile) set_fdin(cmd, &fdin);
{
fdin = open(cmd->current_s_cmd->infile, O_RDWR);
if (fdin < 0)
printf("Minishell: open : bad file descriptor\n");
}
else
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;
if (i > 0 && cmd->current_s_cmd->infile) if (i > 0 && cmd->current_s_cmd->infile)
{ set_fdin_not_first(cmd, &fdin);
close(fdin);
fdin = open(cmd->current_s_cmd->infile, O_RDWR);
if (fdin < 0)
printf("Minishell: open : bad file descriptor\n");
}
if (i == cmd->nb_s_cmd - 1) if (i == cmd->nb_s_cmd - 1)
{ exec_last_cmd(cmd, &fdin, &fdout, i);
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 else
fdout = dup(cmd->tmpout); exec_not_last_cmd(cmd, fdpipe, &fdin, &fdout);
cmd->current_s_cmd->fd[0] = fdin;
cmd->current_s_cmd->fd[1] = fdout;
if (i == 0 && is_builtin(cmd->current_s_cmd->cmd))
call_builtin(cmd);
else
exec_cmd(cmd, env, 0);
}
else
{
cmd->current_s_cmd->last = 0;
pipe(fdpipe);
cmd->current_s_cmd->fd[0] = fdin;
if (cmd->current_s_cmd->outfile)
{
fdout = open(cmd->current_s_cmd->outfile, O_RDWR | O_CREAT | O_APPEND, 0666);
cmd->current_s_cmd->fd[1] = fdout;
}
else
cmd->current_s_cmd->fd[1] = fdpipe[1];
fdin = fdpipe[0];
exec_cmd(cmd, env, fdpipe);
close(cmd->current_s_cmd->fd[0]);
}
if (fdpipe[1] != -1) if (fdpipe[1] != -1)
close(fdpipe[1]); close(fdpipe[1]);
i++; cmd->current_s_cmd = cmd->s_cmds[++i];
cmd->current_s_cmd = cmd->s_cmds[i];
} }
close_pipe(cmd); reset_fds(cmd);
dup2(cmd->tmpin, 0);
dup2(cmd->tmpout, 1);
close(cmd->tmpin);
close(cmd->tmpout);
wait_exit(cmd);
} }

View File

@ -6,12 +6,34 @@
/* 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:34:06 by apommier ### ########.fr */ /* Updated: 2022/04/19 18:11:38 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../../includes/minishell.h" #include "../../includes/minishell.h"
void set_fdin(t_cmd *cmd, int *fdin)
{
if (cmd->current_s_cmd->infile)
{
*fdin = open(cmd->current_s_cmd->infile, O_RDWR);
if (*fdin < 0)
printf("Minishell: open : bad file descriptor\n");
}
else
*fdin = dup(cmd->tmpin);
}
void reset_fds(t_cmd *cmd)
{
close_pipe(cmd);
dup2(cmd->tmpin, 0);
dup2(cmd->tmpout, 1);
close(cmd->tmpin);
close(cmd->tmpout);
wait_exit(cmd);
}
char **get_path(char **env) char **get_path(char **env)
{ {
int i; int i;

View File

@ -6,18 +6,12 @@
/* 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 14:40:24 by apommier ### ########.fr */ /* Updated: 2022/04/19 18:30:14 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../../includes/minishell.h" #include "../../includes/minishell.h"
char *error_parsing(void)
{
ft_putstr_fd("Minishell: error while parsing command\n", 2);
return (0);
}
t_s_cmd *set_s_cmd(char *line, int index) t_s_cmd *set_s_cmd(char *line, int index)
{ {
t_s_cmd *s_cmd; t_s_cmd *s_cmd;
@ -33,7 +27,7 @@ t_s_cmd *set_s_cmd(char *line, int index)
s_cmd->args = 0; s_cmd->args = 0;
s_cmd->infile = 0; s_cmd->infile = 0;
s_cmd->outfile = 0; s_cmd->outfile = 0;
line = set_redirection(s_cmd, line, index); line = set_redirection(s_cmd, line, index, 0);
if (!line) if (!line)
{ {
free(s_cmd); free(s_cmd);
@ -85,25 +79,8 @@ int is_pipe_good(char *str)
return (1); return (1);
} }
t_cmd *set_cmd(char *input, char **env, int nb) t_cmd *initialize_cmd(t_cmd *cmd, char **env, char **cmds, int nb)
{ {
t_cmd *cmd;
char **cmds;
if (!is_quote_good(input) || !is_pipe_good(input))
{
ft_putstr_fd("Minishell: error while parsing command\n", 2);
return (0);
}
cmds = ft_split_with_quote(input, '|');
if (!cmds)
return (0);
cmd = malloc(sizeof(t_cmd));
if (!cmd)
return (0);
cmd->s_cmds = ft_calloc(sizeof(t_s_cmd), double_size(cmds) + 1);
if (!cmd->s_cmds)
return (0);
cmd->tmpin = -1; cmd->tmpin = -1;
cmd->tmpout = -1; cmd->tmpout = -1;
cmd->err_var = nb; cmd->err_var = nb;
@ -126,3 +103,25 @@ t_cmd *set_cmd(char *input, char **env, int nb)
} }
return (0); return (0);
} }
t_cmd *set_cmd(char *input, char **env, int nb)
{
t_cmd *cmd;
char **cmds;
if (!is_quote_good(input) || !is_pipe_good(input))
{
ft_putstr_fd("Minishell: error while parsing command\n", 2);
return (0);
}
cmds = ft_split_with_quote(input, '|');
if (!cmds)
return (0);
cmd = malloc(sizeof(t_cmd));
if (!cmd)
return (0);
cmd->s_cmds = ft_calloc(sizeof(t_s_cmd), double_size(cmds) + 1);
if (!cmd->s_cmds)
return (0);
return (initialize_cmd(cmd, env, cmds, nb));
}

View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* set_cmd_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 16:53:13 by apommier #+# #+# */
/* Updated: 2022/04/19 18:27:58 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/minishell.h"
int error_parsing(void)
{
ft_putstr_fd("Minishell: error while parsing command\n", 2);
return (0);
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/09 23:58:21 by apommier #+# #+# */ /* Created: 2022/04/09 23:58:21 by apommier #+# #+# */
/* Updated: 2022/04/19 15:07:01 by apommier ### ########.fr */ /* Updated: 2022/04/19 17:19:17 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -40,6 +40,26 @@ int is_in_quote(char *str, int index)
return (open); return (open);
} }
void count_quote(char c, int *open, int *simple_quote, int *double_quote)
{
if (c == '\'' && *open != 2)
{
if (!(*open))
(*open) = 1;
else if ((*open) == 1)
(*open) = 0;
(*simple_quote)++;
}
else if (c == '"' && (*open) != 1)
{
if (!(*open))
(*open) = 2;
else if ((*open) == 2)
(*open) = 0;
(*double_quote)++;
}
}
int is_quote_good(char *str) int is_quote_good(char *str)
{ {
int simple_quote; int simple_quote;
@ -53,22 +73,7 @@ int is_quote_good(char *str)
double_quote = 0; double_quote = 0;
while (str[i]) while (str[i])
{ {
if (str[i] == '\'' && open != 2) count_quote(str[i], &open, &simple_quote, &double_quote);
{
if (!open)
open = 1;
else if (open == 1)
open = 0;
simple_quote++;
}
else if (str[i] == '"' && open != 1)
{
if (!open)
open = 2;
else if (open == 2)
open = 0;
double_quote++;
}
i++; i++;
} }
if (simple_quote % 2 || double_quote % 2) if (simple_quote % 2 || double_quote % 2)

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 15:02:01 by apommier #+# #+# */ /* Created: 2022/04/19 15:02:01 by apommier #+# #+# */
/* Updated: 2022/04/19 15:04:45 by apommier ### ########.fr */ /* Updated: 2022/04/19 19:55:04 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -77,6 +77,22 @@ char *change_var(t_cmd *big_cmd, char *cmd, int *index)
return (ret); return (ret);
} }
char *check_quote(t_cmd *big_cmd, char *cmd, int *i)
{
cmd = del_char(cmd, i);
if (cmd[*i])
{
while (cmd[*i] != '"')
{
if (cmd[*i] == '$')
cmd = change_var(big_cmd, cmd, i);
(*i)++;
}
cmd = del_char(cmd, i);
}
return (cmd);
}
char *set_var(t_cmd *big_cmd, char *cmd) char *set_var(t_cmd *big_cmd, char *cmd)
{ {
int i; int i;
@ -90,26 +106,12 @@ char *set_var(t_cmd *big_cmd, char *cmd)
if (cmd[i]) if (cmd[i])
{ {
while (cmd[i] != '\'') while (cmd[i] != '\'')
{
i++; i++;
}
cmd = del_char(cmd, &i); cmd = del_char(cmd, &i);
} }
} }
else if (cmd[i] == '"') else if (cmd[i] == '"')
{ cmd = check_quote(big_cmd, cmd, &i);
cmd = del_char(cmd, &i);
if (cmd[i])
{
while (cmd[i] != '"')
{
if (cmd[i] == '$')
cmd = change_var(big_cmd, cmd, &i);
i++;
}
cmd = del_char(cmd, &i);
}
}
else if (cmd[i] == '$') else if (cmd[i] == '$')
cmd = change_var(big_cmd, cmd, &i); cmd = change_var(big_cmd, cmd, &i);
else else

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/10 19:50:50 by apommier #+# #+# */ /* Created: 2022/04/10 19:50:50 by apommier #+# #+# */
/* Updated: 2022/04/19 12:58:16 by apommier ### ########.fr */ /* Updated: 2022/04/19 16:15:11 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,8 +25,6 @@ int next_quote(const char *s, int i)
double_quote = 1; double_quote = 1;
else if (s[i] == '\'') else if (s[i] == '\'')
simple_quote = 1; simple_quote = 1;
else
printf("qu'es tu fous ?\n");
i++; i++;
if (simple_quote) if (simple_quote)
{ {
@ -38,9 +36,8 @@ int next_quote(const char *s, int i)
while (s[i] != '"') while (s[i] != '"')
i++; i++;
} }
i++;
} }
return (i); return (i + 1);
} }
static int fill_tab(char *s, char c, char **dest, size_t index) static int fill_tab(char *s, char c, char **dest, size_t index)
@ -94,6 +91,23 @@ static void call(char *s, char c, char **dest, int j)
} }
} }
void count_word(const char *s, int *i, int *j, char c)
{
while (s[*i])
{
while (s[*i] != c && s[*i])
{
if (s[*i] == '"' || s[*i] == '\'')
*i = next_quote(s, *i);
else
(*i)++;
}
(*j)++;
while (s[*i] == c && s[*i])
(*i)++;
}
}
char **ft_split_with_quote(char const *s, char c) char **ft_split_with_quote(char const *s, char c)
{ {
int i; int i;
@ -109,19 +123,7 @@ char **ft_split_with_quote(char const *s, char c)
j++; j++;
while (s[i] == c && s[i]) while (s[i] == c && s[i])
i++; i++;
while (s[i]) count_word(s, &i, &j, c);
{
while (s[i] != c && s[i])
{
if (s[i] == '"' || s[i] == '\'')
i = next_quote(s, i);
else
i++;
}
j++;
while (s[i] == c && s[i])
i++;
}
dest = (char **)ft_calloc(sizeof(char *), (1 + j)); dest = (char **)ft_calloc(sizeof(char *), (1 + j));
if (!dest) if (!dest)
return (0); return (0);

View File

@ -0,0 +1,69 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* heredoc_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 19:25:16 by apommier #+# #+# */
/* Updated: 2022/04/19 19:36:02 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/minishell.h"
void sig_heredoc(int num)
{
struct sigaction base;
(void)num;
memset(&base, 0, sizeof(base));
base.sa_handler = &crtl_c;
base.sa_flags = 0;
ft_putchar_fd('\n', 1);
if (sigaction(SIGINT, &base, 0) == -1)
{
printf("sigaction error2\n");
return ;
}
}
void change_signal(void)
{
struct sigaction test;
memset(&test, 0, sizeof(test));
test.sa_handler = &sig_heredoc;
test.sa_flags = 0;
if (sigaction(SIGINT, &test, 0) == -1)
{
printf("sigaction error\n");
exit(1);
}
}
int free_wait_prompt(char *in, char**history)
{
free(in);
free_double(history);
return (-1);
}
char **fill_history(t_s_cmd *cmd, char *input, char *in, char **history)
{
char *del;
char *dup;
if (ft_strcmp(input, in))
{
dup = ft_strdup(input);
dup[ft_strlen(input) - 1] = 0;
del = dup;
dup = set_var(cmd->big_cmd, dup);
history = add_line(history, dup);
if (dup != del)
free(del);
free(dup);
}
return (history);
}

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/09 15:18:58 by apommier #+# #+# */ /* Created: 2022/03/09 15:18:58 by apommier #+# #+# */
/* Updated: 2022/04/19 14:56:33 by apommier ### ########.fr */ /* Updated: 2022/04/19 19:15:00 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -73,31 +73,35 @@ char **add_line(char **tab, char *line)
return (ret); return (ret);
} }
char *set_redirection(t_s_cmd *cmd, char *line, int index) int set_redirect_in(t_s_cmd *cmd, char **line, int *i, int index)
{ {
int i; if (!is_in_quote(*line, *i))
{
*line = ft_input(*line, cmd, *i);
if (!(*line))
return (error_parsing());
if (cmd->in_type == 1)
{
if (wait_prompt(cmd, index, 0, 0) == -1)
{
free(*line);
return (0);
}
}
*i = 0;
}
return (1);
}
i = 0; char *set_redirection(t_s_cmd *cmd, char *line, int index, int i)
{
while (line[i]) while (line[i])
{ {
if (line[i] == '<') if (line[i] == '<')
{ {
if (!is_in_quote(line, i)) if (!set_redirect_in(cmd, &line, &i, index))
{
line = ft_input(line, cmd, i);
if (!line)
return (error_parsing());
if (cmd->in_type == 1)
{
if (wait_prompt(cmd, index) == -1)
{
free(line);
return (0); return (0);
} }
}
i = 0;
}
}
else if (line[i] == '>') else if (line[i] == '>')
{ {
if (!is_in_quote(line, i)) if (!is_in_quote(line, i))
@ -108,7 +112,8 @@ char *set_redirection(t_s_cmd *cmd, char *line, int index)
i = 0; i = 0;
} }
} }
if (line[i] && (line[i] == '<' || line[i] == '>') && is_in_quote(line, i)) if (line[i] && (line[i] == '<' || line[i] == '>')
&& is_in_quote(line, i))
i++; i++;
else if (line[i] && line[i] != '<' && line[i] != '>') else if (line[i] && line[i] != '<' && line[i] != '>')
i++; i++;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 14:46:01 by apommier #+# #+# */ /* Created: 2022/04/19 14:46:01 by apommier #+# #+# */
/* Updated: 2022/04/19 14:51:11 by apommier ### ########.fr */ /* Updated: 2022/04/19 19:36:08 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,46 +37,16 @@ char *set_heredoc(int index, char **in)
return (file_name); return (file_name);
} }
void sig_heredoc(int num) int wait_prompt(t_s_cmd *cmd, int index, int i, char *input)
{ {
struct sigaction base;
(void)num;
memset(&base, 0, sizeof(base));
base.sa_handler = &crtl_c;
base.sa_flags = 0;
ft_putchar_fd('\n', 1);
if (sigaction(SIGINT, &base, 0) == -1)
{
printf("sigaction error2\n");
return ;
}
}
int wait_prompt(t_s_cmd *cmd, int index)
{
char *input;
int i;
char **history; char **history;
char *in; char *in;
char *dup;
char *del;
struct sigaction test;
memset(&test, 0, sizeof(test)); change_signal();
test.sa_handler = &sig_heredoc;
test.sa_flags = 0;
in = ft_strjoin(cmd->infile, "\n"); in = ft_strjoin(cmd->infile, "\n");
free(cmd->infile); free(cmd->infile);
cmd->infile = 0; cmd->infile = 0;
if (sigaction(SIGINT, &test, 0) == -1)
{
printf("sigaction error\n");
exit(1);
}
history = 0; history = 0;
input = 0;
i = 0;
while (i == 0 || (input && ft_strlen(input) && ft_strcmp(input, in))) while (i == 0 || (input && ft_strlen(input) && ft_strcmp(input, in)))
{ {
i = 1; i = 1;
@ -85,22 +55,8 @@ int wait_prompt(t_s_cmd *cmd, int index)
ft_putstr_fd("> ", 0); ft_putstr_fd("> ", 0);
input = get_next_line(0); input = get_next_line(0);
if (!input) if (!input)
{ return (free_wait_prompt(in, history));
free(in); history = fill_history(cmd, input, in, history);
free_double(history);
return (-1);
}
if (ft_strcmp(input, in))
{
dup = ft_strdup(input);
dup[ft_strlen(input) - 1] = 0;
del = dup;
dup = set_var(cmd->big_cmd, dup);
history = add_line(history, dup);
if (dup != del)
free(del);
free(dup);
}
} }
free(in); free(in);
free(input); free(input);

View File

@ -6,20 +6,35 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */ /* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 14:44:22 by apommier #+# #+# */ /* Created: 2022/04/19 14:44:22 by apommier #+# #+# */
/* Updated: 2022/04/19 14:51:14 by apommier ### ########.fr */ /* Updated: 2022/04/19 18:20:56 by apommier ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "../../includes/minishell.h" #include "../../includes/minishell.h"
int check_access_input(t_s_cmd *cmd)
{
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);
free(cmd->infile);
return (0);
}
return (1);
}
char *set_input(char *line, t_s_cmd *cmd, int index) char *set_input(char *line, t_s_cmd *cmd, int index)
{ {
int i; int i;
int word_index; int word_index;
word_index = 0; word_index = 0;
i = index; i = index + 1;
i++;
if (line[i] == '<') if (line[i] == '<')
i++; i++;
word_index = i; word_index = i;
@ -33,18 +48,9 @@ char *set_input(char *line, t_s_cmd *cmd, int index)
cmd->infile = set_var(cmd->big_cmd, cmd->infile); cmd->infile = set_var(cmd->big_cmd, cmd->infile);
if (cmd->in_type == 0) if (cmd->in_type == 0)
{ {
if (access(cmd->infile, R_OK)) if (!check_access_input(cmd))
{
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);
free(cmd->infile);
return (0); return (0);
} }
}
line = cut_str(line, index, i); line = cut_str(line, index, i);
return (line); return (line);
} }

View File

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* set_signal.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 15:30:30 by apommier #+# #+# */
/* Updated: 2022/04/19 16:58:02 by apommier ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/minishell.h"
void crtl_c(int num)
{
num = 0;
printf("\n");
rl_replace_line("", 0);
rl_on_new_line();
rl_redisplay();
}
void sig_quit(int num)
{
(void)num;
ft_putstr_fd("\b \b\b \b", 1);
}