From b3921e090a3b900240e81ada562438f74ffc5b4d Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Wed, 16 Dec 2020 17:05:38 +0100
Subject: Merge

---
 .gitignore     |   2 +
 src/c_ctrls.c  |  23 ++++++++-
 src/c_ctrls.h  |   1 +
 src/c_init.c   |  74 ++++++++-------------------
 src/c_keys.c   |   2 +-
 src/c_utils.c  | 159 +++++++++++++++++++++++++++------------------------------
 src/c_utils.h  |   6 +--
 src/p_redirs.c |   2 +-
 8 files changed, 125 insertions(+), 144 deletions(-)

diff --git a/.gitignore b/.gitignore
index aa126af..d608b22 100644
--- a/.gitignore
+++ b/.gitignore
@@ -138,6 +138,7 @@ Sessionx.vim
 *~
 # Auto-generated tag files
 tags
+TAGS
 # Persistent undo
 [._]*.un~
 
@@ -152,6 +153,7 @@ qwe
 noread
 .clang_complete
 compile_flags.txt
+*.txt
 /bash
 /mini
 .ccls*
diff --git a/src/c_ctrls.c b/src/c_ctrls.c
index f841705..58b4517 100644
--- a/src/c_ctrls.c
+++ b/src/c_ctrls.c
@@ -11,6 +11,7 @@
 /* ************************************************************************** */
 
 #include <signal.h>
+#include <stdlib.h>
 #include <libft.h>
 #include <unistd.h>
 #include <term.h>
@@ -18,6 +19,9 @@
 #include "c_ctrls.h"
 #include "c_utils.h"
 #include "m_prompt.h"
+#include "m_loop.h"
+#include "s_destroy.h"
+#include "u_utils.h"
 
 t_msh	*c_get_msh(int mode, t_msh *src)
 {
@@ -30,11 +34,25 @@ t_msh	*c_get_msh(int mode, t_msh *src)
 	return (msh);
 }
 
+short	c_ctrl_d(t_msh *msh)
+{
+	int tmp;
+
+	c_set_term_raw(0);
+	write(STDERR_FILENO, "exit\n", 5);
+	u_eof_fd(msh->fd);
+	m_dump_hist(msh->curr_hist_ptr, msh);
+	tmp = msh->ret;
+	s_destroy(msh);
+	exit(tmp);
+	return (1);
+}
+
 short	c_ctrl_l(char *line, t_caps *tcaps, t_msh *msh)
 {
 	tputs(tgetstr("cl", NULL), 1, ft_putchar);
 	m_prompt_psx(1, msh);
-	c_redraw_line(line, tcaps, msh);
+	c_redraw_line(line, tcaps);
 	return (1);
 }
 
@@ -44,7 +62,8 @@ char	*c_ctrl_c(char **line, char *buf, t_msh *msh)
 	buf[0] = '\n';
 	write(1, "\n", 1);
 	ft_memdel((void**)line);
-	return (ft_strdup("\0"));
+	*line = ft_strdup("\0");
+	return (NULL);
 }
 
 void	c_signal_int(int signo)
diff --git a/src/c_ctrls.h b/src/c_ctrls.h
index 63710a0..d4b932b 100644
--- a/src/c_ctrls.h
+++ b/src/c_ctrls.h
@@ -20,6 +20,7 @@ void	c_signal_int(int signo);
 void	c_signal_ign(int signo);
 t_msh	*c_get_msh(int mode, t_msh *src);
 short	c_ctrl_l(char *line, t_caps *tcaps, t_msh *msh);
+short	c_ctrl_d(t_msh *msh);
 char	*c_ctrl_c(char **line, char *buf, t_msh *msh);
 
 #endif
diff --git a/src/c_init.c b/src/c_init.c
index 1d2f588..4e74d87 100644
--- a/src/c_init.c
+++ b/src/c_init.c
@@ -11,12 +11,12 @@
 /* ************************************************************************** */
 
 #include <unistd.h>
-#include <stdlib.h>
 #include <libft.h>
 #include <signal.h>
 #include <string.h>
 #include <term.h>
 
+#include "b_exit.h"
 #include "c_ctrls.h"
 #include "c_init.h"
 #include "c_keys.h"
@@ -24,14 +24,10 @@
 #include "c_input.h"
 #include "c_utils.h"
 #include "d_define.h"
-#include "b_exit.h"
+#include "m_prompt.h"
 #include "s_struct.h"
 #include "s_com.h"
 #include "s_line.h"
-#include "s_destroy.h"
-#include "m_prompt.h"
-#include "m_loop.h"
-#include "u_utils.h"
 
 short
 	c_set_term_raw(char mode)
@@ -57,25 +53,9 @@ short
 	return (1);
 }
 
