minishell/src/postprocess/fieldsplit/fieldsplit.c

109 lines
2.9 KiB
C
Raw Normal View History

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fieldsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/03 15:48:52 by khais #+# #+# */
fix(field_splitting): ensure no extraneous spaces appear 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
2025-04-25 13:43:41 +02:00
/* Updated: 2025/04/25 14:27:56 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "fieldsplit.h"
#include "../../buffer/buffer.h"
#include "../../parser/matchers/blank.h"
#include "redirect_fieldsplit.h"
#include <stdlib.h>
static bool should_delimit(t_worddesc *original, size_t i)
{
if (is_blank(original->word[i]) && original->marker[i] == '$')
return (true);
return (false);
}
static void fieldsplit_delimit(t_buffer *word, t_buffer *marker,
t_worddesc *original, t_wordlist **outlist)
{
t_worddesc *out;
fix(field_splitting): ensure no extraneous spaces appear 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
2025-04-25 13:43:41 +02:00
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,
t_wordlist **outlist)
{
size_t i;
t_buffer *word;
t_buffer *marker;
i = 0;
word = ft_buffer_new();
marker = ft_buffer_new();
while (original->word[i] != '\0')
{
if (should_delimit(original, i))
{
fieldsplit_delimit(word, marker, original, outlist);
word = ft_buffer_new();
marker = ft_buffer_new();
while (should_delimit(original, i))
i++;
}
else
{
word = ft_buffer_pushchar(word, original->word[i]);
marker = ft_buffer_pushchar(marker, original->marker[i++]);
}
}
fieldsplit_delimit(word, marker, original, outlist);
return (*outlist);
}
static t_wordlist *wordlist_fieldsplit(t_wordlist **original)
{
t_worddesc *current;
t_wordlist *out;
if (original == NULL || *original == NULL)
return (NULL);
out = NULL;
current = wordlist_pop(original);
while (current != NULL)
{
if (minishell_fieldsplit(current, &out) == NULL)
{
wordlist_destroy(out);
return (NULL);
}
worddesc_destroy(current);
current = wordlist_pop(original);
}
(*original) = out;
return (*original);
}
t_simple_cmd *simple_cmd_fieldsplit(t_simple_cmd *cmd)
{
if (cmd == NULL)
return (NULL);
if (wordlist_fieldsplit(&cmd->words) == NULL)
return (NULL);
if (redirect_fieldsplit(cmd) == NULL)
return (NULL);
return (cmd);
}