-
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.
Normed most files, created here_doc_utils to hold here_doc collecting…
… 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
Showing
11 changed files
with
127 additions
and
95 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: 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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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[]); | ||
|
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: 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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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; | ||
|
||
|
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: 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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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); | ||
} |
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: 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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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); | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
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,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; | ||
} |
Oops, something went wrong.