diff --git a/Makefile b/Makefile index 9954661..2bee8fa 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: marde-vr +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # 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\ diff --git a/srcs/alias.c b/srcs/alias.c index a830676..b66c683 100644 --- a/srcs/alias.c +++ b/srcs/alias.c @@ -6,7 +6,7 @@ /* 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); } -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); } } diff --git a/srcs/cd.c b/srcs/cd.c index 6e1f630..e88948a 100644 --- a/srcs/cd.c +++ b/srcs/cd.c @@ -6,7 +6,7 @@ /* 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; + 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"); diff --git a/srcs/debug.c b/srcs/debug.c index d342711..8e06760 100644 --- a/srcs/debug.c +++ b/srcs/debug.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* debug.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: tomoron +#+ +:+ +#+ */ +/* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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; diff --git a/srcs/exec.c b/srcs/exec.c index 7248959..e1ab712 100755 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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) { diff --git a/srcs/handle_alias.c b/srcs/handle_alias.c index 667fc7d..4c85289 100644 --- a/srcs/handle_alias.c +++ b/srcs/handle_alias.c @@ -3,33 +3,33 @@ /* ::: :::::::: */ /* handle_alias.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: tomoron +#+ +:+ +#+ */ +/* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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" -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 *tmp; - char *alias_command; + char *alias_command; 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); } diff --git a/srcs/lst_alias.c b/srcs/lst_alias.c index a3351ed..38ffbb2 100755 --- a/srcs/lst_alias.c +++ b/srcs/lst_alias.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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) { diff --git a/srcs/main.c b/srcs/main.c index 5d6cecb..3a12c9a 100755 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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, "\033[1m\002", 1); cwd = getcwd(cwd_buffer, 99); - if(ft_get_env(env, "HOME") && !ft_strncmp(cwd_buffer, ft_get_env(env, - "HOME"), ft_strlen(ft_get_env(env, "HOME")))) + 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; cwd[0] = '~'; @@ -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) { diff --git a/srcs/minishell.h b/srcs/minishell.h index 41b00fa..bf7fbd5 100755 --- a/srcs/minishell.h +++ b/srcs/minishell.h @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 { - char *name; - char *value; - struct s_alias *next; + char *name; + char *value; + struct s_alias *next; } t_alias; 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 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); diff --git a/srcs/minishellrc.c b/srcs/minishellrc.c index 08dd059..46c30a5 100644 --- a/srcs/minishellrc.c +++ b/srcs/minishellrc.c @@ -6,19 +6,36 @@ /* 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" +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) { 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"); @@ -31,18 +48,7 @@ void handle_minishellrc(t_env **env, t_alias **aliases) perror("open"); return ; } - 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); - } + exec_rc_file(env, aliases, fd); close(fd); } free(rc_path); diff --git a/srcs/parsing.c b/srcs/parsing.c index 16f1143..6dc531a 100755 --- a/srcs/parsing.c +++ b/srcs/parsing.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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" @@ -18,17 +18,17 @@ int is_cmd_char(char c) int add_home_to_str(char *res) { - int i; - char *str; + int i; + char *str; 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,10 +51,10 @@ 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 != '"')) + || (**cmd != '\'' && **cmd != '"')) res[i++] = **cmd; (*cmd)++; } diff --git a/srcs/pwd.c b/srcs/pwd.c index 2605550..7a5b7f7 100755 --- a/srcs/pwd.c +++ b/srcs/pwd.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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); diff --git a/srcs/unalias.c b/srcs/unalias.c new file mode 100644 index 0000000..8625d28 --- /dev/null +++ b/srcs/unalias.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* unalias.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: marde-vr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +}