Added: Pipex as ressources, not most readable code tho...

This commit is contained in:
Maieul BOYER 2024-03-27 13:42:54 +01:00
parent 9482844642
commit 5d425048f2
No known key found for this signature in database
38 changed files with 2975 additions and 0 deletions

View file

@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/06 18:51:07 by maiboyer #+# #+# */
/* Updated: 2024/01/06 18:52:58 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MAIN_H
# define MAIN_H
# include "me/fs/open.h"
# include "me/fs/read.h"
# include "me/fs/write.h"
# include "me/gnl/gnl.h"
# include "me/mem/mem_find_bytes.h"
# include "me/mem/mem_move.h"
# include "me/os/pipe.h"
# include "me/os/process.h"
# include "me/printf/printf.h"
# include "me/string/split_literals.h"
# include "me/string/str_clone.h"
# include "me/string/str_len.h"
# include "me/string/str_n_compare.h"
# include "me/string/str_n_find_str.h"
# include "me/vec/vec_process.h"
# include "me/vec/vec_str.h"
# include <stdlib.h>
# include <sys/wait.h>
# include <unistd.h>
# ifndef BONUS
# define BONUS 0
# endif
typedef struct s_pipex_args
{
t_usize argc;
t_str *argv;
bool here_doc;
t_str here_doc_limiter;
t_file here_doc_fd;
t_vec_str env;
t_file in;
t_file out;
t_vec_process processes;
t_usize min;
t_usize max;
} t_pipex_args;
void set_here_doc(t_pipex_args *s);
void open_read_file(t_pipex_args *s);
void clone_vec_str_iter_fn(t_usize idx, t_str *s, void *out);
t_vec_str clone_vec_str(t_vec_str *v);
void vec_process_free_ptr(void *s);
t_error spawn_helper(t_pipex_args *s, t_usize idx, t_redirection in,
t_redirection out);
void cleanup(t_pipex_args *s, bool error);
void process_cleanup(t_process elem);
void do_here_doc_thing(t_pipex_args *s);
int main2(t_pipex_args *s);
#endif /* MAIN_H */

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipe.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 17:57:29 by maiboyer #+# #+# */
/* Updated: 2024/01/04 17:59:30 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PIPE_H
# define PIPE_H
# include "me/types.h"
typedef struct s_pipe
{
t_file read;
t_file write;
} t_pipe;
t_error create_pipe(t_pipe *out);
#endif /* PIPE_H */

View file

@ -0,0 +1,145 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* process.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/03 15:43:08 by maiboyer #+# #+# */
/* Updated: 2024/01/06 18:39:58 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PROCESS_H
# define PROCESS_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_redirection
{
R_INHERITED = 0,
R_PIPED = 1,
R_FD = 2,
};
union u_redirection
{
struct s_fd
{
t_file value;
} fd;
struct s_piped
{
} piped;
struct s_inherited
{
} inherited;
};
typedef struct s_redirection
{
enum e_redirection tag;
union u_redirection vals;
} t_redirection;
static inline t_redirection piped(void)
{
return ((t_redirection){
.tag = R_PIPED,
});
}
static inline t_redirection inherited(void)
{
return ((t_redirection){
.tag = R_INHERITED,
});
}
static inline t_redirection fd(t_file fd)
{
return ((t_redirection){.tag = R_FD, \
.vals = (union u_redirection){.fd = {.value = fd},}});
}
enum e_wrapped_fd_tag
{
READ_ONLY,
WRITE_ONLY,
READ_WRITE,
INVALID,
};
union u_wrapped_fd
{
struct s_read_only
{
t_file fd;
} ro;
struct s_write_only
{
t_file fd;
} wo;
struct s_read_write
{
t_file fd;
} rw;
};
typedef struct s_wrapped_fd
{
enum e_wrapped_fd_tag tag;
union u_wrapped_fd vals;
} t_wrapped_fd;
static inline t_wrapped_fd ro(t_file fd)
{
return ((t_wrapped_fd){.tag = READ_ONLY,
.vals = (union u_wrapped_fd){
.ro = {.fd = fd},
}});
}
static inline t_wrapped_fd wo(t_file fd)
{
return ((t_wrapped_fd){.tag = WRITE_ONLY,
.vals = (union u_wrapped_fd){.wo = {.fd = fd}}});
}
typedef struct s_spawn_info
{
t_redirection stdin;
t_redirection stdout;
t_redirection stderr;
t_vec_str arguments;
t_vec_str environement;
t_str binary_path;
void (*forked_free)(void *);
void *forked_free_args;
} t_spawn_info;
typedef struct s_process
{
t_wrapped_fd stdin;
t_wrapped_fd stdout;
t_wrapped_fd stderr;
t_pid pid;
} t_process;
typedef struct s_process_output
{
t_pid pid;
t_vec_u8 stdout;
t_vec_u8 stderr;
t_exit_code exit_code;
} t_process_output;
t_error spawn_process(t_spawn_info info,
t_process *process);
#endif /* PROCESS_H */

View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* inner_split_literals.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 14:33:41 by maiboyer #+# #+# */
/* Updated: 2024/01/06 18:42:41 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef INNER_SPLIT_LITERALS_H
# define INNER_SPLIT_LITERALS_H
# include "me/string/split_literals.h"
# include "me/types.h"
typedef struct s_booleans
{
bool error;
bool lit_sq;
bool lit_dq;
bool esc;
bool append;
} t_booleans;
char unescape(t_const_str s, t_usize *current_index, bool *did_escape,
t_error *error);
#endif /* INNER_SPLIT_LITERALS_H */

View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* split_literals.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/04 14:31:04 by maiboyer #+# #+# */
/* Updated: 2024/01/04 14:32:40 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef SPLIT_LITERALS_H
# define SPLIT_LITERALS_H
# include "me/types.h"
# include "me/vec/vec_str.h"
t_error split_literals(t_const_str s, t_vec_str *out);
#endif /* SPLIT_LITERALS_H */