Welcome to mirror list, hosted at ThFree Co, Russian Federation.

dprintf.c « misc « libc « newlib - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 0b780f59ba15432bc05bf008052c8fa2cb0a2ae6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* Debugging printf, for debugging the library itself.

   We don't assume stdio is working.
   We do assume _write_r is working.
*/

#include "ctype.h"
#include "reent.h"
#include "string.h"
#include "unctrl.h"

#ifdef __STDC__
#include "stdarg.h"
#else
#include "varargs.h"
#endif

#if 0
static char *parse_number ();
#endif

static long get_number ();
static void print_number ();
static void write_char ();
static void write_string ();

/* 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.

   Example: __dprintf (stderr, "%Nx\n", 20, p); /-* print 20 ints at `p' *-/

   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__
__dprintf (char *fmt, ...)
#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
/* 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

/* Fetch the number at S of SIZE bytes.  */

static long
get_number (s, size, unsigned_p)
     char *s;
     long size;
     int unsigned_p;
{
  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 (base, unsigned_p, n)
     int base;
     int unsigned_p;
     long n;
{
  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 (c)
     char c;
{
  _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 (s)
     char *s;
{
  _write_r (_REENT, CONSOLE_FD, s, strlen (s));
}