OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [misc/] [dprintf.c] - Diff between revs 1010 and 1765

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 1010 Rev 1765
/* Debugging printf, for debugging the library itself.
/* Debugging printf, for debugging the library itself.
 
 
   We don't assume stdio is working.
   We don't assume stdio is working.
   We do assume _write_r is working.
   We do assume _write_r is working.
*/
*/
 
 
#include "ctype.h"
#include "ctype.h"
#include "reent.h"
#include "reent.h"
#include "string.h"
#include "string.h"
#include "unctrl.h"
#include "unctrl.h"
 
 
#ifdef __STDC__
#ifdef __STDC__
#include "stdarg.h"
#include "stdarg.h"
#else
#else
#include "varargs.h"
#include "varargs.h"
#endif
#endif
 
 
static char *parse_number ();
static char *parse_number ();
static long get_number ();
static long get_number ();
static void print_number ();
static void print_number ();
static void write_char ();
static void write_char ();
static void write_string ();
static void write_string ();
 
 
/* Non-zero for big-endian systems.  */
/* Non-zero for big-endian systems.  */
static int big_endian_p;
static int big_endian_p;
 
 
/* For now hardcode 2 (stderr) as the console file descriptor.
/* For now hardcode 2 (stderr) as the console file descriptor.
   May wish to let the caller pass in a file descriptor or some such but
   May wish to let the caller pass in a file descriptor or some such but
   this is only for debugging purposes anyway.  */
   this is only for debugging purposes anyway.  */
#define CONSOLE_FD 2
#define CONSOLE_FD 2
 
 
/* Standalone printf routine.
/* Standalone printf routine.
 
 
   The format string has been enhanced so that multiple values can be dumped
   The format string has been enhanced so that multiple values can be dumped
   without having to have a %-field for each one (say if you want to dump
   without having to have a %-field for each one (say if you want to dump
   20 words at a certain address).  A modifier of `N' says the next argument
   20 words at a certain address).  A modifier of `N' says the next argument
   is a count, and the one after that is a pointer.
   is a count, and the one after that is a pointer.
 
 
   Example: __dprintf (stderr, "%Nx\n", 20, p); /-* print 20 ints at `p' *-/
   Example: __dprintf (stderr, "%Nx\n", 20, p); /-* print 20 ints at `p' *-/
 
 
   Supported formats are: c d u x s p.
   Supported formats are: c d u x s p.
 
 
   All ints are retrieved a byte at a time so alignment issues are not
   All ints are retrieved a byte at a time so alignment issues are not
   a problem.
   a problem.
 
 
   This routine is used in situations where the only debugging capability
   This routine is used in situations where the only debugging capability
   is console output and was written to aid debugging newlib itself.  We don't
   is console output and was written to aid debugging newlib itself.  We don't
   use printf ourselves as we may be debugging it.  We do assume _write_r is
   use printf ourselves as we may be debugging it.  We do assume _write_r is
   working.
   working.
*/
*/
 
 
void
void
#ifdef __STDC__
#ifdef __STDC__
__dprintf (char *fmt, ...)
__dprintf (char *fmt, ...)
#else
#else
__dprintf (fmt, va_alist)
__dprintf (fmt, va_alist)
     char *fmt;
     char *fmt;
     va_dcl
     va_dcl
