/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* 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 "c_init.h" #include "c_input.h" #include "c_keys.h" #include "c_utils.h" #include "s_struct.h" #include "m_prompt.h" static 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); mode ? tputs(tgetstr("ns", NULL), 1, ft_putchar) : 0; return (1); } int16_t 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 (-1); } if (ft_isprint(buf[0])) { line = c_insert_char(line, buf[0], tcaps); c_redraw_line(line, tcaps, msh); tcaps->cpos++; } else { if (ft_strncmp(buf, tcaps->KL, 4) == 0) return (c_key_left(ft_strlen(line), ft_strlen(msh->ps[0]), tcaps)); else if (ft_strncmp(buf, tcaps->KR, 4) == 0) return (c_key_right(ft_strlen(line), ft_strlen(msh->ps[0]), tcaps)); else if (ft_strncmp(buf, tcaps->HM, 4) == 0) return (c_home_key(tcaps)); else if (ft_strncmp(buf, tcaps->ND, 4) == 0) return (c_end_key(ft_strlen(line), tcaps)); else if (ft_strncmp(buf, tcaps->CC, 4) == 0) return (c_ctrl_c(NULL, tcaps, msh)); else if (ft_strncmp(buf, tcaps->CL, 4) == 0) return (c_ctrl_l(line, tcaps, msh)); else if (ft_strncmp(buf, tgetstr("kb", NULL), ft_strlen(tgetstr("kb", NULL))) == 0) { c_back_slash(&line, ft_strlen(msh->ps[0]), tcaps); return (1); } else if (buf[0] == '\n') { c_new_line(ft_strdup(line), msh, tcaps); ft_memdel((void**)&line); return (1); } else return (0); } return (0); } int16_t c_init_tcaps(t_msh *msh) { t_caps tcaps; char *term; char nread[5]; int ret; term = getenv("TERM"); if (!tgetent(NULL, term)) return (-1); tcaps.cpos = 0; tcaps.lpos = 0; c_set_term_raw(1); c_get_struct(1, &tcaps); c_init_keys(&tcaps); m_prompt_psx(1, msh); signal(SIGINT, SIG_IGN); if (!(c_get_win_size(&tcaps.ws))) return (-1); while (1) { ft_bzero(nread, 5); if (!(read(STDIN_FILENO, nread, 4))) return (0); ret = c_process_key(nread, &tcaps, msh); tputs(tgetstr("ve", NULL), 1, ft_putchar); } return (1); }