Files
42_INT_07_minishell/srcs/init.c
2021-12-04 18:14:24 +01:00

91 lines
2.8 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
/* Updated: 2021/12/04 15:11:20 by lperrey ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static char *init_prompt_base(void);
static int init_readline_hook(void);
int init(t_all *c)
{
ft_bzero(c, sizeof (*c));
rl_outstream = stderr;
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) error"));
c->path = retrieve_path();
c->prompt_base = init_prompt_base();
if (!c->prompt_base)
return (ft_reti_perror(0, "init_prompt_base() error"));
c->prompt = init_prompt(c->prompt_base);
if (!c->prompt)
return (ft_reti_perror(0, "init_prompt() error"));
set_signals_handling(&c->signal_behaviour);
return (1);
}
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);
}
static char *init_prompt_base(void) // WIP, error return TODO
{
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);
prompt_base = ft_strjoinfree_s1(prompt_base, "@");
tmp = getenv("NAME");
if (!tmp)
tmp = U_DEFAULT_NAME;
prompt_base = ft_strjoinfree_s1(prompt_base, tmp);
prompt_base = ft_strjoinfree_s1(prompt_base, TERM_RESET":"TERM_LIGHT_BLUE);
return (prompt_base);
}
char *init_prompt(char *prompt_base) // WIP, error return TODO
{
char *prompt;
prompt = ft_strjoinfree_s2(prompt_base, getcwd(NULL, 0));
prompt = ft_strjoinfree_s1(prompt, TERM_RESET U_PROMPT_END);
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);
}