Skip to content

Commit

Permalink
Normed most files, created here_doc_utils to hold here_doc collecting…
Browse files Browse the repository at this point in the history
… and unlink functions. Append does not work sometimes, yet to be traced
  • Loading branch information
jteissie committed Jul 15, 2024
1 parent 618eed2 commit 862c9d1
Show file tree
Hide file tree
Showing 11 changed files with 127 additions and 95 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CFILES = main.c \
process_command.c \
redir_child.c \
parse_here_doc.c \
here_doc_utils.c \
get_next_line.c \
get_next_line_utils.c \
lex_bools1.c \
Expand Down
5 changes: 2 additions & 3 deletions include/execution.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: marvin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 16:53:36 by jteissie #+# #+# */
/* Updated: 2024/07/15 20:52:44 by marvin ### ########.fr */
/* Updated: 2024/07/15 23:08:20 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -22,13 +22,12 @@
# define PATH_ERROR 127
# define EXIT_FAILURE 1


void trash(char **array);
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 *roaming, char *redirection[]);
int get_redirections(t_lex_parser *roaming, char *redirection[]);

int execute_data(t_parser *parsed_data, char **env);
int execute_commands(t_parser *parsed, char **envp, int std_fds[]);
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: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/27 19:30:56 by bthomas #+# #+# */
/* Updated: 2024/07/15 17:23:09 by jteissie ### ########.fr */
/* Updated: 2024/07/15 22:27:59 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down
12 changes: 8 additions & 4 deletions include/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* minishell.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 13:21:34 by bthomas #+# #+# */
/* Updated: 2024/07/15 18:54:32 by jteissie ### ########.fr */
/* Updated: 2024/07/15 22:28:15 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -40,14 +40,15 @@
# include "lexer.h"
# include "execution.h"

typedef struct s_heredoc_data t_heredoc_data;
typedef struct s_data t_data;

typedef struct s_heredoc
{
int fd;
char path[22];
} t_heredoc;

typedef struct s_heredoc_data t_heredoc_data;

typedef struct s_heredoc_data
{
t_heredoc *heredoc;
Expand All @@ -57,6 +58,9 @@ typedef struct s_heredoc_data
void handle_signals(void);
t_heredoc *process_here_doc(char *limiter);

int collect_heredocs(t_heredoc_data *here_data, t_data *data);
void unlink_heredocs(t_heredoc_data *here_data);

char *get_prompt(char *orig_prompt);

#endif
4 changes: 2 additions & 2 deletions include/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: marvin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/01 13:17:51 by jteissie #+# #+# */
/* Updated: 2024/07/15 21:25:11 by marvin ### ########.fr */
/* Updated: 2024/07/15 23:08:43 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -31,7 +31,7 @@ typedef enum e_redir_token
{
TK_PARS_IN,
TK_PARS_OUT,
TK_PARS_OUT_APPEND,
TK_PARS_APPEND,
TK_PARS_HEREDOC,
} t_redir_token;

Expand Down
5 changes: 2 additions & 3 deletions src/execution/execute_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* execute_commands.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 14:19:59 by jteissie #+# #+# */
/* Updated: 2024/07/15 19:23:37 by jteissie ### ########.fr */
/* Updated: 2024/07/15 22:07:17 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -75,7 +75,6 @@ int execute_commands(t_parser *data, char **envp, int std_fds[])
wait_for_children(cmd_count);
return (EXIT_SUCCESS);
}
#include <stdio.h>

int execute_data(t_parser *parsed_data, char **env)
{
Expand Down
11 changes: 8 additions & 3 deletions src/execution/execution_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: marvin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 18:14:25 by jteissie #+# #+# */
/* Updated: 2024/07/15 20:52:22 by marvin ### ########.fr */
/* Updated: 2024/07/15 23:08:10 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -34,24 +34,29 @@ void trash(char **array)
free(array);
}

void get_redirections(t_lex_parser *roaming, char *redirection[])
int get_redirections(t_lex_parser *roaming, char *redirection[])
{
char *outfile;
char *infile;
t_redirect_table *redir;
int append;

infile = NULL;
outfile = NULL;
append = FALSE;
if (roaming->type == TK_PARS_REDIR)
{
redir = roaming->table;
if (redir->type == TK_PARS_HEREDOC)
infile = redir->redir_str;
else if (redir->type == TK_PARS_IN)
infile = redir->redir_str;
else if (redir->type == TK_PARS_OUT)
else if (redir->type == TK_PARS_OUT || redir->type == TK_PARS_APPEND)
outfile = redir->redir_str;
}
if (redir->type == TK_PARS_APPEND)
append = TRUE;
redirection[0] = infile;
redirection[1] = outfile;
return (append);
}
25 changes: 15 additions & 10 deletions src/execution/redir_child.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: marvin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/12 14:00:07 by jteissie #+# #+# */
/* Updated: 2024/07/15 20:55:05 by marvin ### ########.fr */
/* Updated: 2024/07/15 23:13:53 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -32,12 +32,18 @@ int redirect_files(int file_fd[])
return (dup_status);
}

int open_files(char *redir[], int file_fd[])
int open_files(char *redir[], int file_fd[], int *append)
{
if (redir[0])
file_fd[0] = open(redir[0], O_RDONLY);
if (redir[1])
file_fd[1] = open(redir[1], O_CREAT | O_TRUNC | O_WRONLY, 0644);
{
if (*append == TRUE)
file_fd[1] = open(redir[1], O_CREAT | O_APPEND | O_WRONLY, 0644);
if (*append == FALSE)
file_fd[1] = open(redir[1], O_CREAT | O_TRUNC | O_WRONLY, 0644);
}
*append = FALSE;
if (file_fd[0] < 0 || file_fd[1] < 0)
return (PANIC);
return (SUCCESS);
Expand All @@ -46,11 +52,12 @@ int open_files(char *redir[], int file_fd[])
int process_files(t_lex_parser *table)
{
char *redir[2];
int file_fd[2];
int f_fd[2];
int append;
t_lex_parser *roaming;

file_fd[0] = 0;
file_fd[1] = 0;
f_fd[0] = 0;
f_fd[1] = 0;
redir[0] = NULL;
redir[1] = NULL;
roaming = table;
Expand All @@ -60,10 +67,8 @@ int process_files(t_lex_parser *table)
{
if (roaming->type == TK_PARS_REDIR)
{
get_redirections(roaming, redir);
if (open_files(redir, file_fd) == PANIC)
return (-1);
if (redirect_files(file_fd) < 0)
append = get_redirections(roaming, redir);
if (open_files(redir, f_fd, &append) == PANIC || redir_files(f_fd) < 0)
return (-1);
}
roaming = roaming->next;
Expand Down
77 changes: 77 additions & 0 deletions src/here_doc_parsing/here_doc_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* here_doc_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marvin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/15 22:08:52 by marvin #+# #+# */
/* Updated: 2024/07/15 22:08:52 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

int heredoc_add_back(t_heredoc_data *data, t_token *roaming)
{
t_heredoc_data *temp;
t_heredoc_data *new_node;

temp = 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;
return (SUCCESS);
}

int collect_heredocs(t_heredoc_data *here_data, t_data *data)
{
t_token *roaming;

roaming = data->token;
while (roaming)
{
if (roaming->type == TK_HEREDOC)
{
if (!here_data->heredoc)
here_data->heredoc = roaming->heredoc;
else
{
if (heredoc_add_back(here_data, roaming) == PANIC)
return (PANIC);
}
}
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;
}
Loading

0 comments on commit 862c9d1

Please sign in to comment.