;;; -*- Mode:Lisp; Readtable:CL; Package:USER; Base:10; Patch-File:T -*-
;;; Patch file for System version 123.243
;;; Reason:
;;;  Fix to GENERATE-FROM-HOSTS2-TABLE-2: if last line in 
;;;  the file SYS:SITE;HOSTS.TEXT did not have a <newline>,
;;;  then the last host didn't get defined.  Fix is to 
;;;  return from parsing loop when EOF is encountered
;;;  *and* a null string has been read.
;;; Written 27-Apr-88 14:01:14 by keith at site Gigamos Cambridge
;;; while running on Azathoth from band 1
;;; with Experimental System 123.242, Experimental Local-File 73.4, Experimental FILE-Server 22.2, Experimental Unix-Interface 11.0, Experimental KERMIT 34.3, Experimental ZMail 71.0, Experimental Lambda-Diag 15.0, Experimental Tape 22.2, Experimental KMC 1.0, microcode 1755, SDU Boot Tape 3.14, SDU ROM 8, the old ones.



; From modified file DJ: L.NETWORK; TABLE.LISP#10 at 27-Apr-88 14:01:28
#10R NETWORK#: 
(COMPILER-LET ((*PACKAGE* (GLOBAL:PKG-FIND-PACKAGE "NETWORK")))
  (COMPILER::PATCH-SOURCE-FILE "SYS: NETWORK; TABLE  "


(defun generate-from-hosts2-table-2 (input-file output-stream)
  (with-open-file (input-stream input-file :direction :input :characters t)
    (do ((line) (eof)
	 (i) (j)
	 (ni) (nj)
	 (hostl) (delim)
	 (result))
	(nil)
      (multiple-value-setq (line eof)
	(send input-stream :line-in nil))
      (and eof (zerop (string-length line)) (return result))
      (multiple-value-setq (i j)
	(parse-hosts2-table-token line 0))
      (cond ((and i (string-equal line "HOST" :start1 i :end1 j))
	     ;; Host name
	     (multiple-value-setq (ni nj)
	       (parse-hosts2-table-token line (1+ j)))
	     (multiple-value-setq (i j delim)
	       (parse-hosts2-table-token line (1+ nj)))
	     (setq hostl (ncons (substring line ni nj)))
	     (if (char= delim #\[)
		 (do ((l nil)
		      (i1) (j1))
		     ((char= delim #\])
		      (incf j)
		      (nreverse l))
		   (multiple-value-setq (i1 j1 delim)
		     (parse-hosts2-table-token line (1+ j)))
		   (if (char= delim #\Sp)
		       (multiple-value-setq (i j delim)
			 (parse-hosts2-table-token line (1+ j1)))
		       (setq i i1 j j1 j1 i1))
		   (add-hosts2-table-address line i1 j1 i j hostl))
		 (let ((i1 i) (j1 j))
		   (if (char= delim #\Sp)
		       (multiple-value-setq (i j)
			 (parse-hosts2-table-token line (1+ j)))
		       (setq i i1 j j1 j1 i1))
		   (add-hosts2-table-address line i1 j1 i j hostl)))
;	     (COND ((OR (GET HOSTL :CHAOS)	;If there were any chaosnet addresses
;			;; Include some popular ARPA sites for speed in SUPDUP/TELNET, etc.
;			(SYS:MEMBER-EQUAL (CAR HOSTL) INCLUDED-NON-CHAOS-HOSTS))
	     (dotimes (k 2)
	       (multiple-value-setq (i j delim)
		 (parse-hosts2-table-token line (1+ j))))
	     (when i
	       (setf (get hostl :system-type) (intern (substring line i j) "")))
	     (multiple-value-setq (i j delim)
	       (parse-hosts2-table-token line (1+ j)))
	     (when i
	       (setf (get hostl :machine-type) (intern (substring line i j) "")))
	     (multiple-value-setq (i j delim)
	       (parse-hosts2-table-token line (1+ j)))
	     (or i (setq delim -1))
	     (let* ((first-name (car hostl))
		    (namel (ncons first-name)))
	       (and (char= delim #\[)
		    (do () ((char= delim #\])
			    (setq namel (stable-sort namel
						     #'(lambda (x y)
							 ;; EQ is OK here...
							 (and (not (eq x first-name))
							      (< (string-length x)
								 (string-length y)))))))
		      (multiple-value-setq (i j delim)
			(parse-hosts2-table-token line (1+ j)))
		      (unless (equal i j) ;kmc-dle's suggestion for avoiding null hostnames
			(push (substring line i j) namel))))
	       (setf (get hostl :host-names) namel))
	     (if output-stream
		 (let ((*package* (or (find-package si:*force-package*) *package*)))
		      (format output-stream "(~S ~S~{~%  '~S '~S~})~2%"
			      'si::define-host (car hostl) (cdr hostl)))
	       (push hostl result)))))))

))
