A team project with cperron for the 42 school cursus. The main goal of the project was to make a small shell that is similar to bash. You can read the subject here.
- Compatible with linux and mac
- Has a working history which can be access by press up and down
- Can execute commands or executable from relative or absolute path
- Handle ' and "
- Handle environment variables with $ or ${}
- Handle $? exit status
- Handle these redirections: < << | > >>
- Handle these signals like bash: ctr+c ctrl+d ctrl+\
- Implement these built-ins that were asked: echo cd pwd exit env export unset
Building a shell from scratch is no easy task and can be a daunting task. Knowing where to start is the hardest part of the project. We ended up splitting the logic of execution into 5 main part. Here are the parts in order of execution:
- Input reading
- Lexical analysis
- Parser
- Interpreter
- Executor
The input reading part also known has interactive mode take care of taking the input and sending it to the lexical analysis routine. Here the function that does all the work for the interactive mode. It loops forever unless exit command is entered or a signal is sent with the keyboard with ctr+c or ctrl+d. It also check for any potential failure from readline, it also has checks for signals and will return the correct exit status code if someone use a signal.
Now this is where things start to be complicated and were a nightmare to deal with. Splitting words into tokens were really difficult at first with all the edge cases that were possible.
The role of this part is to remove these: " ' $
The tricky part is the order you do things for this. Here an example of a tricky situation: '$USER'
Removing the single quote first would result in me not knowing it had single quotes. To remove any confusion I ended
up splitting my tokens into more tokens and do the things individually and paste them back together at the end.
Here an representation of how the tokens were getting split into sub tokens:
If we got here it mean there was no syntax error from the user. We first have to check for the heredoc...tbc