fixed norme

This commit is contained in:
mdev9
2024-02-21 13:15:46 +01:00
parent a9c8551ffc
commit 3d540f0d6d
13 changed files with 130 additions and 96 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/02/19 22:56:25 by tomoron ### ########.fr #
# Updated: 2024/02/21 13:12:47 by marde-vr ### ########.fr #
# #
# **************************************************************************** #
@ -19,6 +19,7 @@ SRCS_RAW = main.c\
exit.c\
echo.c\
alias.c\
unalias.c\
pwd.c\
parsing.c\
debug.c\

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/16 23:16:07 by marde-vr #+# #+# */
/* Updated: 2024/02/18 19:05:38 by marde-vr ### ########.fr */
/* Updated: 2024/02/21 13:12:07 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -39,56 +39,25 @@ char *get_alias_value(t_cmd *arg)
return (res);
}
int unalias(t_cmd *args, t_alias **aliases)
void print_aliases(t_alias **aliases)
{
t_alias *alias;
t_alias *cur_alias;
if (args->next && !ft_strcmp(args->next->token, "-a"))
cur_alias = *aliases;
while (cur_alias)
{
free_alias(*aliases);
*aliases = 0;
return (0);
ft_printf("alias %s=%s\n", cur_alias->name, cur_alias->value);
cur_alias = cur_alias->next;
}
alias = *aliases;
while (alias)
{
if (alias->next && args->next && !ft_strcmp(alias->next->name, args->next->token))
{
if (alias->next->next)
alias->next = alias->next->next;
else
alias->next = 0;
alias->next = 0;
free_alias(alias);
return (0);
}
if (alias->next)
alias = alias->next;
else
alias = 0;
}
if (args->next && args->next->type == ARG)
ft_printf("minishell: unalias: %s: not found\n", args->next->token);
else
ft_printf("unalias: usage: unalias name\n");
return (1);
}
int alias(t_cmd *args, t_alias **aliases)
{
char *name;
char *value;
t_alias *cur_alias;
cur_alias = *aliases;
if (!args->next || args->next->type != ARG)
{
while (cur_alias)
{
ft_printf("alias %s=%s\n", cur_alias->name, cur_alias->value);
cur_alias = cur_alias->next;
}
}
print_aliases(aliases);
else
{
name = get_alias_name(args->next);
@ -99,7 +68,7 @@ int alias(t_cmd *args, t_alias **aliases)
}
else
{
ft_printf("alias %s=%s\n", name, ft_get_alias(*aliases, name));
ft_printf("alias %s=%s\n", name, get_alias(*aliases, name));
free(name);
}
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/16 21:02:54 by marde-vr #+# #+# */
/* Updated: 2024/02/16 23:59:37 by marde-vr ### ########.fr */
/* Updated: 2024/02/20 21:04:53 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,6 +16,7 @@ int cd(t_cmd *args)
{
char *new_wd;
ft_printf_fd(2, "%s\n", args->next->next->token);
if (args->next && args->next->next && args->next->next->type == ARG)
{
ft_printf_fd(2, "minishell: cd: too many arguments\n");

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* debug.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/18 15:46:50 by tomoron #+# #+# */
/* Updated: 2024/02/18 15:46:51 by tomoron ### ########.fr */
/* Updated: 2024/02/21 12:59:08 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -25,9 +25,9 @@ void print_parsed_cmd(t_cmd *cmd)
printf("RED_O");
else if (cmd->type == RED_O_APP)
printf("RED_O_APP");
else if(cmd->type == RED_I)
else if (cmd->type == RED_I)
printf("RED_I");
else if(cmd->type == HERE_DOC)
else if (cmd->type == HERE_DOC)
printf("HERE_DOC");
printf("] ");
cmd = cmd->next;

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */
/* Updated: 2024/02/18 18:32:33 by marde-vr ### ########.fr */
/* Updated: 2024/02/21 12:50:01 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -143,6 +143,7 @@ void exec_command(t_cmd *parsed_cmd, t_env **env, t_alias **aliases)
int args_count;
char **cmd_args;
int i;
pid_t pid;
if (!parsed_cmd || exec_builtin(parsed_cmd, env, aliases))
return ;
@ -159,7 +160,6 @@ void exec_command(t_cmd *parsed_cmd, t_env **env, t_alias **aliases)
cur_cmd = cur_cmd->next;
i++;
}
pid_t pid;
pid = fork();
if (pid == -1)
{

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* handle_alias.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/17 02:54:36 by tomoron #+# #+# */
/* Updated: 2024/02/18 13:35:11 by tomoron ### ########.fr */
/* Updated: 2024/02/21 13:09:40 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,16 +20,16 @@ t_cmd *handle_alias(t_cmd *cmd, t_env *env, t_alias *alias)
alias_command = 0;
if (!cmd)
return(0);
if(cmd->type == ARG)
alias_command = ft_get_alias(alias,cmd->token);
if(!alias_command)
return(cmd);
return (0);
if (cmd->type == ARG)
alias_command = get_alias(alias, cmd->token);
if (!alias_command)
return (cmd);
res = parse_command(alias_command, env);
tmp = res;
while(tmp->next)
while (tmp->next)
tmp = tmp->next;
tmp->next = cmd->next;
free(cmd);
return(res);
return (res);
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */
/* Updated: 2024/02/18 16:31:52 by marde-vr ### ########.fr */
/* Updated: 2024/02/21 13:09:00 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -43,7 +43,7 @@ void free_alias(t_alias *alias)
free(alias);
}
char *ft_get_alias(t_alias *alias, char *name)
char *get_alias(t_alias *alias, char *name)
{
while (alias)
{

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */
/* Updated: 2024/02/19 22:51:05 by tomoron ### ########.fr */
/* Updated: 2024/02/21 12:56:33 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -28,7 +28,7 @@ char *get_prompt(t_env *env)
res = ft_strjoin_free(res, ft_get_color(80, 80, 255), 3);
res = ft_strjoin_free(res, "\033[1m\002", 1);
cwd = getcwd(cwd_buffer, 99);
if(ft_get_env(env, "HOME") && !ft_strncmp(cwd_buffer, ft_get_env(env,
if (ft_get_env(env, "HOME") && !ft_strncmp(cwd_buffer, ft_get_env(env,
"HOME"), ft_strlen(ft_get_env(env, "HOME"))))
{
cwd += ft_strlen(getenv("HOME")) - 1;
@ -81,7 +81,8 @@ int main(int argc, char **argv, char **envp)
(void)argv;
env = get_env(envp);
aliases = 0;
aliases = alias_add_back(0, ft_strdup("test"), ft_strdup("echo test")); // debug
aliases = alias_add_back(0, ft_strdup("test"), ft_strdup("echo test"));
// debug
handle_minishellrc(&env, &aliases);
while (env && command)
{

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
/* Updated: 2024/02/18 18:32:24 by marde-vr ### ########.fr */
/* Updated: 2024/02/21 13:09:11 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
@ -77,7 +77,7 @@ t_cmd *handle_alias(t_cmd *cmd, t_env *env, t_alias *alias);
int cd(t_cmd *args);
int alias(t_cmd *args, t_alias **aliases);
void free_alias(t_alias *alias);
char *ft_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);
int unalias(t_cmd *args, t_alias **aliases);
int ft_export(t_cmd *cmd, t_env **env);

View File

@ -6,31 +6,17 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/16 17:40:16 by marde-vr #+# #+# */
/* Updated: 2024/02/18 18:01:11 by marde-vr ### ########.fr */
/* Updated: 2024/02/21 12:54:01 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void handle_minishellrc(t_env **env, t_alias **aliases)
void exec_rc_file(t_env **env, t_alias **aliases, int fd)
{
char *home;
char *rc_path;
int fd;
char *line;
t_cmd *parsed_cmd;
home = ft_get_env(*env, "HOME");
rc_path = ft_strjoin(home, "/.minishellrc");
if (access(rc_path, R_OK) != -1)
{
fd = open(rc_path, O_RDONLY);
if (fd == -1)
{
free(env);
perror("open");
return ;
}
line = get_next_line(fd);
while (line)
{
@ -43,6 +29,26 @@ void handle_minishellrc(t_env **env, t_alias **aliases)
free(line);
line = get_next_line(fd);
}
}
void handle_minishellrc(t_env **env, t_alias **aliases)
{
char *home;
char *rc_path;
int fd;
home = ft_get_env(*env, "HOME");
rc_path = ft_strjoin(home, "/.minishellrc");
if (access(rc_path, R_OK) != -1)
{
fd = open(rc_path, O_RDONLY);
if (fd == -1)
{
free(env);
perror("open");
return ;
}
exec_rc_file(env, aliases, fd);
close(fd);
}
free(rc_path);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */
/* Updated: 2024/02/18 12:41:34 by tomoron ### ########.fr */
/* Updated: 2024/02/21 12:57:40 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
@ -23,12 +23,12 @@ int add_home_to_str(char *res)
i = 0;
str = getenv("HOME");
while(str[i])
while (str[i])
{
res[i] = str[i];
i++;
}
return(i);
return (i);
}
char *get_token(char **cmd, int *in_quote, int *in_dquote, t_env *env)
@ -51,8 +51,8 @@ char *get_token(char **cmd, int *in_quote, int *in_dquote, t_env *env)
(*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_quote && !*in_dquote)
i += add_home_to_str(res + i);
else if (((**cmd == '\'' && *in_dquote) || (**cmd == '"' && *in_quote))
|| (**cmd != '\'' && **cmd != '"'))
res[i++] = **cmd;

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 18:31:21 by tomoron #+# #+# */
/* Updated: 2024/02/16 21:19:30 by tomoron ### ########.fr */
/* Updated: 2024/02/21 12:47:39 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
@ -16,8 +16,8 @@ int pwd(void)
char *buffer;
buffer = getcwd(NULL, 0);
if(!buffer)
return(1);
if (!buffer)
return (1);
ft_printf("%s\n", buffer);
free(buffer);
return (0);

56
srcs/unalias.c Normal file
View File

@ -0,0 +1,56 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* unalias.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/21 13:11:45 by marde-vr #+# #+# */
/* Updated: 2024/02/21 13:11:59 by marde-vr ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int remove_alias(t_cmd *args, t_alias **aliases)
{
t_alias *alias;
alias = *aliases;
while (alias)
{
if (alias->next && args->next
&& !ft_strcmp(alias->next->name, args->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_cmd *args, t_alias **aliases)
{
if (args->next && !ft_strcmp(args->next->token, "-a"))
{
free_alias(*aliases);
*aliases = 0;
return (0);
}
if (remove_alias(args, aliases))
return (1);
if (args->next && args->next->type == ARG)
ft_printf("minishell: unalias: %s: not found\n", args->next->token);
else
ft_printf("unalias: usage: unalias name\n");
return (1);
}