diff --git a/include/minishell.h b/include/minishell.h index 7d4c0e5..4a3b4ff 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -16,7 +16,9 @@ # include # include +# include # include +# include # include # include # include @@ -24,17 +26,15 @@ # include # include # include +# include # include "libft.h" +# include "error_msg.h" # include "env.h" +# include "prompt.h" # include "parsing.h" # include "execution.h" -char set_env(char **env, const char *name, char *content); -bool is_str(char *src, char *dst); -void print_cmd(t_cmd *cmd); - - void free_redir(void *redir_v); void free_token(void *token_v); void free_cmd(void *content); diff --git a/include/parsing.h b/include/parsing.h index a11f897..14b7539 100644 --- a/include/parsing.h +++ b/include/parsing.h @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/04/25 12:20:26 by adjoly #+# #+# */ -/* Updated: 2024/07/06 18:06:23 by adjoly ### ########.fr */ +/* Updated: 2024/07/07 14:37:59 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,22 +33,46 @@ typedef enum s_quote DOUBLE = 34 } t_quote; -bool check_syntax(char *readline); -void send_error(char *msg, char **argv); -bool check_redir(t_list *redir); -bool check_argv(t_list *token); t_cmd *get_redir_fd(void *content, t_env *env); t_list *get_cmd_list(t_list *list, t_env *env); void open_redir(t_redirection *redir, t_cmd *cmd, t_redir_sign sign[2]); -char **split_argv(char *readline); -char *env_var_replace(char *readline, t_env *env); -size_t get_size_with_env(char *readline, t_env *env); + +/** + * @brief Take a string and a character and return the lengh + * until the given character + * + * @param s The string + * @param The character the lengh stops at + * + * @return (size_t) The lengh until the given character + */ size_t strlen_till_char(char *s, int c); + + bool check_quote(char *readline); -t_quote __is_quote(char c); -char *search_for_next_quote(char *s, t_quote quote_type); +bool check_syntax(char *readline); +bool check_redir(t_list *redir); +bool check_argv(t_list *token); +/** + * @brief Take the readline output and check if all the pipe + * a command after them + * + * @param The readline output + * + * @return (bool) A boolean of whether or not there is an error + */ bool check_pipe(char *readline); + +/** + * @brief Take the readline output and split it into an argv + * while taking into account quotes + * + * @param The readline output + * + * @return (char **) The argv of the command + */ char **split_argv(char *readline); + /** * @brief Take the argv of a command a split the argv and the * command it self @@ -81,12 +105,35 @@ t_quote is_inquote(char *s, size_t i); */ t_quote __is_quote(char c); +/** + * @brief Take a string and a quote type and return the pointer to + * the next quote + * + * @param s A pointer to a string + * @param quote_type A type of quote + * + * @return (char *) A pointer to the next quote + */ +char *search_for_next_quote(char *s, t_quote quote_type); + +/** + * @brief Take the readline output and the env and replace all the + * $("variable") by their variable content + * + * @param readline The readline output + * @param env The env + * + * @return (char *) The curated string + */ +char *env_var_replace(char *readline, t_env *env); +size_t get_size_with_env(char *readline, t_env *env); + /* * ONLY FOR DEBUG TO BE REMOVED */ -void print_quote_type(t_quote type); -void print_redir_sign(t_redir_sign redir_sign); -void print_token(t_token *token); -void print_redir(t_redirection *redir); +void print_quote_type(t_quote type); +void print_redir_sign(t_redir_sign redir_sign); +void print_token(t_token *token); +void print_redir(t_redirection *redir); #endif diff --git a/src/exec/heredoc.c b/src/exec/heredoc.c index a77ebab..65d0c4e 100644 --- a/src/exec/heredoc.c +++ b/src/exec/heredoc.c @@ -6,24 +6,23 @@ /* By: mmoussou 0) - fd = open(path, O_RDONLY); + return (open(path, O_RDONLY)); else - fd = open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644); - return (fd); + return (open(path, O_WRONLY | O_TRUNC | O_CREAT, 0644)); } static int get_input(char *delimiter, int fd) @@ -60,12 +58,12 @@ static int get_input(char *delimiter, int fd) if (status == -1) fd_manager(fd); if (status == -1) - return (-1); + break ; status = write(fd, "\n", 1); if (status == -1) fd_manager(fd); if (status == -1) - return (-1); + break ; free(line); line = readline("heredoc> "); } diff --git a/src/main.c b/src/main.c index 8250be1..034dc6f 100644 --- a/src/main.c +++ b/src/main.c @@ -6,23 +6,11 @@ /* By: mmoussou -#include -#include -#include "error_msg.h" -#include -#include -#include -#include -#include "libft.h" -#include "builtins.h" #include "minishell.h" -#include "parsing.h" -#include "prompt.h" void sig_c(int code) { @@ -33,6 +21,19 @@ void sig_c(int code) rl_redisplay(); } +bool run_checks(char *rl) +{ + if (!*rl) + return (true); + if (check_syntax(rl)) + return (true); + if (check_quote(rl)) + return (true); + if (check_pipe(rl)) + return (true); + return (false); +} + int main(int ac, char **av, char **env) { char *rl; @@ -53,13 +54,7 @@ int main(int ac, char **av, char **env) free(prompt); if (!rl) exit(727); - if (!*rl) - continue ; - if (check_syntax(rl)) - continue ; - if (check_quote(rl)) - continue ; - if (check_pipe(rl)) + if (run_checks(rl)) continue ; piped = tokenizer(rl); if (check_argv(piped)) diff --git a/src/parsing/get_redir_fd.c b/src/parsing/get_redir_fd.c index a263365..33530a9 100644 --- a/src/parsing/get_redir_fd.c +++ b/src/parsing/get_redir_fd.c @@ -6,7 +6,7 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/30 10:48:41 by adjoly #+# #+# */ -/* Updated: 2024/07/06 18:06:14 by adjoly ### ########.fr */ +/* Updated: 2024/07/07 14:57:54 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */