Repository navigation
Expand file tree
/
Copy pathSimulator.php
More file actions
61 lines (55 loc) · 1.47 KB
/
Copy pathSimulator.php
File metadata and controls
61 lines (55 loc) · 1.47 KB
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
<?php
session_start();
$sid = session_id();
$type = $_POST['type'];
$code = $_POST['code'];
$stdin = $_POST['stdin'];
// **** make files
$fileName = 'prog_' . $sid;
$file = fopen($fileName . '.s', 'w');
fwrite($file, $code);
fclose($file);
$file = fopen($fileName . '.in', 'w');
fwrite($file, $stdin);
fclose($file);
// **** assemble
$ext = "s";
$compiler = '';
if ($type == 'arm-windows') {
$compiler = "arm-elf-gcc";
} else if ($type == 'arm-linux') {
$compiler = "arm-linux-gnueabi-gcc";
}
$compile = $compiler . " -Wall -o " . $fileName . ".o " . $fileName . "." . $ext . " > " . $fileName . ".cout";
exec("$compile 2>&1", $cout, $cerr);
readfile($fileName . ".cout");
// **** simulate
if (file_exists($fileName . '.o')) {
$simulator = '';
if ($type == 'arm-windows') {
$simulator = 'arm-elf-run ';
} else if ($type == 'arm-linux') {
$simulator = 'qemu-arm -L /usr/arm-linux-gnueabi ';
}
$simulate = $simulator . $fileName . ".o < " . $fileName . ".in > " . $fileName . ".out";
exec("$simulate 2>&1", $sout, $serr);
readfile($fileName . ".out");
if (!file_exists($fileName . '.out')) {
echo $serr;
}
} else {
echo $cerr;
}
// **** delete temp files
unlink($fileName . '.s');
unlink($fileName . '.in');
if (file_exists($fileName . '.o')) {
unlink($fileName . '.o');
}
if (file_exists($fileName . '.cout')) {
unlink($fileName . '.cout');
}
if (file_exists($fileName . '.out')) {
unlink($fileName . '.out');
}
?>