As a starting point, it's worth noting that the term 'mini' doesn't fundamentally distinguish this project from a regular shell. While it may have fewer functionalities, at its core, it performs tasks similar to a standard shell.
- Requirement
- Usage
- Features
- Minishell Overview
- Tokenization
- Syntax Analysis
- Tree Construction
- Command Execution
- Credits
- c compiler (Ensure to adjust the compiler in the makefile to match the one you are using)
CC = cc - Ensure that you have 'make' installed before proceeding with the build process.
- readline library
- clone the reposetory
make./minishellyou should be able to see a prompt like this :minishel1 $>
minishel1 $> false && echo "minishell" || pwd
minishell $> (true || pwd) && (echo "hello" || ls -la)minishel1 $> cat * | grep .c | wc -l # count the number of .c filesminishel1 $> << he cat | grep 'ft_' > outputecho # Write arguments to the standard output.
cd # Change the shell working directory.
pwd # Print the name of the current working directory.
export # Set export attribute for shell variables.
unset # Unset values and attributes of shell variables.
env # Set environment and execute command, or print environment.
exit # Exit the shell.
true, false # Just to test the conditionalsThe Minishell project is designed to parse commands, build a tree structure, and execute them, closely mirroring the functionality of the 'bash' shell. The process involves several key steps:
-
Tokenization #Tokenization: The command is initially tokenized to break it down into individual components, facilitating further analysis.
-
Syntax Analysis #Syntax Analysis: The syntax of the command is then analyzed to ensure it adheres to the shell syntax. This step is crucial for correctly interpreting user input.
-
Tree Construction #Tree Construction: A tree structure is constructed to represent the command. This tree serves as a logical representation that aids in the execution process.
-
Command Execution #Command Execution: The parsed and analyzed command is executed based on the constructed tree. This step involves handling various signals, including Ctrl-C, Ctrl-D, Ctrl-Z, and Ctrl-, to emulate the behavior of the 'bash' shell.
-
Redo
This complex process ensures that the Minishell performs command parsing and execution seamlessly, providing a shell experience similar to established counterparts.
The tokenization process in Minishell is designed to efficiently handle command input. The primary focus is on avoiding unnecessary data structures, such as linked lists, when extracting tokens. Instead, the tokenization is driven by three key functions:
-
line: The address of the command line taken as input. It is passed by address since it will change with each function call.
-
token: In C, returning two values from a function is not straightforward. The token parameter is used to store the actual token value.
-
Return Value: The function returns an integer representing the type of the token. The defined token types are as follows:
#define T_OPRNTHS 1 // open parenthesis #define T_CPRNTHS 2 // close parenthesis #define T_AND 3 // and #define T_OR 4 // or #define T_PIPE 5 // pipe #define T_LESS 6 // < #define T_LLESS 7 // << #define T_GREAT 8 // > #define T_GGREAT 9 // >> #define T_WORD 10 // word #define T_UNKNOWN 11 // unknown
The ft_get_word function is designed to extract a word from a string, handling various complexities. Examples of complexities include words within quotes or spaces between words. The function parameters are:
-
s: The string from which the word is to be extracted.
-
j: A pointer used to store the length of the extracted word.
-
Return Value: If successful, the function returns 0. In case of failure, it returns -1.
This function is particularly useful in scenarios where words may contain special characters or spaces, ensuring accurate extraction for further processing in the tokenization process.
"hello'world"itisme --> hello'world
the ft_peak function is designed to check whether the next non-whitespace character in the given string (*line) is present in a specified set of characters (toks).
- line: A pointer to a pointer to a string. The function uses a double pointer to be able to modify the original pointer and update it to the new position in the string.
- toks: A string containing characters to be checked.
- Return Value: If the next non-whitespace character in the string is found in the specified set of characters, it returns 1 (true). Otherwise, it returns 0 (false).
This function is created to address the issue with ft_peak, where it might confuse between | and ||.
In the context of Minishell, syntax analysis is performed using the concept of automata from language theory. In simple terms, an automaton takes a line of input and answers the question: Does this line belong to the language defined by Minishell?
Using automata in the syntax analysis allows for a systematic and rule-based approach to validate command structures. It ensures that the Minishell language is interpreted accurately, helping to prevent unexpected behavior and enhancing the user experience.
This implementation of automata aligns with language theory principles, making the syntax analysis robust and efficient for a wide range of Minishell commands.
The automata for the minishell :
After making sure that the syntax is correct we move to build our tree : The tree building is based on the folowing Grammer :
CMDLINE --> PIPELINE
--> PIPELINE "||" CMDLINE
--> PIPELINE "&&" CMDLINE
PIPELINE --> CMD
--> CMD "|" PIPELINE
CMD --> BLOCK
--> (REDIRECTION | word)+
BLOCK --> "(" CMDLINE ")" ((">" | ">>") filename)*
REDIRECTION --> REDIRECTIONOP filename
REDIRECTIONOP --> "<"
--> ">"
--> "<<"
--> ">>"
The building of the tree make the execution realy easy, just make sure to execute a single commande right and the whole commande line is just a recursive process
void ft_exc_and(t_cmdline *cmdline);
void ft_exc_block(t_cmdline *cmdline);
void ft_exc_cmd(t_cmdline *cmdline);
void ft_exc_cmdline(t_cmdline *cmdline);
void ft_exc_or(t_cmdline *cmdline);
void ft_exc_pipe(t_cmdline *cmdline);Teamed up with mlahlafi on this awesome project – we basically rocked it! Our skills meshed like peanut butter and jelly, turning dull ideas into something seriously cool. It was a blast working together, and the project? Well, let's just say it's a masterpiece of teamwork!