aboutsummaryrefslogtreecommitdiff
path: root/src/kernel/lib/stdio
diff options
context:
space:
mode:
authorMarvin Borner2020-05-28 23:02:51 +0200
committerMarvin Borner2020-05-28 23:02:51 +0200
commitf600c79559892b73b019102478af501cf71fe6a4 (patch)
treedb5bca15169c9990e3c6ab52568d526d1e25bac5 /src/kernel/lib/stdio
parent731ea56513bd199597a905eb1937e67cbd762b32 (diff)
Added vsprintf support for serial connections
Diffstat (limited to 'src/kernel/lib/stdio')
-rw-r--r--src/kernel/lib/stdio/debug.c47
-rw-r--r--src/kernel/lib/stdio/sprintf.c58
-rw-r--r--src/kernel/lib/stdio/vprintf.c51
-rw-r--r--src/kernel/lib/stdio/vsprintf.c63
4 files changed, 77 insertions, 142 deletions
diff --git a/src/kernel/lib/stdio/debug.c b/src/kernel/lib/stdio/debug.c
index 3acb987..879329e 100644
--- a/src/kernel/lib/stdio/debug.c
+++ b/src/kernel/lib/stdio/debug.c
@@ -1,8 +1,7 @@
#include <io/io.h>
+#include <lib/lib.h>
+#include <lib/stdio.h>
#include <lib/stdlib.h>
-#include <lib/string.h>
-#include <memory/alloc.h>
-#include <stdarg.h>
#include <stdint.h>
void serial_print(const char *data)
@@ -13,44 +12,10 @@ void serial_print(const char *data)
void serial_vprintf(const char *fmt, va_list args)
{
- u8 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 *);
- serial_print(str);
- readyToFormat = 0;
- } else if (buff == 'x') {
- char *p = htoa((u32)va_arg(args, int));
- serial_print(p);
- free(p);
- readyToFormat = 0;
- } else if (buff == 'd') {
- char *p = itoa(va_arg(args, int));
- serial_print(p);
- free(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);
- }
- }
+ char buf[1024];
+ memset(buf, 0, 1024);
+ vsprintf(buf, fmt, args);
+ serial_print(buf);
}
void serial_printf(const char *fmt, ...)
diff --git a/src/kernel/lib/stdio/sprintf.c b/src/kernel/lib/stdio/sprintf.c
index 0fb6eb5..8492363 100644
--- a/src/kernel/lib/stdio/sprintf.c
+++ b/src/kernel/lib/stdio/sprintf.c
@@ -1,63 +1,11 @@
-#include <lib/stdlib.h>
+#include <lib/stdio.h>
#include <stdarg.h>
-#include <stdint.h>
-
-static void append(char *dest, char *src, int index)
-{
- for (int i = index; i < strlen(src) + index; i++)
- dest[i] = src[i - index];
- dest[index + strlen(src)] = 0;
-}
int sprintf(char *str, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
-
- u8 ready_to_format = 0;
-
- int i = 0;
- char buf = 0;
- char format_buffer[20] = "\0";
-
- for (; *fmt; fmt++) {
- if (ready_to_format) {
- ready_to_format = 0;
-
- if (*fmt == '%') {
- str[i] = '%';
- continue;
- }
-
- buf = *fmt;
- if (buf == 's') {
- char *string = va_arg(args, char *);
- append(str, string, i);
- i = strlen(str);
- } else if (buf == 'x') {
- itoa_base((u32)va_arg(args, int), format_buffer, 16);
- append(str, format_buffer, i);
- i = strlen(str);
- } else if (buf == 'd') {
- itoa_base(va_arg(args, int), format_buffer, 10);
- append(str, format_buffer, i);
- i = strlen(str);
- } else if (buf == 'c') {
- str[i] = (char)va_arg(args, int);
- i++;
- }
- } else {
- if (*fmt == '%')
- ready_to_format = 1;
- else {
- str[i] = *fmt;
- i++;
- }
- }
-
- format_buffer[0] = '\0';
- }
-
+ int ret = vsprintf(str, fmt, args);
va_end(args);
- return strlen(str);
+ return ret;
} \ No newline at end of file
diff --git a/src/kernel/lib/stdio/vprintf.c b/src/kernel/lib/stdio/vprintf.c
index 4c0c432..2351d39 100644
--- a/src/kernel/lib/stdio/vprintf.c
+++ b/src/kernel/lib/stdio/vprintf.c
@@ -1,54 +1,13 @@
#include <lib/stdio.h>
#include <lib/stdlib.h>
-#include <lib/string.h>
-#include <memory/alloc.h>
-#include <stdarg.h>
#include <stdint.h>
-void _puts(const char *data)
-{
- for (u32 i = 0; i < strlen(data); i++)
- putch(data[i]);
-}
-
+// TODO: Use global formatting function
void vprintf(const char *fmt, va_list args)
{
- u8 readyToFormat = 0;
-
- char buff = 0;
-
- for (; *fmt; fmt++) {
- if (readyToFormat) {
- if (*fmt == '%') {
- putch('%');
- readyToFormat = 0;
- continue;
- }
+ char buf[1024];
+ vsprintf(buf, fmt, args);
- buff = *fmt;
- if (buff == 's') {
- const char *str = va_arg(args, const char *);
- _puts(str);
- readyToFormat = 0;
- } else if (buff == 'x') {
- char *p = htoa((u32)va_arg(args, int));
- _puts(p);
- free(p);
- readyToFormat = 0;
- } else if (buff == 'd') {
- char *p = itoa(va_arg(args, int));
- _puts(p);
- free(p);
- readyToFormat = 0;
- } else if (buff == 'c') {
- putch((char)va_arg(args, int));
- readyToFormat = 0;
- }
- } else {
- if (*fmt == '%')
- readyToFormat = 1;
- else
- putch(*fmt);
- }
- }
+ for (u32 i = 0; i < strlen(buf); i++)
+ putch(buf[i]);
} \ No newline at end of file
diff --git a/src/kernel/lib/stdio/vsprintf.c b/src/kernel/lib/stdio/vsprintf.c
new file mode 100644
index 0000000..6584ed1
--- /dev/null
+++ b/src/kernel/lib/stdio/vsprintf.c
@@ -0,0 +1,63 @@
+#include <lib/stdlib.h>
+#include <stdarg.h>
+#include <stdint.h>
+
+static void append(char *dest, char *src, int index)
+{
+ for (int i = index; i < strlen(src) + index; i++)
+ dest[i] = src[i - index];
+ dest[index + strlen(src)] = 0;
+}
+
+int vsprintf(char *str, const char *fmt, va_list args)
+{
+ u8 ready_to_format = 0;
+
+ int i = 0;
+ char buf = 0;
+ char format_buffer[20] = "\0";
+
+ for (; *fmt; fmt++) {
+ if (ready_to_format) {
+ ready_to_format = 0;
+
+ if (*fmt == '%') {
+ str[i] = '%';
+ continue;
+ }
+
+ buf = *fmt;
+ if (buf == 's') {
+ char *string = va_arg(args, char *);
+ append(str, string, i);
+ i = strlen(str);
+ } else if (buf == 'x') {
+ itoa_base((u32)va_arg(args, int), format_buffer, 16);
+ append(str, format_buffer, i);
+ i = strlen(str);
+ } else if (buf == 'd') {
+ itoa_base(va_arg(args, int), format_buffer, 10);
+ append(str, format_buffer, i);
+ i = strlen(str);
+ } else if (buf == 'o') {
+ itoa_base(va_arg(args, int), format_buffer, 8);
+ append(str, format_buffer, i);
+ i = strlen(str);
+ } else if (buf == 'c') {
+ str[i] = (char)va_arg(args, int);
+ i++;
+ }
+ } else {
+ if (*fmt == '%')
+ ready_to_format = 1;
+ else {
+ str[i] = *fmt;
+ i++;
+ }
+ }
+
+ format_buffer[0] = '\0';
+ }
+
+ return strlen(str);
+} \ No newline at end of file