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

@ -2,8 +2,4 @@ SRC_FILES = \
arith/arith \
arith/arith_operation \
run_ast \
spawn_cmd/handle_redirection \
spawn_cmd/iter_funcs \
spawn_cmd/pipe \
spawn_cmd/process \

View file

@ -1,31 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ppipe.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 17:57:29 by maiboyer #+# #+# */
/* Updated: 2024/07/30 17:03:32 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PPIPE_H
#define PPIPE_H
#include "me/fs/fs.h"
#include "me/types.h"
/// @brief Pipe structure
typedef struct s_exec_pipe
{
t_fd *read;
t_fd *write;
} t_exec_pipe;
/// @brief Create a pipe
/// @param[out] out the created pipe
/// @return the error
t_error create_pipe(t_exec_pipe *out);
#endif /* PIPE_H */

View file

@ -1,110 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pprocess.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 15:43:08 by maiboyer #+# #+# */
/* Updated: 2024/07/30 17:06:38 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PPROCESS_H
#define PPROCESS_H
#include "me/fs/fs.h"
#include "me/types.h"
#include "me/vec/vec_str.h"
#include "me/vec/vec_u8.h"
typedef t_i32 t_pid;
typedef t_i32 t_exit_code;
enum e_p_redirection
{
PR_INHERITED = 0,
PR_PIPED = 1,
PR_FD = 2,
};
union u_p_redirection {
struct s_pfd_redirection
{
t_fd *value;
} fd;
struct s_ppiped_redirection
{
} piped;
struct s_pinherited_redirection
{
} inherited;
};
/// @brief Redirection for spawning a process
typedef struct s_p_redirection
{
enum e_p_redirection tag;
union u_p_redirection vals;
} t_p_redirection;
/// @brief Create a piped redirection
/// @param void
/// @return the redirection
static inline t_p_redirection ppiped(void)
{
return ((t_p_redirection){
.tag = PR_PIPED,
});
}
/// @brief Create an inherited redirection (inherit from the parent process)
/// @param void
/// @return the redirection
static inline t_p_redirection pinherited(void)
{
return ((t_p_redirection){
.tag = PR_INHERITED,
});
}
/// @brief Create a file descriptor redirection
/// @param fd file descriptor to redirect
/// @return the redirection
static inline t_p_redirection pfd(t_fd *fd)
{
return ((t_p_redirection){.tag = PR_FD,
.vals = (union u_p_redirection){
.fd = {.value = fd},
}});
}
/// @brief Spawn information
typedef struct s_p_spawn_info
{
t_p_redirection stdin;
t_p_redirection stdout;
t_p_redirection stderr;
t_vec_str arguments;
t_vec_str environement;
t_str binary_path;
void (*forked_free)(void *);
void *forked_free_args;
} t_p_spawn_info;
/// @brief Process information
typedef struct s_p_process
{
t_fd *stdin;
t_fd *stderr;
t_fd *stdout;
t_pid pid;
} t_p_process;
/// @brief Spawn a new process with the given information
/// @param info the information to spawn the process
/// @param process data returned by the function
/// @return true if an error occured, false otherwise
t_error pspawn_process(t_p_spawn_info info, t_p_process *process);
#endif /* PROCESS_H */

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);
}