general improvement
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -4,6 +4,7 @@ ast.xml
|
||||
.$ast.xml.bkp
|
||||
.TEST_objs
|
||||
.mmoat_history
|
||||
.heredoc
|
||||
|
||||
file
|
||||
file2
|
||||
|
||||
@@ -27,18 +27,21 @@ typedef struct s_msh
|
||||
char **env;
|
||||
} t_msh;
|
||||
|
||||
|
||||
# include <stdio.h>
|
||||
# include <readline/readline.h>
|
||||
# include <readline/history.h>
|
||||
# include <unistd.h>
|
||||
# include <stdlib.h>
|
||||
# include <stdbool.h>
|
||||
# include <signal.h>
|
||||
|
||||
# include "../lib/libft/libft.h"
|
||||
# include "parser/ast.h"
|
||||
# include "parser/drawio.h"
|
||||
# include "parser/tokenizer.h"
|
||||
# include "parser/parsing.h"
|
||||
# include "parser/heredoc.h"
|
||||
# include "exec/builtins.h"
|
||||
# include "exec/env.h"
|
||||
# include "exec/exec.h"
|
||||
@@ -47,6 +50,10 @@ typedef struct s_msh
|
||||
t_msh *init_msh(char **envp);
|
||||
void free_msh(t_msh *msh);
|
||||
void free_child(t_msh *msh);
|
||||
void init_sig();
|
||||
void handle_sigint(int sig);
|
||||
void handle_sigquit(int sig);
|
||||
char *powerline(t_msh *msh);
|
||||
|
||||
# define POW1 "\033[1;38;2;21;22;26;48;2;92;106;178m"
|
||||
# define POW2 "\033[1;38;2;92;106;178;48;2;54;54;54m"
|
||||
|
||||
22
includes/parser/heredoc.h
Normal file
22
includes/parser/heredoc.h
Normal file
@@ -0,0 +1,22 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* heredoc.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/10 13:42:28 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/02/10 13:42:28 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef HEREDOC_H
|
||||
# define HEREDOC_H
|
||||
|
||||
# include "../minishell.h"
|
||||
|
||||
void read_hereinput(char *limiter);
|
||||
void parse_heredoc(char *limiter);
|
||||
void create_heredoc(t_node *lst);
|
||||
|
||||
#endif
|
||||
@@ -87,6 +87,7 @@ int ft_printf(const char *fstr, ...);
|
||||
int ft_fprintf(int fd, const char *str, ...);
|
||||
char *ft_sprintf(const char *str, ...);
|
||||
int ft_debug(const char *fstr, ...);
|
||||
char *rep_c(char c, int count);
|
||||
|
||||
char *get_next_line(int fd);
|
||||
|
||||
|
||||
23
lib/libft/srcs/print/rep_c.c
Normal file
23
lib/libft/srcs/print/rep_c.c
Normal file
@@ -0,0 +1,23 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* rep_c.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/10 10:19:40 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/02/10 10:19:40 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../libft.h"
|
||||
|
||||
char *rep_c(char c, int count)
|
||||
{
|
||||
char *out;
|
||||
|
||||
out = malloc(sizeof(char) * (count + 1));
|
||||
ft_memset(out, c, count);
|
||||
out[count] = 0;
|
||||
return (out);
|
||||
}
|
||||
@@ -71,61 +71,6 @@ void read_input(t_ast_n *node, int j)
|
||||
ft_fprintf(node->fds[1], "%s", buf);
|
||||
}
|
||||
|
||||
void read_hereinput(char *limiter)
|
||||
{
|
||||
char buf[100000];
|
||||
char c;
|
||||
int r;
|
||||
int i;
|
||||
|
||||
r = 0;
|
||||
i = 0;
|
||||
ft_fprintf(2, "heredoc> ");
|
||||
r = read(0, &c, 1);
|
||||
while (r && c != '\n' && c != '\0')
|
||||
{
|
||||
if (c != '\n' && c != '\0')
|
||||
buf[i++] = c;
|
||||
r = read(0, &c, 1);
|
||||
}
|
||||
buf[i] = '\0';
|
||||
if (ft_strncmp(buf, limiter, ft_strlen(limiter)) == 0)
|
||||
{
|
||||
ft_fprintf(1, "%s", buf);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
buf[i++] = '\n';
|
||||
buf[i] = '\0';
|
||||
ft_fprintf(1, "%s", buf);
|
||||
}
|
||||
|
||||
void parse_heredoc(char *limiter)
|
||||
{
|
||||
int fd;
|
||||
pid_t pid;
|
||||
|
||||
fd = open(".heredoc", O_WRONLY | O_CREAT | O_APPEND, 0666);
|
||||
pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
dup2(fd, STDOUT_FILENO);
|
||||
close (fd);
|
||||
while (1)
|
||||
read_hereinput(limiter);
|
||||
}
|
||||
else if (pid > 0)
|
||||
{
|
||||
waitpid(pid, NULL, 0);
|
||||
close (fd);
|
||||
}
|
||||
else
|
||||
{
|
||||
close (fd);
|
||||
perror("fork");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void here_doc(t_ast_n *node, int i)
|
||||
{
|
||||
pid_t pid;
|
||||
@@ -174,8 +119,8 @@ void handle_redir(t_ast_n *node)
|
||||
handle_file(node, 2, i);
|
||||
else if (node->redir[i] == _RED_DR)
|
||||
handle_file(node, 3, i);
|
||||
else if (node->redir[i] == _RED_DL)
|
||||
here_doc(node, i);
|
||||
// else if (node->redir[i] == _RED_DL)
|
||||
// here_doc(node, i);
|
||||
if (node->redir[i] == _RED_L)
|
||||
{
|
||||
dup2(node->_stdin, STDIN_FILENO);
|
||||
@@ -191,7 +136,7 @@ void handle_redir(t_ast_n *node)
|
||||
|
||||
int is_builtin(char *str)
|
||||
{
|
||||
if (ft_strncmp(str, "exit", -1) == 0)
|
||||
if (ft_strncmp(str, "exit", 4) == 0)
|
||||
return (1);
|
||||
else if (ft_strncmp(str, "pwd", 3) == 0)
|
||||
return (1);
|
||||
@@ -342,6 +287,8 @@ void exec_pcmd(t_ast_n *pcmd)
|
||||
{
|
||||
int ret;
|
||||
|
||||
if (!pcmd->cmd)
|
||||
exit(0);
|
||||
if (is_builtin(pcmd->cmd))
|
||||
{
|
||||
ret = exec_builtin(pcmd);
|
||||
|
||||
66
srcs/main.c
66
srcs/main.c
@@ -3,51 +3,33 @@
|
||||
/* ::: :::::::: */
|
||||
/* main.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/27 14:16:52 by lderidde #+# #+# */
|
||||
/* Updated: 2025/02/08 14:46:35 by lderidde ### ########.fr */
|
||||
/* Created: 2025/01/27 14:16:52 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/02/10 12:31:46 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/minishell.h"
|
||||
#include <readline/history.h>
|
||||
#include <readline/readline.h>
|
||||
|
||||
static void handle_input(char *in, char *li, char *pr, t_msh *msh)
|
||||
int g_sig = 0;
|
||||
|
||||
void handle_sigint(int sig)
|
||||
{
|
||||
if (ft_strlen(in) > 0 && !is_only_space(in))
|
||||
{
|
||||
add_history(in);
|
||||
ft_fprintf(msh->hist, "%s\n", in);
|
||||
}
|
||||
free(pr);
|
||||
free(li);
|
||||
(void)sig;
|
||||
g_sig = SIGINT;
|
||||
rl_replace_line("", 0);
|
||||
// rl_on_new_line();
|
||||
rl_done = 1;
|
||||
rl_redisplay();
|
||||
}
|
||||
|
||||
static char *powerline(t_msh *msh)
|
||||
void handle_sigquit(int sig)
|
||||
{
|
||||
char *pwd;
|
||||
char *tilt;
|
||||
char *input;
|
||||
char *prompt;
|
||||
char *line;
|
||||
|
||||
pwd = getcwd(NULL, 0);
|
||||
tilt = " ";
|
||||
if (ft_strncmp(pwd, "/home/", 6) == 0)
|
||||
{
|
||||
pwd += 6;
|
||||
while (*pwd && (*pwd) != '/')
|
||||
pwd ++;
|
||||
tilt = " ~";
|
||||
}
|
||||
line = ft_sprintf("%s----------------------------------------------\
|
||||
----------------------------------%s", POW5, RESET);
|
||||
prompt = ft_sprintf("%s\n%s MMOAT %s%s%s%s%s %s%s%s ",
|
||||
line, POW1, POW2, POW3, POW4, tilt, pwd, RESET, POW5, RESET);
|
||||
input = readline(prompt);
|
||||
handle_input(input, line, prompt, msh);
|
||||
return (input);
|
||||
(void)sig;
|
||||
// ft_printf("\b\b");
|
||||
}
|
||||
|
||||
static void add_prevhistory(t_msh *msh)
|
||||
@@ -75,6 +57,7 @@ static void interpret_cmd(char **input, t_msh *msh)
|
||||
return ;
|
||||
}
|
||||
msh->ex_code = execute_command(msh->head);
|
||||
unlink(".heredoc");
|
||||
free_ast(msh->head);
|
||||
msh->head = NULL;
|
||||
ft_free(input);
|
||||
@@ -84,9 +67,8 @@ int main(int ac, char **av, char **envp)
|
||||
{
|
||||
t_msh *msh;
|
||||
|
||||
(void)ac;
|
||||
(void)av;
|
||||
msh = init_msh(envp);
|
||||
init_sig();
|
||||
add_prevhistory(msh);
|
||||
if (!msh)
|
||||
return (1);
|
||||
@@ -94,8 +76,18 @@ int main(int ac, char **av, char **envp)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
while (!msh->input || !msh->input[0] || is_only_space(msh->input))
|
||||
msh->input = malloc (1);
|
||||
msh->input[0] = 0;
|
||||
while (!msh->input[0] || is_only_space(msh->input) || g_sig == SIGINT)
|
||||
{
|
||||
g_sig = 0;
|
||||
msh->input = powerline(msh);
|
||||
if (!msh->input)
|
||||
break;
|
||||
}
|
||||
if (!msh->input)
|
||||
break;
|
||||
if (g_sig != SIGINT)
|
||||
interpret_cmd(&msh->input, msh);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,8 +30,11 @@ static char **ft_setnewenv(void)
|
||||
t_msh *init_msh(char **envp)
|
||||
{
|
||||
t_msh *msh;
|
||||
int fd;
|
||||
|
||||
msh = malloc(sizeof(t_msh) * 1);
|
||||
fd = open(".heredoc", O_WRONLY | O_CREAT | O_TRUNC, 0666);
|
||||
close(fd);
|
||||
msh->hist = open(".mmoat_history", O_RDWR | O_CREAT | O_APPEND, 0666);
|
||||
msh->ex_code = 0;
|
||||
msh->input = NULL;
|
||||
|
||||
74
srcs/parsing/heredoc.c
Normal file
74
srcs/parsing/heredoc.c
Normal file
@@ -0,0 +1,74 @@
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
void read_hereinput(char *limiter)
|
||||
{
|
||||
char buf[100000];
|
||||
char c;
|
||||
int r;
|
||||
int i;
|
||||
|
||||
r = 0;
|
||||
i = 0;
|
||||
ft_fprintf(2, "heredoc> ");
|
||||
r = read(0, &c, 1);
|
||||
if (r == 0)
|
||||
{
|
||||
ft_fprintf (2, "etdsttdt\n");
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
while (r && c != '\n' && c != '\0')
|
||||
{
|
||||
if (c != '\n' && c != '\0')
|
||||
buf[i++] = c;
|
||||
r = read(0, &c, 1);
|
||||
}
|
||||
buf[i] = '\0';
|
||||
if (ft_strncmp(buf, limiter, ft_strlen(limiter)) == 0)
|
||||
{
|
||||
ft_fprintf(1, "%s", buf);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
buf[i++] = '\n';
|
||||
buf[i] = '\0';
|
||||
ft_fprintf(1, "%s", buf);
|
||||
}
|
||||
|
||||
void parse_heredoc(char *limiter)
|
||||
{
|
||||
int fd;
|
||||
pid_t pid;
|
||||
|
||||
fd = open(".heredoc", O_WRONLY | O_CREAT | O_APPEND, 0666);
|
||||
pid = fork();
|
||||
if (pid == 0)
|
||||
{
|
||||
dup2(fd, STDOUT_FILENO);
|
||||
close (fd);
|
||||
while (1)
|
||||
read_hereinput(limiter);
|
||||
}
|
||||
else if (pid > 0)
|
||||
{
|
||||
waitpid(pid, NULL, 0);
|
||||
close (fd);
|
||||
}
|
||||
else
|
||||
{
|
||||
close (fd);
|
||||
perror("fork");
|
||||
exit (EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void create_heredoc(t_node *lst)
|
||||
{
|
||||
while (lst)
|
||||
{
|
||||
if (lst->pressision == HEREDOC && lst->next && lst->next->pressision)
|
||||
{
|
||||
lst = lst->next;
|
||||
parse_heredoc(lst->val);
|
||||
}
|
||||
lst = lst->next;
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,7 @@ t_ast_n *parser(char *input, t_msh *msh)
|
||||
lst = tokenize(input);
|
||||
if (!lst)
|
||||
return (NULL);
|
||||
create_heredoc(lst);
|
||||
if (DEBUG)
|
||||
{
|
||||
dio = drawio_init("ast.xml");
|
||||
|
||||
@@ -109,13 +109,13 @@ void debug_token_list(t_node* lst, char *msg)
|
||||
cpy = lst;
|
||||
if (DEBUG)
|
||||
{
|
||||
ft_debug("========================={%s}\n", msg);
|
||||
ft_debug("==================================================================={%s}\n", msg);
|
||||
while (cpy)
|
||||
{
|
||||
ft_debug("|%s|\n", cpy->val);
|
||||
ft_fprintf(2, "| %10s | TOKEN : %3d | PRESSISION : %3d |\n", cpy->val, cpy->token, cpy->pressision);
|
||||
cpy = cpy->next;
|
||||
}
|
||||
ft_debug("=========================\n\n");
|
||||
ft_debug("===================================================================\n\n");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
50
srcs/powerline.c
Normal file
50
srcs/powerline.c
Normal file
@@ -0,0 +1,50 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* powerline.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/10 12:19:10 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/02/10 12:19:10 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/minishell.h"
|
||||
|
||||
static void handle_input(char *in, char *li, char *pr, t_msh *msh)
|
||||
{
|
||||
if (ft_strlen(in) > 0 && !is_only_space(in))
|
||||
{
|
||||
add_history(in);
|
||||
ft_fprintf(msh->hist, "%s\n", in);
|
||||
}
|
||||
free(pr);
|
||||
free(li);
|
||||
}
|
||||
|
||||
char *powerline(t_msh *msh)
|
||||
{
|
||||
char *pwd;
|
||||
char *tilt;
|
||||
char *input;
|
||||
char *prompt;
|
||||
char *line;
|
||||
|
||||
pwd = getcwd(NULL, 0);
|
||||
tilt = " ";
|
||||
if (ft_strncmp(pwd, "/home/", 6) == 0)
|
||||
{
|
||||
pwd += 6;
|
||||
while (*pwd && (*pwd) != '/')
|
||||
pwd ++;
|
||||
tilt = " ~";
|
||||
}
|
||||
line = ft_sprintf("%s----------------------------------------------\
|
||||
----------------------------------%s", POW5, RESET);
|
||||
prompt = ft_sprintf("%s\n%s MMOAT %s%s%s%s%s %s%s%s ",
|
||||
line, POW1, POW2, POW3, POW4, tilt, pwd, RESET, POW5, RESET);
|
||||
input = readline(prompt);
|
||||
handle_input(input, line, prompt, msh);
|
||||
return (input);
|
||||
}
|
||||
32
srcs/sig.c
Normal file
32
srcs/sig.c
Normal file
@@ -0,0 +1,32 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* sig.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/10 08:32:20 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/02/10 08:32:20 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../includes/minishell.h"
|
||||
#include <readline/chardefs.h>
|
||||
|
||||
void init_sig()
|
||||
{
|
||||
int i;
|
||||
struct sigaction sa[2];
|
||||
|
||||
i = -1;
|
||||
while (++i < 2)
|
||||
{
|
||||
ft_memset(&(sa[i]), 0, sizeof(sa[i]));
|
||||
(sa[i]).sa_flags = SA_RESTART;
|
||||
sigemptyset(&((sa[i]).sa_mask));
|
||||
}
|
||||
(sa[0]).sa_handler = handle_sigint;
|
||||
sigaction(SIGINT, &(sa[0]), NULL);
|
||||
(sa[1]).sa_handler = handle_sigquit;
|
||||
sigaction(SIGQUIT, &(sa[1]), NULL);
|
||||
}
|
||||
Reference in New Issue
Block a user