commit pour merge

This commit is contained in:
hugogogo
2021-12-17 12:00:32 +01:00
32 changed files with 621 additions and 565 deletions

View File

@@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* error_wrappers.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/01 17:16:30 by lperrey #+# #+# */
/* Updated: 2021/12/16 04:38:05 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int shell_error(char *s1, char *s2, char *s3, int ret_val)
{
char *prefix;
prefix = "minishell: ";
write(STDERR_FILENO, prefix, ft_strlen(prefix));
if (s1)
write(STDERR_FILENO, s1, ft_strlen(s1));
if (s2)
write(STDERR_FILENO, s2, ft_strlen(s2));
if (s3)
write(STDERR_FILENO, s3, ft_strlen(s3));
write(STDERR_FILENO, "\n", 1);
return (ret_val);
}
int shell_perror(char *s1, char *s2, char *s3, int ret_val)
{
char *prefix;
prefix = "minishell: ";
write(STDERR_FILENO, prefix, ft_strlen(prefix));
if (s1)
write(STDERR_FILENO, s1, ft_strlen(s1));
if (s2)
write(STDERR_FILENO, s2, ft_strlen(s2));
if (s3)
write(STDERR_FILENO, s3, ft_strlen(s3));
perror(NULL);
return (ret_val);
}
int ft_reti_perror_io(int ret, char *err_str, char *io_file)
{
ft_putstr_fd(err_str, STDERR_FILENO);
perror(io_file);
return (ret);
}

81
srcs/misc/free.c Normal file
View File

@@ -0,0 +1,81 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/10 23:53:17 by lperrey #+# #+# */
/* Updated: 2021/12/16 06:35:03 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int exit_free(t_all *c, int exit_status)
{
free(c->prompt_base);
free(c->prompt);
ft_lstclear((t_list **)&c->token_list, free);
ft_free_2d_arr(environ);
ft_free_2d_arr(c->path);
free_pipeline(&c->pipeline);
rl_clear_history();
close_stdio();
exit(exit_status);
}
void free_pipeline(t_cmd **pipeline_ptr[])
{
int i;
t_cmd **pipeline;
pipeline = *pipeline_ptr;
if (!pipeline)
return ;
close_pipeline_fd(pipeline);
i = 0;
while (pipeline[i])
{
ft_free_2d_arr(pipeline[i]->argv);
free(pipeline[i]->path);
i++;
}
ft_free_2d_arr(pipeline);
*pipeline_ptr = NULL;
}
void close_pipeline_fd(t_cmd *pipeline[])
{
int i;
if (!pipeline)
return ;
i = 0;
while (pipeline[i])
{
if (pipeline[i]->fd_in != STDIN_FILENO && pipeline[i]->fd_in > 0)
{
if (close(pipeline[i]->fd_in) == -1)
perror("close()");
pipeline[i]->fd_in = 0;
}
if (pipeline[i]->fd_out != STDOUT_FILENO && pipeline[i]->fd_out > 0)
{
if (close(pipeline[i]->fd_out) == -1)
perror("close()");
pipeline[i]->fd_out = 0;
}
i++;
}
}
void close_stdio(void)
{
if (close(STDIN_FILENO) == -1)
perror("close()");
if (close(STDOUT_FILENO) == -1)
perror("close()");
if (close(STDERR_FILENO) == -1)
perror("close()");
}

117
srcs/misc/init.c Normal file
View File

@@ -0,0 +1,117 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/12/10 10:28:16 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static char *init_prompt_base(void);
static int init_readline_hook(void);
static int shlvl_init(void);
int init(t_all *c)
{
ft_bzero(c, sizeof (*c));
// rl_instream = stdin;
rl_outstream = stderr;
// rl_outstream = stdout;
rl_startup_hook = init_readline_hook;
readline(NULL);
rl_startup_hook = NULL;
environ = ft_dup_2d_arr(environ, (t_dup_f)ft_strdup);
if (!environ)
return (ft_reti_perror(0, "ft_dup_2d_arr(environ)"));
c->path = retrieve_path();
if (!shlvl_init())
return (ft_reti_perror(0, "shlvl_init()"));
c->prompt_base = init_prompt_base();
if (!c->prompt_base)
return (ft_reti_perror(0, "init_prompt_base()"));
c->prompt = init_prompt(c->prompt_base);
if (!c->prompt)
return (ft_reti_perror(0, "init_prompt()"));
set_signals_behaviour();
return (1);
}
static int shlvl_init(void)
{
char *tmp;
int ret;
tmp = getenv("SHLVL");
if (tmp && ft_isdigit_str(tmp))
{
tmp = ft_itoa(ft_atoi(tmp) + 1);
if (!tmp)
return (0);
tmp = ft_strjoinfree_s2("SHLVL=", tmp);
if (!tmp)
return (0);
ret = export_var(tmp);
free(tmp);
}
else
ret = export_var("SHLVL=1");
if (ret == -1)
return (0);
return (1);
}
static char *init_prompt_base(void)
{
char *prompt_base;
char *tmp;
tmp = getenv("USER");
if (!tmp)
tmp = getenv("LOGNAME");
if (!tmp)
tmp = U_DEFAULT_USER;
prompt_base = ft_strjoin(TERM_LIGHT_GREEN, tmp);
if (!prompt_base)
return (NULL);
prompt_base = ft_strjoinfree_s1(prompt_base, "@");
if (!prompt_base)
return (NULL);
tmp = getenv("NAME");
if (!tmp)
tmp = U_DEFAULT_NAME;
prompt_base = ft_strjoinfree_s1(prompt_base, tmp);
if (!prompt_base)
return (NULL);
prompt_base = ft_strjoinfree_s1(prompt_base, TERM_RESET":"TERM_LIGHT_BLUE);
if (!prompt_base)
return (NULL);
return (prompt_base);
}
char *init_prompt(char *prompt_base)
{
char *prompt;
prompt = ft_strjoinfree_s2(prompt_base, getcwd(NULL, 0));
if (!prompt)
return (NULL);
prompt = ft_strjoinfree_s1(prompt, TERM_RESET U_PROMPT_END);
if (!prompt)
return (NULL);
return (prompt);
}
/*
** set rl_startup_hook with this, for init COLUMNS and LINES variables
** and prevent leak/double_free with **environ
*/
static int init_readline_hook(void)
{
rl_done = 1;
return (0);
}

