From 5e5a4a122a3b144ef479409c08e4a7fa57223565 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Thu, 9 Mar 2017 19:48:34 +0100 Subject: [PATCH 1/7] parsing tk_bang && brace_clause} --- 42sh/error | 30 ------------------------------ 42sh/file3 | 0 42sh/ret2 | 0 42sh/src/lexer/lexer_sep.c | 4 +++- 42sh/src/main/ft_putast.c | 6 +++++- 42sh/src/main/main.c | 2 +- 42sh/src/parser/add_cmd.c | 2 +- 42sh/src/parser/add_subshell.c | 4 +++- 42sh/src/parser/aggregate_sym.c | 9 +++++---- 42sh/src/parser/build_tree.c | 2 +- 42sh/src/parser/eval_sym.c | 7 ++++--- 11 files changed, 23 insertions(+), 43 deletions(-) delete mode 100644 42sh/error delete mode 100644 42sh/file3 delete mode 100644 42sh/ret2 diff --git a/42sh/error b/42sh/error deleted file mode 100644 index 4c17682f..00000000 --- a/42sh/error +++ /dev/null @@ -1,30 +0,0 @@ -42sh -Makefile -STDBUG -TESTSHELL -donovan_segaults_06-02 -error -includes -libft -objs -pdf -sample -src -test_framework.sh -testmake -update_makefile.sh -42sh -Makefile -STDBUG -TESTSHELL -donovan_segaults_06-02 -error -includes -libft -objs -pdf -sample -src -test_framework.sh -testmake -update_makefile.sh diff --git a/42sh/file3 b/42sh/file3 deleted file mode 100644 index e69de29b..00000000 diff --git a/42sh/ret2 b/42sh/ret2 deleted file mode 100644 index e69de29b..00000000 diff --git a/42sh/src/lexer/lexer_sep.c b/42sh/src/lexer/lexer_sep.c index 980aad75..76c6b7e8 100644 --- a/42sh/src/lexer/lexer_sep.c +++ b/42sh/src/lexer/lexer_sep.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/30 16:29:57 by jhalford #+# #+# */ -/* Updated: 2017/03/08 17:11:35 by jhalford ### ########.fr */ +/* Updated: 2017/03/09 18:48:00 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,6 +31,8 @@ int lexer_sep(t_list **alst, t_lexer *lexer) token->type = cn == '|' ? TK_OR_IF : TK_PIPE; else if (c == ';') token->type = cn == ';' ? TK_DSEMI : TK_SEMI; + else if (c == '!') + token->type = TK_BANG; if (token->type == TK_AND_IF || token->type == TK_OR_IF || token->type == TK_DSEMI) lexer->pos++; diff --git a/42sh/src/main/ft_putast.c b/42sh/src/main/ft_putast.c index b223e7e3..2c6fe982 100644 --- a/42sh/src/main/ft_putast.c +++ b/42sh/src/main/ft_putast.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/14 18:18:04 by jhalford #+# #+# */ -/* Updated: 2017/03/07 23:35:32 by ariard ### ########.fr */ +/* Updated: 2017/03/09 18:54:07 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,8 +17,12 @@ char *ft_putast(void *nodein) t_astnode *node; node = nodein; + if (node->type == TK_BANG) + return ("TK_BANG"); if (node->type == MATH) return ("MATH"); + if (node->type == MATH_PLUS) + return ("MATH_PLUS"); if (node->type == TK_DSEMI) return ("TK_DSEMI"); if (node->type == WORDLIST) diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 50fb92eb..152691b1 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/09 17:37:38 by ariard ### ########.fr */ +/* Updated: 2017/03/09 19:46:24 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index 64e1931d..c6c28d3d 100644 --- a/42sh/src/parser/add_cmd.c +++ b/42sh/src/parser/add_cmd.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 20:49:15 by ariard #+# #+# */ -/* Updated: 2017/03/09 16:04:46 by ariard ### ########.fr */ +/* Updated: 2017/03/09 19:44:51 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_subshell.c b/42sh/src/parser/add_subshell.c index 88f25c43..dcbe85f4 100644 --- a/42sh/src/parser/add_subshell.c +++ b/42sh/src/parser/add_subshell.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/24 18:41:50 by ariard #+# #+# */ -/* Updated: 2017/03/07 22:46:36 by ariard ### ########.fr */ +/* Updated: 2017/03/09 19:46:44 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,8 @@ int issubshell(t_btree **ast, t_list **lst) node = (*ast)->item; if (node->type == SUBSHELL && node->full == 0) return (1); + if (node->type == TK_LBRACE && node->full == 0) + return (1); } return (0); } diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index 25ea1ce6..4bdf4ec9 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -97,6 +97,7 @@ t_aggrematch g_aggrematch[] = {NEWLINE_LIST, TK_ELIF, TK_ELIF, TK_ELIF}, {NEWLINE_LIST, TK_ELSE, TK_ELSE, TK_ELSE}, {NEWLINE_LIST, TK_WHILE, TK_WHILE, TK_WHILE}, + {NEWLINE_LIST, TK_LBRACE, TK_LBRACE, TK_LBRACE}, {NEWLINE_LIST, TK_UNTIL, TK_UNTIL, TK_UNTIL}, {NEWLINE_LIST, CASE_LIST_NS, CASE_LIST_NS, CASE_LIST_NS}, {NEWLINE_LIST, NEWLINE_LIST, NEWLINE_LIST, NEWLINE}, @@ -367,21 +368,21 @@ int aggregate_sym(t_list **stack, t_sym *new_sym, t_parstate *state) return (1); i = 0; head = (*stack)->content; -// DG("aggregate head %s && sym %s", -// read_state(*head), read_state(*new_sym)); + DG("aggregate head %s && sym %s", + read_state(*head), read_state(*new_sym)); while (g_aggrematch[i].top) { if (*new_sym == g_aggrematch[i].top && MATCH_STACK(*head, g_aggrematch[i].under)) { -// DG("MATCH : %s", read_state(g_aggrematch[i].new_sym)); + DG("MATCH : %s", read_state(g_aggrematch[i].new_sym)); *new_sym = g_aggrematch[i].new_sym; if (g_aggrematch[i].erase_sym) { pop_stack(stack, g_aggrematch[i].erase_sym); head = (*stack)->content; -// DG("stack after pop: %s", read_state(*head)); + DG("stack after pop: %s", read_state(*head)); } if (eval_sym(stack, *new_sym)) { diff --git a/42sh/src/parser/build_tree.c b/42sh/src/parser/build_tree.c index 803e384e..f8cc9635 100644 --- a/42sh/src/parser/build_tree.c +++ b/42sh/src/parser/build_tree.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/15 18:32:59 by ariard #+# #+# */ -/* Updated: 2017/03/09 18:33:05 by ariard ### ########.fr */ +/* Updated: 2017/03/09 19:45:53 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index 5626f27d..9ca21d4a 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -429,6 +429,7 @@ t_stackmatch g_stackmatch[] = // watch ! {TK_PAREN_OPEN, AND_OR_MAJOR}, {TK_PAREN_OPEN, TK_WHILE}, + {TK_PAREN_OPEN, TK_LBRACE}, {TK_PAREN_OPEN, FUNC_NAME}, {TK_PAREN_OPEN, TK_UNTIL}, {TK_PAREN_OPEN, TK_DO}, @@ -817,6 +818,7 @@ t_stackmatch g_stackmatch[] = {WHILE_CLAUSE, TK_DO}, {WHILE_CLAUSE, TK_PAREN_CLOSE}, {WHILE_CLAUSE, TK_WHILE}, + {WHILE_CLAUSE, TK_LBRACE}, {WHILE_CLAUSE, FUNC_NAME}, {WHILE_CLAUSE, TK_UNTIL}, {WHILE_CLAUSE, TK_IF}, @@ -954,6 +956,7 @@ t_stackmatch g_stackmatch[] = {SUBSHELL, SEPARATOR_OP}, {SUBSHELL, NEWLINE_LIST}, {SUBSHELL, SEQUENCE}, + {SUBSHELL, TK_LBRACE}, // watch ! {SUBSHELL, SEPARATOR_OP}, {SUBSHELL, NEWLINE_LIST}, @@ -1059,8 +1062,6 @@ t_stackmatch g_stackmatch[] = {TK_BANG, TK_PAREN_OPEN}, {TK_BANG, TK_LBRACE}, {TK_BANG, COMPLETE_COMMANDS}, - {TK_BANG, TK_BANG}, - {TK_BANG, TK_BANG}, {TK_BANG, SEPARATOR_OP}, {TK_BANG, NEWLINE_LIST}, {TK_BANG, AND_OR_MAJOR}, @@ -1191,7 +1192,7 @@ int eval_sym(t_list **stack, t_sym new_sym) if (!*stack) return (1); head = (*stack)->content; -// DG("eval head %s && sym %s", read_state(*head), read_state(new_sym)); + DG("eval head %s && sym %s", read_state(*head), read_state(new_sym)); i = 0; while (g_stackmatch[i].top) { From 98f43eb911333af96e9cb0d5f9798b7c0ccd6bc1 Mon Sep 17 00:00:00 2001 From: gwojda Date: Fri, 10 Mar 2017 12:13:18 +0100 Subject: [PATCH 2/7] adios les lignes vides dans l'historique --- 42sh/src/history/add_str_in_history.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/42sh/src/history/add_str_in_history.c b/42sh/src/history/add_str_in_history.c index fb4e636f..f8fd0ccc 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/04 15:52:19 by gwojda ### ########.fr */ +/* Updated: 2017/03/10 12:10:03 by gwojda ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,17 +15,26 @@ void ft_add_in_history_file(char *str) { int fd; + int i; char *home; char *path; + char **hist; + i = 0; + hist = ft_strsplit(str, '\n'); if (!(home = ft_getenv(data_singleton()->env, "HOME"))) return ; path = ft_str3join(home, "/", ".42sh_history"); fd = open(path, O_CREAT | O_WRONLY | O_APPEND, S_IWUSR | S_IRUSR); if (fd == -1) return ; - write(fd, str, ft_strlen(str)); - write(fd, "\n", 1); + while (hist[i]) + { + write(fd, hist[i], ft_strlen(str)); + write(fd, "\n", 1); + ++i; + } + ft_sstrfree(hist); free(path); close(fd); } From dd6817ff61d8e4901fdc130b9fe25fdf66a4563c Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 10 Mar 2017 12:58:34 +0100 Subject: [PATCH 3/7] issues #71 , #72 --- 42sh/includes/exec.h | 2 +- 42sh/src/exec/exec_leaf.c | 2 +- 42sh/src/exec/exec_reset.c | 17 ++++++--- 42sh/src/exec/launch_file.c | 2 +- 42sh/src/exec/launch_process.c | 2 +- 42sh/src/exec/launch_subshell.c | 2 +- 42sh/src/exec/mark_process_status.c | 40 ++++++++++------------ 42sh/src/exec/process_resetfds.c | 3 +- 42sh/src/job-control/do_job_notification.c | 2 +- 42sh/src/job-control/job_update_status.c | 4 +-- 42sh/src/main/data_init.c | 2 +- 42sh/src/main/main.c | 6 ++-- 42sh/src/main/shell_init.c | 3 +- 13 files changed, 47 insertions(+), 40 deletions(-) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index b1853896..1505a765 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/09 15:14:34 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 12:11:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_leaf.c b/42sh/src/exec/exec_leaf.c index dc4cc9ab..6aa92a4d 100644 --- a/42sh/src/exec/exec_leaf.c +++ b/42sh/src/exec/exec_leaf.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 15:47:30 by wescande #+# #+# */ -/* Updated: 2017/03/09 15:21:14 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 11:58:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_reset.c b/42sh/src/exec/exec_reset.c index 35c26757..51ef6d5d 100644 --- a/42sh/src/exec/exec_reset.c +++ b/42sh/src/exec/exec_reset.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 14:31:42 by jhalford #+# #+# */ -/* Updated: 2017/03/09 15:22:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 12:55:57 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,9 +17,18 @@ int exec_reset(void) t_exec *exec; exec = &data_singleton()->exec; - exec->fd_save[0] = fcntl(STDIN, F_DUPFD_CLOEXEC); - exec->fd_save[1] = fcntl(STDOUT, F_DUPFD_CLOEXEC); - exec->fd_save[2] = fcntl(STDERR, F_DUPFD_CLOEXEC); + /* exec->fd_save[0] = 1;//fcntl(STDIN, F_DUPFD_CLOEXEC); */ + /* exec->fd_save[1] = 1;//fcntl(STDOUT, F_DUPFD_CLOEXEC); */ + /* exec->fd_save[2] = 1;//fcntl(STDERR, F_DUPFD_CLOEXEC); */ + DG("check 0"); + if ((exec->fd_save[0] = fcntl(STDIN, F_DUPFD_CLOEXEC, 10)) == -1) + ft_dprintf(2, "{red}%s: internal fcntl error errno=%i %s{eoc}\n", SHELL_NAME, errno); + DG("check 1"); + if ((exec->fd_save[1] = fcntl(STDOUT, F_DUPFD_CLOEXEC, 10)) == -1) + ft_dprintf(2, "{red}%s: internal fcntl error errno=%i %s{eoc}\n", SHELL_NAME, errno); + if ((exec->fd_save[2] = fcntl(STDERR, F_DUPFD_CLOEXEC, 10)) == -1) + ft_dprintf(2, "{red}%s: internal fcntl error errno=%i %s{eoc}\n", SHELL_NAME, errno); + DG("saved [%i:%i:%i]", exec->fd_save[0], exec->fd_save[1], exec->fd_save[2]); exec->op_stack = NULL; exec->fdin = STDIN; exec->attrs = 0; diff --git a/42sh/src/exec/launch_file.c b/42sh/src/exec/launch_file.c index ba26d9cc..c9d29b79 100644 --- a/42sh/src/exec/launch_file.c +++ b/42sh/src/exec/launch_file.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 14:53:31 by jhalford #+# #+# */ -/* Updated: 2017/03/09 15:10:15 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 12:12:59 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/launch_process.c b/42sh/src/exec/launch_process.c index db17f981..0fbbd7db 100644 --- a/42sh/src/exec/launch_process.c +++ b/42sh/src/exec/launch_process.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/13 14:20:45 by jhalford #+# #+# */ -/* Updated: 2017/03/09 15:15:33 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 11:58:53 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/launch_subshell.c b/42sh/src/exec/launch_subshell.c index 1c2703b1..401aa434 100644 --- a/42sh/src/exec/launch_subshell.c +++ b/42sh/src/exec/launch_subshell.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 00:11:44 by wescande #+# #+# */ -/* Updated: 2017/03/08 15:08:04 by wescande ### ########.fr */ +/* Updated: 2017/03/10 11:51:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/mark_process_status.c b/42sh/src/exec/mark_process_status.c index c2e7dd97..57b21b34 100644 --- a/42sh/src/exec/mark_process_status.c +++ b/42sh/src/exec/mark_process_status.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/12 12:41:11 by jhalford #+# #+# */ -/* Updated: 2017/03/08 18:49:49 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 12:51:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,30 +18,26 @@ int mark_process_status(pid_t pid, int status) t_process *p; DG("MPS pid=%i,s=%i", pid, status); - if (pid > 1) + if ((plist = job_getprocess(pid))) { - if ((plist = job_getprocess(pid))) + p = plist->content; + p->status = status; + DG("marking pid=[%i], name=[%s]", p->pid, p->data.cmd.av[0]); + if (WIFSTOPPED(status)) { - p = plist->content; - p->status = status; - DG("marking pid=[%i], name=[%s]", p->pid, p->data.cmd.av[0]); - if (WIFSTOPPED(status)) - { - p->attrs &= ~PROCESS_STATE_MASK; - p->attrs |= PROCESS_SUSPENDED; - } - else - { - p->attrs &= ~PROCESS_STATE_MASK; - p->attrs |= PROCESS_COMPLETED; - if (WIFSIGNALED(status)) - ft_printf("{mag}%d: Terminated by signal %d.\n{eoc}", - (int)pid, WTERMSIG(status)); - } - return (0); + p->attrs &= ~PROCESS_STATE_MASK; + p->attrs |= PROCESS_SUSPENDED; + } + else + { + p->attrs &= ~PROCESS_STATE_MASK; + p->attrs |= PROCESS_COMPLETED; + if (WIFSIGNALED(status)) + ft_printf("{mag}%d: Terminated by signal %d.\n{eoc}", + (int)pid, WTERMSIG(status)); } - ft_dprintf(2, "{red}No child process %d.\n", pid); return (0); } - return (-1); + ft_dprintf(2, "{red}No child process %d.\n", pid); + return (0); } diff --git a/42sh/src/exec/process_resetfds.c b/42sh/src/exec/process_resetfds.c index 61cd1289..127ccc10 100644 --- a/42sh/src/exec/process_resetfds.c +++ b/42sh/src/exec/process_resetfds.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/09 14:51:23 by jhalford #+# #+# */ -/* Updated: 2017/03/09 15:20:41 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 12:51:38 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,7 @@ void process_resetfds(t_process *p) (void)p; exec = &data_singleton()->exec; dup2(exec->fd_save[0], STDIN); + close(STDOUT); dup2(exec->fd_save[1], STDOUT); dup2(exec->fd_save[2], STDERR); } diff --git a/42sh/src/job-control/do_job_notification.c b/42sh/src/job-control/do_job_notification.c index 7d73c78a..2ea310d1 100644 --- a/42sh/src/job-control/do_job_notification.c +++ b/42sh/src/job-control/do_job_notification.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 13:01:19 by jhalford #+# #+# */ -/* Updated: 2017/03/08 20:35:18 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 12:40:35 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/job-control/job_update_status.c b/42sh/src/job-control/job_update_status.c index 1cae0944..575becb1 100644 --- a/42sh/src/job-control/job_update_status.c +++ b/42sh/src/job-control/job_update_status.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 12:56:11 by jhalford #+# #+# */ -/* Updated: 2017/03/08 20:35:12 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 12:41:11 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,5 +22,5 @@ void job_update_status(void) pid = waitpid(WAIT_ANY, &status, WUNTRACED | WNOHANG); DG("pid=[%d]", pid); } - while (!mark_process_status(pid, status)); + while (pid > 1 && !mark_process_status(pid, status)); } diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 908c9983..43442aa4 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/09 15:23:31 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 12:12:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 152691b1..e847841a 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/09 19:46:24 by ariard ### ########.fr */ +/* Updated: 2017/03/10 12:12:24 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -109,14 +109,14 @@ int main(int ac, char **av) int fd; setlocale(LC_ALL, ""); + DG("{inv}{bol}{gre}start of shell{eoc}"); shell_init(ac, av); if ((fd = get_input_fd()) < 0) { ft_printf("{red}%s: %s: No such file or directory\n{eoc}", SHELL_NAME, shell_get_avdata()); return (1); } - DG("{inv}{bol}{gre}start of shell{eoc} JOBC is %s, fd=[%i]", - SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF", fd); + DG("JOBC is %s, fd=[%i]", SH_HAS_JOBC(data_singleton()->opts)?"ON":"OFF", fd); while (handle_instruction(fd) == 0) { DG(); diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index 3a8ad244..177a7520 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/08 23:42:47 by ariard ### ########.fr */ +/* Updated: 2017/03/10 12:12:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ void shell_init(int ac, char **av) int *shell_pgid; t_data *data; + DG("check"); data_init(); data = data_singleton(); data->argc = ac; From 2659b68d288e573a7954d1d7d9f3e7871f3a817d Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 10 Mar 2017 13:13:12 +0100 Subject: [PATCH 4/7] issue #71 done (lexer problem) --- 42sh/includes/exec.h | 2 +- 42sh/src/exec/exec_reset.c | 2 +- 42sh/src/exec/process_resetfds.c | 7 +++++-- 42sh/src/history/add_str_in_history.c | 2 +- 42sh/src/lexer/get_state_redir.c | 2 +- 42sh/src/lexer/lexer_great.c | 6 +++--- 42sh/src/lexer/lexer_less.c | 4 +++- 42sh/src/lexer/lexer_number.c | 4 ++-- 42sh/src/main/main.c | 2 +- 9 files changed, 18 insertions(+), 13 deletions(-) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 1505a765..df7f3d02 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/10 12:11:21 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 13:10:03 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_reset.c b/42sh/src/exec/exec_reset.c index 51ef6d5d..fd0e7a66 100644 --- a/42sh/src/exec/exec_reset.c +++ b/42sh/src/exec/exec_reset.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 14:31:42 by jhalford #+# #+# */ -/* Updated: 2017/03/10 12:55:57 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 13:13:49 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/process_resetfds.c b/42sh/src/exec/process_resetfds.c index 127ccc10..7cfd2220 100644 --- a/42sh/src/exec/process_resetfds.c +++ b/42sh/src/exec/process_resetfds.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/09 14:51:23 by jhalford #+# #+# */ -/* Updated: 2017/03/10 12:51:38 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 13:09:25 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,11 +15,14 @@ void process_resetfds(t_process *p) { t_exec *exec; + int i; (void)p; exec = &data_singleton()->exec; + i = 0; + while (i < 10) + close(i++); dup2(exec->fd_save[0], STDIN); - close(STDOUT); dup2(exec->fd_save[1], STDOUT); dup2(exec->fd_save[2], STDERR); } diff --git a/42sh/src/history/add_str_in_history.c b/42sh/src/history/add_str_in_history.c index f8fd0ccc..333f5225 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/10 12:10:03 by gwojda ### ########.fr */ +/* Updated: 2017/03/10 12:59:26 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/get_state_redir.c b/42sh/src/lexer/get_state_redir.c index d4d60d3b..5ac62b6c 100644 --- a/42sh/src/lexer/get_state_redir.c +++ b/42sh/src/lexer/get_state_redir.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/09 20:37:28 by jhalford #+# #+# */ -/* Updated: 2017/02/24 20:44:40 by ariard ### ########.fr */ +/* Updated: 2017/03/10 13:12:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_great.c b/42sh/src/lexer/lexer_great.c index 333504a1..d47125f4 100644 --- a/42sh/src/lexer/lexer_great.c +++ b/42sh/src/lexer/lexer_great.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:35 by jhalford #+# #+# */ -/* Updated: 2017/03/08 12:10:46 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 13:11:30 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,8 +17,8 @@ int lexer_great(t_list **alst, t_lexer *lexer) t_token *token; token = (*alst)->content; - /* if (token->type) */ - /* return (lexer_lex(&(*alst)->next, lexer)); */ + if (token->type) + return (lexer_lex(&(*alst)->next, lexer)); token_append(token, lexer, 0, 0); lexer->pos++; if (lexer->str[lexer->pos] == '&') diff --git a/42sh/src/lexer/lexer_less.c b/42sh/src/lexer/lexer_less.c index 7f54359c..1e8571f1 100644 --- a/42sh/src/lexer/lexer_less.c +++ b/42sh/src/lexer/lexer_less.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:53 by jhalford #+# #+# */ -/* Updated: 2017/03/08 23:34:58 by ariard ### ########.fr */ +/* Updated: 2017/03/10 13:11:27 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,8 @@ int lexer_less(t_list **alst, t_lexer *lexer) t_token *token; token = (*alst)->content; + if (token->type) + return (lexer_lex(&(*alst)->next, lexer)); lexer->pos++; if (lexer->str[lexer->pos] == '&') { diff --git a/42sh/src/lexer/lexer_number.c b/42sh/src/lexer/lexer_number.c index 203308b8..b63ac61a 100644 --- a/42sh/src/lexer/lexer_number.c +++ b/42sh/src/lexer/lexer_number.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 12:06:45 by jhalford #+# #+# */ -/* Updated: 2017/03/08 23:35:37 by ariard ### ########.fr */ +/* Updated: 2017/03/10 13:12:43 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,7 +38,7 @@ int lexer_number(t_list **alst, t_lexer *lexer) { token_append(token, lexer, 0, 0); lexer->pos++; - return (lexer_lex(&(*alst)->next, lexer)); + return (lexer_lex(alst, lexer)); } else { diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index e847841a..3ccd3935 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/10 12:12:24 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 13:13:51 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From 7dd8b1861b76bcc8ce59573eabe92e2c48b610a8 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 10 Mar 2017 13:21:35 +0100 Subject: [PATCH 5/7] issue #78 execution side done --- 42sh/includes/exec.h | 2 +- 42sh/includes/parser.h | 1 - 42sh/src/exec/redirect_dless.c | 3 ++- 42sh/src/lexer/lexer_heredoc.c | 2 +- 42sh/src/main/main.c | 2 +- 42sh/src/parser/add_redir.c | 2 +- 42sh/src/parser/heredoc_parser.c | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index df7f3d02..71991239 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/10 13:10:03 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 13:19:04 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/includes/parser.h b/42sh/includes/parser.h index e0b83080..52efea58 100644 --- a/42sh/includes/parser.h +++ b/42sh/includes/parser.h @@ -161,7 +161,6 @@ struct s_redir int n; char *word; char *heredoc_data; - /* int close; */ }; struct s_cmd diff --git a/42sh/src/exec/redirect_dless.c b/42sh/src/exec/redirect_dless.c index 9a1a7873..67511e4f 100644 --- a/42sh/src/exec/redirect_dless.c +++ b/42sh/src/exec/redirect_dless.c @@ -17,7 +17,8 @@ int redirect_dless(t_redir *redir) char *str; pipe(fds); - str = redir->word; + str = redir->heredoc_data; + DG("[%s]", str); write(fds[PIPE_WRITE], str, ft_strlen(str)); close(fds[PIPE_WRITE]); dup2(fds[PIPE_READ], 0); diff --git a/42sh/src/lexer/lexer_heredoc.c b/42sh/src/lexer/lexer_heredoc.c index 7f0d9c63..966d2821 100644 --- a/42sh/src/lexer/lexer_heredoc.c +++ b/42sh/src/lexer/lexer_heredoc.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 15:51:17 by jhalford #+# #+# */ -/* Updated: 2017/03/08 23:19:57 by ariard ### ########.fr */ +/* Updated: 2017/03/10 13:16:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 3ccd3935..0d9b5b15 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/10 13:13:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 13:19:01 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_redir.c b/42sh/src/parser/add_redir.c index 85d0e6cd..28bd864c 100644 --- a/42sh/src/parser/add_redir.c +++ b/42sh/src/parser/add_redir.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 16:39:05 by ariard #+# #+# */ -/* Updated: 2017/03/09 18:29:45 by ariard ### ########.fr */ +/* Updated: 2017/03/10 13:19:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/heredoc_parser.c b/42sh/src/parser/heredoc_parser.c index 888bb42f..6ed353a9 100644 --- a/42sh/src/parser/heredoc_parser.c +++ b/42sh/src/parser/heredoc_parser.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 16:21:05 by ariard #+# #+# */ -/* Updated: 2017/03/09 17:34:31 by ariard ### ########.fr */ +/* Updated: 2017/03/10 13:19:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ From 9361d965bcf02fd5fc35878468aa7c87b61c76b2 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 10 Mar 2017 13:29:00 +0100 Subject: [PATCH 6/7] multiline heredoc in history --- 42sh/includes/exec.h | 2 +- 42sh/src/lexer/lexer_heredoc.c | 2 +- 42sh/src/main/main.c | 7 ++++++- 42sh/src/parser/heredoc_parser.c | 20 +++++++++++++------- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 71991239..fab2584b 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/10 13:19:04 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 13:26:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/lexer/lexer_heredoc.c b/42sh/src/lexer/lexer_heredoc.c index 966d2821..67cfd884 100644 --- a/42sh/src/lexer/lexer_heredoc.c +++ b/42sh/src/lexer/lexer_heredoc.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 15:51:17 by jhalford #+# #+# */ -/* Updated: 2017/03/10 13:16:54 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 13:27:32 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 0d9b5b15..a71a1ebc 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/10 13:19:01 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 13:28:24 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,6 +36,11 @@ int handle_instruction(int fd) return (parser.state == UNDEFINED ? error_EOF(&token, &parser, &ast) : 1); } + if (lexer.state == HEREDOC) + { + ft_strappend(&lexer.str, (char[]){'\n', 0}); + lexer.pos++; + } ft_strappend(&lexer.str, str); if (get_lexer_stack(lexer) == BACKSLASH) pop(&lexer.stack); diff --git a/42sh/src/parser/heredoc_parser.c b/42sh/src/parser/heredoc_parser.c index 6ed353a9..6da6ff5e 100644 --- a/42sh/src/parser/heredoc_parser.c +++ b/42sh/src/parser/heredoc_parser.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 16:21:05 by ariard #+# #+# */ -/* Updated: 2017/03/10 13:19:06 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 13:26:15 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,15 +26,21 @@ int pop_heredoc(t_list **lst) head = data_singleton()->heredoc_queue->content; temp = data_singleton()->heredoc_queue; DG("compare %s with %s", (char *)token->data, head->word); - if (head && token && ft_strcmp((char *)token->data, head->word) == 0) + if (head && token) { DG(); - temp2 = temp->next; - free(temp); - data_singleton()->heredoc_queue = temp2; + if (ft_strcmp((char *)token->data, head->word) == 0) + { + temp2 = temp->next; + free(temp); + data_singleton()->heredoc_queue = temp2; + } + else + { + DG("joining [%s] to heredoc", (char*)token->data); + head->heredoc_data = ft_strjoin(head->heredoc_data, token->data); + } } - else if (token) - head->heredoc_data = ft_strjoin(head->heredoc_data, token->data); ft_lstdel(lst, &token_free); return (1); } From d051ccc127e981f720d70d0f6820c4758b745a75 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 10 Mar 2017 14:01:39 +0100 Subject: [PATCH 7/7] redirection tweaks --- 42sh/includes/exec.h | 2 +- 42sh/src/exec/exec_reset.c | 14 +++++++------- 42sh/src/exec/fd_is_valid.c | 12 +++++++----- 42sh/src/exec/redirect_greatand.c | 11 ++++++----- 42sh/src/exec/redirect_lessand.c | 11 ++++++----- 42sh/src/lexer/lexer_greatand.c | 2 +- 42sh/src/main/data_init.c | 2 +- 42sh/src/main/main.c | 2 +- 42sh/src/parser/add_redir.c | 2 +- 42sh/src/parser/heredoc_parser.c | 2 +- 10 files changed, 32 insertions(+), 28 deletions(-) diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index fab2584b..dc8504c9 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/10 13:26:16 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 13:31:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/exec/exec_reset.c b/42sh/src/exec/exec_reset.c index fd0e7a66..354b3cde 100644 --- a/42sh/src/exec/exec_reset.c +++ b/42sh/src/exec/exec_reset.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 14:31:42 by jhalford #+# #+# */ -/* Updated: 2017/03/10 13:13:49 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 13:48:19 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,13 +21,13 @@ int exec_reset(void) /* exec->fd_save[1] = 1;//fcntl(STDOUT, F_DUPFD_CLOEXEC); */ /* exec->fd_save[2] = 1;//fcntl(STDERR, F_DUPFD_CLOEXEC); */ DG("check 0"); - if ((exec->fd_save[0] = fcntl(STDIN, F_DUPFD_CLOEXEC, 10)) == -1) - ft_dprintf(2, "{red}%s: internal fcntl error errno=%i %s{eoc}\n", SHELL_NAME, errno); + if ((exec->fd_save[0] = fcntl(STDIN, F_DUPFD_CLOEXEC, 10)) == -1 && errno != EBADF) + ft_dprintf(2, "{red}%s: internal fcntl STDIN error errno=%i %s{eoc}\n", SHELL_NAME, errno); DG("check 1"); - if ((exec->fd_save[1] = fcntl(STDOUT, F_DUPFD_CLOEXEC, 10)) == -1) - ft_dprintf(2, "{red}%s: internal fcntl error errno=%i %s{eoc}\n", SHELL_NAME, errno); - if ((exec->fd_save[2] = fcntl(STDERR, F_DUPFD_CLOEXEC, 10)) == -1) - ft_dprintf(2, "{red}%s: internal fcntl error errno=%i %s{eoc}\n", SHELL_NAME, errno); + if ((exec->fd_save[1] = fcntl(STDOUT, F_DUPFD_CLOEXEC, 10)) == -1 && errno != EBADF) + ft_dprintf(2, "{red}%s: internal fcntl STDOUT error errno=%i %s{eoc}\n", SHELL_NAME, errno); + if ((exec->fd_save[2] = fcntl(STDERR, F_DUPFD_CLOEXEC, 10)) == -1 && errno != EBADF) + ft_dprintf(2, "{red}%s: internal fcntl STDERR error errno=%i %s{eoc}\n", SHELL_NAME, errno); DG("saved [%i:%i:%i]", exec->fd_save[0], exec->fd_save[1], exec->fd_save[2]); exec->op_stack = NULL; exec->fdin = STDIN; diff --git a/42sh/src/exec/fd_is_valid.c b/42sh/src/exec/fd_is_valid.c index 39fac840..b0347bcf 100644 --- a/42sh/src/exec/fd_is_valid.c +++ b/42sh/src/exec/fd_is_valid.c @@ -6,16 +6,18 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/03 13:46:40 by jhalford #+# #+# */ -/* Updated: 2017/03/06 16:58:38 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 13:59:28 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int fd_is_valid(int fd, int has_flag) +int fd_is_valid(int fd, int has_mode) { - int flags; + int fd_flags; + int fd_modes; - flags = fcntl(fd, F_GETFD); - return ((flags != -1 || errno != EBADF) && flags & has_flag); + fd_modes = fcntl(fd, F_GETFL); + fd_flags = fcntl(fd, F_GETFD); + return ((fd_flags != -1 || errno != EBADF) && fd_modes & has_mode); } diff --git a/42sh/src/exec/redirect_greatand.c b/42sh/src/exec/redirect_greatand.c index d2bf02aa..ad449c2b 100644 --- a/42sh/src/exec/redirect_greatand.c +++ b/42sh/src/exec/redirect_greatand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:12:31 by jhalford #+# #+# */ -/* Updated: 2017/03/06 16:54:43 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 14:01:06 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,14 +17,14 @@ int redirect_greatand(t_redir *redir) int fdold; int fdnew; - if (ft_strcmp(redir->word, "-")) + if (ft_strcmp(redir->word, "-") == 0) { close(redir->n); return (0); } if (!ft_stris(redir->word, ft_isdigit)) { - ft_dprintf(2, "%s: %s: can only be digits", SHELL_NAME, redir->word); + ft_dprintf(2, "{red}%s: %s: can only be digits{eoc}\n", SHELL_NAME, redir->word); return (1); } fdold = ft_atoi(redir->word); @@ -33,9 +33,10 @@ int redirect_greatand(t_redir *redir) return (0); if (fdold > 9) return (bad_fd(fdold)); - if (fd_is_valid(fdold, O_RDONLY)) + if (fd_is_valid(fdold, O_RDONLY | O_RDWR)) dup2_close(fdold, fdnew); else - return (bad_fd(fdold)); + close(fdnew); + /* return (bad_fd(fdold)); */ return (0); } diff --git a/42sh/src/exec/redirect_lessand.c b/42sh/src/exec/redirect_lessand.c index 9c184b82..30f11cdc 100644 --- a/42sh/src/exec/redirect_lessand.c +++ b/42sh/src/exec/redirect_lessand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/06 22:11:18 by jhalford #+# #+# */ -/* Updated: 2017/03/06 16:53:29 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 14:00:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,14 +17,14 @@ int redirect_lessand(t_redir *redir) int fdold; int fdnew; - if (ft_strcmp(redir->word, "-")) + if (ft_strcmp(redir->word, "-") == 0) { close(redir->n); return (0); } if (!ft_stris(redir->word, ft_isdigit)) { - ft_dprintf(2, "%s: %s: can only be digits", SHELL_NAME, redir->word); + ft_dprintf(2, "{red}%s: %s: can only be digits{eoc}\n", SHELL_NAME, redir->word); return (1); } fdold = ft_atoi(redir->word); @@ -33,9 +33,10 @@ int redirect_lessand(t_redir *redir) return (0); if (fdold > 9) return (bad_fd(fdold)); - if (fd_is_valid(fdold, O_WRONLY)) + if (fd_is_valid(fdold, O_WRONLY | O_RDWR)) dup2_close(fdold, fdnew); else - return (bad_fd(fdold)); + close(fdnew); + /* return (bad_fd(fdold)); */ return (0); } diff --git a/42sh/src/lexer/lexer_greatand.c b/42sh/src/lexer/lexer_greatand.c index c7f17888..e3bccbe7 100644 --- a/42sh/src/lexer/lexer_greatand.c +++ b/42sh/src/lexer/lexer_greatand.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:56:58 by jhalford #+# #+# */ -/* Updated: 2017/03/08 23:34:08 by ariard ### ########.fr */ +/* Updated: 2017/03/10 13:45:47 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/data_init.c b/42sh/src/main/data_init.c index 43442aa4..4aa0ab28 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/10 12:12:26 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 13:48:20 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index a71a1ebc..c9a68cc8 100644 --- a/42sh/src/main/main.c +++ b/42sh/src/main/main.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/06 18:40:58 by jhalford #+# #+# */ -/* Updated: 2017/03/10 13:28:24 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 13:31:16 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/add_redir.c b/42sh/src/parser/add_redir.c index 28bd864c..45080542 100644 --- a/42sh/src/parser/add_redir.c +++ b/42sh/src/parser/add_redir.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/17 16:39:05 by ariard #+# #+# */ -/* Updated: 2017/03/10 13:19:14 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 13:45:42 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/parser/heredoc_parser.c b/42sh/src/parser/heredoc_parser.c index 6da6ff5e..b8d77513 100644 --- a/42sh/src/parser/heredoc_parser.c +++ b/42sh/src/parser/heredoc_parser.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/08 16:21:05 by ariard #+# #+# */ -/* Updated: 2017/03/10 13:26:15 by jhalford ### ########.fr */ +/* Updated: 2017/03/10 13:33:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */