diff --git a/srcs/builtins.c b/srcs/builtins.c index 32b98e8..6526f20 100644 --- a/srcs/builtins.c +++ b/srcs/builtins.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:20:21 by marde-vr #+# #+# */ -/* Updated: 2024/03/27 14:59:17 by tomoron ### ########.fr */ +/* Updated: 2024/03/27 16:25:53 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ int cmd_is_builtin(t_msh *msh, char *cmd_token) } else if (!ft_strcmp(cmd_token, "exit")) { - exit_bt(msh); + g_return_code = exit_bt(msh); return (1); } else if (!ft_strcmp(cmd_token, "export")) diff --git a/srcs/commands.c b/srcs/commands.c index b1ccc9b..34ef592 100644 --- a/srcs/commands.c +++ b/srcs/commands.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:22:15 by marde-vr #+# #+# */ -/* Updated: 2024/03/27 16:20:06 by tomoron ### ########.fr */ +/* Updated: 2024/03/27 17:01:49 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -71,10 +71,9 @@ char **get_cmd_args(t_msh *msh) if (cur_cmd->type == ARG) { if (!i) - cmd_args[i] = remove_path(cur_cmd->value); + cmd_args[i++] = remove_path(cur_cmd->value); else - cmd_args[i] = cur_cmd->value; - i++; + cmd_args[i++] = cur_cmd->value; } else cur_cmd = cur_cmd->next; diff --git a/srcs/echo.c b/srcs/echo.c index d7b7357..6b9bd44 100755 --- a/srcs/echo.c +++ b/srcs/echo.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/07 15:30:37 by tomoron #+# #+# */ -/* Updated: 2024/03/27 16:21:14 by tomoron ### ########.fr */ +/* Updated: 2024/03/27 17:20:40 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,7 +47,7 @@ int echo(t_token *args) if (!args->value[i]) put_nl = 0; else - ft_printf("%s ",args->value); + ft_printf("%s ", args->value); args = args->next; } put_args(args); diff --git a/srcs/exec.c b/srcs/exec.c index 42e9f7b..e9d8119 100755 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */ -/* Updated: 2024/03/27 14:49:53 by tomoron ### ########.fr */ +/* Updated: 2024/03/27 16:52:02 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -66,31 +66,17 @@ void exec_command(t_msh *msh, int i, int cmd_count) remove_command_from_msh(msh); } -void exec_commands(t_msh *msh) +void end_execution(t_msh *msh, int cmd_count) { - int cmd_count; int i; int status; - i = -1; - if (!msh->cmds) - return ; - cmd_count = get_cmd_count(msh->cmds); - msh->fds = ft_calloc(cmd_count, sizeof(int **)); - msh->pids = ft_calloc(cmd_count, sizeof(int *)); - if (!msh->pids || !msh->fds) - ft_exit(msh, 1); - while (++i < cmd_count) - exec_command(msh, i, cmd_count); i = 0; while (i < cmd_count) - { - waitpid(msh->pids[i], &status, 0); - i++; - } + waitpid(msh->pids[i++], &status, 0); if (!g_return_code && WIFEXITED(status)) g_return_code = WEXITSTATUS(status); - if(WIFSIGNALED(status) && WTERMSIG(status) == SIGQUIT) + if (WIFSIGNALED(status) && WTERMSIG(status) == SIGQUIT) printf("Quit (core dumped)\n"); i = 0; while (i < cmd_count) @@ -107,3 +93,21 @@ void exec_commands(t_msh *msh) signal(SIGQUIT, signal_handler_interactive); set_echoctl(0); } + +void exec_commands(t_msh *msh) +{ + int cmd_count; + int i; + + i = -1; + if (!msh->cmds) + return ; + cmd_count = get_cmd_count(msh->cmds); + msh->fds = ft_calloc(cmd_count, sizeof(int **)); + msh->pids = ft_calloc(cmd_count, sizeof(int *)); + if (!msh->pids || !msh->fds) + ft_exit(msh, 1); + while (++i < cmd_count) + exec_command(msh, i, cmd_count); + end_execution(msh, cmd_count); +} diff --git a/srcs/exit.c b/srcs/exit.c index 2ec9513..44e469e 100755 --- a/srcs/exit.c +++ b/srcs/exit.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */ -/* Updated: 2024/03/27 15:35:57 by tomoron ### ########.fr */ +/* Updated: 2024/03/27 16:24:45 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,7 +33,7 @@ void get_exit_bt_return_code(t_msh *msh, int *exit_code) *exit_code = g_return_code; } -void exit_bt(t_msh *msh) +int exit_bt(t_msh *msh) { t_token *cur_cmd; int exit_code; @@ -43,10 +43,7 @@ void exit_bt(t_msh *msh) ft_printf("exit\n"); if (cur_cmd && cur_cmd->next && cur_cmd->next->type == ARG && ft_strisnbr(cur_cmd->value)) - { ft_putstr_fd("minishell: exit: too many arguments\n", 2); - g_return_code = 1; - } else { get_exit_bt_return_code(msh, &exit_code); @@ -62,4 +59,5 @@ void exit_bt(t_msh *msh) free_msh(msh); exit(exit_code); } + return (1); } diff --git a/srcs/export.c b/srcs/export.c index 048c43e..ae87291 100644 --- a/srcs/export.c +++ b/srcs/export.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/18 18:29:20 by marde-vr #+# #+# */ -/* Updated: 2024/03/27 14:57:44 by tomoron ### ########.fr */ +/* Updated: 2024/03/27 16:55:22 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ void print_env_declare(t_env *env) { if (strcmp(env->name, "_")) { - if(*(env->value)) + if (*(env->value)) printf("declare -x %s=\"%s\"\n", env->name, env->value); else printf("declare -x %s\n", env->name); @@ -27,22 +27,18 @@ void print_env_declare(t_env *env) } } -int check_var_name(char *name) +int export_invalid_identifier(char *arg, char *name) { - if (ft_isdigit(*name) || !*name) - return (0); - while (*name) - { - if (!ft_isalnum(*name) && *name != '_') - return (0); - name++; - } + ft_putstr_fd("minishell: export: `", 2); + ft_putstr_fd(arg, 2); + ft_putstr_fd("': not a valid identifier\n", 2); + free(name); return (1); } int ft_export(t_msh *msh) { - t_cmd *cmd; + t_token *cmd; char *arg; char *name; char *value; @@ -60,13 +56,7 @@ int ft_export(t_msh *msh) len++; name = ft_substr(arg, 0, len); if (!name || !check_var_name(name)) - { - ft_putstr_fd("minishell: export: `", 2); - ft_putstr_fd(arg, 2); - ft_putstr_fd("': not a valid identifier\n", 2); - free(name); - return (1); - } + return (export_invalid_identifier(arg, name)); if (arg[len]) len++; value = ft_strdup(arg + len); @@ -102,7 +92,7 @@ void delete_from_env(t_msh *msh, char *name) int ft_unset(t_msh *msh) { - t_cmd *cmd; + t_token *cmd; cmd = msh->cmds; if (cmd) diff --git a/srcs/lst_cmd.c b/srcs/lst_cmd.c index ff04eec..db3defb 100755 --- a/srcs/lst_cmd.c +++ b/srcs/lst_cmd.c @@ -6,17 +6,22 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */ -/* Updated: 2024/03/27 15:56:11 by tomoron ### ########.fr */ +/* Updated: 2024/03/27 17:18:03 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_cmd *cmd_add_back(t_token *cmd, char *value, t_token_type type) +t_token *cmd_add_back(t_token *cmd, char *value, t_token_type type) { - t_cmd *res; - t_cmd *current; + t_token *res; + t_token *current; + if (value && !*value) + { + free(value); + return (cmd); + } res = ft_calloc(1, sizeof(t_token)); if (!res) return (cmd); @@ -31,7 +36,7 @@ t_cmd *cmd_add_back(t_token *cmd, char *value, t_token_type type) return (cmd); } -void free_cmd(t_token *cmd) +int free_cmd(t_token *cmd) { if (cmd) { @@ -48,4 +53,5 @@ void free_cmd(t_token *cmd) free(cmd); cmd = 0; } + return (0); } diff --git a/srcs/main.c b/srcs/main.c index 3af54e7..e09ee56 100755 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */ -/* Updated: 2024/03/27 15:05:42 by tomoron ### ########.fr */ +/* Updated: 2024/03/27 17:02:16 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -71,39 +71,11 @@ t_env *get_env(char **envp) return (env); } -void print_binary(unsigned int num) -{ - int size; - int i; - - if (num == 0) - { - printf("0"); - return ; - } - - //Tom qu'est-ce que t'a fait ????? - - // Taille d'un unsigned int en bits - size = sizeof(unsigned int) * 8; - // Parcours de chaque bit de droite à gauche - for (i = size - 1; i >= 0; i--) - { - // Vérifie si le bit est 1 ou 0 - if (num & (1u << i)) - printf("1"); - else - printf("0"); - } -} - int init_minishell(t_msh **msh, int argc, char **argv, char **envp) { struct termios t_p; - *msh = ft_calloc(1, sizeof(t_msh)); - if (!*msh) - ft_exit(*msh, 1); + ft_exit(*msh, 1); (void)argc; (void)argv; (*msh)->env = get_env(envp); @@ -111,7 +83,7 @@ int init_minishell(t_msh **msh, int argc, char **argv, char **envp) (*msh)->echoctl = t_p.c_lflag & ECHOCTL; signal(SIGINT, signal_handler_interactive); signal(SIGQUIT, signal_handler_interactive); - if(set_echoctl(0)) + if (set_echoctl(0)) ft_exit(*msh, 1); return (0); } diff --git a/srcs/minishell.h b/srcs/minishell.h index 64b1411..04a44d1 100755 --- a/srcs/minishell.h +++ b/srcs/minishell.h @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */ -/* Updated: 2024/03/27 15:24:41 by tomoron ### ########.fr */ +/* Updated: 2024/03/27 17:20:14 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,7 +52,7 @@ typedef struct s_env typedef struct s_msh { struct s_env *env; - struct s_cmd *cmds; + struct s_token *cmds; int **fds; int *pids; enum e_token_type in_type; @@ -66,10 +66,10 @@ typedef struct s_msh extern int g_return_code; t_token *cmd_add_back(t_token *res, char *token, t_token_type type); -void free_cmd(t_token *cmd); +int free_cmd(t_token *cmd); void exec_commands(t_msh *msh); int echo(t_token *args); -void exit_bt(t_msh *msh); +int exit_bt(t_msh *msh); t_env *env_add_back(t_env *env, char *name, char *value); void free_env(t_env *env); int print_env(t_env *env); @@ -118,5 +118,7 @@ char *get_tmp_file_name(t_msh *msh); int contains_newline(char *str); void parse_var(t_msh *msh, char *line); int set_echoctl(int value); +int add_return_code_to_str(char *res); +int parsing_syntax_error(t_token *res); #endif diff --git a/srcs/parsing.c b/srcs/parsing.c index cdd59da..abd69e0 100755 --- a/srcs/parsing.c +++ b/srcs/parsing.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */ -/* Updated: 2024/03/24 08:50:11 by marde-vr ### ########.fr */ +/* Updated: 2024/03/27 17:20:20 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" @@ -47,10 +47,7 @@ char *get_token(char **cmd, int *in_quote, int *in_dquote, t_env *env) if (**cmd == '\'' && !*in_dquote) *in_quote = !*in_quote; if (**cmd == '$' && !*in_quote) - { - (*cmd)++; i += add_var_to_str(res + i, cmd, env); - } else if (**cmd == '~' && !*in_quote && !*in_dquote) i += add_home_to_str(res + i); else if (((**cmd == '\'' && *in_dquote) || (**cmd == '"' && *in_quote)) @@ -86,12 +83,12 @@ t_token_type get_token_type(char **command) return (res); } -t_cmd *parse_command(char *command, t_env *env) +t_token *parse_command(char *command, t_env *env) { int in_quote; int in_dquote; - t_cmd *res; - char *token; + t_token *res; + char *value; t_token_type type; in_quote = 0; @@ -101,26 +98,16 @@ t_cmd *parse_command(char *command, t_env *env) { type = get_token_type(&command); if (type == ARG) - token = get_token(&command, &in_quote, &in_dquote, env); + value = get_token(&command, &in_quote, &in_dquote, env); else - token = 0; - if (type == ARG && token == 0) - { - free_cmd(res); - return (0); - } - if (token && !*token) - free(token); - else - res = cmd_add_back(res, token, type); + value = 0; + if (type == ARG && value == 0) + return (free_cmd(res)); + res = cmd_add_back(res, value, type); while (ft_isspace(*command)) command++; } if (command && (in_quote || in_dquote)) - { - free_cmd(res); - ft_putstr_fd("minishell: syntax error\n", 2); - return (0); - } + return (parsing_syntax_error(res)); return (res); } diff --git a/srcs/parsing_bonus.c b/srcs/parsing_bonus.c index 83ccdec..98ca660 100644 --- a/srcs/parsing_bonus.c +++ b/srcs/parsing_bonus.c @@ -6,10 +6,8 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/27 14:40:44 by tomoron #+# #+# */ -/* Updated: 2024/03/27 15:01:00 by tomoron ### ########.fr */ +/* Updated: 2024/03/27 17:20:51 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" - - diff --git a/srcs/parsing_var.c b/srcs/parsing_var.c index a2f6a2a..5339ee0 100755 --- a/srcs/parsing_var.c +++ b/srcs/parsing_var.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/09 15:24:36 by tomoron #+# #+# */ -/* Updated: 2024/03/05 17:29:06 by marde-vr ### ########.fr */ +/* Updated: 2024/03/27 17:09:50 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -72,20 +72,11 @@ int get_token_len(char *command, t_env *env) return (res); } -int add_return_code_to_str(char *res) +int invalid_variable_char(char *res, char c) { - char *var; - int i; - - i = 0; - var = ft_itoa(g_return_code); - while (var && var[i]) - { - res[i] = var[i]; - i++; - } - free(var); - return (i); + res[0] = '$'; + res[1] = c; + return (2); } int add_var_to_str(char *res, char **command, t_env *env) @@ -95,6 +86,7 @@ int add_var_to_str(char *res, char **command, t_env *env) int i; i = 0; + (*command)++; if (**command == '\'' || **command == '"') { *res = '$'; @@ -102,21 +94,14 @@ int add_var_to_str(char *res, char **command, t_env *env) return (1); } if (!ft_isalnum(**command) && **command != '_' && **command != '?') - { - res[0] = '$'; - res[1] = **command; - return (2); - } + return (invalid_variable_char(res, **command)); if (**command == '?') return (add_return_code_to_str(res)); var_name = get_var_name(*command); var = ft_get_env(env, var_name); free(var_name); while (var && var[i]) - { - res[i] = var[i]; - i++; - } + res[i] = var[i++]; *command += get_var_name_len(*command) - 1; return (i); } diff --git a/srcs/signal_handler.c b/srcs/signal_handler.c index 738990b..373f909 100644 --- a/srcs/signal_handler.c +++ b/srcs/signal_handler.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/22 14:31:13 by tomoron #+# #+# */ -/* Updated: 2024/03/27 16:22:39 by tomoron ### ########.fr */ +/* Updated: 2024/03/27 17:21:09 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -76,6 +76,7 @@ ags"); } return (0); } + void signal_handler_command(int signum) { (void)signum; diff --git a/srcs/utils.c b/srcs/utils.c index 7ef81e1..1aaa2ca 100644 --- a/srcs/utils.c +++ b/srcs/utils.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:19:26 by marde-vr #+# #+# */ -/* Updated: 2024/03/27 15:00:24 by tomoron ### ########.fr */ +/* Updated: 2024/03/27 16:55:04 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,6 +31,35 @@ void ft_exit(t_msh *msh, int exit_code) exit(exit_code); } +int check_var_name(char *name) +{ + if (ft_isdigit(*name) || !*name) + return (0); + while (*name) + { + if (!ft_isalnum(*name) && *name != '_') + return (0); + name++; + } + return (1); +} + +int add_return_code_to_str(char *res) +{ + char *var; + int i; + + i = 0; + var = ft_itoa(g_return_code); + while (var && var[i]) + { + res[i] = var[i]; + i++; + } + free(var); + return (i); +} + int file_access(t_msh *msh, int *found) { int fd; diff --git a/srcs/utils2.c b/srcs/utils2.c new file mode 100644 index 0000000..57e907c --- /dev/null +++ b/srcs/utils2.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/03/27 17:19:27 by tomoron #+# #+# */ +/* Updated: 2024/03/27 17:19:35 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int parsing_syntax_error(t_token *res) +{ + free_cmd(res); + ft_putstr_fd("minishell: syntax error\n", 2); + return (0); +}