summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-11-02 15:08:38 +0100
committerJozanLeClerc <bousset.rudy@gmail.com>2020-11-02 15:08:38 +0100
commite0a74558be8d71054ae3c8e6afe8774fc1890b84 (patch)
tree57bc32451216e00fac58bd8f79eee12543badf56
parentNo more fixed int (diff)
download42-minishell-e0a74558be8d71054ae3c8e6afe8774fc1890b84.tar.gz
42-minishell-e0a74558be8d71054ae3c8e6afe8774fc1890b84.tar.bz2
42-minishell-e0a74558be8d71054ae3c8e6afe8774fc1890b84.tar.xz
42-minishell-e0a74558be8d71054ae3c8e6afe8774fc1890b84.tar.zst
42-minishell-e0a74558be8d71054ae3c8e6afe8774fc1890b84.zip
Caps files
-rw-r--r--src/c_init.c149
-rw-r--r--src/c_init.h49
-rw-r--r--src/c_input.c92
-rw-r--r--src/c_input.h23
-rw-r--r--src/c_keys.c125
-rw-r--r--src/c_keys.h22
-rw-r--r--src/c_utils.c155
-rw-r--r--src/c_utils.h23
8 files changed, 638 insertions, 0 deletions
diff --git a/src/c_init.c b/src/c_init.c
new file mode 100644
index 0000000..77d3023
--- /dev/null
+++ b/src/c_init.c
@@ -0,0 +1,149 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* u_init.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <libft.h>
+#include <signal.h>
+#include <term.h>
+
+#include "c_init.h"
+#include "c_keys.h"
+#include "c_input.h"
+#include "c_utils.h"
+#include "s_struct.h"
+#include "m_prompt.h"
+
+int16_t
+ c_set_term_raw(uint8_t mode)
+{
+ struct termios tios;
+
+ ft_memset(&tios, 0, sizeof(tios));
+ tcgetattr(STDIN_FILENO, &tios);
+ if (mode)
+ {
+ tios.c_lflag &= ~(ECHO | ICANON | ISIG);
+ tios.c_oflag &= ~(OPOST);
+ tios.c_cc[VMIN] = 1;
+ tios.c_cc[VTIME] = 0;
+ }
+ else
+ {
+ tios.c_lflag |= (ECHO | ICANON | ISIG);
+ tios.c_oflag |= (OPOST);
+ }
+ tcsetattr(STDIN_FILENO, TCSAFLUSH, &tios);
+ mode ? tputs(tgetstr("ns", NULL), 1, ft_putchar) : 0;
+ 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) + ft_strlen(msh->ps[tcaps->psx])) %
+ tcaps->ws.ws_col) == 0)
+ {
+ write(1, "\n\r", 2);
+ return ;
+ }
+ c_redraw_line(*line, tcaps, msh);
+}
+
+static int16_t
+ c_read_cap(char *buf, char *line, t_caps *tcaps, t_msh *msh)
+{
+ uint32_t plen;
+
+ plen = ft_strlen(msh->ps[tcaps->psx]);
+ if (((*((unsigned int *)buf)) == LEFT_K) ||
+ ((*((unsigned int *)buf)) == CTRL_B))
+ return (c_key_left(plen, tcaps));
+ else if (((*((unsigned int *)buf)) == RIGHT_K) ||
+ ((*((unsigned int *)buf)) == CTRL_F))
+ return (c_key_right(ft_strlen(line), plen, tcaps));
+ else if (((*((unsigned int *)buf)) == HOME_K) ||
+ ((*((unsigned int *)buf)) == CTRL_A))
+ return (c_home_key(plen, tcaps));
+ else if (((*((unsigned int *)buf)) == END_K) ||
+ ((*((unsigned int *)buf)) == CTRL_E))
+ return (c_end_key(ft_strlen(line), plen, tcaps));
+ else if ((*((unsigned int *)buf)) == CTRL_L)
+ return (c_ctrl_l(line, tcaps, msh));
+ else if ((*((unsigned int *)buf)) == CTRL_C)
+ {
+ msh->ret = 130;
+ return (1);
+ }
+ else
+ return (0);
+}
+
+static char
+ *c_process_key(char *buf, t_caps *tcaps, t_msh *msh)
+{
+ static char *line = NULL;
+ int i;
+
+ i = -1;
+ if (line == NULL)
+ if (!(line = ft_calloc(1, sizeof(char))))
+ return (NULL);
+ if (ft_isprint(buf[0]))
+ c_add_char(&line, buf, tcaps, msh);
+ else if ((*((unsigned int *)buf)) == DEL_K)
+ {
+ c_back_slash(&line, ft_strlen(msh->ps[tcaps->psx]), tcaps);
+ if ((((tcaps->cpos) + ft_strlen(msh->ps[tcaps->psx])) %
+ tcaps->ws.ws_col) == 0)
+ {
+ return (NULL);
+ }
+ c_redraw_line(line, tcaps, msh);
+ }
+ else
+ {
+ c_read_cap(buf, line, tcaps, msh);
+ }
+ return ((buf[0] == '\n') ? c_new_line(line, tcaps) : NULL);
+}
+
+int16_t
+ c_gnl(int32_t fd, char **line, uint8_t psx, t_msh *msh)
+{
+ t_caps tcaps;
+ char nread[5];
+
+ c_init_line(psx, &tcaps);
+ tputs(tgetstr("cr", NULL), 1, ft_putchar);
+ m_prompt_psx(psx, msh);
+ ft_bzero(nread, 5);
+ if (!(c_get_win_size(&tcaps.ws)))
+ return (-1);
+ while (!(ft_strchr(nread, '\n')))
+ {
+ if (msh->ret == 130)
+ {
+ *line = ft_strdup("\0");
+ return (1);
+ }
+ ft_bzero(nread, 5);
+ if (!(read(fd, nread, 4)))
+ return (0);
+ else
+ *line = c_process_key(nread, &tcaps, msh);
+ tputs(tgetstr("ve", NULL), 1, ft_putchar);
+ }
+ return (1);
+}
diff --git a/src/c_init.h b/src/c_init.h
new file mode 100644
index 0000000..e723563
--- /dev/null
+++ b/src/c_init.h
@@ -0,0 +1,49 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* c_init.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef C_INIT_H
+# define C_INIT_H
+
+# include <termios.h>
+# include <sys/ioctl.h>
+
+# include "s_struct.h"
+
+# define LEFT_K 0x445b1b
+# define RIGHT_K 0x435b1b
+# define HOME_K 0x485b1b
+# define END_K 0x7e345b1b
+# define DEL_K 0x7f
+# define RET_K 0x0d
+
+# define CTRL_A 0x01
+# define CTRL_B 0x02
+# define CTRL_C 0x03
+# define CTRL_D 0x04
+# define CTRL_E 0x05
+# define CTRL_F 0x06
+# define CTRL_L 0x0c
+
+typedef struct s_caps
+{
+ struct termios tios;
+ struct winsize ws;
+ uint32_t cpos;
+ uint32_t lpos;
+ uint32_t nlines;
+ uint8_t psx;
+} t_caps;
+
+int16_t c_gnl(int32_t fd, char **line, uint8_t psx, t_msh *msh);
+int16_t c_set_term_raw(uint8_t mode);
+
+#endif
diff --git a/src/c_input.c b/src/c_input.c
new file mode 100644
index 0000000..8fb4b72
--- /dev/null
+++ b/src/c_input.c
@@ -0,0 +1,92 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* c_input.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <libft.h>
+#include <signal.h>
+#include <term.h>
+
+#include "c_init.h"
+#include "c_keys.h"
+#include "c_input.h"
+#include "c_utils.h"
+#include "m_prompt.h"
+#include "m_loop.h"
+
+int16_t
+ c_init_line(uint8_t psx, t_caps *tcaps)
+{
+ char *term;
+
+ if (tcaps)
+ {
+ term = getenv("TERM");
+ if (!tgetent(NULL, term))
+ return (-1);
+ c_set_term_raw(1);
+ signal(SIGINT, SIG_IGN);
+ tcaps->cpos = 0;
+ tcaps->lpos = 0;
+ tcaps->nlines = 1;
+ tcaps->psx = psx - 1;
+ return (1);
+ }
+ else
+ return (-1);
+}
+
+uint16_t
+ c_get_win_size(struct winsize *ws)
+{
+ if (!(ioctl(STDOUT_FILENO, TIOCGWINSZ, ws)))
+ return (-1);
+ if (!ws->ws_col)
+ ws->ws_col = 80;
+ if (!ws->ws_row)
+ ws->ws_row = 80;
+ return (1);
+}
+
+uint32_t
+ c_get_line_num(char *line, uint32_t cpos, uint32_t plen, t_caps *tcaps)
+{
+ uint32_t it;
+ uint32_t line_num;
+ uint32_t len;
+
+ it = 0;
+ line_num = 0;
+ tcaps->lpos = 1;
+ len = ft_strlen(line);
+ if ((len) < (tcaps->ws.ws_col - plen))
+ return (1);
+ while (it < len)
+ {
+ it += (it == 0) ? (tcaps->ws.ws_col - plen) : tcaps->ws.ws_col;
+ tcaps->lpos += (it < cpos) ? 1 : 0;
+ line_num++;
+ }
+ return (line_num);
+}
+
+int16_t
+ c_back_slash(char **line, uint32_t plen, t_caps *tcaps)
+{
+ if (tcaps->cpos >= 1)
+ {
+ *line = c_delchar(*line, tcaps->cpos);
+ c_key_left(plen, tcaps);
+ tputs(tgetstr("cd", NULL), 1, ft_putchar);
+ }
+ return (1);
+}
diff --git a/src/c_input.h b/src/c_input.h
new file mode 100644
index 0000000..f22166a
--- /dev/null
+++ b/src/c_input.h
@@ -0,0 +1,23 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* c_input.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef C_INPUT_H
+# define C_INPUT_H
+
+int16_t c_back_slash(char **line, uint32_t plen, t_caps *tcaps);
+int16_t c_ctrl_c(char *line, t_caps *tcaps, t_msh *msh);
+uint32_t c_get_line_num(char *line, uint32_t cpos, uint32_t plen,
+ t_caps *tcaps);
+int16_t c_init_line(uint8_t psx, t_caps *tcaps);
+uint16_t c_get_win_size(struct winsize *ws);
+
+#endif
diff --git a/src/c_keys.c b/src/c_keys.c
new file mode 100644
index 0000000..e0f734c
--- /dev/null
+++ b/src/c_keys.c
@@ -0,0 +1,125 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* c_keys.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#include <term.h>
+#include <libft.h>
+
+#include "c_init.h"
+#include "c_utils.h"
+#include "m_prompt.h"
+
+int16_t
+ 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);
+ return (1);
+}
+
+int16_t
+ c_home_key(uint32_t plen, t_caps *tcaps)
+{
+ uint32_t i;
+ int16_t j;
+
+ i = tcaps->cpos;
+ j = tcaps->lpos;
+ if (tcaps->cpos > 0 && j > 0)
+ {
+ i = 0;
+ while (--j > 0)
+ tputs(tgetstr("up", NULL), 1, ft_putchar);
+ tputs(tgetstr("cr", NULL), 1, ft_putchar);
+ while (++i <= plen)
+ tputs(tgetstr("nd", NULL), 1, ft_putchar);
+ }
+ else if (i >= 1)
+ {
+ while (--i >= 0)
+ tputs(tgetstr("le", NULL), 1, ft_putchar);
+ }
+ tcaps->cpos = 0;
+ tcaps->lpos = 1;
+ return (1);
+}
+
+int16_t
+ c_end_key(uint16_t size, uint32_t plen, t_caps *tcaps)
+{
+ uint16_t i;
+ uint16_t j;
+
+ i = tcaps->cpos;
+ j = tcaps->lpos;
+ if ((tcaps->cpos < size) && (j < tcaps->nlines))
+ {
+ i = 0;
+ while (j < tcaps->nlines)
+ {
+ tputs(tgetstr("do", NULL), 1, ft_putchar);
+ i += tcaps->ws.ws_col;
+ j++;
+ }
+ tputs(tgetstr("cr", NULL), 1, ft_putchar);
+ while (i++ < (size + plen))
+ tputs(tgetstr("nd", NULL), 1, ft_putchar);
+ }
+ else
+ while (i++ < size)
+ tputs(tgetstr("nd", NULL), 1, ft_putchar);
+ tcaps->cpos = size;
+ tcaps->lpos = tcaps->nlines;
+ return (1);
+}
+
+int16_t
+ c_key_right(uint32_t len, uint32_t plen, t_caps *tcaps)
+{
+ if ((((tcaps->cpos + plen + 1) % (tcaps->ws.ws_col)) == 0) &&
+ tcaps->cpos < len)
+ {
+ tputs(tgetstr("do", NULL), 1, ft_putchar);
+ tputs(tgetstr("cr", NULL), 1, ft_putchar);
+ tcaps->cpos++;
+ tcaps->lpos++;
+ }
+ else if (tcaps->cpos < len)
+ {
+ tputs(tgetstr("nd", NULL), 1, ft_putchar);
+ tcaps->cpos++;
+ }
+ return (1);
+}
+
+int16_t
+ c_key_left(uint32_t plen, t_caps *tcaps)
+{
+ int32_t pos;
+
+ pos = -1;
+ if (((tcaps->cpos + plen) % tcaps->ws.ws_col) == 0 &&
+ tcaps->cpos >= 1)
+ {
+ tputs(tgetstr("up", NULL), 1, ft_putchar);
+ while (++pos <= tcaps->ws.ws_col)
+ tputs(tgetstr("nd", NULL), 1, ft_putchar);
+ tcaps->cpos--;
+ tcaps->lpos--;
+ }
+ else if (tcaps->cpos >= 1)
+ {
+ tputs(tgetstr("le", NULL), 1, ft_putchar);
+ tcaps->cpos--;
+ }
+ return (1);
+}
diff --git a/src/c_keys.h b/src/c_keys.h
new file mode 100644
index 0000000..12b17ee
--- /dev/null
+++ b/src/c_keys.h
@@ -0,0 +1,22 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* c_keys.h :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef C_KEYS_H
+# define C_KEYS_H
+
+int16_t c_key_right(uint32_t len, uint32_t plen, t_caps *tcaps);
+int16_t c_key_left(uint32_t plen, t_caps *tcaps);
+int16_t c_home_key(uint32_t plen, t_caps *tcaps);
+int16_t c_end_key(uint16_t size, uint32_t plen, t_caps *tcaps);
+int16_t c_ctrl_l(char *line, t_caps *tcaps, t_msh *msh);
+
+#endif
diff --git a/src/c_utils.c b/src/c_utils.c
new file mode 100644
index 0000000..043bd27
--- /dev/null
+++ b/src/c_utils.c
@@ -0,0 +1,155 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* c_utils.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#include <libft.h>
+#include <stdlib.h>
+#include <term.h>
+#include <unistd.h>
+
+#include "c_init.h"
+#include "c_input.h"
+#include "m_prompt.h"
+#include "m_loop.h"
+#include "s_struct.h"
+
+char
+ *c_new_line(char *line, t_caps *tcaps)
+{
+ char *ret;
+ size_t i;
+
+ i = tcaps->lpos;
+ ret = NULL;
+ write(1, "\n", 1);
+ tputs(tgetstr("cr", NULL), 1, ft_putchar);
+ c_set_term_raw(0);
+ if (line != NULL)
+ {
+ ret = ft_strdup(line);
+ line[0] = '\0';
+ }
+ tcaps->cpos = 0;
+ tputs(tgetstr("cr", NULL), 1, ft_putchar);
+ if (i < tcaps->nlines && tcaps->nlines != 1)
+ while (i++ < tcaps->nlines)
+ {
+ write(1, "\n", 1);
+ }
+ return (ret);
+}
+
+char
+ *c_insert_char(char *str, char c, t_caps *tcaps)
+{
+ int32_t i;
+ size_t j;
+ size_t len;
+ char *dst;
+
+ i = -1;
+ j = 0;
+ len = (ft_strlen(str) + 2);
+ if (!(dst = (char*)malloc((len) * sizeof(char))))
+ return (NULL);
+ while (++i < (int32_t)len)
+ {
+ if (i == (int32_t)tcaps->cpos)
+ dst[i] = c;
+ else
+ {
+ dst[i] = str[j];
+ j++;
+ }
+ }
+ ft_memdel((void**)&str);
+ return (dst);
+}
+
+char
+ *c_delchar(char *str, uint16_t cpos)
+{
+ char *dst;
+ uint16_t i;
+ uint16_t j;
+
+ j = 0;
+ i = 0;
+ if (!(dst = malloc((ft_strlen(str)) * sizeof(char))))
+ return (NULL);
+ cpos -= 1;
+ while (j < (ft_strlen(str) - 1))
+ {
+ if (i == cpos)
+ i++;
+ dst[j] = str[i];
+ i++;
+ j++;
+ }
+ ft_memdel((void**)&str);
+ dst[j] = '\0';
+ return (dst);
+}
+
+static int16_t
+ c_redraw_next(size_t plen, uint32_t len, t_caps *tcaps)
+{
+ uint32_t i;
+ uint32_t j;
+
+ 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)
+ {
+ tputs(tgetstr("le", NULL), 1, ft_putchar);
+ len--;
+ }
+ return (len);
+}
+
+int16_t
+ c_redraw_line(char *line, t_caps *tcaps, t_msh *msh)
+{
+ uint32_t i;
+ uint32_t j;
+
+ tcaps->nlines = c_get_line_num(line, tcaps->cpos,
+ ft_strlen(msh->ps[tcaps->psx]), 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++ < ft_strlen(msh->ps[tcaps->psx]))
+ tputs(tgetstr("nd", NULL), 1, ft_putchar);
+ ft_printf("%s", line);
+ return ((tcaps->cpos != ft_strlen(line)) ?
+ c_redraw_next(ft_strlen(msh->ps[tcaps->psx]), ft_strlen(line), tcaps) :
+ ft_strlen(line));
+}
diff --git a/src/c_utils.h b/src/c_utils.h
new file mode 100644
index 0000000..730d29a
--- /dev/null
+++ b/src/c_utils.h
@@ -0,0 +1,23 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* c_utils.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
+/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
+/* */
+/* ************************************************************************** */
+
+#ifndef C_UTILS_H
+# define C_UTILS_H
+
+# include "s_struct.h"
+
+char *c_new_line(char *line, t_caps *tcaps);
+char *c_insert_char(char *str, char c, t_caps *tcaps);
+char *c_delchar(char *str, uint16_t cpos);
+int16_t c_redraw_line(char *line, t_caps *tcaps, t_msh *msh);
+
+#endif