main samengevoegd, prompt home ~, kleurtjes

This commit is contained in:
whaffman 2025-02-04 22:53:46 +01:00
parent f90b371ace
commit 1ee3793433
7 changed files with 86 additions and 61 deletions

View File

@ -1,5 +1,21 @@
# Minishell # 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 ## Allowed Functions
### `<fcntl.h>` ### `<fcntl.h>`

View File

@ -24,14 +24,19 @@
# define SUCCESS 1 # define SUCCESS 1
# define FAILURE 0 # 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 typedef struct s_minishell
{ {
t_enviroment *enviroment; t_enviroment *enviroment;
char *pwd;
} t_minishell;
} t_minishell;
typedef enum typedef enum
{ {
@ -42,11 +47,11 @@ typedef enum
T_APPEND_OUT, T_APPEND_OUT,
T_EOF, T_EOF,
T_ERROR T_ERROR
} TokenType; } t_token_type;
typedef struct s_token typedef struct s_token
{ {
TokenType type; t_token_type type;
char *value; char *value;
int position; int position;
} t_token; } t_token;
@ -73,7 +78,7 @@ t_list *ft_parse_input(t_lexer *lexer);
* Token * Token
*/ */
t_token *ft_token_next(t_lexer *lexer); 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_token_free(t_token *token);
void ft_clear_tokenlist(void *content); void ft_clear_tokenlist(void *content);
t_token *ft_parse_token(t_lexer *lexer); t_token *ft_parse_token(t_lexer *lexer);

View File

@ -15,6 +15,8 @@
# include "minishell.h" # 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 #endif // PROMPT_H

View File

@ -11,57 +11,52 @@
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
#include "prompt.h"
// void print_list(void *content) void print_list(void *content)
// { {
// t_token *token; t_token *token;
// token = (t_token *)content; token = (t_token *)content;
// ft_printf("%s\n", token->value); 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;
// }
int main(int argc, char **argv, char **envp) int main(int argc, char **argv, char **envp)
{ {
t_enviroment *enviroment; t_enviroment *enviroment;
char *line;
char *line;
t_lexer *lexer;
t_list *list;
t_minishell *minishell;
(void)argc; (void)argc;
(void)argv; (void)argv;
parse_enviroment(envp, &enviroment); minishell = malloc(sizeof(t_minishell));
while (1) if (!minishell)
{ {
line = prompt(); perror("failed assigning minishell memory");
if (line != NULL) exit(EXIT_FAILURE);
add_history(line);
printf("%s\n", line);
free(line);
} }
//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); free_enviroment(enviroment);
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }

View File

@ -14,33 +14,40 @@
char *get_path(t_minishell *minishell) char *get_path(t_minishell *minishell)
{ {
char *path;
char *home; char *home;
char *temp;
char *cwd; char *cwd;
cwd = getcwd(NULL, 0); cwd = getcwd(NULL, 0);
home = get_enviroment(minishell->enviroment, "HOME");
if (cwd == NULL) if (cwd == NULL)
{ {
perror("getcwd"); perror("getcwd");
return (NULL); 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); free(cwd);
cwd = home; cwd = temp;
} }
return (path); return (cwd);
} }
char *prompt(t_minishell *minishell) char *ft_prompt(t_minishell *minishell)
{ {
char *line; char *line;
char *pwd; char *cwd;
char *prompt; char *prompt;
char *temp;
pwd = get_path(minishell); cwd = get_path(minishell);
prompt = ft_strjoin(pwd, " > "); temp = ft_strjoin(BLUE, cwd);
free(cwd);
cwd = ft_strjoin(temp, RESET);
free(temp);
prompt = ft_strjoin(cwd, " > ");
line = readline(prompt); line = readline(prompt);
free(prompt); free(prompt);

View File

@ -82,7 +82,7 @@ char *ft_lexer_readword(t_lexer *lexer)
ft_lexer_readchar(lexer); ft_lexer_readchar(lexer);
} }
len = lexer->pos - start; len = lexer->pos - start;
word = malloc(sizeof(char) * len); word = malloc(sizeof(char) * len + 1);
ft_strlcpy(word, lexer->input + start, len + 1); ft_strlcpy(word, lexer->input + start, len + 1);
return (word); return (word);
} }

View File

@ -38,7 +38,7 @@ t_token *ft_parse_token(t_lexer *lexer)
return (token); 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; t_token *token;