mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
redirection parings: handle redirections not at start
This commit is contained in:
parent
4116c2d92f
commit
06dd3c3e83
2 changed files with 70 additions and 13 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/07 12:30:04 by khais #+# #+# */
|
/* Created: 2025/03/07 12:30:04 by khais #+# #+# */
|
||||||
/* Updated: 2025/03/07 14:58:22 by khais ### ########.fr */
|
/* Updated: 2025/03/10 16:11:46 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -15,6 +15,7 @@
|
||||||
#include "redirection_list.h"
|
#include "redirection_list.h"
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "redirection_type.h"
|
#include "redirection_type.h"
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Modify the given simple_cmd in-place, removing all redirection directives
|
** Modify the given simple_cmd in-place, removing all redirection directives
|
||||||
|
|
@ -25,24 +26,42 @@
|
||||||
**
|
**
|
||||||
** In case of error, return null (this may leave the given command in an
|
** In case of error, return null (this may leave the given command in an
|
||||||
** inconsitent state) (but it can still be freed correctly).
|
** inconsitent state) (but it can still be freed correctly).
|
||||||
|
**
|
||||||
|
** Algorithm:
|
||||||
|
**
|
||||||
|
** iterate though the words of the given cmd
|
||||||
|
**
|
||||||
|
** if a redirection token is detected, the current word will be the operator
|
||||||
|
** token. Destroy it.
|
||||||
|
**
|
||||||
|
** the current word is now the marker. save it and destroy it
|
||||||
|
**
|
||||||
|
** create a new redirection with the operator and the maker, and save it.
|
||||||
*/
|
*/
|
||||||
struct s_simple_cmd *parse_redirections(struct s_simple_cmd *cmd)
|
struct s_simple_cmd *parse_redirections(struct s_simple_cmd *cmd)
|
||||||
{
|
{
|
||||||
t_worddesc *marker;
|
t_worddesc *marker;
|
||||||
t_redirection *redirection;
|
t_redirection *redirection;
|
||||||
t_redir_type type;
|
t_redir_type type;
|
||||||
|
size_t i;
|
||||||
|
|
||||||
type = redir_type_from_worddesc(cmd->words->word);
|
i = 0;
|
||||||
if (type == REDIR_OUTPUT)
|
while (wordlist_get(cmd->words, i) != NULL)
|
||||||
{
|
{
|
||||||
worddesc_destroy(wordlist_pop(&cmd->words));
|
type = redir_type_from_worddesc(wordlist_get(cmd->words, i));
|
||||||
marker = wordlist_pop(&cmd->words);
|
if (type == REDIR_OUTPUT)
|
||||||
redirection = redirection_create(type, marker);
|
{
|
||||||
if (redirection == NULL)
|
wordlist_destroy_idx(&cmd->words, i);
|
||||||
return (NULL);
|
marker = wordlist_pop_idx(&cmd->words, i);
|
||||||
cmd->redirections = redir_list_push(cmd->redirections, redirection);
|
redirection = redirection_create(type, marker);
|
||||||
if (cmd->redirections == NULL)
|
if (redirection == NULL)
|
||||||
return (redirection_destroy(redirection), NULL);
|
return (NULL);
|
||||||
|
cmd->redirections = redir_list_push(cmd->redirections, redirection);
|
||||||
|
if (cmd->redirections == NULL)
|
||||||
|
return (redirection_destroy(redirection), NULL);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
return (cmd);
|
return (cmd);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,8 +5,8 @@
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/07 14:47/34 by khais #+# #+# */
|
/* Created: 2025/03/10 16:13/18 by khais #+# #+# */
|
||||||
/* Updated: 2025/03/07 14:47:34 by khais ### ########.fr */
|
/* Updated: 2025/03/10 16:13:18 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -16,6 +16,7 @@
|
||||||
#include "../src/postprocess/redirections/redirection_parsing.h"
|
#include "../src/postprocess/redirections/redirection_parsing.h"
|
||||||
#include "../src/postprocess/redirections/redirection_parsing.h"
|
#include "../src/postprocess/redirections/redirection_parsing.h"
|
||||||
#include "../src/postprocess/redirections/redirection_list.h"
|
#include "../src/postprocess/redirections/redirection_list.h"
|
||||||
|
#include "ft_printf.h"
|
||||||
#include "testutil.h"
|
#include "testutil.h"
|
||||||
|
|
||||||
static t_simple_cmd *parse_simple_cmd(char *input)
|
static t_simple_cmd *parse_simple_cmd(char *input)
|
||||||
|
|
@ -27,6 +28,7 @@ static t_simple_cmd *parse_simple_cmd(char *input)
|
||||||
|
|
||||||
static void test_redirection_parsing_no_redirections(void)
|
static void test_redirection_parsing_no_redirections(void)
|
||||||
{
|
{
|
||||||
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||||
t_simple_cmd *cmd = parse_simple_cmd("echo hello world");
|
t_simple_cmd *cmd = parse_simple_cmd("echo hello world");
|
||||||
assert(parse_redirections(cmd) != NULL);
|
assert(parse_redirections(cmd) != NULL);
|
||||||
assert(cmd->redirections == NULL);
|
assert(cmd->redirections == NULL);
|
||||||
|
|
@ -36,6 +38,7 @@ static void test_redirection_parsing_no_redirections(void)
|
||||||
|
|
||||||
static void test_redirection_parsing_output_at_start(void)
|
static void test_redirection_parsing_output_at_start(void)
|
||||||
{
|
{
|
||||||
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||||
t_simple_cmd *cmd = parse_simple_cmd(">outfile echo hello world");
|
t_simple_cmd *cmd = parse_simple_cmd(">outfile echo hello world");
|
||||||
assert(parse_redirections(cmd) != NULL);
|
assert(parse_redirections(cmd) != NULL);
|
||||||
assert_strequal("outfile", cmd->redirections->redirection->marker->word);
|
assert_strequal("outfile", cmd->redirections->redirection->marker->word);
|
||||||
|
|
@ -49,10 +52,45 @@ static void test_redirection_parsing_output_at_start(void)
|
||||||
do_leak_check();
|
do_leak_check();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_redirection_parsing_output_at_end(void)
|
||||||
|
{
|
||||||
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||||
|
t_simple_cmd *cmd = parse_simple_cmd("echo hello world > outfile");
|
||||||
|
assert(parse_redirections(cmd) != NULL);
|
||||||
|
assert_strequal("echo", cmd->words->word->word);
|
||||||
|
assert_strequal("hello", cmd->words->next->word->word);
|
||||||
|
assert_strequal("world", cmd->words->next->next->word->word);
|
||||||
|
assert(NULL == cmd->words->next->next->next);
|
||||||
|
assert_strequal("outfile", cmd->redirections->redirection->marker->word);
|
||||||
|
assert(cmd->redirections->redirection->type == REDIR_OUTPUT);
|
||||||
|
assert(cmd->redirections->next == NULL);
|
||||||
|
simple_cmd_destroy(cmd);
|
||||||
|
do_leak_check();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_redirection_parsing_output_in_middle(void)
|
||||||
|
{
|
||||||
|
ft_dprintf(STDERR_FILENO, "==> %s <==\n", __FUNCTION__);
|
||||||
|
t_simple_cmd *cmd = parse_simple_cmd("echo hello >outfile world");
|
||||||
|
assert(parse_redirections(cmd) != NULL);
|
||||||
|
assert_strequal("echo", cmd->words->word->word);
|
||||||
|
assert_strequal("hello", cmd->words->next->word->word);
|
||||||
|
assert_strequal("world", cmd->words->next->next->word->word);
|
||||||
|
assert(NULL == cmd->words->next->next->next);
|
||||||
|
assert_strequal("outfile", cmd->redirections->redirection->marker->word);
|
||||||
|
assert(cmd->redirections->redirection->type == REDIR_OUTPUT);
|
||||||
|
assert(cmd->redirections->next == NULL);
|
||||||
|
simple_cmd_destroy(cmd);
|
||||||
|
do_leak_check();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
test_redirection_parsing_no_redirections();
|
test_redirection_parsing_no_redirections();
|
||||||
test_redirection_parsing_output_at_start();
|
test_redirection_parsing_output_at_start();
|
||||||
|
test_redirection_parsing_output_at_end();
|
||||||
|
test_redirection_parsing_output_in_middle();
|
||||||
// check operator alone without further tokens
|
// check operator alone without further tokens
|
||||||
|
// check null input
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue