mise a la norme edition de ligne

This commit is contained in:
gwojda 2017-03-07 17:37:32 +01:00
parent f70722a5ae
commit b4e7d8ab5e
12 changed files with 186 additions and 128 deletions

View file

@ -126,6 +126,8 @@ history/history_parsing_toolz.c\
history/history_parsing_toolz_2.c\ history/history_parsing_toolz_2.c\
history/list_toolz.c\ history/list_toolz.c\
history/surch_in_history.c\ history/surch_in_history.c\
init_history.c\
init_line.c\
job-control/builtin_bg.c\ job-control/builtin_bg.c\
job-control/builtin_fg.c\ job-control/builtin_fg.c\
job-control/builtin_jobs.c\ job-control/builtin_jobs.c\
@ -195,6 +197,7 @@ 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/home_end.c\ line-editing/home_end.c\
line-editing/init_termcaps.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\
line-editing/lib_line_editing/toolz.c\ line-editing/lib_line_editing/toolz.c\
@ -216,7 +219,6 @@ main/ft_putast2.c\
main/lib_expansion.c\ main/lib_expansion.c\
main/main.c\ main/main.c\
main/remove_trailing_esc_nl.c\ main/remove_trailing_esc_nl.c\
main/shell_exit.c\
main/shell_get_avdata.c\ main/shell_get_avdata.c\
main/shell_get_opts.c\ main/shell_get_opts.c\
main/shell_init.c\ main/shell_init.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/03/05 19:37:38 by gwojda ### ########.fr */ /* Updated: 2017/03/07 17:36:15 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -160,5 +160,8 @@ char *ft_strdupi_w(char const *s);
void ft_add_str_in_history(char *str); void ft_add_str_in_history(char *str);
void ft_init_history(void); void ft_init_history(void);
char *ft_history_parsing(void); char *ft_history_parsing(void);
struct termios *ft_save_termios(int save);
void ft_init_termios(void);
void readline_init(char *prompt);
#endif #endif

