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