Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Tiny printf version for SPL |
| 3 | * |
| 4 | * Copied from: |
| 5 | * http://www.sparetimelabs.com/printfrevisited/printfrevisited.php |
| 6 | * |
| 7 | * Copyright (C) 2004,2008 Kustaa Nyholm |
| 8 | * |
| 9 | * SPDX-License-Identifier: LGPL-2.1+ |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <stdarg.h> |
| 14 | #include <serial.h> |
| 15 | |
| 16 | static char *bf; |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 17 | static char zs; |
| 18 | |
Simon Glass | 5c411d8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 19 | /* Current position in sprintf() output string */ |
| 20 | static char *outstr; |
| 21 | |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 22 | static void out(char c) |
| 23 | { |
| 24 | *bf++ = c; |
| 25 | } |
| 26 | |
| 27 | static void out_dgt(char dgt) |
| 28 | { |
Stefan Roese | a5ecdd0 | 2015-11-16 15:26:34 +0100 | [diff] [blame] | 29 | out(dgt + (dgt < 10 ? '0' : 'a' - 10)); |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 30 | zs = 1; |
| 31 | } |
| 32 | |
Stefan Roese | a5ecdd0 | 2015-11-16 15:26:34 +0100 | [diff] [blame] | 33 | static void div_out(unsigned int *num, unsigned int div) |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 34 | { |
| 35 | unsigned char dgt = 0; |
| 36 | |
Stefan Roese | a5ecdd0 | 2015-11-16 15:26:34 +0100 | [diff] [blame] | 37 | while (*num >= div) { |
| 38 | *num -= div; |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 39 | dgt++; |
| 40 | } |
| 41 | |
| 42 | if (zs || dgt > 0) |
| 43 | out_dgt(dgt); |
| 44 | } |
| 45 | |
Simon Glass | 5c411d8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 46 | int _vprintf(const char *fmt, va_list va, void (*putc)(const char ch)) |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 47 | { |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 48 | char ch; |
| 49 | char *p; |
Stefan Roese | a5ecdd0 | 2015-11-16 15:26:34 +0100 | [diff] [blame] | 50 | unsigned int num; |
| 51 | char buf[12]; |
| 52 | unsigned int div; |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 53 | |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 54 | while ((ch = *(fmt++))) { |
| 55 | if (ch != '%') { |
| 56 | putc(ch); |
| 57 | } else { |
Simon Glass | 1fb6760 | 2016-05-14 14:02:52 -0600 | [diff] [blame] | 58 | bool lz = false; |
| 59 | int width = 0; |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 60 | |
| 61 | ch = *(fmt++); |
| 62 | if (ch == '0') { |
| 63 | ch = *(fmt++); |
| 64 | lz = 1; |
| 65 | } |
| 66 | |
| 67 | if (ch >= '0' && ch <= '9') { |
Simon Glass | 1fb6760 | 2016-05-14 14:02:52 -0600 | [diff] [blame] | 68 | width = 0; |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 69 | while (ch >= '0' && ch <= '9') { |
Simon Glass | 1fb6760 | 2016-05-14 14:02:52 -0600 | [diff] [blame] | 70 | width = (width * 10) + ch - '0'; |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 71 | ch = *fmt++; |
| 72 | } |
| 73 | } |
| 74 | bf = buf; |
| 75 | p = bf; |
| 76 | zs = 0; |
| 77 | |
| 78 | switch (ch) { |
Simon Glass | 1fb6760 | 2016-05-14 14:02:52 -0600 | [diff] [blame] | 79 | case '\0': |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 80 | goto abort; |
| 81 | case 'u': |
| 82 | case 'd': |
| 83 | num = va_arg(va, unsigned int); |
| 84 | if (ch == 'd' && (int)num < 0) { |
| 85 | num = -(int)num; |
| 86 | out('-'); |
| 87 | } |
Simon Glass | 74b1320 | 2016-01-05 09:30:57 -0700 | [diff] [blame] | 88 | if (!num) { |
| 89 | out_dgt(0); |
| 90 | } else { |
| 91 | for (div = 1000000000; div; div /= 10) |
| 92 | div_out(&num, div); |
| 93 | } |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 94 | break; |
| 95 | case 'x': |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 96 | num = va_arg(va, unsigned int); |
Simon Glass | 74b1320 | 2016-01-05 09:30:57 -0700 | [diff] [blame] | 97 | if (!num) { |
| 98 | out_dgt(0); |
| 99 | } else { |
| 100 | for (div = 0x10000000; div; div /= 0x10) |
| 101 | div_out(&num, div); |
| 102 | } |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 103 | break; |
| 104 | case 'c': |
| 105 | out((char)(va_arg(va, int))); |
| 106 | break; |
| 107 | case 's': |
| 108 | p = va_arg(va, char*); |
| 109 | break; |
| 110 | case '%': |
| 111 | out('%'); |
| 112 | default: |
| 113 | break; |
| 114 | } |
| 115 | |
| 116 | *bf = 0; |
| 117 | bf = p; |
Simon Glass | 1fb6760 | 2016-05-14 14:02:52 -0600 | [diff] [blame] | 118 | while (*bf++ && width > 0) |
| 119 | width--; |
| 120 | while (width-- > 0) |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 121 | putc(lz ? '0' : ' '); |
Simon Glass | 8e31681 | 2015-12-29 05:22:46 -0700 | [diff] [blame] | 122 | if (p) { |
| 123 | while ((ch = *p++)) |
| 124 | putc(ch); |
| 125 | } |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | abort: |
Stefan Roese | 7d9cde1 | 2015-11-23 07:00:22 +0100 | [diff] [blame] | 130 | return 0; |
| 131 | } |
Sjoerd Simons | 962a43c | 2015-12-04 23:27:37 +0100 | [diff] [blame] | 132 | |
| 133 | int printf(const char *fmt, ...) |
| 134 | { |
| 135 | va_list va; |
| 136 | int ret; |
| 137 | |
| 138 | va_start(va, fmt); |
Simon Glass | 5c411d8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 139 | ret = _vprintf(fmt, va, putc); |
Sjoerd Simons | 962a43c | 2015-12-04 23:27:37 +0100 | [diff] [blame] | 140 | va_end(va); |
| 141 | |
| 142 | return ret; |
| 143 | } |
Simon Glass | 5c411d8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 144 | |
| 145 | static void putc_outstr(char ch) |
| 146 | { |
| 147 | *outstr++ = ch; |
| 148 | } |
| 149 | |
Marek Vasut | abeb272 | 2016-05-31 23:12:46 +0200 | [diff] [blame] | 150 | int sprintf(char *buf, const char *fmt, ...) |
Simon Glass | 5c411d8 | 2016-05-14 14:02:53 -0600 | [diff] [blame] | 151 | { |
| 152 | va_list va; |
| 153 | int ret; |
| 154 | |
| 155 | va_start(va, fmt); |
| 156 | outstr = buf; |
| 157 | ret = _vprintf(fmt, va, putc_outstr); |
| 158 | va_end(va); |
| 159 | *outstr = '\0'; |
| 160 | |
| 161 | return ret; |
| 162 | } |
Marek Vasut | abeb272 | 2016-05-31 23:12:46 +0200 | [diff] [blame] | 163 | |
| 164 | /* Note that size is ignored */ |
| 165 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
| 166 | { |
| 167 | va_list va; |
| 168 | int ret; |
| 169 | |
| 170 | va_start(va, fmt); |
Simon Glass | 3191d84 | 2016-06-08 20:55:15 -0600 | [diff] [blame^] | 171 | outstr = buf; |
| 172 | ret = _vprintf(fmt, va, putc_outstr); |
Marek Vasut | abeb272 | 2016-05-31 23:12:46 +0200 | [diff] [blame] | 173 | va_end(va); |
Simon Glass | 3191d84 | 2016-06-08 20:55:15 -0600 | [diff] [blame^] | 174 | *outstr = '\0'; |
Marek Vasut | abeb272 | 2016-05-31 23:12:46 +0200 | [diff] [blame] | 175 | |
| 176 | return ret; |
| 177 | } |