To know more about what a shell is, click here.
We all use the built in terminal window in Linux distributions like Ubuntu, Fedora, etc. But how do they actually work? In this article, We are going to handle some under the hood features and algorithms what actually work inside a shell. All Linux operating systems have a terminal window to write in commands. But how are they executed properly after they are entered?
Also, how are extra features like keeping the history of commands and showing help handled? All of this can be understood by creating your own shell.
After a command is entered, the following things are done:
- Command is entered and if length is non-null, keep it in history.
- Parsing : Parsing is the breaking up of commands into individual words and strings
- Checking for special characters like pipes, etc is done
- Checking if built-in commands are asked for.
- If pipes are present, handling pipes.
- Executing system commands and libraries by forking a child and calling execvp.
- Printing current directory name and asking for next input.
For keeping history of commands, recovering history using arrow keys and handling autocomplete using the tab key, we will be using the readline library provided by GNU.
To install the readline library, open the terminal window and write
sudo apt-get install libreadline-dev
It will ask for your password. Enter it. Press y in the next step.
- Printing the directory can be done using getcwd.
- Getting user name can be done by getenv(“USER”)
- Parsing can be done by using strsep(“”). It will separate words based on spaces. Always skip words with zero length to avoid storing of extra spaces.
- After parsing, check the list of built-in commands, and if present, execute it. If not, execute it as a system command. To check for built-in commands, store the commands in an array of character pointers, and compare all with strcmp().
Note: “cd” does not work natively using execvp, so it is a built-in command, executed with chdir().
- For executing a system command, a new child will be created and then by using the execvp, execute the command, and wait until it is finished.
- Detecting pipes can also be done by using strsep(“|”).To handle pipes, first separate the first part of the command from the second part. Then after parsing each part, call both parts in two separate new children, using execvp. Piping means passing the output of first command as the input of second command.
- Declare an integer array of size 2 for storing file descriptors. File descriptor 0 is for reading and 1 is for writing.
- Open a pipe using the pipe() function.
- Create two children.
- In child 1->
Here the output has to be taken into the pipe. Copy file descriptor 1 to stdout. Close file descriptor 0. Execute the first command using execvp()
- In child 2->
Here the input has to be taken from the pipe. Copy file descriptor 0 to stdin. Close file descriptor 1. Execute the second command using execvp()
- Wait for the two children to finish in the parent.
To Run the code –
gcc shell.c -lreadline ./a.out
This article is contributed by Suprotik Dey. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.