diff --git a/bin/box2.rb b/bin/box2.rb new file mode 100755 index 0000000..d58b555 --- /dev/null +++ b/bin/box2.rb @@ -0,0 +1,19 @@ +#!/usr/bin/ruby + +require 'safebox' + +_ = _e = nil +$stdout.print "(0)$ " +db = {} +db.taint +STDIN.each.each_with_index do |line, i| + ret = Safebox.run line, Class.new( Safebox::Box), db, _, _e + if :value == ret.first + _ = ret.last + $stdout.puts "=> #{ret.last.inspect}" + else + _e = ret.last + $stdout.puts ret.last.inspect, ret.last.backtrace[0..-4].map( &"\t%s".method( :%)), "\tSafebox:1:in `run'" + end + $stdout.print "(#{i+1})$ " +end diff --git a/lib/safebox/box.rb b/lib/safebox/box.rb index c85133f..1ac7ffa 100644 --- a/lib/safebox/box.rb +++ b/lib/safebox/box.rb @@ -3,7 +3,7 @@ require 'safebox/safebox' class Safebox::Box attr_reader :_, :db - def initialize db, _ = nil, _e + def initialize db, _ = nil, _e = nil @_, @db, @_e = _, db, _e end diff --git a/lib/safebox/safebox.rb b/lib/safebox/safebox.rb index b6efb34..a848bdb 100644 --- a/lib/safebox/safebox.rb +++ b/lib/safebox/safebox.rb @@ -8,7 +8,7 @@ module Safebox $SAFE = 4 this = box.new *paras begin - [:value, this.instance_eval( exe, "Safebox")] + [:value, String === exe ? this.instance_eval( exe, "Safebox") : this.instance_eval( &exe)] rescue Object [:exception, $!] end @@ -23,5 +23,22 @@ module Safebox end end alias new_class create_class + + def on_exception exc + $stdout.puts "#{exc} (#{exc.class})\n\t#{exc.backtrace.join"\n\t"}" + rescue Object + on_exception $! + end + + def eval *paras, &exe + ret = self.run( *paras, &exe) + case ret.first + when :exception # Really unsecure. Somebody can create an own exception with own #to_s, #class or #backtrace. + on_exception ret.last + nil + when :value then ret.last + end + end + public :eval end end