Newer
Older
2019-madoka / root / textcode.rb
@mamadoka mamadoka on 11 Nov 2019 1 KB editor.rb textcode.rb timeout 追加
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
Encoding.default_external = 'utf-8'

require'cgi'
require'open3'
require'sqlite3'
require'timeout'
  
c = CGI.new(:accept_charset => "UTF-8")

begin
Timeout.timeout(2){
print"Content-type: text/html; charset=utf-8\n\n"

coderead = c["example"]
namae = c["name"]
ans = c["ans"].delete("\r")

cmd = "ruby -Ku -e '#{coderead}'"
o,e,s = Open3.capture3(cmd) #o,e,s = 実行結果、エラー文、プロセス?

print <<EOF
<DOCTYPE html>
  <head> 
    <meta charset=utf-8>
    <title>テキストエディタ読み込み</title>
    <link rel="stylesheet" type="text/css" href="root/editor.css">
    <style>
<!--
-->
    </style>
  </head>
  <body>
EOF


puts("<h1>入力したコード</h1>")
printf("<textarea name=\"example\" readonly=\"readonly\">%s</textarea>", coderead)


puts("<h1>実行結果</h1>")
puts("<div>")
printf("<pre>%s</pre>", o) #evalでRubyコードとして読み込む
puts("</div>")

if ans == o
  puts("<h2><span>sucsess!!おめでとう!!</span></h2>")
else
  puts("<h2>no!!!実行結果が違います</h2>")
  if e != ""  
    puts("<h2>エラー文</h2>")
    printf("<p>%s</p>\n", e)
  else
    puts("<p>エラー文は出てないよ!文字が間違っていたりしないかな</p>")
  end
end

puts("<a href=\"./editor.rb\">エディタ画面へ戻る</a>")
puts("</body>\n</html>")
}
rescue Timeout::Error
  file = File.open("errormes.txt", "r")
  puts file.read
  file.close
end