username in prompt
banner at starting of shell preliminary builtins: exit, clear, env
This commit is contained in:
parent
1ee3793433
commit
ebecccc201
23
src/main.c
23
src/main.c
@ -13,6 +13,15 @@
|
||||
#include "minishell.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)
|
||||
{
|
||||
t_token *token;
|
||||
@ -23,27 +32,24 @@ void print_list(void *content)
|
||||
int main(int argc, char **argv, char **envp)
|
||||
{
|
||||
t_enviroment *enviroment;
|
||||
|
||||
char *line;
|
||||
t_lexer *lexer;
|
||||
t_list *list;
|
||||
t_minishell *minishell;
|
||||
|
||||
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
print_banner();
|
||||
minishell = malloc(sizeof(t_minishell));
|
||||
if (!minishell)
|
||||
{
|
||||
perror("failed assigning minishell memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
parse_enviroment(envp, &enviroment);
|
||||
minishell->enviroment = enviroment;
|
||||
while (TRUE)
|
||||
{
|
||||
|
||||
line = ft_prompt(minishell);
|
||||
if (line != NULL)
|
||||
add_history(line);
|
||||
@ -51,12 +57,19 @@ int main(int argc, char **argv, char **envp)
|
||||
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)
|
||||
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;
|
||||
ft_lstclear(&list, ft_clear_tokenlist);
|
||||
ft_lexer_free(lexer);
|
||||
}
|
||||
ft_lstclear(&list, ft_clear_tokenlist);
|
||||
ft_lexer_free(lexer);
|
||||
// print_enviroment(enviroment);
|
||||
free_enviroment(enviroment);
|
||||
free(minishell);
|
||||
return (EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
@ -12,6 +12,25 @@
|
||||
|
||||
#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)
|
||||
{
|
||||
|
||||
@ -32,6 +51,10 @@ char *get_path(t_minishell *minishell)
|
||||
free(cwd);
|
||||
cwd = temp;
|
||||
}
|
||||
temp = ft_strjoin(BLUE, cwd);
|
||||
free(cwd);
|
||||
cwd = ft_strjoin(temp, RESET);
|
||||
free(temp);
|
||||
return (cwd);
|
||||
}
|
||||
|
||||
@ -40,15 +63,20 @@ char *ft_prompt(t_minishell *minishell)
|
||||
char *line;
|
||||
char *cwd;
|
||||
char *prompt;
|
||||
char *user;
|
||||
char *temp;
|
||||
|
||||
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);
|
||||
cwd = ft_strjoin(temp, RESET);
|
||||
prompt = ft_strjoin(temp, "> ");
|
||||
free(temp);
|
||||
prompt = ft_strjoin(cwd, " > ");
|
||||
|
||||
line = readline(prompt);
|
||||
free(prompt);
|
||||
return (line);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user