Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 1 | /* vtxprintf.c, from |
| 2 | * linux/lib/vsprintf.c |
| 3 | * |
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 5 | */ |
| 6 | |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 7 | #include <string.h> |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 8 | #include <div64.h> |
Stefan Reinauer | 3aa067f | 2012-04-02 13:24:04 -0700 | [diff] [blame] | 9 | #include <console/console.h> |
Stefan Reinauer | 52fc6b1 | 2009-10-24 13:06:04 +0000 | [diff] [blame] | 10 | #include <console/vtxprintf.h> |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 11 | |
| 12 | /* haha, don't need ctype.c */ |
| 13 | #define isdigit(c) ((c) >= '0' && (c) <= '9') |
| 14 | #define is_digit isdigit |
| 15 | #define isxdigit(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F')) |
| 16 | |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 17 | static int skip_atoi(const char **s) |
| 18 | { |
| 19 | int i=0; |
| 20 | |
| 21 | while (is_digit(**s)) |
| 22 | i = i*10 + *((*s)++) - '0'; |
| 23 | return i; |
| 24 | } |
| 25 | |
| 26 | #define ZEROPAD 1 /* pad with zero */ |
| 27 | #define SIGN 2 /* unsigned/signed long */ |
| 28 | #define PLUS 4 /* show plus */ |
| 29 | #define SPACE 8 /* space if plus */ |
| 30 | #define LEFT 16 /* left justified */ |
| 31 | #define SPECIAL 32 /* 0x */ |
| 32 | #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */ |
| 33 | |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 34 | static int number(void (*tx_byte)(unsigned char byte), |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 35 | unsigned long long num, int base, int size, int precision, int type) |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 36 | { |
| 37 | char c,sign,tmp[66]; |
| 38 | const char *digits="0123456789abcdefghijklmnopqrstuvwxyz"; |
| 39 | int i; |
| 40 | int count = 0; |
| 41 | |
| 42 | if (type & LARGE) |
| 43 | digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
| 44 | if (type & LEFT) |
| 45 | type &= ~ZEROPAD; |
| 46 | if (base < 2 || base > 36) |
| 47 | return 0; |
| 48 | c = (type & ZEROPAD) ? '0' : ' '; |
| 49 | sign = 0; |
| 50 | if (type & SIGN) { |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 51 | if ((signed long long)num < 0) { |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 52 | sign = '-'; |
| 53 | num = -num; |
| 54 | size--; |
| 55 | } else if (type & PLUS) { |
| 56 | sign = '+'; |
| 57 | size--; |
| 58 | } else if (type & SPACE) { |
| 59 | sign = ' '; |
| 60 | size--; |
| 61 | } |
| 62 | } |
| 63 | if (type & SPECIAL) { |
| 64 | if (base == 16) |
| 65 | size -= 2; |
| 66 | else if (base == 8) |
| 67 | size--; |
| 68 | } |
| 69 | i = 0; |
| 70 | if (num == 0) |
| 71 | tmp[i++]='0'; |
| 72 | else while (num != 0) |
| 73 | tmp[i++] = digits[do_div(num,base)]; |
| 74 | if (i > precision) |
| 75 | precision = i; |
| 76 | size -= precision; |
| 77 | if (!(type&(ZEROPAD+LEFT))) |
| 78 | while(size-->0) |
| 79 | tx_byte(' '), count++; |
| 80 | if (sign) |
| 81 | tx_byte(sign), count++; |
| 82 | if (type & SPECIAL) { |
| 83 | if (base==8) |
| 84 | tx_byte('0'), count++; |
| 85 | else if (base==16) { |
| 86 | tx_byte('0'), count++; |
| 87 | tx_byte(digits[33]), count++; |
| 88 | } |
| 89 | } |
| 90 | if (!(type & LEFT)) |
| 91 | while (size-- > 0) |
| 92 | tx_byte(c), count++; |
| 93 | while (i < precision--) |
| 94 | tx_byte('0'), count++; |
| 95 | while (i-- > 0) |
| 96 | tx_byte(tmp[i]), count++; |
| 97 | while (size-- > 0) |
| 98 | tx_byte(' '), count++; |
| 99 | return count; |
| 100 | } |
| 101 | |
| 102 | |
| 103 | int vtxprintf(void (*tx_byte)(unsigned char byte), const char *fmt, va_list args) |
| 104 | { |
| 105 | int len; |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 106 | unsigned long long num; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 107 | int i, base; |
| 108 | const char *s; |
| 109 | |
| 110 | int flags; /* flags to number() */ |
| 111 | |
| 112 | int field_width; /* width of output field */ |
| 113 | int precision; /* min. # of digits for integers; max |
| 114 | number of chars for from string */ |
| 115 | int qualifier; /* 'h', 'l', or 'L' for integer fields */ |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 116 | |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 117 | int count; |
| 118 | |
Stefan Reinauer | 3aa067f | 2012-04-02 13:24:04 -0700 | [diff] [blame] | 119 | #if defined(__SMM__) && CONFIG_SMM_TSEG |
| 120 | /* Fix pointer in TSEG */ |
| 121 | tx_byte = console_tx_byte; |
| 122 | #endif |
| 123 | |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 124 | for (count=0; *fmt ; ++fmt) { |
| 125 | if (*fmt != '%') { |
| 126 | tx_byte(*fmt), count++; |
| 127 | continue; |
| 128 | } |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 129 | |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 130 | /* process flags */ |
| 131 | flags = 0; |
Patrick Georgi | c5fc7db | 2012-03-07 15:55:47 +0100 | [diff] [blame] | 132 | repeat: |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 133 | ++fmt; /* this also skips first '%' */ |
| 134 | switch (*fmt) { |
| 135 | case '-': flags |= LEFT; goto repeat; |
| 136 | case '+': flags |= PLUS; goto repeat; |
| 137 | case ' ': flags |= SPACE; goto repeat; |
| 138 | case '#': flags |= SPECIAL; goto repeat; |
| 139 | case '0': flags |= ZEROPAD; goto repeat; |
| 140 | } |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 141 | |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 142 | /* get field width */ |
| 143 | field_width = -1; |
| 144 | if (is_digit(*fmt)) |
| 145 | field_width = skip_atoi(&fmt); |
| 146 | else if (*fmt == '*') { |
| 147 | ++fmt; |
| 148 | /* it's the next argument */ |
| 149 | field_width = va_arg(args, int); |
| 150 | if (field_width < 0) { |
| 151 | field_width = -field_width; |
| 152 | flags |= LEFT; |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | /* get the precision */ |
| 157 | precision = -1; |
| 158 | if (*fmt == '.') { |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 159 | ++fmt; |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 160 | if (is_digit(*fmt)) |
| 161 | precision = skip_atoi(&fmt); |
| 162 | else if (*fmt == '*') { |
| 163 | ++fmt; |
| 164 | /* it's the next argument */ |
| 165 | precision = va_arg(args, int); |
| 166 | } |
| 167 | if (precision < 0) |
| 168 | precision = 0; |
| 169 | } |
| 170 | |
| 171 | /* get the conversion qualifier */ |
| 172 | qualifier = -1; |
Stefan Reinauer | bfff6de | 2012-05-15 13:28:07 -0700 | [diff] [blame] | 173 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'z') { |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 174 | qualifier = *fmt; |
| 175 | ++fmt; |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 176 | if (*fmt == 'l') { |
| 177 | qualifier = 'L'; |
| 178 | ++fmt; |
| 179 | } |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /* default base */ |
| 183 | base = 10; |
| 184 | |
| 185 | switch (*fmt) { |
| 186 | case 'c': |
| 187 | if (!(flags & LEFT)) |
| 188 | while (--field_width > 0) |
| 189 | tx_byte(' '), count++; |
| 190 | tx_byte((unsigned char) va_arg(args, int)), count++; |
| 191 | while (--field_width > 0) |
| 192 | tx_byte(' '), count++; |
| 193 | continue; |
| 194 | |
| 195 | case 's': |
| 196 | s = va_arg(args, char *); |
| 197 | if (!s) |
| 198 | s = "<NULL>"; |
| 199 | |
| 200 | len = strnlen(s, precision); |
| 201 | |
| 202 | if (!(flags & LEFT)) |
| 203 | while (len < field_width--) |
| 204 | tx_byte(' '), count++; |
| 205 | for (i = 0; i < len; ++i) |
| 206 | tx_byte(*s++), count++; |
| 207 | while (len < field_width--) |
| 208 | tx_byte(' '), count++; |
| 209 | continue; |
| 210 | |
| 211 | case 'p': |
| 212 | if (field_width == -1) { |
| 213 | field_width = 2*sizeof(void *); |
| 214 | flags |= ZEROPAD; |
| 215 | } |
| 216 | count += number(tx_byte, |
| 217 | (unsigned long) va_arg(args, void *), 16, |
| 218 | field_width, precision, flags); |
| 219 | continue; |
| 220 | |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 221 | case 'n': |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 222 | if (qualifier == 'L') { |
| 223 | long long *ip = va_arg(args, long long *); |
| 224 | *ip = count; |
| 225 | } else if (qualifier == 'l') { |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 226 | long * ip = va_arg(args, long *); |
| 227 | *ip = count; |
| 228 | } else { |
| 229 | int * ip = va_arg(args, int *); |
| 230 | *ip = count; |
| 231 | } |
| 232 | continue; |
| 233 | |
| 234 | case '%': |
| 235 | tx_byte('%'), count++; |
| 236 | continue; |
| 237 | |
| 238 | /* integer number formats - set up the flags and "break" */ |
| 239 | case 'o': |
| 240 | base = 8; |
| 241 | break; |
| 242 | |
| 243 | case 'X': |
| 244 | flags |= LARGE; |
| 245 | case 'x': |
| 246 | base = 16; |
| 247 | break; |
| 248 | |
| 249 | case 'd': |
| 250 | case 'i': |
| 251 | flags |= SIGN; |
| 252 | case 'u': |
| 253 | break; |
| 254 | |
| 255 | default: |
| 256 | tx_byte('%'), count++; |
| 257 | if (*fmt) |
| 258 | tx_byte(*fmt), count++; |
| 259 | else |
| 260 | --fmt; |
| 261 | continue; |
| 262 | } |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 263 | if (qualifier == 'L') { |
| 264 | num = va_arg(args, unsigned long long); |
| 265 | } else if (qualifier == 'l') { |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 266 | num = va_arg(args, unsigned long); |
Stefan Reinauer | bfff6de | 2012-05-15 13:28:07 -0700 | [diff] [blame] | 267 | } else if (qualifier == 'z') { |
| 268 | num = va_arg(args, size_t); |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 269 | } else if (qualifier == 'h') { |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 270 | num = (unsigned short) va_arg(args, int); |
| 271 | if (flags & SIGN) |
| 272 | num = (short) num; |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 273 | } else if (flags & SIGN) { |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 274 | num = va_arg(args, int); |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 275 | } else { |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 276 | num = va_arg(args, unsigned int); |
Eric Biederman | f3ed1cf | 2004-10-16 08:38:58 +0000 | [diff] [blame] | 277 | } |
Stefan Reinauer | 34e3a14 | 2004-05-28 15:07:03 +0000 | [diff] [blame] | 278 | count += number(tx_byte, num, base, field_width, precision, flags); |
| 279 | } |
| 280 | return count; |
| 281 | } |
| 282 | |