On Sat, Jul 31, 2010 at 08:12, Silver Spoon
<jatinkumar.nitk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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
* * * * * WATCH THIS SPACE * * * * * | Ruby: http://mars.groupsite.com
--
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.