ajout d'un fichier pour garder l'historique des utilisations precedentes (.42sh_history dans le dossier du 42sh) + gestion du resize de fenetre
This commit is contained in:
parent
f03692e762
commit
97ac417f81
3 changed files with 43 additions and 18 deletions
|
|
@ -6,13 +6,14 @@
|
||||||
/* 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/01 15:10:44 by gwojda ### ########.fr */
|
/* Updated: 2017/02/02 13:23:12 by gwojda ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#ifndef FT_READLINE_H
|
#ifndef FT_READLINE_H
|
||||||
# define FT_READLINE_H
|
# define FT_READLINE_H
|
||||||
|
|
||||||
|
# include <sys/ioctl.h>
|
||||||
# include <termios.h>
|
# include <termios.h>
|
||||||
# include <unistd.h>
|
# include <unistd.h>
|
||||||
# include <stdlib.h>
|
# include <stdlib.h>
|
||||||
|
|
@ -71,6 +72,7 @@ typedef struct s_line
|
||||||
t_list_history *list_beg;
|
t_list_history *list_beg;
|
||||||
} t_line;
|
} t_line;
|
||||||
|
|
||||||
|
void ft_add_in_history_file(char *str);
|
||||||
int builtin_history(const char *path, char *const av[], char *const envp[]);
|
int builtin_history(const char *path, char *const av[], char *const envp[]);
|
||||||
void ft_check_backslash(char **str);
|
void ft_check_backslash(char **str);
|
||||||
char *ft_strget_history(char *str);
|
char *ft_strget_history(char *str);
|
||||||
|
|
|
||||||
|
|
@ -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/02 10:41:26 by gwojda ### ########.fr */
|
/* Updated: 2017/02/02 13:34:47 by gwojda ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -22,6 +22,24 @@ void ft_init_line(void)
|
||||||
data_singleton()->line.opt = 0;
|
data_singleton()->line.opt = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ft_init_history(void)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
char *str;
|
||||||
|
|
||||||
|
fd = open(".42sh_history", O_RDONLY);
|
||||||
|
if (fd == -1)
|
||||||
|
return ;
|
||||||
|
while (get_next_line(fd, &str) > 0)
|
||||||
|
{
|
||||||
|
ft_push_back_history(&data_singleton()->line.list_beg,
|
||||||
|
ft_create_history_list(str));
|
||||||
|
free(str);
|
||||||
|
}
|
||||||
|
free(str);
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
struct termios *ft_save_stats_term(void)
|
struct termios *ft_save_stats_term(void)
|
||||||
{
|
{
|
||||||
static struct termios *term_save = NULL;
|
static struct termios *term_save = NULL;
|
||||||
|
|
@ -41,6 +59,7 @@ struct termios *ft_stats_term_termcaps(void)
|
||||||
if (!term)
|
if (!term)
|
||||||
{
|
{
|
||||||
ft_init_line();
|
ft_init_line();
|
||||||
|
ft_init_history();
|
||||||
term = (struct termios *)malloc(sizeof(struct termios));
|
term = (struct termios *)malloc(sizeof(struct termios));
|
||||||
tcgetattr(0, term);
|
tcgetattr(0, term);
|
||||||
(*term).c_lflag &= ~(ECHO | ICANON | ISIG);
|
(*term).c_lflag &= ~(ECHO | ICANON | ISIG);
|
||||||
|
|
@ -50,22 +69,8 @@ struct termios *ft_stats_term_termcaps(void)
|
||||||
return (term);
|
return (term);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ft_reset_stats_term(int signal)
|
|
||||||
{
|
|
||||||
char *name_term;
|
|
||||||
|
|
||||||
if (signal == SIGWINCH)
|
|
||||||
{
|
|
||||||
if ((name_term = getenv("TERM")) == NULL)
|
|
||||||
return ;
|
|
||||||
if (tgetent(NULL, name_term) == -1)
|
|
||||||
return ;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int ft_readline(void)
|
int ft_readline(void)
|
||||||
{
|
{
|
||||||
signal(SIGWINCH, ft_reset_stats_term);
|
|
||||||
ft_save_stats_term();
|
ft_save_stats_term();
|
||||||
if (tcsetattr(0, TCSANOW, ft_stats_term_termcaps()) == -1)
|
if (tcsetattr(0, TCSANOW, ft_stats_term_termcaps()) == -1)
|
||||||
return (-1);
|
return (-1);
|
||||||
|
|
@ -80,8 +85,11 @@ int ft_readline(void)
|
||||||
ft_check_backslash(&data_singleton()->line.input);
|
ft_check_backslash(&data_singleton()->line.input);
|
||||||
ft_history_parsing();
|
ft_history_parsing();
|
||||||
if (data_singleton()->line.input)
|
if (data_singleton()->line.input)
|
||||||
|
{
|
||||||
ft_push_back_history(&data_singleton()->line.list_beg,
|
ft_push_back_history(&data_singleton()->line.list_beg,
|
||||||
ft_create_history_list(data_singleton()->line.input));
|
ft_create_history_list(data_singleton()->line.input));
|
||||||
|
ft_add_in_history_file(data_singleton()->line.input);
|
||||||
|
}
|
||||||
if (tcsetattr(0, TCSANOW, ft_save_stats_term()) == -1)
|
if (tcsetattr(0, TCSANOW, ft_save_stats_term()) == -1)
|
||||||
return (-1);
|
return (-1);
|
||||||
return (0);
|
return (0);
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,24 @@
|
||||||
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/01/07 11:00:28 by gwojda #+# #+# */
|
/* Created: 2017/01/07 11:00:28 by gwojda #+# #+# */
|
||||||
/* Updated: 2017/01/24 15:00:16 by gwojda ### ########.fr */
|
/* Updated: 2017/02/02 13:31:57 by gwojda ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
|
void ft_add_in_history_file(char *str)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
fd = open(".42sh_history", O_CREAT | O_WRONLY | O_APPEND, S_IWUSR | S_IRUSR);
|
||||||
|
if (fd == -1)
|
||||||
|
return ;
|
||||||
|
write(fd, str, ft_strlen(str));
|
||||||
|
write(fd, "\n", 1);
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
int ft_nbr_len(int nbr)
|
int ft_nbr_len(int nbr)
|
||||||
{
|
{
|
||||||
if (nbr % 10 != nbr)
|
if (nbr % 10 != nbr)
|
||||||
|
|
@ -33,7 +45,10 @@ void ft_puttermcaps(char *str)
|
||||||
|
|
||||||
int ft_size_term(void)
|
int ft_size_term(void)
|
||||||
{
|
{
|
||||||
return (tgetnum("co"));
|
struct winsize w;
|
||||||
|
|
||||||
|
ioctl(0, TIOCGWINSZ, &w);
|
||||||
|
return (w.ws_col);
|
||||||
}
|
}
|
||||||
|
|
||||||
long long ft_pow(int nbr, int power)
|
long long ft_pow(int nbr, int power)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue