tiny-printf.c 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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>

16 17 18 19
struct printf_info {
	char *bf;	/* Digit buffer */
	char zs;	/* non-zero if a digit has been written */
	char *outstr;	/* Next output position for sprintf() */
20

21 22 23
	/* Output a character */
	void (*putc)(struct printf_info *info, char ch);
};
S
Simon Glass 已提交
24

25
void putc_normal(struct printf_info *info, char ch)
26
{
27
	putc(ch);
28 29
}

30
static void out(struct printf_info *info, char c)
31
{
32
	*info->bf++ = c;
33 34
}

35 36 37 38 39 40 41 42
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)
43 44 45
{
	unsigned char dgt = 0;

46 47
	while (*num >= div) {
		*num -= div;
48 49 50
		dgt++;
	}

51 52
	if (info->zs || dgt > 0)
		out_dgt(info, dgt);
53 54
}

55
int _vprintf(struct printf_info *info, const char *fmt, va_list va)
56 57 58
{
	char ch;
	char *p;
59 60 61
	unsigned int num;
	char buf[12];
	unsigned int div;
62 63 64

	while ((ch = *(fmt++))) {
		if (ch != '%') {
65
			info->putc(info, ch);
66
		} else {
S
Simon Glass 已提交
67 68
			bool lz = false;
			int width = 0;
69 70 71 72 73 74 75 76

			ch = *(fmt++);
			if (ch == '0') {
				ch = *(fmt++);
				lz = 1;
			}

			if (ch >= '0' && ch <= '9') {
S
Simon Glass 已提交
77
				width = 0;
78
				while (ch >= '0' && ch <= '9') {
S
Simon Glass 已提交
79
					width = (width * 10) + ch - '0';
80 81 82
					ch = *fmt++;
				}
			}
83 84 85
			info->bf = buf;
			p = info->bf;
			info->zs = 0;
86 87

			switch (ch) {
S
Simon Glass 已提交
88
			case '\0':
89 90 91 92 93 94
				goto abort;
			case 'u':
			case 'd':
				num = va_arg(va, unsigned int);
				if (ch == 'd' && (int)num < 0) {
					num = -(int)num;
95
					out(info, '-');
96
				}
S
Simon Glass 已提交
97
				if (!num) {
98
					out_dgt(info, 0);
S
Simon Glass 已提交
99 100
				} else {
					for (div = 1000000000; div; div /= 10)
101
						div_out(info, &num, div);
S
Simon Glass 已提交
102
				}
103 104 105
				break;
			case 'x':
				num = va_arg(va, unsigned int);
S
Simon Glass 已提交
106
				if (!num) {
107
					out_dgt(info, 0);
S
Simon Glass 已提交
108 109
				} else {
					for (div = 0x10000000; div; div /= 0x10)
110
						div_out(info, &num, div);
S
Simon Glass 已提交
111
				}
112 113
				break;
			case 'c':
114
				out(info, (char)(va_arg(va, int)));
115 116 117 118 119
				break;
			case 's':
				p = va_arg(va, char*);
				break;
			case '%':
120
				out(info, '%');
121 122 123 124
			default:
				break;
			}

125 126 127
			*info->bf = 0;
			info->bf = p;
			while (*info->bf++ && width > 0)
S
Simon Glass 已提交
128 129
				width--;
			while (width-- > 0)
130
				info->putc(info, lz ? '0' : ' ');
131 132
			if (p) {
				while ((ch = *p++))
133
					info->putc(info, ch);
134
			}
135 136 137 138 139 140
		}
	}

abort:
	return 0;
}
141

142 143
int vprintf(const char *fmt, va_list va)
{
144 145 146 147
	struct printf_info info;

	info.putc = putc_normal;
	return _vprintf(&info, fmt, va);
148 149
}

150 151
int printf(const char *fmt, ...)
{
152 153
	struct printf_info info;

154 155 156
	va_list va;
	int ret;

157
	info.putc = putc_normal;
158
	va_start(va, fmt);
159
	ret = _vprintf(&info, fmt, va);
S
Simon Glass 已提交
160 161 162 163 164
	va_end(va);

	return ret;
}

165
static void putc_outstr(struct printf_info *info, char ch)
S
Simon Glass 已提交
166
{
167
	*info->outstr++ = ch;
S
Simon Glass 已提交
168 169
}

M
Marek Vasut 已提交
170
int sprintf(char *buf, const char *fmt, ...)
S
Simon Glass 已提交
171
{
172
	struct printf_info info;
S
Simon Glass 已提交
173 174 175 176
	va_list va;
	int ret;

	va_start(va, fmt);
177 178 179
	info.outstr = buf;
	info.putc = putc_outstr;
	ret = _vprintf(&info, fmt, va);
180
	va_end(va);
181
	*info.outstr = '\0';
182 183 184

	return ret;
}
M
Marek Vasut 已提交
185 186 187 188

/* Note that size is ignored */
int snprintf(char *buf, size_t size, const char *fmt, ...)
{
189
	struct printf_info info;
M
Marek Vasut 已提交
190 191 192 193
	va_list va;
	int ret;

	va_start(va, fmt);
194 195 196
	info.outstr = buf;
	info.putc = putc_outstr;
	ret = _vprintf(&info, fmt, va);
M
Marek Vasut 已提交
197
	va_end(va);
198
	*info.outstr = '\0';
M
Marek Vasut 已提交
199 200 201

	return ret;
}
S
Simon Glass 已提交
202 203 204 205 206 207 208 209 210

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();
}