153
|
1 ;;; ssh.el --- remote login interface
|
|
2
|
|
3 ;; Copyright (C) 1996, 1997 Noah S. Friedman
|
|
4
|
|
5 ;; Author: Noah Friedman <friedman@prep.ai.mit.edu>
|
|
6 ;; Maintainer: friedman@prep.ai.mit.edu
|
|
7 ;; Keywords: unix, comm
|
|
8 ;; Created: 1996-07-03
|
|
9
|
|
10 ;; $Id: ssh.el,v 1.1 1997/05/28 16:30:50 steve Exp $
|
|
11
|
|
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)
|
|
15 ;; any later version.
|
|
16 ;;
|
|
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.
|
|
21 ;;
|
|
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.
|
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
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.
|
|
33
|
|
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'.
|
|
38
|
|
39 ;;; Code:
|
|
40
|
|
41 (require 'comint)
|
|
42 (require 'shell)
|
|
43
|
|
44 (defgroup ssh nil
|
|
45 "Secure remote login interface"
|
|
46 :group 'processes
|
|
47 :group 'unix)
|
|
48
|
|
49 (defcustom ssh-program "ssh"
|
|
50 "*Name of program to invoke ssh"
|
|
51 :type 'string
|
|
52 :group 'ssh)
|
|
53
|
|
54 (defcustom ssh-explicit-args '()
|
|
55 "*List of arguments to pass to ssh on the command line."
|
|
56 :type '(repeat (string :tag "Argument"))
|
|
57 :group 'ssh)
|
|
58
|
|
59 (defcustom ssh-mode-hook nil
|
|
60 "*Hooks to run after setting current buffer to ssh-mode."
|
|
61 :type 'hook
|
|
62 :group 'ssh)
|
|
63
|
|
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).
|
|
67
|
|
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))
|
|
73 :group 'ssh)
|
|
74
|
|
75 (defcustom ssh-directory-tracking-mode 'local
|
|
76 "*Control whether and how to do directory tracking in an ssh buffer.
|
|
77
|
|
78 nil means don't do directory tracking.
|
|
79
|
|
80 t means do so using an ftp remote file name.
|
|
81
|
|
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.
|
|
85
|
|
86 This variable becomes local to a buffer when set in any fashion for it.
|
|
87
|
|
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)
|
|
93 (const :tag "ftp" t)
|
|
94 (const :tag "local" local))
|
|
95 :group 'ssh)
|
|
96
|
|
97 (make-variable-buffer-local 'ssh-directory-tracking-mode)
|
|
98
|
|
99 (defcustom ssh-host nil
|
|
100 "*The name of the remote host. This variable is buffer-local."
|
|
101 :type '(choice (const nil) string)
|
|
102 :group 'ssh)
|
|
103
|
|
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)
|
|
110 :group 'ssh)
|
|
111
|
|
112 ;; Initialize ssh mode map.
|
|
113 (defvar ssh-mode-map '())
|
|
114 (cond
|
|
115 ((null 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)))
|
|
125
|
|
126
|
|
127 ;;;###autoload (add-hook 'same-window-regexps "^\\*ssh-.*\\*\\(\\|<[0-9]+>\\)")
|
|
128
|
|
129 (defvar ssh-history nil)
|
|
130
|
|
131 ;;;###autoload
|
|
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'.
|
|
136
|
|
137 Input is sent line-at-a-time to the remote connection.
|
|
138
|
|
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.
|
|
143
|
|
144 When called from a program, if the optional second argument BUFFER is
|
|
145 a string or buffer, it specifies the buffer to use.
|
|
146
|
|
147 The variable `ssh-program' contains the name of the actual program to
|
|
148 run. It can be a relative or absolute path.
|
|
149
|
|
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
|
|
152 INPUT-ARGS.
|
|
153
|
|
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.
|
|
159
|
|
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.
|
|
164
|
|
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
|
|
167 variable."
|
|
168 (interactive (list
|
|
169 (read-from-minibuffer "ssh arguments (hostname first): "
|
|
170 nil nil nil 'ssh-history)
|
|
171 current-prefix-arg))
|
|
172
|
|
173 (let* ((process-connection-type ssh-process-connection-type)
|
|
174 (args (ssh-parse-words input-args))
|
|
175 (host (car args))
|
|
176 (user (or (car (cdr (member "-l" args)))
|
|
177 (user-login-name)))
|
|
178 (buffer-name (if (string= user (user-login-name))
|
|
179 (format "*ssh-%s*" host)
|
|
180 (format "*ssh-%s@%s*" user host)))
|
|
181 proc)
|
|
182
|
|
183 (and ssh-explicit-args
|
|
184 (setq args (append ssh-explicit-args args)))
|
|
185
|
|
186 (cond ((null buffer))
|
|
187 ((stringp buffer)
|
|
188 (setq buffer-name buffer))
|
|
189 ((bufferp buffer)
|
|
190 (setq buffer-name (buffer-name buffer)))
|
|
191 ((numberp buffer)
|
|
192 (setq buffer-name (format "%s<%d>" buffer-name buffer)))
|
|
193 (t
|
|
194 (setq buffer-name (generate-new-buffer-name buffer-name))))
|
|
195
|
|
196 (setq buffer (get-buffer-create buffer-name))
|
|
197 (pop-to-buffer buffer-name)
|
|
198
|
|
199 (cond
|
|
200 ((comint-check-proc buffer-name))
|
|
201 (t
|
|
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))
|
|
207
|
|
208 ;; comint-output-filter-functions is just like a hook, except that the
|
|
209 ;; functions in that list are passed arguments. add-hook serves well
|
|
210 ;; enough for modifying it.
|
|
211 ;; comint-output-filter-functions should already have a
|
|
212 ;; permanent-local property, at least in emacs 19.27 or later.
|
|
213 (if (fboundp 'make-local-hook)
|
|
214 (make-local-hook 'comint-output-filter-functions)
|
|
215 (make-local-variable 'comint-output-filter-functions))
|
|
216 (add-hook 'comint-output-filter-functions 'ssh-carriage-filter)
|
|
217
|
|
218 (ssh-mode)
|
|
219
|
|
220 (make-local-variable 'ssh-host)
|
|
221 (setq ssh-host host)
|
|
222 (make-local-variable 'ssh-remote-user)
|
|
223 (setq ssh-remote-user user)
|
|
224
|
|
225 (condition-case ()
|
|
226 (cond ((eq ssh-directory-tracking-mode t)
|
|
227 ;; Do this here, rather than calling the tracking mode
|
|
228 ;; function, to avoid a gratuitous resync check; the default
|
|
229 ;; should be the user's home directory, be it local or remote.
|
|
230 (setq comint-file-name-prefix
|
|
231 (concat "/" ssh-remote-user "@" ssh-host ":"))
|
|
232 (cd-absolute comint-file-name-prefix))
|
|
233 ((null ssh-directory-tracking-mode))
|
|
234 (t
|
|
235 (cd-absolute (concat comint-file-name-prefix "~/"))))
|
|
236 (error nil))))))
|
|
237
|
|
238 (put 'ssh-mode 'mode-class 'special)
|
|
239
|
|
240 (defun ssh-mode ()
|
|
241 "Set major-mode for ssh sessions.
|
|
242 If `ssh-mode-hook' is set, run it."
|
|
243 (interactive)
|
|
244 (kill-all-local-variables)
|
|
245 (shell-mode)
|
|
246 (setq major-mode 'ssh-mode)
|
|
247 (setq mode-name "ssh")
|
|
248 (use-local-map ssh-mode-map)
|
|
249 (setq shell-dirtrackp ssh-directory-tracking-mode)
|
|
250 (make-local-variable 'comint-file-name-prefix)
|
|
251 (run-hooks 'ssh-mode-hook))
|
|
252
|
|
253 (defun ssh-directory-tracking-mode (&optional prefix)
|
|
254 "Do remote or local directory tracking, or disable entirely.
|
|
255
|
|
256 If called with no prefix argument or a unspecified prefix argument (just
|
|
257 ``\\[universal-argument]'' with no number) do remote directory tracking via
|
|
258 ange-ftp. If called as a function, give it no argument.
|
|
259
|
|
260 If called with a negative prefix argument, disable directory tracking
|
|
261 entirely.
|
|
262
|
|
263 If called with a positive, numeric prefix argument, e.g.
|
|
264 ``\\[universal-argument] 1 M-x ssh-directory-tracking-mode\'',
|
|
265 then do directory tracking but assume the remote filesystem is the same as
|
|
266 the local system. This only works in general if the remote machine and the
|
|
267 local one share the same directories (through NFS)."
|
|
268 (interactive "P")
|
|
269 (cond
|
|
270 ((or (null prefix)
|
|
271 (consp prefix))
|
|
272 (setq ssh-directory-tracking-mode t)
|
|
273 (setq shell-dirtrackp t)
|
|
274 (setq comint-file-name-prefix
|
|
275 (concat "/" ssh-remote-user "@" ssh-host ":")))
|
|
276 ((< prefix 0)
|
|
277 (setq ssh-directory-tracking-mode nil)
|
|
278 (setq shell-dirtrackp nil))
|
|
279 (t
|
|
280 (setq ssh-directory-tracking-mode 'local)
|
|
281 (setq comint-file-name-prefix "")
|
|
282 (setq shell-dirtrackp t)))
|
|
283 (cond
|
|
284 (shell-dirtrackp
|
|
285 (let* ((proc (get-buffer-process (current-buffer)))
|
|
286 (proc-mark (process-mark proc))
|
|
287 (current-input (buffer-substring proc-mark (point-max)))
|
|
288 (orig-point (point))
|
|
289 (offset (and (>= orig-point proc-mark)
|
|
290 (- (point-max) orig-point))))
|
|
291 (unwind-protect
|
|
292 (progn
|
|
293 (delete-region proc-mark (point-max))
|
|
294 (goto-char (point-max))
|
|
295 (shell-resync-dirs))
|
|
296 (goto-char proc-mark)
|
|
297 (insert current-input)
|
|
298 (if offset
|
|
299 (goto-char (- (point-max) offset))
|
|
300 (goto-char orig-point)))))))
|
|
301
|
|
302
|
|
303 ;; Parse a line into its constituent parts (words separated by
|
|
304 ;; whitespace). Return a list of the words.
|
|
305 (defun ssh-parse-words (line)
|
|
306 (let ((list nil)
|
|
307 (posn 0)
|
|
308 (match-data (match-data)))
|
|
309 (while (string-match "[^ \t\n]+" line posn)
|
|
310 (setq list (cons (substring line (match-beginning 0) (match-end 0))
|
|
311 list))
|
|
312 (setq posn (match-end 0)))
|
|
313 (store-match-data (match-data))
|
|
314 (nreverse list)))
|
|
315
|
|
316 (defun ssh-carriage-filter (string)
|
|
317 (let* ((point-marker (point-marker))
|
|
318 (end (process-mark (get-buffer-process (current-buffer))))
|
|
319 (beg (or (and (boundp 'comint-last-output-start)
|
|
320 comint-last-output-start)
|
|
321 (- end (length string)))))
|
|
322 (goto-char beg)
|
|
323 (while (search-forward "\C-m" end t)
|
|
324 (delete-char -1))
|
|
325 (goto-char point-marker)))
|
|
326
|
|
327 (defun ssh-send-Ctrl-C ()
|
|
328 (interactive)
|
|
329 (send-string nil "\C-c"))
|
|
330
|
|
331 (defun ssh-send-Ctrl-D ()
|
|
332 (interactive)
|
|
333 (send-string nil "\C-d"))
|
|
334
|
|
335 (defun ssh-send-Ctrl-Z ()
|
|
336 (interactive)
|
|
337 (send-string nil "\C-z"))
|
|
338
|
|
339 (defun ssh-send-Ctrl-backslash ()
|
|
340 (interactive)
|
|
341 (send-string nil "\C-\\"))
|
|
342
|
|
343 (defun ssh-delchar-or-send-Ctrl-D (arg)
|
|
344 "\
|
|
345 Delete ARG characters forward, or send a C-d to process if at end of buffer."
|
|
346 (interactive "p")
|
|
347 (if (eobp)
|
|
348 (ssh-send-Ctrl-D)
|
|
349 (delete-char arg)))
|
|
350
|
|
351 (defun ssh-tab-or-complete ()
|
|
352 "Complete file name if doing directory tracking, or just insert TAB."
|
|
353 (interactive)
|
|
354 (if ssh-directory-tracking-mode
|
|
355 (comint-dynamic-complete)
|
|
356 (insert "\C-i")))
|
|
357
|
|
358 ;;; ssh.el ends here
|