diff --git a/Makefile b/Makefile index 5d92a94..af60ce8 100644 --- a/Makefile +++ b/Makefile @@ -36,6 +36,7 @@ srcs = \ src/parser/pipeline/pipeline.c \ src/parser/pipeline/pipeline_parse_baseops.c \ src/parser/pipeline/pipeline_parse.c \ + src/parser/remove_quotes/remove_quotes.c \ src/parser/simple_cmd/simple_cmd.c \ src/parser/worddesc/worddesc.c \ src/parser/wordlist/wordlist.c \ diff --git a/src/parser/remove_quotes/remove_quotes.c b/src/parser/remove_quotes/remove_quotes.c new file mode 100644 index 0000000..030e01c --- /dev/null +++ b/src/parser/remove_quotes/remove_quotes.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* remove_quotes.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/28 13:52:02 by khais #+# #+# */ +/* Updated: 2025/03/07 11:09:17 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "remove_quotes.h" +#include "libft.h" + +/* +** use the marker in the given worddesc to create a new worddesc by removing +** unquoted quotes. +** +** The new worddesc will have the marker set to NULL. +*/ +t_worddesc *remove_quotes(t_worddesc *word) +{ + t_worddesc *output; + + output = worddesc_create(ft_strdup(word->word), word->flags, NULL); + if (output->word == NULL) + return (worddesc_destroy(output), NULL); + return (output); +} diff --git a/src/parser/remove_quotes/remove_quotes.h b/src/parser/remove_quotes/remove_quotes.h new file mode 100644 index 0000000..06805fa --- /dev/null +++ b/src/parser/remove_quotes/remove_quotes.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* remove_quotes.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/28 13:51:03 by khais #+# #+# */ +/* Updated: 2025/02/28 13:51:53 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef REMOVE_QUOTES_H +# define REMOVE_QUOTES_H + +# include "../worddesc/worddesc.h" + +t_worddesc *remove_quotes(t_worddesc *word); + +#endif // REMOVE_QUOTES_H diff --git a/tests/Makefile b/tests/Makefile index 86e1226..52c92c7 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,6 +1,7 @@ # make gets confused if a file with the same name exists in the sources, so some # file are prefixed with test_ rawtests = \ + quote_removal \ metacharacters \ parse_pipelines \ parse_simple_cmds \ diff --git a/tests/quote_removal.c b/tests/quote_removal.c new file mode 100644 index 0000000..956f6e9 --- /dev/null +++ b/tests/quote_removal.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* quote_removal.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/28 13:46:56 by khais #+# #+# */ +/* Updated: 2025/03/07 10:59:54 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../src/parser/wordlist/wordlist.h" +#include "../src/parser/wordsplit/wordsplit.h" +#include "../src/parser/remove_quotes/remove_quotes.h" +#include "testutil.h" +#include + +static t_worddesc *create_single_word(char *str) +{ + t_wordlist *words = minishell_wordsplit(str); + assert(wordlist_get(words, 0) != NULL); + assert(wordlist_get(words, 1) == NULL); + t_worddesc *word = wordlist_pop(&words); + return (word); +} + +static void test_quote_removal_no_quotes_single_word(void) +{ + t_worddesc *word = create_single_word("word"); + t_worddesc *got_word = remove_quotes(word); + assert_strequal("word", got_word->word); + assert(got_word->marker == NULL); + worddesc_destroy(word); + worddesc_destroy(got_word); +} + +int main(void) { + test_quote_removal_no_quotes_single_word(); + return (0); +}