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