mirror of
https://codeberg.org/la-chouette/minishell.git
synced 2025-12-06 07:28:09 +01:00
redirection parsing refactor: put redirection found actions in subroutine
This commit is contained in:
parent
5df876bba3
commit
d9dfac106d
1 changed files with 35 additions and 20 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/10 16:40:17 by khais ### ########.fr */
|
/* Updated: 2025/03/11 14:57:56 by khais ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -18,6 +18,38 @@
|
||||||
#include "../../ft_errno.h"
|
#include "../../ft_errno.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
**
|
||||||
|
** 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 marker, and save it.
|
||||||
|
**
|
||||||
|
** in case of error, return NULL, and set ft_errno
|
||||||
|
*/
|
||||||
|
static struct s_simple_cmd *redirection_found(
|
||||||
|
struct s_simple_cmd *cmd,
|
||||||
|
size_t i,
|
||||||
|
t_redir_type type)
|
||||||
|
{
|
||||||
|
t_worddesc *marker;
|
||||||
|
t_redirection *redirection;
|
||||||
|
|
||||||
|
wordlist_destroy_idx(&cmd->words, i);
|
||||||
|
marker = wordlist_pop_idx(&cmd->words, i);
|
||||||
|
if (marker == NULL)
|
||||||
|
return (ft_errno(FT_EMALFORMED_REDIRECTION), NULL);
|
||||||
|
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);
|
||||||
|
return (cmd);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Modify the given simple_cmd in-place, removing all redirection directives
|
** Modify the given simple_cmd in-place, removing all redirection directives
|
||||||
** from words, parsing them, and storing them in redirection.
|
** from words, parsing them, and storing them in redirection.
|
||||||
|
|
@ -32,17 +64,10 @@
|
||||||
**
|
**
|
||||||
** iterate though the words of the given cmd
|
** iterate though the words of the given cmd
|
||||||
**
|
**
|
||||||
** if a redirection token is detected, the current word will be the operator
|
** if a redirection is detected, call redirection_found
|
||||||
** 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_redirection *redirection;
|
|
||||||
t_redir_type type;
|
t_redir_type type;
|
||||||
size_t i;
|
size_t i;
|
||||||
|
|
||||||
|
|
@ -51,18 +76,8 @@ struct s_simple_cmd *parse_redirections(struct s_simple_cmd *cmd)
|
||||||
{
|
{
|
||||||
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_OUTPUT)
|
||||||
{
|
if (redirection_found(cmd, i, type) == NULL)
|
||||||
wordlist_destroy_idx(&cmd->words, i);
|
|
||||||
marker = wordlist_pop_idx(&cmd->words, i);
|
|
||||||
if (marker == NULL)
|
|
||||||
return (ft_errno(FT_EMALFORMED_REDIRECTION), NULL);
|
|
||||||
redirection = redirection_create(type, marker);
|
|
||||||
if (redirection == NULL)
|
|
||||||
return (NULL);
|
return (NULL);
|
||||||
cmd->redirections = redir_list_push(cmd->redirections, redirection);
|
|
||||||
if (cmd->redirections == NULL)
|
|
||||||
return (redirection_destroy(redirection), NULL);
|
|
||||||
}
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
return (cmd);
|
return (cmd);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue