This commit is contained in:
Loic Deridder
2025-02-12 13:48:54 +01:00
parent 256724e601
commit 3ac1625f5a
12 changed files with 89 additions and 61 deletions

View File

@@ -56,7 +56,7 @@ typedef struct s_msh
t_msh *init_msh(char **envp); t_msh *init_msh(char **envp);
void free_msh(t_msh *msh); void free_msh(t_msh *msh);
void free_child(t_msh *msh); void free_child(t_msh *msh);
void init_sig(); void init_sig(void);
void handle_sigint(int sig); void handle_sigint(int sig);
void handle_sigquit(int sig); void handle_sigquit(int sig);
char *powerline(t_msh *msh); char *powerline(t_msh *msh);

View File

@@ -13,6 +13,8 @@
#ifndef AST_H #ifndef AST_H
# define AST_H # define AST_H
# include <unistd.h>
typedef struct s_node t_node; typedef struct s_node t_node;
typedef struct s_msh t_msh; typedef struct s_msh t_msh;
@@ -50,7 +52,7 @@ typedef struct s_ast_n
int _stdin; int _stdin;
int save_stdo; int save_stdo;
int save_stdi; int save_stdi;
t_redir *redir; t_redir *redir;
char *input; char *input;
char **files; char **files;
bool sh; bool sh;
@@ -58,16 +60,16 @@ typedef struct s_ast_n
typedef struct s_nodell typedef struct s_nodell
{ {
t_node *node; t_node *node;
struct s_nodell *next; struct s_nodell *next;
} t_nodell; } t_nodell;
t_ast_n *get_ast(t_msh *msh, t_node *lst); t_ast_n *get_ast(t_msh *msh, t_node *lst);
t_nodell *cutll(t_node *lst, t_node *expected, size_t limiter); t_nodell *cutll(t_node *lst, t_node *expected, size_t limiter);
t_node *get_top_token(t_node *lst, t_state *state); t_node *get_top_token(t_node *lst, t_state *state);
// recurce // recurce
t_ast_n *create_ast_n(t_node *lst, t_ast_n *parent, t_msh *msh, bool subsh); t_ast_n *create_ast_n(t_node *lst, t_ast_n *parent, t_msh *msh, bool subsh);
// redir // redir
t_redir get_redir(t_node *node); t_redir get_redir(t_node *node);
void create_redir(t_node *cpy, t_ast_n *self); void create_redir(t_node *cpy, t_ast_n *self);
@@ -75,14 +77,15 @@ void create_redir_subsh(t_node *head, t_ast_n *self);
// cmd // cmd
void create_cmd(t_ast_n *self, t_node *lst); void create_cmd(t_ast_n *self, t_node *lst);
// subsh // subsh
void create_subsh(t_ast_n *parent, t_node *lst, t_msh *msh); void create_subsh(t_ast_n *parent, t_node *lst, t_msh *msh);
// pipeline // pipeline
void create_pline(t_ast_n *self, t_node *lst, t_node *token, t_msh *msh); void create_pline(t_ast_n *self, t_node *lst, t_node *token, t_msh *msh);
// and_or // and_or
void create_and_or(t_ast_n *parrent, t_node *lst, t_node *token, t_msh *msh); void create_and_or(t_ast_n *parrent, t_node *lst, t_node *token,
t_msh *msh);
// free // free
void free_ast(t_ast_n *node); void free_ast(t_ast_n *node);
void free_lltab(t_nodell *nodell); void free_lltab(t_nodell *nodell);
#endif #endif

View File

@@ -33,12 +33,13 @@ typedef struct s_elems
} t_elems; } t_elems;
// internal // internal
char *replace_ampercent(char *src); char *replace_ampercent(char *src);
char *replace_left_red(char *src); char *replace_left_red(char *src);
t_dio_node get_cmd_txt(t_ast_n *node); t_dio_node get_cmd_txt(t_ast_n *node);
int print_ast(t_ast_n *node, t_elems *e, int fd); int print_ast(t_ast_n *node, t_elems *e, int fd);
// external // external
void gen_dio_linked_list(t_node *head, int fd); void gen_dio_linked_list(t_node *head, int fd);
void gen_dio_ast(t_ast_n *head, int fd); void gen_dio_ast(t_ast_n *head, int fd);
#endif #endif

View File

@@ -13,7 +13,6 @@
#ifndef HEREDOC_H #ifndef HEREDOC_H
# define HEREDOC_H # define HEREDOC_H
typedef struct s_node t_node; typedef struct s_node t_node;
typedef struct s_msh t_msh; typedef struct s_msh t_msh;

View File

@@ -17,10 +17,12 @@ typedef struct s_node t_node;
typedef struct s_ast_n t_ast_n; typedef struct s_ast_n t_ast_n;
typedef struct s_msh t_msh; typedef struct s_msh t_msh;
t_ast_n *parser(char *input, t_msh *msh); t_ast_n *parser(char *input, t_msh *msh);
int unexpected_token(t_node *node); int unexpected_token(t_node *node);
int is_aop_operator(t_node *node); int is_aop_operator(t_node *node);
void interpret_cmd(char **input, t_msh *msh); void interpret_cmd(char **input, t_msh *msh);
void end_heredoc(char *buf, t_msh *msh, t_node *lst);
void exit_heredoc(char *limiter, t_msh *msh, t_node *lst);
#endif #endif

View File

@@ -46,23 +46,25 @@ typedef struct s_node
enum e_pres pressision; enum e_pres pressision;
} t_node; } t_node;
t_node *tokenize(char *str); t_node *tokenize(char *str);
t_node *create_node(char *val, t_token token); t_node *create_node(char *val, t_token token);
int add_node_back(t_node **head, char *val, t_token token, t_pres pres); int add_node_back(t_node **head, char *val,
int merge_with_next_node(t_node *node); t_token token, t_pres pres);
void free_linked_list(t_node *stack); int merge_with_next_node(t_node *node);
t_token get_token(char *str); void free_linked_list(t_node *stack);
t_pres get_pressision(char *s, t_token token, t_token last_token, t_pres last_pres); t_token get_token(char *str);
int create_node_after(t_node *elem, char *val); t_pres get_pressision(char *s, t_token token,
int is_meta(char c); t_token last_token, t_pres last_pres);
int is_sticked(char *val); int create_node_after(t_node *elem, char *val);
int trim_nodes(t_node *head); int is_meta(char c);
void debug_linked_list(t_node *head, char *msg); int is_sticked(char *val);
int find_quote_node(t_node *head, char q); int trim_nodes(t_node *head);
int syntax_error(t_node *head); void debug_linked_list(t_node *head, char *msg);
char *copy_meta(char *val, int *copied); int find_quote_node(t_node *head, char q);
char *copy_unmeta(char *val, int *copied); int syntax_error(t_node *head);
void debug_token_list(t_node* lst, char *msg); char *copy_meta(char *val, int *copied);
void set_token(t_node *head); char *copy_unmeta(char *val, int *copied);
void debug_token_list(t_node *lst, char *msg);
void set_token(t_node *head);
#endif #endif

View File

