2004-05-28 17:07:03 +02:00
|
|
|
/* vtxprintf.c, from
|
|
|
|
* linux/lib/vsprintf.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 1991, 1992 Linus Torvalds
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <string.h>
|
2004-10-16 10:38:58 +02:00
|
|
|
#include <div64.h>
|
2012-04-02 22:24:04 +02:00
|
|
|
#include <console/console.h>
|
2009-10-24 15:06:04 +02:00
|
|
|
#include <console/vtxprintf.h>
|
2004-05-28 17:07:03 +02:00
|
|
|
|
|
|
|
/* haha, don't need ctype.c */
|
|
|
|
#define isdigit(c) ((c) >= '0' && (c) <= '9')
|
|
|
|
#define is_digit isdigit
|
|
|
|
#define isxdigit(c) (((c) >= '0' && (c) <= '9') || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
|
|
|
|
|
|
|
|
static int skip_atoi(const char **s)
|
|
|
|
{
|
|
|
|
int i=0;
|
|
|
|
|
|
|
|
while (is_digit(**s))
|
|
|
|
i = i*10 + *((*s)++) - '0';
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define ZEROPAD 1 /* pad with zero */
|
|
|
|
#define SIGN 2 /* unsigned/signed long */
|
|
|
|
#define PLUS 4 /* show plus */
|
|
|
|
#define SPACE 8 /* space if plus */
|
|
|
|
#define LEFT 16 /* left justified */
|
|
|
|
#define SPECIAL 32 /* 0x */
|
|
|
|
#define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
|
|
|
|
|
2010-04-27 08:56:47 +02:00
|
|
|
static int number(void (*tx_byte)(unsigned char byte),
|
2004-10-16 10:38:58 +02:00
|
|
|
unsigned long long num, int base, int size, int precision, int type)
|
2004-05-28 17:07:03 +02:00
|
|
|
{
|
|
|
|
char c,sign,tmp[66];
|
|
|
|
const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
|
|
|
|
int i;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
if (type & LARGE)
|
|
|
|
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
if (type & LEFT)
|
|
|
|
type &= ~ZEROPAD;
|
|
|
|
if (base < 2 || base > 36)
|
|
|
|
return 0;
|
|
|
|
c = (type & ZEROPAD) ? '0' : ' ';
|
|
|
|
sign = 0;
|
|
|
|
if (type & SIGN) {
|
2004-10-16 10:38:58 +02:00
|
|
|
if ((signed long long)num < 0) {
|
2004-05-28 17:07:03 +02:00
|
|
|
sign = '-';
|
|
|
|
num = -num;
|
|
|
|
size--;
|
|
|
|
} else if (type & PLUS) {
|
|
|
|
sign = '+';
|
|
|
|
size--;
|
|
|
|
} else if (type & SPACE) {
|
|
|
|
sign = ' ';
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (type & SPECIAL) {
|
|
|
|
if (base == 16)
|
|
|
|
size -= 2;
|
|
|
|
else if (base == 8)
|
|
|
|
size--;
|
|
|
|
}
|
|
|
|
i = 0;
|
|
|
|
if (num == 0)
|
|
|
|
tmp[i++]='0';
|
|
|
|
else while (num != 0)
|
|
|
|
tmp[i++] = digits[do_div(num,base)];
|
|
|
|
if (i > precision)
|
|
|
|
precision = i;
|
|
|
|
size -= precision;
|
|
|
|
if (!(type&(ZEROPAD+LEFT)))
|
|
|
|
while(size-->0)
|
|
|
|
tx_byte(' '), count++;
|
|
|
|
if (sign)
|
|
|
|
tx_byte(sign), count++;
|
|
|
|
if (type & SPECIAL) {
|
|
|
|
if (base==8)
|
|
|
|
tx_byte('0'), count++;
|
|
|
|
else if (base==16) {
|
|
|
|
tx_byte('0'), count++;
|
|
|
|
tx_byte(digits[33]), count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!(type & LEFT))
|
|
|
|
while (size-- > 0)
|
|
|
|
tx_byte(c), count++;
|
|
|
|
while (i < precision--)
|
|
|
|
tx_byte('0'), count++;
|
|
|
|
while (i-- > 0)
|
|
|
|
tx_byte(tmp[i]), count++;
|
|
|
|
while (size-- > 0)
|
|
|
|
tx_byte(' '), count++;
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int vtxprintf(void (*tx_byte)(unsigned char byte), const char *fmt, va_list args)
|
|
|
|
{
|
|
|
|
int len;
|
2004-10-16 10:38:58 +02:00
|
|
|
unsigned long long num;
|
2004-05-28 17:07:03 +02:00
|
|
|
int i, base;
|
|
|
|
const char *s;
|
|
|
|
|
|
|
|
int flags; /* flags to number() */
|
|
|
|
|
|
|
|
int field_width; /* width of output field */
|
|
|
|
int precision; /* min. # of digits for integers; max
|
|
|
|
number of chars for from string */
|
|
|
|
int qualifier; /* 'h', 'l', or 'L' for integer fields */
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2004-05-28 17:07:03 +02:00
|
|
|
int count;
|
|
|
|
|
2012-04-02 22:24:04 +02:00
|
|
|
#if defined(__SMM__) && CONFIG_SMM_TSEG
|
|
|
|
/* Fix pointer in TSEG */
|
|
|
|
tx_byte = console_tx_byte;
|
|
|
|
#endif
|
|
|
|
|
2004-05-28 17:07:03 +02:00
|
|
|
for (count=0; *fmt ; ++fmt) {
|
|
|
|
if (*fmt != '%') {
|
|
|
|
tx_byte(*fmt), count++;
|
|
|
|
continue;
|
|
|
|
}
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2004-05-28 17:07:03 +02:00
|
|
|
/* process flags */
|
|
|
|
flags = 0;
|
2012-03-07 15:55:47 +01:00
|
|
|
repeat:
|
2004-05-28 17:07:03 +02:00
|
|
|
++fmt; /* this also skips first '%' */
|
|
|
|
switch (*fmt) {
|
|
|
|
case '-': flags |= LEFT; goto repeat;
|
|
|
|
case '+': flags |= PLUS; goto repeat;
|
|
|
|
case ' ': flags |= SPACE; goto repeat;
|
|
|
|
case '#': flags |= SPECIAL; goto repeat;
|
|
|
|
case '0': flags |= ZEROPAD; goto repeat;
|
|
|
|
}
|
2010-04-27 08:56:47 +02:00
|
|
|
|
2004-05-28 17:07:03 +02:00
|
|
|
/* get field width */
|
|
|
|
field_width = -1;
|
|
|
|
if (is_digit(*fmt))
|
|
|
|
field_width = skip_atoi(&fmt);
|
|
|
|
else if (*fmt == '*') {
|
|
|
|
++fmt;
|
|
|
|
/* it's the next argument */
|
|
|
|
field_width = va_arg(args, int);
|
|
|
|
if (field_width < 0) {
|
|
|
|
field_width = -field_width;
|
|
|
|
flags |= LEFT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the precision */
|
|
|
|
precision = -1;
|
|
|
|
if (*fmt == '.') {
|
2010-04-27 08:56:47 +02:00
|
|
|
++fmt;
|
2004-05-28 17:07:03 +02:00
|
|
|
if (is_digit(*fmt))
|
|
|
|
precision = skip_atoi(&fmt);
|
|
|
|
else if (*fmt == '*') {
|
|
|
|
++fmt;
|
|
|
|
/* it's the next argument */
|
|
|
|
precision = va_arg(args, int);
|
|
|
|
}
|
|
|
|
if (precision < 0)
|
|
|
|
precision = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the conversion qualifier */
|
|
|
|
qualifier = -1;
|
2012-05-15 22:28:07 +02:00
|
|
|
if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'z') {
|
2004-05-28 17:07:03 +02:00
|
|
|
qualifier = *fmt;
|
|
|
|
++fmt;
|
2004-10-16 10:38:58 +02:00
|
|
|
if (*fmt == 'l') {
|
|
|
|
qualifier = 'L';
|
|
|
|
++fmt;
|
|
|
|
}
|
2004-05-28 17:07:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* default base */
|
|
|
|
base = 10;
|
|
|
|
|
|
|
|
switch (*fmt) {
|
|
|
|
case 'c':
|
|
|
|
if (!(flags & LEFT))
|
|
|
|
while (--field_width > 0)
|
|
|
|
tx_byte(' '), count++;
|
|
|
|
tx_byte((unsigned char) va_arg(args, int)), count++;
|
|
|
|
while (--field_width > 0)
|
|
|
|
tx_byte(' '), count++;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 's':
|
|
|
|
s = va_arg(args, char *);
|
|
|
|
if (!s)
|
|
|
|
s = "<NULL>";
|
|
|
|
|
|
|
|
len = strnlen(s, precision);
|
|
|
|
|
|
|
|
if (!(flags & LEFT))
|
|
|
|
while (len < field_width--)
|
|
|
|
tx_byte(' '), count++;
|
|
|
|
for (i = 0; i < len; ++i)
|
|
|
|
tx_byte(*s++), count++;
|
|
|
|
while (len < field_width--)
|
|
|
|
tx_byte(' '), count++;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
if (field_width == -1) {
|
|
|
|
field_width = 2*sizeof(void *);
|
|
|
|
flags |= ZEROPAD;
|
|
|
|
}
|
|
|
|
count += number(tx_byte,
|
|
|
|
(unsigned long) va_arg(args, void *), 16,
|
|
|
|
field_width, precision, flags);
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case 'n':
|
2004-10-16 10:38:58 +02:00
|
|
|
if (qualifier == 'L') {
|
|
|
|
long long *ip = va_arg(args, long long *);
|
|
|
|
*ip = count;
|
|
|
|
} else if (qualifier == 'l') {
|
2004-05-28 17:07:03 +02:00
|
|
|
long * ip = va_arg(args, long *);
|
|
|
|
*ip = count;
|
|
|
|
} else {
|
|
|
|
int * ip = va_arg(args, int *);
|
|
|
|
*ip = count;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
|
|
|
|
case '%':
|
|
|
|
tx_byte('%'), count++;
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* integer number formats - set up the flags and "break" */
|
|
|
|
case 'o':
|
|
|
|
base = 8;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'X':
|
|
|
|
flags |= LARGE;
|
|
|
|
case 'x':
|
|
|
|
base = 16;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'd':
|
|
|
|
case 'i':
|
|
|
|
flags |= SIGN;
|
|
|
|
case 'u':
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
tx_byte('%'), count++;
|
|
|
|
if (*fmt)
|
|
|
|
tx_byte(*fmt), count++;
|
|
|
|
else
|
|
|
|
--fmt;
|
|
|
|
continue;
|
|
|
|
}
|
2004-10-16 10:38:58 +02:00
|
|
|
if (qualifier == 'L') {
|
|
|
|
num = va_arg(args, unsigned long long);
|
|
|
|
} else if (qualifier == 'l') {
|
2004-05-28 17:07:03 +02:00
|
|
|
num = va_arg(args, unsigned long);
|
2012-05-15 22:28:07 +02:00
|
|
|
} else if (qualifier == 'z') {
|
|
|
|
num = va_arg(args, size_t);
|
2004-10-16 10:38:58 +02:00
|
|
|
} else if (qualifier == 'h') {
|
2004-05-28 17:07:03 +02:00
|
|
|
num = (unsigned short) va_arg(args, int);
|
|
|
|
if (flags & SIGN)
|
|
|
|
num = (short) num;
|
2004-10-16 10:38:58 +02:00
|
|
|
} else if (flags & SIGN) {
|
2004-05-28 17:07:03 +02:00
|
|
|
num = va_arg(args, int);
|
2004-10-16 10:38:58 +02:00
|
|
|
} else {
|
2004-05-28 17:07:03 +02:00
|
|
|
num = va_arg(args, unsigned int);
|
2004-10-16 10:38:58 +02:00
|
|
|
}
|
2004-05-28 17:07:03 +02:00
|
|
|
count += number(tx_byte, num, base, field_width, precision, flags);
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|