import static cTools.KernelWrapper.*; //imports a Java Wrapper for C kernel functions, needs the sourced source_me import java.util.ArrayList; import java.util.Scanner; import java.lang.*; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.Arrays; import java.nio.charset.StandardCharsets; //TODO: switch case for printcolor //TODO: implement auto source? public class main{ public static void main(String[] args){ System.out.println("Welcome!"); String shellPrefix = "EN@GdBShell:";//print actual user instead of EN?? int runningInt = 0; Scanner scn = new Scanner(System.in); while (true){ parseInput(getInput(runningInt,shellPrefix, scn)); runningInt++; } } static String[] getInput(int runningInt, String shellPrefix, Scanner scn){ String directoryPath = System.getProperty("user.dir"); String[] directoryArray = directoryPath.split("/"); StringBuffer buffer = new StringBuffer("~"); //gets rid of the home/user directory and builds a string for (int i=3;(i concatPosition = new ArrayList(); //save the positions of concats for (int i=0; i0){ //split the inputArray into singular commands that were separated by && ArrayList> commandsList = new ArrayList>(); for (int i=0;i()); } //upper Arraylist used with fixed size, doesnt throw warning like using an array would //because one concat splits the input into two parts --->+1 //iterate over every command except last, because no concat comes after the last one int prevConcatPos=0; for (int i=0;i> for append? //find last occurrences of < and > int redirectInPos = -1; int redirectOutPos = -1; boolean setFirstBool = true; int firstPos = inputArray.length; for(int i=0;i< inputArray.length;i++){ if(inputArray[i].equals("<")){ redirectInPos = i; if(setFirstBool){ firstPos=i; setFirstBool = false; } } if(inputArray[i].equals(">")){ redirectOutPos = i; if(setFirstBool){ firstPos=i; setFirstBool = false; } } /*if(inputArray[i].equals(">>")){ appendOutPos = i; }*/ } int fd_in=-1; if(redirectInPos!=-1){ if(checkls(inputArray[redirectInPos+1])){ fd_in = open(inputArray[redirectInPos+1],O_RDONLY); } else{return -10;}//throw error because invalid input file } int fd_out=-1; if(redirectOutPos!=-1){ fd_out = open(inputArray[redirectOutPos+1],O_WRONLY|O_CREAT|O_TRUNC);//create file if non existent, overwrite if it is } String[] commands = new String[firstPos]; for (int i=0;i< commands.length;i++){ commands[i]=inputArray[i]; } int returnValue= parsePipe(commands,fd_in,fd_out); if(fd_in!=-1){ close(fd_in); } if(fd_out!= -1){ close(fd_out); } return returnValue; } static int parsePipe(String[] inputArray,int fd_in, int fd_out){ int returnValue = -5; ArrayList pipePos = new ArrayList(); int count =0; for (int i =0;i< inputArray.length; i++){ if (inputArray[i].equals("|")){ pipePos.add(count); count=0; } else{count++;} } pipePos.add(count); //populate array of commands separated by | String[][] commands = new String[pipePos.size()][]; int prevPipePos = 0; for (int i=0;i< pipePos.size();i++){ commands[i] = new String[pipePos.get(i)]; for (int j=0;j1){ pipe(pipefd); returnValue= execute(commands[0],fd_in,pipefd[1]); for(int i=1;i-1){ if (checkls(input)==true){ path = input; } } else{*/ path = which(input); //} } else{return Integer.MIN_VALUE+1;}// add error in parseInput for this number //if path found, compile argslist and start execv if (isNumeric(path)==false){ printColor("Executing program at "+path,"green",true); //System.out.println(); //int[] pipeArray = new int[]{0};//sets the pip int forkInt = fork();// saves the return value because every call will fork the process again //baby process if (forkInt==0){ //Array[0] mit rest als parameter ausführen (path, args[]) //read from pipe or write to it if(fd_in!=-1){ close(0); dup2(fd_in,0); } if(fd_out!=-1){ close(1); dup2(fd_out,1); } //execute the program execv(path, inputArray); printColor("execv fatal error","red",true); exit(0); } //papa process else{ waitpid(forkInt,intArray,0); //read from pipe HEEERE??? } } return intArray[0]; } public static String which(String arguments){ ProcessBuilder prcBuilder = new ProcessBuilder(); prcBuilder.command("bash","-c","which "+arguments); try { Process process = prcBuilder.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); String line =null; StringBuffer buffer = new StringBuffer(); while ((line=reader.readLine())!=null){ buffer.append(line); } String path = buffer.toString(); if (path.contains("/")){ return path; } else{ int exitCode = process.waitFor(); return Integer.toString(exitCode); //System.out.println("\nExited with error code : " + exitCode); } } catch (IOException e) { printColor("err1","red",true); //e.toString(); } catch (InterruptedException e) { printColor("err2","red",true); //e.toString(); } return "t"; } public static boolean checkls(String input){ //check what ls does in this program if executed locally String[] inputArray = input.split("/"); String checkfile = inputArray[inputArray.length-1]; StringBuffer buffer1 = new StringBuffer(); if (input.indexOf("/")==0){ buffer1.append("/"); } for (int i=0;i