telnet.el 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. ;; Copyright (C) 1985 Free Software Foundation
  2. ;; This file is part of GNU Emacs.
  3. ;; GNU Emacs is distributed in the hope that it will be useful,
  4. ;; but WITHOUT ANY WARRANTY. No author or distributor
  5. ;; accepts responsibility to anyone for the consequences of using it
  6. ;; or for whether it serves any particular purpose or works at all,
  7. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  8. ;; License for full details.
  9. ;; Everyone is granted permission to copy, modify and redistribute
  10. ;; GNU Emacs, but only under the conditions described in the
  11. ;; GNU Emacs General Public License. A copy of this license is
  12. ;; supposed to have been given to you along with GNU Emacs so you
  13. ;; can know your rights and responsibilities. It should be in a
  14. ;; file named COPYING. Among other things, the copyright notice
  15. ;; and this notice must be preserved on all copies.
  16. ;; Author William F. Schelter
  17. ;;to do fix software types for lispm:
  18. ;;to eval current expression. Also to try to send escape keys correctly.
  19. ;;essentially we'll want the rubout-handler off.
  20. (defvar telnet-new-line "\r")
  21. (defvar telnet-mode-map nil)
  22. (defvar telnet-prompt-pattern "^[^#$%>]*[#$%>] *")
  23. (defvar telnet-interrupt-string "\^c" "String sent by C-c.")
  24. (defvar telnet-count 0)
  25. (defvar telnet-replace-c-g nil)
  26. (defvar telnet-remote-echoes nil)
  27. (defun telnet-interrupt-subjob ()
  28. (interactive)
  29. "Interrupt the program running through telnet on the remote host."
  30. (send-string nil telnet-interrupt-string))
  31. (defun telnet-c-z ()
  32. (interactive)
  33. (send-string nil ""))
  34. (defun send-process-next-char ()
  35. (interactive)
  36. (send-string nil
  37. (char-to-string
  38. (let ((inhibit-quit t))
  39. (prog1 (read-char)
  40. (setq quit-flag nil))))))
  41. (if telnet-mode-map
  42. nil
  43. (setq telnet-mode-map (make-sparse-keymap))
  44. (define-key telnet-mode-map "\C-m" 'telnet-send-input)
  45. (define-key telnet-mode-map "\C-c\C-d" 'shell-send-eof)
  46. (define-key telnet-mode-map "\C-c\C-q" 'send-process-next-char)
  47. (define-key telnet-mode-map "\C-c\C-c" 'telnet-interrupt-subjob)
  48. (define-key telnet-mode-map "\C-c\C-z" 'telnet-c-z)
  49. (define-key telnet-mode-map "\C-c\C-u" 'kill-shell-input)
  50. (define-key telnet-mode-map "\C-c\C-w" 'backward-kill-word)
  51. (define-key telnet-mode-map "\C-c\C-o" 'kill-output-from-shell)
  52. (define-key telnet-mode-map "\C-c\C-r" 'show-output-from-shell)
  53. (define-key telnet-mode-map "\C-c\C-y" 'copy-last-shell-input))
  54. ;;maybe should have a flag for when have found type
  55. (defun telnet-check-software-type-initialize (string)
  56. "Tries to put correct initializations in. Needs work."
  57. (cond ((string-match "unix" string)
  58. (setq telnet-prompt-pattern shell-prompt-pattern)
  59. (setq telnet-new-line "\n"))
  60. ((string-match "tops-20" string) ;;maybe add telnet-replace-c-g
  61. (setq telnet-prompt-pattern "[@>]*"))
  62. ((string-match "its" string)
  63. (setq telnet-prompt-pattern "^[^*>]*[*>] *"))
  64. ((string-match "explorer" string) ;;explorer telnet needs work
  65. (setq telnet-replace-c-g ?\n))
  66. ))
  67. (defun telnet-initial-filter (proc string)
  68. ;For reading up to and including password; also will get machine type.
  69. (cond ((string-match "No such host" string)
  70. (kill-buffer (process-buffer proc))
  71. (error "No such host."))
  72. ((string-match "passw" string)
  73. (telnet-filter proc string)
  74. (setq password (read-password))
  75. (setq telnet-count 0)
  76. (send-string proc (concat password telnet-new-line)))
  77. (t (telnet-check-software-type-initialize string)
  78. (telnet-filter proc string)
  79. (cond ((> telnet-count 4)
  80. (set-process-filter proc 'telnet-filter))
  81. (t (setq telnet-count (1+ telnet-count)))))))
  82. (defun telnet-filter (proc string)
  83. (save-excursion
  84. (set-buffer (process-buffer proc))
  85. (goto-char (point-max))
  86. (let ((now (point)))
  87. (insert string)
  88. (subst-char-in-region now (point) ?\^m ?\ )
  89. (and telnet-replace-c-g (subst-char-in-region now (point) ?\^g telnet-replace-c-g)))
  90. (if (process-mark proc)
  91. (set-marker (process-mark proc) (point)))
  92. (if (and (integer-or-marker-p last-input-start)
  93. (marker-position last-input-start)
  94. telnet-remote-echoes)
  95. (delete-region last-input-start last-input-end)))
  96. (if (eq (process-buffer proc)
  97. (current-buffer))
  98. (goto-char (point-max))))
  99. (defun delete-char-or-send-eof (arg killp)
  100. "At end of buffer, send eof to subshell. Otherwise delete character."
  101. (interactive "p\nP")
  102. (if (and (eobp) (not killp))
  103. (process-send-eof)
  104. (delete-char arg killp)))
  105. (defun telnet-send-input ()
  106. "Send input to remote host
  107. At end of buffer, sends all text after last output
  108. as input to the telnet, including a telnet-new-line inserted at the end.
  109. Not at end, copies current line to the end of the buffer and sends it,
  110. after first attempting to discard any prompt at the beginning of the line
  111. by matching the regexp that is the value of telnet-prompt-pattern if possible."
  112. (interactive)
  113. (let (copied)
  114. (end-of-line)
  115. (if (eobp)
  116. (progn
  117. (move-marker last-input-start
  118. (process-mark (get-buffer-process (current-buffer))))
  119. (move-marker last-input-end (point)))
  120. (beginning-of-line)
  121. (re-search-forward telnet-prompt-pattern nil t)
  122. (let ((copy (buffer-substring (point)
  123. (progn (forward-line 1) (point)))))
  124. (goto-char (point-max))
  125. (move-marker last-input-start (point))
  126. (insert copy) (setq copied t)
  127. (move-marker last-input-end (point))))
  128. (save-excursion
  129. (goto-char last-input-start)
  130. (let ((process (get-buffer-process (current-buffer))))
  131. (send-region process last-input-start last-input-end)
  132. (if (not copied) (send-string process telnet-new-line))
  133. (set-marker (process-mark process) (point))))))
  134. (defun telnet (arg)
  135. "Open a network login connection to host named HOST (a string).
  136. Communication with HOST is recorded in a buffer *HOST-telnet*.
  137. Normally input is edited in Emacs and sent a line at a time."
  138. (interactive "sOpen telnet connection to host: ")
  139. (require 'shell)
  140. (let ((name (concat arg "-telnet" )))
  141. (switch-to-buffer (make-shell name "telnet"))
  142. (set-process-filter (get-process name) 'telnet-initial-filter)
  143. (erase-buffer)
  144. (send-string name (concat "open " arg "\n"))
  145. (telnet-mode)
  146. (setq telnet-count -16)))
  147. (defun read-password ()
  148. (let ((answ "") tem)
  149. (while (not (= (setq tem (read-char)) ?\^m))
  150. (setq answ (concat answ (char-to-string tem))))
  151. answ))
  152. (defun telnet-mode ()
  153. "This mode is for use during telnet from a buffer to another
  154. host. It has most of the same commands as shell mode.
  155. There is a variable `telnet-interrupt-string' which is the character
  156. sent to try to stop execution of a job on the remote host.
  157. Data is sent to the remote host when `return' is typed.
  158. Thus if you may need to edit the data before sending you
  159. should use c-n to move down a line. Then you can return
  160. to alter a previous line. Of course you should not use this
  161. mode of telnet if you want to run emacs like programs on the
  162. remote host (at least not yet!).
  163. The following commands imitate the usual Unix interrupt and
  164. editing control characters:
  165. \\{telnet-mode-map}
  166. Bugs:
  167. --Replace by a space, really should remove.
  168. --For Unix interacts poorly with tcsh although csh,sh,ksh are ok."
  169. (interactive)
  170. (kill-all-local-variables)
  171. (setq major-mode 'telnet-mode)
  172. (setq mode-name "Telnet")
  173. (setq mode-line-process '(": %s"))
  174. (make-local-variable 'last-input-start)
  175. (use-local-map telnet-mode-map)
  176. (let ((tem telnet-prompt-pattern))
  177. (make-local-variable 'telnet-prompt-pattern)
  178. (setq telnet-prompt-pattern tem))
  179. (make-local-variable 'telnet-interrupt-string)
  180. (setq telnet-interrupt-string "")
  181. (make-local-variable 'telnet-new-line)
  182. (setq telnet-new-line "\r")
  183. (make-local-variable 'last-input-start)
  184. (setq last-input-start (make-marker))
  185. (make-local-variable 'last-input-end)
  186. (setq last-input-end (make-marker))
  187. (make-local-variable 'telnet-remote-echoes)
  188. (setq telnet-remote-echoes t)
  189. (make-local-variable 'telnet-replace-c-g)
  190. (setq telnet-replace-c-g nil))