telnet.el 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. ;;; telnet.el --- run a telnet session from within an Emacs buffer
  2. ;; Copyright (C) 1985, 1988, 1992, 1994, 2001-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Author: William F. Schelter
  5. ;; Maintainer: FSF
  6. ;; Keywords: unix, comm
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This mode is intended to be used for telnet or rsh to a remote host;
  20. ;; `telnet' and `rsh' are the two entry points. Multiple telnet or rsh
  21. ;; sessions are supported.
  22. ;;
  23. ;; Normally, input is sent to the remote telnet/rsh line-by-line, as you
  24. ;; type RET or LFD. C-c C-c sends a C-c to the remote immediately;
  25. ;; C-c C-z sends C-z immediately. C-c C-q followed by any character
  26. ;; sends that character immediately.
  27. ;;
  28. ;; All RET characters are filtered out of the output coming back from the
  29. ;; remote system. The mode tries to do other useful translations based
  30. ;; on what it sees coming back from the other system before the password
  31. ;; query. It knows about UNIX, ITS, TOPS-20 and Explorer systems.
  32. ;;
  33. ;; You can use the global telnet-host-properties to associate a telnet
  34. ;; program and login name with each host you regularly telnet to.
  35. ;;; Code:
  36. ;; to do fix software types for lispm:
  37. ;; to eval current expression. Also to try to send escape keys correctly.
  38. ;; essentially we'll want the rubout-handler off.
  39. ;; filter is simplistic but should be okay for typical shell usage.
  40. ;; needs hacking if it is going to deal with asynchronous output in a sane
  41. ;; manner
  42. (require 'comint)
  43. (defvar telnet-host-properties ()
  44. "Specify which telnet program to use for particular hosts.
  45. Each element has the form (HOSTNAME PROGRAM [LOGIN-NAME])
  46. HOSTNAME says which machine the element applies to.
  47. PROGRAM says which program to run, to talk to that machine.
  48. LOGIN-NAME, which is optional, says what to log in as on that machine.")
  49. (defvar telnet-new-line "\r")
  50. (defvar telnet-mode-map
  51. (let ((map (nconc (make-sparse-keymap) comint-mode-map)))
  52. (define-key map "\C-m" 'telnet-send-input)
  53. ;; (define-key map "\C-j" 'telnet-send-input)
  54. (define-key map "\C-c\C-q" 'send-process-next-char)
  55. (define-key map "\C-c\C-c" 'telnet-interrupt-subjob)
  56. (define-key map "\C-c\C-z" 'telnet-c-z)
  57. map))
  58. (defvar telnet-prompt-pattern "^[^#$%>\n]*[#$%>] *")
  59. (defvar telnet-replace-c-g nil)
  60. (make-variable-buffer-local
  61. (defvar telnet-remote-echoes t
  62. "True if the telnet process will echo input."))
  63. (make-variable-buffer-local
  64. (defvar telnet-interrupt-string "\C-c" "String sent by C-c."))
  65. (defvar telnet-count 0
  66. "Number of output strings from telnet process while looking for password.")
  67. (make-variable-buffer-local 'telnet-count)
  68. (defvar telnet-program "telnet"
  69. "Program to run to open a telnet connection.")
  70. (defvar telnet-initial-count -50
  71. "Initial value of `telnet-count'. Should be set to the negative of the
  72. number of terminal writes telnet will make setting up the host connection.")
  73. (defvar telnet-maximum-count 4
  74. "Maximum value `telnet-count' can have.
  75. After this many passes, we stop looking for initial setup data.
  76. Should be set to the number of terminal writes telnet will make
  77. rejecting one login and prompting again for a username and password.")
  78. (defun telnet-interrupt-subjob ()
  79. "Interrupt the program running through telnet on the remote host."
  80. (interactive)
  81. (process-send-string nil telnet-interrupt-string))
  82. (defun telnet-c-z ()
  83. (interactive)
  84. (process-send-string nil "\C-z"))
  85. (defun send-process-next-char ()
  86. (interactive)
  87. (process-send-string nil
  88. (char-to-string
  89. (let ((inhibit-quit t))
  90. (prog1 (read-char)
  91. (setq quit-flag nil))))))
  92. ;;maybe should have a flag for when have found type
  93. (defun telnet-check-software-type-initialize (string)
  94. "Tries to put correct initializations in. Needs work."
  95. (let ((case-fold-search t))
  96. (cond ((string-match "unix" string)
  97. (setq telnet-prompt-pattern comint-prompt-regexp)
  98. (setq telnet-new-line "\n"))
  99. ((string-match "tops-20" string) ;;maybe add telnet-replace-c-g
  100. (setq telnet-prompt-pattern "[@>]*"))
  101. ((string-match "its" string)
  102. (setq telnet-prompt-pattern "^[^*>\n]*[*>] *"))
  103. ((string-match "explorer" string) ;;explorer telnet needs work
  104. (setq telnet-replace-c-g ?\n))))
  105. (setq comint-prompt-regexp telnet-prompt-pattern))
  106. (defun telnet-initial-filter (proc string)
  107. ;For reading up to and including password; also will get machine type.
  108. (save-current-buffer
  109. (set-buffer (process-buffer proc))
  110. (let ((case-fold-search t))
  111. (cond ((string-match "No such host" string)
  112. (kill-buffer (process-buffer proc))
  113. (error "No such host"))
  114. ((string-match "passw" string)
  115. (telnet-filter proc string)
  116. (setq telnet-count 0)
  117. (process-send-string proc (concat (comint-read-noecho "Password: " t)
  118. telnet-new-line))
  119. (clear-this-command-keys))
  120. (t (telnet-check-software-type-initialize string)
  121. (telnet-filter proc string)
  122. (cond ((> telnet-count telnet-maximum-count)
  123. (set-process-filter proc 'telnet-filter))
  124. (t (setq telnet-count (1+ telnet-count)))))))))
  125. ;; Identical to comint-simple-send, except that it sends telnet-new-line
  126. ;; instead of "\n".
  127. (defun telnet-simple-send (proc string)
  128. (comint-send-string proc string)
  129. (if comint-input-sender-no-newline
  130. (if (not (string-equal string ""))
  131. (process-send-eof))
  132. (comint-send-string proc telnet-new-line)))
  133. (defun telnet-filter (proc string)
  134. (with-current-buffer (process-buffer proc)
  135. (let* ((last-insertion (marker-position (process-mark proc)))
  136. (delta (- (point) last-insertion))
  137. (ie (and comint-last-input-end
  138. (marker-position comint-last-input-end)))
  139. (w (get-buffer-window (current-buffer)))
  140. (ws (and w (window-start w))))
  141. (goto-char last-insertion)
  142. (insert string)
  143. (set-marker comint-last-output-start last-insertion)
  144. (set-marker (process-mark proc) (point))
  145. (if ws (set-window-start w ws t))
  146. (if ie (set-marker comint-last-input-end ie))
  147. (while (progn (skip-chars-backward "^\C-m" last-insertion)
  148. (> (point) last-insertion))
  149. (delete-region (1- (point)) (point)))
  150. (goto-char (process-mark proc))
  151. (and telnet-replace-c-g
  152. (subst-char-in-region last-insertion (point) ?\C-g
  153. telnet-replace-c-g t))
  154. ;; If point is after the insertion place, move it
  155. ;; along with the text.
  156. (if (> delta 0)
  157. (goto-char (+ (process-mark proc) delta))))))
  158. (defun telnet-send-input ()
  159. (interactive)
  160. ; (comint-send-input telnet-new-line telnet-remote-echoes)
  161. (comint-send-input)
  162. (if telnet-remote-echoes
  163. (delete-region comint-last-input-start
  164. comint-last-input-end)))
  165. ;;;###autoload
  166. (defun telnet (host &optional port)
  167. "Open a network login connection to host named HOST (a string).
  168. Optional arg PORT specifies alternative port to connect to.
  169. Interactively, use \\[universal-argument] prefix to be prompted for port number.
  170. Communication with HOST is recorded in a buffer `*PROGRAM-HOST*'
  171. where PROGRAM is the telnet program being used. This program
  172. is controlled by the contents of the global variable `telnet-host-properties',
  173. falling back on the value of the global variable `telnet-program'.
  174. Normally input is edited in Emacs and sent a line at a time."
  175. (interactive (list (read-string "Open connection to host: ")
  176. (cond
  177. ((null current-prefix-arg) nil)
  178. ((consp current-prefix-arg) (read-string "Port: "))
  179. (t (prefix-numeric-value current-prefix-arg)))))
  180. (if (and port (numberp port))
  181. (setq port (int-to-string port)))
  182. (let* ((comint-delimiter-argument-list '(?\ ?\t))
  183. (properties (cdr (assoc host telnet-host-properties)))
  184. (telnet-program (if properties (car properties) telnet-program))
  185. (hname (if port (concat host ":" port) host))
  186. (name (concat telnet-program "-" (comint-arguments hname 0 nil) ))
  187. (buffer (get-buffer (concat "*" name "*")))
  188. (telnet-options (if (cdr properties) (cons "-l" (cdr properties))))
  189. process)
  190. (if (and buffer (get-buffer-process buffer))
  191. (switch-to-buffer (concat "*" name "*"))
  192. (switch-to-buffer
  193. (apply 'make-comint name telnet-program nil telnet-options))
  194. (setq process (get-buffer-process (current-buffer)))
  195. (set-process-filter process 'telnet-initial-filter)
  196. ;; Don't send the `open' cmd till telnet is ready for it.
  197. (accept-process-output process)
  198. (erase-buffer)
  199. (process-send-string process (concat "open " host
  200. (if port " " "") (or port "")
  201. "\n"))
  202. (telnet-mode)
  203. (setq comint-input-sender 'telnet-simple-send)
  204. (setq telnet-count telnet-initial-count))))
  205. (put 'telnet-mode 'mode-class 'special)
  206. (define-derived-mode telnet-mode comint-mode "Telnet"
  207. "This mode is for using telnet (or rsh) from a buffer to another host.
  208. It has most of the same commands as comint-mode.
  209. There is a variable ``telnet-interrupt-string'' which is the character
  210. sent to try to stop execution of a job on the remote host.
  211. Data is sent to the remote host when RET is typed."
  212. (set (make-local-variable 'window-point-insertion-type) t)
  213. (set (make-local-variable 'comint-prompt-regexp) telnet-prompt-pattern)
  214. (set (make-local-variable 'comint-use-prompt-regexp) t))
  215. ;;;###autoload
  216. (defun rsh (host)
  217. "Open a network login connection to host named HOST (a string).
  218. Communication with HOST is recorded in a buffer `*rsh-HOST*'.
  219. Normally input is edited in Emacs and sent a line at a time."
  220. (interactive "sOpen rsh connection to host: ")
  221. (require 'shell)
  222. (let ((name (concat "rsh-" host )))
  223. (switch-to-buffer (make-comint name remote-shell-program nil host))
  224. (set-process-filter (get-process name) 'telnet-initial-filter)
  225. (telnet-mode)
  226. (setq telnet-count -16)))
  227. (provide 'telnet)
  228. ;;; telnet.el ends here