View File

@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* last_exit_status.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/26 19:02:27 by lperrey #+# #+# */
/* Updated: 2021/11/26 20:48:11 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int set_last_exit_status(int new_value)
{
static int last_exit_status = 0;
if (new_value >= 0)
last_exit_status = new_value;
return (last_exit_status);
}
int get_last_exit_status(void)
{
return (set_last_exit_status(-1));
}
/* void ALT_set_last_exit_status(int new_value, int *set_last_exit_status_ptr)
{
static int *last_exit_status_ptr = NULL;
if (set_last_exit_status_ptr)
last_exit_status_ptr = set_last_exit_status_ptr;
else if (new_value >= 0)
*last_exit_status_ptr = new_value;
} */

27
srcs/misc/retrieve_path.c Normal file
View File

@@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* retrieve_path.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/12/08 06:27:23 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char **retrieve_path(void)
{
char *path;
char **path_split;
path = getenv("PATH");
if (!path)
return (ft_retp_print(NULL, "minishell: Warning, $PATH not set\n", 2));
path_split = ft_split(path, ':');
if (!path_split)
return (ft_retp_perror(NULL, "retrieve_path()"));
return (path_split);
}

38
srcs/misc/signals.c Normal file
View File

@@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* signals.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/23 18:56:53 by lperrey #+# #+# */
/* Updated: 2021/12/16 15:06:50 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void sigint_handler_interactive(int signum)
{
write(1, "\n", 1);
rl_on_new_line();
rl_replace_line("", 1);
rl_redisplay();
set_last_exit_status(EXIT_SIGNAL + signum);
}
void sigint_handler_heredoc(int signum)
{
(void)signum;
rl_done = 1;
g_switch_heredoc_sigint = 1;
}
void set_signals_behaviour(void)
{
struct sigaction signal_behaviour;
ft_bzero(&signal_behaviour, sizeof signal_behaviour);
signal_behaviour.sa_handler = SIG_IGN;
sigaction(SIGQUIT, &signal_behaviour, NULL);
}