From 726cc9bbac524d6e3ed8775721ea6c4192af4685 Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Sun, 1 Sep 2024 14:40:18 +0200 Subject: [PATCH] Make the project entirely compliant with the Norm --- inc/builtins.h | 4 +- inc/execution.h | 55 +++++++++++++------------- inc/minishell.h | 11 ++++-- inc/minishell_structs.h | 18 +++++---- inc/parsing_info.h | 6 +-- src/add_word.c | 4 +- src/assignments.c | 6 +-- src/exe_helpers.c | 4 +- src/exe_prepare.c | 10 ++--- src/execute_builtin.c | 87 ++++++++++++++++++++--------------------- src/execute_cmd.c | 5 ++- src/expand.c | 4 +- src/expand_split.c | 4 +- src/redir_file.c | 3 +- src/redir_here.c | 6 +-- src/redirection.c | 3 +- src/token_finish.c | 4 +- 17 files changed, 118 insertions(+), 116 deletions(-) diff --git a/inc/builtins.h b/inc/builtins.h index 30ee6b6..6922207 100644 --- a/inc/builtins.h +++ b/inc/builtins.h @@ -6,14 +6,14 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/26 11:39:54 by ljiriste #+# #+# */ -/* Updated: 2024/08/27 15:02:28 by ljiriste ### ########.fr */ +/* Updated: 2024/09/01 14:29:59 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef BUILTINS_H # define BUILTINS_H -#include "minishell_structs.h" +# include "minishell_structs.h" int cd(int argc, char **argv, t_execution_env *env); int echo(int argc, char **argv); diff --git a/inc/execution.h b/inc/execution.h index bc27ae1..d76503a 100644 --- a/inc/execution.h +++ b/inc/execution.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 17:40:19 by ljiriste #+# #+# */ -/* Updated: 2024/09/01 14:03:42 by lnikolov ### ########.fr */ +/* Updated: 2024/09/01 14:43:27 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,39 +19,40 @@ extern volatile sig_atomic_t g_last_signal; void free_split(char **fields); -int set_quote(char *quote, char c); -int is_token_type(const t_parse_tree_node *node, const char *type); +int set_quote(char *quote, char c); +int is_token_type(const t_parse_tree_node *node, const char *type); -int dup_pipes(const t_execution_env *env); -int dup_redirections(const t_vec *redirections); +int dup_pipes(const t_execution_env *env); +void dup_redirections(const t_vec *redirections); char **quoted_split(const char *str); -int unquote_field(char *field); -int add_word(t_vec *exp_str, const char *word, - const t_execution_env *env, int enquote_result); +int unquote_field(char *field); +int add_word(t_vec *exp_str, const char *word, + const t_execution_env *env, int enquote_result); char **expand(t_parse_tree_node *simple_command, const t_execution_env *env); char *find_exe(const char *exe_name, const t_execution_env *env); -int prepare_env(t_execution_env *env, - const t_vec *redirections, t_vec *assignments); - -int ex_builtin(char **fields, __attribute__((unused)) - t_vec *assignments, const t_vec *redirections, t_execution_env *env); -int ex_fields(char **fields, t_vec *assignments, - const t_vec *redirections, t_execution_env *env); -int ex_command(t_parse_tree_node *command, t_execution_env *env); -int ex_pipeline(t_parse_tree_node *pipeline, t_execution_env *env, int depth); -int ex_program(t_parse_tree_node *program, t_execution_env *env); +int prepare_env(t_execution_env *env, + const t_vec *redirections, t_vec *assignments); + +int ex_builtin(char **fields, __attribute__((unused)) t_vec *assignments, + const t_vec *redirections, t_execution_env *env); +int ex_fields(char **fields, t_vec *assignments, + const t_vec *redirections, t_execution_env *env); +int ex_command(t_parse_tree_node *command, t_execution_env *env); +int ex_pipeline(t_parse_tree_node *pipeline, + t_execution_env *env, int depth); +int ex_program(t_parse_tree_node *program, t_execution_env *env); char *get_word(const t_parse_tree_node *parent); -int add_redirection_here(t_vec *redirections, - t_parse_tree_node *io_here, int fd, const t_execution_env *env); -int add_redirection_file(t_vec *redirections, - t_parse_tree_node *io_file, int fd, t_execution_env *env); - -int save_redirections(t_vec *redirections, - t_parse_tree_node *simple_command, t_execution_env *env); -int save_assignments(t_vec *assignments, t_parse_tree_node *simple_command, - const t_execution_env *env); +int add_redirection_here(t_vec *redirections, + t_parse_tree_node *io_here, int fd, const t_execution_env *env); +int add_redirection_file(t_vec *redirections, + t_parse_tree_node *io_file, int fd, t_execution_env *env); + +int save_redirections(t_vec *redirections, + t_parse_tree_node *simple_command, t_execution_env *env); +int save_assignments(t_vec *assignments, t_parse_tree_node *simple_command, + const t_execution_env *env); #endif // EXECUTION_H diff --git a/inc/minishell.h b/inc/minishell.h index 3516221..bb2634b 100644 --- a/inc/minishell.h +++ b/inc/minishell.h @@ -6,7 +6,7 @@ /* By: lnikolov +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 12:57:18 by ljiriste #+# #+# */ -/* Updated: 2024/08/02 14:18:00 by ljiriste ### ########.fr */ +/* Updated: 2024/09/01 14:30:23 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,7 @@ // Generated by xxd -i with small changes // from shell_parsing_table and shell_rules -static char g_str_table[6725] = { +static const char g_str_table[6725] = { 0x53, 0x74, 0x61, 0x74, 0x65, 0x3b, 0x44, 0x47, 0x52, 0x45, 0x41, 0x54, 0x3b, 0x47, 0x52, 0x45, 0x41, 0x54, 0x3b, 0x4c, 0x45, 0x53, 0x53, 0x3b, 0x44, 0x4c, 0x45, 0x53, 0x53, 0x3b, 0x49, 0x4f, 0x5f, 0x4e, 0x55, 0x4d, @@ -580,7 +580,7 @@ static char g_str_table[6725] = { 0x3b, 0x3b, 0x3b, 0x0a, 0x00 }; -static char g_str_rules[941] = { +static const char g_str_rules[941] = { 0x70, 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x20, 0x2d, 0x3e, 0x20, 0x70, 0x72, 0x6f, 0x67, 0x72, 0x61, 0x6d, 0x20, 0x41, 0x4e, 0x44, 0x5f, 0x49, 0x46, 0x20, 0x70, 0x69, 0x70, 0x65, 0x6c, 0x69, 0x6e, 0x65, 0x0a, 0x70, diff --git a/src/add_word.c b/src/add_word.c index 6db9d3d..cb510ba 100644 --- a/src/add_word.c +++ b/src/add_word.c @@ -6,7 +6,7 @@ /* By: lnikolov from_to_fds[1]); ++i; } - return (0); + return ; } static int add_exported(t_vec *assignments, const t_execution_env *env) @@ -85,7 +85,7 @@ int prepare_env(t_execution_env *env, close_fds(&env->fds_to_close); res = dup_pipes(env); - res = dup_redirections(redirections) || res; + dup_redirections(redirections); res = add_exported(assignments, env) || res; return (res); -} \ No newline at end of file +} diff --git a/src/execute_builtin.c b/src/execute_builtin.c index e3dbab9..4854dac 100644 --- a/src/execute_builtin.c +++ b/src/execute_builtin.c @@ -6,7 +6,7 @@ /* By: lnikolov #include -static int save_std_filenos(int fds[3]) +static void close_redirections(const t_vec *redirections) { - fds[0] = dup(STDIN_FILENO); - if (fds[0] < 0) - return (1); - fds[1] = dup(STDOUT_FILENO); - if (fds[1] < 0) - { - close(fds[0]); - return (1); - } - fds[2] = dup(STDERR_FILENO); - if (fds[2] < 0) + size_t i; + const t_redirection *redir; + + i = 0; + while (i < redirections->size) { - close(fds[0]); - close(fds[1]); - return (1); + redir = ft_vec_caccess(redirections, i); + close(redir->from_to_fds[0]); + ++i; } - return (0); + return ; } -static int restore_std_filenos(int fds[3]) +static int restore_fds(int fds[3], const t_vec *redirections, + t_execution_env *env) { + close_redirections(redirections); + close(env->stdin_fd); + close(env->stdout_fd); if (dup2(fds[0], STDIN_FILENO) == -1) return (1); if (dup2(fds[1], STDOUT_FILENO) == -1) @@ -52,11 +50,30 @@ static int restore_std_filenos(int fds[3]) return (0); } -static void close_pipes(const t_execution_env *env) +static int prepare_fds(int fds[3], const t_vec *redirections, + t_execution_env *env) { - close(env->stdin_fd); - close(env->stdout_fd); - return ; + fds[0] = dup(STDIN_FILENO); + if (fds[0] < 0) + return (1); + fds[1] = dup(STDOUT_FILENO); + if (fds[1] < 0) + { + close(fds[0]); + return (1); + } + fds[2] = dup(STDERR_FILENO); + if (fds[2] < 0) + { + close(fds[0]); + close(fds[1]); + return (1); + } + dup_redirections(redirections); + if (!dup_pipes(env)) + return (0); + restore_fds(fds, redirections, env); + return (1); } static size_t count_fields(char **fields) @@ -69,29 +86,13 @@ static size_t count_fields(char **fields) return (i); } -static void close_redirections(const t_vec *redirections) -{ - size_t i; - const t_redirection *redir; - - i = 0; - while (i < redirections->size) - { - redir = ft_vec_caccess(redirections, i); - close(redir->from_to_fds[0]); - ++i; - } - return ; -} - int ex_builtin(char **fields, __attribute__((unused)) t_vec *assignments, const t_vec *redirections, t_execution_env *env) { int fds[3]; - save_std_filenos(fds); - dup_pipes(env); - dup_redirections(redirections); + if (prepare_fds(fds, redirections, env)) + return (1); if (!ft_strcmp(fields[0], "cd")) env->builtin_ret_val = cd(count_fields(fields), fields, env); else if (!ft_strcmp(fields[0], "echo")) @@ -108,8 +109,6 @@ int ex_builtin(char **fields, __attribute__((unused)) env->builtin_ret_val = execute_exit(count_fields(fields), fields, env); else return (1); - close_redirections(redirections); - close_pipes(env); - restore_std_filenos(fds); + restore_fds(fds, redirections, env); return (0); -} \ No newline at end of file +} diff --git a/src/execute_cmd.c b/src/execute_cmd.c index 36cc216..4d8f973 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -6,7 +6,7 @@ /* By: lnikolov #include - - static char *here_file_getline(void) { ft_printf("> "); @@ -109,4 +107,4 @@ int add_redirection_here(t_vec *redirections, redir = (t_redirection){.from_to_fds[0] = fd, .from_to_fds[1] = pipe_fds[0]}; return (ft_vec_append(redirections, &redir) != success); -} \ No newline at end of file +} diff --git a/src/redirection.c b/src/redirection.c index 9f4bbce..97005c5 100644 --- a/src/redirection.c +++ b/src/redirection.c @@ -6,7 +6,7 @@ /* By: lnikolov -const char *type_enum_to_str(enum token_type type) +const char *type_enum_to_str(t_token_type type) { static const char *tokens[12] = { "WORD", -- 2.30.2