Changed _xdata, _near, etc and removed _generic from library files
[fw/sdcc] / device / lib / vprintf.c
index 09f936ab94b531b59c4301cb8aac6769b15061c8..77bd361fab7df3a33580a8701466cb6fe8496f36 100644 (file)
    You are forbidden to forbid anyone else to use, share and improve
    what you give them.   Help stamp out software-hoarding!  
 -------------------------------------------------------------------------*/
+
+/* this module uses some global variables instead function parameters, so: */
+#ifdef SDCC_STACK_AUTO
+#warning "this module cannot yet be use as a reentrant one"
+#endif
+
+#ifdef __ds390
 #define USE_FLOATS 1
+#endif
 
 #include <stdarg.h>
 #include <string.h>
@@ -41,7 +49,7 @@
 
 /****************************************************************************/
 
-typedef char _generic *ptr_t;
+typedef char ptr_t;
 
 #ifdef toupper
 #undef toupper
@@ -56,7 +64,7 @@ typedef union
   long           l;
   unsigned long  ul;
   float          f;
-  char _generic *p;
+  char           *p;
 } value_t;
 
 
@@ -70,7 +78,10 @@ static bit   lsd;
 /* this one NEEDS to be in data */
 static data value_t value;
 
-static unsigned short radix;
+static unsigned char radix;
+
+// jwk: TODO: this makes the whole dammed thing nonreentrent
+static int charsOutputted;
 
 /****************************************************************************/
 
@@ -84,6 +95,7 @@ static void output_char( char c ) reentrant
   {
     putchar( c );
   }
+  charsOutputted++;
 }
 
 /*--------------------------------------------------------------------------*/
@@ -153,12 +165,12 @@ static void output_float (float f, unsigned char reqWidth,
                          signed char reqDecimals,
                          bit left, bit zero, bit sign, bit space)
 {
-  XSPEC char negative=0;
-  XSPEC long integerPart;
-  XSPEC float decimalPart;
-  XSPEC char fpBuffer[128];
-  XSPEC char fpBI=0, fpBD;
-  XSPEC unsigned char minWidth, i;
+  char negative=0;
+  long integerPart;
+  float decimalPart;
+  char fpBuffer[128];
+  char fpBI=0, fpBD;
+  unsigned char minWidth, i;
 
   // save the sign
   if (f<0) {
@@ -246,19 +258,22 @@ static void output_float (float f, unsigned char reqWidth,
 
 int vsprintf (const char *buf, const char *format, va_list ap)
 {
-  bit            left_justify;
-  bit            zero_padding;
-  bit            prefix_sign;
-  bit            prefix_space;
-  bit            signed_argument;
-  bit            char_argument;
-  bit            long_argument;
-  bit            float_argument;
-
-  XSPEC unsigned char  width;
-  XSPEC signed char decimals;
-  XSPEC unsigned char  length;
-  XSPEC char           c;
+  static bit            left_justify;
+  static bit            zero_padding;
+  static bit            prefix_sign;
+  static bit            prefix_space;
+  static bit            signed_argument;
+  static bit            char_argument;
+  static bit            long_argument;
+  static bit            float_argument;
+
+  unsigned char  width;
+  signed char decimals;
+  unsigned char  length;
+  char           c;
+
+  // reset output chars
+  charsOutputted=0;
 
   output_ptr = buf;
   if ( !buf )
@@ -346,7 +361,7 @@ get_conversion_spec:
        goto get_conversion_spec;
 
       case 'C':
-               output_char( va_arg(ap,unsigned char) );
+               output_char( va_arg(ap,int) );
        break;
 
       case 'S':
@@ -596,7 +611,12 @@ _endasm;
        
   // Copy \0 to the end of buf
   // Modified by JB 17/12/99
-  if (output_to_string) output_char(0);
+  if (output_to_string) {
+    output_char(0);
+    return charsOutputted-1;
+  } else {
+    return charsOutputted;
+  }
 }
 
 /*--------------------------------------------------------------------------*/