Merge from emacs--rel--22
[bpt/emacs.git] / lisp / net / rlogin.el
1 ;;; rlogin.el --- remote login interface
2
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1997, 1998, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
5
6 ;; Author: Noah Friedman
7 ;; Maintainer: Noah Friedman <friedman@splode.com>
8 ;; Keywords: unix, comm
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs 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 ;; GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
26
27 ;;; Commentary:
28
29 ;; Support for remote logins using `rlogin'.
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 rlogin 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 rlogin nil
45 "Remote login interface."
46 :group 'processes
47 :group 'unix)
48
49 (defcustom rlogin-program "rlogin"
50 "*Name of program to invoke rlogin"
51 :type 'string
52 :group 'rlogin)
53
54 (defcustom rlogin-explicit-args nil
55 "*List of arguments to pass to rlogin on the command line."
56 :type '(repeat (string :tag "Argument"))
57 :group 'rlogin)
58
59 (defcustom rlogin-mode-hook nil
60 "*Hooks to run after setting current buffer to rlogin-mode."
61 :type 'hook
62 :group 'rlogin)
63
64 (defcustom rlogin-process-connection-type
65 (save-match-data
66 ;; Solaris 2.x `rlogin' will spew a bunch of ioctl error messages if
67 ;; stdin isn't a tty.
68 (cond ((and (boundp 'system-configuration)
69 (stringp system-configuration)
70 (string-match "-solaris2" system-configuration))
71 t)
72 (t nil)))
73 "*If non-nil, use a pty for the local rlogin process.
74 If nil, use a pipe (if pipes are supported on the local system).
75
76 Generally it is better not to waste ptys on systems which have a static
77 number of them. On the other hand, some implementations of `rlogin' assume
78 a pty is being used, and errors will result from using a pipe instead."
79 :type '(choice (const :tag "pipes" nil)
80 (other :tag "ptys" t))
81 :group 'rlogin)
82
83 (defcustom rlogin-directory-tracking-mode 'local
84 "*Control whether and how to do directory tracking in an rlogin buffer.
85
86 nil means don't do directory tracking.
87
88 t means do so using an ftp remote file name.
89
90 Any other value means do directory tracking using local file names.
91 This works only if the remote machine and the local one
92 share the same directories (through NFS). This is the default.
93
94 This variable becomes local to a buffer when set in any fashion for it.
95
96 It is better to use the function of the same name to change the behavior of
97 directory tracking in an rlogin session once it has begun, rather than
98 simply setting this variable, since the function does the necessary
99 re-synching of directories."
100 :type '(choice (const :tag "off" nil)
101 (const :tag "ftp" t)
102 (other :tag "local" local))
103 :group 'rlogin)
104
105 (make-variable-buffer-local 'rlogin-directory-tracking-mode)
106
107 (defcustom rlogin-host nil
108 "*The name of the remote host. This variable is buffer-local."
109 :type '(choice (const nil) string)
110 :group 'rlogin)
111
112 (defcustom rlogin-remote-user nil
113 "*The username used on the remote host.
114 This variable is buffer-local and defaults to your local user name.
115 If rlogin is invoked with the `-l' option to specify the remote username,
116 this variable is set from that."
117 :type '(choice (const nil) string)
118 :group 'rlogin)
119
120 ;; Initialize rlogin mode map.
121 (defvar rlogin-mode-map '())
122 (cond
123 ((null rlogin-mode-map)
124 (setq rlogin-mode-map (if (consp shell-mode-map)
125 (cons 'keymap shell-mode-map)
126 (copy-keymap shell-mode-map)))
127 (define-key rlogin-mode-map "\C-c\C-c" 'rlogin-send-Ctrl-C)
128 (define-key rlogin-mode-map "\C-c\C-d" 'rlogin-send-Ctrl-D)
129 (define-key rlogin-mode-map "\C-c\C-z" 'rlogin-send-Ctrl-Z)
130 (define-key rlogin-mode-map "\C-c\C-\\" 'rlogin-send-Ctrl-backslash)
131 (define-key rlogin-mode-map "\C-d" 'rlogin-delchar-or-send-Ctrl-D)
132 (define-key rlogin-mode-map "\C-i" 'rlogin-tab-or-complete)))
133
134 \f
135 ;;;###autoload (add-hook 'same-window-regexps "^\\*rlogin-.*\\*\\(\\|<[0-9]+>\\)")
136
137 (defvar rlogin-history nil)
138
139 ;;;###autoload
140 (defun rlogin (input-args &optional buffer)
141 "Open a network login connection via `rlogin' with args INPUT-ARGS.
142 INPUT-ARGS should start with a host name; it may also contain
143 other arguments for `rlogin'.
144
145 Input is sent line-at-a-time to the remote connection.
146
147 Communication with the remote host is recorded in a buffer `*rlogin-HOST*'
148 \(or `*rlogin-USER@HOST*' if the remote username differs\).
149 If a prefix argument is given and the buffer `*rlogin-HOST*' already exists,
150 a new buffer with a different connection will be made.
151
152 When called from a program, if the optional second argument BUFFER is
153 a string or buffer, it specifies the buffer to use.
154
155 The variable `rlogin-program' contains the name of the actual program to
156 run. It can be a relative or absolute path.
157
158 The variable `rlogin-explicit-args' is a list of arguments to give to
159 the rlogin when starting. They are added after any arguments given in
160 INPUT-ARGS.
161
162 If the default value of `rlogin-directory-tracking-mode' is t, then the
163 default directory in that buffer is set to a remote (FTP) file name to
164 access your home directory on the remote machine. Occasionally this causes
165 an error, if you cannot access the home directory on that machine. This
166 error is harmless as long as you don't try to use that default directory.
167
168 If `rlogin-directory-tracking-mode' is neither t nor nil, then the default
169 directory is initially set up to your (local) home directory.
170 This is useful if the remote machine and your local machine
171 share the same files via NFS. This is the default.
172
173 If you wish to change directory tracking styles during a session, use the
174 function `rlogin-directory-tracking-mode' rather than simply setting the
175 variable."
176 (interactive (list
177 (read-from-minibuffer "rlogin arguments (hostname first): "
178 nil nil nil 'rlogin-history)
179 current-prefix-arg))
180
181 (let* ((process-connection-type rlogin-process-connection-type)
182 (args (if rlogin-explicit-args
183 (append (split-string input-args)
184 rlogin-explicit-args)
185 (split-string input-args)))
186 (host (let ((tail args))
187 ;; Find first arg that doesn't look like an option.
188 ;; This still loses for args that take values, feh.
189 (while (and tail (= ?- (aref (car tail) 0)))
190 (setq tail (cdr tail)))
191 (car tail)))
192 (user (or (car (cdr (member "-l" args)))
193 (user-login-name)))
194 (buffer-name (if (string= user (user-login-name))
195 (format "*rlogin-%s*" host)
196 (format "*rlogin-%s@%s*" user host))))
197
198 (cond ((null buffer))
199 ((stringp buffer)
200 (setq buffer-name buffer))
201 ((bufferp buffer)
202 (setq buffer-name (buffer-name buffer)))
203 ((numberp buffer)
204 (setq buffer-name (format "%s<%d>" buffer-name buffer)))
205 (t
206 (setq buffer-name (generate-new-buffer-name buffer-name))))
207
208 (setq buffer (get-buffer-create buffer-name))
209 (pop-to-buffer buffer-name)
210
211 (unless (comint-check-proc buffer-name)
212 (comint-exec buffer buffer-name rlogin-program nil args)
213
214 (rlogin-mode)
215
216 (make-local-variable 'rlogin-host)
217 (setq rlogin-host host)
218 (make-local-variable 'rlogin-remote-user)
219 (setq rlogin-remote-user user)
220
221 (condition-case ()
222 (cond ((eq rlogin-directory-tracking-mode t)
223 ;; Do this here, rather than calling the tracking mode
224 ;; function, to avoid a gratuitous resync check; the default
225 ;; should be the user's home directory, be it local or remote.
226 (setq comint-file-name-prefix
227 (concat "/" rlogin-remote-user "@" rlogin-host ":"))
228 (cd-absolute comint-file-name-prefix))
229 ((null rlogin-directory-tracking-mode))
230 (t
231 (cd-absolute (concat comint-file-name-prefix "~/"))))
232 (error nil)))))
233
234 (put 'rlogin-mode 'mode-class 'special)
235
236 (define-derived-mode rlogin-mode shell-mode "Rlogin"
237 (setq shell-dirtrackp rlogin-directory-tracking-mode)
238 (make-local-variable 'comint-file-name-prefix))
239
240 (defun rlogin-directory-tracking-mode (&optional prefix)
241 "Do remote or local directory tracking, or disable entirely.
242
243 If called with no prefix argument or a unspecified prefix argument (just
244 ``\\[universal-argument]'' with no number) do remote directory tracking via
245 ange-ftp. If called as a function, give it no argument.
246
247 If called with a negative prefix argument, disable directory tracking
248 entirely.
249
250 If called with a positive, numeric prefix argument, e.g.
251 ``\\[universal-argument] 1 M-x rlogin-directory-tracking-mode\'',
252 then do directory tracking but assume the remote filesystem is the same as
253 the local system. This only works in general if the remote machine and the
254 local one share the same directories (through NFS)."
255 (interactive "P")
256 (cond
257 ((or (null prefix)
258 (consp prefix))
259 (setq rlogin-directory-tracking-mode t)
260 (setq shell-dirtrackp t)
261 (setq comint-file-name-prefix
262 (concat "/" rlogin-remote-user "@" rlogin-host ":")))
263 ((< prefix 0)
264 (setq rlogin-directory-tracking-mode nil)
265 (setq shell-dirtrackp nil))
266 (t
267 (setq rlogin-directory-tracking-mode 'local)
268 (setq comint-file-name-prefix "")
269 (setq shell-dirtrackp t)))
270 (cond
271 (shell-dirtrackp
272 (let* ((proc (get-buffer-process (current-buffer)))
273 (proc-mark (process-mark proc))
274 (current-input (buffer-substring proc-mark (point-max)))
275 (orig-point (point))
276 (offset (and (>= orig-point proc-mark)
277 (- (point-max) orig-point))))
278 (unwind-protect
279 (progn
280 (delete-region proc-mark (point-max))
281 (goto-char (point-max))
282 (shell-resync-dirs))
283 (goto-char proc-mark)
284 (insert current-input)
285 (if offset
286 (goto-char (- (point-max) offset))
287 (goto-char orig-point)))))))
288
289 \f
290 (defun rlogin-send-Ctrl-C ()
291 (interactive)
292 (process-send-string nil "\C-c"))
293
294 (defun rlogin-send-Ctrl-D ()
295 (interactive)
296 (process-send-string nil "\C-d"))
297
298 (defun rlogin-send-Ctrl-Z ()
299 (interactive)
300 (process-send-string nil "\C-z"))
301
302 (defun rlogin-send-Ctrl-backslash ()
303 (interactive)
304 (process-send-string nil "\C-\\"))
305
306 (defun rlogin-delchar-or-send-Ctrl-D (arg)
307 "\
308 Delete ARG characters forward, or send a C-d to process if at end of buffer."
309 (interactive "p")
310 (if (eobp)
311 (rlogin-send-Ctrl-D)
312 (delete-char arg)))
313
314 (defun rlogin-tab-or-complete ()
315 "Complete file name if doing directory tracking, or just insert TAB."
316 (interactive)
317 (if rlogin-directory-tracking-mode
318 (comint-dynamic-complete)
319 (insert "\C-i")))
320
321 (provide 'rlogin)
322
323 ;;; arch-tag: 6e20eabf-feda-40fa-ab40-0d156db447e4
324 ;;; rlogin.el ends here