diff --git a/Makefile b/Makefile index 89fd3425..9e61b4c3 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: rparodi +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/11/12 11:05:05 by rparodi #+# #+# # -# Updated: 2024/08/05 14:52:59 by maiboyer ### ########.fr # +# Updated: 2024/08/11 11:56:54 by maiboyer ### ########.fr # # # # **************************************************************************** # diff --git a/exec/Filelist.exec.mk b/exec/Filelist.exec.mk index 4bb119e2..7793dda1 100644 --- a/exec/Filelist.exec.mk +++ b/exec/Filelist.exec.mk @@ -1,4 +1,5 @@ SRC_FILES = \ +builtins/_debug \ builtins/cd \ builtins/echo \ builtins/env \ diff --git a/exec/include/exec/builtins.h b/exec/include/exec/builtins.h index 7e671643..16df1c56 100644 --- a/exec/include/exec/builtins.h +++ b/exec/include/exec/builtins.h @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/10 18:43:56 by maiboyer #+# #+# */ -/* Updated: 2024/08/11 11:20:22 by maiboyer ### ########.fr */ +/* Updated: 2024/08/11 12:01:18 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,5 +39,6 @@ t_error builtin_exit__(t_state *state, t_builtin_spawn_info info, t_i32 *exit_co t_error builtin_export(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code); t_error builtin_pwd___(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code); t_error builtin_unset_(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code); +t_error builtin_debug_(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code); #endif /* BUILTINS_H */ diff --git a/exec/src/builtins/_debug.c b/exec/src/builtins/_debug.c new file mode 100644 index 00000000..77443ac6 --- /dev/null +++ b/exec/src/builtins/_debug.c @@ -0,0 +1,52 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* _debug.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/10 18:43:18 by maiboyer #+# #+# */ +/* Updated: 2024/08/11 12:11:43 by maiboyer ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "exec/builtins.h" +#include "me/fs/fs.h" +#include "me/printf/printf.h" +#include "me/str/str.h" +#include "me/string/string.h" +#include "me/types.h" + +#define DEBUG_USAGE \ + "Usage:\n" \ + " - print_fd: print the opened file descritors informations" + +static t_error _debug_fd(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code) +{ + const t_fd_array *fds = get_fd_arrays(); + t_usize i; + + i = 0; + while (i < FILE_SLOT_LEN) + { + if (fds->storage[i].ty == SLOT_FD) + me_printf_fd(info.stderr, " FD[%i] => %s\n", fds->storage[i].slot.fd.fd, fds->storage[i].slot.fd.name); + if (fds->storage[i].ty == SLOT_FILE) + me_printf_fd(info.stderr, "FILE[%p] => %s\n", fds->storage[i].slot.file.ptr, fds->storage[i].slot.file.name); + if (fds->storage[i].ty == SLOT_DIR) + me_printf_fd(info.stderr, " DIR[%p] => %s\n", fds->storage[i].slot.dir.ptr, fds->storage[i].slot.dir.name); + i++; + } + *exit_code = 0; + return (NO_ERROR); +} + +t_error builtin_debug_(t_state *state, t_builtin_spawn_info info, t_i32 *exit_code) +{ + if (info.args.len != 2) + return (me_printf_fd(info.stdout, DEBUG_USAGE), *exit_code = 1, ERROR); + if (str_compare(info.args.buffer[1], "print_fd")) + return (_debug_fd(state, info, exit_code)); + *exit_code = 2; + return (ERROR); +} diff --git a/exec/src/run_ast.c b/exec/src/run_ast.c index 07c129ee..9f2eb14e 100644 --- a/exec/src/run_ast.c +++ b/exec/src/run_ast.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */ -/* Updated: 2024/08/11 11:41:48 by maiboyer ### ########.fr */ +/* Updated: 2024/08/11 12:19:11 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -597,7 +597,7 @@ t_error run_program(t_ast_program *self, t_state *state, t_program_result *out) i++; } - return (ERROR); + return (NO_ERROR); } // FUNCTIONS @@ -636,18 +636,30 @@ void _ffree_func(struct s_ffree_state *state) hmap_env_free(state->state->env); hmap_env_free(state->state->tmp_var); close_fd(state->cmd_pipe.input); - me_exit(127); } -bool _is_builtin(t_const_str argv0); +bool _is_builtin(t_const_str argv0) +{ + t_usize i; + const t_str value[] = {"cd", "echo", "env", "exit", "export", "pwd", "unset", "_debug", NULL}; + + i = 0; + if (argv0 == NULL) + return (false); + while (value[i] != NULL) + if (str_compare(argv0, value[i++])) + return (true); + return (false); +} t_error _handle_builtin(t_spawn_info info, t_state *state, t_cmd_pipe cmd_pipe, t_command_result *out) { t_usize i; const t_const_str argv0 = info.binary_path; - const t_str value[] = {"cd", "echo", "env", "exit", "export", "pwd", "unset", NULL}; - const t_builtin_func funcs[] = {builtin_cd____, builtin_echo__, builtin_env___, builtin_exit__, - builtin_export, builtin_pwd___, builtin_unset_, NULL}; + const t_str value[] = {"cd", "echo", "env", "exit", "export", "pwd", "unset", "_debug", NULL}; + const t_builtin_func funcs[] = {builtin_cd____, builtin_echo__, builtin_env___, + builtin_exit__, builtin_export, builtin_pwd___, + builtin_unset_, builtin_debug_, NULL}; t_builtin_func actual_func; i = 0; @@ -715,13 +727,18 @@ t_error _handle_builtin(t_spawn_info info, t_state *state, t_cmd_pipe cmd_pipe, else { if (actual_func(state, binfo, &exit_code)) - return (out->exit = 126, ERROR); - out->exit = exit_code; + out->exit = 126; + else + out->exit = exit_code; } + if (binfo.stdin) + close_fd(binfo.stdin); + if (binfo.stdout) + close_fd(binfo.stdout); + if (binfo.stderr) + close_fd(binfo.stderr); vec_str_free(info.arguments); str_free(info.binary_path); - // we need to check if we have to fork ! - return (NO_ERROR); } @@ -837,7 +854,7 @@ t_error _spawn_cmd_and_run(t_vec_str args, t_vec_ast redirection, t_state *state signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); if (spawn_process(info, &out->process)) - return (ERROR); + return (close_fd(cmd_pipe.input), out->exit = 127, ERROR); int status; if (waitpid(out->process.pid, &status, 0) == -1) return (ERROR); @@ -848,21 +865,6 @@ t_error _spawn_cmd_and_run(t_vec_str args, t_vec_ast redirection, t_state *state return (NO_ERROR); } -bool _is_builtin(t_const_str argv0) -{ - t_usize i; - const t_str value[] = {"cd", "echo", "env", "exit", "export", "pwd", "unset", NULL}; - const t_str funcs[] = {"cd", "echo", "env", "exit", "export", "pwd", "unset", NULL}; - - i = 0; - if (argv0 == NULL) - return (false); - while (value[i] != NULL) - if (str_compare(argv0, value[i++])) - return (true); - return (false); -} - t_error run_command(t_ast_command *command, t_state *state, t_cmd_pipe cmd_pipe, t_command_result *out) { t_vec_str args; diff --git a/stdme/src/os/process.c b/stdme/src/os/process.c index 10f2c222..ccd21585 100644 --- a/stdme/src/os/process.c +++ b/stdme/src/os/process.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/03 16:22:41 by maiboyer #+# #+# */ -/* Updated: 2024/08/10 18:41:56 by maiboyer ### ########.fr */ +/* Updated: 2024/08/11 12:00:25 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,7 +44,7 @@ t_error spawn_process_exec(t_spawn_info info, t_process *process) vec_str_push(&info.arguments, NULL); vec_str_push(&info.environement, NULL); execve(info.binary_path, info.arguments.buffer, info.environement.buffer); - return (NO_ERROR); + return (ERROR); } t_error in_path(t_spawn_info *info, t_process *process, t_const_str path_raw,