NEW
PREFIX
AUTO 4,1
				.LIST OFF
				.OP	65C02
				.OR	$2000
				.TF BIN/SHELL
*--------------------------------------
CL.MAX			.EQ 159
History.MAX		.EQ 256
CORE.STACK.MAX	.EQ 64
PUSHD.STACK.MAX	.EQ 8
*--------------------------------------
C.STARTPROC		.EQ 0
C.CD			.EQ 2
C.DATE			.EQ 4
C.ECHO			.EQ 6
C.EXIT			.EQ 8
C.GETKEY		.EQ 10
C.MD			.EQ 12
C.PAUSE			.EQ 14
C.PWD			.EQ 16
C.RD			.EQ 18
C.READ			.EQ 20
C.REN			.EQ 22
C.SET			.EQ 24
C.SLEEP			.EQ 26
C.SHIFT			.EQ 28
C.TIME			.EQ 30
C.PUSHD			.EQ 32
C.POPD			.EQ 34
C.BREAK			.EQ 36
C.CONTINUE		.EQ 38
*--------------------------------------
C.FOR			.EQ 40
C.NEXT			.EQ 42
C.WHILE			.EQ 44
C.LOOP			.EQ 46
C.IF			.EQ 48
C.ELSE			.EQ 50
C.FI			.EQ 52
*--------------------------------------
E.CSYN			.EQ $C0
E.SSYN			.EQ $C1
E.ESYN			.EQ $C2
E.STKOVERFLOW	.EQ $C3
E.NOWHILE		.EQ $C4
E.NOFOR			.EQ $C5
E.NOIF			.EQ $C6
E.BADEXP		.EQ $C7
E.BADFTYPE		.EQ $C8
*--------------------------------------
				.INB INC/MACROS.I
				.INB INC/A2OSX.I
				.INB INC/MLI.I
				.INB INC/MLI.E.I
*--------------------------------------
				.DUMMY
				.OR ZPBIN
ZS.START
ZPPtr1			.BS 2
ZPPtr2			.BS 2
ZPPtr3			.BS 2
ZPCLBuf			.BS 2
ZPCLBufPtr		.BS 2
ZPInputBufPtr	.BS 2
ZPInputBufPtrBak	.BS 2
ZPArgVBuf		.BS 2
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		S.PS.F
				.DA #0
				.DA CS.END-CS.START		Code Size (without Constants)
				.DA DS.END-DS.START		Data SegmentSize
				.DA #64					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.GREETINGS	.DA MSG.GREETINGS
L.MSG.PROMPT	.DA MSG.PROMPT
L.MSG.TRACE		.DA MSG.TRACE
L.MSG.ECHOCRLF	.DA MSG.ECHOCRLF
L.MSG.BATCHERR	.DA MSG.BATCHERR
L.MSG.ERROR		.DA MSG.ERROR
L.MSG.PRINTENV	.DA MSG.PRINTENV
L.MSG.PID		.DA MSG.PID
L.ENV.ROOT		.DA ENV.ROOT
L.ENV.PATH		.DA ENV.PATH
L.ENV.PWD		.DA ENV.PWD
L.ENV.PS1		.DA ENV.PS1
L.ENV.HOME		.DA ENV.HOME
L.FMT.DATE		.DA FMT.DATE
L.FMT.TIME		.DA FMT.TIME
L.FMT.GETKEY	.DA FMT.GETKEY
J.ESC			.DA CL.BS			left arrow
				.DA HIS.GetNext			
				.DA HIS.GetPrev
				.DA CL.NAK			right arow
L.CL.IO			.DA CL.IO
J.CL.IO			.DA CL.IO.AMP
				.DA CL.IO.IN
				.DA CL.IO.OUTA
				.DA CL.IO.OUT
				.DA CL.IO.1OUTA
				.DA CL.IO.1OUT
				.DA CL.IO.2OUTA
				.DA CL.IO.2OUT