-static void
-	c_add_char(char **line, char *buf, t_caps *tcaps, t_msh *msh)
-{
-	*line = c_insert_char(*line, buf[0], tcaps);
-	tcaps->cpos++;
-	if ((((tcaps->cpos) + tcaps->plen) %
-		tcaps->ws.ws_col) == 0)
-	{
-		write(STDOUT_FILENO, "\n\r", 2);
-		return ;
-	}
-	c_redraw_line(*line, tcaps, msh);
-}
-
 static short
 	c_read_cap(char *buf, char *line, t_caps *tcaps, t_msh *msh)
 {
-	int tmp;
-
 	if (((*((unsigned int *)buf)) == LEFT_K) ||
 		((*((unsigned int *)buf)) == CTRL_B))
 		return (c_key_left(tcaps->plen, tcaps));
@@ -92,18 +72,8 @@ static short
 		return (c_ctrl_l(line, tcaps, msh));
 	else if ((*((unsigned int *)buf)) == CTRL_D &&
 			line[0] == '\0')
-	{
-		c_set_term_raw(0);
-		write(STDERR_FILENO, "exit\n", 5);
-		u_eof_fd(msh->fd);
-		m_dump_hist(msh->curr_hist_ptr, msh);
-		tmp = msh->ret;
-		s_destroy(msh);
-		exit(tmp);
-		return (0);
-	}
-	else
-		return (0);
+		return (c_ctrl_d(msh));
+	return (0);
 }
 
 char	*ft_strrbavchr(const char *s, int c, size_t start)
@@ -159,48 +129,45 @@ char	*c_set_ptr(t_bool reset, char key, t_msh *msh)
 	return (p);
 }
 
+/* TODO : key_up_down should manage and set the correct key */
+
 static char
-	*c_process_key(char *buf, t_caps *tcaps, t_msh *msh)
+	*c_process_key(char **line, char *buf, t_caps *tcaps, t_msh *msh)
 {
-	static char	*line = NULL;
-	int			i;
 	char		*ptr;
 
-	i = -1;
-	if (line == NULL)
-		if (!(line = ft_calloc(1, sizeof(char))))
+	if (*line == NULL)
+		if (!(*line = ft_calloc(1, sizeof(char))))
 			return (NULL);
 	if (ft_isprint(buf[0]))
-		c_add_char(&line, buf, tcaps, msh);
+		c_add_char(line, buf, tcaps);
 	else if (((*((unsigned int *)buf)) == UP_K) ||
 			((*((unsigned int *)buf)) == CTRL_P))
 	{
 		ptr = c_set_ptr(FALSE, -1, msh);
-		c_key_up_down(&line, tcaps, ptr);
-		c_redraw_line(line, tcaps, msh);
+		c_key_up_down(line, tcaps, ptr);
+		c_redraw_line(*line, tcaps);
 	}
 	else if (((*((unsigned int *)buf)) == DOWN_K) ||
 			((*((unsigned int *)buf)) == CTRL_N))
 	{
 		ptr = c_set_ptr(FALSE, 1, msh);
-		c_key_up_down(&line, tcaps, ptr);
-		c_redraw_line(line, tcaps, msh);
+		c_key_up_down(line, tcaps, ptr);
+		c_redraw_line(*line, tcaps);
 	}
 	else if ((*((unsigned int *)buf)) == DEL_K)
 	{
-		c_back_slash(&line, tcaps->plen, tcaps);
+		c_back_slash(line, tcaps->plen, tcaps);
 		if ((((tcaps->cpos) + tcaps->plen) %
 			tcaps->ws.ws_col) == 0)
-		{
 			return (NULL);
-		}
-		c_redraw_line(line, tcaps, msh);
+		c_redraw_line(*line, tcaps);
 	}
 	else if ((*((unsigned int *)buf)) == CTRL_C)
-		return (c_ctrl_c(&line, buf, msh));
+		return (c_ctrl_c(line, buf, msh));
 	else
-		c_read_cap(buf, line, tcaps, msh);
-	return ((buf[0] == '\n') ? c_new_line(line, tcaps) : NULL);
+		c_read_cap(buf, *line, tcaps, msh);
+	return ((buf[0] == '\n') ? c_new_line(tcaps) : NULL);
 }
 
 short
@@ -215,6 +182,7 @@ short
 	ft_bzero(nread, 4);
 	msh->sig = 0;
 	tcaps.plen = (unsigned int)m_plen(msh->ps[psx - 1]);
+	*line = NULL;
 	if (!(c_get_win_size(&tcaps.ws)))
 		return (-1);
 	while (!(ft_strchr(nread, '\n')))
@@ -223,7 +191,7 @@ short
 		if (!(read(fd, nread, 3)))
 			return (0);
 		else
