mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
redirection parsing: handle other types of redirection
This commit is contained in:
parent
47ac767f2a
commit
d196649106
3 changed files with 60 additions and 2 deletions
|
|
@ -77,7 +77,7 @@ struct s_simple_cmd *parse_redirections(struct s_simple_cmd *cmd)
|
||||||
while (wordlist_get(cmd->words, i) != NULL)
|
while (wordlist_get(cmd->words, i) != NULL)
|
||||||
{
|
{
|
||||||
type = redir_type_from_worddesc(wordlist_get(cmd->words, i));
|
type = redir_type_from_worddesc(wordlist_get(cmd->words, i));
|
||||||
if (type == REDIR_OUTPUT)
|
if (type != REDIR_INVALID)
|
||||||
if (redirection_found(cmd, i, type) == NULL)
|
if (redirection_found(cmd, i, type) == NULL)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
i++;
|
i++;
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/07 14:56:29 by khais #+# #+# */
|
/* Created: 2025/03/07 14:56:29 by khais #+# #+# */
|
||||||
/* Updated: 2025/03/07 14:57:32 by khais ### ########.fr */
|
/* Updated: 2025/03/10 16:51:39 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -17,5 +17,11 @@ t_redir_type redir_type_from_worddesc(t_worddesc *word)
|
||||||
{
|
{
|
||||||
if (ft_strcmp(">", word->word) == 0)
|
if (ft_strcmp(">", word->word) == 0)
|
||||||
return (REDIR_OUTPUT);
|
return (REDIR_OUTPUT);
|
||||||
|
if (ft_strcmp("<", word->word) == 0)
|
||||||
|
return (REDIR_INPUT);
|
||||||
|
if (ft_strcmp("<<", word->word) == 0)
|
||||||
|
return (REDIR_HERE_DOC);
|
||||||
|
if (ft_strcmp(">>", word->word) == 0)
|
||||||
|
return (REDIR_APPEND);
|
||||||
return (REDIR_INVALID);
|
return (REDIR_INVALID);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -104,6 +104,55 @@ static void test_redirection_parsing_null(void)
|
||||||
do_leak_check();
|
do_leak_check();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void test_redirection_parsing_input_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_INPUT);
|
||||||
|
assert(cmd->redirections->next == NULL);
|
||||||
|
simple_cmd_destroy(cmd);
|
||||||
|
do_leak_check();
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_redirection_parsing_here_doc_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_HERE_DOC);
|
||||||
|
assert(cmd->redirections->next == NULL);
|
||||||
|
simple_cmd_destroy(cmd);
|
||||||
|
do_leak_check();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void test_redirection_parsing_append_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_APPEND);
|
||||||
|
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();
|
||||||
|
|
@ -112,5 +161,8 @@ int main(void) {
|
||||||
test_redirection_parsing_output_in_middle();
|
test_redirection_parsing_output_in_middle();
|
||||||
test_redirection_parsing_output_no_filename_at_end();
|
test_redirection_parsing_output_no_filename_at_end();
|
||||||
test_redirection_parsing_null();
|
test_redirection_parsing_null();
|
||||||
|
test_redirection_parsing_input_in_middle();
|
||||||
|
test_redirection_parsing_here_doc_in_middle();
|
||||||
|
test_redirection_parsing_append_in_middle();
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue