MiniDevil As beautiful as a shell
parser_redir_utils.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* parser_redir_utils.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2026/02/21 21:48:21 by baelgadi #+# #+# */
9 /* Updated: 2026/03/04 09:03:31 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "libft.h"
14 #include "parser.h"
15 
22 static int is_redir(t_ast *node)
23 {
24  if (!node)
25  return (0);
26  return (node->type >= NODE_REDIR_IN && node->type <= NODE_REDIR_HEREDOC);
27 }
28 
40 {
41  t_ast *prev;
42  t_ast *next;
43 
44  if (!is_redir(node))
45  return (node);
46  prev = node;
47  while (is_redir(prev))
48  prev = prev->data.redir.cmd;
49  while (is_redir(node))
50  {
51  next = node->data.redir.cmd;
52  node->data.redir.cmd = prev;
53  prev = node;
54  node = next;
55  }
56  return (prev);
57 }
58 
69 char *join_connected_delim(t_token **tokens, int *quoted)
70 {
71  char *file;
72  char *tmp;
73 
74  file = ft_strdup((*tokens)->value);
75  if (!file)
76  return (NULL);
77  *quoted = ((*tokens)->quote_type != QUOTE_NONE);
78  while ((*tokens)->connected && (*tokens)->next)
79  {
80  *tokens = (*tokens)->next;
81  if ((*tokens)->quote_type != QUOTE_NONE)
82  *quoted = 1;
83  tmp = ft_strjoin(file, (*tokens)->value);
84  free(file);
85  if (!tmp)
86  return (NULL);
87  file = tmp;
88  }
89  *tokens = (*tokens)->next;
90  return (file);
91 }
Parser entry point & sub parser prototypes.
char * join_connected_delim(t_token **tokens, int *quoted)
Join connected tokens in a single string.
static int is_redir(t_ast *node)
Check if an AST node is of redirection type.
t_ast * reverse_redir_chain(t_ast *node)
Reverse a redirection chain for left to right execution order.
@ NODE_REDIR_HEREDOC
Definition: structs.h:57
@ NODE_REDIR_IN
Definition: structs.h:54
@ QUOTE_NONE
Definition: structs.h:42
AST node (union based ↑)
Definition: structs.h:151
t_ast_data data
cmd, redir or binary data
Definition: structs.h:153
t_node_type type
To determine which union member to pick.
Definition: structs.h:152
struct s_ast * cmd
Command subtree.
Definition: structs.h:113
Lexer token (singly linked list)
Definition: structs.h:71
struct s_token * next
Next token.
Definition: structs.h:76
t_redir_node redir
Definition: structs.h:142