-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from neo-jgrec/parenthese
Parenthese
- Loading branch information
Showing
8 changed files
with
200 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |