Compiling c using ruby -
how go making website codepad? want able compile c code after user types in , out put success or error message. if successful, how able run parameters? (this coding competition site)
you can call c compiler ruby code using backticks. here quick , dirty example sinatra:
require 'sinatra' # display html form '/code' '<html><form method="post"><textarea name="code"></textarea><input type="submit" /></form></html>' end # compile code post '/code' exec = "gcc -x c -o tst - <<eof\n#{params[:code]} \neof\n" `#{exec}` end
when calling http://localhost:4567, form appears can enter c code. when press submit, code compiled tst
executable.
you can use backticks execute tst
whatever parameters want, similar done compilation process itself.
the stdin output can captured storing result of backtick command in variable, e.g.:
output = `tst`
stderr can captured redirecting stdin, e.g.:
output = `tst 2>&1`
nb: please careful though, security perspective, very dangerous: can submit c code, , compile , execute on server.
Comments
Post a Comment