New bitmap method SetRGBConversionFunction which can be used to
[tangerine.git] / compiler / arossupport / kprintf.c
blob7506d8ba836fb4824e48ae7e8cc47edff1ff18f5
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Formats a message and makes sure the user will see it.
6 Lang: english
7 */
9 #include <aros/config.h>
10 #include <aros/arossupportbase.h>
11 #include <stdarg.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <aros/system.h>
15 #include <proto/exec.h>
16 #include <proto/arossupport.h>
17 #undef kprintf
18 #undef vkprintf
19 #include <unistd.h>
20 #include <exec/execbase.h>
22 #define AROSBase ((struct AROSBase *)(SysBase->DebugData))
24 /* Can't use ctypt.h *sigh* */
25 #define isdigit(x) ((x) >= '0' && (x) <= '9')
26 #define isprint(x) (((x) >= ' ' && (x) <= 128) || (x) >= 160)
28 /*****************************************************************************
30 NAME */
31 #include <proto/arossupport.h>
33 int kprintf (
35 /* SYNOPSIS */
36 const UBYTE * fmt,
37 ...)
39 /* FUNCTION
40 Formats fmt with the specified arguments like printf() (and *not*
41 like RawDoFmt()) and uses a secure way to deliver the message to
42 the user; ie. the user *will* see this message no matter what.
44 INPUTS
45 fmt - printf()-style format string
47 RESULT
48 The number of characters output.
50 NOTES
51 This function is not part of a library and may thus be called
52 any time.
54 EXAMPLE
56 BUGS
58 SEE ALSO
60 INTERNALS
62 HISTORY
63 24-12-95 digulla created
65 ******************************************************************************/
67 va_list ap;
68 int result;
70 va_start (ap, fmt);
71 result = vkprintf (fmt, ap);
72 va_end (ap);
74 return result;
75 } /* kprintf */
79 int vkprintf (const UBYTE * fmt, va_list args)
81 AROS_GET_SYSBASE_OK
82 int ret;
83 static const char uhex[] = "0123456789ABCDEF";
84 static const char lhex[] = "0123456789abcdef";
85 char * fill;
86 ULONG val;
87 LONG lval;
89 if (!fmt)
91 RawPutChars ("(null)", 6);
92 return 6;
95 ret = 0;
97 while (*fmt)
99 if (*fmt == '%')
101 int width = 0;
102 int precision = 0;
104 fmt ++;
106 if (*fmt == '0')
108 fill = "00000000";
109 fmt ++;
111 else
113 fill = " ";
116 if (*fmt == '*')
118 width = va_arg (args, int);
119 fmt++;
121 else
122 if (isdigit (*fmt))
124 width = atoi (fmt);
125 while (isdigit(*fmt)) fmt++;
128 if (*fmt == '.') fmt++;
130 if (*fmt == '*')
132 precision = va_arg (args, int);
133 fmt++;
135 else
136 if (isdigit (*fmt))
137 precision = atoi (fmt);
139 while (isdigit(*fmt) || *fmt=='.' || *fmt=='-' || *fmt=='+')
140 fmt ++;
142 switch (*fmt)
144 case '%':
145 RawPutChar (*fmt);
146 ret ++;
147 break;
149 case 's':
150 case 'S': {
151 char * str = va_arg (args, char *);
152 int len;
154 if (!str)
155 str = "(null)";
157 if (*fmt == 'S')
159 RawPutChar ('"');
160 ret ++;
163 if (precision)
164 len = precision;
165 else
166 len = strlen (str);
168 RawPutChars (str, len);
169 ret += len;
171 if (*fmt == 'S')
173 RawPutChar ('"');
174 ret ++;
177 break; }
179 case 'p': {
180 int t;
181 char puffer[sizeof (void *)*2];
182 ULONG val;
184 t = sizeof (void *)*2;
185 val = va_arg (args, ULONG);
187 while (t)
189 puffer[--t] = lhex[val & 0x0F];
190 val >>= 4;
193 RawPutChars (puffer, sizeof (void *)*2);
195 break; }
197 case 'c': {
198 UBYTE c;
200 c = va_arg (args, int);
202 if (isprint (c))
203 RawPutChar (c);
204 else
206 RawPutChars ("'\\0x", 4);
207 RawPutChar (lhex[c / 16]);
208 RawPutChar (lhex[c & 15]);
209 RawPutChar ('\'');
212 break; }
214 case 'l': {
215 int t;
216 char puffer[32];
218 if (fmt[1] == 'u' || fmt[1] == 'd' || fmt[1] == 'x' || fmt[1] == 'X')
219 fmt ++;
221 if (*fmt == 'd')
223 lval = va_arg (args, LONG);
225 val = (lval < 0) ? -lval : lval;
227 else
229 val = va_arg (args, ULONG);
232 print_int:
233 if (val==0)
235 if (width == 0)
236 width = 1;
238 if (*fill == ' ')
239 width --;
241 while (width > 0)
243 RawPutChars (fill, (width < 8) ? width : 8);
244 width -= 8;
247 if (*fill == ' ')
248 RawPutChar ('0');
250 ret ++;
251 break;
254 t = 32;
256 if (*fmt == 'd' || *fmt == 'u')
258 if (*fmt == 'd')
260 if (lval < 0)
262 RawPutChar ('-');
263 ret ++;
264 val = -lval;
266 else
267 val = lval;
270 while (val && t)
272 puffer[--t] = lhex[val % 10];
274 val /= 10;
277 else if (*fmt == 'x')
279 while (val && t)
281 puffer[--t] = lhex[val & 0x0F];
283 val >>= 4;
286 else
288 while (val && t)
290 puffer[--t] = uhex[val & 0x0F];
292 val >>= 4;
296 width -= 32-t;
298 while (width > 0)
300 RawPutChars (fill, (width < 8) ? width : 8);
301 width -= 8;
304 RawPutChars (&puffer[t], 32-t);
305 ret += 32-t;
307 break; }
309 default: {
310 if (*fmt == 'd')
312 lval = va_arg (args, int);
314 val = (lval < 0) ? -lval : lval;
316 else
318 val = va_arg (args, unsigned int);
321 goto print_int;
323 break; }
324 } /* switch */
326 else
328 RawPutChar (*fmt);
329 ret ++;
332 fmt ++; /* Next char */
333 } /* while (*fmt); */
335 return ret;
336 } /* vkprintf */