remove `declare' macro
[bpt/emacs.git] / lisp / net / telnet.el
CommitLineData
8749abea
GM
1;;; telnet.el --- run a telnet session from within an Emacs buffer
2
ba318903 3;; Copyright (C) 1985, 1988, 1992, 1994, 2001-2014 Free Software
ab422c4d 4;; Foundation, Inc.
8749abea
GM
5
6;; Author: William F. Schelter
34dc21db 7;; Maintainer: emacs-devel@gnu.org
299248aa 8;; Keywords: unix, comm
8749abea
GM
9
10;; This file is part of GNU Emacs.
11
874a927a 12;; GNU Emacs is free software: you can redistribute it and/or modify
8749abea 13;; it under the terms of the GNU General Public License as published by
874a927a
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
8749abea
GM
16
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.
21
22;; You should have received a copy of the GNU General Public License
874a927a 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
8749abea
GM
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
a1506d29 32;; type RET or LFD. C-c C-c sends a C-c to the remote immediately;
8749abea
GM
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.
58Each element has the form (HOSTNAME PROGRAM [LOGIN-NAME])
59HOSTNAME says which machine the element applies to.
60PROGRAM says which program to run, to talk to that machine.
61LOGIN-NAME, which is optional, says what to log in as on that machine.")
62
63(defvar telnet-new-line "\r")
a0310a6c
DN
64(defvar telnet-mode-map
65 (let ((map (nconc (make-sparse-keymap) comint-mode-map)))
16366259 66 (define-key map "\C-m" 'telnet-send-input)
57b6ae53 67 ;; (define-key map "\C-j" 'telnet-send-input)
a0310a6c
DN
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))
72
8749abea
GM
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."))
80
81(defvar telnet-count 0
82 "Number of output strings from telnet process while looking for password.")
83(make-variable-buffer-local 'telnet-count)
84
85(defvar telnet-program "telnet"
86 "Program to run to open a telnet connection.")
87
88(defvar telnet-initial-count -50
89 "Initial value of `telnet-count'. Should be set to the negative of the
90number of terminal writes telnet will make setting up the host connection.")
91
92(defvar telnet-maximum-count 4
93 "Maximum value `telnet-count' can have.
94After this many passes, we stop looking for initial setup data.
95Should be set to the number of terminal writes telnet will make
96rejecting one login and prompting again for a username and password.")
97
98(defun telnet-interrupt-subjob ()
8749abea 99 "Interrupt the program running through telnet on the remote host."
279dffd6 100 (interactive)
ff660307 101 (process-send-string nil telnet-interrupt-string))
8749abea
GM
102
103(defun telnet-c-z ()
104 (interactive)
ff660307 105 (process-send-string nil "\C-z"))
8749abea
GM
106
107(defun send-process-next-char ()
108 (interactive)
ff660307
JB
109 (process-send-string nil
110 (char-to-string
111 (let ((inhibit-quit t))
112 (prog1 (read-char)
113 (setq quit-flag nil))))))
8749abea 114
8749abea
GM
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))
129
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)
ff660307
JB
141 (process-send-string proc (concat (comint-read-noecho "Password: " t)
142 telnet-new-line))
8749abea
GM
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)))))))))
149
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)
732576c8 154 (if comint-input-sender-no-newline
ff78c721 155 (if (not (string-equal string ""))
732576c8
RS
156 (process-send-eof))
157 (comint-send-string proc telnet-new-line)))
8749abea
GM
158
159(defun telnet-filter (proc string)
a1562258 160 (with-current-buffer (process-buffer proc)
8749abea
GM
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)
a1562258 168 (insert string)
8749abea
GM
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))))))
184
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)))
192
8749abea 193;;;###autoload
8fb5d2f1 194(defun telnet (host &optional port)
8749abea 195 "Open a network login connection to host named HOST (a string).
8fb5d2f1
KS
196Optional arg PORT specifies alternative port to connect to.
197Interactively, use \\[universal-argument] prefix to be prompted for port number.
198
8749abea
GM
199Communication with HOST is recorded in a buffer `*PROGRAM-HOST*'
200where PROGRAM is the telnet program being used. This program
201is controlled by the contents of the global variable `telnet-host-properties',
202falling back on the value of the global variable `telnet-program'.
203Normally input is edited in Emacs and sent a line at a time."
8fb5d2f1
KS
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)))
8749abea
GM
211 (let* ((comint-delimiter-argument-list '(?\ ?\t))
212 (properties (cdr (assoc host telnet-host-properties)))
213 (telnet-program (if properties (car properties) telnet-program))
8fb5d2f1
KS
214 (hname (if port (concat host ":" port) host))
215 (name (concat telnet-program "-" (comint-arguments hname 0 nil) ))
8749abea
GM
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))
37ac18a3
CY
220 (switch-to-buffer (concat "*" name "*"))
221 (switch-to-buffer
8749abea
GM
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)
ff660307
JB
228 (process-send-string process (concat "open " host
229 (if port " " "") (or port "")
230 "\n"))
8749abea
GM
231 (telnet-mode)
232 (setq comint-input-sender 'telnet-simple-send)
233 (setq telnet-count telnet-initial-count))))
234
235(put 'telnet-mode 'mode-class 'special)
236
46554de4 237(define-derived-mode telnet-mode comint-mode "Telnet"
8749abea
GM
238 "This mode is for using telnet (or rsh) from a buffer to another host.
239It has most of the same commands as comint-mode.
240There is a variable ``telnet-interrupt-string'' which is the character
241sent to try to stop execution of a job on the remote host.
46554de4 242Data is sent to the remote host when RET is typed."
a1562258 243 (set (make-local-variable 'window-point-insertion-type) t)
3ff67968 244 (set (make-local-variable 'comint-prompt-regexp) telnet-prompt-pattern)
14f7b89d 245 (set (make-local-variable 'comint-use-prompt-regexp) t))
8749abea 246
8749abea
GM
247;;;###autoload
248(defun rsh (host)
249 "Open a network login connection to host named HOST (a string).
250Communication with HOST is recorded in a buffer `*rsh-HOST*'.
251Normally 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 )))
37ac18a3 255 (switch-to-buffer (make-comint name remote-shell-program nil host))
8749abea
GM
256 (set-process-filter (get-process name) 'telnet-initial-filter)
257 (telnet-mode)
258 (setq telnet-count -16)))
259
260(provide 'telnet)
261
262;;; telnet.el ends here