--- ssh-agent.el 2004/03/04 10:15:38 1.1
+++ ssh-agent.el 2004/03/04 10:17:06
@@ -21,7 +21,7 @@
;; Run ssh-agent and ssh-add as emacs processes, setting the proper env
;; variables, and accepting password input
-(defcustom ssh-agent-buffer "*ssh-agent*"
+(defcustom ssh-agent-buffer " *ssh-agent*"
"buffer to display ssh-agent output in"
:type 'string
:group 'ssh-agent)
@@ -51,7 +51,7 @@
:type 'string
:group 'ssh-agent)
-(defcustom ssh-add-invalid-prompt "Bad passphrase, try again:"
+(defcustom ssh-add-invalid-prompt "Bad passphrase, try again for \\([^:]+\\):"
"ssh-add prompt indicating an invalid passphrase"
:type 'string
:group 'ssh-agent)
@@ -59,10 +59,28 @@
(defun ssh-agent ()
"execute the ssh-agent"
(interactive)
- (let ((args (split-string ssh-agent-args)))
- (set-process-filter
- (apply #'start-process "ssh-agent" nil ssh-agent-program args)
- #'ssh-agent-process-filter)))
+ (let ((args (split-string ssh-agent-args))
+ (sshagent (getenv "SSH_AGENT_PID"))
+ (sshadd (ssh-noadd-check)))
+ (cond
+ ((and sshagent sshadd)
+ (ssh-add-run))
+ ((not sshagent)
+ (set-process-filter
+ (apply #'start-process "ssh-agent" nil ssh-agent-program args)
+ #'ssh-agent-process-filter)
+ (add-hook 'kill-emacs-hook 'ssh-agent-kill))
+ (t
+ (message "no want.")))))
+
+(defun ssh-noadd-check ()
+ (with-temp-buffer
+ (call-process ssh-add-program nil (current-buffer) nil "-l")
+ (goto-char (point-min))
+ (looking-at "^The agent has no identities.")))
+
+(defun ssh-agent-kill ()
+ (call-process ssh-agent-program nil nil nil "-k"))
(defun ssh-add-run ()
"run ssh-add"
@@ -73,9 +91,13 @@
(defun ssh-agent-process-filter (process input)
"filter for ssh-agent input"
- (cond ((ssh-agent-read-var "SSH_AUTH_SOCK" input)
- (ssh-add-run))
- ((ssh-agent-read-var "SSH_AGENT_PID" input))))
+ (let ((lines (split-string input "\n")))
+ (while lines
+ (cond ((ssh-agent-read-var "SSH_AUTH_SOCK" (car lines))
+ ())
+ ((ssh-agent-read-var "SSH_AGENT_PID" (car lines))
+ (ssh-add-run)))
+ (setq lines (cdr lines)))))
(defun ssh-agent-read-var (var line)
"read a shell script variable from ssh-agent's output"
@@ -83,17 +105,21 @@
(with-current-buffer (get-buffer-create ssh-agent-buffer)
(let ((value (match-string 1 line)))
(setenv var value)
- (insert line))
+ (insert line "\n"))
t)))
(defun ssh-add-process-filter (process input)
"filter for ssh-add input"
+ (when (string-match "^\n" input)
+ (setq input (substring input 1)))
(cond ((string-match ssh-add-prompt input)
(ssh-send-passwd process input))
((string-match ssh-add-invalid-prompt input)
(ssh-send-passwd process input))
(t (with-current-buffer (get-buffer-create ssh-agent-buffer)
- (insert input)))))
+ (when (string-match "^Identity added:" input)
+ (message input))
+ (insert "\n" input)))))
(defun ssh-send-passwd (process prompt)
"read a password with `read-passwd` and pass it to the ssh-add process"
@@ -102,3 +128,4 @@
(process-send-string process "\n")
(clear-string passwd)))
+(provide 'ssh-agent)
立ち上げた ssh-agent を止めないから、勝手に増殖していたことと、process filter の入力が各ラインでばらばらにくると仮定していたところはちょっとなぁ。。。
こんなことをやっている場合じゃない。
2004/3/2 version 2.59 - Mozilla Firefox 上で M-v が期待通りに動かない問題を解消。とのこと。ちょっと動きが変かもしれない。