This one of the hardest group project of the 42 common core. Why? Because we have to code from scratch a bash command line in C. I worked on this project as a team with Cyrena Cramdani.
The end product had to reproduce identically the behaviour of a bash command line. Obviously not all of it had to be implemented since it would take years to reproduce. You can see below the basic behaviour of our program with some typicall bash commands. The project will not be explained in its entirety since it is very detailled. However, the main concepts are explained below.
This project can be divided into two main parts:
- Parsing.
- Execution.
Parsing was done using a tokenized lexer/parser. Each element of the command was stored into a linked list with each element having a specific type (e.g. CMD, REDIR, PIPE, etc.). All $ variables were also converted.
Execution was done using an AST (Abstract Synthax Tree). The point of using an AST is to simplify the execution process and be able to implement levels of priority in the command interpreter easily. It was not necessary for this project but I thought it would be fun to understand and implement.
On one part we had to recode built-in functions such as "cd", "pwd", etc. On the other hand we had to use the standard library function "execve" to execute all executables from bash (e.g. ls, cat, wc, etc.).
- In depth Bash --posix.
- Advanced multi-processing.
- Process communication with pipes.
- redirections ("<", ">", ">>").
- dup2 function to redirect STDOUT content into a different process.
- How heredocs work ("<< redirections").
- Advanced fd managment.
- TDD.
- Signals overload (Ctr C, Ctrl D, etc.).
- Readline fucntion and history to create the command line entry.
- Advanced debugging and leaks managment (the most painful part... 😭).
- Abstract Syntax Trees (loved it 😎).
- Lexer / Parsers.
- Tokenizers.
- Pids and processes managment (painfully with the reimplementation of the "cat | ls" commands 😆).
- Environment variables.