/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */
-/* Updated: 2025/01/15 16:35:40 by dkaiser ### ########.fr */
+/* Updated: 2025/01/15 17:20:56 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
t_list *parse(t_token *tokens, t_env **env);
t_node *parse_cmd(t_token *tokens, t_env **env);
-t_redirection *collect_redirs(t_token **tokens);
+t_redirection *collect_redirs(t_token **tokens, t_env *env);
void print_ast(t_node *ast);
int eval(t_node *node, t_env **env);
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */
-/* Updated: 2025/01/14 16:55:20 by chuhlig ### ########.fr */
+/* Updated: 2025/01/15 18:10:46 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
-static void collect_and_check_redir(t_redirection *result, t_token **cur);
-static void set_redir(t_redirection *redir, int type, char *specifier);
+static void collect_and_check_redir(t_redirection *result, t_token **cur,
+ t_env *env);
+static void set_redir(t_redirection *redir, int type, char *specifier,
+ t_env *env);
static char *read_heredoc(char *delimiter)
{
return (result);
}
-t_redirection *collect_redirs(t_token **tokens)
+t_redirection *collect_redirs(t_token **tokens, t_env *env)
{
t_redirection *result;
t_token *cur;
result = malloc(sizeof(t_redirection) * 2);
if (result == NULL)
return (free_tokens(*tokens), NULL);
- set_redir(&result[0], 0, NULL);
- set_redir(&result[1], 0, NULL);
+ set_redir(&result[0], 0, NULL, env);
+ set_redir(&result[1], 0, NULL, env);
while (cur != NULL && cur->next != NULL)
{
if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN)
- collect_and_check_redir(result, &cur);
+ collect_and_check_redir(result, &cur, env);
else if (cur->type == REDIR_TOKEN)
return (free(result), NULL);
else
return (result);
}
-static void set_redir(t_redirection *redir, int type, char *specifier)
+static void set_redir(t_redirection *redir, int type, char *specifier,
+ t_env *env)
{
redir->type = type;
- redir->specifier = specifier;
+ if (specifier != NULL)
+ redir->specifier = format_string(specifier, env);
+ else
+ redir->specifier = specifier;
}
-static void collect_and_check_redir(t_redirection *result, t_token **cur)
+static void collect_and_check_redir(t_redirection *result, t_token **cur,
+ t_env *env)
{
char *heredoc_data;
t_token *next_token;
perror("Heredoc allocation failed");
return ;
}
- set_redir(&result[0], INPUT_LIMITER, heredoc_data);
+ set_redir(&result[0], INPUT_LIMITER, heredoc_data, env);
}
else if ((*cur)->content.redir_type == INPUT_FILE)
set_redir(&result[0], INPUT_FILE,
- ft_strdup((*cur)->next->content.string));
+ ft_strdup((*cur)->next->content.string), env);
else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE)
set_redir(&result[1], OUTPUT_OVERRIDE,
- ft_strdup((*cur)->next->content.string));
+ ft_strdup((*cur)->next->content.string), env);
else if ((*cur)->content.redir_type == OUTPUT_APPEND)
set_redir(&result[1], OUTPUT_APPEND,
- ft_strdup((*cur)->next->content.string));
+ ft_strdup((*cur)->next->content.string), env);
next_token = (*cur)->next;
free_token_and_connect(*cur);
if (next_token)
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:15:49 by chuhlig #+# #+# */
-/* Updated: 2025/01/14 19:52:27 by chuhlig ### ########.fr */
+/* Updated: 2025/01/15 18:10:25 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
pid_t pid;
int p[2];
int result;
- int status;
int original_stdin;
if (node->type == PIPE_NODE)
original_stdin = dup(STDIN_FILENO);
dup2(p[0], STDIN_FILENO);
result = eval_rec(node->content.pipe.right, env, p[0]);
- waitpid(pid, &status, 0);
dup2(original_stdin, STDIN_FILENO);
close(original_stdin);
}