Question
In this assignment, you will implement a Command Line Interpreter (CLI) or, as it is more commonly known, a Shell. The shell should operate in
In this assignment, you will implement a Command Line Interpreter (CLI) or, as it is more commonly known, a Shell. The shell should operate in this basic way: when you type in a command (in response to its prompt), the shell creates a child process that executes the command you entered and then prompts for more user input when it has finished.
Your basic shell, called wish, is basically an interactive loop: it repeatedly prints a prompt wish> (note the space after the greater-than sign), parses the input, executes the command specified on that line of input, and waits for the command to finish. This is repeated until the user types exit. The name of your final executable should be wish. The shell can be invoked with either no arguments or a single argument; anything else is an error. Here is the no-argument way: prompt>./wish wish>
Your basic shell should be able to parse a command and run the program corresponding to the command. For example, if the user types ls -la /tmp your shell should run the program /bin/ls with the given arguments -la and /tmp (how does the shell know to run/bin/ls ? Its something called the shell path; more on this in the next section).
TO MAKE SHELL
1- Make a loop continues indefinitely, until the user types the built-in command exit, at which point it exits.
2- For reading lines of input, you should use getline().
3- However, your shell will also support batch mode, in which the shell is given an input file of commands; in this case, the shell should not read user input (from stdin) but rather from this file to get the commands to execute.
4- In either mode, if you hit the end-of-file marker (EOF), you should call exit(0) and exit gracefully.
5- To parse the input line into constituent pieces, you might want to use strsep().
6- To execute commands, look into fork(), exec(), and wait()/waitpid().
7- You will note that there are a variety of commands in the exec family; for this project, you must use execv. You should not use the system() library function call to run a command. Remember that if execv() is successful, it will not return; if it does return, there was an error (e.g., the command does not exist).
8- Your initial shell path should contain one directory: /bin
9-you should implement exit, cd, and path as built-in commands. exit: When the user types exit, your shell should simply call the exit system call with 0 as a parameter. It is an error to pass any arguments to exit.
cd: cd always take one argument (0 or >1 args should be signaled as an error). To change directories, use the chdir() system call with the argument supplied by the user; if chdir fails, that is also an error. path: The path command takes 0 or more arguments, with each argument separated by whitespace from the others. A typical usage would be like this: wish> path /bin /usr/bin, which would add /bin and /usr/bin to the search path of the shell. If the user sets path to be empty, then the shell should not be able to run any programs (except built-in commands). The path command always overwrites the old path with the newly specified path
10-Usually, a shell provides this nice feature with the > character. Formally this is named as redirection of standard output. To make your shell users happy, your shell should also include this feature, but with a slight twist (explained below). For example, if a user types ls -la /tmp > output, nothing should be printed on the screen. Instead, the standard output of the ls program should be rerouted to the file output. In addition, the standard error output of the program should be rerouted to the file output (the twist is that this is a little different than standard redirection). If the output file exists before you run your program, you should simple overwrite it (after truncating it). For example, if a user types ls -la /tmp > output, nothing should be printed on the screen. Instead, the standard output of the ls program should be rerouted to the file output. In addition, the standard error output of the program should be rerouted to the file output (the twist is that this is a little different than standard redirection). If the output file exists before you run your program, you should simple overwrite it (after truncating it).
11- Your shell will also allow the user to launch parallel commands. This is accomplished with the ampersand operator as follows: wish> cmd1 & cmd2 args1 args2 & cmd3 args1.
Then, after starting all such processes, you must make sure to use wait() (or waitpid) to wait for them to complete. After all processes are done, return control to the user as usual (or, if in batch mode, move on to the next line).
12- The one and only error message. You should print this one and only error message whenever you encounter an error of any type: char error_message[30] = "An error has occurred "; write(STDERR_FILENO, error_message, strlen(error_message)); The error message should be printed to stderr (standard error), as shown above. After any most errors, your shell simply continues processing after printing the one and only error message. However, if the shell is invoked with more than one file, or if the shell is passed a bad batch file, it should exit by calling exit (1)
Thanks!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started