PR#3
PREFIX /A2OSX.SRC
NEW
INC 1
AUTO 6
				.LIST OFF	
*--------------------------------------
* Y,A -> Command line (formatted & not empty)
*--------------------------------------
Cmd.ExecYA		stz	Cmd.Exec.Mode
Cmd.ExecYA.1	>SYSCALL SYS.ExpandPStrYA
				bcs .99
				stx hCmdLine

				>STYA ZPPtr1
				
				ldy #1
				ldx #0					ARGS len
				
				lda (ZPPtr1),y
.10				cmp #'a'
				bcc .11
				cmp #'z'+1
				bcs .11
				eor #$20				to Uppercase
				
.11				sta CMD,y				Store Cmd in Buffer
				sty CMD					update CMD len	
				
				tya
				cmp (ZPPtr1)
				beq .13					end of string
				
				iny
				lda (ZPPtr1),y
				cmp #' '
				bne .10
				
.12				tya
				cmp (ZPPtr1)
				beq .13					end of string
				
				iny
				lda (ZPPtr1),y
				inx
				sta ARGS,x
				bra .12
				
.13				stx ARGS				update ARGS len

				lda hCmdLine			Discard Expanded Cmd Line
				>SYSCALL SYS.FreeMemA

				>LDYA L.CMDS
				>STYA ZPPtr2
				
				ldx #0
				
.1				lda (ZPPtr2)
				beq Cmd.Exec.EXT		Ending 0, must be an external Cmd....
				
				cmp CMD
				bne .4
				
				tay
				
.2				lda CMD,y
.3				cmp (ZPPtr2),y
				bne .4
				dey
				bne .2
				
				jmp (J.INTCMDS,x)		Found an internal Cmd...
				
.4				inx
				inx
				
				lda ZPPtr2
				sec
				adc (ZPPtr2)
				sta ZPPtr2
				bcc .1
				inc ZPPtr2+1
				bra .1
				
.99				rts	
*--------------------------------------
Cmd.Exec.EXT	ldy #1
				lda CMD,y
				cmp #'/'				Command line is already full path, no search
				bne .10
				jmp .3
				
.10				>LDYA L.ENV.PATH		push ENVNAME=PATH for search
				>SYSCALL SYS.GetEnvYA	get value for ENV=PATH
				bcs .1					No PATH, try in CD

				>PUSHYA					push search list
				
				>PUSHW L.CMD
				>SYSCALL SYS.FileSearch
				bcc .2

.1				ldy #S.PS.hPREFIX		not found, try in CD
				lda (pPs),y
				>SYSCALL SYS.GetMemPtrA
				>PUSHYA					push search list
				>PUSHW L.CMD
				>SYSCALL SYS.FileSearch
				bcs .99

.2				phx 					save X=hMem, Y,A = Filename
				>PUSHYA

				
				>PUSHW L.CMD			replace CMD in TmpBuffer with full path
				>SYSCALL SYS.PStrCpy
				pla
				>SYSCALL SYS.FreeMemA	Discard this string...
				
.3				stz UsrBuf256		reset UsrBuf256 for final CMDLINE

				>PUSHW L.STAT
				>PUSHW L.CMD
				>SYSCALL SYS.STAT
				bcs .99

				lda STAT+S.STAT.PRODOS.TYPE
				cmp #$04				TXT File ?
				beq Cmd.Exec.EXT.TXT
				cmp #$06				BIN File ?
				beq Cmd.Exec.EXT.BIN
				cmp #$ff				SYS File ?
				bne .9
				
				bra *
				
.9				lda #SYSMGR.ERRSYN
				sec	
.99				rts
*--------------------------------------
* TXT : Launch "/PATH/SHELL /PATH/CMD ARGS"
*--------------------------------------
Cmd.Exec.EXT.TXT
				lda #0					Get arg[0] = /PATH/SHELL
				>SYSCALL SYS.GetArgA
				>PUSHYA
				>PUSHWI UsrBuf256
				>SYSCALL SYS.PStrCpy
				
				inc UsrBuf256		Add a space....
				ldy UsrBuf256
				lda #' '
				sta UsrBuf256,y
*--------------------------------------
* BIN : Launch "/PATH/CMD ARGS"
*--------------------------------------
Cmd.Exec.EXT.BIN
				>PUSHW L.CMD
				>PUSHWI UsrBuf256
				>SYSCALL SYS.PStrCat
				
				lda ARGS
				beq .1
				
				inc UsrBuf256		Add a space....
				ldy UsrBuf256
				lda #' '
				sta UsrBuf256,y
				
				>PUSHW L.ARGS
				>PUSHWI UsrBuf256
				>SYSCALL SYS.PStrCat
			
.1				>LDYAI UsrBuf256
				bit Cmd.Exec.Mode
				bmi .2					startproc
				
				>SYSCALL SYS.ExecProcessNewEnvYA
				rts

.2				>SYSCALL SYS.CreateProcessYA
				rts
*--------------------------------------
* Internal Commands
*--------------------------------------
Cmd.Exec.CD		lda ARGS
				bne Cmd.Exec.CD1
				ldy #S.PS.hPREFIX
				lda (pPs),y
				>SYSCALL SYS.GetMemPtrA
				>PUSHYA
				>LDYA L.MSG.PSTR
				>SYSCALL SYS.PrintFYA
				rts
				
Cmd.Exec.CD1	lda ARGS+1
				cmp #'/'				Full Path?
				bne .1
				
				>LDYA L.ARGS
				bra Cmd.Exec.CD.Change
				
.1				ldy #S.PS.hPREFIX		no, init target prefix with actual
				lda (pPs),y
				>SYSCALL SYS.GetMemPtrA
				>PUSHYA
				>PUSHWI UsrBuf256
				>SYSCALL SYS.PStrCpy
				
				lda ARGS+1
				cmp #'.'				".." ?
				bne .3
				
				cmp ARGS+2				".." ?
				
				bne .9

				ldy UsrBuf256		remove last dir component in actual prefix
				
.2				dey
				beq .9					nothing to remove, synerr
				
				lda UsrBuf256,y
				cmp #'/'
				bne .2
				
				sty UsrBuf256
				
				>LDYAI UsrBuf256
				bra Cmd.Exec.CD.Change
				
.9				lda #SYSMGR.ERRSYN
				sec
				rts

.3				>PUSHW L.ARGS			Append ARG to buffer
				>PUSHWI UsrBuf256
				>SYSCALL SYS.PStrCat
				bcs .9
				
				ldx UsrBuf256
				lda #'/'				Ending with '/'?
				cmp UsrBuf256,x
				beq .4
				sta UsrBuf256+1,x
				inc UsrBuf256
				
.4				>LDYAI UsrBuf256
				
Cmd.Exec.CD.Change
				>STYA ZPPtr1
				
				>PUSHW L.STAT
				>PUSHW ZPPtr1
				>SYSCALL SYS.STAT
				bcs .9

				lda STAT+S.STAT.PRODOS.TYPE
				sec
				eor #$0F				Directory ?
				bne .9
				
				>LDYA ZPPtr1
				>SYSCALL SYS.NewPStrYA
				bcs .9
				
				phx
				
				ldy #S.PS.hPREFIX
				lda (pPs),y
				>SYSCALL SYS.FreeMemA
				
				pla
				
				ldy #S.PS.hPREFIX
				sta (pPs),y
				
				clc
.9				rts
*--------------------------------------
Cmd.Exec.STARTPROC
				lda ARGS
				beq Cmd.Exec.ERRSYN
				sec
				ror	Cmd.Exec.Mode

				>LDYA L.ARGS
				jmp Cmd.ExecYA.1

Cmd.Exec.ERRSYN	lda #SYSMGR.ERRSYN
				sec
				rts
*--------------------------------------
Cmd.Exec.SET	lda ARGS
				bne Cmd.Exec.SETVAR
				
				ldy #S.PS.hENV
				lda (pPs),y
				>SYSCALL SYS.GetMemPtrA
				>STYA ZPPTR1
				
* No arg, print all ENV
		
