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
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: charles <charles@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/14 10:41:31 by charles #+# #+# */
/* Updated: 2020/07/20 14:53:40 by nahaddac ### ########.fr */
/* */
/* ************************************************************************** */
#include "eval.h"
pid_t g_child_pid = -1;
int g_last_status_code = 0;
void token_debug(void *v);
/*
** \brief Wrap a function in a fork
** \param fd_in fork input file descriptor
** \param fd_out fork output file descriptor
** \param passed param of the wrapped function
** \param wrapped function to wrap
*/
int fork_wrap(
int fds[2],
void *passed,
int (*wrapped)(void *param))
{
int status;
pid_t child_pid;
if ((child_pid = fork()) == -1)
return (-1);
if (child_pid == 0)
{
if ((fds[FDS_READ] != MS_NO_FD && dup2(fds[FDS_READ], STDIN_FILENO) == -1) ||
(fds[FDS_WRITE] != MS_NO_FD && dup2(fds[FDS_WRITE], STDOUT_FILENO) == -1))
exit(EXIT_FAILURE);
if ((status = wrapped(passed)) == -1)
exit(EXIT_FAILURE);
exit(status);
}
g_child_pid = child_pid;
wait(&child_pid);
close(fds[FDS_WRITE]);
// also read end?
return (WEXITSTATUS(child_pid));
}
int forked_cmd(void *void_param)
{
t_fork_param_cmd *param;
int ret;
param = void_param;
ft_vecpop(param->env_local, NULL);
if (ft_vecswallow_at(param->env, param->env->size - 1, param->env_local) == NULL)
{
ft_vecdestroy(param->env_local, free);
return (-1);
}
if (param->builtin != NULL)
return (param->builtin->func(param->argv, param->env));
else
{
errno = 0;
ret = execve(param->exec_path, param->argv, (char**)param->env->data);
if (ret == -1)
{
if (errno == ENOEXEC)
return (0);
struct stat statbuf;
if (stat(param->exec_path, &statbuf) != -1 && S_ISDIR(statbuf.st_mode))
{
errorf("%s: Is a directory\n", param->exec_path);
ret = 126;
}
else
{
errorf("%s: %s\n", param->exec_path, strerror(errno));
ret = 126;
}
}
return (ret);
}
}
int eval_cmd(int fds[2], t_env env, t_path path, t_ast *ast)
{
t_fork_param_cmd param;
char **argv;
char *id;
t_ftlst *tmp;
if (!redir_extract(ast->redirs, env, fds))
{
ast->redirs = NULL;
return (-1);
}
ast->redirs = NULL;
if ((param.env_local = env_from_array((char*[]){NULL})) == NULL)
return (-1);
// TODO generate token list after `=` for variable value preprocessing
while (ast->cmd_argv != NULL
&& ((t_token*)ast->cmd_argv->data)->tag & TAG_IS_STR
&& utils_start_with_valid_identifier(((t_token*)ast->cmd_argv->data)->content))
{
id = ((t_token*)ast->cmd_argv->data)->content;
*ft_strchr(id, '=') = '\0';
((t_token*)ast->cmd_argv->data)->content = ft_strchr(id, '\0') + 1;
tmp = split_token(&ast->cmd_argv, TAG_STICK);
preprocess(tmp, env);
ft_lstiter(tmp, token_debug);
if (env_export(param.env_local,id, ((t_token*)ast->cmd_argv->data)->content) == NULL)
return (-1);
ft_lstpop_front(&ast->cmd_argv, (void (*)(void*))token_destroy);
}
if (ast->cmd_argv == NULL) // FIXME special env not passed to child processes
{
ft_vecpop(param.env_local, NULL);
if (ft_vecswallow_at(env, env->size - 1, param.env_local) == NULL)
{
ft_vecdestroy(param.env_local, free);
return (-1);
}
g_last_status_code = 0;
return (0);
}
if ((argv = preprocess(&ast->cmd_argv, env)) == NULL)
{
ast->cmd_argv = NULL;
return (-1);
}
// can have no command (e.g `< file`)
if (argv[0] == NULL)
return (0);
param.builtin = builtin_search_func(argv[0]);
if (param.builtin == NULL)
{
// check env local for PATH
param.exec_path = exec_search_path(path, env_search(env, "PATH"), argv[0]);
if (param.exec_path == NULL)
{
g_last_status_code = 127;
errorf("%s: command not found\n", argv[0]);
ft_split_destroy(argv);
return (-1); // return error status
}
}
else if (!param.builtin->forked)
{
g_last_status_code = param.builtin->func(argv, env);
return (g_last_status_code);
}
param.argv = argv;
param.env = env;
int ret = fork_wrap(fds, ¶m, &forked_cmd);
g_last_status_code = ret;
ft_split_destroy(argv);
ft_vecdestroy(param.env_local, free);
return (ret);
}
|