@@ -14,7 +14,7 @@
int execute_command(t_ast_n *node) int execute_command(t_ast_n *node)
{ {
if (!node || (node->state == _CMD && node->cmd == NULL)) if (!node || (node->state == _CMD && node->cmd == NULL))
return (0); return (0);
if (node->state == _CMD) if (node->state == _CMD)
handle_redir(node); handle_redir(node);

View File

@@ -13,6 +13,7 @@
#include "../includes/minishell.h" #include "../includes/minishell.h"
#include <readline/history.h> #include <readline/history.h>
#include <readline/readline.h> #include <readline/readline.h>
#include <termios.h>
static void add_prevhistory(t_msh *msh) static void add_prevhistory(t_msh *msh)
{ {
@@ -49,13 +50,13 @@ int interactive_mode(t_msh *msh)
int main(int ac, char **av, char **envp) int main(int ac, char **av, char **envp)
{ {
t_msh *msh; t_msh *msh;
struct termios term; struct termios term;
(void)av; (void)av;
tcgetattr(STDIN_FILENO, &term); tcgetattr(STDIN_FILENO, &term);
term.c_cc[VQUIT] = _POSIX_VDISABLE; term.c_cc[VQUIT] = _POSIX_VDISABLE;
tcsetattr(STDIN_FILENO, TCSANOW, &term); tcsetattr(STDIN_FILENO, TCSANOW, &term);
msh = init_msh(envp); msh = init_msh(envp);
init_sig(); init_sig();
add_prevhistory(msh); add_prevhistory(msh);

View File

@@ -72,13 +72,7 @@ void read_hereinput(char *limiter, t_node *lst, t_msh *msh)
ft_fprintf(2, "heredoc> "); ft_fprintf(2, "heredoc> ");
r = read(0, &c, 1); r = read(0, &c, 1);
if (r == 0) if (r == 0)
{ exit_heredoc(limiter, msh, lst);
ft_fprintf(2, "\n");
ft_fprintf(1, "%s\n", limiter);
free_linked_list(lst);
free_msh(msh);
exit(EXIT_SUCCESS);
}
while (r && c != '\n' && c != '\0') while (r && c != '\n' && c != '\0')
{ {
if (c != '\n' && c != '\0') if (c != '\n' && c != '\0')
@@ -87,12 +81,7 @@ void read_hereinput(char *limiter, t_node *lst, t_msh *msh)
} }
buf[i] = '\0'; buf[i] = '\0';
if (ft_strncmp(buf, limiter, ft_strlen(limiter)) == 0) if (ft_strncmp(buf, limiter, ft_strlen(limiter)) == 0)
{ end_heredoc(buf, msh, lst);
ft_fprintf(1, "%s\n", buf);
free_msh(msh);
free_linked_list(lst);
exit(EXIT_SUCCESS);
}
buf[i++] = '\n'; buf[i++] = '\n';
buf[i] = '\0'; buf[i] = '\0';
ft_fprintf(1, "%s", buf); ft_fprintf(1, "%s", buf);
@@ -128,7 +117,7 @@ void parse_heredoc(char *limiter, t_node *lst, t_msh *msh)
void create_heredoc(t_node *lst, t_msh *msh) void create_heredoc(t_node *lst, t_msh *msh)
{ {
t_node *tmp; t_node *tmp;
tmp = lst; tmp = lst;
while (lst) while (lst)

View File

@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* heredoc_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/12 13:41:03 by lderidde #+# #+# */
/* Updated: 2025/02/12 13:41:03 by lderidde ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/minishell.h"
void exit_heredoc(char *limiter, t_msh *msh, t_node *lst)
{
ft_fprintf(2, "\n");
ft_fprintf(1, "%s\n", limiter);
free_linked_list(lst);
free_msh(msh);
exit(EXIT_SUCCESS);
}
void end_heredoc(char *buf, t_msh *msh, t_node *lst)
{
ft_fprintf(1, "%s\n", buf);
free_msh(msh);
free_linked_list(lst);
exit(EXIT_SUCCESS);
}

View File

@@ -11,6 +11,7 @@
/* ************************************************************************** */ /* ************************************************************************** */
#include "../includes/minishell.h" #include "../includes/minishell.h"
#include <sys/ioctl.h>
static void handle_input(char *in, t_msh *msh) static void handle_input(char *in, t_msh *msh)
{ {
@@ -50,16 +51,17 @@ char *get_pwd(void)
char *powerline(t_msh *msh) char *powerline(t_msh *msh)
{ {
char *pwd; char *pwd;
char *input; char *input;
char *prompt; char *prompt;
char *separator; char *separator;
struct winsize w; struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
pwd = get_pwd(); pwd = get_pwd();
separator = rep_c('-', w.ws_col); separator = rep_c('-', w.ws_col);
prompt = ft_sprintf("%s%s%s\n%s  MMOAT %s%s %s%s %s%s%s ", SEP, separator, RESET, POW1, POW2, POW3, prompt = ft_sprintf("%s%s%s\n%s  MMOAT %s%s %s%s %s%s%s ",
SEP, separator, RESET, POW1, POW2, POW3,
POW4, pwd, RESET, POW5, RESET); POW4, pwd, RESET, POW5, RESET);
input = readline(prompt); input = readline(prompt);
handle_input(input, msh); handle_input(input, msh);

View File

@@ -43,5 +43,4 @@ void handle_sigint(int sig)
void handle_sigquit(int sig) void handle_sigquit(int sig)
{ {
(void)sig; (void)sig;
// ft_printf("\b\b");
} }