diff options
Diffstat (limited to 'src/m_loop.c')
-rw-r--r-- | src/m_loop.c | 49 |
1 files changed, 46 insertions, 3 deletions
diff --git a/src/m_loop.c b/src/m_loop.c index c25188e..a5e2723 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -13,6 +13,7 @@ #include <libft.h> #include <stdint.h> #include <stdlib.h> +#include <fcntl.h> #include <unistd.h> #include "e_line.h" @@ -20,9 +21,10 @@ #include "m_prompt.h" #include "p_line.h" #include "s_line.h" +#include "u_vars.h" static void - m_parse_and_run_line(char *line, t_msh *msh) + m_parse_and_run_line(char line[], t_msh *msh) { p_line(line, msh); ft_memdel((void*)&line); @@ -30,11 +32,48 @@ static void s_line_clear(&msh->curr); } +void + m_dump_hist(t_msh *msh) +{ + int32_t fd; + char *histfile_path; + + if (ft_strlen(msh->hist) > 0 && + (histfile_path = u_get_var_value("$HISTFILE", msh)) != NULL) + { + if ((fd = open(histfile_path, + O_WRONLY | O_CREAT | O_APPEND, 0644)) != -1) + { + write(fd, msh->hist, ft_strlen(msh->hist)); + close(fd); + } + ft_memdel((void*)&histfile_path); + } +} + +static void + m_handle_hist(char line[], t_msh *msh) +{ + static uint16_t hist_i = 0; + + if (hist_i == 0) + msh->hist[0] = '\0'; + ft_strlcpy(msh->hist + ft_strlen(msh->hist), line, 4096); + msh->hist[ft_strlen(msh->hist) + 1] = '\0'; + msh->hist[ft_strlen(msh->hist)] = '\n'; + hist_i += 1; + if (hist_i == 255) + { + m_dump_hist(msh); + hist_i = 0; + } +} + uint8_t m_loop(int32_t fd, t_msh *msh) { - char *line; - int8_t gnl; + char *line; + int8_t gnl; msh->fd = fd; gnl = 1; @@ -47,6 +86,8 @@ uint8_t { line = m_check_multi_backslash(fd, line, msh); line = m_check_multi_pipe(fd, line, msh); + if (fd == STDIN_FILENO) + m_handle_hist(line, msh); m_parse_and_run_line(line, msh); /* TODO: (null): Bad address on "msh ~> echo a > asd; cat < asd" but not on "msh ~> echo a > asd; cat asd" */ /* TODO: "msh ~> some command \": re GNL into ft_nrealloc */ @@ -57,5 +98,7 @@ uint8_t ft_memdel((void*)&line); } } + if (fd == STDIN_FILENO) + m_dump_hist(msh); return (msh->ret); } |