From 890fdd1511549ff9886249c66396c5f2fef0a6de Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Fri, 24 Mar 2017 19:17:40 +0100 Subject: [PATCH 1/5] oubli while_clause and tk_paren_open close #188 --- 42sh/42ShellTester/log | 234 +------------------------------- 42sh/VAR | 23 ++++ 42sh/src/lexer/token_print.c | 3 +- 42sh/src/main/main.c | 3 +- 42sh/src/parser/aggregate_sym.c | 5 +- 42sh/src/parser/eval_sym.c | 4 +- 6 files changed, 36 insertions(+), 236 deletions(-) create mode 100644 42sh/VAR diff --git a/42sh/42ShellTester/log b/42sh/42ShellTester/log index 4bca6790..b5966b63 100644 --- a/42sh/42ShellTester/log +++ b/42sh/42ShellTester/log @@ -1,235 +1,5 @@ -.x....................xxx...xxxx..x. - ----------------------------------------------------------------- - -21sh/misc/002-simple-command-line (FAILED) - - Description: - The purpose of this test is to check that the Shell is able to execute a simple command line that contains separators `;`, pipes `|`, and a right redirection `>`. - - Before test: - 01: rm -rf "./size" - 02: rm -rf "TOKEN201703241733" - 03: echo '^'$(echo TOKEN201703241733_FILE_TOKEN201703241733_STDOUT | wc -c)'$' > "./size" - - STDIN: - 01: mkdir TOKEN201703241733 ; cd TOKEN201703241733 ; touch TOKEN201703241733_FILE ; ls -1 ; ls | cat | wc -c > TOKEN201703241733_STDOUT ; cat TOKEN201703241733_STDOUT - - STDOUT: - SUCCESS expected_to match_regex `TOKEN201703241733_FILE$` - FAILURE expected_to match_each_regex_of_file `./size` - 01: TOKEN201703241733_FILE - 02: 94296 redirect_great.c 24 3 1 - 03: 23 - - STDERR: - SUCCESS expected_to be_empty - (no output) - ----------------------------------------------------------------- - -21sh/redirections/outputs/truncating/001-creates-file-if-not-exits (FAILED) - - Description: - The right redirection `>` opens the file with the oflag `O_CREAT` so that the file is created if it does not exists. - - Before test: - 01: rm -f new_file - - STDIN: - 01: ./write_on_stdout TOKEN201703241733 >new_file - - STDOUT: - SUCCESS expected_to_not match_regex `TOKEN201703241733` - (no output) - - STDERR: - SUCCESS expected_to be_empty - (no output) - - MISC: - SUCCESS expected_to create_file `new_file` matching_regex `TOKEN201703241733` - FAILURE expected_to create_file `new_file` with_nb_of_lines `1` - ----------------------------------------------------------------- - -21sh/redirections/outputs/truncating/002-truncates-file-if-exists (FAILED) - - Description: - The right redirection `>` opens the file with the oflag `O_TRUNC` so that the file size is truncated to 0 before writing in it. - - Before test: - 01: ./write_on_stdout TOKEN201703241733_first >truncated_file - - STDIN: - 01: ./write_on_stdout TOKEN201703241733_second >truncated_file - - STDOUT: - SUCCESS expected_to_not match_regex TOKEN201703241733_second - (no output) - - STDERR: - SUCCESS expected_to be_empty - (no output) - - MISC: - SUCCESS expected_to create_file `truncated_file` matching_regex `TOKEN201703241733_second` - SUCCESS expected_to create_file `truncated_file` not_matching_regex `TOKEN201703241733_first` - FAILURE expected_to create_file `truncated_file` with_nb_of_lines `1` - ----------------------------------------------------------------- - -21sh/redirections/outputs/truncating/003-whitespace-before-filename (FAILED) - - Description: - The file name for a right redirection can be specified closely to the character `>` or in a separated field. - - Before test: - 01: rm -f "new_file" - - STDIN: - 01: ./write_on_stdout TOKEN201703241733 > new_file - - STDOUT: - SUCCESS expected_to_not match_regex `TOKEN201703241733` - (no output) - - STDERR: - SUCCESS expected_to be_empty - (no output) - - MISC: - SUCCESS expected_to create_file `new_file` matching_regex `TOKEN201703241733` - FAILURE expected_to create_file `new_file` with_nb_of_lines `1` - ----------------------------------------------------------------- - -21sh/redirections/outputs/truncating/multiple/004-together (FAILED) - - Description: - A right redirection can be associated to the twice outputs by using `&>...`, that means `redirect stdout and stderr to ...`. - - Before test: - 01: rm -f "new_file_stderr_and_stdout" - - STDIN: - 01: ./write_on_stdout_and_stderr TOKEN201703241733_1 TOKEN201703241733_2 &>new_file_stderr_and_stdout - - STDOUT: - FAILURE expected_to_not match_regex `TOKEN201703241733_1` - SUCCESS expected_to_not match_regex `TOKEN201703241733_2` - 01: TOKEN201703241733_1 - - STDERR: - SUCCESS expected_to_not match_regex `TOKEN201703241733_1` - FAILURE expected_to_not match_regex `TOKEN201703241733_2` - 01: TOKEN201703241733_2 - - MISC: - FAILURE expected_to create_file `new_file_stderr_and_stdout` matching_regex `TOKEN201703241733_1$` - FAILURE expected_to create_file `new_file_stderr_and_stdout` matching_regex `TOKEN201703241733_2$` - ----------------------------------------------------------------- - -21sh/redirections/outputs/truncating/multiple/005-together-with-whitespaces (FAILED) - - Description: - A right redirection can be associated to the twice outputs by using `&>...`, that means `redirect stdout and stderr to ...`. - In this test, we specify the file name in a separate field. - - Before test: - 01: rm -f new_file_stderr_and_stdout - - STDIN: - 01: ./write_on_stdout_and_stderr TOKEN201703241733_1 TOKEN201703241733_2 &> new_file_stderr_and_stdout - - STDOUT: - FAILURE expected_to_not match_regex `TOKEN201703241733_1` - SUCCESS expected_to_not match_regex `TOKEN201703241733_2` - 01: TOKEN201703241733_1 - - STDERR: - SUCCESS expected_to_not match_regex `TOKEN201703241733_1` - FAILURE expected_to_not match_regex `TOKEN201703241733_2` - 01: TOKEN201703241733_2 - - MISC: - FAILURE expected_to create_file `new_file_stderr_and_stdout` matching_regex `TOKEN201703241733_1$` - FAILURE expected_to create_file `new_file_stderr_and_stdout` matching_regex `TOKEN201703241733_2$` - ----------------------------------------------------------------- - -21sh/redirections/outputs/truncating/stderr/001-works (FAILED) - - Description: - The purpose of this test is to check that redirecting the standard error STDERR to a file `2>` works. In this test, the binary writes a token on each standard and error output, so that only the STDOUT is outputted and STDERR is written in a file `new_file_stderr`. - - Before test: - 01: rm -f "./new_file_stderr" - - STDIN: - 01: ./write_on_stdout_and_stderr TOKEN201703241733_STDOUT TOKEN201703241733_STDERR 2>new_file_stderr - - STDOUT: - SUCCESS expected_to match_regex `TOKEN201703241733_STDOUT` - 01: TOKEN201703241733_STDOUT - - STDERR: - SUCCESS expected_to_not match_regex `TOKEN201703241733_STDERR` - (no output) - - MISC: - SUCCESS expected_to create_file `new_file_stderr` matching_regex `TOKEN201703241733_STDERR` - FAILURE expected_to create_file `new_file_stderr` with_nb_of_lines 1 - ----------------------------------------------------------------- - -21sh/redirections/outputs/truncating/stdout/001-with-explicit-fd (FAILED) - - Description: - The purpose of this test is to check if '1>' redirect STDIN to a file. - We are using echo with one argument. The output should NOT return anything on the standard output. - - Before test: - 01: rm -f new_file - - STDIN: - 01: /bin/echo TOKEN1 1>new_file - - STDOUT: - SUCCESS expected_to_not match_regex TOKEN1 - (no output) - - STDERR: - (no output) - - MISC: - SUCCESS expected_to create_file new_file matching_regex TOKEN1 - FAILURE expected_to create_file new_file with_nb_of_lines 1 - ----------------------------------------------------------------- - -21sh/separators/semicolon/003-parse-error-empty-inline-command (FAILED) - - Description: - The purpose of this test is to check that using the simicolon separator `;` with empty commands results in error. - - STDIN: - 01: ./write_on_stdout TOKEN201703241733 ; ; ./exit_with_status 42 - - STDOUT: - SUCCESS expected_to be_empty - (no output) - - STDERR: - SUCCESS expected_to_not be_empty - SUCCESS might match_regex `([Ss]yntax|[Pp]arse) error` - 01: syntax error near unexpected token `;' - - MISC: - SUCCESS expected_to_not exit_with_status `42` - FAILURE expected_to_not exit_with_status `0` +.................................... Total tests: 36 -Total failed tests: 9 +Total failed tests: 0 Total pending tests: 0 diff --git a/42sh/VAR b/42sh/VAR new file mode 100644 index 00000000..7813ba73 --- /dev/null +++ b/42sh/VAR @@ -0,0 +1,23 @@ +$TOKEN201703241737_NAME +21sh_error +42ShellTester +42sh +42sh_error +Makefile +README.md +STDBUG +VAR +auteur +donovan_segaults_06-02 +file +includes +libft +minishell_error +new_file +objs +pdf +sample +scriptheader.sh +src +update_makefile.sh +write_on_stdout_and_stderr diff --git a/42sh/src/lexer/token_print.c b/42sh/src/lexer/token_print.c index 6579d827..d43d2fca 100644 --- a/42sh/src/lexer/token_print.c +++ b/42sh/src/lexer/token_print.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */ -/* Updated: 2017/03/24 17:24:09 by gwojda ### ########.fr */ +/* Updated: 2017/03/24 18:59:40 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,7 @@ void token_print(t_list *lst) { if (lst->content) token = lst->content; + DG("%s", read_state(token->type)); lst = lst->next; } } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 8f84dbe6..cff79553 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/24 17:24:04 by gwojda ### ########.fr */ +/* Updated: 2017/03/24 18:58:30 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,6 +43,7 @@ static int handle_instruction(t_list **token, t_btree **ast) return (ret); if (do_lexer_routine(token, stream) > 0) continue ; + token_print(*token); if ((ret = do_parser_routine(token, ast)) == 1 && SH_NO_INTERACTIVE(data->opts)) return (ret); diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index e5e9c7bb..a3b7519b 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/11 15:58:38 by ariard #+# #+# */ -/* Updated: 2017/03/24 17:23:26 by gwojda ### ########.fr */ +/* Updated: 2017/03/24 19:09:05 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -380,15 +380,18 @@ int aggregate_sym(t_list **stack, t_sym *new_sym, t_parstate *state) return (1); i = -1; head = (*stack)->content; + 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("MATH: %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)); } if (eval_sym(stack, *new_sym) && !(*state = ERROR)) return (1); diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index e8fbda17..f3483d14 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/11 16:11:21 by ariard #+# #+# */ -/* Updated: 2017/03/24 17:23:52 by gwojda ### ########.fr */ +/* Updated: 2017/03/24 19:11:10 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -905,6 +905,7 @@ static t_stackmatch g_stackmatch[] = {WHILE_CLAUSE, NEWLINE_LIST}, {WHILE_CLAUSE, SEQUENCE}, {WHILE_CLAUSE, TK_DO}, + {WHILE_CLAUSE, TK_PAREN_OPEN}, {WHILE_CLAUSE, TK_PAREN_CLOSE}, {WHILE_CLAUSE, TK_WHILE}, {WHILE_CLAUSE, TK_LBRACE}, @@ -1287,6 +1288,7 @@ int eval_sym(t_list **stack, t_sym new_sym) return (1); head = (*stack)->content; i = 0; + DG("eval head %s && sym %s", read_state(*head), read_state(new_sym)); while (g_stackmatch[i].top) { if (new_sym == g_stackmatch[i].top && *head == g_stackmatch[i].under) From eeae2232a5b1ca48810ae27386ebbc48edfaed58 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 24 Mar 2017 19:21:51 +0100 Subject: [PATCH 2/5] issue #177 --- 42sh/libft/src/sys/fd_replace.c | 3 ++- 42sh/src/exec/exec_leaf.c | 3 ++- 42sh/src/exec/exec_reset.c | 4 ++-- 42sh/src/exec/exec_var.c | 3 ++- 42sh/src/exec/process_launch.c | 5 +---- 42sh/src/exec/process_set.c | 6 +++--- 42sh/src/job_control/builtin_jobs.c | 5 ++++- 42sh/src/lexer/token_print.c | 5 ++++- 42sh/src/main/main.c | 7 +++++-- 42sh/src/main/shell_init.c | 7 ++----- 10 files changed, 27 insertions(+), 21 deletions(-) diff --git a/42sh/libft/src/sys/fd_replace.c b/42sh/libft/src/sys/fd_replace.c index 2e22e75e..aac43597 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/22 17:22:55 by gwojda ### ########.fr */ +/* Updated: 2017/03/24 19:09:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ int fd_replace(int fd1, int fd2) { + DG("%i ----> %i", fd2, fd1); if (fd1 != fd2) return (dup2_close(fd1, fd2)); return (0); diff --git a/42sh/src/exec/exec_leaf.c b/42sh/src/exec/exec_leaf.c index 9fb93b0e..5efe775d 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/22 21:48:12 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 19:14:08 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,6 +43,7 @@ int exec_leaf(t_btree **ast) job_addprocess(&p); if (IS_PIPEEND(p)) { + DG("end of pipe"); if (JOB_IS_FG(job->attrs)) put_job_in_foreground(job, 0); else diff --git a/42sh/src/exec/exec_reset.c b/42sh/src/exec/exec_reset.c index f2fd8909..36ea6ae0 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/24 18:23:49 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 18:41:14 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ int exec_reset(void) jobc = &data_singleton()->jobc; i = -1; while (++i < 10) - exec->fd_save[i] = fcntl(i, F_DUPFD, 10); + exec->fd_save[i] = fcntl(i, F_DUPFD_CLOEXEC, 10); exec->op_stack = NULL; exec->fdin = STDIN; exec->attrs = 0; diff --git a/42sh/src/exec/exec_var.c b/42sh/src/exec/exec_var.c index 4a227eb0..bab39c69 100644 --- a/42sh/src/exec/exec_var.c +++ b/42sh/src/exec/exec_var.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 11:12:05 by ariard #+# #+# */ -/* Updated: 2017/03/24 15:40:00 by ariard ### ########.fr */ +/* Updated: 2017/03/24 19:20:40 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,7 @@ int exec_var(t_btree **ast) char *equal; node = (*ast)->item; + pop(&data_singleton()->exec.op_stack); if (!(av = token_to_argv(node->data.cmd.token, 1))) return (0); var = av[0]; diff --git a/42sh/src/exec/process_launch.c b/42sh/src/exec/process_launch.c index eb01f277..ef7724fc 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 18:22:16 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 19:21:07 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,10 +41,7 @@ int process_launch(t_process *p) if (process_redirect(p)) set_exitstatus(1, 1); else - { - exec_reset(); p->map.launch(p); - } shell_resetfds(); shell_resetsig(); process_free(p, 0); diff --git a/42sh/src/exec/process_set.c b/42sh/src/exec/process_set.c index de3b37f1..be240f3f 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/22 18:26:37 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 19:19:34 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -68,7 +68,7 @@ int process_set(t_process *p, t_btree *ast) p->pid = 0; exec->fdin = fds[PIPE_READ]; if (ast) - p->redirs = ft_lstmap(((t_astnode *)ast->item)->data.cmd.redir, - &redir_copy); + p->redirs = ft_lstmap( + ((t_astnode *)ast->item)->data.cmd.redir, &redir_copy); return ((!ast) ? 0 : process_set_spec(p, ast)); } diff --git a/42sh/src/job_control/builtin_jobs.c b/42sh/src/job_control/builtin_jobs.c index 68ae3c86..a23b8184 100644 --- a/42sh/src/job_control/builtin_jobs.c +++ b/42sh/src/job_control/builtin_jobs.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/15 17:43:01 by jhalford #+# #+# */ -/* Updated: 2017/03/24 18:17:02 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 18:48:46 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -67,7 +67,10 @@ int builtin_jobs(const char *path, char *const av[], char *const envp[]) (void)path; (void)envp; if (!SH_HAS_JOBC(data_singleton()->opts)) + { + DG("no job control :("); return (SH_ERR("jobs: %s", SH_MSG_NOJOBC)); + } do_job_notification(); ft_bzero(&data, sizeof(t_data_template)); if (cliopts_get((char**)av, g_jobs_opts, &data)) diff --git a/42sh/src/lexer/token_print.c b/42sh/src/lexer/token_print.c index 6579d827..68c61e64 100644 --- a/42sh/src/lexer/token_print.c +++ b/42sh/src/lexer/token_print.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:39:01 by jhalford #+# #+# */ -/* Updated: 2017/03/24 17:24:09 by gwojda ### ########.fr */ +/* Updated: 2017/03/24 18:39:53 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,10 @@ void token_print(t_list *lst) while (lst) { if (lst->content) + { token = lst->content; + DG("token : %s data [%s]", read_state(token->type), token->data); + } lst = lst->next; } } diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index fd66ee2b..80e87820 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/24 18:25:10 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 18:55:21 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,13 +46,15 @@ static int handle_instruction(t_list **token, t_btree **ast) return (ret); if (do_lexer_routine(token, stream) > 0) continue ; + /* token_print(*token); */ + /* exit(1); */ if ((ret = do_parser_routine(token, ast)) == 1 && SH_NO_INTERACTIVE(data->opts)) return (ret); else if (ret > 0) break ; } - /* btree_print(3, *ast, ft_putast); */ + btree_print(3, *ast, ft_putast); /* exit(1); */ if (data->parser.state == SUCCESS && ft_exec(ast) < 0) exit(1); @@ -71,6 +73,7 @@ int main(int ac, char **av, char **env) t_btree *ast; g_argv = av; + DG("----------------------"); if (shell_init(ac, av, env) != 0) return (1); token = NULL; diff --git a/42sh/src/main/shell_init.c b/42sh/src/main/shell_init.c index d3a5dc78..45d9092c 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/24 17:11:20 by wescande ### ########.fr */ +/* Updated: 2017/03/24 18:57:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -87,10 +87,7 @@ int shell_init(int ac, char **av, char **env) if (data_init(ac, av, env) < 0) return (-1); if (cliopts_get(av, g_opts, data)) - { - ft_perror(NULL); - return (SH_ERR("usage: %s", SHELL_USAGE)); - } + return (ft_perror(NULL) && SH_ERR("usage: %s", SHELL_USAGE)); if (!isatty(STDIN) || *data->av_data) data->opts &= ~(SH_INTERACTIVE | SH_OPTS_JOBC); if ((data->fd = get_input_fd(data, NULL)) < 0) From 5d07da9fb3743638c273b21e8a2d5ad3a2252e99 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Fri, 24 Mar 2017 19:44:04 +0100 Subject: [PATCH 3/5] close #189 close #190 --- 42sh/src/exec/exec_var.c | 3 ++- 42sh/src/parser/add_cmd.c | 4 ++-- 42sh/src/parser/aggregate_sym.c | 9 +++++---- 42sh/src/parser/eval_sym.c | 5 +++-- 42sh/src/parser/ft_parse.c | 3 ++- 5 files changed, 14 insertions(+), 10 deletions(-) diff --git a/42sh/src/exec/exec_var.c b/42sh/src/exec/exec_var.c index 4a227eb0..2660bef4 100644 --- a/42sh/src/exec/exec_var.c +++ b/42sh/src/exec/exec_var.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 11:12:05 by ariard #+# #+# */ -/* Updated: 2017/03/24 15:40:00 by ariard ### ########.fr */ +/* Updated: 2017/03/24 19:43:40 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,5 +29,6 @@ int exec_var(t_btree **ast) value = equal ? equal + 1 : NULL; builtin_setenv("internal", (char*[]){"local", var, value, 0}, NULL); ft_sstrfree(av); + ft_exec(&(*ast)->right); return (0); } diff --git a/42sh/src/parser/add_cmd.c b/42sh/src/parser/add_cmd.c index 3465e91c..b1c71b6e 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/24 16:56:50 by ariard ### ########.fr */ +/* Updated: 2017/03/24 19:25:02 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,7 +59,7 @@ static int emblematic_token(t_btree **ast, t_list **lst) return (0); if (node->type != TK_DO && node->type != TK_THEN && node->type != CMD && node->type != REDIR - && node->type != TK_ASSIGNMENT_WORD && node->type != TK_FOR) + && node->type != TK_FOR) return (1); } return (0); diff --git a/42sh/src/parser/aggregate_sym.c b/42sh/src/parser/aggregate_sym.c index a3b7519b..0a2dd092 100644 --- a/42sh/src/parser/aggregate_sym.c +++ b/42sh/src/parser/aggregate_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/11 15:58:38 by ariard #+# #+# */ -/* Updated: 2017/03/24 19:09:05 by ariard ### ########.fr */ +/* Updated: 2017/03/24 19:26:26 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -380,18 +380,15 @@ int aggregate_sym(t_list **stack, t_sym *new_sym, t_parstate *state) return (1); i = -1; head = (*stack)->content; - 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("MATH: %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)); } if (eval_sym(stack, *new_sym) && !(*state = ERROR)) return (1); @@ -400,3 +397,7 @@ int aggregate_sym(t_list **stack, t_sym *new_sym, t_parstate *state) } return (0); } + +// DG("aggregate head %s && sym %s", read_state(*head), read_state(*new_sym)); +// DG("MATH: %s", read_state(g_aggrematch[i].new_sym)); +// DG("stack after pop %s", read_state(*head)); diff --git a/42sh/src/parser/eval_sym.c b/42sh/src/parser/eval_sym.c index f3483d14..dfd9e6cb 100644 --- a/42sh/src/parser/eval_sym.c +++ b/42sh/src/parser/eval_sym.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/11 16:11:21 by ariard #+# #+# */ -/* Updated: 2017/03/24 19:11:10 by ariard ### ########.fr */ +/* Updated: 2017/03/24 19:25:44 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -1288,7 +1288,6 @@ int eval_sym(t_list **stack, t_sym new_sym) return (1); head = (*stack)->content; i = 0; - DG("eval head %s && sym %s", read_state(*head), read_state(new_sym)); while (g_stackmatch[i].top) { if (new_sym == g_stackmatch[i].top && *head == g_stackmatch[i].under) @@ -1297,3 +1296,5 @@ int eval_sym(t_list **stack, t_sym new_sym) } return (1); } + +//DG("eval head %s && sym %s", read_state(*head), read_state(new_sym)); diff --git a/42sh/src/parser/ft_parse.c b/42sh/src/parser/ft_parse.c index 08d51084..6d544b9a 100644 --- a/42sh/src/parser/ft_parse.c +++ b/42sh/src/parser/ft_parse.c @@ -6,7 +6,7 @@ /* By: ariard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/11 16:17:38 by ariard #+# #+# */ -/* Updated: 2017/03/24 17:01:05 by ariard ### ########.fr */ +/* Updated: 2017/03/24 19:26:48 by ariard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,6 +49,7 @@ int ft_parse(t_btree **ast, t_list **token, t_parser *parser) parser->state = SUCCESS; else parser->state = UNDEFINED; + btree_print(STDBUG, *ast, &ft_putast); build_tree(ast, token); if ((end_instruction(&parser->stack) && !(*token)->next)) insert_linebreak(token); From 1aebf7531a4508cd011cabb74304e43378138ee6 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 24 Mar 2017 20:11:24 +0100 Subject: [PATCH 4/5] redirection while works --- 42sh/includes/exec.h | 6 +++-- 42sh/libft/Makefile | 1 + 42sh/libft/includes/libft.h | 2 +- 42sh/libft/includes/lst.h | 3 ++- 42sh/libft/src/lst/pop.c | 2 +- 42sh/libft/src/lst/top.c | 18 ++++++++++++++ 42sh/libft/src/sys/dup2_close.c | 6 ++--- 42sh/libft/src/sys/fd_replace.c | 6 +++-- 42sh/src/exec/exec_leaf.c | 3 +-- 42sh/src/exec/exec_reset.c | 33 ++++++++++++++++++++----- 42sh/src/exec/process_launch.c | 41 ++++++++++++++++++-------------- 42sh/src/exec/redirect_great.c | 9 ++++--- 42sh/src/lexer/get_lexer_stack.c | 2 +- 42sh/src/main/main.c | 2 +- 42sh/src/main/shell_reset.c | 8 ++++--- 15 files changed, 98 insertions(+), 44 deletions(-) create mode 100644 42sh/libft/src/lst/top.c diff --git a/42sh/includes/exec.h b/42sh/includes/exec.h index 2d24813b..1bf1074b 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 14:47:12 by ariard ### ########.fr */ +/* Updated: 2017/03/24 20:07:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -131,7 +131,7 @@ struct s_job struct s_exec { t_job job; - int fd_save[10]; + t_list *fd_save[10]; t_flag attrs; int fdin; t_list *op_stack; @@ -140,6 +140,8 @@ struct s_exec }; int exec_reset(void); +int exec_pushfds(); +int exec_popfds(); int process_fork(t_process *p); int process_setgroup(t_process *p, pid_t pid); void process_setsig(void); diff --git a/42sh/libft/Makefile b/42sh/libft/Makefile index 48456c09..30571112 100644 --- a/42sh/libft/Makefile +++ b/42sh/libft/Makefile @@ -108,6 +108,7 @@ lst/ft_lstnew_range.c\ lst/ft_lstsort.c\ lst/pop.c\ lst/push.c\ +lst/top.c\ math/ft_addrcmp.c\ math/ft_ilen.c\ math/ft_ilen_base.c\ diff --git a/42sh/libft/includes/libft.h b/42sh/libft/includes/libft.h index 98477d16..c9baf04d 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/21 16:32:40 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:08:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/includes/lst.h b/42sh/libft/includes/lst.h index 166cf985..2b6a9e24 100644 --- a/42sh/libft/includes/lst.h +++ b/42sh/libft/includes/lst.h @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/07 13:27:46 by jhalford #+# #+# */ -/* Updated: 2017/03/11 16:18:00 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:09:05 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,7 @@ typedef struct s_list t_list; int pop(t_list **lst); t_list *push(t_list **stack, int elem); +int top(t_list *top); t_list *ft_lstnew(void const *content, size_t content_size); void ft_lstdel(t_list **alst, void (*del)(void *, size_t)); diff --git a/42sh/libft/src/lst/pop.c b/42sh/libft/src/lst/pop.c index 148d6d7c..df3be22e 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/03 16:49:59 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:05:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/libft/src/lst/top.c b/42sh/libft/src/lst/top.c new file mode 100644 index 00000000..2c3186e2 --- /dev/null +++ b/42sh/libft/src/lst/top.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* top.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/03/24 20:04:27 by jhalford #+# #+# */ +/* Updated: 2017/03/24 20:09:06 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int top(t_list *top) +{ + return (top ? *(int*)top->content : 0); +} diff --git a/42sh/libft/src/sys/dup2_close.c b/42sh/libft/src/sys/dup2_close.c index 5a7924a6..c1c10bcd 100644 --- a/42sh/libft/src/sys/dup2_close.c +++ b/42sh/libft/src/sys/dup2_close.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/02/07 16:07:28 by jhalford #+# #+# */ -/* Updated: 2017/02/07 16:15:52 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 19:33:48 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,9 +14,9 @@ int dup2_close(int fd1, int fd2) { - if (dup2(fd1, fd2)) + if (dup2(fd1, fd2) < 0) return (-1); - if (close(fd1)) + if (close(fd1) < 0) return (-1); return (0); } diff --git a/42sh/libft/src/sys/fd_replace.c b/42sh/libft/src/sys/fd_replace.c index aac43597..6af06a9e 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 19:09:44 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:08:12 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,8 +14,10 @@ int fd_replace(int fd1, int fd2) { - DG("%i ----> %i", fd2, fd1); if (fd1 != fd2) + { + DG("%i ---> %i", fd2, fd1); return (dup2_close(fd1, fd2)); + } return (0); } diff --git a/42sh/src/exec/exec_leaf.c b/42sh/src/exec/exec_leaf.c index 5efe775d..88ac6da0 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/24 19:14:08 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 19:24:44 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,7 +43,6 @@ int exec_leaf(t_btree **ast) job_addprocess(&p); if (IS_PIPEEND(p)) { - DG("end of pipe"); if (JOB_IS_FG(job->attrs)) put_job_in_foreground(job, 0); else diff --git a/42sh/src/exec/exec_reset.c b/42sh/src/exec/exec_reset.c index 36ea6ae0..07ab4d0b 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/24 18:41:14 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:09:13 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,17 +22,38 @@ int exec_reset_job(t_job *job) return (0); } +int exec_pushfds() +{ + 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() +{ + 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) { - t_exec *exec; t_jobc *jobc; - int i; + t_exec *exec; exec = &data_singleton()->exec; jobc = &data_singleton()->jobc; - i = -1; - while (++i < 10) - exec->fd_save[i] = fcntl(i, F_DUPFD_CLOEXEC, 10); + exec_pushfds(); exec->op_stack = NULL; exec->fdin = STDIN; exec->attrs = 0; diff --git a/42sh/src/exec/process_launch.c b/42sh/src/exec/process_launch.c index ef7724fc..ac107417 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 19:21:07 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:02:54 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,23 +35,28 @@ int process_fork(t_process *p) int process_launch(t_process *p) { p->state = PROCESS_RUNNING; - if (IS_PIPESINGLE(*p) - && p->type != PROCESS_FILE && p->type != PROCESS_SUBSHELL) + if (!IS_PIPESINGLE(*p) + || p->type == PROCESS_FILE + || p->type == PROCESS_SUBSHELL) { - if (process_redirect(p)) - set_exitstatus(1, 1); - else - p->map.launch(p); - shell_resetfds(); - shell_resetsig(); - process_free(p, 0); - return (0); + p->pid = process_fork(p); + process_setgroup(p, p->pid); + if (p->fdin != STDIN) + close(p->fdin); + if (p->fdout != STDOUT) + close(p->fdout); + return (1); } - p->pid = process_fork(p); - process_setgroup(p, p->pid); - if (p->fdin != STDIN) - close(p->fdin); - if (p->fdout != STDOUT) - close(p->fdout); - return (1); + if (process_redirect(p)) + set_exitstatus(1, 1); + else + { + exec_pushfds(); + p->map.launch(p); + } + exec_popfds(); + shell_resetfds(); + shell_resetsig(); + process_free(p, 0); + return (0); } diff --git a/42sh/src/exec/redirect_great.c b/42sh/src/exec/redirect_great.c index 5c39be5e..8f4fe029 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/24 18:25:41 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:09:09 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,10 @@ int redirect_great(t_redir *redir) fdnew = redir->n; if ((fdold = open(redir->word, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0) - exit(1); - dup2_close(fdold, fdnew); + { + SH_ERR("open(): %s", strerror(errno)); + exit (1); + } + fd_replace(fdold, fdnew); return (0); } diff --git a/42sh/src/lexer/get_lexer_stack.c b/42sh/src/lexer/get_lexer_stack.c index a3c91de3..8e04ad7a 100644 --- a/42sh/src/lexer/get_lexer_stack.c +++ b/42sh/src/lexer/get_lexer_stack.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/03 17:39:45 by jhalford #+# #+# */ -/* Updated: 2017/03/16 22:50:16 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:04:00 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/main.c b/42sh/src/main/main.c index 5788f10e..d96b37c6 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/24 19:23:34 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:02:55 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/main/shell_reset.c b/42sh/src/main/shell_reset.c index 125f30c6..dca33bfd 100644 --- a/42sh/src/main/shell_reset.c +++ b/42sh/src/main/shell_reset.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/22 16:07:14 by jhalford #+# #+# */ -/* Updated: 2017/03/22 17:49:38 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 20:09:29 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,12 +26,14 @@ void shell_resetfds(void) { t_exec *exec; int i; + int fd; exec = &data_singleton()->exec; i = -1; while (++i < 10) { - if (exec->fd_save[i] != -1) - dup2(exec->fd_save[i], i); + fd = top(exec->fd_save[i]); + if (fd != -1) + dup2(fd, i); } } From 79ac0a8edcb88a16ae4bc26d82f9900e840882f6 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Fri, 24 Mar 2017 23:05:52 +0100 Subject: [PATCH 5/5] unsetenv OK --- 42sh/libft/src/sys/fd_replace.c | 5 +-- 42sh/src/builtin/builtin_cd.c | 48 ++++++++++------------------- 42sh/src/builtin/builtin_setenv.c | 2 +- 42sh/src/builtin/builtin_unsetenv.c | 4 +-- 42sh/src/exec/plaunch_while.c | 2 +- 5 files changed, 22 insertions(+), 39 deletions(-) diff --git a/42sh/libft/src/sys/fd_replace.c b/42sh/libft/src/sys/fd_replace.c index 6af06a9e..495ae484 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 20:08:12 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 22:59:28 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,9 +15,6 @@ int fd_replace(int fd1, int fd2) { if (fd1 != fd2) - { - DG("%i ---> %i", fd2, fd1); return (dup2_close(fd1, fd2)); - } return (0); } diff --git a/42sh/src/builtin/builtin_cd.c b/42sh/src/builtin/builtin_cd.c index 5e96339f..4d40ae44 100644 --- a/42sh/src/builtin/builtin_cd.c +++ b/42sh/src/builtin/builtin_cd.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */ -/* Updated: 2017/03/21 15:28:51 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 23:03:58 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,6 +20,13 @@ #define CDERR_2 "cd: HOME not set" #define CDERR_3 "cd: too many arguments{eoc}" +static g_cliopts g_cdotps = +{ + {'P', NULL, CD_OPT_P, CD_OPT_L, NULL}, + {'L', NULL, CD_OPT_L, CD_OPT_P, NULL}, + {0, NULL, 0, 0, NULL}, +} + static char *builtin_cd_special(char *const av[], char *const env[]) { char *target; @@ -44,30 +51,13 @@ static char *builtin_cd_special(char *const av[], char *const env[]) return (target); } -static int builtin_cd_opts(char *const av[], int *opts) +void setwd(char *var) { - int i; - int j; + char *cwd; - i = 1; - if (av) - while (av[i] && av[i][0] == '-' && av[i][1]) - { - j = 0; - while (av[i][++j]) - { - if (av[i][j] == 'P') - *opts = CDOPT_P; - else if (av[i][j] == 'L') - *opts = CDOPT_L; - else if (av[i][j] == '-') - return (i + 1); - else - return (i); - } - ++i; - } - return (i); + cwd = getcwd(NULL, 0); + builtin_setenv(path, (char*[4]){"setenv", var, cwd, NULL}, envp); + free(cwd); } int builtin_cd(const char *path, @@ -76,15 +66,13 @@ int builtin_cd(const char *path, int i; int opts; char *target; - char *cwd; - opts = CDOPT_L; + data->flag = CD_OPT_L; + if (cliopts(av, g_cdopts, &data)) i = builtin_cd_opts(av, &opts); + setwd("OLDPWD"); if (!(target = builtin_cd_special(av + i, envp))) return (1); - cwd = getcwd(NULL, 0); - builtin_setenv(path, (char*[4]){"setenv", "OLDPWD", cwd, NULL}, envp); - free(cwd); if (chdir(target)) { SH_ERR(CDERR_1, target); @@ -92,9 +80,7 @@ int builtin_cd(const char *path, } else if (target != av[i]) ft_printf("%s\n", target); - cwd = getcwd(NULL, 0); - builtin_setenv(path, (char*[4]){"setenv", "PWD", cwd, NULL}, envp); - free(cwd); + setwd("PWD"); if (!ft_strcmp(*(av + i), "-")) free(target); return (0); diff --git a/42sh/src/builtin/builtin_setenv.c b/42sh/src/builtin/builtin_setenv.c index 5dd2bd1f..26852d3d 100644 --- a/42sh/src/builtin/builtin_setenv.c +++ b/42sh/src/builtin/builtin_setenv.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */ -/* Updated: 2017/03/23 18:26:59 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 22:43:49 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/42sh/src/builtin/builtin_unsetenv.c b/42sh/src/builtin/builtin_unsetenv.c index a7d2a674..22bea5d2 100644 --- a/42sh/src/builtin/builtin_unsetenv.c +++ b/42sh/src/builtin/builtin_unsetenv.c @@ -6,7 +6,7 @@ /* By: jhalford +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2016/11/28 14:29:17 by jhalford #+# #+# */ -/* Updated: 2017/03/23 18:24:41 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 22:59:17 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ int builtin_unsetenv(const char *path, char *const av[], char *const envp[]) while (*env && (*env)[j]) { if (ft_strcmp((*env)[j], av[i]) == '=' - && ft_strlen(av[1]) == ft_strlenchr((*env)[i], '=')) + && ft_strlen(av[i]) == ft_strlenchr((*env)[j], '=')) ft_sstrdel(*env, j); else j++; diff --git a/42sh/src/exec/plaunch_while.c b/42sh/src/exec/plaunch_while.c index 44c2d554..d4631982 100644 --- a/42sh/src/exec/plaunch_while.c +++ b/42sh/src/exec/plaunch_while.c @@ -6,7 +6,7 @@ /* By: wescande +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2017/03/07 17:20:53 by wescande #+# #+# */ -/* Updated: 2017/03/22 19:23:55 by jhalford ### ########.fr */ +/* Updated: 2017/03/24 22:43:37 by jhalford ### ########.fr */ /* */ /* ************************************************************************** */