in reply to Accessing a C++ compiler through a CGI script

alright... heres my solution. I don't think it was the perl part that was so hard neccessarilly but there's a lot of weird/cool things you can do using ajax/dhtml. You should just be able to put these files in the same directory... and assuming your apache is ready to go, this should work.

if you have problems with this or need more help, let me know. i was very interested in this topic when you first made it... hope this helps. i hate to post so much JS on these forums... but here goes...



code.cgi
#!/usr/bin/perl use CGI; use strict; #getting code... my $cgi = new CGI; my $code = $cgi->param('code'); #making a temp file for the code open(CPP, ">code.cpp"); print CPP $code; close(CPP); #compiling file with output to file system("g++ -c code.cpp >& output.txt"); #HTTP headers print "Content-type: text/html\n"; print "\n"; #printing output file to response header open(OUTPUT, "<output.txt"); while(<OUTPUT>) { print $_ . "<BR>"; }


code.html
<HTML> <HEAD> <SCRIPT type="text/javascript" language="JavaScript"> //request object var req; function load_code(code) { //url and div var url="code.cgi"; var results_div = document.getElementById('results'); results_div.innerHTML = 'compiling...'; // code for Mozilla, etc. if (window.XMLHttpRequest) { req=new XMLHttpRequest(); req.onreadystatechange=reqChange; } //code for IE else if (window.ActiveXObject) { req=new ActiveXObject("Microsoft.XMLHTTP"); } //otherwise an error else { alert('unable to create xmlhttprequest object'); } //using post method (allows for 2000+ chars) if(req) { req.open("POST",url,true); req.setRequestHeader('Content-Type', 'application/x-www-form-url +encoded charset=UTF-8'); req.send("code=" + escape(code) + '\n'); } } //function called after perl script returns function reqChange() { // if xmlhttp shows "loaded" if (req.readyState==4) { // if "OK" if (req.status==200) { //getting div and the response var result_div = document.getElementById('results'); var response = req.responseText; //no response... no errors! if(response == "") { result_div.innerHTML = "There were no errors"; } //otherwise output errors else { result_div.innerHTML = response; } } //problem getting data... else { alert("Problem retrieving data") } } } </SCRIPT> <TITLE>Compiler Test</TITLE> </HEAD> <BODY> <TABLE width="100%" border=0> <TR> <TD width="50%"> <FORM name="compiler_form"> <TEXTAREA name="code" rows="30" cols="50"></TEXTAREA><b +r> <INPUT type="button" value="Compile" onClick='load_code +(this.form.code.value)'> </FORM> </TD> <TD width="50%"> <DIV id="results">Results</DIV> </TD> </TR> </TABLE> </BODY> </HTML>

Replies are listed 'Best First'.
Re^2: Accessing a C++ compiler through a CGI script
by Spidy (Chaplain) on Oct 27, 2005 at 04:38 UTC
    Ooooh, XmlHttpRequest is nifty. Thanks for all the code though, and I will let you know how it all goes in a couple days when I manage to get everything figured out.
      Also, here is a quick demo of the program actually working. I guess you could use this in the meantime until you get yours up and running. Best of luck.
        Alright, I've gotten my own version running, and tried yours, and it sort of appears to work, but it also gives an error when you actually compile things. (problem retrieving data). Any ideas?

        UPDATE: Also, after looking inside my web directory, it appears that there is no output.txt. Also, code.cpp isn't being created, either.