Initial Commit
[packages] / xemacs-packages / os-utils / ssh.el
1 ;;; ssh.el --- remote login interface
2
3 ;; Copyright (C) 1996, 1997, 1998 Noah S. Friedman
4
5 ;; Author: Noah Friedman <friedman@splode.com>
6 ;; Maintainer: friedman@splode.com
7 ;; Keywords: unix, comm
8 ;; Created: 1996-07-03
9
10 ;; $Id: ssh.el,v 1.6 1998/09/11 01:14:34 friedman 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 \f
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 treated like a hook: it is
209       ;; processed via run-hooks or run-hooks-with-args in later versions
210       ;; of emacs.
211       ;; comint-output-filter-functions should already have a
212       ;; permanent-local property, at least in emacs 19.27 or later.
213       (cond
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))
217        (t
218         (make-local-variable 'comint-output-filter-functions)
219         (add-hook 'comint-output-filter-functions 'ssh-carriage-filter)))
220
221       (ssh-mode)
222
223       (make-local-variable 'ssh-host)
224       (setq ssh-host host)
225       (make-local-variable 'ssh-remote-user)
226       (setq ssh-remote-user user)
227
228       (condition-case ()
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))
237                 (t
238                  (cd-absolute (concat comint-file-name-prefix "~/"))))
239         (error nil))))))
240
241 (put 'ssh-mode 'mode-class 'special)
242
243 (defun ssh-mode ()
244   "Set major-mode for ssh sessions.
245 If `ssh-mode-hook' is set, run it."
246   (interactive)
247   (kill-all-local-variables)
248   (shell-mode)
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))
255
256 (defun ssh-directory-tracking-mode (&optional prefix)
257   "Do remote or local directory tracking, or disable entirely.
258
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.
262
263 If called with a negative prefix argument, disable directory tracking
264 entirely.
265
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)."
271   (interactive "P")
272   (cond
273    ((or (null prefix)
274         (consp prefix))
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 ":")))
279    ((< prefix 0)
280     (setq ssh-directory-tracking-mode nil)
281     (setq shell-dirtrackp nil))
282    (t
283     (setq ssh-directory-tracking-mode 'local)
284     (setq comint-file-name-prefix "")
285     (setq shell-dirtrackp t)))
286   (cond
287    (shell-dirtrackp
288     (let* ((proc (get-buffer-process (current-buffer)))
289            (proc-mark (process-mark proc))
290            (current-input (buffer-substring proc-mark (point-max)))
291            (orig-point (point))
292            (offset (and (>= orig-point proc-mark)
293                         (- (point-max) orig-point))))
294       (unwind-protect
295           (progn
296             (delete-region proc-mark (point-max))
297             (goto-char (point-max))
298             (shell-resync-dirs))
299         (goto-char proc-mark)
300         (insert current-input)
301         (if offset
302             (goto-char (- (point-max) offset))
303           (goto-char orig-point)))))))
304
305 \f
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)
309   (let ((list nil)
310         (posn 0)
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))
314                        list))
315       (setq posn (match-end 0)))
316     (store-match-data (match-data))
317     (nreverse list)))
318
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)))))
325     (goto-char beg)
326     (while (search-forward "\C-m" end t)
327       (delete-char -1))
328     (goto-char point-marker)))
329
330 (defun ssh-send-Ctrl-C ()
331   (interactive)
332   (process-send-string nil "\C-c"))
333
334 (defun ssh-send-Ctrl-D ()
335   (interactive)
336   (process-send-string nil "\C-d"))
337
338 (defun ssh-send-Ctrl-Z ()
339   (interactive)
340   (process-send-string nil "\C-z"))
341
342 (defun ssh-send-Ctrl-backslash ()
343   (interactive)
344   (process-send-string nil "\C-\\"))
345
346 (defun ssh-delchar-or-send-Ctrl-D (arg)
347   "\
348 Delete ARG characters forward, or send a C-d to process if at end of buffer."
349   (interactive "p")
350   (if (eobp)
351       (ssh-send-Ctrl-D)
352     (delete-char arg)))
353
354 (defun ssh-tab-or-complete ()
355   "Complete file name if doing directory tracking, or just insert TAB."
356   (interactive)
357   (if ssh-directory-tracking-mode
358       (comint-dynamic-complete)
359     (insert "\C-i")))
360
361 (provide 'ssh)
362
363 ;;; ssh.el ends here