From eb5f2bd32ec9e749059b194479b708c058f95751 Mon Sep 17 00:00:00 2001 From: tomoron Date: Mon, 25 Mar 2024 12:42:23 +0100 Subject: [PATCH] =?UTF-8?q?h=C3=A9=C3=A9=C3=A9=C3=A9h=C3=A9=C3=A9=C3=A9?= =?UTF-8?q?=C3=A9=C3=A9=C3=A9h=C3=A9=C3=A9h=C3=A9=C3=A9=C3=A9h=C3=A9=C3=A9?= =?UTF-8?q?=C3=A9=C3=A9=C3=A9h=C3=A9=C3=A9=C3=A9h=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 7 +--- srcs/alias.c | 79 --------------------------------------------- srcs/builtins.c | 16 +-------- srcs/exec.c | 16 ++++----- srcs/handle_alias.c | 37 --------------------- srcs/lst_alias.c | 55 ------------------------------- srcs/main.c | 7 ++-- srcs/minishell.h | 16 +-------- srcs/pipe.c | 4 +-- srcs/unalias.c | 57 -------------------------------- srcs/utils.c | 3 +- 11 files changed, 13 insertions(+), 284 deletions(-) delete mode 100644 srcs/alias.c delete mode 100644 srcs/handle_alias.c delete mode 100755 srcs/lst_alias.c delete mode 100644 srcs/unalias.c diff --git a/Makefile b/Makefile index 960a38e..293b2f8 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: marde-vr +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/07/28 00:35:01 by tomoron #+# #+# # -# Updated: 2024/03/22 14:32:18 by tomoron ### ########.fr # +# Updated: 2024/03/25 12:33:26 by tomoron ### ########.fr # # # # **************************************************************************** # @@ -18,16 +18,11 @@ SRCS_RAW = main.c\ exec.c\ exit.c\ echo.c\ - alias.c\ - unalias.c\ pwd.c\ parsing.c\ debug.c\ env_to_char_tab.c\ parsing_var.c\ - handle_alias.c\ - lst_alias.c\ - minishellrc.c\ path.c\ here_doc.c\ export.c\ diff --git a/srcs/alias.c b/srcs/alias.c deleted file mode 100644 index f0c30b1..0000000 --- a/srcs/alias.c +++ /dev/null @@ -1,79 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* alias.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: marde-vr +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/02/16 23:16:07 by marde-vr #+# #+# */ -/* Updated: 2024/02/21 17:39:40 by marde-vr ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -char *get_alias_name(t_cmd *arg) -{ - int i; - char *res; - - i = 0; - while (arg->token[i] && arg->token[i] != '=') - i++; - i++; - res = ft_calloc(i, 1); - ft_strlcpy(res, arg->token, i); - return (res); -} - -char *get_alias_value(t_cmd *arg) -{ - int i; - char *res; - - i = 0; - while (arg->token[i] != '=') - i++; - i++; - res = ft_strdup(arg->token + i); - return (res); -} - -void print_aliases(t_alias *aliases) -{ - t_alias *cur_alias; - - cur_alias = aliases; - while (cur_alias) - { - ft_printf("alias %s=%s\n", cur_alias->name, cur_alias->value); - cur_alias = cur_alias->next; - } -} - -int alias(t_msh *msh) -{ - char *name; - char *value; - - if (!msh->cmds->next || msh->cmds->next->type != ARG) - print_aliases(msh->aliases); - else - { - name = get_alias_name(msh->cmds->next); - if (ft_strchr(msh->cmds->next->token, '=')) - { - value = get_alias_value(msh->cmds->next); - msh->aliases = alias_add_back(msh->aliases, name, value); - } - else - { - if (get_alias(msh->aliases, name)) - ft_printf("alias %s=%s\n", name, get_alias(msh->aliases, name)); - else - ft_printf("minishell: alias %s: not found\n", name); - free(name); - } - } - return (0); -} diff --git a/srcs/builtins.c b/srcs/builtins.c index 5ae500a..9c3bd9f 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/23 19:21:57 by marde-vr ### ########.fr */ +/* Updated: 2024/03/25 12:36:27 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,16 +30,6 @@ int cmd_is_builtin(t_msh *msh, char *cmd_token) cd(msh->cmds); return (1); } - else if (!ft_strcmp(cmd_token, "unalias")) - { - g_return_code = unalias(msh); - return (1); - } - else if (!ft_strcmp(cmd_token, "alias")) - { - alias(msh); - return (1); - } else if (!ft_strcmp(cmd_token, "exit")) { exit_bt(msh); @@ -75,10 +65,6 @@ int exec_builtin(t_msh *msh) g_return_code = pwd(); else if (!ft_strcmp(msh->cmds->token, "cd")) g_return_code = cd(msh->cmds); - else if (!ft_strcmp(msh->cmds->token, "alias")) - g_return_code = alias(msh); - else if (!ft_strcmp(msh->cmds->token, "unalias")) - g_return_code = unalias(msh); else return (0); return (1); diff --git a/srcs/exec.c b/srcs/exec.c index 4073141..bfbd7ce 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/24 09:59:16 by marde-vr ### ########.fr */ +/* Updated: 2024/03/24 16:27:24 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,7 +44,7 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) void exec_command(t_msh *msh, int i, int cmd_count) { - //g_return_code = 0; + g_return_code = 0; msh->fds[i] = ft_calloc(2, sizeof(int *)); if (!msh->fds[i]) ft_exit(msh, 1); @@ -62,7 +62,8 @@ void exec_command(t_msh *msh, int i, int cmd_count) } if (!cmd_is_builtin(msh, msh->cmds->token)) get_cmd_path(msh); - exec(msh, get_cmd_args(msh), i, cmd_count); + if(!g_return_code) + exec(msh, get_cmd_args(msh), i, cmd_count); remove_command_from_msh(msh); } @@ -86,15 +87,10 @@ void exec_commands(t_msh *msh) while (i < cmd_count) { waitpid(msh->pids[i], &status, 0); - //if (/*!g_return_code*/i == cmd_count - 1) - //{ - //ft_printf_fd(2, "old: %d, new: %d\n", g_return_code, WEXITSTATUS(status)); - //ft_printf_fd(2, "replace value\n"); - //g_return_code = WEXITSTATUS(status); - //} i++; } - g_return_code = WEXITSTATUS(status); + if(!g_return_code) + g_return_code = WEXITSTATUS(status); i = 0; while (i < cmd_count) { diff --git a/srcs/handle_alias.c b/srcs/handle_alias.c deleted file mode 100644 index a76c81e..0000000 --- a/srcs/handle_alias.c +++ /dev/null @@ -1,37 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* handle_alias.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: marde-vr +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/02/17 02:54:36 by tomoron #+# #+# */ -/* Updated: 2024/03/23 19:23:28 by marde-vr ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -t_cmd *handle_alias(t_msh *msh) -{ - t_cmd *res; - t_cmd *tmp; - char *alias_command; - - alias_command = 0; - if (!msh->cmds) - return (0); - if (msh->cmds->type == ARG) - alias_command = get_alias(msh->aliases, msh->cmds->token); - if (!alias_command) - return (msh->cmds); - res = parse_command(alias_command, msh->env); - tmp = res; - while (tmp->next) - tmp = tmp->next; - tmp->next = msh->cmds->next; - if (msh->cmds) - free(msh->cmds->token); - free(msh->cmds); - return (res); -} diff --git a/srcs/lst_alias.c b/srcs/lst_alias.c deleted file mode 100755 index 38ffbb2..0000000 --- a/srcs/lst_alias.c +++ /dev/null @@ -1,55 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* lst_alias.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: marde-vr +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */ -/* Updated: 2024/02/21 13:09:00 by marde-vr ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -t_alias *alias_add_back(t_alias *alias, char *name, char *value) -{ - t_alias *res; - t_alias *current; - - res = ft_calloc(1, sizeof(t_alias)); - if (!res) - return (alias); - res->name = name; - res->value = value; - if (!alias) - return (res); - current = alias; - while (current->next) - current = current->next; - current->next = res; - return (alias); -} - -void free_alias(t_alias *alias) -{ - if (alias && alias->next) - free_alias(alias->next); - if (alias) - { - free(alias->name); - free(alias->value); - } - free(alias); -} - -char *get_alias(t_alias *alias, char *name) -{ - while (alias) - { - if (!ft_strcmp(alias->name, name)) - return (alias->value); - alias = alias->next; - } - return (0); -} diff --git a/srcs/main.c b/srcs/main.c index 89e7f14..906a520 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/23 19:24:40 by marde-vr ### ########.fr */ +/* Updated: 2024/03/25 12:34:39 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -90,19 +90,16 @@ int main(int argc, char **argv, char **envp) commands = (char *)1; init_minishell(&msh, argc, argv, envp); - msh->aliases = alias_add_back(msh->aliases, - ft_strdup("ls"), ft_strdup("ls --color=auto")); while (msh->env && commands) { prompt = get_prompt(msh->env); if (!prompt) - exit(STDIN_FILENO); + exit(1); commands = readline(prompt); free(prompt); add_history(commands); msh->cmds = parse_command(commands, msh->env); free(commands); - msh->cmds = handle_alias(msh); exec_commands(msh); free_cmd(msh->cmds); } diff --git a/srcs/minishell.h b/srcs/minishell.h index 6a618cf..4b16180 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/24 09:53:43 by marde-vr ### ########.fr */ +/* Updated: 2024/03/25 12:36:03 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,16 +48,8 @@ typedef struct s_env struct s_env *next; } t_env; -typedef struct s_alias -{ - char *name; - char *value; - struct s_alias *next; -} t_alias; - typedef struct s_msh { - struct s_alias *aliases; struct s_env *env; struct s_cmd *cmds; int **fds; @@ -90,13 +82,7 @@ void print_parsed_cmd(t_cmd *cmd);//debug void ft_exit(t_msh *msh, int error_code); char **env_to_char_tab(t_env *env); void handle_minishellrc(t_msh *msh); -t_cmd *handle_alias(t_msh *msh); int cd(t_cmd *args); -int alias(t_msh *msh); -void free_alias(t_alias *alias); -char *get_alias(t_alias *alias, char *var_name); -t_alias *alias_add_back(t_alias *alias, char *name, char *value); -int unalias(t_msh *msh); int ft_export(t_msh *msh); void free_msh(t_msh *msh); char **split_paths_from_env(t_env *env); diff --git a/srcs/pipe.c b/srcs/pipe.c index c37119d..ea1c3c8 100644 --- a/srcs/pipe.c +++ b/srcs/pipe.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */ -/* Updated: 2024/03/24 09:09:25 by marde-vr ### ########.fr */ +/* Updated: 2024/03/25 12:36:14 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,8 +32,6 @@ void execute_command(t_msh *msh, char **cmd_args, int i) char **env; if (msh->cmds->token && (!ft_strcmp(msh->cmds->token, "cd") - || !ft_strcmp(msh->cmds->token, "alias") - || !ft_strcmp(msh->cmds->token, "unalias") || !ft_strcmp(msh->cmds->token, "exit") || exec_builtin(msh))) { while (i >= 0) diff --git a/srcs/unalias.c b/srcs/unalias.c deleted file mode 100644 index 653a2fe..0000000 --- a/srcs/unalias.c +++ /dev/null @@ -1,57 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* unalias.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: marde-vr +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2024/02/21 13:11:45 by marde-vr #+# #+# */ -/* Updated: 2024/02/21 17:44:30 by marde-vr ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "minishell.h" - -int remove_alias(t_msh *msh) -{ - t_alias *alias; - - alias = msh->aliases; - while (alias) - { - if (alias->next && msh->cmds->next - && !ft_strcmp(alias->next->name, msh->cmds->next->token)) - { - if (alias->next->next) - alias->next = alias->next->next; - else - alias->next = 0; - alias->next = 0; - free_alias(alias); - return (1); - } - if (alias->next) - alias = alias->next; - else - alias = 0; - } - return (0); -} - -int unalias(t_msh *msh) -{ - if (msh->cmds->next && !ft_strcmp(msh->cmds->next->token, "-a")) - { - free_alias(msh->aliases); - msh->aliases = 0; - return (0); - } - if (remove_alias(msh)) - return (1); - if (msh->cmds->next && msh->cmds->next->type == ARG) - ft_printf("minishell: unalias: %s: not found\n", - msh->cmds->next->token); - else - ft_printf("unalias: usage: unalias name\n"); - return (1); -} diff --git a/srcs/utils.c b/srcs/utils.c index 0b93cbc..dda5234 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/24 08:56:03 by marde-vr ### ########.fr */ +/* Updated: 2024/03/25 12:33:44 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,6 @@ void free_msh(t_msh *msh) if (msh) { free_env(msh->env); - free_alias(msh->aliases); free(msh->pids); free(msh->fds); free_cmd(msh->cmds);