MiniDevil As beautiful as a shell
main.c File Reference

Shell entry point. More...

#include <readline/readline.h>
#include <readline/history.h>
#include "libft.h"
#include "get_next_line.h"
#include "signals.h"
#include "token.h"
#include "parser.h"
#include "ast.h"
#include "executor.h"
#include "env.h"
#include "minishell_ui.h"
+ Include dependency graph for main.c:

Detailed Description

Shell entry point.

Definition in file main.c.

Functions

static int process_input (char *input, t_shell *shell)
 tokenize -> expand -> parse -> execute More...
 
static char * read_input (t_shell *shell)
 Read 1 line of input from the user. More...
 
static void handle_input (char *input, t_shell *shell)
 Filter and dispatch input to processing. More...
 
static void main_loop (t_shell *shell)
 Main REPL (standard mode) More...
 
int main (int ac, char **av, char **envp)
 Entry point. More...
 

Function Documentation

◆ process_input()

static int process_input ( char *  input,
t_shell shell 
)
static

tokenize -> expand -> parse -> execute

On any stage failure, cleans up and returns an error status

Parameters
inputRaw input string
shellShell context
Returns
Exit status of the executed command or 2 on parse error
Note
Heredocs are collected before the execution begins

Definition at line 35 of file main.c.

References collect_heredocs(), t_shell::current_ast, executor(), expand_all_tokens(), free_ast(), free_token_list(), parse(), and tokenize().

Referenced by handle_input().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ read_input()

static char* read_input ( t_shell shell)
static

Read 1 line of input from the user.

Uses readline with a prompt in interactive mode and get_next_line() otherwise (strips the trailing \n)

Parameters
shellShell context
Returns
Newly allocated input line or NULL on EOF

Definition at line 72 of file main.c.

References t_shell::interactive.

Referenced by main_loop().

+ Here is the caller graph for this function:

◆ handle_input()

static void handle_input ( char *  input,
t_shell shell 
)
static

Filter and dispatch input to processing.

  • Skips empty and whitespace only inputs
  • Adds non empty input to readline history in interactive mode
Parameters
inputRaw input string
shellShell context

Definition at line 98 of file main.c.

References t_shell::exit_status, t_shell::interactive, and process_input().

Referenced by main_loop().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ main_loop()

static void main_loop ( t_shell shell)
static

Main REPL (standard mode)

setup signals -> read input -> handle SIGINT -> process

  • On EOF (NULL input) it prints "exit" in interactive mode & breaks
  • It frees input after each iteration
Parameters
shellShell context (the running flag controls the loop)

Definition at line 123 of file main.c.

References t_shell::current_input, t_shell::exit_status, g_signal, handle_input(), t_shell::interactive, read_input(), t_shell::running, setup_execution_signals(), and setup_interactive_signals().

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ main()

int main ( int  ac,
char **  av,
char **  envp 
)

Entry point.

Initializes the shell state and eithers enters UI mode (–ui flag) or the standard REPL.

  • Cleans up env and GNL on exit.
Parameters
acArgument count
avArgument vector (–ui triggers UI mode)
envpEnvironment passed by the OS
Returns
The shell's final exit status

Definition at line 163 of file main.c.

References t_shell::env, t_shell::exit_status, free_env_list(), init_env(), t_shell::interactive, t_shell::is_child, main_loop(), run_ui_mode(), t_shell::ui, and t_shell::ui_mode.

+ Here is the call graph for this function:

Go to the source code of this file.