2015-11-23 14:00:22 +08: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>
|
|
|
|
|
2016-08-05 11:58:14 +08:00
|
|
|
struct printf_info {
|
|
|
|
char *bf; /* Digit buffer */
|
|
|
|
char zs; /* non-zero if a digit has been written */
|
|
|
|
char *outstr; /* Next output position for sprintf() */
|
2015-11-23 14:00:22 +08:00
|
|
|
|
2016-08-05 11:58:14 +08:00
|
|
|
/* Output a character */
|
|
|
|
void (*putc)(struct printf_info *info, char ch);
|
|
|
|
};
|
2016-05-15 04:02:53 +08:00
|
|
|
|
2016-08-05 11:58:14 +08:00
|
|
|
void putc_normal(struct printf_info *info, char ch)
|
2015-11-23 14:00:22 +08:00
|
|
|
{
|
2016-08-05 11:58:14 +08:00
|
|
|
putc(ch);
|
2015-11-23 14:00:22 +08:00
|
|
|
}
|
|
|
|
|
2016-08-05 11:58:14 +08:00
|
|
|
static void out(struct printf_info *info, char c)
|
2015-11-23 14:00:22 +08:00
|
|
|
{
|
2016-08-05 11:58:14 +08:00
|
|
|
*info->bf++ = c;
|
2015-11-23 14:00:22 +08:00
|
|
|
}
|
|
|
|
|
2016-08-05 11:58:14 +08:00
|
|
|
static void out_dgt(struct printf_info *info, char dgt)
|
|
|
|
{
|
|
|
|
out(info, dgt + (dgt < 10 ? '0' : 'a' - 10));
|
|
|
|
info->zs = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void div_out(struct printf_info *info, unsigned int *num,
|
|
|
|
unsigned int div)
|
2015-11-23 14:00:22 +08:00
|
|
|
{
|
|
|
|
unsigned char dgt = 0;
|
|
|
|
|
2015-11-16 22:26:34 +08:00
|
|
|
while (*num >= div) {
|
|
|
|
*num -= div;
|
2015-11-23 14:00:22 +08:00
|
|
|
dgt++;
|
|
|
|
}
|
|
|
|
|
2016-08-05 11:58:14 +08:00
|
|
|
if (info->zs || dgt > 0)
|
|
|
|
out_dgt(info, dgt);
|
2015-11-23 14:00:22 +08:00
|
|
|
}
|
|
|
|
|
2016-08-05 11:58:14 +08:00
|
|
|
int _vprintf(struct printf_info *info, const char *fmt, va_list va)
|
2015-11-23 14:00:22 +08:00
|
|
|
{
|
|
|
|
char ch;
|
|
|
|
char *p;
|
2015-11-16 22:26:34 +08:00
|
|
|
unsigned int num;
|
|
|
|
char buf[12];
|
|
|
|
unsigned int div;
|
2015-11-23 14:00:22 +08:00
|
|
|
|
|
|
|
while ((ch = *(fmt++))) {
|
|
|
|
if (ch != '%') {
|
2016-08-05 11:58:14 +08:00
|
|
|
info->putc(info, ch);
|
2015-11-23 14:00:22 +08:00
|
|
|
} else {
|
2016-05-15 04:02:52 +08:00
|
|
|
bool lz = false;
|
|
|
|
int width = 0;
|
2015-11-23 14:00:22 +08:00
|
|
|
|
|
|
|
ch = *(fmt++);
|
|
|
|
if (ch == '0') {
|
|
|
|
ch = *(fmt++);
|
|
|
|
lz = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ch >= '0' && ch <= '9') {
|
2016-05-15 04:02:52 +08:00
|
|
|
width = 0;
|
2015-11-23 14:00:22 +08:00
|
|
|
while (ch >= '0' && ch <= '9') {
|
2016-05-15 04:02:52 +08:00
|
|
|
width = (width * 10) + ch - '0';
|
2015-11-23 14:00:22 +08:00
|
|
|
ch = *fmt++;
|
|
|
|
}
|
|
|
|
}
|
2016-08-05 11:58:14 +08:00
|
|
|
info->bf = buf;
|
|
|
|
p = info->bf;
|
|
|
|
info->zs = 0;
|
2015-11-23 14:00:22 +08:00
|
|
|
|
|
|
|
switch (ch) {
|
2016-05-15 04:02:52 +08:00
|
|
|
case '\0':
|
2015-11-23 14:00:22 +08:00
|
|
|
goto abort;
|
|
|
|
case 'u':
|
|
|
|
case 'd':
|
|
|
|
num = va_arg(va, unsigned int);
|
|
|
|
if (ch == 'd' && (int)num < 0) {
|
|
|
|
num = -(int)num;
|
2016-08-05 11:58:14 +08:00
|
|
|
out(info, '-');
|
2015-11-23 14:00:22 +08:00
|
|
|
}
|
2016-01-06 00:30:57 +08:00
|
|
|
if (!num) {
|
2016-08-05 11:58:14 +08:00
|
|
|
out_dgt(info, 0);
|
2016-01-06 00:30:57 +08:00
|
|
|
} else {
|
|
|
|
for (div = 1000000000; div; div /= 10)
|
2016-08-05 11:58:14 +08:00
|
|
|
div_out(info, &num, div);
|
2016-01-06 00:30:57 +08:00
|
|
|
}
|
2015-11-23 14:00:22 +08:00
|
|
|
break;
|
|
|
|
case 'x':
|
|
|
|
num = va_arg(va, unsigned int);
|
2016-01-06 00:30:57 +08:00
|
|
|
if (!num) {
|
2016-08-05 11:58:14 +08:00
|
|
|
out_dgt(info, 0);
|
2016-01-06 00:30:57 +08:00
|
|
|
} else {
|
|
|
|
for (div = 0x10000000; div; div /= 0x10)
|
2016-08-05 11:58:14 +08:00
|
|
|
div_out(info, &num, div);
|
2016-01-06 00:30:57 +08:00
|
|
|
}
|
2015-11-23 14:00:22 +08:00
|
|
|
break;
|
|
|
|
case 'c':
|
2016-08-05 11:58:14 +08:00
|
|
|
out(info, (char)(va_arg(va, int)));
|
2015-11-23 14:00:22 +08:00
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
p = va_arg(va, char*);
|
|
|
|
break;
|
|
|
|
case '%':
|
2016-08-05 11:58:14 +08:00
|
|
|
out(info, '%');
|
2015-11-23 14:00:22 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-08-05 11:58:14 +08:00
|
|
|
*info->bf = 0;
|
|
|
|
info->bf = p;
|
|
|
|
while (*info->bf++ && width > 0)
|
2016-05-15 04:02:52 +08:00
|
|
|
width--;
|
|
|
|
while (width-- > 0)
|
2016-08-05 11:58:14 +08:00
|
|
|
info->putc(info, lz ? '0' : ' ');
|
2015-12-29 20:22:46 +08:00
|
|
|
if (p) {
|
|
|
|
while ((ch = *p++))
|
2016-08-05 11:58:14 +08:00
|
|
|
info->putc(info, ch);
|
2015-12-29 20:22:46 +08:00
|
|
|
}
|
2015-11-23 14:00:22 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
abort:
|
|
|
|
return 0;
|
|
|
|
}
|
2015-12-05 06:27:37 +08:00
|
|
|
|
2016-06-11 03:03:34 +08:00
|
|
|
int vprintf(const char *fmt, va_list va)
|
|
|
|
{
|
2016-08-05 11:58:14 +08:00
|
|
|
struct printf_info info;
|
|
|
|
|
|
|
|
info.putc = putc_normal;
|
|
|
|
return _vprintf(&info, fmt, va);
|
2016-06-11 03:03:34 +08:00
|
|
|
}
|
|
|
|
|
2015-12-05 06:27:37 +08:00
|
|
|
int printf(const char *fmt, ...)
|
|
|
|
{
|
2016-08-05 11:58:14 +08:00
|
|
|
struct printf_info info;
|
|
|
|
|
2015-12-05 06:27:37 +08:00
|
|
|
va_list va;
|
|
|
|
int ret;
|
|
|
|
|
2016-08-05 11:58:14 +08:00
|
|
|
info.putc = putc_normal;
|
2015-12-05 06:27:37 +08:00
|
|
|
va_start(va, fmt);
|
2016-08-05 11:58:14 +08:00
|
|
|
ret = _vprintf(&info, fmt, va);
|
2016-05-15 04:02:53 +08:00
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-08-05 11:58:14 +08:00
|
|
|
static void putc_outstr(struct printf_info *info, char ch)
|
2016-05-15 04:02:53 +08:00
|
|
|
{
|
2016-08-05 11:58:14 +08:00
|
|
|
*info->outstr++ = ch;
|
2016-05-15 04:02:53 +08:00
|
|
|
}
|
|
|
|
|
2016-06-01 05:12:46 +08:00
|
|
|
int sprintf(char *buf, const char *fmt, ...)
|
2016-05-15 04:02:53 +08:00
|
|
|
{
|
2016-08-05 11:58:14 +08:00
|
|
|
struct printf_info info;
|
2016-05-15 04:02:53 +08:00
|
|
|
va_list va;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(va, fmt);
|
2016-08-05 11:58:14 +08:00
|
|
|
info.outstr = buf;
|
|
|
|
info.putc = putc_outstr;
|
|
|
|
ret = _vprintf(&info, fmt, va);
|
2015-12-05 06:27:37 +08:00
|
|
|
va_end(va);
|
2016-08-05 11:58:14 +08:00
|
|
|
*info.outstr = '\0';
|
2015-12-05 06:27:37 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2016-06-01 05:12:46 +08:00
|
|
|
|
|
|
|
/* Note that size is ignored */
|
|
|
|
int snprintf(char *buf, size_t size, const char *fmt, ...)
|
|
|
|
{
|
2016-08-05 11:58:14 +08:00
|
|
|
struct printf_info info;
|
2016-06-01 05:12:46 +08:00
|
|
|
va_list va;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
va_start(va, fmt);
|
2016-08-05 11:58:14 +08:00
|
|
|
info.outstr = buf;
|
|
|
|
info.putc = putc_outstr;
|
|
|
|
ret = _vprintf(&info, fmt, va);
|
2016-06-01 05:12:46 +08:00
|
|
|
va_end(va);
|
2016-08-05 11:58:14 +08:00
|
|
|
*info.outstr = '\0';
|
2016-06-01 05:12:46 +08:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2016-07-05 01:58:13 +08:00
|
|
|
|
|
|
|
void __assert_fail(const char *assertion, const char *file, unsigned line,
|
|
|
|
const char *function)
|
|
|
|
{
|
|
|
|
/* This will not return */
|
|
|
|
printf("%s:%u: %s: Assertion `%s' failed.", file, line, function,
|
|
|
|
assertion);
|
|
|
|
hang();
|
|
|
|
}
|