]> code.delx.au - gnu-emacs/blob - lisp/telnet.el
(tar-mode): Position point on the name of the first file.
[gnu-emacs] / lisp / telnet.el
1 ;;; telnet.el --- run a telnet session from within an Emacs buffer
2
3 ;; Copyright (C) 1985, 1988, 1992, 1994 Free Software Foundation, Inc.
4
5 ;; Author: William F. Schelter
6 ;; Maintainer: FSF
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
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.
30 ;;
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.
35 ;;
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.
40 ;;
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.
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
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.")
62
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."))
72
73 (defvar telnet-count 0
74 "Number of output strings from telnet process while looking for password.")
75 (make-variable-buffer-local 'telnet-count)
76
77 (defvar telnet-program "telnet"
78 "Program to run to open a telnet connection.")
79
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.")
83
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.")
89
90 (defun telnet-interrupt-subjob ()
91 (interactive)
92 "Interrupt the program running through telnet on the remote host."
93 (send-string nil telnet-interrupt-string))
94
95 (defun telnet-c-z ()
96 (interactive)
97 (send-string nil "\C-z"))
98
99 (defun send-process-next-char ()
100 (interactive)
101 (send-string nil
102 (char-to-string
103 (let ((inhibit-quit t))
104 (prog1 (read-char)
105 (setq quit-flag nil))))))
106
107 ; initialization on first load.
108 (if telnet-mode-map
109 nil
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))
116
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))
131
132 (defun telnet-initial-filter (proc string)
133 ;For reading up to and including password; also will get machine type.
134 (save-current-buffer
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 (send-string proc (concat (comint-read-noecho "Password: " t)
144 telnet-new-line)))
145 (t (telnet-check-software-type-initialize string)
146 (telnet-filter proc string)
147 (cond ((> telnet-count telnet-maximum-count)
148 (set-process-filter proc 'telnet-filter))
149 (t (setq telnet-count (1+ telnet-count)))))))))
150
151 ;; Identical to comint-simple-send, except that it sends telnet-new-line
152 ;; instead of "\n".
153 (defun telnet-simple-send (proc string)
154 (comint-send-string proc string)
155 (comint-send-string proc telnet-new-line))
156
157 (defun telnet-filter (proc string)
158 (save-excursion
159 (set-buffer (process-buffer proc))
160 (let* ((last-insertion (marker-position (process-mark proc)))
161 (delta (- (point) last-insertion))
162 (ie (and comint-last-input-end
163 (marker-position comint-last-input-end)))
164 (w (get-buffer-window (current-buffer)))
165 (ws (and w (window-start w))))
166 (goto-char last-insertion)
167 (insert-before-markers string)
168 (set-marker comint-last-output-start last-insertion)
169 (set-marker (process-mark proc) (point))
170 (if ws (set-window-start w ws t))
171 (if ie (set-marker comint-last-input-end ie))
172 (while (progn (skip-chars-backward "^\C-m" last-insertion)
173 (> (point) last-insertion))
174 (delete-region (1- (point)) (point)))
175 (goto-char (process-mark proc))
176 (and telnet-replace-c-g
177 (subst-char-in-region last-insertion (point) ?\C-g
178 telnet-replace-c-g t))
179 ;; If point is after the insertion place, move it
180 ;; along with the text.
181 (if (> delta 0)
182 (goto-char (+ (process-mark proc) delta))))))
183
184 (defun telnet-send-input ()
185 (interactive)
186 ; (comint-send-input telnet-new-line telnet-remote-echoes)
187 (comint-send-input)
188 (if telnet-remote-echoes
189 (delete-region comint-last-input-start
190 comint-last-input-end)))
191
192 ;;;###autoload (add-hook 'same-window-regexps "\\*telnet-.*\\*\\(\\|<[0-9]+>\\)")
193
194 ;;;###autoload
195 (defun telnet (host)
196 "Open a network login connection to host named HOST (a string).
197 Communication with HOST is recorded in a buffer `*PROGRAM-HOST*'
198 where PROGRAM is the telnet program being used. This program
199 is controlled by the contents of the global variable `telnet-host-properties',
200 falling back on the value of the global variable `telnet-program'.
201 Normally input is edited in Emacs and sent a line at a time."
202 (interactive "sOpen connection to host: ")
203 (let* ((comint-delimiter-argument-list '(?\ ?\t))
204 (properties (cdr (assoc host telnet-host-properties)))
205 (telnet-program (if properties (car properties) telnet-program))
206 (name (concat telnet-program "-" (comint-arguments host 0 nil) ))
207 (buffer (get-buffer (concat "*" name "*")))
208 (telnet-options (if (cdr properties) (cons "-l" (cdr properties))))
209 process)
210 (if (and buffer (get-buffer-process buffer))
211 (pop-to-buffer (concat "*" name "*"))
212 (pop-to-buffer
213 (apply 'make-comint name telnet-program nil telnet-options))
214 (setq process (get-buffer-process (current-buffer)))
215 (set-process-filter process 'telnet-initial-filter)
216 ;; Don't send the `open' cmd till telnet is ready for it.
217 (accept-process-output process)
218 (erase-buffer)
219 (send-string process (concat "open " host "\n"))
220 (telnet-mode)
221 (setq comint-input-sender 'telnet-simple-send)
222 (setq telnet-count telnet-initial-count))))
223
224 (put 'telnet-mode 'mode-class 'special)
225
226 (defun telnet-mode ()
227 "This mode is for using telnet (or rsh) from a buffer to another host.
228 It has most of the same commands as comint-mode.
229 There is a variable ``telnet-interrupt-string'' which is the character
230 sent to try to stop execution of a job on the remote host.
231 Data is sent to the remote host when RET is typed.
232
233 \\{telnet-mode-map}
234 "
235 (interactive)
236 (comint-mode)
237 (setq major-mode 'telnet-mode
238 mode-name "Telnet"
239 comint-prompt-regexp telnet-prompt-pattern)
240 (use-local-map telnet-mode-map)
241 (run-hooks 'telnet-mode-hook))
242
243 ;;;###autoload (add-hook 'same-window-regexps "\\*rsh-[^-]*\\*\\(\\|<[0-9]*>\\)")
244
245 ;;;###autoload
246 (defun rsh (host)
247 "Open a network login connection to host named HOST (a string).
248 Communication with HOST is recorded in a buffer `*rsh-HOST*'.
249 Normally input is edited in Emacs and sent a line at a time."
250 (interactive "sOpen rsh connection to host: ")
251 (require 'shell)
252 (let ((name (concat "rsh-" host )))
253 (pop-to-buffer (make-comint name remote-shell-program nil host))
254 (set-process-filter (get-process name) 'telnet-initial-filter)
255 (telnet-mode)
256 (setq telnet-count -16)))
257
258 (provide 'telnet)
259
260 ;;; telnet.el ends here