;;; -*- Mode:Lisp; Readtable:ZL; Package:USER; Base:8; Patch-File:T -*-
;;; Patch file for Kermit version 36.4
;;; Reason:
;;;  The new Kermit parameter *sdu-serial-ascii-p* was ill-considered; Kermit
;;;  does its own Ascii translation.  Left the code commented out, since Ascii
;;;  stream translation should eventually be centralized, and the serial port
;;;  would then be the right place to Ascii-translate text that Kermit receives.
;;;  
;;;  Also changed the way SDU-SERIAL-OPEN sets the baud rate.  Now, specify a
;;;  NEW-BAUD-RATE = NIL (don't set baud rate) and T (use the default);
;;;  otherwise, NEW-BAUD-RATE must be a number to set baud rate to.  Kermit
;;;  uses this as before, but now the serial-open-forms need to say T so that
;;;  the globally specified baud rate gets used.
;;; Written 20-Jul-88 17:09:13 by keith (Keith Corbett) at site Gigamos Cambridge
;;; while running on Breaking Glass from band 1
;;; with System 125.7, ZWEI 125.2, ZMail 73.0, Local-File 75.0, File-Server 24.0, Unix-Interface 13.0, Tape 24.0, Lambda-Diag 17.0, Experimental Window-Maker 2.0, Experimental Kermit 36.3, microcode 1761, SDU Boot Tape 3.14, SDU ROM 103, 7/19.



; From modified file DJ: L.NETWORK.KERMIT; WINDOW.LISP#82 at 20-Jul-88 17:09:20
#8R KERMIT#: 
(COMPILER-LET ((*PACKAGE* (PKG-FIND-PACKAGE "KERMIT")))
  (COMPILER::PATCH-SOURCE-FILE "SYS: NETWORK; KERMIT; WINDOW  "


(defmethod (kermit-frame :top-level) (kermit-frame)
  (let ((status-pane (funcall kermit-frame :get-pane 'status-pane))
	(command-pane (funcall kermit-frame :get-pane 'command-pane))
	(interaction-pane (funcall kermit-frame :get-pane 'interaction-pane))
	(terminal-pane (funcall kermit-frame :get-pane 'terminal-pane))
	(terminal-io-syn-stream (make-syn-stream 'terminal-io))
	(*sdu-serial-default-baud-rate* *sdu-serial-default-baud-rate*)
	(*sdu-serial-xon-xoff-p* *sdu-serial-xon-xoff-p*)
	(*sdu-serial-ascii-p* NIL)		;For now, Kermit controls Ascii translation
	)
    (let ((terminal-io interaction-pane)
	  (standard-input terminal-io-syn-stream)
	  (standard-output terminal-io-syn-stream)
	  (query-io terminal-io-syn-stream)
	  (trace-output terminal-io-syn-stream)
	  (error-output terminal-io-syn-stream)
	  (debug-io terminal-io-syn-stream)
	  (ibase 10.)
	  (base 10.)
	  )
      
      ;; if kermit is not yet ready to accept commands, either because it is
      ;; just being started up or because a reset or warm boot has been done
      ;; before it was ready for commands, do various initialization actions.
      
      (cond
	((not kermit-ready-for-commands?)
	 
	 (setq kterm-state (make-instance 'kterm-state))
	 (setq kstate				;have kstate bound to a kstate instance
	       (progn
		 (fs:force-user-to-login)	;default-pathname setup depends on user
		 (make-instance 'kstate)	;  being logged in!
		 ))
	 (setf kermit-ready-for-commands? t)))      
      ;;;
      ;; this is kermit's top-level command execution loop.      
      ;;
      (error-restart-loop (sys:abort "Restart kermit process")
	(loop as character = (funcall terminal-io :any-tyi)	      
	      as command?
	      = (cond
		  ((and (not (atom character))
			(eq (car character) :menu))
		   (cadr character)))
	      doing
	      (cond
		((memq (get command? :funcall) all-commands-requiring-kermit-serial-stream)
		 (or kermit-serial-stream
		     (setq kermit-serial-stream (eval serial-stream-open-form)))
		 (if (eq (funcall command-pane :execute command?) :close)
		     (send self :close-serial-stream)))
		(command?
		 (funcall command-pane :execute command?))
		((not (atom character))
		 (beep-at-user))
		((= character #\hand-down)
		 (send kermit-frame ':set-configuration 'long-terminal)
		 (setq debug-io terminal-pane))
		((= character #\hand-up)
		 (send kermit-frame ':set-configuration 'default)
		 (setq debug-io terminal-io-syn-stream)
		 (send kermit-frame :refresh))
		('else
		 (handle-unanticipated-terminal-input character))))))))

))

; From modified file DJ: L.NETWORK.KERMIT; LAMBDA-SDU-SERIAL.LISP#3 at 20-Jul-88 17:09:41
#10R KERMIT#: #!:CL
(COMPILER-LET ((*PACKAGE* (PKG-FIND-PACKAGE "KERMIT")))
  (COMPILER::PATCH-SOURCE-FILE "SYS: NETWORK; KERMIT; LAMBDA-SDU-SERIAL  "


(defun gather-sdu-serial-devices(&optional reset-baud-rate)
  (loop for p in fs:*pathname-host-list*
	when (typep p 'si:sdu-serial-b-shared-device)
	collect `(,(send p :name) (sdu-serial-open ,(send p :name) ,reset-baud-rate))))

))

; From modified file DJ: L.NETWORK.KERMIT; CALLS.LISP#65 at 20-Jul-88 17:09:48
#8R KERMIT#: 
(COMPILER-LET ((*PACKAGE* (PKG-FIND-PACKAGE "KERMIT")))
  (COMPILER::PATCH-SOURCE-FILE "SYS: NETWORK; KERMIT; CALLS  "


(defmethod (kstate :set-params) ()
  (declare (special kermit-frame serial-stream-open-form))
  (let ((oldx tv:mouse-x) (oldy tv:mouse-y)
	(menux (tv:sheet-inside-right kermit-frame))
	(menuy (tv:sheet-inside-bottom kermit-frame))
	;;
	;; APPEND NEW SYMBOLS TO THESE TWO LISTS!
	;;
	(vars '(kermit-default-pathname
		 serial-stream-open-form
		 *sdu-serial-xon-xoff-p*
		 ;;; *sdu-serial-ascii-p* ;;Don't want this now - Kermit translates Ascii
		 *file-closing-disposition* *filnamcnv* *8-bit-lispm* *image*
		 ascii-extra-safe-filter?
		 *soh* *mytime* *myquote* *mypad* *mypchar* *image* *debug*
		 *checksum-type*
		  ))
	(old-vals (list kermit-default-pathname
			serial-stream-open-form
			*sdu-serial-xon-xoff-p*
			;;*sdu-serial-ascii-p* ;;See above
			*file-closing-disposition* *filnamcnv* *8-bit-lispm* *image*
			ascii-extra-safe-filter?
			*soh* *mytime* *myquote* *mypad* *mypchar* *image* *debug*
			*checksum-type*
			)))
    ;;
    (tv:mouse-warp (- menux 50.) (- menuy 50.))	;try to put the mouse around the ctr of menu

    (multiple-value-bind (nil abort-p)
	(*catch 'legal-abortion
	  (tv:choose-variable-values
	    `("Mouse-click on the desired option, type a new value, and press <RETURN>.        "
	      "When you are done modifying parameters, click in the margin on /"Execute:/"     "
	      "================================================================================"
	      "The follow parameters determine the form of login or Kermit I//O connection.    "
	      "Note that they take effect only after the current connection, if any, is closed."
	      "                                                                                "
	      (serial-stream-open-form
		:documentation "The serial stream//device for login or Kermit connections."
		:menu-alist
		(
		 ;;Map over fs:*pathname-host-list* to get serial-port open forms:
		 ;; ("Serial Port A" (sdu-serial-open "SDU-SERIAL-A:"))
		 ;; ("Serial Port B" (sdu-serial-open "SDU-SERIAL-B:"))
		 ,@(gather-sdu-serial-devices T)	;Let baud rate default dynamically

		 ,@(IF (FIND-PACKAGE "TCP") '(("TCP TELNET" (OPEN-TCP-TELNET-SERIAL-STREAM))))
		 ("CHAOS TELNET" (OPEN-CHAOS-TELNET-STREAM))

		 ;; SUPDUP doesnt work because we are not sending the right negotiations.
		 ;; ("CHAOS SUPDUP" (OPEN-CHAOS-TELNET-STREAM "SUPDUP"))

		 ("REMOTE SERIAL STREAM" (OPEN-REMOTE-SERIAL-STREAM))

		 ;;This was pace's own hack, and doesn't work anywhere anymore:
		 ;("REMOTE UNIX SERIAL" (OPEN-REMOTE-UNIX-SERIAL-STREAM))

		 ("Prompt User" (prompt-and-read :eval-read "~&Form to EVAL and return a stream: "))

		 ;;Unix share ttys
		 ;; One should make sure the pathname exists; otherwise, you'll
		 ;; open an 'i//o stream' to some random file probably.
		 . ,(loop for share-tty in (and (boundp 'unix:*share-ttys*)
						unix:*share-ttys*)
			  as port-number from 0
			  collect
			   (list
			     (format nil "Unix Port ~D (//dev//ttyl~D)"
				     port-number port-number)
			     `(open
				,(format nil "UNIX-STREAM-~D:"
					 port-number))))))

	      ;;We may want this in the future, but for now Kermit controls Ascii translation
	      ;;(*sdu-serial-ascii-p*
	      ;; :documentation "For serial ports, Yes to perform ASCII//LISPM character conversion."
	      ;; :boolean)

	      ;;We usually <want> software flow control, but only the other host knows!
	      (*sdu-serial-xon-xoff-p*
		:documentation "For serial ports, Yes to use software Xon//Xoff flow control."
		:boolean)

	      "--------------------------------------------------------------------------------"
	      "The remaining parameters affect the ongoing behavior of Kermit transfers.       "
	      "Note that they take effect after the current connection, if any, is closed.     "
	      "                                                                                "

	      (kermit-default-pathname
		:documentation "Default local pathname/directory for Kermit file transfers"
		:pathname kermit-default-pathname)
	      
	      (*filnamcnv* :documentation "Specify mode of filename conversion, if any."
			   :menu-alist ,(cons '("Raw - no conversion" :raw)
					      (cons '("Unknown - generic" :generic)
						    (mapcar #'(lambda (x)
								(list (car x) (car x)))
							    (get (locf fs:canonical-types)
								 :lisp)))))

	      (ascii-extra-safe-filter?
		:documentation
		"Either NIL, or a LISP function that filters unwanted control characters.")

	      (*8-bit-lispm* :documentation
	       "Yes if you can send 8-bit characters, and want LISPM//ASCII character translation."
	       :boolean)

	      (*image* :documentation
		         "Yes, if you want 8-bit, binary mode (no character translation)."
		       :boolean)

	      (*file-closing-disposition*
		:documentation
		  "Decide whether files only partially written due to interrupt should be saved."
		:menu-alist (("delete-if-abort" :abort)
			     ("dont-delete" nil)))

	      (*debug* :documentation
		         "Yes, if you want verbose debugging information during transfer."
		       :boolean)
	      (*terminal-debug-mode* :documentation "Yes for debugging the terminal emulator"
				     :boolean)

	      "--------------------------------------------------------------------------------"
	      "Some less commonly changed, Kermit packet level parameters - for expert users   "
              "with knowledge of the remote Kermit Protocol and//or operating system           "
              "and their features//problems:"

	      (*soh* :documentation
		       "mark for start of packet (a non-printing character)"
		     :number)
	      (*mytime* :documentation
			  "max time to wait for packet"
			:number)
	      (*myquote* :documentation "Character to use to quote non-printing chars."
			 :number)
	      (*myeol* :documentation "mark for end of packet"
		       :number)
	      (*mypad* :documentation
		         "Number of padding characters to use in packet (usually 0)"
		       :number)
	      (*mypchar* :documentation
			   "Padding character to use in packet (usually NUL (0))" 
			 :number)
	      (*checksum-type* :documentation
			       "[Only one character checksums are supported at this time]"
			       :menu-alist (("Normal-one-character" 1)))
	      "      ")
	    
	    :label "Review//Modify Kermit Parameters"
	    :near-mode `(:point ,menux ,menuy)
	    :superior kermit-frame
	    :margin-choices '("Execute:" ("Abort:" (*throw 'legal-abortion nil)))
	    :function #'(lambda(window var old new)
			  (if (and (member var '(*sdu-serial-ascii-p* *image*))
				   new
				   *sdu-serial-ascii-p* *image*)
			      (progn
				(beep)
				(send window :clear-window)
				(send window :fat-string-out *invalid-image-opt-msg*)
				(send window :tyi)
				(send window :refresh)
				(set var old))
			    nil))
	    ))
      (and abort-p
	   (loop for var in vars and old-val in old-vals doing (set var old-val)))
      nil)
    
    (tv:mouse-warp oldx oldy)))

))

; From modified file DJ: L.NETWORK.KERMIT; LAMBDA-SDU-SERIAL.LISP#3 at 20-Jul-88 17:20:02
#10R KERMIT#: #!:CL
(COMPILER-LET ((*PACKAGE* (PKG-FIND-PACKAGE "KERMIT")))
  (COMPILER::PATCH-SOURCE-FILE "SYS: NETWORK; KERMIT; LAMBDA-SDU-SERIAL  "

(defun sdu-serial-open (&optional
			(name *sdu-serial-default-device-name*)
			(new-baud-rate T))
  "Open an SDU serial port with options specified by global parameters.
NAME is a serial device name; defaults to *SDU-SERIAL-DEFAULT-DEVICE-NAME*.
NEW-BAUD-RATE should be a
  1) a number to set baud rate to, or
  2) T (the default) to use the global parameter *SDU-SERIAL-DEFAULT-BAUD-RATE*, or
  3) NIL to not specify a baud rate (value defaults from existing or previous
     SDU serial streams).  
 Note that changing the baud rate completely resets the serial port."
  ;;;;
  (declare (values sdu-serial-stream))
  (unless (member name *sdu-serial-device-alist* :test #'string-equal :key #'car)
    (cerror "Proceed to open ~s anyway"
	    "~s is not the name of a known serial port shared device / pathname host" name))
  ;;;;
  (let ((device (make-pathname :host name)))
    (check-type device si:shared-device-pathname)
    (open device
	  :flavor-and-init-options
	  `(
	    ,(si:combined-sdu-serial-stream-flavor :ascii    *sdu-serial-ascii-p*
						   :xon-xoff *sdu-serial-xon-xoff-p*)
	    :input-buffer-size  ,(* 3 si:page-size)
	    :output-buffer-size ,(* 2 si:page-size)
	    ,@(if new-baud-rate (list :baud-rate
				      (if (eq new-baud-rate t)
					  *sdu-serial-default-baud-rate*
					new-baud-rate)))))))

))
