migrated to msh struct and normed
This commit is contained in:
25
srcs/alias.c
25
srcs/alias.c
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/16 23:16:07 by marde-vr #+# #+# */
|
/* Created: 2024/02/16 23:16:07 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/02/21 13:12:07 by marde-vr ### ########.fr */
|
/* Updated: 2024/02/21 17:39:40 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -39,11 +39,11 @@ char *get_alias_value(t_cmd *arg)
|
|||||||
return (res);
|
return (res);
|
||||||
}
|
}
|
||||||
|
|
||||||
void print_aliases(t_alias **aliases)
|
void print_aliases(t_alias *aliases)
|
||||||
{
|
{
|
||||||
t_alias *cur_alias;
|
t_alias *cur_alias;
|
||||||
|
|
||||||
cur_alias = *aliases;
|
cur_alias = aliases;
|
||||||
while (cur_alias)
|
while (cur_alias)
|
||||||
{
|
{
|
||||||
ft_printf("alias %s=%s\n", cur_alias->name, cur_alias->value);
|
ft_printf("alias %s=%s\n", cur_alias->name, cur_alias->value);
|
||||||
@ -51,24 +51,27 @@ void print_aliases(t_alias **aliases)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int alias(t_cmd *args, t_alias **aliases)
|
int alias(t_msh *msh)
|
||||||
{
|
{
|
||||||
char *name;
|
char *name;
|
||||||
char *value;
|
char *value;
|
||||||
|
|
||||||
if (!args->next || args->next->type != ARG)
|
if (!msh->cmds->next || msh->cmds->next->type != ARG)
|
||||||
print_aliases(aliases);
|
print_aliases(msh->aliases);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
name = get_alias_name(args->next);
|
name = get_alias_name(msh->cmds->next);
|
||||||
if (ft_strchr(args->next->token, '='))
|
if (ft_strchr(msh->cmds->next->token, '='))
|
||||||
{
|
{
|
||||||
value = get_alias_value(args->next);
|
value = get_alias_value(msh->cmds->next);
|
||||||
*aliases = alias_add_back(*aliases, name, value);
|
msh->aliases = alias_add_back(msh->aliases, name, value);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ft_printf("alias %s=%s\n", name, get_alias(*aliases, name));
|
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);
|
free(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/16 21:02:54 by marde-vr #+# #+# */
|
/* Created: 2024/02/16 21:02:54 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/02/20 21:04:53 by marde-vr ### ########.fr */
|
/* Updated: 2024/02/21 17:30:36 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -16,7 +16,6 @@ int cd(t_cmd *args)
|
|||||||
{
|
{
|
||||||
char *new_wd;
|
char *new_wd;
|
||||||
|
|
||||||
ft_printf_fd(2, "%s\n", args->next->next->token);
|
|
||||||
if (args->next && args->next->next && args->next->next->type == ARG)
|
if (args->next && args->next->next && args->next->next->type == ARG)
|
||||||
{
|
{
|
||||||
ft_printf_fd(2, "minishell: cd: too many arguments\n");
|
ft_printf_fd(2, "minishell: cd: too many arguments\n");
|
||||||
|
89
srcs/exec.c
89
srcs/exec.c
@ -6,32 +6,32 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */
|
/* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/21 15:54:56 by marde-vr ### ########.fr */
|
/* Updated: 2024/02/21 17:46:47 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
int exec_builtin(t_cmd *parsed_cmd, t_env **env, t_alias **aliases)
|
int exec_builtin(t_msh *msh)
|
||||||
{
|
{
|
||||||
if (!ft_strcmp(parsed_cmd->token, "echo"))
|
if (!ft_strcmp(msh->cmds->token, "echo"))
|
||||||
g_return_code = echo(parsed_cmd->next);
|
g_return_code = echo(msh->cmds->next);
|
||||||
else if (!ft_strcmp(parsed_cmd->token, "ret"))
|
else if (!ft_strcmp(msh->cmds->token, "ret"))
|
||||||
g_return_code = ft_atoi(parsed_cmd->next->token);
|
g_return_code = ft_atoi(msh->cmds->next->token);
|
||||||
else if (!ft_strcmp(parsed_cmd->token, "env"))
|
else if (!ft_strcmp(msh->cmds->token, "env"))
|
||||||
g_return_code = print_env(*env);
|
g_return_code = print_env(msh->env);
|
||||||
else if (!ft_strcmp(parsed_cmd->token, "exit"))
|
else if (!ft_strcmp(msh->cmds->token, "exit"))
|
||||||
exit_bt(parsed_cmd, *env, *aliases);
|
exit_bt(msh);
|
||||||
else if (!ft_strcmp(parsed_cmd->token, "pwd"))
|
else if (!ft_strcmp(msh->cmds->token, "pwd"))
|
||||||
g_return_code = pwd();
|
g_return_code = pwd();
|
||||||
else if (!ft_strcmp(parsed_cmd->token, "cd"))
|
else if (!ft_strcmp(msh->cmds->token, "cd"))
|
||||||
g_return_code = cd(parsed_cmd);
|
g_return_code = cd(msh->cmds);
|
||||||
else if (!ft_strcmp(parsed_cmd->token, "export"))
|
else if (!ft_strcmp(msh->cmds->token, "export"))
|
||||||
g_return_code = ft_export(parsed_cmd, env);
|
g_return_code = ft_export(msh);
|
||||||
else if (!ft_strcmp(parsed_cmd->token, "alias"))
|
else if (!ft_strcmp(msh->cmds->token, "alias"))
|
||||||
g_return_code = alias(parsed_cmd, aliases);
|
g_return_code = alias(msh);
|
||||||
else if (!ft_strcmp(parsed_cmd->token, "unalias"))
|
else if (!ft_strcmp(msh->cmds->token, "unalias"))
|
||||||
g_return_code = unalias(parsed_cmd, aliases);
|
g_return_code = unalias(msh);
|
||||||
else
|
else
|
||||||
return (STDIN_FILENO);
|
return (STDIN_FILENO);
|
||||||
return (STDOUT_FILENO);
|
return (STDOUT_FILENO);
|
||||||
@ -57,13 +57,13 @@ void ft_exit(t_msh *msh, int exit_code)
|
|||||||
exit(exit_code);
|
exit(exit_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_args_count(t_cmd *parsed_cmd)
|
int get_args_count(t_cmd *cmds)
|
||||||
{
|
{
|
||||||
int count;
|
int count;
|
||||||
t_cmd *cur_cmd;
|
t_cmd *cur_cmd;
|
||||||
|
|
||||||
count = 0;
|
count = 0;
|
||||||
cur_cmd = parsed_cmd;
|
cur_cmd = cmds;
|
||||||
while (cur_cmd->next != 0 && cur_cmd->type == ARG)
|
while (cur_cmd->next != 0 && cur_cmd->type == ARG)
|
||||||
{
|
{
|
||||||
cur_cmd = cur_cmd->next;
|
cur_cmd = cur_cmd->next;
|
||||||
@ -111,15 +111,15 @@ void find_cmd_path(t_msh *msh, char **paths, int *found)
|
|||||||
tmp = ft_strjoin(path, "/");
|
tmp = ft_strjoin(path, "/");
|
||||||
if (!tmp)
|
if (!tmp)
|
||||||
ft_exit(msh, 1);
|
ft_exit(msh, 1);
|
||||||
path = ft_strjoin(tmp, cmd->token);
|
path = ft_strjoin(tmp, msh->cmds->token);
|
||||||
if (!path)
|
if (!path)
|
||||||
ft_exit(cmd, env, 1);
|
ft_exit(msh, 1);
|
||||||
free(tmp);
|
free(tmp);
|
||||||
if (access(path, X_OK) != -1)
|
if (access(path, X_OK) != -1)
|
||||||
{
|
{
|
||||||
*found = 1;
|
*found = 1;
|
||||||
free(cmd->token);
|
free(msh->cmds->token);
|
||||||
cmd->token = path;
|
msh->cmds->token = path;
|
||||||
break ;
|
break ;
|
||||||
}
|
}
|
||||||
free(path);
|
free(path);
|
||||||
@ -127,30 +127,31 @@ void find_cmd_path(t_msh *msh, char **paths, int *found)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_cmd_path(t_cmd *cmd, t_env *env)
|
void get_cmd_path(t_msh *msh)
|
||||||
{
|
{
|
||||||
char **paths;
|
char **paths;
|
||||||
int found;
|
int found;
|
||||||
|
|
||||||
found = 0;
|
found = 0;
|
||||||
if (ft_strchr(cmd->token, '/') && access(cmd->token, X_OK) != -1)
|
if (ft_strchr(msh->cmds->token, '/')
|
||||||
|
&& access(msh->cmds->token, X_OK) != -1)
|
||||||
found = 1;
|
found = 1;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
paths = split_paths_from_env(env);
|
paths = split_paths_from_env(msh->env);
|
||||||
if (!paths)
|
if (!paths)
|
||||||
ft_exit(cmd, env, 1);
|
ft_exit(msh, 1);
|
||||||
find_cmd_path(cmd, env, paths, &found);
|
find_cmd_path(msh, paths, &found);
|
||||||
}
|
}
|
||||||
if (!found)
|
if (!found)
|
||||||
{
|
{
|
||||||
ft_printf_fd(2, "%s: command not found\n", cmd->token);
|
ft_printf_fd(2, "%s: command not found\n", msh->cmds->token);
|
||||||
free(cmd->token);
|
free(msh->cmds->token);
|
||||||
cmd->token = 0;
|
msh->cmds->token = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void exec_command(t_cmd *parsed_cmd, t_env **env, t_alias **aliases)
|
void exec_command(t_msh *msh)
|
||||||
{
|
{
|
||||||
t_cmd *cur_cmd;
|
t_cmd *cur_cmd;
|
||||||
int args_count;
|
int args_count;
|
||||||
@ -158,15 +159,15 @@ void exec_command(t_cmd *parsed_cmd, t_env **env, t_alias **aliases)
|
|||||||
int i;
|
int i;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
|
|
||||||
if (!parsed_cmd || exec_builtin(parsed_cmd, env, aliases))
|
if (!msh->cmds || exec_builtin(msh))
|
||||||
return ;
|
return ;
|
||||||
cur_cmd = parsed_cmd;
|
cur_cmd = msh->cmds;
|
||||||
args_count = get_args_count(parsed_cmd);
|
args_count = get_args_count(msh->cmds);
|
||||||
cmd_args = ft_calloc(args_count + 1, sizeof(char *));
|
cmd_args = ft_calloc(args_count + 1, sizeof(char *));
|
||||||
if (!cmd_args)
|
if (!cmd_args)
|
||||||
ft_exit(parsed_cmd, *env, 1);
|
ft_exit(msh, 1);
|
||||||
i = 0;
|
i = 0;
|
||||||
get_cmd_path(parsed_cmd, *env);
|
get_cmd_path(msh);
|
||||||
while (i < args_count)
|
while (i < args_count)
|
||||||
{
|
{
|
||||||
cmd_args[i] = cur_cmd->token;
|
cmd_args[i] = cur_cmd->token;
|
||||||
@ -177,19 +178,19 @@ void exec_command(t_cmd *parsed_cmd, t_env **env, t_alias **aliases)
|
|||||||
if (pid == -1)
|
if (pid == -1)
|
||||||
{
|
{
|
||||||
perror("fork");
|
perror("fork");
|
||||||
ft_exit(parsed_cmd, *env, 1);
|
ft_exit(msh, 1);
|
||||||
}
|
}
|
||||||
if (pid == 0)
|
if (pid == 0)
|
||||||
{
|
{
|
||||||
if (parsed_cmd->token)
|
if (msh->cmds->token)
|
||||||
execve(parsed_cmd->token, cmd_args, env_to_char_tab(*env));
|
execve(msh->cmds->token, cmd_args, env_to_char_tab(msh->env));
|
||||||
ft_exit(parsed_cmd, *env, 1);
|
ft_exit(msh, 1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
rl_redisplay();
|
rl_redisplay();
|
||||||
if (waitpid(pid, 0, 0) < 0)
|
if (waitpid(pid, 0, 0) < 0)
|
||||||
ft_exit(parsed_cmd, *env, 1);
|
ft_exit(msh, 1);
|
||||||
}
|
}
|
||||||
//if (cur_cmd->type == PIPE)
|
//if (cur_cmd->type == PIPE)
|
||||||
// exec_command(cur_cmd->next, env);
|
// exec_command(cur_cmd->next, env);
|
||||||
|
27
srcs/exit.c
27
srcs/exit.c
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */
|
/* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/17 04:11:51 by tomoron ### ########.fr */
|
/* Updated: 2024/02/21 17:43:52 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -19,30 +19,31 @@ void print_numeric_arg_err(char *arg)
|
|||||||
ft_putstr_fd(": numeric argument required\n", 2);
|
ft_putstr_fd(": numeric argument required\n", 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
void exit_bt(t_cmd *args, t_env *env, t_alias *aliases)
|
void exit_bt(t_msh *msh)
|
||||||
{
|
{
|
||||||
t_cmd *start;
|
t_cmd *start;
|
||||||
int exit_code;
|
int exit_code;
|
||||||
|
|
||||||
start = args;
|
start = msh->cmds;
|
||||||
args = args->next;
|
msh->cmds = msh->cmds->next;
|
||||||
ft_printf("exit\n");
|
ft_printf("exit\n");
|
||||||
if (args && args->next && args->next->type == ARG
|
if (msh->cmds && msh->cmds->next && msh->cmds->next->type == ARG
|
||||||
&& ft_strisnbr(args->token))
|
&& ft_strisnbr(msh->cmds->token))
|
||||||
ft_putstr_fd("minishell: exit: too many arguments\n", 2);
|
ft_putstr_fd("minishell: exit: too many arguments\n", 2);
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (args && args->type == ARG && !ft_strisnbr(args->token))
|
if (msh->cmds && msh->cmds->type == ARG
|
||||||
print_numeric_arg_err(args->token);
|
&& !ft_strisnbr(msh->cmds->token))
|
||||||
if (args && args->type == ARG)
|
print_numeric_arg_err(msh->cmds->token);
|
||||||
exit_code = (unsigned char)ft_atoi(args->token);
|
if (msh->cmds && msh->cmds->type == ARG)
|
||||||
else if (args && args->type == ARG && !ft_strisnbr(args->token))
|
exit_code = (unsigned char)ft_atoi(msh->cmds->token);
|
||||||
|
else if (msh->cmds && msh->cmds->type == ARG
|
||||||
|
&& !ft_strisnbr(msh->cmds->token))
|
||||||
exit_code = 2;
|
exit_code = 2;
|
||||||
else
|
else
|
||||||
exit_code = g_return_code;
|
exit_code = g_return_code;
|
||||||
free_cmd(start);
|
free_cmd(start);
|
||||||
free_alias(aliases);
|
free_msh(msh);
|
||||||
free_env(env);
|
|
||||||
exit(exit_code);
|
exit(exit_code);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,15 +6,17 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/18 18:29:20 by marde-vr #+# #+# */
|
/* Created: 2024/02/18 18:29:20 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/02/18 18:39:39 by marde-vr ### ########.fr */
|
/* Updated: 2024/02/21 17:44:43 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
int ft_export(t_cmd *cmd, t_env **env)
|
int ft_export(t_msh *msh)
|
||||||
{
|
{
|
||||||
|
t_cmd *cmd;
|
||||||
|
|
||||||
|
cmd = msh->cmds;
|
||||||
(void)cmd;
|
(void)cmd;
|
||||||
(void)env;
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
@ -6,30 +6,30 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/17 02:54:36 by tomoron #+# #+# */
|
/* Created: 2024/02/17 02:54:36 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/21 13:09:40 by marde-vr ### ########.fr */
|
/* Updated: 2024/02/21 17:21:27 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
t_cmd *handle_alias(t_cmd *cmd, t_env *env, t_alias *alias)
|
t_cmd *handle_alias(t_msh *msh)
|
||||||
{
|
{
|
||||||
t_cmd *res;
|
t_cmd *res;
|
||||||
t_cmd *tmp;
|
t_cmd *tmp;
|
||||||
char *alias_command;
|
char *alias_command;
|
||||||
|
|
||||||
alias_command = 0;
|
alias_command = 0;
|
||||||
if (!cmd)
|
if (!msh->cmds)
|
||||||
return (0);
|
return (0);
|
||||||
if (cmd->type == ARG)
|
if (msh->cmds->type == ARG)
|
||||||
alias_command = get_alias(alias, cmd->token);
|
alias_command = get_alias(msh->aliases, msh->cmds->token);
|
||||||
if (!alias_command)
|
if (!alias_command)
|
||||||
return (cmd);
|
return (msh->cmds);
|
||||||
res = parse_command(alias_command, env);
|
res = parse_command(alias_command, msh->env);
|
||||||
tmp = res;
|
tmp = res;
|
||||||
while (tmp->next)
|
while (tmp->next)
|
||||||
tmp = tmp->next;
|
tmp = tmp->next;
|
||||||
tmp->next = cmd->next;
|
tmp->next = msh->cmds->next;
|
||||||
free(cmd);
|
free(msh->cmds);
|
||||||
return (res);
|
return (res);
|
||||||
}
|
}
|
||||||
|
26
srcs/main.c
26
srcs/main.c
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */
|
/* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/21 15:53:24 by marde-vr ### ########.fr */
|
/* Updated: 2024/02/21 17:45:16 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -68,27 +68,27 @@ t_env *get_env(char **envp)
|
|||||||
return (env);
|
return (env);
|
||||||
}
|
}
|
||||||
|
|
||||||
int init_minishell(t_msh **msh, int argc, char **envp)
|
int init_minishell(t_msh **msh, int argc, char **argv, char **envp)
|
||||||
{
|
{
|
||||||
*msh = ft_calloc(1, sizeof(t_msh));
|
*msh = ft_calloc(1, sizeof(t_msh));
|
||||||
if (!*msh)
|
if (!*msh)
|
||||||
//
|
ft_exit(*msh, 1);
|
||||||
(void)argc;
|
(void)argc;
|
||||||
|
(void)argv;
|
||||||
(*msh)->env = get_env(envp);
|
(*msh)->env = get_env(envp);
|
||||||
(*msh)->aliases = 0;
|
(*msh)->aliases = 0;
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **envp)
|
int main(int argc, char **argv, char **envp)
|
||||||
{
|
{
|
||||||
char *command;
|
char *command;
|
||||||
t_cmd *parsed_cmd;
|
|
||||||
char *prompt;
|
char *prompt;
|
||||||
t_msh *msh;
|
t_msh *msh;
|
||||||
|
|
||||||
command = (char *)1;
|
command = (char *)1;
|
||||||
init_minishell(&msh, argc, envp);
|
init_minishell(&msh, argc, argv, envp);
|
||||||
handle_minishellrc(&env, &aliases);
|
handle_minishellrc(msh);
|
||||||
while (msh->env && command)
|
while (msh->env && command)
|
||||||
{
|
{
|
||||||
prompt = get_prompt(msh->env);
|
prompt = get_prompt(msh->env);
|
||||||
@ -97,13 +97,13 @@ int main(int argc, char **envp)
|
|||||||
command = readline(prompt);
|
command = readline(prompt);
|
||||||
free(prompt);
|
free(prompt);
|
||||||
add_history(command);
|
add_history(command);
|
||||||
msh->cmd = parse_command(command, msh->env);
|
msh->cmds = parse_command(command, msh->env);
|
||||||
//print_parsed_cmd(parsed_cmd);//debug
|
//print_parsed_cmd(parsed_cmd);//debug
|
||||||
msh->cmd = handle_alias(msh->cmd, msh->env, msh->aliases);
|
msh->cmds = handle_alias(msh);
|
||||||
free(command);
|
free(command);
|
||||||
//print_parsed_cmd(parsed_cmd);//debug
|
//print_parsed_cmd(parsed_cmd);//debug
|
||||||
exec_command(msh->cmd, &env, &aliases);
|
exec_command(msh);
|
||||||
free_cmd(parsed_cmd);
|
free_cmd(msh->cmds);
|
||||||
}
|
}
|
||||||
rl_clear_history();
|
rl_clear_history();
|
||||||
free(msh);
|
free(msh);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
|
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
|
||||||
/* Updated: 2024/02/21 15:46:50 by marde-vr ### ########.fr */
|
/* Updated: 2024/02/21 17:43:17 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -63,9 +63,9 @@ extern int g_return_code;
|
|||||||
|
|
||||||
t_cmd *cmd_add_back(t_cmd *res, char *token, t_token_type type);
|
t_cmd *cmd_add_back(t_cmd *res, char *token, t_token_type type);
|
||||||
void free_cmd(t_cmd *cmd);
|
void free_cmd(t_cmd *cmd);
|
||||||
void exec_command(t_cmd *cmd, t_env **env, t_alias **alias);
|
void exec_command(t_msh *msh);
|
||||||
int echo(t_cmd *args);
|
int echo(t_cmd *args);
|
||||||
void exit_bt(t_cmd *args, t_env *env, t_alias *aliases);
|
void exit_bt(t_msh *msh);
|
||||||
t_env *env_add_back(t_env *env, char *name, char *value);
|
t_env *env_add_back(t_env *env, char *name, char *value);
|
||||||
void free_env(t_env *env);
|
void free_env(t_env *env);
|
||||||
int print_env(t_env *env);
|
int print_env(t_env *env);
|
||||||
@ -79,14 +79,15 @@ int is_cmd_char(char c);
|
|||||||
void print_parsed_cmd(t_cmd *cmd);//debug
|
void print_parsed_cmd(t_cmd *cmd);//debug
|
||||||
void ft_exit(t_msh *msh, int error_code);
|
void ft_exit(t_msh *msh, int error_code);
|
||||||
char **env_to_char_tab(t_env *env);
|
char **env_to_char_tab(t_env *env);
|
||||||
void handle_minishellrc(t_env **env, t_alias **aliases);
|
void handle_minishellrc(t_msh *msh);
|
||||||
t_cmd *handle_alias(t_cmd *cmd, t_env *env, t_alias *alias);
|
t_cmd *handle_alias(t_msh *msh);
|
||||||
int cd(t_cmd *args);
|
int cd(t_cmd *args);
|
||||||
int alias(t_cmd *args, t_alias **aliases);
|
int alias(t_msh *msh);
|
||||||
void free_alias(t_alias *alias);
|
void free_alias(t_alias *alias);
|
||||||
char *get_alias(t_alias *alias, char *var_name);
|
char *get_alias(t_alias *alias, char *var_name);
|
||||||
t_alias *alias_add_back(t_alias *alias, char *name, char *value);
|
t_alias *alias_add_back(t_alias *alias, char *name, char *value);
|
||||||
int unalias(t_cmd *args, t_alias **aliases);
|
int unalias(t_msh *msh);
|
||||||
int ft_export(t_cmd *cmd, t_env **env);
|
int ft_export(t_msh *msh);
|
||||||
|
void free_msh(t_msh *msh);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -6,49 +6,48 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/16 17:40:16 by marde-vr #+# #+# */
|
/* Created: 2024/02/16 17:40:16 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/02/21 12:54:01 by marde-vr ### ########.fr */
|
/* Updated: 2024/02/21 17:34:26 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
void exec_rc_file(t_env **env, t_alias **aliases, int fd)
|
void exec_rc_file(t_msh *msh, int fd)
|
||||||
{
|
{
|
||||||
char *line;
|
char *line;
|
||||||
t_cmd *parsed_cmd;
|
|
||||||
|
|
||||||
line = get_next_line(fd);
|
line = get_next_line(fd);
|
||||||
while (line)
|
while (line)
|
||||||
{
|
{
|
||||||
if (line[0] != '#')
|
if (line[0] != '#')
|
||||||
{
|
{
|
||||||
parsed_cmd = parse_command(line, *env);
|
msh->cmds = parse_command(line, msh->env);
|
||||||
exec_command(parsed_cmd, env, aliases);
|
exec_command(msh);
|
||||||
free_cmd(parsed_cmd);
|
free_cmd(msh->cmds);
|
||||||
}
|
}
|
||||||
free(line);
|
free(line);
|
||||||
line = get_next_line(fd);
|
line = get_next_line(fd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void handle_minishellrc(t_env **env, t_alias **aliases)
|
void handle_minishellrc(t_msh *msh)
|
||||||
{
|
{
|
||||||
char *home;
|
char *home;
|
||||||
char *rc_path;
|
char *rc_path;
|
||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
home = ft_get_env(*env, "HOME");
|
home = ft_get_env(msh->env, "HOME");
|
||||||
rc_path = ft_strjoin(home, "/.minishellrc");
|
rc_path = ft_strjoin(home, "/.minishellrc");
|
||||||
if (access(rc_path, R_OK) != -1)
|
if (access(rc_path, R_OK) != -1)
|
||||||
{
|
{
|
||||||
fd = open(rc_path, O_RDONLY);
|
fd = open(rc_path, O_RDONLY);
|
||||||
if (fd == -1)
|
if (fd == -1)
|
||||||
{
|
{
|
||||||
free(env);
|
free(msh->env);
|
||||||
perror("open");
|
perror("open");
|
||||||
return ;
|
return ;
|
||||||
}
|
}
|
||||||
exec_rc_file(env, aliases, fd);
|
exec_rc_file(msh, fd);
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
free(rc_path);
|
free(rc_path);
|
||||||
|
@ -6,21 +6,21 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/21 13:11:45 by marde-vr #+# #+# */
|
/* Created: 2024/02/21 13:11:45 by marde-vr #+# #+# */
|
||||||
/* Updated: 2024/02/21 13:11:59 by marde-vr ### ########.fr */
|
/* Updated: 2024/02/21 17:44:30 by marde-vr ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
int remove_alias(t_cmd *args, t_alias **aliases)
|
int remove_alias(t_msh *msh)
|
||||||
{
|
{
|
||||||
t_alias *alias;
|
t_alias *alias;
|
||||||
|
|
||||||
alias = *aliases;
|
alias = msh->aliases;
|
||||||
while (alias)
|
while (alias)
|
||||||
{
|
{
|
||||||
if (alias->next && args->next
|
if (alias->next && msh->cmds->next
|
||||||
&& !ft_strcmp(alias->next->name, args->next->token))
|
&& !ft_strcmp(alias->next->name, msh->cmds->next->token))
|
||||||
{
|
{
|
||||||
if (alias->next->next)
|
if (alias->next->next)
|
||||||
alias->next = alias->next->next;
|
alias->next = alias->next->next;
|
||||||
@ -38,18 +38,19 @@ int remove_alias(t_cmd *args, t_alias **aliases)
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int unalias(t_cmd *args, t_alias **aliases)
|
int unalias(t_msh *msh)
|
||||||
{
|
{
|
||||||
if (args->next && !ft_strcmp(args->next->token, "-a"))
|
if (msh->cmds->next && !ft_strcmp(msh->cmds->next->token, "-a"))
|
||||||
{
|
{
|
||||||
free_alias(*aliases);
|
free_alias(msh->aliases);
|
||||||
*aliases = 0;
|
msh->aliases = 0;
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
if (remove_alias(args, aliases))
|
if (remove_alias(msh))
|
||||||
return (1);
|
return (1);
|
||||||
if (args->next && args->next->type == ARG)
|
if (msh->cmds->next && msh->cmds->next->type == ARG)
|
||||||
ft_printf("minishell: unalias: %s: not found\n", args->next->token);
|
ft_printf("minishell: unalias: %s: not found\n",
|
||||||
|
msh->cmds->next->token);
|
||||||
else
|
else
|
||||||
ft_printf("unalias: usage: unalias name\n");
|
ft_printf("unalias: usage: unalias name\n");
|
||||||
return (1);
|
return (1);
|
||||||
|
Reference in New Issue
Block a user