L.CORE.INT		.DA CORE.INT
J.CORE.INT		.DA CORE.INT.STARTPROC
				.DA CORE.INT.CD
				.DA CORE.INT.DATE
				.DA CORE.INT.ECHO
				.DA CORE.INT.EXIT
				.DA CORE.INT.GETKEY
				.DA CORE.INT.MD
				.DA CORE.INT.PAUSE
				.DA CORE.INT.PWD
				.DA CORE.INT.RD
				.DA CORE.INT.READ
				.DA CORE.INT.REN
				.DA CORE.INT.SET
				.DA CORE.INT.SLEEP
				.DA CORE.INT.SHIFT
				.DA CORE.INT.TIME
				.DA CORE.INT.PUSHD
				.DA CORE.INT.POPD
				.DA CORE.INT.BREAK
				.DA CORE.INT.CONTINUE
				.DA CORE.INT.FOR
				.DA CORE.INT.NEXT
				.DA CORE.INT.WHILE
				.DA CORE.INT.LOOP
				.DA CORE.INT.IF
				.DA CORE.INT.ELSE
				.DA CORE.INT.FI
L.CORE.Eval.TOKEN1	.DA CORE.Eval.TOKEN1
L.CORE.Eval.TOKEN2	.DA CORE.Eval.TOKEN2
J.CORE.Eval.TOKEN2	.DA CORE.Eval.D
					.DA CORE.Eval.E
					.DA CORE.Eval.F
L.CORE.Eval.TOKEN3	.DA CORE.Eval.TOKEN3
L.CORE.Eval.TOKEN4	.DA CORE.Eval.TOKEN4
J.CORE.Eval.TOKEN4	.DA M32.Add
				.DA M32.Sub
				.DA M32.Mul
				.DA M32.Div
				.DA M32.Mod
L.HOME.PROFILE	.DA HOME.PROFILE
L.M32.Printf	.DA M32.Printf
L.ERR.Codes		.DA ERR.Codes
L.ERR.Messages	.DA ERR.Messages
				.DA 0
*--------------------------------------
CS.INIT			clc
CS.INIT.RTS		rts
*--------------------------------------
CS.RUN			jsr CMD.Init
				bcs CS.INIT.RTS

				ldy #S.PS.ARGC
				lda (pPS),y
				beq .1					no arg, continue starting interactive
				
				>PUSHWI 0				Aux type
				>PUSHBI S.FI.T.TXT
				>PUSHBI	O.RDONLY
				lda #1
				>SYSCALL ArgV
				>SYSCALL LoadTxtFile
				bcs CS.INIT.RTS
				
				txa
				>STA.G hFileBuf
				>SYSCALL GetMemPtr
				>STYA ZPInputBufPtr
				
				lda #0
				jsr ShiftA 				Remove $0=/bin/shell
				
				bra CS.RUN.LOOP
*--------------------------------------				
.1				jsr CORE.INT.CD.HOME
				jsr CL.Init
				jsr HIS.Init
				bcs CS.INIT.RTS

				>PUSHWI K.VER
				>PUSHBI 2
				>LDYA L.MSG.GREETINGS
				>SYSCALL printf
				bcs CS.INIT.RTS
				
				>PUSHEA.G StatBuf
				>LDYA L.HOME.PROFILE
				>SYSCALL STAT
				bcs CS.RUN.LOOP
				
				>PUSHBI S.PS.F.HOLD
				>LDYA L.HOME.PROFILE
				>SYSCALL execl
				>SLEEP
*--------------------------------------
CS.RUN.LOOP		jsr IO.Reset

.1				jsr CheckSleep
				beq .3
				
				>SLEEP
				>SYSCALL GetChar
				bcs .2
				
				cmp #3					Ctrl-C
				bne .1
				
				>LDA.G hFileBuf			batch mode ?
				beq .1
				
				>LDA.G bSET.C
				beq .8
				bra .1

.2				tay
				beq .1
				rts						I/O err

.3				>LDA.G bPause
				bpl .6
				
