(read_minibuf): Get the string from the minibuffer
[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
7e939f29 26;; $Id: rlogin.el,v 1.34 1996/06/20 17:30:41 friedman Exp friedman $
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)
4131662f
RS
112 "Open a network login connection via `rlogin' with args INPUT-ARGS.
113INPUT-ARGS should start with a host name; it may also contain
114other arguments for `rlogin'.
115
8d30fe17
NF
116Input is sent line-at-a-time to the remote connection.
117
67398f34
RS
118Communication with the remote host is recorded in a buffer `*rlogin-HOST*'
119\(or `*rlogin-USER@HOST*' if the remote username differs\).
120If a prefix argument is given and the buffer `*rlogin-HOST*' already exists,
32992017 121a new buffer with a different connection will be made.
8d30fe17 122
4131662f
RS
123When called from a program, if the optional second argument BUFFER is
124a string or buffer, it specifies the buffer to use.
965f7ac9 125
8d30fe17 126The variable `rlogin-program' contains the name of the actual program to
2601d12e 127run. It can be a relative or absolute path.
8d30fe17
NF
128
129The variable `rlogin-explicit-args' is a list of arguments to give to
ce992678
RS
130the rlogin when starting. They are added after any arguments given in
131INPUT-ARGS.
b79164c7 132
32992017
NF
133If the default value of `rlogin-directory-tracking-mode' is t, then the
134default directory in that buffer is set to a remote (FTP) file name to
135access your home directory on the remote machine. Occasionally this causes
136an error, if you cannot access the home directory on that machine. This
137error is harmless as long as you don't try to use that default directory.
ce992678 138
32992017 139If `rlogin-directory-tracking-mode' is neither t nor nil, then the default
ce992678
RS
140directory is initially set up to your (local) home directory.
141This is useful if the remote machine and your local machine
32992017
NF
142share the same files via NFS. This is the default.
143
144If you wish to change directory tracking styles during a session, use the
145function `rlogin-directory-tracking-mode' rather than simply setting the
146variable."
5236f937 147 (interactive (list
cd17ae3f
RM
148 (read-from-minibuffer "rlogin arguments (hostname first): "
149 nil nil nil 'rlogin-history)
5236f937 150 current-prefix-arg))
32992017 151
8ae3bc9f 152 (let* ((process-connection-type rlogin-process-connection-type)
32992017
NF
153 (args (if rlogin-explicit-args
154 (append (rlogin-parse-words input-args)
155 rlogin-explicit-args)
156 (rlogin-parse-words input-args)))
157 (host (car args))
158 (user (or (car (cdr (member "-l" args)))
159 (user-login-name)))
160 (buffer-name (if (string= user (user-login-name))
161 (format "*rlogin-%s*" host)
162 (format "*rlogin-%s@%s*" user host)))
163 proc)
164
965f7ac9 165 (cond ((null buffer))
e48e5617 166 ((stringp buffer)
965f7ac9 167 (setq buffer-name buffer))
e48e5617
NF
168 ((bufferp buffer)
169 (setq buffer-name (buffer-name buffer)))
965f7ac9
RM
170 ((numberp buffer)
171 (setq buffer-name (format "%s<%d>" buffer-name buffer)))
e1f06ce8
NF
172 (t
173 (setq buffer-name (generate-new-buffer-name buffer-name))))
174
e48e5617 175 (setq buffer (get-buffer-create buffer-name))
e1f06ce8 176 (pop-to-buffer buffer-name)
e48e5617 177
32992017 178 (cond
e1f06ce8 179 ((comint-check-proc buffer-name))
32992017 180 (t
e48e5617
NF
181 (comint-exec buffer buffer-name rlogin-program nil args)
182 (setq proc (get-buffer-process buffer))
32992017
NF
183 ;; Set process-mark to point-max in case there is text in the
184 ;; buffer from a previous exited process.
185 (set-marker (process-mark proc) (point-max))
2601d12e 186
32992017
NF
187 ;; comint-output-filter-functions is just like a hook, except that the
188 ;; functions in that list are passed arguments. add-hook serves well
189 ;; enough for modifying it.
fb7741f5
NF
190 ;; comint-output-filter-functions should already have a
191 ;; permanent-local property, at least in emacs 19.27 or later.
192 (if (fboundp 'make-local-hook)
193 (make-local-hook 'comint-output-filter-functions)
194 (make-local-variable 'comint-output-filter-functions))
7e939f29 195 (add-hook 'comint-output-filter-functions 'rlogin-carriage-filter)
32992017 196
9fa128e8
NF
197 (rlogin-mode)
198
32992017
NF
199 (make-local-variable 'rlogin-host)
200 (setq rlogin-host host)
60667917
NF
201 (make-local-variable 'rlogin-remote-user)
202 (setq rlogin-remote-user user)
32992017 203
fb7741f5
NF
204 (condition-case ()
205 (cond ((eq rlogin-directory-tracking-mode t)
206 ;; Do this here, rather than calling the tracking mode
207 ;; function, to avoid a gratuitous resync check; the default
208 ;; should be the user's home directory, be it local or remote.
209 (setq comint-file-name-prefix
210 (concat "/" rlogin-remote-user "@" rlogin-host ":"))
211 (cd-absolute comint-file-name-prefix))
212 ((null rlogin-directory-tracking-mode))
213 (t
214 (cd-absolute (concat comint-file-name-prefix "~/"))))
215 (error nil))))))
ce992678 216
c7986c18 217(defun rlogin-mode ()
2601d12e 218 "Set major-mode for rlogin sessions.
8ae3bc9f 219If `rlogin-mode-hook' is set, run it."
c7986c18 220 (interactive)
8d30fe17 221 (kill-all-local-variables)
09567b5c 222 (shell-mode)
c7986c18 223 (setq major-mode 'rlogin-mode)
8d30fe17 224 (setq mode-name "rlogin")
c7986c18 225 (use-local-map rlogin-mode-map)
32992017 226 (setq shell-dirtrackp rlogin-directory-tracking-mode)
f3417b13 227 (make-local-variable 'comint-file-name-prefix)
c7986c18
ER
228 (run-hooks 'rlogin-mode-hook))
229
32992017
NF
230(defun rlogin-directory-tracking-mode (&optional prefix)
231 "Do remote or local directory tracking, or disable entirely.
232
233If called with no prefix argument or a unspecified prefix argument (just
234``\\[universal-argument]'' with no number) do remote directory tracking via
235ange-ftp. If called as a function, give it no argument.
236
237If called with a negative prefix argument, disable directory tracking
238entirely.
239
2601d12e 240If called with a positive, numeric prefix argument, e.g.
32992017
NF
241``\\[universal-argument] 1 M-x rlogin-directory-tracking-mode\'',
242then do directory tracking but assume the remote filesystem is the same as
243the local system. This only works in general if the remote machine and the
244local one share the same directories (through NFS)."
245 (interactive "P")
246 (cond
247 ((or (null prefix)
248 (consp prefix))
249 (setq rlogin-directory-tracking-mode t)
f9c4665a 250 (setq shell-dirtrackp t)
2601d12e 251 (setq comint-file-name-prefix
60667917 252 (concat "/" rlogin-remote-user "@" rlogin-host ":")))
32992017
NF
253 ((< prefix 0)
254 (setq rlogin-directory-tracking-mode nil)
f9c4665a 255 (setq shell-dirtrackp nil))
32992017
NF
256 (t
257 (setq rlogin-directory-tracking-mode 'local)
258 (setq comint-file-name-prefix "")
f9c4665a 259 (setq shell-dirtrackp t)))
2601d12e 260 (cond
f9c4665a 261 (shell-dirtrackp
32992017
NF
262 (let* ((proc (get-buffer-process (current-buffer)))
263 (proc-mark (process-mark proc))
264 (current-input (buffer-substring proc-mark (point-max)))
265 (orig-point (point))
266 (offset (and (>= orig-point proc-mark)
267 (- (point-max) orig-point))))
268 (unwind-protect
269 (progn
270 (delete-region proc-mark (point-max))
271 (goto-char (point-max))
272 (shell-resync-dirs))
273 (goto-char proc-mark)
274 (insert current-input)
275 (if offset
276 (goto-char (- (point-max) offset))
277 (goto-char orig-point)))))))
278
8ae3bc9f 279\f
32992017
NF
280;; Parse a line into its constituent parts (words separated by
281;; whitespace). Return a list of the words.
282(defun rlogin-parse-words (line)
283 (let ((list nil)
284 (posn 0)
285 (match-data (match-data)))
286 (while (string-match "[^ \t\n]+" line posn)
287 (setq list (cons (substring line (match-beginning 0) (match-end 0))
288 list))
289 (setq posn (match-end 0)))
290 (store-match-data (match-data))
291 (nreverse list)))
292
2601d12e
NF
293(defun rlogin-carriage-filter (string)
294 (let* ((point-marker (point-marker))
295 (end (process-mark (get-buffer-process (current-buffer))))
296 (beg (or (and (boundp 'comint-last-output-start)
297 comint-last-output-start)
298 (- end (length string)))))
299 (goto-char beg)
300 (while (search-forward "\C-m" end t)
32992017
NF
301 (delete-char -1))
302 (goto-char point-marker)))
303
c7986c18
ER
304(defun rlogin-send-Ctrl-C ()
305 (interactive)
306 (send-string nil "\C-c"))
307
099dcd39
RM
308(defun rlogin-send-Ctrl-D ()
309 (interactive)
310 (send-string nil "\C-d"))
311
c7986c18
ER
312(defun rlogin-send-Ctrl-Z ()
313 (interactive)
314 (send-string nil "\C-z"))
315
316(defun rlogin-send-Ctrl-backslash ()
317 (interactive)
318 (send-string nil "\C-\\"))
319
320(defun rlogin-delchar-or-send-Ctrl-D (arg)
057795ba 321 "\
2601d12e
NF
322Delete ARG characters forward, or send a C-d to process if at end of buffer."
323 (interactive "p")
c7986c18 324 (if (eobp)
057795ba 325 (rlogin-send-Ctrl-D)
c7986c18
ER
326 (delete-char arg)))
327
32992017 328(defun rlogin-tab-or-complete ()
60667917 329 "Complete file name if doing directory tracking, or just insert TAB."
32992017
NF
330 (interactive)
331 (if rlogin-directory-tracking-mode
332 (comint-dynamic-complete)
60667917 333 (insert "\C-i")))
32992017 334
76550a57 335;;; rlogin.el ends here