-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathprintf.c
153 lines (143 loc) · 4.68 KB
/
printf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include "os.h"
/*
* ref: https://github.com/cccriscv/mini-riscv-os/blob/master/05-Preemptive/lib.c
*/
static int _vsnprintf(char *out, size_t n, const char *s, va_list v1) {
size_t pos = 0;
int is_format = 0;
int is_long = 0;
for (; *s; s++) {
if (is_format) {
switch (*s) {
case 'l': {
is_long = 1;
break;
}
case 'p': {
is_long = 1;
if (out && pos < n) {
out[pos] = '0';
}
++pos;
if (out && pos < n) {
out[pos] = 'x';
}
++pos;
} // don't break here!!!
case 'x': {
long num = is_long ? va_arg(v1, long) : va_arg(v1, int);
int hex_digits = 2 * (is_long ? sizeof(long) : sizeof(int)); // two hex numbers in one byte
for (int i = hex_digits - 1; i >= 0; i--) {
int digit = (num >> (i * 4)) & 0xF;
if (out && pos < n) {
out[pos] = digit < 10 ? '0' + digit : 'a' + digit - 10;
}
++pos;
}
is_format = 0;
is_long = 0;
break;
}
case 'X': {
long num = is_long ? va_arg(v1, long) : va_arg(v1, int);
int hex_digits = 2 * (is_long ? sizeof(long) : sizeof(int)); // two hex numbers in one byte
for (int i = hex_digits - 1; i >= 0; i--) {
int digit = (num >> (i * 4)) & 0xF;
if (out && pos < n) {
out[pos] = digit < 10 ? '0' + digit : 'A' + digit - 10;
}
++pos;
}
is_format = 0;
is_long = 0;
break;
}
case 'd': {
long num = is_long ? va_arg(v1, long) : va_arg(v1, int);
if (num < 0) {
num = -num;
if (out && pos < n) {
out[pos] = '-';
}
++pos;
}
int digits = 1;
for (int nn = num; nn /= 10; ++digits);
for (int i = digits - 1; i >= 0; --i) {
if (out && pos + i < n) {
out[pos + i] = '0' + (num % 10);
}
num /= 10;
}
pos += digits;
is_format = 0;
is_long = 0;
break;
}
case 'c': {
if (out && pos < n) {
out[pos] = (char)va_arg(v1, int);
}
++pos;
is_format = 0;
is_long = 0;
break;
}
case 's': {
const char *str = va_arg(v1, const char *);
while (*str) {
if (out && pos < n) {
out[pos] = *str;
}
++str;
++pos;
}
is_format = 0;
is_long = 0;
break;
}
default:
break;
}
}
else if (*s == '%') {
is_format = 1;
} else {
if (out && pos < n) {
out[pos] = *s;
}
++pos;
}
if (out && pos < n) {
out[pos] = 0;
}
}
return pos;
}
static char out_buf[1024]; // buffer for _vprintf
static int _vprintf(const char *s, va_list v1)
{
int len = 0;
len = _vsnprintf(NULL, -1, s, v1);
if (len > sizeof(out_buf) - 1) {
uart_puts("[ERROR] _vprintf(): output string size overflow.\r\n");
while (1) {}
}
_vsnprintf(out_buf, len + 1, s, v1); // add 1 for '\0'
uart_puts(out_buf);
return len;
}
int printf(const char *s, ...)
{
int ret = 0;
va_list v1; // pointer to the variable argument list
va_start(v1, s); // point v1 to the first element of s
ret = _vprintf(s, v1);
va_end(v1); // release v1
return ret;
}
void panic(char *s)
{
printf("[ERROR] panic: %s\n", s);
while (1) {}
}