0
|
1 ;;; telnet.el --- run a telnet session from within an Emacs buffer
|
|
2
|
74
|
3 ;;; Copyright (C) 1985, 1988, 1992, 1994 Free Software Foundation, Inc.
|
0
|
4
|
|
5 ;; Author: William F. Schelter
|
|
6 ;; Keywords: comm, unix
|
|
7 ;; Maintainer: FSF
|
|
8
|
|
9 ;; This file is part of XEmacs.
|
|
10
|
|
11 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
12 ;; 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 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
19 ;; General Public License for more details.
|
|
20
|
|
21 ;; You should have received a copy of the GNU General Public License
|
74
|
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
23 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
24 ;; 02111-1307, USA.
|
0
|
25
|
74
|
26 ;;; Synched up with: FSF 19.34.
|
0
|
27
|
|
28 ;;; Commentary:
|
|
29
|
|
30 ;; This mode is intended to be used for telnet or rsh to a remode host;
|
|
31 ;; `telnet' and `rsh' are the two entry points. Multiple telnet or rsh
|
|
32 ;; sessions are supported.
|
|
33 ;;
|
|
34 ;; Normally, input is sent to the remote telnet/rsh line-by-line, as you
|
|
35 ;; type RET or LFD. C-c C-c sends a C-c to the remote immediately;
|
|
36 ;; C-c C-z sends C-z immediately. C-c C-q followed by any character
|
|
37 ;; sends that character immediately.
|
|
38 ;;
|
|
39 ;; All RET characters are filtered out of the output coming back from the
|
|
40 ;; remote system. The mode tries to do other useful translations based
|
|
41 ;; on what it sees coming back from the other system before the password
|
|
42 ;; query. It knows about UNIX, ITS, TOPS-20 and Explorer systems.
|
|
43
|
|
44 ;;; Code:
|
|
45
|
|
46 ;; to do fix software types for lispm:
|
|
47 ;; to eval current expression. Also to try to send escape keys correctly.
|
|
48 ;; essentially we'll want the rubout-handler off.
|
|
49
|
|
50 ;; filter is simplistic but should be okay for typical shell usage.
|
|
51 ;; needs hacking if it is going to deal with asynchronous output in a sane
|
|
52 ;; manner
|
|
53
|
|
54 (require 'comint)
|
|
55
|
124
|
56 (defgroup telnet nil
|
|
57 "Telnet/rsh stuff"
|
|
58 :group 'comint)
|
|
59
|
0
|
60 (defvar telnet-new-line "\r")
|
|
61 (defvar telnet-mode-map nil)
|
8
|
62 (defvar telnet-default-prompt-pattern "^[^#$%>\n]*[#$%>] *")
|
|
63 (defvar telnet-prompt-pattern telnet-default-prompt-pattern)
|
74
|
64
|
0
|
65 (defvar telnet-replace-c-g nil)
|
74
|
66 (make-variable-buffer-local
|
|
67 (defvar telnet-remote-echoes t
|
|
68 "True if the telnet process will echo input."))
|
|
69 (make-variable-buffer-local
|
|
70 (defvar telnet-interrupt-string "\C-c" "String sent by C-c."))
|
0
|
71
|
|
72 (defvar telnet-count 0
|
|
73 "Number of output strings read from the telnet process
|
|
74 while looking for the initial password.")
|
82
|
75 ;; (make-variable-buffer-local 'telnet-count)
|
48
|
76
|
124
|
77 (defcustom telnet-program "telnet"
|
|
78 "*Program to run to open a telnet connection."
|
|
79 :group 'telnet)
|
|
80
|
|
81 (defcustom rsh-eat-password-string nil
|
|
82 "Non-nil means rsh will look for a string matching a password prompt."
|
|
83 :type 'boolean
|
|
84 :group 'telnet)
|
0
|
85
|
80
|
86 (defvar telnet-initial-count -75
|
0
|
87 "Initial value of `telnet-count'. Should be set to the negative of the
|
|
88 number of terminal writes telnet will make setting up the host connection.")
|
|
89
|
|
90 (defvar telnet-maximum-count 4
|
|
91 "Maximum value `telnet-count' can have.
|
|
92 After this many passes, we stop looking for initial setup data.
|
|
93 Should be set to the number of terminal writes telnet will make
|
|
94 rejecting one login and prompting again for a username and password.")
|
|
95
|
|
96 (defun telnet-interrupt-subjob ()
|
|
97 (interactive)
|
|
98 "Interrupt the program running through telnet on the remote host."
|
|
99 (process-send-string nil telnet-interrupt-string))
|
|
100
|
|
101 (defun telnet-c-z ()
|
|
102 (interactive)
|
|
103 (process-send-string nil "\C-z"))
|
|
104
|
74
|
105 ;; XEmacs change (Keep telnet- prefix)
|
0
|
106 (defun telnet-send-process-next-char ()
|
|
107 (interactive)
|
|
108 (process-send-string nil
|
|
109 (char-to-string
|
|
110 (let ((inhibit-quit t))
|
|
111 (prog1 (read-char)
|
|
112 (setq quit-flag nil))))))
|
|
113
|
|
114 ; initialization on first load.
|
|
115 (if telnet-mode-map
|
|
116 nil
|
74
|
117 ;; FSF
|
|
118 ;; (setq telnet-mode-map (nconc (make-sparse-keymap) comint-mode-map))
|
|
119 (setq telnet-mode-map (make-sparse-keymap))
|
|
120 (set-keymap-parents telnet-mode-map (list comint-mode-map))
|
|
121 (define-key telnet-mode-map "\C-m" 'telnet-send-input)
|
|
122 ; (define-key telnet-mode-map "\C-j" 'telnet-send-input)
|
138
|
123 (define-key telnet-mode-map "\C-c\C-q" 'telnet-send-process-next-char)
|
74
|
124 (define-key telnet-mode-map "\C-c\C-c" 'telnet-interrupt-subjob)
|
|
125 (define-key telnet-mode-map "\C-c\C-z" 'telnet-c-z))
|
0
|
126
|
|
127 ;;maybe should have a flag for when have found type
|
|
128 (defun telnet-check-software-type-initialize (string)
|
|
129 "Tries to put correct initializations in. Needs work."
|
|
130 (let ((case-fold-search t))
|
|
131 (cond ((string-match "unix" string)
|
124
|
132 (setq telnet-prompt-pattern shell-prompt-pattern)
|
0
|
133 (setq telnet-new-line "\n"))
|
|
134 ((string-match "tops-20" string) ;;maybe add telnet-replace-c-g
|
|
135 (setq telnet-prompt-pattern "[@>] *"))
|
|
136 ((string-match "its" string)
|
|
137 (setq telnet-prompt-pattern "^[^*>\n]*[*>] *"))
|
|
138 ((string-match "explorer" string) ;;explorer telnet needs work
|
78
|
139 (setq telnet-replace-c-g ?\n))))
|
0
|
140 (setq comint-prompt-regexp telnet-prompt-pattern))
|
|
141
|
|
142 (defun telnet-initial-filter (proc string)
|
80
|
143 (let ((case-fold-search t))
|
|
144 ;For reading up to and including password; also will get machine type.
|
|
145 (cond ((string-match "No such host" string)
|
|
146 (kill-buffer (process-buffer proc))
|
|
147 (error "No such host."))
|
|
148 ((string-match "passw" string)
|
0
|
149 (telnet-filter proc string)
|
80
|
150 (let ((password (comint-read-noecho "Password: " t)))
|
|
151 (setq telnet-count 0)
|
|
152 (process-send-string proc (concat password telnet-new-line))))
|
|
153 (t (telnet-check-software-type-initialize string)
|
|
154 (telnet-filter proc string)
|
|
155 (cond ((> telnet-count telnet-maximum-count)
|
|
156 ;; (set-process-filter proc 'telnet-filter) Kludge
|
|
157 ;; for shell-fonts -- this is the only mode that
|
|
158 ;; actually changes what its process filter is at
|
|
159 ;; run time, which confuses shell-font. So we
|
|
160 ;; special-case that here.
|
|
161 ;; #### Danger, knows an internal shell-font variable name.
|
|
162 (let ((old-filter (process-filter proc)))
|
|
163 (if (eq old-filter 'shell-font-process-filter)
|
|
164 (set (make-local-variable 'shell-font-process-filter)
|
|
165 'telnet-filter)
|
|
166 (set-process-filter proc 'telnet-filter))))
|
|
167 (t (setq telnet-count (1+ telnet-count))))))))
|
0
|
168
|
|
169 ;; Identical to comint-simple-send, except that it sends telnet-new-line
|
|
170 ;; instead of "\n".
|
|
171 (defun telnet-simple-send (proc string)
|
|
172 (comint-send-string proc string)
|
|
173 (comint-send-string proc telnet-new-line))
|
|
174
|
|
175 (defun telnet-filter (proc string)
|
|
176 (save-excursion
|
|
177 (set-buffer (process-buffer proc))
|
|
178 (save-match-data
|
|
179 (let* ((last-insertion (marker-position (process-mark proc)))
|
|
180 (delta (- (point) last-insertion))
|
|
181 (ie (and comint-last-input-end
|
|
182 (marker-position comint-last-input-end)))
|
|
183 (w (get-buffer-window (current-buffer)))
|
|
184 (ws (and w (window-start w))))
|
|
185 (goto-char last-insertion)
|
|
186 ;; Insert STRING, omitting all C-m characters.
|
|
187 (insert-before-markers string)
|
|
188 (set-marker (process-mark proc) (point))
|
|
189 ;; the insert-before-markers may have screwed window-start
|
|
190 ;; and likely moved comint-last-input-end. This is why the
|
|
191 ;; insertion-reaction should be a property of markers, not
|
|
192 ;; of the function which does the inserting.
|
|
193 (if ws (set-window-start w ws t))
|
|
194 (if ie (set-marker comint-last-input-end ie))
|
|
195 (while (progn (skip-chars-backward "^\C-m" last-insertion)
|
|
196 (> (point) last-insertion))
|
|
197 (delete-region (1- (point)) (point)))
|
|
198 (goto-char (process-mark proc))
|
|
199 (and telnet-replace-c-g
|
|
200 (subst-char-in-region last-insertion (point) ?\C-g
|
|
201 telnet-replace-c-g t))
|
|
202 ;; If point is after the insertion place, move it
|
|
203 ;; along with the text.
|
|
204 (if (> delta 0)
|
|
205 (goto-char (+ (process-mark proc) delta)))))))
|
|
206
|
|
207 (defun telnet-send-input ()
|
|
208 (interactive)
|
|
209 (let ((proc (get-buffer-process (current-buffer)))
|
|
210 p1 p2)
|
|
211 (if (and telnet-remote-echoes
|
|
212 (>= (point) (process-mark proc)))
|
|
213 (save-excursion
|
|
214 (if comint-eol-on-send (end-of-line))
|
|
215 (setq p1 (marker-position (process-mark proc))
|
|
216 p2 (point))))
|
|
217 (prog1
|
|
218 (comint-send-input)
|
|
219 ;; at this point, comint-send-input has moved the process mark, inserted
|
|
220 ;; a newline, and possibly inserted the (echoed) output. If the host is
|
|
221 ;; in remote-echo mode, then delete our local copy of the command, and
|
|
222 ;; the newline that comint-send-input sent.
|
|
223 (if p1
|
|
224 (delete-region p1 (1+ p2))))))
|
|
225
|
|
226 ;;;###autoload (add-hook 'same-window-regexps "\\*telnet-.*\\*\\(\\|<[0-9]+>\\)")
|
|
227
|
|
228 ;;;###autoload
|
|
229 (defun telnet (host &optional port)
|
|
230 "Open a network login connection to host named HOST (a string).
|
|
231 With a prefix argument, prompts for the port name or number as well.
|
|
232 Communication with HOST is recorded in a buffer `*HOST-telnet*'.
|
|
233 Normally input is edited in Emacs and sent a line at a time.
|
|
234 See also `\\[rsh]'."
|
|
235 (interactive (list (read-string "Open telnet connection to host: ")
|
|
236 (if current-prefix-arg
|
|
237 (read-string "Port name or number: ")
|
|
238 nil)))
|
|
239 (let* ((comint-delimiter-argument-list '(?\ ?\t))
|
|
240 (name (concat "telnet-" (comint-arguments host 0 nil)
|
|
241 (if port (concat "/" port) "")))
|
|
242 (buffer (get-buffer (concat "*" name "*")))
|
|
243 process)
|
|
244 (if (and buffer (get-buffer-process buffer))
|
|
245 (pop-to-buffer buffer)
|
|
246 (pop-to-buffer (make-comint name telnet-program))
|
|
247 (setq process (get-buffer-process (current-buffer)))
|
|
248 (set-process-filter process 'telnet-initial-filter)
|
|
249
|
|
250 ;; SunOS and IRIX don't print "unix" in their rsh or telnet
|
|
251 ;; login banners, so let's get a reasonable default here.
|
|
252 ;; #### This patch from jwz mimics what is done in rsh done
|
|
253 ;; below. However, it (along with the one in rsh) mean that
|
|
254 ;; telnet-check-software-type-initialize is effectively a
|
|
255 ;; wastoid function. Reworking it like it claims to need is
|
|
256 ;; probably the better solution but I'm not going to do it.
|
|
257 ;; --cet
|
|
258 (telnet-check-software-type-initialize "unix")
|
|
259
|
|
260 ;; Don't send the `open' cmd till telnet is ready for it.
|
|
261 (accept-process-output process)
|
|
262 (erase-buffer)
|
|
263 (process-send-string process (concat "open " host
|
|
264 (if port (concat " " port) "")
|
|
265 "\n"))
|
|
266 (setq comint-input-sender 'telnet-simple-send)
|
|
267 ;; run last so that hooks can change things.
|
|
268 (telnet-mode))))
|
|
269
|
|
270 (defun telnet-mode ()
|
|
271 "This mode is for using telnet (or rsh) from a buffer to another host.
|
|
272 It has most of the same commands as comint-mode.
|
|
273 There is a variable ``telnet-interrupt-string'' which is the character
|
|
274 sent to try to stop execution of a job on the remote host.
|
|
275 Data is sent to the remote host when RET is typed.
|
|
276
|
|
277 \\{telnet-mode-map}
|
|
278 "
|
|
279 (interactive)
|
|
280 (comint-mode)
|
|
281 (setq major-mode 'telnet-mode
|
|
282 mode-name "Telnet"
|
|
283 comint-prompt-regexp telnet-prompt-pattern)
|
|
284 (use-local-map telnet-mode-map)
|
82
|
285 (set (make-local-variable 'telnet-count) telnet-initial-count)
|
0
|
286 (run-hooks 'telnet-mode-hook))
|
|
287
|
|
288 ;;;###autoload (add-hook 'same-window-regexps "\\*rsh-[^-]*\\*\\(\\|<[0-9]*>\\)")
|
|
289
|
|
290 ;; Berkeley spawn of hell
|
|
291 ;;;###autoload
|
|
292 (defun rsh (host)
|
|
293 "Open a network login connection to host named HOST (a string).
|
|
294 Communication with HOST is recorded in a buffer `*rsh-HOST*'.
|
|
295 Normally input is edited in Emacs and sent a line at a time.
|
|
296 See also `\\[telnet]'."
|
|
297 (interactive "sOpen rsh connection to host: ")
|
|
298 (require 'shell)
|
|
299 (let ((name (concat "rsh-" host)))
|
|
300 (pop-to-buffer (make-comint name remote-shell-program nil host))
|
70
|
301 (setq telnet-count telnet-initial-count)
|
0
|
302 ;;
|
|
303 ;; SunOS doesn't print "unix" in its rsh login banner, so let's get a
|
|
304 ;; reasonable default here. There do exist non-Unix machines which
|
|
305 ;; speak the rsh protocol, but let's hope they print their OS name
|
|
306 ;; when one connects.
|
|
307 ;;
|
|
308 (telnet-check-software-type-initialize "unix")
|
|
309 ;;
|
|
310 ;; I think we should use telnet-filter here instead of -initial-filter,
|
|
311 ;; because rsh generally doesn't prompt for a password, and gobbling the
|
|
312 ;; first line that contains "passw" is extremely antisocial. More
|
|
313 ;; antisocial than echoing a password, and more likely than connecting
|
|
314 ;; to a non-Unix rsh host these days...
|
|
315 ;;
|
82
|
316 ;; I disagree with the above. -sb
|
|
317 ;;
|
124
|
318 (set-process-filter (get-process name) (if rsh-eat-password-string
|
|
319 'telnet-initial-filter
|
|
320 'telnet-filter))
|
82
|
321 ;; (set-process-filter (get-process name) 'telnet-filter)
|
0
|
322 ;; run last so that hooks can change things.
|
|
323 (telnet-mode)))
|
|
324
|
|
325 (provide 'telnet)
|
|
326
|
|
327 ;;; telnet.el ends here
|