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