/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* u_init.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ /* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ /* */ /* ************************************************************************** */ #include #include #include #include #include #include #include #include "c_init.h" static char *ft_strjoin_m(char *s1, char *s2) { size_t i; size_t j; char *dst; size_t size1; size_t size2; i = -1; j = -1; size1 = ft_strlen(s1); size2 = ft_strlen(s2); if (!(dst = (char*)malloc((size1 + size2 + 1) * sizeof(char)))) return (NULL); while (++i < size1) dst[i] = s1[i]; while (++j < size2) dst[i + j] = s2[j]; dst[i + j] = '\0'; ft_memdel((void*)&s1); return (dst); } t_caps *c_get_struct(int mode, t_caps *src) { static t_caps *caps; if (mode == 1) { caps = src; } return (caps); } 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); return (1); } int16_t c_process_key(char *buf) { int i; static char *line; i = -1; if (line == NULL) if (!(line = ft_calloc(2, sizeof(char)))) return (0); if (ft_isprint(buf[0])) { if (*buf == 'q') exit(0); line = ft_strjoin_m(line, buf); ft_printf("line [%s][%lu]\n", line, ft_strlen(line)); } else { if(strncmp(buf, tgetstr("kr", NULL), ft_strlen(tgetstr("kr", NULL))) == 0) { tputs(tgetstr("nd", NULL), 1, ft_putchar); return (1); } else if(strncmp(buf, tgetstr("kl", NULL), ft_strlen(tgetstr("kl", NULL))) == 0) { tputs(tgetstr("le", NULL), 1, ft_putchar); return (1); } } return (0); } 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); /* 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); */ c_set_term_raw(1); c_get_struct(1, &tcaps); while (1) { ft_bzero(nread, 5); if (!(read(STDIN_FILENO, nread, 4))) return (0); ret = c_process_key(nread); } ft_printf("%d\n", ret); return (1); }