-			*line = c_process_key(nread, &tcaps, msh);
+			c_process_key(line, nread, &tcaps, msh);
 		tputs(tgetstr("ve", NULL), 1, ft_putchar);
 	}
 	return (1);
diff --git a/src/c_keys.c b/src/c_keys.c
index f0e67fd..3687a84 100644
--- a/src/c_keys.c
+++ b/src/c_keys.c
@@ -37,7 +37,7 @@ short
 	}
 	else if (i >= 1)
 	{
-		while (--i >= 0)
+		while (--i > 0)
 			tputs(tgetstr("le", NULL), 1, ft_putchar);
 	}
 	tcaps->cpos = 0;
diff --git a/src/c_utils.c b/src/c_utils.c
index 95ad28d..10013d7 100644
--- a/src/c_utils.c
+++ b/src/c_utils.c
@@ -21,34 +21,63 @@
 #include "m_loop.h"
 #include "s_struct.h"
 
-char
-	*c_new_line(char *line, t_caps *tcaps)
+static unsigned int
+	c_redraw_next(size_t plen, unsigned int len, t_caps *tcaps)
 {
-	char	*ret;
-	size_t	i;
+	unsigned int	i;
+	unsigned int	j;
 
-	i = tcaps->lpos;
-	ret = NULL;
-	write(1, "\n", 1);
-	tputs(tgetstr("cr", NULL), 1, ft_putchar);
-	c_set_term_raw(0);
-	if (line != NULL)
+	i = (tcaps->nlines == tcaps->lpos) ? 0 :
+		tcaps->ws.ws_col * (tcaps->lpos - 1);
+	j = tcaps->nlines;
+	if (tcaps->nlines != tcaps->lpos)
 	{
-		ret = ft_strdup(line);
-		line[0] = '\0';
+		while (--j > tcaps->lpos)
+			tputs(tgetstr("up", NULL), 1, ft_putchar);
+		tputs(tgetstr("up", NULL), 1, ft_putchar);
+		tputs(tgetstr("cr", NULL), 1, ft_putchar);
+		while (i < (tcaps->cpos + plen))
+		{
+			tputs(tgetstr("nd", NULL), 1, ft_putchar);
+			i++;
+		}
 	}
-	tcaps->cpos = 0;
-	tputs(tgetstr("cr", NULL), 1, ft_putchar);
-	if (i < tcaps->nlines && tcaps->nlines != 1)
-		while (i++ < tcaps->nlines)
+	else
+		while (len > tcaps->cpos)
 		{
-			write(1, "\n", 1);
+			tputs(tgetstr("le", NULL), 1, ft_putchar);
+			len--;
 		}
-	return (ret);
+	return (len);
 }
 
-char
-	*c_insert_char(char *str, char c, t_caps *tcaps)
+unsigned int
+	c_redraw_line(char *line, t_caps *tcaps)
+{
+	unsigned int	i;
+	unsigned int	j;
+
+	tcaps->nlines = c_get_line_num(line, tcaps->cpos,
+		tcaps->plen, tcaps);
+	i = 0;
+	j = tcaps->lpos;
+	tputs(tgetstr("cr", NULL), 1, ft_putchar);
+	while (--j > 0)
+	{
+		tputs(tgetstr("sf", NULL), 1, ft_putchar);
+		tputs(tgetstr("up", NULL), 1, ft_putchar);
+		tputs(tgetstr("up", NULL), 1, ft_putchar);
+	}
+	while (i++ < tcaps->plen)
+		tputs(tgetstr("nd", NULL), 1, ft_putchar);
+	ft_printf("%s", line);
+	return ((tcaps->cpos != ft_strlen(line)) ?
+		c_redraw_next(tcaps->plen, ft_strlen(line), tcaps) :
+		ft_strlen(line));
+}
+
+void
+	c_add_char(char **line, char *buf, t_caps *tcaps)
 {
 	int		i;
 	size_t	j;
@@ -57,21 +86,24 @@ char
 
 	i = -1;
 	j = 0;
-	len = (ft_strlen(str) + 2);
-	if (!(dst = (char*)malloc((len) * sizeof(char))))
-		return (NULL);
+	len = (ft_strlen(*line) + 2);
+	if (!(dst = (char*)ft_calloc(sizeof(char), len)))
+		return ;
 	while (++i < (int)len)
-	{
 		if (i == (int)tcaps->cpos)
-			dst[i] = c;
+			dst[i] = buf[0];
 		else
-		{
-			dst[i] = str[j];
-			j++;
-		}
+			dst[i] = line[0][j++];
+	ft_memdel((void**)&line[0]);
+	*line = ft_strdup(dst);
+	ft_memdel((void**)&dst);
+	tcaps->cpos++;
+	if ((((tcaps->cpos) + tcaps->plen) % tcaps->ws.ws_col) == 0)
+	{
+		write(STDOUT_FILENO, "\n\r", 2);
+		return ;
 	}
-	ft_memdel((void*)&str);
-return (dst);
+	c_redraw_line(*line, tcaps);
 }
 
 char
@@ -99,61 +131,20 @@ char
 	return (dst);
 }
 
