Skip to content

Commit

Permalink
Merge pull request #22 from neo-jgrec/parenthese
Browse files Browse the repository at this point in the history
Parenthese
  • Loading branch information
neo-jgrec authored May 14, 2023
2 parents a734bc7 + ee21a79 commit ceeba1f
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 11 deletions.
4 changes: 4 additions & 0 deletions include/my.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,8 @@

void display_variables(char **var, history_list_t *history);

int check_parenthesis(term_t *term);
void my_parenthesis(exec_t *exec, int *i, term_t *term, char **args);
int exec_parenthesis(char **argv, char **env);

#endif /* !MY_H_ */
19 changes: 9 additions & 10 deletions src/execution/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +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)
(!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
3 changes: 2 additions & 1 deletion src/minishell.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ int minishell(char **env)
if ((term.argv = check_str(term.str, &term)) == NULL)
continue;
manage_history(term.history, term.argv);
if (parsing_error(term.argv, &term) == 1) continue;
if (parsing_error(term.argv, &term) || check_parenthesis(&term))
continue;
if (edit_args_env(term.argv, term.env) == NULL) continue;
signal(SIGINT, handle_sigint_program);
execute_commands(term.argv, &term);
Expand Down
60 changes: 60 additions & 0 deletions src/parenthese/err_handling/handle_bp.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
** EPITECH PROJECT, 2023
** 42sh
** File description:
** handle_bp
*/

#include "my.h"

int is_joker(const char *str, const char **jokers);
int print_par_error(int err_id);
const char *bf_jokers[] = {";", "||", "|", "<<", ">>", ">", "<",
"&&", "(", NULL};
const char *af_jokers[] = {";", "||", "|", "<<", ">>", ">", "<",
"&&", ")", NULL};

static int handle_type_err(char *arg)
{
if (!strcmp(arg, ")") || !strcmp(arg, "("))
return print_par_error(1);
else
return print_par_error(3);
}

static int check_before_pos(char **argv, size_t i)
{
if (i == 0) {
return 0;
} else {
if (!is_joker(argv[i - 1], bf_jokers))
return handle_type_err(argv[i - 1]);
}
return 0;
}

static int check_after_pos (char **argv, size_t i)
{
if (argv[i + 1] == NULL) {
return 0;
} else {
if (!is_joker(argv[i + 1], af_jokers))
return handle_type_err(argv[i + 1]);
}
return 0;
}

int handle_bp(char **argv)
{
int ret;

for (size_t i = 0; argv[i] != NULL; i++) {
if (!strcmp(argv[i], "("))
ret = check_before_pos(argv, i);
if (!strcmp(argv[i], ")"))
ret = check_after_pos(argv, i);
if (ret)
return 1;
}
return 0;
}
27 changes: 27 additions & 0 deletions src/parenthese/err_handling/handle_tm.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
** EPITECH PROJECT, 2023
** 42sh
** File description:
** handle_tm
*/

#include "my.h"

int print_par_error(int err_id);

int handle_tm(char *input)
{
ssize_t nb = 0;

for (size_t i = 0; input[i] != '\0'; i++) {
if (input[i] == '(')
nb++;
if (input[i] == ')')
nb--;
if (nb < 0)
return print_par_error(1);
}
if (nb > 0)
return print_par_error(2);
return 0;
}
21 changes: 21 additions & 0 deletions src/parenthese/err_handling/print_par_err.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
** EPITECH PROJECT, 2023
** 42sh
** File description:
** print_par_error
*/

#include "my.h"

int print_par_error(int err_id)
{
if (err_id == 1)
my_printf("Too many )'s.\n");
if (err_id == 2)
my_printf("Too many ('s.\n");
if (err_id == 3)
my_printf("Badly placed ()'s\n");
if (err_id == 4)
my_printf("Invalid null command.\n");
return 1;
}
57 changes: 57 additions & 0 deletions src/parenthese/my_parenthesis.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
** EPITECH PROJECT, 2023
** 42sh
** File description:
** parenthese handing
*/

#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)
{
int pid, status;
char **subshell_args;

if (args[*i + 1] == NULL || strcmp(args[*i + 1], ")") == 0) {
dprintf(2, "Missing command for subshell.\n");
remove_element_at_index(args, *i);
return;
}
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;
free(subshell_args);
}
20 changes: 20 additions & 0 deletions src/parsing/check_parenthesis.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
** EPITECH PROJECT, 2023
** 42sh
** File description:
** check_parentesis
*/

#include "my.h"

int handle_tm(char *input);
int handle_bp(char **argv);

int check_parenthesis(term_t *term)
{
if (handle_tm(term->str))
return 1;
else if (handle_bp(term-> argv))
return 1;
return 0;
}

0 comments on commit ceeba1f

Please sign in to comment.