;;;-*-Midas-*-

TITLE DRAGON -- top-level daemon
	
;;; The ITS system job looks for the file DSK:SYS;ATSIGN DRAGON, and if it exists, 
;;; launches this DRAGON daemon. When launched, DRAGON logs in as TARAKA and searches
;;; the CHANNA directory for files whose FN1 is RAKASH. Any that it finds, it launches
;;; as daemons.

;;;
;;; The source for DRAGON has been long lost. When ITS was active, the last time it
;;; was built was in 1977. This source was recreated from the binary by EJS on
;;; on 2016-11-15. When assembled, this version results in an identical binary
;;; to the one that was included in the PI/MX distribution, and therefore to the
;;; version in place on MC/AI at the time of the shutdown of those systems.
;;;
;;; Note: I'm sure some of the constants in this file should be replaced with
;;; symbolic constants. At this point, I haven't yet figured out what they should
;;; all be (it's been over 36 years since I last used ITS at MIT.  As I and others
;;; see fit, we can update this source to be more in keeping with the standards
;;; expected of ITS developers!  -- EJS@MIT-MC (today: eric@swenson.org)
;;;

dirchn=4
filchn=5
jobchn=6

dirbsz=2000

nop=255000000000

taraka=sixbit/TARAKA/
channa=sixbit /CHANNA/
rakash=sixbit /RAKASH/

lpdl==:100.
.vector pdl(lpdl)

go:	.call [setz        ;; login as TARAKA
	   sixbit/LOGIN /
	   [ taraka ]
           setzi 0]
	nop                ;; ignore errors logging in
	.suset [.ssnam,,[channa]]       ;; set SNAME to CHANNA
	;; open CHANNA UDIR
	.open dirchn, [.bii,,'DSK ? sixbit/.FILE./ ? sixbit /(DIR) /]
	  jsr done
	.iot dirchn, [-dirbsz,,dirblk]  ;; read in UDIR
	.close dirchn,
	move 1, dirblk+1   ;; get offset to name area into R1
	addi 1, dirblk     ;; convert to address of name area
filoop:	cail 1, [ taraka ] ;; have we reached end of buffer?
	jsr done           ;; yes, abort
	move 2, (1)        ;; get FN1 into R2
	camn 2, [ rakash ] ;; is FN1 == RAKASH
	.call [setz        ;; yes, open the file
	  sixbit /OPEN  /
	  [ .uii,,filchn ]
	  [ sixbit /DSK   / ]  ;; DSK: device
	  [ rakash ]           ;; FN1
	  1,,1                 ;; FN2 (from dirblk in R1 + 1)
          400000,, [ channa ]]
	jrst nextfl        ;; try next file
	  .call [setz            ;; start create daemon job
	    SIXBIT/OPEN  /
	    [ 1,,jobchn ]
	    [ sixbit /USR   / ]  ;; USR: device
            001000000000         ;; use uname of TARAKA
            400001,,1]           ;; use jname of FN2 of daemon
	  jrst nextfl            ;; process next file on error
	  .reset jobchn,
          .call    [setz         ;; load program into job
	    sixbit/LOAD  /
            001000,,jobchn       ;; specify job channel
            401000,,filchn]      ;; specify file channel
	  jrst nextjb              ;; on error, clean up job and process next file
	  move 2, 1(1)           ;; get FN2/Job name in R2
          .uset jobchn, [.ssnam,,2]  ;; set SNAME of job
	  .iot filchn, 2             ;; get starting address?
	  hrrzs    0, 2              ;; zero out instruction opcode?
  	  .uset jobchn, [.supc,,2]   ;; set starting address of new job
	  .call [setz                ;; disown job (doc doesn't seem to match
            SIXBIT/DISOWN/           ;;  this call from sysdoc; _calls 127
            201000,,jobchn           ;; don't know what these bits do
            405000,,5]               ;; don't see second argument documented???
 	 .value 0                    ;; kill ourselves on error?
	 caia 0, 0
nextjb:    .uclose  jobchn,          ;; kill job
nextfl:  .close  filchn,             ;; close file
         .close   jobchn,            ;; close job
         addi     1, 5               ;; compute address of next file
         jrst     filoop             ;; and loop

done:  0
       .logout  0,
       .value   0

dirblk:  0
         0

bufsize==1632
loc .+bufsiz

	end go
