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> +#+ +:+ +#+ # # By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# # # 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\ exit.c\
echo.c\ echo.c\
alias.c\ alias.c\
unalias.c\
pwd.c\ pwd.c\
parsing.c\ parsing.c\
debug.c\ debug.c\

View File

@ -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/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); 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); ft_printf("alias %s=%s\n", cur_alias->name, cur_alias->value);
*aliases = 0; cur_alias = cur_alias->next;
return (0);
} }
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) int alias(t_cmd *args, t_alias **aliases)
{ {
char *name; char *name;
char *value; char *value;
t_alias *cur_alias;
cur_alias = *aliases;
if (!args->next || args->next->type != ARG) if (!args->next || args->next->type != ARG)
{ print_aliases(aliases);
while (cur_alias)
{
ft_printf("alias %s=%s\n", cur_alias->name, cur_alias->value);
cur_alias = cur_alias->next;
}
}
else else
{ {
name = get_alias_name(args->next); name = get_alias_name(args->next);
@ -99,7 +68,7 @@ int alias(t_cmd *args, t_alias **aliases)
} }
else 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); free(name);
} }
} }

View File

@ -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/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; 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");

View File

@ -3,10 +3,10 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* debug.c :+: :+: :+: */ /* debug.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/18 15:46:50 by tomoron #+# #+# */ /* 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"); printf("RED_O");
else if (cmd->type == RED_O_APP) else if (cmd->type == RED_O_APP)
printf("RED_O_APP"); printf("RED_O_APP");
else if(cmd->type == RED_I) else if (cmd->type == RED_I)
printf("RED_I"); printf("RED_I");
else if(cmd->type == HERE_DOC) else if (cmd->type == HERE_DOC)
printf("HERE_DOC"); printf("HERE_DOC");
printf("] "); printf("] ");
cmd = cmd->next; cmd = cmd->next;

View File

@ -6,7 +6,7 @@
/* 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/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; int args_count;
char **cmd_args; char **cmd_args;
int i; int i;
pid_t pid;
if (!parsed_cmd || exec_builtin(parsed_cmd, env, aliases)) if (!parsed_cmd || exec_builtin(parsed_cmd, env, aliases))
return ; return ;
@ -159,7 +160,6 @@ void exec_command(t_cmd *parsed_cmd, t_env **env, t_alias **aliases)
cur_cmd = cur_cmd->next; cur_cmd = cur_cmd->next;
i++; i++;
} }
pid_t pid;
pid = fork(); pid = fork();
if (pid == -1) if (pid == -1)
{ {

View File

@ -3,33 +3,33 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* handle_alias.c :+: :+: :+: */ /* handle_alias.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.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/18 13:35:11 by tomoron ### ########.fr */ /* Updated: 2024/02/21 13:09:40 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_cmd *cmd, t_env *env, t_alias *alias)
{ {
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 (!cmd)
return(0); return (0);
if(cmd->type == ARG) if (cmd->type == ARG)
alias_command = ft_get_alias(alias,cmd->token); alias_command = get_alias(alias, cmd->token);
if(!alias_command) if (!alias_command)
return(cmd); return (cmd);
res = parse_command(alias_command, env); res = parse_command(alias_command, env);
tmp = res; tmp = res;
while(tmp->next) while (tmp->next)
tmp = tmp->next; tmp = tmp->next;
tmp->next = cmd->next; tmp->next = cmd->next;
free(cmd); free(cmd);
return(res); return (res);
} }

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */ /* 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); free(alias);
} }
char *ft_get_alias(t_alias *alias, char *name) char *get_alias(t_alias *alias, char *name)
{ {
while (alias) while (alias)
{ {

View File

@ -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/19 22:51:05 by tomoron ### ########.fr */ /* Updated: 2024/02/21 12:56:33 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -28,8 +28,8 @@ char *get_prompt(t_env *env)
res = ft_strjoin_free(res, ft_get_color(80, 80, 255), 3); res = ft_strjoin_free(res, ft_get_color(80, 80, 255), 3);
res = ft_strjoin_free(res, "\033[1m\002", 1); res = ft_strjoin_free(res, "\033[1m\002", 1);
cwd = getcwd(cwd_buffer, 99); 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")))) "HOME"), ft_strlen(ft_get_env(env, "HOME"))))
{ {
cwd += ft_strlen(getenv("HOME")) - 1; cwd += ft_strlen(getenv("HOME")) - 1;
cwd[0] = '~'; cwd[0] = '~';
@ -81,7 +81,8 @@ int main(int argc, char **argv, char **envp)
(void)argv; (void)argv;
env = get_env(envp); env = get_env(envp);
aliases = 0; 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); handle_minishellrc(&env, &aliases);
while (env && command) while (env && command)
{ {

View File

@ -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/18 18:32:24 by marde-vr ### ########.fr */ /* Updated: 2024/02/21 13:09:11 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -47,9 +47,9 @@ typedef struct s_env
typedef struct s_alias typedef struct s_alias
{ {
char *name; char *name;
char *value; char *value;
struct s_alias *next; struct s_alias *next;
} t_alias; } t_alias;
extern int g_return_code; extern int g_return_code;
@ -77,7 +77,7 @@ t_cmd *handle_alias(t_cmd *cmd, t_env *env, t_alias *alias);
int cd(t_cmd *args); int cd(t_cmd *args);
int alias(t_cmd *args, t_alias **aliases); int alias(t_cmd *args, t_alias **aliases);
void free_alias(t_alias *alias); 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); t_alias *alias_add_back(t_alias *alias, char *name, char *value);
int unalias(t_cmd *args, t_alias **aliases); int unalias(t_cmd *args, t_alias **aliases);
int ft_export(t_cmd *cmd, t_env **env); int ft_export(t_cmd *cmd, t_env **env);

View File

@ -6,19 +6,36 @@
/* 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/18 18:01:11 by marde-vr ### ########.fr */ /* Updated: 2024/02/21 12:54:01 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void exec_rc_file(t_env **env, t_alias **aliases, int fd)
{
char *line;
t_cmd *parsed_cmd;
line = get_next_line(fd);
while (line)
{
if (line[0] != '#')
{
parsed_cmd = parse_command(line, *env);
exec_command(parsed_cmd, env, aliases);
free_cmd(parsed_cmd);
}
free(line);
line = get_next_line(fd);
}
}
void handle_minishellrc(t_env **env, t_alias **aliases) void handle_minishellrc(t_env **env, t_alias **aliases)
{ {
char *home; char *home;
char *rc_path; char *rc_path;
int fd; int fd;
char *line;
t_cmd *parsed_cmd;
home = ft_get_env(*env, "HOME"); home = ft_get_env(*env, "HOME");
rc_path = ft_strjoin(home, "/.minishellrc"); rc_path = ft_strjoin(home, "/.minishellrc");
@ -31,18 +48,7 @@ void handle_minishellrc(t_env **env, t_alias **aliases)
perror("open"); perror("open");
return ; return ;
} }
line = get_next_line(fd); exec_rc_file(env, aliases, fd);
while (line)
{
if (line[0] != '#')
{
parsed_cmd = parse_command(line, *env);
exec_command(parsed_cmd, env, aliases);
free_cmd(parsed_cmd);
}
free(line);
line = get_next_line(fd);
}
close(fd); close(fd);
} }
free(rc_path); free(rc_path);

View File

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

View File

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