exec start

This commit is contained in:
Maieul BOYER 2024-07-29 19:10:26 +02:00
parent bec7ecf5c3
commit ab8b8a77bb
No known key found for this signature in database
9 changed files with 482 additions and 32 deletions

View file

@ -6,18 +6,18 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */
/* Updated: 2024/07/28 19:19:09 by maiboyer ### ########.fr */
/* Updated: 2024/07/29 19:04:56 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "app/state.h"
#include "ast/ast.h"
#include "exec/run.h"
#include "exec/spawn_cmd/pipe.h"
#include "exec/spawn_cmd/process.h"
#include "me/convert/numbers_to_str.h"
#include "me/hashmap/hashmap_env.h"
#include "me/mem/mem.h"
#include "me/os/pipe.h"
#include "me/os/process.h"
#include "me/str/str.h"
#include "me/string/string.h"
#include "me/types.h"
@ -629,36 +629,8 @@ t_error run_command(t_ast_command *command, t_state *state, t_command_result *ou
i = 0;
while (i < command->cmd_word.len)
{
tmp = command->cmd_word.buffer[i];
if (tmp->kind == AST_WORD)
{
if (_word_into_str(tmp, state, &args))
return (ERROR);
}
if (tmp->kind == AST_EXPANSION)
{
if (run_expansion(&tmp->data.expansion, state, &exp_out))
return (ERROR);
if (!(exp_out.exists && exp_out.value != NULL))
continue;
if (str_split(exp_out.value, _get_ifs_value(state), &split))
return (ERROR);
while (!vec_str_pop_front(&split, &tmp_str))
vec_str_push(&args, tmp_str);
vec_str_free(split);
}
if (tmp->kind == AST_ARITHMETIC_EXPANSION)
{
if (run_arithmetic_expansion(&tmp->data.arithmetic_expansion, state, &arith_out))
return (ERROR);
if (i64_to_str(arith_out, &tmp_str))
return (ERROR);
vec_str_push(&args, tmp_str);
}
if (tmp->kind == AST_COMMAND_SUBSTITUTION)
{
if (_ast_into_str(command->cmd_word.buffer[i], state, &args))
return (ERROR);
}
i++;
}
while (i < command->prefixes.len)

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* gnu_source.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 17:53:46 by maiboyer #+# #+# */
/* Updated: 2024/07/10 17:54:49 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef GNU_SOURCE_H
# define GNU_SOURCE_H
# define _GNU_SOURCE
#endif /* GNU_SOURCE_H */

