This commit is contained in:
kinou-p 2022-04-19 19:36:52 +02:00
parent d44a1b5e44
commit 1bb471f7a9
17 changed files with 492 additions and 343 deletions

View File

@ -6,7 +6,7 @@
# By: apommier <apommier@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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/pipe/pipe.c\
srcs/pipe/pipex_utils.c\
srcs/pipe/exec_utils.c\
srcs/set_cmd/free_cmd.c\
srcs/set_cmd/set_cmd.c\
srcs/set_cmd/set_cmd_utils.c\
srcs/set_redirection/redirection.c\
srcs/set_redirection/utils.c\
srcs/set_redirection/set_heredoc.c\
srcs/set_redirection/heredoc_utils.c\
srcs/set_redirection/set_input.c\
srcs/set_redirection/set_output.c\
srcs/built_in/unset.c\
@ -30,10 +33,12 @@ SRCS = srcs/main.c\
srcs/built_in/export2.c\
srcs/built_in/env.c\
srcs/built_in/exit.c\
srcs/built_in/choose_builtin.c\
srcs/set_quote/split_with_quote.c\
srcs/set_quote/set_quote.c\
srcs/set_quote/set_var.c\
srcs/built_in/choose_builtin.c
srcs/set_signals/set_signal.c\
srcs/set_quote/set_var.c
OBJS = ${SRCS:.c=.o}
CC = clang

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
@ -58,9 +58,7 @@ typedef struct s_command {
int err_var;
struct s_simple *current_s_cmd;
char **path;
} t_cmd;
char *error_parsing(void);
} t_cmd;
//main.c
int main(int ac, char **av, char **path);
@ -70,9 +68,18 @@ char **ft_dup_double(char **env);
void execute(t_cmd *cmd, char **env);
//set_cmd.c
int error_parsing(void);
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
void set_fdin(t_cmd *cmd, int *fdin);
void reset_fds(t_cmd *cmd);
char **get_path(char **env);
char *get_command(char **exec, char **env);
@ -89,15 +96,23 @@ char **ft_split_with_quote(char const *s, char c);
//signals
void crtl_c(int num);
void sig_heredoc(int num);
void sig_quit(int num);
//redirection.c set redirection and input good
char *get_word(char *str, int start);
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);
//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
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
char *set_var(t_cmd *big_cmd, char *cmd);
//set_signals.c
//utils redirection
int parse_quote(t_cmd *cmd);
char *get_str(char *str, int start, int end);

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
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)
{
int i;
@ -43,16 +49,11 @@ void ft_exit(t_s_cmd *cmd)
exit_shell(cmd->big_cmd, 0);
while (cmd->args[1][++i])
{
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_shell(cmd->big_cmd, 2);
}
if ((!ft_isdigit(cmd->args[1][i]) && !(cmd->args[1][i] == '-'
&& ft_isdigit(cmd->args[1][i + 1]))))
exit_error(cmd->big_cmd);
}
if (max_long(cmd->args[1]))
{
ft_putstr_fd("Minishell: exit: numeric argument required\n", 2);
exit_shell(cmd->big_cmd, 2);
}
exit_error(cmd->big_cmd);
exit_shell(cmd->big_cmd, ft_atoi(cmd->args[1]));
}

View File

@ -6,26 +6,26 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
void crtl_c(int num)
{
num = 0;
printf("\n");
rl_replace_line("", 0);
rl_on_new_line();
rl_redisplay();
}
// 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);
}
// void sig_quit(int num)
// {
// (void)num;
// ft_putstr_fd("\b \b\b \b", 1);
// }
char **ft_dup_double(char **env)
{
@ -48,48 +48,39 @@ char **ft_dup_double(char **env)
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)
input = readline("\033[1;31m~$ \033[0m");
if (!input)
{
printf("Minishell: sigaction error\n");
exit(1);
free_double(path);
exit_shell(cmd, 0);
}
while (1)
add_history(input);
if (ft_strlen(input) && next_space(input, 0) && input && path)
{
input = readline("\033[1;31m~$ \033[0m");
if (!input)
cmd = set_cmd(input, path, *err_var);
if (cmd)
{
free_double(path);
exit_shell(cmd, 0);
execute(cmd, cmd->env);
*err_var = cmd->err_var;
path = ft_dup_double(cmd->env);
free_cmd(cmd);
cmd = 0;
}
add_history(input);
if (ft_strlen(input) && next_space(input, 0) && input && path)
{
cmd = set_cmd(input, path, err_var);
if (cmd)
{
free_double(path);
execute(cmd, cmd->env);
err_var = cmd->err_var;
path = ft_dup_double(cmd->env);
free_cmd(cmd);
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)
@ -110,6 +101,7 @@ int main(int ac, char **av, char **path)
}
printf("---MINISHELL START---\n");
signal(SIGINT, crtl_c);
signal(SIGQUIT, sig_quit);
if (env)
ft_shlvl(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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
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)
{
cmd->current_s_cmd->child = fork();
@ -79,19 +28,9 @@ void exec_cmd(t_cmd *cmd, char **env, int *fdpipe)
call_builtin(cmd);
exit(0);
}
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);
}
if (-1 == execve(cmd->current_s_cmd->cmd, cmd->current_s_cmd->args, env))
check_access(cmd);
if (-1 == execve(cmd->current_s_cmd->cmd,
cmd->current_s_cmd->args, env))
ft_putstr_fd("Minishell: exec error\n", 2);
exit(126);
}
@ -101,6 +40,48 @@ void exec_cmd(t_cmd *cmd, char **env, int *fdpipe)
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)
{
int fdpipe[2];
@ -112,64 +93,20 @@ void execute(t_cmd *cmd, char **env)
fdpipe[1] = -1;
cmd->tmpin = dup(0);
cmd->tmpout = dup(1);
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);
set_fdin(cmd, &fdin);
while (cmd->current_s_cmd)
{
cmd->current_s_cmd->child = 1;
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 : bad file descriptor\n");
}
set_fdin_not_first(cmd, &fdin);
if (i == cmd->nb_s_cmd - 1)
{
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, env, 0);
}
exec_last_cmd(cmd, &fdin, &fdout, i);
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]);
}
exec_not_last_cmd(cmd, fdpipe, &fdin, &fdout);
if (fdpipe[1] != -1)
close(fdpipe[1]);
i++;
cmd->current_s_cmd = cmd->s_cmds[i];
cmd->current_s_cmd = cmd->s_cmds[++i];
}
close_pipe(cmd);
dup2(cmd->tmpin, 0);
dup2(cmd->tmpout, 1);
close(cmd->tmpin);
close(cmd->tmpout);
wait_exit(cmd);
reset_fds(cmd);
}

