PR#3
PREFIX /A2OSX.SRC
NEW
INC 1
AUTO 6
				.LIST OFF
*--------------------------------------
* PULLW PString to scan (ex: "192.168.1.5")
* PULLW PString pattern	(ex: "%d.%d.%d.%d")
* PULLW PTR to target buffer
*--------------------------------------
K.SScanF		jsr PullPtr1Ptr2Ptr3
				
				ldy	#0					Y = PTR in pattern
				
				lda (ZPQuickPtr1)
				beq .9
				tax						X = COUNT to scan
				inc ZPQuickPtr1
				bne .1
				inc ZPQuickPtr1+1

.1				txa						End Of String?
				beq .8
				tya
				cmp (ZPQuickPtr2)			End of pattern?
				beq .8
				iny
				lda (ZPQuickPtr2),y
				cmp #'%' 				Escape?
				beq .2
				cmp (ZPQuickPtr1)			Same char?
				bne .9
				jsr K.SScanF.IncPtr1
				bne .1
				clc
				rts
				
.2				tya 
				cmp (ZPQuickPtr2)			unexpected End of pattern after "%" ?
				beq .9

				iny
				lda (ZPQuickPtr2),y
				cmp #'d'				BYTE ?
				bne .3
				
				stz ASCBUF
.20				lda (ZPQuickPtr1)
				jsr K.SScanF.IsDigit
				bcs .21
				phx
				inc ASCBUF
				ldx ASCBUF
				sta ASCBUF,x
				plx
				jsr K.SScanF.IncPtr1
				bne .20					end of string ?
				
.21				jsr DEC2HEX
				lda HEXBUF
				sta (ZPQuickPtr3)
				inc ZPQuickPtr3
				bne .1
				inc ZPQuickPtr3+1
				bra .1

.3				cmp #'D'				WORD ?
				bne .4
				
				bra .1

.4				cmp #'s'				STRING ?
				bne .9
				
				bra .1
				
.8				clc
				rts
				
.9				sec
				rts

*--------------------------------------
K.SScanF.IncPtr1	dex
				beq .1
				inc ZPQuickPtr1
				bne .1
				inc ZPQuickPtr1+1			never Zero
.1				rts
*--------------------------------------
K.SScanF.IsDigit	cmp #'0'
				bcc .1
				cmp #'9'+1
				bcs .1
				rts
.1				sec
				rts				
*--------------------------------------
* K.PrintFYA :
* Prints C-Style String
* Y,A = PTR to Sting,Last Byte negative 
*--------------------------------------
* %a pull 2 bytes to Print Access right String drwxrwxrwx
* %b pull 1 byte to Print BIN 
* %B pull 2 bytes to Print BIN
* %d pull 1 byte unsigned DEC 0..255
* %D pull 2 bytes unsigned DEC 0..65535
* %u pull 2 bytes PTR to 4 bytes long unsigned DEC 0..4294967295
* %e pull 2 bytes PTR to 6 Bytes Real +1.23456789e+12
* %f pull 2 bytes PTR to 6 Bytes Real 3.1415
* %h pull 1 byte to Print HEX
* %H pull 2 bytes to Print HEX
* %i pull 1 byte to Print signed DEC -128..127
* %I pull 2 bytes to Print signed DEC -32768..32767
* %L pull 2 bytes PTR to 4 bytes signed DEC -2147483648..2147483647
* %n pull 1 byte to Print low Nibble HEX
* %N pull 1 byte to Print high Nibble HEX
* %s pull 2 bytes ptr to C-Style String
* %S pull 2 bytes ptr to P-Style String
* \e Print 'ESC' ($1B,27)
* \n Print CR=13
* \\ Print \
* \% Print %
* len/padding : 
*   %d	   '9'  '12'
*   %2d	   ' 9' '12'   				
*	%02d   '09' '12'
*	%11s   'ABCDEFGH   '	
*	%011s  'ABCDEFGH000'
*   %2f		'3.14'
*--------------------------------------
K.PrintFYA		>STYA ZPQuickPtr1

				ldy	#0
				