.4				>SLEEP
				>SYSCALL GetChar
				bcc .5					got one
				tay
				beq .4					no char
				rts						I/O err
				
.5				cmp #3					Ctrl-C
				bne .60
				
				>LDA.G hFileBuf			batch mode ?
				beq .61
				
				>LDA.G bSET.C
				beq .8
				bra .61
				
.60				cmp #C.CR
				bne .4
				
.61				>STZ.G bPause
				bra .62

.6				>LDA.G hVarName			read mode ?
				bne .7
				
.62				>LDA.G hFileBuf			batch mode ?
				beq CS.RUN.INTERACTIVE
				jmp CS.RUN.BATCH
.7				jmp CS.RUN.READ

.8				lda #3					User Interrupt
.9				sec
				rts
*--------------------------------------
CS.RUN.INTERACTIVE
				jsr CL.Reset
				jsr CL.PrintPrompt
				bcs .9

.1				>SLEEP
				>SYSCALL GetChar
				bcc .2

				tay
				beq .1					no char

.9				rts						I/O error 
				
.2				cmp #C.EOF				....or Ctrl-D
				beq .9					CS

				jsr CL.CHARIN

				>LDA.G CL.bExec			Something to execute ?
				bpl .1
				
				lda (ZPCLBuf)
				beq .8					Empty line

				jsr HIS.Add

				jmp CS.RUN.Exec
.8				jmp CS.RUN.LOOP				
*--------------------------------------
CS.RUN.READ
.1				>SLEEP
				>SYSCALL GetChar
				bcs .1					no char

				jsr CL.CHARIN

				>LDA.G CL.bExec
				bpl .1

				lda (ZPCLBuf)
				bne .2
				
				>LDA.G hVarName
				>SYSCALL GetMemPtr
				>SYSCALL UnSetEnv
				bra .7
				
.2				>PUSHW ZPCLBuf

				>LDA.G hVarName
				>SYSCALL GetMemPtr
				>SYSCALL SetEnv
				
.7				php
				pha
				
				>LDA.G hVarName
				>SYSCALL freemem
				>STZ.G hVarName
				>STA.G bREAD.S
				
				pla
				plp
				
.8				jmp CS.RUN.LOOP.END
*--------------------------------------
CS.RUN.BATCH	>SYSCALL GetChar
				bcs .2
				
				cmp #3					test Ctrl-c
				bne .1

*				sec
.9				rts

.1				cmp #19					test Ctrl-s
				bne .2

				lda #$80
				>STA.G bPause
				clc
				jmp CS.RUN.LOOP.END

.2				jsr CMD.Get
				bcc .7

				cmp #C.EOF
				bne .3

				lda #$ff
				>STA.G bExit
				inc
				clc
				bra CS.RUN.LOOP.END
				
.3				sec
				bra CS.RUN.LOOP.END

.7				>LDA.G CL.bExec
				bpl CS.RUN.LOOP.80

				lda (ZPCLBuf)
				beq CS.RUN.LOOP.80

				>LDA.G bSET.X
				beq CS.RUN.Exec

				>PUSHW ZPCLBuf
				>PUSHBI 2
				>LDYA L.MSG.TRACE
				>SYSCALL printf
				
				bcs .9
*--------------------------------------
CS.RUN.Exec		jsr CL.Parse
				bcs CS.RUN.LOOP.END
				
				lda (ZPArgVBuf)
				beq CS.RUN.LOOP.8
				jsr CORE.Exec
*--------------------------------------
CS.RUN.LOOP.END	ldy #S.PS.RC
				sta (pPs),y
				
				bcc CS.RUN.LOOP.8

				>LDA.G hFileBuf			batch mode ?
				beq .1
				
				jsr IO.PrintBatchErrMsg
				bcs CS.RUN.LOOP.RTS
				
.1				ldy #S.PS.RC
				lda (pPs),y
				
				jsr IO.PrintErrMsg
				bcs CS.RUN.LOOP.RTS

