1 ;;; telnet.el --- run a telnet session from within an Emacs buffer
3 ;;; Copyright (C) 1985, 1988, 1992 Free Software Foundation, Inc.
5 ;; Author: William F. Schelter
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 ;; This mode is intended to be used for telnet or rsh to a remode host;
27 ;; `telnet' and `rsh' are the two entry points. Multiple telnet or rsh
28 ;; sessions are supported.
30 ;; Normally, input is sent to the remote telnet/rsh line-by-line, as you
31 ;; type RET or LFD. C-c C-c sends a C-c to the remote immediately;
32 ;; C-c C-z sends C-z immediately. C-c C-q followed by any character
33 ;; sends that character immediately.
35 ;; All RET characters are filtered out of the output coming back from the
36 ;; remote system. The mode tries to do other useful translations based
37 ;; on what it sees coming back from the other system before the password
38 ;; query. It knows about UNIX, ITS, TOPS-20 and Explorer systems.
42 ;; to do fix software types for lispm:
43 ;; to eval current expression. Also to try to send escape keys correctly.
44 ;; essentially we'll want the rubout-handler off.
46 ;; filter is simplistic but should be okay for typical shell usage.
47 ;; needs hacking if it is going to deal with asynchronous output in a sane
52 (defvar telnet-new-line
"\r")
53 (defvar telnet-mode-map nil
)
54 (defvar telnet-prompt-pattern
"^[^#$%>]*[#$%>] *")
55 (defvar telnet-replace-c-g nil
)
56 (make-variable-buffer-local
57 (defvar telnet-remote-echoes t
58 "True if the telnet process will echo input."))
59 (make-variable-buffer-local
60 (defvar telnet-interrupt-string
"\C-c" "String sent by C-c."))
62 (defvar telnet-count
0
63 "Number of output strings read from the telnet process
64 while looking for the initial password.")
66 (defvar telnet-initial-count -
50
67 "Initial value of `telnet-count'. Should be set to the negative of the
68 number of terminal writes telnet will make setting up the host connection.")
70 (defvar telnet-maximum-count
4
71 "Maximum value `telnet-count' can have.
72 After this many passes, we stop looking for initial setup data.
73 Should be set to the number of terminal writes telnet will make
74 rejecting one login and prompting for the again for a username and password.")
76 (defun telnet-interrupt-subjob ()
78 "Interrupt the program running through telnet on the remote host."
79 (send-string nil telnet-interrupt-string
))
83 (send-string nil
"\C-z"))
85 (defun send-process-next-char ()
89 (let ((inhibit-quit t
))
91 (setq quit-flag nil
))))))
93 ; initialization on first load.
96 (setq telnet-mode-map
(copy-keymap comint-mode-map
))
97 (define-key telnet-mode-map
"\C-m" 'telnet-send-input
)
98 ; (define-key telnet-mode-map "\C-j" 'telnet-send-input)
99 (define-key telnet-mode-map
"\C-c\C-q" 'send-process-next-char
)
100 (define-key telnet-mode-map
"\C-c\C-c" 'telnet-interrupt-subjob
)
101 (define-key telnet-mode-map
"\C-c\C-z" 'telnet-c-z
))
103 ;;maybe should have a flag for when have found type
104 (defun telnet-check-software-type-initialize (string)
105 "Tries to put correct initializations in. Needs work."
106 (let ((case-fold-search t
))
107 (cond ((string-match "unix" string
)
108 (setq telnet-prompt-pattern comint-prompt-regexp
)
109 (setq telnet-new-line
"\n"))
110 ((string-match "tops-20" string
) ;;maybe add telnet-replace-c-g
111 (setq telnet-prompt-pattern
"[@>]*"))
112 ((string-match "its" string
)
113 (setq telnet-prompt-pattern
"^[^*>]*[*>] *"))
114 ((string-match "explorer" string
) ;;explorer telnet needs work
115 (setq telnet-replace-c-g ?
\n))))
116 (setq comint-prompt-regexp telnet-prompt-pattern
))
118 (defun telnet-initial-filter (proc string
)
119 ;For reading up to and including password; also will get machine type.
120 (cond ((string-match "No such host" string
)
121 (kill-buffer (process-buffer proc
))
122 (error "No such host."))
123 ((string-match "passw" string
)
124 (telnet-filter proc string
)
125 (let* ((echo-keystrokes 0)
126 (password (read-password)))
127 (setq telnet-count
0)
128 (send-string proc
(concat password telnet-new-line
))))
129 (t (telnet-check-software-type-initialize string
)
130 (telnet-filter proc string
)
131 (cond ((> telnet-count telnet-maximum-count
)
132 (set-process-filter proc
'telnet-filter
))
133 (t (setq telnet-count
(1+ telnet-count
)))))))
135 (defun telnet-filter (proc string
)
137 (and (eq (process-buffer proc
) (current-buffer))
138 (= (point) (point-max)))))
140 (set-buffer (process-buffer proc
))
141 (goto-char (process-mark proc
))
143 ;; Insert STRING, omitting all C-m characters.
145 (while (setq c-m
(string-match "\C-m" string index
))
146 (insert-before-markers (substring string index c-m
))
147 (setq index
(1+ c-m
)))
148 (insert-before-markers (substring string index
)))
149 (and telnet-replace-c-g
150 (subst-char-in-region now
(point) ?\C-g telnet-replace-c-g
)))
151 ; (if (and (integer-or-marker-p last-input-start)
152 ; (marker-position last-input-start)
153 ; telnet-remote-echoes)
154 ; (delete-region last-input-start last-input-end))
157 (goto-char (point-max)))))
159 (defun telnet-send-input ()
161 ; (comint-send-input telnet-new-line telnet-remote-echoes)
163 (if telnet-remote-echoes
164 (delete-region comint-last-input-start
165 comint-last-input-end
)))
169 "Open a network login connection to host named HOST (a string).
170 Communication with HOST is recorded in a buffer *HOST-telnet*.
171 Normally input is edited in Emacs and sent a line at a time."
172 (interactive "sOpen telnet connection to host: ")
173 (let ((name (concat arg
"-telnet" )))
174 (switch-to-buffer (make-comint name
"telnet"))
175 (set-process-filter (get-process name
) 'telnet-initial-filter
)
176 ;; Don't send the `open' cmd till telnet is ready for it.
177 (accept-process-output (get-process name
))
179 (send-string name
(concat "open " arg
"\n"))
181 (setq telnet-count telnet-initial-count
)))
183 (defun telnet-mode ()
184 "This mode is for using telnet (or rsh) from a buffer to another host.
185 It has most of the same commands as comint-mode.
186 There is a variable ``telnet-interrupt-string'' which is the character
187 sent to try to stop execution of a job on the remote host.
188 Data is sent to the remote host when RET is typed.
194 (setq major-mode
'telnet-mode
196 comint-prompt-regexp telnet-prompt-pattern
)
197 (use-local-map telnet-mode-map
)
198 (run-hooks 'telnet-mode-hook
))
202 "Open a network login connection to host named HOST (a string).
203 Communication with HOST is recorded in a buffer *HOST-rsh*.
204 Normally input is edited in Emacs and sent a line at a time."
205 (interactive "sOpen rsh connection to host: ")
207 (let ((name (concat arg
"-rsh" )))
208 (switch-to-buffer (make-comint name
"rsh"))
209 (set-process-filter (get-process name
) 'telnet-initial-filter
)
211 (setq telnet-count -
16)))
213 (defun read-password ()
215 (message "Reading password...")
216 (while (prog1 (not (memq (setq tem
(read-char)) '(?\C-m ?
\n ?\C-g
)))
217 (setq quit-flag nil
))
218 (setq answ
(concat answ
(char-to-string tem
))))
223 ;;; telnet.el ends here