.1				lda (ZPQuickPtr1),y
				beq .99 
				cmp #'%'
				bne .10
						
				stz PADLEN
				lda #' '
				sta PADCHAR
				
.2				ldx	#PrintFTBL1.END-PrintFTBL1-1
				iny
				lda	(ZPQuickPtr1),y
				beq .99
				
.3				cmp PrintFTBL1,x		do we have a %x command?
				beq .8					yes, jmp to it!
				dex
				bpl	.3					no valid letter...
				
				cmp #'0'				...a 0...mmm... padding char?
				bne .4
				
				ldx PADLEN				PADLEN is not nul, so this 0 is second digit
				bne .5
				
				lda #'0'
				sta PADCHAR				no, this is the first 0, so make it PADCHAR
				bra .2

.4				bcc .99					less than '0'....error				
				cmp #'9'+1
				bcs .99					more than '9' ....error			
				
.5				and #$0F				we have a digit
				pha						save it...
				lda PADLEN				starts PADLEN * 10
				asl 
				asl 					A=times 4
				adc PADLEN				CC by ASL, A=times 5			
				asl						times 10
				sta PADLEN

				pla						get back digit
				adc PADLEN
				sta PADLEN
				bra .2					go get next char...

.8				phy
				txa
				asl
				tax
				jsr PrintFESC
				ply
				iny
				bne .1
				rts

.10				cmp #'\'
				bne .20
				
				ldx	#PrintFTBL2.END-PrintFTBL2-1
				iny
				lda	(ZPQuickPtr1),y
				beq .99
.12				cmp PrintFTBL2,x
				beq .13
				dex
				bpl	.12
				bra .80
				
.13				lda	PrintFTBL2.OUT,x	

.20				jsr	K.COutA
.80				iny
				bne	.1
.99				rts
*--------------------------------------
PrintFESC		jmp	(PrintFJMP,x)
*--------------------------------------
PrintFTBL1		.AS	"abBdDuefhHiILnNsS"
PrintFTBL1.END
PrintFTBL2		.AS	"en\%"
PrintFTBL2.END
PrintFTBL2.OUT	.HS 1B.0D				\e\n
				.DA #'\'				\\
				.DA #'%'				\%
PrintFJMP		.DA PrintF.A
				.DA PrintF.B,PrintF.BB
				.DA PrintF.D,PrintF.DD,PrintF.U
				.DA PrintF.E,PrintF.F
				.DA PrintF.H,PrintF.HH
				.DA PrintF.I,PrintF.II,PrintF.L
				.DA PrintF.N,PrintF.NN
				.DA PrintF.S,PrintF.SS
*--------------------------------------
PrintF.A		>PULLW HEXBUF

				lda HEXBUF+1
				lsr
				lsr
				lsr
				lsr
				tax
				lda TYPES,x
				jsr K.COutA
				
				ldx #0
				
.1				lda #'-'
				lsr HEXBUF
				bcc .2
				lda ACCESS,x
				
.2				jsr K.COutA
				
				inx
				cpx #8
				bne .1
				
.3				lda #'-'
				lsr HEXBUF+1
				bcc .4
				lda ACCESS,x
				
.4				jsr K.COutA
				rts
*--------------------------------------
PrintF.BB		>PULLA
				pha
				jsr PrintF.B
				pla
				bra PrintF.B.1
*--------------------------------------
PrintF.B		>PULLA
				
PrintF.B.1		ldx #8
				
.1				asl
				pha
				lda #'0'
				adc #0					add Carry
				jsr K.COutA
				pla
				dex
				bne .1
				rts
*--------------------------------------
PrintF.D		>PULLB HEXBUF
				stz HEXBUF+1
				bra PrintF.DD.1
				
PrintF.DD		>PULLW HEXBUF
PrintF.DD.1		stz HEXBUF+2
				stz HEXBUF+3
				bra PrintF.U.1
				
PrintF.U		>PULLW ZPQuickPtr2
				ldy #3
				
