Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Universität Ulm
- * Grundlagen der Betriebssysteme - Labor
- * Versuch 1 : Kommandozeilen-Interpreter
- * Aufgabe 1 : Erstellen einer minimalen Shell
- * @author Julian Bestler (julian.bestler@uni-ulm.de)
- * @version 1.0
- */
- import static cTools.KernelWrapper.*;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.io.File;
- class Shell {
- final static int STD_IN = 0;
- final static int STD_OUT = 1;
- public static void main(String[] args) throws Exception {
- String login = "";
- try {
- login = System.getProperty("user.name") + "@" + java.net.InetAddress.getLocalHost().getHostName();
- }
- atch (Exception ex) {
- }
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- while (true) {
- System.out.print(login + " " + System.getProperty("user.dir") + ">> "); // Display
- // prompt
- String[] commands = reader.readLine().split("\\|"); // Read commands
- int in = 0;
- int[] fd = new int[2];
- for (int i = 0; i < commands.length; i++) {
- String cmd = new String(commands[i].trim().replace("'", ""));
- if (cmd.compareToIgnoreCase("Exit") == 0) {
- exit(0);
- }
- /***************************************
- *** Replace * with all filenames in the directory
- ***************************************/
- File[] currentFolder = new File(System.getProperty("user.dir")).listFiles();
- String filenames = "";
- for (int j = 0; j < currentFolder.length; j++) {
- if (currentFolder[j].isFile()) {
- filenames += " " + currentFolder[j];
- }
- }
- System.out.println("Replacing * by " + filenames);
- cmd = cmd.replaceFirst("\\*", filenames);
- /****************************************
- *** Redirection operators < and >
- ****************************************/
- Boolean stdinRedirect = cmd.contains("<");
- Boolean stdoutRedirect = cmd.contains(">");
- int stdin_old = 0;
- int stdout_old = 1;
- if (stdinRedirect) { // redirect stdin
- stdin_old = dup2(STD_IN, 2); // back up original stdin
- close(STD_IN); // close original stdin
- open(cmd.substring(cmd.indexOf("<") + 1, cmd.length()).trim(), O_RDONLY); // open
- // new
- // stdin
- cmd = cmd.substring(0, cmd.indexOf("<")); // remove redirect
- // from command
- // string
- }
- if (stdoutRedirect) {
- stdout_old = dup2(STD_OUT, 3); // back up original stdout
- close(STD_OUT); // close original stdout
- open(cmd.substring(cmd.indexOf(">") + 1, cmd.length()).trim(), O_WRONLY | O_CREAT); // open
- // or
- // create
- // new
- // stdout
- cmd = cmd.substring(0, cmd.indexOf(">")); // remove redirect
- // from command
- // string
- }
- String[] opt = cmd.split(" ");
- String path = null;
- File pgm = new File(opt[0]);
- if (pgm.isFile() && pgm.canExecute()) { // Maybe it's an
- // absolute path
- path = opt[0];
- } else { // Else search in path variable for program location
- for (String folder : System.getenv("PATH").split(":")) {
- pgm = new File(folder + "/" + opt[0]);
- if (pgm.isFile() && pgm.canExecute()) {
- path = folder + "/" + opt[0];
- break;
- }
- }
- }
- if (path == null) {
- System.err.println("No executable program found!");
- } else {
- if (i < commands.length - 1) {
- pipe(fd);
- execute(in, fd[1], path, opt);
- close(fd[1]);
- in = fd[0];
- } else {
- int pid = fork();
- if (pid == 0) {
- if (in != 0) {
- dup2(in, 0);
- }
- execv(path, opt);
- } else {
- int[] status = new int[1];
- waitpid(pid, status, 0);
- }
- }
- }
- // Restore original stdin / stdout
- if (stdinRedirect) {
- close(STD_IN);
- dup2(stdin_old, STD_IN);
- close(stdin_old);
- }
- if (stdoutRedirect) {
- close(STD_OUT);
- dup2(stdout_old, STD_OUT);
- close(stdout_old);
- }
- }
- }
- }
- private static void execute(int in, int out, String pgm, String[] argv) {
- int pid = fork();
- if (pid == 0) {
- if (in != 0) {
- dup2(in, 0);
- close(in);
- }
- if (out != 1) {
- dup2(out, 1);
- close(out);
- }
- execv(pgm, argv);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement