NEW
  AUTO 3,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF bin/shutdown
*--------------------------------------
				.INB inc/macros.i
				.INB inc/a2osx.i
				.INB inc/kernel.i
*--------------------------------------
DEFAULT.TIMEOUT	.EQ 60
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPtr1			.BS 2
CmdPtr			.BS 2
ArgPtr			.BS 2

Count			.BS 1
Index			.BS 1

Timeout			.BS 4
bSec			.BS 1
bTimeout		.BS 1
bRestart		.BS 1
ZS.END			.ED
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA #S.PS.F.EVENT
				.DA #0
				.DA CS.END-CS.START		Code Size (without Constants)
				.DA DS.END-DS.START		Data SegmentSize
				.DA #32					Stack Size
				.DA #ZS.END-ZS.START	Zero Page Size
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT
				.DA	CS.QUIT
L.MSG.USAGE		.DA MSG.USAGE
L.MSG.CRLF		.DA MSG.CRLF
L.MSG.SHUTDOWN	.DA MSG.SHUTDOWN
L.MSG.PS		.DA MSG.PS
L.MSG.DEV		.DA MSG.DEV
				.DA 0
*--------------------------------------
CS.INIT			clc
				rts
*--------------------------------------
CS.RUN			lda #DEFAULT.TIMEOUT
				sta Timeout

.1				inc Index
				lda Index
				>SYSCALL ArgV
				bcs .8

				>STYA ZPPtr1

				lda (ZPPtr1)
				cmp #'-'
				bne .5

				ldy #2
				lda (ZPPtr1),y
				bne .9

				dey
				lda (ZPPtr1),y

				cmp #'a'
				bcc .3

				cmp #'z'+1
				bcs .3

				eor #$20

.3				cmp #'T'
				bne .4

				inc Index
				lda Index
				>SYSCALL ArgV
				bcs .9

				>SYSCALL AToL
				bcs .9

				>PULLL Timeout

				bra .1

.4				cmp #'R'
				bne .9

				sec
				ror bRestart
				bra .1
				
.5				>LDYA ZPPtr1
				ldx CmdPtr+1
				bne .6

				>STYA CmdPtr
				lda #3
				sta Timeout
				bra .1

.6				ldx ArgPtr+1
				bne .1	

				>STYA ArgPtr
				bra .1

.8				jsr CS.RUN.WAIT
				bcs .99

				jsr CS.RUN.PS
				jsr CS.RUN.DEV
				jsr CS.RUN.SYS

				>PUSHBI 1					INITD
				>PUSHA						SIGNAL
				>SYSCALL Kill

				lda #0
				sec
.99				rts
*--------------------------------------
.9				>LDYA L.MSG.USAGE
				>SYSCALL PutS

				lda #E.SYN
				sec
				rts
*--------------------------------------
CS.RUN.WAIT		>PUSHW L.MSG.SHUTDOWN
				>PUSHL Timeout
				>PUSHBI 4
				>SYSCALL PrintF

.1				>SLEEP

				ldy #S.PS.hStdIn
				lda (pPS),y
				>SYSCALL FEOF
				bcs .2

				tay
				bne .2					CC

				>SYSCALL GetChar
				bcs .9

				cmp #$03				Ctrl-C
				beq .9					CS

.2				lda bSec
				bpl .1

				stz bSec

				lda bTimeout
				bpl CS.RUN.WAIT

				>PUSHW L.MSG.CRLF
				>PUSHBI 0
				>SYSCALL PrintF

				clc
.9				rts
*--------------------------------------
CS.RUN.PS		stz Count

				ldx #0
				stx Index

.1				lda PSs.hPS,x
				beq .7

				>SYSCALL GetMemPtr
				>STYA ZPPtr1

.2				lda (ZPPtr1)
				bpl .4					HOLD ?

				ldy #S.PS.CPID
				lda (ZPPtr1),y

.3				jsr CS.GetPSByID
				bra .2

.4				ldy #S.PS.S
				lda (ZPPtr1),y
				cmp #S.PS.S.QUIT
				beq .6

				ldy #S.PS.PID
				lda (ZPPtr1),y
				beq .7					don't kill PS0

				cmp #1
				beq .7					don't kill INITD

				cmp (pPS),y				don't kill myself
				beq .7

				jsr CS.RUN.KILL

