+ TODO : need refactoring and fix to valgrind invalid read size + redirections() WIP + Generic ft_dup_2d_arr(), ft_split_quotes(), ft_strdup_quotes() + shell_loop() continue on error + various small fix
70 lines
2.4 KiB
C
70 lines
2.4 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* init.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
|
|
/* Updated: 2021/11/13 05:31:47 by lperrey ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "minishell.h"
|
|
|
|
static char *init_prompt_base(void);
|
|
static char *init_prompt(char *prompt_base);
|
|
|
|
int init(t_all *c, char *envp[])
|
|
{
|
|
g_all = c;
|
|
ft_bzero(c, sizeof (*c));
|
|
c->envp = ft_dup_2d_arr(envp, (t_dup_func)ft_strdup); // TEST WIP
|
|
if (!c->envp)
|
|
return (ft_reti_perror(0, "ft_dup_2d_char_arr(envp) error"));
|
|
//print_matrix(c->envp, "\n --- \n"); // TEST WIP
|
|
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->ori_signal_behaviour, &c->signal_behaviour);
|
|
if (isatty(STDIN_FILENO))
|
|
{
|
|
if (!set_terminal_attributes(&c->ori_termios, &c->interactive_termios,
|
|
&c->termios_changed))
|
|
return (ft_reti_perror(0, "set_terminal_attributes() error"));
|
|
}
|
|
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);
|
|
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);
|
|
}
|
|
|
|
static char *init_prompt(char *prompt_base)
|
|
{
|
|
char *prompt;
|
|
|
|
prompt = ft_strjoinfree_s2(prompt_base, getcwd(NULL, 0));
|
|
prompt = ft_strjoinfree_s1(prompt, TERM_RESET U_PROMPT_END);
|
|
return (prompt);
|
|
}
|