Initial revision
[bpt/emacs.git] / lisp / rlogin.el
CommitLineData
76550a57
ER
1;;; rlogin.el --- remote login interface
2
8ae3bc9f 3;; Author: Noah Friedman
76550a57 4;; Maintainer: Noah Friedman <friedman@prep.ai.mit.edu>
5d1dd3c0 5;; Keywords: unix, comm
76550a57 6
0b899bd2 7;; Copyright (C) 1992, 1993 Free Software Foundation, Inc.
d9ecc911
ER
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
8d30fe17
NF
20;; along with this program; if not, write to: The Free Software Foundation,
21;; Inc.; 675 Massachusetts Avenue.; Cambridge, MA 02139, USA.
d9ecc911
ER
22
23;;; Commentary:
24
0b899bd2 25;; Support for remote logins using `rlogin'.
d9ecc911
ER
26;;
27;; Todo: add directory tracking using ange-ftp style patchnames for the cwd.
c7986c18 28
76550a57
ER
29;;; Code:
30
c7986c18
ER
31(require 'comint)
32
8ae3bc9f 33;;;###autoload
c7986c18
ER
34(defvar rlogin-program "rlogin"
35 "*Name of program to invoke rlogin")
36
8ae3bc9f 37;;;###autoload
8d30fe17
NF
38(defvar rlogin-explicit-args nil
39 "*List of arguments to pass to rlogin on the command line.")
40
8ae3bc9f 41;;;###autoload
c7986c18
ER
42(defvar rlogin-mode-hook nil
43 "*Hooks to run after setting current buffer to rlogin-mode.")
44
8ae3bc9f
NF
45;;;###autoload
46(defvar rlogin-process-connection-type nil
47 "*If non-`nil', use a pty for the local rlogin process.
48If `nil', use a pipe (if pipes are supported on the local system).
8d30fe17 49
8ae3bc9f
NF
50Generally it is better not to waste ptys on systems which have a static
51number of them. On the other hand, some implementations of `rlogin' assume
52a pty is being used, and errors will result from using a pipe instead.")
8d30fe17 53
8ae3bc9f
NF
54;; Leave this nil because it makes rlogin-filter a tiny bit faster. Plus
55;; you can still call rlogin-password by hand.
56;;;###autoload
57(defvar rlogin-password-paranoia nil
58 "*If non-`nil', query user for a password in the minibuffer when a Password: prompt appears.
59It's also possible to selectively enter passwords without echoing them in
60the minibuffer using the command `rlogin-password' explicitly.")
8d30fe17 61
c7986c18 62;; Initialize rlogin mode map.
8ae3bc9f 63;;;###autoload
c7986c18
ER
64(defvar rlogin-mode-map '())
65(cond ((not rlogin-mode-map)
66 (setq rlogin-mode-map (full-copy-sparse-keymap comint-mode-map))
67 ;(define-key rlogin-mode-map "\M-\t" 'comint-dynamic-complete)
68 ;(define-key rlogin-mode-map "\M-?" 'comint-dynamic-list-completions)
69 (define-key rlogin-mode-map "\C-c\C-c" 'rlogin-send-Ctrl-C)
70 (define-key rlogin-mode-map "\C-c\C-z" 'rlogin-send-Ctrl-Z)
71 (define-key rlogin-mode-map "\C-c\C-\\" 'rlogin-send-Ctrl-backslash)
72 (define-key rlogin-mode-map "\C-d" 'rlogin-delchar-or-send-Ctrl-D)))
73
0b899bd2 74;;;###autoload
8d30fe17
NF
75(defun rlogin (&optional prefix host)
76 "Open a network login connection to HOST via the `rlogin' program.
77Input is sent line-at-a-time to the remote connection.
78
79Communication with HOST is recorded in a buffer *rlogin-HOST*.
80If a prefix argument is given and the buffer *rlogin-HOST* already exists,
81a new buffer with a different connection will be made.
82
83The variable `rlogin-program' contains the name of the actual program to
84run. It can be a relative or absolute path.
85
86The variable `rlogin-explicit-args' is a list of arguments to give to
87the rlogin when starting."
88 (interactive (list current-prefix-arg
89 (read-from-minibuffer "Open rlogin connection to host: ")))
8ae3bc9f
NF
90 (let* ((process-connection-type rlogin-process-connection-type)
91 (buffer-name (format "*rlogin-%s*" host))
8d30fe17
NF
92 (args (if (and rlogin-explicit-args (listp rlogin-explicit-args))
93 (cons host rlogin-explicit-args)
6fc81d6b
NF
94 (list host)))
95 proc)
8d30fe17
NF
96 (and prefix (setq buffer-name
97 (buffer-name (generate-new-buffer buffer-name))))
98 (switch-to-buffer buffer-name)
99 (or (comint-check-proc buffer-name)
100 (progn
101 (comint-mode)
102 (comint-exec (current-buffer) buffer-name rlogin-program nil args)
103 (setq proc (get-process buffer-name))
8ae3bc9f
NF
104 ;; Set process-mark to point-max in case there is text in the
105 ;; buffer from a previous exited process.
106 (set-marker (process-mark proc) (point-max))
8d30fe17
NF
107 (set-process-filter proc 'rlogin-filter)
108 (rlogin-mode)))))
109
110;;;###autoload
111(defun rlogin-with-args (host args)
112 "Open a new rlogin connection to HOST, even if one already exists.
113String ARGS is given as arguments to the `rlogin' program, overriding the
114value of `rlogin-explicit-args'."
115 (interactive (list (read-from-minibuffer "Open rlogin connection to host: ")
116 (read-from-minibuffer "with arguments: ")))
117 (let ((old-match-data (match-data))
118 (rlogin-explicit-args nil))
119 (unwind-protect
120 (progn
8ae3bc9f 121 (while (string-match "[ \t]*\\([^ \t]+\\)$" args)
8d30fe17
NF
122 (setq rlogin-explicit-args
123 (cons (substring args
124 (match-beginning 1)
125 (match-end 1))
126 rlogin-explicit-args)
127 args (substring args 0 (match-beginning 0)))))
128 (store-match-data old-match-data))
129 (rlogin 1 host)))
130
131;;;###autoload
8ae3bc9f
NF
132(defun rlogin-password (&optional proc)
133 "Read a password and send it to an rlogin session.
134For each character typed, a `*' is echoed in the minibuffer.
135End with RET, LFD, or ESC. DEL or C-h rubs out. C-u kills line.
136C-g aborts attempt to read and send password.
137
138Optional argument PROC is the process to which the password should be sent.
139If not provided, send to the process in the current buffer. This argument
140is intended primarily for use by `rlogin-filter'."
8d30fe17 141 (interactive)
8ae3bc9f
NF
142 (or proc (setq proc (get-buffer-process (current-buffer))))
143 (let* ((buffer-name (buffer-name (process-buffer proc)))
144 (pass (comint-read-noecho (format "Password for buffer \"%s\": "
145 buffer-name)
146 'stars)))
147 (and pass
148 (save-excursion
149 (set-buffer buffer-name)
150 (insert-before-markers "\n")
151 (comint-send-string proc (format "%s\n" pass))))))
c7986c18 152
0b899bd2 153;;;###autoload
c7986c18 154(defun rlogin-mode ()
8ae3bc9f
NF
155 "Set major-mode for rlogin sessions.
156If `rlogin-mode-hook' is set, run it."
c7986c18 157 (interactive)
8d30fe17 158 (kill-all-local-variables)
c7986c18
ER
159 (comint-mode)
160 (setq comint-prompt-regexp shell-prompt-pattern)
161 (setq major-mode 'rlogin-mode)
8d30fe17 162 (setq mode-name "rlogin")
c7986c18
ER
163 (use-local-map rlogin-mode-map)
164 (run-hooks 'rlogin-mode-hook))
165
8ae3bc9f 166\f
c7986c18 167(defun rlogin-filter (proc string)
4a6ac398
NF
168 (let (proc-mark region-begin window)
169 (save-excursion
170 (set-buffer (process-buffer proc))
171 (setq proc-mark (process-mark proc)
172 region-begin (point)
173 ;; If process mark is at window start, insert-before-markers
174 ;; will insert text off-window since it's also inserting before
175 ;; the start window mark. Make sure we can see the most recent
176 ;; text. (note: it's a buglet that this isn't necessary if
177 ;; scroll-step is 0, but that works to our advantage since it
178 ;; makes the filter a little faster.)
179 window (and (/= 0 scroll-step)
180 (= proc-mark (window-start))
181 (get-buffer-window (current-buffer))))
8ae3bc9f
NF
182 (goto-char proc-mark)
183 (insert-before-markers string)
184 (goto-char region-begin)
4a6ac398
NF
185 ;; I think something fishy is going on with save-excursion and
186 ;; search-forward. If you don't make search-forward move to the end
187 ;; of the search region when it's done, then if the user switches
188 ;; buffers back and forth, it leaves point sitting behind the
189 ;; process-mark, so that text inserted later goes off-screen.
190 (while (search-forward "\C-m" proc-mark 'goto-end)
191 (delete-char -1)))
192 ;; Frob window-start outside of save-excursion so it works whether the
193 ;; current buffer is the process buffer or not.
194 (and window
195 (>= (window-start window) region-begin)
196 (set-window-start window region-begin 'noforce)))
8d30fe17
NF
197 (and rlogin-password-paranoia
198 (string= "Password:" string)
8ae3bc9f 199 (rlogin-password proc)))
c7986c18 200
8ae3bc9f 201;;;###autoload
c7986c18
ER
202(defun rlogin-send-Ctrl-C ()
203 (interactive)
204 (send-string nil "\C-c"))
205
8ae3bc9f 206;;;###autoload
c7986c18
ER
207(defun rlogin-send-Ctrl-Z ()
208 (interactive)
209 (send-string nil "\C-z"))
210
8ae3bc9f 211;;;###autoload
c7986c18
ER
212(defun rlogin-send-Ctrl-backslash ()
213 (interactive)
214 (send-string nil "\C-\\"))
215
8ae3bc9f 216;;;###autoload
c7986c18
ER
217(defun rlogin-delchar-or-send-Ctrl-D (arg)
218 "Delete ARG characters forward, or send a C-d to process if at end of
219buffer."
220 (interactive "p")
221 (if (eobp)
222 (send-string nil "\C-d")
223 (delete-char arg)))
224
76550a57 225;;; rlogin.el ends here