2015-11-23 06:00:22 +00:00
|
|
|
/*
|
|
|
|
* Tiny printf version for SPL
|
|
|
|
*
|
|
|
|
* Copied from:
|
|
|
|
* http://www.sparetimelabs.com/printfrevisited/printfrevisited.php
|
|
|
|
*
|
|
|
|
* Copyright (C) 2004,2008 Kustaa Nyholm
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: LGPL-2.1+
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <serial.h>
|
|
|
|
|
|
|
|
static char *bf;
|
|
|
|
static char zs;
|
|
|
|
|
2016-05-14 20:02:53 +00:00
|
|
|
/* Current position in sprintf() output string */
|
|
|
|
static char *outstr;
|
|
|
|
|
2015-11-23 06:00:22 +00:00
|
|
|
static void out(char c)
|
|
|
|
{
|
|
|
|
*bf++ = c;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void out_dgt(char dgt)
|
|
|
|
{
|
2015-11-16 14:26:34 +00:00
|
|
|
out(dgt + (dgt < 10 ? '0' : 'a' - 10));
|
2015-11-23 06:00:22 +00:00
|
|
|
zs = 1;
|
|
|
|
}
|
|
|
|
|
2015-11-16 14:26:34 +00:00
|
|
|
static void div_out(unsigned int *num, unsigned int div)
|
2015-11-23 06:00:22 +00:00
|
|
|
{
|
|
|
|
unsigned char dgt = 0;
|
|
|
|
|
2015-11-16 14:26:34 +00:00
|
|
|
while (*num >= div) {
|
|
|
|
*num -= div;
|
2015-11-23 06:00:22 +00:00
|
|
|
dgt++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (zs || dgt > 0)
|
|
|
|
out_dgt(dgt);
|
|
|
|
}
|
|
|
|
|
2016-05-14 20:02:53 +00:00
|
|
|
int _vprintf(const char *fmt, va_list va, void (*putc)(const char ch))
|
2015-11-23 06:00:22 +00:00
|
|
|
{
|
|
|
|
char ch;
|
|
|
|
char *p;
|
2015-11-16 14:26:34 +00:00
|
|
|
unsigned int num;
|
|
|
|
char buf[12];
|
|
|
|
unsigned int div;
|
2015-11-23 06:00:22 +00:00
|
|
|
|
|
|
|
while ((ch = *(fmt++))) {
|
|
|
|
if (ch != '%') {
|
|
|
|
putc(ch);
|
|
|
|
} else {
|
2016-05-14 20:02:52 +00:00
|
|
|
bool lz = false;
|
|
|
|
int width = 0;
|
2015-11-23 06:00:22 +00:00
|
|
|
|
|
|
|
ch = *(fmt++);
|
|
|
|
if (ch == '0') {
|
|
|
|
ch = *(fmt++);
|
|
|
|
lz = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch >= '0' && ch <= '9') {
|
2016-05-14 20:02:52 +00:00
|
|
|
width = 0;
|
2015-11-23 06:00:22 +00:00
|
|
|
while (ch >= '0' && ch <= '9') {
|
2016-05-14 20:02:52 +00:00
|
|
|
width = (width * 10) + ch - '0';
|
2015-11-23 06:00:22 +00:00
|
|
|
ch = *fmt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bf = buf;
|
|
|
|
p = bf;
|
|
|
|
zs = 0;
|
|
|
|
|
|
|
|
switch (ch) {
|
2016-05-14 20:02:52 +00:00
|
|
|
case '\0':
|
2015-11-23 06:00:22 +00:00
|
|
|
goto abort;
|
|
|
|
case 'u':
|
|
|
|
case 'd':
|
|
|
|
num = va_arg(va, unsigned int);
|
|
|
|
if (ch == 'd' && (int)num < 0) {
|
|
|
|
num = -(int)num;
|
|
|
|
out('-');
|
|
|
|
}
|
2016-01-05 16:30:57 +00:00
|
|
|
if (!num) {
|
|
|
|
out_dgt(0);
|
|
|
|
} else {
|
|
|
|
for (div = 1000000000; div; div /= 10)
|
|
|
|
div_out(&num, div);
|
|
|
|
}
|
2015-11-23 06:00:22 +00:00
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
num = va_arg(va, unsigned int);
|
2016-01-05 16:30:57 +00:00
|
|
|
if (!num) {
|
|
|
|
out_dgt(0);
|
|
|
|
} else {
|
|
|
|
for (div = 0x10000000; div; div /= 0x10)
|
|
|
|
div_out(&num, div);
|
|
|
|
}
|
2015-11-23 06:00:22 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
out((char)(va_arg(va, int)));
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
p = va_arg(va, char*);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
out('%');
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*bf = 0;
|
|
|
|
bf = p;
|
2016-05-14 20:02:52 +00:00
|
|
|
while (*bf++ && width > 0)
|
|
|
|
width--;
|
|
|
|
while (width-- > 0)
|
2015-11-23 06:00:22 +00:00
|
|
|
putc(lz ? '0' : ' ');
|
2015-12-29 12:22:46 +00:00
|
|
|
if (p) {
|
|
|
|
while ((ch = *p++))
|
|
|
|
putc(ch);
|
|
|
|
}
|
2015-11-23 06:00:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abort:
|
|
|
|
return 0;
|
|
|
|
}
|
2015-12-04 22:27:37 +00:00
|
|
|
|
|
|
|
int printf(const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(va, fmt);
|
2016-05-14 20:02:53 +00:00
|
|
|
ret = _vprintf(fmt, va, putc);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void putc_outstr(char ch)
|
|
|
|
{
|
|
|
|
*outstr++ = ch;
|
|
|
|
}
|
|
|
|
|
2016-05-31 21:12:46 +00:00
|
|
|
int sprintf(char *buf, const char *fmt, ...)
|
2016-05-14 20:02:53 +00:00
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(va, fmt);
|
|
|
|
outstr = buf;
|
|
|
|
ret = _vprintf(fmt, va, putc_outstr);
|
2015-12-04 22:27:37 +00:00
|
|
|
va_end(va);
|
2016-05-14 20:02:53 +00:00
|
|
|
*outstr = '\0';
|
2015-12-04 22:27:37 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2016-05-31 21:12:46 +00:00
|
|
|
|
|
|
|
/* Note that size is ignored */
|
|
|
|
int snprintf(char *buf, size_t size, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list va;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(va, fmt);
|
|
|
|
ret = sprintf(buf, fmt, va);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|