aboutsummaryrefslogtreecommitdiff
path: root/src/kprintf
diff options
context:
space:
mode:
authorXavier Del Campo Romero <xavi92@disroot.org>2025-07-07 13:22:53 +0200
committerXavier Del Campo Romero <xavi92@disroot.org>2025-07-25 14:16:41 +0200
commit14f60e4fd65c42f126eaee7e09cb4251c167c6ed (patch)
tree313b5e16d7d99cf1518c953e2efe5e5fc920dfbf /src/kprintf
parent48a61c16eaa6dcfc75d00dba302537ce1492db98 (diff)
wiptty
Diffstat (limited to 'src/kprintf')
-rw-r--r--src/kprintf/CMakeLists.txt20
-rw-r--r--src/kprintf/include/kprintf.h24
-rw-r--r--src/kprintf/src/CMakeLists.txt17
-rw-r--r--src/kprintf/src/kprintf.c96
4 files changed, 157 insertions, 0 deletions
diff --git a/src/kprintf/CMakeLists.txt b/src/kprintf/CMakeLists.txt
new file mode 100644
index 0000000..b847938
--- /dev/null
+++ b/src/kprintf/CMakeLists.txt
@@ -0,0 +1,20 @@
+# wanix, a Unix-like operating system for WebAssembly applications.
+# Copyright (C) 2025 Xavier Del Campo Romero
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+add_library(kprintf ${src})
+add_subdirectory(src)
+target_include_directories(kprintf PUBLIC include PRIVATE private_include)
+target_link_libraries(kprintf PUBLIC c PRIVATE aio)
diff --git a/src/kprintf/include/kprintf.h b/src/kprintf/include/kprintf.h
new file mode 100644
index 0000000..606adc1
--- /dev/null
+++ b/src/kprintf/include/kprintf.h
@@ -0,0 +1,24 @@
+/*
+ * wanix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef KPRINTF_H
+#define KPRINTF_H
+
+int kprintf(const char *fmt, ...);
+
+#endif
diff --git a/src/kprintf/src/CMakeLists.txt b/src/kprintf/src/CMakeLists.txt
new file mode 100644
index 0000000..abd972e
--- /dev/null
+++ b/src/kprintf/src/CMakeLists.txt
@@ -0,0 +1,17 @@
+# wanix, a Unix-like operating system for WebAssembly applications.
+# Copyright (C) 2025 Xavier Del Campo Romero
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+target_sources(kprintf PRIVATE kprintf.c)
diff --git a/src/kprintf/src/kprintf.c b/src/kprintf/src/kprintf.c
new file mode 100644
index 0000000..d5504fe
--- /dev/null
+++ b/src/kprintf/src/kprintf.c
@@ -0,0 +1,96 @@
+/*
+ * wanix, a Unix-like operating system for WebAssembly applications.
+ * Copyright (C) 2025 Xavier Del Campo Romero
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include <kprintf.h>
+#include <aio.h>
+#include <fcntl.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <string.h>
+#include <time.h>
+
+static void writechar(const char c, void *const args)
+{
+ static char buf[128];
+ static size_t bufi;
+ struct fs_fd *fd = args;
+ struct aio *aio = NULL;
+
+ buf[bufi++] = c;
+
+ if (c == '\n' || bufi >= sizeof buf - 1)
+ {
+ const struct fs_write w =
+ {
+ .buf = buf,
+ .fd = fd,
+ .n = bufi
+ };
+
+ if (!(aio = aio_write(&w, NULL)))
+ goto end;
+
+ struct aio_poll p = (const struct aio_poll){.aio = aio};
+
+ aio_poll(&p, -1);
+ bufi = 0;
+ }
+
+end:
+ aio_free(aio);
+}
+
+int kprintf(const char *const fmt, ...)
+{
+ va_list ap;
+ struct fs_fd fd;
+ struct timespec ts;
+ const struct fs_open o =
+ {
+ .fd = &fd,
+ .path = "/dev/tty",
+ .mode = O_WRONLY
+ };
+
+ struct aio *aio_op = NULL;
+
+ va_start(ap, fmt);
+
+ if (clock_gettime(CLOCK_REALTIME, &ts))
+ goto failure;
+
+ if (!(aio_op = aio_open(&o, NULL)))
+ goto failure;
+
+ struct aio_poll p = {.aio = aio_op};
+
+ if (aio_poll(&p, -1) < 0
+ || p.error
+ || fctprintf(writechar, &fd, "[%lld.%03ld] ", ts.tv_sec,
+ ts.tv_nsec / 1000000) < 0
+ || fctvprintf(writechar, &fd, fmt, ap) < 0)
+ goto failure;
+
+ va_end(ap);
+ return 0;
+
+failure:
+ aio_free(aio_op);
+ va_end(ap);
+ return -1;
+}