Expansion: temporarily sharing broken version

This commit is contained in:
Jérôme Guélen 2025-03-19 17:20:03 +01:00
parent 0f8b18b32f
commit ea3ecaaf31
No known key found for this signature in database
7 changed files with 163 additions and 72 deletions

View file

@ -6,7 +6,7 @@
/* By: jguelen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/08 17:29:05 by jguelen #+# #+# */
/* Updated: 2025/03/10 12:36:27 by jguelen ### ########.fr */
/* Updated: 2025/03/19 16:29:58 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
@ -18,7 +18,7 @@ static void ft_swap_wordlist_contents(t_wordlist *list1, t_wordlist *list2)
tmp_word = list1->word;
list1->word = list2->word;
list2->word = tmp_word->word;
list2->word = tmp_word;
}
/*

View file

@ -6,7 +6,7 @@
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/08 17:26:52 by jguelen #+# #+# */
/* Updated: 2025/03/10 12:34:23 by jguelen ### ########.fr */
/* Updated: 2025/03/19 16:31:18 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,6 +14,7 @@
# define WORDLIST_QUICKSORT_H
# include "wordlist.h"
# include "../../../libft/libft.h"
t_wordlist *wordlist_quicksort(t_wordlist *list, int low, int high);
t_wordlist *wordlist_quicksort_full(t_wordlist *list);

View file

@ -6,7 +6,7 @@
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/10 09:51:34 by jguelen #+# #+# */
/* Updated: 2025/03/10 12:23:36 by jguelen ### ########.fr */
/* Updated: 2025/03/19 13:43:24 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,7 +15,7 @@
/*
** Returns the number of words present in the wordlist given as parameter.
** NOTE: Assumes that that list not to be circular.
** NOTE: Assumes list not to be circular.
*/
int wordlist_size(t_wordlist *wordlist)
{

View file

@ -6,11 +6,11 @@
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/02 13:40:10 by jguelen #+# #+# */
/* Updated: 2025/03/19 09:01:54 by jguelen ### ########.fr */
/* Updated: 2025/03/19 16:46:55 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "../minishell.h"
#include "libft.h"
#include "../ft_errno.h"
#include "../env/env_manip.h"
@ -31,7 +31,7 @@ static char **get_paths_array(t_env *env)
char **path_array;
t_env *path_node;
path_node = env_get_val(env, "PATH");
path_node = env_find_node_bykey(env, "PATH");
if (path_node == NULL || !path_node->value || !path_node->value[0])
{
path_array = ft_calloc(1, sizeof(char *));
@ -53,19 +53,27 @@ static char *select_path(char *filepath, char **oldpath, char **path,
ft_errno(FT_ESUCCESS);
ret = stat(filepath, fstat);
if (ret == -1)
return (destroy_split_array(path), ft_errno(FT_STATFAIL), NULL);
return (path_split_destroy(path), ft_errno(FT_STATFAIL), NULL);
if (access(filepath, F_OK) == 0 && ret == 0 && S_ISREG(fstat->st_mode))
{
if (access(filepath, X_OK) != 0 && !oldpath)
*oldpath = filepath;
if (access(filepath, X_OK) != 0)
{
if (!oldpath)
*oldpath = filepath;
}
else
return (free(*oldpath), destroy_split_array(path), filepath);
return (free(*oldpath), path_split_destroy(path), filepath);
}
if (*oldpath != filepath)
free(filepath);
return (NULL);
}
/*
** Returns NULL if an error occurred or nothing corresponds to filename
** in PATH. ft_errno is set in the course of this function to help distinguish
** the nature of the case.
*/
static char *deal_with_filled_path(char *filename, char **path,
struct stat *fstat)
{
@ -91,7 +99,7 @@ static char *deal_with_filled_path(char *filename, char **path,
return (NULL);
i++;
}
destroy_split_array(path);
path_split_destroy(path);
return (oldpath);
}
@ -138,12 +146,12 @@ char *get_cmdpath(const char *name, t_minishell *app)
{
char *cmd_path;
if (ft_trchr(name, '/'))
if (ft_strchr(name, '/'))
{
cmd_path = ft_strdup(name);
if (!cmd_path)
return (ft_errno(FT_ENOMEM), NULL);
return (cmd_path);
}
return (filepath_from_env(name, app));
return (filepath_from_env((char *)name, app));
}

View file

@ -6,11 +6,11 @@
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/18 18:40:14 by jguelen #+# #+# */
/* Updated: 2025/03/19 09:02:22 by jguelen ### ########.fr */
/* Updated: 2025/03/19 16:34:10 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "../minishell.h"
#include "libft.h"
#include "../ft_errno.h"
#include "../env/env_manip.h"

View file

@ -6,7 +6,7 @@
/* By: jguelen <jguelen@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/23 15:02:59 by jguelen #+# #+# */
/* Updated: 2025/03/19 09:18:53 by jguelen ### ########.fr */
/* Updated: 2025/03/19 16:48:12 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
@ -132,7 +132,7 @@ char fits_pattern(char *str, t_worddesc *pattern)
{
pattern_check[i] = ft_calloc(pattern_len + 1, sizeof(char));
if (!pattern_check[i])
return (destroy_pattern_check(pattern_check, str_len + 1), NULL);
return (destroy_pattern_check(pattern_check, str_len + 1), -1);
i++;
}
build_pattern_checks(str, pattern, pattern_check);
@ -153,7 +153,6 @@ t_wordlist *expand_star(t_worddesc *file_pattern)
char *tmp;
i = 0;
clean_pattern(file_pattern);
while (file_pattern->word[i])
{
if (file_pattern->marker[i] != '\'' && file_pattern->marker[i] != '"'

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/06 13:01/15 by khais #+# #+# */
/* Updated: 2025/03/06 18:07:20 by jguelen ### ########.fr */
/* Updated: 2025/03/19 17:16:02 by jguelen ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,6 +17,7 @@
#include "../src/subst/subst.h"
#include "../src/env/env.h"
#include "../src/env/env_manip.h"
#include "../src/parser/wordsplit/wordsplit.h"
static t_worddesc *create_single_word(char *str)
@ -53,72 +54,154 @@ static void test_insert_instr(void)
free(line);
}
//Test to be remade since structures changed in the function calls and returns.
static void test_env_variable_expansion(void)
/*
** NOTE/REMINDER: I currently replace $0 to $9 with nothing but I can change
** this behavior at any point if you would rather we ignored them and therefore
** did not replace those.
*/
void test_env_variable_expansion(void)
{
t_worddesc *token;
t_worddesc *tk;
t_env *env;
struct s_minishell app;
t_wordlist *list;
t_minishell *app;
char *value;
char *key;
app.last_return_value = 0;
token = worddesc_create(ft_strdup("$USER"), W_HASDOLLAR);
if (!token)
assert("ft_strdup failed" && false);
env = NULL;
app.env = env;
env = env_set_entry(&env, "USER", "jguelen");
if (env == NULL)
assert("malloc failed" && false);
env = env_set_entry(&env, "_canard", "coing coing");
if (!env)
assert("malloc failed: slipped on a duck" && false);
tk = word_var_expansion(token, &app);
if (!tk)
assert("internal word_var_expansion failure" && false);
assert_strequal("jguelen", tk->word);
free(token);
token = worddesc_create(ft_strdup("\"$_caneton\""), W_HASDOLLAR);
if (!token)
assert("ft_strdup failed" && false);
tk = word_var_expansion(token, &app);
if (!tk)
assert("internal word_var_expansion failure" && false);
assert_strequal("\"\"", tk->word);
free(token);
token = worddesc_create(ft_strdup("$_canard$USER$''$USER\"\"\"$_canard\"$"), W_HASDOLLAR);
if (!token)
assert("ft_strdup failed" && false);
tk = word_var_expansion(token, &app);
if (!tk)
assert("internal word_var_expansion failure" && false);
assert_strequal("coing coingjguelen''jguelencoing coing$", tk->word);
free(token);
token = worddesc_create(ft_strdup("$_can'a'rd"), W_HASDOLLAR);
if (!token)
assert("ft_strdup failed" && false);
tk = word_var_expansion(token, &app);
if (!tk)
assert("internal word_var_expansion failure" && false);
assert_strequal("'a'rd", tk->word);
free(token);
env_destroy(env);
value = ft_strdup("/home/jguelen/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin");
key = ft_strdup("PATH");
app = ft_calloc(1, sizeof(t_minishell));
app->env = env_set_entry(&(app->env), key, value);
key = ft_strdup("USER");
value = ft_strdup("jguelen");
app->env = env_set_entry(&(app->env), key, value);
list = minishell_wordsplit("$USER$USER $PATH \"'$USER'$USER\" a$test'b' '$USER'");
list = wordlist_var_expansion(list, app);
assert_strequal("jguelenjguelen", list->word->word);
assert_strequal("/home/jguelen/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/usr/local/go/bin", list->next->word->word);
assert_strequal("\"'jguelen'jguelen\"", list->next->next->word->word);
assert_strequal("a'b'", list->next->next->next->word->word);
assert_strequal("'$USER'", list->next->next->next->next->word->word);
wordlist_destroy(list);
env_destroy(app->env);
free(app);
}
static void test_filename_path_expansion(void)
static void test_cmd_path_expansion(void)
{
t_minishell *app;
char *key;
char *value;
char *cmdpath;
key = ft_strdup("PATH");
value = ft_strdup("/usr/bin");
app = ft_calloc(1, sizeof(t_minishell));
app->env = env_set_entry(&(app->env), key, value);
cmdpath = get_cmdpath("ls", app);
assert_strequal("/usr/bin/ls", cmdpath);
free(cmdpath);
value = ft_strdup(":/usr/bin");
app->env = env_set_entry(&(app->env), key, value);
cmdpath = get_cmdpath("ls", app);
assert_strequal("./ls", cmdpath);
free(cmdpath);
cmdpath = get_cmdpath("peekaboo", app);
assert(cmdpath == NULL);
free(cmdpath);
env_destroy(app->env);
free(app);
}
static void test_filename_star_expansion(void)
{
t_worddesc *filepattern;
t_wordlist *expanded;
t_wordlist *tmp;
//test1
filepattern = create_single_word("*");
expanded = expand_star(filepattern);
tmp = expanded;
assert_strequal("blabla", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("exp", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("expansion.c", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("flagadaPATH", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("ls", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("Makefile", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("metacharacters.c", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("parse_pipelines.c", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("parse_simple_cmds.c", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("test_env_manip.c", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("testutil.c", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("testutil.h", tmp->word->word);
tmp = tmp->next;
assert(tmp);
assert_strequal("word_splitting.c", tmp->word->word);
tmp = tmp->next;
assert(!tmp);
wordlist_destroy(expanded);
worddesc_destroy(filepattern);
//test2
filepattern = create_single_word("**bla'b'*la*");
expanded = expand_star(filepattern);
assert(wordlist_size(expanded) == 1);
assert_strequal("blabla", expanded->word->word);
worddesc_destroy(filepattern);
wordlist_destroy(expanded);
//test zero résultat
filepattern = create_single_word("e*x***p*b");
expanded = expand_star(filepattern);
worddesc_destroy(filepattern);
assert(!expanded);
wordlist_destroy(expanded);
}
void simple_sub_test(void)
{
t_wordlist *list;
t_minishell *app;
char *value;
char *key;
value = ft_strdup("val");
key = ft_strdup("KEY");
app = ft_calloc(1, sizeof(t_minishell));
app->env = env_set_entry(&(app->env), key, value);
list = minishell_wordsplit("v$KEY");
list = wordlist_var_expansion(list, app);
assert_strequal("vval", list->word->word);
wordlist_destroy(list);
env_destroy(app->env);
free(app);
}
int main(void)
{
simple_sub_test();
test_insert_instr();
test_env_variable_expansion();
test_filename_path_expansion();
test_cmd_path_expansion();
test_filename_star_expansion();
return (0);
}