From 12df14ea0ba0d64236ceaeb0f3ad0990db60e03c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Janis=20Daniel=20Da=CC=88hne?=
 <janis.daehne2@student.uni-halle.de>
Date: Thu, 6 Jun 2019 22:45:06 +0200
Subject: [PATCH] - fixed frontend issue nr. 130 - added compiler options

---
 src/Main.java | 96 ++++++++++++++++++++++++---------------------------
 1 file changed, 46 insertions(+), 50 deletions(-)

diff --git a/src/Main.java b/src/Main.java
index 42509c9..1b3901b 100644
--- a/src/Main.java
+++ b/src/Main.java
@@ -9,7 +9,7 @@ import java.util.concurrent.*;
 @SuppressWarnings("JavadocReference")
 public class Main {
 
-  static String versionString = "2.2.0";
+  static String versionString = "2.3.0";
 
   //use stdin for test content (1 line)
   //TODO limit memory... currently we could use cgroups but problem with memswap (no permission and file not exists)
@@ -25,15 +25,6 @@ public class Main {
     echo 5000000 > /sys/fs/cgroup/memory/stest2/memory.memsw.limit_in_bytes
      */
 
-  /**
-   * a unique identifier for the overlay to use (e.g. with firejail)
-   * we cannot use the same for every user else the cleanup would wipe other users changes
-   * <p>
-   * so we need to get a unique id from the calling process if we use a sandbox
-   */
-  static String uniqueOverlayFSIdentifier = "fromcallingprogram";
-  static String overlayUserDir = "/home/yapextester/.firejail/";
-
   static int readLinesCount = 0; //store over thread lifetime
   static int readErrLinesCount = 0; //store over thread lifetime
   static int writeLinesCount = 0; //store over thread lifetime
@@ -247,7 +238,6 @@ public class Main {
   static String DefaultCGroupName = "yapex";
 
 
-
   /**
    * we rename some files (e.g. files in the compare files test so that we don't get file name collisions when we e.g. ccompare f1 f1)
    * we rename first f1 to some unique string and then the user program can write f1 normally
@@ -284,7 +274,6 @@ public class Main {
     //e.g. args
     //blackBoxTest "C:\Users\theju\syndromTetsts\14717247 30" Main.java "\"C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\jdk1.8.0_65\bin\javac\" \"#5\"" "\"C:\Program Files (x86)\JetBrains\IntelliJ IDEA 14.1.4\jdk1.8.0_65\bin\java\" \"-cp\" \"#1\" \"#2\"" 1000 1000 1000 "java" true
 
-
     totalTimeTimer.start();
 
     if (isDebug) {
@@ -292,18 +281,17 @@ public class Main {
     }
 
 
-
     Main.args = myArgs;
 
-    if (args.length == 8 && Objects.equals(args[0], compileMethod)) {
+    if (args.length == 9 && Objects.equals(args[0], compileMethod)) {
 
-      uniqueOverlayFSIdentifier = args[5];
+//      uniqueOverlayFSIdentifier = args[5];
       DirectoryPath = args[1];
 
 
       boolean doDebugOutput = args[7].equals("1");
 
-      runResult = compile(args[1], args[2], args[3], args[4], args[5], doDebugOutput);
+      runResult = compile(args[1], args[2], args[3], args[4], args[5], args[8], doDebugOutput);
 
       totalTimeTimer.end();
       outputHeaderPart();
@@ -328,11 +316,11 @@ public class Main {
       System.exit(runResult);
       return;
 
-    } else if (args.length == 16 && Objects.equals(args[0], executeBlackBoxTestMethod)) {
+    } else if (args.length == 17 && Objects.equals(args[0], executeBlackBoxTestMethod)) {
 
       boolean doDebugOutput = args[15].equals("1");
 
-      uniqueOverlayFSIdentifier = args[14];
+//      uniqueOverlayFSIdentifier = args[14];
       DirectoryPath = args[1];
 
 
@@ -365,11 +353,11 @@ public class Main {
       //done in php (no need to compile or write any file
 
 
-    } else if (args.length == 16 && Objects.equals(args[0], executeJustRunTestMethod)) {
+    } else if (args.length == 17 && Objects.equals(args[0], executeJustRunTestMethod)) {
 
       boolean doDebugOutput = args[15].equals("1");
 
-      uniqueOverlayFSIdentifier = args[14];
+//      uniqueOverlayFSIdentifier = args[14];
       DirectoryPath = args[1];
 
 
@@ -398,11 +386,11 @@ public class Main {
       System.exit(runResult);
       return;
 
-    } else if (args.length == 17 && Objects.equals(args[0], executeCompareFilesTestMethod)) {
+    } else if (args.length == 18 && Objects.equals(args[0], executeCompareFilesTestMethod)) {
 
       boolean doDebugOutput = args[15].equals("1");
 
-      uniqueOverlayFSIdentifier = args[14];
+//      uniqueOverlayFSIdentifier = args[14];
       DirectoryPath = args[1];
 
       runResult = executeCompareFilesTest(args, doDebugOutput);
@@ -440,6 +428,7 @@ public class Main {
 
   /**
    * you must call this before
+   *
    * @see OutputConversation
    * else it is not printed
    */
@@ -471,6 +460,7 @@ public class Main {
 
   /**
    * outputs the saved conversation to the stdout
+   *
    * @see outputHeaderPart which must be called fore this
    */
   public static void OutputConversation() {
@@ -574,7 +564,7 @@ public class Main {
   /**
    * calculates the md5 string for the given file
    *
-   * @param file the file
+   * @param file          the file
    * @param doDebugOutput
    * @return the md5 hex string or null
    */
@@ -620,6 +610,7 @@ public class Main {
 
   /**
    * returns a guid
+   *
    * @return
    */
   public static String getGuid() {
@@ -630,8 +621,9 @@ public class Main {
   public static int executeCompareFilesTest(String[] args, boolean doDebugOutput) {
 
 
+    //these are for the execution of the (temp) black box test
     List<String> argsCopy = new ArrayList<String>();
-    for (int i = 0; i <= 15; i++) {
+    for (int i = 0; i <= 16; i++) {
       argsCopy.add(args[i]);
     }
     String[] argsCopy2 = new String[argsCopy.size()];
@@ -644,7 +636,7 @@ public class Main {
 
     String[] hashPairs = fileHahesString.split("\\s+");
 
-    for(String pair : hashPairs) {
+    for (String pair : hashPairs) {
 
       if (pair.equals("")) continue;
 
@@ -797,7 +789,7 @@ public class Main {
     //after the program executed the files should exist
     //check file integrity (md5 should be the same after the user program was run)
 
-    for(CompareFileTest compareTest : compareFileTests) {
+    for (CompareFileTest compareTest : compareFileTests) {
 
       String fileExpectedHash = Main.filesHashMap.get(compareTest.fileName1WithExtension);
 
@@ -988,7 +980,7 @@ public class Main {
    * @param sourceFileExtensionsWithoutDot the extensions of the soruce files for the current plang (to not include asset files in the compilation files)
    * @return the test result code
    */
-  public static int compile(String directory, String mainFileNameWithExtension, String compileTimeoutInMsString, String compileCmd, String sourceFileExtensionsWithoutDot, boolean doDebugOutput) {
+  public static int compile(String directory, String mainFileNameWithExtension, String compileTimeoutInMsString, String compileCmd, String sourceFileExtensionsWithoutDot, String compilerOptions, boolean doDebugOutput) {
 
 
     File directoryFile = new File(directory);
@@ -1013,7 +1005,8 @@ public class Main {
     }
 
     //better use the tokenizer ... else we need to escape the arguments what is ... bad
-    List<String> arguments = transformCommandWithSpecialStrings(directoryFile, mainFile, mainFileNameWithExtension, compileCmd, -1, -1, -1, sourceFileExtensionsWithoutDot, doDebugOutput);
+    List<String> arguments = transformCommandWithSpecialStrings(directoryFile, mainFile, mainFileNameWithExtension, compileCmd, -1, -1, -1,
+        sourceFileExtensionsWithoutDot, compilerOptions, doDebugOutput);
 
     if (arguments == null) {
       debugOutput(OutputPrefix + " Could not read other files");
@@ -1021,7 +1014,6 @@ public class Main {
     }
 
 
-
     try {
       compileTimeoutInMs = Integer.parseInt(compileTimeoutInMsString);
     } catch (NumberFormatException e) {
@@ -1104,7 +1096,7 @@ public class Main {
 
     try {
       p.destroy();
-    } catch(Exception e) {
+    } catch (Exception e) {
 //        e.printStackTrace();
       debugOutput(toSafeLine(OutputPrefix + " could not terminate compiling user program"));
     }
@@ -1156,6 +1148,7 @@ public class Main {
     String maxLinesToReadString = args[11];
     String maxErrLinesToReadString = args[12];
     String maxLinesToWriteString = args[13];
+    String compilerOptions = args[16];
 
     String arguments = "";
 
@@ -1310,7 +1303,7 @@ public class Main {
     //check if we need to compile
     if (shouldCompile) {
       compileResult = compile(directoryFile.getAbsolutePath(), mainFileNameWithExtension, compileTimeoutString,
-          compileCmd, sourceFileExtensionsWithoutDot, doDebugOutput);
+          compileCmd, sourceFileExtensionsWithoutDot, compilerOptions, doDebugOutput);
     }
 
     if (isDebug || doDebugOutput) {
@@ -1373,7 +1366,7 @@ public class Main {
 
     List<String> cmdToEx = transformCommandWithSpecialStrings(directoryFile, mainFile,
         mainFileNameWithExtension, executeTestCmd, timeoutInMs, memoryLimitInKb, diskLimitInKb,
-        sourceFileExtensionsWithoutDot, doDebugOutput);
+        sourceFileExtensionsWithoutDot, null, doDebugOutput);
 
     if (cmdToEx == null) {
       debugOutput(OutputPrefix + " Could not read other files");
@@ -2125,18 +2118,21 @@ public class Main {
    *
    * @param directoryFile                  the base directory where all files are located
    * @param mainFileNameWithExtension      the main file
-   * @param commandToExecute   error            the command to execute
+   * @param commandToExecute               error            the command to execute
    * @param timeoutInMs                    the timeout in ms (use 0 or negative value of not set)
    * @param memoryLimitInKb                the memory limit in kb (use 0 or negative value of not set)
    * @param diskLimitInKb                  the disk limit in kb (use 0 or negative value of not set)
-   * @param sourceFileExtensionsWithoutDot the extensions of the soruce files for the current plang (to not include asset files in the compilation files)
+   * @param sourceFileExtensionsWithoutDot the extensions of the sources files for the current plang (to not include asset files in the compilation files)
+   * @param compilerOptions                the compiler options or empty/null
    * @return the transformed command or null if the files could not be read
    */
   private static List<String> transformCommandWithSpecialStrings(File directoryFile, File mainFile,
                                                                  String mainFileNameWithExtension,
                                                                  String commandToExecute, int timeoutInMs,
                                                                  int memoryLimitInKb, int diskLimitInKb,
-                                                                 String sourceFileExtensionsWithoutDot, boolean doDebugOutput) {
+                                                                 String sourceFileExtensionsWithoutDot,
+                                                                 String compilerOptions,
+                                                                 boolean doDebugOutput) {
 
     final String[] sourceFileExtensions = sourceFileExtensionsWithoutDot.split(",");
 
@@ -2146,6 +2142,12 @@ public class Main {
 
     List<String> executionParts = ArgumentTokenizer.tokenize(commandToExecute);
 
+    List<String> compilerOptionsList = null;
+
+    if (compilerOptions != null && compilerOptions.equals("") == false) {
+      compilerOptionsList = ArgumentTokenizer.tokenize(compilerOptions);
+    }
+
     StringBuilder fileNameList = new StringBuilder();
     StringBuilder fileNameWithExtensionsList = new StringBuilder();
     StringBuilder fileNameWithAbsolutePathList = new StringBuilder();
@@ -2212,16 +2214,6 @@ public class Main {
       }
     }
 
-    if ((isDebug || doDebugOutput) && commandToExecute.contains(Main.SpecialParamsSign + "50")) {
-      debugOutput(OutputPrefix + " compile command contains " + Main.SpecialParamsSign + "50" + " but compile command do not use this, set to empty");
-    }
-    if ((isDebug || doDebugOutput) && commandToExecute.contains(Main.SpecialParamsSign + "51")) {
-      debugOutput(OutputPrefix + " compile command contains " + Main.SpecialParamsSign + "51" + " but compile command do not use this, set to empty");
-    }
-    if ((isDebug || doDebugOutput) && commandToExecute.contains(Main.SpecialParamsSign + "51")) {
-      debugOutput(OutputPrefix + " compile command contains " + Main.SpecialParamsSign + "52" + " but compile command do not use this, set to empty");
-    }
-
     //inject special char values
     for (int i = 0; i < executionParts.size(); i++) {
       String executionPart = executionParts.get(i);
@@ -2257,21 +2249,21 @@ public class Main {
 
       } else if (executionPart.contains(Main.SpecialParamsSign + "50")) {
 
-        if (timeoutInMs <= 0) {
+        if (timeoutInMs > 0) {
           executionParts.set(i, executionPart.replace(Main.SpecialParamsSign + "50", timeoutInMs + ""));
         } else {
           executionParts.set(i, executionPart.replace(Main.SpecialParamsSign + "50", ""));
         }
 
       } else if (executionPart.contains(Main.SpecialParamsSign + "51")) {
-        if (memoryLimitInKb <= 0) {
+        if (memoryLimitInKb > 0) {
           executionParts.set(i, executionPart.replace(Main.SpecialParamsSign + "51", memoryLimitInKb + ""));
         } else {
           executionParts.set(i, executionPart.replace(Main.SpecialParamsSign + "51", ""));
         }
 
       } else if (executionPart.contains(Main.SpecialParamsSign + "52")) {
-        if (diskLimitInKb <= 0) {
+        if (diskLimitInKb > 0) {
           executionParts.set(i, executionPart.replace(Main.SpecialParamsSign + "52", diskLimitInKb + ""));
         } else {
           executionParts.set(i, executionPart.replace(Main.SpecialParamsSign + "52", ""));
@@ -2281,6 +2273,10 @@ public class Main {
 
     }
 
+    if (compilerOptionsList != null) {
+      executionParts.addAll(compilerOptionsList);
+    }
+
     return executionParts;
   }
 
@@ -2413,7 +2409,7 @@ class StreamGobbler extends Thread {
         }
         rounds++;
 
-        safeLine = Main.toSafeLine(linePrefixToAdd +line);
+        safeLine = Main.toSafeLine(linePrefixToAdd + line);
         Main.Conversation.add(safeLine);
       }
 
@@ -2454,7 +2450,7 @@ class CompareFileTest {
         + (ignoreCase ? " " + Main.compare_ignoreCase : "")
         + (ignoreLeadingWhitespaces ? " " + Main.compare_ignoreLeadingWhitespaces : "")
         + (ignoreTrailingWhitespaces ? " " + Main.compare_ignoreTrailingWhitespaces : "")
-        + (ignoreAllEmptyOrWhitespacesOnlyLines ? " " + Main.compare_ignoreAllEmptyOrWhitespacesOnlyLines: "")
+        + (ignoreAllEmptyOrWhitespacesOnlyLines ? " " + Main.compare_ignoreAllEmptyOrWhitespacesOnlyLines : "")
         ;
 
   }
-- 
GitLab