2001-04-19 06:46:30 +00:00
|
|
|
/*
|
|
|
|
* _scanf.c
|
|
|
|
*
|
2005-02-14 09:19:59 +00:00
|
|
|
* (c) Copyright 2001-2005, Ullrich von Bassewitz <uz@cc65.org>
|
|
|
|
* 2005-01-24, Greg King <gngking@erols.com>
|
2001-04-19 06:46:30 +00:00
|
|
|
*
|
2005-02-14 09:19:59 +00:00
|
|
|
* This is the basic layer for all scanf-type functions. It should be
|
|
|
|
* rewritten in assembly, at some time in the future. So, some of the code
|
2002-03-24 13:26:18 +00:00
|
|
|
* is not as elegant as it could be.
|
2001-04-19 06:46:30 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
2004-11-27 14:45:49 +00:00
|
|
|
#include <stddef.h>
|
2005-02-14 09:19:59 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdio.h>
|
2002-03-24 13:26:18 +00:00
|
|
|
#include <string.h>
|
2001-04-19 06:46:30 +00:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <limits.h>
|
2005-02-14 09:19:59 +00:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
/* _scanf() can give EOF to these functions. But, the macroes can't
|
|
|
|
** understand it; so, they are removed.
|
|
|
|
*/
|
|
|
|
#undef isspace
|
|
|
|
#undef isxdigit
|
2001-04-19 06:46:30 +00:00
|
|
|
|
|
|
|
#include "_scanf.h"
|
|
|
|
|
2005-02-14 09:19:59 +00:00
|
|
|
extern void __fastcall__ _seterrno (unsigned char code);
|
|
|
|
|
|
|
|
#pragma staticlocals(on)
|
|
|
|
|
2001-04-19 06:46:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2005-02-14 09:38:30 +00:00
|
|
|
/* SetJmp return codes */
|
2001-04-19 06:46:30 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-02-14 09:19:59 +00:00
|
|
|
enum {
|
2005-02-14 09:38:30 +00:00
|
|
|
RC_OK, /* setjmp() call */
|
|
|
|
RC_NOCONV, /* No conversion possible */
|
|
|
|
RC_EOF /* EOF reached */
|
2005-02-14 09:19:59 +00:00
|
|
|
};
|
2001-04-19 06:46:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2005-02-14 09:38:30 +00:00
|
|
|
/* Data */
|
2001-04-19 06:46:30 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-02-14 09:38:30 +00:00
|
|
|
static const char* format; /* Copy of function argument */
|
2005-02-14 09:22:37 +00:00
|
|
|
static const struct scanfdata* D_; /* Copy of function argument */
|
2005-02-14 09:38:30 +00:00
|
|
|
static va_list ap; /* Copy of function argument */
|
|
|
|
static jmp_buf JumpBuf; /* "Label" that is used for failures */
|
|
|
|
static char F; /* Character from format string */
|
2005-02-14 09:22:37 +00:00
|
|
|
static unsigned CharCount; /* Characters read so far */
|
2005-02-14 09:38:30 +00:00
|
|
|
static int C; /* Character from input */
|
|
|
|
static unsigned Width; /* Maximum field width */
|
|
|
|
static long IntVal; /* Converted int value */
|
|
|
|
static int Assignments; /* Number of assignments */
|
2005-02-14 09:22:37 +00:00
|
|
|
static unsigned char IntBytes; /* Number of bytes-1 for int conversions */
|
2001-07-14 14:41:30 +00:00
|
|
|
|
|
|
|
/* Flags */
|
2005-02-14 09:38:30 +00:00
|
|
|
static bool Converted; /* Some object was converted */
|
|
|
|
static bool Positive; /* Flag for positive value */
|
|
|
|
static bool NoAssign; /* Suppress assignment */
|
|
|
|
static bool Invert; /* Do we need to invert the charset? */
|
2005-02-14 09:19:59 +00:00
|
|
|
static unsigned char CharSet[(1+UCHAR_MAX)/CHAR_BIT];
|
|
|
|
static const unsigned char Bits[CHAR_BIT] = {
|
2002-03-24 13:26:18 +00:00
|
|
|
0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80
|
|
|
|
};
|
2001-04-19 06:46:30 +00:00
|
|
|
|
2004-11-27 14:45:49 +00:00
|
|
|
/* We need C to be 16 bits since we cannot check for EOF otherwise.
|
|
|
|
* Unfortunately, this causes the code to be quite larger, even if for most
|
|
|
|
* purposes, checking the low byte would be enough, since if C is EOF, the
|
|
|
|
* low byte will not match any useful character anyway (at least for the
|
|
|
|
* supported platforms - I know that this is not portable). So the following
|
|
|
|
* macro is used to access just the low byte of C.
|
|
|
|
*/
|
|
|
|
#define CHAR(c) (*((unsigned char*)&(c)))
|
|
|
|
|
2001-04-19 06:46:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2005-02-14 09:38:30 +00:00
|
|
|
/* Character sets */
|
2001-04-19 06:46:30 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-02-14 09:19:59 +00:00
|
|
|
/* We don't want the optimizer to ruin our "perfect" ;-)
|
|
|
|
* assembly code!
|
|
|
|
*/
|
|
|
|
#pragma optimize (push, off)
|
|
|
|
|
|
|
|
static unsigned FindBit (void)
|
|
|
|
/* Locate the character's bit in the charset array.
|
|
|
|
* < .A - Argument character
|
|
|
|
* > .X - Offset of the byte in the character-set mask
|
|
|
|
* > .A - Bit-mask
|
|
|
|
*/
|
2002-03-24 13:26:18 +00:00
|
|
|
{
|
2005-02-14 09:19:59 +00:00
|
|
|
asm ("pha");
|
2005-02-14 09:38:30 +00:00
|
|
|
asm ("lsr a"); /* Divide by CHAR_BIT */
|
2002-03-24 13:26:18 +00:00
|
|
|
asm ("lsr a");
|
|
|
|
asm ("lsr a");
|
2005-02-14 09:38:30 +00:00
|
|
|
asm ("tax"); /* Byte's offset */
|
2005-02-14 09:19:59 +00:00
|
|
|
asm ("pla");
|
|
|
|
asm ("and #%b", CHAR_BIT-1);
|
2005-02-14 09:38:30 +00:00
|
|
|
asm ("tay"); /* Bit's offset */
|
2002-03-24 13:26:18 +00:00
|
|
|
asm ("lda %v,y", Bits);
|
2005-02-14 09:19:59 +00:00
|
|
|
return (unsigned) __AX__;
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma optimize (pop)
|
|
|
|
|
|
|
|
|
|
|
|
static void __fastcall__ AddCharToSet (unsigned char /* C */)
|
|
|
|
/* Set the given bit in the character set */
|
|
|
|
{
|
|
|
|
FindBit();
|
2002-03-24 13:26:18 +00:00
|
|
|
asm ("ora %v,x", CharSet);
|
|
|
|
asm ("sta %v,x", CharSet);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-02-14 09:19:59 +00:00
|
|
|
#pragma optimize (push, off)
|
|
|
|
|
|
|
|
static unsigned char IsCharInSet (void)
|
|
|
|
/* Check if the char. is part of the character set. */
|
2002-03-24 13:26:18 +00:00
|
|
|
{
|
2005-02-14 09:19:59 +00:00
|
|
|
/* Get the character from C. */
|
|
|
|
asm ("lda #$00");
|
|
|
|
asm ("ldx %v+1", C);
|
2005-02-14 09:38:30 +00:00
|
|
|
asm ("bne L1"); /* EOF never is in the set */
|
2005-02-14 09:19:59 +00:00
|
|
|
asm ("lda %v", C);
|
|
|
|
FindBit();
|
2002-03-24 13:26:18 +00:00
|
|
|
asm ("and %v,x", CharSet);
|
2005-02-14 09:19:59 +00:00
|
|
|
asm ("L1:");
|
2002-03-24 13:26:18 +00:00
|
|
|
asm ("ldx #$00");
|
2005-02-14 09:19:59 +00:00
|
|
|
return (unsigned char) __AX__;
|
2002-03-24 13:26:18 +00:00
|
|
|
}
|
|
|
|
|
2005-02-14 09:19:59 +00:00
|
|
|
#pragma optimize (pop)
|
|
|
|
|
2002-03-24 13:26:18 +00:00
|
|
|
|
|
|
|
|
2004-05-02 21:01:12 +00:00
|
|
|
static void InvertCharSet (void)
|
|
|
|
/* Invert the character set */
|
|
|
|
{
|
|
|
|
asm ("ldy #%b", sizeof (CharSet) - 1);
|
|
|
|
asm ("L1:");
|
|
|
|
asm ("lda %v,y", CharSet);
|
|
|
|
asm ("eor #$FF");
|
|
|
|
asm ("sta %v,y", CharSet);
|
|
|
|
asm ("dey");
|
|
|
|
asm ("bpl L1");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-04-19 06:46:30 +00:00
|
|
|
/*****************************************************************************/
|
2005-02-14 09:38:30 +00:00
|
|
|
/* Code */
|
2001-04-19 06:46:30 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-02-14 09:19:59 +00:00
|
|
|
static void PushBack (void)
|
|
|
|
/* Push back the last (unused) character, provided it is not EOF. */
|
2004-11-27 14:45:49 +00:00
|
|
|
{
|
2005-02-14 09:19:59 +00:00
|
|
|
/* Get the character from C. */
|
|
|
|
/* Only the high-byte needs to be checked for EOF. */
|
|
|
|
asm ("ldx %v+1", C);
|
|
|
|
asm ("bne %g", Done);
|
|
|
|
asm ("lda %v", C);
|
|
|
|
|
|
|
|
/* Put unget()'s first argument on the stack. */
|
|
|
|
asm ("jsr pushax");
|
|
|
|
|
|
|
|
/* Copy D into the zero-page. */
|
|
|
|
(const struct scanfdata*) __AX__ = D_;
|
|
|
|
asm ("sta ptr1");
|
|
|
|
asm ("stx ptr1+1");
|
|
|
|
|
|
|
|
/* Copy the unget vector to jmpvec. */
|
|
|
|
asm ("ldy #%b", offsetof (struct scanfdata, unget));
|
|
|
|
asm ("lda (ptr1),y");
|
|
|
|
asm ("sta jmpvec+1");
|
|
|
|
asm ("iny");
|
|
|
|
asm ("lda (ptr1),y");
|
|
|
|
asm ("sta jmpvec+2");
|
|
|
|
|
|
|
|
/* Load D->data into __AX__. */
|
|
|
|
asm ("ldy #%b", offsetof (struct scanfdata, data) + 1);
|
|
|
|
asm ("lda (ptr1),y");
|
|
|
|
asm ("tax");
|
|
|
|
asm ("dey");
|
|
|
|
asm ("lda (ptr1),y");
|
|
|
|
|
|
|
|
/* Call the unget routine. */
|
|
|
|
asm ("jsr jmpvec");
|
|
|
|
|
|
|
|
/* Take back that character's count. */
|
|
|
|
asm ("lda %v", CharCount);
|
|
|
|
asm ("bne %g", Yank);
|
|
|
|
asm ("dec %v+1", CharCount);
|
|
|
|
Yank:
|
|
|
|
asm ("dec %v", CharCount);
|
|
|
|
|
|
|
|
Done:
|
2008-08-03 12:54:11 +00:00
|
|
|
;
|
2004-11-27 14:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-07-14 14:41:30 +00:00
|
|
|
static void ReadChar (void)
|
2001-04-19 06:46:30 +00:00
|
|
|
/* Get an input character, count characters */
|
|
|
|
{
|
2004-11-27 14:45:49 +00:00
|
|
|
/* Move D to ptr1 */
|
|
|
|
asm ("lda %v", D_);
|
|
|
|
asm ("ldx %v+1", D_);
|
|
|
|
asm ("sta ptr1");
|
|
|
|
asm ("stx ptr1+1");
|
|
|
|
|
|
|
|
/* Copy the get vector to jmpvec */
|
|
|
|
asm ("ldy #%b", offsetof (struct scanfdata, get));
|
|
|
|
asm ("lda (ptr1),y");
|
|
|
|
asm ("sta jmpvec+1");
|
|
|
|
asm ("iny");
|
|
|
|
asm ("lda (ptr1),y");
|
|
|
|
asm ("sta jmpvec+2");
|
|
|
|
|
|
|
|
/* Load D->data into __AX__ */
|
2005-02-14 09:19:59 +00:00
|
|
|
asm ("ldy #%b", offsetof (struct scanfdata, data) + 1);
|
2004-11-27 14:45:49 +00:00
|
|
|
asm ("lda (ptr1),y");
|
|
|
|
asm ("tax");
|
|
|
|
asm ("dey");
|
|
|
|
asm ("lda (ptr1),y");
|
|
|
|
|
|
|
|
/* Call the get routine */
|
|
|
|
asm ("jsr jmpvec");
|
|
|
|
|
|
|
|
/* Assign the result to C */
|
|
|
|
asm ("sta %v", C);
|
|
|
|
asm ("stx %v+1", C);
|
|
|
|
|
2005-02-14 09:19:59 +00:00
|
|
|
/* If C is EOF, don't bump the character counter.
|
|
|
|
* Only the high-byte needs to be checked.
|
|
|
|
*/
|
2004-11-27 14:45:49 +00:00
|
|
|
asm ("inx");
|
2005-02-14 09:19:59 +00:00
|
|
|
asm ("beq %g", Done);
|
2004-11-27 14:45:49 +00:00
|
|
|
|
|
|
|
/* Must bump CharCount. */
|
|
|
|
asm ("inc %v", CharCount);
|
|
|
|
asm ("bne %g", Done);
|
|
|
|
asm ("inc %v+1", CharCount);
|
|
|
|
|
|
|
|
Done:
|
2008-08-03 12:54:11 +00:00
|
|
|
;
|
2004-11-26 22:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-02-14 09:19:59 +00:00
|
|
|
#pragma optimize (push, off)
|
|
|
|
|
|
|
|
static void __fastcall__ Error (unsigned char /* Code */)
|
|
|
|
/* Does a longjmp using the given code */
|
2004-11-26 22:16:54 +00:00
|
|
|
{
|
2005-02-14 09:19:59 +00:00
|
|
|
asm ("pha");
|
|
|
|
(char*) __AX__ = JumpBuf;
|
|
|
|
asm ("jsr pushax");
|
|
|
|
asm ("pla");
|
|
|
|
asm ("ldx #>0");
|
|
|
|
asm ("jmp %v", longjmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma optimize (pop)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void CheckEnd (void)
|
|
|
|
/* Stop a scan if it prematurely reaches the end of a string or a file. */
|
|
|
|
{
|
|
|
|
/* Only the high-byte needs to be checked for EOF. */
|
|
|
|
asm ("ldx %v+1", C);
|
|
|
|
asm ("beq %g", Done);
|
|
|
|
|
2005-02-14 09:22:37 +00:00
|
|
|
Error (RC_EOF);
|
2005-02-14 09:19:59 +00:00
|
|
|
Done:
|
2008-08-03 12:54:11 +00:00
|
|
|
;
|
2001-04-19 06:46:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-07-14 14:41:30 +00:00
|
|
|
static void SkipWhite (void)
|
2001-04-19 06:46:30 +00:00
|
|
|
/* Skip white space in the input and return the first non white character */
|
|
|
|
{
|
2005-02-14 09:19:59 +00:00
|
|
|
while ((bool) isspace (C)) {
|
2005-02-14 09:22:37 +00:00
|
|
|
ReadChar ();
|
2001-04-19 06:46:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-02-14 09:19:59 +00:00
|
|
|
#pragma optimize (push, off)
|
|
|
|
|
2001-07-14 14:41:30 +00:00
|
|
|
static void ReadSign (void)
|
|
|
|
/* Read an optional sign and skip it. Store 1 in Positive if the value is
|
|
|
|
* positive, store 0 otherwise.
|
2001-04-19 06:46:30 +00:00
|
|
|
*/
|
|
|
|
{
|
2004-11-27 14:45:49 +00:00
|
|
|
/* We can ignore the high byte of C here, since if it is EOF, the lower
|
|
|
|
* byte won't match anyway.
|
|
|
|
*/
|
|
|
|
asm ("lda %v", C);
|
|
|
|
asm ("cmp #'-'");
|
|
|
|
asm ("bne %g", NotNeg);
|
|
|
|
|
|
|
|
/* Negative value */
|
2005-02-14 09:19:59 +00:00
|
|
|
asm ("sta %v", Converted);
|
2004-11-27 14:45:49 +00:00
|
|
|
asm ("jsr %v", ReadChar);
|
|
|
|
asm ("lda #$00"); /* Flag as negative */
|
|
|
|
asm ("beq %g", Store);
|
|
|
|
|
|
|
|
/* Positive value */
|
|
|
|
NotNeg:
|
|
|
|
asm ("cmp #'+'");
|
|
|
|
asm ("bne %g", Pos);
|
2005-02-14 09:19:59 +00:00
|
|
|
asm ("sta %v", Converted);
|
2004-11-27 14:45:49 +00:00
|
|
|
asm ("jsr %v", ReadChar); /* Skip the + sign */
|
|
|
|
Pos:
|
|
|
|
asm ("lda #$01"); /* Flag as positive */
|
|
|
|
Store:
|
|
|
|
asm ("sta %v", Positive);
|
2001-04-19 06:46:30 +00:00
|
|
|
}
|
|
|
|
|
2005-02-14 09:19:59 +00:00
|
|
|
#pragma optimize (pop)
|
|
|
|
|
2001-04-19 06:46:30 +00:00
|
|
|
|
|
|
|
|
2005-02-14 09:19:59 +00:00
|
|
|
static unsigned char __fastcall__ HexVal (char C)
|
2001-04-19 06:46:30 +00:00
|
|
|
/* Convert a digit to a value */
|
|
|
|
{
|
2005-02-14 09:19:59 +00:00
|
|
|
return (bool) isdigit (C) ?
|
2005-02-14 09:22:37 +00:00
|
|
|
C - '0' :
|
|
|
|
(char) tolower ((int) C) - ('a' - 10);
|
2005-02-14 09:19:59 +00:00
|
|
|
}
|
2001-04-19 06:46:30 +00:00
|
|
|
|
2005-02-14 09:19:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
static void __fastcall__ ReadInt (unsigned char Base)
|
|
|
|
/* Read an integer, and store it into IntVal. */
|
|
|
|
{
|
|
|
|
unsigned char Val, CharCount = 0;
|
|
|
|
|
|
|
|
/* Read the integer value */
|
|
|
|
IntVal = 0L;
|
|
|
|
while ((bool) isxdigit (C) && ++Width != 0
|
2005-02-14 09:22:37 +00:00
|
|
|
&& (Val = HexVal ((char) C)) < Base) {
|
|
|
|
++CharCount;
|
|
|
|
IntVal = IntVal * (long) Base + (long) Val;
|
|
|
|
ReadChar ();
|
2001-04-19 06:46:30 +00:00
|
|
|
}
|
2005-02-14 09:19:59 +00:00
|
|
|
|
|
|
|
/* If we didn't convert anything, it's a failure. */
|
|
|
|
if (CharCount == 0) {
|
2005-02-14 09:22:37 +00:00
|
|
|
Error (RC_NOCONV);
|
2005-02-14 09:19:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Another conversion */
|
|
|
|
Converted = true;
|
2001-04-19 06:46:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-07-14 14:41:30 +00:00
|
|
|
static void AssignInt (void)
|
|
|
|
/* Assign the integer value in Val to the next argument. The function makes
|
2005-02-14 09:19:59 +00:00
|
|
|
* several non-portable assumptions, to reduce code size:
|
|
|
|
* - signed and unsigned types have the same representation.
|
2001-07-14 14:41:30 +00:00
|
|
|
* - short and int have the same representation.
|
|
|
|
* - all pointer types have the same representation.
|
2001-04-19 06:46:30 +00:00
|
|
|
*/
|
|
|
|
{
|
2005-02-14 09:19:59 +00:00
|
|
|
if (NoAssign == false) {
|
2004-05-13 23:01:53 +00:00
|
|
|
|
|
|
|
/* Get the next argument pointer */
|
2005-02-14 09:19:59 +00:00
|
|
|
(void*) __AX__ = va_arg (ap, void*);
|
2004-05-13 23:01:53 +00:00
|
|
|
|
2005-02-14 09:19:59 +00:00
|
|
|
/* Put the argument pointer into the zero-page. */
|
2004-05-13 23:01:53 +00:00
|
|
|
asm ("sta ptr1");
|
|
|
|
asm ("stx ptr1+1");
|
|
|
|
|
|
|
|
/* Get the number of bytes-1 to copy */
|
2004-11-26 22:16:54 +00:00
|
|
|
asm ("ldy %v", IntBytes);
|
2004-05-13 23:01:53 +00:00
|
|
|
|
|
|
|
/* Assign the integer value */
|
2004-11-27 14:45:49 +00:00
|
|
|
Loop: asm ("lda %v,y", IntVal);
|
2004-05-13 23:01:53 +00:00
|
|
|
asm ("sta (ptr1),y");
|
|
|
|
asm ("dey");
|
2004-11-27 14:45:49 +00:00
|
|
|
asm ("bpl %g", Loop);
|
2004-05-13 23:01:53 +00:00
|
|
|
|
2005-02-14 09:22:37 +00:00
|
|
|
/* Another assignment */
|
|
|
|
asm ("inc %v", Assignments);
|
|
|
|
asm ("bne %g", Done);
|
|
|
|
asm ("inc %v+1", Assignments);
|
2008-08-03 12:54:11 +00:00
|
|
|
Done: ;
|
2001-07-14 14:41:30 +00:00
|
|
|
}
|
|
|
|
}
|
2001-04-19 06:46:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2004-11-27 14:45:49 +00:00
|
|
|
static void __fastcall__ ScanInt (unsigned char Base)
|
2004-11-26 22:16:54 +00:00
|
|
|
/* Scan an integer including white space, sign and optional base spec,
|
|
|
|
* and store it into IntVal.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
/* Skip whitespace */
|
|
|
|
SkipWhite ();
|
|
|
|
|
|
|
|
/* Read an optional sign */
|
|
|
|
ReadSign ();
|
|
|
|
|
|
|
|
/* If Base is unknown (zero), figure it out */
|
|
|
|
if (Base == 0) {
|
2004-11-27 14:45:49 +00:00
|
|
|
if (CHAR (C) == '0') {
|
2004-11-26 22:16:54 +00:00
|
|
|
ReadChar ();
|
2004-11-27 14:45:49 +00:00
|
|
|
switch (CHAR (C)) {
|
2004-11-26 22:16:54 +00:00
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
Base = 16;
|
2005-02-14 09:38:30 +00:00
|
|
|
Converted = true;
|
2004-11-26 22:16:54 +00:00
|
|
|
ReadChar ();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Base = 8;
|
2005-02-14 09:19:59 +00:00
|
|
|
|
2005-02-14 09:38:30 +00:00
|
|
|
/* Restart at the beginning of the number because it might
|
|
|
|
* be only a single zero digit (which already was read).
|
|
|
|
*/
|
|
|
|
PushBack ();
|
|
|
|
C = '0';
|
2004-11-26 22:16:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Base = 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the integer value */
|
|
|
|
ReadInt (Base);
|
|
|
|
|
|
|
|
/* Apply the sign */
|
2005-02-14 09:19:59 +00:00
|
|
|
if (Positive == false) {
|
2004-11-26 22:16:54 +00:00
|
|
|
IntVal = -IntVal;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assign the value to the next argument unless suppressed */
|
|
|
|
AssignInt ();
|
2005-02-14 09:19:59 +00:00
|
|
|
}
|
2004-11-26 22:16:54 +00:00
|
|
|
|
2005-02-14 09:19:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
static char GetFormat (void)
|
|
|
|
/* Pick up the next character from the format string. */
|
|
|
|
{
|
|
|
|
/* return (F = *format++); */
|
|
|
|
(const char*) __AX__ = format;
|
|
|
|
asm ("sta regsave");
|
|
|
|
asm ("stx regsave+1");
|
|
|
|
++format;
|
|
|
|
asm ("ldy #0");
|
|
|
|
asm ("lda (regsave),y");
|
|
|
|
asm ("ldx #>0");
|
|
|
|
return (F = (char) __AX__);
|
2004-11-26 22:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2005-02-14 09:19:59 +00:00
|
|
|
int __fastcall__ _scanf (const struct scanfdata* D,
|
2005-02-14 09:38:30 +00:00
|
|
|
const char* format_, va_list ap_)
|
2001-07-14 14:41:30 +00:00
|
|
|
/* This is the routine used to do the actual work. It is called from several
|
|
|
|
* types of wrappers to implement the actual ISO xxscanf functions.
|
|
|
|
*/
|
|
|
|
{
|
2005-02-14 09:19:59 +00:00
|
|
|
register char* S;
|
|
|
|
bool HaveWidth; /* True if a width was given */
|
2005-02-14 09:22:37 +00:00
|
|
|
bool Match; /* True if a character-set has any matches */
|
2005-02-14 09:19:59 +00:00
|
|
|
char Start; /* Walks over a range */
|
2001-07-14 14:41:30 +00:00
|
|
|
|
|
|
|
/* Place copies of the arguments into global variables. This is not very
|
|
|
|
* nice, but on a 6502 platform it gives better code, since the values
|
|
|
|
* do not have to be passed as parameters.
|
|
|
|
*/
|
2005-02-14 09:22:37 +00:00
|
|
|
D_ = D;
|
2005-02-14 09:19:59 +00:00
|
|
|
format = format_;
|
2005-02-14 09:22:37 +00:00
|
|
|
ap = ap_;
|
2001-04-19 06:46:30 +00:00
|
|
|
|
|
|
|
/* Initialize variables */
|
2005-02-14 09:19:59 +00:00
|
|
|
Converted = false;
|
|
|
|
Assignments = 0;
|
2004-11-27 14:45:49 +00:00
|
|
|
CharCount = 0;
|
2001-04-19 06:46:30 +00:00
|
|
|
|
2005-02-14 09:19:59 +00:00
|
|
|
/* Set up the jump "label". CheckEnd() will use that label when EOF
|
|
|
|
* is reached. ReadInt() will use it when number-conversion fails.
|
2001-04-19 06:46:30 +00:00
|
|
|
*/
|
2005-02-14 09:19:59 +00:00
|
|
|
if ((unsigned char) setjmp (JumpBuf) == RC_OK) {
|
2001-04-19 06:46:30 +00:00
|
|
|
Again:
|
2005-02-14 09:19:59 +00:00
|
|
|
|
2005-02-14 09:22:37 +00:00
|
|
|
/* Get the next input character */
|
|
|
|
ReadChar ();
|
2001-04-19 06:46:30 +00:00
|
|
|
|
2005-02-14 09:22:37 +00:00
|
|
|
/* Walk over the format string */
|
|
|
|
while (GetFormat ()) {
|
2001-04-19 06:46:30 +00:00
|
|
|
|
2005-02-14 09:22:37 +00:00
|
|
|
/* Check for a conversion */
|
|
|
|
if (F != '%') {
|
2001-04-19 06:46:30 +00:00
|
|
|
|
2005-02-14 09:38:30 +00:00
|
|
|
/* Check for a match */
|
|
|
|
if ((bool) isspace ((int) F)) {
|
2001-04-19 06:46:30 +00:00
|
|
|
|
2005-02-14 09:38:30 +00:00
|
|
|
/* Special white space handling: Any whitespace in the
|
2004-11-26 22:16:54 +00:00
|
|
|
* format string matches any amount of whitespace including
|
|
|
|
* none(!). So this match will never fail.
|
2005-02-14 09:38:30 +00:00
|
|
|
*/
|
|
|
|
SkipWhite ();
|
|
|
|
continue;
|
|
|
|
}
|
2001-04-19 06:46:30 +00:00
|
|
|
|
2005-02-14 09:19:59 +00:00
|
|
|
Percent:
|
2005-02-14 09:38:30 +00:00
|
|
|
/* ### Note: The opposite test (C == F)
|
|
|
|
** would be optimized into buggy code!
|
|
|
|
*/
|
|
|
|
if (C != (int) F) {
|
2005-02-14 09:22:37 +00:00
|
|
|
|
2005-02-14 09:38:30 +00:00
|
|
|
/* A mismatch -- we will stop scanning the input,
|
|
|
|
* and return the number of assigned conversions.
|
|
|
|
*/
|
|
|
|
goto NoConv;
|
|
|
|
}
|
2005-02-14 09:22:37 +00:00
|
|
|
|
2005-02-14 09:38:30 +00:00
|
|
|
/* A match -- get the next input character, and continue. */
|
|
|
|
goto Again;
|
2005-02-14 09:22:37 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2005-02-14 09:38:30 +00:00
|
|
|
/* A conversion. Skip the percent sign. */
|
|
|
|
/* 0. Check for %% */
|
|
|
|
if (GetFormat () == '%') {
|
|
|
|
goto Percent;
|
|
|
|
}
|
2005-02-14 09:22:37 +00:00
|
|
|
|
2005-02-14 09:38:30 +00:00
|
|
|
/* 1. Assignment suppression */
|
|
|
|
NoAssign = (F == '*');
|
|
|
|
if (NoAssign) {
|
|
|
|
GetFormat ();
|
2004-11-26 22:16:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 2. Maximum field width */
|
2005-02-14 09:38:30 +00:00
|
|
|
Width = UINT_MAX;
|
|
|
|
HaveWidth = (bool) isdigit (F);
|
|
|
|
if (HaveWidth) {
|
|
|
|
Width = 0;
|
2004-11-26 22:16:54 +00:00
|
|
|
do {
|
|
|
|
/* ### Non portable ### */
|
|
|
|
Width = Width * 10 + (F & 0x0F);
|
2005-02-14 09:38:30 +00:00
|
|
|
} while ((bool) isdigit (GetFormat ()));
|
2004-11-26 22:16:54 +00:00
|
|
|
}
|
2005-02-14 09:38:30 +00:00
|
|
|
if (Width == 0) {
|
|
|
|
/* Invalid specification */
|
|
|
|
/* Note: This method of leaving the function might seem
|
|
|
|
* to be crude, but it optimizes very well because
|
|
|
|
* the four exits can share this code.
|
|
|
|
*/
|
|
|
|
_seterrno (EINVAL);
|
|
|
|
Assignments = EOF;
|
|
|
|
PushBack ();
|
|
|
|
return Assignments;
|
|
|
|
}
|
|
|
|
/* Increment-and-test makes better code than test-and-decrement
|
|
|
|
* does. So, change the width into a form that can be used in
|
|
|
|
* that way.
|
|
|
|
*/
|
|
|
|
Width = ~Width;
|
2004-11-26 22:16:54 +00:00
|
|
|
|
|
|
|
/* 3. Length modifier */
|
|
|
|
IntBytes = sizeof(int) - 1;
|
|
|
|
switch (F) {
|
|
|
|
case 'h':
|
|
|
|
if (*format == 'h') {
|
|
|
|
IntBytes = sizeof(char) - 1;
|
|
|
|
++format;
|
|
|
|
}
|
2005-02-14 09:38:30 +00:00
|
|
|
GetFormat ();
|
2004-11-26 22:16:54 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
|
|
|
if (*format == 'l') {
|
|
|
|
/* Treat long long as long */
|
|
|
|
++format;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 'j': /* intmax_t */
|
|
|
|
IntBytes = sizeof(long) - 1;
|
2005-02-14 09:38:30 +00:00
|
|
|
/* FALLTHROUGH */
|
2004-11-26 22:16:54 +00:00
|
|
|
|
|
|
|
case 'z': /* size_t */
|
|
|
|
case 't': /* ptrdiff_t */
|
2005-02-14 09:38:30 +00:00
|
|
|
/* Same size as int */
|
2005-02-14 09:19:59 +00:00
|
|
|
|
2004-11-26 22:16:54 +00:00
|
|
|
case 'L': /* long double - ignore this one */
|
2005-02-14 09:38:30 +00:00
|
|
|
GetFormat ();
|
2004-11-26 22:16:54 +00:00
|
|
|
}
|
2001-04-19 06:46:30 +00:00
|
|
|
|
2004-11-26 22:16:54 +00:00
|
|
|
/* 4. Conversion specifier */
|
2005-02-14 09:38:30 +00:00
|
|
|
switch (F) {
|
2004-11-26 22:16:54 +00:00
|
|
|
/* 'd' and 'u' conversions are actually the same, since the
|
2005-02-14 09:19:59 +00:00
|
|
|
* standard says that even the 'u' modifier allows an
|
2004-11-26 22:16:54 +00:00
|
|
|
* optionally signed integer.
|
|
|
|
*/
|
2005-02-14 09:38:30 +00:00
|
|
|
case 'd': /* Optionally signed decimal integer */
|
2004-11-26 22:16:54 +00:00
|
|
|
case 'u':
|
2005-02-14 09:38:30 +00:00
|
|
|
ScanInt (10);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
|
|
|
/* Optionally signed integer with a base */
|
|
|
|
ScanInt (0);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o':
|
|
|
|
/* Optionally signed octal integer */
|
|
|
|
ScanInt (8);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
/* Optionally signed hexadecimal integer */
|
|
|
|
ScanInt (16);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
/* Whitespace-terminated string */
|
|
|
|
SkipWhite ();
|
|
|
|
CheckEnd (); /* Is it an input failure? */
|
2005-02-14 09:19:59 +00:00
|
|
|
Converted = true; /* No, conversion will succeed */
|
2005-02-14 09:38:30 +00:00
|
|
|
if (NoAssign == false) {
|
|
|
|
S = va_arg (ap, char*);
|
|
|
|
}
|
|
|
|
while (C != EOF
|
|
|
|
&& (bool) isspace (C) == false
|
|
|
|
&& ++Width) {
|
|
|
|
if (NoAssign == false) {
|
|
|
|
*S++ = C;
|
|
|
|
}
|
|
|
|
ReadChar ();
|
|
|
|
}
|
|
|
|
/* Terminate the string just read */
|
|
|
|
if (NoAssign == false) {
|
|
|
|
*S = '\0';
|
|
|
|
++Assignments;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
|
|
|
/* Fixed-length string, NOT zero-terminated */
|
|
|
|
if (HaveWidth == false) {
|
|
|
|
/* No width given, default is 1 */
|
|
|
|
Width = ~1u;
|
|
|
|
}
|
|
|
|
CheckEnd (); /* Is it an input failure? */
|
|
|
|
Converted = true; /* No, at least 1 char. available */
|
|
|
|
if (NoAssign == false) {
|
|
|
|
S = va_arg (ap, char*);
|
|
|
|
/* ## This loop is convenient for us, but it isn't
|
|
|
|
* standard C. The standard implies that a failure
|
|
|
|
* shouldn't put anything into the array argument.
|
|
|
|
*/
|
2005-02-14 09:19:59 +00:00
|
|
|
while (++Width) {
|
2005-02-14 09:38:30 +00:00
|
|
|
CheckEnd (); /* Is it a matching failure? */
|
2002-03-24 13:26:18 +00:00
|
|
|
*S++ = C;
|
2005-02-14 09:19:59 +00:00
|
|
|
ReadChar ();
|
2002-03-24 13:26:18 +00:00
|
|
|
}
|
2005-02-14 09:38:30 +00:00
|
|
|
++Assignments;
|
|
|
|
} else {
|
2002-03-24 13:26:18 +00:00
|
|
|
/* Just skip as many chars as given */
|
2005-02-14 09:19:59 +00:00
|
|
|
while (++Width) {
|
2005-02-14 09:38:30 +00:00
|
|
|
CheckEnd (); /* Is it a matching failure? */
|
2005-02-14 09:19:59 +00:00
|
|
|
ReadChar ();
|
2002-03-24 13:26:18 +00:00
|
|
|
}
|
|
|
|
}
|
2005-02-14 09:38:30 +00:00
|
|
|
break;
|
2001-04-19 06:46:30 +00:00
|
|
|
|
2005-02-14 09:38:30 +00:00
|
|
|
case '[':
|
|
|
|
/* String using characters from a set */
|
2002-03-24 13:26:18 +00:00
|
|
|
/* Clear the set */
|
|
|
|
memset (CharSet, 0, sizeof (CharSet));
|
2005-02-14 09:38:30 +00:00
|
|
|
/* Skip the left-bracket, and test for inversion. */
|
|
|
|
Invert = (GetFormat () == '^');
|
2005-02-14 09:19:59 +00:00
|
|
|
if (Invert) {
|
|
|
|
GetFormat ();
|
2002-03-24 13:26:18 +00:00
|
|
|
}
|
|
|
|
if (F == ']') {
|
2005-02-14 09:38:30 +00:00
|
|
|
/* Empty sets aren't allowed; so, a right-bracket
|
|
|
|
* at the beginning must be a member of the set.
|
|
|
|
*/
|
2005-02-14 09:19:59 +00:00
|
|
|
AddCharToSet (F);
|
|
|
|
GetFormat ();
|
2002-03-24 13:26:18 +00:00
|
|
|
}
|
|
|
|
/* Read the characters that are part of the set */
|
2005-02-14 09:19:59 +00:00
|
|
|
while (F != '\0' && F != ']') {
|
|
|
|
if (*format == '-') { /* Look ahead at next char. */
|
2002-03-24 13:26:18 +00:00
|
|
|
/* A range. Get start and end, skip the '-' */
|
|
|
|
Start = F;
|
|
|
|
++format;
|
2005-02-14 09:19:59 +00:00
|
|
|
switch (GetFormat ()) {
|
2005-02-14 09:38:30 +00:00
|
|
|
case '\0':
|
|
|
|
case ']':
|
2005-02-14 09:19:59 +00:00
|
|
|
/* '-' as last char means: include '-' */
|
2002-03-24 13:26:18 +00:00
|
|
|
AddCharToSet (Start);
|
2005-02-14 09:19:59 +00:00
|
|
|
AddCharToSet ('-');
|
2005-02-14 09:38:30 +00:00
|
|
|
break;
|
|
|
|
default:
|
2005-02-14 09:19:59 +00:00
|
|
|
/* Include all characters
|
2005-02-14 09:38:30 +00:00
|
|
|
* that are in the range.
|
|
|
|
*/
|
2005-02-14 09:19:59 +00:00
|
|
|
while (1) {
|
|
|
|
AddCharToSet (Start);
|
|
|
|
if (Start == F) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++Start;
|
2002-03-24 13:26:18 +00:00
|
|
|
}
|
2005-02-14 09:19:59 +00:00
|
|
|
/* Get next char after range */
|
|
|
|
GetFormat ();
|
2002-03-24 13:26:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* Just a character */
|
|
|
|
AddCharToSet (F);
|
|
|
|
/* Get next char */
|
2005-02-14 09:19:59 +00:00
|
|
|
GetFormat ();
|
2002-03-24 13:26:18 +00:00
|
|
|
}
|
|
|
|
}
|
2005-02-14 09:38:30 +00:00
|
|
|
/* Don't go beyond the end of the format string. */
|
|
|
|
/* (Maybe, this should mean an invalid specification.) */
|
|
|
|
if (F == '\0') {
|
|
|
|
--format;
|
|
|
|
}
|
2002-03-24 13:26:18 +00:00
|
|
|
|
|
|
|
/* Invert the set if requested */
|
|
|
|
if (Invert) {
|
2004-05-02 21:01:12 +00:00
|
|
|
InvertCharSet ();
|
2002-03-24 13:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We have the set in CharSet. Read characters and
|
|
|
|
* store them into a string while they are part of
|
|
|
|
* the set.
|
|
|
|
*/
|
2005-02-14 09:38:30 +00:00
|
|
|
Match = false;
|
|
|
|
if (NoAssign == false) {
|
|
|
|
S = va_arg (ap, char*);
|
|
|
|
}
|
2005-02-14 09:19:59 +00:00
|
|
|
while (IsCharInSet () && ++Width) {
|
2005-02-14 09:38:30 +00:00
|
|
|
if (NoAssign == false) {
|
2002-03-24 13:26:18 +00:00
|
|
|
*S++ = C;
|
2005-02-14 09:38:30 +00:00
|
|
|
}
|
|
|
|
Match = Converted = true;
|
|
|
|
ReadChar ();
|
|
|
|
}
|
|
|
|
/* At least one character must match the set. */
|
|
|
|
if (Match == false) {
|
|
|
|
goto NoConv;
|
2002-03-24 13:26:18 +00:00
|
|
|
}
|
2005-02-14 09:38:30 +00:00
|
|
|
if (NoAssign == false) {
|
2005-02-14 09:19:59 +00:00
|
|
|
*S = '\0';
|
2005-02-14 09:38:30 +00:00
|
|
|
++Assignments;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
/* Pointer, general format is 0xABCD.
|
|
|
|
* %hhp --> zero-page pointer
|
|
|
|
* %hp --> near pointer
|
|
|
|
* %lp --> far pointer
|
|
|
|
*/
|
|
|
|
SkipWhite ();
|
|
|
|
if (CHAR (C) != '0') {
|
|
|
|
goto NoConv;
|
|
|
|
}
|
|
|
|
Converted = true;
|
|
|
|
ReadChar ();
|
|
|
|
switch (CHAR (C)) {
|
|
|
|
case 'x':
|
|
|
|
case 'X':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
goto NoConv;
|
2002-03-24 13:26:18 +00:00
|
|
|
}
|
|
|
|
ReadChar ();
|
2005-02-14 09:19:59 +00:00
|
|
|
ReadInt (16);
|
2005-02-14 09:38:30 +00:00
|
|
|
AssignInt ();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n':
|
|
|
|
/* Store the number of characters consumed so far
|
|
|
|
* (the read-ahead character hasn't been consumed).
|
|
|
|
*/
|
|
|
|
IntVal = (long) (CharCount - (C == EOF ? 0u : 1u));
|
|
|
|
AssignInt ();
|
|
|
|
/* Don't count it. */
|
|
|
|
if (NoAssign == false) {
|
|
|
|
--Assignments;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'S':
|
|
|
|
case 'C':
|
|
|
|
/* Wide characters */
|
|
|
|
|
|
|
|
case 'a':
|
|
|
|
case 'A':
|
|
|
|
case 'e':
|
|
|
|
case 'E':
|
|
|
|
case 'f':
|
|
|
|
case 'F':
|
|
|
|
case 'g':
|
|
|
|
case 'G':
|
|
|
|
/* Optionally signed float */
|
|
|
|
|
|
|
|
/* Those 2 groups aren't implemented. */
|
|
|
|
_seterrno (ENOSYS);
|
|
|
|
Assignments = EOF;
|
|
|
|
PushBack ();
|
|
|
|
return Assignments;
|
|
|
|
|
|
|
|
default:
|
|
|
|
/* Invalid specification */
|
|
|
|
_seterrno (EINVAL);
|
|
|
|
Assignments = EOF;
|
|
|
|
PushBack ();
|
|
|
|
return Assignments;
|
|
|
|
}
|
2005-02-14 09:22:37 +00:00
|
|
|
}
|
|
|
|
}
|
2004-11-26 22:16:54 +00:00
|
|
|
} else {
|
2005-02-14 09:19:59 +00:00
|
|
|
NoConv:
|
2001-04-19 06:46:30 +00:00
|
|
|
|
2005-02-14 09:22:37 +00:00
|
|
|
/* Coming here means a failure. If that happens at EOF, with no
|
2005-02-14 09:19:59 +00:00
|
|
|
* conversion attempts, then it is considered an error; otherwise,
|
2005-02-14 09:22:37 +00:00
|
|
|
* the number of assignments is returned (the default behaviour).
|
2004-11-26 22:16:54 +00:00
|
|
|
*/
|
2005-02-14 09:22:37 +00:00
|
|
|
if (C == EOF && Converted == false) {
|
2005-02-14 09:38:30 +00:00
|
|
|
Assignments = EOF; /* Special case: error */
|
2005-02-14 09:22:37 +00:00
|
|
|
}
|
2001-04-19 06:46:30 +00:00
|
|
|
}
|
|
|
|
|
2005-02-14 09:19:59 +00:00
|
|
|
/* Put the read-ahead character back into the input stream. */
|
|
|
|
PushBack ();
|
2001-04-19 06:46:30 +00:00
|
|
|
|
2005-02-14 09:19:59 +00:00
|
|
|
/* Return the number of conversion-and-assignments. */
|
|
|
|
return Assignments;
|
|
|
|
}
|
2001-04-19 06:46:30 +00:00
|
|
|
|
|
|
|
|
2004-11-27 14:45:49 +00:00
|
|
|
|