mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
wordsplit: handle multiple words
This commit is contained in:
parent
00fd2380cf
commit
4171a3d07b
4 changed files with 68 additions and 17 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/13 17:07:01 by khais #+# #+# */
|
/* Created: 2025/02/13 17:07:01 by khais #+# #+# */
|
||||||
/* Updated: 2025/02/14 13:56:45 by khais ### ########.fr */
|
/* Updated: 2025/02/14 16:47:04 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -37,10 +37,15 @@ t_wordlist *wordlist_create(t_worddesc *word)
|
||||||
*/
|
*/
|
||||||
void wordlist_destroy(t_wordlist *wordlist)
|
void wordlist_destroy(t_wordlist *wordlist)
|
||||||
{
|
{
|
||||||
if (wordlist == NULL)
|
t_wordlist *prev;
|
||||||
return ;
|
|
||||||
worddesc_destroy(wordlist->word);
|
while (wordlist != NULL)
|
||||||
free(wordlist);
|
{
|
||||||
|
worddesc_destroy(wordlist->word);
|
||||||
|
prev = wordlist;
|
||||||
|
wordlist = wordlist->next;
|
||||||
|
free(prev);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -53,8 +58,36 @@ t_worddesc *wordlist_get(t_wordlist *wordlist, int idx)
|
||||||
if (wordlist == NULL || idx < 0)
|
if (wordlist == NULL || idx < 0)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
while (idx > 0 && wordlist != NULL)
|
while (idx > 0 && wordlist != NULL)
|
||||||
|
{
|
||||||
wordlist = wordlist->next;
|
wordlist = wordlist->next;
|
||||||
|
idx--;
|
||||||
|
}
|
||||||
if (wordlist == NULL)
|
if (wordlist == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
return (wordlist->word);
|
return (wordlist->word);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** push the given worddesc to the end of the given wordlist.
|
||||||
|
**
|
||||||
|
** if wordlist is null, create a new wordlist.
|
||||||
|
**
|
||||||
|
** returns a pointer to the first element in the wordlist.
|
||||||
|
**
|
||||||
|
** if malloc failure is encountered, all memory for this wordlist is freed, and
|
||||||
|
** null is returned.
|
||||||
|
*/
|
||||||
|
t_wordlist *wordlist_push(t_wordlist *wordlist, t_worddesc *worddesc)
|
||||||
|
{
|
||||||
|
t_wordlist *start;
|
||||||
|
|
||||||
|
if (wordlist == NULL)
|
||||||
|
return (wordlist_create(worddesc));
|
||||||
|
start = wordlist;
|
||||||
|
while (wordlist->next != NULL)
|
||||||
|
wordlist = wordlist->next;
|
||||||
|
wordlist->next = wordlist_create(worddesc);
|
||||||
|
if (wordlist->next == NULL)
|
||||||
|
return (wordlist_destroy(start), NULL);
|
||||||
|
return (start);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/13 15:46:02 by khais #+# #+# */
|
/* Created: 2025/02/13 15:46:02 by khais #+# #+# */
|
||||||
/* Updated: 2025/02/14 13:29:05 by khais ### ########.fr */
|
/* Updated: 2025/02/14 15:48:36 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -37,5 +37,6 @@ typedef struct s_wordlist
|
||||||
t_wordlist *wordlist_create(t_worddesc *word);
|
t_wordlist *wordlist_create(t_worddesc *word);
|
||||||
void wordlist_destroy(t_wordlist *wordlist);
|
void wordlist_destroy(t_wordlist *wordlist);
|
||||||
t_worddesc *wordlist_get(t_wordlist *wordlist, int idx);
|
t_worddesc *wordlist_get(t_wordlist *wordlist, int idx);
|
||||||
|
t_wordlist *wordlist_push(t_wordlist *wordlist, t_worddesc *worddesc);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/13 17:02:32 by khais #+# #+# */
|
/* Created: 2025/02/13 17:02:32 by khais #+# #+# */
|
||||||
/* Updated: 2025/02/14 15:22:57 by khais ### ########.fr */
|
/* Updated: 2025/02/14 16:46:27 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -26,12 +26,27 @@
|
||||||
*/
|
*/
|
||||||
t_wordlist *minishell_wordsplit(char *original)
|
t_wordlist *minishell_wordsplit(char *original)
|
||||||
{
|
{
|
||||||
size_t start;
|
size_t start;
|
||||||
size_t length;
|
size_t idx;
|
||||||
char *outstr;
|
size_t length;
|
||||||
|
char *word;
|
||||||
|
t_wordlist *wordlist;
|
||||||
|
|
||||||
start = ft_strnfchridx(original, is_blank);
|
start = 0;
|
||||||
length = ft_strfchridx(original + start, is_blank);
|
idx = 0;
|
||||||
outstr = ft_substr(original, start, length);
|
wordlist = NULL;
|
||||||
return (wordlist_create(worddesc_create(outstr)));
|
length = 1;
|
||||||
|
while (length != 0)
|
||||||
|
{
|
||||||
|
start = ft_strnfchridx(original + idx, is_blank);
|
||||||
|
length = ft_strfchridx(original + idx + start, is_blank);
|
||||||
|
if (length == 0)
|
||||||
|
break ;
|
||||||
|
word = ft_substr(original + idx, start, length);
|
||||||
|
wordlist = wordlist_push(wordlist, worddesc_create(word));
|
||||||
|
if (wordlist == NULL)
|
||||||
|
return (NULL);
|
||||||
|
idx += start + length;
|
||||||
|
}
|
||||||
|
return (wordlist);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,12 +6,13 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/13 15:17:56 by khais #+# #+# */
|
/* Created: 2025/02/13 15:17:56 by khais #+# #+# */
|
||||||
/* Updated: 2025/02/14 15:06:50 by khais ### ########.fr */
|
/* Updated: 2025/02/14 16:16:14 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include "testutil.h"
|
#include "testutil.h"
|
||||||
|
#include "libft.h"
|
||||||
#include "../src/parser/wordsplit/wordsplit.h"
|
#include "../src/parser/wordsplit/wordsplit.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|
@ -38,11 +39,11 @@ void test_wordsplit_singleword_with_blanks(void)
|
||||||
wordlist_destroy(words);
|
wordlist_destroy(words);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test_wordsplit_basic(void)
|
void test_wordsplit_multiword(void)
|
||||||
{
|
{
|
||||||
t_wordlist *words;
|
t_wordlist *words;
|
||||||
|
|
||||||
words = minishell_wordsplit("echo The file is named $MYFILE");
|
words = minishell_wordsplit("\t echo\tThe file is named $MYFILE \t");
|
||||||
assert_strequal("echo", wordlist_get(words, 0)->word);
|
assert_strequal("echo", wordlist_get(words, 0)->word);
|
||||||
assert_strequal("The", wordlist_get(words, 1)->word);
|
assert_strequal("The", wordlist_get(words, 1)->word);
|
||||||
assert_strequal("file", wordlist_get(words, 2)->word);
|
assert_strequal("file", wordlist_get(words, 2)->word);
|
||||||
|
|
@ -56,5 +57,6 @@ void test_wordsplit_basic(void)
|
||||||
int main(void) {
|
int main(void) {
|
||||||
test_wordsplit_singleword();
|
test_wordsplit_singleword();
|
||||||
test_wordsplit_singleword_with_blanks();
|
test_wordsplit_singleword_with_blanks();
|
||||||
|
test_wordsplit_multiword();
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue