NEW
  AUTO 3,1
*--------------------------------------
				.DUMMY
				.OR ZPTMP+21
PS.Flags		.BS 1
PS.ArgV			.BS 2
PS.ArgSize		.BS 2
PS.NewPSPtr		.BS 2
				.ED
*/--------------------------------------
* # ExecL
* ## C
* `int execl(const char* cmdline, short int flags);`
* ## ASM
* `>PUSHB flags`
* `>LDYA cmdline`
* `>SYSCALL execl`
* ## RETURN VALUE
* A = Child PSID
*\--------------------------------------
K.ExecL			sty .1+1
				sta .2+1
				>PULLB PS.Flags

				>LDYAI 256
				jsr K.GetMem
				bcs .99
				
				stx .90+1
				>STYA PS.ArgV
				>PUSHYA
.1				ldy #$ff				SELF MODIFIED
.2				lda #$ff				SELF MODIFIED

				jsr K.Args2ArgV

				bcs .9
			
				jsr PS.Exec
				
.9				php				
				pha
.90				lda #$ff				SELF MODIFIED
				jsr K.FreeMem
				pla
				plp
				
.99				rts				
*/--------------------------------------
* # ExecV
* ## C
* `int execv(const char* argv[], short int flags);`
* ## ASM
* `>PUSHB flags`
* `>LDYA argv`
* `>SYSCALL execv`
* ## RETURN VALUE
* A = Child PSID
*\--------------------------------------
K.ExecV			>STYA PS.ArgV
				>PULLB PS.Flags
				
PS.Exec			jsr PS.CreateChild		A=PID,Child S.PS at PS.NewPSPtr,PS.Load.hMem
				bcs .9
				sta .8+1

				stz K.IOBuf				If !=0 after PS.Load, it's a script
				jsr PS.Load
				bcs .90

				jsr PS.AddArgV			K.IOBuf= /.../SHELL, K.Buf256=/.../SCRIPT
				bcc .8					or K.IOBuf= 0, K.Buf256=/.../BINFILE
				
.90				pha						save error code
				
.91				lda .8+1
				jsr CORE.PSFree
				pla						get back error code
				sec
				rts

.8				lda #$ff				SELF MODIFIED

.9				rts
*--------------------------------------
* PS.CreateChild
*  in :
*  out :
*   A = PSID
*--------------------------------------
PS.CreateChild	ldx #0
				bra .2					Skip PS0
				
.1				lda PS.Table.PID,x
				beq .4					Found an empty slot
				
.2				inx
				cpx CORE.PSCount
				bne .1
				
				cpx #K.PS.MAX
				bne .3
				
				lda #E.OOH
				sec
				rts

.99				plx				
				rts
				
.3				inc CORE.PSCount				
				
.4				inc CORE.LastPSID		Get a PSID not already running
				beq .4					not = 0
				lda CORE.LastPSID
				jsr CORE.GetPS			Y=PS Index, X,A unmodified
				bcc .4					running...loop

				lda CORE.LastPSID
				sta PS.Table.PID,x
				phx						save PS Index
				
				>LDYAI S.PS
				jsr K.GetMem0			Blank Everything in this S.PS
				bcs .99
				
				>STYA PS.NewPSPtr
				txa
				plx
				sta PS.Table.hPS,x
				
				jsr PS.SetMemOwner		Set Ownership

				lda CORE.LastPSID
				ldy #S.PS.PID
				sta (PS.NewPSPtr),y
				
				lda #S.PS.F.NOHUP
				bit PS.Flags
				beq .5
				
				lda PS.Table.hPS		Select PS0
				jsr K.GetMemPtr
				bra .6
				
.5				>LDYA pPs				
.6				>STYA ZPPtr1				

				ldy #S.PS.PID
				lda (ZPPtr1),y
				dey						#S.PS.PPID
				sta (PS.NewPSPtr),y

				ldy #S.PS.hPREFIX		copy hPREFIX...
				lda (ZPPtr1),y
				jsr K.GetMemPtr
				
				jsr K.StrDup
				bcs .9
				
				jsr PS.SetMemOwner		Set Ownership

				txa
				ldy #S.PS.hPREFIX
				sta (PS.NewPSPtr),y
				
				jsr ENV.Dup				dup hENV from parent PS
				bcs .9

				ldy #S.PS.hENV
				
				sta (PS.NewPSPtr),y
				txa
				dey
				sta (PS.NewPSPtr),y
				
				ldy #S.PS.hStdIn

