aboutsummaryrefslogtreecommitdiff
path: root/src/eval/eval.c
blob: 0e57f15030f153b2d657e500a012a486929bf777 (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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   eval.c                                             :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: charles <charles.cabergs@gmail.com>        +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/04/01 17:05:21 by charles           #+#    #+#             */
/*   Updated: 2020/04/02 17:08:18 by charles          ###   ########.fr       */
/*                                                                            */
/* ************************************************************************** */

/*
** \file   eval.c
** \brief  Evaluation of an AST
*/

#include "eval.h"

#define PARAM_SIZE 3

#define PARAM_STATE 0
#define PARAM_LINE 1

#define PARAM_EXEC_PATH 0
#define PARAM_ARGV 1
#define PARAM_ENVP 2

int			io_frame_init(t_io_frame *frame)
{
	if (pipe(frame->pipe_in) == -1
		|| pipe(frame->pipe_out) == -1)
		return (-1);
	/* frame->pipe_in[PIPE_READ] = STDIN_FILENO; */
	/* frame->pipe_in[PIPE_WRITE] = PIPE_CLOSED; */
	/* frame->pipe_out[PIPE_READ] = PIPE_CLOSED; */
	/* frame->pipe_out[PIPE_WRITE] = STDOUT_FILENO; */
	return (0);
}

static int	eval_root(void *params[PARAM_SIZE])
{
	int				status;
	t_eval_state	*state;
	t_line			*line;
	t_io_frame		frame_left;
	t_io_frame		frame_right;

	state = params[PARAM_STATE];
	line = params[PARAM_LINE];
	io_frame_init(&frame_left);
	if (line->right == NULL)
		return (eval(&frame_left, state, line->left));

	if (line->sep == SEP_PIPE)
		dup2(STDOUT_FILENO, frame_left.pipe_out[PIPE_WRITE]);

	if ((status = eval(&frame_left, state, line->left)) == -1)
		return (-1);
	if ((line->sep == SEP_AND && status != 0) ||
		(line->sep == SEP_OR && status == 0))
		return (status);

	if (line->sep == SEP_PIPE)
		dup2(frame_right.pipe_in[PIPE_WRITE], frame_left.pipe_out[PIPE_READ]);

	return (eval(&frame_right, state, line->right));
}

int execve_wrapper(void *params[PARAM_SIZE])
{
	return (execve(
		params[PARAM_EXEC_PATH],
		params[PARAM_ARGV],
		params[PARAM_ENVP]
	));
}

int exec_wrap(
	t_io_frame *frame,
	void *passed[PARAM_SIZE],
	int (*wrapped)(void *params[PARAM_SIZE])
)
{
	int		status;
	pid_t	child_pid;

	if ((child_pid = fork()) == -1)
		return (-1);
	if (child_pid == 0)
	{
		if (dup2(STDIN_FILENO, frame->pipe_in[PIPE_READ]) == -1 ||
			dup2(STDOUT_FILENO, frame->pipe_out[PIPE_WRITE]) == -1)
			exit(EXIT_FAILURE);
		if ((status = wrapped(passed)) == -1)
			exit(EXIT_FAILURE);
		exit(status);
	}
	wait(&child_pid);
	return (WEXITSTATUS(child_pid));
}

/*
** \brief        Evaluate a line
** \param state  State of the evaluation
** \param line   Line to evaluate
** \return       Last Executed command status or -1 on error
*/

static int	eval_line(t_io_frame *frame, t_eval_state *state, t_line *line)
{
	void	*params[PARAM_SIZE];

	params[PARAM_STATE] = state;
	params[PARAM_LINE] = line;
	return (exec_wrap(frame, params, &eval_root));
}

int run_builtin(t_eval_state *state, char **argv)
{
	return (builtin_dispatch_run(argv, state->env));
}

/*
** \brief        Evaluate a command
** \param state  Evaluation state
** \param cmd    Command to evaluate
** \return       Executable status or -1 on error
*/

static int	eval_cmd(t_io_frame *frame, t_eval_state *state, t_cmd *cmd)
{
	void	*params[PARAM_SIZE];

	if (builtin_check_exec_name(cmd->argv[0]))
		return (run_builtin(state, cmd->argv));
	params[PARAM_EXEC_PATH] = exec_search_path(
			state->path, env_search(state->env, "PATH"), cmd->argv[0]);
	if (params[PARAM_EXEC_PATH] == NULL)
		return (-1);
	if (cmd->in != NULL)
		if ((frame->pipe_in[PIPE_WRITE] = open(cmd->in, O_RDONLY)) == -1)
			return (-1);
	if (cmd->out != NULL)
		if ((frame->pipe_out[PIPE_READ] = open(cmd->out,
				(cmd->is_append ? O_APPEND : O_RDONLY) | O_CREAT)) == -1)
			return (-1);
	params[PARAM_ARGV] = cmd->argv;
	params[PARAM_ENVP] = state->env->data;
	return (exec_wrap(frame, params, &execve_wrapper));
}

/*
** \brief        Evaluate an AST
** \param state  State of the evaluation
** \param ast    Abstract syntax tree to evaluate
** \return       Executable status or -1 on error
*/

int			eval(t_io_frame *frame, t_eval_state *state, t_ast *ast)
{
	void	*params[PARAM_SIZE];

	errno = 0;
	if (ast->tag == TAG_ROOT)
	{
		params[PARAM_STATE] = state;
		params[PARAM_LINE] = &ast->data.line;
		return (eval_root(params));
	}
	if (ast->tag == TAG_LINE)
		return (eval_line(frame, state, &ast->data.line));
	if (ast->tag == TAG_CMD)
		return (eval_cmd(frame, state, &ast->data.cmd));
	return (-1);
}