.1				lda (ZPQuickPtr2),y
				sta HEXBUF,y
				dey
				bpl .1

PrintF.U.1		stz SIGN
				jsr HEX2DEC
				jmp PrintDEC
*--------------------------------------				
PrintF.E		>PULLW ZPQuickPtr2
				rts
*--------------------------------------
PrintF.F		>PULLW ZPQuickPtr2
				rts
*--------------------------------------
PrintF.HH		>PULLA
				pha
				jsr PrintF.H
				pla
				bra PrintF.H.1
*--------------------------------------
PrintF.H		>PULLA
PrintF.H.1		pha
				jsr PrintF.NN.1
				pla
				jmp PrintF.N.1
*--------------------------------------
PrintF.I		>PULLB HEXBUF
				eor #$ff				if positive, it becomes neg so branch if....minus!
				clc
				bmi .1
				sec
				inc						2s complement...
				sta HEXBUF

				
.1				stz HEXBUF+1
				bra PrintF.II.1
				
PrintF.II		>PULLW HEXBUF			A=HI
				eor #$ff
				clc
				bmi PrintF.II.1
				sec
				tax
				lda HEXBUF
				eor #$ff
				inc						2s complement...
				sta HEXBUF
				bne .1
				inx
.1				sta HEXBUF
				stx HEXBUF+1

PrintF.II.1		stz HEXBUF+2
				stz HEXBUF+3
				bra PrintF.L.1
				
PrintF.L		>PULLW ZPQuickPtr2
				ldy #3
				
.1				lda (ZPQuickPtr2),y
				sta HEXBUF,y
				dey
				bpl .1

				
PrintF.L.1		ror SIGN
				jsr HEX2DEC
				jmp PrintDEC
*--------------------------------------
PrintF.N		>PULLA
PrintF.N.1		and #$0F
				bra PrintF.NN.2

PrintF.NN		>PULLA
PrintF.NN.1		lsr
				lsr
				lsr
				lsr
				
PrintF.NN.2		ora #$30
				cmp #$3A
				bcc .1
				adc #6
.1				jmp K.COutA
*--------------------------------------
* CStr 
*--------------------------------------
PrintF.S		>PULLW ZPQuickPtr2

				ldy	#0
				
.1				lda	(ZPQuickPtr2),y
				beq .2
		
				jsr K.COutA
				iny
				lda PADLEN
				
				beq .1
				cpy	PADLEN
				bne .1
				rts
				
.2				lda PADLEN
				beq .8
				
.3				lda PADCHAR				
				jsr K.COutA
				iny
				cpy PADLEN
				bne .3
				
.8				rts
*--------------------------------------
* PStr 
*--------------------------------------
PrintF.SS		>PULLW ZPQuickPtr2
				
				ldy	#0
				lda	(ZPQuickPtr2),y
				tax
				beq .8
				
.1				iny
				lda	(ZPQuickPtr2),y
				jsr K.COutA
				lda PADLEN
				
				beq .2
				cpy	PADLEN
				beq .8
				
.2				dex
				bne .1
				
				lda PADLEN
				beq .8
				
.3				lda PADCHAR				
				jsr K.COutA
				iny
				cpy PADLEN
				bne .3
				
.8				rts
*--------------------------------------
K.COutA			phx
				phy
				cmp #13
				bne .1
				ldx #DEVMGR.COUT
				jsr pDevJmp
				bcs .2
				lda #10
.1				ldx #DEVMGR.COUT
				jsr pDevJmp
.2				ply
				plx
				rts
*--------------------------------------
* Convert HEXBUF to ASCBUF decimal padded with 0
*--------------------------------------
HEX2DEC			ldx #4

.1				stz BCDBUF,x	Clear all 5 bytes
				dex
				bpl .1
				
				sed				switch to BCD mode

				ldx #32			let's roll 32 bits
				
.2				asl HEXBUF
				rol	HEXBUF+1
				rol HEXBUF+2
				rol HEXBUF+3

				ldy #4
				