View File

@ -6,12 +6,34 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
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)
{
int i;

View File

@ -6,18 +6,12 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
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 *s_cmd;
@ -33,7 +27,7 @@ t_s_cmd *set_s_cmd(char *line, int index)
s_cmd->args = 0;
s_cmd->infile = 0;
s_cmd->outfile = 0;
line = set_redirection(s_cmd, line, index);
line = set_redirection(s_cmd, line, index, 0);
if (!line)
{
free(s_cmd);
@ -85,25 +79,8 @@ int is_pipe_good(char *str)
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->tmpout = -1;
cmd->err_var = nb;
@ -126,3 +103,25 @@ t_cmd *set_cmd(char *input, char **env, int nb)
}
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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
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 simple_quote;
@ -53,22 +73,7 @@ int is_quote_good(char *str)
double_quote = 0;
while (str[i])
{
if (str[i] == '\'' && open != 2)
{
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++;
}
count_quote(str[i], &open, &simple_quote, &double_quote);
i++;
}
if (simple_quote % 2 || double_quote % 2)

View File

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

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
else if (s[i] == '\'')
simple_quote = 1;
else
printf("qu'es tu fous ?\n");
i++;
if (simple_quote)
{
@ -38,9 +36,8 @@ int next_quote(const char *s, int i)
while (s[i] != '"')
i++;
}
i++;
}
return (i);
return (i + 1);
}
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)
{
int i;
@ -109,19 +123,7 @@ char **ft_split_with_quote(char const *s, char c)
j++;
while (s[i] == c && s[i])
i++;
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++;
}
count_word(s, &i, &j, c);
dest = (char **)ft_calloc(sizeof(char *), (1 + j));
if (!dest)
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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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,30 +73,34 @@ char **add_line(char **tab, char *line)
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])
{
if (line[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) == -1)
{
free(line);
return (0);
}
}
i = 0;
}
if (!set_redirect_in(cmd, &line, &i, index))
return (0);
}
else if (line[i] == '>')
{
@ -108,7 +112,8 @@ char *set_redirection(t_s_cmd *cmd, char *line, int index)
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++;
else if (line[i] && line[i] != '<' && line[i] != '>')
i++;

View File

@ -6,7 +6,7 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
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 *in;
char *dup;
char *del;
struct sigaction test;
memset(&test, 0, sizeof(test));
test.sa_handler = &sig_heredoc;
test.sa_flags = 0;
change_signal();
in = ft_strjoin(cmd->infile, "\n");
free(cmd->infile);
cmd->infile = 0;
if (sigaction(SIGINT, &test, 0) == -1)
{
printf("sigaction error\n");
exit(1);
}
history = 0;
input = 0;
i = 0;
while (i == 0 || (input && ft_strlen(input) && ft_strcmp(input, in)))
{
i = 1;
@ -85,26 +55,12 @@ int wait_prompt(t_s_cmd *cmd, int index)
ft_putstr_fd("> ", 0);
input = get_next_line(0);
if (!input)
{
free(in);
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);
}
return (free_wait_prompt(in, history));
history = fill_history(cmd, input, in, history);
}
free(in);
free(input);
cmd->infile = set_heredoc(index, history);
cmd->in_type = 0;
return (1);
}
}

View File

@ -6,20 +6,35 @@
/* By: apommier <apommier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
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)
{
int i;
int word_index;
word_index = 0;
i = index;
i++;
i = index + 1;
if (line[i] == '<')
i++;
word_index = i;
@ -33,17 +48,8 @@ char *set_input(char *line, t_s_cmd *cmd, int index)
cmd->infile = set_var(cmd->big_cmd, cmd->infile);
if (cmd->in_type == 0)
{
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);
if (!check_access_input(cmd))
return (0);
}
}
line = cut_str(line, index, i);
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);
}