From e5027323e19b633ef94d3db56eb5f7ef80cea5f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Fri, 25 Apr 2025 13:43:41 +0200 Subject: [PATCH] fix(field_splitting): ensure no extraneous spaces appear MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit debug notes: = 250 !! OK =================================================================== export HOLA="bonjour " echo $HOLA | cat -e = out = bonjour $ = err = rc: 0 = refout = bonjour$ ++++++++++++++++++++++++++++++++++++++++++++++++++++ failed: quote removal before word splitting ++++++++++++++++++++++++++++++++++++++++++++++++++++ ++++ test input: export VAR="hello " echo $VAR | cat -e ++++ Got output: hello $ ++++ But expected: hello$ Problem is in post-processing: parsed command ╰─ t_cmd ├─ t_cmd_type = FT_SIMPLE ├─ flags = 0 ├─ line = 0 ╰─ value ╰─ t_simple_cmd ├─ line = 0 ├─ t_wordlist │ ├─ t_worddesc │ │ ├─ word = [echo] │ │ ├─ marker = [ ] │ │ ├─ flags = 0 │ │ ╰─ t_token_type = WORD_TOKEN │ ╰─ t_worddesc │ ├─ word = [$VAR] │ ├─ marker = [ ] │ ├─ flags = 1 │ ╰─ t_token_type = WORD_TOKEN ╰─ redirections = (empty redir list) about to post-process ╰─ t_simple_cmd ├─ line = 0 ├─ t_wordlist │ ├─ t_worddesc │ │ ├─ word = [echo] │ │ ├─ marker = [ ] │ │ ├─ flags = 0 │ │ ╰─ t_token_type = WORD_TOKEN │ ╰─ t_worddesc │ ├─ word = [$VAR] │ ├─ marker = [ ] │ ├─ flags = 1 │ ╰─ t_token_type = WORD_TOKEN ╰─ redirections = (empty redir list) about to post-process ╰─ t_simple_cmd ├─ line = 0 ├─ t_wordlist │ ├─ t_worddesc │ │ ├─ word = [echo] │ │ ├─ marker = [ ] │ │ ├─ flags = 0 │ │ ╰─ t_token_type = WORD_TOKEN │ ╰─ t_worddesc │ ├─ word = [hello ] │ ├─ marker = [$$$$$$] │ ├─ flags = 1 │ ╰─ t_token_type = WORD_TOKEN ╰─ redirections = (empty redir list) about to post-process ╰─ t_simple_cmd ├─ line = 0 ├─ t_wordlist │ ├─ t_worddesc │ │ ├─ word = [echo] │ │ ├─ marker = [ ] │ │ ├─ flags = 0 │ │ ╰─ t_token_type = WORD_TOKEN │ ├─ t_worddesc │ │ ├─ word = [hello] │ │ ├─ marker = [$$$$$] │ │ ├─ flags = 1 │ │ ╰─ t_token_type = WORD_TOKEN │ ╰─ t_worddesc <---- WHAT IS THIS DOING HERE??? │ ├─ word = [] │ ├─ marker = [] │ ├─ flags = 1 │ ╰─ t_token_type = WORD_TOKEN ╰─ redirections = (empty redir list) about to post-process ╰─ t_simple_cmd ├─ line = 0 ├─ t_wordlist │ ├─ t_worddesc │ │ ├─ word = [echo] │ │ ├─ marker = [ ] │ │ ├─ flags = 0 │ │ ╰─ t_token_type = WORD_TOKEN │ ├─ t_worddesc │ │ ├─ word = [hello] │ │ ├─ marker = [$$$$$] │ │ ├─ flags = 1 │ │ ╰─ t_token_type = WORD_TOKEN │ ╰─ t_worddesc │ ├─ word = [] │ ├─ marker = [] │ ├─ flags = 1 │ ╰─ t_token_type = WORD_TOKEN ╰─ redirections = (empty redir list) about to execute ╰─ t_simple_cmd ├─ line = 0 ├─ t_wordlist │ ├─ t_worddesc │ │ ├─ word = [echo] │ │ ├─ marker = [(null)] │ │ ├─ flags = 0 │ │ ╰─ t_token_type = WORD_TOKEN │ ├─ t_worddesc │ │ ├─ word = [hello] │ │ ├─ marker = [(null)] │ │ ├─ flags = 1 │ │ ╰─ t_token_type = WORD_TOKEN │ ╰─ t_worddesc │ ├─ word = [] │ ├─ marker = [(null)] │ ├─ flags = 1 │ ╰─ t_token_type = WORD_TOKEN ╰─ redirections = (empty redir list) Problem seems to be in field splitting --- src/postprocess/fieldsplit/fieldsplit.c | 20 ++++++++++++++------ test.sh | 24 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/postprocess/fieldsplit/fieldsplit.c b/src/postprocess/fieldsplit/fieldsplit.c index b8d9fcf..68c4338 100644 --- a/src/postprocess/fieldsplit/fieldsplit.c +++ b/src/postprocess/fieldsplit/fieldsplit.c @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/04/03 15:48:52 by khais #+# #+# */ -/* Updated: 2025/04/21 11:56:26 by khais ### ########.fr */ +/* Updated: 2025/04/25 14:27:56 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,11 +28,19 @@ static void fieldsplit_delimit(t_buffer *word, t_buffer *marker, { t_worddesc *out; - out = worddesc_create(word->buffer, original->flags, marker->buffer, - WORD_TOKEN); - (*outlist) = wordlist_push(*outlist, out); - free(word); - free(marker); + if (ft_strlen(word->buffer) > 0) + { + out = worddesc_create(word->buffer, original->flags, marker->buffer, + WORD_TOKEN); + (*outlist) = wordlist_push(*outlist, out); + free(word); + free(marker); + } + else + { + ft_buffer_free(word); + ft_buffer_free(marker); + } } t_wordlist *minishell_fieldsplit(t_worddesc *original, diff --git a/test.sh b/test.sh index ee31bc7..669828d 100755 --- a/test.sh +++ b/test.sh @@ -1096,4 +1096,28 @@ expecting <