here_doc: perform variable expansion

This commit is contained in:
Khaïs COLIN 2025-03-13 17:26:44 +01:00
parent 5ff990ef50
commit a4c482e8f9
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
7 changed files with 97 additions and 14 deletions

View file

@ -26,6 +26,7 @@ srcs = \
src/env/env_manip.c \
src/env/envp.c \
src/executing/here_doc/here_doc.c \
src/executing/here_doc/here_doc_expand_line.c \
src/executing/here_doc/random_filename.c \
src/executing/here_doc/strip_newline.c \
src/executing/simple_cmd/simple_cmd_execute.c \

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/07 11:42:29 by khais #+# #+# */
/* Updated: 2025/03/11 13:45:09 by khais ### ########.fr */
/* Updated: 2025/03/28 16:43:47 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,6 +16,8 @@
#include <unistd.h>
#include "libft.h"
char *expand_line(char *line, t_minishell *app);
/*
** - check that arguments are not invalid
** - create a random filename
@ -91,11 +93,14 @@ static bool is_marker(char *line, t_worddesc *marker)
**
** Then unlink the file, and return a filedescriptor to it, seeked to the start.
**
** If the marker is not quoted, perform variable expansion on each line. If app
** is NULL, no variable expansions are performed.
**
** If NULL is given as argument, or marker->word is null, or infd is negative,
** or getting a random filename fails, or we are unable to create the file, or
** the file already exists, or any other error occurs, return -1.
*/
int here_doc(t_worddesc *marker, int infd)
int here_doc(t_worddesc *marker, int infd, t_minishell *app)
{
int outfd;
char *filename;
@ -110,7 +115,7 @@ int here_doc(t_worddesc *marker, int infd)
if (is_marker(line, marker))
return (finalize(outfd, filename));
if (!(marker->flags & (W_QUOTED | W_DQUOTE)))
ft_dprintf(STDERR_FILENO, "TODO: handle expansion\n");
line = expand_line(line, app);
line = output_line_and_next(infd, outfd, line);
}
ft_dprintf(STDERR_FILENO, "minishell: warning: here-document delimited by \

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/07 11:41:27 by khais #+# #+# */
/* Updated: 2025/03/11 13:31:52 by khais ### ########.fr */
/* Updated: 2025/03/28 15:57:17 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,8 +14,9 @@
# define HERE_DOC_H
# include "../../parser/worddesc/worddesc.h"
# include "../../minishell.h"
int here_doc(t_worddesc *marker, int infd);
int here_doc(t_worddesc *marker, int infd, t_minishell *app);
char *here_doc_random_filename(void);
char *strip_newline(char *str);

View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* here_doc_expand_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/28 16:43:11 by khais #+# #+# */
/* Updated: 2025/03/28 16:43:41 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../subst/subst.h"
char *expand_line(char *line, t_minishell *app)
{
t_worddesc *word;
word = worddesc_create(line, W_HASDOLLAR,
construct_repeting_char_string(' ', ft_strlen(line)));
word_var_expansion(word, app);
line = ft_strdup(word->word);
worddesc_destroy(word);
return (line);
}

View file

@ -1,9 +1,9 @@
# make gets confused if a file with the same name exists in the sources, so some
# file are prefixed with test_
rawtests = \
test_here_doc \
expansion \
test_cmdlist_use_after_free \
test_here_doc \
test_wordlist_idx \
test_quote_removal \
test_metacharacters \

View file

@ -0,0 +1,3 @@
hello
$USER
end

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/07 11:43:32 by khais #+# #+# */
/* Updated: 2025/03/11 13:45:45 by khais ### ########.fr */
/* Updated: 2025/03/28 16:41:08 by khais ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,6 +16,7 @@
#include <unistd.h>
#include <fcntl.h>
#include "../src/executing/here_doc/here_doc.h"
#include "../src/env/env_manip.h"
static void test_here_doc_filename_generation(void)
{
@ -40,12 +41,12 @@ static void test_here_doc_invalid_args(void)
t_worddesc *marker;
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
assert(-1 == here_doc(NULL, 0));
assert(-1 == here_doc(NULL, 0, NULL));
marker = worddesc_create(NULL, 0, NULL);
assert(-1 == here_doc(marker, 0));
assert(-1 == here_doc(marker, 0, NULL));
worddesc_destroy(marker);
marker = worddesc_create(ft_strdup("EOF"), 0, NULL);
assert(-1 == here_doc(marker, -1));
assert(-1 == here_doc(marker, -1, NULL));
worddesc_destroy(marker);
do_leak_check();
}
@ -59,7 +60,7 @@ static void test_here_doc_only_end_marker(void)
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
marker = worddesc_create(ft_strdup("EOF"), 0, NULL);
infile = open("./here_doc_only_eof.input", O_RDONLY);
result = here_doc(marker, infile);
result = here_doc(marker, infile, NULL);
close(infile);
assert(result != -1);
assert(NULL == get_next_line(result));
@ -77,7 +78,7 @@ static void test_here_doc_input_plus_end_marker(void)
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
marker = worddesc_create(ft_strdup("EOF"), 0, NULL);
infile = open("./here_doc_input_plus_eof.input", O_RDONLY);
result = here_doc(marker, infile);
result = here_doc(marker, infile, NULL);
close(infile);
worddesc_destroy(marker);
assert(result != -1);
@ -95,7 +96,7 @@ static void test_here_doc_input_no_end_marker(void)
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
marker = worddesc_create(ft_strdup("EOF"), 0, NULL);
infile = open("./here_doc_input_no_eof.input", O_RDONLY);
result = here_doc(marker, infile);
result = here_doc(marker, infile, NULL);
close(infile);
worddesc_destroy(marker);
assert(result != -1);
@ -104,12 +105,59 @@ static void test_here_doc_input_no_end_marker(void)
do_leak_check();
}
static void test_here_doc_with_expansion(void)
{
t_worddesc *marker;
int infile;
int result;
t_minishell app;
ft_bzero(&app, sizeof(t_minishell));
app.env = env_set_entry(&app.env, ft_strdup("USER"), ft_strdup("kcolin"));
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
marker = worddesc_create(ft_strdup("EOF"), 0, NULL);
infile = open("./here_doc_with_expansion.input", O_RDONLY);
result = here_doc(marker, infile, &app);
close(infile);
worddesc_destroy(marker);
assert(result != -1);
assert_strequal("hello\n", get_next_line(result));
assert_strequal("kcolin\n", get_next_line(result));
assert_strequal("end\n", get_next_line(result));
close(result);
do_leak_check();
}
static void test_here_doc_with_no_expansion(void)
{
t_worddesc *marker;
int infile;
int result;
t_minishell app;
ft_bzero(&app, sizeof(t_minishell));
app.env = env_set_entry(&app.env, ft_strdup("USER"), ft_strdup("kcolin"));
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
marker = worddesc_create(ft_strdup("EOF"), W_QUOTED, NULL);
infile = open("./here_doc_with_expansion.input", O_RDONLY);
result = here_doc(marker, infile, &app);
close(infile);
worddesc_destroy(marker);
assert(result != -1);
assert_strequal("hello\n", get_next_line(result));
assert_strequal("$USER\n", get_next_line(result));
assert_strequal("end\n", get_next_line(result));
close(result);
do_leak_check();
}
int main(void) {
test_here_doc_filename_generation();
test_here_doc_invalid_args();
test_here_doc_only_end_marker();
test_here_doc_input_plus_end_marker();
test_here_doc_input_no_end_marker();
// TODO: expansion handling
test_here_doc_with_expansion();
test_here_doc_with_no_expansion();
return (0);
}