Skip to content

Commit

Permalink
trying to fix seg fault
Browse files Browse the repository at this point in the history
  • Loading branch information
biralavor committed Oct 10, 2024
1 parent 5924b7b commit a410cfd
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 86 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ SRC_FILES += ./02_parser/18.check_redirects_utils.c
SRC_FILES += ./02_parser/19.error_manager_parser.c
SRC_FILES += ./02_parser/20.manage_heredoc.c
SRC_FILES += ./02_parser/heredoc_expansions.c
SRC_FILES += ./02_parser/heredoc_fds.c
SRC_FILES += ./02_parser/heredoc_errors.c
SRC_FILES += ./03_binary_tree/21.building_tree.c
SRC_FILES += ./03_binary_tree/22.building_tree_utils.c
SRC_FILES += ./04_tree_execution/23.tree_execution.c
Expand Down
7 changes: 6 additions & 1 deletion _tdd_utils/ft_lst_printer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/* */
/* ************************************************************************** */

Expand All @@ -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");
Expand Down
11 changes: 7 additions & 4 deletions headers/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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);
Expand All @@ -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
72 changes: 27 additions & 45 deletions src/02_parser/20.manage_heredoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -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);
}
73 changes: 73 additions & 0 deletions src/02_parser/heredoc_errors.c
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;
}
33 changes: 0 additions & 33 deletions src/02_parser/heredoc_fds.c

This file was deleted.

26 changes: 24 additions & 2 deletions src/05.loop_routine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/* */
/* ************************************************************************** */

Expand All @@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit a410cfd

Please sign in to comment.