-
Notifications
You must be signed in to change notification settings - Fork 99
/
play_util.php
145 lines (130 loc) · 4.09 KB
/
play_util.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
use \Tsugi\Util\U;
function cc4e_play_header($lines) {
global $CFG;
?>
<link href="<?= $CFG->apphome ?>/static/codemirror-5.62.0/lib/codemirror.css" rel="stylesheet"/>
<style>
body {
font-family: Courier, monospace;
}
.CodeMirror { height: auto; border: 1px solid #ddd; }
/* .CodeMirror-scroll { max-height: <?= intval(($lines/13)*20) ?>em; } */
.pre_text {
height: auto;
max-height: 200px;
overflow: auto;
/*overflow-y: none;*/
background-color: #eeeeee;
}
</style>
<?php } ?>
<?php
function cc4e_play_errors($retval) {
global $LOGGED_IN;
if ( ! $LOGGED_IN ) return false;
$compiler = $retval->pipe->stderr ?? false;
if ( is_string($compiler) && strlen($compiler) > 0 ) {
echo '<pre style="color:red;">'."\n";
echo "Compiler errors:\n\n";
echo(htmlentities($compiler, ENT_NOQUOTES));
echo("</pre>\n");
return true;
} else if ( isset($retval->reject) && is_string($retval->reject) ) {
echo('<p style="color:red;">'.htmlentities($retval->reject).'</p>'."\n");
return true;
} else if ( isset($retval->allowed) && $retval->allowed === false ) {
$disallowed = "";
if ( isset($retval->disallowed) && is_array($retval->disallowed) && count($retval->disallowed) > 0 ) {
$disallowed = implode(', ',$retval->disallowed);
}
echo('<p style="color:red;">Your program used disallowed function(s) '.htmlentities($disallowed).'</p>'."\n");
return true;
} else if ( isset($retval->hasmain) && $retval->hasmain === false ) {
echo('<p style="color:blue;">Compile only: You need a main() for your code to be run</p>'."\n");
return true;
} else if ( isset($retval->pipe->stdout) && strlen($retval->pipe->stdout) > 0 ) {
return false;
} else if ( isset($retval->minimum) && $retval->minimum === false ) {
echo('<p style="color:red;">Your program did not produce any output</p>'."\n");
return true;
}
return false;
}
function cc4e_play_inputs($lines, $code) {
?>
<p>
<textarea id="mycode" name="code" style="height: auto;">
<?php
if ( is_string($code) ) {
echo(htmlentities($code));
} else {
?>
#include <stdio.h>
main() {
printf("Hello World\n");
}
<?php } ?>
</textarea>
<?php }
function cc4e_play_output($retval) {
global $LOGGED_IN;
?>
</p>
<?php
if ( ! $LOGGED_IN ) return;
// https://stackoverflow.com/questions/3008035/stop-an-input-field-in-a-form-from-being-submitted
if ( isset($retval->pipe->stdout) ) {
echo '<form style="color: blue;">'."\n";
echo '<div style="color: blue;">'."\n";
echo "Output from your program:\n\n";
// echo '<textarea id="myouput" readonly name="output" style="color: blue; width:100%; border: 1px black solid;">';
echo('<div id="myoutput" class="xpre_text"><pre>');
echo(htmlentities($retval->pipe->stdout, ENT_NOQUOTES));
// echo("</textarea>\n");
echo("</pre></div>\n");
echo("</div>\n");
echo("</form>\n");
}
?>
</p>
<?php
}
function cc4e_play_debug($retval) {
if ( ! is_object($retval) ) return;
?>
<pre style="display:none;" id="debug">
Debug output:
<?php
echo(htmlentities(json_encode($retval, JSON_PRETTY_PRINT), ENT_NOQUOTES));
if ( isset($retval->assembly->stdout) && is_string($retval->assembly->stdout) ) {
echo("\n\n------ Assembly --------\n");
echo(htmlentities($retval->assembly->stdout, ENT_NOQUOTES));
}
?>
</pre>
<?php } ?>
<?php function cc4e_play_footer() {
global $CFG, $LOGGED_IN;
?>
<script type="text/javascript" src="<?= $CFG->apphome ?>/static/codemirror-5.62.0/lib/codemirror.js"></script>
<script type="text/javascript" src="<?= $CFG->apphome ?>/static/codemirror-5.62.0/mode/clike/clike.js"></script>
<script>
var myTextarea = document.getElementById("mycode");
var editor = CodeMirror.fromTextArea(myTextarea, {
lineNumbers: true,
matchBrackets: true,
mode: "text/x-csrc"
});
</script>
<?php if ( $LOGGED_IN ) { ?>
<script>
$(document).ready(function() {
$('#runcode').attr('disabled' , false);
$('#runstatus').html('');
$('#editstatus').show();
});
</script>
<?php } ?>
<?php
}