diff --git a/42sh/Makefile b/42sh/Makefile index 2a2c7a0e..957be2a2 100644 --- a/42sh/Makefile +++ b/42sh/Makefile @@ -6,7 +6,7 @@ # By: wescande +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2016/08/29 21:32:58 by wescande #+# #+# # -# Updated: 2017/03/25 21:38:21 by jhalford ### ########.fr # +# Updated: 2017/03/27 02:54:02 by jhalford ### ########.fr # # # # **************************************************************************** # @@ -70,13 +70,16 @@ exec/exec_ampersand.c\ exec/exec_and_if.c\ exec/exec_bang.c\ exec/exec_case_branch.c\ +exec/exec_destroy.c\ exec/exec_elif.c\ exec/exec_else.c\ exec/exec_func.c\ +exec/exec_init.c\ exec/exec_leaf.c\ exec/exec_or_if.c\ exec/exec_pipe.c\ -exec/exec_reset.c\ +exec/exec_popfds.c\ +exec/exec_pushfds.c\ exec/exec_semi.c\ exec/exec_var.c\ exec/fd_is_valid.c\ diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index b14e1d2d..3f0cdd7c 100644 --- a/42sh/includes/exec.h +++ b/42sh/includes/exec.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/27 20:29:56 by jhalford #+# #+# */ -/* Updated: 2017/03/24 20:07:48 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 02:54:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -139,8 +139,8 @@ struct s_exec int control_count; }; -int exec_reset(void); -int exec_destroy(void); +int exec_init(t_exec *exec); +int exec_destroy(t_exec *exec); int exec_pushfds(); int exec_popfds(); int process_fork(t_process *p); diff --git a/42sh/libft/Makefile b/42sh/libft/Makefile index 7d483cd1..088122d9 100644 --- a/42sh/libft/Makefile +++ b/42sh/libft/Makefile @@ -199,7 +199,7 @@ sys/ft_getenv.c\ sys/ft_xattr_count.c\ sys/ft_xattr_print.c\ sys/is_directory.c\ -sys/open_access.c\ +sys/try_access.c\ time/ft_mytime_free.c\ time/ft_mytime_get.c\ time/ft_time_isrecent.c diff --git a/42sh/libft/includes/libft.h b/42sh/libft/includes/libft.h index dbecfc8c..96b3f0c3 100644 --- a/42sh/libft/includes/libft.h +++ b/42sh/libft/includes/libft.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */ -/* Updated: 2017/03/25 01:56:46 by jhalford ### ########.fr */ +/* Updated: 2017/03/26 21:12:20 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/includes/sys.h b/42sh/libft/includes/sys.h index b3c42e22..25f77020 100644 --- a/42sh/libft/includes/sys.h +++ b/42sh/libft/includes/sys.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/14 17:24:23 by jhalford #+# #+# */ -/* Updated: 2017/03/25 15:12:52 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 00:05:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,11 +33,12 @@ int ft_xattr_count(char *path); char *ft_getenv(char **env, char *key); -int open_access(char *file, t_flag a_flag, t_flag o_flag, t_flag o_perm); +int try_access(char *file, int exists, t_flag a_flag); int is_directory(const char *path); char *create_directory(const char *path, const char *old_pathnames); int dup2_close(int fd1, int fd2); int fd_replace(int fd1, int fd2); +int fd_minor(int fd, int minor); #endif diff --git a/42sh/libft/src/lst/ft_lst_print.c b/42sh/libft/src/lst/ft_lst_print.c index 94e5f315..0ff1d1a6 100644 --- a/42sh/libft/src/lst/ft_lst_print.c +++ b/42sh/libft/src/lst/ft_lst_print.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/04 11:09:27 by jhalford #+# #+# */ -/* Updated: 2016/11/04 11:09:28 by jhalford ### ########.fr */ +/* Updated: 2017/03/26 21:25:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/lst/ft_lstdel.c b/42sh/libft/src/lst/ft_lstdel.c index ecebf830..720dd7e9 100644 --- a/42sh/libft/src/lst/ft_lstdel.c +++ b/42sh/libft/src/lst/ft_lstdel.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/03 15:18:57 by jhalford #+# #+# */ -/* Updated: 2016/12/05 13:39:14 by jhalford ### ########.fr */ +/* Updated: 2017/03/26 21:56:50 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,8 @@ void ft_lstdel(t_list **alst, void (*del)(void *, size_t)) { - if (alst && *alst && del) - { - ft_lstdel(&(*alst)->next, del); - ft_lstdelone(alst, del); - } + if (!(alst && *alst && del)) + return ; + ft_lstdel(&(*alst)->next, del); + ft_lstdelone(alst, del); } diff --git a/42sh/libft/src/lst/ft_lstdelone.c b/42sh/libft/src/lst/ft_lstdelone.c index d9743a95..e550e0f3 100644 --- a/42sh/libft/src/lst/ft_lstdelone.c +++ b/42sh/libft/src/lst/ft_lstdelone.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/05 13:39:33 by jhalford #+# #+# */ -/* Updated: 2017/03/18 17:17:50 by ariard ### ########.fr */ +/* Updated: 2017/03/26 21:24:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/lst/pop.c b/42sh/libft/src/lst/pop.c index df3be22e..05beda83 100644 --- a/42sh/libft/src/lst/pop.c +++ b/42sh/libft/src/lst/pop.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/04 11:09:56 by jhalford #+# #+# */ -/* Updated: 2017/03/24 20:05:13 by jhalford ### ########.fr */ +/* Updated: 2017/03/26 22:14:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,10 +17,11 @@ int pop(t_list **lst) t_list *top; int item; + if (!(lst && *lst)) + return (0); top = *lst; item = top ? *(int*)top->content : 0; - if (lst && *lst) - *lst = (*lst)->next; + *lst = (*lst)->next; ft_lstdelone(&top, ft_lst_cfree); return (item); } diff --git a/42sh/libft/src/sys/fd_replace.c b/42sh/libft/src/sys/fd_replace.c index 495ae484..a3502c52 100644 --- a/42sh/libft/src/sys/fd_replace.c +++ b/42sh/libft/src/sys/fd_replace.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/21 16:32:26 by jhalford #+# #+# */ -/* Updated: 2017/03/24 22:59:28 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 00:18:52 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,5 +16,5 @@ int fd_replace(int fd1, int fd2) { if (fd1 != fd2) return (dup2_close(fd1, fd2)); - return (0); + return (-1); } diff --git a/42sh/libft/src/sys/open_access.c b/42sh/libft/src/sys/try_access.c similarity index 64% rename from 42sh/libft/src/sys/open_access.c rename to 42sh/libft/src/sys/try_access.c index bc1ee88e..729eed6d 100644 --- a/42sh/libft/src/sys/open_access.c +++ b/42sh/libft/src/sys/try_access.c @@ -6,23 +6,19 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/25 01:10:56 by jhalford #+# #+# */ -/* Updated: 2017/03/25 15:07:42 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 02:54:39 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -int open_access(char *file, t_flag a_flag, t_flag o_flag, t_flag o_perm) +int try_access(char *file, int exists, t_flag a_flag) { - int fd; - - if (a_flag & F_OK && access(file, F_OK) != 0) - return (-ERR_SET(E_SYS_NOFILE, file)); - if (is_directory(file)) - return (-ERR_SET(E_SYS_ISDIR, file)); - if (access(file, F_OK) == 0 && access(file, a_flag) != 0) - return (-ERR_SET(E_SYS_NOPERM, file)); - if ((fd = open(file, o_flag, o_perm)) < 0) - exit(1); - return (fd); + if (exists && access(file, F_OK) != 0) + return (ERR_SET(E_SYS_NOFILE, file) * 0 - 1); + else if (is_directory(file)) + return (ERR_SET(E_SYS_ISDIR, file) * 0 - 1); + else if (access(file, F_OK) == 0 && access(file, a_flag) != 0) + return (ERR_SET(E_SYS_NOPERM, file) * 0 - 1); + return (0); } diff --git a/42sh/src/exec/exec_reset.c b/42sh/src/exec/exec_destroy.c similarity index 50% rename from 42sh/src/exec/exec_reset.c rename to 42sh/src/exec/exec_destroy.c index 0e55431e..e6e2b525 100644 --- a/42sh/src/exec/exec_reset.c +++ b/42sh/src/exec/exec_destroy.c @@ -6,75 +6,22 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 14:31:42 by jhalford #+# #+# */ -/* Updated: 2017/03/24 23:29:00 by ariard ### ########.fr */ +/* Updated: 2017/03/27 00:33:31 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int exec_reset_job(t_job *job) -{ - job->id = 0; - job->pgid = 0; - job->attrs = 0; - job->first_process = NULL; - tcgetattr(STDIN, &job->tmodes); - return (0); -} - -int exec_pushfds(void) -{ - int i; - t_exec *exec; - - exec = &data_singleton()->exec; - i = -1; - while (++i < 10) - push(&(exec->fd_save[i]), fcntl(i, F_DUPFD_CLOEXEC, 10)); - return (0); -} - -int exec_popfds(void) -{ - int i; - t_exec *exec; - - exec = &data_singleton()->exec; - i = -1; - while (++i < 10) - pop(&exec->fd_save[i]); - return (0); -} - -int exec_reset(void) +int exec_destroy(t_exec *exec) { t_jobc *jobc; - t_exec *exec; - - exec = &data_singleton()->exec; - jobc = &data_singleton()->jobc; - exec_pushfds(); - exec->op_stack = NULL; - exec->fdin = STDIN; - exec->attrs = 0; - exec_reset_job(&exec->job); - jobc->first_job = NULL; - jobc->current_id = 1; - return (0); -} - -int exec_destroy(void) -{ int i; - t_jobc *jobc; - t_exec *exec; - exec = &data_singleton()->exec; + if (!exec) + return (0); jobc = &data_singleton()->jobc; ft_lstdel(&exec->op_stack, ft_lst_cfree); ft_lstdel(&jobc->first_job, job_free); i = -1; - while (++i < 10) - ft_lstdel(&exec->fd_save[i], ft_lst_cfree); return (0); } diff --git a/42sh/src/exec/exec_init.c b/42sh/src/exec/exec_init.c new file mode 100644 index 00000000..fc66168d --- /dev/null +++ b/42sh/src/exec/exec_init.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/26 21:28:09 by jhalford #+# #+# */ +/* Updated: 2017/03/27 02:55:43 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int job_init(t_job *job) +{ + job->id = 0; + job->pgid = 0; + job->attrs = 0; + job->first_process = NULL; + tcgetattr(STDIN, &job->tmodes); + return (0); +} + +int exec_init(t_exec *exec) +{ + t_jobc *jobc; + int i; + + jobc = &data_singleton()->jobc; + job_init(&exec->job); + i = -1; + while (++i < 10) + exec->fd_save[i] = NULL; + exec_pushfds(); + exec->attrs = 0; + exec->fdin = STDIN; + exec->op_stack = NULL; + jobc->first_job = NULL; + jobc->current_id = 1; + return (0); +} diff --git a/42sh/src/exec/exec_popfds.c b/42sh/src/exec/exec_popfds.c new file mode 100644 index 00000000..d4fc942e --- /dev/null +++ b/42sh/src/exec/exec_popfds.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_popfds.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/26 21:31:05 by jhalford #+# #+# */ +/* Updated: 2017/03/27 00:58:34 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int exec_popfds(void) +{ + int i; + int fd; + t_exec *exec; + + exec = &data_singleton()->exec; + i = -1; + while (++i < 10) + { + fd = pop(&exec->fd_save[i]); + if (fd != -1) + close(fd); + } + return (0); +} diff --git a/42sh/src/exec/exec_pushfds.c b/42sh/src/exec/exec_pushfds.c new file mode 100644 index 00000000..31c16cde --- /dev/null +++ b/42sh/src/exec/exec_pushfds.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* exec_pushfds.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/26 21:31:46 by jhalford #+# #+# */ +/* Updated: 2017/03/27 00:58:25 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int exec_pushfds(void) +{ + int i; + int fd; + t_exec *exec; + + exec = &data_singleton()->exec; + i = -1; + while (++i < 10) + { + fd = fcntl(i, F_DUPFD_CLOEXEC, 10); + push(&(exec->fd_save[i]), fd); + } + return (0); +} diff --git a/42sh/src/exec/fd_is_valid.c b/42sh/src/exec/fd_is_valid.c index ebf60e6d..67a1c7f1 100644 --- a/42sh/src/exec/fd_is_valid.c +++ b/42sh/src/exec/fd_is_valid.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 13:46:40 by jhalford #+# #+# */ -/* Updated: 2017/03/20 21:13:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 00:51:02 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,5 +19,6 @@ int fd_is_valid(int fd, int has_mode) fd_modes = fcntl(fd, F_GETFL); fd_flags = fcntl(fd, F_GETFD); - return ((fd_flags != -1 || errno != EBADF) && fd_modes & has_mode); + return ((fd_flags != -1 || errno != EBADF) + && (has_mode == -1 || fd_modes & has_mode)); } diff --git a/42sh/src/exec/process_launch.c b/42sh/src/exec/process_launch.c index 5a0b711b..81ba525e 100644 --- a/42sh/src/exec/process_launch.c +++ b/42sh/src/exec/process_launch.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/13 22:21:19 by jhalford #+# #+# */ -/* Updated: 2017/03/24 20:02:54 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 02:54:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,8 +26,8 @@ int process_fork(t_process *p) exit(1); process_setgroup(p, 0); process_setsig(); - exec_destroy(); - exec_reset(); + exec_destroy(&data_singleton()->exec); + exec_init(&data_singleton()->exec); data_singleton()->opts &= ~SH_INTERACTIVE; data_singleton()->opts &= ~SH_OPTS_JOBC; exit(p->map.launch(p)); @@ -54,10 +54,10 @@ int process_launch(t_process *p) { exec_pushfds(); p->map.launch(p); + exec_popfds(); + shell_resetfds(); + shell_resetsig(); } - exec_popfds(); - shell_resetfds(); - shell_resetsig(); process_free(p, 0); return (0); } diff --git a/42sh/src/exec/process_set.c b/42sh/src/exec/process_set.c index be240f3f..cbb3d11a 100644 --- a/42sh/src/exec/process_set.c +++ b/42sh/src/exec/process_set.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/05 14:54:45 by jhalford #+# #+# */ -/* Updated: 2017/03/24 19:19:34 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 00:10:47 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,10 +30,10 @@ static int process_set_spec(t_process *p, t_btree *ast) int i; t_astnode *item; - i = -1; if (!ast) return (0); item = ast->item; + i = -1; while (g_setprocessmap[++i].id) if (item->type == g_setprocessmap[i].id) { @@ -58,17 +58,15 @@ int process_set(t_process *p, t_btree *ast) || (EXEC_IS_OR_IF(exec->attrs) && ft_strcmp(ft_getenv(data_singleton()->env, "?"), "0") == 0)) return (1); - fds[PIPE_WRITE] = STDOUT; - fds[PIPE_READ] = STDIN; - (op == TK_PIPE) ? pipe(fds) : (0); exec->job.attrs |= (op == TK_AMP ? JOB_BG : 0); p->fdin = exec->fdin; - p->to_close = fds[PIPE_READ]; - p->fdout = fds[PIPE_WRITE]; + (op == TK_PIPE) ? pipe(fds) : (0); + p->fdout = (op == TK_PIPE) ? fds[PIPE_WRITE] : STDOUT; + exec->fdin = (op == TK_PIPE) ? fds[PIPE_READ] : STDIN; + p->to_close = exec->fdin; p->pid = 0; - exec->fdin = fds[PIPE_READ]; if (ast) p->redirs = ft_lstmap( ((t_astnode *)ast->item)->data.cmd.redir, &redir_copy); - return ((!ast) ? 0 : process_set_spec(p, ast)); + return (process_set_spec(p, ast)); } diff --git a/42sh/src/exec/redirect_dgreat.c b/42sh/src/exec/redirect_dgreat.c index 3e448fd8..323122a2 100644 --- a/42sh/src/exec/redirect_dgreat.c +++ b/42sh/src/exec/redirect_dgreat.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:07:37 by jhalford #+# #+# */ -/* Updated: 2017/03/25 01:54:05 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 02:56:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,9 +18,11 @@ int redirect_dgreat(t_redir *redir) int fdnew; fdnew = redir->n; - if ((fdold = open_access(redir->word, R_OK, - O_WRONLY | O_CREAT | O_APPEND, 0644)) < 0) + fdnew = redir->n; + if ((try_access(redir->word, 0, W_OK))) return (ft_perror(NULL)); - dup2(fdold, fdnew); + if ((fdold = open(redir->word, O_WRONLY | O_CREAT | O_APPEND, 0644)) < 0) + exit(1); + fd_replace(fdold, fdnew); return (0); } diff --git a/42sh/src/exec/redirect_great.c b/42sh/src/exec/redirect_great.c index d0cc00a9..1eb17b37 100644 --- a/42sh/src/exec/redirect_great.c +++ b/42sh/src/exec/redirect_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:03:53 by jhalford #+# #+# */ -/* Updated: 2017/03/25 02:25:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 02:56:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,9 +18,10 @@ int redirect_great(t_redir *redir) int fdnew; fdnew = redir->n; - if ((fdold = open_access(redir->word, R_OK, - O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) + if ((try_access(redir->word, 0, W_OK))) return (ft_perror(NULL)); + if ((fdold = open(redir->word, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) + exit(1); fd_replace(fdold, fdnew); return (0); } diff --git a/42sh/src/exec/redirect_less.c b/42sh/src/exec/redirect_less.c index 390c91e9..92fa3ed5 100644 --- a/42sh/src/exec/redirect_less.c +++ b/42sh/src/exec/redirect_less.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:09:53 by jhalford #+# #+# */ -/* Updated: 2017/03/26 18:25:38 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 02:56:59 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,9 +18,10 @@ int redirect_less(t_redir *redir) int fdnew; fdnew = redir->n; - if ((fdold = open_access(redir->word, F_OK | W_OK, - O_RDONLY, 0)) < 0) + if ((try_access(redir->word, 1, R_OK)) != 0) return (ft_perror(NULL)); - dup2(fdold, fdnew); + if ((fdold = open(redir->word, O_RDONLY, 0644)) < 0) + exit(1); + fd_replace(fdold, fdnew); return (0); } diff --git a/42sh/src/glob/match_pattern.c b/42sh/src/glob/match_pattern.c index 94cbed7d..7ac647e6 100644 --- a/42sh/src/glob/match_pattern.c +++ b/42sh/src/glob/match_pattern.c @@ -113,7 +113,7 @@ int match_pattern(t_glob *gl, char *str, char *full_word) if (*str != *gl->pat) return (0); } - else if (*gl->pat == '?') + else if (*gl->pat == '?' && *str) ; else if (*gl->pat == '[') { diff --git a/42sh/src/history/add_str_in_history.c b/42sh/src/history/add_str_in_history.c index 0ccfaff2..bfb83140 100644 --- a/42sh/src/history/add_str_in_history.c +++ b/42sh/src/history/add_str_in_history.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/14 11:27:03 by gwojda #+# #+# */ -/* Updated: 2017/03/25 20:26:01 by jhalford ### ########.fr */ +/* Updated: 2017/03/26 21:59:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/history/history_parsing.c b/42sh/src/history/history_parsing.c index 70372393..b664743c 100644 --- a/42sh/src/history/history_parsing.c +++ b/42sh/src/history/history_parsing.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/01/25 11:39:47 by gwojda #+# #+# */ -/* Updated: 2017/03/22 12:09:59 by gwojda ### ########.fr */ +/* Updated: 2017/03/27 02:56:33 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ static int history_parsing_nb_and_previous(char *str, int *i) if (!ft_strncmp("!!", str + *i, 2)) { ft_realloc_str_history(&(data_singleton()->line.input), *i, 0, 2); - ++(*i); + (*i) += 2; return (1); } else if (str[*i + 1] == '-' && ft_isdigit(str[*i + 2]) && @@ -34,10 +34,21 @@ static int history_parsing_nb_and_previous(char *str, int *i) return (0); } +static int check_validity(char *str, int i) +{ + if (i && str[i - 1] == '\\') + return (0); + else if (!str[i + 1] || str[i + 1] == '"') + return (0); + return (1); +} + static int history_parsing_nb_and_name(char *str, int *i) { int tmp; + if (!check_validity(str, *i)) + return (0); if (history_parsing_nb_and_previous(str, i)) return (1); else if (ft_isdigit(str[(*i) + 1]) && (size_t)ft_atoi(str + (*i) + 1) < @@ -80,23 +91,26 @@ int ft_history_parsing(int has_prompt, char **input) { int i; char boolean; + char quote; i = 0; + quote = 0; boolean = 0; - if (!data_singleton()->line.input) - return (0); while (data_singleton()->line.input && data_singleton()->line.input[i]) { - if (data_singleton()->line.input[i] == '!') + if (data_singleton()->line.input[i] == '\'') + quote = quote ? 0 : 1; + if (!quote && data_singleton()->line.input[i] == '!') { boolean = 1; if (!history_parsing_nb_and_name(data_singleton()->line.input, &i)) boolean = 0; else - break ; + i = -1; } ++i; } + *input = data_singleton()->line.input; if (boolean) return (rematch_history_parsing(has_prompt, input)); return (0); diff --git a/42sh/src/lexer/lexer_backslash.c b/42sh/src/lexer/lexer_backslash.c index e9970a94..2a0a62c0 100644 --- a/42sh/src/lexer/lexer_backslash.c +++ b/42sh/src/lexer/lexer_backslash.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:56:49 by jhalford #+# #+# */ -/* Updated: 2017/03/21 13:42:01 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 02:16:36 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,16 +17,14 @@ int lexer_backslash(t_list **alst, t_lexer *lexer) t_token *token; token = (*alst)->content; - token->type = token->type ? token->type : TK_WORD; - lexer->pos++; - lexer->state = WORD; - if (lexer->str[lexer->pos] == 0) + lexer->state = DEFAULT; + if (lexer->str[lexer->pos + 1] == 0) { - if (!*token->data) - ft_lst_delif(alst, token, &ft_addrcmp, &token_free); + lexer->str[lexer->pos] = 0; push(&lexer->stack, BACKSLASH); return (0); } + lexer->pos++; token_append(token, lexer, 1, 1); lexer->pos++; return (lexer_lex(alst, lexer)); diff --git a/42sh/src/line_editing/ft_prompt.c b/42sh/src/line_editing/ft_prompt.c index c4d0201e..3f12ed95 100644 --- a/42sh/src/line_editing/ft_prompt.c +++ b/42sh/src/line_editing/ft_prompt.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 13:51:33 by gwojda #+# #+# */ -/* Updated: 2017/03/23 10:52:08 by gwojda ### ########.fr */ +/* Updated: 2017/03/27 01:00:56 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ static int promt_git_status(int fd) char *line; get_next_line(fd, &line); + close(fd); tmp = line; if (ft_strrchr(line, '/')) line = ft_strdup(ft_strrchr(line, '/') + 1); diff --git a/42sh/src/main/data_exit.c b/42sh/src/main/data_exit.c index b43d0b07..66d6494f 100644 --- a/42sh/src/main/data_exit.c +++ b/42sh/src/main/data_exit.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/07 18:07:50 by jhalford #+# #+# */ -/* Updated: 2017/03/26 14:11:44 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 02:55:04 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,16 +15,19 @@ void data_exit(void) { t_data *data; + int i; data = data_singleton(); - ft_strdel(&data->line.input); ft_strdel(&data->binary); ft_sstrfree(data->env); ft_sstrfree(data->local_var); ft_sstrfree(data->argv); lexer_destroy(&data->lexer); parser_destroy(&data->parser); - exec_destroy(); + exec_destroy(&data->exec); + i = -1; + while (++i < 10) + ft_lstdel(&data->exec.fd_save[i], ft_lst_cfree); ft_lstdel(&data->lst_func, &tree_func_free); ft_save_termios(-1); ft_free_hash_table(); diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 4b0a771c..a554d48f 100644 --- a/42sh/src/main/data_init.c +++ b/42sh/src/main/data_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 19:26:32 by jhalford #+# #+# */ -/* Updated: 2017/03/24 17:12:51 by wescande ### ########.fr */ +/* Updated: 2017/03/26 21:54:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -82,10 +82,10 @@ int data_init(int ac, char **av, char **env) return (-1); data->comp = NULL; data->opts = SH_INTERACTIVE | SH_OPTS_JOBC; - exec_reset(); data->lst_func = NULL; lexer_init(&data->lexer); parser_init(&data->parser); + exec_init(&data->exec); if ((term_name = ft_getenv(data->env, "TERM")) == NULL) term_name = "dumb"; if (tgetent(NULL, term_name) != 1) diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index e7f55e88..73156a39 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: gwojda +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/20 14:45:40 by gwojda #+# #+# */ -/* Updated: 2017/03/25 20:12:37 by ariard ### ########.fr */ +/* Updated: 2017/03/26 21:57:37 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -79,7 +79,7 @@ int main(int ac, char **av, char **env) ret = handle_instruction(&token, &ast); lexer_destroy(&data->lexer); parser_destroy(&data->parser); - ft_lstdel(&data_singleton()->exec.op_stack, &ft_lst_cfree); + exec_destroy(&data->exec); ft_lstdel(&token, &token_free); btree_del(&ast, &ast_free); if (ret == 1) diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index e769df3c..3da5594a 100644 --- a/42sh/src/main/shell_init.c +++ b/42sh/src/main/shell_init.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 17:23:59 by jhalford #+# #+# */ -/* Updated: 2017/03/26 14:13:44 by jhalford ### ########.fr */ +/* Updated: 2017/03/27 02:55:15 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parser_destroy.c b/42sh/src/parser/parser_destroy.c index 02b4bf6e..0ca8ffcd 100644 --- a/42sh/src/parser/parser_destroy.c +++ b/42sh/src/parser/parser_destroy.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/16 19:30:17 by ariard #+# #+# */ -/* Updated: 2017/03/18 18:45:22 by ariard ### ########.fr */ +/* Updated: 2017/03/26 21:46:24 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/parser_init.c b/42sh/src/parser/parser_init.c index 8f86e68c..e31e0e19 100644 --- a/42sh/src/parser/parser_init.c +++ b/42sh/src/parser/parser_init.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/21 16:14:04 by ariard #+# #+# */ -/* Updated: 2017/03/16 22:13:39 by ariard ### ########.fr */ +/* Updated: 2017/03/26 21:36:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/push_stack.c b/42sh/src/parser/push_stack.c index 3ab774c2..7f74703e 100644 --- a/42sh/src/parser/push_stack.c +++ b/42sh/src/parser/push_stack.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 16:48:30 by ariard #+# #+# */ -/* Updated: 2017/03/18 19:15:39 by ariard ### ########.fr */ +/* Updated: 2017/03/26 21:35:32 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/stack_init.c b/42sh/src/parser/stack_init.c index a9ae3199..67270a1c 100644 --- a/42sh/src/parser/stack_init.c +++ b/42sh/src/parser/stack_init.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/16 20:31:32 by ariard #+# #+# */ -/* Updated: 2017/03/17 18:24:46 by ariard ### ########.fr */ +/* Updated: 2017/03/26 21:47:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,6 @@ int stack_init(t_parser *parser) { - ft_lstdel(&parser->stack, NULL); push_stack(&parser->stack, TERMINUS); push_stack(&parser->stack, LINEBREAK); if (!parser->new_sym && !(parser->new_sym = ft_memalloc(sizeof(t_sym))))