merge de luke

This commit is contained in:
hugogogo
2021-12-18 12:34:26 +01:00
10 changed files with 233 additions and 83 deletions

62
srcs/init/handle_argv.c Normal file
View File

@@ -0,0 +1,62 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_argv.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/12/18 04:46:05 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int remap_stdin_to_script_file(char *script_file);
char *init_prompt_base(void);
int handle_argv(t_all *c, char *argv[])
{
int ret;
if (argv[1])
{
ret = remap_stdin_to_script_file(argv[1]);
if (ret)
exit_free(c, ret);
}
else if (isatty(STDIN_FILENO))
{
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()"));
}
return (1);
}
int remap_stdin_to_script_file(char *script_file)
{
int fd;
int ret;
int tmp;
fd = open(script_file, O_RDONLY);
if (fd == -1)
{
shell_perror(script_file, ": ", "", 0);
return (EXIT_CMD_NOT_FOUND);
}
ret = dup2(fd, STDIN_FILENO);
tmp = errno;
if (close(fd) == -1)
perror("close()");
if (ret == -1)
{
errno = tmp;
return (ft_reti_perror(EXIT_FAILURE, "dup2()"));
}
return (0);
}

33
srcs/init/init.c Normal file
View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/12/18 04:53:11 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void init_readline(void);
int init_shlvl(void);
int handle_argv(t_all *c, char *argv[]);
int init(t_all *c, char *argv[])
{
ft_bzero(c, sizeof (*c));
init_readline();
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 (!init_shlvl())
return (ft_reti_perror(0, "init_shlvl()"));
if (!handle_argv(c, argv))
return (0);
set_signals_behaviour();
return (1);
}

54
srcs/init/init_prompt.c Normal file
View File

@@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_prompt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/12/18 12:33:50 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
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);
}

33
srcs/init/init_readline.c Normal file
View File

@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_readline.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/12/18 04:31:59 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int init_readline_hook(void);
void init_readline(void)
{
rl_outstream = stderr;
rl_startup_hook = init_readline_hook;
readline(NULL);
rl_startup_hook = NULL;
}
/*
** 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);
}

37
srcs/init/init_shlvl.c Normal file
View File

@@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_shlvl.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/12/18 04:35:33 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int init_shlvl(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);
}