Question: operation system, computer science , please complete code Shell Interface Assignment Creating a Shell Interface Using Java This assignment consists of modifying a Java program
operation system, computer science , please complete code
Shell Interface Assignment Creating a Shell Interface Using Java This assignment consists of modifying a Java program so that it serves as a shell interface that accepts user commands and then executes each command in a separate process external to the Java virtual machine. Overview A shell interface provides the user with a prompt, after which the user enters the next command. The example below illustrates the prompt jsh> and the users next command: cat Prog.java. This command displays the file Prog.java on the terminal using the UNIX cat command. jsh> cat Prog.java Perhaps the easiest technique for implementing a shell interface is to have the program first read what the user enters on the command line (here, cat Prog.java) and then create a separate external process that performs the command. We create the separate process using the ProcessBuilder() object, as illustrated in the section labeled Process Creation in Java found later in this assignment description. In our example, this separate process is external to the JVM and begins execution when its run() method is invoked. A Java program that provides the basic operations of a command-line shell is supplied in the code of Figure 1 on the next page. The main() method presents the prompt jsh> (for java shell) and waits to read input from the user. The program is terminated when the user enters . This assignment is organized into three parts: (1) creating the external process and executing the command in that process, (2) modifying the shell to allow changing directories, and (3) adding a history feature. Page 2 of 9 import java.io.*; public class SimpleShell { public static void main(String[] args) throws java.io.IOException { String commandLine; BufferedReader console = new BufferedReader (new InputStreamReader(System.in)); // we break out with while (true) { // read what the user entered System.out.print("jsh>"); commandLine = console.readLine(); // if the user entered a return, just loop again if (commandLine.equals("")) continue; /** The steps are: (1) parse the input to obtain the command and any parameters (2) create a ProcessBuilder object (3) start the process (4) obtain the output stream (5) output the contents returned by the command */ } } } Figure 1 Outline of simple shell. Page 3 of 9 Part 1: Creating an External Process The first part of this assignment is to modify the main() method in Figure 1 so that an external process is created and executes the command specified by the user. Initially, the command must be parsed into separate parameters and passed to the constructor for the ProcessBuilder object. For example, if the user enters the command jsh> cat Prog.java the parameters are (1) cat and (2) Prog.java, and these parameters must be passed to the ProcessBuilder constructor. Perhaps the easiest strategy for doing this is to use the constructor with the following signature: public ProcessBuilder(List command) A java.util.ArrayList, which implements the java.util.List interface, can be used in this instance, where the first element of the list is cat and the second element is Prog.java. This is an especially useful strategy because the number of arguments passed to UNIX commands may vary (the cat command accepts one argument, the cp command accepts two, and so forth.) If the user enters an invalid command, the start() method in the ProcessBuilder class throws an java.io.IOException. If this occurs, your program should output an appropriate error message and resume waiting for further commands from the user. Part 2: Changing Directories The next task is to modify the program in Figure 1 so that it changes directories. In UNIX systems, we encounter the concept of the current working directory, which is simply the directory you are currently in. The cd command allows a user to change current directories. Your shell interface must support this command. For example, if the current directory is /usr/tom and the user enters cd music, the current directory becomes /usr/tom/music. Subsequent commands relate to this current directory. For example, entering ls will output all the files in /usr/tom/music. The ProcessBuilder class provides the following method for changing the working directory: public ProcessBuilder directory(File directory) When the start() method of a subsequent process is invoked, the new process will use this as the current working directory. For example, if one process with a current working directory of /usr/tom invokes the command cd music, subsequent processes must set their working directories to /usr/tom/music before beginning execution. It is important to note that your program must first make sure the new path being specified is a valid directory. If not, your program should output an appropriate error message. If the user enters the command cd, change the current working directory to the users home directory. The home directory for the current user can be obtained by invoking the static getProperty() method in the System class as follows: System.getProperty("user.dir"); Page 4 of 9 Part 3: Adding a History Feature Many UNIX shells provide a history feature that allows users to see the history of commands they have entered and to rerun a command from that history. The history includes all commands that have been entered by the user since the shell was invoked. For example, if the user entered the history command and saw as output: 0 pwd 1 ls -l 2 cat Prog.java the history would list pwd as the first command entered, ls -l as the second command, and so on. Modify your shell program so that commands are entered into a history. (Hint: The java.util.ArrayList provides a useful data structure for storing these commands.) Your program must allow users to rerun commands from their history by supporting the following three techniques: 1. When the user enters the command history, you will print out the contents of the history of commands that have been entered into the shell, along with the command numbers. 2. When the user enters !!, run the previous command in the history. If there is no previous command, output an appropriate error message. 3. When the user enters !, run the ith command in the history. For example, entering !4 would run the fourth command in the command history. Make sure you perform proper error checking to ensure that the integer value is a valid number in the command history.