From 2c5d5abdc43761f3057d37db84e3041823ab1550 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Thu, 6 Mar 2025 14:53:46 +0100 Subject: [PATCH] quote marking: no quotes lead to marker filled with spaces --- src/parser/worddesc/worddesc.c | 19 +++++++++++++++---- src/parser/worddesc/worddesc.h | 14 +++++++++++++- tests/word_splitting.c | 12 +++++++++++- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/src/parser/worddesc/worddesc.c b/src/parser/worddesc/worddesc.c index ce4facf..a385fe0 100644 --- a/src/parser/worddesc/worddesc.c +++ b/src/parser/worddesc/worddesc.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/13 17:20:36 by khais #+# #+# */ -/* Updated: 2025/02/20 14:02:11 by khais ### ########.fr */ +/* Updated: 2025/03/06 15:03:56 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,18 +18,28 @@ ** allocate a new worddesc with given flags and the given word as word. ** ** return null in case of error, or if word is null +** +** mark_string is initialized as a string of the same length as word, but filled +** with spaces +** +** In case of allocation error, word is freed, as well as any memory allocated +** in this function */ t_worddesc *worddesc_create(char *word, char flags) { - t_worddesc *retvalue; + t_worddesc *retvalue; if (word == NULL) - return (NULL); + return (NULL); retvalue = ft_calloc(1, sizeof(t_worddesc)); if (retvalue == NULL) - return (NULL); + return (free(word), NULL); retvalue->word = word; retvalue->flags = flags; + retvalue->marker = ft_calloc(ft_strlen(word) + 1, sizeof(char)); + if (retvalue->marker == NULL) + return (free(word), free(retvalue), NULL); + ft_memset(retvalue->marker, ' ', ft_strlen(word)); return (retvalue); } @@ -41,5 +51,6 @@ void worddesc_destroy(t_worddesc *worddesc) if (worddesc == NULL) return ; free(worddesc->word); + free(worddesc->marker); free(worddesc); } diff --git a/src/parser/worddesc/worddesc.h b/src/parser/worddesc/worddesc.h index c5d4fbf..3eb620e 100644 --- a/src/parser/worddesc/worddesc.h +++ b/src/parser/worddesc/worddesc.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/13 15:47:58 by khais #+# #+# */ -/* Updated: 2025/02/20 14:02:21 by khais ### ########.fr */ +/* Updated: 2025/03/06 17:20:50 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,6 +41,18 @@ typedef struct s_worddesc ** See above for flag definitions */ char flags; + /* + ** a character mask for word to designate the status + ** of its characters and wether or not they are subject to modifications + ** + ** Possible flags are ''', '"', '$', ' ' + ** + ** ' ' the default, no flag, no meaning + ** ''' corresponding character is single-quoted + ** '"' corresponding character is double-quoted + ** '$' corresponding character is a result of $var expansion + */ + char *marker; } t_worddesc; t_worddesc *worddesc_create(char *word, char flags); diff --git a/tests/word_splitting.c b/tests/word_splitting.c index 0ca53f6..ceb374e 100644 --- a/tests/word_splitting.c +++ b/tests/word_splitting.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/13 15:17:56 by khais #+# #+# */ -/* Updated: 2025/02/24 14:49:09 by khais ### ########.fr */ +/* Updated: 2025/03/06 15:05:58 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,7 @@ static void test_wordsplit_singleword(void) words = minishell_wordsplit("echo"); assert_strequal("echo", wordlist_get(words, 0)->word); + assert_strequal(" ", wordlist_get(words, 0)->marker); assert(NULL == wordlist_get(words, 1)); wordlist_destroy(words); } @@ -34,6 +35,7 @@ static void test_wordsplit_singleword_with_blanks(void) words = minishell_wordsplit("\t \t echo \t\t "); assert_strequal("echo", wordlist_get(words, 0)->word); + assert_strequal(" ", wordlist_get(words, 0)->marker); assert(NULL == wordlist_get(words, 1)); wordlist_destroy(words); } @@ -44,15 +46,23 @@ static void test_wordsplit_multiword(void) words = minishell_wordsplit("\t echo\tThe file is named $MYFILE \t"); assert_strequal("echo", wordlist_get(words, 0)->word); + assert_strequal(" ", wordlist_get(words, 0)->marker); assert_strequal("The", wordlist_get(words, 1)->word); + assert_strequal(" ", wordlist_get(words, 1)->marker); assert_strequal("file", wordlist_get(words, 2)->word); + assert_strequal(" ", wordlist_get(words, 2)->marker); assert_strequal("is", wordlist_get(words, 3)->word); + assert_strequal(" ", wordlist_get(words, 3)->marker); assert_strequal("named", wordlist_get(words, 4)->word); + assert_strequal(" ", wordlist_get(words, 4)->marker); assert_strequal("$MYFILE", wordlist_get(words, 5)->word); + assert_strequal(" ", wordlist_get(words, 5)->marker); assert(NULL == wordlist_get(words, 6)); wordlist_destroy(words); } +// kco work marker + static void test_wordsplit_multiword_with_single_quotes(void) { t_wordlist *words;