2009-09-27 12:04:36 +00:00
|
|
|
;
|
2015-06-22 04:15:48 +00:00
|
|
|
; int __fastcall__ vsnprintf (char* Buf, size_t size, const char* Format, va_list ap);
|
2009-09-27 12:04:36 +00:00
|
|
|
;
|
2015-07-09 14:28:38 +00:00
|
|
|
; 2009-09-26, Ullrich von Bassewitz
|
2015-07-18 00:36:56 +00:00
|
|
|
; 2015-07-17, Greg King
|
2009-09-27 12:04:36 +00:00
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.export _vsnprintf, vsnprintf
|
|
|
|
.import ldaxysp, popax, incsp2, incsp6
|
|
|
|
.import _memcpy, __printf
|
|
|
|
.importzp sp, ptr1
|
2009-09-27 12:04:36 +00:00
|
|
|
|
2015-07-09 14:28:38 +00:00
|
|
|
.include "errno.inc"
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
.macpack generic
|
2009-09-27 12:04:36 +00:00
|
|
|
|
|
|
|
.data
|
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
;
|
|
|
|
; Static data for the _vsnprintf routine
|
|
|
|
;
|
|
|
|
|
2013-05-09 11:56:54 +00:00
|
|
|
outdesc: ; Static outdesc structure
|
2009-09-28 17:47:33 +00:00
|
|
|
ccount: .word 0 ; ccount
|
2013-05-09 11:56:54 +00:00
|
|
|
func: .word out ; Output function pointer
|
|
|
|
bufptr: .word 0 ; ptr
|
2009-09-28 17:47:33 +00:00
|
|
|
bufsize:.word 0 ; Buffer size
|
2009-09-27 12:04:36 +00:00
|
|
|
|
|
|
|
.code
|
|
|
|
|
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; vsprintf - formatted output into a buffer
|
|
|
|
;
|
|
|
|
; int __fastcall__ vsnprintf (char* buf, size_t size, const char* format, va_list ap);
|
|
|
|
;
|
|
|
|
|
|
|
|
_vsnprintf:
|
2013-05-09 11:56:54 +00:00
|
|
|
pha ; Save ap
|
2009-09-28 18:39:55 +00:00
|
|
|
txa
|
|
|
|
pha
|
2009-09-27 12:04:36 +00:00
|
|
|
|
2009-09-28 18:39:55 +00:00
|
|
|
; Setup the outdesc structure. This is also an additional entry point for
|
|
|
|
; vsprintf with ap on stack
|
2009-09-27 12:04:36 +00:00
|
|
|
|
2009-09-28 18:39:55 +00:00
|
|
|
vsnprintf:
|
2013-05-09 11:56:54 +00:00
|
|
|
lda #0
|
|
|
|
sta ccount+0
|
|
|
|
sta ccount+1 ; Clear ccount
|
2009-09-27 12:04:36 +00:00
|
|
|
|
|
|
|
; Get the size parameter and replace it by a pointer to outdesc. This is to
|
2015-07-09 14:28:38 +00:00
|
|
|
; build a stack frame for the call to _printf. The size must not be greater
|
|
|
|
; than INT_MAX because the return type is int. If the size is zero,
|
|
|
|
; then nothing will be written into the buffer; but, the arguments still will
|
|
|
|
; be formatted and counted.
|
2009-09-27 12:04:36 +00:00
|
|
|
|
|
|
|
ldy #2
|
|
|
|
lda (sp),y
|
|
|
|
sta ptr1
|
2009-09-28 18:39:55 +00:00
|
|
|
|
2009-09-27 12:04:36 +00:00
|
|
|
lda #<outdesc
|
|
|
|
sta (sp),y
|
2009-09-28 18:39:55 +00:00
|
|
|
|
2009-09-27 12:04:36 +00:00
|
|
|
iny
|
|
|
|
lda (sp),y
|
2015-07-09 14:28:38 +00:00
|
|
|
bmi L9 ; More than $7FFF
|
2009-09-27 12:04:36 +00:00
|
|
|
sta ptr1+1
|
2009-09-28 18:39:55 +00:00
|
|
|
|
2009-09-27 12:04:36 +00:00
|
|
|
lda #>outdesc
|
|
|
|
sta (sp),y
|
|
|
|
|
2015-07-09 14:28:38 +00:00
|
|
|
; Write size-1 to outdesc.uns. It will be -1 if there is no buffer.
|
2009-09-27 12:04:36 +00:00
|
|
|
|
|
|
|
ldy ptr1+1
|
2009-09-28 18:39:55 +00:00
|
|
|
ldx ptr1
|
2009-09-27 12:04:36 +00:00
|
|
|
bne L1
|
|
|
|
dey
|
2009-09-28 18:39:55 +00:00
|
|
|
L1: dex
|
|
|
|
stx bufsize+0
|
2009-09-28 17:47:33 +00:00
|
|
|
sty bufsize+1
|
2009-09-27 12:04:36 +00:00
|
|
|
|
2009-09-28 17:47:33 +00:00
|
|
|
; Copy buf to the outdesc.ptr
|
2009-09-27 12:04:36 +00:00
|
|
|
|
|
|
|
ldy #5
|
|
|
|
jsr ldaxysp
|
2009-09-28 17:47:33 +00:00
|
|
|
sta bufptr+0
|
|
|
|
stx bufptr+1
|
2009-09-27 12:04:36 +00:00
|
|
|
|
2015-07-09 18:46:28 +00:00
|
|
|
; There must be a buffer if its size is non-zero.
|
|
|
|
|
|
|
|
bit bufsize+1
|
|
|
|
bmi L5
|
|
|
|
ora bufptr+1
|
|
|
|
bze L0 ; The pointer shouldn't be NULL
|
|
|
|
|
2009-09-28 18:39:55 +00:00
|
|
|
; Restore ap and call _printf
|
2009-09-27 12:04:36 +00:00
|
|
|
|
2015-07-09 18:46:28 +00:00
|
|
|
L5: pla
|
2009-09-28 18:39:55 +00:00
|
|
|
tax
|
|
|
|
pla
|
2013-05-09 11:56:54 +00:00
|
|
|
jsr __printf
|
2009-09-27 12:04:36 +00:00
|
|
|
|
2015-07-09 14:28:38 +00:00
|
|
|
; Terminate the string if there is a buffer. The last char. is at either
|
|
|
|
; bufptr+bufsize or bufptr+ccount, whichever is smaller.
|
2009-09-27 12:04:36 +00:00
|
|
|
|
2015-07-09 14:28:38 +00:00
|
|
|
ldx bufsize+1
|
|
|
|
bmi L4 ; -1 -- No buffer
|
|
|
|
lda bufsize+0
|
|
|
|
cpx ccount+1
|
2009-09-27 12:04:36 +00:00
|
|
|
bne L2
|
2015-07-09 14:28:38 +00:00
|
|
|
cmp ccount+0
|
2009-09-27 12:04:36 +00:00
|
|
|
L2: bcc L3
|
2015-07-09 14:28:38 +00:00
|
|
|
lda ccount+0
|
|
|
|
ldx ccount+1
|
2009-09-27 12:04:36 +00:00
|
|
|
clc
|
2009-09-28 17:47:33 +00:00
|
|
|
L3: adc bufptr+0
|
2009-09-27 12:04:36 +00:00
|
|
|
sta ptr1
|
|
|
|
txa
|
2009-09-28 17:47:33 +00:00
|
|
|
adc bufptr+1
|
2009-09-27 12:04:36 +00:00
|
|
|
sta ptr1+1
|
|
|
|
|
|
|
|
lda #0
|
|
|
|
tay
|
|
|
|
sta (ptr1),y
|
|
|
|
|
|
|
|
; Return the number of bytes written and drop buf
|
|
|
|
|
2015-07-09 14:28:38 +00:00
|
|
|
L4: lda ccount+0
|
2009-09-28 17:47:33 +00:00
|
|
|
ldx ccount+1
|
2013-05-09 11:56:54 +00:00
|
|
|
jmp incsp2
|
2009-09-27 12:04:36 +00:00
|
|
|
|
2015-07-09 14:28:38 +00:00
|
|
|
; Bail out if size is too high.
|
2009-09-27 12:04:36 +00:00
|
|
|
|
2015-07-18 00:36:56 +00:00
|
|
|
L9: ldy #ERANGE
|
2015-07-09 18:46:28 +00:00
|
|
|
.byte $2C ;(bit $xxxx)
|
|
|
|
|
|
|
|
; NULL buffer pointers usually are invalid.
|
|
|
|
|
2015-07-18 00:36:56 +00:00
|
|
|
L0: ldy #EINVAL
|
|
|
|
pla ; Drop ap
|
|
|
|
pla
|
|
|
|
tya
|
2015-07-09 14:28:38 +00:00
|
|
|
jsr __directerrno ; Return -1
|
2009-09-27 12:04:36 +00:00
|
|
|
jmp incsp6 ; Drop parameters
|
|
|
|
|
|
|
|
|
2009-09-28 18:39:55 +00:00
|
|
|
; ----------------------------------------------------------------------------
|
|
|
|
; Callback routine used for the actual output.
|
|
|
|
;
|
2015-06-22 04:15:48 +00:00
|
|
|
; static void __cdecl__ out (struct outdesc* d, const char* buf, unsigned count)
|
2009-09-28 18:39:55 +00:00
|
|
|
; /* Routine used for writing */
|
|
|
|
;
|
|
|
|
; Since we know, we're called with a pointer to our static outdesc structure,
|
|
|
|
; we don't need the pointer passed on the stack.
|
|
|
|
|
|
|
|
out:
|
|
|
|
|
|
|
|
; Calculate the space left in the buffer. If no space is left, don't copy
|
|
|
|
; any characters
|
|
|
|
|
|
|
|
lda bufsize+0 ; Low byte of buffer size
|
|
|
|
sec
|
|
|
|
sbc ccount+0 ; Low byte of bytes already written
|
|
|
|
sta ptr1
|
|
|
|
lda bufsize+1
|
2015-07-09 14:28:38 +00:00
|
|
|
bmi @L9 ; -1 -- No buffer
|
2009-09-28 18:39:55 +00:00
|
|
|
sbc ccount+1
|
|
|
|
sta ptr1+1
|
|
|
|
bcs @L0 ; Branch if space left
|
2015-07-09 14:28:38 +00:00
|
|
|
@L9: lda #$0000
|
2009-09-28 18:39:55 +00:00
|
|
|
sta ptr1
|
|
|
|
sta ptr1+1 ; No space left
|
|
|
|
|
|
|
|
; Replace the pointer to d by a pointer to the write position in the buffer
|
|
|
|
; for the call to memcpy that follows.
|
|
|
|
|
|
|
|
@L0: lda bufptr+0
|
|
|
|
clc
|
|
|
|
adc ccount+0
|
|
|
|
ldy #4
|
|
|
|
sta (sp),y
|
|
|
|
|
|
|
|
lda bufptr+1
|
|
|
|
adc ccount+1
|
|
|
|
iny
|
|
|
|
sta (sp),y
|
|
|
|
|
|
|
|
; Get Count from stack
|
|
|
|
|
|
|
|
jsr popax
|
|
|
|
|
|
|
|
; outdesc.ccount += Count;
|
|
|
|
|
|
|
|
pha
|
|
|
|
clc
|
|
|
|
adc ccount+0
|
|
|
|
sta ccount+0
|
|
|
|
txa
|
|
|
|
adc ccount+1
|
|
|
|
sta ccount+1
|
|
|
|
pla
|
|
|
|
|
|
|
|
; if (Count > Left) Count = Left;
|
|
|
|
|
|
|
|
cpx ptr1+1
|
|
|
|
bne @L1
|
|
|
|
cmp ptr1
|
|
|
|
@L1: bcc @L2
|
|
|
|
lda ptr1
|
|
|
|
ldx ptr1+1
|
|
|
|
|
|
|
|
; Jump to memcpy, which will cleanup the stack and return to the caller
|
|
|
|
|
|
|
|
@L2: jmp _memcpy
|
|
|
|
|
|
|
|
|
|
|
|
|