[comp.emacs] process filters

pedz@bobkat.UUCP (Pedz Thing) (11/13/86)

I want to write a process filter for GNU emacs and can not seem to
find all the hooks I am looking for.  Basically, I would like to take
the string passed to the filter function and look at it character by
character.  With each character, I would want to either insert it into
the process's buffer or execute some function.  Actually I would
prefer to add the characters to the buffer in "overwrite-mode".

I am assuming that the most effecient way to go about doing this is to
tie into emac's key-map facilities.  I have looked in the help
facility and can not find what I need.  I can obviously do it by
indexing into the string and getting each character.  Using the
character to index into another vector of functions (like a keymap)
and execute the function.  That might not be too bad but thought I'd
ask if that was a better method.

-- 
Perry Smith
{convex!ctvax,{ti-csl,infotel}!pollux}!bobkat!pedz

manheime@nbs-amrf.UUCP (11/18/86)

> I want to write a process filter for GNU emacs and can not seem to
> find all the hooks I am looking for.  Basically, I would like to take
> the string passed to the filter function and look at it character by
> character.  With each character, I would want to either insert it into
> the process's buffer or execute some function.  Actually I would
> prefer to add the characters to the buffer in "overwrite-mode".
> 
> I am assuming that the most effecient way to go about doing this is to
> tie into emac's key-map facilities.  I have looked in the help
> facility and can not find what I need.  I can obviously do it by
> indexing into the string and getting each character.  Using the
> character to index into another vector of functions (like a keymap)
> and execute the function.  That might not be too bad but thought I'd
> ask if that was a better method.
> Perry Smith
> {convex!ctvax,{ti-csl,infotel}!pollux}!bobkat!pedz

A while ago i spent some time working on this problem but didn't
have time to complete what i started.  The approach, however, ought
to be interesting, and i've implemented enough to demonstrate that
the method i'm using should be effective to provide fairly general
on-the-fly parsing and execution of control strings embedded in
some process output.

What i did was start with the telnet-filter in telnet.el (standard
distribution) and embed a parser that detects and performs
control-significant character sequences output from a process (in
this case the process running under telnet), and is optimized to
collect output from the process in blocks and emit
literal-sequences in blocks (rather than character-at-a-time).  I
started on implementing the basic vt100 control-sequences.

I'm appending my tailored version of telnet.el below in it's
entirety - while i have regrets about the volume of the posting, i
don't have the time to tidy it into a smaller package.  My
apologies, and i hope someone will be interested enough to wade
through it - the interesting items are: first, telnet-vt100-filter,
then toggle-telnet-vt100-mode (and the key binding, so you can
activate it), then just search for the occurrences of the string
'vt100' for the bindings and etc pertinent miscellany.

For the same reason of time-constraints i'm putting this message
together rather quickly; i've been interested in following up on
this issue for a while now, but lacking the time and interested in
punting what i've done hoping to get some feedback, maybe
development or else pointers about what i've done wrong in my
efforts.  To the original poster - good luck finding what you're
looking for, if you do and it's something along the lines of what
i'm presenting here (or accomplishes the same sort of thing),
please share your answers with me...

Thanks,

Ken Manheimer		...!{seismo,umcp-cs}!nbs-amrf!manheime

telnet.el begins after dotted line.
----------------------------------------------------------------
;; Copyright (C) 1985 Free Software Foundation

;; This is a tailored version of the standard-distribution
;; telnet.el, intended to begin to provide active process-output
;; filtering that performs functions keyed by specified
;; control-sequences in the output.  Ken Manheimer, June, 1986

;; This file is part of GNU Emacs.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY.  No author or distributor
;; accepts responsibility to anyone for the consequences of using it
;; or for whether it serves any particular purpose or works at all,
;; unless he says so in writing.  Refer to the GNU Emacs General Public
;; License for full details.

;; Everyone is granted permission to copy, modify and redistribute
;; GNU Emacs, but only under the conditions described in the
;; GNU Emacs General Public License.   A copy of this license is
;; supposed to have been given to you along with GNU Emacs so you
;; can know your rights and responsibilities.  It should be in a
;; file named COPYING.  Among other things, the copyright notice
;; and this notice must be preserved on all copies.


;; Author William F. Schelter

