Stdin/stdout/stderr problem in g++ and rails

In my rails controller, I am doing the following :

@output = g++ j.cpp -o "prog" && ./prog

This gives the output in the @output variable which i can display in
my view. But the above works only if the j.cpp is correct and doesn’t
expect any user input. How can I use the stdin/stderr and stdout
streams here so that :

If the user has to give input, I open a dialog box on the view with a
textfield where he/she can enter the input, and the program continues
to execute.
If there are any errors in the file, then I should be able to get the
errors and display them to the user.
I tried doing this :

@output = g++ j.cpp -o "prog" && ./prog| tee prog

This allows me to enter the user input at the server command prompt
(the server log…i don’t know what do we call it), but I want it to
be entered in a textfield in the view. Help me.

On Sat, Jul 31, 2010 at 08:12, Silver S. [email protected]
wrote:

In my rails controller, I am doing the following :

@output = g++ j.cpp -o "prog" && ./prog

This gives the output in the @output variable which i can display in
my view. But the above works only if the j.cpp is correct and doesn’t
expect any user input. How can I use the stdin/stderr and stdout
streams here so that :

If the user has to give input, I open a dialog box

Sorry, can’t help you there, at least nothing springs immediately to my
mind.

If there are any errors in the file, then I should be able to get the
errors and display them to the user.
I tried doing this :

@output = g++ j.cpp -o "prog" && ./prog| tee prog

There’s probably some more Rubyish way to do it by messing with the
definitions of stout and stderr, but a quick and dirty way to do it,
by messing with the the command line to make stderr go to stdout, is:

@output = g++ j.cpp -o "prog" 2&>1

Check $? to see if it succeeded (should be 0; make sure you check
IMMEDIATELY after the command). if so, then you can run prog as
normal.

This is an old shell-scripting type kluge that I wasn’t sure would
work, but does. Proof-of-concept code:

#! /usr/bin/ruby
out = ls -l #{ARGV.join ' '} 2>&1
status = $?
puts “command %sed (status %d)” % [(status == 0 ? ‘work’ : ‘fail’),
status]
puts out # insert your own joke here, folks!

Also be VERY careful about how you are feeding the program name into
the command, especially if there’s any way a user can influence it.
Google “SQL Injection”; it’s not just for SQL.

-Dave


Specialization is for insects. -RAH | Have Pun, Will Babble! -me
Programming Blog: http://codosaur.us | Work: http://davearonson.com
Leadership Blog: http://dare2xl.com | Play: http://davearonson.net