main samengevoegd, prompt home ~, kleurtjes
This commit is contained in:
parent
f90b371ace
commit
1ee3793433
16
README.md
16
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
|
||||
|
||||
### `<fcntl.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);
|
||||
|
||||
@ -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
|
||||
|
||||
79
src/main.c
79
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);
|
||||
}
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user