PR#3
PREFIX /A2OSX.SRC
NEW
INC 1
AUTO 6
				.LIST OFF	
*--------------------------------------
HIS.Init		>PUSHWI 256				Get a buffer for History
				>PUSHBI S.MEM.F.INIT0
				>SYSCALL SYS.GetMem
				bcs .9

				txa
				ldy #hCmdHistory
				sta (pData),y
				
				lda #0
				ldy #CmdHistory.IDX
				sta (pData),y
				ldy #CmdHistory.END
				sta (pData),y
.9				rts
*--------------------------------------
HIS.Add			ldy #hCmdHistory
				lda (pData),y
				beq .9
				>SYSCALL SYS.GetMemPtrA
				>STYA ZPPTR1
				
.10				ldy #CmdHistory.END		
				lda (pData),y
				sec						add len+1
				adc (pData)				CmdBuffer length
				bcc .2					enough room
				
				lda (ZPPTR1)			get len of oldest string
				sec
				adc ZPPTR1
				sta ZPPTR2
				lda ZPPTR1+1
				adc #0
				sta ZPPTR2+1
				
				ldy #$FF
				
.1				iny
				lda (ZPPTR2),y			move back until ending 0 found
				sta (ZPPTR1),y
				bne .1
				
				tya
				
				ldy #CmdHistory.END		mark new end of history
				sta (pData),y
				bra .10					retry....
				
.2				tax						keep new CmdHistory.END

				tay
				lda #0
				sta (ZPPTR1),y			set ending 0

				ldy #CmdHistory.END		save old END
				lda (pData),y
				pha
				
				txa						get back new END

				ldy #CmdHistory.IDX		set it as new IDX
				sta (pData),y
			
				ldy #CmdHistory.END
				sta (pData),y			set new CmdHistory.END
				
				pla						get back old END
				clc
				adc ZPPTR1
				sta ZPPTR1
				bcc .3
				inc ZPPTR1+1
				
.3				lda (pData)
				tay
				
.4				lda (pData),y
				sta (ZPPTR1),y
				dey
				bpl .4
.9				rts
*--------------------------------------
HIS.GetPrev		ldy #hCmdHistory
				lda (pData),y
				beq HISTORY.RTS
				
				tax
				
				ldy #CmdHistory.END
				lda (pData),y
				beq HISTORY.RTS			empty history,nothing to do
				ldy #CmdHistory.IDX
				lda (pData),y
				beq HISTORY.RTS			already oldest,nothing to do
				
				txa

				>SYSCALL SYS.GetMemPtrA
				>STYA ZPPTR1
				
				lda #0
				
.1				tax
				tay
				sec
				adc (ZPPTR1),y
				bcs History.RTS			out of buffer
				
				ldy #CmdHistory.IDX
				cmp (pData),y
				bne .1					reached current index
				
.2				txa
				ldy #CmdHistory.IDX		set new index
				sta (pData),y
				bra HIS.SetBuf
History.RTS		rts
*--------------------------------------
HIS.GetNext		ldy #hCmdHistory
				lda (pData),y
				beq HISTORY.RTS
				
				tax
				
				ldy #CmdHistory.END
				lda (pData),y
				beq HISTORY.RTS			empty history,nothing to do
				ldy #CmdHistory.IDX
				cmp (pData),y
				beq HISTORY.RTS			already oldest,nothing to do
				
				txa
	
				>SYSCALL SYS.GetMemPtrA
				>STYA ZPPTR1

				ldy #CmdHistory.IDX
				lda (pData),y
				phy
				tay
				sec
				adc (ZPPTR1),y			add length+1 of current line in history
				ply
				sta (pData),y
				
				ldy #CmdHistory.END
				cmp (pData),y
				bne HIS.SetBuf
				jmp CmdBuffer.CLR		end of history, just blank buffer
*--------------------------------------
HIS.SetBuf		jsr CmdBuffer.CLR

				ldy	#CmdHistory.IDX
				lda (pData),y
				tay						keep length of string
				
				clc
				adc ZPPTR1
				sta ZPPTR1
				bcc .1
				inc ZPPTR1+1
				
.1				lda (ZPPTR1),y
				sta (pData),y
				dey
				bpl .1
				
				jmp CmdBuffer.PRINT
*--------------------------------------
HIS.Quit		ldy #hCmdHistory
				lda (pData),y
				beq .9
				>SYSCALL SYS.FreeMemA
.9				rts
*--------------------------------------
MAN
SAVE SBIN/SHELL.S.HIS
LOAD SBIN/SHELL.S
ASM
