Thursday, July 30, 2009

How can we compile and run a soure code wrtiten on java or C/C++ from Another webpage or application software?

Actually we want to implement a simpler version of an Online Judge like some available online judging system ( Valladolid(UVA) / Sphere / TJU / ZJU / Saratov Online Judge etc. ) which can receive a source code and verify the output produced by that source code .





But we can not find any solutions about how to run a source code ( java / c / c++) that we received.





We are interested to use PHP as our tools. ( any opinion ? )





* * * We just need to implement a simpler Judge. Not as sophisticated as Others.





Addresses of some Available Online Judge :


UVA - old - http://acm.uva.es/p


new - http://icpcres.ecs.baylor.edu/onlinejudg...


Saratov - http://acm.sgu.ru/


Sphere - http://acm.sgu.ru/

How can we compile and run a soure code wrtiten on java or C/C++ from Another webpage or application software?
Having been involved in writing one a while back, writing judging software is not straightforward at all, and there are some security considerations that you should take VERY seriously. That said, once you've somehow (this is the hard part) verified that this source code is safe, all you need to do is to compile it to a temporary executable with the languages native compiler and run it, capturing its stdout for making purposes. It should not be too difficult?





EDIT:


ok, to update with a more specific answer. I'm not good with PHP, but lets pretend that you're using perl which I'm more familiar with. The syntax will be very similar. This will be a little pseudocodey, but close enough to real perl.





# So suppose you have all your request data in the hash CGI,


# things will be a little different in PHP but I'm sure you can figure


# it out.





use IO::File;


use POSIX qw(tmpnam);





my $sourceCode = $CGI{source};





# try new temporary filenames until we get one that didn't already exist


do { $name = tmpnam() }


until $fh = IO::File-%26gt;new($name, O_RDWR|O_CREAT|O_EXCL);





# install atexit-style handler so that when we exit or die,


# we automatically delete this temporary file


END { unlink($name) or die "Couldn't unlink $name : $!" }





# now go on to use the file ...


print $fh $sourceCode;


system("javac $name");


if ( `java $name.class` == $expectedOutput) {


print("Correct!\n");


}else{


print("Incorrect!\n");


}





So the steps are:


- Read the input file


- write it to a temp file


- invoke the correct compile (in this case javac)


- run the compile program and capture its output (in perl we can use ` ` to do that)


- check the captured output.





Did that answer your questions?


No comments:

Post a Comment