diff --git a/src/ft_errno.c b/src/ft_errno.c index 2a74b6b..69aa835 100644 --- a/src/ft_errno.c +++ b/src/ft_errno.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/21 12:40:46 by khais #+# #+# */ -/* Updated: 2025/02/21 15:38:54 by khais ### ########.fr */ +/* Updated: 2025/03/10 16:56:52 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,6 +51,8 @@ char *ft_strerror(t_errno errno) [FT_EINVAL] = "Invalid argument", [FT_EBADID] = "Bad identifier", [FT_EUNEXPECTED_PIPE] = "minishell: syntax error near unexpected token `|'", + [FT_EMALFORMED_REDIRECTION] = "minishell: malformed redirection (perhaps a \ + missing filename)", }; if (errno >= 0 && errno < FT_EMAXERRNO) diff --git a/src/ft_errno.h b/src/ft_errno.h index 67941e9..f17cbaa 100644 --- a/src/ft_errno.h +++ b/src/ft_errno.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/21 12:40:58 by khais #+# #+# */ -/* Updated: 2025/02/21 15:37:56 by khais ### ########.fr */ +/* Updated: 2025/03/10 16:38:28 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,7 @@ typedef enum e_errno FT_EINVAL, FT_EBADID, FT_EUNEXPECTED_PIPE, + FT_EMALFORMED_REDIRECTION, FT_EMAXERRNO, } t_errno; diff --git a/src/postprocess/redirections/redirection_parsing.c b/src/postprocess/redirections/redirection_parsing.c index bb4ac78..21dae57 100644 --- a/src/postprocess/redirections/redirection_parsing.c +++ b/src/postprocess/redirections/redirection_parsing.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/07 12:30:04 by khais #+# #+# */ -/* Updated: 2025/03/10 16:11:46 by khais ### ########.fr */ +/* Updated: 2025/03/10 16:40:17 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,6 +15,7 @@ #include "redirection_list.h" #include #include "redirection_type.h" +#include "../../ft_errno.h" #include /* @@ -53,6 +54,8 @@ struct s_simple_cmd *parse_redirections(struct s_simple_cmd *cmd) { wordlist_destroy_idx(&cmd->words, i); marker = wordlist_pop_idx(&cmd->words, i); + if (marker == NULL) + return (ft_errno(FT_EMALFORMED_REDIRECTION), NULL); redirection = redirection_create(type, marker); if (redirection == NULL) return (NULL); @@ -60,8 +63,7 @@ struct s_simple_cmd *parse_redirections(struct s_simple_cmd *cmd) if (cmd->redirections == NULL) return (redirection_destroy(redirection), NULL); } - else - i++; + i++; } return (cmd); } diff --git a/tests/test_redirection_parsing.c b/tests/test_redirection_parsing.c index 36324f0..14eb42c 100644 --- a/tests/test_redirection_parsing.c +++ b/tests/test_redirection_parsing.c @@ -5,8 +5,8 @@ /* +:+ +:+ +:+ */ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/03/10 16:13/18 by khais #+# #+# */ -/* Updated: 2025/03/10 16:13:18 by khais ### ########.fr */ +/* Created: 2025/03/10 16:38/00 by khais #+# #+# */ +/* Updated: 2025/03/10 16:38:00 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,7 +16,9 @@ #include "../src/postprocess/redirections/redirection_parsing.h" #include "../src/postprocess/redirections/redirection_parsing.h" #include "../src/postprocess/redirections/redirection_list.h" +#include "../src/ft_errno.h" #include "ft_printf.h" +#include "stdio.h" #include "testutil.h" static t_simple_cmd *parse_simple_cmd(char *input) @@ -84,13 +86,24 @@ static void test_redirection_parsing_output_in_middle(void) do_leak_check(); } +static void test_redirection_parsing_output_no_filename_at_end(void) +{ + ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); + ft_errno(FT_ESUCCESS); + t_simple_cmd *cmd = parse_simple_cmd("echo hello world >"); + assert(parse_redirections(cmd) == NULL); + assert(FT_EMALFORMED_REDIRECTION == ft_errno_get()); + simple_cmd_destroy(cmd); + do_leak_check(); +} + int main(void) { test_redirection_parsing_no_redirections(); test_redirection_parsing_output_at_start(); test_redirection_parsing_output_at_end(); test_redirection_parsing_output_in_middle(); - // check operator alone without further tokens + test_redirection_parsing_output_no_filename_at_end(); // check null input return (0); }