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 
27 #include "py/mpconfig.h"
28 
29 #include <stdint.h>
30 #include <string.h>
31 #include <stdarg.h>
32 
33 #include "py/obj.h"
34 #include "py/mphal.h"
35 
36 #if MICROPY_PY_BUILTINS_FLOAT
37 #include "py/formatfloat.h"
38 #endif
39 
40 #if MICROPY_DEBUG_PRINTERS
DEBUG_printf(const char * fmt,...)41 int DEBUG_printf(const char *fmt, ...) {
42     va_list ap;
43     va_start(ap, fmt);
44     int ret = mp_vprintf(MICROPY_DEBUG_PRINTER, fmt, ap);
45     va_end(ap);
46     return ret;
47 }
48 #endif
49 
50 #if MICROPY_USE_INTERNAL_PRINTF
51 
52 #undef putchar  // Some stdlibs have a #define for putchar
53 int printf(const char *fmt, ...);
54 int vprintf(const char *fmt, va_list ap);
55 int putchar(int c);
56 int puts(const char *s);
57 int vsnprintf(char *str, size_t size, const char *fmt, va_list ap);
58 int snprintf(char *str, size_t size, const char *fmt, ...);
59 
printf(const char * fmt,...)60 int printf(const char *fmt, ...) {
61     va_list ap;
62     va_start(ap, fmt);
63     int ret = mp_vprintf(&mp_plat_print, fmt, ap);
64     va_end(ap);
65     return ret;
66 }
67 
vprintf(const char * fmt,va_list ap)68 int vprintf(const char *fmt, va_list ap) {
69     return mp_vprintf(&mp_plat_print, fmt, ap);
70 }
71 
72 // need this because gcc optimises printf("%c", c) -> putchar(c), and printf("a") -> putchar('a')
putchar(int c)73 int putchar(int c) {
74     char chr = c;
75     mp_hal_stdout_tx_strn_cooked(&chr, 1);
76     return chr;
77 }
78 
79 // need this because gcc optimises printf("string\n") -> puts("string")
puts(const char * s)80 int puts(const char *s) {
81     mp_hal_stdout_tx_strn_cooked(s, strlen(s));
82     char chr = '\n';
83     mp_hal_stdout_tx_strn_cooked(&chr, 1);
84     return 1;
85 }
86 
87 typedef struct _strn_print_env_t {
88     char *cur;
89     size_t remain;
90 } strn_print_env_t;
91 
strn_print_strn(void * data,const char * str,size_t len)92 STATIC void strn_print_strn(void *data, const char *str, size_t len) {
93     strn_print_env_t *strn_print_env = data;
94     if (len > strn_print_env->remain) {
95         len = strn_print_env->remain;
96     }
97     memcpy(strn_print_env->cur, str, len);
98     strn_print_env->cur += len;
99     strn_print_env->remain -= len;
100 }
101 
102 #if defined(__GNUC__) && !defined(__clang__) && __GNUC__ < 9
103 // uClibc requires this alias to be defined, or there may be link errors
104 // when linkings against it statically.
105 // GCC 9 gives a warning about missing attributes so it's excluded until
106 // uClibc+GCC9 support is needed.
107 int __GI_vsnprintf(char *str, size_t size, const char *fmt, va_list ap) __attribute__((weak, alias("vsnprintf")));
108 #endif
109 
vsnprintf(char * str,size_t size,const char * fmt,va_list ap)110 int vsnprintf(char *str, size_t size, const char *fmt, va_list ap) {
111     strn_print_env_t strn_print_env = {str, size};
112     mp_print_t print = {&strn_print_env, strn_print_strn};
113     int len = mp_vprintf(&print, fmt, ap);
114     // add terminating null byte
115     if (size > 0) {
116         if (strn_print_env.remain == 0) {
117             strn_print_env.cur[-1] = 0;
118         } else {
119             strn_print_env.cur[0] = 0;
120         }
121     }
122     return len;
123 }
124 
snprintf(char * str,size_t size,const char * fmt,...)125 int snprintf(char *str, size_t size, const char *fmt, ...) {
126     va_list ap;
127     va_start(ap, fmt);
128     int ret = vsnprintf(str, size, fmt, ap);
129     va_end(ap);
130     return ret;
131 }
132 
133 #endif // MICROPY_USE_INTERNAL_PRINTF
134