diff --git a/42sh/Makefile b/42sh/Makefile index 31d30f64..2c2e3fbe 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -184,7 +184,6 @@ line-editing/control_c_and_d.c\ line-editing/copy_cut_paste.c\ line-editing/ft_prompt.c\ line-editing/get_key.c\ -line-editing/heredoc.c\ line-editing/home_end.c\ line-editing/lib_line_editing/tool_line.c\ line-editing/lib_line_editing/tool_line_2.c\ diff --git a/42sh/includes/ft_readline.h b/42sh/includes/ft_readline.h index e2742ad1..06b7f449 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/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 *); 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); diff --git a/42sh/src/builtin/builtin_env.c b/42sh/src/builtin/builtin_env.c index 1a8c71b3..67d0c2bb 100644 --- a/42sh/src/builtin/builtin_env.c +++ b/42sh/src/builtin/builtin_env.c @@ -6,7 +6,7 @@ /* 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; + if (!sstr || !sstr[index]) + return ; tmp = sstr[index]; while (sstr[index]) { diff --git a/42sh/src/builtin/builtin_exit.c b/42sh/src/builtin/builtin_exit.c index 220886c9..42539ac2 100644 --- a/42sh/src/builtin/builtin_exit.c +++ b/42sh/src/builtin/builtin_exit.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* builtin_exit.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jhalford +#+ +:+ +#+ */ +/* 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 = 0; } + ft_save_termios(-1); exit(status); return (0); } diff --git a/42sh/src/history/add_str_in_history.c b/42sh/src/history/add_str_in_history.c index 91574ece..a0b37c4a 100644 --- a/42sh/src/history/add_str_in_history.c +++ b/42sh/src/history/add_str_in_history.c @@ -6,7 +6,7 @@ /* 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) { int fd; + char *home; + char *path; - fd = open(".42sh_history", - O_CREAT | O_WRONLY | O_APPEND, S_IWUSR | S_IRUSR); + if (!(home = ft_getenv(data_singleton()->env, "HOME"))) + return ; + path = ft_str3join(home, "/", ".42sh_history"); + fd = open(path, O_CREAT | O_WRONLY | O_APPEND, S_IWUSR | S_IRUSR); if (fd == -1) return ; write(fd, str, ft_strlen(str)); write(fd, "\n", 1); + free(path); close(fd); } diff --git a/42sh/src/history/history_parsing.c b/42sh/src/history/history_parsing.c index c96e94e8..d2ec5ae2 100644 --- a/42sh/src/history/history_parsing.c +++ b/42sh/src/history/history_parsing.c @@ -6,7 +6,7 @@ /* 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(); ft_putchar('\n'); data_singleton()->line.opt = data_singleton()->line.opt | ~HIST; - ft_check_heredoc(&data_singleton()->line.input); ft_history_parsing(); } diff --git a/42sh/src/line-editing/ft_prompt.c b/42sh/src/line-editing/ft_prompt.c index 264e9237..2302f512 100644 --- a/42sh/src/line-editing/ft_prompt.c +++ b/42sh/src/line-editing/ft_prompt.c @@ -6,7 +6,7 @@ /* 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) { - int i; - int j; char **env; + char *pwd; + char *currend_dir; - i = 0; env = data_singleton()->env; - while (ft_strncmp(env[i], "PWD=", 4)) - ++i; - j = ft_strlen(env[i]); - while (j && env[i][j] != '/') - --j; - ft_printf("%s ", env[i] + j + 1); - return (ft_strlen(env[i] + j + 1)); + if (!(pwd = ft_getenv(env, "PWD"))) + return (0); + if (ft_getenv(env, "HOME") && !ft_strcmp(pwd, ft_getenv(env, "HOME"))) + { + ft_printf("%c ", '~'); + return (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() @@ -94,8 +102,6 @@ void ft_prompt() ft_putstr("\x1b[38;5;361m"); ret += ft_currend_dir(); ret += ft_git_status(); - printf("\x1b[38;5;184m%C ", L'›'); - fflush(NULL); ft_putstr("\033[22;37m"); - data_singleton()->line.prompt_size = ret + 6; + data_singleton()->line.prompt_size = ret + 4; } diff --git a/42sh/src/line-editing/heredoc.c b/42sh/src/line-editing/heredoc.c deleted file mode 100644 index dac00a24..00000000 --- a/42sh/src/line-editing/heredoc.c +++ /dev/null @@ -1,109 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* heredoc.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: gwojda +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* 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 ; -} diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c index a37b90fa..5d9795ec 100644 --- a/42sh/src/line-editing/readline.c +++ b/42sh/src/line-editing/readline.c @@ -6,7 +6,7 @@ /* 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; 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) return ; while (get_next_line(fd, &str) > 0) @@ -38,6 +43,7 @@ void ft_init_history(void) ft_create_history_list(str)); free(str); } + free(path); free(str); close(fd); } @@ -46,7 +52,12 @@ struct termios *ft_save_termios(int save) { 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)); tcgetattr(0, term_save); @@ -73,9 +84,9 @@ void readline_init(char *prompt) { ft_init_line(); ft_init_history(); + ft_save_termios(1); beg = 1; } - ft_save_termios(1); ft_init_termios(); if (STR) ft_strdel(&STR);