diff --git a/ft_echo.c b/ft_echo.c index 2f9530d..6b5ea4f 100644 --- a/ft_echo.c +++ b/ft_echo.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/07 15:30:37 by tomoron #+# #+# */ -/* Updated: 2024/02/07 23:15:04 by tomoron ### ########.fr */ +/* Updated: 2024/02/09 14:56:57 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,22 +14,22 @@ int ft_echo(t_cmd *args) { - int put_nl; + int put_nl; put_nl = 1; - while(args && !strcmp(args->token,"-n")) + while (args && !strcmp(args->token, "-n")) { put_nl = 0; args = args->next; } - while(args) + while (args) { ft_putstr_fd(args->token, STDOUT_FILENO); - if(args->next) - ft_putchar_fd(' ',STDOUT_FILENO); + if (args->next) + ft_putchar_fd(' ', STDOUT_FILENO); args = args->next; } - if(put_nl) - ft_putchar_fd('\n',STDOUT_FILENO); - return(0); + if (put_nl) + ft_putchar_fd('\n', STDOUT_FILENO); + return (0); } diff --git a/ft_exec.c b/ft_exec.c index caf1614..c9fe9b8 100644 --- a/ft_exec.c +++ b/ft_exec.c @@ -6,32 +6,32 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */ -/* Updated: 2024/02/08 16:57:26 by tomoron ### ########.fr */ +/* Updated: 2024/02/09 14:59:22 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int exec_builtin(t_cmd *parsed_cmd, t_env *env) +int exec_builtin(t_cmd *parsed_cmd, t_env *env) { - if(!strcmp(parsed_cmd->token, "echo")) + if (!strcmp(parsed_cmd->token, "echo")) g_return_code = ft_echo(parsed_cmd->next); - else if (!strcmp(parsed_cmd->token,"ret")) + else if (!strcmp(parsed_cmd->token, "ret")) g_return_code = ft_atoi(parsed_cmd->next->token); - else if(!strcmp(parsed_cmd->token, "env")) + else if (!strcmp(parsed_cmd->token, "env")) g_return_code = ft_print_env(env); - else if(!strcmp(parsed_cmd->token, "exit")) - ft_exit(parsed_cmd); + else if (!strcmp(parsed_cmd->token, "exit")) + ft_exit(parsed_cmd, env); else - return(STDIN_FILENO); - return(STDOUT_FILENO); + return (STDIN_FILENO); + return (STDOUT_FILENO); } void ft_exec_command(t_cmd *parsed_cmd, t_env *env) { - if(!parsed_cmd) - return; - if(exec_builtin(parsed_cmd, env)) + if (!parsed_cmd) + return ; + if (exec_builtin(parsed_cmd, env)) return ; ft_printf("not a builtin\n"); } diff --git a/ft_exit.c b/ft_exit.c index 3c95749..97bcdbf 100644 --- a/ft_exit.c +++ b/ft_exit.c @@ -6,26 +6,30 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/07 16:04:11 by tomoron #+# #+# */ -/* Updated: 2024/02/07 16:48:07 by tomoron ### ########.fr */ +/* Updated: 2024/02/09 15:08:01 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void ft_exit(t_cmd *args) +void ft_exit(t_cmd *args, t_env *env) { - t_cmd *start; + t_cmd *start; + int exit_code; start = args; args = args->next; ft_printf("exit\n"); - if(args && args->next) + if (args && args->next) ft_printf("minishell: exit: too many arguments\n"); else { + if (args) + exit_code = (unsigned char)ft_atoi(args->token); + else + exit_code = g_return_code; ft_free_cmd(start); - if(args) - exit((unsigned char)ft_atoi(args->token)); + ft_free_env(env); exit(g_return_code); } } diff --git a/ft_lst_cmd.c b/ft_lst_cmd.c index d89719c..3a78cf7 100644 --- a/ft_lst_cmd.c +++ b/ft_lst_cmd.c @@ -6,16 +6,16 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */ -/* Updated: 2024/02/08 12:42:12 by tomoron ### ########.fr */ +/* Updated: 2024/02/09 15:09:02 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_cmd *ft_cmd_add_back(t_cmd *cmd, char *token) +t_cmd *ft_cmd_add_back(t_cmd *cmd, char *token) { - t_cmd *res; - t_cmd *current; + t_cmd *res; + t_cmd *current; res = ft_calloc(1, sizeof(t_cmd)); if (!res) @@ -26,15 +26,15 @@ t_cmd *ft_cmd_add_back(t_cmd *cmd, char *token) current = cmd; while (current->next) current = current->next; - current->next = res; + current->next = res; return (cmd); } void ft_free_cmd(t_cmd *cmd) { - if(cmd && cmd->next) + if (cmd && cmd->next) ft_free_cmd(cmd->next); - if(cmd) + if (cmd) free(cmd->token); free(cmd); } diff --git a/ft_lst_env.c b/ft_lst_env.c index 8215094..f18e31c 100644 --- a/ft_lst_env.c +++ b/ft_lst_env.c @@ -6,16 +6,16 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */ -/* Updated: 2024/02/08 16:58:48 by tomoron ### ########.fr */ +/* Updated: 2024/02/09 15:24:59 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_env *ft_env_add_back(t_env *env, char *name, char *value) +t_env *ft_env_add_back(t_env *env, char *name, char *value) { - t_env *res; - t_env *current; + t_env *res; + t_env *current; res = ft_calloc(1, sizeof(t_env)); if (!res) @@ -27,15 +27,15 @@ t_env *ft_env_add_back(t_env *env, char *name, char *value) current = env; while (current->next) current = current->next; - current->next = res; + current->next = res; return (env); } void ft_free_env(t_env *env) { - if(env && env->next) + if (env && env->next) ft_free_env(env->next); - if(env) + if (env) { free(env->name); free(env->value); @@ -45,10 +45,33 @@ void ft_free_env(t_env *env) int ft_print_env(t_env *env) { - while(env) + while (env) { - printf("%s=%s\n",env->name, env->value); + ft_printf("%s=%s\n", env->name, env->value); env = env->next; } - return(0); + return (0); +} + +char *ft_getenv(t_env *env, char *name) +{ + while (env) + { + if (!ft_strcmp(env->name, name)) + return (env->value); + env = env->next; + } + return (0); +} + +int get_var_name_len(char *command) +{ + int res; + + res = 0; + if (command[res] == '?') + return (1); + while (ft_isalnum(command[res]) || command[res] == '_') + res++; + return (res); } diff --git a/ft_parsing.c b/ft_parsing.c new file mode 100644 index 0000000..885b925 --- /dev/null +++ b/ft_parsing.c @@ -0,0 +1,66 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parsing.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */ +/* Updated: 2024/02/09 16:40:07 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ +#include "minishell.h" + +char *ft_get_token(char **cmd, int *in_quote, int *in_dquote, t_env *env) +{ + char *res; + int i; + + i = 0; + while (ft_isspace(**cmd)) + (*cmd)++; + res = ft_calloc(ft_get_token_len(*cmd, env) + 1, 1); + while (res && **cmd && (!ft_isspace(**cmd) || *in_quote || *in_dquote)) + { + if (**cmd == '"' && !*in_quote) + *in_dquote = !*in_dquote; + if (**cmd == '\'' && !*in_dquote) + *in_quote = !*in_quote; + if (**cmd == '$' && !*in_quote) + { + (*cmd)++; + i += ft_add_var_to_str(res + i, cmd, env); + } + else if (((**cmd == '\'' && *in_dquote) || (**cmd == '"' && *in_quote)) + || (**cmd != '\'' && **command != '"')) + res[i++] = **cmd; + (*cmd)++; + } + return (res); +} + +t_cmd *ft_parse_command(char *command, t_env *env) +{ + int in_quote; + int in_dquote; + t_cmd *res; + char *token; + + in_quote = 0; + in_dquote = 0; + res = STDIN_FILENO; + if (!command) + return (STDIN_FILENO); + while (*command) + { + token = ft_get_token(&command, &in_quote, &in_dquote, env); + res = ft_cmd_add_back(res, token); + } + if (in_quote || in_dquote) + { + ft_free_cmd(res); + ft_putstr_fd("minishell: syntax error\n", 2); + return (0); + } + return (res); +} diff --git a/ft_parsing_var.c b/ft_parsing_var.c new file mode 100644 index 0000000..9fb4aea --- /dev/null +++ b/ft_parsing_var.c @@ -0,0 +1,64 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_parsing_var.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tomoron +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/02/09 15:24:36 by tomoron #+# #+# */ +/* Updated: 2024/02/09 15:24:43 by tomoron ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +char *get_var_name(char *command) +{ + char *res; + int len; + + len = get_var_name_len(command); + res = ft_substr(command, 0, len); + return (res); +} + +int ft_get_var_len(char **command, t_env *env) +{ + char *var_name; + char *env_var; + + (*command)++; + if (!ft_isalnum(**command) && **command != '_' && **command != '?') + return (1); + if (**command == '?') + return (get_number_len(g_return_code)); + var_name = get_var_name(*command); + env_var = ft_getenv(env, var_name); + free(var_name); + if (!env_var) + return (0); + *command += get_var_name_len(*command) - 1; + return (ft_strlen(env_var)); +} + +int ft_get_token_len(char *command, t_env *env) +{ + int in_quote; + int in_dquote; + int res; + + in_quote = 0; + in_dquote = 0; + res = 0; + while (*command && (!ft_isspace(*command) || in_quote || in_dquote)) + { + if (*command == '"' && !in_quote) + in_dquote = !in_dquote; + if (*command == '\'' && !in_dquote) + in_quote = !in_quote; + if (*command == '$' && !in_quote) + res += ft_get_var_len(&command, env); + else if (*command != '\'' && *command != '"') + res++; + else if ((*command == '\'' && in_dquote) + || (*command == '"' && in_quote)) diff --git a/libft/ft_isspace.c b/libft/ft_isspace.c index 4a75de2..acf38bb 100644 --- a/libft/ft_isspace.c +++ b/libft/ft_isspace.c @@ -6,12 +6,12 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/05 00:58:08 by tomoron #+# #+# */ -/* Updated: 2024/02/06 22:17:28 by tomoron ### ########.fr */ +/* Updated: 2024/02/09 16:40:35 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ -int ft_isspace(int c) +int ft_isspace(int c) { - return (c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v' + return (c == '\f' || c == '\n' || c == '\r' || c == '\t' || c == '\v' || c == ' '); } diff --git a/main.c b/main.c index 03e7cd9..193b698 100644 --- a/main.c +++ b/main.c @@ -6,194 +6,33 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */ -/* Updated: 2024/02/09 14:37:48 by tomoron ### ########.fr */ +/* Updated: 2024/02/09 16:28:28 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -int g_return_code = 0; +int g_return_code = 0; -char *get_prompt(void) +char *get_prompt(void) { - char *res; + char *res; - res = ft_strjoin_free("\001",ft_get_color(0,255,0),2); - res = ft_strjoin_free(res,"\002", 1); - res = ft_strjoin_free(res, getenv("USER"),1); - res = ft_strjoin_free(res, "@",1); - res = ft_strjoin_free(res, "minishell \001\033[0m\002$>",1); - return (res); -} - -char *ft_getenv(t_env *env, char *name) -{ - while(env) - { - if(!ft_strcmp(env->name, name)) - return(env->value); - env = env->next; - } - return(0); -} - -int get_var_name_len(char *command) -{ - int res; - - res = 0; - if(command[res] == '?') - return(1); - while(ft_isalnum(command[res]) || command[res] == '_') - res++; - return(res); -} - -char *get_var_name(char *command) -{ - char *res; - int len; - - len = get_var_name_len(command); - res = ft_substr(command,0, len); - return(res); -} - -int ft_get_var_len(char **command, t_env *env) -{ - char *var_name; - char *env_var; - - (*command)++; - if (!ft_isalnum(**command) && **command != '_' && **command != '?') - return (1); - if (**command == '?') - return (get_number_len(g_return_code)); - var_name = get_var_name(*command); - env_var = ft_getenv(env, var_name); - free(var_name); - if (!env_var) - return (0); - *command += get_var_name_len(*command) - 1; - return (ft_strlen(env_var)); -} - -int ft_get_token_len(char *command, t_env *env) -{ - int in_quote; - int in_dquote; - int res; - - in_quote = 0; - in_dquote = 0; - res = 0; - while (*command && (!ft_isspace(*command) || in_quote || in_dquote)) - { - if (*command == '"' && !in_quote) - in_dquote = !in_dquote; - if (*command == '\'' && !in_dquote) - in_quote = !in_quote; - if (*command == '$' && !in_quote) - res += ft_get_var_len(&command, env); - else if (*command != '\'' && *command != '"') - res++; - else if ((*command == '\'' && in_dquote) || (*command == '"' && in_quote)) - res++; - command++; - } - return (res); -} - -int ft_add_var_to_str(char *res, char **command, t_env *env) -{ - char *var_name; - char *var; - int i; - - i = -1; - if (!ft_isalnum(**command) && **command != '_' && **command != '?') - { - *res = '$'; - return(1); - } - if (**command == '?') - { - var = ft_itoa(g_return_code); - while(var && var[++i]) - res[i] = var[i]; - free(var); - return(i + 1); - } - var_name = get_var_name(*command); - var = ft_getenv(env, var_name); - free(var_name); - while(var && var[++i]) - res[i] = var[i]; - *command += get_var_name_len(*command) - 1; - return (i + !var); -} - -char *ft_get_token(char **command, int *in_quote, int *in_dquote, t_env *env) -{ - char *res; - int i; - - i = 0; - while (ft_isspace(**command)) - (*command)++; - res = ft_calloc(ft_get_token_len(*command, env) + 1, 1); - while (res && **command && (!ft_isspace(**command) || *in_quote || *in_dquote)) - { - if (**command == '"' && !*in_quote) - *in_dquote = !*in_dquote; - if (**command == '\'' && !*in_dquote) - *in_quote = !*in_quote; - if (**command == '$' && !*in_quote) - { - (*command)++; - i += ft_add_var_to_str(res + i, command , env); - } - else if (((**command == '\'' && *in_dquote) || (**command == '"' && *in_quote)) - || (**command != '\'' && **command != '"')) - res[i++] = **command; - (*command)++; - } - return (res); -} - -t_cmd *ft_parse_command(char *command, t_env *env) -{ - int in_quote; - int in_dquote; - t_cmd *res; - char *token; - - in_quote = 0; - in_dquote = 0; - res = STDIN_FILENO; - if (!command) - return (STDIN_FILENO); - while (*command) - { - token = ft_get_token(&command, &in_quote, &in_dquote, env); - res = ft_cmd_add_back(res, token); - } - if (in_quote || in_dquote) - { - ft_free_cmd(res); - ft_putstr_fd("minishell: syntax error\n", 2); - return (0); - } + res = ft_strjoin_free("\001", ft_get_color(0, 255, 0), 2); + res = ft_strjoin_free(res, "\002", 1); + res = ft_strjoin_free(res, getenv("USER"), 1); + res = ft_strjoin_free(res, "@", 1); + res = ft_strjoin_free(res, "minishell \001\033[0m\002$>", 1); return (res); } t_env *ft_get_env(char **envp) { - t_env *env; - char *name; - char *value; - int i; - int j; + t_env *env; + char *name; + char *value; + int i; + int j; env = 0; while (*envp) @@ -216,12 +55,12 @@ t_env *ft_get_env(char **envp) return (env); } -int main(int argc, char **argv, char **envp) +int main(int argc, char **argv, char **envp) { - char *command; - t_cmd *parsed_cmd; - t_env *env; - char *prompt; + char *command; + t_cmd *parsed_cmd; + t_env *env; + char *prompt; command = (char *)STDOUT_FILENO; (void)argc; diff --git a/minishell.h b/minishell.h index c2ac5ae..eb52d9c 100644 --- a/minishell.h +++ b/minishell.h @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */ -/* Updated: 2024/02/08 16:58:15 by tomoron ### ########.fr */ +/* Updated: 2024/02/09 14:58:21 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,10 +17,9 @@ # include //debug # include "libft/libft.h" - typedef struct s_cmd { - char *token; + char *token; struct s_cmd *next; } t_cmd; @@ -31,13 +30,13 @@ typedef struct s_env struct s_env *next; } t_env; -extern int g_return_code; +extern int g_return_code; -t_cmd *ft_cmd_add_back(t_cmd *res, char *token); +t_cmd *ft_cmd_add_back(t_cmd *res, char *token); void ft_free_cmd(t_cmd *cmd); void ft_exec_command(t_cmd *cmd, t_env *env); int ft_echo(t_cmd *args); -void ft_exit(t_cmd *args); +void ft_exit(t_cmd *args, t_env *env); t_env *ft_env_add_back(t_env *env, char *name, char *value); void ft_free_env(t_env *env); int ft_print_env(t_env *env); diff --git a/other/test.c b/other/test.c index f5a77a2..73b952b 100644 --- a/other/test.c +++ b/other/test.c @@ -6,20 +6,22 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/04 17:30:08 by tomoron #+# #+# */ -/* Updated: 2024/02/07 14:29:12 by tomoron ### ########.fr */ +/* Updated: 2024/02/09 16:41:52 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include #include -int main(int argc, char **argv) +int main(int argc, char **argv) { - int i = 0; - while(i < argc) + int i; + + i = 0; + while (i < argc) { - printf("\"%s\"\n",argv[i]); + printf("\"%s\"\n", argv[i]); i++; } - return(0); + return (0); } diff --git a/other/test2.c b/other/test2.c index c07b453..a870f6b 100644 --- a/other/test2.c +++ b/other/test2.c @@ -6,14 +6,15 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/07 13:30:04 by tomoron #+# #+# */ -/* Updated: 2024/02/07 13:34:55 by tomoron ### ########.fr */ +/* Updated: 2024/02/09 16:42:59 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include #include -int main(int argc,char **argv) + +int main(int argc, char **argv) { - if(argc == 2) - return(atoi(argv[1])); - return(0); + if (argc == 2) + return (atoi(argv[1])); + return (0); }