;;to do fix software types for lispm:
;;to eval current expression.  Also to try to send escape keys correctly.
;;essentially we'll want the rubout-handler off.

(defvar telnet-new-line "\r")
(defvar telnet-mode-map nil)
(defvar telnet-prompt-pattern "^[^#$%>]*[#$%>] *")
(defvar telnet-interrupt-string "\^c" "*String sent by C-c.")
(defvar telnet-count 0)
(defvar telnet-replace-c-g nil)
(defvar telnet-remote-echoes nil)

(defun send-self-command ()
  (interactive)
  (send-string nil (char-to-string last-command-char)))

(defun telnet-interrupt-subjob ()
  (interactive)
  "Interrupt the program running through telnet on the remote host."
  (send-string nil telnet-interrupt-string))

(defun telnet-c-z ()
  (interactive)
  (send-string nil "\^Z"))

(defun telnet-c-o ()
  (interactive)
  (send-string nil "\^O"))

(defun telnet-c-s ()
  (interactive)
  (send-string nil "\^S"))

(defun telnet-c-q ()
  (interactive)
  (send-string nil "\^Q"))

(defun send-process-next-char ()
  (interactive)
  (send-string nil
	       (char-to-string
		(let ((inhibit-quit t))
		  (prog1 (read-char)
		    (setq quit-flag nil))))))

(if telnet-mode-map
    nil
  (setq telnet-mode-map (make-keymap))
  (define-key telnet-mode-map "\e\C-xt" 'toggle-telnet-vt100-mode)
  (define-key telnet-mode-map "\C-m" 'telnet-send-input)
  (define-key telnet-mode-map "\C-c\C-d" 'shell-send-eof)
;  (define-key telnet-mode-map "\C-c\C-q" 'send-process-next-char)
  (define-key telnet-mode-map "\C-c\C-u" 'kill-shell-input)
  (define-key telnet-mode-map "\C-c\C-w" 'backward-kill-word)
  (define-key telnet-mode-map "\C-c\C-c" 'telnet-interrupt-subjob) 
  (define-key telnet-mode-map "\C-c\C-o"  'telnet-c-o)
  (define-key telnet-mode-map "\C-c\C-q"  'telnet-c-q)
  (define-key telnet-mode-map "\C-c\C-s"  'telnet-c-s)
  (define-key telnet-mode-map "\C-c\C-z"  'telnet-c-z)
  (setq telnet-vt100-mode-map (copy-keymap telnet-mode-map))
  (let ((char ?\ ))
    (while (<= char 126)
      (define-key telnet-vt100-mode-map
	(char-to-string char) 'send-self-command)
      (setq char (1+ char))))
  (define-key telnet-vt100-mode-map "\C-m" 'send-self-command)
  (define-key telnet-vt100-mode-map "\C-j" 'send-self-command)
  )

;;maybe should have a flag for when have found type
(defun telnet-check-software-type-initialize (string)
  "Tries to put correct initializations in.  Needs work."
  (cond ((string-match "unix" string)
	 (setq telnet-prompt-pattern shell-prompt-pattern)
	 (setq telnet-new-line "\n"))
	((string-match "tops-20" string) ;;maybe add telnet-replace-c-g
	 (setq telnet-prompt-pattern  "[@>]*"))
	((string-match "its" string)
	 (setq telnet-prompt-pattern  "^[^*>]*[*>] *"))
	((string-match "explorer" string)  ;;explorer telnet needs work
	 (setq telnet-replace-c-g ?\n))
	))

(defun telnet-initial-filter (proc string)
  ;For reading up to and including password; also will get machine type.
  (cond ((string-match "passw" string)
	 (telnet-filter proc string)
	 (setq password (read-password))
	 (setq telnet-count 0)
	 (send-string proc (concat password  telnet-new-line)))
	(t (telnet-check-software-type-initialize string)
	   (telnet-filter proc string)
	   (cond ((> telnet-count 4)
		  (set-process-filter proc 'telnet-filter))
		 (t (setq telnet-count (1+ telnet-count)))))))

(defun telnet-filter (proc string)
  (save-excursion
    (set-buffer (process-buffer proc))
    (goto-char (point-max))
    (let ((now (point)))
      (insert string)
      (subst-char-in-region now (point) ?\
      (and telnet-replace-c-g (subst-char-in-region now (point) ?\ telnet-replace-c-g)))
    (if (process-mark proc)
	(set-marker (process-mark proc) (point)))
    (if (and (integer-or-marker-p last-input-start)
	     (marker-position last-input-start)
	     telnet-remote-echoes)
	(delete-region last-input-start last-input-end)))
  (if (eq (process-buffer proc)
	  (current-buffer))
      (goto-char (point-max))))

(defun delete-char-or-send-eof (arg killp)
  "At end of buffer, send eof to subshell.  Otherwise delete character."
  (interactive "p\nP")
  (if (and (eobp) (not killp))
      (process-send-eof)
    (delete-char arg killp)))

(defun telnet-send-input ()
  "Send input to remote host
At end of buffer, sends all text after last output
as input to the telnet, including a telnet-new-line inserted at the end.
Not at end, copies current line to the end of the buffer and sends it,
after first attempting to discard any prompt at the beginning of the line
by matching the regexp that is the value of telnet-prompt-pattern if possible."
 (interactive)
 (let (copied)
  (end-of-line)
  (if (eobp)
      (progn
	(move-marker last-input-start
		     (process-mark (get-buffer-process (current-buffer))))
	(move-marker last-input-end (point)))
    (beginning-of-line)
    (re-search-forward telnet-prompt-pattern nil t)
    (let ((copy (buffer-substring (point)
				  (progn (forward-line 1) (point)))))
      (goto-char (point-max))
      (move-marker last-input-start (point))
      (insert copy) (setq copied t)
      (move-marker last-input-end (point))))
  (save-excursion
    (goto-char last-input-start)
    (let ((process (get-buffer-process (current-buffer))))
      (send-region process last-input-start last-input-end)
      (if (not copied) (send-string process telnet-new-line))
      (set-marker (process-mark process) (point))))))

(defun telnet (arg)
  "Open a network login connection to host named HOST (a string).
Communication with HOST is recorded in a buffer *HOST-telnet*.
Normally input is edited in Emacs and sent a line at a time."
  (interactive "sOpen telnet connection to host: ")
  (require 'shell)
  (let ((name (concat arg "-telnet" )))
    (switch-to-buffer (make-shell name "telnet"))
    (set-process-filter (get-process name) 'telnet-initial-filter)
    (erase-buffer)
    (send-string  name (concat "open " arg "\n"))
    (telnet-mode)
    (setq telnet-count -16)))

(defun read-password ()
  (let ((answ "") tem)
    (while (not (= (setq tem (read-char)) ?\^M))
      (setq answ (concat answ (char-to-string tem))))
    answ))

(defun telnet-mode ()
  "This mode is for use during telnet from a buffer to another
host. It has most of the same commands as shell 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 `return' is typed.
Thus if you may need to edit the data before sending you
should use c-n to move down a line.  Then you can return
to alter a previous line.  Of course you should not use this
mode of telnet if you want to run emacs-like programs on the
remote host (at least not yet!).

 \\[telnet-send-input] Causes the last string of input to be sent to remote host
 \\[delete-char-or-send-eof] Will delete a character (or send eof if it is the last character.
 \\[kill-shell-input]  Kills the last shell-input
 \\[backward-kill-word] runs backward-kill-word.
 \\[send-process-next-char] sends the next keystroke directly without interpretation
    eg. useful for sending c-q or ` '  to continue typeout with more processing.
 \\[telnet-interrupt-subjob] interrupts the current program running on the remote host.
 \\[telnet-c-z] Sends a c-z to the remote host

Bugs:
--Replace 
--For Unix interacts poorly with tcsh although csh,sh,ksh are ok."
  (interactive)
  (kill-all-local-variables)
  (setq major-mode 'telnet-mode)
  (setq mode-name "telnet")
  (setq mode-line-format 
	"--%1*%1*-Emacs: %17b   %M   %[(%m: %s)%]----%3p--%-")
  (make-local-variable 'last-input-start)
  (use-local-map telnet-mode-map)
  (let ((tem telnet-prompt-pattern))
    (make-local-variable 'telnet-prompt-pattern)
    (setq telnet-prompt-pattern tem))
  (make-local-variable 'telnet-interrupt-string)
  (setq telnet-interrupt-string "")
  (make-local-variable 'telnet-new-line)
  (setq telnet-new-line "\r")
  (make-local-variable 'last-input-start)
  (setq last-input-start (make-marker))
  (make-local-variable 'last-input-end)
  (setq last-input-end (make-marker))
  (make-local-variable 'telnet-remote-echoes)
  (setq telnet-remote-echoes t)
  (make-local-variable 'telnet-replace-c-g)
  (setq telnet-replace-c-g nil)
  (defvar output-carryover ""
    "Receptacle for carryover of divided esc-sequence output.")
  (make-local-variable output-carryover)
  (defvar buffer-proc nil "Current process of buffer.")
  (make-local-variable buffer-proc)
  (defvar remote-hosted-io nil "True when remote is handling io char by char.")
  (make-local-variable remote-hosted-io)
)


; klm 21-Jun-1986
(defun telnet-vt100-filter (proc string)
  " Emulate vt100 operation on a telnet process.  Traverse the input string,
gradually emitting chunks of literals and performing vt100 control-sequence
operations."
  (let* ((receipt (concat output-carryover string))
	 (index 0)
	 (from (current-buffer))
	 ctrl esc-mod next-ctrl (narg-1 "")(narg-2 "") index-ckpt)
    (set-buffer (process-buffer proc))
    (goto-char (point-max))
      ;;; Successively find next control char, emit literals between current
      ;;; point ('index') and there, parse and perform the control sequence of
      ;;; the char and repeat on remaining string until no more controls left.
      ;;; (Any literals trailing final controls are emitted after the loop.)
      ;;; Incompleted escape sequence are carried over for prepending to the
      ;;; subsequent receipt string in 'output-carryover'.
    (setq output-carryover "")	;;; zero carryover
    (while (setq next-ctrl (string-match "\\([      (insert-string (substring receipt index (setq index next-ctrl)))
      (setq ctrl (substring receipt index (setq index (1+ index))))
      (cond		     ;;; determine type of control and act accordingly -
			     ;;; Order common chars before less common ones!
       ((string= ctrl "\^J")		;;; linefeed - do a newline.
	(next-line 1))
       ((string= ctrl "\^M") (beginning-of-line)) ;;; <carriage return>
       ((string= ctrl "\^@"))		;;; discard nulls.
       ((string= ctrl "\^G") (beep))	;;; bell
       ((string= ctrl "\e")		;;; beginning of esc sequence
	(condition-case err	;;; Guard against divided esc sequences -
				;;; Errors in that case will occur during
				;;; substring refs beyond end of string.
	    (progn
		;;; first, checkpoint from beginning of esc-sequence:
	      (setq output-carryover (substring receipt (1- index)))
		;;; now process sequence:
	      (setq esc-mod
		    (substring receipt index (setq index (1+ index))))
	      (cond
	       ((string= esc-mod "[")		;;; most esc seqs:
		(setq esc-mod (substring receipt index (1+ index)))
		(message "bracket: %s" (substring receipt index))(sit-for 2)
		(if (not (string-match "[0-9]" esc-mod))
		      ;;; no numeric arguments:
		    (progn
		      (message "nonums: %s" (substring receipt index))(sit-for 2)
		      (setq index (1+ index))		;;; Update index.
			;;; Erase from cursor to eol:
		      (cond
		       ((string= esc-mod "K")
			(delete-region (point) (progn (end-of-line) (point))))
		       ((string= esc-mod ";")
			(setq esc-mod
			      (substring receipt (setq index (1+ index))))
			(cond
			 ((string= esc-mod "H")(beginning-of-buffer));;; home
			 ))
			 ;;; Arrow keys:
		       ((string= esc-mod "A")
			(let ((at-column (current-column))) ;;; NOTE: gotta stick to col.
			  (previous-line 1)))
		       ((string= esc-mod "B")
			(let ((at-column (current-column))) ;;; NOTE: gotta stick to col.
			  (next-line 1)))
		       ((string= esc-mod "C")
			(let ((at-column (current-column))) ;;; NOTE: gotta stick to col.
			  (forward-char)))
		       ((string= esc-mod "D")
			(let ((at-column (current-column))) ;;; NOTE: gotta stick to col.
			  (backward-char)))
		       (t (insert-string esc-mod)))
		      )
		    ;;; Numeric args; parse 'em, get modifier, and process:
		  (message "Nums: %s" (substring receipt index))(sit-for 2)
		  (string-match "\\([0-9]+\\)\\(;\\([0-9]+\\)\\)?"
				receipt index)
		  (setq narg-1 (substring receipt index (match-end 1)))
		  (setq index (match-end 0))
		  (setq narg-2 (substring receipt (match-beginning 3) index))
		  (setq esc-mod
			(substring receipt index (setq index (1+ index))))
		  (cond
		   ((string= esc-mod "H")	     ;;; cursor motion
		    (goto-line (string-to-int narg-1))
		    (move-to-column (string-to-int narg-2)))
		   ((string= esc-mod "K")		;;; clear line
		    (cond ((string= narg-1 "0")	;;; delete cursor to eol
			   (delete-region (point)
					  (progn (end-of-line) (point))))
			  ((string= narg-1 "1")	;;; delete cursor to bol
			   (delete-region
			    (point) (progn (beginning-of-line) (point))))
			  ((string= narg-1 "2")	;;; delete entire line
			   (delete-region
			    (progn (beginning-of-line) (point))
			    (progn (next-line 1) (point))))))
		   ((string= esc-mod "J")		;;; clear screen
		    (message "Erase in display.")(sit-for 2)
		    (cond ((string= narg-1 "0")	;;; delete cursor to eob
			   (delete-region (point)
					  (progn (end-of-buffer) (point))))
			  ((string= narg-1 "1")	;;; delete cursor to eob
			   (delete-region
			    (point) (progn (beginning-of-buffer) (point))))
			  ((string= narg-1 "2")	;;; erase entire screen
			   (erase-buffer))))
		   ;;; other - insert as literal:
		   ((string= esc-mod "r")
		    (message "Scrolling region %s %s" narg-1 narg-2))
		   (t (insert-string narg-1) (insert-string narg-2)
		      (insert-string esc-mod)) ) ) ) )
	      (setq output-carryover "") )
	    ;;; Condition case caught reference to incomplete esc-seq;
	    ;;; set index to last char (carryover has already been set):
	  (args-out-of-range (setq index (1- index)))
	    ;;; if unanticipated error, clear carryover and reraise the error:
	  (error (setq output-carryover "")(signal (car err)(cdr err))))
	)))
      ;;; rest of string (if condition-case caught incomplete esc-sequence then this
      ;;; will be the empty string)
    (insert-string (substring receipt index))
    (if (process-mark proc)
	(set-marker (process-mark proc) (point)))
    (if (and (integer-or-marker-p last-input-start)
	     (marker-position last-input-start)
	     telnet-remote-echoes)
	(delete-region last-input-start last-input-end))
    (if (not (eq from (current-buffer)))
	(switch-to-buffer from))))

(defun toggle-telnet-vt100-mode ()
  (interactive)
  (if (equal (process-filter (get-buffer-process (current-buffer)))
	     'telnet-filter)
      (progn (set-process-filter (get-buffer-process (current-buffer))
				 'telnet-vt100-filter)
	     (use-local-map telnet-vt100-mode-map)
	     (message "Toggled to telnet/vt100 mode."))
    (set-process-filter (get-buffer-process (current-buffer)) 'telnet-filter)
    (use-local-map telnet-mode-map)
    (message "Toggled to plain telnet mode.")))

sdl@mitre-bedford.ARPA.UUCP (11/23/86)

Ken, 

the function telnet-vt100-filter seems to have gotten somewhat
garbaged in transmission (see below); could you post a corrected
version?

The string-match below looks wrong....



      ;;; (Any literals trailing final controls are emitted after the loop.)
      ;;; Incompleted escape sequence are carried over for prepending to the
      ;;; subsequent receipt string in 'output-carryover'.
    (setq output-carryover "")	;;; zero carryover
    (while (setq next-ctrl (string-match "\\([      (insert-string (substring receipt index (setq index next-ctrl)))
      (setq ctrl (substring receipt index (setq index (1+ index))))




Steven Litvintchouk
MITRE Corporation
Burlington Road
Bedford, MA  01730
(617)271-7753

ARPA:  sdl@mitre-bedford
UUCP:  ...{cbosgd,decvax,genrad,ll-xn,philabs,security,utzoo}!linus!sdl