lib: Split panic functions out of vsprintf.c
[oweals/u-boot.git] / lib / vsprintf.c
index e0f264850f7fa75bc6cbeaad2537d3fffc4a318d..bf5fd01b62e210e06a1715f72afa16fee05f5781 100644 (file)
@@ -166,6 +166,25 @@ unsigned long long simple_strtoull(const char *cp, char **endp,
        return result;
 }
 
+long trailing_strtoln(const char *str, const char *end)
+{
+       const char *p;
+
+       if (!end)
+               end = str + strlen(str);
+       for (p = end - 1; p > str; p--) {
+               if (!isdigit(*p))
+                       return simple_strtoul(p + 1, NULL, 10);
+       }
+
+       return -1;
+}
+
+long trailing_strtol(const char *str)
+{
+       return trailing_strtoln(str, NULL);
+}
+
 /* we use this so that we can do without the ctype library */
 #define is_digit(c)    ((c) >= '0' && (c) <= '9')
 
@@ -842,23 +861,43 @@ int sprintf(char *buf, const char *fmt, ...)
        return i;
 }
 
-void panic(const char *fmt, ...)
+int printf(const char *fmt, ...)
 {
        va_list args;
+       uint i;
+       char printbuffer[CONFIG_SYS_PBSIZE];
+
        va_start(args, fmt);
-       vprintf(fmt, args);
-       putc('\n');
+
+       /*
+        * For this to work, printbuffer must be larger than
+        * anything we ever want to print.
+        */
+       i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
        va_end(args);
-#if defined(CONFIG_PANIC_HANG)
-       hang();
-#else
-       udelay(100000); /* allow messages to go out */
-       do_reset(NULL, 0, 0, NULL);
-#endif
-       while (1)
-               ;
+
+       /* Print the string */
+       puts(printbuffer);
+       return i;
 }
 
+int vprintf(const char *fmt, va_list args)
+{
+       uint i;
+       char printbuffer[CONFIG_SYS_PBSIZE];
+
+       /*
+        * For this to work, printbuffer must be larger than
+        * anything we ever want to print.
+        */
+       i = vscnprintf(printbuffer, sizeof(printbuffer), fmt, args);
+
+       /* Print the string */
+       puts(printbuffer);
+       return i;
+}
+
+
 void __assert_fail(const char *assertion, const char *file, unsigned line,
                   const char *function)
 {
@@ -896,3 +935,19 @@ void print_grouped_ull(unsigned long long int_val, int digits)
                grab = 3;
        }
 }
+
+bool str2off(const char *p, loff_t *num)
+{
+       char *endptr;
+
+       *num = simple_strtoull(p, &endptr, 16);
+       return *p != '\0' && *endptr == '\0';
+}
+
+bool str2long(const char *p, ulong *num)
+{
+       char *endptr;
+
+       *num = simple_strtoul(p, &endptr, 16);
+       return *p != '\0' && *endptr == '\0';
+}