37
42sh/src/init_history.c Normal file
View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_history.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 17:34:23 by gwojda #+# #+# */
/* Updated: 2017/03/07 17:34:35 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void ft_init_history(void)
{
int fd;
char *str;
char *home;
char *path;
if (!(home = ft_getenv(data_singleton()->env, "HOME")))
return ;
path = ft_str3join(home, "/", ".42sh_history");
fd = open(path, 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(path);
free(str);
close(fd);
}

44
42sh/src/init_line.c Normal file
View file

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 17:34:44 by gwojda #+# #+# */
/* Updated: 2017/03/07 17:35:09 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void ft_init_line(void)
{
data_singleton()->line.input = NULL;
data_singleton()->line.copy_tmp = NULL;
data_singleton()->line.pos = 0;
data_singleton()->line.prompt_size = 0;
data_singleton()->line.list_size = 0;
data_singleton()->line.list_end = NULL;
data_singleton()->line.list_beg = NULL;
data_singleton()->line.opt = 0;
}
void readline_init(char *prompt)
{
static int beg = 0;
if (!beg)
{
ft_init_line();
ft_init_history();
ft_save_termios(1);
beg = 1;
}
ft_init_termios();
if (STR)
ft_strdel(&STR);
data_singleton()->line.list_cur = data_singleton()->line.list_beg;
POS = 0;
prompt ? ft_putstr(prompt) : ft_prompt();
}

View file

@ -6,20 +6,45 @@
/* 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/16 14:27:57 by gwojda ### ########.fr */ /* Updated: 2017/03/07 17:32:22 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
static int ft_git_status(void) static int promt_git_status(int fd)
{ {
int pip[2];
int len; int len;
char *tmp; char *tmp;
char *line; char *line;
get_next_line(fd, &line);
tmp = line;
if (ft_strrchr(line, '/'))
line = ft_strdup(ft_strrchr(line, '/') + 1);
else
line = ft_strdup(line + 3);
ft_printf("\x1b[38;5;47mgit:(\x1b[38;5;203m%s\x1b[38;5;47m)", line);
free(tmp);
if (!get_next_line(fd, &tmp))
printf("\x1b[38;5;83m %C ", L'');
else
{
printf("\x1b[38;5;1m %C ", L'');
while (get_next_line(fd, &tmp))
free(tmp);
}
len = ft_strlen(line);
ft_strdel(&line);
fflush(NULL);
return (len + 8);
}
static int ft_git_status(void)
{
static char *exec[] = {"git", "status", "--porcelain", "--branch", NULL};
int pip[2];
pid_t soon; pid_t soon;
char *exec[] = {"git", "status", "--porcelain", "--branch", NULL};
pipe(pip); pipe(pip);
if ((soon = fork())) if ((soon = fork()))
@ -28,28 +53,7 @@ static int ft_git_status(void)
if (WEXITSTATUS(soon)) if (WEXITSTATUS(soon))
return (-1); return (-1);
close(pip[1]); close(pip[1]);
get_next_line(pip[0], &line); return (promt_git_status(pip[0]));
tmp = line;
if (ft_strrchr(line, '/'))
{
line = ft_strdup(ft_strrchr(line, '/') + 1);
ft_printf("\x1b[38;5;47mgit:(\x1b[38;5;203m%s\x1b[38;5;47m)", line);
free(tmp);
}
else
{
line = ft_strdup(line + 3);
ft_printf("\x1b[38;5;47mgit:(\x1b[38;5;203m%s\x1b[38;5;47m)", line);
free(tmp);
}
if (!get_next_line(pip[0], &tmp))
printf("\x1b[38;5;83m %C ", L'');
else
{
printf("\x1b[38;5;1m %C ", L'');
free(tmp);
}
fflush(NULL);
} }
else else
{ {
@ -58,9 +62,7 @@ static int ft_git_status(void)
close(pip[0]); close(pip[0]);
execve("/usr/bin/git", exec, data_singleton()->env); execve("/usr/bin/git", exec, data_singleton()->env);
} }
len = ft_strlen(line); return (0);
ft_strdel(&line);
return (len + 8);
} }
static int ft_currend_dir(void) static int ft_currend_dir(void)
@ -88,13 +90,14 @@ static int ft_currend_dir(void)
return (ft_strlen(currend_dir + 1)); return (ft_strlen(currend_dir + 1));
} }
void ft_prompt() void ft_prompt(void)
{ {
int ret; int ret;
ret = 0; ret = 0;
do_job_notification(); do_job_notification();
if (ft_getenv(data_singleton()->env, "?") && ft_atoi(ft_getenv(data_singleton()->env, "?"))) if (ft_getenv(data_singleton()->env, "?") &&
ft_atoi(ft_getenv(data_singleton()->env, "?")))
printf("\x1b[38;5;1m%C ", L''); printf("\x1b[38;5;1m%C ", L'');
else else
printf("\x1b[38;5;10m%C ", L''); printf("\x1b[38;5;10m%C ", L'');

View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_termcaps.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 17:33:41 by gwojda #+# #+# */
/* Updated: 2017/03/07 17:36:38 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
struct termios *ft_save_termios(int save)
{
static struct termios *term_save = NULL;
if (save < 0)
{
free(term_save);
return (NULL);
}
if (save > 0)
{
term_save = (struct termios *)malloc(sizeof(struct termios));
tcgetattr(0, term_save);
}
return (term_save);
}
void ft_init_termios(void)
{
struct termios term;
tcgetattr(0, &term);
term.c_lflag &= ~(ECHO | ICANON | ISIG);
term.c_cc[VMIN] = 1;
term.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &term);
}

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/20 19:07:52 by gwojda #+# #+# */ /* Created: 2017/01/20 19:07:52 by gwojda #+# #+# */
/* Updated: 2017/03/05 19:36:44 by gwojda ### ########.fr */ /* Updated: 2017/03/07 17:32:58 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* 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/02/20 14:32:08 by gwojda ### ########.fr */ /* Updated: 2017/03/07 17:32:44 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/08 12:35:11 by gwojda #+# #+# */ /* Created: 2017/01/08 12:35:11 by gwojda #+# #+# */
/* Updated: 2017/02/28 11:11:26 by gwojda ### ########.fr */ /* Updated: 2017/03/07 17:33:05 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,95 +6,12 @@
/* 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/28 10:43:33 by gwojda ### ########.fr */ /* Updated: 2017/03/07 17:35:13 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void ft_init_line(void)
{
data_singleton()->line.input = NULL;
data_singleton()->line.copy_tmp = NULL;
data_singleton()->line.pos = 0;
data_singleton()->line.prompt_size = 0;
data_singleton()->line.list_size = 0;
data_singleton()->line.list_end = NULL;
data_singleton()->line.list_beg = NULL;
data_singleton()->line.opt = 0;
}
void ft_init_history(void)
{
int fd;
char *str;
char *home;
char *path;
if (!(home = ft_getenv(data_singleton()->env, "HOME")))
return ;
path = ft_str3join(home, "/", ".42sh_history");
fd = open(path, 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(path);
free(str);
close(fd);
}
struct termios *ft_save_termios(int save)
{
static struct termios *term_save = NULL;
if (save < 0)
{
free(term_save);
return (NULL);
}
if (save > 0)
{
term_save = (struct termios *)malloc(sizeof(struct termios));
tcgetattr(0, term_save);
}
return (term_save);
}
void ft_init_termios(void)
{
struct termios term;
tcgetattr(0, &term);
term.c_lflag &= ~(ECHO | ICANON | ISIG);
term.c_cc[VMIN] = 1;
term.c_cc[VTIME] = 0;
tcsetattr(0, TCSANOW, &term);
}
void readline_init(char *prompt)
{
static int beg = 0;
if (!beg)
{
ft_init_line();
ft_init_history();
ft_save_termios(1);
beg = 1;
}
ft_init_termios();
if (STR)
ft_strdel(&STR);
data_singleton()->line.list_cur = data_singleton()->line.list_beg;
POS = 0;
prompt ? ft_putstr(prompt) : ft_prompt();
}
char *readline(char *prompt) char *readline(char *prompt)
{ {
char *input; char *input;

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */
/* Updated: 2017/03/07 15:24:35 by jhalford ### ########.fr */ /* Updated: 2017/03/07 17:28:34 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -94,6 +94,6 @@ int main(int ac, char **av)
} }
else else
non_interactive_shell(shell_get_avdata()); non_interactive_shell(shell_get_avdata());
builtin_exit(0); // builtin_exit(0);
return (0); return (0);
} }

View file

@ -1,3 +1,14 @@
 shell_init.c 28 interactive shell settings  main.c 86 
 main.c 86 
 shell_init.c 27 interactive shell settings
 token_print.c 29 13:[ls]  token_print.c 29 13:[ls]
 token_print.c 29 13:[ls]  token_print.c 29 13:[ls]
 token_print.c 29 13:[ls]
 token_print.c 29 13:[ls]
 token_print.c 29 13:[cd]
 token_print.c 29 13:[cd]
 token_print.c 29 13:[-]
 token_print.c 29 13:[cd]
 token_print.c 29 13:[includes]
 token_print.c 29 13:[cd]
 token_print.c 29 13:[-]