.3				lda BCDBUF,y
				adc BCDBUF,y
				sta BCDBUF,y
				dey
				bpl .3
				
				dex
				bne .2
				
				cld
				
				ldy #4
				ldx #9
				
.4				lda BCDBUF,y
				pha
				and #$0F
				ora #$30
				sta	ASCBUF,x
				dex
				pla
				lsr
				lsr
				lsr
				lsr
				ora #$30
				sta	ASCBUF,x
				dey
				dex
				bpl .4
				
				rts
*--------------------------------------
PRINTDEC		lda PADLEN			any Len format ?
				beq .1				no
				
				lda #10
				sec					yes, Print only digits starting at pos 10-padlen
				sbc PADLEN
				
.1				tax					x=0 if no padlen, or x=10-padlen

.2				lda ASCBUF,x		
				cmp #'0'			a zero?
				beq .3
				
				inc PADLEN			found a non zero, Print all digits, even if 0, next time
				ldy #'0'
				sty PADCHAR
				bra .4
				
.3				cpx #9				last digit ?
				beq .4				Print always
				
				ldy PADLEN			no pad to fill, do not Print 0
				beq .9
				
				lda PADCHAR			fill with PADCHAR
				
.4				bit SIGN			a sign to print before digits ?
				bpl .5
				pha					yes, save whatever we have to print....
				lda #'-'
				jsr K.COutA
				stz SIGN			reset flag for next char...
				pla
				
.5				jsr K.COutA

.9				inx
				cpx #10
				bne .2
				
				rts
*--------------------------------------
* Convert ASCBUF decimal to HEXBUF
*--------------------------------------
DEC2HEX			stz HEXBUF
				stz HEXBUF+1
				stz HEXBUF+2
				stz HEXBUF+3
				
				ldx #0
				
.1				lda HEXBUF+3			save HEXBUF for 4+1
				pha
				lda HEXBUF+2
				pha
				lda HEXBUF+1
				pha
				lda HEXBUF
				pha

				asl HEXBUF				HEXBUF * 2 -> HEXBUF
				rol HEXBUF+1
				rol HEXBUF+2
				rol HEXBUF+3
*				bcs .9					overflow!!!
				
				asl HEXBUF				HEXBUF * 4 -> HEXBUF
				rol HEXBUF+1
				rol HEXBUF+2
				rol HEXBUF+3
*				bcs .9					overflow!!!

				pla				HEXBUF * 4 + HEXBUF -> HEXBUF
				adc HEXBUF
				sta HEXBUF
				pla
				adc HEXBUF+1
				sta HEXBUF+1
				pla
				adc HEXBUF+2
				sta HEXBUF+2
				pla
				adc HEXBUF+3
				sta HEXBUF+3
*				bcs .99					overflow!!!

				asl HEXBUF				HEXBUF * 10 -> HEXBUF
				rol HEXBUF+1
				rol HEXBUF+2
				rol HEXBUF+3
*				bcs .99					overflow!!!

				inx
				lda ASCBUF,x
				and #$0F

				adc HEXBUF
				sta HEXBUF
				bcc .2
				inc HEXBUF+1
				bne .2
				inc HEXBUF+2
				bne .2
				inc HEXBUF+3
				
.2				cpx ASCBUF
				bne .1
				rts
				
*.9				pla					discard saved HEXBUF
*				pla
*				pla
*				pla
*.99			rts
*--------------------------------------
TYPES			.AS "-pcbdls"
ACCESS			.AS "rwxrwxrwx"
HEXBUF			.BS 4					32 bits max
BCDBUF			.BS 5					5, enough to handle 10 digits (32bits)
ASCBUF			.BS 12					LEN + sign + 10 digits ($FFFFFFFF=4.294.967.295)
SIGN			.BS 1
PADLEN			.BS 1
PADCHAR			.BS 1
*--------------------------------------
MAN
SAVE SYS/KERNEL.S.CIO
LOAD SYS/KERNEL.S
ASM
