Skip to content

Commit 90d0db7

Browse files
committed
MVP
1 parent a567417 commit 90d0db7

File tree

2 files changed

+92
-8
lines changed

2 files changed

+92
-8
lines changed

docker/apache/compile.php

+82-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

3-
$ip = $_SERVER['REMOTE_ADDR'];
3+
$ip = $_SERVER['REMOTE_ADDR'] ?? false;
4+
if ( ! is_string($ip) ) die('No REMOTE_ADDR');
45

56
$public = filter_var(
67
$ip,
@@ -12,9 +13,84 @@
1213
die('bad address');
1314
}
1415

15-
$content = isset($_POST['content']) ? $_POST['content'] : false;
16-
if ( ! is_string($content) ) die('No content');
16+
$code = $_POST['code'] ?? false;
17+
$input = $_POST['input'] ?? false;
18+
if ( ! is_string($code) ) die('No code');
1719

18-
$json = json_decode($content);
19-
if ( ! is_object($json)) die ('Bad JSON');
20-
die('Yada');
20+
$folder = sys_get_temp_dir() . '/compile-' . md5(uniqid());
21+
mkdir($folder);
22+
23+
$retval = new \stdClass();
24+
$retval->code = $code;
25+
$retval->input = $input;
26+
$retval->folder = $folder;
27+
$retval->compile_status = false;
28+
$retval->compile_out = false;
29+
$retval->compile_err = false;
30+
$retval->run_status = false;
31+
$retval->run_out = false;
32+
$retval->run_err = false;
33+
34+
35+
$descriptorspec = array(
36+
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
37+
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
38+
2 => array("pipe", "w") // stderr is a file to write to
39+
);
40+
41+
$cwd = $folder;
42+
$env = array(
43+
'some_option' => 'aeiou',
44+
'PATH' => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
45+
);
46+
47+
$command = 'gcc -ansi -x c -o a.out -';
48+
$process = proc_open($command, $descriptorspec, $pipes, $cwd, $env);
49+
50+
if (is_resource($process)) {
51+
// $pipes now looks like this:
52+
// 0 => writeable handle connected to child stdin
53+
// 1 => readable handle connected to child stdout
54+
// Any error output will be appended to /tmp/error-output.txt
55+
56+
fwrite($pipes[0], $code);
57+
fclose($pipes[0]);
58+
59+
$retval->compile_out = stream_get_contents($pipes[1]);
60+
fclose($pipes[1]);
61+
$retval->compile_err = stream_get_contents($pipes[2]);
62+
fclose($pipes[2]);
63+
64+
// It is important that you close any pipes before calling
65+
// proc_close in order to avoid a deadlock
66+
$retval->compile_status = proc_close($process);
67+
68+
}
69+
70+
if ( $retval->compile_status === 0 ) {
71+
72+
$command = './a.out';
73+
$process = proc_open($command, $descriptorspec, $pipes, $cwd, $env);
74+
75+
if (is_resource($process)) {
76+
// $pipes now looks like this:
77+
// 0 => writeable handle connected to child stdin
78+
// 1 => readable handle connected to child stdout
79+
// Any error output will be appended to /tmp/error-output.txt
80+
81+
fwrite($pipes[0], $input);
82+
fclose($pipes[0]);
83+
84+
$retval->run_out = stream_get_contents($pipes[1]);
85+
fclose($pipes[1]);
86+
$retval->run_err = stream_get_contents($pipes[2]);
87+
fclose($pipes[2]);
88+
89+
// It is important that you close any pipes before calling
90+
// proc_close in order to avoid a deadlock
91+
$retval->run_status = proc_close($process);
92+
93+
}
94+
95+
header("Content-type: application/json; charset=utf-8");
96+
echo(json_encode($retval, JSON_PRETTY_PRINT));

docker/apache/test.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
<h1>Test Harness</h1>
22
<form method="POST" action="compile.php" target="output">
33
<input type="submit">
4-
<textarea name="content" style="width:95%;" rows="20">
4+
<textarea name="code" style="width:95%;" rows="10">
5+
#include <stdio.h>
6+
7+
main() {
8+
printf("Hello world\n");
9+
}
10+
</textarea>
11+
<p>Input data (optional)</p>
12+
<textarea name="input" style="width:95%;" rows="5">
513
</textarea>
614
<h1>Output</h1>
7-
<iframe name="output" id="output" style="width:90%;height: 500px; border: 1px blue solid">
15+
<iframe name="output" id="output" style="width:95%;height: 500px; border: 1px blue solid">
816
</iframe>
917
</form>
1018

0 commit comments

Comments
 (0)