summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Makefile1
-rw-r--r--src/c_init.c51
-rw-r--r--src/c_init.h1
-rw-r--r--src/c_input.c105
-rw-r--r--src/c_input.h12
-rw-r--r--src/c_utils.c91
-rw-r--r--src/c_utils.h23
7 files changed, 183 insertions, 101 deletions
diff --git a/Makefile b/Makefile
index 952d5d9..a721d71 100644
--- a/Makefile
+++ b/Makefile
@@ -27,6 +27,7 @@ SRCS_NAME += b_type
SRCS_NAME += b_unset
SRCS_NAME += c_init
SRCS_NAME += c_input
+SRCS_NAME += c_utils
SRCS_NAME += e_builtins
SRCS_NAME += e_externs
SRCS_NAME += e_externs_next
diff --git a/src/c_init.c b/src/c_init.c
index 35296e4..ad18d92 100644
--- a/src/c_init.c
+++ b/src/c_init.c
@@ -19,8 +19,7 @@
#include "c_init.h"
#include "c_input.h"
-#include "m_prompt.h"
-#include "m_loop.h"
+#include "c_utils.h"
#include "s_struct.h"
static char
@@ -49,7 +48,8 @@ static char
}
static t_caps
- *c_get_struct(int mode, t_caps *src)
+ *c_get_struct(int mode,
+ t_caps *src)
{
static t_caps *caps;
@@ -60,7 +60,7 @@ static t_caps
return (caps);
}
-static int16_t
+int16_t
c_set_term_raw(uint8_t mode)
{
struct termios tios;
@@ -85,7 +85,9 @@ static int16_t
}
int16_t
- c_process_key(char *buf, t_caps *tcaps, t_msh *msh)
+ c_process_key(char *buf,
+ t_caps *tcaps,
+ t_msh *msh)
{
static char *line = NULL;
int i;
@@ -102,55 +104,28 @@ int16_t
}
else
{
- /* if (!(c_catch_tcaps(buf, tcaps, msh)) */
- /* return (-1); */
if (strncmp(buf, tgetstr("kr", NULL), ft_strlen(tgetstr("kr", NULL))) == 0)
{
- if (tcaps->cpos <= ft_strlen(line))
- {
- tputs(tgetstr("nd", NULL), 1, ft_putchar);
- tcaps->cpos++;
- }
- return (1);
+ return (c_key_right(ft_strlen(line), tcaps));
}
else if (buf[0] == '\n')
{
- write(1, buf, ft_strlen(buf));
- tputs(tgetstr("cr", NULL), 1, ft_putchar);
- c_set_term_raw(0);
- m_parse_and_run_line(line, msh);
- line = NULL;
- tcaps->cpos = 0;
- tputs(tgetstr("cr", NULL), 1, ft_putchar);
- c_set_term_raw(1);
- m_prompt_psx(1, msh);
- return (1);
+ return (c_new_line(buf, &line, msh, tcaps));
}
else if (strncmp(buf, tgetstr("kl", NULL), ft_strlen(tgetstr("kl", NULL))) == 0)
{
- if (tcaps->cpos >= 1)
- {
- tputs(tgetstr("le", NULL), 1, ft_putchar);
- tcaps->cpos--;
- return (1);
- }
+ return (c_key_left(ft_strlen(line), tcaps));
}
else if (strncmp(buf, tgetstr("kb", NULL), ft_strlen(tgetstr("kb", NULL))) == 0)
{
- if (tcaps->cpos >= 1)
- {
- line = c_delchar(line, tcaps->cpos);
- tcaps->cpos -= 1;
- tputs(tgetstr("le", NULL), 1, ft_putchar);
- tputs(tgetstr("dc", NULL), 1, ft_putchar);
- }
- return (1);
+ return (c_back_slash(&line, tcaps));
}
}
return (0);
}
-int16_t c_init_tcaps(t_msh *msh)
+int16_t
+ c_init_tcaps(t_msh *msh)
{
t_caps tcaps;
char *bp;
diff --git a/src/c_init.h b/src/c_init.h
index 281bf72..1d298c4 100644
--- a/src/c_init.h
+++ b/src/c_init.h
@@ -38,6 +38,7 @@ typedef struct s_caps {
} t_caps;
int16_t c_init_tcaps(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
index a4b62ca..fa61c6a 100644
--- a/src/c_input.c
+++ b/src/c_input.c
@@ -11,76 +11,69 @@
/* ************************************************************************** */
#include <libft.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <term.h>
#include <unistd.h>
-#include <sys/ioctl.h>
+#include <term.h>
#include "c_init.h"
+#include "c_input.h"
+#include "c_utils.h"
#include "m_prompt.h"
#include "m_loop.h"
-#include "s_struct.h"
-/*
-** TODO:
-** 1 : cleanup
-** 2 : prompt on beginning
-** 3 : c_set_term(0) on command
-** 4 : MAKE IT WORK ON XTERM FFS
-** 5 : leaks
- */
-uint16_t
- c_get_win_size(struct winsize *ws)
+int16_t
+ c_new_line(char *buf,
+ char **line,
+ t_msh *msh,
+ t_caps *tcaps)
{
-
- 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);
+ write(1, buf, ft_strlen(buf));
+ tputs(tgetstr("cr", NULL), 1, ft_putchar);
+ c_set_term_raw(0);
+ m_parse_and_run_line(*line, msh);
+ *line = NULL;
+ tcaps->cpos = 0;
+ tputs(tgetstr("cr", NULL), 1, ft_putchar);
+ c_set_term_raw(1);
+ m_prompt_psx(1, msh);
+ return (1);
}
-char *c_delchar(char *str, uint16_t cpos)
+int16_t
+ c_back_slash(char **line,
+ t_caps *tcaps)
{
- 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++;
- }
- dst[j] = '\0';
- return (dst);
+ if (tcaps->cpos >= 1)
+ {
+ *line = c_delchar(*line, tcaps->cpos);
+ tcaps->cpos -= 1;
+ tputs(tgetstr("le", NULL), 1, ft_putchar);
+ tputs(tgetstr("dc", NULL), 1, ft_putchar);
+ }
+ return (1);
}
int16_t
- c_redraw_line(char *line, uint16_t cpos, t_msh *msh)
+ c_key_right(uint32_t len,
+ t_caps *tcaps)
{
- uint32_t i;
- int16_t ret;
+ if (tcaps->cpos < len)
+ {
+ tputs(tgetstr("nd", NULL), 1, ft_putchar);
+ tcaps->cpos++;
+ }
+ return (1);
+}
- i = 0;
- (void)cpos;
- tputs(tgetstr("cr", NULL), 1, ft_putchar);
- while (i < ft_strlen(msh->ps[0]))
- {
- tputs(tgetstr("nd", NULL), 1, ft_putchar);
- i++;
- }
- ret = ft_printf("%s", line) + ft_strlen(msh->ps[0]);
- return (ret);
+int16_t
+ c_key_left(uint32_t len,
+ t_caps *tcaps)
+{
+ (void)len;
+ if (tcaps->cpos >= 1)
+ {
+ tputs(tgetstr("le", NULL), 1, ft_putchar);
+ tcaps->cpos--;
+ }
+ return (1);
}
diff --git a/src/c_input.h b/src/c_input.h
index c340194..f6fa54d 100644
--- a/src/c_input.h
+++ b/src/c_input.h
@@ -1,7 +1,7 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
-/* c_input.c :+: :+: :+: */
+/* c_input.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
@@ -13,11 +13,9 @@
#ifndef C_INPUT_H
#define C_INPUT_H
-#include "s_struct.h"
-
-int16_t c_redraw_line(char *line, uint16_t cpos, t_msh *msh);
-char *c_delchar(char *str, uint16_t cpos);
-uint16_t c_get_win_size(struct winsize *ws);
-int32_t c_catch_tcaps(char *buf, char **line, t_caps *tcaps, t_msh *msh);
+int16_t c_key_right(uint32_t len, t_caps *tcaps);
+int16_t c_key_left(uint32_t len, t_caps *tcaps);
+int16_t c_new_line(char *buf, char **line, t_msh *msh, t_caps *tcaps);
+int16_t c_back_slash(char **line, t_caps *tcaps);
#endif
diff --git a/src/c_utils.c b/src/c_utils.c
new file mode 100644
index 0000000..3eb7f4d
--- /dev/null
+++ b/src/c_utils.c
@@ -0,0 +1,91 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* 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 <stdio.h>
+#include <stdlib.h>
+#include <term.h>
+#include <unistd.h>
+#include <sys/ioctl.h>
+
+#include "c_init.h"
+#include "m_prompt.h"
+#include "m_loop.h"
+#include "s_struct.h"
+
+/*
+** TODO:
+** 1 : cleanup
+** 2 : prompt on beginning
+** 3 : c_set_term(0) on command
+** 4 : MAKE IT WORK ON XTERM FFS (arrows + reset term)
+** 5 : leaks
+ */
+
+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);
+}
+
+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++;
+ }
+ free(str);
+ dst[j] = '\0';
+ return (dst);
+}
+
+int16_t
+ c_redraw_line(char *line,
+ uint16_t cpos,
+ t_msh *msh)
+{
+ uint32_t i;
+ int16_t ret;
+
+ i = 0;
+ (void)cpos;
+ tputs(tgetstr("cr", NULL), 1, ft_putchar);
+ while (i < ft_strlen(msh->ps[0]))
+ {
+ tputs(tgetstr("nd", NULL), 1, ft_putchar);
+ i++;
+ }
+ ret = ft_printf("%s", line) + ft_strlen(msh->ps[0]);
+ return (ret);
+}
diff --git a/src/c_utils.h b/src/c_utils.h
new file mode 100644
index 0000000..967939f
--- /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"
+
+int16_t c_redraw_line(char *line, uint16_t cpos, t_msh *msh);
+char *c_delchar(char *str, uint16_t cpos);
+uint16_t c_get_win_size(struct winsize *ws);
+int32_t c_catch_tcaps(char *buf, char **line, t_caps *tcaps, t_msh *msh);
+
+#endif