Advertisement
Guest User

Untitled

a guest
Jun 22nd, 2017
829
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.32 KB | None | 0 0
  1.  
  2. /**
  3.  * Universität Ulm
  4.  * Grundlagen der Betriebssysteme - Labor
  5.  * Versuch 1 : Kommandozeilen-Interpreter
  6.  * Aufgabe 1 : Erstellen einer minimalen Shell
  7.  * @author Julian Bestler (julian.bestler@uni-ulm.de)
  8.  * @version 1.0
  9.  */
  10.  
  11. import static cTools.KernelWrapper.*;
  12. import java.io.BufferedReader;
  13. import java.io.InputStreamReader;
  14. import java.io.File;
  15.  
  16. class Shell {
  17.     final static int STD_IN = 0;
  18.     final static int STD_OUT = 1;
  19.  
  20.     public static void main(String[] args) throws Exception {
  21.         String login = "";
  22.         try {
  23.             login = System.getProperty("user.name") + "@" + java.net.InetAddress.getLocalHost().getHostName();
  24.         }
  25.         atch (Exception ex) {
  26.         }
  27.  
  28.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  29.         while (true) {
  30.  
  31.             System.out.print(login + " " + System.getProperty("user.dir") + ">> "); // Display
  32.                                                 // prompt
  33.             String[] commands = reader.readLine().split("\\|"); // Read commands
  34.  
  35.             int in = 0;
  36.             int[] fd = new int[2];
  37.  
  38.             for (int i = 0; i < commands.length; i++) {
  39.                 String cmd = new String(commands[i].trim().replace("'", ""));
  40.                 if (cmd.compareToIgnoreCase("Exit") == 0) {
  41.                     exit(0);
  42.                 }
  43.                 /***************************************
  44.                  *** Replace * with all filenames in the directory
  45.                  ***************************************/
  46.                 File[] currentFolder = new File(System.getProperty("user.dir")).listFiles();
  47.                 String filenames = "";
  48.                 for (int j = 0; j < currentFolder.length; j++) {
  49.                     if (currentFolder[j].isFile()) {
  50.                         filenames += " " + currentFolder[j];
  51.                     }
  52.                 }
  53.                 System.out.println("Replacing * by " + filenames);
  54.                 cmd = cmd.replaceFirst("\\*", filenames);
  55.  
  56.                 /****************************************
  57.                  *** Redirection operators < and >
  58.                  ****************************************/
  59.                 Boolean stdinRedirect = cmd.contains("<");
  60.                 Boolean stdoutRedirect = cmd.contains(">");
  61.                 int stdin_old = 0;
  62.                 int stdout_old = 1;
  63.  
  64.                 if (stdinRedirect) { // redirect stdin
  65.                     stdin_old = dup2(STD_IN, 2); // back up original stdin
  66.                     close(STD_IN); // close original stdin
  67.                     open(cmd.substring(cmd.indexOf("<") + 1, cmd.length()).trim(), O_RDONLY);   // open
  68.                                                             // new
  69.                                                             // stdin
  70.                     cmd = cmd.substring(0, cmd.indexOf("<"));   // remove redirect
  71.                                             // from command
  72.                                             // string
  73.                 }
  74.                 if (stdoutRedirect) {
  75.                     stdout_old = dup2(STD_OUT, 3); // back up original stdout
  76.                     close(STD_OUT); // close original stdout
  77.                     open(cmd.substring(cmd.indexOf(">") + 1, cmd.length()).trim(), O_WRONLY | O_CREAT);     // open
  78.                                                                 // or
  79.                                                                 // create
  80.                                                                 // new
  81.                                                                 // stdout
  82.                     cmd = cmd.substring(0, cmd.indexOf(">"));   // remove redirect
  83.                                             // from command
  84.                                             // string
  85.                 }
  86.  
  87.                 String[] opt = cmd.split(" ");
  88.                 String path = null;
  89.  
  90.                 File pgm = new File(opt[0]);
  91.                 if (pgm.isFile() && pgm.canExecute()) { // Maybe it's an
  92.                                     // absolute path
  93.                     path = opt[0];
  94.                 } else { // Else search in path variable for program location
  95.                     for (String folder : System.getenv("PATH").split(":")) {
  96.                         pgm = new File(folder + "/" + opt[0]);
  97.                         if (pgm.isFile() && pgm.canExecute()) {
  98.                             path = folder + "/" + opt[0];
  99.                             break;
  100.                         }
  101.                     }
  102.                 }
  103.  
  104.                 if (path == null) {
  105.                     System.err.println("No executable program found!");
  106.                 } else {
  107.                     if (i < commands.length - 1) {
  108.                         pipe(fd);
  109.                         execute(in, fd[1], path, opt);
  110.                         close(fd[1]);
  111.                         in = fd[0];
  112.                     } else {
  113.                         int pid = fork();
  114.                         if (pid == 0) {
  115.                             if (in != 0) {
  116.                                 dup2(in, 0);
  117.                             }
  118.                             execv(path, opt);
  119.                         } else {
  120.                             int[] status = new int[1];
  121.                             waitpid(pid, status, 0);
  122.                         }
  123.                     }
  124.                 }
  125.  
  126.                 // Restore original stdin / stdout
  127.                 if (stdinRedirect) {
  128.                     close(STD_IN);
  129.                     dup2(stdin_old, STD_IN);
  130.                     close(stdin_old);
  131.                 }
  132.                 if (stdoutRedirect) {
  133.                     close(STD_OUT);
  134.                     dup2(stdout_old, STD_OUT);
  135.                     close(stdout_old);
  136.                 }
  137.             }
  138.         }
  139.     }
  140.  
  141.     private static void execute(int in, int out, String pgm, String[] argv) {
  142.         int pid = fork();
  143.         if (pid == 0) {
  144.             if (in != 0) {
  145.                 dup2(in, 0);
  146.                 close(in);
  147.             }
  148.             if (out != 1) {
  149.                 dup2(out, 1);
  150.                 close(out);
  151.             }
  152.             execv(pgm, argv);
  153.         }
  154.     }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement