diff --git a/Makefile b/Makefile index 85592be..cf1a5b6 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: mmoussou +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/20 20:25:06 by adjoly #+# #+# */ +/* Updated: 2024/05/20 21:00:45 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef ERROR_MSG_H +# define ERROR_MSG_H +/** + * Here we define all the error message + */ + +#endif diff --git a/include/parsing.h b/include/parsing.h index b24a40a..a6dc36d 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/05/19 13:45:01 by adjoly ### ########.fr */ +/* Updated: 2024/05/23 19:56:20 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,18 +24,52 @@ typedef struct s_cmd int outfile; } t_cmd; +typedef enum s_quote +{ + NOT_CLOSED = -1, + FALSE, + SINGLE, + DOUBLE +} t_quote; + /** - * @brief Take the argv of a command a split the argv and the command it self + * @brief Take the argv of a command a split the argv and the + * command it self * - * @param The full argv of the command + * @param cmd_av The full argv of the command * * @return (t_cmd *) cmd and argv splited into a struct */ t_cmd *split_cmd(char *cmd_av); -/* - * @deprecated +/** + * @brief Take a string and an index and check if the character + * at the index is in quote + * + * @param s The string to search in + * @param i The index of the character to check + * + * @return (t_quote) The type of quote if between, if not return FALSE or + * NOT_CLOSED if the quote is not closed */ -t_list *split_pipe(char *readline); +t_quote is_inquote(char *s, size_t i); + +/** + * @brief Take a character and check if it is a quote and return the + * type of quote + * + * @param c The character to check + * + * @return (t_quote) The type of quote or FALSE + */ +t_quote __is_quote(char c); + +/* + * ONLY FOR DEBUG TO BE REMOVED + */ +void print_quote_type(t_quote type); +void print_redir_sign(t_redirection_sign redir_sign); +void print_token(t_token *token); +void print_redir(t_redirection *redir); #endif diff --git a/include/tokenizer.h b/include/tokenizer.h index fcbcf53..3b2471f 100644 --- a/include/tokenizer.h +++ b/include/tokenizer.h @@ -6,20 +6,19 @@ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/05/18 20:14:15 by adjoly #+# #+# */ -/* Updated: 2024/05/19 14:02:58 by adjoly ### ########.fr */ +/* Updated: 2024/05/24 15:00:52 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef TOKENIZER_H +#ifndef TOKENIZER_H # define TOKENIZER_H # include "libft.h" -# include -typedef enum s_redirection_sign +typedef enum s_redirection_sign { - HEREDOC, INFILE, + HEREDOC, OUTFILE, OUT_APPEND, } t_redirection_sign; @@ -37,9 +36,27 @@ typedef struct s_token } t_token; /** - * @brief Convert the raw command into a t_token that contains the argv of the command an a linked list of redirection + * @brief Take a string and tell what type of redirect it is * - * @param cmd A string that containt the command to tokenize + * @param A string that contain a redirection sign + * + * @return (t_redirection_sign) The sign of the redirecition + */ +t_redirection_sign __to_redir_sign(char *redir_sign); + +/** + * @brief Take a string and split the filename and the redirect sign + * @param redir_s A string that contain a redirection + * + * @return (t_redirection) The splited redirection + */ +t_redirection *__to_redir(char *redir_s); + +/** + * @brief Convert the raw command into a t_token that contains + * the argv of the command an a linked list of redirection + * + * @param cmd A string that contain the command to tokenize * * @return (t_token *) The tokenized version of the command * @@ -47,16 +64,18 @@ typedef struct s_token t_token *__to_token(char *cmd); /** - * @brief ** + * @brief * - * @param + * @param The readline output * - * @return () + * @return (t_list *) A linked lst of all the command splited * */ +t_list *__split_pipe(char *readline); /** - * @brief Convert the readline output, split all command and put it in linked list of t_token (given by t_token function) + * @brief Convert the readline output, split all command and put + * it in linked list of t_token (given by t_token function) * * @param readline The readline output * diff --git a/libft/include/libft.h.gch b/libft/include/libft.h.gch new file mode 100644 index 0000000..f51c866 Binary files /dev/null and b/libft/include/libft.h.gch differ diff --git a/libft/src/linked_lists/ft_lstclear.c b/libft/src/linked_lists/ft_lstclear.c index 81d116c..8bb19a4 100644 --- a/libft/src/linked_lists/ft_lstclear.c +++ b/libft/src/linked_lists/ft_lstclear.c @@ -6,7 +6,7 @@ /* By: mmoussou next; ft_lstdelone(*lst, del); diff --git a/libft/src/str/ft_strjoin.c b/libft/src/str/ft_strjoin.c index 7511850..4b64541 100644 --- a/libft/src/str/ft_strjoin.c +++ b/libft/src/str/ft_strjoin.c @@ -6,7 +6,7 @@ /* By: mmoussou next) + while (tmp) { - print_cmd(*(t_cmd*)tmp->content); - return; - } - while (tmp) - { - print_cmd(*(t_cmd*)tmp->content); + ft_putendl_fd(tmp->content, STDOUT_FILENO); tmp = tmp->next; } } @@ -51,9 +47,10 @@ int main(int ac, char **av, char **env) { //ft_heredoc("EOF"); char *test; - char **lll; char *prompt; - t_list *cmd; + char **lll; + t_list *piped; + //t_token *token; (void)ac; (void)av; @@ -68,11 +65,24 @@ int main(int ac, char **av, char **env) if (!*lll) continue ; if (is_str(test, "exit")) - break ; - cmd = split_pipe(test); - print_pipe(cmd); + break; + piped = __split_pipe(test); + print_redir(__to_redir(piped->content)); + //free(token); + free(test); + ft_lstclear(&piped, &free); ft_free("a", &lll); } ft_free("a", &lll); return (0); } + +/*int main() +{ + char *ll = "asdf\"xf\"asfffd"; + t_quote d; + + d = is_inquote(ll, 6); + ft_printf("%c\n", *(ll+6)); + print_quote_type(d); +}*/ diff --git a/src/parsing/debug_print.c b/src/parsing/debug_print.c new file mode 100644 index 0000000..93dd116 --- /dev/null +++ b/src/parsing/debug_print.c @@ -0,0 +1,47 @@ +#include "parsing.h" + +void print_quote_type(t_quote type) +{ + if (type == SINGLE) + ft_putendl_fd("SINGLE", STDOUT_FILENO); + else if (type == DOUBLE) + ft_putendl_fd("DOUBLE", STDOUT_FILENO); + else if (type == FALSE) + ft_putendl_fd("FALSE", STDOUT_FILENO); + else if (type == NOT_CLOSED) + ft_putendl_fd("NOT_CLOSED", STDOUT_FILENO); +} + +void print_redir_sign(t_redirection_sign redir_sign) +{ + if (redir_sign == HEREDOC) + ft_putendl_fd("HEREDOC", STDOUT_FILENO); + else if (redir_sign == INFILE) + ft_putendl_fd("INFILE", STDOUT_FILENO); + else if (redir_sign == OUTFILE) + ft_putendl_fd("OUTFILE", STDOUT_FILENO); + else if (redir_sign == OUT_APPEND) + ft_putendl_fd("OUT_APPEND", STDOUT_FILENO); +} + +void print_redir(t_redirection *redir) +{ + ft_putstr_fd("file_name : ", STDOUT_FILENO); + if (redir->file_name) + ft_putendl_fd(redir->file_name, STDOUT_FILENO); + ft_putstr_fd("", STDOUT_FILENO); + print_redir_sign(redir->sign); +} + +void print_token(t_token *token) +{ + t_list *tmp; + + tmp = token->redirection; + while (tmp) + { + print_redir((t_redirection*)tmp->content); + tmp = tmp->next; + } + ft_putendl_fd(token->argv, STDOUT_FILENO); +} diff --git a/src/parsing/is_inquote.c b/src/parsing/is_inquote.c new file mode 100644 index 0000000..ade548f --- /dev/null +++ b/src/parsing/is_inquote.c @@ -0,0 +1,66 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* is_inquote.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/20 20:06:13 by adjoly #+# #+# */ +/* Updated: 2024/05/21 20:34:14 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parsing.h" +#include "libft.h" + +t_quote __is_quote(char c) +{ + if (c == 39) + return (SINGLE); + if (c == 34) + return (DOUBLE); + return (FALSE); +} + +char *search_for_next_quote(char *s, t_quote quote_type) +{ + char *tmp; + + tmp = s; + while (*tmp) + { + if (__is_quote(*tmp) == quote_type) + break ; + tmp++; + } + return (tmp); +} + +t_quote is_inquote(char *s, size_t i) +{ + char *tmp; + size_t start_quote; + t_quote quote_type; + + start_quote = 0; + tmp = s; + quote_type = FALSE; + while (*tmp) + { + if ((size_t)(tmp - s) > i) + break ; + if (__is_quote(*tmp) != FALSE) + { + start_quote = tmp - s; + quote_type = __is_quote(*tmp); + tmp = search_for_next_quote(tmp, quote_type); + tmp++; + if (*tmp && (start_quote < i && (size_t)(tmp - s) > i)) + return (quote_type); + else if (!*tmp) + return (NOT_CLOSED); + } + tmp++; + } + return (FALSE); +} diff --git a/src/parsing/tokenizer/__to_token.c b/src/parsing/tokenizer/__to_token.c new file mode 100644 index 0000000..4dc011d --- /dev/null +++ b/src/parsing/tokenizer/__to_token.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* __to_token.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/05/20 21:05:04 by adjoly #+# #+# */ +/* Updated: 2024/05/25 14:05:02 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "tokenizer.h" +#include "parsing.h" + +t_redirection_sign __to_redir_sign(char *redir_sign) +{ + if (redir_sign[0] == '<' && redir_sign[1] != '<') + return (INFILE); + else if (redir_sign[0] == '<' && redir_sign[1] == '<') + return (HEREDOC); + else if (redir_sign[0] == '>' && redir_sign[1] == '>') + return (OUT_APPEND); + else if (redir_sign[0] == '>' && redir_sign[1] != '>') + return (OUTFILE); + return (0); +} + +t_redirection *__to_redir(char *redir_s) +{ + t_redirection *redir; + char *tmp; + + redir = ft_calloc(sizeof(t_redirection), 1); + redir->sign = __to_redir_sign(redir_s); + if (redir->sign == HEREDOC || redir->sign == OUT_APPEND) + redir_s += 2; + else + redir_s++; + while (*redir_s && *redir_s == ' ') + redir_s++; + tmp = redir_s; + while (*tmp && ft_isalnum(*tmp)) + tmp++; + redir->file_name = ft_calloc(tmp - redir_s + 1, sizeof(char)); + ft_strlcpy(redir->file_name, redir_s, tmp - redir_s + 1); + return (redir); +} + +/*t_token *__to_token(char *cmd) +{ + char *tmp; + t_token *token; + t_redirection *tmp_redir; + char *argv; + + tmp = cmd; + argv = NULL; + token = ft_calloc(sizeof(t_token), 1); + while (*tmp) + { + if (*tmp == '<' || *tmp == '>') + { + tmp_redir = __to_redir(tmp); + ft_lstadd_back(&(token->redirection), ft_lstnew((void *)&tmp_redir)); + } + else + ft_strjoin_free_s1(argv, &(*tmp)); + tmp++; + } + return (token); +}*/ diff --git a/src/parsing/split_pipe.c b/src/parsing/tokenizer/tokenizer.c similarity index 56% rename from src/parsing/split_pipe.c rename to src/parsing/tokenizer/tokenizer.c index a25ac4b..f00191a 100644 --- a/src/parsing/split_pipe.c +++ b/src/parsing/tokenizer/tokenizer.c @@ -1,34 +1,25 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* split_pipe.c :+: :+: :+: */ +/* tokenizer.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: adjoly +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2024/05/07 13:26:40 by adjoly #+# #+# */ -/* Updated: 2024/05/18 17:12:42 by adjoly ### ########.fr */ +/* Created: 2024/05/18 20:13:50 by adjoly #+# #+# */ +/* Updated: 2024/05/20 22:51:45 by adjoly ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" -#include "parsing.h" +#include "tokenizer.h" -t_list *split_pipe(char *readline) +/*t_list *tokenizer(char *readline) { - char **split; - char **tmp; - t_list *list; - - split = ft_split(readline, '|'); - tmp = split; - if (!*(split+1)) - return (ft_lstnew((void *)readline)); - list = NULL; - while (tmp && *tmp) + t_list *token; + t_list *piped; + + piped = __split_pipe(readline); + while (piped && *piped) { - ft_lstadd_back(&list, ft_lstnew((void *)(*tmp))); - tmp++; + } - free(split); - return (list); -} +}*/