diff options
-rw-r--r-- | src/c_init.c | 65 | ||||
-rw-r--r-- | src/c_init.h | 41 | ||||
-rw-r--r-- | src/c_input.c | 41 | ||||
-rw-r--r-- | src/c_input.h | 2 | ||||
-rw-r--r-- | src/c_keys.c | 18 | ||||
-rw-r--r-- | src/c_utils.c | 117 | ||||
-rw-r--r-- | src/c_utils.h | 9 |
7 files changed, 137 insertions, 156 deletions
diff --git a/src/c_init.c b/src/c_init.c index 16f6cba..50c974b 100644 --- a/src/c_init.c +++ b/src/c_init.c @@ -61,6 +61,28 @@ int16_t } int16_t +c_read_cap(char *buf, + char *line, + t_caps *tcaps, + t_msh *msh) +{ + if ((*((unsigned int *)buf)) == LEFT_K) + return (c_key_left(ft_strlen(line), ft_strlen(msh->ps[0]), tcaps)); + else if ((*((unsigned int *)buf)) == RIGHT_K) + return (c_key_right(ft_strlen(line), ft_strlen(msh->ps[0]), tcaps)); + else if ((*((unsigned int *)buf)) == HOME_K) + return (c_home_key(tcaps)); + else if ((*((unsigned int *)buf)) == END_K) + return (c_end_key(ft_strlen(line), tcaps)); + else if ((*((unsigned int *)buf)) == CTRL_C) + return (c_ctrl_c(NULL, tcaps, msh)); + else if ((*((unsigned int *)buf)) == CTRL_L) + return (c_ctrl_l(line, tcaps, msh)); + else + return (0); +} + +int16_t c_process_key(char *buf, t_caps *tcaps, t_msh *msh) @@ -70,44 +92,27 @@ int16_t 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 + else if ((*((unsigned int *)buf)) == DEL_K) { - 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); + 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 (c_read_cap(buf, line, tcaps, msh)); return (0); } @@ -122,11 +127,9 @@ int16_t 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); + c_init_line(&tcaps); m_prompt_psx(1, msh); signal(SIGINT, SIG_IGN); if (!(c_get_win_size(&tcaps.ws))) diff --git a/src/c_init.h b/src/c_init.h index 730ba9f..d5f2c89 100644 --- a/src/c_init.h +++ b/src/c_init.h @@ -18,34 +18,39 @@ #include "s_struct.h" +# define LEFT_K 0x445b1b +# define RIGHT_K 0x435b1b +# define HOME_K 0x485b1b +# define END_K 0x7e345b1b +# define DEL_K 0x7f +# define RET_K 0x0d + +# define CTRL_C 0x03 +# define CTRL_L 0x0c + + typedef struct s_caps { - char KL[4]; - char KR[4]; - char CL[4]; - char CC[4]; - char HM[4]; - char ND[4]; struct termios tios; struct winsize ws; uint32_t cpos; /*cursor position (column)*/ uint32_t lpos; /*cursor position (line)*/ uint32_t nlines; /*cursor position (line)*/ - char *cm_str; /*cursor mobility*/ - char *nl; /*newline, returned by tgoto()*/ - char *ks; /*indicate that keys transmit from now on*/ - char *ke; /*indicate that keys transmit from now on*/ - char *kr; - char *pc; /*padding char --> do not touch (default 0)*/ - char *bc; /*left one char*/ - char *le; /*left one char*/ - char *up; /*up one line*/ - char *DO; /*down one line*/ - char *nd; /*right one char*/ - char *cl; /*line clear*/ } t_caps; int16_t c_init_tcaps(t_msh *msh); int16_t c_set_term_raw(uint8_t mode); #endif + +/* cm_str; cursor mobility*/ +/* nl; newline, returned by tgoto()*/ +/* ks; indicate that keys transmit from now on*/ +/* ke; indicate that keys transmit from now on*/ +/* pc; padding char --> do not touch (default 0)*/ +/* bc; left one char*/ +/* le; left one char*/ +/* up; up one line*/ +/* DO; down one line*/ +/* nd; right one char*/ +/* cl; line clear*/ diff --git a/src/c_input.c b/src/c_input.c index 37412bd..10451bc 100644 --- a/src/c_input.c +++ b/src/c_input.c @@ -12,6 +12,7 @@ #include <libft.h> #include <term.h> +#include <unistd.h> #include "c_init.h" #include "c_input.h" @@ -20,40 +21,30 @@ #include "m_loop.h" int16_t - c_init_keys(t_caps *tcaps) + c_init_line(t_caps *tcaps) { if (tcaps) { - tcaps->KL[0] = 27; - tcaps->KL[1] = 91; - tcaps->KL[2] = 68; - tcaps->KL[3] = 0; - tcaps->KR[0] = 27; - tcaps->KR[1] = 91; - tcaps->KR[2] = 67; - tcaps->KR[3] = 0; - tcaps->CL[0] = 12; - tcaps->CL[1] = 0; - tcaps->CL[2] = 0; - tcaps->CL[3] = 0; - tcaps->CC[0] = 3; - tcaps->CC[1] = 0; - tcaps->CC[2] = 0; - tcaps->CC[3] = 0; - tcaps->HM[0] = 27; - tcaps->HM[1] = 91; - tcaps->HM[2] = 72; - tcaps->HM[3] = 0; - tcaps->ND[0] = 27; - tcaps->ND[1] = 91; - tcaps->ND[2] = 52; - tcaps->ND[3] = 126; + tcaps->cpos = 0; + tcaps->lpos = 0; return (1); } else return (-1); } +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); +} + uint32_t c_get_line_num(char *line, uint32_t cpos, diff --git a/src/c_input.h b/src/c_input.h index 78b35d6..cc2e969 100644 --- a/src/c_input.h +++ b/src/c_input.h @@ -16,5 +16,7 @@ int16_t c_back_slash(char **line, uint32_t plen, t_caps *tcaps); int16_t c_ctrl_c(char *line, t_caps *tcaps, t_msh *msh); uint32_t c_get_line_num(char *line, uint32_t cpos, uint32_t plen, t_caps *tcaps); +int16_t c_init_line(t_caps *tcaps); +uint16_t c_get_win_size(struct winsize *ws); #endif diff --git a/src/c_keys.c b/src/c_keys.c index d37a76e..8232521 100644 --- a/src/c_keys.c +++ b/src/c_keys.c @@ -35,11 +35,11 @@ int16_t int16_t j; i = tcaps->cpos; - j = (tcaps->lpos - 1); + j = tcaps->lpos; if (tcaps->cpos > 0 && j > 0) { i = 0; - while (--j >= 0) + while (--j > 0) tputs(tgetstr("up", NULL), 1, ft_putchar); tputs(tgetstr("cr", NULL), 1, ft_putchar); while ((uint32_t)++i <= 13) @@ -60,24 +60,20 @@ int16_t uint16_t i; uint16_t j; - /* i = (tcaps->nlines == (tcaps->lpos)) ? 0 : tcaps->ws.ws_col * (tcaps->lpos); */ - i = 0; - j = (tcaps->lpos); + i = tcaps->cpos; + j = tcaps->lpos; if (tcaps->cpos < size && j < tcaps->nlines) { + i = 0; while (j < (tcaps->nlines)) { tputs(tgetstr("do", NULL), 1, ft_putchar); - j++; i += tcaps->ws.ws_col; + j++; } tputs(tgetstr("cr", NULL), 1, ft_putchar); - /* ft_printf("[%d][%d]", i, size); */ - while (i < (size + 13)) - { + while (i++ < (size + 13)) tputs(tgetstr("nd", NULL), 1, ft_putchar); - i++; - } } else while (i++ < size) diff --git a/src/c_utils.c b/src/c_utils.c index 24545f3..8bc940e 100644 --- a/src/c_utils.c +++ b/src/c_utils.c @@ -11,7 +11,6 @@ /* ************************************************************************** */ #include <libft.h> -#include <stdio.h> #include <stdlib.h> #include <term.h> #include <unistd.h> @@ -25,24 +24,11 @@ /* ** TODO: ** delchar MOVE -** END MONGARS +** plen opti ** NORME ** FLECHES UP DOWN ** C-c globul pid - */ - -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); -} +*/ int16_t c_new_line(char *line, @@ -70,27 +56,23 @@ char char c, t_caps *tcaps) { - size_t i; - size_t j; - size_t len; - char *dst; + int32_t i; + size_t j; + size_t len; + char *dst; - i = 0; + i = -1; j = 0; len = (ft_strlen(str) + 2); if (!(dst = (char*)malloc((len) * sizeof(char)))) return (NULL); - while (i < len) + while (++i < (int32_t)len) { - if (i == tcaps->cpos) - { + if (i == (int32_t)tcaps->cpos) dst[i] = c; - i++; - } else { dst[i] = str[j]; - i++; j++; } } @@ -102,9 +84,9 @@ char *c_delchar(char *str, uint16_t cpos) { - char *dst; - uint16_t i; - uint16_t j; + char *dst; + uint16_t i; + uint16_t j; j = 0; i = 0; @@ -124,58 +106,63 @@ char return (dst); } +static int16_t + c_redraw_next(size_t plen, + uint32_t len, + t_caps *tcaps) +{ + uint32_t i; + uint32_t j; + + i = (tcaps->nlines == tcaps->lpos) ? 0 : tcaps->ws.ws_col * (tcaps->lpos - 1); + j = tcaps->nlines; + if (tcaps->nlines != tcaps->lpos) + { + while (--j > tcaps->lpos) + tputs(tgetstr("up", NULL), 1, ft_putchar); + tputs(tgetstr("up", NULL), 1, ft_putchar); + tputs(tgetstr("cr", NULL), 1, ft_putchar); /* cr, cpos mauvaise col, bonne ligne */ + while (i <= (tcaps->cpos + plen)) + { + tputs(tgetstr("nd", NULL), 1, ft_putchar); /* cr, cpos mauvaise col, bonne ligne */ + i++; + } + } + else + while (--len > tcaps->cpos) + tputs(tgetstr("le", NULL), 1, ft_putchar); + return (len); +} + int16_t c_redraw_line(char *line, t_caps *tcaps, t_msh *msh) { - uint32_t i; - uint32_t len; - uint32_t mod; - int16_t ret; + uint32_t i; + uint32_t j; + uint32_t len; - i = 0; - tcaps->nlines = c_get_line_num(line, tcaps->cpos, ft_strlen(msh->ps[0]), tcaps); len = ft_strlen(line); - mod = (tcaps->ws.ws_col); + tcaps->nlines = c_get_line_num(line, tcaps->cpos, ft_strlen(msh->ps[0]), tcaps); + j = tcaps->lpos; + i = 0; tputs(tgetstr("cr", NULL), 1, ft_putchar); - if (((tcaps->cpos + ft_strlen(msh->ps[0])) % mod) == 0) + if (((tcaps->cpos + ft_strlen(msh->ps[0])) % tcaps->ws.ws_col) == 0) { tputs(tgetstr("sf", NULL), 1, ft_putchar); return (1); } - while (--tcaps->lpos > 0) + while (--j > 0) { tputs(tgetstr("sf", NULL), 1, ft_putchar); tputs(tgetstr("up", NULL), 1, ft_putchar); tputs(tgetstr("up", NULL), 1, ft_putchar); } - while (i < ft_strlen(msh->ps[0])) - { + while (i++ < ft_strlen(msh->ps[0])) tputs(tgetstr("nd", NULL), 1, ft_putchar); - i++; - } - tcaps->nlines = c_get_line_num(line, tcaps->cpos, ft_strlen(msh->ps[0]), tcaps); - ret = ft_printf("%s", line); - i = (tcaps->nlines == (tcaps->lpos)) ? 0 : tcaps->ws.ws_col * (tcaps->lpos - 1); + ft_printf("%s", line); if (tcaps->cpos != len) - { - if (tcaps->nlines != tcaps->lpos) - { - while (--tcaps->nlines > tcaps->lpos) - tputs(tgetstr("up", NULL), 1, ft_putchar); - tputs(tgetstr("up", NULL), 1, ft_putchar); - tputs(tgetstr("cr", NULL), 1, ft_putchar); /* cr, cpos mauvaise col, bonne ligne */ - while (i <= (tcaps->cpos + ft_strlen(msh->ps[0]))) - { - tputs(tgetstr("nd", NULL), 1, ft_putchar); /* cr, cpos mauvaise col, bonne ligne */ - i++; - } - } - else - while (--len > tcaps->cpos) - tputs(tgetstr("le", NULL), 1, ft_putchar); - } - tcaps->nlines = c_get_line_num(line, tcaps->cpos, ft_strlen(msh->ps[0]), tcaps); - return (ret); + return (c_redraw_next(ft_strlen(msh->ps[0]), len, tcaps)); + return (len); } diff --git a/src/c_utils.h b/src/c_utils.h index 7ef396f..9680b90 100644 --- a/src/c_utils.h +++ b/src/c_utils.h @@ -15,12 +15,9 @@ #include "s_struct.h" -int16_t c_redraw_line(char *line, t_caps *tcaps, 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); -char *c_insert_char(char *str, char c, t_caps *tcaps); -int16_t c_init_keys(t_caps *tcaps); int16_t c_new_line(char *line, t_msh *msh, t_caps *tcaps); +char *c_insert_char(char *str, char c, t_caps *tcaps); +char *c_delchar(char *str, uint16_t cpos); +int16_t c_redraw_line(char *line, t_caps *tcaps, t_msh *msh); #endif |