hééééhééééééhééhéééhéééééhéééhé

This commit is contained in:
2024-03-25 12:42:23 +01:00
parent b25a87953b
commit eb5f2bd32e
11 changed files with 13 additions and 284 deletions

View File

@ -6,7 +6,7 @@
# By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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\

View File

@ -1,79 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* alias.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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,6 +62,7 @@ void exec_command(t_msh *msh, int i, int cmd_count)
}
if (!cmd_is_builtin(msh, msh->cmds->token))
get_cmd_path(msh);
if(!g_return_code)
exec(msh, get_cmd_args(msh), i, cmd_count);
remove_command_from_msh(msh);
}
@ -86,14 +87,9 @@ 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++;
}
if(!g_return_code)
g_return_code = WEXITSTATUS(status);
i = 0;
while (i < cmd_count)

View File

@ -1,37 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_alias.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -1,55 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lst_alias.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)

View File

@ -1,57 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* unalias.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);