-static short
-	c_redraw_next(size_t plen, unsigned int len, t_caps *tcaps)
+void
+	c_new_line(t_caps *tcaps)
 {
-	unsigned int	i;
-	unsigned int	j;
+	size_t	i;
 
-	i = (tcaps->nlines == tcaps->lpos) ? 0 :
-		tcaps->ws.ws_col * (tcaps->lpos - 1);
-	j = tcaps->nlines;
-	if (tcaps->nlines != tcaps->lpos)
-	{
-		while (--j > tcaps->lpos)
-			tputs(tgetstr("up", NULL), 1, ft_putchar);
-		tputs(tgetstr("up", NULL), 1, ft_putchar);
-		tputs(tgetstr("cr", NULL), 1, ft_putchar);
-		while (i < (tcaps->cpos + plen))
-		{
-			tputs(tgetstr("nd", NULL), 1, ft_putchar);
-			i++;
-		}
-	}
-	else
-		while (len > tcaps->cpos)
+	i = tcaps->lpos;
+	write(1, "\n", 1);
+	tputs(tgetstr("cr", NULL), 1, ft_putchar);
+	c_set_term_raw(0);
+	tcaps->cpos = 0;
+	tputs(tgetstr("cr", NULL), 1, ft_putchar);
+	if (i < tcaps->nlines && tcaps->nlines != 1)
+		while (i++ < tcaps->nlines)
 		{
-			tputs(tgetstr("le", NULL), 1, ft_putchar);
-			len--;
+			write(1, "\n", 1);
 		}
-	return (len);
-}
-
-short
-	c_redraw_line(char *line, t_caps *tcaps, t_msh *msh)
-{
-	unsigned int	i;
-	unsigned int	j;
-	size_t			len;
-
-	(void)msh;
-	tcaps->nlines = c_get_line_num(line, tcaps->cpos,
-		tcaps->plen, tcaps);
-	i = 0;
-	j = tcaps->lpos;
-	tputs(tgetstr("cr", NULL), 1, ft_putchar);
-	while (--j > 0)
-	{
-		tputs(tgetstr("sf", NULL), 1, ft_putchar);
-		tputs(tgetstr("up", NULL), 1, ft_putchar);
-		tputs(tgetstr("up", NULL), 1, ft_putchar);
-	}
-	while (i++ < tcaps->plen)
-		tputs(tgetstr("nd", NULL), 1, ft_putchar);
-	ft_printf("%s", line);
-	len = ft_strlen(line);
-	/* ft_memdel((void*)&line); */
-	return ((tcaps->cpos != len) ?
-		c_redraw_next(tcaps->plen, len, tcaps) :
-		len);
 }
diff --git a/src/c_utils.h b/src/c_utils.h
index f33c961..549c536 100644
--- a/src/c_utils.h
+++ b/src/c_utils.h
@@ -15,9 +15,9 @@
 
 # include "s_struct.h"
 
-char		*c_new_line(char *line, t_caps *tcaps);
-char		*c_insert_char(char *str, char c, t_caps *tcaps);
+void		c_add_char(char **line, char *buf, t_caps *tcaps);
+char		*c_new_line(t_caps *tcaps);
 char		*c_delchar(char *str, unsigned short cpos);
-short		c_redraw_line(char *line, t_caps *tcaps, t_msh *msh);
+short		c_redraw_line(char *line, t_caps *tcaps);
 
 #endif
diff --git a/src/p_redirs.c b/src/p_redirs.c
index b1e6914..8b38f44 100644
--- a/src/p_redirs.c
+++ b/src/p_redirs.c
@@ -129,7 +129,7 @@ static unsigned char	p_get_redir(char word[],
 	fd_rdr[RDR] = (fd_rdr[RDR] == -2 && *(ptr + 2) == '-') ? (-3) : (fd_rdr[1]);
 	pos[0] = (fd_rdr[FD] == STDOUT_FILENO) ?
 		(ptr - word) : ((ptr - word) - ft_intlen(fd_rdr[FD]));
-	pos[0] = (fd_rdr[FD] == -2) ? ((ptr - word) - 1) : (pos[0]);
+	pos[0] = (fd_rdr[FD] == -2) ? (size_t)((ptr - word) - 1) : (pos[0]);
 	fd_rdr[FD] = (fd_rdr[FD] == -2) ? (STDOUT_FILENO) : (fd_rdr[FD]);
 	tmp.word = word;
 	tmp.ptr = ptr;
-- 
cgit v1.2.3