username in prompt

banner at starting of shell
preliminary builtins: exit, clear, env
This commit is contained in:
whaffman 2025-02-04 23:45:19 +01:00
parent 1ee3793433
commit ebecccc201
2 changed files with 55 additions and 14 deletions

View File

@ -13,6 +13,15 @@
#include "minishell.h" #include "minishell.h"
#include "prompt.h" #include "prompt.h"
void print_banner(void)
{
ft_printf("%s\n", RED "__ ________________________________________________________________ __" GREEN);
ft_printf("%s\n", RED "__" GREEN " _______ _____ __ _ _____ _______ _ _ _______ " RED "__");
ft_printf("%s\n", RED "__" GREEN " | | | | | \\ | | |______ |_____| |______ | | "RED "__");
ft_printf("%s\n", RED "__" GREEN " | | | __|__ | \\_| __|__ ______| | | |______ |_____ |_____ "RED "__");
ft_printf("%s\n", RED "__ ________________________________________________________________ __\n" RESET);
}
void print_list(void *content) void print_list(void *content)
{ {
t_token *token; t_token *token;
@ -23,27 +32,24 @@ void print_list(void *content)
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_lexer *lexer;
t_list *list; t_list *list;
t_minishell *minishell; t_minishell *minishell;
(void)argc; (void)argc;
(void)argv; (void)argv;
print_banner();
minishell = malloc(sizeof(t_minishell)); minishell = malloc(sizeof(t_minishell));
if (!minishell) if (!minishell)
{ {
perror("failed assigning minishell memory"); perror("failed assigning minishell memory");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
parse_enviroment(envp, &enviroment); parse_enviroment(envp, &enviroment);
minishell->enviroment = enviroment; minishell->enviroment = enviroment;
while (TRUE) while (TRUE)
{ {
line = ft_prompt(minishell); line = ft_prompt(minishell);
if (line != NULL) if (line != NULL)
add_history(line); add_history(line);
@ -51,12 +57,19 @@ int main(int argc, char **argv, char **envp)
list = ft_parse_input(lexer); list = ft_parse_input(lexer);
ft_lstiter(list, print_list); ft_lstiter(list, print_list);
free(line); free(line);
if (list != NULL && ft_strncmp(((t_token *)list->content)->value, "exit\0", 5) == 0) if (list != NULL && ft_strncmp(((t_token *)list->content)->value, "clear\0", 6) == 0)
printf("\033[2J\033[1;1H");
else if (list != NULL && ft_strncmp(((t_token *)list->content)->value, "env\0", 4) == 0)
print_enviroment(enviroment);
else if (list != NULL && ft_strncmp(((t_token *)list->content)->value, "exit\0", 5) == 0)
break; break;
ft_lstclear(&list, ft_clear_tokenlist); ft_lstclear(&list, ft_clear_tokenlist);
ft_lexer_free(lexer); ft_lexer_free(lexer);
} }
ft_lstclear(&list, ft_clear_tokenlist);
ft_lexer_free(lexer);
// print_enviroment(enviroment); // print_enviroment(enviroment);
free_enviroment(enviroment); free_enviroment(enviroment);
free(minishell);
return (EXIT_SUCCESS); return (EXIT_SUCCESS);
} }

View File

@ -12,6 +12,25 @@
#include "minishell.h" #include "minishell.h"
char *get_user(t_minishell *minishell)
{
char *str1;
char *str2;
str1 = ft_strdup(get_enviroment(minishell->enviroment, "USER"));
if (str1 == NULL)
{
str1 = ft_strdup("guest");
}
str2 = ft_strjoin(GREEN, str1);
free(str1);
str1 = ft_strjoin(str2, RESET "@" GREEN "minishell" RESET ": ");
free(str2);
return (str1);
}
char *get_path(t_minishell *minishell) char *get_path(t_minishell *minishell)
{ {
@ -32,6 +51,10 @@ char *get_path(t_minishell *minishell)
free(cwd); free(cwd);
cwd = temp; cwd = temp;
} }
temp = ft_strjoin(BLUE, cwd);
free(cwd);
cwd = ft_strjoin(temp, RESET);
free(temp);
return (cwd); return (cwd);
} }
@ -40,15 +63,20 @@ char *ft_prompt(t_minishell *minishell)
char *line; char *line;
char *cwd; char *cwd;
char *prompt; char *prompt;
char *user;
char *temp; char *temp;
cwd = get_path(minishell); cwd = get_path(minishell);
temp = ft_strjoin(BLUE, cwd); if (cwd == NULL)
{
return (NULL);
}
user = get_user(minishell);
temp = ft_strjoin(user, cwd);
free(user);
free(cwd); free(cwd);
cwd = ft_strjoin(temp, RESET); prompt = ft_strjoin(temp, "> ");
free(temp); free(temp);
prompt = ft_strjoin(cwd, " > ");
line = readline(prompt); line = readline(prompt);
free(prompt); free(prompt);
return (line); return (line);