diff options
Diffstat (limited to 'src/kernel/lib')
-rw-r--r-- | src/kernel/lib/stdio.h | 6 | ||||
-rw-r--r-- | src/kernel/lib/stdio/debug.c | 59 | ||||
-rw-r--r-- | src/kernel/lib/stdio/vprintf.c | 8 | ||||
-rw-r--r-- | src/kernel/lib/stdlib/htoa.c | 4 | ||||
-rw-r--r-- | src/kernel/lib/stdlib/itoa.c | 4 |
5 files changed, 71 insertions, 10 deletions
diff --git a/src/kernel/lib/stdio.h b/src/kernel/lib/stdio.h index d102dd6..8299dea 100644 --- a/src/kernel/lib/stdio.h +++ b/src/kernel/lib/stdio.h @@ -9,8 +9,10 @@ char *readline(); void writec(char c); -void vprintf(const char *format, va_list args); +void vprintf(const char *fmt, va_list args); -void printf(const char *format, ...); +void printf(const char *fmt, ...); + +void serial_printf(const char *fmt, ...); #endif diff --git a/src/kernel/lib/stdio/debug.c b/src/kernel/lib/stdio/debug.c new file mode 100644 index 0000000..d66ee59 --- /dev/null +++ b/src/kernel/lib/stdio/debug.c @@ -0,0 +1,59 @@ +#include <stdarg.h> +#include <stdint.h> +#include <kernel/lib/string.h> +#include <kernel/lib/stdlib.h> +#include <kernel/io/io.h> + +void _write_serial(const char *data) +{ + for (size_t i = 0; i < strlen(data); i++) serial_put(data[i]); +} + +void serial_printf(const char *fmt, ...) +{ + va_list args; + va_start(args, fmt); + + uint8_t readyToFormat = 0; + + char buff = 0; + + for (; *fmt; fmt++) { + if (readyToFormat) { + if (*fmt == '%') { + serial_put('%'); + readyToFormat = 0; + continue; + } + + buff = *fmt; + if (buff == 's') { + const char *str = va_arg(args, const char*); + _write_serial(str); + readyToFormat = 0; + } else if (buff == 'x') { + char *p = htoa((uint32_t) va_arg(args, int)); + _write_serial(p); + kfree(p); + readyToFormat = 0; + } else if (buff == 'd') { + char *p = itoa(va_arg(args, int)); + _write_serial(p); + kfree(p); + readyToFormat = 0; + } else if (buff == 'c') { + serial_put((char) va_arg(args, int)); + readyToFormat = 0; + } + } else { + if (*fmt == '%') + readyToFormat = 1; + else + serial_put(*fmt); + } + } + + serial_put('\n'); + + va_end(args); +}
\ No newline at end of file diff --git a/src/kernel/lib/stdio/vprintf.c b/src/kernel/lib/stdio/vprintf.c index 885f584..81b7be0 100644 --- a/src/kernel/lib/stdio/vprintf.c +++ b/src/kernel/lib/stdio/vprintf.c @@ -4,7 +4,7 @@ #include <kernel/lib/string.h> #include <kernel/lib/stdlib.h> -void __writes(const char *data) +void _writes(const char *data) { for (size_t i = 0; i < strlen(data); i++) writec(data[i]); } @@ -26,16 +26,16 @@ void vprintf(const char *fmt, va_list args) buff = *fmt; if (buff == 's') { const char *str = va_arg(args, const char*); - __writes(str); + _writes(str); readyToFormat = 0; } else if (buff == 'x') { char *p = htoa((uint32_t) va_arg(args, int)); - __writes(p); + _writes(p); kfree(p); readyToFormat = 0; } else if (buff == 'd') { char *p = itoa(va_arg(args, int)); - __writes(p); + _writes(p); kfree(p); readyToFormat = 0; } else if (buff == 'c') { diff --git a/src/kernel/lib/stdlib/htoa.c b/src/kernel/lib/stdlib/htoa.c index 85bd750..660e26c 100644 --- a/src/kernel/lib/stdlib/htoa.c +++ b/src/kernel/lib/stdlib/htoa.c @@ -2,7 +2,7 @@ #include <kernel/lib/string.h> #include <kernel/lib/stdlib.h> -static const char __HTOA_TABLE[] = "0123456789ABCDEF"; +static const char HTOA_TABLE[] = "0123456789ABCDEF"; char *htoa(uint32_t n) { @@ -10,7 +10,7 @@ char *htoa(uint32_t n) int i = 0; while (n) { - ret[i++] = __HTOA_TABLE[n & 0xF]; + ret[i++] = HTOA_TABLE[n & 0xF]; n >>= 4; } diff --git a/src/kernel/lib/stdlib/itoa.c b/src/kernel/lib/stdlib/itoa.c index 897fd55..67273b3 100644 --- a/src/kernel/lib/stdlib/itoa.c +++ b/src/kernel/lib/stdlib/itoa.c @@ -4,7 +4,7 @@ #include <kernel/lib/stdlib.h> #include <kernel/paging/paging.h> -static const char __ITOA_TABLE[] = "0123456789"; +static const char ITOA_TABLE[] = "0123456789"; char *itoa(int n) { @@ -27,7 +27,7 @@ char *itoa(int n) for (int i = 0; i < sz; i++) { int digit = (n % pow(10, i + 1)) / pow(10, i); - ret[i] = __ITOA_TABLE[digit]; + ret[i] = ITOA_TABLE[digit]; } ret[sz] = 0; |