aboutsummaryrefslogtreecommitdiff
path: root/src/interpreter.c
blob: af1eb825924e9302074d287bb7d5a3ac7999fb57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   interpreter.c                                      :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: chuhlig <chuhlig@student.42.fr>            +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2024/12/17 19:15:49 by chuhlig           #+#    #+#             */
/*   Updated: 2025/01/14 16:59:45 by chuhlig          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

#include "debug_tools.h"
#include "minishell.h"

int	handle_redirections(t_redirection *redirs)
{
	int	fd;

	if (redirs[0].type == INPUT_FILE)
	{
		fd = open(redirs[0].specifier, O_RDONLY);
		if (fd < 0)
		{
			perror(redirs[0].specifier);
			return (-1);
		}
		dup2(fd, STDIN_FILENO);
		close(fd);
	}
	else if (redirs[0].type == INPUT_LIMITER)
	{
		fd = open("/tmp/heredoc_tmp", O_WRONLY | O_CREAT | O_TRUNC, 0644);
		if (fd < 0)
		{
			perror("open");
			return (-1);
		}
		write(fd, redirs[0].specifier, ft_strlen(redirs[0].specifier));
		close(fd);
		fd = open("/tmp/heredoc_tmp", O_RDONLY);
		if (fd < 0)
		{
			perror("open");
			return (-1);
		}
		dup2(fd, STDIN_FILENO);
		close(fd);
	}
	if (redirs[1].type == OUTPUT_OVERRIDE)
	{
		fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644);
		if (fd < 0)
		{
			perror("open");
			return (-1);
		}
		dup2(fd, STDOUT_FILENO);
		close(fd);
	}
	else if (redirs[1].type == OUTPUT_APPEND)
	{
		fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_APPEND, 0644);
		if (fd < 0)
		{
			perror("open");
			return (-1);
		}
		dup2(fd, STDOUT_FILENO);
		close(fd);
	}
	return (0);
}

int	eval_rec(t_node *node, t_env **env, int in_fd)
{
	pid_t	pid;
	int		p[2];
	int		result;
	int		status;
	int		original_stdin;

	if (node->type == PIPE_NODE)
	{
		pipe(p);
		pid = fork();
		if (pid == 0)
		{
			close(p[0]);
			dup2(in_fd, STDIN_FILENO);
			dup2(p[1], STDOUT_FILENO);
			result = eval_rec(node->content.pipe.left, env, in_fd);
			exit(result);
		}
		else
		{
			close(p[1]);
			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);
		}
	}
	else if (node->type == CMD_NODE)
		result = execute_cmd(&node->content.cmd, env);
	else
		result = EXIT_FAILURE;
	return (result);
}

int	eval(t_node *node, t_env **env)
{
	return (eval_rec(node, env, STDIN_FILENO));
}