How boughatat911/minishell works
Project Type & Purpose
• Minishell is a standalone, minimal shell interpreter (a command-line interface tool) written in C.
• Purpose: read user commands, parse/tokenize them, handle built-ins and external binaries with redirections, pipes, environment management, and process control.
File-Structure Analysis
• Top-Level:
• minishell.c / minishell.h – entry point and shared types
• Makefile – build config
• builtins/ – each builtin (echo, cd, pwd, export, unset, env, exit)
• parsing/ – three submodules:
– token/ (lexical analysis & syntax error detection)
– expand/ (environment‐variable and quote expansion)
– cmd/ (building a linked list of t_cmd structures, including heredoc & redirection logic)
• execution/ – orchestrates fork/exec, pipes, builtin dispatch, IO setup
• tools/ – utilities (custom ft_malloc with leak detection, string helpers)
README-Derived Insights
• Core modules: CLI loop, signal handling (SIGINT), memory management, error handling.
• Data structures: t_shell (global state), t_token (lexer output), t_cmd (command pipeline).
• External dependency: readline library for line editing & history.
Main Components
A. CLI & Main Loop
• readline() → raw_input
• signal handlers installed/managed
B. Parser Layer
• Tokenizer (parsing/token) → list of t_token
• Syntax checker
• Expander (parsing/expand) → tokens with variables & quotes processed
• Command assembler (parsing/cmd) → linked list of t_cmd, each with I/O fds and args
C. Execution Layer
• Builtin dispatcher (execution/builtins.c)
• External exec (fork + execve in execution/exec.c)
• Pipe-chain handler (setting up pipe_fd between t_cmd nodes)
• I/O redirection setup per t_cmd (in, out fields)
• Signal restoration in children
D. Environment Manager
• t_env linked list (builtins/add_env, my_env, export, unset, etc.)
• PATH resolution (execution/check_path_cmd)
E. Memory & Tools
• ft_malloc wrapper & leak detection (tools/ft_malloc.c)
• general utilities for strings, lists, error messages
Component Interactions & Data Flow
• User types command → CLI reads with readline → passes to Parser
• Parser emits token stream → expand variables/quotes → build t_cmd list
• Execution module inspects each t_cmd:
• If builtin → call builtin implementation (cd, export, etc.)
• Else → resolve PATH, fork child, set up redirections & pipes, execve
• Parent collects child PIDs & statuses, updates t_shell.exit_status
• Builtins update t_env structure in-process
• Loop repeats until exit
Architectural Patterns & Principles
• Modular layering: CLI → Parser → Executor → OS/procs
• Single responsibility per directory
• Linked data structures to represent streams (token, cmd, env)
• Custom memory management to track allocations & leaks
• Signal‐safe handling and restoration in children