From: ueno Date: Thu, 11 Aug 2005 03:51:05 +0000 (+0000) Subject: * riece-ruby.el: Fixed example. X-Git-Tag: channel-coding-branchpoint~171 X-Git-Url: http://git.chise.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ffdbc4a2580cbf9b7481174eb0533b4fa94d5c1b;p=elisp%2Friece.git * riece-ruby.el: Fixed example. (riece-ruby-server-program): New variable. (riece-ruby-status-alist): New variable. (riece-ruby-status): Abolished. (riece-ruby-escaped-data): Renamed from riece-ruby-input. (riece-ruby-reset-process-buffer): New function. (riece-ruby-send-exit): New function. (riece-ruby-filter): Call riece-ruby-run-exit-handler if the program is exited. (riece-ruby-run-exit-handler): New function. (riece-ruby-execute): Don't expect program name. (riece-ruby-clear): New function. (riece-ruby-set-exit-handler): New function. * rubyserv.rb (RubyServ::dispatch_eval): Don't expect program name; eval under the binding of RubyServ::C. (RubyServ::dispatch_poll): Don't clear @thr. (RubyServ::dispatch_exit): New method. --- diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 7914d27..6bbb578 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,24 @@ +2005-08-11 Daiki Ueno + + * riece-ruby.el: Fixed example. + (riece-ruby-server-program): New variable. + (riece-ruby-status-alist): New variable. + (riece-ruby-status): Abolished. + (riece-ruby-escaped-data): Renamed from riece-ruby-input. + (riece-ruby-reset-process-buffer): New function. + (riece-ruby-send-exit): New function. + (riece-ruby-filter): Call riece-ruby-run-exit-handler if the + program is exited. + (riece-ruby-run-exit-handler): New function. + (riece-ruby-execute): Don't expect program name. + (riece-ruby-clear): New function. + (riece-ruby-set-exit-handler): New function. + + * rubyserv.rb (RubyServ::dispatch_eval): Don't expect program + name; eval under the binding of RubyServ::C. + (RubyServ::dispatch_poll): Don't clear @thr. + (RubyServ::dispatch_exit): New method. + 2005-08-10 Daiki Ueno * riece-ruby.el: New file. diff --git a/lisp/riece-ruby.el b/lisp/riece-ruby.el index 5b21239..db1bcbc 100644 --- a/lisp/riece-ruby.el +++ b/lisp/riece-ruby.el @@ -28,22 +28,22 @@ ;; It supports concurrent execution of Ruby programs in a single ;; session. For example: ;; -;; (riece-ruby-execute "t1" "sleep 30"); returns immediately -;; => nil +;; (riece-ruby-execute "sleep 30"); returns immediately +;; => "rubyserv0" ;; -;; (riece-ruby-execute "t2" "1 + 1") -;; => nil +;; (riece-ruby-execute "1 + 1") +;; => "rubyserv1" ;; -;; (riece-ruby-execute "t3" "\"") -;; => nil +;; (riece-ruby-execute "\"") +;; => "rubyserv2" ;; -;; (riece-ruby-inspect "t1") +;; (riece-ruby-inspect "rubyserv0") ;; => ((OK nil) nil "running") ;; -;; (riece-ruby-inspect "t2") +;; (riece-ruby-inspect "rubyserv1") ;; => ((OK nil) "2" "finished") ;; -;; (riece-ruby-inspect "t3") +;; (riece-ruby-inspect "rubyserv2") ;; => ((OK nil) "(eval):1: unterminated string meets end of file" "exited") ;;; Code: @@ -51,13 +51,15 @@ (defvar riece-ruby-command "ruby" "Command name for Ruby interpreter.") +(defvar riece-ruby-server-program "rubyserv.rb") + (defvar riece-ruby-process nil) (defvar riece-ruby-lock nil) (defvar riece-ruby-response nil) (defvar riece-ruby-data nil) -(defvar riece-ruby-input nil) -(defvar riece-ruby-status nil) +(defvar riece-ruby-escaped-data nil) +(defvar riece-ruby-status-alist nil) (defvar riece-ruby-output-handler-alist nil) (defvar riece-ruby-exit-handler-alist nil) @@ -91,7 +93,20 @@ index (- (match-end 0) 2))) data)) -(defun riece-ruby-send-eval (name program) +(defun riece-ruby-reset-process-buffer () + (save-excursion + (set-buffer (process-buffer riece-ruby-process)) + (buffer-disable-undo) + (make-local-variable 'riece-ruby-response) + (setq riece-ruby-response nil) + (make-local-variable 'riece-ruby-data) + (setq riece-ruby-data nil) + (make-local-variable 'riece-ruby-escaped-data) + (setq riece-ruby-escaped-data nil) + (make-local-variable 'riece-ruby-status-alist) + (setq riece-ruby-status-alist nil))) + +(defun riece-ruby-send-eval (program) (let* ((string (riece-ruby-escape-data program)) (length (- (length string) 998)) (index 0) @@ -101,42 +116,21 @@ data))) (setq data (cons (substring string index) data) data (nreverse data)) - (save-excursion - (set-buffer (process-buffer riece-ruby-process)) - (make-local-variable 'riece-ruby-lock) - (setq riece-ruby-lock nil) - (make-local-variable 'riece-ruby-response) - (setq riece-ruby-response nil) - (make-local-variable 'riece-ruby-data) - (setq riece-ruby-data nil) - (make-local-variable 'riece-ruby-input) - (setq riece-ruby-input nil) - (make-local-variable 'riece-ruby-status) - (setq riece-ruby-status nil)) - (process-send-string riece-ruby-process - (concat "EVAL " name "\r\n")) + (process-send-string riece-ruby-process "EVAL\r\n") (while data (process-send-string riece-ruby-process (concat "D " (car data) "\r\n")) (setq data (cdr data))) (process-send-string riece-ruby-process "END\r\n"))) - + (defun riece-ruby-send-poll (name) - (save-excursion - (set-buffer (process-buffer riece-ruby-process)) - (make-local-variable 'riece-ruby-lock) - (setq riece-ruby-lock nil) - (make-local-variable 'riece-ruby-response) - (setq riece-ruby-response nil) - (make-local-variable 'riece-ruby-data) - (setq riece-ruby-data nil) - (make-local-variable 'riece-ruby-input) - (setq riece-ruby-input nil) - (make-local-variable 'riece-ruby-status) - (setq riece-ruby-status nil)) (process-send-string riece-ruby-process (concat "POLL " name "\r\n"))) +(defun riece-ruby-send-exit (name) + (process-send-string riece-ruby-process + (concat "EXIT " name "\r\n"))) + (defun riece-ruby-filter (process input) (save-excursion (set-buffer (process-buffer process)) @@ -144,71 +138,115 @@ (insert input) (goto-char (process-mark process)) (beginning-of-line) - (while (looking-at ".*\r?\n") + (while (looking-at ".*\r\n") (if (looking-at "OK\\( \\(.*\\)\\)?\r") (progn - (if riece-ruby-input + (if riece-ruby-escaped-data (setq riece-ruby-data (mapconcat #'riece-ruby-unescape-data - riece-ruby-input ""))) - (setq riece-ruby-input nil + riece-ruby-escaped-data ""))) + (setq riece-ruby-escaped-data nil riece-ruby-response (list 'OK (match-string 2)) riece-ruby-lock nil)) (if (looking-at "ERR \\([0-9]+\\)\\( \\(.*\\)\\)?\r") (progn - (setq riece-ruby-input nil + (setq riece-ruby-escaped-data nil riece-ruby-response (list 'ERR (string-to-number (match-string 2)) (match-string 3)) riece-ruby-lock nil)) (if (looking-at "D \\(.*\\)\r") - (setq riece-ruby-input (cons (match-string 1) riece-ruby-input)) - (if (looking-at "S program \\(.*\\)\r") - (setq riece-ruby-status (match-string 1)) + (setq riece-ruby-escaped-data (cons (match-string 1) + riece-ruby-escaped-data)) + (if (looking-at "S \\(.*\\) \\(.*\\)\r") + (progn + (setq riece-ruby-status-alist (cons (cons (match-string 1) + (match-string 2)) + riece-ruby-status-alist)) + (if (member (car (car riece-ruby-status-alist)) + '("finished" "exited")) + (riece-ruby-run-exit-handler + (cdr (car riece-ruby-status-alist))))) (if (looking-at "# output \\(.*\\) \\(.*\\)\r") (let ((entry (assoc (match-string 1) riece-ruby-output-handler-alist))) (if entry (funcall (cdr entry) (match-string 2)))) (if (looking-at "# exit \\(.*\\)\r") - (let ((entry (assoc (match-string 1) - riece-ruby-exit-handler-alist))) - (if entry - (funcall (cdr entry)))))))))) + (riece-ruby-run-exit-handler (match-string 1)))))))) (forward-line)) (set-marker (process-mark process) (point-marker)))) +(defun riece-ruby-run-exit-handler (name) + (let ((entry (assoc name riece-ruby-exit-handler-alist))) + (if entry + (progn + (funcall (cdr entry)) + (setq riece-ruby-exit-handler-alist (delq entry)))))) + (defun riece-ruby-sentinel (process status) (kill-buffer (process-buffer process))) -(defun riece-ruby-execute (name program) +(defun riece-ruby-execute (program) (unless (and riece-ruby-process (eq (process-status riece-ruby-process) 'run)) - (setq riece-ruby-process - (start-process "riece-ruby" (generate-new-buffer " *Ruby*") - riece-ruby-command - (expand-file-name - "rubyserv.rb" - (file-name-directory - (symbol-file 'riece-ruby-execute))))) - (set-process-filter riece-ruby-process #'riece-ruby-filter) - (set-process-sentinel riece-ruby-process #'riece-ruby-sentinel)) + (let (selective-display + (coding-system-for-write 'binary) + (coding-system-for-read 'binary)) + (setq riece-ruby-process + (start-process "riece-ruby" (generate-new-buffer " *Ruby*") + riece-ruby-command + (if (file-name-absolute-p riece-ruby-server-program) + riece-ruby-server-program + (expand-file-name + riece-ruby-server-program + (file-name-directory + (symbol-file 'riece-ruby-execute)))))) + (set-process-filter riece-ruby-process #'riece-ruby-filter) + (set-process-sentinel riece-ruby-process #'riece-ruby-sentinel))) (save-excursion (set-buffer (process-buffer riece-ruby-process)) + (riece-ruby-reset-process-buffer) + (make-local-variable 'riece-ruby-lock) (setq riece-ruby-lock t) - (riece-ruby-send-eval name program) + (riece-ruby-send-eval program) (while riece-ruby-lock - (accept-process-output riece-ruby-process)))) + (accept-process-output riece-ruby-process)) + (if (eq (car riece-ruby-response) 'ERR) + (error "Couldn't execute: %S" (cdr riece-ruby-response))) + (cdr (assoc "name" riece-ruby-status-alist)))) (defun riece-ruby-inspect (name) (save-excursion (set-buffer (process-buffer riece-ruby-process)) + (riece-ruby-reset-process-buffer) + (make-local-variable 'riece-ruby-lock) (setq riece-ruby-lock t) (riece-ruby-send-poll name) (while (null riece-ruby-response) (accept-process-output riece-ruby-process)) (list riece-ruby-response riece-ruby-data - riece-ruby-status))) + riece-ruby-status-alist))) + +(defun riece-ruby-clear (name) + (save-excursion + (set-buffer (process-buffer riece-ruby-process)) + (riece-ruby-reset-process-buffer) + (make-local-variable 'riece-ruby-lock) + (setq riece-ruby-lock t) + (riece-ruby-send-exit name) + (while (null riece-ruby-response) + (accept-process-output riece-ruby-process)))) + +(defun riece-ruby-set-exit-handler (name handler) + (let ((entry (assoc name riece-ruby-exit-handler-alist))) + (if entry + (setcdr entry handler) + (setq riece-ruby-exit-handler-alist + (cons (cons name handler) + riece-ruby-exit-handler-alist))) + ;;check if the program already exited + (riece-ruby-inspect))) (provide 'riece-ruby) diff --git a/lisp/rubyserv.rb b/lisp/rubyserv.rb index 99e7de6..5fa85de 100644 --- a/lisp/rubyserv.rb +++ b/lisp/rubyserv.rb @@ -3,10 +3,14 @@ require 'thread' class RubyServ + module C + end + def initialize @buf = '' @que = Queue.new @thr = Hash.new + @cnt = 0 end def dispatch(line) @@ -56,21 +60,23 @@ class RubyServ end def dispatch_eval(c, r) - name, code = r.split(/\s+/, 2) - if @thr.include?(name) && @thr[name].alive? - puts("ERR 105 Parameter error: \"#{name}\" is already in use\r\n") - return + r = deq_data unless r + name = nil + Thread.exclusive do + while @thr.include?(name = @cnt.to_s) + @cnt += 1 + end + @thr[name] = Thread.current end - code = deq_data unless code + puts("S name #{name}\r\n") puts("OK\r\n") - @thr[name] = Thread.current Thread.current[:rubyserv_name] = name begin Thread.current[:rubyserv_error] = false - Thread.current[:rubyserv_response] = eval(code) + Thread.current[:rubyserv_response] = eval(r, C.module_eval('binding()')) rescue Exception => e Thread.current[:rubyserv_error] = true - Thread.current[:rubyserv_response] = e.to_s.sub(/\A.*?\n/m, '') + Thread.current[:rubyserv_response] = e end puts("# exit #{name}\r\n") end @@ -80,14 +86,13 @@ class RubyServ if !thr puts("ERR 105 Parameter error: no such name \"#{r}\"\r\n") elsif thr.alive? - puts("S program running\r\n") + puts("S running #{r}\r\n") puts("OK\r\n") else - @thr.delete(r) if thr[:rubyserv_error] - puts("S program exited\r\n") + puts("S exited #{r}\r\n") else - puts("S program finished\r\n") + puts("S finished #{r}\r\n") end if d = thr[:rubyserv_response] send_data(d.to_s) @@ -96,6 +101,17 @@ class RubyServ end end + def dispatch_exit(c, r) + thr = @thr[r] + if !thr + puts("ERR 105 Parameter error: no such name \"#{r}\"\r\n") + return + end + thr.kill if thr.alive? + @thr.delete(r) + puts("OK\r\n") + end + def escape(s) s.gsub(/[%\r\n]/) {|m| '%%%02X' % m[0]} end