diff --git a/src/postprocess/redirections/redirection_parsing.c b/src/postprocess/redirections/redirection_parsing.c index 28cbfa4..78962e4 100644 --- a/src/postprocess/redirections/redirection_parsing.c +++ b/src/postprocess/redirections/redirection_parsing.c @@ -77,7 +77,7 @@ struct s_simple_cmd *parse_redirections(struct s_simple_cmd *cmd) while (wordlist_get(cmd->words, i) != NULL) { type = redir_type_from_worddesc(wordlist_get(cmd->words, i)); - if (type == REDIR_OUTPUT) + if (type != REDIR_INVALID) if (redirection_found(cmd, i, type) == NULL) return (NULL); i++; diff --git a/src/postprocess/redirections/redirection_type.c b/src/postprocess/redirections/redirection_type.c index 3a6ddf5..46ef148 100644 --- a/src/postprocess/redirections/redirection_type.c +++ b/src/postprocess/redirections/redirection_type.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/07 14:56:29 by khais #+# #+# */ -/* Updated: 2025/03/07 14:57:32 by khais ### ########.fr */ +/* Updated: 2025/03/10 16:51:39 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,5 +17,11 @@ t_redir_type redir_type_from_worddesc(t_worddesc *word) { if (ft_strcmp(">", word->word) == 0) return (REDIR_OUTPUT); + if (ft_strcmp("<", word->word) == 0) + return (REDIR_INPUT); + if (ft_strcmp("<<", word->word) == 0) + return (REDIR_HERE_DOC); + if (ft_strcmp(">>", word->word) == 0) + return (REDIR_APPEND); return (REDIR_INVALID); } diff --git a/tests/test_redirection_parsing.c b/tests/test_redirection_parsing.c index 830b6d7..cd6d002 100644 --- a/tests/test_redirection_parsing.c +++ b/tests/test_redirection_parsing.c @@ -104,6 +104,55 @@ static void test_redirection_parsing_null(void) do_leak_check(); } +static void test_redirection_parsing_input_in_middle(void) +{ + ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); + t_simple_cmd *cmd = parse_simple_cmd("echo hello words->word->word); + assert_strequal("hello", cmd->words->next->word->word); + assert_strequal("world", cmd->words->next->next->word->word); + assert(NULL == cmd->words->next->next->next); + assert_strequal("outfile", cmd->redirections->redirection->marker->word); + assert(cmd->redirections->redirection->type == REDIR_INPUT); + assert(cmd->redirections->next == NULL); + simple_cmd_destroy(cmd); + do_leak_check(); +} + +static void test_redirection_parsing_here_doc_in_middle(void) +{ + ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); + t_simple_cmd *cmd = parse_simple_cmd("echo hello <words->word->word); + assert_strequal("hello", cmd->words->next->word->word); + assert_strequal("world", cmd->words->next->next->word->word); + assert(NULL == cmd->words->next->next->next); + assert_strequal("outfile", cmd->redirections->redirection->marker->word); + assert(cmd->redirections->redirection->type == REDIR_HERE_DOC); + assert(cmd->redirections->next == NULL); + simple_cmd_destroy(cmd); + do_leak_check(); +} + + +static void test_redirection_parsing_append_in_middle(void) +{ + ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); + t_simple_cmd *cmd = parse_simple_cmd("echo hello >> outfile world"); + assert(parse_redirections(cmd) != NULL); + assert_strequal("echo", cmd->words->word->word); + assert_strequal("hello", cmd->words->next->word->word); + assert_strequal("world", cmd->words->next->next->word->word); + assert(NULL == cmd->words->next->next->next); + assert_strequal("outfile", cmd->redirections->redirection->marker->word); + assert(cmd->redirections->redirection->type == REDIR_APPEND); + assert(cmd->redirections->next == NULL); + simple_cmd_destroy(cmd); + do_leak_check(); +} + int main(void) { test_redirection_parsing_no_redirections(); @@ -112,5 +161,8 @@ int main(void) { test_redirection_parsing_output_in_middle(); test_redirection_parsing_output_no_filename_at_end(); test_redirection_parsing_null(); + test_redirection_parsing_input_in_middle(); + test_redirection_parsing_here_doc_in_middle(); + test_redirection_parsing_append_in_middle(); return (0); }