X-Git-Url: https://cgit.sxemacs.org/?a=blobdiff_plain;f=lisp%2Fserver.rb;h=87b094eff3cec6834e04ae8b25a7f79373879c4a;hb=2ab33ab15fea7f999d738bded402898995fd32b0;hp=59aadf076667de053e9be5c8ee72b0c07302ee92;hpb=78f91c18ee172720f5c11f0abf3945f349ad7ea9;p=riece diff --git a/lisp/server.rb b/lisp/server.rb index 59aadf0..87b094e 100644 --- a/lisp/server.rb +++ b/lisp/server.rb @@ -1,4 +1,26 @@ -# A simple IPC server executing Ruby programs. +# server.rb --- A simple IPC server executing Ruby programs. +# Copyright (C) 1998-2005 Daiki Ueno + +# Author: Daiki Ueno +# Created: 1998-09-28 +# Keywords: IRC, riece, Ruby + +# This file is part of Riece. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2, or (at your option) +# any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with GNU Emacs; see the file COPYING. If not, write to the +# Free Software Foundation, Inc., 59 Temple Place - Suite 330, +# Boston, MA 02111-1307, USA. require 'thread' require 'stringio' @@ -76,28 +98,13 @@ class Server send_line("S name #{name}\r\n") send_line("OK\r\n") Thread.current[:rubyserv_name] = name - out, log = @out, @log - env = Module.new - env.module_eval do - @out, @log = out, log - - def send_line(line) - @out.puts(line) - @log.puts(line) if @log - end - module_function :send_line - - def output(s) - send_line("# output #{Thread.current[:rubyserv_name]} #{s}\r\n") - end - module_function :output - end begin Thread.current[:rubyserv_error] = false - Thread.current[:rubyserv_response] = eval(r, env.module_eval {binding()}) + Thread.current[:rubyserv_response] = eval(r, exec_env.empty_binding) rescue Exception => e Thread.current[:rubyserv_error] = true - Thread.current[:rubyserv_response] = e.to_s.sub(/\A.*?\n/, '') + Thread.current[:rubyserv_response] = + e.to_s.sub(/\A.*?\n#{Regexp.quote(__FILE__)}:\d+: /o, '') end send_line("# exit #{name}\r\n") end @@ -164,6 +171,23 @@ class Server @out.puts(line) @log.puts(line) if @log end + + def exec_env + env = Object.new + def env.empty_binding + binding + end + out, log = @out, @log + env.instance_eval {@out, @log = out, log} + def env.send_line(line) + @out.puts(line) + @log.puts(line) if @log + end + def env.output(s) + send_line("# output #{Thread.current[:rubyserv_name]} #{s}\r\n") + end + env + end end if $0 == __FILE__ @@ -180,7 +204,7 @@ End opts.on('-e', '--err ERRFILE', 'Send stderr to ERRFILE.') do |errfile| opt_errfile = errfile end - opts.on('-e', '--log LOGFILE', 'Send stdlog to LOGFILE.') do |logfile| + opts.on('-l', '--log LOGFILE', 'Send log to LOGFILE.') do |logfile| opt_logfile = logfile end opts.on_tail('--help', '-h', 'Show this message.') do