-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added here_doc struct to collect fds and close them + unlink files AF…
…TER the execution processes are done. Also fixed bad parsing of heredoc tokens. Execution does not work, it receives a bad file descriptor
- Loading branch information
Jean Teissier
authored and
Jean Teissier
committed
Jul 15, 2024
1 parent
7f44021
commit 2fa1f00
Showing
9 changed files
with
103 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: jteissie <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/06/07 16:53:36 by jteissie #+# #+# */ | ||
/* Updated: 2024/07/15 15:05:03 by jteissie ### ########.fr */ | ||
/* Updated: 2024/07/15 17:27:39 by jteissie ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -27,10 +27,10 @@ void handle_error(char *message, int code); | |
void execute_cmd(char *cmd, char **env, t_parser *data); | ||
|
||
int process_files(t_lex_parser *table); | ||
void get_redirections(t_lex_parser *table, char *redirection[]); | ||
void get_redirections(t_lex_parser *table, char *redirection[], int *heredoc_fd); | ||
|
||
int execute_data(t_parser *parsed_data, char **env); | ||
int execute_commands(t_parser *data, char **envp, int std_fds[]); | ||
int execute_commands(t_parser *parsed, char **envp, int std_fds[]); | ||
int process_command(t_lex_parser *p, char **env, t_parser *d, int std_fd[]); | ||
int redir_child(t_lex_parser *p, int p_fd[], int has_pipe[], int std_fd[]); | ||
|
||
|
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: jteissie <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/06/25 13:21:34 by bthomas #+# #+# */ | ||
/* Updated: 2024/07/15 14:43:36 by jteissie ### ########.fr */ | ||
/* Updated: 2024/07/15 18:54:32 by jteissie ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -46,6 +46,14 @@ typedef struct s_heredoc | |
char path[22]; | ||
} t_heredoc; | ||
|
||
typedef struct s_heredoc_data t_heredoc_data; | ||
|
||
typedef struct s_heredoc_data | ||
{ | ||
t_heredoc *heredoc; | ||
t_heredoc_data *next; | ||
} t_heredoc_data; | ||
|
||
void handle_signals(void); | ||
t_heredoc *process_here_doc(char *limiter); | ||
|
||
|
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: jteissie <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/07/01 13:17:51 by jteissie #+# #+# */ | ||
/* Updated: 2024/07/15 14:50:58 by jteissie ### ########.fr */ | ||
/* Updated: 2024/07/15 15:55:57 by jteissie ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -32,6 +32,7 @@ typedef enum e_redir_token | |
TK_PARS_IN, | ||
TK_PARS_OUT, | ||
TK_PARS_OUT_APPEND, | ||
TK_PARS_HEREDOC, | ||
} t_redir_token; | ||
|
||
typedef struct s_cmd_table | ||
|
@@ -41,6 +42,7 @@ typedef struct s_cmd_table | |
typedef struct s_redirect_table | ||
{ | ||
char *redir_str; | ||
int heredoc_fd; | ||
t_redir_token type; | ||
} t_redirect_table; | ||
|
||
|
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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: jteissie <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/06/25 13:43:42 by bthomas #+# #+# */ | ||
/* Updated: 2024/07/15 14:54:30 by jteissie ### ########.fr */ | ||
/* Updated: 2024/07/15 19:01:17 by jteissie ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -58,16 +58,75 @@ int get_input(t_data *data) | |
return (SUCCESS); | ||
} | ||
|
||
int collect_heredocs(t_heredoc_data *here_data, t_data *data) | ||
{ | ||
t_token *roaming; | ||
t_heredoc_data *temp; | ||
t_heredoc_data *new_node; | ||
|
||
roaming = data->token; | ||
while (roaming) | ||
{ | ||
if (roaming->type == TK_HEREDOC) | ||
{ | ||
if (!here_data->heredoc) | ||
here_data->heredoc = roaming->heredoc; | ||
else | ||
{ | ||
temp = here_data; | ||
while (temp->next) | ||
temp = temp->next; | ||
new_node = ft_calloc(1, sizeof(t_heredoc_data)); | ||
if (!new_node) | ||
return (PANIC); | ||
new_node->heredoc = roaming->heredoc; | ||
new_node->next = NULL; | ||
temp->next = new_node; | ||
} | ||
} | ||
roaming = roaming->next; | ||
} | ||
return (SUCCESS); | ||
} | ||
|
||
void unlink_heredocs(t_heredoc_data *here_data) | ||
{ | ||
t_heredoc_data *roaming; | ||
t_heredoc_data *temp; | ||
int index; | ||
|
||
if (!here_data->heredoc) | ||
return ; | ||
roaming = here_data; | ||
index = 0; | ||
while (roaming) | ||
{ | ||
temp = roaming; | ||
if (ft_strlen(temp->heredoc->path)) | ||
if (unlink(temp->heredoc->path) != 0) | ||
ft_printf("Error deleting file '%s': %s\n", | ||
temp->heredoc->path, strerror(errno)); | ||
free(temp->heredoc); | ||
if (index > 0) | ||
free(temp); | ||
roaming = roaming->next; | ||
} | ||
here_data->heredoc = NULL; | ||
} | ||
|
||
int main(int argc, char **argv, char **env) | ||
{ | ||
t_data data; | ||
t_parser parsed_data; | ||
t_heredoc_data here_doc_data; | ||
char *prompt; | ||
|
||
(void)argv; | ||
(void)argc; | ||
init(&data, env); | ||
prompt = get_prompt(NULL); | ||
here_doc_data.heredoc = NULL; | ||
here_doc_data.next = NULL; | ||
parsed_data.node = NULL; | ||
while (1) | ||
{ | ||
|
@@ -76,8 +135,12 @@ int main(int argc, char **argv, char **env) | |
break ; | ||
if (tokenize_data(&data) == PANIC) | ||
break ; | ||
if (collect_heredocs(&here_doc_data, &data) == PANIC) | ||
break ; | ||
parse_data(&data, &parsed_data); | ||
free_lexmem(&data); | ||
execute_data(&parsed_data, env); | ||
unlink_heredocs(&here_doc_data); | ||
} | ||
if (prompt) | ||
free(prompt); | ||
|
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,21 +6,23 @@ | |
/* By: jteissie <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/07/08 18:21:54 by jteissie #+# #+# */ | ||
/* Updated: 2024/07/14 13:46:35 by jteissie ### ########.fr */ | ||
/* Updated: 2024/07/15 16:10:48 by jteissie ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "minishell.h" | ||
|
||
int get_redir_type(t_token *lexer) | ||
{ | ||
if (ft_strncmp(lexer->lexstr, "<", 1) == 0) | ||
if (lexer->type == TK_HEREDOC) | ||
return (TK_PARS_HEREDOC); | ||
else if (ft_strncmp(lexer->lexstr, "<", 1) == 0) | ||
return (TK_PARS_IN); | ||
else if (ft_strncmp(lexer->lexstr, ">", 1) == 0) | ||
return (TK_PARS_OUT); | ||
else if (ft_strncmp(lexer->lexstr, ">>", 2) == 0) | ||
return (TK_PARS_OUT_APPEND); | ||
return (TK_INVALID); | ||
return (TK_PARS_NULL); | ||
} | ||
|
||
int build_redirect_table(t_lex_parser *parsed, t_token *lexer) | ||
|
@@ -30,11 +32,18 @@ int build_redirect_table(t_lex_parser *parsed, t_token *lexer) | |
redir_table = ft_calloc(1, sizeof(t_redirect_table)); | ||
if (!redir_table) | ||
return (PANIC); | ||
redir_table->redir_str = ft_strdup(lexer->next->lexstr); | ||
if (!redir_table->redir_str) | ||
return (PANIC); | ||
redir_table->heredoc_fd = 0; | ||
redir_table->redir_str = NULL; | ||
if (lexer->type == TK_HEREDOC) | ||
redir_table->heredoc_fd = lexer->heredoc->fd; | ||
else | ||
{ | ||
redir_table->redir_str = ft_strdup(lexer->next->lexstr); | ||
if (!redir_table->redir_str) | ||
return (PANIC); | ||
lexer->next->type = TK_RESERVED; | ||
} | ||
redir_table->type = get_redir_type(lexer); | ||
lexer->next->type = TK_RESERVED; | ||
lexer->type = TK_RESERVED; | ||
if (parsed_add_back(parsed, redir_table, TK_PARS_REDIR) == PANIC) | ||
return (PANIC); | ||
|
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: jteissie <[email protected]> +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/07/01 13:21:33 by jteissie #+# #+# */ | ||
/* Updated: 2024/07/15 14:53:26 by jteissie ### ########.fr */ | ||
/* Updated: 2024/07/15 15:36:56 by jteissie ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -49,7 +49,7 @@ void collect_redir_tk(t_lex_parser *parsed, t_token *roaming) | |
{ | ||
while (roaming && roaming->type != TK_PIPE) | ||
{ | ||
if (roaming->type == TK_REDIR) | ||
if (roaming->type == TK_REDIR || roaming->type == TK_HEREDOC) | ||
{ | ||
if (build_redirect_table(parsed, roaming) == PANIC) | ||
panic(parsed); | ||
|