.7				lda (ZPPtr1),y
				sta (PS.NewPSPtr),y
				iny
				cpy #S.PS.hStdErr+1
				bne .7
				
				ldy #S.PS.hSID
				lda (ZPPtr1),y
				sta (PS.NewPSPtr),y

				lda #S.PS.F.HOLD
				bit PS.Flags
				beq .8
				
				ora (ZPPtr1)
				sta (ZPPtr1)			HOLD parent PS
				
				lda CORE.LastPSID
				ldy #S.PS.CPID
				sta (ZPPtr1),y
				
.8				lda #S.PS.F.INIT
				ora (PS.NewPSPtr)
				sta (PS.NewPSPtr)		Make this PS Init....

				lda CORE.LastPSID		Exit with A=PSID
				clc
				rts
				
.9				pha
				lda	CORE.LastPSID
				jsr CORE.PSFree
				pla
				sec
				rts
*--------------------------------------
PS.Load			ldy #$ff

.1				iny
				lda (PS.ArgV),y
				beq .2					no / in path...try search in PATH
				cmp #'/'
				bne .1
				
				>PUSHWI K.S.STAT
				>LDYA PS.ArgV			found /, some path specified, no search
				ldx #SYS.stat
				jsr K.SYSCALL2.BANK
				bcc .6					REAL path in K.Buf256
				
.99				rts
				
.2				>LDYA PS.ArgV
				jsr ENV.Search.PATH		Get fullpath in K.Buf256
				bcc .6
				
				ldy #S.PS.hPREFIX
				lda (PS.NewPSPtr),y
				jsr K.GetMemPtr
				>STYA ENV.SearchPath
				>LDYA PS.ArgV
				jsr ENV.Search.YA
				bcs .99
				
.6				lda K.S.STAT+S.STAT.P.TYPE
				cmp #S.FI.T.BIN
				beq PS.LoadBIN
				
.7				cmp #S.FI.T.TXT
				beq PS.Load.SCRIPT

*				cmp #S.FI.T.SYS
*				bra *

PS.Load.IBIN	lda #E.IBIN
				.HS 2C
PS.Load.IPATH	lda #E.BADPATH
				sec
PS.Load.RTS		rts

PS.Load.SCRIPT	ldx #$ff

.10				inx						Save Script name in IOBuf
				lda K.Buf256,x
				sta K.IOBuf,x
				bne .10
				
				jsr PS.LoadGetHeader
				bcs PS.Load.RTS

				>LDYAI PS.Load.ROOT
				jsr K.GetEnv
				bcs PS.Load.IPATH

				>STYA .2+1
				
				ldx #$ff

.1				inx
.2				lda $ffff,x				SELF MODIFIED
				sta K.Buf256,x			K.Buf256 = "/ROOT/"
				bne .1
				
				ldy #0
				
.3				lda K.IOBuf+128,y		Check HEADER= "#!/"
				cmp PS.Load.HEADER,y
				bne PS.Load.IBIN
				iny
				cpy #3
				bne .3
				
.4				iny						
				inx
				lda K.IOBuf+128-1,y
				sta K.Buf256-1,x		K.Buf256 = "/ROOT/BIN/SHELL"
				beq .5
				eor #C.CR
				bne .4 
	
				sta K.Buf256-1,x		K.Buf256 = "/ROOT/BIN/SHELL\0"
				
.5				>PUSHWI K.S.STAT
				>LDYAI K.Buf256
				ldx #SYS.stat
				jsr K.SYSCALL2.BANK
				bcs PS.Load.RTS
				
				lda K.S.STAT+S.STAT.P.TYPE
				cmp #S.FI.T.BIN
				bne PS.Load.IBIN		K.Buf256= /.../SHELL, K.IOBuf=/.../SCRIPT
