Updated grammar to remove input fd for redirection

This commit is contained in:
Maieul BOYER 2024-07-31 16:50:00 +00:00
parent 27875d8466
commit 18bf872c44
1005 changed files with 77750 additions and 102351 deletions

View file

@ -6,18 +6,17 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */
/* Updated: 2024/07/30 17:03:12 by maiboyer ### ########.fr */
/* Updated: 2024/07/31 15:33:50 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "app/state.h"
#include "ast/ast.h"
#include "exec/run.h"
#include "exec/spawn_cmd/ppipe.h"
#include "exec/spawn_cmd/pprocess.h"
#include "me/convert/numbers_to_str.h"
#include "me/hashmap/hashmap_env.h"
#include "me/mem/mem.h"
#include "me/os/process.h"
#include "me/str/str.h"
#include "me/string/string.h"
#include "me/types.h"
@ -26,7 +25,11 @@
#include <stdio.h>
#pragma clang diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunknown-pragmas"
#pragma GCC diagnostic ignored "-Wempty-body"
#pragma clang diagnostic ignored "-Wunknown-pragmas"
#pragma clang diagnostic ignored "-Wunused-parameter"
#pragma clang diagnostic ignored "-Wunused-variable"
@ -42,7 +45,7 @@ typedef struct s_command_result t_command_result;
struct s_command_result
{
t_p_process process;
t_process process;
};
typedef struct s_word_result t_word_result;
@ -88,11 +91,6 @@ t_error run_until(t_ast_until *until, t_state *state, void *out);
t_error run_variable_assignment(t_ast_variable_assignment *variable_assignment, t_state *state, void *out);
t_error run_while_(t_ast_while *while_, t_state *state, void *out);
#ifdef ERROR
# undef ERROR
#endif
#define ERROR ((void)printf("ERROR HERE: " __FILE__ ":%d in %s\n", __LINE__, __func__), 1)
#define NOT_DONE \
{ \
printf("function `%s` isn't done !\n", __func__); \
@ -729,5 +727,3 @@ t_error run_node(t_ast_node self, t_state *state, void *out)
}
*/
#pragma clang diagnostic pop

View file

@ -1,18 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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 */

View file

@ -1,22 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_redirection.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 22:27:00 by maiboyer #+# #+# */
/* Updated: 2024/07/30 17:02:51 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec/spawn_cmd/ppipe.h"
#include "exec/spawn_cmd/pprocess.h"
#include "me/types.h"
t_error handle_redirections(t_p_spawn_info *info, t_p_process *process)
{
(void)(info);
(void)(process);
return (ERROR);
}

View file

@ -1,30 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* iter_funcs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 22:25:44 by maiboyer #+# #+# */
/* Updated: 2024/07/30 13:22:34 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_find_path(const t_str *s)
{
t_str str;
if (*s == NULL)
return (false);
str = *s;
return (str[0] == 'P' && str[1] == 'A' && str[2] == 'T' && str[3] == 'H' && str[4] == '=');
}

View file

@ -1,38 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipe.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 17:59:48 by maiboyer #+# #+# */
/* Updated: 2024/07/30 17:02:58 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec/spawn_cmd/ppipe.h"
#include "me/fs/fs.h"
#include "me/str/str.h"
t_error exec_create_pipe(t_exec_pipe *out)
{
int fd[2];
struct s_file_slot *read;
struct s_file_slot *write;
if (pipe(fd))
return (ERROR);
read = get_unused_fd_slot();
if (read == NULL)
return (close(fd[0]), close(fd[1]), ERROR);
read->ty = SLOT_FD;
write = get_unused_fd_slot();
if (write == NULL)
return (read->ty = SLOT_UNUSED, close(fd[0]), close(fd[1]), ERROR);
write->ty = SLOT_FD;
read->slot.fd = (t_fd){.fd = fd[0], .type = FD_PIPE, .name = str_clone("<pipe[R]>"), .perms = FD_READ};
read->slot.fd = (t_fd){.fd = fd[1], .type = FD_PIPE, .name = str_clone("<pipe[W]>"), .perms = FD_WRITE};
out->read = &read->slot.fd;
out->write = &write->slot.fd;
return (NO_ERROR);
}

View file

@ -1,125 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* process.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */
/* Updated: 2024/07/30 17:03:05 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec/spawn_cmd/pprocess.h"
#include "exec/spawn_cmd/ppipe.h"
#include "me/mem/mem.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);
t_error exec_handle_redirections(t_p_spawn_info *info, t_p_process *process);
t_error exec_spawn_process_exec(t_p_spawn_info info, t_p_process *process)
{
bool res;
(void)(process);
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 (ERROR);
}
t_error exec_in_path(t_p_spawn_info *info, t_p_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) == 0)
return (vec_str_free(path), NO_ERROR);
}
return (vec_str_free(path), ERROR);
}
t_error exec_find_binary(t_p_spawn_info *info, t_p_process *process)
{
t_usize p_idx;
t_string s;
(void)(process);
if (info->binary_path == NULL)
return (ERROR);
s = string_new(256);
if (info->binary_path[0] == '/' || 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) == 0)
{
mem_free(info->binary_path);
info->binary_path = s.buf;
return (NO_ERROR);
}
return (string_free(s), ERROR);
}
static void cleanup(t_p_spawn_info info, t_p_process *process, bool cleanup_process)
{
if (cleanup_process && process->stdin != NULL)
close_fd(process->stdin);
if (cleanup_process && process->stdout != NULL)
close_fd(process->stdout);
if (cleanup_process && process->stderr != NULL)
close_fd(process->stderr);
close_fd(info.stdin.vals.fd.value);
close_fd(info.stdout.vals.fd.value);
close_fd(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_p_spawn_info info, t_vec_ast *redirection, t_p_process *process)
{
(void)(redirection);
if (exec_handle_redirections(&info, process))
return (cleanup(info, process, true), ERROR);
if (exec_find_binary(&info, process))
return (cleanup(info, process, true), ERROR);
process->pid = fork();
if (process->pid == 0)
(exec_spawn_process_exec(info, process), exit(1));
else
{
cleanup(info, process, false);
if (process->pid == -1)
return (ERROR);
}
return (NO_ERROR);
}