From abf37eb7f5003894ec56d1b88bdd8dc5fd8c8225 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Thu, 20 Feb 2025 14:22:38 +0100 Subject: [PATCH] wordsplit: set quote and dquote flags --- src/parser/wordsplit/tokenizing_1_5.c | 5 ++- src/parser/wordsplit/wordsplit.c | 8 +--- tests/word_splitting.c | 58 +++++++++++++++++++++++++-- 3 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/parser/wordsplit/tokenizing_1_5.c b/src/parser/wordsplit/tokenizing_1_5.c index 5c8ae0e..e7d56a6 100644 --- a/src/parser/wordsplit/tokenizing_1_5.c +++ b/src/parser/wordsplit/tokenizing_1_5.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/19 13:20:01 by jguelen #+# #+# */ -/* Updated: 2025/02/20 14:10:57 by khais ### ########.fr */ +/* Updated: 2025/02/20 14:17:03 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -78,6 +78,9 @@ bool rule_quote(t_token_build *builder, char *original) if (is_quote(original[builder->idx])) { quote_flip(builder, original[builder->idx]); + builder->cur_flags |= W_QUOTED; + if (builder->quote == '"') + builder->cur_flags |= W_DQUOTE; builder->idx++; return (true); } diff --git a/src/parser/wordsplit/wordsplit.c b/src/parser/wordsplit/wordsplit.c index 78d5e3b..798aa31 100644 --- a/src/parser/wordsplit/wordsplit.c +++ b/src/parser/wordsplit/wordsplit.c @@ -5,18 +5,14 @@ /* +:+ +:+ +:+ */ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2025/02/20 14:10/48 by khais #+# #+# */ -/* Updated: 2025/02/20 14:10:48 by khais ### ########.fr */ +/* Created: 2025/02/20 15:39/01 by khais #+# #+# */ +/* Updated: 2025/02/20 15:39:01 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include "wordsplit.h" #include "libft.h" -/* -** TODO: set flags -*/ - /* ** split a string into words, respecting quotes etc. ** diff --git a/tests/word_splitting.c b/tests/word_splitting.c index 8b9fdb2..a74f76b 100644 --- a/tests/word_splitting.c +++ b/tests/word_splitting.c @@ -6,15 +6,13 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/13 15:17:56 by khais #+# #+# */ -/* Updated: 2025/02/20 14:05:21 by khais ### ########.fr */ +/* Updated: 2025/02/20 14:22:06 by khais ### ########.fr */ /* */ /* ************************************************************************** */ #include #include "testutil.h" -#include "libft.h" #include "../src/parser/wordsplit/wordsplit.h" -#include "unistd.h" #include /* @@ -187,7 +185,58 @@ static void test_wordsplit_var_substitution(void) assert_strequal("here", wordlist_get(words, 2)->word); assert(0 == wordlist_get(words, 2)->flags); wordlist_destroy(words); +} +static void test_wordsplit_quote_detection_nonnested(void) +{ + t_wordlist *words; + + words = minishell_wordsplit("echo 'single quotes' here \"double quotes\" here"); + assert_strequal("echo", wordlist_get(words, 0)->word); + assert(0 == wordlist_get(words, 0)->flags); + assert_strequal("'single quotes'", wordlist_get(words, 1)->word); + assert(W_QUOTED == wordlist_get(words, 1)->flags); + assert_strequal("here", wordlist_get(words, 2)->word); + assert(0 == wordlist_get(words, 2)->flags); + assert_strequal("\"double quotes\"", wordlist_get(words, 3)->word); + assert((W_QUOTED | W_DQUOTE) == wordlist_get(words, 3)->flags); + assert_strequal("here", wordlist_get(words, 4)->word); + assert(0 == wordlist_get(words, 4)->flags); + wordlist_destroy(words); +} + +static void test_wordsplit_quote_detection_nested_double_in_simple(void) +{ + t_wordlist *words; + char *str; + + str = "'these are single quotes \"with double\" inside'"; + words = minishell_wordsplit(str); + assert_strequal(str, wordlist_get(words, 0)->word); + assert(W_QUOTED == wordlist_get(words, 0)->flags); + wordlist_destroy(words); + str = "'\"these are single quotes with double inside\"'"; + words = minishell_wordsplit(str); + assert_strequal(str, wordlist_get(words, 0)->word); + assert(W_QUOTED == wordlist_get(words, 0)->flags); + wordlist_destroy(words); +} + +static void test_wordsplit_quote_detection_nested_single_in_double(void) +{ + t_wordlist *words; + char *str; + + str = "\"these are double quotes 'with single' inside\""; + words = minishell_wordsplit(str); + assert_strequal(str, wordlist_get(words, 0)->word); + assert((W_QUOTED | W_DQUOTE) == wordlist_get(words, 0)->flags); + wordlist_destroy(words); + str = "\"'these are double quotes with single inside'\""; + words = minishell_wordsplit(str); + assert_strequal(str, wordlist_get(words, 0)->word); + assert((W_QUOTED | W_DQUOTE) == wordlist_get(words, 0)->flags); + wordlist_destroy(words); } int main(void) { @@ -205,5 +254,8 @@ int main(void) { test_wordsplit_all_operators(); test_wordsplit_operator_combining(); test_wordsplit_var_substitution(); + test_wordsplit_quote_detection_nonnested(); + test_wordsplit_quote_detection_nested_double_in_simple(); + test_wordsplit_quote_detection_nested_single_in_double(); return (0); }