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