Module Main - alteixeira20/42_minishell GitHub Wiki
This module serves as the starting point of the shell.
It initializes the shell environment, enters the main event loop, and handles user input, parsing, and command execution.
main(int ac, char **av, char **env): Initializes the shell structure ('t_msh'), sets environment variables, and starts the main event loop ('loop').
-
Initializes:
- 't_msh' structure using 'ft_calloc'.
- Environment variables with 'init_sh'.
-
Calls:
- 'loop' — Handles user interaction and command processing.
-
Cleans Up:
- Frees 'env', command list, and the main shell structure before exit.
-
Prints
"exit\n"
when the shell ends.
loop(t_msh *sh) — Main event loop of the shell.
-
Handles:
- Signal setup with 'set_interactive_signals'.
- Prompts the user for input using 'read_multiline_input'.
- Parses and executes each command line with 'handle_input_line'.
- Breaks on EOF (Ctrl+D) or a failure in parsing.
-
Notes:
- Runs infinitely until an EOF or an error occurs.
- Uses readline to fetch user input with proper signal handling.
read_multiline_input(t_msh *sh) — Reads a line of input from the user, with continuation support for multi-line commands.
-
Uses:
- 'build_prompt()' to display the shell prompt.
- 'readline()' to capture user input.
- 'read_continuation_loop()' for handling line continuations (
|
or unclosed quotes).
-
Returns:
- The complete command line, or
NULL
if EOF is detected.
- The complete command line, or
read_continuation_loop(char *line, t_msh *sh) — Handles the continuation of user input if a command is incomplete (e.g., ends with `|`).
-
Loops Until:
- Syntax is complete or 'readline()' returns
NULL
.
- Syntax is complete or 'readline()' returns
-
Uses:
- 'parse_input()' for token generation.
- 'check_cmd_syntax()' to detect incomplete commands.
-
Memory Management:
- Frees previous lines and tokens, allocates new memory for concatenated input.
handle_input_line(t_msh *sh, char *line) — Processes a single line of input.
-
History Handling:
- Adds valid commands to history with 'add_history()'.
-
Parsing and Execution:
- Parses tokens with 'parse_input()'.
- Syntax is checked using 'check_cmd_syntax()'.
- Executes the AST with 'exec_ast()' if syntax is valid.
-
Error Handling:
- Frees tokens and input line if errors are detected.
-
Global Variables:
- Updates 'g_exit' with the status of the execution.
-
g_exit:
- Type: 'int'
- Usage: Holds the last exit status of the shell for proper
$?
support. - Updated after every command execution.
- Init → 'init_sh()' is called to set up the environment.
- Parser → Uses 'parse_input()' for command tokenization.
- Execution → Uses 'exec_ast()' for command execution.
- Signals → Calls 'set_interactive_signals()' for proper signal behavior.
- Cleanup → Calls 'free_env_array()', 'free_cmd()', and 'free_minishell()' during cleanup.
- This module represents the main lifecycle of the shell, from initialization to termination.
- It is entirely interactive, handling multi-line commands gracefully.
- Memory management is ensured through careful 'free()' calls.