summaryrefslogtreecommitdiffstats
path: root/src/m_argv.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/m_argv.c')
-rw-r--r--src/m_argv.c34
1 files changed, 30 insertions, 4 deletions
diff --git a/src/m_argv.c b/src/m_argv.c
index 98a8e3e..bdebd23 100644
--- a/src/m_argv.c
+++ b/src/m_argv.c
@@ -10,7 +10,9 @@
/* */
/* ************************************************************************** */
+#include <sys/stat.h>
#include <libft.h>
+#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
@@ -22,25 +24,49 @@
#include "m_loop.h"
#include "m_mshrc.h"
#include "s_struct.h"
+#include "u_vars.h"
-static void
- m_read_script(char *const argv[], t_msh *msh)
+static char *m_get_prev_hist(t_msh *msh)
+{
+ struct stat sb;
+ char *hist;
+ char histfile[PATH_MAX];
+ int32_t fd;
+
+ hist = NULL;
+ u_get_var_value(histfile, "$HISTFILE", PATH_MAX, msh);
+ if (histfile[0] == C_NUL)
+ return (NULL);
+ if ((fd = open(histfile, O_RDONLY)) == -1)
+ return (NULL);
+ if (stat(histfile, &sb) == -1)
+ return (NULL);
+ if ((hist = (char*)malloc(sb.st_size * sizeof(char))) == NULL)
+ return (NULL);
+ if (read(fd, hist, sb.st_size) == -1)
+ return (NULL);
+ return (hist);
+}
+
+static void m_read_script(char *const argv[], t_msh *msh)
{
int32_t fd;
if ((fd = open(*(argv + 1), O_RDONLY)) == -1)
f_open_file(*(argv + 1), msh);
+ msh->fd = fd;
msh->ret = m_loop(fd, msh);
close(fd);
}
-uint8_t
- m_argv(int argc, char *const argv[], t_msh *msh)
+uint8_t m_argv(int argc, char *const argv[], t_msh *msh)
{
if (argc == 1)
{
m_init_custom_vars(msh);
+ msh->prev_hist = m_get_prev_hist(msh);
msh->ret = m_source_mshrc(msh);
+ msh->fd = STDIN_FILENO;
msh->ret = m_loop(STDIN_FILENO, msh);
}
else if (argc > 1 && ft_strncmp(*(argv + 1), FT_OPT_COMMAND, 3) == 0)