1 /*
2  * SPDX-License-Identifier: MIT
3  * SPDX-FileCopyrightText: Copyright Marco Paland (info@paland.com), PALANDesign Hannover, Germany
4  * SPDX-FileCopyrightText: Copyright TF-RMM Contributors.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * n the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  *
24  * \brief Tiny printf, sprintf and snprintf implementation, optimized for
25  * speed on
26  *        embedded systems with a very limited resources.
27  *        Use this instead of bloated standard/newlib printf.
28  *        These routines are thread safe and reentrant.
29  */
30 
31 #ifndef STDIO_H
32 #define STDIO_H
33 
34 #include <stdarg.h>
35 #include <stddef.h>
36 
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
42 
43 /**
44  * Output a character to a custom device like UART, used by the printf() function
45  * This function is declared here only. You have to write your custom implementation somewhere
46  * \param character: Character to output
47  */
48 void _putchar(char character);
49 
50 
51 /**
52  * Tiny printf implementation
53  * You have to implement _putchar if you use printf()
54  * To avoid conflicts with the regular printf() API it is overridden by macro defines
55  * and internal underscore-appended functions like printf_() are used
56  * \param format A string that specifies the format of the output
57  * \return The number of characters that are written into the array, not counting the terminating null character
58  */
59 #define printf printf_
60 int printf_(const char *format, ...);
61 
62 
63 /**
64  * Tiny sprintf implementation
65  * Due to security reasons (buffer overflow) YOU SHOULD CONSIDER USING (V)SNPRINTF INSTEAD!
66  * \param buffer A pointer to the buffer where to store the formatted string. MUST be big enough to store the output!
67  * \param format A string that specifies the format of the output
68  * \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character
69  */
70 #define sprintf sprintf_
71 int sprintf_(char *buffer, const char *format, ...);
72 
73 
74 /**
75  * Tiny snprintf/vsnprintf implementation
76  * \param buffer A pointer to the buffer where to store the formatted string
77  * \param count The maximum number of characters to store in the buffer, including a terminating null character
78  * \param format A string that specifies the format of the output
79  * \param va A value identifying a variable arguments list
80  * \return The number of characters that COULD have been written into the buffer, not counting the terminating
81  *         null character. A value equal or larger than count indicates truncation. Only when the returned value
82  *         is non-negative and less than count, the string has been completely written.
83  */
84 #define snprintf  snprintf_
85 #define vsnprintf vsnprintf_
86 int  snprintf_(char *buffer, size_t count, const char *format, ...);
87 int vsnprintf_(char *buffer, size_t count, const char *format, va_list va);
88 
89 
90 /**
91  * Tiny vprintf implementation
92  * \param format A string that specifies the format of the output
93  * \param va A value identifying a variable arguments list
94  * \return The number of characters that are WRITTEN into the buffer, not counting the terminating null character
95  */
96 #define vprintf vprintf_
97 int vprintf_(const char *format, va_list va);
98 
99 
100 /**
101  * printf with output function
102  * You may use this as dynamic alternative to printf() with its fixed _putchar() output
103  * \param out An output function which takes one character and an argument pointer
104  * \param arg An argument pointer for user data passed to output function
105  * \param format A string that specifies the format of the output
106  * \return The number of characters that are sent to the output function, not counting the terminating null character
107  */
108 int fctprintf(void (*out)(char character, void *arg), void *arg,
109 	      const char *format, ...);
110 
111 
112 #ifdef __cplusplus
113 }
114 #endif
115 
116 
117 #endif  /* STDIO_H */
118