Compare commits

..

2 Commits

Author SHA1 Message Date
Camille Chauvet
6498031d59 clean: norm part2 (FINAL) 2023-03-29 19:07:57 +02:00
Camille Chauvet
3e656abf5d fix: env file 2023-03-29 19:07:29 +02:00
19 changed files with 357 additions and 192 deletions

View File

@ -23,7 +23,9 @@ SRCS = ${BUILTINS_SRC} \
./redirection/heredoc.c \ ./redirection/heredoc.c \
./redirection/file.c \ ./redirection/file.c \
./redirection/redirection.c \ ./redirection/redirection.c \
./parse/parse.c ./redirection/check.c \
./parse/parse.c \
./signal/signal.c
OBJS = ${SRCS:.c=.o} OBJS = ${SRCS:.c=.o}

27
env/env_fill.c vendored
View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 16:29:08 by cchauvet #+# #+# */ /* Created: 2023/02/16 16:29:08 by cchauvet #+# #+# */
/* Updated: 2023/03/27 16:42:27 by cchauvet ### ########.fr */ /* Updated: 2023/03/29 19:06:44 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -73,12 +73,25 @@ static char *ft_getvalue_by_str(t_data *data, const char *str,
return (value); return (value);
} }
char *ft_str_formator(t_data *data, char *str, size_t *i)
{
char *value;
size_t key_len;
char *out;
value = ft_getvalue_by_str(data, str + *i, &key_len);
if (value == NULL)
return (NULL);
out = ft_strreplace(str, value, *i, key_len + *i);
*i = *i + ft_strlen(value);
free(value);
return (out);
}
char *ft_env_filler(t_data *data, const char *str) char *ft_env_filler(t_data *data, const char *str)
{ {
char *out; char *out;
char *temp; char *temp;
char *value;
size_t key_len;
size_t i; size_t i;
out = ft_strdup(str); out = ft_strdup(str);
@ -91,14 +104,10 @@ char *ft_env_filler(t_data *data, const char *str)
i++; i++;
while (out[i] == '$') while (out[i] == '$')
{ {
value = ft_getvalue_by_str(data, out + i, &key_len); temp = ft_str_formator(data, out, &i);
if (value == NULL)
return (NULL);
temp = ft_strreplace(out, value, i, key_len + i);
i = i + ft_strlen(value);
ft_freer_ultimate(2, value, out);
if (temp == NULL) if (temp == NULL)
return (NULL); return (NULL);
free(out);
out = temp; out = temp;
} }
if (out[i] != '\0') if (out[i] != '\0')

View File

@ -6,17 +6,16 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */ /* Created: 2023/02/21 12:45:16 by cchauvet #+# #+# */
/* Updated: 2023/03/28 15:46:43 by cchauvet ### ########.fr */ /* Updated: 2023/03/29 18:53:35 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "execution_private.h" #include "execution_private.h"
static int ft_own_cmd(t_data *data, t_cmd *cmd) static int ft_execute_own_cmd(t_data *data, t_cmd *cmd)
{ {
int return_code; int return_code;
return_code = -1;
if (ft_strcmp(cmd->executable, "pwd") == 0) if (ft_strcmp(cmd->executable, "pwd") == 0)
return_code = pwd(cmd->fd_out[0]); return_code = pwd(cmd->fd_out[0]);
else if (ft_strcmp(cmd->executable, "env") == 0) else if (ft_strcmp(cmd->executable, "env") == 0)
@ -29,7 +28,7 @@ static int ft_own_cmd(t_data *data, t_cmd *cmd)
return_code = (unset(data->env, cmd->args, cmd->fd_out[0])); return_code = (unset(data->env, cmd->args, cmd->fd_out[0]));
else if (ft_strcmp(cmd->executable, "echo") == 0) else if (ft_strcmp(cmd->executable, "echo") == 0)
return_code = (echo(cmd->fd_out[0], cmd->args + 1)); return_code = (echo(cmd->fd_out[0], cmd->args + 1));
else if (ft_strcmp(cmd->executable, "exit") == 0) else
{ {
return_code = ft_exit(cmd->args + 1); return_code = ft_exit(cmd->args + 1);
if (return_code >= 0) if (return_code >= 0)
@ -72,7 +71,7 @@ static int ft_cmd_executor(t_data *data, t_cmd *cmd, int fd)
if (cmd->own_cmd == 1) if (cmd->own_cmd == 1)
{ {
exit_code = ft_own_cmd(data, cmd); exit_code = ft_execute_own_cmd(data, cmd);
ft_closer(cmd->fd_in); ft_closer(cmd->fd_in);
ft_closer(cmd->fd_out); ft_closer(cmd->fd_out);
if (exit_code == -1) if (exit_code == -1)
@ -104,7 +103,6 @@ int ft_cmds_executor(t_data *data)
{ {
content = current->content; content = current->content;
fds[0] = -1; fds[0] = -1;
fds[1] = -1;
if (current->next != NULL) if (current->next != NULL)
{ {
if (pipe(fds) == -1) if (pipe(fds) == -1)
@ -112,7 +110,9 @@ int ft_cmds_executor(t_data *data)
ft_add_fd(content->fd_out, fds[1]); ft_add_fd(content->fd_out, fds[1]);
ft_add_fd(((t_cmd *)(current->next->content))->fd_in, fds[0]); ft_add_fd(((t_cmd *)(current->next->content))->fd_in, fds[0]);
} }
if (ft_cmd_executor(data, content, fds[0])) if (content->fd_in[0] == -2 || content->fd_out[0] == -2)
ft_mega_closer(content->fd_in, content->fd_out);
else if (ft_cmd_executor(data, content, fds[0]))
return (1); return (1);
current = current->next; current = current->next;
} }

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/28 15:45:53 by cchauvet #+# #+# */ /* Created: 2023/03/28 15:45:53 by cchauvet #+# #+# */
/* Updated: 2023/03/28 15:45:54 by cchauvet ### ########.fr */ /* Updated: 2023/03/29 18:55:22 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,7 +17,6 @@
# include "../cmd/cmd.h" # include "../cmd/cmd.h"
# include "../env/env.h" # include "../env/env.h"
# include "../builtins/builtins.h" # include "../builtins/builtins.h"
# include "../utils/utils.h"
void ft_closer(int fds[2]);
void ft_add_fd(int fds[2], int fd);
#endif #endif

View File

@ -6,16 +6,30 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 13:35:50 by cchauvet #+# #+# */ /* Created: 2023/02/15 13:35:50 by cchauvet #+# #+# */
/* Updated: 2023/02/16 16:26:15 by cchauvet ### ########.fr */ /* Updated: 2023/03/29 16:53:15 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "./format_private.h" #include "./format_private.h"
static int ft_replace(char **str, size_t i)
{
char *temp;
temp = ft_strreplace(*str, " ", i, i);
free(*str);
if (temp == NULL)
{
ft_eprintf("minishell: malloc failed\n");
return (1);
}
*str = temp;
return (0);
}
static char *ft_spacer_after(const char *str) static char *ft_spacer_after(const char *str)
{ {
char *out; char *out;
char *temp;
size_t i; size_t i;
out = ft_strdup(str); out = ft_strdup(str);
@ -32,10 +46,7 @@ static char *ft_spacer_after(const char *str)
{ {
while (out[i] == out[i - 1]) while (out[i] == out[i - 1])
i++; i++;
temp = ft_strreplace(out, " ", i, i); if (ft_replace(&out, i))
free(out);
out = temp;
if (out == NULL)
return (NULL); return (NULL);
} }
if (out[i] != '\0') if (out[i] != '\0')
@ -47,14 +58,13 @@ static char *ft_spacer_after(const char *str)
static char *ft_spacer_before(const char *str) static char *ft_spacer_before(const char *str)
{ {
char *out; char *out;
char *temp; ssize_t i;
size_t i;
out = ft_strdup(str); out = ft_strdup(str);
if (out == NULL) if (out == NULL)
return (NULL); return (NULL);
i = 0; i = -1;
while (out[i + 1] != '\0') while (out[++i] != '\0')
{ {
while (ft_is_in_quote(out, i)) while (ft_is_in_quote(out, i))
i++; i++;
@ -66,13 +76,9 @@ static char *ft_spacer_before(const char *str)
i++; i++;
while (out[i] == out[i + 1]) while (out[i] == out[i + 1])
i++; i++;
temp = ft_strreplace(out, " ", i + 1, i + 1); if (ft_replace(&out, i + 1))
free(out);
out = temp;
if (out == NULL)
return (NULL); return (NULL);
} }
i++;
} }
return (out); return (out);
} }

75
main.c
View File

@ -6,19 +6,14 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */ /* Created: 2023/02/16 15:16:14 by cchauvet #+# #+# */
/* Updated: 2023/03/28 15:39:53 by cchauvet ### ########.fr */ /* Updated: 2023/03/29 18:48:50 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "data/data.h" #include "data/data.h"
#include "execution/execution.h" #include "env/env.h"
#include "libftx/libft/libft.h"
#include "libftx/libft/list.h"
#include "libftx/libftx.h" #include "libftx/libftx.h"
#include "minishell.h" #include "minishell.h"
#include <signal.h>
#include <stdlib.h>
#include <sys/wait.h>
static char *ft_get_user_input(void) static char *ft_get_user_input(void)
{ {
@ -55,7 +50,8 @@ static void ft_cmds_waiter(t_data *data)
while (current != NULL) while (current != NULL)
{ {
content = current->content; content = current->content;
if (content->own_cmd == 0 && content->pid != -1) if (content->own_cmd == 0 && content->pid != -1
&& content->fd_in[0] != -2 && content->fd_out[0] != -2)
{ {
waitpid(content->pid, &exit_status, 0); waitpid(content->pid, &exit_status, 0);
if (WIFSIGNALED(exit_status)) if (WIFSIGNALED(exit_status))
@ -95,53 +91,25 @@ static int ft_minishell(t_data *data, char *line)
return (0); return (0);
} }
void ft_ctrlc(int num) int ft_init_data(t_data *data, char **env)
{
t_data *data;
if (num == SIGQUIT)
return ;
data = ft_get_data();
data->exit_code = 130;
if (*ft_get_heredoc() != -1)
{
close(*ft_get_heredoc());
*ft_get_heredoc() = -1;
}
else
{
if (data->child_pid > 1)
{ {
data->exit_code = 0;
data->child_pid = 0; data->child_pid = 0;
ft_putchar_fd('\n', 1); data->cmds = malloc(sizeof(t_cmd *));
} if (data->cmds == NULL)
else
{ {
rl_replace_line("", 0); ft_eprintf("minishell: malloc failed\n");
rl_on_new_line(); return (1);
ft_putchar_fd('\n', 1);
rl_redisplay();
} }
} *data->cmds = NULL;
} data->env = init_env(env);
if (data->env == NULL)
void ft_quit(int num)
{ {
t_data *data; ft_eprintf("minishell: malloc failed\n");
free(data->cmds);
(void) num; return (1);
data = ft_get_data();
data->exit_code = 131;
if (data->child_pid > 1)
{
ft_printf("Quit (core dumped)\n");
data->child_pid = 0;
}
else
{
rl_replace_line("", 0);
rl_redisplay();
} }
return (0);
} }
int main(int ac, char **av, char **env) int main(int ac, char **av, char **env)
@ -154,14 +122,7 @@ int main(int ac, char **av, char **env)
signal(SIGINT, ft_ctrlc); signal(SIGINT, ft_ctrlc);
signal(SIGQUIT, ft_quit); signal(SIGQUIT, ft_quit);
data = ft_get_data(); data = ft_get_data();
data->exit_code = 0; if (ft_init_data(data, env))
data->child_pid = 0;
data->cmds = malloc(sizeof(t_cmd *));
if (data->cmds == NULL)
return (1);
*data->cmds = NULL;
data->env = init_env(env);
if (data->env == NULL)
return (1); return (1);
line = ft_get_user_input(); line = ft_get_user_input();
while (line != NULL) while (line != NULL)

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */ /* Created: 2023/02/14 13:45:30 by cchauvet #+# #+# */
/* Updated: 2023/03/09 14:59:49 by erey-bet ### ########.fr */ /* Updated: 2023/03/28 16:03:54 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,6 +23,7 @@
# include "./libftx/libftx.h" # include "./libftx/libftx.h"
# include "./utils/utils.h" # include "./utils/utils.h"
# include "./data/data.h" # include "./data/data.h"
# include "./signal/signal.h"
# include <readline/readline.h> # include <readline/readline.h>
# include <readline/history.h> # include <readline/history.h>
# include <signal.h> # include <signal.h>

67
redirection/check.c Normal file
View File

@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/29 17:32:06 by cchauvet #+# #+# */
/* Updated: 2023/03/29 18:43:20 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "./redirection_private.h"
static void ft_check_heredoc(t_data *data, t_cmd *cmd,
char *redirection_identifier, char *redirection)
{
if (ft_strcmp(redirection_identifier, "<<") == 0)
cmd->fd_in[0] = ft_heredoc(data, redirection);
}
static void ft_check_infile(t_data *data, t_cmd *cmd,
char *redirection_identifier, char *redirection)
{
if (ft_strcmp(redirection_identifier, "<") == 0)
{
if (ft_file_is_readable(data, redirection))
cmd->fd_in[0] = open(redirection, O_RDONLY);
else
cmd->fd_in[0] = -2;
}
}
static void ft_check_outfile(t_data *data, t_cmd *cmd,
char *redirection_identifier, char *redirection)
{
if (ft_strcmp(redirection_identifier, ">") == 0)
{
if (ft_file_is_writable(data, redirection))
cmd->fd_out[0] = open(redirection,
O_WRONLY | O_TRUNC | O_CREAT, 0644);
else
cmd->fd_out[0] = -2;
}
}
static void ft_check_outfile_append(t_data *data, t_cmd *cmd,
char *redirection_identifier, char *redirection)
{
if (ft_strcmp(redirection_identifier, ">>") == 0)
{
if (ft_file_is_writable(data, redirection))
cmd->fd_out[0] = open(redirection,
O_WRONLY | O_APPEND | O_CREAT, 0644);
else
cmd->fd_out[0] = -2;
}
}
void ft_check_redirection(t_data *data, t_cmd *cmd,
char *redirection_identifier, char *redirection)
{
ft_check_heredoc(data, cmd, redirection_identifier, redirection);
ft_check_infile(data, cmd, redirection_identifier, redirection);
ft_check_outfile(data, cmd, redirection_identifier, redirection);
ft_check_outfile_append(data, cmd, redirection_identifier, redirection);
}

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 15:36:26 by cchauvet #+# #+# */ /* Created: 2023/02/17 15:36:26 by cchauvet #+# #+# */
/* Updated: 2023/03/28 15:47:25 by cchauvet ### ########.fr */ /* Updated: 2023/03/29 16:37:05 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,37 +19,61 @@ int *ft_get_heredoc(void)
return (&heredoc); return (&heredoc);
} }
int ft_heredoc(t_data *data, char *stop) static int ft_format_and_write(t_data *data, const char *str, int fd)
{ {
char *line;
char *line_clean; char *line_clean;
int fds[2];
if (pipe(fds) == -1) line_clean = ft_env_filler(data, str);
return (-2); if (line_clean == NULL)
*ft_get_heredoc() = dup(0); return (1);
ft_printf("> "); ft_putendl_fd(line_clean, fd);
line = get_next_line(*ft_get_heredoc()); free(line_clean);
while (line != NULL) return (0);
}
static int ft_heredoc2(t_data *data, char *stop, char *line, int fds[2])
{ {
line[ft_strlen(line) - 1] = '\0'; line[ft_strlen(line) - 1] = '\0';
if (ft_strcmp(line, stop) == 0) if (ft_strcmp(line, stop) == 0)
{ {
free(line); free(line);
break ; return (2);
}
if (ft_format_and_write(data, line, fds[1]))
{
ft_closer(fds);
free(line);
return (1);
} }
line_clean = ft_env_filler(data, line);
free(line); free(line);
ft_putendl_fd(line_clean, fds[1]);
free(line_clean);
ft_printf("> "); ft_printf("> ");
line = get_next_line(*ft_get_heredoc()); line = get_next_line(*ft_get_heredoc());
if (line == NULL && *ft_get_heredoc() == -1) if (line == NULL && *ft_get_heredoc() == -1)
{ {
close(fds[0]); ft_closer(fds);
close(fds[1]); return (1);
return (-2);
} }
return (0);
}
int ft_heredoc(t_data *data, char *stop)
{
char *line;
int fds[2];
int return_code;
if (pipe(fds) == -1)
return (-1);
*ft_get_heredoc() = dup(0);
ft_printf("> ");
line = get_next_line(*ft_get_heredoc());
while (line != NULL)
{
return_code = ft_heredoc2(data, stop, line, fds);
if (return_code == 2)
break ;
else if (return_code)
return (-2);
} }
close(fds[1]); close(fds[1]);
*ft_get_heredoc() = -1; *ft_get_heredoc() = -1;

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:44:22 by cchauvet #+# #+# */ /* Created: 2023/03/27 13:44:22 by cchauvet #+# #+# */
/* Updated: 2023/03/28 15:49:58 by cchauvet ### ########.fr */ /* Updated: 2023/03/29 17:58:57 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -35,11 +35,21 @@ int ft_replace_file(t_data *data, char **tab)
return (0); return (0);
} }
void ft_remove_redirection(char *cmd_str) static void ft_skip(char *str, size_t *i, ssize_t *start)
{
while (str[*i] == str[*start])
(*i)++;
while (str[*i] == ' ')
(*i)++;
while (str[*i] != '\0' && (str[*i] != ' '
|| ft_is_in_quote(str, *i)))
(*i)++;
}
static void ft_remove_redirection(char *cmd_str)
{ {
size_t i; size_t i;
ssize_t start; ssize_t start;
ssize_t stop;
i = 0; i = 0;
while (cmd_str[i] != '\0') while (cmd_str[i] != '\0')
@ -50,19 +60,12 @@ void ft_remove_redirection(char *cmd_str)
i++; i++;
if (ft_is_in("<>", cmd_str[i])) if (ft_is_in("<>", cmd_str[i]))
start = i; start = i;
if (start == -1) else
continue ; continue ;
while (cmd_str[i] == cmd_str[start]) ft_skip(cmd_str, &i, &start);
i++;
while (cmd_str[i] == ' ')
i++;
while (cmd_str[i] != '\0' && (cmd_str[i] != ' '
|| ft_is_in_quote(cmd_str, i)))
i++;
stop = i - start;
if (start != -1) if (start != -1)
{ {
ft_strshift(cmd_str + start, -1 * stop); ft_strshift(cmd_str + start, -1 * (i - start));
i = start; i = start;
} }
} }
@ -76,35 +79,8 @@ int ft_set_redirection(t_data *data, t_cmd *cmd, char **tab)
while (tab[i + 1] != NULL) while (tab[i + 1] != NULL)
{ {
ft_quote_remover(tab[i + 1]); ft_quote_remover(tab[i + 1]);
if (ft_strcmp(tab[i], "<<") == 0) if (ft_check_redirection(data, cmd, tab[i], tab[i + 1]) == 2)
{
cmd->fd_in[0] = ft_heredoc(data, tab[i + 1]);
if (cmd->fd_in[0] == -2)
return (1); return (1);
}
else if (ft_strcmp(tab[i], "<") == 0)
{
if (ft_file_is_readable(data, tab[i + 1]))
cmd->fd_in[0] = open(tab[i + 1], O_RDONLY);
else
return (1);
}
else if (ft_strcmp(tab[i], ">") == 0)
{
if (ft_file_is_writable(data, tab[i + 1]))
cmd->fd_out[0] = open(tab[i + 1],
O_WRONLY | O_TRUNC | O_CREAT, 0644);
else
return (1);
}
else if (ft_strcmp(tab[i], ">>") == 0)
{
if (ft_file_is_appendable(data, tab[i + 1]))
cmd->fd_out[0] = open(tab[i + 1],
O_WRONLY | O_APPEND | O_CREAT, 0644);
else
return (1);
}
i++; i++;
} }
return (0); return (0);

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:44:31 by cchauvet #+# #+# */ /* Created: 2023/03/27 13:44:31 by cchauvet #+# #+# */
/* Updated: 2023/03/28 15:44:38 by cchauvet ### ########.fr */ /* Updated: 2023/03/29 18:21:45 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,6 +24,8 @@
int ft_file_is_readable(t_data *data, const char *path); int ft_file_is_readable(t_data *data, const char *path);
int ft_file_is_writable(t_data *data, const char *path); int ft_file_is_writable(t_data *data, const char *path);
void ft_check_redirection(t_data *data, t_cmd *cmd,
char *redirection_identifier, char *redirection);
int ft_file_is_appendable(t_data *data, const char *path); int ft_file_is_appendable(t_data *data, const char *path);
int ft_heredoc(t_data *data, char *stop); int ft_heredoc(t_data *data, char *stop);

65
signal/signal.c Normal file
View File

@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* signal.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/28 15:59:01 by cchauvet #+# #+# */
/* Updated: 2023/03/28 16:09:37 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "signal_private.h"
static void ft_new_line(void)
{
rl_replace_line("", 0);
rl_on_new_line();
ft_putchar_fd('\n', 1);
rl_redisplay();
}
void ft_ctrlc(int num)
{
t_data *data;
if (num == SIGQUIT)
return ;
data = ft_get_data();
data->exit_code = 130;
if (*ft_get_heredoc() != -1)
{
close(*ft_get_heredoc());
*ft_get_heredoc() = -1;
}
else
{
if (data->child_pid > 1)
{
data->child_pid = 0;
ft_putchar_fd('\n', 1);
}
else
ft_new_line();
}
}
void ft_quit(int num)
{
t_data *data;
(void) num;
data = ft_get_data();
data->exit_code = 131;
if (data->child_pid > 1)
{
ft_printf("Quit (core dumped)\n");
data->child_pid = 0;
}
else
{
rl_replace_line("", 0);
rl_redisplay();
}
}

19
signal/signal.h Normal file
View File

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* signal.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/28 16:02:31 by cchauvet #+# #+# */
/* Updated: 2023/03/28 16:03:11 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SIGNAL_H
# define SIGNAL_H
void ft_quit(int num);
void ft_ctrlc(int num);
#endif

21
signal/signal_private.h Normal file
View File

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* signal_private.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/28 15:59:42 by cchauvet #+# #+# */
/* Updated: 2023/03/28 16:04:46 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SIGNAL_PRIVATE_H
# define SIGNAL_PRIVATE_H
# include <signal.h>
# include <readline/readline.h>
# include <unistd.h>
# include "../libftx/libftx.h"
# include "../data/data.h"
# include "../redirection/redirection.h"
#endif

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* syntatics.c :+: :+: :+: */ /* syntax.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 13:00:05 by cchauvet #+# #+# */ /* Created: 2023/02/21 13:00:05 by cchauvet #+# #+# */
/* Updated: 2023/02/21 23:40:20 by cchauvet ### ########.fr */ /* Updated: 2023/03/28 16:37:29 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,16 +26,14 @@ static int ft_quote_verif(const char *str)
static int ft_pipe_is_alone(const char *str) static int ft_pipe_is_alone(const char *str)
{ {
size_t i; ssize_t i;
int check; int check;
check = 0; check = 0;
i = 0; i = -1;
while (str[i] != '\0') while (str[++i] != '\0')
{ {
while (str[i] == ' ') while (str[i] == ' ' || ft_is_in_quote(str, i))
i++;
while (ft_is_in_quote(str, i))
i++; i++;
if (str[i] == '\0') if (str[i] == '\0')
break ; break ;
@ -44,20 +42,13 @@ static int ft_pipe_is_alone(const char *str)
else else
{ {
if (check == 0) if (check == 0)
{ break ;
ft_eprintf("minishell: Pipe must be followed and ");
ft_eprintf("preced by a command or redirection\n");
return (1);
}
check = 0; check = 0;
} }
i++;
} }
if (check == 0) if (check == 0)
{ ft_eprintf("minishell: Pipe must be followed and ",
ft_eprintf("minishell: Pipe must be followed and "); "preced by a command or redirection\n");
ft_eprintf("preced by a command or redirection\n");
}
return (check == 0); return (check == 0);
} }

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:39:58 by cchauvet #+# #+# */ /* Created: 2023/03/27 13:39:58 by cchauvet #+# #+# */
/* Updated: 2023/03/27 14:14:17 by cchauvet ### ########.fr */ /* Updated: 2023/03/29 18:51:58 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,6 +25,12 @@ void ft_closer(int fds[2])
} }
} }
void ft_mega_closer(int fds1[2], int fds2[2])
{
ft_closer(fds1);
ft_closer(fds2);
}
void ft_add_fd(int fds[2], int fd) void ft_add_fd(int fds[2], int fd)
{ {
if (fds[0] == -1) if (fds[0] == -1)

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/27 13:39:48 by cchauvet #+# #+# */ /* Created: 2023/03/27 13:39:48 by cchauvet #+# #+# */
/* Updated: 2023/03/27 13:39:50 by cchauvet ### ########.fr */ /* Updated: 2023/03/28 16:34:37 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -59,6 +59,23 @@ static char **ft_get_paths(t_data *data, const char *name)
return (tab); return (tab);
} }
static char *ft_file_is_executable(const char *path, const char *name)
{
char *out;
out = ft_strmerger(3, path, "/", name);
if (out == NULL)
{
ft_eprintf("minishell: malloc failed\n");
free(out);
return (NULL);
}
if (access(out, X_OK) == 0)
return (out);
free(out);
return (NULL);
}
static char *ft_get_executable_without_path(t_data *data, const char *name) static char *ft_get_executable_without_path(t_data *data, const char *name)
{ {
char **paths; char **paths;
@ -72,16 +89,9 @@ static char *ft_get_executable_without_path(t_data *data, const char *name)
i = 0; i = 0;
while (paths[i] != NULL) while (paths[i] != NULL)
{ {
path = ft_strmerger(3, paths[i], "/", name); path = ft_file_is_executable(paths[i], name);
if (path == NULL) if (path != NULL)
{
ft_eprintf("minishell: malloc failed\n");
break ; break ;
}
if (access(path, X_OK) == 0)
break ;
free(path);
path = NULL;
i++; i++;
} }
ft_freer_tab_ultimate(1, paths); ft_freer_tab_ultimate(1, paths);

View File

@ -6,12 +6,20 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/15 14:12:00 by cchauvet #+# #+# */ /* Created: 2023/02/15 14:12:00 by cchauvet #+# #+# */
/* Updated: 2023/02/21 14:33:28 by cchauvet ### ########.fr */ /* Updated: 2023/03/29 15:11:44 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "utils.h" #include "utils.h"
static void ft_remove_quote_and_reset(char *str, ssize_t *start, ssize_t *stop)
{
ft_strshift(str + *start, -1);
ft_strshift(str + *stop - 1, -1);
*start = -1;
*stop = -1;
}
char *ft_quote_remover(char *str) char *ft_quote_remover(char *str)
{ {
size_t i; size_t i;
@ -32,10 +40,7 @@ char *ft_quote_remover(char *str)
} }
if (stop != -1) if (stop != -1)
{ {
ft_strshift(str + start, -1); ft_remove_quote_and_reset(str, &start, &stop);
ft_strshift(str + stop - 1, -1);
start = -1;
stop = -1;
i = i - 1; i = i - 1;
} }
else else

View File

@ -6,7 +6,7 @@
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */ /* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/14 14:46:40 by cchauvet #+# #+# */ /* Created: 2023/02/14 14:46:40 by cchauvet #+# #+# */
/* Updated: 2023/02/16 16:13:47 by cchauvet ### ########.fr */ /* Updated: 2023/03/29 18:52:32 by cchauvet ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,6 +30,7 @@ char *ft_quote_remover(char *str);
int ft_atoi_check(const char *nptr); int ft_atoi_check(const char *nptr);
char *ft_get_executable(t_data *data, const char *name); char *ft_get_executable(t_data *data, const char *name);
void ft_closer(int fds[2]); void ft_closer(int fds[2]);
void ft_mega_closer(int fds1[2], int fds2[2]);
void ft_add_fd(int fds[2], int fd); void ft_add_fd(int fds[2], int fd);
#endif #endif