123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- (require 'comint)
- (defvar telnet-host-properties ()
- "Specify which telnet program to use for particular hosts.
- Each element has the form (HOSTNAME PROGRAM [LOGIN-NAME])
- HOSTNAME says which machine the element applies to.
- PROGRAM says which program to run, to talk to that machine.
- LOGIN-NAME, which is optional, says what to log in as on that machine.")
- (defvar telnet-new-line "\r")
- (defvar telnet-mode-map
- (let ((map (nconc (make-sparse-keymap) comint-mode-map)))
- (define-key map "\C-m" 'telnet-send-input)
-
- (define-key map "\C-c\C-q" 'send-process-next-char)
- (define-key map "\C-c\C-c" 'telnet-interrupt-subjob)
- (define-key map "\C-c\C-z" 'telnet-c-z)
- map))
- (defvar telnet-prompt-pattern "^[^#$%>\n]*[#$%>] *")
- (defvar telnet-replace-c-g nil)
- (make-variable-buffer-local
- (defvar telnet-remote-echoes t
- "True if the telnet process will echo input."))
- (make-variable-buffer-local
- (defvar telnet-interrupt-string "\C-c" "String sent by C-c."))
- (defvar telnet-count 0
- "Number of output strings from telnet process while looking for password.")
- (make-variable-buffer-local 'telnet-count)
- (defvar telnet-program "telnet"
- "Program to run to open a telnet connection.")
- (defvar telnet-initial-count -50
- "Initial value of `telnet-count'. Should be set to the negative of the
- number of terminal writes telnet will make setting up the host connection.")
- (defvar telnet-maximum-count 4
- "Maximum value `telnet-count' can have.
- After this many passes, we stop looking for initial setup data.
- Should be set to the number of terminal writes telnet will make
- rejecting one login and prompting again for a username and password.")
- (defun telnet-interrupt-subjob ()
- "Interrupt the program running through telnet on the remote host."
- (interactive)
- (process-send-string nil telnet-interrupt-string))
- (defun telnet-c-z ()
- (interactive)
- (process-send-string nil "\C-z"))
- (defun send-process-next-char ()
- (interactive)
- (process-send-string nil
- (char-to-string
- (let ((inhibit-quit t))
- (prog1 (read-char)
- (setq quit-flag nil))))))
- (defun telnet-check-software-type-initialize (string)
- "Tries to put correct initializations in. Needs work."
- (let ((case-fold-search t))
- (cond ((string-match "unix" string)
- (setq telnet-prompt-pattern comint-prompt-regexp)
- (setq telnet-new-line "\n"))
- ((string-match "tops-20" string)
- (setq telnet-prompt-pattern "[@>]*"))
- ((string-match "its" string)
- (setq telnet-prompt-pattern "^[^*>\n]*[*>] *"))
- ((string-match "explorer" string)
- (setq telnet-replace-c-g ?\n))))
- (setq comint-prompt-regexp telnet-prompt-pattern))
- (defun telnet-initial-filter (proc string)
-
- (save-current-buffer
- (set-buffer (process-buffer proc))
- (let ((case-fold-search t))
- (cond ((string-match "No such host" string)
- (kill-buffer (process-buffer proc))
- (error "No such host"))
- ((string-match "passw" string)
- (telnet-filter proc string)
- (setq telnet-count 0)
- (process-send-string proc (concat (comint-read-noecho "Password: " t)
- telnet-new-line))
- (clear-this-command-keys))
- (t (telnet-check-software-type-initialize string)
- (telnet-filter proc string)
- (cond ((> telnet-count telnet-maximum-count)
- (set-process-filter proc 'telnet-filter))
- (t (setq telnet-count (1+ telnet-count)))))))))
- (defun telnet-simple-send (proc string)
- (comint-send-string proc string)
- (if comint-input-sender-no-newline
- (if (not (string-equal string ""))
- (process-send-eof))
- (comint-send-string proc telnet-new-line)))
- (defun telnet-filter (proc string)
- (with-current-buffer (process-buffer proc)
- (let* ((last-insertion (marker-position (process-mark proc)))
- (delta (- (point) last-insertion))
- (ie (and comint-last-input-end
- (marker-position comint-last-input-end)))
- (w (get-buffer-window (current-buffer)))
- (ws (and w (window-start w))))
- (goto-char last-insertion)
- (insert string)
- (set-marker comint-last-output-start last-insertion)
- (set-marker (process-mark proc) (point))
- (if ws (set-window-start w ws t))
- (if ie (set-marker comint-last-input-end ie))
- (while (progn (skip-chars-backward "^\C-m" last-insertion)
- (> (point) last-insertion))
- (delete-region (1- (point)) (point)))
- (goto-char (process-mark proc))
- (and telnet-replace-c-g
- (subst-char-in-region last-insertion (point) ?\C-g
- telnet-replace-c-g t))
-
-
- (if (> delta 0)
- (goto-char (+ (process-mark proc) delta))))))
- (defun telnet-send-input ()
- (interactive)
- (comint-send-input)
- (if telnet-remote-echoes
- (delete-region comint-last-input-start
- comint-last-input-end)))
- (defun telnet (host &optional port)
- "Open a network login connection to host named HOST (a string).
- Optional arg PORT specifies alternative port to connect to.
- Interactively, use \\[universal-argument] prefix to be prompted for port number.
- Communication with HOST is recorded in a buffer `*PROGRAM-HOST*'
- where PROGRAM is the telnet program being used. This program
- is controlled by the contents of the global variable `telnet-host-properties',
- falling back on the value of the global variable `telnet-program'.
- Normally input is edited in Emacs and sent a line at a time."
- (interactive (list (read-string "Open connection to host: ")
- (cond
- ((null current-prefix-arg) nil)
- ((consp current-prefix-arg) (read-string "Port: "))
- (t (prefix-numeric-value current-prefix-arg)))))
- (if (and port (numberp port))
- (setq port (int-to-string port)))
- (let* ((comint-delimiter-argument-list '(?\ ?\t))
- (properties (cdr (assoc host telnet-host-properties)))
- (telnet-program (if properties (car properties) telnet-program))
- (hname (if port (concat host ":" port) host))
- (name (concat telnet-program "-" (comint-arguments hname 0 nil) ))
- (buffer (get-buffer (concat "*" name "*")))
- (telnet-options (if (cdr properties) (cons "-l" (cdr properties))))
- process)
- (if (and buffer (get-buffer-process buffer))
- (switch-to-buffer (concat "*" name "*"))
- (switch-to-buffer
- (apply 'make-comint name telnet-program nil telnet-options))
- (setq process (get-buffer-process (current-buffer)))
- (set-process-filter process 'telnet-initial-filter)
-
- (accept-process-output process)
- (erase-buffer)
- (process-send-string process (concat "open " host
- (if port " " "") (or port "")
- "\n"))
- (telnet-mode)
- (setq comint-input-sender 'telnet-simple-send)
- (setq telnet-count telnet-initial-count))))
- (put 'telnet-mode 'mode-class 'special)
- (define-derived-mode telnet-mode comint-mode "Telnet"
- "This mode is for using telnet (or rsh) from a buffer to another host.
- It has most of the same commands as comint-mode.
- There is a variable ``telnet-interrupt-string'' which is the character
- sent to try to stop execution of a job on the remote host.
- Data is sent to the remote host when RET is typed."
- (set (make-local-variable 'window-point-insertion-type) t)
- (set (make-local-variable 'comint-prompt-regexp) telnet-prompt-pattern)
- (set (make-local-variable 'comint-use-prompt-regexp) t))
- (defun rsh (host)
- "Open a network login connection to host named HOST (a string).
- Communication with HOST is recorded in a buffer `*rsh-HOST*'.
- Normally input is edited in Emacs and sent a line at a time."
- (interactive "sOpen rsh connection to host: ")
- (require 'shell)
- (let ((name (concat "rsh-" host )))
- (switch-to-buffer (make-comint name remote-shell-program nil host))
- (set-process-filter (get-process name) 'telnet-initial-filter)
- (telnet-mode)
- (setq telnet-count -16)))
- (provide 'telnet)
|