CS.RUN.LOOP.8	>LDA.G bExit
				beq CS.RUN.LOOP.80
				
				ldy #S.PS.RC
				lda (pPs),y
				
				sec
CS.RUN.LOOP.RTS	rts
				
CS.RUN.LOOP.80	jmp CS.RUN.LOOP
*--------------------------------------
CS.DOEVENT		lda (pEvent)
				bpl .9

				jsr CheckSleep
				beq .9

*				ldy #Sleep				already set by CheckSleep
				
				ldx #4
				
				clc
				
.1				lda (pData),y
				sbc #0
				sta (pData),y
				iny
				dex
				bne .1

.9				sec
				rts
*--------------------------------------
CS.QUIT			jsr HIS.Quit
				jsr CMD.Quit
				clc
				rts
*--------------------------------------
CheckSleep		ldy #Sleep+3
				lda (pData),y
				dey
				ora (pData),y
				dey
				ora (pData),y
				dey
				ora (pData),y
				rts
*--------------------------------------
ShiftA			>SYSCALL ArgV
				bcs .8
				
				>STYA ZPPtr1
				>STYA ZPPtr2

.10				ldy #$ff
				
.1				iny						Compute ArgX len
				lda (ZPPtr1),y			
				bne .1
				
.2				tya
				sec
				adc ZPPtr1
				sta ZPPtr1
				bcc .3
				inc ZPPtr1+1
				
.3				ldy #$ff
				
.4				iny
				lda (ZPPtr1),y
				sta (ZPPtr2),y
				bne .4
				tya
				beq .7
				
				sec
				adc ZPPtr2
				sta ZPPtr2
				bcc .2
				inc ZPPtr2+1
				bra .2
				
.7				ldy #S.PS.ARGC
				lda (pPS),y
				dec
				sta (pPS),y
				
.8				clc
				rts
*--------------------------------------
Lookup			>STYA ZPPtr2
				>PULLW ZPPtr1

				ldx #0
				
.1				phx
				
				>PUSHW ZPPtr2
				>LDYA ZPPtr1
				>SYSCALL StrCaseCmp
				bcs .2
				
				plx
				>LDYA ZPPtr2
				clc
				rts
				
.2				inc ZPPtr2
				bne .3
				inc ZPPtr2+1

.3				lda (ZPPtr2)
				bne .2
				
				inc ZPPtr2
				bne .4
				inc ZPPtr2+1
								
.4				plx
				inx
				inx
				
				lda (ZPPtr2)			Array Ending 0, must be an external Cmd....
				bne .1
				
				sec
				rts
*--------------------------------------
				.INB USR/SRC/BIN/SHELL.S.CL
				.INB USR/SRC/BIN/SHELL.S.HIS
				.INB USR/SRC/BIN/SHELL.S.CORE
				.INB USR/SRC/BIN/SHELL.S.CMD
				.INB USR/SRC/BIN/SHELL.S.IO
*--------------------------------------
CS.END
*--------------------------------------
MSG.GREETINGS	.AZ "\r\nA2osX-Shell %d.%d\r\n\r\n"
MSG.PROMPT		.AZ "\e[7h$ "
MSG.TRACE		.AZ ">%s\r\n"
MSG.BATCHERR	.AZ "^\r\nLine #%D:"
MSG.ERROR		.AS "[$%h]:%S."
MSG.ECHOCRLF	.AZ "\r\n"
MSG.PRINTENV	.AZ "%s = %s\r\n"
MSG.PID			.AZ "PID=%d\r\n"
FMT.DATE		.AZ "%A (%w), %B %d %Y"
FMT.TIME		.AZ "%H:%M:%S (%I:%M:%S%p)"
FMT.GETKEY		.AZ "%d"		
*--------------------------------------
ENV.ROOT		.AZ "ROOT"
ENV.PATH		.AZ "PATH"
ENV.PWD			.AZ "PWD"
ENV.PS1			.AZ "PS1"
ENV.HOME		.AZ "HOME"
HOME.PROFILE	.AZ "${HOME}PROFILE"
*--------------------------------------
* https://www.tldp.org/LDP/abs/html/io-redirection.html
*--------------------------------------
CL.IO			.AZ "&"
				.AZ "<"
				.AZ ">>"
				.AZ ">"
				.AZ "1>>"
				.AZ "1>"
				.AZ "2>>"
				.AZ "2>"
				.HS 00
