redirection parings: handle redirections not at start

This commit is contained in:
Khaïs COLIN 2025-03-07 15:18:06 +01:00
parent 4116c2d92f
commit 06dd3c3e83
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
2 changed files with 70 additions and 13 deletions

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 <stdlib.h>
#include "redirection_type.h"
#include <unistd.h>
/*
** 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
** 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)
{
t_worddesc *marker;
t_redirection *redirection;
t_redir_type type;
size_t i;
type = redir_type_from_worddesc(cmd->words->word);
if (type == REDIR_OUTPUT)
i = 0;
while (wordlist_get(cmd->words, i) != NULL)
{
worddesc_destroy(wordlist_pop(&cmd->words));
marker = wordlist_pop(&cmd->words);
redirection = redirection_create(type, marker);
if (redirection == NULL)
return (NULL);
cmd->redirections = redir_list_push(cmd->redirections, redirection);
if (cmd->redirections == NULL)
return (redirection_destroy(redirection), NULL);
type = redir_type_from_worddesc(wordlist_get(cmd->words, i));
if (type == REDIR_OUTPUT)
{
wordlist_destroy_idx(&cmd->words, i);
marker = wordlist_pop_idx(&cmd->words, i);
redirection = redirection_create(type, marker);
if (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);
}