-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathcompiler.js
More file actions
36 lines (34 loc) · 1.42 KB
/
compiler.js
File metadata and controls
36 lines (34 loc) · 1.42 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
/**********************************************************************
*
* compiler.js: compila o arquivo file e executa-o (se o retorno da execução
* for 0, retorna a stdout do arquivo, caso contrário, exibe erro de compi
* lação).
*
***********************************************************************/
module.exports = function (file, res) {
const exec = require('child_process').exec;
const fs = require('fs');
//compila com o gcc
exec('gcc ' + file + '.c -o ' + file + ' -lm && timeout 2s ./' + file + ' < ' + file + '.txt', function (error, stdout, stderr) {
//se houver erro de compilação, respondemos a requisição com um erro.
res.setHeader('Content-Type', 'application/json');
if (error) {
console.log ("ERROR: " + error);
res.end(JSON.stringify({ error: "CODEI PRA CARALHO MAS NÃO COMPILOU!\n",
stdout: null
}));
}
else {
console.log ("STDOUT: " + stdout);
res.end(JSON.stringify({ error: null,
stdout: stdout
}));
}
})
.on('close', function () {
exec('rm ' + file + '*', function (error, stdout, stderr) {
console.log ('-----------------------------------------');
console.log ("REMOVING FILES");
});
});
};