libc/newlib/libc/misc/__dprintf.c

269 lines
5.1 KiB
C
Raw Normal View History

2000-02-17 20:39:52 +01:00
/* Debugging printf, for debugging the library itself.
We don't assume stdio is working.
We do assume _write_r is working.
*/
2008-12-11 Craig Howland <howland@LGSInnovations.com> * libc/include/sys/lock.h: Add void cast to avoid "statement has no effect" warnings from gcc. * libc/include/sys/stdio.h: Ditto. * libc/include/sys/time.h: Correct gettimeofday() prototype. * libc/stdlib/__exp10.c: Add #include "std.h" for function prototype. * libc/stdlib/__ten_mu.c: Ditto. * libc/stdlib/std.h: Correct __exp10's ANSI prototype. * libc/stdlib/ldtoa.c: Change eiisinf definition to ANSI form. (Are already others in file without _ansi method, so did not bother.) * libc/stdlib/system.c: Use _ansi forms for function prototypes and definitions. * libc/time/mktime.c: Ditto. * libc/misc/__dprintf.c: Ditto. * libc/include/stdio.h: Add function prototypes for _fgetc_r, _fgetpos_r, _fsetpos_r, _freopen_r, _rewind_r, freopen64, _freopen64_r, _funopen_r, and _fopencookie_r. * libc/include/reent.h: Add function prototype for _stat64_r, align _execve_r prototype with POSIX definition for execve. * libc/reent/execr.c: Align function prototype with POSIX definition. * libc/stdio/asniprintf.c: Add #include "local.h". * libc/stdio/vasniprintf.c: Ditto. * libc/stdio/fread.c: Remove unused variable newcount. * libc/stdio/local.h: Add function prototype for __sccl. * libc/stdio/open_memstream.c: Remove unused variable flags. * libc/stdio/vfscanf.c: Proper prototyping for ccfn, remove prototype for __sccl since now in local.h. * libc/string/memcpy.c: Add #include <string.h> (for real and for traditional synopsis), remove extraneous stddef.h and limits.h. * libc/syscalls/sysclose.c: Add #include <unistd.h>. * libc/syscalls/sysfork.c: Ditto. * libc/syscalls/sysgetpid.c: Ditto. * libc/syscalls/sysexecve.c: Add #include <unistd.h>, align function prototype with POSIX definition. * libc/syscalls/sysfstat.c: Add #include <sys/stat.h>. * libc/syscalls/sysgettod.c: Correct sys/times.h to sys/time.h. * libc/syscalls/syskill.c: Add #include <signal.h>. * libc/syscalls/syslink.c: Add #include <unistd.h>, fix prototype. * libc/syscalls/sysunlink.c: Ditto. * libc/syscalls/sysstat.c: Add #include <sys/stat.h>, fix prototype. * libc/syscalls/syswait.c: Add #include <sys/wait.h>, fix prototype.
2008-12-11 18:27:56 +01:00
#include <_ansi.h>
2000-02-17 20:39:52 +01:00
#include "ctype.h"
#include "reent.h"
#include "string.h"
#include "unctrl.h"
#ifdef __STDC__
#include "stdarg.h"
#else
#include "varargs.h"
#endif
#if 0
2000-02-17 20:39:52 +01:00
static char *parse_number ();
#endif
static long get_number (char *, long, int);
static void print_number (int, int, long);
static void write_char (char c);
static void write_string (const char *s);
2000-02-17 20:39:52 +01:00
/* Non-zero for big-endian systems. */
static int big_endian_p;
/* 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
this is only for debugging purposes anyway. */
#define CONSOLE_FD 2
/* Standalone printf routine.
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
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.
2008-12-11 Craig Howland <howland@LGSInnovations.com> * libc/include/sys/lock.h: Add void cast to avoid "statement has no effect" warnings from gcc. * libc/include/sys/stdio.h: Ditto. * libc/include/sys/time.h: Correct gettimeofday() prototype. * libc/stdlib/__exp10.c: Add #include "std.h" for function prototype. * libc/stdlib/__ten_mu.c: Ditto. * libc/stdlib/std.h: Correct __exp10's ANSI prototype. * libc/stdlib/ldtoa.c: Change eiisinf definition to ANSI form. (Are already others in file without _ansi method, so did not bother.) * libc/stdlib/system.c: Use _ansi forms for function prototypes and definitions. * libc/time/mktime.c: Ditto. * libc/misc/__dprintf.c: Ditto. * libc/include/stdio.h: Add function prototypes for _fgetc_r, _fgetpos_r, _fsetpos_r, _freopen_r, _rewind_r, freopen64, _freopen64_r, _funopen_r, and _fopencookie_r. * libc/include/reent.h: Add function prototype for _stat64_r, align _execve_r prototype with POSIX definition for execve. * libc/reent/execr.c: Align function prototype with POSIX definition. * libc/stdio/asniprintf.c: Add #include "local.h". * libc/stdio/vasniprintf.c: Ditto. * libc/stdio/fread.c: Remove unused variable newcount. * libc/stdio/local.h: Add function prototype for __sccl. * libc/stdio/open_memstream.c: Remove unused variable flags. * libc/stdio/vfscanf.c: Proper prototyping for ccfn, remove prototype for __sccl since now in local.h. * libc/string/memcpy.c: Add #include <string.h> (for real and for traditional synopsis), remove extraneous stddef.h and limits.h. * libc/syscalls/sysclose.c: Add #include <unistd.h>. * libc/syscalls/sysfork.c: Ditto. * libc/syscalls/sysgetpid.c: Ditto. * libc/syscalls/sysexecve.c: Add #include <unistd.h>, align function prototype with POSIX definition. * libc/syscalls/sysfstat.c: Add #include <sys/stat.h>. * libc/syscalls/sysgettod.c: Correct sys/times.h to sys/time.h. * libc/syscalls/syskill.c: Add #include <signal.h>. * libc/syscalls/syslink.c: Add #include <unistd.h>, fix prototype. * libc/syscalls/sysunlink.c: Ditto. * libc/syscalls/sysstat.c: Add #include <sys/stat.h>, fix prototype. * libc/syscalls/syswait.c: Add #include <sys/wait.h>, fix prototype.
2008-12-11 18:27:56 +01:00
Example: __dprintf ("%Nx\n", 20, p); /-* print 20 ints at `p' *-/
2000-02-17 20:39:52 +01:00
Supported formats are: c d u x s p.
All ints are retrieved a byte at a time so alignment issues are not
a problem.
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
use printf ourselves as we may be debugging it. We do assume _write_r is
working.
*/
void
#ifdef __STDC__
2008-12-11 Craig Howland <howland@LGSInnovations.com> * libc/include/sys/lock.h: Add void cast to avoid "statement has no effect" warnings from gcc. * libc/include/sys/stdio.h: Ditto. * libc/include/sys/time.h: Correct gettimeofday() prototype. * libc/stdlib/__exp10.c: Add #include "std.h" for function prototype. * libc/stdlib/__ten_mu.c: Ditto. * libc/stdlib/std.h: Correct __exp10's ANSI prototype. * libc/stdlib/ldtoa.c: Change eiisinf definition to ANSI form. (Are already others in file without _ansi method, so did not bother.) * libc/stdlib/system.c: Use _ansi forms for function prototypes and definitions. * libc/time/mktime.c: Ditto. * libc/misc/__dprintf.c: Ditto. * libc/include/stdio.h: Add function prototypes for _fgetc_r, _fgetpos_r, _fsetpos_r, _freopen_r, _rewind_r, freopen64, _freopen64_r, _funopen_r, and _fopencookie_r. * libc/include/reent.h: Add function prototype for _stat64_r, align _execve_r prototype with POSIX definition for execve. * libc/reent/execr.c: Align function prototype with POSIX definition. * libc/stdio/asniprintf.c: Add #include "local.h". * libc/stdio/vasniprintf.c: Ditto. * libc/stdio/fread.c: Remove unused variable newcount. * libc/stdio/local.h: Add function prototype for __sccl. * libc/stdio/open_memstream.c: Remove unused variable flags. * libc/stdio/vfscanf.c: Proper prototyping for ccfn, remove prototype for __sccl since now in local.h. * libc/string/memcpy.c: Add #include <string.h> (for real and for traditional synopsis), remove extraneous stddef.h and limits.h. * libc/syscalls/sysclose.c: Add #include <unistd.h>. * libc/syscalls/sysfork.c: Ditto. * libc/syscalls/sysgetpid.c: Ditto. * libc/syscalls/sysexecve.c: Add #include <unistd.h>, align function prototype with POSIX definition. * libc/syscalls/sysfstat.c: Add #include <sys/stat.h>. * libc/syscalls/sysgettod.c: Correct sys/times.h to sys/time.h. * libc/syscalls/syskill.c: Add #include <signal.h>. * libc/syscalls/syslink.c: Add #include <unistd.h>, fix prototype. * libc/syscalls/sysunlink.c: Ditto. * libc/syscalls/sysstat.c: Add #include <sys/stat.h>, fix prototype. * libc/syscalls/syswait.c: Add #include <sys/wait.h>, fix prototype.
2008-12-11 18:27:56 +01:00
__dprintf (const char *fmt, ...)
2000-02-17 20:39:52 +01:00
#else
__dprintf (fmt, va_alist)
char *fmt;
va_dcl
#endif
{
va_list args;
/* Which endian are we? */
{
short tmp = 1;
big_endian_p = *(char *) &tmp == 0;
}
#ifdef __STDC__
va_start (args, fmt);
#else
va_start (args);
#endif
while (*fmt)
{
char c, *p;
int count;
long l;
if (*fmt != '%' || *++fmt == '%')
{
write_char (*fmt++);
continue;
}
if (*fmt == 'N')
{
count = va_arg (args, int);
p = va_arg (args, char *);
++fmt;
c = *fmt++;
while (--count >= 0)
{
switch (c)
{
case 'c' :
write_string (unctrl (*p++));
break;
case 'p' :
print_number (16, 1, get_number (p, sizeof (char *), 1));
p += sizeof (char *);
break;
case 'd' :
case 'u' :
case 'x' :
print_number (c == 'x' ? 16 : 10, c != 'd',
get_number (p, sizeof (int), c != 'd'));
p += sizeof (int);
break;
case 's' :
write_string (*(char **) p);
p += sizeof (char *);
break;
}
if (count > 0)
write_char (' ');
}
}
else
{
switch (c = *fmt++)
{
case 'c' :
c = va_arg (args, int);
write_string (unctrl (c));
break;
case 'p' :
l = (_POINTER_INT) va_arg (args, char *);
print_number (16, 1, l);
break;
case 'd' :
case 'u' :
case 'x' :
l = va_arg (args, int);
print_number (c == 'x' ? 16 : 10, c != 'd', l);
break;
case 's' :
p = va_arg (args, char *);
write_string (p);
break;
}
}
}
va_end (args);
}
#if 0
2000-02-17 20:39:52 +01:00
/* Parse a positive decimal integer at S.
FIXME: Was used in earlier version, but not currently used.
Keep for now. */
static char *
parse_number (s, p)
char *s;
long *p;
{
long x = 0;
while (isdigit (*s))
{
x = (x * 10) + (*s - '0');
++s;
}
*p = x;
return s;
}
#endif
2000-02-17 20:39:52 +01:00
/* Fetch the number at S of SIZE bytes. */
static long
get_number (char *s,
long size,
2008-12-11 Craig Howland <howland@LGSInnovations.com> * libc/include/sys/lock.h: Add void cast to avoid "statement has no effect" warnings from gcc. * libc/include/sys/stdio.h: Ditto. * libc/include/sys/time.h: Correct gettimeofday() prototype. * libc/stdlib/__exp10.c: Add #include "std.h" for function prototype. * libc/stdlib/__ten_mu.c: Ditto. * libc/stdlib/std.h: Correct __exp10's ANSI prototype. * libc/stdlib/ldtoa.c: Change eiisinf definition to ANSI form. (Are already others in file without _ansi method, so did not bother.) * libc/stdlib/system.c: Use _ansi forms for function prototypes and definitions. * libc/time/mktime.c: Ditto. * libc/misc/__dprintf.c: Ditto. * libc/include/stdio.h: Add function prototypes for _fgetc_r, _fgetpos_r, _fsetpos_r, _freopen_r, _rewind_r, freopen64, _freopen64_r, _funopen_r, and _fopencookie_r. * libc/include/reent.h: Add function prototype for _stat64_r, align _execve_r prototype with POSIX definition for execve. * libc/reent/execr.c: Align function prototype with POSIX definition. * libc/stdio/asniprintf.c: Add #include "local.h". * libc/stdio/vasniprintf.c: Ditto. * libc/stdio/fread.c: Remove unused variable newcount. * libc/stdio/local.h: Add function prototype for __sccl. * libc/stdio/open_memstream.c: Remove unused variable flags. * libc/stdio/vfscanf.c: Proper prototyping for ccfn, remove prototype for __sccl since now in local.h. * libc/string/memcpy.c: Add #include <string.h> (for real and for traditional synopsis), remove extraneous stddef.h and limits.h. * libc/syscalls/sysclose.c: Add #include <unistd.h>. * libc/syscalls/sysfork.c: Ditto. * libc/syscalls/sysgetpid.c: Ditto. * libc/syscalls/sysexecve.c: Add #include <unistd.h>, align function prototype with POSIX definition. * libc/syscalls/sysfstat.c: Add #include <sys/stat.h>. * libc/syscalls/sysgettod.c: Correct sys/times.h to sys/time.h. * libc/syscalls/syskill.c: Add #include <signal.h>. * libc/syscalls/syslink.c: Add #include <unistd.h>, fix prototype. * libc/syscalls/sysunlink.c: Ditto. * libc/syscalls/sysstat.c: Add #include <sys/stat.h>, fix prototype. * libc/syscalls/syswait.c: Add #include <sys/wait.h>, fix prototype.
2008-12-11 18:27:56 +01:00
int unsigned_p)
2000-02-17 20:39:52 +01:00
{
long x;
unsigned char *p = (unsigned char *) s;
switch (size)
{
case 1 :
x = *p;
if (!unsigned_p)
x = (x ^ 0x80) - 0x80;
return x;
case 2 :
if (big_endian_p)
x = (p[0] << 8) | p[1];
else
x = (p[1] << 8) | p[0];
if (!unsigned_p)
x = (x ^ 0x8000) - 0x8000;
return x;
case 4 :
if (big_endian_p)
x = ((long)p[0] << 24) | ((long)p[1] << 16) | (p[2] << 8) | p[3];
else
x = ((long)p[3] << 24) | ((long)p[2] << 16) | (p[1] << 8) | p[0];
if (!unsigned_p)
x = (x ^ 0x80000000L) - 0x80000000L;
return x;
#if 0 /* FIXME: Is there a standard mechanism for knowing if
long longs exist? */
case 8 :
#endif
default :
return 0;
}
}
/* Print X in base BASE. */
static void
print_number (int base,
int unsigned_p,
2008-12-11 Craig Howland <howland@LGSInnovations.com> * libc/include/sys/lock.h: Add void cast to avoid "statement has no effect" warnings from gcc. * libc/include/sys/stdio.h: Ditto. * libc/include/sys/time.h: Correct gettimeofday() prototype. * libc/stdlib/__exp10.c: Add #include "std.h" for function prototype. * libc/stdlib/__ten_mu.c: Ditto. * libc/stdlib/std.h: Correct __exp10's ANSI prototype. * libc/stdlib/ldtoa.c: Change eiisinf definition to ANSI form. (Are already others in file without _ansi method, so did not bother.) * libc/stdlib/system.c: Use _ansi forms for function prototypes and definitions. * libc/time/mktime.c: Ditto. * libc/misc/__dprintf.c: Ditto. * libc/include/stdio.h: Add function prototypes for _fgetc_r, _fgetpos_r, _fsetpos_r, _freopen_r, _rewind_r, freopen64, _freopen64_r, _funopen_r, and _fopencookie_r. * libc/include/reent.h: Add function prototype for _stat64_r, align _execve_r prototype with POSIX definition for execve. * libc/reent/execr.c: Align function prototype with POSIX definition. * libc/stdio/asniprintf.c: Add #include "local.h". * libc/stdio/vasniprintf.c: Ditto. * libc/stdio/fread.c: Remove unused variable newcount. * libc/stdio/local.h: Add function prototype for __sccl. * libc/stdio/open_memstream.c: Remove unused variable flags. * libc/stdio/vfscanf.c: Proper prototyping for ccfn, remove prototype for __sccl since now in local.h. * libc/string/memcpy.c: Add #include <string.h> (for real and for traditional synopsis), remove extraneous stddef.h and limits.h. * libc/syscalls/sysclose.c: Add #include <unistd.h>. * libc/syscalls/sysfork.c: Ditto. * libc/syscalls/sysgetpid.c: Ditto. * libc/syscalls/sysexecve.c: Add #include <unistd.h>, align function prototype with POSIX definition. * libc/syscalls/sysfstat.c: Add #include <sys/stat.h>. * libc/syscalls/sysgettod.c: Correct sys/times.h to sys/time.h. * libc/syscalls/syskill.c: Add #include <signal.h>. * libc/syscalls/syslink.c: Add #include <unistd.h>, fix prototype. * libc/syscalls/sysunlink.c: Ditto. * libc/syscalls/sysstat.c: Add #include <sys/stat.h>, fix prototype. * libc/syscalls/syswait.c: Add #include <sys/wait.h>, fix prototype.
2008-12-11 18:27:56 +01:00
long n)
2000-02-17 20:39:52 +01:00
{
static char chars[16] = "0123456789abcdef";
char *p, buf[32];
unsigned long x;
if (!unsigned_p && n < 0)
{
write_char ('-');
x = -n;
}
else
x = n;
p = buf + sizeof (buf);
*--p = '\0';
do
{
*--p = chars[x % base];
x /= base;
}
while (x != 0);
write_string (p);
}
/* Write C to the console.
We go through the file descriptor directly because we can't assume
stdio is working. */
static void
write_char (char c)
2000-02-17 20:39:52 +01:00
{
_write_r (_REENT, CONSOLE_FD, &c, 1);
}
/* Write S to the console.
We go through the file descriptor directly because we can't assume
stdio is working. */
static void
write_string (const char *s)
2000-02-17 20:39:52 +01:00
{
_write_r (_REENT, CONSOLE_FD, s, strlen (s));
}