From 47f449d804905e638b1c9341a57245df313c68ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Tue, 11 Mar 2025 13:41:27 +0100 Subject: [PATCH] here_doc: handle unexpected EOF --- src/executing/here_doc/here_doc.c | 9 +++++---- tests/here_doc_input_no_eof.input | 1 + tests/test_here_doc.c | 21 ++++++++++++++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 tests/here_doc_input_no_eof.input diff --git a/src/executing/here_doc/here_doc.c b/src/executing/here_doc/here_doc.c index 36efaa7..a0f1ac3 100644 --- a/src/executing/here_doc/here_doc.c +++ b/src/executing/here_doc/here_doc.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/07 11:42:29 by khais #+# #+# */ -/* Updated: 2025/03/11 13:31:15 by khais ### ########.fr */ +/* Updated: 2025/03/11 13:42:05 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -108,9 +108,10 @@ int here_doc(t_worddesc *marker, int infd) while (line != NULL) { if (is_marker(line, marker)) - line = NULL; - else - line = output_line_and_next(infd, outfd, line); + return (finalize(outfd, filename)); + line = output_line_and_next(infd, outfd, line); } + ft_dprintf(STDERR_FILENO, "minishell: warning: here-document delimited by \ +end-of-file (wanted `%s')\n", marker->word); return (finalize(outfd, filename)); } diff --git a/tests/here_doc_input_no_eof.input b/tests/here_doc_input_no_eof.input new file mode 100644 index 0000000..3f9177e --- /dev/null +++ b/tests/here_doc_input_no_eof.input @@ -0,0 +1 @@ +input diff --git a/tests/test_here_doc.c b/tests/test_here_doc.c index dd291cf..9f49899 100644 --- a/tests/test_here_doc.c +++ b/tests/test_here_doc.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/07 11:43:32 by khais #+# #+# */ -/* Updated: 2025/03/11 11:44:34 by khais ### ########.fr */ +/* Updated: 2025/03/11 13:37:00 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -86,10 +86,29 @@ static void test_here_doc_input_plus_end_marker(void) do_leak_check(); } +static void test_here_doc_input_no_end_marker(void) +{ + t_worddesc *marker; + int infile; + int result; + + ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__); + marker = worddesc_create(ft_strdup("EOF"), 0, NULL); + infile = open("./here_doc_input_no_eof.input", O_RDONLY); + result = here_doc(marker, infile); + close(infile); + worddesc_destroy(marker); + assert(result != -1); + assert_strequal("input\n", get_next_line(result)); + close(result); + do_leak_check(); +} + int main(void) { test_here_doc_filename_generation(); test_here_doc_invalid_args(); test_here_doc_only_end_marker(); test_here_doc_input_plus_end_marker(); + test_here_doc_input_no_end_marker(); return (0); }