diff --git a/README.md b/README.md index e91f981..9350ac0 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,21 @@ # Minishell ======= +## Dependencies +- libreadline-dev +- libncurses-dev + +## merge info +origen/quinten -> willem: + +ik het TokenType veranderd naar t_token_type vanwege de norm + +Volgens mij had je een off-by-one error in ft_lexer_readword:85 +je moet de null terminator ook meerekenen in de malloc size +```c + if (!(word = malloc(sizeof(char) * (i + 1)))) +``` + + ## Allowed Functions ### `` diff --git a/inc/minishell.h b/inc/minishell.h index cb1371a..35a59c9 100644 --- a/inc/minishell.h +++ b/inc/minishell.h @@ -24,15 +24,20 @@ # define SUCCESS 1 # define FAILURE 0 - +# define RED "\033[0;31m" +# define GREEN "\033[0;32m" +# define YELLOW "\033[0;33m" +# define BLUE "\033[0;34m" +# define MAGENTA "\033[0;35m" +# define CYAN "\033[0;36m" +# define RESET "\033[0m" typedef struct s_minishell { t_enviroment *enviroment; - char *pwd; + } t_minishell; - typedef enum { T_WORD, @@ -42,11 +47,11 @@ typedef enum T_APPEND_OUT, T_EOF, T_ERROR -} TokenType; +} t_token_type; typedef struct s_token { - TokenType type; + t_token_type type; char *value; int position; } t_token; @@ -73,7 +78,7 @@ t_list *ft_parse_input(t_lexer *lexer); * Token */ t_token *ft_token_next(t_lexer *lexer); -t_token *ft_token_new(TokenType type, char *c, int pos); +t_token *ft_token_new(t_token_type type, char *c, int pos); void ft_token_free(t_token *token); void ft_clear_tokenlist(void *content); t_token *ft_parse_token(t_lexer *lexer); diff --git a/inc/prompt.h b/inc/prompt.h index 655e832..9981ef3 100644 --- a/inc/prompt.h +++ b/inc/prompt.h @@ -15,6 +15,8 @@ # include "minishell.h" -char *prompt(t_minishell *minishell); +typedef struct s_minishell t_minishell; // Forward declaration WHY? + +char *ft_prompt(t_minishell *minishell); #endif // PROMPT_H diff --git a/src/main.c b/src/main.c index 4265206..f17eef1 100644 --- a/src/main.c +++ b/src/main.c @@ -11,57 +11,52 @@ /* ************************************************************************** */ #include "minishell.h" +#include "prompt.h" -// void print_list(void *content) -// { -// t_token *token; -// token = (t_token *)content; -// ft_printf("%s\n", token->value); -// } - -// int main(int argc, char **argv, char **envp) -// { -// (void)argc; -// (void)envp; -// // char **env; -// // t_enviroment *enviroment = NULL; -// t_lexer *lexer; -// t_list *list; - -// // while (*envp != NULL) -// // { -// // env = ft_split(*envp, '='); -// // add_enviroment(&enviroment, env[0], env[1]); -// // envp++; -// // } - -// lexer = ft_lexer_new(argv[1]); -// list = ft_parse_input(lexer); -// ft_lstiter(list, print_list); - -// ft_lstclear(&list, ft_clear_tokenlist); -// ft_lexer_free(lexer); -// // print_enviroment(enviroment); -// return 0; -// } +void print_list(void *content) +{ + t_token *token; + token = (t_token *)content; + ft_printf("%s\n", token->value); +} int main(int argc, char **argv, char **envp) { - t_enviroment *enviroment; - char *line; + t_enviroment *enviroment; + + char *line; + t_lexer *lexer; + t_list *list; + t_minishell *minishell; + (void)argc; (void)argv; - parse_enviroment(envp, &enviroment); - while (1) + minishell = malloc(sizeof(t_minishell)); + if (!minishell) { - line = prompt(); - if (line != NULL) - add_history(line); - printf("%s\n", line); - free(line); + perror("failed assigning minishell memory"); + exit(EXIT_FAILURE); } - //print_enviroment(enviroment); + + parse_enviroment(envp, &enviroment); + minishell->enviroment = enviroment; + while (TRUE) + { + + line = ft_prompt(minishell); + if (line != NULL) + add_history(line); + lexer = ft_lexer_new(line); + list = ft_parse_input(lexer); + ft_lstiter(list, print_list); + free(line); + if (list != NULL && ft_strncmp(((t_token *)list->content)->value, "exit\0", 5) == 0) + break; + ft_lstclear(&list, ft_clear_tokenlist); + ft_lexer_free(lexer); + } + // print_enviroment(enviroment); free_enviroment(enviroment); return (EXIT_SUCCESS); } diff --git a/src/prompt/prompt.c b/src/prompt/prompt.c index af64f81..dc2dacb 100644 --- a/src/prompt/prompt.c +++ b/src/prompt/prompt.c @@ -14,33 +14,40 @@ char *get_path(t_minishell *minishell) { - char *path; + char *home; + char *temp; char *cwd; - + cwd = getcwd(NULL, 0); + home = get_enviroment(minishell->enviroment, "HOME"); if (cwd == NULL) { perror("getcwd"); return (NULL); } - while (ft_strncmp(cwd, minishell->pwd, ft_strlen(minishell->pwd)) == 0) + if (home && ft_strncmp(cwd, home, ft_strlen(home)) == 0) { - home = ft_strjoin("~", cwd + ft_strlen(minishell->pwd)); + temp = ft_strjoin("~", cwd + ft_strlen(home)); free(cwd); - cwd = home; + cwd = temp; } - return (path); + return (cwd); } -char *prompt(t_minishell *minishell) +char *ft_prompt(t_minishell *minishell) { char *line; - char *pwd; + char *cwd; char *prompt; + char *temp; - pwd = get_path(minishell); - prompt = ft_strjoin(pwd, " > "); + cwd = get_path(minishell); + temp = ft_strjoin(BLUE, cwd); + free(cwd); + cwd = ft_strjoin(temp, RESET); + free(temp); + prompt = ft_strjoin(cwd, " > "); line = readline(prompt); free(prompt); diff --git a/src/tokenizer/lexer.c b/src/tokenizer/lexer.c index ed9a65a..601268c 100644 --- a/src/tokenizer/lexer.c +++ b/src/tokenizer/lexer.c @@ -82,7 +82,7 @@ char *ft_lexer_readword(t_lexer *lexer) ft_lexer_readchar(lexer); } len = lexer->pos - start; - word = malloc(sizeof(char) * len); + word = malloc(sizeof(char) * len + 1); ft_strlcpy(word, lexer->input + start, len + 1); return (word); } diff --git a/src/tokenizer/tokens.c b/src/tokenizer/tokens.c index 00ad78f..f555cf1 100644 --- a/src/tokenizer/tokens.c +++ b/src/tokenizer/tokens.c @@ -38,7 +38,7 @@ t_token *ft_parse_token(t_lexer *lexer) return (token); } -t_token *ft_token_new(TokenType type, char *c, int pos) +t_token *ft_token_new(t_token_type type, char *c, int pos) { t_token *token;