Untitled

 avatar
unknown
java
4 years ago
7.9 kB
4
Indexable
import java.io.*;
        import java.util.ArrayList;

public class ABasicShell {

    public static void main(String[] args) {
        // the commandline string
        String commandLine;

        // reader for the console
        BufferedReader console = new BufferedReader(new InputStreamReader((System.in)));

        // the directory for the ProcessBuilder
        String dir = System.getProperty("user.dir");

        // the process builder instance
        // we must declare it here so that we can maintain the state of the
        // ProcessBuilder
        // especially for tracking the current directory
        ProcessBuilder pb = new ProcessBuilder();

        // the history arraylist
        ArrayList<String> history = new ArrayList<String>();

        while (true) {
            try {

                // read what the user entered
                System.out.print("jsh> ");
                commandLine = console.readLine();

                if (commandLine.equals(""))
                    continue;

                // add the command to the history arraylist
                history.add(commandLine);

                // split the command into an array of strings
                // to be used as parameter for the ProcessBuilder command()
                String[] commands = commandLine.split(" ");

                // check if the command is a cd command
                if (commands[0].equals("cd")) {

                    // check if there is directory specified for the cd command
                    if (commands.length == 1) {

                        // no directory was specified
                        // change directory to the user's home directory
                        dir = System.getProperty("user.home");

                    } else {

                        // a directory is specified
                        // load it into a variable
                        String tmpDir = commands[1];

                        // create a file instance from the directory
                        File f = new File(tmpDir);

                        // check if the directory exists
                        if (f.exists()) {

                            // directory exists
                            // assign it to the current directory variable
                            dir = tmpDir;

                        } else {

                            // the directory does not exist
                            // check if the specified directory is a relative path
                            // (it does not begin with a slash "/")
                            if (!f.isAbsolute()) {

                                // set an absolute path using the current directory as the parent directory
                                String absDir = dir + "/" + tmpDir;

                                // create a file instance based on the absolute path
                                File f2 = new File(absDir);

                                // check if the absolute path exists
                                if (f2.exists()) {

                                    // the path exist
                                    // assign it to the current directory variable
                                    dir = absDir;

                                } else {
                                    // the path does not exist
                                    // show an error to the user
                                    System.out.println("Directory does not exist.");
                                }
                            } else {
                                // the directory does not exist
                                // show an error to the user
                                System.out.println("Directory does not exist.");
                            }
                        }

                    }

                    // change the directory of the ProcessBuilder
                    // using the dir variable assigned earlier
                    pb.directory(new File(dir));

                    // check if the command is the history command
                } else if (commandLine.equals("history")) {

                    // the command is the history command
                    // print all the element of the history arraylist
                    // with its respective index
                    // the index can be used to rerun the command
                    for (int i = 0; i < history.size(); i++) {
                        System.out.printf("%d %s\n", i, history.get(i));
                    }

                    // other commands
                } else {

                    // check if the command is an index in the history arraylist
                    try {

                        // parse the command if it's numeric
                        int histnum = Integer.parseInt(commands[0]);

                        // the command is numeric
                        // check if it's a valid index in the history arraylist
                        if (histnum < history.size()) {

                            // index is valid
                            // fetch the command from the history arraylist
                            String fromHistory = history.get(histnum);

                            // check if the command is the history command
                            // we need to perform this again
                            // because we have passed the if condition that checks for the history command
                            if (fromHistory.equals("history")) {

                                // print all the element of the history arraylist
                                // with its respective index
                                // the index can be used to rerun the command
                                for (int i = 0; i < history.size(); i++) {
                                    System.out.printf("%d %s\n", i, history.get(i));
                                }

                                // jump out of the loop
                                continue;

                            } else {
                                // split the command into an array of strings
                                String[] arr = fromHistory.split(" ");

                                // set the command in the ProcessBuilder
                                pb.command(arr);
                            }

                        } else {
                            // the index is invalid
                            // display an error
                            System.out.println("Invalid history index");
                        }

                    } catch (NumberFormatException e) {
                        // the command is not numeric
                        // set the command to the ProcessBuilder
                        pb.command(commands);
                    }

                    // start the process
                    Process process = pb.start();

                    // obtain the output stream
                    InputStream inputStream = process.getInputStream();

                    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

                    BufferedReader br = new BufferedReader(inputStreamReader);

                    String line;

                    // output the contents returned by the command
                    while ((line = br.readLine()) != null) {
                        System.out.println(line);
                    }

                    br.close();
                }

            } catch (IOException ex) {
                System.out.println(ex.getMessage());
            }

        }

    }

}
Editor is loading...