summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-08-27 17:47:04 +0200
committerJozanLeClerc <bousset.rudy@gmail.com>2020-08-27 17:47:04 +0200
commit8a1352d62c0dd66ecdd5807e83c16a00fb864635 (patch)
tree41d8a9c612e0373f0beed119ccae88e55b6ba8c2
parentTODO update (diff)
download42-minishell-8a1352d62c0dd66ecdd5807e83c16a00fb864635.tar.gz
42-minishell-8a1352d62c0dd66ecdd5807e83c16a00fb864635.tar.bz2
42-minishell-8a1352d62c0dd66ecdd5807e83c16a00fb864635.tar.xz
42-minishell-8a1352d62c0dd66ecdd5807e83c16a00fb864635.tar.zst
42-minishell-8a1352d62c0dd66ecdd5807e83c16a00fb864635.zip
Histfile is BAV
Diffstat (limited to '')
-rw-r--r--src/e_builtins.c3
-rw-r--r--src/m_init.c12
-rw-r--r--src/m_loop.c49
-rw-r--r--src/m_loop.h1
-rw-r--r--src/s_struct.h5
5 files changed, 64 insertions, 6 deletions
diff --git a/src/e_builtins.c b/src/e_builtins.c
index 666cf11..f4b363b 100644
--- a/src/e_builtins.c
+++ b/src/e_builtins.c
@@ -18,6 +18,7 @@
#include "b_export_next.h"
#include "b_export_mute.h"
+#include "m_loop.h"
#include "m_redirs.h"
#include "s_destroy.h"
#include "s_line.h"
@@ -77,6 +78,8 @@ static void
msh->bu_ptr[bu_id](ptr->argv + 1, msh);
else if (bu_id == FT_ID_EXIT)
{
+ if (msh->fd == STDIN_FILENO)
+ m_dump_hist(msh);
s_line_clear(&msh->curr);
s_destroy(msh);
exit(ret);
diff --git a/src/m_init.c b/src/m_init.c
index c145063..8b8a994 100644
--- a/src/m_init.c
+++ b/src/m_init.c
@@ -10,17 +10,27 @@
/* */
/* ************************************************************************** */
+#include <libft.h>
#include <stdlib.h>
#include "s_struct.h"
+#include "u_vars.h"
#include "u_vars_next.h"
void
m_init_custom_vars(t_msh *msh)
{
+ char *home;
+ char fmt[255];
+
u_subst_var_value("$PS1", FT_DEFAULT_PS_ONE, msh);
u_subst_var_value("$PS2", FT_DEFAULT_PS_TWO, msh);
u_subst_var_value("$PS3", FT_DEFAULT_PS_THR, msh);
u_subst_var_value("$PS4", FT_DEFAULT_PS_FOU, msh);
- u_subst_var_value("$HISTFILE", FT_DEFAULT_HISTFILE, msh);
+ if ((home = u_get_var_value("$HOME", msh)) != NULL)
+ {
+ ft_sprintf(fmt, "%s/%s", home, FT_DEFAULT_HISTFILE);
+ u_subst_var_value("$HISTFILE", fmt, msh);
+ ft_memdel((void*)&home);
+ }
}
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);
}
diff --git a/src/m_loop.h b/src/m_loop.h
index 3f95a5e..1f60744 100644
--- a/src/m_loop.h
+++ b/src/m_loop.h
@@ -17,6 +17,7 @@
#include "s_struct.h"
+void m_dump_hist(t_msh *msh);
uint8_t m_loop(int32_t fd, t_msh *msh);
#endif
diff --git a/src/s_struct.h b/src/s_struct.h
index 3f9e84f..01c8f1f 100644
--- a/src/s_struct.h
+++ b/src/s_struct.h
@@ -72,11 +72,12 @@ typedef struct s_msh
char **envp;
char **bu_ref;
char ps[4][1024];
+ char env_fork_tmp[128][1024];
+ char sqb_ref[FT_ID_SQB_COUNT][4];
+ char hist[1037595];
char *shname;
char *cwd;
int32_t fd;
- char env_fork_tmp[128][1024];
- char sqb_ref[FT_ID_SQB_COUNT][4];
uint8_t (*bu_ptr[FT_BUILTINS_COUNT])(char **, struct s_msh*);
uint8_t ret;
} t_msh;