Command Shell Interpreter Project
Write a command shell interpreter program. The program should present a prompt, read a command typed by the user, spawn a process and execute the command in the spawned process. After executing the command, the program should prompt the user for the next command. The user must be able to exit the program by typing the command exit.
The program jjshell (see codes on mat375 page) is a command shell interpreter. You can run that program to get an idea of how the interpreter should work. At a minimum, your program must execute simple commands with arguments, e.g.,
ps -x -f. The program can be written in C or C++.
How do I get started?
The project is straightforward. Here is just one approach if you are considering writing the program in C++.
First, you should write a program that reads a command line and separates the line into tokens (words). This is the most time consuming part of the project. You can use any technique you like to implement this part. The program jjshell.cpp uses the C++ string library function strtok(), which greatly simplifies the creation of tokens from the command line. In addition, you may want to use function cin.get() to read characters. Reading the line with the cin.getline() member function is even easier; however, this method has some limitations. After creating the tokens, an array of string pointers should be constructed. Each position of the array will contain a pointer to a token. Any of the C++ editions of Deitel &
Deitel show how to used these functions.
Once the pointer array has been created, you should use the fork() system call to create a child process. In the child process, you can use the system call execvp() to execute the command typed by the user. The function execvp() executes commands where the reference to the command and parameters are stored in an array of pointers. The parent process should wait for the child to execute the command and then terminate. In summary, the command interpreter program requires the wait(), fork() and execvp() system calls.
Enhancing the Program
For extra credit, you can modify the program so it handles I/O redirection and pipes between two processes. In other words, the user can type instructions such as cat file1 > file2, ps > file1, cat file1 | wc, or ps -e | grep init.
What do I hand in?
(1) You must hand in a hard copy of the source code with your name in a comment on the first line. Please do not hand in any output. You can print you program in the Distributed Computing Lab (4301) with the command mpage -2 -f -P jjshell.cpp
(2) Leave a copy of the source code for your program in your directory in a file named jjshell.cpp (if in C++), or jjshell.c (if in c). Your program will be compiled and run by a script so it is very important that you save it as indicated.
When is the project due?
The project is due Thursday, October 21, before 8:00 p.m.