minishell

This commit is contained in:
2023-12-12 17:32:54 +01:00
commit 196e143285
92 changed files with 4905 additions and 0 deletions

71
prompt/history.c Executable file
View File

@@ -0,0 +1,71 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* history.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/01 17:14:12 by fgras-ca #+# #+# */
/* Updated: 2023/12/07 15:55:33 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../minishell.h"
void display_history_entry(HIST_ENTRY **the_history_list, int i)
{
printf("%d: %s\n", i + history_base, the_history_list[i]->line);
}
void display_history_list(void)
{
int i;
HIST_ENTRY **the_history_list;
i = 0;
the_history_list = history_list();
if (the_history_list)
{
while (the_history_list[i])
{
display_history_entry(the_history_list, i);
i++;
}
}
}
void search_in_history(HIST_ENTRY **the_history_list
, const char *search_string)
{
int i;
i = 0;
while (the_history_list[i])
{
if (ft_strstr(the_history_list[i]->line, search_string))
{
printf("%d: %s\n", i + history_base, the_history_list[i]->line);
}
i++;
}
}
void search_history(void)
{
char *search_string;
HIST_ENTRY **the_history_list;
search_string = readline("Rechercher dans l'historique: ");
if (search_string && *search_string)
{
the_history_list = history_list();
if (the_history_list)
{
search_in_history(the_history_list, search_string);
}
}
if (search_string)
{
free(search_string);
}
}

111
prompt/init_shell.c Executable file
View File

@@ -0,0 +1,111 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* init_shell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/21 20:21:25 by fgras-ca #+# #+# */
/* Updated: 2023/12/09 15:47:18 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../minishell.h"
static char *create_new_env_entry(char *name, char *value)
{
char *new_env_entry;
int name_len;
int value_len;
name_len = ft_strlen(name);
if (value)
{
value_len = ft_strlen(value);
}
else
{
value_len = 0;
}
new_env_entry = malloc(name_len + value_len + 2);
if (!new_env_entry)
exit(EXIT_FAILURE);
ft_strcpy(new_env_entry, name);
ft_strcat(new_env_entry, "=");
if (value)
ft_strcat(new_env_entry, value);
return (new_env_entry);
}
static char *process_env_entry(char *env_entry)
{
char *name;
char *value;
char *new_env_entry;
name = extract_name(env_entry);
value = extract_value(env_entry);
new_env_entry = create_new_env_entry(name, value);
free(name);
free(value);
return (new_env_entry);
}
static void init_environ(t_minishell *shell, char **envp)
{
int count;
int i;
count = 0;
while (envp[count])
count++;
shell->environ = malloc((count + 1) * sizeof(char *));
if (!shell->environ)
exit(EXIT_FAILURE);
i = 0;
while (envp[i])
{
shell->environ[i] = process_env_entry(envp[i]);
i++;
}
shell->environ[count] = NULL;
}
t_minishell init_minishell(char **envp, char **builtin_str,
int (**builtin_func)(char **, t_minishell *))
{
t_minishell shell;
shell.builtin_str = builtin_str;
shell.builtin_func = builtin_func;
init_environ(&shell, envp);
shell.last_exit_status = 0;
shell.tokenizer = malloc(sizeof(t_Tokenizer));
if (!shell.tokenizer)
return (shell);
shell.tokenizer->state = NORMAL;
shell.tokenizer->tokens = NULL;
shell.tokenizer->token = NULL;
shell.tokenizer->token_length = 0;
shell.tokenizer->input_pos = 0;
shell.tokenizer->token_pos = 0;
shell.tokenizer->in_quotes = false;
shell.tokenizer->variable = 0;
shell.tokenizer->symbol = true;
shell.is_piped = false;
return (shell);
}
bool is_builtin_command(const char *command, t_minishell *shell)
{
int i;
i = 0;
while (shell->builtin_str[i] != NULL)
{
if (ft_strcmp(command, shell->builtin_str[i]) == 0)
return (true);
i++;
}
return (false);
}

73
prompt/main.c Executable file
View File

@@ -0,0 +1,73 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/14 19:37:18 by fgras-ca #+# #+# */
/* Updated: 2023/12/09 18:52:00 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../minishell.h"
char **initialize_builtin_str(void)
{
char **local_builtin_str;
local_builtin_str = malloc(8 * sizeof(char *));
if (!local_builtin_str)
return (NULL);
local_builtin_str[0] = "cd";
local_builtin_str[1] = "echo";
local_builtin_str[2] = "exit";
local_builtin_str[3] = "export";
local_builtin_str[4] = "env";
local_builtin_str[5] = "pwd";
local_builtin_str[6] = "unset";
local_builtin_str[7] = NULL;
return (local_builtin_str);
}
t_builtin_func_t *initialize_builtin_func(void)
{
t_builtin_func_t *local_builtin_func;
local_builtin_func = malloc(8 * sizeof(int (*)(char **, t_minishell *)));
if (!local_builtin_func)
return (NULL);
local_builtin_func[0] = &builtin_cd;
local_builtin_func[1] = &builtin_echo_wrapper;
local_builtin_func[2] = &builtin_exit;
local_builtin_func[3] = &builtin_export;
local_builtin_func[4] = &builtin_env;
local_builtin_func[5] = &builtin_pwd;
local_builtin_func[6] = &builtin_unset;
local_builtin_func[7] = NULL;
return (local_builtin_func);
}
int main(int argc, char **argv, char **envp)
{
t_minishell shell;
char **local_builtin_str;
int (**local_builtin_func)(char **, t_minishell *);
int fd;
(void)argc;
(void)argv;
fd = STDOUT_FILENO;
local_builtin_str = initialize_builtin_str();
local_builtin_func = initialize_builtin_func();
if (!local_builtin_str || !local_builtin_func)
{
return (1);
}
rl_bind_key('\t', rl_complete);
shell = init_minishell(envp, local_builtin_str, local_builtin_func);
prompt_loop(&shell, fd);
free(local_builtin_str);
free(local_builtin_func);
return (0);
}

57
prompt/prompt.c Executable file
View File

@@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* prompt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 15:51:21 by fgras-ca #+# #+# */
/* Updated: 2023/12/09 18:49:47 by fgras-ca ### ########.fr */
/* */
/* ************************************************************************** */
#include "../minishell.h"
void disable_tab_completion(void)
{
rl_bind_key('\t', rl_insert);
}
static void process_input_line(t_minishell *shell, char *line, int fd)
{
if (ft_strcmp(line, "history") == 0)
{
display_history_list();
}
else
{
tokenize_and_parse_input(line, shell, fd);
}
}
void prompt_loop(t_minishell *shell, int fd)
{
char *line;
int status;
setup_signal_handlers();
status = 1;
using_history();
disable_tab_completion();
while (status)
{
line = readline("\001"ORANGE"\002Minishell$> \001"RST"\002");
if (!line)
{
write(STDOUT_FILENO, "\n", 1);
break ;
}
if (!is_only_spaces(line) && ft_strcmp(line, "") != 0)
{
add_history(line);
process_input_line(shell, line, fd);
}
free(line);
}
clear_history();
}