-
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.
- Loading branch information
Showing
7 changed files
with
138 additions
and
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: umeneses <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/06/25 11:35:49 by umeneses #+# #+# */ | ||
/* Updated: 2024/09/11 11:55:30 by umeneses ### ########.fr */ | ||
/* Updated: 2024/10/10 15:35:51 by umeneses ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -18,6 +18,11 @@ void ft_lst_printer(t_token_list *lst) | |
t_token_list *aux; | ||
|
||
aux = lst; | ||
if (!aux) | ||
{ | ||
fprintf(stderr, "\033[0;33mEmpty List\033[0m\n"); | ||
return ; | ||
} | ||
lst_size = ft_lst_size(aux); | ||
fprintf(stderr, "\nTotal lst size: %d\n", lst_size); | ||
fprintf(stderr, "\033[0;33mPrinting the List Now \033[0m \n"); | ||
|
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: umeneses <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/06/27 12:53:52 by tmalheir #+# #+# */ | ||
/* Updated: 2024/10/10 09:28:11 by umeneses ### ########.fr */ | ||
/* Updated: 2024/10/10 15:22:17 by umeneses ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -124,9 +124,10 @@ t_token_list *find_src_node(t_token_list *lst, int src_idx); | |
*/ | ||
t_token_list *find_dst_node(t_token_list *lst, int dst_idx); | ||
|
||
bool heredoc_detector(t_token_list *lst); | ||
void manage_heredoc(t_token_list *lst); | ||
void path_file(t_token_list *lst); | ||
int check_delimiter(char *delimiter, int fd); | ||
bool check_eof_del(char *delimiter, int fd); | ||
|
||
void heredoc_fd_reset(int *heredoc_fd); | ||
bool is_heredoc_running(bool update, bool caller); | ||
|
@@ -135,7 +136,9 @@ void heredoc_forcing_exit_warning(char *input, char *delimiter, | |
|
||
int check_dollar_sign_for_heredoc(char *input, int idx, int fd); | ||
int check_question_mark_for_heredoc(int idx, int fd); | ||
int heredoc_fd_error_runner(int heredoc_fd); | ||
void heredoc_fd_reset(int *heredoc_fd); | ||
bool hd_fd_error_runner(int heredoc_fd); | ||
bool is_demiliter_null(char *delimiter); | ||
bool is_signal_sigint(int heredoc_fd); | ||
void heredoc_cleanup(int hd_fd, int original_stdin, char *eof_del); | ||
|
||
#endif |
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 |
---|---|---|
|
@@ -6,54 +6,47 @@ | |
/* By: umeneses <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/09/12 18:23:53 by umeneses #+# #+# */ | ||
/* Updated: 2024/10/10 09:30:18 by umeneses ### ########.fr */ | ||
/* Updated: 2024/10/10 15:45:01 by umeneses ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "minishell.h" | ||
|
||
void manage_heredoc(t_token_list *lst) | ||
{ | ||
int heredoc_fd; | ||
int flag; | ||
char *delimiter; | ||
int hd_fd; | ||
char *eof_del; | ||
t_token_list *tmp; | ||
int original_stdin; | ||
|
||
flag = 0; | ||
heredoc_fd = -1; | ||
delimiter = NULL; | ||
|
||
hd_fd = -1; | ||
eof_del = NULL; | ||
tmp = lst; | ||
original_stdin = dup(STDIN_FILENO); | ||
while (tmp && tmp->next) | ||
{ | ||
if (tmp->type == REDIR_HDOC) | ||
{ | ||
delimiter = redir_quote_detector(ft_strdup(tmp->next->lexeme), &flag); | ||
if (delimiter == NULL) | ||
{ | ||
ft_putendl_fd(" syntax error near unexpected token `newline'", STDERR_FILENO); | ||
exit_status_holder(2, true); | ||
break ; | ||
} | ||
eof_del = redir_quote_detector(ft_strdup(tmp->next->lexeme), 0); | ||
path_file(lst); | ||
heredoc_fd_reset(&heredoc_fd); | ||
heredoc_fd = open(tmp->next->lexeme, O_CREAT | O_RDWR | O_TRUNC, 0644); | ||
if (heredoc_fd_error_runner(heredoc_fd)) | ||
break ; | ||
check_delimiter(delimiter, heredoc_fd); | ||
free(delimiter); | ||
if (g_sigmonitor == SIGINT) | ||
{ | ||
dup2(heredoc_fd, STDIN_FILENO); | ||
heredoc_fd_reset(&hd_fd); | ||
hd_fd = open(tmp->next->lexeme, O_CREAT | O_RDWR | O_TRUNC, 0644); | ||
if (is_demiliter_null(eof_del) || hd_fd_error_runner(hd_fd) | ||
|| !check_eof_del(eof_del, hd_fd) || is_signal_sigint(hd_fd)) | ||
break ; | ||
} | ||
} | ||
tmp = tmp->next; | ||
} | ||
heredoc_fd_reset(&heredoc_fd); | ||
heredoc_cleanup(hd_fd, original_stdin, eof_del); | ||
} | ||
|
||
void heredoc_cleanup(int hd_fd, int original_stdin, char *eof_del) | ||
{ | ||
heredoc_fd_reset(&hd_fd); | ||
dup2(original_stdin, STDIN_FILENO); | ||
close(original_stdin); | ||
if (eof_del) | ||
free(eof_del); | ||
is_heredoc_running(false, false); | ||
} | ||
|
||
|
@@ -81,7 +74,7 @@ void path_file(t_token_list *lst) | |
free(pathname); | ||
} | ||
|
||
int check_delimiter(char *delimiter, int fd) | ||
bool check_eof_del(char *delimiter, int fd) | ||
{ | ||
int idx; | ||
int line; | ||
|
@@ -112,10 +105,16 @@ int check_delimiter(char *delimiter, int fd) | |
free(input); | ||
} | ||
if (input == NULL && is_heredoc_running(false, true) && g_sigmonitor != SIGINT) | ||
{ | ||
heredoc_forcing_exit_warning(input, delimiter, line, fd); | ||
return (false); | ||
} | ||
else | ||
exit_status_holder(EXIT_SUCCESS, true); | ||
if (input) | ||
free(input); | ||
return (0); | ||
free(delimiter); | ||
return (true); | ||
} | ||
|
||
bool is_heredoc_running(bool update, bool caller) | ||
|
@@ -130,20 +129,3 @@ bool is_heredoc_running(bool update, bool caller) | |
heredoc_running = false; | ||
return (heredoc_running); | ||
} | ||
|
||
void heredoc_forcing_exit_warning(char *input, char *delimiter, int line, int fd) | ||
{ | ||
char *line_as_str; | ||
|
||
(void)fd; | ||
line_as_str = ft_itoa(line); | ||
ft_putstr_fd(" here-document at line ", STDERR_FILENO); | ||
ft_putstr_fd(line_as_str, STDERR_FILENO); | ||
ft_putstr_fd(" delimited by end-of-file (wanted `", STDERR_FILENO); | ||
ft_putstr_fd(delimiter, STDERR_FILENO); | ||
ft_putendl_fd("')", STDERR_FILENO); | ||
env_holder(NULL, false, true); | ||
free(input); | ||
free(line_as_str); | ||
exit_status_holder(EXIT_SUCCESS, true); | ||
} |
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,73 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* heredoc_errors.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: umeneses <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/10/09 20:56:22 by umeneses #+# #+# */ | ||
/* Updated: 2024/10/10 15:28:01 by umeneses ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "minishell.h" | ||
|
||
void heredoc_fd_reset(int *heredoc_fd) | ||
{ | ||
if (*heredoc_fd != -1) | ||
{ | ||
close(*heredoc_fd); | ||
*heredoc_fd = -1; | ||
} | ||
} | ||
|
||
bool hd_fd_error_runner(int heredoc_fd) | ||
{ | ||
if (heredoc_fd == -1) | ||
{ | ||
ft_putendl_fd("Failed to open heredoc file\n", STDERR_FILENO); | ||
exit_status_holder(EXIT_FAILURE, true); | ||
return (true); | ||
} | ||
return (false); | ||
} | ||
|
||
bool is_demiliter_null(char *delimiter) | ||
{ | ||
if (delimiter == NULL) | ||
{ | ||
ft_putendl_fd(" syntax error near unexpected token `newline'", STDERR_FILENO); | ||
exit_status_holder(2, true); | ||
return (true); | ||
} | ||
return (false); | ||
} | ||
|
||
bool is_signal_sigint(int heredoc_fd) | ||
{ | ||
if (g_sigmonitor == SIGINT) | ||
{ | ||
dup2(heredoc_fd, STDIN_FILENO); | ||
return (true); | ||
} | ||
return (false); | ||
} | ||
|
||
void heredoc_forcing_exit_warning(char *input, char *delimiter, int line, int fd) | ||
{ | ||
char *line_as_str; | ||
|
||
(void)fd; | ||
is_heredoc_running(false, false); | ||
line_as_str = ft_itoa(line); | ||
ft_putstr_fd(" here-document at line ", STDERR_FILENO); | ||
ft_putstr_fd(line_as_str, STDERR_FILENO); | ||
ft_putstr_fd(" delimited by end-of-file (wanted `", STDERR_FILENO); | ||
ft_putstr_fd(delimiter, STDERR_FILENO); | ||
ft_putendl_fd("')", STDERR_FILENO); | ||
env_holder(NULL, false, true); | ||
free(input); | ||
free(line_as_str); | ||
exit_status_holder(EXIT_SUCCESS, true); | ||
g_sigmonitor = SIGUSR1; | ||
} |
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: umeneses <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/06/19 09:20:45 by tmalheir #+# #+# */ | ||
/* Updated: 2024/10/09 22:02:55 by umeneses ### ########.fr */ | ||
/* Updated: 2024/10/10 15:39:51 by umeneses ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -21,6 +21,20 @@ bool check_initial_errors(char *str) | |
return (true); | ||
} | ||
|
||
bool heredoc_detector(t_token_list *lst) | ||
{ | ||
t_token_list *tmp; | ||
|
||
tmp = lst; | ||
while (tmp) | ||
{ | ||
if (tmp->type == REDIR_HDOC) | ||
return (true); | ||
tmp = tmp->next; | ||
} | ||
return (false); | ||
} | ||
|
||
void loop_routine(char *str) | ||
{ | ||
int flag; | ||
|
@@ -41,7 +55,15 @@ void loop_routine(char *str) | |
error_manager_lexer(LIST_NOT_CREATED); | ||
if (lst && syntax_analysis(lst)) | ||
{ | ||
manage_heredoc(lst); | ||
if (heredoc_detector(lst)) | ||
manage_heredoc(lst); | ||
if (g_sigmonitor == SIGUSR1 | ||
&& !child_process_is_running(false, true)) | ||
{ | ||
free_token_list(&lst); | ||
g_sigmonitor = 0; | ||
return ; | ||
} | ||
token_tree = initiate_tree(token_list_holder(NULL, false, false)); | ||
tree_holder(token_tree, false); | ||
tree_execution(token_tree, &flag); | ||
|