24
exec/src/spawn_cmd/pipe.c Normal file
View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipe.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 17:59:48 by maiboyer #+# #+# */
/* Updated: 2024/07/29 19:05:12 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec/spawn_cmd/pipe.h"
t_error exec_create_pipe(t_pipe *out)
{
int fds[2];
if (pipe(fds))
return (ERROR);
out->read = fds[0];
out->write = fds[1];
return (NO_ERROR);
}

View file

@ -0,0 +1,124 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* process.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */
/* Updated: 2024/07/29 19:06:23 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec/spawn_cmd/process.h"
#include "me/mem/mem.h"
#include "me/os/pipe.h"
#include "me/printf/printf.h"
#include "me/str/str.h"
#include "me/string/string.h"
#include "me/types.h"
#include "me/vec/vec_ast.h"
#include "me/vec/vec_str.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
bool exec_find_path(const t_str *s);
bool exec_find_null(const t_str *s);
bool exec_str_start_with(t_const_str s, t_const_str prefix);
t_error exec_handle_redirections(t_spawn_info *info, t_process *process);
t_error exec_spawn_process_exec(t_spawn_info info, t_process *process)
{
bool res;
if (info.forked_free)
info.forked_free(info.forked_free_args);
if (!vec_str_any(&info.arguments, exec_find_null, &res) && res)
vec_str_push(&info.arguments, NULL);
res = false;
if (!vec_str_any(&info.environement, exec_find_null, &res) && res)
vec_str_push(&info.environement, NULL);
execve(info.binary_path, info.arguments.buffer, info.environement.buffer);
return (NO_ERROR);
}
t_error exec_in_path(t_spawn_info *info, t_process *process, t_const_str path_raw, t_string *s)
{
t_vec_str path;
t_usize idx;
(void)(process);
if (str_split(path_raw + 5, ":", &path))
return (string_free(*s), ERROR);
idx = 0;
while (idx < path.len)
{
string_clear(s);
me_printf_str(s, "%s/%s", path.buffer[idx++], info->binary_path);
if (access(s->buf, X_OK | R_OK) == 0)
return (vec_str_free(path), NO_ERROR);
}
return (vec_str_free(path), ERROR);
}
t_error exec_find_binary(t_spawn_info *info, t_process *process)
{
t_usize p_idx;
t_string s;
(void)(process);
if (info->binary_path == NULL)
return (ERROR);
s = string_new(256);
if (exec_str_start_with(info->binary_path, "/") || str_find_chr(info->binary_path, '/') != NULL)
string_push(&s, info->binary_path);
else
{
if (vec_str_find(&info->environement, exec_find_path, &p_idx))
return (string_free(s), ERROR);
if (exec_in_path(info, process, info->environement.buffer[p_idx], &s))
return (ERROR);
}
if (access(s.buf, X_OK | R_OK) == 0)
{
mem_free(info->binary_path);
info->binary_path = s.buf;
return (NO_ERROR);
}
return (string_free(s), ERROR);
}
static void exec_cleanup(t_spawn_info info, t_process *process, bool cleanup_process)
{
if (cleanup_process && process->stdin.tag != INVALID)
close(process->stdin.vals.ro.fd);
if (cleanup_process && process->stdout.tag != INVALID)
close(process->stdout.vals.ro.fd);
if (cleanup_process && process->stderr.tag != INVALID)
close(process->stderr.vals.ro.fd);
close(info.stdin.vals.fd.value);
close(info.stdout.vals.fd.value);
close(info.stderr.vals.fd.value);
vec_str_free(info.arguments);
vec_str_free(info.environement);
mem_free(info.binary_path);
}
t_error exec_spawn_process(t_spawn_info info, t_vec_ast *redirection, t_process *process)
{
if (exec_handle_redirections(&info, process))
return (exec_cleanup(info, process, true), ERROR);
if (exec_find_binary(&info, process))
return (exec_cleanup(info, process, true), ERROR);
process->pid = fork();
if (process->pid == 0)
(exec_spawn_process_exec(info, process), exit(1));
else
{
exec_cleanup(info, process, false);
if (process->pid == -1)
return (ERROR);
}
return (NO_ERROR);
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* process_inner.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 22:27:00 by maiboyer #+# #+# */
/* Updated: 2024/07/29 19:05:38 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec/spawn_cmd/pipe.h"
#include "exec/spawn_cmd/process.h"
#include "me/types.h"
t_error handle_redirections(t_spawn_info *info, t_process *process)
{
}

View file

@ -0,0 +1,41 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* process_iter_funcs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 22:25:44 by maiboyer #+# #+# */
/* Updated: 2024/07/29 17:22:42 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/types.h"
#include <stdbool.h>
#include <stdio.h>
bool exec_find_null(const t_str *s)
{
return (s == NULL);
}
bool exec_str_start_with(t_const_str s, t_const_str prefix)
{
while (*prefix && *s)
{
if (*prefix++ != *s++)
return (false);
}
return (*prefix == '\0');
}
bool exec_find_path(const t_str *s)
{
t_str ss;
if (*s == NULL)
return (false);
ss = *s;
return (ss[0] == 'P' && ss[1] == 'A' && ss[2] == 'T' && ss[3] == 'H' && \
ss[4] == '=');
}