summaryrefslogtreecommitdiffstats
path: root/src/c_init.c
diff options
context:
space:
mode:
authorsalad <fmoenne-@student.le-101.fr>2020-09-02 13:45:33 +0200
committersalad <fmoenne-@student.le-101.fr>2020-09-02 13:45:33 +0200
commit72becbb81c5ac3f2052674da5c851aa465c29d17 (patch)
tree41170fd3aa1be1013b313cf9d1882c7f226dc935 /src/c_init.c
parentit all failed ? (diff)
download42-minishell-72becbb81c5ac3f2052674da5c851aa465c29d17.tar.gz
42-minishell-72becbb81c5ac3f2052674da5c851aa465c29d17.tar.bz2
42-minishell-72becbb81c5ac3f2052674da5c851aa465c29d17.tar.xz
42-minishell-72becbb81c5ac3f2052674da5c851aa465c29d17.tar.zst
42-minishell-72becbb81c5ac3f2052674da5c851aa465c29d17.zip
HOLY SHIT MAN
Diffstat (limited to 'src/c_init.c')
-rw-r--r--src/c_init.c119
1 files changed, 61 insertions, 58 deletions
diff --git a/src/c_init.c b/src/c_init.c
index ee4d0a7..2c0b7c3 100644
--- a/src/c_init.c
+++ b/src/c_init.c
@@ -16,7 +16,7 @@
#include <stdlib.h>
#include <string.h>
#include <term.h>
-#include <curses.h>
+#include <libft.h>
#include "c_init.h"
@@ -32,39 +32,52 @@ t_caps
return (caps);
}
-void c_process_key(t_caps *tcaps) {
- char nread[64] = {0};
- int bread;
+int16_t
+ c_set_term_raw(uint8_t mode)
+{
+ struct termios tios;
- while ((bread = read(STDIN_FILENO, nread, 3) != 1)) {
- if (bread == -1)
- {
- printf("error on read");
- return ;
- }
- }
+ 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);
+ return (1);
+}
- if (*nread == 'q')
- exit(0);
- else if ((strncmp(nread, "l", 1) == 0) ||
- strncmp(nread, tcaps->kr, 3) == 0)
- {
- tputs(tgoto(tcaps->nd, 1, 1), 1, putchar);
- }
- else if (strncmp(nread, "c", 1) == 0)
- write(1, tcaps->cl, strlen(tcaps->cl));
- else if (strncmp(nread, "j", 1) == 0)
- write(1, tcaps->DO, strlen(tcaps->DO));
- else if (strncmp(nread, "k", 1) == 0)
+int16_t c_process_key(char *buf)
+{
+ int i;
+
+ /* tputs(tgetstr("cl", NULL), 1, ft_putchar); */
+ i = 0;
+
+ if (ft_isprint(buf[0]))
{
- write(1, tcaps->up, strlen(tcaps->kl));
+ if (*buf == 'q')
+ exit(0);
+ write(1, buf, 1);
}
- else if (strncmp(nread, "h", 1) == 0)
+ else
{
- tputs(tgoto(tcaps->le, 1, 1), 1, putchar);
+ ft_printf("escaped char inc,[%c] [%c] [%c]\n", buf[0], buf[1], buf[2]);
+ if(strncmp(buf, tgetstr("kr", NULL), ft_strlen(tgetstr("kr", NULL))) == 0)
+ {
+ tputs(tgetstr("nd", NULL), 1, ft_putchar);
+ return (1);
+ }
}
- else
- write(1, nread, 1);
+ return (0);
}
int16_t c_init_tcaps(void)
@@ -72,45 +85,35 @@ int16_t c_init_tcaps(void)
t_caps tcaps;
char *bp;
char *term;
+ char nread[5];
+ int ret;
bp = NULL;
term = getenv("TERM");
if (!tgetent(bp, term))
return (-1);
- if (!(tcaps.cl = tgetstr("cl", &term)))
- return (-1);
- if (!(tcaps.ks = tgetstr("ks", &term)))
- return (-1);
- if (!(tcaps.kl = tgetstr("kl", &term)))
- return (-1);
- if (!(tcaps.ke = tgetstr("kr", &term)))
- return (-1);
- if (!(tcaps.kr = tgetstr("kr", &term)))
- return (-1);
- if (!(tcaps.pc = tgetstr("pc", &term)))
- return (-1);
- if (!(tcaps.bc = tgetstr("bc", &term)))
- return (-1);
- if (!(tcaps.up = tgetstr("up", &term)))
- return (-1);
- if (!(tcaps.nd = tgetstr("nd", &term)))
- return (-1);
- if (!(tcaps.le = tgetstr("le", &term)))
- return (-1);
- if (!(tcaps.DO = tgetstr("do", &term)))
- return (-1);
- tcgetattr(STDIN_FILENO, &tcaps.tios);
+ /* tcaps.cl = tgetstr("cl", &term); */
+ /* tcaps.ks = tgetstr("ks", &term); */
+ /* tcaps.kl = tgetstr("kl", &term); */
+ /* tcaps.ke = tgetstr("ke", &term); */
+ /* tcaps.kr = tgetstr("kr", &term); */
+ /* tcaps.pc = tgetstr("pc", &term); */
+ /* tcaps.bc = tgetstr("bc", &term); */
+ /* tcaps.up = tgetstr("up", &term); */
+ /* tcaps.nd = tgetstr("nd", &term); */
+ /* tcaps.le = tgetstr("le", &term); */
+ /* tcaps.DO = tgetstr("do", &term); */
- printf("%d\n [%s]", KEY_LEFT, tcaps.ks);
- write(1, tcaps.ks, 1);
- tcaps.tios.c_lflag &= ~(ECHO | ICANON);
- tcaps.tios.c_cc[VMIN] = 0;
- tcaps.tios.c_cc[VTIME] = 1;
- tcsetattr(STDIN_FILENO, TCSAFLUSH, &tcaps.tios);
+ c_set_term_raw(1);
c_get_struct(1, &tcaps);
while (1)
{
- c_process_key(&tcaps);
+
+ ft_bzero(nread, 5);
+ if (!(read(STDIN_FILENO, nread, 4)))
+ return (0);
+ ret = c_process_key(nread);
}
+ ft_printf("%d\n", ret);
return (1);
}