mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
quote removal: handle strings with no quotes
This commit is contained in:
parent
4f6910eccd
commit
0fecded23b
5 changed files with 93 additions and 0 deletions
1
Makefile
1
Makefile
|
|
@ -36,6 +36,7 @@ srcs = \
|
||||||
src/parser/pipeline/pipeline.c \
|
src/parser/pipeline/pipeline.c \
|
||||||
src/parser/pipeline/pipeline_parse_baseops.c \
|
src/parser/pipeline/pipeline_parse_baseops.c \
|
||||||
src/parser/pipeline/pipeline_parse.c \
|
src/parser/pipeline/pipeline_parse.c \
|
||||||
|
src/parser/remove_quotes/remove_quotes.c \
|
||||||
src/parser/simple_cmd/simple_cmd.c \
|
src/parser/simple_cmd/simple_cmd.c \
|
||||||
src/parser/worddesc/worddesc.c \
|
src/parser/worddesc/worddesc.c \
|
||||||
src/parser/wordlist/wordlist.c \
|
src/parser/wordlist/wordlist.c \
|
||||||
|
|
|
||||||
30
src/parser/remove_quotes/remove_quotes.c
Normal file
30
src/parser/remove_quotes/remove_quotes.c
Normal file
|
|
@ -0,0 +1,30 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* remove_quotes.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
20
src/parser/remove_quotes/remove_quotes.h
Normal file
20
src/parser/remove_quotes/remove_quotes.h
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* remove_quotes.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
# make gets confused if a file with the same name exists in the sources, so some
|
# make gets confused if a file with the same name exists in the sources, so some
|
||||||
# file are prefixed with test_
|
# file are prefixed with test_
|
||||||
rawtests = \
|
rawtests = \
|
||||||
|
quote_removal \
|
||||||
metacharacters \
|
metacharacters \
|
||||||
parse_pipelines \
|
parse_pipelines \
|
||||||
parse_simple_cmds \
|
parse_simple_cmds \
|
||||||
|
|
|
||||||
41
tests/quote_removal.c
Normal file
41
tests/quote_removal.c
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* quote_removal.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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 <assert.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue