Subversion Repositories HelenOS

Rev

Rev 992 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
974 cejka 1
/*
2
 * Copyright (C) 2001-2004 Jakub Jermar
3
 * Copyright (C) 2006 Josef Cejka
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * - Redistributions of source code must retain the above copyright
11
 *   notice, this list of conditions and the following disclaimer.
12
 * - Redistributions in binary form must reproduce the above copyright
13
 *   notice, this list of conditions and the following disclaimer in the
14
 *   documentation and/or other materials provided with the distribution.
15
 * - The name of the author may not be used to endorse or promote products
16
 *   derived from this software without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 */
29
 
30
#include <stdio.h>
31
#include <unistd.h>
32
#include <io/io.h>
33
#include <stdarg.h>
34
 
995 cejka 35
#define __PRINTF_FLAG_PREFIX 0x00000001
36
#define __PRINTF_FLAG_SIGNED 0x00000002
37
 
974 cejka 38
static char digits[] = "0123456789abcdef"; 	/**< Hexadecimal characters */
39
 
40
/** Print hexadecimal digits
41
 *
42
 * Print fixed count of hexadecimal digits from
43
 * the number num. The digits are printed in
44
 * natural left-to-right order starting with
45
 * the width-th digit.
46
 *
47
 * @param num   Number containing digits.
48
 * @param width Count of digits to print.
49
 *
50
 */
995 cejka 51
static int print_fixed_hex(const uint64_t num, const int width, uint64_t flags)
974 cejka 52
{
53
	int i;
995 cejka 54
	char buf[18]; /* 16 bytes for number + 2 for optionaly prefix */
974 cejka 55
	char *bptr;
56
 
57
	bptr = buf;
995 cejka 58
 
59
	if (flags & __PRINTF_FLAG_PREFIX) {
60
		buf[0] = '0';
61
		buf[1] = 'x';
62
		bptr += 2;
63
	}
64
 
974 cejka 65
	for (i = width*8 - 4; i >= 0; i -= 4)
66
		*bptr++ = digits[(num>>i) & 0xf];
67
	*bptr = '\0';
68
 
69
	return putstr(buf);	
70
}
71
 
72
 
73
/** Print number in given base
74
 *
75
 * Print significant digits of a number in given
76
 * base.
77
 *
78
 * @param num  Number to print.
79
 * @param base Base to print the number in (should
80
 *             be in range 2 .. 16).
81
 *
82
 */
995 cejka 83
static int print_number(const unsigned long num, const unsigned int base, uint64_t flags)
974 cejka 84
{
85
	int val = num;
995 cejka 86
	char d[sizeof(unsigned long)*8+1];	/* this is good enough even for base == 2 */
87
	int i = sizeof(unsigned long)*8-1;
974 cejka 88
 
995 cejka 89
	/* FIXME: if signed, print sign */
90
 
974 cejka 91
	do {
92
		d[i--] = digits[val % base];
93
	} while (val /= base);
94
 
995 cejka 95
	d[sizeof(unsigned long)*8] = 0;	
974 cejka 96
 
97
	return putstr(&d[i + 1]);
98
}
99
 
100
 
101
 
102
/** General formatted text print
103
 *
104
 * Print text formatted according the fmt parameter
105
 * and variant arguments. Each formatting directive
106
 * begins with \% (percentage) character and one of the
107
 * following character:
108
 *
109
 * \%    Prints the percentage character.
110
 *
111
 * s    The next variant argument is treated as char*
112
 *      and printed as a NULL terminated string.
113
 *
114
 * c    The next variant argument is treated as a single char.
115
 *
116
 * p    The next variant argument is treated as a maximum
117
 *      bit-width integer with respect to architecture
118
 *      and printed in full hexadecimal width.
119
 *
120
 * P    As with 'p', but '0x' is prefixed.
121
 *
122
 * q    The next variant argument is treated as a 64b integer
123
 *      and printed in full hexadecimal width.
124
 *
125
 * Q    As with 'q', but '0x' is prefixed.
126
 *
127
 * l    The next variant argument is treated as a 32b integer
128
 *      and printed in full hexadecimal width.
129
 *
130
 * L    As with 'l', but '0x' is prefixed.
131
 *
132
 * w    The next variant argument is treated as a 16b integer
133
 *      and printed in full hexadecimal width.
134
 *
135
 * W    As with 'w', but '0x' is prefixed.
136
 *
137
 * b    The next variant argument is treated as a 8b integer
138
 *      and printed in full hexadecimal width.
139
 *
140
 * B    As with 'b', but '0x' is prefixed.
141
 *
142
 * d    The next variant argument is treated as integer
143
 *      and printed in standard decimal format (only significant
144
 *      digits).
145
 *
146
 * x    The next variant argument is treated as integer
147
 *      and printed in standard hexadecimal format (only significant
148
 *      digits).
149
 *
150
 * X    As with 'x', but '0x' is prefixed.
151
 *
152
 * All other characters from fmt except the formatting directives
153
 * are printed in verbatim.
154
 *
155
 * @param fmt Formatting NULL terminated string.
156
 */
