How fraqioui/minishell works
Main Components
• Input/Initialization Layer
• This module is responsible for initializing global structures (like the environment variables list) and saving stdin/stdout.
• It wraps the shell’s main loop and reads input using the readline function.
• Parsing Layer
• The “parser” directory implements functions for:
▪ Tokenizing the user input (using defined token types for pipes, redirections, logical operators, etc.).
▪ Handling syntax validations (ensuring quotes, parentheses, and redirections are well-formed).
▪ Re-ordering commands using the shunting yard algorithm to generate a proper list of tokens.
▪ Building an abstract syntax tree (AST) or command tree from the re-ordered tokens.
• Expansion Module
• Located under executor/expander and utils/tokenize, this module handles:
▪ Environment variable expansion.
▪ Special handling for wildcards (using the directory access routines and matching algorithm).
▪ Quote elimination and similar string processing before execution.
• Execution Layer
• Split between “executor/executor” and “executor/builtins,” this layer covers:
▪ Processing of logical operators (AND/OR) and pipes.
▪ Forking processes for non-builtin commands.
▪ Builtin command handling (cd, echo, env, export, unset, pwd, exit).
▪ Managing redirections and file descriptor duplication.
• Utility/Helper Functions
• Helpers in directories like utils/build, utils/libft, and utils/utils provide:
▪ Data structure building (e.g., command tree nodes).
▪ Custom printing (ft_printf) and various string/memory utilities.
Relationships and Interactions
• User Input flows into the Initialization component, which then passes the command string to the Parser.
• The Parser tokenizes, validates, re-orders, and builds the command tree (AST).
• The AST is then passed to the Executor:
• The Executor first checks for builtins; if the command is not a builtin, it will perform necessary expansions.
• The Execution layer then handles redirections, forks processes if needed, and finally calls execve for external commands.
• Utility modules support all layers by offering common functionalities that include token handling, error management, and memory utilities.
Architectural Patterns
• Layered Architecture: The design is separable into initialization, parsing, expansion, and execution layers.
• AST Construction: The shell uses the shunting yard algorithm to convert infix shell commands into an abstract syntax tree enabling recursive execution.
• Recursive Tree Evaluation: Execution is performed by recursively traversing the AST, processing operators (like “&&”, “||”, “|”) and individual commands.
• Modularity and Reuse: Helper functions and utility libraries (e.g., libft) ensure code reuse and cleaner separation of concerns.
Relevant Technologies/Frameworks
• C standard libraries for system calls (e.g., fork, execve, dup, chdir).
• Readline for user input.
• POSIX APIs for process management, pipe operations, and redirection.
• Custom libraries (libft, ft_printf) that provide support functions which are part of the overall architecture.
Detailed Steps for the Principal Software Engineer
1. User Input (readline) → Initialization.
2. Initialization → Parser:
▪ Tokenizer (handle quotes and syntax errors).
▪ Re-order command (applying shunting yard algorithm).
▪ AST/Tree Builder.
3. Parser → Expander:
▪ Environment variable expansion.
▪ Wildcard and quote removal.
4. Expander → Executor:
▪ Branching into Builtins vs. Forking for External Commands.
▪ Manage redirections, heredocs, and pipes.
5. Supporting Utilities work throughout to provide essential functions.
• Annotations for any module that implements a key algorithm (for the shunting yard algorithm and for wildcard matching).