.1				lda (ZPPTR1)
				beq .8					Ending 0 ?

				lda ZPPTR1				get next for value
				sec
				adc (ZPPTR1)			Add len+1 to PTR
				sta ZPPTR2
				lda ZPPTR1+1
				adc #0
				sta ZPPTR2+1
				
				lda (ZPPTR2)			just in case...
				beq .8	
				
				>PUSHW ZPPTR2			Push value
				>PUSHW ZPPTR1			Push name

				>LDYA L.MSG.PRINTENV
				>SYSCALL SYS.PrintFYA
				bcs .9

				lda ZPPTR2
				sec
				adc (ZPPTR2)			Add len+1 to PTR
				sta ZPPTR1
				lda ZPPTR2+1
				adc #0
				sta ZPPTR1+1
				bra .1
				
.8				clc
.9				rts				
				
Cmd.Exec.SETVAR	tax
				lda ARGS,x
				cmp #'='				ARGS ends with =, UnsetEnv
				beq .2
				
.1				dex
				beq .3					no =, GetEnv
				
				lda ARGS,x
				cmp #'='				ARGS contains =, PutEnv
				bne .1
				
				cpx #1					string is '=value' ?
				beq Cmd.Exec.ERRSYN		syntax error
				
				>LDYA L.ARGS			String is VAR=VALUE...	
				>SYSCALL SYS.PutEnvYA
				rts
				
.2				dec ARGS				String is "VAR=", Remove endig '='
				>LDYA L.ARGS			String is "VAR"...	
				>SYSCALL SYS.UnsetEnvYA
				rts

* Print requeted VAR
				
.3				>LDYA L.ARGS
				>SYSCALL SYS.GetEnvYA
				bcs .8
				
				>PUSHYA					push value
				>PUSHW L.ARGS			push name
				>LDYA L.MSG.PRINTENV
				>SYSCALL SYS.PrintFYA
				
.8				clc
.9				rts
*--------------------------------------
Cmd.Exec.DATE	sec
				.HS 90					bcc

Cmd.Exec.TIME	clc
				php
				>LDYA L.TIME
				>SYSCALL SYS.TimeYA

				>PUSHWI UsrBuf256
				plp
				bcc .1
				>PUSHW L.FMT.DATE
				bra .2
.1				>PUSHW L.FMT.TIME

.2				>PUSHW L.TIME
				>SYSCALL SYS.PStrFTime
				
				>PUSHWI UsrBuf256
				>LDYA L.MSG.PSTR
				>SYSCALL SYS.PrintFYA
				rts
*--------------------------------------
Cmd.Exec.ECHO	lda ARGS
				beq .9
				>PUSHW L.ARGS
				>LDYA L.MSG.PSTR
				>SYSCALL SYS.PrintFYA
				clc
				rts
				
.9				lda #SYSMGR.ERRSYN
				sec
				rts
*--------------------------------------
Cmd.Exec.TYPE	lda ARGS
				beq .98
				>PUSHWI 0				Aux type
				>PUSHBI 4				S.FILEINFO.TYPE.TXT
				>PUSHBI	SYS.FOPEN.R
				>PUSHW L.ARGS
				>SYSCALL SYS.LoadFile
				bcs .99

				>STYA ZPPTR2			store file len
				phx
				txa
				>SYSCALL SYS.GetMemPtrA
				>STYA ZPPTR1
.1				lda ZPPTR2
				bne .2
				lda ZPPTR2+1
				beq .3
				dec ZPPTR2+1
.2				dec ZPPTR2

				lda (ZPPTR1)
				>SYSCALL SYS.COutA
				inc ZPPTR1
				bne .1
				inc ZPPTR1+1
				bra .1

.3				pla
				>SYSCALL SYS.FreeMemA
				clc
				rts
				
.98				lda #SYSMGR.ERRSYN
				sec
.99				rts
*--------------------------------------
Cmd.Exec.READ
				clc
				rts
*--------------------------------------
Cmd.Exec.PAUSE	ldy #bPause
				lda #$80
				sta (pData),y
				clc
				rts
*--------------------------------------
Cmd.Exec.EXIT	ldy #bEXIT
				lda #$FF
				sta (pData),y
				clc
				rts
*--------------------------------------
MAN
SAVE SBIN/SHELL.S.CMD
LOAD SBIN/SHELL.S
ASM
