printf.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. int ret = mp_vprintf(MICROPY_DEBUG_PRINTER, fmt, ap);
  40. va_end(ap);
  41. return ret;
  42. }
  43. #endif
  44. #if MICROPY_USE_INTERNAL_PRINTF
  45. #undef putchar // Some stdlibs have a #define for putchar
  46. int printf(const char *fmt, ...);
  47. int vprintf(const char *fmt, va_list ap);
  48. int putchar(int c);
  49. int puts(const char *s);
  50. int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
  51. int snprintf(char *str, size_t size, const char *fmt, ...);
  52. int printf(const char *fmt, ...) {
  53. va_list ap;
  54. va_start(ap, fmt);
  55. int ret = mp_vprintf(&mp_plat_print, fmt, ap);
  56. va_end(ap);
  57. return ret;
  58. }
  59. int vprintf(const char *fmt, va_list ap) {
  60. return mp_vprintf(&mp_plat_print, fmt, ap);
  61. }
  62. // need this because gcc optimises printf("%c", c) -> putchar(c), and printf("a") -> putchar('a')
  63. int putchar(int c) {
  64. char chr = c;
  65. mp_hal_stdout_tx_strn_cooked(&chr, 1);
  66. return chr;
  67. }
  68. // need this because gcc optimises printf("string\n") -> puts("string")
  69. int puts(const char *s) {
  70. mp_hal_stdout_tx_strn_cooked(s, strlen(s));
  71. char chr = '\n';
  72. mp_hal_stdout_tx_strn_cooked(&chr, 1);
  73. return 1;
  74. }
  75. typedef struct _strn_print_env_t {
  76. char *cur;
  77. size_t remain;
  78. } strn_print_env_t;
  79. STATIC void strn_print_strn(void *data, const char *str, size_t len) {
  80. strn_print_env_t *strn_print_env = data;
  81. if (len > strn_print_env->remain) {
  82. len = strn_print_env->remain;
  83. }
  84. memcpy(strn_print_env->cur, str, len);
  85. strn_print_env->cur += len;
  86. strn_print_env->remain -= len;
  87. }
  88. #if defined(__GNUC__) && !defined(__clang__)
  89. // uClibc requires this alias to be defined, or there may be link errors
  90. // when linkings against it statically.
  91. int __GI_vsnprintf(char *str, size_t size, const char *fmt, va_list ap) __attribute__((weak, alias ("vsnprintf")));
  92. #endif
  93. int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) {
  94. strn_print_env_t strn_print_env = {str, size};
  95. mp_print_t print = {&strn_print_env, strn_print_strn};
  96. int len = mp_vprintf(&print, fmt, ap);
  97. // add terminating null byte
  98. if (size > 0) {
  99. if (strn_print_env.remain == 0) {
  100. strn_print_env.cur[-1] = 0;
  101. } else {
  102. strn_print_env.cur[0] = 0;
  103. }
  104. }
  105. return len;
  106. }
  107. int snprintf(char *str, size_t size, const char *fmt, ...) {
  108. va_list ap;
  109. va_start(ap, fmt);
  110. int ret = vsnprintf(str, size, fmt, ap);
  111. va_end(ap);
  112. return ret;
  113. }
  114. #endif //MICROPY_USE_INTERNAL_PRINTF