Skip to content

Commit

Permalink
Added here_doc struct to collect fds and close them + unlink files AF…
Browse files Browse the repository at this point in the history
…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
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 30 deletions.
6 changes: 3 additions & 3 deletions include/execution.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/* */
/* ************************************************************************** */

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

Expand Down
4 changes: 2 additions & 2 deletions include/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* lexer.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bthomas <bthomas@student.42.fr> +#+ +:+ +#+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 19:30:56 by bthomas #+# #+# */
/* Updated: 2024/07/12 16:31:18 by bthomas ### ########.fr */
/* Updated: 2024/07/15 17:23:09 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
10 changes: 9 additions & 1 deletion include/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/* */
/* ************************************************************************** */

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

Expand Down
4 changes: 3 additions & 1 deletion include/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/* */
/* ************************************************************************** */

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

Expand Down
5 changes: 2 additions & 3 deletions src/here_doc_parsing/parse_here_doc.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* parse_here_doc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bthomas <bthomas@student.42.fr> +#+ +:+ +#+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/15 16:09:14 by jteissie #+# #+# */
/* Updated: 2024/07/12 17:41:54 by bthomas ### ########.fr */
/* Updated: 2024/07/15 19:05:50 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -16,7 +16,6 @@
#define FILENAME_TAKEN -1
#define SYS_ERROR -2
#define SUCCESS 1
//call unlink() in main process once we are done with the file

int get_random_nbr(int lower, int upper)
{
Expand Down
12 changes: 2 additions & 10 deletions src/lexer/lex_clean_exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* lex_clean_exit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bthomas <bthomas@student.42.fr> +#+ +:+ +#+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/07 12:30:45 by bthomas #+# #+# */
/* Updated: 2024/07/12 17:43:14 by bthomas ### ########.fr */
/* Updated: 2024/07/15 18:28:35 by jteissie ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,14 +34,6 @@ void free_tokens(t_token *token)
free(token->lexstr);
if (token->path)
free(token->path);
if (token->heredoc)
{
if (ft_strlen(token->heredoc->path))
if (unlink(token->heredoc->path) != 0)
ft_printf("Error deleting file '%s': %s\n",
token->heredoc->path, strerror(errno));
free(token->heredoc);
}
free(token);
token = NULL;
token = tmp;
Expand Down
65 changes: 64 additions & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
Expand Down
23 changes: 16 additions & 7 deletions src/parser/build_redirect_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/* */
/* ************************************************************************** */

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

0 comments on commit 2fa1f00

Please sign in to comment.