diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | libft/Makefile | 1 | ||||
-rw-r--r-- | libft/include/libft.h | 1 | ||||
-rw-r--r-- | libft/src/ft_strrnchr.c | 27 | ||||
-rw-r--r-- | src/b_alias_register.c | 8 | ||||
-rw-r--r-- | src/b_exit.c | 1 | ||||
-rw-r--r-- | src/c_ctrls.c | 13 | ||||
-rw-r--r-- | src/c_ctrls.h | 1 | ||||
-rw-r--r-- | src/c_init.c | 62 | ||||
-rw-r--r-- | src/c_init.h | 1 | ||||
-rw-r--r-- | src/c_input.c | 3 | ||||
-rw-r--r-- | src/c_utils_next.c (renamed from src/c_keys_next.c) | 28 | ||||
-rw-r--r-- | src/c_utils_next.h (renamed from src/c_keys_next.h) | 12 | ||||
-rw-r--r-- | src/m_argv.c | 4 | ||||
-rw-r--r-- | src/m_loop.c | 5 | ||||
-rw-r--r-- | src/s_init_next.c | 2 |
16 files changed, 101 insertions, 70 deletions
@@ -38,7 +38,7 @@ SRCS_NAME += c_ctrls SRCS_NAME += c_init SRCS_NAME += c_input SRCS_NAME += c_keys -SRCS_NAME += c_keys_next +SRCS_NAME += c_utils_next SRCS_NAME += c_utils SRCS_NAME += e_builtins SRCS_NAME += e_externs diff --git a/libft/Makefile b/libft/Makefile index 66b8081..fb67ba7 100644 --- a/libft/Makefile +++ b/libft/Makefile @@ -37,6 +37,7 @@ SRCS_NAME += ft_tolower.c SRCS_NAME += ft_toupper.c SRCS_NAME += ft_strchr.c SRCS_NAME += ft_strrchr.c +SRCS_NAME += ft_strrnchr.c SRCS_NAME += ft_strlchr.c SRCS_NAME += ft_strncmp.c SRCS_NAME += ft_strlcpy.c diff --git a/libft/include/libft.h b/libft/include/libft.h index 427f873..8ba0563 100644 --- a/libft/include/libft.h +++ b/libft/include/libft.h @@ -111,6 +111,7 @@ void *ft_nrealloc(void *ptr, size_t oldsize, size_t newsize); char *ft_strcat(char *s1, const char *s2); char *ft_strchr(const char *s, int c); char *ft_strrchr(const char *s, int c); +char *ft_strrnchr(const char *s, int c, size_t start); char *ft_strnstr(const char *haystack, const char *needle, size_t len); char *ft_strdup(const char *s1); diff --git a/libft/src/ft_strrnchr.c b/libft/src/ft_strrnchr.c new file mode 100644 index 0000000..d6b8fdd --- /dev/null +++ b/libft/src/ft_strrnchr.c @@ -0,0 +1,27 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrnchr.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 <libft.h> + +char *ft_strrnchr(const char *s, int c, size_t start) +{ + size_t i; + + i = ft_strlen(s) - start; + while (s[i] != c) + { + if (i == 0) + return (NULL); + i--; + } + return ((char*)&s[i]); +} diff --git a/src/b_alias_register.c b/src/b_alias_register.c index 8f75e36..0170a59 100644 --- a/src/b_alias_register.c +++ b/src/b_alias_register.c @@ -31,14 +31,16 @@ static void b_cpy_alias(char name[], char *ptr, char value[]) rptr++; tmp = *(rptr + ft_strlen(name)); *(rptr + ft_strlen(name)) = C_NUL; - if (ft_strncmp(rptr, name, ft_strlen(name) + 1) == 0) { + if (ft_strncmp(rptr, name, ft_strlen(name) + 1) == 0) + { *(rptr + ft_strlen(name)) = tmp; ft_strlcpy(value, ptr, ARG_MAX); ft_memmove(value + (rptr - ptr) + 1, value + (rptr - ptr), ft_strlen(value + (rptr - ptr)) + 1); *(value + (rptr - ptr)) = C_BACKS; } - else { + else + { *(rptr + ft_strlen(name)) = tmp; ft_strlcpy(value, ptr, ARG_MAX); } @@ -63,5 +65,3 @@ void b_register_arg(const char arg[], t_msh *msh) u_set_alias_value(name, value, msh); } } - - diff --git a/src/b_exit.c b/src/b_exit.c index 4f395cf..b9b17ea 100644 --- a/src/b_exit.c +++ b/src/b_exit.c @@ -25,6 +25,7 @@ unsigned char b_exit(char *args[], t_msh *msh) unsigned char ret; const unsigned long argc = u_builtins_get_argc((const char**)args); + write(STDERR_FILENO, "exit\n", 5 * sizeof(char)); if (argc > 1) { f_fail_too_many_args("exit", msh); diff --git a/src/c_ctrls.c b/src/c_ctrls.c index 58b4517..af0528e 100644 --- a/src/c_ctrls.c +++ b/src/c_ctrls.c @@ -17,23 +17,13 @@ #include <term.h> #include "c_ctrls.h" +#include "c_utils_next.h" #include "c_utils.h" #include "m_prompt.h" #include "m_loop.h" #include "s_destroy.h" #include "u_utils.h" -t_msh *c_get_msh(int mode, t_msh *src) -{ - static t_msh *msh; - - if (mode == 1) - { - msh = src; - } - return (msh); -} - short c_ctrl_d(t_msh *msh) { int tmp; @@ -76,7 +66,6 @@ void c_signal_int(int signo) write(1, "\n", 1); msh = c_get_msh(0, msh); msh->sig = 130; - ioctl(1, TIOCSTI, "\002"); } void c_signal_ign(int signo) diff --git a/src/c_ctrls.h b/src/c_ctrls.h index d4b932b..337a6f5 100644 --- a/src/c_ctrls.h +++ b/src/c_ctrls.h @@ -18,7 +18,6 @@ void c_signal_int(int signo); void c_signal_ign(int signo); -t_msh *c_get_msh(int mode, t_msh *src); short c_ctrl_l(char *line, t_caps *tcaps, t_msh *msh); short c_ctrl_d(t_msh *msh); char *c_ctrl_c(char **line, char *buf, t_msh *msh); diff --git a/src/c_init.c b/src/c_init.c index 4e74d87..cea3cce 100644 --- a/src/c_init.c +++ b/src/c_init.c @@ -20,7 +20,7 @@ #include "c_ctrls.h" #include "c_init.h" #include "c_keys.h" -#include "c_keys_next.h" +#include "c_utils_next.h" #include "c_input.h" #include "c_utils.h" #include "d_define.h" @@ -76,21 +76,24 @@ static short return (0); } -char *ft_strrbavchr(const char *s, int c, size_t start) +static void + c_set_ptr_norme(char key, t_bool *sw, char **p, char **context) { - size_t i; - - i = ft_strlen(s) - start; - while (s[i] != c) + if ((key == -1 && *p - *context != 0) || key == 1) { - if (i == 0) - return (NULL); - i--; + *p += (key == -1) ? (2 * key) : (0); + while (*sw == FALSE && ((key == -1 && *p - *context != 0) || key == 1) + && **p != C_LF && **p != C_NUL) + *p += key; + if (*sw == TRUE) + *sw = FALSE; + if (*p - *context != 0 && **p != C_NUL) + *p += 1; } - return ((char*)&s[i]); } -char *c_set_ptr(t_bool reset, char key, t_msh *msh) +char + *c_set_ptr(t_bool reset, char key, t_msh *msh) { static char *p = NULL; static char *context = NULL; @@ -109,51 +112,28 @@ char *c_set_ptr(t_bool reset, char key, t_msh *msh) context = msh->prev_hist; } if (key == 1 && context == msh->prev_hist - && p == ft_strrbavchr(msh->prev_hist, '\n', 2) + 1) + && p == ft_strrnchr(msh->prev_hist, '\n', 2) + 1) { p = msh->curr_hist_ptr; context = msh->curr_hist_ptr; sw = TRUE; } - if ((key == -1 && p - context != 0) || key == 1) - { - p += (key == -1) ? (2 * key) : (0); - while (sw == FALSE && ((key == -1 && p - context != 0) || key == 1) - && *p != C_LF && *p != C_NUL) - p += key; - if (sw == TRUE) - sw = FALSE; - if (p - context != 0 && *p != C_NUL) - p += 1; - } + c_set_ptr_norme(key, &sw, &p, &context); return (p); } -/* TODO : key_up_down should manage and set the correct key */ - static char *c_process_key(char **line, char *buf, t_caps *tcaps, t_msh *msh) { - char *ptr; - - if (*line == NULL) - if (!(*line = ft_calloc(1, sizeof(char)))) - return (NULL); if (ft_isprint(buf[0])) c_add_char(line, buf, tcaps); else if (((*((unsigned int *)buf)) == UP_K) || - ((*((unsigned int *)buf)) == CTRL_P)) - { - ptr = c_set_ptr(FALSE, -1, msh); - c_key_up_down(line, tcaps, ptr); - c_redraw_line(*line, tcaps); - } - else if (((*((unsigned int *)buf)) == DOWN_K) || + ((*((unsigned int *)buf)) == CTRL_P) || + ((*((unsigned int *)buf)) == DOWN_K) || ((*((unsigned int *)buf)) == CTRL_N)) { - ptr = c_set_ptr(FALSE, 1, msh); - c_key_up_down(line, tcaps, ptr); - c_redraw_line(*line, tcaps); + tcaps->key = c_set_key(buf); + c_key_up_down(line, tcaps, msh); } else if ((*((unsigned int *)buf)) == DEL_K) { @@ -183,6 +163,8 @@ short msh->sig = 0; tcaps.plen = (unsigned int)m_plen(msh->ps[psx - 1]); *line = NULL; + if (!(*line = ft_calloc(sizeof(char), 1))) + return (-1); if (!(c_get_win_size(&tcaps.ws))) return (-1); while (!(ft_strchr(nread, '\n'))) diff --git a/src/c_init.h b/src/c_init.h index dae8513..a9e7605 100644 --- a/src/c_init.h +++ b/src/c_init.h @@ -47,6 +47,7 @@ typedef struct s_caps unsigned int nlines; unsigned short psx; unsigned short plen; + short key; } t_caps; char *c_set_ptr(t_bool reset, char key, t_msh *msh); diff --git a/src/c_input.c b/src/c_input.c index 874d55a..7848f93 100644 --- a/src/c_input.c +++ b/src/c_input.c @@ -14,6 +14,7 @@ #include <stdlib.h> #include <libft.h> #include <term.h> +#include <signal.h> #include "c_ctrls.h" #include "c_init.h" @@ -29,6 +30,8 @@ short { char term[4096]; + signal(SIGINT, c_signal_int); + signal(SIGQUIT, c_signal_ign); if (tcaps) { u_get_var_value(term, "$TERM", 4096, msh); diff --git a/src/c_keys_next.c b/src/c_utils_next.c index ec063d3..27a88db 100644 --- a/src/c_keys_next.c +++ b/src/c_utils_next.c @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* c_keys_next.c :+: :+: :+: */ +/* c_utils_next.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -16,16 +16,40 @@ #include "c_init.h" #include "c_keys.h" +#include "c_utils.h" #include "d_define.h" -void c_key_up_down(char *line[], t_caps *tcaps, char *ptr) +short c_set_key(char *buf) +{ + if (((*((unsigned int *)buf)) == UP_K) || + ((*((unsigned int *)buf)) == CTRL_P)) + return (-1); + else + return (1); +} + +t_msh *c_get_msh(int mode, t_msh *src) +{ + static t_msh *msh; + + if (mode == 1) + { + msh = src; + } + return (msh); +} + +void c_key_up_down(char *line[], t_caps *tcaps, t_msh *msh) { char tmp[ARG_MAX]; + char *ptr; + ptr = c_set_ptr(FALSE, tcaps->key, msh); c_home_key(tcaps->plen, tcaps); ft_substr_s(tmp, ptr, 0, ft_strclen(ptr, '\n')); ft_memdel((void*)&(*line)); tputs(tgetstr("ce", NULL), 1, ft_putchar); *line = ft_strdup(tmp); tcaps->cpos = ft_strlen(*line); + c_redraw_line(*line, tcaps); } diff --git a/src/c_keys_next.h b/src/c_utils_next.h index fa79d85..1cf0132 100644 --- a/src/c_keys_next.h +++ b/src/c_utils_next.h @@ -1,7 +1,7 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* c_keys_next.h :+: :+: :+: */ +/* c_utils_next.h :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ @@ -10,11 +10,13 @@ /* */ /* ************************************************************************** */ -#ifndef C_KEYS_NEXT_H -# define C_KEYS_NEXT_H +#ifndef C_UTILS_NEXT_H +# define C_UTILS_NEXT_H -#include "c_init.h" +# include "c_init.h" -void c_key_up_down(char *line[], t_caps *tcaps, char *ptr); +short c_set_key(char *buf); +void c_key_up_down(char *line[], t_caps *tcaps, t_msh *msh); +t_msh *c_get_msh(int mode, t_msh *src); #endif diff --git a/src/m_argv.c b/src/m_argv.c index 525439b..859fa72 100644 --- a/src/m_argv.c +++ b/src/m_argv.c @@ -16,6 +16,8 @@ #include <fcntl.h> #include <unistd.h> +#include "c_init.h" +#include "c_utils_next.h" #include "f_fail.h" #include "d_define.h" #include "m_comm.h" @@ -69,6 +71,8 @@ unsigned char m_argv(int argc, char *const argv[], t_msh *msh) msh->prev_hist = m_get_prev_hist(msh); msh->ret = m_source_mshrc(msh); msh->fd = STDIN_FILENO; + c_get_msh(1, msh); + c_set_ptr(TRUE, 0, msh); msh->ret = m_loop(STDIN_FILENO, msh); } else if (argc > 1 && ft_strncmp(*(argv + 1), M_OPT_COMMAND, 3) == 0) diff --git a/src/m_loop.c b/src/m_loop.c index 18636da..1641d53 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -22,6 +22,7 @@ #endif #include "c_init.h" +#include "c_utils_next.h" #include "c_ctrls.h" #include "d_define.h" #include "e_line.h" @@ -154,10 +155,6 @@ unsigned char m_loop(int fd, t_msh *msh) gnl = 1; msh->curr_hist_ptr = hist; - c_get_msh(1, msh); - c_set_ptr(TRUE, 0, msh); - signal(SIGINT, c_signal_int); - signal(SIGQUIT, c_signal_ign); while (gnl > 0) { gnl = (fd == STDIN_FILENO) ? c_gnl(fd, &line, 1, msh) : diff --git a/src/s_init_next.c b/src/s_init_next.c index a013611..6ee17d4 100644 --- a/src/s_init_next.c +++ b/src/s_init_next.c @@ -109,7 +109,7 @@ char **s_dupenv(char *const envp[]) i++; } if (s_cpy_path(ptr, nenvp, &i) == 1) - return (s_dupenv_del(nenvp, i)); + return (s_dupenv_del(nenvp, i)); nenvp[i] = NULL; return (nenvp); } |