.5				ldy #S.PS.PPID
				lda (ZPPtr1),y
				bne .3					go kill parent if not 0

.6				inc Count

.7				>SLEEP

				inc Index
				ldx Index
				cpx #K.PS.MAX
				bne .1

				lda Count
				bne CS.RUN.PS

				sec
				rts
*--------------------------------------
CS.RUN.KILL		>PUSHW L.MSG.PS
				ldy #S.PS.PID
				lda (ZPPtr1),y
				>PUSHA

				ldy #S.PS.hARGV
				lda (ZPPtr1),y
				>SYSCALL GetMemPtr
				>PUSHYA

				>PUSHBI 3
				>SYSCALL PrintF

				ldy #S.PS.PID
				lda (ZPPtr1),y
				>PUSHA
				>PUSHBI 1				SIG
				>SYSCALL Kill
				rts
*--------------------------------------
CS.RUN.DEV		ldx #K.FILE.MAX
				
.1				dex
				stx Index
				lda FILEs.hFD,x
				beq .7

				>SYSCALL GetMemPtr
				>STYA ZPPtr1

				>PUSHW L.MSG.DEV
				>PUSHB Index

				lda ZPPtr1
				clc
				adc #S.FD.DEV
				tay
				lda ZPPtr1+1
				adc /S.FD.DEV
				>PUSHYA

				>PUSHBI 3
				>SYSCALL PrintF
				
				ldx Index
				>PUSHB FILEs.hFD,x
				>PUSHBI IOCTL.CLOSE
				>PUSHWZ
				>SYSCALL IOCTL

.7				ldx Index
				bne .1

				rts
*--------------------------------------
CS.RUN.SYS		stz $280

				>LDYA CmdPtr
				beq .8
				
				>PUSHYA
				>PUSHWI $281
				>SYSCALL RealPath
				bcs .8
				
				ldx #$ff
				
.1				inx
				lda $281,x
				bne .1
				
				stx $280
				
				>LDYA ArgPtr
				beq .8
	
				>PUSHYA
				>PUSHBI 2
				txa						Arg0 len
				ora #$80				$280
				inc						+1 byte len
				pha
				inc						skip arg1 len byte
				>PUSHA
				>SYSCALL RealPath
				
				ply
				ldy $280
				ldx #$ff
				
.2				iny
				inx
				lda $281,y
				bne .2
				
				txa
				ldy $280
				iny
				sta $280,y

.8				rts
*--------------------------------------
CS.GetPSByID	ldx #0

.1				ldy PSs.hPS,x
				beq .7

				pha
				tya

				phx
				>SYSCALL GetMemPtr
				>STYA ZPPtr1
				plx

				pla

				ldy #S.PS.PID
				cmp (ZPPtr1),y
				beq .8

.7				inx
				cpx #K.PS.MAX
				bne .1

*				sec

				rts

.8				clc
				rts
*--------------------------------------
CS.DOEVENT		lda (pEvent)
				bpl .9
				asl
				bpl .9

				sta bSec

				ldx #4
				ldy #0

				clc

.1				lda Timeout,y
				sbc #0
				sta Timeout,y
				iny
				dex
				bne .1

				ror
				eor #$80
				sta bTimeout

.9				sec
				rts
*--------------------------------------
CS.QUIT			clc
				rts
*--------------------------------------
CS.END
MSG.USAGE		.CS "Usage : SHUTDOWN <NEXT.SYSTEM>\r\n"
				.CS "   -R : Restart System\r\n"
				.CS "   -T <seconds> : default to 60"
MSG.CRLF		.CZ "\r\n"
MSG.SHUTDOWN	.CZ "\r\e[KSHUTDOWN:System will stop in %u seconds..."
MSG.PS			.CZ "SHUTDOWN:Killing process %3d, %s\r\n"
MSG.DEV			.CZ "SHUTDOWN:Closing device  %3d, %s\r\n"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
DS.END			.ED
*--------------------------------------
MAN
SAVE usr/src/bin/shutdown.s
ASM
