correction de quelques bugs sur le prompt - correction de segv sur env - correction de leaks

This commit is contained in:
gwojda 2017-02-16 12:46:42 +01:00
parent 81b4c83c70
commit 3dfc6abb61
9 changed files with 52 additions and 138 deletions

View file

@ -184,7 +184,6 @@ line-editing/control_c_and_d.c\
line-editing/copy_cut_paste.c\ line-editing/copy_cut_paste.c\
line-editing/ft_prompt.c\ line-editing/ft_prompt.c\
line-editing/get_key.c\ line-editing/get_key.c\
line-editing/heredoc.c\
line-editing/home_end.c\ line-editing/home_end.c\
line-editing/lib_line_editing/tool_line.c\ line-editing/lib_line_editing/tool_line.c\
line-editing/lib_line_editing/tool_line_2.c\ line-editing/lib_line_editing/tool_line_2.c\

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */ /* Created: 2017/01/23 10:35:44 by gwojda #+# #+# */
/* Updated: 2017/02/14 15:14:57 by gwojda ### ########.fr */ /* Updated: 2017/02/16 12:44:23 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -179,7 +179,7 @@ void ft_read_it(int input, size_t *pos, char **str);
char *readline(char *); char *readline(char *);
int ft_completion(int ret); int ft_completion(int ret);
void ft_check_heredoc(char **str); struct termios *ft_save_termios(int save);
char *ft_strdupi_w(char const *s); char *ft_strdupi_w(char const *s);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */ /* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */
/* Updated: 2017/02/15 15:45:54 by gwojda ### ########.fr */ /* Updated: 2017/02/16 12:40:08 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,6 +31,8 @@ void ft_sstr_freeone(char **sstr, int index)
{ {
char *tmp; char *tmp;
if (!sstr || !sstr[index])
return ;
tmp = sstr[index]; tmp = sstr[index];
while (sstr[index]) while (sstr[index])
{ {

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* builtin_exit.c :+: :+: :+: */ /* builtin_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:28:41 by jhalford #+# #+# */ /* Created: 2016/11/28 14:28:41 by jhalford #+# #+# */
/* Updated: 2017/02/02 15:04:48 by jhalford ### ########.fr */ /* Updated: 2017/02/16 12:44:44 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -36,6 +36,7 @@ int builtin_exit(const char *path, char *const av[], char *const envp[])
/* status = ft_atoi(ft_getenv(data_singleton()->env, "?")); */ /* status = ft_atoi(ft_getenv(data_singleton()->env, "?")); */
status = 0; status = 0;
} }
ft_save_termios(-1);
exit(status); exit(status);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/14 11:27:03 by gwojda #+# #+# */ /* Created: 2017/02/14 11:27:03 by gwojda #+# #+# */
/* Updated: 2017/02/14 14:57:01 by gwojda ### ########.fr */ /* Updated: 2017/02/16 12:37:35 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -15,13 +15,18 @@
void ft_add_in_history_file(char *str) void ft_add_in_history_file(char *str)
{ {
int fd; int fd;
char *home;
char *path;
fd = open(".42sh_history", if (!(home = ft_getenv(data_singleton()->env, "HOME")))
O_CREAT | O_WRONLY | O_APPEND, S_IWUSR | S_IRUSR); return ;
path = ft_str3join(home, "/", ".42sh_history");
fd = open(path, O_CREAT | O_WRONLY | O_APPEND, S_IWUSR | S_IRUSR);
if (fd == -1) if (fd == -1)
return ; return ;
write(fd, str, ft_strlen(str)); write(fd, str, ft_strlen(str));
write(fd, "\n", 1); write(fd, "\n", 1);
free(path);
close(fd); close(fd);
} }

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/25 11:39:47 by gwojda #+# #+# */ /* Created: 2017/01/25 11:39:47 by gwojda #+# #+# */
/* Updated: 2017/02/14 15:15:16 by gwojda ### ########.fr */ /* Updated: 2017/02/16 12:28:52 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -66,7 +66,6 @@ static void ft_history_parsing_2(void)
data_singleton()->line.input = ft_read_stdin(); data_singleton()->line.input = ft_read_stdin();
ft_putchar('\n'); ft_putchar('\n');
data_singleton()->line.opt = data_singleton()->line.opt | ~HIST; data_singleton()->line.opt = data_singleton()->line.opt | ~HIST;
ft_check_heredoc(&data_singleton()->line.input);
ft_history_parsing(); ft_history_parsing();
} }

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 13:51:33 by gwojda #+# #+# */ /* Created: 2016/12/13 13:51:33 by gwojda #+# #+# */
/* Updated: 2017/02/15 11:52:24 by gwojda ### ########.fr */ /* Updated: 2017/02/16 12:38:58 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -65,19 +65,27 @@ static int ft_git_status(void)
static int ft_currend_dir(void) static int ft_currend_dir(void)
{ {
int i;
int j;
char **env; char **env;
char *pwd;
char *currend_dir;
i = 0;
env = data_singleton()->env; env = data_singleton()->env;
while (ft_strncmp(env[i], "PWD=", 4)) if (!(pwd = ft_getenv(env, "PWD")))
++i; return (0);
j = ft_strlen(env[i]); if (ft_getenv(env, "HOME") && !ft_strcmp(pwd, ft_getenv(env, "HOME")))
while (j && env[i][j] != '/') {
--j; ft_printf("%c ", '~');
ft_printf("%s ", env[i] + j + 1); return (1);
return (ft_strlen(env[i] + j + 1)); }
if (!(currend_dir = ft_strrchr(pwd, '/')))
return (0);
if (!*(currend_dir + 1))
{
ft_printf("%c ", '/');
return (1);
}
ft_printf("%s ", currend_dir + 1);
return (ft_strlen(currend_dir + 1));
} }
void ft_prompt() void ft_prompt()
@ -94,8 +102,6 @@ void ft_prompt()
ft_putstr("\x1b[38;5;361m"); ft_putstr("\x1b[38;5;361m");
ret += ft_currend_dir(); ret += ft_currend_dir();
ret += ft_git_status(); ret += ft_git_status();
printf("\x1b[38;5;184m%C ", L'');
fflush(NULL);
ft_putstr("\033[22;37m"); ft_putstr("\033[22;37m");
data_singleton()->line.prompt_size = ret + 6; data_singleton()->line.prompt_size = ret + 4;
} }

View file

@ -1,109 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* heredoc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/24 15:52:34 by gwojda #+# #+# */
/* Updated: 2017/02/05 12:21:21 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *ft_get_next_word(char *str)
{
int j;
int k;
j = 0;
k = 0;
while (str[j] && (str[j] == ' ' || str[j] == '\n'))
++j;
while (str[j + k] && str[j + k] != ' ' && str[j + k] != '\n')
++k;
return (ft_strndup(str + j, k));
}
static void ft_after_retline(char *str, int *i)
{
while (str[*i] && str[*i] != '\n')
++(*i);
if (str[*i] == '\n')
++(*i);
}
static int ft_is_keyword(char *str, char *end)
{
int i;
int j;
i = 0;
while (str[i])
{
ft_after_retline(str, &i);
j = 0;
while (str[i + j] && str[i + j] != '\n')
++j;
if (str[i] && ft_strncmp(str + i, end, j) == 0)
return (0);
}
return (1);
}
static void ft_check_heredoc_2(char **str, char *end)
{
char boolean;
char *tmp;
char *tmp2;
boolean = 0;
while (!boolean)
{
ft_putstr("heredoc> ");
data_singleton()->line.prompt_size = 9;
tmp = *str;
*str = ft_strjoin(*str, "\n");
free(tmp);
tmp = *str;
data_singleton()->line.input = NULL;
data_singleton()->line.pos = 0;
tmp2 = ft_read_stdin();
if (!ft_strcmp(end, tmp2))
boolean = 1;
*str = ft_strjoin(tmp, tmp2);
free(tmp);
free(tmp2);
ft_putchar('\n');
}
}
void ft_check_heredoc(char **str)
{
int i;
char *end;
i = 0;
end = NULL;
if (!*str)
return ;
while ((*str)[i])
{
if ((*str)[i] == '<' && !ft_strncmp(*str + i, "<<", 2))
{
if ((*str)[i + 1] == '<' && (*str)[i + 2])
{
end = ft_get_next_word(*str + i + 2);
if (ft_is_keyword(*str, end))
ft_check_heredoc_2(str, end);
free(end);
}
else
return ;
}
++i;
}
if (!end)
return ;
}

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */
/* Updated: 2017/02/14 15:17:33 by gwojda ### ########.fr */ /* Updated: 2017/02/16 12:45:32 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -28,8 +28,13 @@ void ft_init_history(void)
{ {
int fd; int fd;
char *str; char *str;
char *home;
char *path;
fd = open(".42sh_history", O_RDONLY); if (!(home = ft_getenv(data_singleton()->env, "HOME")))
return ;
path = ft_str3join(home, "/", ".42sh_history");
fd = open(path, O_RDONLY);
if (fd == -1) if (fd == -1)
return ; return ;
while (get_next_line(fd, &str) > 0) while (get_next_line(fd, &str) > 0)
@ -38,6 +43,7 @@ void ft_init_history(void)
ft_create_history_list(str)); ft_create_history_list(str));
free(str); free(str);
} }
free(path);
free(str); free(str);
close(fd); close(fd);
} }
@ -46,7 +52,12 @@ struct termios *ft_save_termios(int save)
{ {
static struct termios *term_save = NULL; static struct termios *term_save = NULL;
if (save) if (save < 0)
{
free(term_save);
return (NULL);
}
if (save > 0)
{ {
term_save = (struct termios *)malloc(sizeof(struct termios)); term_save = (struct termios *)malloc(sizeof(struct termios));
tcgetattr(0, term_save); tcgetattr(0, term_save);
@ -73,9 +84,9 @@ void readline_init(char *prompt)
{ {
ft_init_line(); ft_init_line();
ft_init_history(); ft_init_history();
ft_save_termios(1);
beg = 1; beg = 1;
} }
ft_save_termios(1);
ft_init_termios(); ft_init_termios();
if (STR) if (STR)
ft_strdel(&STR); ft_strdel(&STR);