entered into RCS
[bpt/emacs.git] / lisp / telnet.el
CommitLineData
c88ab9ce 1;;; telnet.el --- run a telnet session from within an Emacs buffer
22a89ee8 2
58142744
ER
3;;; Copyright (C) 1985, 1988, 1992 Free Software Foundation, Inc.
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
21;; along with GNU Emacs; see the file COPYING. If not, write to
22;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
22a89ee8 24;;; Code:
1d23795a 25
26;;to do fix software types for lispm:
27;;to eval current expression. Also to try to send escape keys correctly.
28;;essentially we'll want the rubout-handler off.
29
30;; filter is simplistic but should be okay for typical shell usage.
31;; needs hacking if it is going to deal with asynchronous output in a sane
32;; manner
33
34(require 'comint)
492878e4 35
1d23795a 36(defvar telnet-new-line "\r")
37(defvar telnet-mode-map nil)
38(defvar telnet-prompt-pattern "^[^#$%>]*[#$%>] *")
39(defvar telnet-replace-c-g nil)
40(make-variable-buffer-local
41 (defvar telnet-remote-echoes t
42 "True if the telnet process will echo input."))
43(make-variable-buffer-local
44 (defvar telnet-interrupt-string "\C-c" "String sent by C-c."))
45
46(defvar telnet-count 0
47 "Number of output strings read from the telnet process
48while looking for the initial password.")
49
50(defvar telnet-initial-count -50
51 "Initial value of telnet-count. Should be set to the negative of the
52number of terminal writes telnet will make setting up the host connection.")
53
54(defvar telnet-maximum-count 4
55 "Maximum value telnet-count can have.
56After this many passes, we stop looking for initial setup data.
57Should be set to the number of terminal writes telnet will make
58rejecting one login and prompting for the again for a username and password.")
59
60(defun telnet-interrupt-subjob ()
61 (interactive)
62 "Interrupt the program running through telnet on the remote host."
63 (send-string nil telnet-interrupt-string))
64
65(defun telnet-c-z ()
66 (interactive)
67 (send-string nil "\C-z"))
68
69(defun send-process-next-char ()
70 (interactive)
71 (send-string nil
72 (char-to-string
73 (let ((inhibit-quit t))
74 (prog1 (read-char)
75 (setq quit-flag nil))))))
76
77; initialization on first load.
78(if telnet-mode-map
79 nil
80 (setq telnet-mode-map (copy-keymap comint-mode-map))
81 (define-key telnet-mode-map "\C-m" 'telnet-send-input)
82; (define-key telnet-mode-map "\C-j" 'telnet-send-input)
83 (define-key telnet-mode-map "\C-c\C-q" 'send-process-next-char)
84 (define-key telnet-mode-map "\C-c\C-c" 'telnet-interrupt-subjob)
85 (define-key telnet-mode-map "\C-c\C-z" 'telnet-c-z))
86
87;;maybe should have a flag for when have found type
88(defun telnet-check-software-type-initialize (string)
89 "Tries to put correct initializations in. Needs work."
90 (let ((case-fold-search t))
91 (cond ((string-match "unix" string)
92 (setq telnet-prompt-pattern comint-prompt-regexp)
93 (setq telnet-new-line "\n"))
94 ((string-match "tops-20" string) ;;maybe add telnet-replace-c-g
95 (setq telnet-prompt-pattern "[@>]*"))
96 ((string-match "its" string)
97 (setq telnet-prompt-pattern "^[^*>]*[*>] *"))
98 ((string-match "explorer" string) ;;explorer telnet needs work
99 (setq telnet-replace-c-g ?\n))))
100 (setq comint-prompt-regexp telnet-prompt-pattern))
101
102(defun telnet-initial-filter (proc string)
103 ;For reading up to and including password; also will get machine type.
104 (cond ((string-match "No such host" string)
105 (kill-buffer (process-buffer proc))
106 (error "No such host."))
107 ((string-match "passw" string)
108 (telnet-filter proc string)
109 (let* ((echo-keystrokes 0)
110 (password (read-password)))
111 (setq telnet-count 0)
112 (send-string proc (concat password telnet-new-line))))
113 (t (telnet-check-software-type-initialize string)
114 (telnet-filter proc string)
115 (cond ((> telnet-count telnet-maximum-count)
116 (set-process-filter proc 'telnet-filter))
117 (t (setq telnet-count (1+ telnet-count)))))))
118
119(defun telnet-filter (proc string)
120 (let ((at-end
121 (and (eq (process-buffer proc) (current-buffer))
122 (= (point) (point-max)))))
123 (save-excursion
124 (set-buffer (process-buffer proc))
125 (goto-char (process-mark proc))
126 (let ((now (point)))
492878e4 127 ;; Insert STRING, omitting all C-m characters.
1d23795a 128 (let ((index 0) c-m)
129 (while (setq c-m (string-match "\C-m" string index))
130 (insert-before-markers (substring string index c-m))
131 (setq index (1+ c-m)))
132 (insert-before-markers (substring string index)))
133 (and telnet-replace-c-g
134 (subst-char-in-region now (point) ?\C-g telnet-replace-c-g)))
135; (if (and (integer-or-marker-p last-input-start)
136; (marker-position last-input-start)
137; telnet-remote-echoes)
138; (delete-region last-input-start last-input-end))
139 )
140 (if at-end
141 (goto-char (point-max)))))
142
143(defun telnet-send-input ()
144 (interactive)
492878e4
JB
145; (comint-send-input telnet-new-line telnet-remote-echoes)
146 (comint-send-input)
147 (if telnet-remote-echoes
148 (delete-region comint-last-input-start
149 comint-last-input-end)))
1d23795a 150
f9f9507e 151;;;###autoload
1d23795a 152(defun telnet (arg)
153 "Open a network login connection to host named HOST (a string).
154Communication with HOST is recorded in a buffer *HOST-telnet*.
155Normally input is edited in Emacs and sent a line at a time."
156 (interactive "sOpen telnet connection to host: ")
157 (let ((name (concat arg "-telnet" )))
158 (switch-to-buffer (make-comint name "telnet"))
159 (set-process-filter (get-process name) 'telnet-initial-filter)
160 (erase-buffer)
161 (send-string name (concat "open " arg "\n"))
162 (telnet-mode)
163 (setq telnet-count telnet-initial-count)))
164
165(defun telnet-mode ()
166 "This mode is for use during telnet from a buffer to another
167host. It has most of the same commands as comint-mode.
168There is a variable ``telnet-interrupt-string'' which is the character
169sent to try to stop execution of a job on the remote host.
170Data is sent to the remote host when RET is typed.
171
172\\{telnet-mode-map}
173
174Bugs:
175--Replaces \r by a space, really should remove."
176 (interactive)
177 (comint-mode)
178 (setq major-mode 'telnet-mode
179 mode-name "Telnet"
180 comint-prompt-regexp telnet-prompt-pattern)
181 (use-local-map telnet-mode-map)
182 (run-hooks 'telnet-mode-hook))
183
184(defun read-password ()
185 (let ((answ "") tem)
186 (message "Reading password...")
94a861b5
JB
187 (while (prog1 (not (memq (setq tem (read-char)) '(?\C-m ?\n ?\C-g)))
188 (setq quit-flag nil))
1d23795a 189 (setq answ (concat answ (char-to-string tem))))
190 answ))
49116ac0
JB
191
192(provide 'telnet)
193
c88ab9ce 194;;; telnet.el ends here