diff --git a/42sh/Makefile b/42sh/Makefile index 9fe8f1ca..73e636c3 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -126,6 +126,8 @@ history/history_parsing_toolz.c\ history/history_parsing_toolz_2.c\ history/list_toolz.c\ history/surch_in_history.c\ +init_history.c\ +init_line.c\ job-control/builtin_bg.c\ job-control/builtin_fg.c\ job-control/builtin_jobs.c\ @@ -195,6 +197,7 @@ line-editing/copy_cut_paste.c\ line-editing/ft_prompt.c\ line-editing/get_key.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_2.c\ line-editing/lib_line_editing/toolz.c\ @@ -216,7 +219,6 @@ main/ft_putast2.c\ main/lib_expansion.c\ main/main.c\ main/remove_trailing_esc_nl.c\ -main/shell_exit.c\ main/shell_get_avdata.c\ main/shell_get_opts.c\ main/shell_init.c\ diff --git a/42sh/includes/ft_readline.h b/42sh/includes/ft_readline.h index 5945fdef..a81469fa 100644 --- a/42sh/includes/ft_readline.h +++ b/42sh/includes/ft_readline.h @@ -6,7 +6,7 @@ /* 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_init_history(void); char *ft_history_parsing(void); +struct termios *ft_save_termios(int save); +void ft_init_termios(void); +void readline_init(char *prompt); #endif diff --git a/42sh/src/init_history.c b/42sh/src/init_history.c new file mode 100644 index 00000000..c0be9d22 --- /dev/null +++ b/42sh/src/init_history.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* init_history.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/42sh/src/init_line.c b/42sh/src/init_line.c new file mode 100644 index 00000000..07862a71 --- /dev/null +++ b/42sh/src/init_line.c @@ -0,0 +1,44 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* init_line.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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(); +} diff --git a/42sh/src/line-editing/ft_prompt.c b/42sh/src/line-editing/ft_prompt.c index a9a87efa..515b5b59 100644 --- a/42sh/src/line-editing/ft_prompt.c +++ b/42sh/src/line-editing/ft_prompt.c @@ -6,20 +6,45 @@ /* 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" -static int ft_git_status(void) +static int promt_git_status(int fd) { - int pip[2]; int len; char *tmp; char *line; - pid_t soon; - char *exec[] = {"git", "status", "--porcelain", "--branch", NULL}; + + 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; pipe(pip); if ((soon = fork())) @@ -28,28 +53,7 @@ static int ft_git_status(void) if (WEXITSTATUS(soon)) return (-1); close(pip[1]); - get_next_line(pip[0], &line); - 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); + return (promt_git_status(pip[0])); } else { @@ -58,9 +62,7 @@ static int ft_git_status(void) close(pip[0]); execve("/usr/bin/git", exec, data_singleton()->env); } - len = ft_strlen(line); - ft_strdel(&line); - return (len + 8); + return (0); } static int ft_currend_dir(void) @@ -88,13 +90,14 @@ static int ft_currend_dir(void) return (ft_strlen(currend_dir + 1)); } -void ft_prompt() +void ft_prompt(void) { - int ret; + int ret; ret = 0; 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'➜'); else printf("\x1b[38;5;10m%C ", L'➜'); diff --git a/42sh/src/line-editing/init_termcaps.c b/42sh/src/line-editing/init_termcaps.c new file mode 100644 index 00000000..1e191485 --- /dev/null +++ b/42sh/src/line-editing/init_termcaps.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* init_termcaps.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: gwojda +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/42sh/src/line-editing/lib_line_editing/tool_line_2.c b/42sh/src/line-editing/lib_line_editing/tool_line_2.c index 55777404..1059fdd1 100644 --- a/42sh/src/line-editing/lib_line_editing/tool_line_2.c +++ b/42sh/src/line-editing/lib_line_editing/tool_line_2.c @@ -6,13 +6,13 @@ /* 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 */ /* */ /* ************************************************************************** */ #include "minishell.h" -void ft_clear_window(void) +void ft_clear_window(void) { pid_t soon_pid; char *tab_str[2]; diff --git a/42sh/src/line-editing/lib_line_editing/toolz2.c b/42sh/src/line-editing/lib_line_editing/toolz2.c index ea8836fe..d6cadfc2 100644 --- a/42sh/src/line-editing/lib_line_editing/toolz2.c +++ b/42sh/src/line-editing/lib_line_editing/toolz2.c @@ -6,7 +6,7 @@ /* 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 */ /* */ /* ************************************************************************** */ @@ -54,7 +54,7 @@ long long ft_pow(int nbr, int power) return (ret); } -char *ft_strdupi_w(char const *s) +char *ft_strdupi_w(char const *s) { int i; char *str; diff --git a/42sh/src/line-editing/lib_line_editing/toolz_termcaps.c b/42sh/src/line-editing/lib_line_editing/toolz_termcaps.c index 58e7d6ac..21d16e28 100644 --- a/42sh/src/line-editing/lib_line_editing/toolz_termcaps.c +++ b/42sh/src/line-editing/lib_line_editing/toolz_termcaps.c @@ -6,13 +6,13 @@ /* 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 */ /* */ /* ************************************************************************** */ #include "minishell.h" -int ft_put(int nb) +int ft_put(int nb) { write(1, &nb, 1); return (1); diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c index f4f4321f..f2501d7e 100644 --- a/42sh/src/line-editing/readline.c +++ b/42sh/src/line-editing/readline.c @@ -6,95 +6,12 @@ /* 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" -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 *input; diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 97ed8dde..61980e43 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* 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 non_interactive_shell(shell_get_avdata()); - builtin_exit(0); +// builtin_exit(0); return (0); } diff --git a/42sh/test b/42sh/test index 9054a36f..c5a4516a 100644 --- a/42sh/test +++ b/42sh/test @@ -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:[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:[-]