1 ;;; ssh.el --- remote login interface
3 ;; Copyright (C) 1996, 1997, 1998 Noah S. Friedman
5 ;; Author: Noah Friedman <friedman@splode.com>
6 ;; Maintainer: friedman@splode.com
7 ;; Keywords: unix, comm
10 ;; $Id: ssh.el,v 1.6 1998/09/11 01:14:34 friedman Exp $
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program; if not, you can either send email to this
24 ;; program's maintainer or write to: The Free Software Foundation,
25 ;; Inc.; 59 Temple Place, Suite 330; Boston, MA 02111-1307, USA.
29 ;; Support for remote logins using `ssh'.
30 ;; This program is layered on top of shell.el; the code here only accounts
31 ;; for the variations needed to handle a remote process, e.g. directory
32 ;; tracking and the sending of some special characters.
34 ;; If you wish for ssh mode to prompt you in the minibuffer for
35 ;; passwords when a password prompt appears, just enter m-x send-invisible
36 ;; and type in your line, or add `comint-watch-for-password-prompt' to
37 ;; `comint-output-filter-functions'.
45 "Secure remote login interface"
49 (defcustom ssh-program "ssh"
50 "*Name of program to invoke ssh"
54 (defcustom ssh-explicit-args '()
55 "*List of arguments to pass to ssh on the command line."
56 :type '(repeat (string :tag "Argument"))
59 (defcustom ssh-mode-hook nil
60 "*Hooks to run after setting current buffer to ssh-mode."
64 (defcustom ssh-process-connection-type t
65 "*If non-`nil', use a pty for the local ssh process.
66 If `nil', use a pipe (if pipes are supported on the local system).
68 Generally it is better not to waste ptys on systems which have a static
69 number of them. However, ssh won't allocate a pty on the remote host
70 unless one is used locally as well."
71 :type '(choice (const :tag "ptys" t)
72 (const :tag "pipes" nil))
75 (defcustom ssh-directory-tracking-mode 'local
76 "*Control whether and how to do directory tracking in an ssh buffer.
78 nil means don't do directory tracking.
80 t means do so using an ftp remote file name.
82 Any other value means do directory tracking using local file names.
83 This works only if the remote machine and the local one
84 share the same directories (through NFS). This is the default.
86 This variable becomes local to a buffer when set in any fashion for it.
88 It is better to use the function of the same name to change the behavior of
89 directory tracking in an ssh session once it has begun, rather than
90 simply setting this variable, since the function does the necessary
91 re-synching of directories."
92 :type '(choice (const :tag "off" nil)
94 (const :tag "local" local))
97 (make-variable-buffer-local 'ssh-directory-tracking-mode)
99 (defcustom ssh-host nil
100 "*The name of the remote host. This variable is buffer-local."
101 :type '(choice (const nil) string)
104 (defcustom ssh-remote-user nil
105 "*The username used on the remote host.
106 This variable is buffer-local and defaults to your local user name.
107 If ssh is invoked with the `-l' option to specify the remote username,
108 this variable is set from that."
109 :type '(choice (const nil) string)
112 ;; Initialize ssh mode map.
113 (defvar ssh-mode-map '())
116 (setq ssh-mode-map (if (consp shell-mode-map)
117 (cons 'keymap shell-mode-map)
118 (copy-keymap shell-mode-map)))
119 (define-key ssh-mode-map "\C-c\C-c" 'ssh-send-Ctrl-C)
120 (define-key ssh-mode-map "\C-c\C-d" 'ssh-send-Ctrl-D)
121 (define-key ssh-mode-map "\C-c\C-z" 'ssh-send-Ctrl-Z)
122 (define-key ssh-mode-map "\C-c\C-\\" 'ssh-send-Ctrl-backslash)
123 (define-key ssh-mode-map "\C-d" 'ssh-delchar-or-send-Ctrl-D)
124 (define-key ssh-mode-map "\C-i" 'ssh-tab-or-complete)))
127 ;;;###autoload (add-hook 'same-window-regexps "^\\*ssh-.*\\*\\(\\|<[0-9]+>\\)")
129 (defvar ssh-history nil)
132 (defun ssh (input-args &optional buffer)
133 "Open a network login connection via `ssh' with args INPUT-ARGS.
134 INPUT-ARGS should start with a host name; it may also contain
135 other arguments for `ssh'.
137 Input is sent line-at-a-time to the remote connection.
139 Communication with the remote host is recorded in a buffer `*ssh-HOST*'
140 \(or `*ssh-USER@HOST*' if the remote username differs\).
141 If a prefix argument is given and the buffer `*ssh-HOST*' already exists,
142 a new buffer with a different connection will be made.
144 When called from a program, if the optional second argument BUFFER is
145 a string or buffer, it specifies the buffer to use.
147 The variable `ssh-program' contains the name of the actual program to
148 run. It can be a relative or absolute path.
150 The variable `ssh-explicit-args' is a list of arguments to give to
151 the ssh when starting. They are prepended to any arguments given in
154 If the default value of `ssh-directory-tracking-mode' is t, then the
155 default directory in that buffer is set to a remote (FTP) file name to
156 access your home directory on the remote machine. Occasionally this causes
157 an error, if you cannot access the home directory on that machine. This
158 error is harmless as long as you don't try to use that default directory.
160 If `ssh-directory-tracking-mode' is neither t nor nil, then the default
161 directory is initially set up to your (local) home directory.
162 This is useful if the remote machine and your local machine
163 share the same files via NFS. This is the default.
165 If you wish to change directory tracking styles during a session, use the
166 function `ssh-directory-tracking-mode' rather than simply setting the
169 (read-from-minibuffer "ssh arguments (hostname first): "
170 nil nil nil 'ssh-history)
173 (let* ((process-connection-type ssh-process-connection-type)
174 (args (ssh-parse-words input-args))
176 (user (or (car (cdr (member "-l" args)))
178 (buffer-name (if (string= user (user-login-name))
179 (format "*ssh-%s*" host)
180 (format "*ssh-%s@%s*" user host)))
183 (and ssh-explicit-args
184 (setq args (append ssh-explicit-args args)))
186 (cond ((null buffer))
188 (setq buffer-name buffer))
190 (setq buffer-name (buffer-name buffer)))
192 (setq buffer-name (format "%s<%d>" buffer-name buffer)))
194 (setq buffer-name (generate-new-buffer-name buffer-name))))
196 (setq buffer (get-buffer-create buffer-name))
197 (pop-to-buffer buffer-name)
200 ((comint-check-proc buffer-name))
202 (comint-exec buffer buffer-name ssh-program nil args)
203 (setq proc (get-buffer-process buffer))
204 ;; Set process-mark to point-max in case there is text in the
205 ;; buffer from a previous exited process.
206 (set-marker (process-mark proc) (point-max))
208 ;; comint-output-filter-functions is treated like a hook: it is
209 ;; processed via run-hooks or run-hooks-with-args in later versions
211 ;; comint-output-filter-functions should already have a
212 ;; permanent-local property, at least in emacs 19.27 or later.
214 ((fboundp 'make-local-hook)
215 (make-local-hook 'comint-output-filter-functions)
216 (add-hook 'comint-output-filter-functions 'ssh-carriage-filter nil t))
218 (make-local-variable 'comint-output-filter-functions)
219 (add-hook 'comint-output-filter-functions 'ssh-carriage-filter)))
223 (make-local-variable 'ssh-host)
225 (make-local-variable 'ssh-remote-user)
226 (setq ssh-remote-user user)
229 (cond ((eq ssh-directory-tracking-mode t)
230 ;; Do this here, rather than calling the tracking mode
231 ;; function, to avoid a gratuitous resync check; the default
232 ;; should be the user's home directory, be it local or remote.
233 (setq comint-file-name-prefix
234 (concat "/" ssh-remote-user "@" ssh-host ":"))
235 (cd-absolute comint-file-name-prefix))
236 ((null ssh-directory-tracking-mode))
238 (cd-absolute (concat comint-file-name-prefix "~/"))))
241 (put 'ssh-mode 'mode-class 'special)
244 "Set major-mode for ssh sessions.
245 If `ssh-mode-hook' is set, run it."
247 (kill-all-local-variables)
249 (setq major-mode 'ssh-mode)
250 (setq mode-name "ssh")
251 (use-local-map ssh-mode-map)
252 (setq shell-dirtrackp ssh-directory-tracking-mode)
253 (make-local-variable 'comint-file-name-prefix)
254 (run-hooks 'ssh-mode-hook))
256 (defun ssh-directory-tracking-mode (&optional prefix)
257 "Do remote or local directory tracking, or disable entirely.
259 If called with no prefix argument or a unspecified prefix argument (just
260 ``\\[universal-argument]'' with no number) do remote directory tracking via
261 ange-ftp. If called as a function, give it no argument.
263 If called with a negative prefix argument, disable directory tracking
266 If called with a positive, numeric prefix argument, e.g.
267 ``\\[universal-argument] 1 M-x ssh-directory-tracking-mode\'',
268 then do directory tracking but assume the remote filesystem is the same as
269 the local system. This only works in general if the remote machine and the
270 local one share the same directories (through NFS)."
275 (setq ssh-directory-tracking-mode t)
276 (setq shell-dirtrackp t)
277 (setq comint-file-name-prefix
278 (concat "/" ssh-remote-user "@" ssh-host ":")))
280 (setq ssh-directory-tracking-mode nil)
281 (setq shell-dirtrackp nil))
283 (setq ssh-directory-tracking-mode 'local)
284 (setq comint-file-name-prefix "")
285 (setq shell-dirtrackp t)))
288 (let* ((proc (get-buffer-process (current-buffer)))
289 (proc-mark (process-mark proc))
290 (current-input (buffer-substring proc-mark (point-max)))
292 (offset (and (>= orig-point proc-mark)
293 (- (point-max) orig-point))))
296 (delete-region proc-mark (point-max))
297 (goto-char (point-max))
299 (goto-char proc-mark)
300 (insert current-input)
302 (goto-char (- (point-max) offset))
303 (goto-char orig-point)))))))
306 ;; Parse a line into its constituent parts (words separated by
307 ;; whitespace). Return a list of the words.
308 (defun ssh-parse-words (line)
311 (match-data (match-data)))
312 (while (string-match "[^ \t\n]+" line posn)
313 (setq list (cons (substring line (match-beginning 0) (match-end 0))
315 (setq posn (match-end 0)))
316 (store-match-data (match-data))
319 (defun ssh-carriage-filter (string)
320 (let* ((point-marker (point-marker))
321 (end (process-mark (get-buffer-process (current-buffer))))
322 (beg (or (and (boundp 'comint-last-output-start)
323 comint-last-output-start)
324 (- end (length string)))))
326 (while (search-forward "\C-m" end t)
328 (goto-char point-marker)))
330 (defun ssh-send-Ctrl-C ()
332 (process-send-string nil "\C-c"))
334 (defun ssh-send-Ctrl-D ()
336 (process-send-string nil "\C-d"))
338 (defun ssh-send-Ctrl-Z ()
340 (process-send-string nil "\C-z"))
342 (defun ssh-send-Ctrl-backslash ()
344 (process-send-string nil "\C-\\"))
346 (defun ssh-delchar-or-send-Ctrl-D (arg)
348 Delete ARG characters forward, or send a C-d to process if at end of buffer."
354 (defun ssh-tab-or-complete ()
355 "Complete file name if doing directory tracking, or just insert TAB."
357 (if ssh-directory-tracking-mode
358 (comint-dynamic-complete)