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