index.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. include_once("../../../auth.php");
  3. if (isset($_GET['checkBinaryExists'])){
  4. $filepath = realpath("../" . $_GET['checkBinaryExists']);
  5. $ext = pathinfo($filepath, PATHINFO_EXTENSION);
  6. if ($ext !== "c"){
  7. die("false");
  8. }
  9. if (file_exists(dirname($filepath) . "/" . basename($filepath,".c") . ".exe") || file_exists(dirname($filepath) . "/" . basename($filepath,".c") . ".out")){
  10. die("true");
  11. }else{
  12. die("false");
  13. }
  14. }
  15. ?>
  16. <html>
  17. <head>
  18. <title>gcci</title>
  19. <link rel="stylesheet" href="../../../script/tocas/tocas.css">
  20. <script src="src/jquery.min.js"></script>
  21. <script src="../../../script/ao_module.js"></script>
  22. <style>
  23. body{
  24. background-color:white;
  25. }
  26. .invalid{
  27. color:#c23030;
  28. }
  29. .ready{
  30. color:#30c257;
  31. }
  32. .error{
  33. background-color:#f7e5e4;
  34. }
  35. .testcases{
  36. margin-top:8px;
  37. width:100%;
  38. display:none;
  39. }
  40. .terminalBlock{
  41. background-color:black;
  42. font-family: monospace;
  43. padding:8px;
  44. color:white;
  45. }
  46. </style>
  47. </head>
  48. <body>
  49. <br>
  50. <div class="ts container">
  51. Current Script: <span id="scriptname"></span><br>
  52. <span id="status"></span><br>
  53. <button class="tiny ts button" onClick="$('.testcases').slideToggle('fast');"><i class="caret down icon"></i>Test Cases</button>
  54. <button class="tiny ts button" style="margin-right:6px;" onClick="compile();">Compile Only</button>
  55. <button class="tiny ts primary button" onClick="compile(true);">Compile & Run</button>
  56. <div class="testcases">
  57. <p>
  58. Fill in the input below for testing paramters.<br>
  59. Example: <code>-h</code> will print out the result for <code>./runThis -h</code>.
  60. </p>
  61. <div class="ts fluid input" style="padding-bottom:8px !important;">
  62. <textarea id="testcases" placeholder="Test Parameter, each test occupy one line."></textarea>
  63. </div>
  64. <button class="tiny ts primary button" onClick="test();">Run Test Cases</button>
  65. </div>
  66. <br>
  67. <div class="ts divider"></div>
  68. <div id="compileResult" class="ts text container" style="padding:10px;">
  69. </div>
  70. <div id="terminalOutput">
  71. </div>
  72. </div>
  73. <br><br>
  74. <script>
  75. var parentUID = ao_module_parentID;
  76. var currentFileIsSourceCode = false;
  77. var currentSource = "";
  78. checkParentFocusDocument();
  79. setInterval(checkParentFocusDocument,3000);
  80. //Recover the test case data if exists.
  81. var testcases = ao_module_getStorage("NotepadA","gcci-testcase");
  82. if (!(testcases == null || testcases == undefined)){
  83. $('#testcases').val(testcases);
  84. }
  85. $('#testcases').bind('input propertychange', function() {
  86. //Try to store the value into the browser's localstorage
  87. ao_module_saveStorage("NotepadA","gcci-testcase",$('#testcases').val());
  88. });
  89. function test(){
  90. $.get("index.php?checkBinaryExists=" + currentSource,function(data){
  91. if (data == "true"){
  92. //Run the test
  93. let parameters = $("#testcases").val().split("\n");
  94. var sendObject = JSON.stringify(parameters);
  95. $.get("test.php?testpara=" + sendObject + "&source=" + currentSource,function(data){
  96. if (data.includes("ERROR") == false){
  97. $("#terminalOutput").html("");
  98. for (var i =0; i < data.length; i++){
  99. $("#terminalOutput").append('<div class="terminalBlock">\
  100. >' + basename(currentSource) + ' ' + parameters[i] + '<br>\
  101. ' + data[i] + '\
  102. </div>');
  103. }
  104. }
  105. });
  106. }else{
  107. alert("Execution file not found. Please compile your code first before testing.");
  108. }
  109. });
  110. }
  111. function compile(run=false){
  112. $("#terminalOutput").html("");
  113. if (!currentFileIsSourceCode){
  114. if (!confirm("This doesn't seems like a c source code file. Compile anyway?")){
  115. return;
  116. }
  117. }
  118. $("#compileResult").text("Waiting for server response...");
  119. var command = "";
  120. if (run){
  121. command = "&run";
  122. }
  123. $.ajax({url: "compile.php?source=" + currentSource + command,
  124. success: function(data){
  125. $("#compileResult").html(data);
  126. if (data.includes("Error")){
  127. $("#compileResult").addClass("error");
  128. }else{
  129. $("#compileResult").removeClass("error");
  130. }
  131. }
  132. });
  133. }
  134. function checkParentFocusDocument(){
  135. var parentDOM = parent.document.getElementById(parentUID);
  136. var editor = $(parentDOM).find("iframe")[0].contentWindow.document;
  137. var focusedDocument = $(editor).find(".fileTab.focused");
  138. if (focusedDocument.length == 0){
  139. //No document focused
  140. return;
  141. }
  142. var currentFilepath = $(focusedDocument).attr("filename");
  143. $("#scriptname").text(basename(currentFilepath));
  144. currentSource = currentFilepath;
  145. if (getFileExtension(currentFilepath) == "c"){
  146. //This is a c source code file
  147. $("#status").text("Ready to compile");
  148. $("#status").removeClass("invalid").addClass("ready");
  149. currentFileIsSourceCode = true;
  150. }else{
  151. //This is not a c source code file
  152. $("#status").text("Not C source code");
  153. $("#status").removeClass("ready").addClass("invalid");
  154. currentFileIsSourceCode = false;
  155. }
  156. }
  157. function basename(filepath){
  158. if (filepath.includes("/")){
  159. var tmp = filepath.split("/");
  160. return tmp.pop();
  161. }else{
  162. return filepath;
  163. }
  164. }
  165. function getFileExtension(filepath){
  166. if (filepath.includes(".") == false){
  167. return "";
  168. }
  169. var tmp = filepath.split(".");
  170. return tmp.pop();
  171. }
  172. </script>
  173. </body>
  174. </html>