|
|
@@ -34,7 +34,7 @@
|
|
|
|
|
|
#include <lib/string.h>
|
|
|
|
|
|
-#include <stdarg.h>
|
|
|
+#include <kernel/args.h>
|
|
|
|
|
|
|
|
|
|
|
|
@@ -88,15 +88,16 @@ __uint64_t printf(const char *s, ...) {
|
|
|
char state, c;
|
|
|
/* fmt_s only use in %s, but we cannot define variables in switch-case */
|
|
|
char *fmt_s;
|
|
|
- va_list values;
|
|
|
+
|
|
|
+ int argno;
|
|
|
|
|
|
int newlines;
|
|
|
|
|
|
- va_start(values, s);
|
|
|
state = 0;
|
|
|
newlines = 0;
|
|
|
|
|
|
- for (i = 0, l = 0; s[i]; i++) {
|
|
|
+ /* argno = 2 because first argument is 's' */
|
|
|
+ for (i = 0, l = 0, argno = 2; s[i]; i++) {
|
|
|
c = s[i];
|
|
|
/* check state */
|
|
|
if (!state) {
|
|
|
@@ -125,37 +126,51 @@ __uint64_t printf(const char *s, ...) {
|
|
|
/* it's a formater */
|
|
|
else {
|
|
|
if (c == 'd') {
|
|
|
- l += printint((int) va_arg(values, int), 10, 1);
|
|
|
+ l += printint((int) get_arg(int, argno), 10, 1);
|
|
|
+ argno++;
|
|
|
}
|
|
|
|
|
|
else if (c == 'x') {
|
|
|
/* `x` is 64-bits, I think it's */
|
|
|
- l += printint((long) va_arg(values, long), 16, 0);
|
|
|
+ l += printint((long) get_arg(long, argno), 16, 0);
|
|
|
+ argno++;
|
|
|
}
|
|
|
|
|
|
- else if (c == 'p')
|
|
|
+ else if (c == 'p') {
|
|
|
/* zeros at high bits should display, not like `%x` or `%d` */
|
|
|
- l += printptr((__uint64_t) va_arg(values, __uint64_t), 16);
|
|
|
+ l += printptr((__uint64_t) get_arg(__uint64_t, argno), 16);
|
|
|
+ argno++;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
/* put a byte */
|
|
|
- else if (c == 'b')
|
|
|
- l += printptr((__uint64_t) va_arg(values, __uint32_t), 2);
|
|
|
+ else if (c == 'b') {
|
|
|
+ l += printptr((__uint64_t) get_arg(__uint32_t, argno), 2);
|
|
|
+ argno++;
|
|
|
+ }
|
|
|
|
|
|
/* put a word */
|
|
|
- else if (c == 'w')
|
|
|
- l += printptr((__uint64_t) va_arg(values, __uint32_t), 4);
|
|
|
+ else if (c == 'w') {
|
|
|
+ l += printptr((__uint64_t) get_arg(__uint32_t, argno), 4);
|
|
|
+ argno++;
|
|
|
+ }
|
|
|
|
|
|
- else if (c == 'l')
|
|
|
- l += printptr((__uint64_t) va_arg(values, __uint32_t), 8);
|
|
|
+ else if (c == 'l') {
|
|
|
+ l += printptr((__uint64_t) get_arg(__uint32_t, argno), 8);
|
|
|
+ argno++;
|
|
|
+ }
|
|
|
|
|
|
- else if (c == 'q')
|
|
|
- l += printptr((__uint64_t) va_arg(values, __uint64_t), 16);
|
|
|
+ else if (c == 'q') {
|
|
|
+ l += printptr((__uint64_t) get_arg(__uint64_t, argno), 16);
|
|
|
+ argno++;
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
|
|
else if (c == 's') {
|
|
|
- fmt_s = (char *) va_arg(values, char *);
|
|
|
+ fmt_s = (char *) get_arg(char *, argno);
|
|
|
+ argno++;
|
|
|
+
|
|
|
if (!fmt_s)
|
|
|
fmt_s = "(null)";
|
|
|
|
|
|
@@ -169,7 +184,8 @@ __uint64_t printf(const char *s, ...) {
|
|
|
|
|
|
else if (c == 'c') {
|
|
|
/* `char` aligned to `int` in GCC */
|
|
|
- putc((char) va_arg(values, int));
|
|
|
+ putc((char) get_arg(int, argno));
|
|
|
+ argno++;
|
|
|
l++;
|
|
|
}
|
|
|
|
|
|
@@ -210,8 +226,6 @@ __uint64_t printf(const char *s, ...) {
|
|
|
|
|
|
update_cursor();
|
|
|
|
|
|
- va_end(values);
|
|
|
-
|
|
|
return i;
|
|
|
}
|
|
|
|