#endif
#endif
{
{
  va_list args;
  va_list args;
 
 
  /* Which endian are we?  */
  /* Which endian are we?  */
  {
  {
    short tmp = 1;
    short tmp = 1;
    big_endian_p = *(char *) &tmp == 0;
    big_endian_p = *(char *) &tmp == 0;
  }
  }
 
 
#ifdef __STDC__
#ifdef __STDC__
  va_start (args, fmt);
  va_start (args, fmt);
#else
#else
  va_start (args);
  va_start (args);
#endif
#endif
 
 
  while (*fmt)
  while (*fmt)
    {
    {
      char c, *p;
      char c, *p;
      int count;
      int count;
      long l;
      long l;
 
 
      if (*fmt != '%' || *++fmt == '%')
      if (*fmt != '%' || *++fmt == '%')
        {
        {
          write_char (*fmt++);
          write_char (*fmt++);
          continue;
          continue;
        }
        }
 
 
      if (*fmt == 'N')
      if (*fmt == 'N')
        {
        {
          count = va_arg (args, int);
          count = va_arg (args, int);
          p = va_arg (args, char *);
          p = va_arg (args, char *);
          ++fmt;
          ++fmt;
          c = *fmt++;
          c = *fmt++;
 
 
          while (--count >= 0)
          while (--count >= 0)
            {
            {
              switch (c)
              switch (c)
                {
                {
                case 'c' :
                case 'c' :
                  write_string (unctrl (*p++));
                  write_string (unctrl (*p++));
                  break;
                  break;
                case 'p' :
                case 'p' :
                  print_number (16, 1, get_number (p, sizeof (char *), 1));
                  print_number (16, 1, get_number (p, sizeof (char *), 1));
                  p += sizeof (char *);
                  p += sizeof (char *);
                  break;
                  break;
                case 'd' :
                case 'd' :
                case 'u' :
                case 'u' :
                case 'x' :
                case 'x' :
                  print_number (c == 'x' ? 16 : 10, c != 'd',
                  print_number (c == 'x' ? 16 : 10, c != 'd',
                                get_number (p, sizeof (int), c != 'd'));
                                get_number (p, sizeof (int), c != 'd'));
                  p += sizeof (int);
                  p += sizeof (int);
                  break;
                  break;
                case 's' :
                case 's' :
                  write_string (*(char **) p);
                  write_string (*(char **) p);
                  p += sizeof (char *);
                  p += sizeof (char *);
                  break;
                  break;
                }
                }
              if (count > 0)
              if (count > 0)
                write_char (' ');
                write_char (' ');
            }
            }
        }
        }
      else
      else
        {
        {
          switch (c = *fmt++)
          switch (c = *fmt++)
            {
            {
            case 'c' :
            case 'c' :
              c = va_arg (args, int);
              c = va_arg (args, int);
              write_string (unctrl (c));
              write_string (unctrl (c));
              break;
              break;
            case 'p' :
            case 'p' :
              l = (_POINTER_INT) va_arg (args, char *);
              l = (_POINTER_INT) va_arg (args, char *);
              print_number (16, 1, l);
              print_number (16, 1, l);
              break;
              break;
            case 'd' :
            case 'd' :
            case 'u' :
            case 'u' :
            case 'x' :
            case 'x' :
              l = va_arg (args, int);
              l = va_arg (args, int);
              print_number (c == 'x' ? 16 : 10, c != 'd', l);
              print_number (c == 'x' ? 16 : 10, c != 'd', l);
              break;
              break;
            case 's' :
            case 's' :
              p = va_arg (args, char *);
              p = va_arg (args, char *);
              write_string (p);
              write_string (p);
              break;
              break;
            }
            }
        }
        }
    }
    }
 
 
  va_end (args);
  va_end (args);
}
}
 
 
/* Parse a positive decimal integer at S.
/* Parse a positive decimal integer at S.
   FIXME: Was used in earlier version, but not currently used.
   FIXME: Was used in earlier version, but not currently used.
   Keep for now.  */
   Keep for now.  */
 
 
