Skip to content

Commit

Permalink
feat(parentheses) add of the parentheses feature
Browse files Browse the repository at this point in the history
  • Loading branch information
neo-jgrec committed May 14, 2023
1 parent 2b08571 commit ee21a79
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 64 deletions.
20 changes: 9 additions & 11 deletions src/execution/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
40 changes: 0 additions & 40 deletions src/parenthese/exec_parenthese.c

This file was deleted.

55 changes: 42 additions & 13 deletions src/parenthese/my_parenthesis.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,52 @@
*/

#include "my.h"
#include <sys/wait.h>

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);
}

0 comments on commit ee21a79

Please sign in to comment.