mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
wordsplit: handle mixed quotes
This commit is contained in:
parent
1676cf6696
commit
452d35acdf
2 changed files with 38 additions and 8 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/13 17:02:32 by khais #+# #+# */
|
||||
/* Updated: 2025/02/17 13:54:58 by khais ### ########.fr */
|
||||
/* Updated: 2025/02/17 14:20:45 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -41,10 +41,14 @@ static t_buffer *new_word(t_buffer *token, char c, bool *currently_in_word)
|
|||
return (token);
|
||||
}
|
||||
|
||||
static bool quote_flip(t_buffer **token, char c, bool currently_in_quotes)
|
||||
static char quote_flip(t_buffer **token, char c, char quote)
|
||||
{
|
||||
if (quote == '\0')
|
||||
quote = c;
|
||||
else if (quote == c)
|
||||
quote = '\0';
|
||||
(*token) = push_char((*token), c);
|
||||
return (!currently_in_quotes);
|
||||
return (quote);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -63,13 +67,13 @@ t_wordlist *minishell_wordsplit(char *original)
|
|||
t_wordlist *wordlist;
|
||||
t_buffer *token;
|
||||
bool currently_in_word;
|
||||
bool currently_in_quotes;
|
||||
char quote;
|
||||
|
||||
idx = 0;
|
||||
wordlist = NULL;
|
||||
token = NULL;
|
||||
currently_in_word = false;
|
||||
currently_in_quotes = false;
|
||||
quote = '\0';
|
||||
while (true)
|
||||
{
|
||||
// If the end of input is recognized, the current token (if any) shall
|
||||
|
|
@ -85,11 +89,11 @@ t_wordlist *minishell_wordsplit(char *original)
|
|||
// the end of the quoted text. The token shall not be delimited by the
|
||||
// end of the quoted field.
|
||||
else if (original[idx] == '\'' || original[idx] == '"')
|
||||
currently_in_quotes = quote_flip(&token, original[idx], currently_in_quotes);
|
||||
quote = quote_flip(&token, original[idx], quote);
|
||||
// If the current character is an unquoted <blank>, any token containing
|
||||
// the previous character is delimited and the current character shall
|
||||
// be discarded.
|
||||
else if (is_blank(original[idx]) && !currently_in_quotes)
|
||||
else if (is_blank(original[idx]) && quote == '\0')
|
||||
wordlist = delimit(wordlist, &token, ¤tly_in_word);
|
||||
// If the previous character was part of a word, the current character
|
||||
// shall be appended to that word.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue