(save-match-data): Use save-match-data-internal
[emacs.git] / lisp / rlogin.el
bloba197133d63b7dd6cb9fe0828ecb0b73e0fb033ef
1 ;;; rlogin.el --- remote login interface
3 ;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
5 ;; Author: Noah Friedman
6 ;; Maintainer: Noah Friedman <friedman@prep.ai.mit.edu>
7 ;; Keywords: unix, comm
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
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.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
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.
21 ;; You should have received a copy of the GNU General Public License
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.
26 ;; $Id: rlogin.el,v 1.34 1996/06/20 17:30:41 friedman Exp friedman $
28 ;;; Commentary:
30 ;; Support for remote logins using `rlogin'.
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.
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'.
40 ;;; Code:
42 (require 'comint)
43 (require 'shell)
45 (defvar rlogin-program "rlogin"
46 "*Name of program to invoke rlogin")
48 (defvar rlogin-explicit-args nil
49 "*List of arguments to pass to rlogin on the command line.")
51 (defvar rlogin-mode-hook nil
52 "*Hooks to run after setting current buffer to rlogin-mode.")
54 (defvar rlogin-process-connection-type nil
55 "*If non-`nil', use a pty for the local rlogin process.
56 If `nil', use a pipe (if pipes are supported on the local system).
58 Generally it is better not to waste ptys on systems which have a static
59 number of them. On the other hand, some implementations of `rlogin' assume
60 a pty is being used, and errors will result from using a pipe instead.")
62 (defvar rlogin-directory-tracking-mode 'local
63 "*Control whether and how to do directory tracking in an rlogin buffer.
65 nil means don't do directory tracking.
67 t means do so using an ftp remote file name.
69 Any other value means do directory tracking using local file names.
70 This works only if the remote machine and the local one
71 share the same directories (through NFS). This is the default.
73 This variable becomes local to a buffer when set in any fashion for it.
75 It is better to use the function of the same name to change the behavior of
76 directory tracking in an rlogin session once it has begun, rather than
77 simply setting this variable, since the function does the necessary
78 re-synching of directories.")
80 (make-variable-buffer-local 'rlogin-directory-tracking-mode)
82 (defvar rlogin-host nil
83 "*The name of the remote host. This variable is buffer-local.")
85 (defvar rlogin-remote-user nil
86 "*The username used on the remote host.
87 This variable is buffer-local and defaults to your local user name.
88 If rlogin is invoked with the `-l' option to specify the remote username,
89 this variable is set from that.")
91 ;; Initialize rlogin mode map.
92 (defvar rlogin-mode-map '())
93 (cond
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)))
106 ;;;###autoload (add-hook 'same-window-regexps "^\\*rlogin-.*\\*\\(\\|<[0-9]+>\\)")
108 (defvar rlogin-history nil)
110 ;;;###autoload
111 (defun rlogin (input-args &optional buffer)
112 "Open a network login connection via `rlogin' with args INPUT-ARGS.
113 INPUT-ARGS should start with a host name; it may also contain
114 other arguments for `rlogin'.
116 Input is sent line-at-a-time to the remote connection.
118 Communication with the remote host is recorded in a buffer `*rlogin-HOST*'
119 \(or `*rlogin-USER@HOST*' if the remote username differs\).
120 If a prefix argument is given and the buffer `*rlogin-HOST*' already exists,
121 a new buffer with a different connection will be made.
123 When called from a program, if the optional second argument BUFFER is
124 a string or buffer, it specifies the buffer to use.
126 The variable `rlogin-program' contains the name of the actual program to
127 run. It can be a relative or absolute path.
129 The variable `rlogin-explicit-args' is a list of arguments to give to
130 the rlogin when starting. They are added after any arguments given in
131 INPUT-ARGS.
133 If the default value of `rlogin-directory-tracking-mode' is t, then the
134 default directory in that buffer is set to a remote (FTP) file name to
135 access your home directory on the remote machine. Occasionally this causes
136 an error, if you cannot access the home directory on that machine. This
137 error is harmless as long as you don't try to use that default directory.
139 If `rlogin-directory-tracking-mode' is neither t nor nil, then the default
140 directory is initially set up to your (local) home directory.
141 This is useful if the remote machine and your local machine
142 share the same files via NFS. This is the default.
144 If you wish to change directory tracking styles during a session, use the
145 function `rlogin-directory-tracking-mode' rather than simply setting the
146 variable."
147 (interactive (list
148 (read-from-minibuffer "rlogin arguments (hostname first): "
149 nil nil nil 'rlogin-history)
150 current-prefix-arg))
152 (let* ((process-connection-type rlogin-process-connection-type)
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)
165 (cond ((null buffer))
166 ((stringp buffer)
167 (setq buffer-name buffer))
168 ((bufferp buffer)
169 (setq buffer-name (buffer-name buffer)))
170 ((numberp buffer)
171 (setq buffer-name (format "%s<%d>" buffer-name buffer)))
173 (setq buffer-name (generate-new-buffer-name buffer-name))))
175 (setq buffer (get-buffer-create buffer-name))
176 (pop-to-buffer buffer-name)
178 (cond
179 ((comint-check-proc buffer-name))
181 (comint-exec buffer buffer-name rlogin-program nil args)
182 (setq proc (get-buffer-process buffer))
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))
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.
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))
195 (add-hook 'comint-output-filter-functions 'rlogin-carriage-filter)
197 (rlogin-mode)
199 (make-local-variable 'rlogin-host)
200 (setq rlogin-host host)
201 (make-local-variable 'rlogin-remote-user)
202 (setq rlogin-remote-user user)
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))
214 (cd-absolute (concat comint-file-name-prefix "~/"))))
215 (error nil))))))
217 (defun rlogin-mode ()
218 "Set major-mode for rlogin sessions.
219 If `rlogin-mode-hook' is set, run it."
220 (interactive)
221 (kill-all-local-variables)
222 (shell-mode)
223 (setq major-mode 'rlogin-mode)
224 (setq mode-name "rlogin")
225 (use-local-map rlogin-mode-map)
226 (setq shell-dirtrackp rlogin-directory-tracking-mode)
227 (make-local-variable 'comint-file-name-prefix)
228 (run-hooks 'rlogin-mode-hook))
230 (defun rlogin-directory-tracking-mode (&optional prefix)
231 "Do remote or local directory tracking, or disable entirely.
233 If called with no prefix argument or a unspecified prefix argument (just
234 ``\\[universal-argument]'' with no number) do remote directory tracking via
235 ange-ftp. If called as a function, give it no argument.
237 If called with a negative prefix argument, disable directory tracking
238 entirely.
240 If called with a positive, numeric prefix argument, e.g.
241 ``\\[universal-argument] 1 M-x rlogin-directory-tracking-mode\'',
242 then do directory tracking but assume the remote filesystem is the same as
243 the local system. This only works in general if the remote machine and the
244 local 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)
250 (setq shell-dirtrackp t)
251 (setq comint-file-name-prefix
252 (concat "/" rlogin-remote-user "@" rlogin-host ":")))
253 ((< prefix 0)
254 (setq rlogin-directory-tracking-mode nil)
255 (setq shell-dirtrackp nil))
257 (setq rlogin-directory-tracking-mode 'local)
258 (setq comint-file-name-prefix "")
259 (setq shell-dirtrackp t)))
260 (cond
261 (shell-dirtrackp
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)))))))
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)))
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)
301 (delete-char -1))
302 (goto-char point-marker)))
304 (defun rlogin-send-Ctrl-C ()
305 (interactive)
306 (send-string nil "\C-c"))
308 (defun rlogin-send-Ctrl-D ()
309 (interactive)
310 (send-string nil "\C-d"))
312 (defun rlogin-send-Ctrl-Z ()
313 (interactive)
314 (send-string nil "\C-z"))
316 (defun rlogin-send-Ctrl-backslash ()
317 (interactive)
318 (send-string nil "\C-\\"))
320 (defun rlogin-delchar-or-send-Ctrl-D (arg)
322 Delete ARG characters forward, or send a C-d to process if at end of buffer."
323 (interactive "p")
324 (if (eobp)
325 (rlogin-send-Ctrl-D)
326 (delete-char arg)))
328 (defun rlogin-tab-or-complete ()
329 "Complete file name if doing directory tracking, or just insert TAB."
330 (interactive)
331 (if rlogin-directory-tracking-mode
332 (comint-dynamic-complete)
333 (insert "\C-i")))
335 ;;; rlogin.el ends here