The 8th project of 42 cursus syllabus asks students to implement a simplified shell. It's about minishell, as beautiful as a shell.
As the project's subject states, the existence of shells is linked to the very existence of IT. At the time, all developers agreed that communicating with a computer using aligned 1/0 swiches was seriously irritating. It was only logical that they came up with the idea of creating a software to communicate with a computer using interactive lines of commands in a language somewhat close to the human language.
git clone https://github.com/magalhaesm/minishell.git
sudo apt-get install -y libreadline6 libreadline6-dev
make
./minishell
If you want to run the executable program automatically with valgrind flags to check for leaks, just write make checks
and press enter.
Builtin | Command description |
---|---|
echo with -n |
Displays a line of text to the standard output |
cd |
Changes the working directory of the current shell execution environment |
pwd |
Prints the full filename of the current working directory |
export name[=word] |
Sets the export attribute for variables |
unset |
Unsets values and attributes of variables and functions |
env |
Sets each name to value in the enviroment and run command |
exit |
Causes the shell to exit with exit status specified |
There are two main files at projects's root directory:
include
: contains all the necessary headers for the projectgrammar
: the implemented grammar, which creates and checks the sentenceslibft
: our own C library with some helpful auxiliary functions, such as ft_strlentests
: contains unit tests created with Criterion testing framework to evaluate functions insrc
src
: contains the whole project implementationbuiltins
: implements the required builtins by the project's subjectexec
: consists of functions that executes user's inputexpansion
: implements shell expansors, such as quote marks and wildcardshelpers
: contains utils function used on the project as a wholeparser
: consists of functions that implement the minishell's grammar and create a tree of a given input by the userscanner
: implements a scanner to create tokens of a given input by the usersignals
: consists of functions that handle user events, such asctrl-C
,ctrl-D
andctrl-\
table
: implements a hash table to store enviroment variables
In a nutshell, the project flowchart is as follows:
graph LR;
INPUT-->scanner;
scanner-->parser;
parser-->expansor;
expansor-->executor;
executor-->OUTPUT
As an example, if given the input true && ls || echo
the program generates the tree below to be executed:
graph TD;
OR-->AND;
OR-->echo;
AND-->true;
AND-->ls;
-
General references:
- CS 61: Systems Programming and Machine Organization (2022), by Harvard University
- NYSTROM, Robert. Crafting interpreters. 2020
- AHO, Alfred. Compiladores: princípios, técnicas e ferramentas. 2007
- Playlist about compilers, by Judson Santiago
- Playlist about Programming Languages and Compilers, by Rafael Ivo
- Subshell: types and origins, by prog.shell.linux
-
Useful tools for grammar generation:
-
About hash table implementation
-
About parser:
-
About syntax analyzer:
-
About bonus implementation
-
The same project done by fellows 42SP's students
-
Automated tester that helped us to find some bugs: