alias parsing

This commit is contained in:
2024-02-17 04:37:01 +01:00
parent e6fff0aec3
commit 7b5b5338a4
11 changed files with 129 additions and 29 deletions

View File

@ -6,11 +6,11 @@
# 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/17 02:24:43 by marde-vr ### ########.fr # # Updated: 2024/02/17 04:34:37 by tomoron ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
CC = clang CC = cc
SRCS_RAW = main.c\ SRCS_RAW = main.c\
lst_cmd.c\ lst_cmd.c\
cd.c\ cd.c\
@ -24,6 +24,8 @@ SRCS_RAW = main.c\
debug.c\ debug.c\
env_to_char_tab.c\ env_to_char_tab.c\
parsing_var.c\ parsing_var.c\
handle_alias.c\
lst_alias.c\
minishellrc.c minishellrc.c
OBJS_DIR = objs/ OBJS_DIR = objs/

View File

@ -6,13 +6,16 @@
/* 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/17 02:23:21 by marde-vr ### ########.fr */ /* Updated: 2024/02/17 04:22:39 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void alias(t_cmd *args) int alias(t_cmd *args, t_alias *aliases)
{ {
(void)args;
(void)aliases;
return(0);
//GLHF marijn
} }

View File

@ -6,13 +6,13 @@
/* 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/17 02:21:53 by marde-vr ### ########.fr */ /* Updated: 2024/02/17 04:18:31 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int exec_builtin(t_cmd *parsed_cmd, t_env *env) int exec_builtin(t_cmd *parsed_cmd, t_env *env, t_alias *aliases)
{ {
if (!ft_strcmp(parsed_cmd->token, "echo")) if (!ft_strcmp(parsed_cmd->token, "echo"))
@ -22,13 +22,13 @@ int exec_builtin(t_cmd *parsed_cmd, t_env *env)
else if (!ft_strcmp(parsed_cmd->token, "env")) else if (!ft_strcmp(parsed_cmd->token, "env"))
g_return_code = print_env(env); g_return_code = print_env(env);
else if (!ft_strcmp(parsed_cmd->token, "exit")) else if (!ft_strcmp(parsed_cmd->token, "exit"))
exit_bt(parsed_cmd, env); exit_bt(parsed_cmd, env, aliases);
else if (!ft_strcmp(parsed_cmd->token, "pwd")) else if (!ft_strcmp(parsed_cmd->token, "pwd"))
g_return_code = pwd(); g_return_code = pwd();
else if (!ft_strcmp(parsed_cmd->token, "cd")) else if (!ft_strcmp(parsed_cmd->token, "cd"))
g_return_code = cd(parsed_cmd); g_return_code = cd(parsed_cmd);
else if (!ft_strcmp(parsed_cmd->token, "alias")) else if (!ft_strcmp(parsed_cmd->token, "alias"))
g_return_code = alias(parsed_cmd); g_return_code = alias(parsed_cmd, aliases);
else else
return (STDIN_FILENO); return (STDIN_FILENO);
return (STDOUT_FILENO); return (STDOUT_FILENO);
@ -134,14 +134,14 @@ void get_cmd_path(t_cmd *cmd, t_env *env)
} }
} }
void exec_command(t_cmd *parsed_cmd, t_env *env) void exec_command(t_cmd *parsed_cmd, t_env *env, t_alias *aliases)
{ {
t_cmd *cur_cmd; t_cmd *cur_cmd;
int args_count; int args_count;
char **cmd_args; char **cmd_args;
int i; int i;
if (!parsed_cmd || exec_builtin(parsed_cmd, env)) if (!parsed_cmd || exec_builtin(parsed_cmd, env, aliases))
return ; return ;
cur_cmd = parsed_cmd; cur_cmd = parsed_cmd;
args_count = get_args_count(parsed_cmd); args_count = get_args_count(parsed_cmd);

View File

@ -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/16 13:07:01 by marde-vr ### ########.fr */ /* Updated: 2024/02/17 04:11:51 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,7 +19,7 @@ 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) void exit_bt(t_cmd *args, t_env *env, t_alias *aliases)
{ {
t_cmd *start; t_cmd *start;
int exit_code; int exit_code;
@ -41,6 +41,7 @@ void exit_bt(t_cmd *args, t_env *env)
else else
exit_code = g_return_code; exit_code = g_return_code;
free_cmd(start); free_cmd(start);
free_alias(aliases);
free_env(env); free_env(env);
exit(exit_code); exit(exit_code);
} }

32
srcs/handle_alias.c Normal file
View File

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_alias.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/17 02:54:36 by tomoron #+# #+# */
/* Updated: 2024/02/17 04:26:57 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_cmd *handle_alias(t_cmd *cmd, t_env *env, t_alias *alias)
{
t_cmd *res;
t_cmd *tmp;
char *alias_command;
alias_command = 0;
if(cmd && cmd->type == ARG)
alias_command = ft_get_alias(alias,cmd->token);
if(!alias_command)
return(cmd);
res = parse_command(alias_command, env);
tmp = res;
while(tmp->next)
tmp = tmp->next;
tmp->next = cmd;
return(res);
}

57
srcs/lst_alias.c Executable file
View File

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lst_alias.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */
/* Updated: 2024/02/17 04:34:24 by tomoron ### ########.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 *ft_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> +#+ +:+ +#+ */ /* 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/13 16:21:41 by marde-vr ### ########.fr */ /* Updated: 2024/02/17 04:33:17 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

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/16 21:57:11 by tomoron ### ########.fr */ /* Updated: 2024/02/17 04:30:26 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -65,14 +65,16 @@ int main(int argc, char **argv, char **envp)
char *command; char *command;
t_cmd *parsed_cmd; t_cmd *parsed_cmd;
t_env *env; t_env *env;
t_alias *aliases;
char *prompt; char *prompt;
command = (char *)STDOUT_FILENO; command = (char *)1;
(void)argc; (void)argc;
(void)argv; (void)argv;
env = get_env(envp); env = get_env(envp);
aliases = 0;
if (env) if (env)
handle_minishellrc(env); handle_minishellrc(env, aliases);
while (env && command) while (env && command)
{ {
prompt = get_prompt(); prompt = get_prompt();
@ -82,12 +84,14 @@ int main(int argc, char **argv, char **envp)
free(prompt); free(prompt);
add_history(command); add_history(command);
parsed_cmd = parse_command(command, env); parsed_cmd = parse_command(command, env);
parsed_cmd = handle_alias(parsed_cmd, env, aliases);
free(command); free(command);
//print_parsed_cmd(parsed_cmd);//debug //print_parsed_cmd(parsed_cmd);//debug
exec_command(parsed_cmd, env); exec_command(parsed_cmd, env, aliases);
free_cmd(parsed_cmd); free_cmd(parsed_cmd);
} }
rl_clear_history(); rl_clear_history();
free_env(env); free_env(env);
free_alias(aliases);
return (g_return_code); return (g_return_code);
} }

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/17 02:29:12 by marde-vr ### ########.fr */ /* Updated: 2024/02/17 04:30:53 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -56,9 +56,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); void exec_command(t_cmd *cmd, t_env *env, t_alias *alias);
int echo(t_cmd *args); int echo(t_cmd *args);
void exit_bt(t_cmd *args, t_env *env); void exit_bt(t_cmd *args, t_env *env, t_alias *aliases);
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);
@ -72,9 +72,11 @@ 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_cmd *cmd, t_env *env, int error_code); void ft_exit(t_cmd *cmd, t_env *env, 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); void handle_minishellrc(t_env *env, t_alias *aliases);
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);
int cd(t_cmd *args); int cd(t_cmd *args);
int alias(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);
#endif #endif

View File

@ -6,13 +6,13 @@
/* 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/16 18:25:56 by marde-vr ### ########.fr */ /* Updated: 2024/02/17 04:30:14 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
void handle_minishellrc(t_env *env) void handle_minishellrc(t_env *env, t_alias *aliases)
{ {
char *home; char *home;
char *rc_path; char *rc_path;
@ -35,7 +35,7 @@ void handle_minishellrc(t_env *env)
while (line) while (line)
{ {
parsed_cmd = parse_command(line, env); parsed_cmd = parse_command(line, env);
exec_command(parsed_cmd, env); exec_command(parsed_cmd, env, aliases);
free(parsed_cmd); free(parsed_cmd);
free(line); free(line);
line = get_next_line(fd); line = get_next_line(fd);

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/17 00:29:12 by tomoron ### ########.fr */ /* Updated: 2024/02/17 04:25:54 by tomoron ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
@ -86,7 +86,7 @@ t_token_type get_token_type(char **command)
return (res); return (res);
} }
t_cmd *parse_command(char *command, t_env *env, t_alis *aliases) t_cmd *parse_command(char *command, t_env *env)
{ {
int in_quote; int in_quote;
int in_dquote; int in_dquote;
@ -114,6 +114,5 @@ t_cmd *parse_command(char *command, t_env *env, t_alis *aliases)
ft_putstr_fd("minishell: syntax error\n", 2); ft_putstr_fd("minishell: syntax error\n", 2);
return (0); return (0);
} }
parsed_cmd = handle_alias(parsed_cmd, env, aliases);
return (res); return (res);
} }