dbg_printf.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /* $NetBSD: printf.c,v 1.12 1997/06/26 19:11:48 drochner Exp $ */
  2. /*-
  3. * Copyright (c) 1993
  4. * The Regents of the University of California. 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. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. All advertising materials mentioning features or use of this software
  15. * must display the following acknowledgement:
  16. * This product includes software developed by the University of
  17. * California, Berkeley and its contributors.
  18. * 4. Neither the name of the University nor the names of its contributors
  19. * may be used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32. * SUCH DAMAGE.
  33. *
  34. * @(#)printf.c 8.1 (Berkeley) 6/11/93
  35. */
  36. // This version uses almost no stack, and does not suffer from buffer
  37. // overflows. The downside is that it does not implement a wide range
  38. // of formatting characters.
  39. #include <stdint.h>
  40. #include <stdlib.h>
  41. #include <stdarg.h>
  42. #include <stddef.h>
  43. #include "driver/uart.h"
  44. static void kprintn (void (*)(const char), uint32_t, int, int, char);
  45. static void kdoprnt (void (*)(const char), const char *, va_list);
  46. void
  47. dbg_printf(const char *fmt, ...)
  48. {
  49. va_list ap;
  50. va_start(ap, fmt);
  51. kdoprnt(uart0_putc, fmt, ap);
  52. va_end(ap);
  53. }
  54. void
  55. dbg_vprintf(const char *fmt, va_list ap)
  56. {
  57. kdoprnt(uart0_putc, fmt, ap);
  58. }
  59. void
  60. kdoprnt(void (*put)(const char), const char *fmt, va_list ap)
  61. {
  62. register char *p;
  63. register int ch, n;
  64. unsigned long ul;
  65. int lflag, set;
  66. char zwidth;
  67. char width;
  68. for (;;) {
  69. while ((ch = *fmt++) != '%') {
  70. if (ch == '\0')
  71. return;
  72. put(ch);
  73. }
  74. lflag = 0;
  75. width = 0;
  76. zwidth = ' ';
  77. reswitch: switch (ch = *fmt++) {
  78. case '\0':
  79. /* XXX print the last format character? */
  80. return;
  81. case 'l':
  82. lflag = 1;
  83. goto reswitch;
  84. case 'c':
  85. ch = va_arg(ap, int);
  86. put(ch & 0x7f);
  87. break;
  88. case 's':
  89. p = va_arg(ap, char *);
  90. if (p == 0) {
  91. p = "<null>";
  92. }
  93. while ((ch = *p++))
  94. put(ch);
  95. break;
  96. case 'd':
  97. ul = lflag ?
  98. va_arg(ap, long) : va_arg(ap, int);
  99. if ((long)ul < 0) {
  100. put('-');
  101. ul = -(long)ul;
  102. }
  103. kprintn(put, ul, 10, width, zwidth);
  104. break;
  105. case 'o':
  106. ul = lflag ?
  107. va_arg(ap, uint32_t) : va_arg(ap, uint32_t);
  108. kprintn(put, ul, 8, width, zwidth);
  109. break;
  110. case 'u':
  111. ul = lflag ?
  112. va_arg(ap, uint32_t) : va_arg(ap, uint32_t);
  113. kprintn(put, ul, 10, width, zwidth);
  114. break;
  115. case 'p':
  116. ul = va_arg(ap, ptrdiff_t);
  117. kprintn(put, ul, 16, width, zwidth);
  118. break;
  119. case 'x':
  120. ul = lflag ?
  121. va_arg(ap, uint32_t) : va_arg(ap, uint32_t);
  122. kprintn(put, ul, 16, width, zwidth);
  123. break;
  124. default:
  125. if (ch >= '0' && ch <= '9') {
  126. if (ch == '0' && width == 0 && zwidth == ' ') {
  127. zwidth = '0';
  128. } else {
  129. width = width * 10 + ch - '0';
  130. }
  131. goto reswitch;
  132. }
  133. put('%');
  134. if (lflag)
  135. put('l');
  136. put(ch);
  137. }
  138. }
  139. va_end(ap);
  140. }
  141. static void
  142. kprintn(void (*put)(const char), uint32_t ul, int base, int width, char padchar)
  143. {
  144. /* hold a long in base 8 */
  145. char *p, buf[(sizeof(long) * 8 / 3) + 2];
  146. p = buf;
  147. do {
  148. *p++ = "0123456789abcdef"[ul % base];
  149. } while (ul /= base);
  150. while (p - buf < width--) {
  151. put(padchar);
  152. }
  153. do {
  154. put(*--p);
  155. } while (p > buf);
  156. }