static char *
static char *
parse_number (s, p)
parse_number (s, p)
     char *s;
     char *s;
     long *p;
     long *p;
{
{
  long x = 0;
  long x = 0;
 
 
  while (isdigit (*s))
  while (isdigit (*s))
    {
    {
      x = (x * 10) + (*s - '0');
      x = (x * 10) + (*s - '0');
      ++s;
      ++s;
    }
    }
 
 
  *p = x;
  *p = x;
  return s;
  return s;
}
}
 
 
/* Fetch the number at S of SIZE bytes.  */
/* Fetch the number at S of SIZE bytes.  */
 
 
static long
static long
get_number (s, size, unsigned_p)
get_number (s, size, unsigned_p)
     char *s;
     char *s;
     long size;
     long size;
     int unsigned_p;
     int unsigned_p;
{
{
  long x;
  long x;
  unsigned char *p = (unsigned char *) s;
  unsigned char *p = (unsigned char *) s;
 
 
  switch (size)
  switch (size)
    {
    {
    case 1 :
    case 1 :
      x = *p;
      x = *p;
      if (!unsigned_p)
      if (!unsigned_p)
        x = (x ^ 0x80) - 0x80;
        x = (x ^ 0x80) - 0x80;
      return x;
      return x;
    case 2 :
    case 2 :
      if (big_endian_p)
      if (big_endian_p)
        x = (p[0] << 8) | p[1];
        x = (p[0] << 8) | p[1];
      else
      else
        x = (p[1] << 8) | p[0];
        x = (p[1] << 8) | p[0];
      if (!unsigned_p)
      if (!unsigned_p)
        x = (x ^ 0x8000) - 0x8000;
        x = (x ^ 0x8000) - 0x8000;
      return x;
      return x;
    case 4 :
    case 4 :
      if (big_endian_p)
      if (big_endian_p)
        x = ((long)p[0] << 24) | ((long)p[1] << 16) | (p[2] << 8) | p[3];
        x = ((long)p[0] << 24) | ((long)p[1] << 16) | (p[2] << 8) | p[3];
      else
      else
        x = ((long)p[3] << 24) | ((long)p[2] << 16) | (p[1] << 8) | p[0];
        x = ((long)p[3] << 24) | ((long)p[2] << 16) | (p[1] << 8) | p[0];
      if (!unsigned_p)
      if (!unsigned_p)
        x = (x ^ 0x80000000L) - 0x80000000L;
        x = (x ^ 0x80000000L) - 0x80000000L;
      return x;
      return x;
#if 0 /* FIXME: Is there a standard mechanism for knowing if
#if 0 /* FIXME: Is there a standard mechanism for knowing if
         long longs exist?  */
         long longs exist?  */
    case 8 :
    case 8 :
#endif
#endif
    default :
    default :
      return 0;
      return 0;
  }
  }
}
}
 
 
/* Print X in base BASE.  */
/* Print X in base BASE.  */
 
 
static void
static void
print_number (base, unsigned_p, n)
print_number (base, unsigned_p, n)
     int base;
     int base;
     int unsigned_p;
     int unsigned_p;
     long n;
     long n;
{
{
  static char chars[16] = "0123456789abcdef";
  static char chars[16] = "0123456789abcdef";
  char *p, buf[32];
  char *p, buf[32];
  unsigned long x;
  unsigned long x;
 
 
  if (!unsigned_p && n < 0)
  if (!unsigned_p && n < 0)
    {
    {
      write_char ('-');
      write_char ('-');
      x = -n;
      x = -n;
    }
    }
  else
  else
    x = n;
    x = n;
 
 
  p = buf + sizeof (buf);
  p = buf + sizeof (buf);
  *--p = '\0';
  *--p = '\0';
  do
  do
    {
    {
      *--p = chars[x % base];
      *--p = chars[x % base];
      x /= base;
      x /= base;
    }
    }
  while (x != 0);
  while (x != 0);
 
 
  write_string (p);
  write_string (p);
}
}
 
 
/* Write C to the console.
/* Write C to the console.
   We go through the file descriptor directly because we can't assume
   We go through the file descriptor directly because we can't assume
   stdio is working.  */
   stdio is working.  */
 
 
static void
static void
write_char (c)
write_char (c)
     char c;
     char c;
{
{
  _write_r (_REENT, CONSOLE_FD, &c, 1);
  _write_r (_REENT, CONSOLE_FD, &c, 1);
}
}
 
 
/* Write S to the console.
/* Write S to the console.
   We go through the file descriptor directly because we can't assume
   We go through the file descriptor directly because we can't assume
   stdio is working.  */
   stdio is working.  */
 
 
static void
static void
write_string (s)
write_string (s)
     char *s;
     char *s;
{
{
  _write_r (_REENT, CONSOLE_FD, s, strlen (s));
  _write_r (_REENT, CONSOLE_FD, s, strlen (s));
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.