source: mainline/generic/src/debug/print.c@ 51a7dc1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 51a7dc1 was 040e4e9, checked in by Jakub Jermar <jakub@…>, 20 years ago

Improve doxygen comments for printf().

  • Property mode set to 100644
File size: 16.7 KB
Line 
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 <putchar.h>
31#include <print.h>
32#include <synch/spinlock.h>
33#include <arch/arg.h>
34#include <arch/asm.h>
35
36#include <arch.h>
37
38SPINLOCK_INITIALIZE(printflock); /**< printf spinlock */
39
40#define __PRINTF_FLAG_PREFIX 0x00000001 /* show prefixes 0x or 0 */
41#define __PRINTF_FLAG_SIGNED 0x00000002 /* signed / unsigned number */
42#define __PRINTF_FLAG_ZEROPADDED 0x00000004 /* print leading zeroes */
43#define __PRINTF_FLAG_LEFTALIGNED 0x00000010 /* align to left */
44#define __PRINTF_FLAG_SHOWPLUS 0x00000020 /* always show + sign */
45#define __PRINTF_FLAG_SPACESIGN 0x00000040 /* print space instead of plus */
46#define __PRINTF_FLAG_BIGCHARS 0x00000080 /* show big characters */
47#define __PRINTF_FLAG_NEGATIVE 0x00000100 /* number has - sign */
48
49#define PRINT_NUMBER_BUFFER_SIZE (64+5) /* Buffer big enought for 64 bit number
50 * printed in base 2, sign, prefix and
51 * 0 to terminate string.. (last one is only for better testing
52 * end of buffer by zero-filling subroutine)
53 */
54typedef enum {
55 PrintfQualifierByte = 0,
56 PrintfQualifierShort,
57 PrintfQualifierInt,
58 PrintfQualifierLong,
59 PrintfQualifierLongLong,
60 PrintfQualifierNative,
61 PrintfQualifierPointer
62} qualifier_t;
63
64static char digits_small[] = "0123456789abcdef"; /* Small hexadecimal characters */
65static char digits_big[] = "0123456789ABCDEF"; /* Big hexadecimal characters */
66
67static inline int isdigit(int c)
68{
69 return ((c >= '0' )&&( c <= '9'));
70}
71
72static __native strlen(const char *str)
73{
74 __native counter = 0;
75
76 while (str[counter] != 0) {
77 counter++;
78 }
79
80 return counter;
81}
82
83/** Print one string without appending newline to the end.
84 *
85 * Do not use this function directly - printflock is not locked here.
86 *
87 */
88static int putstr(const char *str)
89{
90 int count;
91 if (str == NULL) {
92 str = "(NULL)";
93 }
94
95 for (count = 0; str[count] != 0; count++) {
96 putchar(str[count]);
97 }
98 return count;
99}
100
101/** Print count characters from buffer to output.
102 *
103 * @param buffer Address of the buffer with charaters to be printed.
104 * @param count Number of characters to be printed.
105 *
106 * @return Number of characters printed.
107 */
108static int putnchars(const char *buffer, __native count)
109{
110 int i;
111 if (buffer == NULL) {
112 buffer = "(NULL)";
113 count = 6;
114 }
115
116 for (i = 0; i < count; i++) {
117 putchar(buffer[i]);
118 }
119
120 return count;
121}
122
123/** Print one formatted character
124 *
125 * @param c Character to print.
126 * @param width
127 * @param flags
128 * @return Number of printed characters or EOF.
129 */
130static int print_char(char c, int width, __u64 flags)
131{
132 int counter = 0;
133
134 if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
135 while (--width > 0) { /* one space is consumed by character itself hence the predecrement */
136 /* FIXME: painfully slow */
137 putchar(' ');
138 ++counter;
139 }
140 }
141
142 putchar(c);
143 ++counter;
144
145 while (--width > 0) { /* one space is consumed by character itself hence the predecrement */
146 putchar(' ');
147 ++counter;
148 }
149
150 return counter;
151}
152
153/** Print one string
154 * @param s string
155 * @param width
156 * @param precision
157 * @param flags
158 * @return number of printed characters or EOF
159 */
160static int print_string(char *s, int width, int precision, __u64 flags)
161{
162 int counter = 0;
163 __native size;
164
165 if (s == NULL) {
166 return putstr("(NULL)");
167 }
168
169 size = strlen(s);
170
171 /* print leading spaces */
172
173 if (precision == 0)
174 precision = size;
175
176 width -= precision;
177
178 if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
179 while (width-- > 0) {
180 putchar(' ');
181 counter++;
182 }
183 }
184
185 while (precision > size) {
186 precision--;
187 putchar(' ');
188 ++counter;
189 }
190
191 if (putnchars(s, precision) == EOF) {
192 return EOF;
193 }
194
195 counter += precision;
196
197 while (width-- > 0) {
198 putchar(' ');
199 ++counter;
200 }
201
202 return ++counter;
203}
204
205
206/** Print number in given base
207 *
208 * Print significant digits of a number in given
209 * base.
210 *
211 * @param num Number to print.
212 * @param width
213 * @param precision
214 * @param base Base to print the number in (should
215 * be in range 2 .. 16).
216 * @param flags output modifiers
217 * @return number of written characters or EOF.
218 */
219static int print_number(__u64 num, int width, int precision, int base , __u64 flags)
220{
221 char *digits = digits_small;
222 char d[PRINT_NUMBER_BUFFER_SIZE]; /* this is good enough even for base == 2, prefix and sign */
223 char *ptr = &d[PRINT_NUMBER_BUFFER_SIZE - 1];
224 int size = 0;
225 int number_size; /* size of plain number */
226 int written = 0;
227 char sgn;
228
229 if (flags & __PRINTF_FLAG_BIGCHARS)
230 digits = digits_big;
231
232 *ptr-- = 0; /* Put zero at end of string */
233
234 if (num == 0) {
235 *ptr-- = '0';
236 size++;
237 } else {
238 do {
239 *ptr-- = digits[num % base];
240 size++;
241 } while (num /= base);
242 }
243
244 number_size = size;
245
246 /* Collect sum of all prefixes/signs/... to calculate padding and leading zeroes */
247 if (flags & __PRINTF_FLAG_PREFIX) {
248 switch(base) {
249 case 2: /* Binary formating is not standard, but usefull */
250 size += 2;
251 break;
252 case 8:
253 size++;
254 break;
255 case 16:
256 size += 2;
257 break;
258 }
259 }
260
261 sgn = 0;
262 if (flags & __PRINTF_FLAG_SIGNED) {
263 if (flags & __PRINTF_FLAG_NEGATIVE) {
264 sgn = '-';
265 size++;
266 } else if (flags & __PRINTF_FLAG_SHOWPLUS) {
267 sgn = '+';
268 size++;
269 } else if (flags & __PRINTF_FLAG_SPACESIGN) {
270 sgn = ' ';
271 size++;
272 }
273 }
274
275 if (flags & __PRINTF_FLAG_LEFTALIGNED) {
276 flags &= ~__PRINTF_FLAG_ZEROPADDED;
277 }
278
279 /* if number is leftaligned or precision is specified then zeropadding is ignored */
280 if (flags & __PRINTF_FLAG_ZEROPADDED) {
281 if ((precision == 0) && (width > size)) {
282 precision = width - size + number_size;
283 }
284 }
285
286 /* print leading spaces */
287 if (number_size > precision) /* We must print whole number not only a part */
288 precision = number_size;
289
290 width -= precision + size - number_size;
291
292 if (!(flags & __PRINTF_FLAG_LEFTALIGNED)) {
293 while (width-- > 0) {
294 putchar(' ');
295 written++;
296 }
297 }
298
299 /* print sign */
300 if (sgn) {
301 putchar(sgn);
302 written++;
303 }
304
305 /* print prefix */
306
307 if (flags & __PRINTF_FLAG_PREFIX) {
308 switch(base) {
309 case 2: /* Binary formating is not standard, but usefull */
310 putchar('0');
311 if (flags & __PRINTF_FLAG_BIGCHARS) {
312 putchar('B');
313 } else {
314 putchar('b');
315 }
316 written += 2;
317 break;
318 case 8:
319 putchar('o');
320 written++;
321 break;
322 case 16:
323 putchar('0');
324 if (flags & __PRINTF_FLAG_BIGCHARS) {
325 putchar('X');
326 } else {
327 putchar('x');
328 }
329 written += 2;
330 break;
331 }
332 }
333
334 /* print leading zeroes */
335 precision -= number_size;
336 while (precision-- > 0) {
337 putchar('0');
338 written++;
339 }
340
341
342 /* print number itself */
343
344 written += putstr(++ptr);
345
346 /* print ending spaces */
347
348 while (width-- > 0) {
349 putchar(' ');
350 written++;
351 }
352
353 return written;
354}
355
356/** Print formatted string.
357 *
358 * Print string formatted according to the fmt parameter
359 * and variadic arguments. Each formatting directive
360 * must have the following form:
361 *
362 * \% [ FLAGS ] [ WIDTH ] [ .PRECISION ] [ TYPE ] CONVERSION
363 *
364 * FLAGS:@n
365 * "#" Force to print prefix.
366 * For conversion \%o the prefix is 0, for %x and \%X prefixes are 0x and 0X
367 * and for conversion \%b the prefix is 0b.
368 *
369 * "-" Align to left.
370 *
371 * "+" Print positive sign just as negative.
372 *
373 * " " If the printed number is positive and "+" flag is not set, print space in
374 * place of sign.
375 *
376 * "0" Print 0 as padding instead of spaces. Zeroes are placed between sign and the
377 * rest of the number. This flag is ignored if "-" flag is specified.
378 *
379 * WIDTH:@n
380 * Specify minimal width of printed argument. If it is bigger, width is ignored.
381 * If width is specified with a "*" character instead of number, width is taken from
382 * parameter list. And integer parameter is expected before parameter for processed
383 * conversion specification. If this value is negative its absolute value is taken
384 * and the "-" flag is set.
385 *
386 * PRECISION:@n
387 * Value precision. For numbers it specifies minimum valid numbers.
388 * Smaller numbers are printed with leading zeroes. Bigger numbers are not affected.
389 * Strings with more than precision characters are cut off.
390 * Just as with width, an "*" can be used used instead of a number.
391 * An integer value is then expected in parameters. When both width and precision
392 * are specified using "*", the first parameter is used for width and the second one
393 * for precision.
394 *
395 * TYPE:@n
396 * "hh" Signed or unsigned char.@n
397 * "h" Signed or usigned short.@n
398 * "" Signed or usigned int (default value).@n
399 * "l" Signed or usigned long int.@n
400 * "ll" Signed or usigned long long int.@n
401 * "z" __native (non-standard extension).@n
402 *
403 *
404 * CONVERSION:@n
405 * % Print percentile character itself.
406 *
407 * c Print single character.
408 *
409 * s Print zero terminated string. If a NULL value is passed as value, "(NULL)" is printed instead.
410 *
411 * P, p Print value of a pointer. Void * value is expected and it is printed in hexadecimal notation with prefix
412 * (as with \%#X or \%#x for 32bit or \%#X / \%#x for 64bit long pointers).
413 *
414 * b Print value as unsigned binary number. Prefix is not printed by default. (Nonstandard extension.)
415 *
416 * o Print value as unsigned octal number. Prefix is not printed by default.
417 *
418 * d,i Print signed decimal number. There is no difference between d and i conversion.
419 *
420 * u Print unsigned decimal number.
421 *
422 * X, x Print hexadecimal number with upper- or lower-case. Prefix is not printed by default.
423 *
424 * All other characters from fmt except the formatting directives
425 * are printed in verbatim.
426 *
427 * @param fmt Formatting NULL terminated string.
428 * @return Number of printed characters or negative value on failure.
429 */
430int printf(const char *fmt, ...)
431{
432 int irqpri;
433 int i = 0, j = 0; /* i is index of currently processed char from fmt, j is index to the first not printed nonformating character */
434 int end;
435 int counter; /* counter of printed characters */
436 int retval; /* used to store return values from called functions */
437 va_list ap;
438 char c;
439 qualifier_t qualifier; /* type of argument */
440 int base; /* base in which will be parameter (numbers only) printed */
441 __u64 number; /* argument value */
442 __native size; /* byte size of integer parameter */
443 int width, precision;
444 __u64 flags;
445
446 counter = 0;
447
448 va_start(ap, fmt);
449
450 irqpri = interrupts_disable();
451 spinlock_lock(&printflock);
452
453
454 while ((c = fmt[i])) {
455 /* control character */
456 if (c == '%' ) {
457 /* print common characters if any processed */
458 if (i > j) {
459 if ((retval = putnchars(&fmt[j], (__native)(i - j))) == EOF) { /* error */
460 counter = -counter;
461 goto out;
462 }
463 counter += retval;
464 }
465
466 j = i;
467 /* parse modifiers */
468 flags = 0;
469 end = 0;
470
471 do {
472 ++i;
473 switch (c = fmt[i]) {
474 case '#': flags |= __PRINTF_FLAG_PREFIX; break;
475 case '-': flags |= __PRINTF_FLAG_LEFTALIGNED; break;
476 case '+': flags |= __PRINTF_FLAG_SHOWPLUS; break;
477 case ' ': flags |= __PRINTF_FLAG_SPACESIGN; break;
478 case '0': flags |= __PRINTF_FLAG_ZEROPADDED; break;
479 default: end = 1;
480 };
481
482 } while (end == 0);
483
484 /* width & '*' operator */
485 width = 0;
486 if (isdigit(fmt[i])) {
487 while (isdigit(fmt[i])) {
488 width *= 10;
489 width += fmt[i++] - '0';
490 }
491 } else if (fmt[i] == '*') {
492 /* get width value from argument list*/
493 i++;
494 width = (int)va_arg(ap, int);
495 if (width < 0) {
496 /* negative width means to set '-' flag */
497 width *= -1;
498 flags |= __PRINTF_FLAG_LEFTALIGNED;
499 }
500 }
501
502 /* precision and '*' operator */
503 precision = 0;
504 if (fmt[i] == '.') {
505 ++i;
506 if (isdigit(fmt[i])) {
507 while (isdigit(fmt[i])) {
508 precision *= 10;
509 precision += fmt[i++] - '0';
510 }
511 } else if (fmt[i] == '*') {
512 /* get precision value from argument list*/
513 i++;
514 precision = (int)va_arg(ap, int);
515 if (precision < 0) {
516 /* negative precision means to ignore it */
517 precision = 0;
518 }
519 }
520 }
521
522 switch (fmt[i++]) {
523 /** TODO: unimplemented qualifiers:
524 * t ptrdiff_t - ISO C 99
525 */
526 case 'h': /* char or short */
527 qualifier = PrintfQualifierShort;
528 if (fmt[i] == 'h') {
529 i++;
530 qualifier = PrintfQualifierByte;
531 }
532 break;
533 case 'l': /* long or long long*/
534 qualifier = PrintfQualifierLong;
535 if (fmt[i] == 'l') {
536 i++;
537 qualifier = PrintfQualifierLongLong;
538 }
539 break;
540 case 'z': /* __native */
541 qualifier = PrintfQualifierNative;
542 break;
543 default:
544 qualifier = PrintfQualifierInt; /* default type */
545 --i;
546 }
547
548 base = 10;
549
550 switch (c = fmt[i]) {
551
552 /*
553 * String and character conversions.
554 */
555 case 's':
556 if ((retval = print_string(va_arg(ap, char*), width, precision, flags)) == EOF) {
557 counter = -counter;
558 goto out;
559 };
560
561 counter += retval;
562 j = i + 1;
563 goto next_char;
564 case 'c':
565 c = va_arg(ap, unsigned int);
566 if ((retval = print_char(c, width, flags )) == EOF) {
567 counter = -counter;
568 goto out;
569 };
570
571 counter += retval;
572 j = i + 1;
573 goto next_char;
574
575 /*
576 * Integer values
577 */
578 case 'P': /* pointer */
579 flags |= __PRINTF_FLAG_BIGCHARS;
580 case 'p':
581 flags |= __PRINTF_FLAG_PREFIX;
582 base = 16;
583 qualifier = PrintfQualifierPointer;
584 break;
585 case 'b':
586 base = 2;
587 break;
588 case 'o':
589 base = 8;
590 break;
591 case 'd':
592 case 'i':
593 flags |= __PRINTF_FLAG_SIGNED;
594 case 'u':
595 break;
596 case 'X':
597 flags |= __PRINTF_FLAG_BIGCHARS;
598 case 'x':
599 base = 16;
600 break;
601 /* percentile itself */
602 case '%':
603 j = i;
604 goto next_char;
605 /*
606 * Bad formatting.
607 */
608 default:
609 /* Unknown format
610 * now, the j is index of '%' so we will
611 * print whole bad format sequence
612 */
613 goto next_char;
614 }
615
616
617 /* Print integers */
618 /* print number */
619 switch (qualifier) {
620 case PrintfQualifierByte:
621 size = sizeof(unsigned char);
622 number = (__u64)va_arg(ap, unsigned int);
623 break;
624 case PrintfQualifierShort:
625 size = sizeof(unsigned short);
626 number = (__u64)va_arg(ap, unsigned int);
627 break;
628 case PrintfQualifierInt:
629 size = sizeof(unsigned int);
630 number = (__u64)va_arg(ap, unsigned int);
631 break;
632 case PrintfQualifierLong:
633 size = sizeof(unsigned long);
634 number = (__u64)va_arg(ap, unsigned long);
635 break;
636 case PrintfQualifierLongLong:
637 size = sizeof(unsigned long long);
638 number = (__u64)va_arg(ap, unsigned long long);
639 break;
640 case PrintfQualifierPointer:
641 size = sizeof(void *);
642 number = (__u64)(unsigned long)va_arg(ap, void *);
643 break;
644 case PrintfQualifierNative:
645 size = sizeof(__native);
646 number = (__u64)va_arg(ap, __native);
647 break;
648 default: /* Unknown qualifier */
649 counter = -counter;
650 goto out;
651
652 }
653
654 if (flags & __PRINTF_FLAG_SIGNED) {
655 if (number & (0x1 << (size*8 - 1))) {
656 flags |= __PRINTF_FLAG_NEGATIVE;
657
658 if (size == sizeof(__u64)) {
659 number = -((__s64)number);
660 } else {
661 number = ~number;
662 number &= (~((0xFFFFFFFFFFFFFFFFll) << (size * 8)));
663 number++;
664 }
665 }
666 }
667
668 if ((retval = print_number(number, width, precision, base, flags)) == EOF ) {
669 counter = -counter;
670 goto out;
671 };
672
673 counter += retval;
674 j = i + 1;
675 }
676next_char:
677
678 ++i;
679 }
680
681 if (i > j) {
682 if ((retval = putnchars(&fmt[j], (__native)(i - j))) == EOF) { /* error */
683 counter = -counter;
684 goto out;
685 }
686 counter += retval;
687 }
688out:
689 spinlock_unlock(&printflock);
690 interrupts_restore(irqpri);
691
692 va_end(ap);
693 return counter;
694}
Note: See TracBrowser for help on using the repository browser.