128 lines
2.7 KiB
C
Raw Normal View History

2024-04-25 11:46:41 +02:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
2024-04-25 11:46:41 +02:00
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 11:18:04 by adjoly #+# #+# */
/* Updated: 2024/07/04 17:02:14 by adjoly ### ########.fr */
2024-04-25 11:46:41 +02:00
/* */
/* ************************************************************************** */
#include <signal.h>
2024-05-13 17:33:29 +02:00
#include <stdio.h>
2024-04-25 11:46:41 +02:00
#include <readline/readline.h>
#include "error_msg.h"
2024-04-30 12:36:16 +02:00
#include <readline/history.h>
2024-04-27 17:39:26 +02:00
#include <stdlib.h>
2024-04-30 12:36:16 +02:00
#include <stdbool.h>
#include <fcntl.h>
2024-05-09 16:46:13 +02:00
#include "libft.h"
2024-06-25 10:38:07 +02:00
#include "builtins.h"
2024-04-30 12:36:16 +02:00
#include "minishell.h"
2024-05-09 16:46:13 +02:00
#include "parsing.h"
#include "prompt.h"
2024-04-25 11:46:41 +02:00
void free_redir(void *redir_v)
{
t_redirection *redir;
redir = redir_v;
free(redir->file_name);
}
void free_token(void *token_v)
{
t_token *token;
token = token_v;
print_token(token);
free(token->argv);
ft_lstclear(&(token->redirection), free_redir);
free(token);
}
/*void print_cmd(t_cmd cmd)
2024-04-25 11:46:41 +02:00
{
2024-05-07 11:04:20 +02:00
ft_putendl_fd(cmd.cmd, 1);
while (*(cmd.argv))
2024-04-27 17:39:26 +02:00
{
2024-05-07 11:04:20 +02:00
ft_putendl_fd(*(cmd.argv), 1);
(cmd.argv)++;
2024-04-27 17:39:26 +02:00
}
}*/
2024-04-25 11:46:41 +02:00
2024-05-09 16:46:13 +02:00
void print_pipe(t_list *pipe)
{
t_list *tmp;
tmp = pipe;
while (tmp)
2024-05-09 16:46:13 +02:00
{
ft_putendl_fd(tmp->content, STDOUT_FILENO);
2024-05-09 16:46:13 +02:00
tmp = tmp->next;
}
}
void sig_c(int code)
{
(void)code;
ft_putchar_fd('\n', STDOUT_FILENO);
rl_on_new_line();
rl_replace_line("", 0);
rl_redisplay();
}
void free_cmd(void *content)
{
t_cmd *cmd;
cmd = (t_cmd *)content;
free(cmd->cmd);
ft_free("a", &(cmd->argv));
free(cmd);
}
2024-04-25 11:46:41 +02:00
int main(int ac, char **av, char **env)
{
char *rl;
2024-05-02 15:50:58 +02:00
char *prompt;
t_env env_l;
t_list *cmd_list;
t_list *piped;
2024-04-25 11:46:41 +02:00
(void)ac;
get_program_name(av[0]);
if (env_init(env, &env_l))
return (EXIT_FAILURE);
signal(SIGINT, &sig_c);
2024-04-25 11:46:41 +02:00
while (1)
{
prompt = get_prompt(env_l);
rl = readline(prompt);
2024-05-02 15:54:16 +02:00
free(prompt);
if (!rl)
exit(727);
if (!*rl)
continue ;
if (check_syntax(rl))
continue ;
if (check_quote(rl))
continue ;
if (check_pipe(rl))
continue ;
piped = tokenizer(rl);
if (check_argv(piped))
continue ;
add_history(rl);
cmd_list = get_cmd_list(piped);
ft_lstclear(&piped, &free_token);
format_quotes(cmd_list);
exec_split_cmd(cmd_list, &env_l);
ft_lstclear(&cmd_list, &free_cmd);
free(rl);
2024-04-25 11:46:41 +02:00
}
return (0);
}