changement edition de line -> STR et POS vous nous manquez

This commit is contained in:
gwojda 2017-03-17 12:21:25 +01:00
parent 8b9516b672
commit a40169e77b
19 changed files with 301 additions and 292 deletions

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/17 10:54:02 by gwojda ### ########.fr */ /* Updated: 2017/03/17 12:17:52 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -57,8 +57,8 @@
# define CORRUPT 1 # define CORRUPT 1
# define STR data_singleton()->line.input //# define STR data_singleton()->line.input
# define POS data_singleton()->line.pos //# define POS data_singleton()->line.pos
# define ERROR_CNTL_R 1 # define ERROR_CNTL_R 1
@ -87,7 +87,7 @@ typedef struct s_line
typedef struct s_key typedef struct s_key
{ {
int value; int value;
int (*f)(void); int (*f)(char **str, size_t *pos);
} t_key; } t_key;
extern t_key g_keys[]; extern t_key g_keys[];
@ -132,32 +132,32 @@ size_t ft_hist_len(void);
int ft_read_stdin(char **input); int ft_read_stdin(char **input);
int ft_end(void); int ft_end(char **str, size_t *pos);
int ft_home(void); int ft_home(char **str, size_t *pos);
int ft_move_right(void); int ft_move_right(char **str, size_t *pos);
int ft_move_left(void); int ft_move_left(char **str, size_t *pos);
int ft_up(void); int ft_up(char **str, size_t *pos);
int ft_down(void); int ft_down(char **str, size_t *pos);
int ft_buff_f6(void); int ft_buff_f6(char **str, size_t *pos);
int ft_control_d(void); int ft_control_d(char **str, size_t *pos);
int ft_control_c(void); int ft_control_c(char **str, size_t *pos);
int ft_control_l(void); int ft_control_l(char **str, size_t *pos);
int ft_del(void); int ft_del(char **str, size_t *pos);
int ft_suppr(void); int ft_suppr(char **str, size_t *pos);
int ft_print(int ret); int ft_surch_in_history(char **str, size_t *pos);
int ft_surch_in_history(void); int ft_printall(char **str, size_t *pos);
int ft_printall(void); int ft_history_down(char **str, size_t *pos);
int ft_history_down(void); int ft_history_up(char **str, size_t *pos);
int ft_history_up(void); int ft_found_next_word(char **str, size_t *pos);
int ft_found_next_word(void); int ft_found_prev_word(char **str, size_t *pos);
int ft_found_prev_word(void); int ft_c(char **str, size_t *pos);
int ft_c(void); int ft_x(char **str, size_t *pos);
int ft_x(void); int ft_v(char **str, size_t *pos);
int ft_v(void);
int ft_read_it(int input, size_t *pos, char **str); int ft_read_it(int input, size_t *pos, char **str);
int ft_print(int ret, char **str, size_t *pos);
int readline(int prompt, char **input); int readline(int prompt, char **input);
int ft_completion(int ret); int ft_completion(int ret, char **str, size_t *pos);
struct termios *ft_save_termios(int save); struct termios *ft_save_termios(int save);

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/08 17:20:01 by gwojda #+# #+# */ /* Created: 2017/03/08 17:20:01 by gwojda #+# #+# */
/* Updated: 2017/03/16 08:39:09 by alao ### ########.fr */ /* Updated: 2017/03/17 11:20:10 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: alao <alao@student.42.fr> +#+ +:+ +#+ */ /* By: alao <alao@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 12:03:30 by alao #+# #+# */ /* Created: 2017/02/15 12:03:30 by alao #+# #+# */
/* Updated: 2017/03/16 10:21:03 by gwojda ### ########.fr */ /* Updated: 2017/03/17 12:03:26 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -59,7 +59,8 @@ int c_rematch(t_comp *c, long int keypress)
c_clear(data_singleton()); c_clear(data_singleton());
else if (c->lst == c->lst->next) else if (c->lst == c->lst->next)
return (1); return (1);
ft_print(keypress); ft_print(keypress, &data_singleton()->line.input,
&data_singleton()->line.pos);
return (1); return (1);
} }
else else

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/02 15:22:19 by gwojda #+# #+# */ /* Created: 2017/02/02 15:22:19 by gwojda #+# #+# */
/* Updated: 2017/03/17 10:48:21 by gwojda ### ########.fr */ /* Updated: 2017/03/17 12:09:42 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,63 +16,63 @@ static void ft_clear_before_history(char **str)
{ {
if (*str) if (*str)
{ {
if (POS) if (data_singleton()->line.pos)
{ {
--POS; --data_singleton()->line.pos;
ft_get_beggin_with_curs(*str, &POS); ft_get_beggin_with_curs(*str, &data_singleton()->line.pos);
} }
ft_puttermcaps("cd"); ft_puttermcaps("cd");
POS = 0; data_singleton()->line.pos = 0;
ft_strdel(str); ft_strdel(str);
} }
} }
int ft_history_down(void) int ft_history_down(char **str, size_t *pos)
{ {
t_list_history *head; t_list_history *head;
head = data_singleton()->line.list_cur; head = data_singleton()->line.list_cur;
if (!head) if (!head)
return (0); return (0);
ft_clear_before_history(&STR); ft_clear_before_history(str);
if (head->next) if (head->next)
head = head->next; head = head->next;
if (!head->str) if (!head->str)
STR = NULL; *str = NULL;
else else
if (!(STR = ft_strdup(head->str))) if (!(*str = ft_strdup(head->str)))
return (-1); return (-1);
if (STR) if (*str)
ft_current_str(STR, POS); ft_current_str(*str, *pos);
if (STR) if (*str)
POS = ft_strlen_next(STR, POS); *pos = ft_strlen_next(*str, *pos);
else else
POS = 0; *pos = 0;
data_singleton()->line.list_cur = head; data_singleton()->line.list_cur = head;
return (0); return (0);
} }
int ft_history_up(void) int ft_history_up(char **str, size_t *pos)
{ {
t_list_history *head; t_list_history *head;
head = data_singleton()->line.list_cur; head = data_singleton()->line.list_cur;
if (!head) if (!head)
return (0); return (0);
ft_clear_before_history(&STR); ft_clear_before_history(str);
if (head->prev) if (head->prev)
head = head->prev; head = head->prev;
if (!head->str) if (!head->str)
STR = NULL; *str = NULL;
else else
if (!(STR = ft_strdup(head->str))) if (!(*str = ft_strdup(head->str)))
return (-1); return (-1);
if (STR) if (*str)
ft_current_str(STR, POS); ft_current_str(*str, *pos);
if (STR) if (*str)
POS = ft_strlen_next(STR, POS); *pos = ft_strlen_next(*str, *pos);
else else
POS = 0; *pos = 0;
data_singleton()->line.list_cur = head; data_singleton()->line.list_cur = head;
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/25 11:39:47 by gwojda #+# #+# */ /* Created: 2017/01/25 11:39:47 by gwojda #+# #+# */
/* Updated: 2017/03/17 10:48:34 by gwojda ### ########.fr */ /* Updated: 2017/03/17 12:10:17 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -67,14 +67,14 @@ int ft_history_parsing(int has_prompt, char **input)
i = 0; i = 0;
boolean = 0; boolean = 0;
if (!STR) if (!data_singleton()->line.input)
return (0); return (0);
while (STR && STR[i]) while (data_singleton()->line.input && data_singleton()->line.input[i])
{ {
if (STR[i] == '!') if (data_singleton()->line.input[i] == '!')
{ {
boolean = 1; boolean = 1;
if (!ft_history_parsing_3(STR, &i)) if (!ft_history_parsing_3(data_singleton()->line.input, &i))
boolean = 0; boolean = 0;
else else
break ; break ;

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/26 10:43:16 by gwojda #+# #+# */ /* Created: 2017/01/26 10:43:16 by gwojda #+# #+# */
/* Updated: 2017/03/17 10:48:06 by gwojda ### ########.fr */ /* Updated: 2017/03/17 12:12:19 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -35,34 +35,37 @@ static int ft_surch_and_realloc(char **str, char **str_srch,
static void ft_give_new_prompt(char *str_srch, size_t srch_pos) static void ft_give_new_prompt(char *str_srch, size_t srch_pos)
{ {
ft_clear_prompt(&POS, srch_pos); ft_clear_prompt(&data_singleton()->line.pos, srch_pos);
data_singleton()->line.prompt_size = 21; data_singleton()->line.prompt_size = 21;
if (str_srch) if (str_srch)
ft_printf("\033[35m(reverse-i-search)`\033[32m%s\033[35m': \033[37m", ft_printf("\033[35m(reverse-i-search)`\033[32m%s\033[35m': \033[37m",
str_srch); str_srch);
else else
ft_putstr("\033[35m(reverse-i-search)`': \033[37m"); ft_putstr("\033[35m(reverse-i-search)`': \033[37m");
if (STR) if (data_singleton()->line.input)
{ {
ft_current_str(STR, POS); ft_current_str(data_singleton()->line.input,
ft_get_next_str(STR, &POS); data_singleton()->line.pos);
ft_get_next_str(data_singleton()->line.input,
&data_singleton()->line.pos);
} }
} }
static void ft_modify_str(char *str_srch, size_t srch_pos) static void ft_modify_str(char *str_srch, size_t srch_pos, char **str,
size_t *pos)
{ {
ft_clear_prompt(&POS, srch_pos); ft_clear_prompt(pos, srch_pos);
data_singleton()->line.is_prompt ? ft_prompt() : ft_putstr("> "); data_singleton()->line.is_prompt ? ft_prompt() : ft_putstr("> ");
if (STR) if (*str)
{ {
STR = ft_strdup(STR); *str = ft_strdup(*str);
ft_current_str(STR, POS); ft_current_str(*str, *pos);
ft_get_next_str(STR, &POS); ft_get_next_str(*str, pos);
} }
free(str_srch); free(str_srch);
} }
int ft_surch_in_history(void) int ft_surch_in_history(char **str, size_t *pos)
{ {
char *str_srch; char *str_srch;
int ret; int ret;
@ -70,7 +73,7 @@ int ft_surch_in_history(void)
srch_pos = 0; srch_pos = 0;
str_srch = NULL; str_srch = NULL;
(STR) ? ft_strdel(&STR) : 0; (*str) ? ft_strdel(str) : 0;
while (42) while (42)
{ {
ft_give_new_prompt(str_srch, srch_pos); ft_give_new_prompt(str_srch, srch_pos);
@ -78,7 +81,7 @@ int ft_surch_in_history(void)
read(0, &ret, sizeof(int)); read(0, &ret, sizeof(int));
if (ft_isprint(ret)) if (ft_isprint(ret))
{ {
if (ft_surch_and_realloc(&STR, &str_srch, ret, &srch_pos) < 0) if (ft_surch_and_realloc(str, &str_srch, ret, &srch_pos) < 0)
return (-1); return (-1);
} }
else if (ret == 127 && srch_pos) else if (ret == 127 && srch_pos)
@ -86,11 +89,11 @@ int ft_surch_in_history(void)
--srch_pos; --srch_pos;
str_srch = ft_remove_imput(str_srch, srch_pos); str_srch = ft_remove_imput(str_srch, srch_pos);
ft_puttermcaps("le"); ft_puttermcaps("le");
STR = (!*str_srch) ? NULL : ft_strget_history(str_srch); *str = (!*str_srch) ? NULL : ft_strget_history(str_srch);
} }
else if (ret != 127) else if (ret != 127)
break ; break ;
} }
ft_modify_str(str_srch, srch_pos); ft_modify_str(str_srch, srch_pos, str, pos);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/03 14:15:55 by gwojda #+# #+# */ /* Created: 2017/02/03 14:15:55 by gwojda #+# #+# */
/* Updated: 2017/03/17 10:45:06 by gwojda ### ########.fr */ /* Updated: 2017/03/17 12:13:41 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,7 +17,7 @@ static size_t ft_strleni_w(char *str, size_t pos, char c)
size_t len; size_t len;
len = 0; len = 0;
if (!STR) if (!str)
return (0); return (0);
while (str[pos] && str[pos] != c) while (str[pos] && str[pos] != c)
{ {
@ -27,7 +27,7 @@ static size_t ft_strleni_w(char *str, size_t pos, char c)
return (len); return (len);
} }
int ft_completion(int ret) int ft_completion(int ret, char **str, size_t *pos)
{ {
size_t tmp; size_t tmp;
size_t pos_tmp; size_t pos_tmp;
@ -35,8 +35,8 @@ int ft_completion(int ret)
char boolean; char boolean;
boolean = 0; boolean = 0;
pos_tmp = POS; pos_tmp = *pos;
right = ft_strleni_w(STR, POS, '\n'); right = ft_strleni_w(*str, *pos, '\n');
if (((ret != TOUCHE_TAB && ret != 10) if (((ret != TOUCHE_TAB && ret != 10)
|| (ret == 10)) && !(data_singleton()->comp)) || (ret == 10)) && !(data_singleton()->comp))
return (0); return (0);
@ -45,13 +45,13 @@ int ft_completion(int ret)
if (boolean || ret == 10) if (boolean || ret == 10)
{ {
(pos_tmp) ? --pos_tmp : ft_puttermcaps("nd"); (pos_tmp) ? --pos_tmp : ft_puttermcaps("nd");
ft_get_beggin_with_curs(STR, &pos_tmp); ft_get_beggin_with_curs(*str, &pos_tmp);
tmp = pos_tmp; tmp = pos_tmp;
ft_puttermcaps("cd"); ft_puttermcaps("cd");
ft_current_str(STR, tmp); ft_current_str(*str, tmp);
ft_get_next_str(STR, &tmp); ft_get_next_str(*str, &tmp);
ft_putnc('\b', right); ft_putnc('\b', right);
POS = ft_strleni_w(STR, pos_tmp, '\n') - right; *pos = ft_strleni_w(*str, pos_tmp, '\n') - right;
} }
return (0); return (1);
} }

View file

@ -6,54 +6,53 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/02 15:17:28 by gwojda #+# #+# */ /* Created: 2017/02/02 15:17:28 by gwojda #+# #+# */
/* Updated: 2017/03/17 10:45:20 by gwojda ### ########.fr */ /* Updated: 2017/03/17 12:20:31 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int ft_buff_f6(void) int ft_buff_f6(char **str, size_t *pos)
{ {
int ret; int ret;
ret = 0; ret = 0;
(void)str;
(void)pos;
if (read(0, &ret, sizeof(int)) < 0) if (read(0, &ret, sizeof(int)) < 0)
return (-1); return (-1);
return (0); return (0);
} }
int ft_control_d(void) int ft_control_d(char **str, size_t *pos)
{ {
if (!STR || STR[0] == '\0') if (!*str || (*str)[0] == '\0')
{ {
ft_putstr("exit\n"); ft_putstr("exit\n");
builtin_exit(NULL, (char*[]){"exit", NULL}, NULL); builtin_exit(NULL, (char*[]){"exit", NULL}, NULL);
data_singleton()->line.is_prompt ? ft_prompt() : ft_putstr("> "); data_singleton()->line.is_prompt ? ft_prompt() : ft_putstr("> ");
} }
else if (POS < ft_strlen(STR)) else if (*pos < ft_strlen(*str))
ft_del(); ft_del(str, pos);
else else
ft_puttermcaps("bl"); ft_puttermcaps("bl");
return (0); return (0);
} }
int ft_control_c(void) int ft_control_c(char **str, size_t *pos)
{ {
/* if (STR) if (*str)
ft_current_str(STR, POS); ft_current_str(*str, *pos);
ft_putchar('\n'); ft_strdel(str);
set_exitstatus(1, 1); *pos = 0;
ft_prompt();*/
ft_strdel(&STR);
POS = 0;
return (42); return (42);
} }
int ft_control_l(void) int ft_control_l(char **str, size_t *pos)
{ {
ft_clear_window(); ft_clear_window();
ft_prompt(); ft_prompt();
ft_strdel(&STR); ft_strdel(str);
POS = 0; *pos = 0;
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/19 12:45:06 by gwojda #+# #+# */ /* Created: 2016/12/19 12:45:06 by gwojda #+# #+# */
/* Updated: 2017/03/17 10:58:09 by gwojda ### ########.fr */ /* Updated: 2017/03/17 11:56:42 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -37,7 +37,7 @@ static char *ft_strdupi_space(char const *s)
return (str); return (str);
} }
int ft_v(void) int ft_v(char **str, size_t *pos)
{ {
size_t tmp_pos; size_t tmp_pos;
int i; int i;
@ -45,40 +45,40 @@ int ft_v(void)
tmp = data_singleton()->line.copy_tmp; tmp = data_singleton()->line.copy_tmp;
i = -1; i = -1;
tmp_pos = POS; tmp_pos = *pos;
if (!STR || !tmp) if (!*str || !tmp)
return (0); return (0);
while (tmp[++i]) while (tmp[++i])
if (!(STR = ft_realloc_imput(STR, tmp[i], POS + i))) if (!(*str = ft_realloc_imput(*str, tmp[i], *pos + i)))
return (-1); return (-1);
if (POS) if (*pos)
{ {
--POS; --(*pos);
ft_get_beggin_with_curs(STR, &POS); ft_get_beggin_with_curs(*str, pos);
} }
ft_current_str(STR, POS); ft_current_str(*str, *pos);
ft_get_next_str(STR, &POS); ft_get_next_str(*str, pos);
ft_putnc('\b', POS - tmp_pos); ft_putnc('\b', *pos - tmp_pos);
POS = tmp_pos; *pos = tmp_pos;
return (0); return (0);
} }
int ft_x(void) int ft_x(char **str, size_t *pos)
{ {
int i; int i;
char **tmp; char **tmp;
tmp = &data_singleton()->line.copy_tmp; tmp = &data_singleton()->line.copy_tmp;
if (!STR) if (!*str)
return (0); return (0);
if (*tmp) if (*tmp)
ft_strdel(tmp); ft_strdel(tmp);
if (!(*tmp = ft_strdupi_space(&STR[POS]))) if (!(*tmp = ft_strdupi_space(&(*str)[*pos])))
return (-1); return (-1);
i = ft_strlen(*tmp); i = ft_strlen(*tmp);
while (i >= 0) while (i >= 0)
{ {
if (!(STR = ft_remove_imput(STR, POS + i))) if (!(*str = ft_remove_imput(*str, *pos + i)))
return (-1); return (-1);
--i; --i;
} }
@ -86,16 +86,16 @@ int ft_x(void)
return (0); return (0);
} }
int ft_c(void) int ft_c(char **str, size_t *pos)
{ {
char **tmp; char **tmp;
if (!STR) if (!*str)
return (0); return (0);
tmp = &data_singleton()->line.copy_tmp; tmp = &data_singleton()->line.copy_tmp;
if (*tmp) if (*tmp)
ft_strdel(tmp); ft_strdel(tmp);
if (!(*tmp = ft_strdupi_space(STR + POS))) if (!(*tmp = ft_strdupi_space(*str + *pos)))
return (-1); return (-1);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/19 16:28:49 by gwojda #+# #+# */ /* Created: 2016/12/19 16:28:49 by gwojda #+# #+# */
/* Updated: 2017/03/17 10:49:59 by gwojda ### ########.fr */ /* Updated: 2017/03/17 12:15:58 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -38,14 +38,14 @@ t_key g_key[] =
{0 , 0 }, {0 , 0 },
}; };
static void init_read_stdin(void) static void init_read_stdin(char **str, size_t *pos)
{ {
if (STR) if (*str)
{ {
ft_current_str(STR, POS); ft_current_str(*str, *pos);
ft_get_next_str(STR, &POS); ft_get_next_str(*str, pos);
if (STR[POS]) if ((*str)[*pos])
++(POS); ++(*pos);
} }
if (data_singleton()->comp) if (data_singleton()->comp)
c_clear(data_singleton()); c_clear(data_singleton());
@ -57,29 +57,33 @@ int ft_read_stdin(char **input)
int ret; int ret;
int j; int j;
init_read_stdin(); init_read_stdin(&data_singleton()->line.input, &data_singleton()->line.pos);
while (42) while (42)
{ {
ret = 0; ret = 0;
j = 0; j = 0;
read(0, &ret, sizeof(int)); read(0, &ret, sizeof(int));
if (ft_completion(ret)) if (ft_completion(ret, &data_singleton()->line.input,
&data_singleton()->line.pos))
continue ; continue ;
while (g_key[j].value && g_key[j].value != ret) while (g_key[j].value && g_key[j].value != ret)
++j; ++j;
if (g_key[j].value) if (g_key[j].value)
{ {
if ((ret = g_key[j].f())) if ((ret = g_key[j].f(&data_singleton()->line.input,
&data_singleton()->line.pos)))
return (ret); return (ret);
} }
else if (ft_isprint(ret)) else if (ft_isprint(ret))
ft_print(ret); ft_print(ret, &data_singleton()->line.input,
&data_singleton()->line.pos);
else if (ret == 10) else if (ret == 10)
{ {
*input = STR; *input = data_singleton()->line.input;
return (0); return (0);
} }
else if (ft_isascii(ret) == 0) else if (ft_isascii(ret) == 0)
ft_read_it(ret, &POS, &STR); ft_read_it(ret, &data_singleton()->line.pos,
&data_singleton()->line.input);
} }
} }

View file

@ -6,48 +6,48 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/14 11:13:24 by gwojda #+# #+# */ /* Created: 2017/02/14 11:13:24 by gwojda #+# #+# */
/* Updated: 2017/03/17 10:46:05 by gwojda ### ########.fr */ /* Updated: 2017/03/17 11:54:33 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int ft_end(void) int ft_end(char **str, size_t *pos)
{ {
if (!STR) if (!*str)
return (0); return (0);
if (POS) if (*pos)
{ {
--POS; --(*pos);
ft_get_beggin_with_curs(STR, &POS); ft_get_beggin_with_curs(*str, pos);
} }
ft_puttermcaps("cd"); ft_puttermcaps("cd");
while (STR[POS]) while ((*str)[*pos])
++POS; ++(*pos);
ft_get_beggin(STR, &POS); ft_get_beggin(*str, pos);
ft_current_str(STR, POS); ft_current_str(*str, *pos);
ft_get_next_str(STR, &POS); ft_get_next_str(*str, pos);
return (0); return (0);
} }
int ft_home(void) int ft_home(char **str, size_t *pos)
{ {
if (!STR) if (!*str)
return (0); return (0);
if (POS) if (*pos)
{ {
--POS; --(*pos);
ft_get_beggin_with_curs(STR, &POS); ft_get_beggin_with_curs(*str, pos);
if (STR[POS + 1] == '\n') if ((*str)[*pos + 1] == '\n')
ft_puttermcaps("nd"); ft_puttermcaps("nd");
} }
ft_puttermcaps("cd"); ft_puttermcaps("cd");
POS = 0; *pos = 0;
ft_current_str(STR, POS); ft_current_str(*str, *pos);
ft_get_next_str(STR, &POS); ft_get_next_str(*str, pos);
ft_check_end_of_line(STR, POS); ft_check_end_of_line(*str, *pos);
if (!STR[POS]) if (!(*str)[*pos])
--POS; --(*pos);
ft_get_beggin_with_curs(STR, &POS); ft_get_beggin_with_curs(*str, pos);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/07 17:34:44 by gwojda #+# #+# */ /* Created: 2017/03/07 17:34:44 by gwojda #+# #+# */
/* Updated: 2017/03/16 15:32:03 by jhalford ### ########.fr */ /* Updated: 2017/03/17 12:16:20 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -36,10 +36,10 @@ void readline_init(int has_prompt)
beg = 1; beg = 1;
} }
ft_init_termios(); ft_init_termios();
if (STR) if (data_singleton()->line.input)
ft_strdel(&STR); ft_strdel(&data_singleton()->line.input);
data_singleton()->line.list_cur = data_singleton()->line.list_beg; data_singleton()->line.list_cur = data_singleton()->line.list_beg;
POS = 0; data_singleton()->line.pos = 0;
has_prompt ? ft_prompt() : ft_putstr("> "); has_prompt ? ft_prompt() : ft_putstr("> ");
data_singleton()->line.is_prompt = has_prompt ? 1 : 0; data_singleton()->line.is_prompt = has_prompt ? 1 : 0;
} }

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/17 10:58:40 by gwojda ### ########.fr */ /* Updated: 2017/03/17 12:17:07 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -39,35 +39,35 @@ void ft_change_affichage(void)
ft_putstr("\033[31m"); ft_putstr("\033[31m");
ft_puttermcaps("cd"); ft_puttermcaps("cd");
ft_puttermcaps("sc"); ft_puttermcaps("sc");
ft_putstr(STR); ft_putstr(data_singleton()->line.input);
ft_read_tmp(); ft_read_tmp();
ft_puttermcaps("rc"); ft_puttermcaps("rc");
ft_puttermcaps("cd"); ft_puttermcaps("cd");
ft_putstr("\033[37m"); ft_putstr("\033[37m");
} }
int ft_printall(void) int ft_printall(char **str, size_t *pos)
{ {
size_t pos_tmp; size_t pos_tmp;
int ret; int ret;
ret = 0; ret = 0;
if (read(0, &ret, sizeof(int)) == -1 || ret != 126 || !STR) if (read(0, &ret, sizeof(int)) == -1 || ret != 126 || !*str)
return (0); return (0);
ft_clear_window(); ft_clear_window();
ft_prompt(); ft_prompt();
pos_tmp = POS; pos_tmp = *pos;
ft_change_affichage(); ft_change_affichage();
if (POS) if (*pos)
--POS; --(*pos);
ft_get_beggin(STR, &POS); ft_get_beggin(*str, pos);
ft_current_str(STR, POS); ft_current_str(*str, *pos);
ft_get_next_str(STR, &POS); ft_get_next_str(*str, pos);
if (POS && !STR[POS]) if (*pos && !(*str)[*pos])
--POS; --(*pos);
if (POS) if (*pos)
ft_putnc('\b', POS - pos_tmp + 1); ft_putnc('\b', *pos - pos_tmp + 1);
POS = pos_tmp; *pos = pos_tmp;
return (0); return (0);
} }

View file

@ -6,65 +6,65 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/19 16:43:58 by gwojda #+# #+# */ /* Created: 2016/12/19 16:43:58 by gwojda #+# #+# */
/* Updated: 2017/03/17 10:46:27 by gwojda ### ########.fr */ /* Updated: 2017/03/17 11:51:32 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int ft_move_right(void) int ft_move_right(char **str, size_t *pos)
{ {
size_t tmp; size_t tmp;
if (ft_strlen(STR) <= POS) if (ft_strlen(*str) <= *pos)
return (0); return (0);
if (STR[POS] == '\n') if ((*str)[*pos] == '\n')
{ {
if (POS) if (*pos)
{ {
tmp = POS - 1; tmp = *pos - 1;
ft_get_beggin_with_curs(STR, &tmp); ft_get_beggin_with_curs(*str, &tmp);
} }
ft_puttermcaps("cd"); ft_puttermcaps("cd");
++POS; ++(*pos);
ft_current_str(STR, POS); ft_current_str(*str, *pos);
ft_get_next_str(STR, &POS); ft_get_next_str(*str, pos);
POS = (!STR[POS]) ? POS - 1 : POS; *pos = (!(*str)[*pos]) ? *pos - 1 : *pos;
ft_get_beggin_with_curs(STR, &POS); ft_get_beggin_with_curs(*str, pos);
} }
else else
{ {
ft_putchar(STR[POS]); ft_putchar((*str)[*pos]);
++POS; ++(*pos);
} }
return (0); return (0);
} }
int ft_move_left(void) int ft_move_left(char **str, size_t *pos)
{ {
if (!POS) if (!*pos)
return (0); return (0);
if (STR[POS - 1] == '\n') if ((*str)[*pos - 1] == '\n')
{ {
if (POS - 1 == 0) if (*pos - 1 == 0)
{ {
ft_puttermcaps("cd"); ft_puttermcaps("cd");
--POS; --(*pos);
return (0); return (0);
} }
ft_puttermcaps("cd"); ft_puttermcaps("cd");
POS -= 2; *pos -= 2;
ft_get_beggin(STR, &POS); ft_get_beggin(*str, pos);
if (!POS && STR[POS] == '\n') if (!*pos && (*str)[*pos] == '\n')
++POS; ++(*pos);
ft_current_str(STR, POS); ft_current_str(*str, *pos);
ft_get_next_str(STR, &POS); ft_get_next_str(*str, pos);
++POS; ++(*pos);
} }
else else
{ {
ft_puttermcaps("le"); ft_puttermcaps("le");
--POS; --(*pos);
} }
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/14 11:12:09 by gwojda #+# #+# */ /* Created: 2017/02/14 11:12:09 by gwojda #+# #+# */
/* Updated: 2017/03/17 10:46:44 by gwojda ### ########.fr */ /* Updated: 2017/03/17 11:49:18 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -55,75 +55,76 @@ static void ft_found_prev_word_2(int i, char *str, size_t *pos)
(*pos) -= i; (*pos) -= i;
} }
int ft_found_prev_word(void) int ft_found_prev_word(char **str, size_t *pos)
{ {
int i; int i;
i = 0; i = 0;
if (!POS || !STR) if (!*pos || !*str)
return (0); return (0);
ft_init_prev_word(&POS, STR); ft_init_prev_word(pos, *str);
if (POS >= 1 && STR[POS - 1] == '\n') if (*pos >= 1 && (*str)[*pos - 1] == '\n')
{ {
if (POS - 1 == 0) if (*pos - 1 == 0)
{ {
ft_puttermcaps("cd"); ft_puttermcaps("cd");
--POS; --(*pos);
return (0); return (0);
} }
ft_puttermcaps("cd"); ft_puttermcaps("cd");
POS -= 2; *pos -= 2;
ft_get_beggin(STR, &POS); ft_get_beggin(*str, pos);
if (!POS && STR[POS] == '\n') if (!*pos && (*str)[*pos] == '\n')
++POS; ++(*pos);
ft_current_str(STR, POS); ft_current_str(*str, *pos);
ft_get_next_str(STR, &POS); ft_get_next_str(*str, pos);
++POS; ++(*pos);
} }
else else
ft_found_prev_word_2(i, STR, &POS); ft_found_prev_word_2(i, *str, pos);
return (0); return (0);
} }
static void ft_found_next_word_2(void) static void ft_found_next_word_2(char **str, size_t *pos)
{ {
if (POS) if (*pos)
{ {
--POS; --(*pos);
ft_get_beggin_with_curs(STR, &POS); ft_get_beggin_with_curs(*str, pos);
} }
ft_puttermcaps("cd"); ft_puttermcaps("cd");
ft_get_next_str(STR, &POS); ft_get_next_str(*str, pos);
POS += (POS || STR[0] != '\n') ? 2 : 1; *pos += (*pos || (*str)[0] != '\n') ? 2 : 1;
ft_current_str(STR, POS); ft_current_str(*str, *pos);
ft_get_next_str(STR, &POS); ft_get_next_str(*str, pos);
if (!STR[POS]) if (!(*str)[*pos])
--POS; --(*pos);
ft_get_beggin_with_curs(STR, &POS); ft_get_beggin_with_curs(*str, pos);
} }
int ft_found_next_word(void) int ft_found_next_word(char **str, size_t *pos)
{ {
int i; int i;
i = 0; i = 0;
if (!STR) if (!*str)
return (0); return (0);
while (STR[i + POS] && STR[i + POS] == ' ') while ((*str)[i + *pos] && (*str)[i + *pos] == ' ')
{ {
ft_putchar(STR[i + POS]); ft_putchar((*str)[i + *pos]);
++i; ++i;
} }
if (STR[POS] == '\n') if ((*str)[*pos] == '\n')
ft_found_next_word_2(); ft_found_next_word_2(str, pos);
else else
{ {
while (STR[i + POS] && STR[i + POS] != '\n' && STR[i + POS] != ' ') while ((*str)[i + *pos] && (*str)[i + *pos] != '\n' &&
(*str)[i + *pos] != ' ')
{ {
ft_putchar(STR[i + POS]); ft_putchar((*str)[i + *pos]);
++i; ++i;
} }
POS += i; *pos += i;
} }
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/09 13:21:40 by gwojda #+# #+# */ /* Created: 2017/01/09 13:21:40 by gwojda #+# #+# */
/* Updated: 2017/03/17 10:46:55 by gwojda ### ########.fr */ /* Updated: 2017/03/17 11:44:08 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -38,22 +38,22 @@ static void ft_up_2(size_t *pos, char *str)
(*pos) -= i; (*pos) -= i;
} }
int ft_up(void) int ft_up(char **str, size_t *pos)
{ {
if (!STR || !POS) if (!*str || !*pos)
return (0); return (0);
if (STR[POS - 1] == '\n') if ((*str)[*pos - 1] == '\n')
{ {
ft_puttermcaps("cd"); ft_puttermcaps("cd");
if (POS >= 2) if (*pos >= 2)
POS -= 2; *pos -= 2;
ft_get_beggin(STR, &POS); ft_get_beggin(*str, pos);
ft_current_str(STR, POS); ft_current_str(*str, *pos);
ft_get_next_str(STR, &POS); ft_get_next_str(*str, pos);
++POS; ++(*pos);
} }
else else
ft_up_2(&POS, STR); ft_up_2(pos, *str);
return (0); return (0);
} }
@ -74,25 +74,25 @@ static void ft_down_2(size_t *pos, char *str)
ft_get_beggin_with_curs(str, pos); ft_get_beggin_with_curs(str, pos);
} }
int ft_down(void) int ft_down(char **str, size_t *pos)
{ {
int i; int i;
int len; int len;
i = 0; i = 0;
if (!STR) if (!*str)
return (0); return (0);
if (STR[POS] == '\n') if ((*str)[*pos] == '\n')
ft_down_2(&POS, STR); ft_down_2(pos, *str);
else else
{ {
len = ft_size_term(); len = ft_size_term();
while (STR[i + POS] && STR[i + POS] != '\n' && --len + 1) while ((*str)[i + *pos] && (*str)[i + *pos] != '\n' && --len + 1)
{ {
ft_putchar(STR[i + POS]); ft_putchar((*str)[i + *pos]);
++i; ++i;
} }
POS += i; *pos += i;
} }
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/05 16:02:43 by gwojda #+# #+# */ /* Created: 2017/01/05 16:02:43 by gwojda #+# #+# */
/* Updated: 2017/03/17 10:47:11 by gwojda ### ########.fr */ /* Updated: 2017/03/17 12:19:23 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,21 +23,21 @@ int ft_found_next_char(char *str, size_t i)
return (0); return (0);
} }
int ft_print(int ret) int ft_print(int ret, char **str, size_t *pos)
{ {
int j; int j;
j = 0; j = 0;
if (!(STR = ft_realloc_imput(STR, ret, POS))) if (!(*str = ft_realloc_imput(*str, ret, *pos)))
return (-1); return (-1);
while (*(STR + POS + j) && *(STR + POS + j) != '\n') while (*(*str + *pos + j) && *(*str + *pos + j) != '\n')
{ {
ft_putchar(*(STR + POS + j)); ft_putchar(*(*str + *pos + j));
++j; ++j;
} }
ft_check_end_of_line(STR, POS + j); ft_check_end_of_line(*str, *pos + j);
ft_putnc('\b', j - 1); ft_putnc('\b', j - 1);
++POS; ++(*pos);
return (0); return (0);
} }
@ -54,54 +54,54 @@ static void ft_suppr_2(char **str, size_t *i, size_t tmp)
ft_strdel(str); ft_strdel(str);
} }
int ft_suppr(void) int ft_suppr(char **str, size_t *pos)
{ {
size_t tmp; size_t tmp;
char boolean; char boolean;
boolean = 0; boolean = 0;
if (POS <= 0) if (*pos <= 0)
return (0); return (0);
if (STR[POS - 1] != '\n') if ((*str)[*pos - 1] != '\n')
boolean = 1; boolean = 1;
--POS; --(*pos);
tmp = POS; tmp = *pos;
if (boolean) if (boolean)
{ {
ft_get_beggin_with_curs(STR, &POS); ft_get_beggin_with_curs(*str, pos);
if (!(STR = ft_remove_imput(STR, tmp))) if (!(*str = ft_remove_imput(*str, tmp)))
return (-1); return (-1);
} }
else else
{ {
if (!(STR = ft_remove_imput(STR, tmp))) if (!(*str = ft_remove_imput(*str, tmp)))
return (-1); return (-1);
ft_get_beggin(STR, &POS); ft_get_beggin(*str, pos);
} }
ft_suppr_2(&STR, &POS, tmp); ft_suppr_2(str, pos, tmp);
return (0); return (0);
} }
int ft_del(void) int ft_del(char **str, size_t *pos)
{ {
size_t tmp; size_t tmp;
tmp = POS; tmp = *pos;
if (!(STR = ft_remove_imput(STR, tmp))) if (!(*str = ft_remove_imput(*str, tmp)))
return (-1); return (-1);
if (!(STR && POS < ft_strlen(STR) + 1)) if (!(*str && *pos < ft_strlen(*str) + 1))
return (0); return (0);
if (POS) if (*pos)
{ {
--POS; --(*pos);
ft_get_beggin_with_curs(STR, &POS); ft_get_beggin_with_curs(*str, pos);
} }
ft_puttermcaps("cd"); ft_puttermcaps("cd");
ft_current_str(STR, POS); ft_current_str(*str, *pos);
ft_get_next_str(STR, &POS); ft_get_next_str(*str, pos);
if (STR[POS] && STR[POS] != '\n') if ((*str)[*pos] && (*str)[*pos] != '\n')
++POS; ++(*pos);
ft_putnc('\b', POS - tmp); ft_putnc('\b', *pos - tmp);
POS = tmp; *pos = tmp;
return (0); return (0);
} }

View file

@ -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/03/17 10:54:25 by gwojda ### ########.fr */ /* Updated: 2017/03/17 12:20:12 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,8 +22,9 @@ int readline(int has_prompt, char **input)
ret = ft_read_stdin(input); ret = ft_read_stdin(input);
if (ret < 0) if (ret < 0)
return (ret); return (ret);
if (STR) if (data_singleton()->line.input)
ft_current_str(STR, POS); ft_current_str(data_singleton()->line.input,
data_singleton()->line.pos);
ft_putchar('\n'); ft_putchar('\n');
if (has_prompt) if (has_prompt)
ret = ft_history_parsing(has_prompt, input); ret = ft_history_parsing(has_prompt, input);

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 10:56:08 by gwojda #+# #+# */ /* Created: 2017/03/14 10:56:08 by gwojda #+# #+# */
/* Updated: 2017/03/15 14:25:51 by gwojda ### ########.fr */ /* Updated: 2017/03/17 11:23:49 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,8 +23,8 @@ void sigwinch_resize(int sig)
c_term_resize(data_singleton()->comp); c_term_resize(data_singleton()->comp);
ft_puttermcaps("cl"); ft_puttermcaps("cl");
data_singleton()->line.is_prompt ? ft_prompt() : ft_putstr("> "); data_singleton()->line.is_prompt ? ft_prompt() : ft_putstr("> ");
ft_putall_current_str(STR, &pos_tmp); ft_putall_current_str(data_singleton()->line.input, &pos_tmp);
ft_putnc('\b', pos_tmp - POS); ft_putnc('\b', pos_tmp - data_singleton()->line.pos);
c_dispatcher(data_singleton()); c_dispatcher(data_singleton());
} }
} }