diff options
author | JozanLeClerc <bousset.rudy@gmail.com> | 2020-11-02 15:08:38 +0100 |
---|---|---|
committer | JozanLeClerc <bousset.rudy@gmail.com> | 2020-11-02 15:08:38 +0100 |
commit | e0a74558be8d71054ae3c8e6afe8774fc1890b84 (patch) | |
tree | 57bc32451216e00fac58bd8f79eee12543badf56 /src/c_init.c | |
parent | No more fixed int (diff) | |
download | 42-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
Diffstat (limited to 'src/c_init.c')
-rw-r--r-- | src/c_init.c | 149 |
1 files changed, 149 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); +} |