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