157
int printf(const char *fmt, ...)
158
{
159
	int i = 0, j = 0;
160
	int counter, retval;
161
	va_list ap;
162
	char c;	
995 cejka 163
 
164
	uint64_t flags;
974 cejka 165
 
166
	counter = 0;
167
	va_start(ap, fmt);
168
 
169
	while ((c = fmt[i])) {
992 jermar 170
		/* control character */
974 cejka 171
		if (c == '%' ) { 
992 jermar 172
			/* print common characters if any processed */	
974 cejka 173
			if (i > j) {
995 cejka 174
				if ((retval = putnchars(&fmt[j], (size_t)(i - j))) == EOF) { /* error */
974 cejka 175
					return -counter;
176
				}
177
				counter += retval;
178
			}
995 cejka 179
 
974 cejka 180
			j = ++i;
995 cejka 181
 
182
			/* parse modifiers */
183
			flags = 0;
184
			/*switch (c = fmt[i]) {
185
				case '-':	
186
			}	
187
			*/
974 cejka 188
			switch (c = fmt[i]) {
189
 
190
				/* percentile itself */
191
				case '%': 
192
					--j;	/* soon will be incremented back */
193
					break;
194
 
195
				/*
196
				* String and character conversions.
197
				*/
198
				case 's':
199
					if ((retval = putstr(va_arg(ap, char*))) == EOF) {
200
						return -counter;
201
					};
202
 
203
					counter += retval;
204
					break;
205
				case 'c':
995 cejka 206
					if ((retval = putnchars((char *)&va_arg(ap, unsigned long), sizeof(char))) == EOF) {
974 cejka 207
						return -counter;
208
					};
209
 
210
					counter += retval;
211
					break;
212
 
213
				/*
214
				* Hexadecimal conversions with fixed width.
215
				*/
995 cejka 216
				case 'P':
217
				       	flags |= __PRINTF_FLAG_PREFIX;
974 cejka 218
				case 'p':
995 cejka 219
	    				if ((retval = print_fixed_hex(va_arg(ap, unsigned long), sizeof(unsigned long), flags)) == EOF ) {
974 cejka 220
						return -counter;
221
					};
222
 
223
					counter += retval;
224
					break;
225
				case 'Q':
995 cejka 226
				       	flags |= __PRINTF_FLAG_PREFIX;
974 cejka 227
				case 'q':
995 cejka 228
	    				if ((retval = print_fixed_hex(va_arg(ap, uint64_t), sizeof(uint64_t), flags)) == EOF ) {
974 cejka 229
						return -counter;
230
					};
231
 
232
					counter += retval;
233
					break;
234
				case 'L': 
995 cejka 235
				       	flags |= __PRINTF_FLAG_PREFIX;
974 cejka 236
				case 'l':
995 cejka 237
	    				if ((retval = print_fixed_hex(va_arg(ap, unsigned long), sizeof(uint32_t), flags)) == EOF ) {
974 cejka 238
						return -counter;
239
					};
240
 
241
					counter += retval;
242
			   		break; 
243
				case 'W':
995 cejka 244
				       	flags |= __PRINTF_FLAG_PREFIX;
974 cejka 245
				case 'w':
995 cejka 246
	    				if ((retval = print_fixed_hex(va_arg(ap, unsigned long), sizeof(uint16_t), flags)) == EOF ) {
974 cejka 247
						return -counter;
248
					};
249
 
250
					counter += retval;
251
					break;
252
				case 'B':
995 cejka 253
				       	flags |= __PRINTF_FLAG_PREFIX;
974 cejka 254
				case 'b':
995 cejka 255
	    				if ((retval = print_fixed_hex(va_arg(ap, unsigned long), sizeof(uint8_t), flags)) == EOF ) {
974 cejka 256
						return -counter;
257
					};
258
 
259
					counter += retval;
260
					break;
261
				/*
262
				* Decimal and hexadecimal conversions.
263
				*/
264
				case 'd':
995 cejka 265
				case 'i':
266
				       	flags |= __PRINTF_FLAG_SIGNED;
267
	    				if ((retval = print_number(va_arg(ap,unsigned long), 10, flags)) == EOF ) {
974 cejka 268
						return -counter;
269
					};
270
 
271
					counter += retval;
272
			   		break; 
273
				case 'X':
995 cejka 274
				       	flags |= __PRINTF_FLAG_PREFIX;
974 cejka 275
				case 'x':
995 cejka 276
	    				if ((retval = print_number(va_arg(ap, unsigned long), 16, flags)) == EOF ) {
974 cejka 277
						return -counter;
278
					};
279
 
280
					counter += retval;
281
			   		break; 
282
				/*
283
				* Bad formatting.
284
				*/
285
				default:
286
					return -counter;
287
			}
288
			++j;
289
		}	
290
		++i;
291
	}
292
 
975 cejka 293
	if (i > j) {
995 cejka 294
		if ((retval = putnchars(&fmt[j], (size_t)(i - j))) == EOF) { /* error */
975 cejka 295
			return -counter;
296
		}
297
		counter += retval;
298
	}
299
 
974 cejka 300
	va_end(ap);
301
	return counter;
302
}
303