1 ;;; telnet.el --- run a telnet session from within an Emacs buffer
3 ;; Copyright (C) 1985, 1988, 1992, 1994, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: William F. Schelter
8 ;; Keywords: unix, comm
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This mode is intended to be used for telnet or rsh to a remote host;
28 ;; `telnet' and `rsh' are the two entry points. Multiple telnet or rsh
29 ;; sessions are supported.
31 ;; Normally, input is sent to the remote telnet/rsh line-by-line, as you
32 ;; type RET or LFD. C-c C-c sends a C-c to the remote immediately;
33 ;; C-c C-z sends C-z immediately. C-c C-q followed by any character
34 ;; sends that character immediately.
36 ;; All RET characters are filtered out of the output coming back from the
37 ;; remote system. The mode tries to do other useful translations based
38 ;; on what it sees coming back from the other system before the password
39 ;; query. It knows about UNIX, ITS, TOPS-20 and Explorer systems.
41 ;; You can use the global telnet-host-properties to associate a telnet
42 ;; program and login name with each host you regularly telnet to.
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.
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
56 (defvar telnet-host-properties
()
57 "Specify which telnet program to use for particular hosts.
58 Each element has the form (HOSTNAME PROGRAM [LOGIN-NAME])
59 HOSTNAME says which machine the element applies to.
60 PROGRAM says which program to run, to talk to that machine.
61 LOGIN-NAME, which is optional, says what to log in as on that machine.")
63 (defvar telnet-new-line
"\r")
64 (defvar telnet-mode-map nil
)
65 (defvar telnet-prompt-pattern
"^[^#$%>\n]*[#$%>] *")
66 (defvar telnet-replace-c-g nil
)
67 (make-variable-buffer-local
68 (defvar telnet-remote-echoes t
69 "True if the telnet process will echo input."))
70 (make-variable-buffer-local
71 (defvar telnet-interrupt-string
"\C-c" "String sent by C-c."))
73 (defvar telnet-count
0
74 "Number of output strings from telnet process while looking for password.")
75 (make-variable-buffer-local 'telnet-count
)
77 (defvar telnet-program
"telnet"
78 "Program to run to open a telnet connection.")
80 (defvar telnet-initial-count -
50
81 "Initial value of `telnet-count'. Should be set to the negative of the
82 number of terminal writes telnet will make setting up the host connection.")
84 (defvar telnet-maximum-count
4
85 "Maximum value `telnet-count' can have.
86 After this many passes, we stop looking for initial setup data.
87 Should be set to the number of terminal writes telnet will make
88 rejecting one login and prompting again for a username and password.")
90 (defun telnet-interrupt-subjob ()
91 "Interrupt the program running through telnet on the remote host."
93 (process-send-string nil telnet-interrupt-string
))
97 (process-send-string nil
"\C-z"))
99 (defun send-process-next-char ()
101 (process-send-string nil
103 (let ((inhibit-quit t
))
105 (setq quit-flag nil
))))))
107 ; initialization on first load.
110 (setq telnet-mode-map
(nconc (make-sparse-keymap) comint-mode-map
))
111 (define-key telnet-mode-map
"\C-m" 'telnet-send-input
)
112 ; (define-key telnet-mode-map "\C-j" 'telnet-send-input)
113 (define-key telnet-mode-map
"\C-c\C-q" 'send-process-next-char
)
114 (define-key telnet-mode-map
"\C-c\C-c" 'telnet-interrupt-subjob
)
115 (define-key telnet-mode-map
"\C-c\C-z" 'telnet-c-z
))
117 ;;maybe should have a flag for when have found type
118 (defun telnet-check-software-type-initialize (string)
119 "Tries to put correct initializations in. Needs work."
120 (let ((case-fold-search t
))
121 (cond ((string-match "unix" string
)
122 (setq telnet-prompt-pattern comint-prompt-regexp
)
123 (setq telnet-new-line
"\n"))
124 ((string-match "tops-20" string
) ;;maybe add telnet-replace-c-g
125 (setq telnet-prompt-pattern
"[@>]*"))
126 ((string-match "its" string
)
127 (setq telnet-prompt-pattern
"^[^*>\n]*[*>] *"))
128 ((string-match "explorer" string
) ;;explorer telnet needs work
129 (setq telnet-replace-c-g ?
\n))))
130 (setq comint-prompt-regexp telnet-prompt-pattern
))
132 (defun telnet-initial-filter (proc string
)
133 ;For reading up to and including password; also will get machine type.
135 (set-buffer (process-buffer proc
))
136 (let ((case-fold-search t
))
137 (cond ((string-match "No such host" string
)
138 (kill-buffer (process-buffer proc
))
139 (error "No such host"))
140 ((string-match "passw" string
)
141 (telnet-filter proc string
)
142 (setq telnet-count
0)
143 (process-send-string proc
(concat (comint-read-noecho "Password: " t
)
145 (clear-this-command-keys))
146 (t (telnet-check-software-type-initialize string
)
147 (telnet-filter proc string
)
148 (cond ((> telnet-count telnet-maximum-count
)
149 (set-process-filter proc
'telnet-filter
))
150 (t (setq telnet-count
(1+ telnet-count
)))))))))
152 ;; Identical to comint-simple-send, except that it sends telnet-new-line
154 (defun telnet-simple-send (proc string
)
155 (comint-send-string proc string
)
156 (if comint-input-sender-no-newline
157 (if (not (string-equal string
""))
159 (comint-send-string proc telnet-new-line
)))
161 (defun telnet-filter (proc string
)
162 (with-current-buffer (process-buffer proc
)
163 (let* ((last-insertion (marker-position (process-mark proc
)))
164 (delta (- (point) last-insertion
))
165 (ie (and comint-last-input-end
166 (marker-position comint-last-input-end
)))
167 (w (get-buffer-window (current-buffer)))
168 (ws (and w
(window-start w
))))
169 (goto-char last-insertion
)
171 (set-marker comint-last-output-start last-insertion
)
172 (set-marker (process-mark proc
) (point))
173 (if ws
(set-window-start w ws t
))
174 (if ie
(set-marker comint-last-input-end ie
))
175 (while (progn (skip-chars-backward "^\C-m" last-insertion
)
176 (> (point) last-insertion
))
177 (delete-region (1- (point)) (point)))
178 (goto-char (process-mark proc
))
179 (and telnet-replace-c-g
180 (subst-char-in-region last-insertion
(point) ?\C-g
181 telnet-replace-c-g t
))
182 ;; If point is after the insertion place, move it
183 ;; along with the text.
185 (goto-char (+ (process-mark proc
) delta
))))))
187 (defun telnet-send-input ()
189 ; (comint-send-input telnet-new-line telnet-remote-echoes)
191 (if telnet-remote-echoes
192 (delete-region comint-last-input-start
193 comint-last-input-end
)))
195 ;;;###autoload (add-hook 'same-window-regexps "\\*telnet-.*\\*\\(\\|<[0-9]+>\\)")
198 (defun telnet (host &optional port
)
199 "Open a network login connection to host named HOST (a string).
200 Optional arg PORT specifies alternative port to connect to.
201 Interactively, use \\[universal-argument] prefix to be prompted for port number.
203 Communication with HOST is recorded in a buffer `*PROGRAM-HOST*'
204 where PROGRAM is the telnet program being used. This program
205 is controlled by the contents of the global variable `telnet-host-properties',
206 falling back on the value of the global variable `telnet-program'.
207 Normally input is edited in Emacs and sent a line at a time."
208 (interactive (list (read-string "Open connection to host: ")
210 ((null current-prefix-arg
) nil
)
211 ((consp current-prefix-arg
) (read-string "Port: "))
212 (t (prefix-numeric-value current-prefix-arg
)))))
213 (if (and port
(numberp port
))
214 (setq port
(int-to-string port
)))
215 (let* ((comint-delimiter-argument-list '(?\ ?
\t))
216 (properties (cdr (assoc host telnet-host-properties
)))
217 (telnet-program (if properties
(car properties
) telnet-program
))
218 (hname (if port
(concat host
":" port
) host
))
219 (name (concat telnet-program
"-" (comint-arguments hname
0 nil
) ))
220 (buffer (get-buffer (concat "*" name
"*")))
221 (telnet-options (if (cdr properties
) (cons "-l" (cdr properties
))))
223 (if (and buffer
(get-buffer-process buffer
))
224 (pop-to-buffer (concat "*" name
"*"))
226 (apply 'make-comint name telnet-program nil telnet-options
))
227 (setq process
(get-buffer-process (current-buffer)))
228 (set-process-filter process
'telnet-initial-filter
)
229 ;; Don't send the `open' cmd till telnet is ready for it.
230 (accept-process-output process
)
232 (process-send-string process
(concat "open " host
233 (if port
" " "") (or port
"")
236 (setq comint-input-sender
'telnet-simple-send
)
237 (setq telnet-count telnet-initial-count
))))
239 (put 'telnet-mode
'mode-class
'special
)
241 (define-derived-mode telnet-mode comint-mode
"Telnet"
242 "This mode is for using telnet (or rsh) from a buffer to another host.
243 It has most of the same commands as comint-mode.
244 There is a variable ``telnet-interrupt-string'' which is the character
245 sent to try to stop execution of a job on the remote host.
246 Data is sent to the remote host when RET is typed."
247 (set (make-local-variable 'window-point-insertion-type
) t
)
248 (set (make-local-variable 'comint-prompt-regexp
) telnet-prompt-pattern
)
249 (set (make-local-variable 'comint-use-prompt-regexp
) t
))
251 ;;;###autoload (add-hook 'same-window-regexps "\\*rsh-[^-]*\\*\\(\\|<[0-9]*>\\)")
255 "Open a network login connection to host named HOST (a string).
256 Communication with HOST is recorded in a buffer `*rsh-HOST*'.
257 Normally input is edited in Emacs and sent a line at a time."
258 (interactive "sOpen rsh connection to host: ")
260 (let ((name (concat "rsh-" host
)))
261 (pop-to-buffer (make-comint name remote-shell-program nil host
))
262 (set-process-filter (get-process name
) 'telnet-initial-filter
)
264 (setq telnet-count -
16)))
268 ;; arch-tag: 98218821-d04a-48b6-9058-57d0d4677a56
269 ;;; telnet.el ends here