Update FSF's address.
[bpt/emacs.git] / lisp / rlogin.el
CommitLineData
76550a57
ER
1;;; rlogin.el --- remote login interface
2
b578f267
EN
3;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
4
8ae3bc9f 5;; Author: Noah Friedman
76550a57 6;; Maintainer: Noah Friedman <friedman@prep.ai.mit.edu>
5d1dd3c0 7;; Keywords: unix, comm
76550a57 8
b578f267
EN
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
d9ecc911
ER
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
b578f267
EN
15
16;; GNU Emacs is distributed in the hope that it will be useful,
d9ecc911
ER
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
b578f267 20
d9ecc911 21;; You should have received a copy of the GNU General Public License
b578f267
EN
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
d9ecc911 25
b578f267 26;; $Id: rlogin.el,v 1.30 1995/10/27 22:34:50 friedman Exp erik $
32992017 27
d9ecc911
ER
28;;; Commentary:
29
0b899bd2 30;; Support for remote logins using `rlogin'.
32992017
NF
31;; This program is layered on top of shell.el; the code here only accounts
32;; for the variations needed to handle a remote process, e.g. directory
33;; tracking and the sending of some special characters.
97b83d9d 34
d933a756
NF
35;; If you wish for rlogin mode to prompt you in the minibuffer for
36;; passwords when a password prompt appears, just enter m-x send-invisible
37;; and type in your line, or add `comint-watch-for-password-prompt' to
38;; `comint-output-filter-functions'.
c7986c18 39
76550a57
ER
40;;; Code:
41
c7986c18 42(require 'comint)
10939dc6 43(require 'shell)
c7986c18
ER
44
45(defvar rlogin-program "rlogin"
46 "*Name of program to invoke rlogin")
47
8d30fe17
NF
48(defvar rlogin-explicit-args nil
49 "*List of arguments to pass to rlogin on the command line.")
50
c7986c18
ER
51(defvar rlogin-mode-hook nil
52 "*Hooks to run after setting current buffer to rlogin-mode.")
53
8ae3bc9f 54(defvar rlogin-process-connection-type nil
2601d12e
NF
55 "*If non-`nil', use a pty for the local rlogin process.
56If `nil', use a pipe (if pipes are supported on the local system).
8d30fe17 57
8ae3bc9f
NF
58Generally it is better not to waste ptys on systems which have a static
59number of them. On the other hand, some implementations of `rlogin' assume
60a pty is being used, and errors will result from using a pipe instead.")
8d30fe17 61
32992017 62(defvar rlogin-directory-tracking-mode 'local
ce992678 63 "*Control whether and how to do directory tracking in an rlogin buffer.
32992017 64
ce992678 65nil means don't do directory tracking.
32992017 66
ce992678 67t means do so using an ftp remote file name.
32992017 68
ce992678
RS
69Any other value means do directory tracking using local file names.
70This works only if the remote machine and the local one
32992017
NF
71share the same directories (through NFS). This is the default.
72
73This variable becomes local to a buffer when set in any fashion for it.
74
75It is better to use the function of the same name to change the behavior of
76directory tracking in an rlogin session once it has begun, rather than
77simply setting this variable, since the function does the necessary
78re-synching of directories.")
79
80(make-variable-buffer-local 'rlogin-directory-tracking-mode)
81
82(defvar rlogin-host nil
83 "*The name of the remote host. This variable is buffer-local.")
84
85(defvar rlogin-remote-user nil
86 "*The username used on the remote host.
87This variable is buffer-local and defaults to your local user name.
88If rlogin is invoked with the `-l' option to specify the remote username,
89this variable is set from that.")
97b83d9d 90
c7986c18
ER
91;; Initialize rlogin mode map.
92(defvar rlogin-mode-map '())
2601d12e 93(cond
32992017
NF
94 ((null rlogin-mode-map)
95 (setq rlogin-mode-map (if (consp shell-mode-map)
96 (cons 'keymap shell-mode-map)
97 (copy-keymap shell-mode-map)))
98 (define-key rlogin-mode-map "\C-c\C-c" 'rlogin-send-Ctrl-C)
99 (define-key rlogin-mode-map "\C-c\C-d" 'rlogin-send-Ctrl-D)
100 (define-key rlogin-mode-map "\C-c\C-z" 'rlogin-send-Ctrl-Z)
101 (define-key rlogin-mode-map "\C-c\C-\\" 'rlogin-send-Ctrl-backslash)
102 (define-key rlogin-mode-map "\C-d" 'rlogin-delchar-or-send-Ctrl-D)
103 (define-key rlogin-mode-map "\C-i" 'rlogin-tab-or-complete)))
c7986c18 104
32992017 105\f
67398f34
RS
106;;;###autoload (add-hook 'same-window-regexps "^\\*rlogin-.*\\*\\(\\|<[0-9]+>\\)")
107
cd17ae3f
RM
108(defvar rlogin-history nil)
109
0b899bd2 110;;;###autoload
965f7ac9 111(defun rlogin (input-args &optional buffer)
8d30fe17
NF
112 "Open a network login connection to HOST via the `rlogin' program.
113Input is sent line-at-a-time to the remote connection.
114
67398f34
RS
115Communication with the remote host is recorded in a buffer `*rlogin-HOST*'
116\(or `*rlogin-USER@HOST*' if the remote username differs\).
117If a prefix argument is given and the buffer `*rlogin-HOST*' already exists,
32992017 118a new buffer with a different connection will be made.
8d30fe17 119
e48e5617 120When called from a program, if the optional second argument is a string or
965f7ac9
RM
121buffer, it names the buffer to use.
122
8d30fe17 123The variable `rlogin-program' contains the name of the actual program to
2601d12e 124run. It can be a relative or absolute path.
8d30fe17
NF
125
126The variable `rlogin-explicit-args' is a list of arguments to give to
ce992678
RS
127the rlogin when starting. They are added after any arguments given in
128INPUT-ARGS.
b79164c7 129
32992017
NF
130If the default value of `rlogin-directory-tracking-mode' is t, then the
131default directory in that buffer is set to a remote (FTP) file name to
132access your home directory on the remote machine. Occasionally this causes
133an error, if you cannot access the home directory on that machine. This
134error is harmless as long as you don't try to use that default directory.
ce992678 135
32992017 136If `rlogin-directory-tracking-mode' is neither t nor nil, then the default
ce992678
RS
137directory is initially set up to your (local) home directory.
138This is useful if the remote machine and your local machine
32992017
NF
139share the same files via NFS. This is the default.
140
141If you wish to change directory tracking styles during a session, use the
142function `rlogin-directory-tracking-mode' rather than simply setting the
143variable."
5236f937 144 (interactive (list
cd17ae3f
RM
145 (read-from-minibuffer "rlogin arguments (hostname first): "
146 nil nil nil 'rlogin-history)
5236f937 147 current-prefix-arg))
32992017 148
8ae3bc9f 149 (let* ((process-connection-type rlogin-process-connection-type)
32992017
NF
150 (args (if rlogin-explicit-args
151 (append (rlogin-parse-words input-args)
152 rlogin-explicit-args)
153 (rlogin-parse-words input-args)))
154 (host (car args))
155 (user (or (car (cdr (member "-l" args)))
156 (user-login-name)))
157 (buffer-name (if (string= user (user-login-name))
158 (format "*rlogin-%s*" host)
159 (format "*rlogin-%s@%s*" user host)))
160 proc)
161
965f7ac9 162 (cond ((null buffer))
e48e5617 163 ((stringp buffer)
965f7ac9 164 (setq buffer-name buffer))
e48e5617
NF
165 ((bufferp buffer)
166 (setq buffer-name (buffer-name buffer)))
965f7ac9
RM
167 ((numberp buffer)
168 (setq buffer-name (format "%s<%d>" buffer-name buffer)))
e1f06ce8
NF
169 (t
170 (setq buffer-name (generate-new-buffer-name buffer-name))))
171
e48e5617 172 (setq buffer (get-buffer-create buffer-name))
e1f06ce8 173 (pop-to-buffer buffer-name)
e48e5617 174
32992017 175 (cond
e1f06ce8 176 ((comint-check-proc buffer-name))
32992017 177 (t
e48e5617
NF
178 (comint-exec buffer buffer-name rlogin-program nil args)
179 (setq proc (get-buffer-process buffer))
32992017
NF
180 ;; Set process-mark to point-max in case there is text in the
181 ;; buffer from a previous exited process.
182 (set-marker (process-mark proc) (point-max))
183 (rlogin-mode)
2601d12e 184
32992017
NF
185 ;; comint-output-filter-functions is just like a hook, except that the
186 ;; functions in that list are passed arguments. add-hook serves well
187 ;; enough for modifying it.
188 (add-hook 'comint-output-filter-functions 'rlogin-carriage-filter)
189
190 (make-local-variable 'rlogin-host)
191 (setq rlogin-host host)
60667917
NF
192 (make-local-variable 'rlogin-remote-user)
193 (setq rlogin-remote-user user)
32992017
NF
194
195 (cond
196 ((eq rlogin-directory-tracking-mode t)
197 ;; Do this here, rather than calling the tracking mode function, to
198 ;; avoid a gratuitous resync check; the default should be the
199 ;; user's home directory, be it local or remote.
2601d12e 200 (setq comint-file-name-prefix
60667917 201 (concat "/" rlogin-remote-user "@" rlogin-host ":"))
32992017
NF
202 (cd-absolute comint-file-name-prefix))
203 ((null rlogin-directory-tracking-mode))
204 (t
205 (cd-absolute (concat comint-file-name-prefix "~/"))))))))
ce992678 206
c7986c18 207(defun rlogin-mode ()
2601d12e 208 "Set major-mode for rlogin sessions.
8ae3bc9f 209If `rlogin-mode-hook' is set, run it."
c7986c18 210 (interactive)
8d30fe17 211 (kill-all-local-variables)
09567b5c 212 (shell-mode)
c7986c18 213 (setq major-mode 'rlogin-mode)
8d30fe17 214 (setq mode-name "rlogin")
c7986c18 215 (use-local-map rlogin-mode-map)
32992017 216 (setq shell-dirtrackp rlogin-directory-tracking-mode)
f3417b13 217 (make-local-variable 'comint-file-name-prefix)
c7986c18
ER
218 (run-hooks 'rlogin-mode-hook))
219
32992017
NF
220(defun rlogin-directory-tracking-mode (&optional prefix)
221 "Do remote or local directory tracking, or disable entirely.
222
223If called with no prefix argument or a unspecified prefix argument (just
224``\\[universal-argument]'' with no number) do remote directory tracking via
225ange-ftp. If called as a function, give it no argument.
226
227If called with a negative prefix argument, disable directory tracking
228entirely.
229
2601d12e 230If called with a positive, numeric prefix argument, e.g.
32992017
NF
231``\\[universal-argument] 1 M-x rlogin-directory-tracking-mode\'',
232then do directory tracking but assume the remote filesystem is the same as
233the local system. This only works in general if the remote machine and the
234local one share the same directories (through NFS)."
235 (interactive "P")
236 (cond
237 ((or (null prefix)
238 (consp prefix))
239 (setq rlogin-directory-tracking-mode t)
f9c4665a 240 (setq shell-dirtrackp t)
2601d12e 241 (setq comint-file-name-prefix
60667917 242 (concat "/" rlogin-remote-user "@" rlogin-host ":")))
32992017
NF
243 ((< prefix 0)
244 (setq rlogin-directory-tracking-mode nil)
f9c4665a 245 (setq shell-dirtrackp nil))
32992017
NF
246 (t
247 (setq rlogin-directory-tracking-mode 'local)
248 (setq comint-file-name-prefix "")
f9c4665a 249 (setq shell-dirtrackp t)))
2601d12e 250 (cond
f9c4665a 251 (shell-dirtrackp
32992017
NF
252 (let* ((proc (get-buffer-process (current-buffer)))
253 (proc-mark (process-mark proc))
254 (current-input (buffer-substring proc-mark (point-max)))
255 (orig-point (point))
256 (offset (and (>= orig-point proc-mark)
257 (- (point-max) orig-point))))
258 (unwind-protect
259 (progn
260 (delete-region proc-mark (point-max))
261 (goto-char (point-max))
262 (shell-resync-dirs))
263 (goto-char proc-mark)
264 (insert current-input)
265 (if offset
266 (goto-char (- (point-max) offset))
267 (goto-char orig-point)))))))
268
8ae3bc9f 269\f
32992017
NF
270;; Parse a line into its constituent parts (words separated by
271;; whitespace). Return a list of the words.
272(defun rlogin-parse-words (line)
273 (let ((list nil)
274 (posn 0)
275 (match-data (match-data)))
276 (while (string-match "[^ \t\n]+" line posn)
277 (setq list (cons (substring line (match-beginning 0) (match-end 0))
278 list))
279 (setq posn (match-end 0)))
280 (store-match-data (match-data))
281 (nreverse list)))
282
2601d12e
NF
283(defun rlogin-carriage-filter (string)
284 (let* ((point-marker (point-marker))
285 (end (process-mark (get-buffer-process (current-buffer))))
286 (beg (or (and (boundp 'comint-last-output-start)
287 comint-last-output-start)
288 (- end (length string)))))
289 (goto-char beg)
290 (while (search-forward "\C-m" end t)
32992017
NF
291 (delete-char -1))
292 (goto-char point-marker)))
293
c7986c18
ER
294(defun rlogin-send-Ctrl-C ()
295 (interactive)
296 (send-string nil "\C-c"))
297
099dcd39
RM
298(defun rlogin-send-Ctrl-D ()
299 (interactive)
300 (send-string nil "\C-d"))
301
c7986c18
ER
302(defun rlogin-send-Ctrl-Z ()
303 (interactive)
304 (send-string nil "\C-z"))
305
306(defun rlogin-send-Ctrl-backslash ()
307 (interactive)
308 (send-string nil "\C-\\"))
309
310(defun rlogin-delchar-or-send-Ctrl-D (arg)
057795ba 311 "\
2601d12e
NF
312Delete ARG characters forward, or send a C-d to process if at end of buffer."
313 (interactive "p")
c7986c18 314 (if (eobp)
057795ba 315 (rlogin-send-Ctrl-D)
c7986c18
ER
316 (delete-char arg)))
317
32992017 318(defun rlogin-tab-or-complete ()
60667917 319 "Complete file name if doing directory tracking, or just insert TAB."
32992017
NF
320 (interactive)
321 (if rlogin-directory-tracking-mode
322 (comint-dynamic-complete)
60667917 323 (insert "\C-i")))
32992017 324
76550a57 325;;; rlogin.el ends here