Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ FILES = main.c \
token.c token_utils.c token_re.c token_filter.c \
syntax.c syntax_utils.c \
expand.c expand_utils.c expand_utils2.c \
parse.c parse_redir.c parse_utils.c parse_print.c heredoc.c \
parse.c parse_redir.c parse_utils.c parse_print.c heredoc.c heredoc_utils.c \
exec.c exec_utils.c exec_utils2.c \
find_path.c \
bn_echo.c bn_env.c bn_export.c bn_pwd.c bn_cd.c bn_exit.c bn_unset.c \
Expand Down
8 changes: 6 additions & 2 deletions include/minishell.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ typedef struct s_shell
bool exit;
int shlvl;
int e_status;
int heredoc;
pid_t last_pid;
t_token *tokens;
t_pak *cmds;
Expand Down Expand Up @@ -175,8 +176,11 @@ void *pak_redir_util(t_shell *shell, t_pak *cmd, int fd[2]);
int parse_redir_out(t_shell *shell, t_pak **curr, t_token **token);
int parse_redir_in(t_shell *shell, t_pak **curr, t_token **token);
int parse_redir_app(t_shell *shell, t_pak **curr, t_token **token);
int parse_heredoc(t_shell *shell, t_pak **curr, t_token **token);
int handle_heredoc(char *delimiter);
int parse_heredoc(t_shell *shell, t_pak **curr, t_token **token,
char *file);
int handle_heredoc(char *delimiter, char *file);
char *get_heredoc_file(int file);
int open_heredoc(char *file);
void print_paks(t_pak *head);
void free_paks(t_shell *shell, t_pak *head);
int count_paks(t_pak *head);
Expand Down
2 changes: 1 addition & 1 deletion src/exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ int executer(t_shell *shell, t_pak *cmd)
int paks;

paks = count_paks(cmd);
update_underscore_env(shell, cmd);
while (cmd)
{
arg = cmd->full_cmd;
Expand All @@ -80,7 +81,6 @@ int executer(t_shell *shell, t_pak *cmd)
shell_signal_ignore();
exec_pak(shell, cmd);
}
update_underscore_env(shell, cmd);
cmd = cmd->next;
}
wait_processes(shell, paks);
Expand Down
2 changes: 1 addition & 1 deletion src/exec_utils2.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void update_underscore_env(t_shell *shell, t_pak *cmd)
int i;

i = 0;
if (!cmd || !cmd->full_cmd || !cmd->full_cmd[i])
if (!cmd || !cmd->full_cmd || !cmd->full_cmd[0] || cmd->next)
return ;
while (cmd && cmd->full_cmd && cmd->full_cmd[i])
i++;
Expand Down
16 changes: 11 additions & 5 deletions src/heredoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ static int write_heredoc_to_file(char *content, char *heredoc_file)
return (open(heredoc_file, O_RDONLY));
}

void heredoc_child(char *delimiter)
void heredoc_child(char *delimiter, char *filename)
{
char *content;
int fd;
Expand All @@ -89,15 +89,21 @@ void heredoc_child(char *delimiter)
content = get_heredoc_input(delimiter);
if (!content)
return ;
fd = write_heredoc_to_file(content, HEREDOC_FILE);
if (!filename)
{
free(content);
exit(1);
}
fd = write_heredoc_to_file(content, filename);
free(content);
free(filename);
if (fd < 0)
exit(1);
close(fd);
exit(0);
}

int handle_heredoc(char *delimiter)
int handle_heredoc(char *delimiter, char *file)
{
t_proc process;

Expand All @@ -107,7 +113,7 @@ int handle_heredoc(char *delimiter)
if (process.pid == 0)
{
signal(SIGINT, SIG_DFL);
heredoc_child(delimiter);
heredoc_child(delimiter, file);
}
else
{
Expand All @@ -121,7 +127,7 @@ int handle_heredoc(char *delimiter)
}
if (WIFEXITED(process.status) && WEXITSTATUS(process.status) != 0)
return (-1);
return (open(HEREDOC_FILE, O_RDONLY));
return (open_heredoc(file));
}
return (-1);
}
49 changes: 49 additions & 0 deletions src/heredoc_utils.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* heredoc_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: drahwanj <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/19 09:13:55 by drahwanj #+# #+# */
/* Updated: 2025/05/19 09:13:56 by drahwanj ### ########.fr */
/* */
/* ************************************************************************** */

#include "minishell.h"

char *get_heredoc_file(int file)
{
char *number;
char *file_name;
char *heredoc_file;

number = ft_itoa(file);
if (!number)
return (NULL);
heredoc_file = ft_strdup(HEREDOC_FILE);
if (!heredoc_file)
{
free(number);
return (NULL);
}
file_name = ft_strjoin(heredoc_file, number);
if (!file_name)
{
free(number);
free(heredoc_file);
return (NULL);
}
free(number);
return (file_name);
}

int open_heredoc(char *file)
{
int fd;

if (!file)
return (-1);
fd = open(file, O_RDONLY);
return (fd);
}
2 changes: 2 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ void update_shlvl(t_shell *shell)
shell->shlvl = ft_atoi(env_grab(shell->envp, "SHLVL")) + 1;
shlvl = ft_itoa(shell->shlvl);
env_update(shell->envp, "SHLVL", shlvl);
env_update(shell->envp, "SHELL", shell->argv[0]);
free(shlvl);
}

Expand All @@ -30,6 +31,7 @@ void shell_init(t_shell *shell)
shell->exit = false;
shell->e_status = 0;
shell->last_pid = 0;
shell->heredoc = 0;
update_shlvl(shell);
}

Expand Down
10 changes: 7 additions & 3 deletions src/parse_redir.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ int parse_redir_in(t_shell *shell, t_pak **curr, t_token **token)

int parse_redir(t_shell *shell, t_pak **curr, t_token **token)
{
char *filename;

if ((*token)->type == TK_REDIR_OUT)
{
parse_redir_out(shell, curr, token);
Expand All @@ -92,20 +94,22 @@ int parse_redir(t_shell *shell, t_pak **curr, t_token **token)
}
else if ((*token)->type == TK_HEREDOC)
{
parse_heredoc(shell, curr, token);
filename = get_heredoc_file(++(shell->heredoc));
parse_heredoc(shell, curr, token, filename);
free(filename);
}
return (0);
}

int parse_heredoc(t_shell *shell, t_pak **curr, t_token **token)
int parse_heredoc(t_shell *shell, t_pak **curr, t_token **token, char *file)
{
int old_infile;

(*token) = (*token)->next;
if ((*curr)->infile > STDERR_FILENO)
close((*curr)->infile);
old_infile = (*curr)->infile;
(*curr)->infile = handle_heredoc((*token)->value);
(*curr)->infile = handle_heredoc((*token)->value, file);
if (old_infile == -1)
close((*curr)->infile);
if (old_infile == -1)
Expand Down
10 changes: 9 additions & 1 deletion src/token_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ static void combine_tokens(t_token *curr, t_token *next)
{
char *value;

value = ft_strjoin(curr->value, next->value);
if (curr->value && !ft_strcmp(curr->value, "$")
&& (next->type == TK_DOUBLE_QUOTED
|| next->type == TK_SINGLE_QUOTED))
{
value = ft_strdup(next->value);
free(curr->value);
}
else
value = ft_strjoin(curr->value, next->value);
if (!value)
return ;
free(next->value);
Expand Down