Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / net / rlogin.el
CommitLineData
8749abea
GM
1;;; rlogin.el --- remote login interface
2
f2e3589a 3;; Copyright (C) 1992, 1993, 1994, 1995, 1997, 1998, 2001, 2002, 2003,
2f043267 4;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
8749abea
GM
5
6;; Author: Noah Friedman
7;; Maintainer: Noah Friedman <friedman@splode.com>
8;; Keywords: unix, comm
9
8749abea
GM
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
ceaeecb0 14;; the Free Software Foundation; either version 3, or (at your option)
8749abea
GM
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
3a35cf56
LK
24;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25;; Boston, MA 02110-1301, USA.
8749abea
GM
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
508334bc 45 "Remote login interface."
8749abea
GM
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)))
5199cf98
JB
73 "*If non-nil, use a pty for the local rlogin process.
74If nil, use a pipe (if pipes are supported on the local system).
8749abea
GM
75
76Generally it is better not to waste ptys on systems which have a static
77number of them. On the other hand, some implementations of `rlogin' assume
78a 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
86nil means don't do directory tracking.
87
88t means do so using an ftp remote file name.
89
90Any other value means do directory tracking using local file names.
91This works only if the remote machine and the local one
92share the same directories (through NFS). This is the default.
93
94This variable becomes local to a buffer when set in any fashion for it.
95
96It is better to use the function of the same name to change the behavior of
97directory tracking in an rlogin session once it has begun, rather than
98simply setting this variable, since the function does the necessary
99re-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.
114This variable is buffer-local and defaults to your local user name.
115If rlogin is invoked with the `-l' option to specify the remote username,
116this 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.
142INPUT-ARGS should start with a host name; it may also contain
143other arguments for `rlogin'.
144
145Input is sent line-at-a-time to the remote connection.
146
147Communication with the remote host is recorded in a buffer `*rlogin-HOST*'
148\(or `*rlogin-USER@HOST*' if the remote username differs\).
149If a prefix argument is given and the buffer `*rlogin-HOST*' already exists,
150a new buffer with a different connection will be made.
151
152When called from a program, if the optional second argument BUFFER is
153a string or buffer, it specifies the buffer to use.
154
155The variable `rlogin-program' contains the name of the actual program to
156run. It can be a relative or absolute path.
157
158The variable `rlogin-explicit-args' is a list of arguments to give to
159the rlogin when starting. They are added after any arguments given in
160INPUT-ARGS.
161
162If the default value of `rlogin-directory-tracking-mode' is t, then the
163default directory in that buffer is set to a remote (FTP) file name to
164access your home directory on the remote machine. Occasionally this causes
165an error, if you cannot access the home directory on that machine. This
166error is harmless as long as you don't try to use that default directory.
167
168If `rlogin-directory-tracking-mode' is neither t nor nil, then the default
169directory is initially set up to your (local) home directory.
170This is useful if the remote machine and your local machine
171share the same files via NFS. This is the default.
172
173If you wish to change directory tracking styles during a session, use the
174function `rlogin-directory-tracking-mode' rather than simply setting the
175variable."
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
19a31306 183 (append (split-string input-args)
8749abea 184 rlogin-explicit-args)
19a31306 185 (split-string input-args)))
9a479835
TTN
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)))
8749abea
GM
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)
0fd2d581 196 (format "*rlogin-%s@%s*" user host))))
8749abea
GM
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
0fd2d581 211 (unless (comint-check-proc buffer-name)
8749abea 212 (comint-exec buffer buffer-name rlogin-program nil args)
8749abea
GM
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 "~/"))))
0fd2d581 232 (error nil)))))
8749abea
GM
233
234(put 'rlogin-mode 'mode-class 'special)
235
cf232e4d 236(define-derived-mode rlogin-mode shell-mode "Rlogin"
8749abea 237 (setq shell-dirtrackp rlogin-directory-tracking-mode)
cf232e4d 238 (make-local-variable 'comint-file-name-prefix))
8749abea
GM
239
240(defun rlogin-directory-tracking-mode (&optional prefix)
241 "Do remote or local directory tracking, or disable entirely.
242
243If called with no prefix argument or a unspecified prefix argument (just
244``\\[universal-argument]'' with no number) do remote directory tracking via
245ange-ftp. If called as a function, give it no argument.
246
247If called with a negative prefix argument, disable directory tracking
248entirely.
249
250If called with a positive, numeric prefix argument, e.g.
251``\\[universal-argument] 1 M-x rlogin-directory-tracking-mode\'',
252then do directory tracking but assume the remote filesystem is the same as
253the local system. This only works in general if the remote machine and the
254local 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
8749abea
GM
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 "\
308Delete 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
cbee283d 323;; arch-tag: 6e20eabf-feda-40fa-ab40-0d156db447e4
8749abea 324;;; rlogin.el ends here