Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

149 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🐚 minishell

A mimic of bash built as part of the 42 Prague curriculum in C.


Architecture

├── Lexical Analysis (lexus/)
│   ├── tokenise.c     → Token creation and management
│   ├── lexer.c        → Main lexical analyser
│   ├── quotes.c       → Quote handling ('single', "double")
│   ├── dollar.c       → Environment variable expansion ($VAR, $?)
│   └── operators.c    → Pipe and redirection operators
│
├── Execution Engine (execute/)
│   ├── execute.c      → Command execution coordinator
│   ├── builtins.c     → Built-in command dispatcher
│   ├── heredoc.c      → Here-document implementation
│   └── redirections.c → I/O redirection handling
│
├── Process Management
│   ├── processes.c    → Fork/exec/wait handling
│   ├── piping.c       → Multi-pipe coordination
│   └── signal.c       → Signal management (SIGINT, SIGQUIT)
│
└── Built-ins (builtins/)
    ├── cd.c           → Directory navigation + PWD/OLDPWD
    ├── export.c       → Environment variable management
    ├── echo.c         → Text output with -n flag
    └── env_pwd_exit.c → Environment display and exit handling

Features

  • Lexical analysis: Tokenises input with quote handling, variable expansion, and operator recognition
  • Piping: Multi-stage pipeline execution with proper file descriptor management
  • Redirections: Input (<), output (>), append (>>), and here-documents (<<)
  • Variable expansion: Environment variables ($VAR) and exit status ($?)
  • Built-in commands: cd, echo, pwd, export, unset, env, exit
  • Signal handling: Proper SIGINT/SIGQUIT handling in interactive and non-interactive modes
  • Command history: Readline integration with history persistence

Command example:

minishell> export SEARCH_TERM="minishell"
minishell> echo "Finding all files containing: $SEARCH_TERM" > search_log.txt
minishell> ls -la src/ | grep "\.c$" | head -5 >> search_log.txt
minishell> cat << DELIMITER
This is a test file for minishell
Testing variable expansion: $SEARCH_TERM
Another line with $PWD directory
Final line of input
DELIMITER

Step-by-step execution:

  1. Variable export (export.c):

    • handle_export() sets SEARCH_TERM=minishell in environment
    • Updates prompt->env_copy with new variable
  2. Variable expansion and output redirection (dollar.c, redir_handlers.c):

    • dollar() function expands $SEARCH_TERM to "minishell"
    • output_redirection() creates/truncates search_log.txt
    • Echo output: "Finding all files containing: minishell" written to file
  3. Pipeline (piping.c):

    • split_tokens() separates three commands: ls -la src/ | grep "\.c$" | head -5
    • piping() creates child processes with pipe coordination
    • Stage 1: ls -la src/ lists source directory contents
    • Stage 2: grep "\.c$" filters only .c files
    • Stage 3: head -5 takes first 5 results
  4. Append redirection:

    • append_redirection() opens search_log.txt with O_APPEND flag
    • Pipeline output appended to existing file content
  5. Heredoc processing (heredoc.c):

    • handle_heredoc() creates temporary file /tmp/filename_temp
    • write_to_tmp() reads lines until "DELIMITER" encountered
    • Each line processed for variable expansion ($SEARCH_TERM, $PWD)
    • Heredoc content displayed directly to stdout (no pipe)
  6. Process management (processes.c, pipe_utils.c):

    • Each pipeline command runs in separate child process
    • wait_for_children() ensures proper cleanup
    • File descriptors properly managed between pipeline stages
    • Exit status tracking through g_received_sig

Authors

Built by Yeva Husieva & Viacheslav Moroz

About

No description, website, or topics provided.

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages