blob: fe951471426d77cf19a86b370b67568bdae7d2b5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001/* SPDX-License-Identifier: GPL-2.0+ */
Simon Glassf7ae68062011-11-02 09:52:07 +00002/*
3 * (C) Copyright 2000-2009
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
Simon Glassf7ae68062011-11-02 09:52:07 +00005 */
6
7#ifndef __VSPRINTF_H
8#define __VSPRINTF_H
9
Maxime Ripard9f6903f2016-07-05 10:26:36 +020010#include <stdarg.h>
Masahiro Yamada70ed9932017-09-16 14:10:43 +090011#include <linux/types.h>
Maxime Ripard9f6903f2016-07-05 10:26:36 +020012
Simon Glass3ff49ec2021-07-24 09:03:29 -060013/**
14 * simple_strtoul - convert a string to an unsigned long
15 *
Simon Glassb2c6f092022-04-24 23:30:56 -060016 * @cp: The string to be converted
17 * @endp: Updated to point to the first character not converted
18 * @base: The number base to use (0 for the default)
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010019 * Return: value decoded from string (0 if invalid)
Simon Glass3ff49ec2021-07-24 09:03:29 -060020 *
21 * Converts a string to an unsigned long. If there are invalid characters at
22 * the end these are ignored. In the worst case, if all characters are invalid,
23 * 0 is returned
Simon Glassf0722ed2021-07-24 09:03:31 -060024 *
Simon Glass90877bb2021-07-24 09:03:38 -060025 * A hex prefix is supported (e.g. 0x123) regardless of the value of @base.
26 * If found, the base is set to hex (16).
27 *
28 * If @base is 0:
29 * - an octal '0' prefix (e.g. 0777) sets the base to octal (8).
30 * - otherwise the base defaults to decimal (10).
Simon Glass3ff49ec2021-07-24 09:03:29 -060031 */
Simon Glassf7ae68062011-11-02 09:52:07 +000032ulong simple_strtoul(const char *cp, char **endp, unsigned int base);
Simon Glass71d4f182011-11-02 09:52:09 +000033
34/**
Simon Glass3ff49ec2021-07-24 09:03:29 -060035 * hex_strtoul - convert a string in hex to an unsigned long
36 *
Simon Glassb2c6f092022-04-24 23:30:56 -060037 * @cp: The string to be converted
38 * @endp: Updated to point to the first character not converted
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010039 * Return: value decoded from string (0 if invalid)
Simon Glass3ff49ec2021-07-24 09:03:29 -060040 *
41 * Converts a hex string to an unsigned long. If there are invalid characters at
42 * the end these are ignored. In the worst case, if all characters are invalid,
43 * 0 is returned
44 */
45unsigned long hextoul(const char *cp, char **endp);
46
47/**
Simon Glassff9b9032021-07-24 09:03:30 -060048 * dec_strtoul - convert a string in decimal to an unsigned long
49 *
Simon Glassb2c6f092022-04-24 23:30:56 -060050 * @cp: The string to be converted
51 * @endp: Updated to point to the first character not converted
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010052 * Return: value decoded from string (0 if invalid)
Simon Glassff9b9032021-07-24 09:03:30 -060053 *
54 * Converts a decimal string to an unsigned long. If there are invalid
55 * characters at the end these are ignored. In the worst case, if all characters
56 * are invalid, 0 is returned
57 */
58unsigned long dectoul(const char *cp, char **endp);
59
60/**
Simon Glass71d4f182011-11-02 09:52:09 +000061 * strict_strtoul - convert a string to an unsigned long strictly
Simon Glassb2c6f092022-04-24 23:30:56 -060062 * @cp: The string to be converted
63 * @base: The number base to use (0 for the default)
64 * @res: The converted result value
65 * Return: 0 if conversion is successful and `*res` is set to the converted
66 * value, otherwise it returns -EINVAL and `*res` is set to 0.
Simon Glass71d4f182011-11-02 09:52:09 +000067 *
68 * strict_strtoul converts a string to an unsigned long only if the
69 * string is really an unsigned long string, any string containing
70 * any invalid char at the tail will be rejected and -EINVAL is returned,
71 * only a newline char at the tail is acceptible because people generally
72 * change a module parameter in the following way:
73 *
74 * echo 1024 > /sys/module/e1000/parameters/copybreak
75 *
76 * echo will append a newline to the tail.
77 *
Simon Glass90877bb2021-07-24 09:03:38 -060078 * A hex prefix is supported (e.g. 0x123) regardless of the value of @base.
79 * If found, the base is set to hex (16).
80 *
81 * If @base is 0:
82 * - an octal '0' prefix (e.g. 0777) sets the base to octal (8).
83 * - otherwise the base defaults to decimal (10).
Simon Glassf0722ed2021-07-24 09:03:31 -060084 *
Simon Glass71d4f182011-11-02 09:52:09 +000085 * Copied this function from Linux 2.6.38 commit ID:
86 * 521cb40b0c44418a4fd36dc633f575813d59a43d
87 *
88 */
Simon Glassf7ae68062011-11-02 09:52:07 +000089int strict_strtoul(const char *cp, unsigned int base, unsigned long *res);
90unsigned long long simple_strtoull(const char *cp, char **endp,
91 unsigned int base);
92long simple_strtol(const char *cp, char **endp, unsigned int base);
Roland Gaudigfd8b0342021-07-23 12:29:18 +000093long long simple_strtoll(const char *cp, char **endp, unsigned int base);
Simon Glass509dbf92015-02-27 22:06:32 -070094
95/**
Simon Glass4aaa4602015-06-23 15:39:08 -060096 * trailing_strtol() - extract a trailing integer from a string
97 *
98 * Given a string this finds a trailing number on the string and returns it.
99 * For example, "abc123" would return 123.
100 *
Simon Glass20888f32022-04-24 23:30:57 -0600101 * Note that this does not handle a string without a prefix. See dectoul() for
102 * that case.
103 *
Simon Glassb2c6f092022-04-24 23:30:56 -0600104 * @str: String to examine
Simon Glasse74efcd2022-04-24 23:30:55 -0600105 * Return: trailing number if found, else -1
Simon Glass4aaa4602015-06-23 15:39:08 -0600106 */
107long trailing_strtol(const char *str);
108
109/**
110 * trailing_strtoln() - extract a trailing integer from a fixed-length string
111 *
112 * Given a fixed-length string this finds a trailing number on the string
113 * and returns it. For example, "abc123" would return 123. Only the
114 * characters between @str and @end - 1 are examined. If @end is NULL, it is
115 * set to str + strlen(str).
116 *
Simon Glassb2c6f092022-04-24 23:30:56 -0600117 * @str: String to examine
Simon Glass4aaa4602015-06-23 15:39:08 -0600118 * @end: Pointer to end of string to examine, or NULL to use the
119 * whole string
Simon Glasse74efcd2022-04-24 23:30:55 -0600120 * Return: trailing number if found, else -1
Simon Glass4aaa4602015-06-23 15:39:08 -0600121 */
122long trailing_strtoln(const char *str, const char *end);
123
124/**
Simon Glass4bf2fc12022-04-24 23:30:58 -0600125 * trailing_strtoln_end() - extract trailing integer from a fixed-length string
126 *
127 * Given a fixed-length string this finds a trailing number on the string
128 * and returns it. For example, "abc123" would return 123. Only the
129 * characters between @str and @end - 1 are examined. If @end is NULL, it is
130 * set to str + strlen(str).
131 *
132 * @str: String to examine
133 * @end: Pointer to end of string to examine, or NULL to use the
134 * whole string
135 * @endp: If non-NULL, this is set to point to the character where the
136 * number starts, e.g. for "mmc0" this would be point to the '0'; if no
137 * trailing number is found, it is set to the end of the string
138 * Return: training number if found, else -1
139 */
140long trailing_strtoln_end(const char *str, const char *end, char const **endp);
141
142/**
Simon Glass509dbf92015-02-27 22:06:32 -0700143 * panic() - Print a message and reset/hang
144 *
145 * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
Vagrant Cascadian259b1fb2016-10-23 20:45:19 -0700146 * defined, then it will hang instead of resetting.
Simon Glass509dbf92015-02-27 22:06:32 -0700147 *
Simon Glassb2c6f092022-04-24 23:30:56 -0600148 * @fmt: printf() format string for message, which should not include
Simon Glass509dbf92015-02-27 22:06:32 -0700149 * \n, followed by arguments
150 */
Simon Glassf7ae68062011-11-02 09:52:07 +0000151void panic(const char *fmt, ...)
152 __attribute__ ((format (__printf__, 1, 2), noreturn));
Simon Glass71d4f182011-11-02 09:52:09 +0000153
154/**
Simon Glass509dbf92015-02-27 22:06:32 -0700155 * panic_str() - Print a message and reset/hang
156 *
157 * Prints a message on the console(s) and then resets. If CONFIG_PANIC_HANG is
Vagrant Cascadian259b1fb2016-10-23 20:45:19 -0700158 * defined, then it will hang instead of resetting.
Simon Glass509dbf92015-02-27 22:06:32 -0700159 *
160 * This function can be used instead of panic() when your board does not
161 * already use printf(), * to keep code size small.
162 *
Simon Glassb2c6f092022-04-24 23:30:56 -0600163 * @str: string to display, which should not include \n
Simon Glass509dbf92015-02-27 22:06:32 -0700164 */
165void panic_str(const char *str) __attribute__ ((noreturn));
166
167/**
Simon Glass71d4f182011-11-02 09:52:09 +0000168 * Format a string and place it in a buffer
169 *
Simon Glassb2c6f092022-04-24 23:30:56 -0600170 * @buf: The buffer to place the result into
171 * @fmt: The format string to use
172 * @...: Arguments for the format string
Simon Glass71d4f182011-11-02 09:52:09 +0000173 *
174 * The function returns the number of characters written
175 * into @buf.
176 *
177 * See the vsprintf() documentation for format string extensions over C99.
178 */
Simon Glassf7ae68062011-11-02 09:52:07 +0000179int sprintf(char *buf, const char *fmt, ...)
180 __attribute__ ((format (__printf__, 2, 3)));
Simon Glass71d4f182011-11-02 09:52:09 +0000181
182/**
183 * Format a string and place it in a buffer (va_list version)
184 *
Simon Glassb2c6f092022-04-24 23:30:56 -0600185 * @buf: The buffer to place the result into
186 * @fmt: The format string to use
187 * @args: Arguments for the format string
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100188 * Return: the number of characters which have been written into
Heinrich Schuchardt20662532017-09-06 17:55:13 +0200189 * the @buf not including the trailing '\0'.
Simon Glass71d4f182011-11-02 09:52:09 +0000190 *
191 * If you're not already dealing with a va_list consider using scnprintf().
192 *
193 * See the vsprintf() documentation for format string extensions over C99.
194 */
Simon Glassf7ae68062011-11-02 09:52:07 +0000195int vsprintf(char *buf, const char *fmt, va_list args);
Simon Glass978739d2021-10-14 12:48:06 -0600196
197/**
198 * simple_itoa() - convert an unsigned integer to a string
199 *
200 * This returns a static string containing the decimal representation of the
Simon Glass1f467182021-10-14 12:48:07 -0600201 * given value. The returned value may be overwritten by other calls to other
Simon Glassb2c6f092022-04-24 23:30:56 -0600202 * simple... functions, so should be used immediately
Simon Glass978739d2021-10-14 12:48:06 -0600203 *
204 * @val: Value to convert
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100205 * Return: string containing the decimal representation of @val
Simon Glass978739d2021-10-14 12:48:06 -0600206 */
207char *simple_itoa(ulong val);
Simon Glassf7ae68062011-11-02 09:52:07 +0000208
Simon Glass71d4f182011-11-02 09:52:09 +0000209/**
Simon Glass1f467182021-10-14 12:48:07 -0600210 * simple_xtoa() - convert an unsigned integer to a hex string
211 *
212 * This returns a static string containing the hexadecimal representation of the
213 * given value. The returned value may be overwritten by other calls to other
Simon Glassb2c6f092022-04-24 23:30:56 -0600214 * simple... functions, so should be used immediately
Simon Glass1f467182021-10-14 12:48:07 -0600215 *
Simon Glassb2c6f092022-04-24 23:30:56 -0600216 * @num: Value to convert
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100217 * Return: string containing the hexecimal representation of @val
Simon Glass1f467182021-10-14 12:48:07 -0600218 */
219char *simple_xtoa(ulong num);
220
221/**
Simon Glass71d4f182011-11-02 09:52:09 +0000222 * Format a string and place it in a buffer
223 *
Simon Glassb2c6f092022-04-24 23:30:56 -0600224 * @buf: The buffer to place the result into
225 * @size: The size of the buffer, including the trailing null space
226 * @fmt: The format string to use
227 * @...: Arguments for the format string
Simon Glass71d4f182011-11-02 09:52:09 +0000228 *
229 * The return value is the number of characters written into @buf not including
230 * the trailing '\0'. If @size is == 0 the function returns 0.
231 *
232 * See the vsprintf() documentation for format string extensions over C99.
233 */
Sonny Raocd55bde2011-11-02 09:52:08 +0000234int scnprintf(char *buf, size_t size, const char *fmt, ...)
235 __attribute__ ((format (__printf__, 3, 4)));
Simon Glass71d4f182011-11-02 09:52:09 +0000236
237/**
238 * Format a string and place it in a buffer (base function)
239 *
Simon Glassb2c6f092022-04-24 23:30:56 -0600240 * @buf: The buffer to place the result into
241 * @size: The size of the buffer, including the trailing null space
242 * @fmt: The format string to use
243 * @args: Arguments for the format string
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100244 * Return: The number characters which would be generated for the given
Simon Glass71d4f182011-11-02 09:52:09 +0000245 * input, excluding the trailing '\0', as per ISO C99. Note that fewer
246 * characters may be written if this number of characters is >= size.
247 *
248 * This function follows C99 vsnprintf, but has some extensions:
249 * %pS output the name of a text symbol
250 * %pF output the name of a function pointer
251 * %pR output the address range in a struct resource
252 *
253 * The function returns the number of characters which would be
254 * generated for the given input, excluding the trailing '\0',
255 * as per ISO C99.
256 *
257 * Call this function if you are already dealing with a va_list.
258 * You probably want snprintf() instead.
259 */
Sonny Raocd55bde2011-11-02 09:52:08 +0000260int vsnprintf(char *buf, size_t size, const char *fmt, va_list args);
Simon Glass71d4f182011-11-02 09:52:09 +0000261
262/**
263 * Format a string and place it in a buffer (va_list version)
264 *
Simon Glassb2c6f092022-04-24 23:30:56 -0600265 * @buf: The buffer to place the result into
266 * @size: The size of the buffer, including the trailing null space
267 * @fmt: The format string to use
268 * @args: Arguments for the format string
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100269 * Return: the number of characters which have been written into
Simon Glass71d4f182011-11-02 09:52:09 +0000270 * the @buf not including the trailing '\0'. If @size is == 0 the function
271 * returns 0.
272 *
273 * If you're not already dealing with a va_list consider using scnprintf().
274 *
275 * See the vsprintf() documentation for format string extensions over C99.
276 */
Sonny Raocd55bde2011-11-02 09:52:08 +0000277int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
Sonny Raocd55bde2011-11-02 09:52:08 +0000278
Simon Glassb09cfd82013-06-11 11:14:38 -0700279/**
280 * print_grouped_ull() - print a value with digits grouped by ','
281 *
282 * This prints a value with grouped digits, like 12,345,678 to make it easier
283 * to read.
284 *
Simon Glassb2c6f092022-04-24 23:30:56 -0600285 * @int_val: Value to print
286 * @digits: Number of digiits to print
Simon Glassb09cfd82013-06-11 11:14:38 -0700287 */
288void print_grouped_ull(unsigned long long int_val, int digits);
289
Heiko Schocher7e5a16c2015-04-27 07:42:05 +0200290bool str2off(const char *p, loff_t *num);
291bool str2long(const char *p, ulong *num);
Simon Glassf5c208d2019-11-14 12:57:20 -0700292
293/**
294 * strmhz() - Convert a value to a Hz string
295 *
296 * This creates a string indicating the number of MHz of a value. For example,
297 * 2700000 produces "2.7".
298 * @buf: Buffer to hold output string, which must be large enough
299 * @hz: Value to convert
300 */
301char *strmhz(char *buf, unsigned long hz);
Simon Glass811a2602020-04-08 08:32:56 -0600302
303/**
304 * str_to_upper() - Convert a string to upper case
305 *
306 * This simply uses toupper() on each character of the string.
307 *
308 * @in: String to convert (must be large enough to hold the output string)
309 * @out: Buffer to put converted string
310 * @len: Number of bytes available in @out (SIZE_MAX for all)
311 */
312void str_to_upper(const char *in, char *out, size_t len);
313
Andrii Anisov6e29ac42020-08-06 12:42:52 +0300314/**
Simon Glass6a9de552023-01-17 10:47:14 -0700315 * str_to_list() - Convert a string to a list of string pointers
316 *
317 * Splits a string containing space-delimited substrings into a number of
318 * separate strings, e.g. "this is" becomes {"this", "is", NULL}. If @instr is
319 * empty then this returns just {NULL}. The string should have only a single
320 * space between items, with no leading or trailing spaces.
321 *
322 * @instr: String to process (this is alloced by this function)
323 * Returns: List of string pointers, terminated by NULL. Each entry points to
324 * a string. If @instr is empty, the list consists just of a single NULL entry.
325 * Note that the first entry points to the alloced string.
326 * Returns NULL if out of memory
327 */
328const char **str_to_list(const char *instr);
329
330/**
331 * str_free_list() - Free a string list
332 *
333 * @ptr: String list to free, as created by str_to_list(). This can also be
334 * NULL, in which case the function does nothing
335 */
336void str_free_list(const char **ptr);
337
338/**
Samuel Dionne-Rielde464932021-12-20 18:19:16 -0500339 * vsscanf - Unformat a buffer into a list of arguments
Simon Glassb2c6f092022-04-24 23:30:56 -0600340 * @inp: input buffer
341 * @fmt0: format of buffer
342 * @ap: arguments
Samuel Dionne-Rielde464932021-12-20 18:19:16 -0500343 */
344int vsscanf(const char *inp, char const *fmt0, va_list ap);
345
346/**
Andrii Anisov6e29ac42020-08-06 12:42:52 +0300347 * sscanf - Unformat a buffer into a list of arguments
348 * @buf: input buffer
349 * @fmt: formatting of buffer
350 * @...: resulting arguments
351 */
352int sscanf(const char *buf, const char *fmt, ...);
353
Simon Glassf7ae68062011-11-02 09:52:07 +0000354#endif