MVC skeleton to YouAreEll. In the 'structure' branch

SimpleShell.java 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.InputStreamReader;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7. public class SimpleShell {
  8. public static void main(String[] args) throws java.io.IOException {
  9. YouAreEll webber = new YouAreEll();
  10. String commandLine;
  11. BufferedReader console = new BufferedReader
  12. (new InputStreamReader(System.in));
  13. ProcessBuilder pb = new ProcessBuilder();
  14. List<String> history = new ArrayList<String>();
  15. int index = 0;
  16. //we break out with <ctrl c>
  17. while (true) {
  18. //read what the user enters
  19. System.out.println("cmd? ");
  20. commandLine = console.readLine();
  21. //input parsed into array of strings(command and arguments)
  22. String[] commands = commandLine.split(" ");
  23. List<String> list = new ArrayList<String>();
  24. //if the user entered a return, just loop again
  25. if (commandLine.equals(""))
  26. continue;
  27. if (commandLine.equals("exit")) {
  28. System.out.println("bye!");
  29. break;
  30. }
  31. //loop through to see if parsing worked
  32. for (int i = 0; i < commands.length; i++) {
  33. //System.out.println(commands[i]); //***check to see if parsing/split worked***
  34. list.add(commands[i]);
  35. }
  36. System.out.print(list); //***check to see if list was added correctly***
  37. history.addAll(list);
  38. try {
  39. //display history of shell with index
  40. if (list.get(list.size() - 1).equals("history")) {
  41. for (String s : history)
  42. System.out.println((index++) + " " + s);
  43. continue;
  44. }
  45. // Specific Commands.
  46. // ids
  47. if (list.contains("ids")) {
  48. String results = webber.get_ids();
  49. System.out.println(results);
  50. continue;
  51. }
  52. // messages
  53. if (list.contains("messages")) {
  54. String results = webber.get_messages();
  55. System.out.println(results);
  56. continue;
  57. }
  58. // you need to add a bunch more.
  59. //!! command returns the last command in history
  60. if (list.get(list.size() - 1).equals("!!")) {
  61. pb.command(history.get(history.size() - 2));
  62. }//!<integer value i> command
  63. else if (list.get(list.size() - 1).charAt(0) == '!') {
  64. int b = Character.getNumericValue(list.get(list.size() - 1).charAt(1));
  65. if (b <= history.size())//check if integer entered isn't bigger than history size
  66. pb.command(history.get(b));
  67. } else {
  68. pb.command(list);
  69. }
  70. // wait, wait, what curiousness is this?
  71. Process process = pb.start();
  72. //obtain the input stream
  73. InputStream is = process.getInputStream();
  74. InputStreamReader isr = new InputStreamReader(is);
  75. BufferedReader br = new BufferedReader(isr);
  76. //read output of the process
  77. String line;
  78. while ((line = br.readLine()) != null)
  79. System.out.println(line);
  80. br.close();
  81. }
  82. //catch ioexception, output appropriate message, resume waiting for input
  83. catch (IOException e) {
  84. System.out.println("Input Error, Please try again!");
  85. }
  86. // So what, do you suppose, is the meaning of this comment?
  87. /** The steps are:
  88. * 1. parse the input to obtain the command and any parameters
  89. * 2. create a ProcessBuilder object
  90. * 3. start the process
  91. * 4. obtain the output stream
  92. * 5. output the contents returned by the command
  93. */
  94. }
  95. }
  96. }