printf.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright(c) 2007-2022 Jianjun Jiang <8192542@qq.com>
  3. * Official site: http://xboot.org
  4. * Mobile phone: +86-18665388956
  5. * QQ: 8192542
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in all
  15. * copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23. * SOFTWARE.
  24. *
  25. */
  26. // (C) 2024 Victor Suarez Rovere <suarezvictor@gmail.com>
  27. // SPDX-License-Identifier: AGPL-3.0-only
  28. #include <stdarg.h>
  29. #include <stdint.h>
  30. #include "driver_uart.h"
  31. #define printf_putc(c) driver_uart_putc(UART_COMM, c)
  32. static int vpf_str_to_num(const char * fmt, int * num)
  33. {
  34. const char * p;
  35. int res, d, isd;
  36. res = 0;
  37. for(p = fmt; *fmt != '\0'; p++)
  38. {
  39. isd = (*p >= '0' && *p <= '9');
  40. if(!isd)
  41. break;
  42. d = *p - '0';
  43. res *= 10;
  44. res += d;
  45. }
  46. *num = res;
  47. return ((int)(p - fmt));
  48. }
  49. static void vpf_num_to_str(uint32_t a, int ish, int pl, int pc)
  50. {
  51. char buf[32];
  52. uint32_t base;
  53. int idx, i, t;
  54. for(i = 0; i < sizeof(buf); i++)
  55. buf[i] = pc;
  56. base = 10;
  57. if(ish)
  58. base = 16;
  59. idx = 0;
  60. do {
  61. t = a % base;
  62. if(t >= 10)
  63. buf[idx] = t - 10 + 'a';
  64. else
  65. buf[idx] = t + '0';
  66. a /= base;
  67. idx++;
  68. } while (a > 0);
  69. if(pl > 0)
  70. {
  71. if(pl >= sizeof(buf))
  72. pl = sizeof(buf) - 1;
  73. if(idx < pl)
  74. idx = pl;
  75. }
  76. buf[idx] = '\0';
  77. for(i = idx - 1; i >= 0; i--)
  78. printf_putc(buf[i]);
  79. }
  80. static int vpf(const char * fmt, va_list va)
  81. {
  82. const char * p, * q;
  83. int f, c, vai, pl, pc, i;
  84. unsigned char t;
  85. pc = ' ';
  86. for(p = fmt; *p != '\0'; p++)
  87. {
  88. f = 0;
  89. pl = 0;
  90. c = *p;
  91. q = p;
  92. if(*p == '%')
  93. {
  94. q = p;
  95. p++;
  96. if(*p >= '0' && *p <= '9')
  97. p += vpf_str_to_num(p, &pl);
  98. f = *p;
  99. }
  100. if((f == 'd') || (f == 'x'))
  101. {
  102. vai = va_arg(va, int);
  103. vpf_num_to_str(vai, f == 'x', pl, pc);
  104. }
  105. else
  106. {
  107. for(i = 0; i < (p - q); i++)
  108. printf_putc(q[i]);
  109. t = (unsigned char)(f != 0 ? f : c);
  110. printf_putc(t);
  111. }
  112. }
  113. return 0;
  114. }
  115. int printf(const char * fmt, ...)
  116. {
  117. va_list va;
  118. va_start(va, fmt);
  119. vpf(fmt, va);
  120. va_end(va);
  121. return 0;
  122. }