diff --git a/42sh/Makefile b/42sh/Makefile index 83c7290c..b561d1da 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,7 +6,7 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/02/03 15:23:50 by gwojda ### ########.fr # +# Updated: 2017/02/04 12:47:31 by gwojda ### ########.fr # # # # **************************************************************************** # diff --git a/42sh/includes/ft_readline.h b/42sh/includes/ft_readline.h index 5a39f39c..da6f2f5c 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/03 16:36:20 by gwojda ### ########.fr */ +/* Updated: 2017/02/04 14:50:04 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -73,6 +73,9 @@ # define IS_BRACKET (1 << 4) # define IS_BSLASH (1 << 5) +# define STR data_singleton()->line.input +# define POS data_singleton()->line.pos + # define HIST 1 # define ERROR_CNTL_R 1 @@ -88,7 +91,6 @@ typedef struct s_line { char *input; char *copy_tmp; - char *input_tmp; size_t pos; int prompt_size; int list_size; diff --git a/42sh/src/line-editing/check_backslash.c b/42sh/src/line-editing/check_backslash.c index 12eb1976..a9ef3294 100644 --- a/42sh/src/line-editing/check_backslash.c +++ b/42sh/src/line-editing/check_backslash.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/26 13:32:52 by gwojda #+# #+# */ -/* Updated: 2017/02/03 15:27:37 by gwojda ### ########.fr */ +/* Updated: 2017/02/04 14:51:44 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,13 +14,13 @@ t_prompt_type g_prompt_tab[] = { - {IS_QUOTES ,SIZE_PROMPT_QUOTES ,PROMPT_QUOTES}, - {IS_BQUOTES ,SIZE_PROMPT_BQUOTES ,PROMPT_BQUOTES}, - {IS_DQUOTES ,SIZE_PROMPT_DQUOTES ,PROMPT_DQUOTES}, - {IS_ACCOLADE ,SIZE_PROMPT_ACCOLADE ,PROMPT_ACCOLADE}, - {IS_BRACKET ,SIZE_PROMPT_BRACKET ,PROMPT_BRACKET}, - {IS_BSLASH ,SIZE_PROMPT_BSLASH ,PROMPT_BSLASH}, - {0 ,0 ,0}, + {IS_QUOTES, SIZE_PROMPT_QUOTES, PROMPT_QUOTES}, + {IS_BQUOTES, SIZE_PROMPT_BQUOTES, PROMPT_BQUOTES}, + {IS_DQUOTES, SIZE_PROMPT_DQUOTES, PROMPT_DQUOTES}, + {IS_ACCOLADE, SIZE_PROMPT_ACCOLADE, PROMPT_ACCOLADE}, + {IS_BRACKET, SIZE_PROMPT_BRACKET, PROMPT_BRACKET}, + {IS_BSLASH, SIZE_PROMPT_BSLASH, PROMPT_BSLASH}, + {0, 0, 0}, }; void ft_read_more(short c) @@ -50,9 +50,10 @@ void ft_read_more(short c) void ft_check_this_char(char c, short *status) { int i; - char stats[] = {'\'', '`', '\"', '{', '(', '\\', '\0'}; + char *stats; i = 0; + stats = "\'`\"{(\\"; while (stats[i] && stats[i] != c) ++i; if (!stats[i]) diff --git a/42sh/src/line-editing/copy_cut_paste.c b/42sh/src/line-editing/copy_cut_paste.c index 587469bb..df50ef51 100644 --- a/42sh/src/line-editing/copy_cut_paste.c +++ b/42sh/src/line-editing/copy_cut_paste.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/19 12:45:06 by gwojda #+# #+# */ -/* Updated: 2017/02/02 16:10:51 by gwojda ### ########.fr */ +/* Updated: 2017/02/04 14:55:05 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,7 +36,7 @@ static char *ft_strdupi_space(char const *s) return (str); } -void ft_v(void) +void ft_v(void) { size_t tmp_pos; int i; @@ -45,17 +45,14 @@ void ft_v(void) size_t *pos; tmp = data_singleton()->line.copy_tmp; - str = &data_singleton()->line.input; - pos = &data_singleton()->line.pos; - i = 0; + str = &STR; + pos = &POS; + i = -1; tmp_pos = *pos; if (!*str || !tmp) return ; - while (tmp[i]) - { + while (tmp[++i]) *str = ft_realloc_imput(*str, tmp[i], *pos + i); - ++i; - } if (*pos) { --(*pos); @@ -67,7 +64,7 @@ void ft_v(void) (*pos) = tmp_pos; } -void ft_x(void) +void ft_x(void) { int i; char **tmp; @@ -75,8 +72,8 @@ void ft_x(void) size_t *pos; tmp = &data_singleton()->line.copy_tmp; - str = &data_singleton()->line.input; - pos = &data_singleton()->line.pos; + str = &STR; + pos = &POS; if (!*str) return ; if (*tmp) @@ -98,8 +95,8 @@ void ft_c(void) size_t *pos; tmp = data_singleton()->line.copy_tmp; - str = &data_singleton()->line.input; - pos = &data_singleton()->line.pos; + str = &STR; + pos = &POS; if (tmp) ft_strdel(&tmp); tmp = ft_strdupi_space((*str) + (*pos)); diff --git a/42sh/src/line-editing/get_touch.c b/42sh/src/line-editing/get_touch.c index 77f5989b..e75a4688 100644 --- a/42sh/src/line-editing/get_touch.c +++ b/42sh/src/line-editing/get_touch.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/19 16:28:49 by gwojda #+# #+# */ -/* Updated: 2017/02/03 18:00:09 by gwojda ### ########.fr */ +/* Updated: 2017/02/04 15:39:49 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ t_key g_key[] = {TOUCHE_DELETE , &ft_del }, {TOUCHE_CTRL_C , &ft_control_c }, {TOUCHE_CTRL_D , &ft_control_d }, - {TOUCHE_CTRL_R , &ft_history_parsing }, + {TOUCHE_CTRL_R , &ft_surch_in_history }, {TOUCHE_SUPPR , &ft_suppr }, {TOUCHE_HOME , &ft_home }, {TOUCHE_END , &ft_end }, @@ -37,18 +37,23 @@ t_key g_key[] = {0 , 0 }, }; -char *ft_read_stdin(void) +static void ft_is_str(void) +{ + if (STR) + { + ft_current_str(STR, POS); + ft_get_next_str(STR, &POS); + if (STR[POS]) + ++(POS); + } +} + +char *ft_read_stdin(void) { int ret; int j; - if (data_singleton()->line.input) - { - ft_current_str(data_singleton()->line.input, data_singleton()->line.pos); - ft_get_next_str(data_singleton()->line.input, &data_singleton()->line.pos); - if (data_singleton()->line.input[data_singleton()->line.pos]) - ++(data_singleton()->line.pos); - } + ft_is_str(); if (data_singleton()->comp) c_clear(data_singleton()); while (42) @@ -65,9 +70,8 @@ char *ft_read_stdin(void) else if (ft_isprint(ret)) ft_print(ret); else if (ret == 10) - return (data_singleton()->line.input); + return (STR); else if (ft_isascii(ret) == 0) - ft_read_it(ret, &data_singleton()->line.pos, - &data_singleton()->line.input); + ft_read_it(ret, &POS, &STR); } } diff --git a/42sh/src/line-editing/get_touch_toolz.c b/42sh/src/line-editing/get_touch_toolz.c index f99820dc..51ddefa4 100644 --- a/42sh/src/line-editing/get_touch_toolz.c +++ b/42sh/src/line-editing/get_touch_toolz.c @@ -6,35 +6,67 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/19 16:42:54 by gwojda #+# #+# */ -/* Updated: 2017/02/03 12:08:45 by gwojda ### ########.fr */ +/* Updated: 2017/02/04 15:17:19 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void ft_found_prev_word(void) +static void ft_init_prev_word(size_t *pos, char *str) { int i; - char *str; - size_t *pos; - str = data_singleton()->line.input; - pos = &data_singleton()->line.pos; i = 0; - if (!*pos) - return ; if (str[*pos - 1] != '\n' && !(str[*pos] == '\n' || str[*pos] == ' ')) { ft_puttermcaps("le"); --(*pos); } - while ((int)*pos - i - 1 > 0 && str[*pos - i - 1] == ' ') + while ((int)*pos - i - 1 > 0 && str[*pos - i - 1] == ' ') { ft_puttermcaps("le"); ++i; } (*pos) = (i && str[*pos - i] == '\n') ? *pos - i + 1 : *pos - i; +} + +static void ft_found_prev_word_2(int i, char *str, size_t *pos) +{ + if (*pos && !(str[*pos] == '\n' || str[*pos] == ' ')) + { + ft_puttermcaps("le"); + --(*pos); + } + while (*pos - i && (str[*pos - i] == '\n' || str[*pos - i] == ' ')) + { + ft_puttermcaps("le"); + ++i; + } + while (*pos - i && str[*pos - i] != '\n' && str[*pos - i] != ' ') + { + ft_puttermcaps("le"); + ++i; + } + if (str[*pos - i] == '\n' || str[*pos - i] == ' ') + { + ft_puttermcaps("nd"); + ++(*pos); + } + (*pos) -= i; +} + +void ft_found_prev_word(void) +{ + int i; + char *str; + size_t *pos; + i = 0; + str = STR; + pos = &POS; + if (!*pos || !str) + return ; + ft_init_prev_word(pos, str); if (*pos >= 1 && str[*pos - 1] == '\n') { ft_puttermcaps("cd"); @@ -46,40 +78,40 @@ void ft_found_prev_word(void) ++(*pos); } else - { - if (*pos && !(str[*pos] == '\n' || str[*pos] == ' ')) - { - ft_puttermcaps("le"); - --(*pos); - } - while (*pos - i && (str[*pos - i] == '\n' || str[*pos - i] == ' ')) - { - ft_puttermcaps("le"); - ++i; - } - while (*pos - i && str[*pos - i] != '\n' && str[*pos - i] != ' ') - { - ft_puttermcaps("le"); - ++i; - } - if (str[*pos - i] == '\n' || str[*pos - i] == ' ') - { - ft_puttermcaps("nd"); - ++(*pos); - } - (*pos) -= i; - } + ft_found_prev_word_2(i, str, pos); } -void ft_found_next_word(void) +static void ft_found_next_word_2(void) +{ + char *str; + size_t *pos; + + str = STR; + pos = &POS; + if (*pos) + { + --(*pos); + ft_get_beggin_with_curs(str, pos); + } + ft_puttermcaps("cd"); + ft_get_next_str(str, pos); + (*pos) += (*pos) ? 2 : 1; + ft_current_str(str, *pos); + ft_get_next_str(str, pos); + if (!str[*pos]) + --(*pos); + ft_get_beggin_with_curs(str, pos); +} + +void ft_found_next_word(void) { int i; char *str; size_t *pos; - str = data_singleton()->line.input; - pos = &data_singleton()->line.pos; i = 0; + str = STR; + pos = &POS; if (!str) return ; while (str[i + *pos] && str[i + *pos] == ' ') @@ -88,21 +120,7 @@ void ft_found_next_word(void) ++i; } if (str[*pos] == '\n') - { - if (*pos) - { - --(*pos); - ft_get_beggin_with_curs(str, pos); - } - ft_puttermcaps("cd"); - ft_get_next_str(str, pos); - (*pos) += (*pos) ? 2 : 1; - ft_current_str(str, *pos); - ft_get_next_str(str, pos); - if (!str[*pos]) - --(*pos); - ft_get_beggin_with_curs(str, pos); - } + ft_found_next_word_2(); else { while (str[i + *pos] && str[i + *pos] != '\n' && str[i + *pos] != ' ') diff --git a/42sh/src/line-editing/get_touch_toolz_2.c b/42sh/src/line-editing/get_touch_toolz_2.c index 05805aa2..045caba9 100644 --- a/42sh/src/line-editing/get_touch_toolz_2.c +++ b/42sh/src/line-editing/get_touch_toolz_2.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/19 16:43:58 by gwojda #+# #+# */ -/* Updated: 2017/02/02 17:54:27 by gwojda ### ########.fr */ +/* Updated: 2017/02/04 15:34:10 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,8 +23,8 @@ void ft_end(void) char *str; size_t *pos; - str = data_singleton()->line.input; - pos = &data_singleton()->line.pos; + str = STR; + pos = &POS; if (!str) return ; if (*pos) @@ -45,8 +45,8 @@ void ft_home(void) char *str; size_t *pos; - str = data_singleton()->line.input; - pos = &data_singleton()->line.pos; + str = STR; + pos = &POS; if (!str) return ; if (*pos) @@ -69,45 +69,40 @@ void ft_home(void) void ft_move_right(void) { size_t tmp; - char *str; size_t *pos; - str = data_singleton()->line.input; - pos = &data_singleton()->line.pos; - if (ft_strlen(str) <= *pos) + pos = &POS; + if (ft_strlen(STR) <= *pos) return ; - if (str[*pos] == '\n') + if (STR[*pos] == '\n') { if (*pos) { tmp = *pos - 1; - ft_get_beggin_with_curs(str, &tmp); + ft_get_beggin_with_curs(STR, &tmp); } ft_puttermcaps("cd"); ++(*pos); - ft_current_str(str, *pos); - ft_get_next_str(str, pos); - if (!str[*pos]) - --(*pos); - ft_get_beggin_with_curs(str, pos); + ft_current_str(STR, *pos); + ft_get_next_str(STR, pos); + (*pos) = (!STR[*pos]) ? *pos - 1 : *pos; + ft_get_beggin_with_curs(STR, pos); } else { - ft_putchar(str[*pos]); + ft_putchar(STR[*pos]); ++(*pos); } } void ft_move_left(void) { - char *str; size_t *pos; - str = data_singleton()->line.input; - pos = &data_singleton()->line.pos; + pos = &POS; if (!*pos) return ; - if (str[*pos - 1] == '\n') + if (STR[*pos - 1] == '\n') { if (*pos - 1 == 0) { @@ -117,9 +112,9 @@ void ft_move_left(void) } ft_puttermcaps("cd"); (*pos) -= 2; - ft_get_beggin(str, pos); - ft_current_str(str, *pos); - ft_get_next_str(str, pos); + ft_get_beggin(STR, pos); + ft_current_str(STR, *pos); + ft_get_next_str(STR, pos); ++(*pos); } else diff --git a/42sh/src/line-editing/history.c b/42sh/src/line-editing/history.c index cd587a51..b01157d3 100644 --- a/42sh/src/line-editing/history.c +++ b/42sh/src/line-editing/history.c @@ -6,23 +6,17 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/02 15:22:19 by gwojda #+# #+# */ -/* Updated: 2017/02/03 11:55:36 by gwojda ### ########.fr */ +/* Updated: 2017/02/04 15:20:49 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void ft_history_down(void) +static void ft_clear_before_history(char **str) { - t_list_history *head; - char **str; size_t *pos; - str = &data_singleton()->line.input; - pos = &data_singleton()->line.pos; - head = data_singleton()->line.list_cur; - if (!head) - return ; + pos = &POS; if (*str) { if (*pos) @@ -34,6 +28,20 @@ void ft_history_down(void) *pos = 0; ft_strdel(str); } +} + +void ft_history_down(void) +{ + t_list_history *head; + char **str; + size_t *pos; + + str = &STR; + pos = &POS; + head = data_singleton()->line.list_cur; + if (!head) + return ; + ft_clear_before_history(str); if (head->next) head = head->next; if (!head->str) @@ -55,22 +63,12 @@ void ft_history_up(void) char **str; size_t *pos; - str = &data_singleton()->line.input; - pos = &data_singleton()->line.pos; + str = &STR; + pos = &POS; head = data_singleton()->line.list_cur; if (!head) return ; - if (*str) - { - if (*pos) - { - --(*pos); - ft_get_beggin_with_curs(*str, pos); - } - ft_puttermcaps("cd"); - *pos = 0; - ft_strdel(str); - } + ft_clear_before_history(str); if (head->prev) head = head->prev; if (!head->str) diff --git a/42sh/src/line-editing/history_parsing.c b/42sh/src/line-editing/history_parsing.c index 4ec1464f..5e7fac2d 100644 --- a/42sh/src/line-editing/history_parsing.c +++ b/42sh/src/line-editing/history_parsing.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/25 11:39:47 by gwojda #+# #+# */ -/* Updated: 2017/02/03 17:57:21 by gwojda ### ########.fr */ +/* Updated: 2017/02/04 15:35:25 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ static int ft_history_parsing_4(char *str, int *i) { int tmp; + if (!ft_strncmp("!!", str + *i, 2)) { ft_realloc_str_history(&(data_singleton()->line.input), *i, 0, 2); @@ -35,6 +36,7 @@ static int ft_history_parsing_4(char *str, int *i) static int ft_history_parsing_3(char *str, int *i) { int tmp; + if (ft_history_parsing_4(str, i)) return (1); else if (str[*i + 1] == '-' && ft_isdigit(str[*i + 2])) diff --git a/42sh/src/line-editing/move_to_line.c b/42sh/src/line-editing/move_to_line.c index c8161cfb..975e3100 100644 --- a/42sh/src/line-editing/move_to_line.c +++ b/42sh/src/line-editing/move_to_line.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/09 13:21:40 by gwojda #+# #+# */ -/* Updated: 2017/02/02 15:07:08 by gwojda ### ########.fr */ +/* Updated: 2017/02/04 15:36:25 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,9 +38,9 @@ static void ft_up_2(size_t *pos, char *str) (*pos) -= i; } -void ft_up(void) +void ft_up(void) { - int i; + int i; char *str; size_t *pos; @@ -79,10 +79,10 @@ static void ft_down_2(size_t *pos, char *str) ft_get_beggin_with_curs(str, pos); } -void ft_down(void) +void ft_down(void) { - int i; - int len; + int i; + int len; char *str; size_t *pos; diff --git a/42sh/src/line-editing/prompt.c b/42sh/src/line-editing/prompt.c index f322c9a5..1bbe01fd 100644 --- a/42sh/src/line-editing/prompt.c +++ b/42sh/src/line-editing/prompt.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:51:33 by gwojda #+# #+# */ -/* Updated: 2017/02/03 15:52:53 by jhalford ### ########.fr */ +/* Updated: 2017/02/04 14:56:51 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,9 +55,6 @@ void ft_prompt(void) int ret; do_job_notification(); - // yo gwojda enlève pas ca, je fait des notif de - // job control juste avant que tu mette ton prompt. - ft_get_date(); ft_putstr("\033[22;36m"); ret = ft_currend_dir(); diff --git a/42sh/src/line-editing/reader.c b/42sh/src/line-editing/reader.c index 70e53052..684afd69 100644 --- a/42sh/src/line-editing/reader.c +++ b/42sh/src/line-editing/reader.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/19 16:52:57 by gwojda #+# #+# */ -/* Updated: 2017/02/03 18:24:47 by gwojda ### ########.fr */ +/* Updated: 2017/02/04 15:37:55 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,15 +57,15 @@ void ft_read_it(int input, size_t *pos, char **str) j = 0; pos_tmp = *pos; if (input == TOUCHE_DELETE || input < 0 || input == 892427035 || - input == 126 || input == 993090331 || input == 925981467 || - input == 21298 || input == 892427035 || input == 8270395 || - input == 942758683 || input == 993090331 || input == 18489 || input == 17977) + input == 126 || input == 993090331 || input == 925981467 || + input == 21298 || input == 892427035 || input == 8270395 || input == + 942758683 || input == 993090331 || input == 18489 || input == 17977) return ; ft_read_it_2(input, t); ft_read_it_3(str, t, pos, &j); *pos = pos_tmp; ft_current_str((*str), *pos); ft_get_next_str((*str), pos); - ft_putnc('\b', *pos - (pos_tmp + j) + 1); + ft_putnc('\b', *pos - (pos_tmp + j)); *pos = (pos_tmp + j); } diff --git a/42sh/src/line-editing/readline.c b/42sh/src/line-editing/readline.c index 61445543..efca1222 100644 --- a/42sh/src/line-editing/readline.c +++ b/42sh/src/line-editing/readline.c @@ -6,13 +6,13 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */ -/* Updated: 2017/02/03 18:36:18 by gwojda ### ########.fr */ +/* Updated: 2017/02/04 16:19:36 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void ft_init_line(void) +void ft_init_line(void) { data_singleton()->line.input = NULL; data_singleton()->line.copy_tmp = NULL; @@ -24,7 +24,7 @@ void ft_init_line(void) data_singleton()->line.opt = 0; } -void ft_init_history(void) +void ft_init_history(void) { int fd; char *str; @@ -71,27 +71,27 @@ struct termios *ft_stats_term_termcaps(void) return (term); } -int ft_readline(void) +int ft_readline(void) { ft_save_stats_term(); if (tcsetattr(0, TCSANOW, ft_stats_term_termcaps()) == -1) return (-1); - if (data_singleton()->line.input) - ft_strdel(&data_singleton()->line.input); + if (STR) + ft_strdel(&STR); data_singleton()->line.list_cur = data_singleton()->line.list_beg; - data_singleton()->line.pos = 0; + POS = 0; ft_prompt(); - data_singleton()->line.input = ft_read_stdin(); + STR = ft_read_stdin(); ft_check_line(); ft_putchar('\n'); - ft_check_heredoc(&data_singleton()->line.input); + ft_check_heredoc(&STR); ft_history_parsing(); - if (data_singleton()->line.input && (!data_singleton()->line.list_beg || - ft_strcmp(data_singleton()->line.list_beg->prev->str, data_singleton()->line.input))) + if (STR && (!data_singleton()->line.list_beg || + ft_strcmp(data_singleton()->line.list_beg->prev->str, STR))) { ft_push_back_history(&data_singleton()->line.list_beg, - ft_create_history_list(data_singleton()->line.input)); - ft_add_in_history_file(data_singleton()->line.input); + ft_create_history_list(STR)); + ft_add_in_history_file(STR); } if (tcsetattr(0, TCSANOW, ft_save_stats_term()) == -1) return (-1); diff --git a/42sh/src/line-editing/surch_in_history.c b/42sh/src/line-editing/surch_in_history.c index 424dc5d2..28360348 100644 --- a/42sh/src/line-editing/surch_in_history.c +++ b/42sh/src/line-editing/surch_in_history.c @@ -6,13 +6,13 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/26 10:43:16 by gwojda #+# #+# */ -/* Updated: 2017/02/02 15:16:42 by gwojda ### ########.fr */ +/* Updated: 2017/02/04 15:50:26 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void ft_clear_prompt(char *str, size_t *pos, size_t srch_pos) +static void ft_clear_prompt(char *str, size_t *pos, size_t srch_pos) { if (str) ft_get_beggin_with_curs(str, pos); @@ -25,63 +25,68 @@ void ft_clear_prompt(char *str, size_t *pos, size_t srch_pos) ft_puttermcaps("cd"); } -void ft_surch_and_realloc(char **str, char **str_srch, int ret, size_t *srch_pos) +static void ft_surch_and_realloc(char **str, char **str_srch, + int ret, size_t *srch_pos) { *str_srch = ft_realloc_imput(*str_srch, ret, *srch_pos); ++(*srch_pos); *str = ft_strget_history(*str_srch); } -void ft_surch_in_history(void) +static void ft_give_new_prompt(char *str_srch, size_t srch_pos) +{ + ft_clear_prompt(STR, &POS, srch_pos); + data_singleton()->line.prompt_size = 21; + if (str_srch) + ft_printf("\033[35m(reverse-i-search)`\033[32m%s\033[35m': \033[37m", + str_srch); + else + ft_putstr("\033[35m(reverse-i-search)`': \033[37m"); + if (STR) + { + ft_current_str(STR, POS); + ft_get_next_str(STR, &POS); + } +} + +static void ft_modify_str(char *str_srch, size_t srch_pos) +{ + ft_clear_prompt(STR, &POS, srch_pos); + ft_prompt(); + if (STR) + { + STR = ft_strdup(STR); + ft_current_str(STR, POS); + ft_get_next_str(STR, &POS); + } + free(str_srch); +} + +void ft_surch_in_history(void) { char *str_srch; int ret; size_t srch_pos; - char **str; - size_t *pos; - str = &data_singleton()->line.input; - pos = &data_singleton()->line.pos; srch_pos = 0; str_srch = NULL; - if (*str) - ft_strdel(str); + if (STR) + ft_strdel(&STR); while (42) { - ft_clear_prompt(*str, pos, srch_pos); - data_singleton()->line.prompt_size = 21; - if (str_srch) - ft_printf("\033[35m(reverse-i-search)`\033[32m%s\033[35m': \033[37m", str_srch); - else - ft_putstr("\033[35m(reverse-i-search)`': \033[37m"); - if (*str) - { - ft_current_str(*str, *pos); - ft_get_next_str(*str, pos); - } + ft_give_new_prompt(str_srch, srch_pos); ret = 0; read(0, &ret, sizeof(int)); if (ft_isprint(ret)) - ft_surch_and_realloc(str, &str_srch, ret, &srch_pos); - else if (ret == 127) + ft_surch_and_realloc(&STR, &str_srch, ret, &srch_pos); + else if (ret == 127 && srch_pos) { - if (srch_pos) - { - --srch_pos; - str_srch = ft_remove_imput(str_srch, srch_pos); - ft_puttermcaps("le"); - } + --srch_pos; + str_srch = ft_remove_imput(str_srch, srch_pos); + ft_puttermcaps("le"); } - else + else if (ret != 127) break ; } - ft_clear_prompt(*str, pos, srch_pos); - ft_prompt(); - if (*str) - { - *str = ft_strdup(*str); - ft_current_str(*str, *pos); - ft_get_next_str(*str, pos); - } - free(str_srch); + ft_modify_str(str_srch, srch_pos); } diff --git a/42sh/src/line-editing/tool_line_2.c b/42sh/src/line-editing/tool_line_2.c index c5c9dd8e..e4783c0a 100644 --- a/42sh/src/line-editing/tool_line_2.c +++ b/42sh/src/line-editing/tool_line_2.c @@ -6,17 +6,19 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/20 19:07:52 by gwojda #+# #+# */ -/* Updated: 2017/02/02 14:16:24 by gwojda ### ########.fr */ +/* Updated: 2017/02/04 16:27:34 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static void ft_clear_window() +static void ft_clear_window(void) { pid_t soon_pid; - char *tab_str[2] = {(char *){"/usr/bin/clear"}, NULL}; + char *tab_str[2]; + tab_str[0] = "/usr/bin/clear"; + tab_str[1] = NULL; soon_pid = fork(); if (soon_pid != 0) wait(NULL); @@ -24,25 +26,10 @@ static void ft_clear_window() execve("/usr/bin/clear", tab_str, data_singleton()->env); } -void ft_printall(void) +static void ft_read_tmp(void) { - size_t pos_tmp; - int ret; - char *str; - size_t *pos; + int ret; - str = data_singleton()->line.input; - pos = &data_singleton()->line.pos; - ret = 0; - if (read(0, &ret, sizeof(int)) == -1 || ret != 126 || !str) - return ; - ft_clear_window(); - ft_prompt(); - pos_tmp = *pos; - ft_putstr("\033[31m"); - ft_puttermcaps("cd"); - ft_puttermcaps("sc"); - ft_putstr(str); while (42) { ret = 0; @@ -54,9 +41,36 @@ void ft_printall(void) break ; } } +} + +void ft_change_affichage(void) +{ + ft_putstr("\033[31m"); + ft_puttermcaps("cd"); + ft_puttermcaps("sc"); + ft_putstr(STR); + ft_read_tmp(); ft_puttermcaps("rc"); ft_puttermcaps("cd"); ft_putstr("\033[37m"); +} + +void ft_printall(void) +{ + size_t pos_tmp; + char *str; + size_t *pos; + int ret; + + str = STR; + pos = &POS; + ret = 0; + if (read(0, &ret, sizeof(int)) == -1 || ret != 126 || !str) + return ; + ft_clear_window(); + ft_prompt(); + pos_tmp = *pos; + ft_change_affichage(); if (*pos) --(*pos); ft_get_beggin(str, pos); @@ -69,18 +83,7 @@ void ft_printall(void) *pos = pos_tmp; } -void ft_check_end_of_line(char *str, size_t pos) -{ - if (!str) - return ; - if (ft_nb_last_line(str, pos) == 0) - { - ft_putchar(' '); - ft_puttermcaps("le"); - } -} - -void ft_get_beggin_with_curs(char *str, size_t *pos) +void ft_get_beggin_with_curs(char *str, size_t *pos) { while ((*pos) && str[(*pos)] != '\n') { diff --git a/42sh/src/line-editing/toolz.c b/42sh/src/line-editing/toolz.c index 1051442e..026f5439 100644 --- a/42sh/src/line-editing/toolz.c +++ b/42sh/src/line-editing/toolz.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/16 17:06:30 by gwojda #+# #+# */ -/* Updated: 2017/02/02 16:22:47 by gwojda ### ########.fr */ +/* Updated: 2017/02/04 16:19:49 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,7 +43,7 @@ char *ft_strndup(char const *s, int n) return (str); } -char *ft_strdupi(char const *s) +char *ft_strdupi(char const *s) { int i; char *str; diff --git a/42sh/src/line-editing/toolz2.c b/42sh/src/line-editing/toolz2.c index ce1c62f6..9a468b8f 100644 --- a/42sh/src/line-editing/toolz2.c +++ b/42sh/src/line-editing/toolz2.c @@ -6,17 +6,18 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/07 11:00:28 by gwojda #+# #+# */ -/* Updated: 2017/02/02 16:04:55 by gwojda ### ########.fr */ +/* Updated: 2017/02/04 16:20:11 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void ft_add_in_history_file(char *str) +void ft_add_in_history_file(char *str) { int fd; - fd = open(".42sh_history", O_CREAT | O_WRONLY | O_APPEND, S_IWUSR | S_IRUSR); + fd = open(".42sh_history", + O_CREAT | O_WRONLY | O_APPEND, S_IWUSR | S_IRUSR); if (fd == -1) return ; write(fd, str, ft_strlen(str)); @@ -24,7 +25,7 @@ void ft_add_in_history_file(char *str) close(fd); } -int ft_nbr_len(int nbr) +int ft_nbr_len(int nbr) { if (nbr % 10 != nbr) return (ft_nbr_len(nbr / 10) + 1); @@ -32,7 +33,7 @@ int ft_nbr_len(int nbr) return (1); } -void ft_puttermcaps(char *str) +void ft_puttermcaps(char *str) { char *res; char *env; @@ -43,7 +44,7 @@ void ft_puttermcaps(char *str) tputs(res, 0, ft_put); } -int ft_size_term(void) +int ft_size_term(void) { struct winsize w; diff --git a/42sh/src/line-editing/toolz_termcaps.c b/42sh/src/line-editing/toolz_termcaps.c index 901b026e..ba933617 100644 --- a/42sh/src/line-editing/toolz_termcaps.c +++ b/42sh/src/line-editing/toolz_termcaps.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/08 12:35:11 by gwojda #+# #+# */ -/* Updated: 2017/01/24 11:19:34 by gwojda ### ########.fr */ +/* Updated: 2017/02/04 16:26:55 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,3 +43,14 @@ int ft_nb_last_line(char *str, size_t pos) len += ft_get_size_prev(str, pos); return ((len) ? len % ft_size_term() : -1); } + +void ft_check_end_of_line(char *str, size_t pos) +{ + if (!str) + return ; + if (ft_nb_last_line(str, pos) == 0) + { + ft_putchar(' '); + ft_puttermcaps("le"); + } +} diff --git a/42sh/src/main/data_singleton.c b/42sh/src/main/data_singleton.c index 22c7306f..e73ed322 100644 --- a/42sh/src/main/data_singleton.c +++ b/42sh/src/main/data_singleton.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/10 11:36:39 by jhalford #+# #+# */ -/* Updated: 2017/02/03 15:25:13 by gwojda ### ########.fr */ +/* Updated: 2017/02/04 10:55:39 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */