;;; -*- Mode:Lisp; Readtable:T; Package:UNIX; Base:8; Patch-File:T -*-
;;; Patch file for Unix-Interface version 5.2
;;; Reason:
;;;  fix error that happens if you say system-U before
;;; Reason:
;;;  the system has finished booting
;;; Written 17-Feb-85 13:35:19 by bobp,
;;; while running on Lambda Two from band 1
;;; with Experimental System 102.68, Experimental Local-File 55.5, Experimental FILE-Server 13.1, Experimental Unix-Interface 5.1, Experimental MagTape 40.12, Experimental ZMail 57.0, microcode 742, almost Beta Test.



; From file LAMTTY.LISP#> QL.UNIX; LAM3: (49)
#8R UNIX#:
(COMPILER-LET ((*PACKAGE* (PKG-FIND-PACKAGE "UNIX")))
  (COMPILER#:PATCH-SOURCE-FILE "SYS: UNIX; LAMTTY  "


(defun find-unix-stream (with-login-p &aux str)
  (assure-*share-ttys*-set-up)
  (do-forever
    (catch-error-restart-explicit-if t (no-unix-stream nil "Try again.")
      (if (setq str (find-unix-stream-internal with-login-p))
	  (return-from find-unix-stream str))
      (ferror 'no-unix-stream
	      "couldn't find unix stream with~:[out~] login" with-login-p))))

))

; From file LAMTTY.LISP#> QL.UNIX; LAM3: (49)
#8R UNIX#:
(COMPILER-LET ((*PACKAGE* (PKG-FIND-PACKAGE "UNIX")))
  (COMPILER#:PATCH-SOURCE-FILE "SYS: UNIX; LAMTTY  "


(defun simple-unix-typein-top-level (window)
  (setf (tv:io-buffer-last-output-process (send window :io-buffer))
	(send window :typein-process))
  (cond ((si:share-mode-active-p)
	 (let ((terminal-io window))
	   (send window :clear-screen)
	   (assure-*share-ttys*-set-up)
	   (cond ((null (send window :port-number))
		  (multiple-value-bind (pathname port-number)
		      (find-unix-stream t)
		    (send window :set-unix-stream (open pathname))
		    (send window :set-port-number port-number)))
		 (t
		  (send window :set-port-number (open (format nil "UNIX-STREAM-~d:"
						(send window :port-number))))))
	   (do-forever
	     (send window :typein-top-level))))))

))

; From file LAMTTY.LISP#> QL.UNIX; LAM3: (49)
#8R UNIX#:
(COMPILER-LET ((*PACKAGE* (PKG-FIND-PACKAGE "UNIX")))
  (COMPILER#:PATCH-SOURCE-FILE "SYS: UNIX; LAMTTY  "

(defun make-all-share-ttys (&aux share-ttys)
  (setq *share-ttys* nil)
  (cond ((zerop (si:%system-configuration-number-of-share-ttys si:*sys-conf*))
	 (process-wait "unix share tty setup"
		       #'(lambda ()
			   (not (zerop (si:%system-configuration-number-of-share-ttys
					 si:*sys-conf*)))))
	 (process-sleep 60.)))
  (let ((base-adr (si:sdu-phys-to-virtual (si:%system-configuration-share-tty-0 si:*sys-conf*)))
	(nttys (si:%system-configuration-number-of-share-ttys si:*sys-conf*))
	share-tty-0)
    (setq share-tty-0 (si:make-share-tty-virtual base-adr))
    (dotimes (i nttys)
      (let* ((vaddr (+ base-adr
		       (* i (+ (max (si:%share-tty-lisp-to-unix-buffer share-tty-0)
				    (si:%share-tty-unix-to-lisp-buffer share-tty-0))
			       (truncate (si:%share-tty-buf-size share-tty-0) 4)))))
	     (st (si:make-share-tty-virtual vaddr)))
	(push st share-ttys)
	(si:add-shared-device :name (format nil "UNIX-STREAM-~d" i)
			      :shared-device-flavor 'unix-stream-shared-device
			      :owner-virtual-address (+ vaddr si:%share-tty-owner)
			      :default-flavor-and-init-options `(unix-stream :port-number ,i)
			      :property-list `(share-tty ,st))
	)))
  (setq *share-ttys* (reverse share-ttys)))

))
