MiniDevil As beautiful as a shell
execute_utils.c
Go to the documentation of this file.
1 /* ************************************************************************** */
2 /* */
3 /* ::: :::::::: */
4 /* execute_utils.c :+: :+: :+: */
5 /* +:+ +:+ +:+ */
6 /* By: baelgadi <baelgadi@student.42.fr> +#+ +:+ +#+ */
7 /* +#+#+#+#+#+ +#+ */
8 /* Created: 2026/02/17 17:49:23 by zotaj-di #+# #+# */
9 /* Updated: 2026/03/04 07:23:01 by baelgadi ### ########.fr */
10 /* */
11 /* ************************************************************************** */
12 
13 #include "minishell_ui.h"
14 #include "token.h"
15 #include "parser.h"
16 #include "ast.h"
17 #include "executor.h"
18 
30 static void process_output_char(t_ui *ui, char c, char *line, int *line_len)
31 {
32  if (c == '\n')
33  {
34  line[*line_len] = '\0';
35  out_add_line(&ui->out, line);
36  *line_len = 0;
37  }
38  else if (*line_len < BUF_SIZE - 2)
39  {
40  line[*line_len] = c;
41  (*line_len)++;
42  }
43 }
44 
53 void read_output_from_fd(t_ui *ui, int fd)
54 {
55  char buf[BUF_SIZE];
56  char line[BUF_SIZE];
57  int line_len;
58  int bytes;
59  int i;
60 
61  line_len = 0;
62  bytes = read(fd, buf, sizeof(buf) - 1);
63  while (bytes > 0)
64  {
65  buf[bytes] = '\0';
66  i = -1;
67  while (++i < bytes)
68  process_output_char(ui, buf[i], line, &line_len);
69  bytes = read(fd, buf, sizeof(buf) - 1);
70  }
71  if (line_len > 0)
72  {
73  line[line_len] = '\0';
74  out_add_line(&ui->out, line);
75  }
76 }
77 
88 int process_ui_input(char *input, t_shell *shell)
89 {
90  t_token *tokens;
91  t_ast *ast;
92  int status;
93 
94  tokens = tokenize(input);
95  if (!tokens)
96  return (2);
97  if (expand_all_tokens(tokens, shell) < 0)
98  {
99  free_token_list(tokens);
100  return (1);
101  }
102  ast = parse(tokens);
103  free_token_list(tokens);
104  if (!ast)
105  return (2);
106  if (collect_heredocs(ast, shell) == -1)
107  {
108  free_ast(ast);
109  return (130);
110  }
111  status = executor(ast, shell);
112  free_ast(ast);
113  return (status);
114 }
AST node creation and destruction prototypes.
void free_ast(t_ast *node)
Recursively free an entire AST tree.
Definition: ast_utils.c:46
int process_ui_input(char *input, t_shell *shell)
Full pipeline inside the UI.
Definition: execute_utils.c:88
void read_output_from_fd(t_ui *ui, int fd)
Read all available bytes from fd and split into output lines.
Definition: execute_utils.c:53
static void process_output_char(t_ui *ui, char c, char *line, int *line_len)
Process 1 output character from the child pipe into line buffers.
Definition: execute_utils.c:30
int executor(t_ast *node, t_shell *shell)
Main AST executor that dispatches by node type.
Definition: executor.c:42
Executor prototypes for commands, pipes, redirections & heredocs.
int expand_all_tokens(t_token *tokens, t_shell *shell)
Expand all token values before parsing.
Definition: expander.c:150
int collect_heredocs(t_ast *node, t_shell *shell)
Pre collect all heredocs in the AST before the execution phase.
UI mode structures, macros & function prototypes.
#define BUF_SIZE
Definition: minishell_ui.h:86
void out_add_line(t_out *out, const char *line)
Append a duplicate of a line to the output buffer.
Definition: output.c:23
t_ast * parse(t_token *tokens)
Parse a token list into an AST.
Definition: parser.c:26
Parser entry point & sub parser prototypes.
AST node (union based ↑)
Definition: structs.h:151
Shell state.
Definition: structs.h:164
Lexer token (singly linked list)
Definition: structs.h:71
UI state.
Definition: minishell_ui.h:167
t_out out
Output state.
Definition: minishell_ui.h:170
void free_token_list(t_token *head)
Free the entire token linked list.
Definition: token.c:64
Lexer, tokenizer, expander, and quote handling prototypes.
t_token * tokenize(char *input)
Tokenize the input string into a linked list of tokens.
Definition: tokenizer.c:144