home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 31 / CDASC_31_1996_juillet_aout.iso / vrac_os2 / e31el3.zip / EMACS / 19.31 / LISP / RLOGIN.EL < prev    next >
Lisp/Scheme  |  1996-05-23  |  12KB  |  327 lines

  1. ;;; rlogin.el --- remote login interface
  2.  
  3. ;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Noah Friedman
  6. ;; Maintainer: Noah Friedman <friedman@prep.ai.mit.edu>
  7. ;; Keywords: unix, comm
  8.  
  9. ;; This file is part of GNU Emacs.
  10.  
  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.
  15.  
  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.
  20.  
  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 the
  23. ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  24. ;; Boston, MA 02111-1307, USA.
  25.  
  26. ;; $Id: rlogin.el,v 1.32 1996/05/08 00:52:30 friedman Exp $
  27.  
  28. ;;; Commentary:
  29.  
  30. ;; Support for remote logins using `rlogin'.
  31. ;; This program is layered on top of shell.el; the code here only accounts
  32. ;; for the variations needed to handle a remote process, e.g. directory
  33. ;; tracking and the sending of some special characters.
  34.  
  35. ;; If you wish for rlogin mode to prompt you in the minibuffer for
  36. ;; passwords when a password prompt appears, just enter m-x send-invisible
  37. ;; and type in your line, or add `comint-watch-for-password-prompt' to
  38. ;; `comint-output-filter-functions'.
  39.  
  40. ;;; Code:
  41.  
  42. (require 'comint)
  43. (require 'shell)
  44.  
  45. (defvar rlogin-program "rlogin"
  46.   "*Name of program to invoke rlogin")
  47.  
  48. (defvar rlogin-explicit-args nil
  49.   "*List of arguments to pass to rlogin on the command line.")
  50.  
  51. (defvar rlogin-mode-hook nil
  52.   "*Hooks to run after setting current buffer to rlogin-mode.")
  53.  
  54. (defvar rlogin-process-connection-type nil
  55.   "*If non-`nil', use a pty for the local rlogin process.
  56. If `nil', use a pipe (if pipes are supported on the local system).
  57.  
  58. Generally it is better not to waste ptys on systems which have a static
  59. number of them.  On the other hand, some implementations of `rlogin' assume
  60. a pty is being used, and errors will result from using a pipe instead.")
  61.  
  62. (defvar rlogin-directory-tracking-mode 'local
  63.   "*Control whether and how to do directory tracking in an rlogin buffer.
  64.  
  65. nil means don't do directory tracking.
  66.  
  67. t means do so using an ftp remote file name.
  68.  
  69. Any other value means do directory tracking using local file names.
  70. This works only if the remote machine and the local one
  71. share the same directories (through NFS).  This is the default.
  72.  
  73. This variable becomes local to a buffer when set in any fashion for it.
  74.  
  75. It is better to use the function of the same name to change the behavior of
  76. directory tracking in an rlogin session once it has begun, rather than
  77. simply setting this variable, since the function does the necessary
  78. re-synching of directories.")
  79.  
  80. (make-variable-buffer-local 'rlogin-directory-tracking-mode)
  81.  
  82. (defvar rlogin-host nil
  83.   "*The name of the remote host.  This variable is buffer-local.")
  84.  
  85. (defvar rlogin-remote-user nil
  86.   "*The username used on the remote host.
  87. This variable is buffer-local and defaults to your local user name.
  88. If rlogin is invoked with the `-l' option to specify the remote username,
  89. this variable is set from that.")
  90.  
  91. ;; Initialize rlogin mode map.
  92. (defvar rlogin-mode-map '())
  93. (cond
  94.  ((null rlogin-mode-map)
  95.   (setq rlogin-mode-map (if (consp shell-mode-map)
  96.                             (cons 'keymap shell-mode-map)
  97.                           (copy-keymap shell-mode-map)))
  98.   (define-key rlogin-mode-map "\C-c\C-c" 'rlogin-send-Ctrl-C)
  99.   (define-key rlogin-mode-map "\C-c\C-d" 'rlogin-send-Ctrl-D)
  100.   (define-key rlogin-mode-map "\C-c\C-z" 'rlogin-send-Ctrl-Z)
  101.   (define-key rlogin-mode-map "\C-c\C-\\" 'rlogin-send-Ctrl-backslash)
  102.   (define-key rlogin-mode-map "\C-d" 'rlogin-delchar-or-send-Ctrl-D)
  103.   (define-key rlogin-mode-map "\C-i" 'rlogin-tab-or-complete)))
  104.  
  105.  
  106. ;;;###autoload (add-hook 'same-window-regexps "^\\*rlogin-.*\\*\\(\\|<[0-9]+>\\)")
  107.  
  108. (defvar rlogin-history nil)
  109.  
  110. ;;;###autoload
  111. (defun rlogin (input-args &optional buffer)
  112.   "Open a network login connection to HOST via the `rlogin' program.
  113. Input is sent line-at-a-time to the remote connection.
  114.  
  115. Communication with the remote host is recorded in a buffer `*rlogin-HOST*'
  116. \(or `*rlogin-USER@HOST*' if the remote username differs\).
  117. If a prefix argument is given and the buffer `*rlogin-HOST*' already exists,
  118. a new buffer with a different connection will be made.
  119.  
  120. When called from a program, if the optional second argument is a string or
  121. buffer, it names the buffer to use.
  122.  
  123. The variable `rlogin-program' contains the name of the actual program to
  124. run.  It can be a relative or absolute path.
  125.  
  126. The variable `rlogin-explicit-args' is a list of arguments to give to
  127. the rlogin when starting.  They are added after any arguments given in
  128. INPUT-ARGS.
  129.  
  130. If the default value of `rlogin-directory-tracking-mode' is t, then the
  131. default directory in that buffer is set to a remote (FTP) file name to
  132. access your home directory on the remote machine.  Occasionally this causes
  133. an error, if you cannot access the home directory on that machine.  This
  134. error is harmless as long as you don't try to use that default directory.
  135.  
  136. If `rlogin-directory-tracking-mode' is neither t nor nil, then the default
  137. directory is initially set up to your (local) home directory.
  138. This is useful if the remote machine and your local machine
  139. share the same files via NFS.  This is the default.
  140.  
  141. If you wish to change directory tracking styles during a session, use the
  142. function `rlogin-directory-tracking-mode' rather than simply setting the
  143. variable."
  144.   (interactive (list
  145.         (read-from-minibuffer "rlogin arguments (hostname first): "
  146.                       nil nil nil 'rlogin-history)
  147.         current-prefix-arg))
  148.  
  149.   (let* ((process-connection-type rlogin-process-connection-type)
  150.          (args (if rlogin-explicit-args
  151.                    (append (rlogin-parse-words input-args)
  152.                            rlogin-explicit-args)
  153.                  (rlogin-parse-words input-args)))
  154.      (host (car args))
  155.      (user (or (car (cdr (member "-l" args)))
  156.                    (user-login-name)))
  157.          (buffer-name (if (string= user (user-login-name))
  158.                           (format "*rlogin-%s*" host)
  159.                         (format "*rlogin-%s@%s*" user host)))
  160.      proc)
  161.  
  162.     (cond ((null buffer))
  163.       ((stringp buffer)
  164.        (setq buffer-name buffer))
  165.           ((bufferp buffer)
  166.            (setq buffer-name (buffer-name buffer)))
  167.           ((numberp buffer)
  168.            (setq buffer-name (format "%s<%d>" buffer-name buffer)))
  169.           (t
  170.            (setq buffer-name (generate-new-buffer-name buffer-name))))
  171.  
  172.     (setq buffer (get-buffer-create buffer-name))
  173.     (pop-to-buffer buffer-name)
  174.  
  175.     (cond
  176.      ((comint-check-proc buffer-name))
  177.      (t
  178.       (comint-exec buffer buffer-name rlogin-program nil args)
  179.       (setq proc (get-buffer-process buffer))
  180.       ;; Set process-mark to point-max in case there is text in the
  181.       ;; buffer from a previous exited process.
  182.       (set-marker (process-mark proc) (point-max))
  183.  
  184.       ;; comint-output-filter-functions is just like a hook, except that the
  185.       ;; functions in that list are passed arguments.  add-hook serves well
  186.       ;; enough for modifying it.
  187.       (add-hook 'comint-output-filter-functions 'rlogin-carriage-filter)
  188.  
  189.       (rlogin-mode)
  190.  
  191.       (make-local-variable 'rlogin-host)
  192.       (setq rlogin-host host)
  193.       (make-local-variable 'rlogin-remote-user)
  194.       (setq rlogin-remote-user user)
  195.  
  196.       (cond
  197.        ((eq rlogin-directory-tracking-mode t)
  198.         ;; Do this here, rather than calling the tracking mode function, to
  199.         ;; avoid a gratuitous resync check; the default should be the
  200.         ;; user's home directory, be it local or remote.
  201.         (setq comint-file-name-prefix
  202.               (concat "/" rlogin-remote-user "@" rlogin-host ":"))
  203.         (cd-absolute comint-file-name-prefix))
  204.        ((null rlogin-directory-tracking-mode))
  205.        (t
  206.         (cd-absolute (concat comint-file-name-prefix "~/"))))))))
  207.  
  208. (defun rlogin-mode ()
  209.   "Set major-mode for rlogin sessions.
  210. If `rlogin-mode-hook' is set, run it."
  211.   (interactive)
  212.   (kill-all-local-variables)
  213.   (shell-mode)
  214.   (setq major-mode 'rlogin-mode)
  215.   (setq mode-name "rlogin")
  216.   (use-local-map rlogin-mode-map)
  217.   (setq shell-dirtrackp rlogin-directory-tracking-mode)
  218.   (make-local-variable 'comint-file-name-prefix)
  219.   (run-hooks 'rlogin-mode-hook))
  220.  
  221. (defun rlogin-directory-tracking-mode (&optional prefix)
  222.   "Do remote or local directory tracking, or disable entirely.
  223.  
  224. If called with no prefix argument or a unspecified prefix argument (just
  225. ``\\[universal-argument]'' with no number) do remote directory tracking via
  226. ange-ftp.  If called as a function, give it no argument.
  227.  
  228. If called with a negative prefix argument, disable directory tracking
  229. entirely.
  230.  
  231. If called with a positive, numeric prefix argument, e.g.
  232. ``\\[universal-argument] 1 M-x rlogin-directory-tracking-mode\'',
  233. then do directory tracking but assume the remote filesystem is the same as
  234. the local system.  This only works in general if the remote machine and the
  235. local one share the same directories (through NFS)."
  236.   (interactive "P")
  237.   (cond
  238.    ((or (null prefix)
  239.         (consp prefix))
  240.     (setq rlogin-directory-tracking-mode t)
  241.     (setq shell-dirtrackp t)
  242.     (setq comint-file-name-prefix
  243.           (concat "/" rlogin-remote-user "@" rlogin-host ":")))
  244.    ((< prefix 0)
  245.     (setq rlogin-directory-tracking-mode nil)
  246.     (setq shell-dirtrackp nil))
  247.    (t
  248.     (setq rlogin-directory-tracking-mode 'local)
  249.     (setq comint-file-name-prefix "")
  250.     (setq shell-dirtrackp t)))
  251.   (cond
  252.    (shell-dirtrackp
  253.     (let* ((proc (get-buffer-process (current-buffer)))
  254.            (proc-mark (process-mark proc))
  255.            (current-input (buffer-substring proc-mark (point-max)))
  256.            (orig-point (point))
  257.            (offset (and (>= orig-point proc-mark)
  258.                         (- (point-max) orig-point))))
  259.       (unwind-protect
  260.           (progn
  261.             (delete-region proc-mark (point-max))
  262.             (goto-char (point-max))
  263.             (shell-resync-dirs))
  264.         (goto-char proc-mark)
  265.         (insert current-input)
  266.         (if offset
  267.             (goto-char (- (point-max) offset))
  268.           (goto-char orig-point)))))))
  269.  
  270.  
  271. ;; Parse a line into its constituent parts (words separated by
  272. ;; whitespace).  Return a list of the words.
  273. (defun rlogin-parse-words (line)
  274.   (let ((list nil)
  275.     (posn 0)
  276.         (match-data (match-data)))
  277.     (while (string-match "[^ \t\n]+" line posn)
  278.       (setq list (cons (substring line (match-beginning 0) (match-end 0))
  279.                        list))
  280.       (setq posn (match-end 0)))
  281.     (store-match-data (match-data))
  282.     (nreverse list)))
  283.  
  284. (defun rlogin-carriage-filter (string)
  285.   (let* ((point-marker (point-marker))
  286.          (end (process-mark (get-buffer-process (current-buffer))))
  287.          (beg (or (and (boundp 'comint-last-output-start)
  288.                        comint-last-output-start)
  289.                   (- end (length string)))))
  290.     (goto-char beg)
  291.     (while (search-forward "\C-m" end t)
  292.       (delete-char -1))
  293.     (goto-char point-marker)))
  294.  
  295. (defun rlogin-send-Ctrl-C ()
  296.   (interactive)
  297.   (send-string nil "\C-c"))
  298.  
  299. (defun rlogin-send-Ctrl-D ()
  300.   (interactive)
  301.   (send-string nil "\C-d"))
  302.  
  303. (defun rlogin-send-Ctrl-Z ()
  304.   (interactive)
  305.   (send-string nil "\C-z"))
  306.  
  307. (defun rlogin-send-Ctrl-backslash ()
  308.   (interactive)
  309.   (send-string nil "\C-\\"))
  310.  
  311. (defun rlogin-delchar-or-send-Ctrl-D (arg)
  312.   "\
  313. Delete ARG characters forward, or send a C-d to process if at end of buffer."
  314.   (interactive "p")
  315.   (if (eobp)
  316.       (rlogin-send-Ctrl-D)
  317.     (delete-char arg)))
  318.  
  319. (defun rlogin-tab-or-complete ()
  320.   "Complete file name if doing directory tracking, or just insert TAB."
  321.   (interactive)
  322.   (if rlogin-directory-tracking-mode
  323.       (comint-dynamic-complete)
  324.     (insert "\C-i")))
  325.  
  326. ;;; rlogin.el ends here
  327.