Fix minor Y10k bugs.
[emacs.git] / lisp / net / telnet.el
blob00a556813c117490814ef49ae7517d1075302038
1 ;;; telnet.el --- run a telnet session from within an Emacs buffer
3 ;; Copyright (C) 1985, 1988, 1992, 1994, 2001-2012
4 ;; Free Software Foundation, Inc.
6 ;; Author: William F. Schelter
7 ;; Maintainer: FSF
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/>.
25 ;;; Commentary:
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.
44 ;;; Code:
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
52 ;; manner
54 (require 'comint)
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
65 (let ((map (nconc (make-sparse-keymap) comint-mode-map)))
66 (define-key map "\C-m" 'telnet-send-input)
67 ;; (define-key map "\C-j" 'telnet-send-input)
68 (define-key map "\C-c\C-q" 'send-process-next-char)
69 (define-key map "\C-c\C-c" 'telnet-interrupt-subjob)
70 (define-key map "\C-c\C-z" 'telnet-c-z)
71 map))
73 (defvar telnet-prompt-pattern "^[^#$%>\n]*[#$%>] *")
74 (defvar telnet-replace-c-g nil)
75 (make-variable-buffer-local
76 (defvar telnet-remote-echoes t
77 "True if the telnet process will echo input."))
78 (make-variable-buffer-local
79 (defvar telnet-interrupt-string "\C-c" "String sent by C-c."))
81 (defvar telnet-count 0
82 "Number of output strings from telnet process while looking for password.")
83 (make-variable-buffer-local 'telnet-count)
85 (defvar telnet-program "telnet"
86 "Program to run to open a telnet connection.")
88 (defvar telnet-initial-count -50
89 "Initial value of `telnet-count'. Should be set to the negative of the
90 number of terminal writes telnet will make setting up the host connection.")
92 (defvar telnet-maximum-count 4
93 "Maximum value `telnet-count' can have.
94 After this many passes, we stop looking for initial setup data.
95 Should be set to the number of terminal writes telnet will make
96 rejecting one login and prompting again for a username and password.")
98 (defun telnet-interrupt-subjob ()
99 "Interrupt the program running through telnet on the remote host."
100 (interactive)
101 (process-send-string nil telnet-interrupt-string))
103 (defun telnet-c-z ()
104 (interactive)
105 (process-send-string nil "\C-z"))
107 (defun send-process-next-char ()
108 (interactive)
109 (process-send-string nil
110 (char-to-string
111 (let ((inhibit-quit t))
112 (prog1 (read-char)
113 (setq quit-flag nil))))))
115 ;;maybe should have a flag for when have found type
116 (defun telnet-check-software-type-initialize (string)
117 "Tries to put correct initializations in. Needs work."
118 (let ((case-fold-search t))
119 (cond ((string-match "unix" string)
120 (setq telnet-prompt-pattern comint-prompt-regexp)
121 (setq telnet-new-line "\n"))
122 ((string-match "tops-20" string) ;;maybe add telnet-replace-c-g
123 (setq telnet-prompt-pattern "[@>]*"))
124 ((string-match "its" string)
125 (setq telnet-prompt-pattern "^[^*>\n]*[*>] *"))
126 ((string-match "explorer" string) ;;explorer telnet needs work
127 (setq telnet-replace-c-g ?\n))))
128 (setq comint-prompt-regexp telnet-prompt-pattern))
130 (defun telnet-initial-filter (proc string)
131 ;For reading up to and including password; also will get machine type.
132 (save-current-buffer
133 (set-buffer (process-buffer proc))
134 (let ((case-fold-search t))
135 (cond ((string-match "No such host" string)
136 (kill-buffer (process-buffer proc))
137 (error "No such host"))
138 ((string-match "passw" string)
139 (telnet-filter proc string)
140 (setq telnet-count 0)
141 (process-send-string proc (concat (comint-read-noecho "Password: " t)
142 telnet-new-line))
143 (clear-this-command-keys))
144 (t (telnet-check-software-type-initialize string)
145 (telnet-filter proc string)
146 (cond ((> telnet-count telnet-maximum-count)
147 (set-process-filter proc 'telnet-filter))
148 (t (setq telnet-count (1+ telnet-count)))))))))
150 ;; Identical to comint-simple-send, except that it sends telnet-new-line
151 ;; instead of "\n".
152 (defun telnet-simple-send (proc string)
153 (comint-send-string proc string)
154 (if comint-input-sender-no-newline
155 (if (not (string-equal string ""))
156 (process-send-eof))
157 (comint-send-string proc telnet-new-line)))
159 (defun telnet-filter (proc string)
160 (with-current-buffer (process-buffer proc)
161 (let* ((last-insertion (marker-position (process-mark proc)))
162 (delta (- (point) last-insertion))
163 (ie (and comint-last-input-end
164 (marker-position comint-last-input-end)))
165 (w (get-buffer-window (current-buffer)))
166 (ws (and w (window-start w))))
167 (goto-char last-insertion)
168 (insert string)
169 (set-marker comint-last-output-start last-insertion)
170 (set-marker (process-mark proc) (point))
171 (if ws (set-window-start w ws t))
172 (if ie (set-marker comint-last-input-end ie))
173 (while (progn (skip-chars-backward "^\C-m" last-insertion)
174 (> (point) last-insertion))
175 (delete-region (1- (point)) (point)))
176 (goto-char (process-mark proc))
177 (and telnet-replace-c-g
178 (subst-char-in-region last-insertion (point) ?\C-g
179 telnet-replace-c-g t))
180 ;; If point is after the insertion place, move it
181 ;; along with the text.
182 (if (> delta 0)
183 (goto-char (+ (process-mark proc) delta))))))
185 (defun telnet-send-input ()
186 (interactive)
187 ; (comint-send-input telnet-new-line telnet-remote-echoes)
188 (comint-send-input)
189 (if telnet-remote-echoes
190 (delete-region comint-last-input-start
191 comint-last-input-end)))
193 ;;;###autoload
194 (defun telnet (host &optional port)
195 "Open a network login connection to host named HOST (a string).
196 Optional arg PORT specifies alternative port to connect to.
197 Interactively, use \\[universal-argument] prefix to be prompted for port number.
199 Communication with HOST is recorded in a buffer `*PROGRAM-HOST*'
200 where PROGRAM is the telnet program being used. This program
201 is controlled by the contents of the global variable `telnet-host-properties',
202 falling back on the value of the global variable `telnet-program'.
203 Normally input is edited in Emacs and sent a line at a time."
204 (interactive (list (read-string "Open connection to host: ")
205 (cond
206 ((null current-prefix-arg) nil)
207 ((consp current-prefix-arg) (read-string "Port: "))
208 (t (prefix-numeric-value current-prefix-arg)))))
209 (if (and port (numberp port))
210 (setq port (int-to-string port)))
211 (let* ((comint-delimiter-argument-list '(?\ ?\t))
212 (properties (cdr (assoc host telnet-host-properties)))
213 (telnet-program (if properties (car properties) telnet-program))
214 (hname (if port (concat host ":" port) host))
215 (name (concat telnet-program "-" (comint-arguments hname 0 nil) ))
216 (buffer (get-buffer (concat "*" name "*")))
217 (telnet-options (if (cdr properties) (cons "-l" (cdr properties))))
218 process)
219 (if (and buffer (get-buffer-process buffer))
220 (switch-to-buffer (concat "*" name "*"))
221 (switch-to-buffer
222 (apply 'make-comint name telnet-program nil telnet-options))
223 (setq process (get-buffer-process (current-buffer)))
224 (set-process-filter process 'telnet-initial-filter)
225 ;; Don't send the `open' cmd till telnet is ready for it.
226 (accept-process-output process)
227 (erase-buffer)
228 (process-send-string process (concat "open " host
229 (if port " " "") (or port "")
230 "\n"))
231 (telnet-mode)
232 (setq comint-input-sender 'telnet-simple-send)
233 (setq telnet-count telnet-initial-count))))
235 (put 'telnet-mode 'mode-class 'special)
237 (define-derived-mode telnet-mode comint-mode "Telnet"
238 "This mode is for using telnet (or rsh) from a buffer to another host.
239 It has most of the same commands as comint-mode.
240 There is a variable ``telnet-interrupt-string'' which is the character
241 sent to try to stop execution of a job on the remote host.
242 Data is sent to the remote host when RET is typed."
243 (set (make-local-variable 'window-point-insertion-type) t)
244 (set (make-local-variable 'comint-prompt-regexp) telnet-prompt-pattern)
245 (set (make-local-variable 'comint-use-prompt-regexp) t))
247 ;;;###autoload
248 (defun rsh (host)
249 "Open a network login connection to host named HOST (a string).
250 Communication with HOST is recorded in a buffer `*rsh-HOST*'.
251 Normally input is edited in Emacs and sent a line at a time."
252 (interactive "sOpen rsh connection to host: ")
253 (require 'shell)
254 (let ((name (concat "rsh-" host )))
255 (switch-to-buffer (make-comint name remote-shell-program nil host))
256 (set-process-filter (get-process name) 'telnet-initial-filter)
257 (telnet-mode)
258 (setq telnet-count -16)))
260 (provide 'telnet)
262 ;;; telnet.el ends here