diff --git a/src/execution/exec.c b/src/execution/exec.c index f61a63b..c00016c 100644 --- a/src/execution/exec.c +++ b/src/execution/exec.c @@ -48,17 +48,15 @@ int execute_command(char **args, int input_fd, int output_fd, term_t *term) void my_parsing(exec_t *exec, char **args, term_t *term) { for (int i = 0; args[i] != NULL; ++i) { - (!my_strcmp(args[i], "<")) ? my_left_redirection(args, - &exec->input_fd, &i) - : (!my_strcmp(args[i], "<<")) ? heredoc(args, &exec->input_fd, &i) - : (!my_strcmp(args[i], ">") || !(exec->append = my_strcmp(args[i], - ">>"))) ? my_right_redirection(args, &exec->output_fd, &i, - exec->append) - : (!my_strcmp(args[i], "|")) ? my_pipe(exec, &i, term, args) - : (!my_strcmp(args[i], "&&")) ? my_and(exec, &i, term, args) - : (!my_strcmp(args[i], ";")) ? my_semicolon(exec, &i, term, args) - : (!my_strcmp(args[i], "||")) ? my_or(exec, &i, term, args) - : (!my_strcmp(args[i], "(")) ? my_parenthesis(exec, &i, term, args) + (!strcmp(args[i], "<")) ? my_left_redirection(args, &exec->input_fd, &i) + : (!strcmp(args[i], "<<")) ? heredoc(args, &exec->input_fd, &i) + : (!strcmp(args[i], ">") || !(exec->append = strcmp(args[i], ">>"))) + ? my_right_redirection(args, &exec->output_fd, &i, exec->append) + : (!strcmp(args[i], "|")) ? my_pipe(exec, &i, term, args) + : (!strcmp(args[i], "&&")) ? my_and(exec, &i, term, args) + : (!strcmp(args[i], ";")) ? my_semicolon(exec, &i, term, args) + : (!strcmp(args[i], "||")) ? my_or(exec, &i, term, args) + : (!strcmp(args[i], "(")) ? my_parenthesis(exec, &i, term, args) : 0; } } diff --git a/src/parenthese/exec_parenthese.c b/src/parenthese/exec_parenthese.c deleted file mode 100644 index ea002b9..0000000 --- a/src/parenthese/exec_parenthese.c +++ /dev/null @@ -1,40 +0,0 @@ -/* -** EPITECH PROJECT, 2023 -** 42sh -** File description: -** exec_parenthese -*/ - -#include "my.h" -#include - -int execute_commands(char **args, term_t *term); - -static void exc_par_child(term_t *n_term) -{ - execute_commands(n_term->argv, n_term); - exit(0); -} - -static int exec_par_input(term_t *n_term) -{ - pid_t pid; - - pid = fork(); - if (pid == -1) - perror_exit("fork"); - if (pid == 0) { - exc_par_child(n_term); - } else { - waitpid(pid, n_term->exit_status, 0); - } - return 0; -} - -int exec_parenthesis(char **argv, char **env) -{ - term_t new_term = { .str = NULL, .argv = argv, - .env = env, .exit_status = &(int){0}, - init_history_list()}; - return exec_par_input(&new_term); -} diff --git a/src/parenthese/my_parenthesis.c b/src/parenthese/my_parenthesis.c index 737f4ea..b3a1bf5 100644 --- a/src/parenthese/my_parenthesis.c +++ b/src/parenthese/my_parenthesis.c @@ -6,23 +6,52 @@ */ #include "my.h" +#include + +int execute_commands(char **args, term_t *term); + +char **extract_commands_in_parentheses(char **args, int *i) +{ + int j = 0; + char **subshell_args = malloc(sizeof(char *) * 100); + int parentheses_count = 0; + + (*i)++; + while (args[*i] != NULL) { + if (strcmp(args[*i], "(") == 0) + parentheses_count++; + if (strcmp(args[*i], ")") == 0 && parentheses_count == 0) + break; + if (strcmp(args[*i], ")") == 0 && parentheses_count > 0) + parentheses_count--; + subshell_args[j] = strdup(args[*i]); + j++; + (*i)++; + } + + subshell_args[j] = NULL; + return subshell_args; +} void my_parenthesis(UNUSED exec_t *exec, int *i, term_t *term, char **args) { - char **par_input = calloc(100, sizeof(char *)); - size_t nb = 1; - size_t index = 0; + int pid, status; + char **subshell_args; - for (size_t j = *i + 1; nb != 0; j++) { - if (!strcmp(args[j], "(")) - nb ++; - if (!strcmp(args[j], ")")) - nb --; - if (nb != 0) - par_input[index++] = strdup(args[j]); + if (args[*i + 1] == NULL || strcmp(args[*i + 1], ")") == 0) { + dprintf(2, "Missing command for subshell.\n"); + remove_element_at_index(args, *i); + return; } - exec_parenthesis(par_input, term->env); - *i += index + 1; + subshell_args = extract_commands_in_parentheses(args, i); + if ((pid = fork()) < 0) { + perror_exit("fork"); + } else if (pid == 0) { + execute_commands(subshell_args, term); + exit(0); + } else + waitpid(pid, &status, 0); + (*i) += 1; exec->cmd_start = *i + 1; - return; + free(subshell_args); }