*--------------------------------------
PS.LoadBIN		>LDYAI K.Buf256
				jsr BIN.Load
				bcs .9
				>STYA ZPPtr4			save PTR to Code Segment

				phy
				ldy #S.PS.ZP.CODE+1
				sta (PS.NewPSPtr),y			save pCode HI...
				dey
				pla
				sta (PS.NewPSPtr),y			save pCode LO...
				
				txa
				ldy #S.PS.hCS
				sta (PS.NewPSPtr),y			save CS hMem in S.PS

*				jsr MEM.GetMemByID
*				jsr PS.SetMemOwner		Set Ownership

				ldy #H.BIN.F			Get Bin S.PS.F
				lda (ZPPtr4),y
				beq .1
				
				ldy #S.PS.F				update PS S.PS.F
				ora (PS.NewPSPtr),y
				sta (PS.NewPSPtr),y

.1				ldy #H.BIN.DS.SIZE+1
				lda (ZPPtr4),y			Load DS.SIZE HI
				tax
				dey	
				ora (ZPPtr4),y

				beq .2					DS.SIZE=0...

				lda (ZPPtr4),y
				tay
				txa						Y,A = DS.SIZE
				ldx #S.MEM.F.INIT0+S.MEM.F.DATA
				jsr MEM.GetMem.YAX
				bcs .9

				phy
				ldy #S.PS.ZP.DATA+1
				sta (PS.NewPSPtr),y			save pData HI...
				dey
				pla
				sta (PS.NewPSPtr),y			save pData LO...

				txa
				ldy #S.PS.hDS
				sta (PS.NewPSPtr),y			save DS hMem in S.PS

				jsr PS.SetMemOwner		Set Ownership

.2				ldy #H.BIN.SS.SIZE
				lda (ZPPtr4),y			Load SS.SIZE

				ldy #S.PS.ZP.STACK
				sta (PS.NewPSPtr),y			save pStack LO...
				
				tay						

				bne .3					SS.SIZE.LO=0, set as default $100
				lda #1					SS.SIZE.HI
				.HS 2C					bit abs
.3				lda #0

				ldx #S.MEM.F.ALIGN+S.MEM.F.STACK
				jsr MEM.GetMem.YAX
				bcs .9

				ldy #S.PS.ZP.STACK+1
				sta (PS.NewPSPtr),y			save pStack HI...(LO already set to Stack TOP)

				ldy #S.PS.ZP.LOCAL+1
				sta (PS.NewPSPtr),y
				
				txa
				ldy #S.PS.hSS
				sta (PS.NewPSPtr),y			save SS hMem in S.PS

				jsr PS.SetMemOwner		Set Ownership
				
				ldy #H.BIN.ZP.SIZE
				lda (ZPPtr4),y
				clc
				adc #8					Add room for 4 saved PTR
				ldy #S.PS.ZP.SIZE
				sta (PS.NewPSPtr),y

				clc
.9				rts
*--------------------------------------
PS.LoadGetHeader
				>PUSHWZ				Aux type
				>PUSHBI S.FI.T.TXT
				>PUSHBI	O.RDONLY
				>LDYAI K.IOBuf
				ldx #SYS.fopen
				jsr K.SYSCALL2.BANK
				bcs .9
				sta .1+1

				>PUSHWI 128
				>PUSHWI K.IOBuf+128
.1				lda #$ff				SELF MODIFIED
				ldx #SYS.fread
				jsr K.SYSCALL2.BANK
				
				php
				pha
				lda .1+1
				ldx #SYS.fclose
				jsr K.SYSCALL2.BANK
				pla
				plp
.9				rts
*--------------------------------------
PS.Load.ROOT	.AZ "ROOT"
PS.Load.HEADER	.AZ "#!/"
*--------------------------------------
* K.Buf256= /.../SHELL, K.IOBuf=/.../SCRIPT
* - or - 
* K.Buf256=/.../BINFILE,K.IOBuf=0 
* Buf256+IOBUF(?)+PS.ArgV[1,...] -> #S.PS.hARGV
*--------------------------------------
PS.AddArgV		ldy #$ff				In both cases, remove ARGV[0]
				
.60				iny
				lda (PS.ArgV),y
				bne .60
				
				tya
				sec
				adc PS.ArgV
				sta PS.ArgV
				bcc .70
				
				inc PS.ArgV+1			Skip old ARGV[0] = scriptename
				
.70				>LDYAI K.Buf256
				jsr K.strlen
				>STYA PS.ArgSize
				
				lda K.IOBuf				is there a /BIN/SHELL ?
				beq .10
				
				>LDYAI K.IOBuf
				jsr K.strlen
				jsr PS.AddYAp12ArgSize

.10				ldx #0					For now, 0 args...
				lda K.IOBuf
				beq .11					+1 for scriptpath
				inx
				
.11				>LDYA PS.ArgV
				>STYA ZPPtr1

.1				lda (ZPPtr1)
				beq .4
				
				inx
				
				ldy #0
				
.2				iny
				lda (ZPPtr1),y
				bne .2
				
*				lda #0				
				jsr PS.AddYAp12ArgSize
				
.3				jsr MEM.AddYp1ToPtr1
				bra .1

.4				txa
				ldy #S.PS.ARGC
				sta (PS.NewPSPtr),y
				
				>LDYA PS.ArgSize		Add 1 for ending 0
				iny
				bne .5
				inc

.5				jsr K.GetMem
				bcs PS.AddPtr1ToStrV.RTS
				
				>STYA ZPPtr2			Target Buffer
				txa
				ldy #S.PS.hARGV
				sta (PS.NewPSPtr),y

				>LDYAI K.Buf256
				jsr PS.AddYAToStrV
				
				lda K.IOBuf
				beq .6
				
				>LDYAI K.IOBuf
				jsr PS.AddYAToStrV
				
.6				>LDYA PS.ArgV
				>STYA ZPPtr1
*--------------------------------------
PS.AddPtr1ToStrVUntil0
.1				lda (ZPPtr1)
				sta (ZPPtr2)
				beq PS.AddPtr1ToStrV.8	Ending \0
				
				jsr PS.AddPtr1ToStrV
				bra .1
*--------------------------------------
PS.AddYAToStrV	>STYA ZPPtr1
PS.AddPtr1ToStrV
				ldy #$ff
				
.2				iny
				lda (ZPPtr1),y
				sta (ZPPtr2),y
				bne .2
				
				jsr MEM.AddYp1ToPtr1
				
				tya
				sec
				adc ZPPtr2
				sta ZPPtr2
				bcc PS.AddPtr1ToStrV.RTS
				inc ZPPtr2+1
PS.AddPtr1ToStrV.8
				clc
PS.AddPtr1ToStrV.RTS
				rts
*--------------------------------------
PS.AddYAp12ArgSize
				pha
				tya
				sec						+1 for \0
				adc PS.ArgSize
				sta PS.ArgSize
				pla
				adc PS.ArgSize+1
				sta PS.ArgSize+1
				rts
*--------------------------------------
PS.SetMemOwner	lda CORE.LastPSID
				ldy #S.MEM.OWNERPID
				sta (ZPMemMgrSPtr),y
				rts
*/--------------------------------------
* # ExecL
* ## C
* `int kill(short int pid, short int sig);`
* ## ASM
* `>PUSHB sig`
* `lda pid`
* `>SYSCALL kill`
* ## RETURN VALUE
* A = Child PSID
*\--------------------------------------				
K.Kill			cmp #0
				beq .99					CS
				
				jsr CORE.GetPSByID
				bcs .99
				>STYA ZPPtr1
				
				>PULLA
				cmp #2
				bcs .9
				asl
				tax
				jmp (.1,x)
				
.1				.DA K.Kill.0
				.DA K.Kill.1
				
.9				lda #E.SYN
*				sec
				rts
				
.99				>RET 1
*--------------------------------------
K.Kill.1		lda (ZPPtr1)

				and #$33
				ora #S.PS.F.QUIT
				sta (ZPPtr1)
				
K.Kill.0		
				clc
				rts
*--------------------------------------
MAN
SAVE USR/SRC/SYS/KERNEL.S.PS
LOAD USR/SRC/SYS/KERNEL.S
ASM
