Spidy has asked for the wisdom of the Perl Monks concerning the following question:

Earlier, I was asking how I would do this, and many people suggested using SSH. However, when I attempted to use SSH at school, it turns out that the school system was too secure(this is a fairly rare occurence).

However, that effectively limits the way that I can get my C++ over to the other computer and compile it, to see which errors it has. I need to create a CGI script that I can put on my linux box, that will take the code I give it, compile it, and give me any of the errors thrown at compile time. I am not planning on actually running the compiled program, I simply want to catch the output from the compiler when compiling.

Does anyone know how exactly I would do this?
I'm fairly certain that in order to get it all to work, I would need to have the CGI script write it's contents(form) out to a file(test.cpp or something), and then somehow call the compiler on it, and trap the errors.

However, I'm not 100% certain of how I would call the compiler and trap the errors. Would I be able to use a system call, something like
system(c++ -o testprogram test.cpp);
Or would I have to do it differently? And how would I capture the output of the compiler, so that I could see if there were any errors?

Thanks,
Spidy

Replies are listed 'Best First'.
Re: Accessing a C++ compiler through a CGI script
by Roy Johnson (Monsignor) on Oct 26, 2005 at 17:09 UTC
    See qx() or backquotes for capturing the output of a subprocess.

    Caution: Contents may have been coded under pressure.
Re: Accessing a C++ compiler through a CGI script
by chromatic (Archbishop) on Oct 26, 2005 at 17:54 UTC

    Are you trying to access a box elsewhere? Have you tried running SSH on a different port, such as 80 or 8000 or something your school doesn't block?

Re: Accessing a C++ compiler through a CGI script
by ickyb0d (Monk) on Oct 26, 2005 at 19:54 UTC

    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>
      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.
Re: Accessing a C++ compiler through a CGI script
by Errto (Vicar) on Oct 26, 2005 at 19:15 UTC
    I still think ssh is a good idea if you can manage it. If the problem is that you cannot get out without an HTTP proxy, then you can use an ssh client that supports HTTP proxies, such as mindterm.