mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
word splitting: a single word is not split
Sorry this commit is a bit big, lots of groudwork is being established here
This commit is contained in:
parent
871571e258
commit
a083800506
9 changed files with 262 additions and 0 deletions
3
Makefile
3
Makefile
|
|
@ -27,6 +27,9 @@ srcs = \
|
||||||
src/get_command.c \
|
src/get_command.c \
|
||||||
src/parser/matchers/identifier.c \
|
src/parser/matchers/identifier.c \
|
||||||
src/parser/matchers/metacharacter.c \
|
src/parser/matchers/metacharacter.c \
|
||||||
|
src/parser/worddesc/worddesc.c \
|
||||||
|
src/parser/wordlist/wordlist.c \
|
||||||
|
src/parser/wordsplit/wordsplit.c \
|
||||||
|
|
||||||
objs = $(srcs:.c=.o)
|
objs = $(srcs:.c=.o)
|
||||||
export objs
|
export objs
|
||||||
|
|
|
||||||
33
src/parser/worddesc/worddesc.c
Normal file
33
src/parser/worddesc/worddesc.c
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* worddesc.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/02/13 17:20:36 by khais #+# #+# */
|
||||||
|
/* Updated: 2025/02/13 17:23:11 by khais ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "worddesc.h"
|
||||||
|
#include "libft.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
** allocate a new worddesc with zeroed flags and the given word as word.
|
||||||
|
**
|
||||||
|
** return null in case of error, or if word is null
|
||||||
|
*/
|
||||||
|
t_worddesc *worddesc_create(char *word)
|
||||||
|
{
|
||||||
|
t_worddesc *retvalue;
|
||||||
|
|
||||||
|
if (word == NULL)
|
||||||
|
return (NULL);
|
||||||
|
retvalue = ft_calloc(1, sizeof(t_worddesc));
|
||||||
|
if (retvalue == NULL)
|
||||||
|
return (NULL);
|
||||||
|
retvalue->word = word;
|
||||||
|
return (retvalue);
|
||||||
|
}
|
||||||
33
src/parser/worddesc/worddesc.h
Normal file
33
src/parser/worddesc/worddesc.h
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* worddesc.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/02/13 15:47:58 by khais #+# #+# */
|
||||||
|
/* Updated: 2025/02/13 17:20:25 by khais ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef WORDDESC_H
|
||||||
|
# define WORDDESC_H
|
||||||
|
|
||||||
|
/*
|
||||||
|
** A logical word for the parser.
|
||||||
|
**
|
||||||
|
** See section 3.2.4 https://aosabook.org/en/v1/bash.html for more details.
|
||||||
|
**
|
||||||
|
** We are basically duplicating the way bash does it.
|
||||||
|
*/
|
||||||
|
typedef struct s_worddesc
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
** The word itself
|
||||||
|
*/
|
||||||
|
char *word;
|
||||||
|
} t_worddesc;
|
||||||
|
|
||||||
|
t_worddesc *worddesc_create(char *word);
|
||||||
|
|
||||||
|
#endif
|
||||||
57
src/parser/wordlist/wordlist.c
Normal file
57
src/parser/wordlist/wordlist.c
Normal file
|
|
@ -0,0 +1,57 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* wordlist.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/02/13 17:07:01 by khais #+# #+# */
|
||||||
|
/* Updated: 2025/02/14 13:29:16 by khais ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "wordlist.h"
|
||||||
|
#include "libft.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
** create a new wordlist element, with next set to null and word set to the
|
||||||
|
** passed worddesc.
|
||||||
|
**
|
||||||
|
** in case of error, return null
|
||||||
|
*/
|
||||||
|
t_wordlist *wordlist_create(t_worddesc *word)
|
||||||
|
{
|
||||||
|
t_wordlist *retvalue;
|
||||||
|
|
||||||
|
retvalue = ft_calloc(1, sizeof(t_wordlist));
|
||||||
|
if (retvalue == NULL)
|
||||||
|
return (NULL);
|
||||||
|
retvalue->next = NULL;
|
||||||
|
retvalue->word = word;
|
||||||
|
return (retvalue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** free all memory associated with this wordlist.
|
||||||
|
*/
|
||||||
|
void wordlist_destroy(t_wordlist *wordlist)
|
||||||
|
{
|
||||||
|
free(wordlist);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
** get the worddesc at position idx in the given wordlist.
|
||||||
|
**
|
||||||
|
** return null if out of range or wordlist is null
|
||||||
|
*/
|
||||||
|
t_worddesc *wordlist_get(t_wordlist *wordlist, int idx)
|
||||||
|
{
|
||||||
|
if (wordlist == NULL || idx < 0)
|
||||||
|
return (NULL);
|
||||||
|
while (idx > 0 && wordlist != NULL)
|
||||||
|
wordlist = wordlist->next;
|
||||||
|
if (wordlist == NULL)
|
||||||
|
return (NULL);
|
||||||
|
return (wordlist->word);
|
||||||
|
}
|
||||||
41
src/parser/wordlist/wordlist.h
Normal file
41
src/parser/wordlist/wordlist.h
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* wordlist.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/02/13 15:46:02 by khais #+# #+# */
|
||||||
|
/* Updated: 2025/02/14 13:29:05 by khais ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef WORDLIST_H
|
||||||
|
# define WORDLIST_H
|
||||||
|
|
||||||
|
# include "../worddesc/worddesc.h"
|
||||||
|
|
||||||
|
/*
|
||||||
|
** Linked-list of WORDS
|
||||||
|
**
|
||||||
|
** cf. section 3.2.4 of https://aosabook.org/en/v1/bash.html
|
||||||
|
**
|
||||||
|
** we are copying how bash does it.
|
||||||
|
*/
|
||||||
|
typedef struct s_wordlist
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
** pointer to the next element in the list
|
||||||
|
*/
|
||||||
|
struct s_wordlist *next;
|
||||||
|
/*
|
||||||
|
** pointer to the word at this position in the list
|
||||||
|
*/
|
||||||
|
struct s_worddesc *word;
|
||||||
|
} t_wordlist;
|
||||||
|
|
||||||
|
t_wordlist *wordlist_create(t_worddesc *word);
|
||||||
|
void wordlist_destroy(t_wordlist *wordlist);
|
||||||
|
t_worddesc *wordlist_get(t_wordlist *wordlist, int idx);
|
||||||
|
|
||||||
|
#endif
|
||||||
25
src/parser/wordsplit/wordsplit.c
Normal file
25
src/parser/wordsplit/wordsplit.c
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* wordsplit.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/02/13 17:02:32 by khais #+# #+# */
|
||||||
|
/* Updated: 2025/02/14 13:28:38 by khais ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "wordsplit.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
** split a string into words, respecting quotes etc.
|
||||||
|
**
|
||||||
|
** cf. Token Recognition section at
|
||||||
|
** https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
|
||||||
|
*/
|
||||||
|
t_wordlist *minishell_wordsplit(char *original)
|
||||||
|
{
|
||||||
|
return (wordlist_create(worddesc_create(original)));
|
||||||
|
}
|
||||||
20
src/parser/wordsplit/wordsplit.h
Normal file
20
src/parser/wordsplit/wordsplit.h
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* wordsplit.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/02/13 15:52:48 by khais #+# #+# */
|
||||||
|
/* Updated: 2025/02/13 15:54:30 by khais ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#ifndef WORDSPLIT_H
|
||||||
|
# define WORDSPLIT_H
|
||||||
|
|
||||||
|
# include "../wordlist/wordlist.h"
|
||||||
|
|
||||||
|
t_wordlist *minishell_wordsplit(char *original);
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
rawtests = \
|
rawtests = \
|
||||||
test_env_manip \
|
test_env_manip \
|
||||||
metacharacters \
|
metacharacters \
|
||||||
|
word_splitting \
|
||||||
|
|
||||||
ifeq ($(CFLAGS),)
|
ifeq ($(CFLAGS),)
|
||||||
CFLAGS = -Wall -Wextra -Werror -g
|
CFLAGS = -Wall -Wextra -Werror -g
|
||||||
|
|
|
||||||
49
tests/word_splitting.c
Normal file
49
tests/word_splitting.c
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* word_splitting.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/02/13 15:17:56 by khais #+# #+# */
|
||||||
|
/* Updated: 2025/02/13 17:24:33 by khais ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include <assert.h>
|
||||||
|
#include "testutil.h"
|
||||||
|
#include "../src/parser/wordsplit/wordsplit.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
** https://bash-hackers.gabe565.com/syntax/words/
|
||||||
|
*/
|
||||||
|
void test_wordsplit_singleword(void)
|
||||||
|
{
|
||||||
|
t_wordlist *words;
|
||||||
|
|
||||||
|
words = minishell_wordsplit("echo");
|
||||||
|
assert_strequal("echo", wordlist_get(words, 0)->word);
|
||||||
|
assert(NULL == wordlist_get(words, 1));
|
||||||
|
wordlist_destroy(words);
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_wordsplit_basic(void)
|
||||||
|
{
|
||||||
|
t_wordlist *words;
|
||||||
|
|
||||||
|
words = minishell_wordsplit("echo The file is named $MYFILE");
|
||||||
|
assert_strequal("echo", wordlist_get(words, 0)->word);
|
||||||
|
assert_strequal("The", wordlist_get(words, 1)->word);
|
||||||
|
assert_strequal("file", wordlist_get(words, 2)->word);
|
||||||
|
assert_strequal("is", wordlist_get(words, 3)->word);
|
||||||
|
assert_strequal("named", wordlist_get(words, 4)->word);
|
||||||
|
assert_strequal("$MYFILE", wordlist_get(words, 5)->word);
|
||||||
|
assert(NULL == wordlist_get(words, 6));
|
||||||
|
wordlist_destroy(words);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
test_wordsplit_singleword();
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue