Commit | Line | Data |
---|---|---|
c88ab9ce | 1 | ;;; telnet.el --- run a telnet session from within an Emacs buffer |
22a89ee8 | 2 | |
b578f267 | 3 | ;; Copyright (C) 1985, 1988, 1992, 1994 Free Software Foundation, Inc. |
58142744 | 4 | |
22a89ee8 ER |
5 | ;; Author: William F. Schelter |
6 | ;; Maintainer: FSF | |
1d23795a | 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 | |
492878e4 | 12 | ;; the Free Software Foundation; either version 2, or (at your option) |
1d23795a | 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 | |
b578f267 EN |
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. | |
1d23795a | 24 | |
b5014350 ER |
25 | ;;; Commentary: |
26 | ||
e259d5a3 | 27 | ;; This mode is intended to be used for telnet or rsh to a remote host; |
b5014350 ER |
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. | |
e259d5a3 RS |
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. | |
b5014350 | 43 | |
22a89ee8 | 44 | ;;; Code: |
1d23795a | 45 | |
b5014350 ER |
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. | |
1d23795a | 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) | |
492878e4 | 55 | |
9da7d084 | 56 | (defvar telnet-host-properties () |
e259d5a3 RS |
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 | ||
1d23795a | 63 | (defvar telnet-new-line "\r") |
64 | (defvar telnet-mode-map nil) | |
e3683fc7 | 65 | (defvar telnet-prompt-pattern "^[^#$%>\n]*[#$%>] *") |
1d23795a | 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 | |
f603e513 RS |
74 | "Number of output strings from telnet process while looking for password.") |
75 | (make-variable-buffer-local 'telnet-count) | |
76 | ||
c4ffc07f RS |
77 | (defvar telnet-program "telnet" |
78 | "Program to run to open a telnet connection.") | |
79 | ||
1d23795a | 80 | (defvar telnet-initial-count -50 |
1433a222 | 81 | "Initial value of `telnet-count'. Should be set to the negative of the |
1d23795a | 82 | number of terminal writes telnet will make setting up the host connection.") |
83 | ||
84 | (defvar telnet-maximum-count 4 | |
1433a222 | 85 | "Maximum value `telnet-count' can have. |
1d23795a | 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 | |
846731a5 | 88 | rejecting one login and prompting again for a username and password.") |
1d23795a | 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 | |
cab10b76 | 110 | (setq telnet-mode-map (nconc (make-sparse-keymap) comint-mode-map)) |
1d23795a | 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) | |
e3683fc7 | 127 | (setq telnet-prompt-pattern "^[^*>\n]*[*>] *")) |
1d23795a | 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. | |
e35832ba RS |
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) | |
e81fd1f7 | 141 | (telnet-filter proc string) |
e35832ba RS |
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))))))))) | |
1d23795a | 150 | |
3ed5409e RS |
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 | ||
1d23795a | 157 | (defun telnet-filter (proc string) |
e3683fc7 RS |
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 (process-mark proc) (point)) | |
169 | (if ws (set-window-start w ws t)) | |
170 | (if ie (set-marker comint-last-input-end ie)) | |
171 | (while (progn (skip-chars-backward "^\C-m" last-insertion) | |
172 | (> (point) last-insertion)) | |
173 | (delete-region (1- (point)) (point))) | |
1d23795a | 174 | (goto-char (process-mark proc)) |
e3683fc7 RS |
175 | (and telnet-replace-c-g |
176 | (subst-char-in-region last-insertion (point) ?\C-g | |
177 | telnet-replace-c-g t)) | |
178 | ;; If point is after the insertion place, move it | |
179 | ;; along with the text. | |
180 | (if (> delta 0) | |
181 | (goto-char (+ (process-mark proc) delta)))))) | |
1d23795a | 182 | |
183 | (defun telnet-send-input () | |
184 | (interactive) | |
492878e4 JB |
185 | ; (comint-send-input telnet-new-line telnet-remote-echoes) |
186 | (comint-send-input) | |
187 | (if telnet-remote-echoes | |
188 | (delete-region comint-last-input-start | |
189 | comint-last-input-end))) | |
1d23795a | 190 | |
f714317c | 191 | ;;;###autoload (add-hook 'same-window-regexps "\\*telnet-.*\\*\\(\\|<[0-9]+>\\)") |
f3453e58 | 192 | |
f9f9507e | 193 | ;;;###autoload |
901249a3 | 194 | (defun telnet (host) |
1d23795a | 195 | "Open a network login connection to host named HOST (a string). |
e259d5a3 RS |
196 | Communication with HOST is recorded in a buffer `*PROGRAM-HOST*' |
197 | where PROGRAM is the telnet program being used. This program | |
198 | is controlled by the contents of the global variable `telnet-host-properties', | |
199 | falling back on the value of the global variable `telnet-program'. | |
1d23795a | 200 | Normally input is edited in Emacs and sent a line at a time." |
e259d5a3 | 201 | (interactive "sOpen connection to host: ") |
0cf4932f | 202 | (let* ((comint-delimiter-argument-list '(?\ ?\t)) |
e259d5a3 RS |
203 | (properties (cdr (assoc host telnet-host-properties))) |
204 | (telnet-program (if properties (car properties) telnet-program)) | |
205 | (name (concat telnet-program "-" (comint-arguments host 0 nil) )) | |
f0ed29b2 | 206 | (buffer (get-buffer (concat "*" name "*"))) |
e259d5a3 | 207 | (telnet-options (if (cdr properties) (cons "-l" (cdr properties)))) |
f0ed29b2 | 208 | process) |
901249a3 | 209 | (if (and buffer (get-buffer-process buffer)) |
f3453e58 | 210 | (pop-to-buffer (concat "*" name "*")) |
e259d5a3 RS |
211 | (pop-to-buffer |
212 | (apply 'make-comint name telnet-program nil telnet-options)) | |
f0ed29b2 RS |
213 | (setq process (get-buffer-process (current-buffer))) |
214 | (set-process-filter process 'telnet-initial-filter) | |
901249a3 | 215 | ;; Don't send the `open' cmd till telnet is ready for it. |
f0ed29b2 | 216 | (accept-process-output process) |
901249a3 | 217 | (erase-buffer) |
f0ed29b2 | 218 | (send-string process (concat "open " host "\n")) |
901249a3 RS |
219 | (telnet-mode) |
220 | (setq comint-input-sender 'telnet-simple-send) | |
221 | (setq telnet-count telnet-initial-count)))) | |
1d23795a | 222 | |
7098039a RS |
223 | (put 'telnet-mode 'mode-class 'special) |
224 | ||
1d23795a | 225 | (defun telnet-mode () |
b5014350 | 226 | "This mode is for using telnet (or rsh) from a buffer to another host. |
1433a222 | 227 | It has most of the same commands as comint-mode. |
1d23795a | 228 | There is a variable ``telnet-interrupt-string'' which is the character |
229 | sent to try to stop execution of a job on the remote host. | |
230 | Data is sent to the remote host when RET is typed. | |
231 | ||
232 | \\{telnet-mode-map} | |
b5014350 | 233 | " |
1d23795a | 234 | (interactive) |
235 | (comint-mode) | |
236 | (setq major-mode 'telnet-mode | |
237 | mode-name "Telnet" | |
238 | comint-prompt-regexp telnet-prompt-pattern) | |
239 | (use-local-map telnet-mode-map) | |
240 | (run-hooks 'telnet-mode-hook)) | |
241 | ||
7d21d421 | 242 | ;;;###autoload (add-hook 'same-window-regexps "\\*rsh-[^-]*\\*\\(\\|<[0-9]*>\\)") |
f3453e58 | 243 | |
b5014350 | 244 | ;;;###autoload |
901249a3 | 245 | (defun rsh (host) |
b5014350 | 246 | "Open a network login connection to host named HOST (a string). |
7d21d421 | 247 | Communication with HOST is recorded in a buffer `*rsh-HOST*'. |
b5014350 ER |
248 | Normally input is edited in Emacs and sent a line at a time." |
249 | (interactive "sOpen rsh connection to host: ") | |
250 | (require 'shell) | |
7d21d421 | 251 | (let ((name (concat "rsh-" host ))) |
f3453e58 | 252 | (pop-to-buffer (make-comint name remote-shell-program nil host)) |
b5014350 ER |
253 | (set-process-filter (get-process name) 'telnet-initial-filter) |
254 | (telnet-mode) | |
255 | (setq telnet-count -16))) | |
256 | ||
49116ac0 JB |
257 | (provide 'telnet) |
258 | ||
c88ab9ce | 259 | ;;; telnet.el ends here |