*--------------------------------------
CORE.INT		.AZ "STARTPROC"
				.AZ "CD"
				.AZ "DATE"
				.AZ "ECHO"
				.AZ "EXIT"
				.AZ "GETKEY"
				.AZ "MD"
				.AZ "PAUSE"
				.AZ "PWD"
				.AZ "RD"
				.AZ "READ"
				.AZ "REN"
				.AZ "SET"
				.AZ "SLEEP"
				.AZ "SHIFT"
				.AZ "TIME"
				.AZ "PUSHD"
				.AZ "POPD"
				.AZ "BREAK"
				.AZ "CONTINUE"
				.AZ "FOR"
				.AZ "NEXT"
				.AZ "WHILE"
				.AZ "LOOP"
				.AZ "IF"
				.AZ "ELSE"
				.AZ "FI"
				.HS 00
*--------------------------------------
CORE.Eval.TOKEN1
				.AZ "!["
				.AZ "["
				.HS 00
CORE.Eval.TOKEN2
				.AZ "-d"
				.AZ "-e"
				.AZ "-f"
				.HS 00
CORE.Eval.TOKEN3
				.AZ "="
				.AZ "!="
				.AZ "-eq"
				.AZ "-ne"
				.AZ "-lt"
				.AZ "-le"
				.AZ "-gt"
				.AZ "-ge"
				.HS 00
CORE.Eval.TOKEN3.BITS
				.DA #%000
				.DA #%000
				.DA #%010
				.DA #%101
				.DA #%100
				.DA #%110
				.DA #%001
				.DA #%011
CORE.Eval.TOKEN4
				.AZ "+"
				.AZ "-"
				.AZ "*"
				.AZ "div"
				.AZ "mod"
				.HS 00
*--------------------------------------
				.INB USR/SRC/X.M32.S
				.INB USR/SRC/X.ERRORS.S
*--------------------------------------
EscChars		.AS 'DBAC'
EscChars.Cnt	.EQ *-EscChars
M32.Printf		.AZ "%L"
*--------------------------------------
				.DUMMY
				.OR 0
DS.START
CORE.STACK		.BS CORE.STACK.MAX+1	pData (0 = Stack Ptr)
PUSHD.STACK		.BS PUSHD.STACK.MAX+1

hFileBuf		.BS 1					batch mode
hVarName		.BS 1					read mode

bEscMode		.BS 1
bPause			.BS 1
bExit			.BS 1
bSET.C			.BS 1
bSET.X			.BS 1
bECHO.N			.BS 1
bREAD.S			.BS 1

Sleep			.BS 4

CL.hCLBuf		.BS 1
CL.Ptr			.BS 1
CL.Len			.BS 1
CL.bExec		.BS 1

CORE.IntCmd		.BS 1
CORE.hArgVBuf	.BS 1

CORE.ArgIndex	.BS 1
CORE.Varname	.BS 1
CORE.Test		.BS 1
CORE.Operator	.BS 1

CORE.PSFlags	.BS 1
StatBuf			.BS S.STAT
TimeBuf			.BS S.TIME

HIS.hBuf		.BS 1
HIS.Count		.BS 1
HIS.Index		.BS 1

IO.hIn			.BS 1
IO.hOut			.BS 1
IO.hErr			.BS 1
*--------------------------------------
M32.BUF			.BS 12					-1234567890\0				
M32.ACC			.BS 4
M32.ARG			.BS 4
M32.TMP			.BS 4				
DS.END			.ED
*--------------------------------------
MAN
SAVE USR/SRC/BIN/SHELL.S
ASM
