diff options
Diffstat (limited to '')
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | src/b_builtins.h | 1 | ||||
-rw-r--r-- | src/b_h.c | 27 | ||||
-rw-r--r-- | src/m_argv.c | 34 | ||||
-rw-r--r-- | src/m_funptr.c | 1 | ||||
-rw-r--r-- | src/m_loop.c | 1 | ||||
-rw-r--r-- | src/m_mshrc.c | 5 | ||||
-rw-r--r-- | src/s_destroy.c | 1 | ||||
-rw-r--r-- | src/s_init.c | 1 | ||||
-rw-r--r-- | src/s_struct.h | 2 |
10 files changed, 62 insertions, 12 deletions
@@ -24,6 +24,7 @@ SRCS_NAME += b_exit SRCS_NAME += b_export SRCS_NAME += b_export_next SRCS_NAME += b_export_mute +SRCS_NAME += b_h SRCS_NAME += b_pwd SRCS_NAME += b_sqb SRCS_NAME += b_sqb_err diff --git a/src/b_builtins.h b/src/b_builtins.h index 62b2a41..6e09535 100644 --- a/src/b_builtins.h +++ b/src/b_builtins.h @@ -19,6 +19,7 @@ #include "b_env.h" #include "b_exit.h" #include "b_export.h" +#include "b_h.h" #include "b_pwd.h" #include "b_type.h" #include "b_unset.h" @@ -12,22 +12,43 @@ #include <libft.h> #include <stdint.h> +#include <stddef.h> #include <unistd.h> #include "d_define.h" +#include "f_fail.h" #include "s_struct.h" #include "u_utils.h" -static void b_print_history_number(const char arg[]) +static void b_print_history(const char arg[], t_msh *msh) { + char *prev_hist; + char *tok; + uint32_t i; + (void)arg; + i = 1; + if (msh->prev_hist != NULL) + { + if ((prev_hist = ft_strdup(msh->prev_hist)) == NULL) + return ; + tok = ft_strtok(prev_hist, "\n"); + ft_printf("qwqwe\n"); + while (tok != NULL) + { + ft_printf("%-5u %s\n", i, tok); + i++; + tok = ft_strtok(NULL, "\n"); + } + ft_memdel((void*)&prev_hist); + } } static t_bool b_check_numeric(const char arg[]) { char *ptr; - ptr = arg; + ptr = (char*)arg; while (*ptr != C_NUL) { if (ft_isdigit(*ptr) == FALSE) @@ -57,7 +78,7 @@ uint8_t b_h(char *args[], t_msh *msh) } else { - b_print_history(*args); + b_print_history(*args, msh); } return (0); } 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) diff --git a/src/m_funptr.c b/src/m_funptr.c index bb8d80d..4f17827 100644 --- a/src/m_funptr.c +++ b/src/m_funptr.c @@ -32,4 +32,5 @@ void msh->bu_ptr[FT_ID_TYPE] = b_type; msh->bu_ptr[FT_ID_SQB] = b_sqb; msh->bu_ptr[FT_ID_ALIAS] = b_alias; + msh->bu_ptr[FT_ID_H] = b_h; } diff --git a/src/m_loop.c b/src/m_loop.c index 41c9e36..0d90b0a 100644 --- a/src/m_loop.c +++ b/src/m_loop.c @@ -128,7 +128,6 @@ uint8_t char *line; int8_t gnl; - msh->fd = fd; gnl = 1; while (gnl > 0) { diff --git a/src/m_mshrc.c b/src/m_mshrc.c index 7f6b8b9..5b1277e 100644 --- a/src/m_mshrc.c +++ b/src/m_mshrc.c @@ -22,8 +22,7 @@ #include "m_loop.h" #include "u_vars.h" -uint8_t - m_source_mshrc(t_msh *msh) +uint8_t m_source_mshrc(t_msh *msh) { char file[PATH_MAX]; char home[PATH_MAX]; @@ -34,7 +33,7 @@ uint8_t ft_sprintf(file, "%s/%s", home, FT_MINISHELLRC); if ((fd = open(file, O_RDONLY)) < 0) return (0); + msh->fd = fd; ret = m_loop(fd, msh); - close(fd); return (ret); } diff --git a/src/s_destroy.c b/src/s_destroy.c index 54f3d4a..3e7eb21 100644 --- a/src/s_destroy.c +++ b/src/s_destroy.c @@ -19,6 +19,7 @@ void s_destroy(t_msh *msh) { + ft_memdel((void*)&msh->prev_hist); ft_memdel((void*)&msh->shname); ft_memdel((void*)&msh->cwd); ft_delwords(msh->envp); diff --git a/src/s_init.c b/src/s_init.c index 58f0cca..f8037b9 100644 --- a/src/s_init.c +++ b/src/s_init.c @@ -171,6 +171,7 @@ t_msh return (NULL); msh->ret = 0; init_buptr(msh); + msh->fd = 0; msh->curr = NULL; msh->com = NULL; msh->pipes = NULL; diff --git a/src/s_struct.h b/src/s_struct.h index b9668de..f9b6ca3 100644 --- a/src/s_struct.h +++ b/src/s_struct.h @@ -81,7 +81,7 @@ typedef struct s_msh struct s_lvars *vars; struct s_lalias *alias; char **envp; - char **prev_hist; + char *prev_hist; char ps[4][255]; char env_fork_tmp[128][4096]; char sqb_ref[FT_ID_SQB_COUNT][4]; |