1 ;;; telnet.el --- run a telnet session from within an Emacs buffer
3 ;; Copyright (C) 1985, 1988, 1992, 1994 Free Software Foundation, Inc.
5 ;; Author: William F. Schelter
6 ;; Keywords: comm, unix
7 ;; Maintainer: Pete Ware <ware@cis.ohio-state.edu>
9 ;; This file is part of GNU Emacs.
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)
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.
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.
28 ;; This mode is intended to be used for telnet or rsh to a remote host;
29 ;; `telnet' and `rsh' are the two entry points. Multiple telnet or rsh
30 ;; sessions are supported.
32 ;; Normally, input is sent to the remote telnet/rsh line-by-line, as you
33 ;; type RET or LFD. C-c C-c sends a C-c to the remote immediately;
34 ;; C-c C-z sends C-z immediately. C-c C-q followed by any character
35 ;; sends that character immediately.
37 ;; All RET characters are filtered out of the output coming back from the
38 ;; remote system. The mode tries to do other useful translations based
39 ;; on what it sees coming back from the other system before the password
40 ;; query. It knows about UNIX, ITS, TOPS-20 and Explorer systems.
42 ;; You can use the global telnet-host-properties to associate a telnet
43 ;; program and login name with each host you regularly telnet to.
47 ;; to do fix software types for lispm:
48 ;; to eval current expression. Also to try to send escape keys correctly.
49 ;; essentially we'll want the rubout-handler off.
51 ;; filter is simplistic but should be okay for typical shell usage.
52 ;; needs hacking if it is going to deal with asynchronous output in a sane
58 "Run a telnet session from within an Emacs buffer."
61 (defvar telnet-host-properties ()
62 "Specify which telnet program to use for particular hosts.
63 Each element has the form (HOSTNAME PROGRAM [LOGIN-NAME])
64 HOSTNAME says which machine the element applies to.
65 PROGRAM says which program to run, to talk to that machine.
66 LOGIN-NAME, which is optional, says what to log in as on that machine.")
68 (defvar telnet-new-line "\r")
69 (defvar telnet-mode-map nil)
70 (defvar telnet-default-prompt-pattern "^[^#$%>\n]*[#$%>] *")
71 (defvar telnet-prompt-pattern telnet-default-prompt-pattern)
73 (defvar telnet-replace-c-g nil)
74 (make-variable-buffer-local
75 (defvar telnet-remote-echoes t
76 "True if the telnet process will echo input."))
77 (make-variable-buffer-local
78 (defvar telnet-interrupt-string "\C-c" "String sent by C-c."))
80 (defvar telnet-count 0
81 "Number of output strings read from the telnet process
82 while looking for the initial password.")
83 ;; (make-variable-buffer-local 'telnet-count)
85 (defcustom telnet-program "telnet"
86 "*Program to run to open a telnet connection."
90 (defcustom rsh-eat-password-string nil
91 "Non-nil means rsh will look for a string matching a password prompt."
95 (defvar telnet-initial-count -75
96 "Initial value of `telnet-count'. Should be set to the negative of the
97 number of terminal writes telnet will make setting up the host connection.")
99 (defvar telnet-maximum-count 4
100 "Maximum value `telnet-count' can have.
101 After this many passes, we stop looking for initial setup data.
102 Should be set to the number of terminal writes telnet will make
103 rejecting one login and prompting again for a username and password.")
105 (defun telnet-interrupt-subjob ()
107 "Interrupt the program running through telnet on the remote host."
108 (process-send-string nil telnet-interrupt-string))
112 (process-send-string nil "\C-z"))
114 ;; Keep telnet- prefix)
115 (defun telnet-send-process-next-char ()
117 (process-send-string nil
119 (let ((inhibit-quit t))
121 (setq quit-flag nil))))))
123 ; initialization on first load.
126 (setq telnet-mode-map (make-sparse-keymap))
127 (set-keymap-parent telnet-mode-map comint-mode-map)
128 (define-key telnet-mode-map "\C-m" 'telnet-send-input)
129 ; (define-key telnet-mode-map "\C-j" 'telnet-send-input)
130 (define-key telnet-mode-map "\C-c\C-q" 'telnet-send-process-next-char)
131 (define-key telnet-mode-map "\C-c\C-c" 'telnet-interrupt-subjob)
132 (define-key telnet-mode-map "\C-c\C-z" 'telnet-c-z))
134 ;;maybe should have a flag for when have found type
135 (defun telnet-check-software-type-initialize (string)
136 "Tries to put correct initializations in. Needs work."
137 (let ((case-fold-search t))
138 (cond ((string-match "unix" string)
139 (setq telnet-prompt-pattern shell-prompt-pattern)
140 (setq telnet-new-line "\n"))
141 ((string-match "tops-20" string) ;;maybe add telnet-replace-c-g
142 (setq telnet-prompt-pattern "[@>] *"))
143 ((string-match "its" string)
144 (setq telnet-prompt-pattern "^[^*>\n]*[*>] *"))
145 ((string-match "explorer" string) ;;explorer telnet needs work
146 (setq telnet-replace-c-g ?\n))))
147 (setq comint-prompt-regexp telnet-prompt-pattern))
149 (defun telnet-initial-filter (proc string)
150 (let ((case-fold-search t))
151 ;For reading up to and including password; also will get machine type.
152 (cond ((string-match "No such host" string)
153 (kill-buffer (process-buffer proc))
154 (error "No such host."))
155 ((string-match "passw" string)
156 (telnet-filter proc string)
157 (let ((password (comint-read-noecho "Password: " t)))
158 (setq telnet-count 0)
159 (process-send-string proc (concat password telnet-new-line))))
160 (t (telnet-check-software-type-initialize string)
161 (telnet-filter proc string)
162 (cond ((> telnet-count telnet-maximum-count)
163 ;; (set-process-filter proc 'telnet-filter) Kludge
164 ;; for shell-fonts -- this is the only mode that
165 ;; actually changes what its process filter is at
166 ;; run time, which confuses shell-font. So we
167 ;; special-case that here.
168 ;; #### Danger, knows an internal shell-font variable name.
169 (let ((old-filter (process-filter proc)))
170 (if (eq old-filter 'shell-font-process-filter)
171 (set (make-local-variable 'shell-font-process-filter)
173 (set-process-filter proc 'telnet-filter))))
174 (t (setq telnet-count (1+ telnet-count))))))))
176 ;; Identical to comint-simple-send, except that it sends telnet-new-line
178 (defun telnet-simple-send (proc string)
179 (comint-send-string proc string)
180 (comint-send-string proc telnet-new-line))
182 (defun telnet-filter (proc string)
184 (set-buffer (process-buffer proc))
186 (let* ((last-insertion (marker-position (process-mark proc)))
187 (delta (- (point) last-insertion))
188 (ie (and comint-last-input-end
189 (marker-position comint-last-input-end)))
190 (w (get-buffer-window (current-buffer)))
191 (ws (and w (window-start w))))
192 (goto-char last-insertion)
193 ;; Insert STRING, omitting all C-m characters.
194 (insert-before-markers string)
195 (set-marker (process-mark proc) (point))
196 ;; the insert-before-markers may have screwed window-start
197 ;; and likely moved comint-last-input-end. This is why the
198 ;; insertion-reaction should be a property of markers, not
199 ;; of the function which does the inserting.
200 (if ws (set-window-start w ws t))
201 (if ie (set-marker comint-last-input-end ie))
202 (while (progn (skip-chars-backward "^\C-m" last-insertion)
203 (> (point) last-insertion))
204 (delete-region (1- (point)) (point)))
205 (goto-char (process-mark proc))
206 (and telnet-replace-c-g
207 (subst-char-in-region last-insertion (point) ?\C-g
208 telnet-replace-c-g t))
209 ;; If point is after the insertion place, move it
210 ;; along with the text.
212 (goto-char (+ (process-mark proc) delta)))))))
214 (defun telnet-send-input ()
216 (let ((proc (get-buffer-process (current-buffer)))
218 (if (and telnet-remote-echoes
219 (>= (point) (process-mark proc)))
221 (if comint-eol-on-send (end-of-line))
222 (setq p1 (marker-position (process-mark proc))
226 ;; at this point, comint-send-input has moved the process mark, inserted
227 ;; a newline, and possibly inserted the (echoed) output. If the host is
228 ;; in remote-echo mode, then delete our local copy of the command, and
229 ;; the newline that comint-send-input sent.
231 (delete-region p1 (1+ p2))))))
233 ;;;###autoload (add-hook 'same-window-regexps "\\*telnet-.*\\*\\(\\|<[0-9]+>\\)")
236 (defun telnet (host &optional port)
237 "Open a network login connection to host named HOST (a string).
238 With a prefix argument, prompts for the port name or number as well.
239 Communication with HOST is recorded in a buffer `*PROGRAM-HOST*'
240 where PROGRAM is the telnet program being used. This program
241 is controlled by the contents of the global variable `telnet-host-properties',
242 falling back on the value of the global variable `telnet-program'.
243 Normally input is edited in Emacs and sent a line at a time.
245 (interactive (list (read-string "Open telnet connection to host: ")
246 (if current-prefix-arg
247 (read-string "Port name or number: ")
249 (let* ((comint-delimiter-argument-list '(?\ ?\t))
250 (properties (cdr (assoc host telnet-host-properties)))
251 (telnet-program (if properties (car properties) telnet-program))
252 (name (concat telnet-program "-" (comint-arguments host 0 nil) ))
253 (buffer (get-buffer (concat "*" name "*")))
254 (telnet-options (if (cdr properties) (cons "-l" (cdr properties))))
256 (if (and buffer (get-buffer-process buffer))
257 (pop-to-buffer buffer)
259 (apply 'make-comint name telnet-program nil telnet-options))
260 (setq process (get-buffer-process (current-buffer)))
261 (set-process-filter process 'telnet-initial-filter)
263 ;; SunOS and IRIX don't print "unix" in their rsh or telnet
264 ;; login banners, so let's get a reasonable default here.
265 ;; #### This patch from jwz mimics what is done in rsh done
266 ;; below. However, it (along with the one in rsh) mean that
267 ;; telnet-check-software-type-initialize is effectively a
268 ;; wastoid function. Reworking it like it claims to need is
269 ;; probably the better solution but I'm not going to do it.
271 (telnet-check-software-type-initialize "unix")
273 ;; Don't send the `open' cmd till telnet is ready for it.
274 (accept-process-output process)
276 (process-send-string process (concat "open " host
277 (if port (concat " " port) "")
279 (setq comint-input-sender 'telnet-simple-send)
280 (setq telnet-count telnet-initial-count)
283 (put 'telnet-mode 'mode-class 'special)
285 (defun telnet-mode ()
286 "This mode is for using telnet (or rsh) from a buffer to another host.
287 It has most of the same commands as comint-mode.
288 There is a variable ``telnet-interrupt-string'' which is the character
289 sent to try to stop execution of a job on the remote host.
290 Data is sent to the remote host when RET is typed.
296 (setq major-mode 'telnet-mode
298 comint-prompt-regexp telnet-prompt-pattern)
299 (use-local-map telnet-mode-map)
300 (set (make-local-variable 'telnet-count) telnet-initial-count)
301 (run-hooks 'telnet-mode-hook))
303 ;;;###autoload (add-hook 'same-window-regexps "\\*rsh-[^-]*\\*\\(\\|<[0-9]*>\\)")
307 "Open a network login connection to host named HOST (a string).
308 Communication with HOST is recorded in a buffer `*rsh-HOST*'.
309 Normally input is edited in Emacs and sent a line at a time.
310 See also `\\[telnet]'."
311 (interactive "sOpen rsh connection to host: ")
313 (let ((name (concat "rsh-" host )))
314 (pop-to-buffer (make-comint name remote-shell-program nil host))
315 (setq telnet-count telnet-initial-count)
317 ;; SunOS doesn't print "unix" in its rsh login banner, so let's get a
318 ;; reasonable default here. There do exist non-Unix machines which
319 ;; speak the rsh protocol, but let's hope they print their OS name
320 ;; when one connects.
322 (telnet-check-software-type-initialize "unix")
324 ;; I think we should use telnet-filter here instead of -initial-filter,
325 ;; because rsh generally doesn't prompt for a password, and gobbling the
326 ;; first line that contains "passw" is extremely antisocial. More
327 ;; antisocial than echoing a password, and more likely than connecting
328 ;; to a non-Unix rsh host these days...
330 ;; I disagree with the above. -sb
332 (set-process-filter (get-process name) (if rsh-eat-password-string
333 'telnet-initial-filter
335 ;; (set-process-filter (get-process name) 'telnet-filter)
336 ;; run last so that hooks can change things.
341 ;;; telnet.el ends here