mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
wordsplit: handle single quotes
This commit is contained in:
parent
81d28c15d4
commit
5276aba278
2 changed files with 31 additions and 10 deletions
|
|
@ -6,14 +6,14 @@
|
|||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/13 17:02:32 by khais #+# #+# */
|
||||
/* Updated: 2025/02/14 18:06:44 by khais ### ########.fr */
|
||||
/* Updated: 2025/02/14 18:45:45 by khais ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "wordsplit.h"
|
||||
#include "libft.h"
|
||||
#include "../../buffer/buffer.h"
|
||||
#include "../matchers/blank.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
static t_wordlist *delimit(t_wordlist *wordlist, t_buffer **token, bool *currently_in_word)
|
||||
{
|
||||
|
|
@ -28,20 +28,25 @@ static t_wordlist *delimit(t_wordlist *wordlist, t_buffer **token, bool *current
|
|||
|
||||
static t_buffer *push_char(t_buffer *token, char c)
|
||||
{
|
||||
if (token == NULL)
|
||||
token = ft_buffer_new();
|
||||
return (ft_buffer_pushchar(token, c));
|
||||
}
|
||||
|
||||
|
||||
static t_buffer *new_word(char c, bool *currently_in_word)
|
||||
static t_buffer *new_word(t_buffer *token, char c, bool *currently_in_word)
|
||||
{
|
||||
t_buffer *token;
|
||||
|
||||
token = ft_buffer_new();
|
||||
ft_buffer_pushchar(token, c);
|
||||
token = push_char(token, c);
|
||||
(*currently_in_word) = true;
|
||||
return (token);
|
||||
}
|
||||
|
||||
static bool quote_flip(t_buffer **token, char c, bool currently_in_quotes)
|
||||
{
|
||||
(*token) = push_char((*token), c);
|
||||
return (!currently_in_quotes);
|
||||
}
|
||||
|
||||
/*
|
||||
** split a string into words, respecting quotes etc.
|
||||
**
|
||||
|
|
@ -58,21 +63,25 @@ t_wordlist *minishell_wordsplit(char *original)
|
|||
t_wordlist *wordlist;
|
||||
t_buffer *token;
|
||||
bool currently_in_word;
|
||||
bool currently_in_quotes;
|
||||
|
||||
idx = 0;
|
||||
wordlist = NULL;
|
||||
token = NULL;
|
||||
currently_in_word = false;
|
||||
currently_in_quotes = false;
|
||||
while (true)
|
||||
{
|
||||
// If the end of input is recognized, the current token (if any) shall
|
||||
// be delimited.
|
||||
if (original[idx] == '\0')
|
||||
wordlist = delimit(wordlist, &token, ¤tly_in_word);
|
||||
else if (original[idx] == '\'')
|
||||
currently_in_quotes = quote_flip(&token, original[idx], currently_in_quotes);
|
||||
// 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]))
|
||||
else if (is_blank(original[idx]) && !currently_in_quotes)
|
||||
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.
|
||||
|
|
@ -80,7 +89,7 @@ t_wordlist *minishell_wordsplit(char *original)
|
|||
token = push_char(token, original[idx]);
|
||||
// The current character is used as the start of a new word.
|
||||
else
|
||||
token = new_word(original[idx], ¤tly_in_word);
|
||||
token = new_word(token, original[idx], ¤tly_in_word);
|
||||
if (original[idx] == '\0')
|
||||
break ;
|
||||
idx++;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue