PR#3
PREFIX /A2OSX.SRC
NEW
INC 1
AUTO 6
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF /A2OSX.BOOT/SBIN/SHELL
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/PRODOS.I
				.INB INC/A2OSX.I
*--------------------------------------
ZPPTR1			.EQ ZPBIN
ZPPTR2			.EQ ZPBIN+2
ZPPTR3			.EQ ZPBIN+4
*--------------------------------------
CmdBuffer.MAX	.EQ 127
*--------------------------------------
*			File Header (16 Bytes)
*--------------------------------------
CS.START		cld
				jmp (.1,x)
				.DA #$61				6502,Level 1 (65c02)
				.DA #1					BIN Layout Version 1
				.DA 0
				.DA CS.END-CS.START		Code Length To Relocate
				.DA DS.END-DS.START		Data Segment to Allocate
				.DA 0
				.DA 0
*--------------------------------------
* Relocation Table
*--------------------------------------
.1				.DA CS.INIT
				.DA CS.RUN
				.DA CS.DOEVENT
				.DA	CS.QUIT
*--------------------------------------
L.MSG.GREETINGS	.DA MSG.GREETINGS
L.MSG.ECHO		.DA MSG.ECHO
L.MSG.ERROR		.DA MSG.ERROR
L.MSG.PSTR		.DA MSG.PSTR
L.MSG.PRINTENV	.DA MSG.PRINTENV
L.ENV.PATH		.DA ENV.PATH
L.CMD			.DA CMD
L.ARGS			.DA ARGS
L.TIME			.DA TIME
L.STAT			.DA STAT
L.FMT.DATE		.DA FMT.DATE
L.FMT.TIME		.DA FMT.TIME
L.CMDS			.DA CMDS
J.INTCMDS		.DA Cmd.Exec.CD
				.DA Cmd.Exec.DATE
				.DA Cmd.Exec.ECHO
				.DA Cmd.Exec.EXIT
				.DA Cmd.Exec.PAUSE
				.DA Cmd.Exec.READ
				.DA Cmd.Exec.SET
				.DA Cmd.Exec.STARTPROC
				.DA Cmd.Exec.TIME
				.DA Cmd.Exec.TYPE
				.DA 0
*--------------------------------------
CS.INIT			>SYSCALL SYS.GetArgC
				cmp #1

				beq CS.INIT.INTERACTIVE	no arg, continue starting interactive

				lda #1
				>SYSCALL SYS.GetArgA
				jsr TXT.OpenFileYA
				bcs .9
				
				ldy #bExitOnEOF
				lda #$FF
				sta (pData),y
				clc
.9				rts
*--------------------------------------
CS.INIT.INTERACTIVE
				jsr HIS.Init
				bcs .9
				
				ldy #S.PS.hINDEV
				lda (pPs),y
				>PUSHA
				>LDYA L.MSG.GREETINGS
				>SYSCALL SYS.PrintFYA
				
				lda (pPs)
				ora #S.PS.F.EVENT		Now accept events
				sta (pPs)
								
				clc
.9				rts				
*--------------------------------------
CS.RUN			lda #0
				sta (pData)
				
.11				ldy #bPause
				lda (pData),y
				bpl .10
				clc
				rts

.10				ldy #hInputFile			batch mode ?
				lda (pData),y
				bne .2
				
				jsr PrintPrompt
				bcs .99
				
.1				>SYSCALL SYS.Sleep
				lda (pData)
				bpl .1

				and #$7F
				sta (pData)
				bra .3

.2				jsr	TXT.ReadFile
				bcs .81
				
.3				jsr CmdBuffer.Normalize
				lda (pData)
				beq .11					Empty line

				ldy #hInputFile			if batch mode, do not add to cmd history
				lda (pData),y
				bne .4
				
				jsr HIS.Add

.4				>LDYA pData
				jsr Cmd.ExecYA
				bcc .8
				tay
				beq .8
				jsr PrintError
				
.8				ldy	#bEXIT
				lda (pData),y
				bne .99
				
				clc
				rts

.81				jsr TXT.CloseFile
				ldy #bExitOnEOF
				lda (pData),y
				beq .8
				lda #0
.99				sec
				rts	
*--------------------------------------
CS.DOEVENT		ldy #S.EVT.hDEV		is Event from active IN device?
				lda (pEvent),y
				ldy #S.PS.hINDEV
				cmp (pPs),y
				bne .9

				lda (pEvent)
				and #S.EVT.F.KEY		is it a KEY event?
				beq .9

				ldy #S.EVT.DATAHI		is it an O or SAPPLE key ?
				lda (pEvent),y
				bne	.9

				ldy #S.EVT.DATALO	
				lda (pEvent),y
				tax
				
				ldy #bPause
				lda (pData),y
				bpl .11
				
				lda #0
				sta (pData),y
				clc
				rts
				
.9				sec
				rts
				
.11				ldy #hInputFile			Batch mode ?
				lda (pData),y
				beq .10
* Batch Mode			
				cpx #3					test Ctrl-c
				bne .12
				
				jsr TXT.CloseFile
				ldy #bExitOnEOF
				lda (pData),y
				ldy #bExit
				sta (pData),y
				clc
				rts

.12				cpx #19					test Ctrl-s
				bne .8
				ldy #bPause
				lda #$80
				sta (pData),y
				clc
				rts
				
* interactive mode				
.10				ldy #bEscMode
				lda (pData),y
				beq .13
				
				cpx #'['
				beq .8
				
				lda #0
				sta (pData),y
				
				txa

				ldx EscChars
.17				cmp EscChars,x
				beq .18
				dex
				bne .17
				bra .8
				
.18				lda EscAscii,x				
				tax
				
.13				cpx #$20
				bcs .1
				jsr CS.EVENT.CTRL.CHAR
				bra .8

.1				cpx #$7f
				bne .2
				jsr CmdBuffer.DEL
				clc
				rts

.2				lda (pData)				CmdBuffer
				
				cmp #CmdBuffer.MAX
				beq .8
				
				inc
				sta (pData)
				tay
				txa
				sta (pData),y

				>SYSCALL SYS.COutA
				
.8				clc
				rts	
*--------------------------------------
CS.EVENT.CTRL.CHAR
				cpx #13					CR
				bne .10
				txa
				>SYSCALL SYS.COutA
				lda (pData)				CmdBuffer
				ora #$80
				sta (pData)
				clc
				rts
				
.10				cpx #$1B				esc
				bne .11
				
				ldy #bEscMode
				lda #$ff
				sta (pData),y
				clc
				rts

.11				cpx #3					Ctrl-C
				bne .1

				jsr CmdBuffer.CLR
				clc
				rts
				
.1				cpx #8					BS (left arrow)
				bne .2
				jsr CmdBuffer.DEL
				clc
				rts
				
.2				cpx #10					LF (down arrow)
				bne .3
				
				jsr HIS.GetNext
				clc
				rts
				
.3				cpx #11					VT (up arrow)
				bne .4
				
				jsr HIS.GetPrev
				clc
				rts
				
.4				cpx #21					NAK (right arrow)
				bne .8
				
				clc
				rts
				
.8				clc
				rts
*--------------------------------------
CS.QUIT			jsr HIS.Quit

				ldy #hInputFile
				lda (pData),y
				beq .1
				>SYSCALL SYS.FCLOSEA

.1								
.8				clc
				rts
*--------------------------------------
*				PRIVATE
*--------------------------------------
PrintPrompt		lda #'$'
				>SYSCALL SYS.COutA
				bcs .9
				lda #' '
				>SYSCALL SYS.COutA
.9				rts
*--------------------------------------
PrintError		pha
				>PUSHA
				>LDYA L.MSG.ERROR
				>SYSCALL SYS.PrintFYA
				pla
				rts
*--------------------------------------
* Remove extra SPACE & comment
*--------------------------------------
CmdBuffer.Normalize
				>LDYA pData
				>STYA ZPPtr1
				
				lda (pData)				empty line...quit
				beq .9

				tax						Char count
				
				ldy #0
								
.1				jsr CmdBuffer.Normalize.Next
				beq .8					no more char, exit
				
				cmp #' '				skip leading spaces
				beq .1
				
				cmp #'#'				Comment ?
				beq .8					ignore remaining chars
				
.3				iny						add char to buffer
				sta (pData),y
				
				jsr CmdBuffer.Normalize.Next
				beq .8
				
				cmp #' '
				bne .3
				
				iny						add One SPACE to buffer
				sta (pData),y
				
.5				jsr CmdBuffer.Normalize.Next
				beq .8
				cmp #' '
				beq .5					skip additional spaces
				bne .3					no more space, add next word...
				
.8				tya
				sta (pData)		
.9				rts

CmdBuffer.Normalize.Next
				txa
				beq .8
				dex
				inc ZPPtr1
				bne .1
				inc ZPPtr1+1
.1				lda (ZPPtr1)
.8				rts
*--------------------------------------
CmdBuffer.PRINT	ldy #0
.1				iny
				lda (pData),y
				>SYSCALL SYS.COutA
				tya
				cmp (pData)
				bne .1
				rts
*--------------------------------------
CmdBuffer.CLR	jsr CmdBuffer.DEL
				bne CmdBuffer.CLR
				rts
*--------------------------------------
CmdBuffer.DEL	lda (pData)
				beq .9
				lda #8
				>SYSCALL SYS.COutA
				lda (pData)
				dec
				sta (pData)
.9				rts
*--------------------------------------
				.INB SBIN/SHELL.S.CMD
				.INB SBIN/SHELL.S.HIS
				.INB SBIN/SHELL.S.TXT
*--------------------------------------
CS.END
*--------------------------------------
ENV.PATH		>PSTRING "PATH"
CMDS			>PSTRING "CD"
				>PSTRING "DATE"
				>PSTRING "ECHO"
				>PSTRING "EXIT"
				>PSTRING "PAUSE"
				>PSTRING "READ"
				>PSTRING "SET"
				>PSTRING "STARTPROC"
				>PSTRING "TIME"
				>PSTRING "TYPE"
				.HS 00
*--------------------------------------
MSG.GREETINGS	>CSTRING "\nA2osX-Shell on Dev=%h\n\n"
MSG.ECHO		>CSTRING ">%S\n"
MSG.ERROR		>CSTRING "[%h]\n"
FMT.DATE		>PSTRING "%A, %B %d %Y"
FMT.TIME		>PSTRING "%H:%M:%S (%I:%M:%S%p)"
MSG.PSTR		>CSTRING "%S\n"
MSG.PRINTENV	>CSTRING "%S=%S\n"
*--------------------------------------
EscChars		>PSTRING "DBAC"
EscAscii		.HS 04080A0B15
CMD				.BS 256
ARGS			.BS 256
TIME			.BS S.TIME
STAT			.BS S.STAT
hNEWPATH		.BS 1
hCmdLine		.BS 1
Cmd.Exec.Mode	.BS 1
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
CmdBuffer		.BS CmdBuffer.MAX+1		(pData)
bEscMode		.BS 1
bPause			.BS 1
bExit			.BS 1
bEcho			.BS 1
bExitOnEOF		.BS 1
hCmdHistory		.BS 1
CmdHistory.IDX	.BS 1
CmdHistory.END	.BS 1
hInputFile		.BS 1
DS.END
				.ED
*--------------------------------------
MAN
SAVE SBIN/SHELL.S
ASM
