printf.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2013, 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include "py/mpconfig.h"
  27. #include <stdint.h>
  28. #include <string.h>
  29. #include <stdarg.h>
  30. #include "py/obj.h"
  31. #include "py/mphal.h"
  32. #if MICROPY_PY_BUILTINS_FLOAT
  33. #include "py/formatfloat.h"
  34. #endif
  35. #if MICROPY_DEBUG_PRINTERS
  36. int DEBUG_printf(const char *fmt, ...) {
  37. va_list ap;
  38. va_start(ap, fmt);
  39. #ifndef MICROPY_DEBUG_PRINTER_DEST
  40. #define MICROPY_DEBUG_PRINTER_DEST mp_plat_print
  41. #endif
  42. extern const mp_print_t MICROPY_DEBUG_PRINTER_DEST;
  43. int ret = mp_vprintf(&MICROPY_DEBUG_PRINTER_DEST, fmt, ap);
  44. va_end(ap);
  45. return ret;
  46. }
  47. #endif
  48. #if MICROPY_USE_INTERNAL_PRINTF
  49. #undef putchar // Some stdlibs have a #define for putchar
  50. int printf(const char *fmt, ...);
  51. int vprintf(const char *fmt, va_list ap);
  52. int putchar(int c);
  53. int puts(const char *s);
  54. int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
  55. int snprintf(char *str, size_t size, const char *fmt, ...);
  56. int printf(const char *fmt, ...) {
  57. va_list ap;
  58. va_start(ap, fmt);
  59. int ret = mp_vprintf(&mp_plat_print, fmt, ap);
  60. va_end(ap);
  61. return ret;
  62. }
  63. int vprintf(const char *fmt, va_list ap) {
  64. return mp_vprintf(&mp_plat_print, fmt, ap);
  65. }
  66. // need this because gcc optimises printf("%c", c) -> putchar(c), and printf("a") -> putchar('a')
  67. int putchar(int c) {
  68. char chr = c;
  69. mp_hal_stdout_tx_strn_cooked(&chr, 1);
  70. return chr;
  71. }
  72. // need this because gcc optimises printf("string\n") -> puts("string")
  73. int puts(const char *s) {
  74. mp_hal_stdout_tx_strn_cooked(s, strlen(s));
  75. char chr = '\n';
  76. mp_hal_stdout_tx_strn_cooked(&chr, 1);
  77. return 1;
  78. }
  79. typedef struct _strn_print_env_t {
  80. char *cur;
  81. size_t remain;
  82. } strn_print_env_t;
  83. STATIC void strn_print_strn(void *data, const char *str, size_t len) {
  84. strn_print_env_t *strn_print_env = data;
  85. if (len > strn_print_env->remain) {
  86. len = strn_print_env->remain;
  87. }
  88. memcpy(strn_print_env->cur, str, len);
  89. strn_print_env->cur += len;
  90. strn_print_env->remain -= len;
  91. }
  92. #if defined(__GNUC__) && !defined(__clang__)
  93. // uClibc requires this alias to be defined, or there may be link errors
  94. // when linkings against it statically.
  95. int __GI_vsnprintf(char *str, size_t size, const char *fmt, va_list ap) __attribute__((weak, alias ("vsnprintf")));
  96. #endif
  97. int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) {
  98. strn_print_env_t strn_print_env = {str, size};
  99. mp_print_t print = {&strn_print_env, strn_print_strn};
  100. int len = mp_vprintf(&print, fmt, ap);
  101. // add terminating null byte
  102. if (size > 0) {
  103. if (strn_print_env.remain == 0) {
  104. strn_print_env.cur[-1] = 0;
  105. } else {
  106. strn_print_env.cur[0] = 0;
  107. }
  108. }
  109. return len;
  110. }
  111. int snprintf(char *str, size_t size, const char *fmt, ...) {
  112. va_list ap;
  113. va_start(ap, fmt);
  114. int ret = vsnprintf(str, size, fmt, ap);
  115. va_end(ap);
  116. return ret;
  117. }
  118. #endif //MICROPY_USE_INTERNAL_PRINTF