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-11-11 00:08:15 +0100
commit7861a52adf92a083bb2aed4c35f98d8035dce032 (patch)
tree28cd3c40e4c878f730f5df3c1d93bdf91af490c3 /src/kprintf
parent7fc48e9216ff809da5f8055a50b0be17628ef1df (diff)
downloadwnix-7861a52adf92a083bb2aed4c35f98d8035dce032.tar.gz
Setup project skeleton
Diffstat (limited to 'src/kprintf')
-rw-r--r--src/kprintf/CMakeLists.txt20
-rw-r--r--src/kprintf/include/kprintf.h38
-rw-r--r--src/kprintf/src/CMakeLists.txt26
-rw-r--r--src/kprintf/src/kprintf.c34
-rw-r--r--src/kprintf/src/ktprintf.c35
-rw-r--r--src/kprintf/src/ktvprintf.c80
-rw-r--r--src/kprintf/src/kvprintf.c26
-rw-r--r--src/kprintf/src/nwp_log.c32
-rw-r--r--src/kprintf/src/wip_log.c44
9 files changed, 335 insertions, 0 deletions
diff --git a/src/kprintf/CMakeLists.txt b/src/kprintf/CMakeLists.txt
new file mode 100644
index 0000000..fcc53c9
--- /dev/null
+++ b/src/kprintf/CMakeLists.txt
@@ -0,0 +1,20 @@
+# wnix, 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)
+add_subdirectory(src)
+target_include_directories(kprintf PUBLIC include PRIVATE private_include)
+target_link_libraries(kprintf PUBLIC c PRIVATE io nanowasm)
diff --git a/src/kprintf/include/kprintf.h b/src/kprintf/include/kprintf.h
new file mode 100644
index 0000000..fb0710f
--- /dev/null
+++ b/src/kprintf/include/kprintf.h
@@ -0,0 +1,38 @@
+/*
+ * wnix, 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
+
+#include <stdarg.h>
+#include <stdbool.h>
+
+#if __GNUC__
+#define KPRINTF_ATTR __attribute__ ((format (__printf__, 1, 2)))
+#define KTPRINTF_ATTR __attribute__ ((format (__printf__, 2, 3)))
+#else
+#define KPRINTF_ATTR
+#define KTPRINTF_ATTR
+#endif
+
+int kprintf(const char *fmt, ...) KPRINTF_ATTR;
+int ktprintf(bool time, const char *fmt, ...);
+int kvprintf(const char *fmt, va_list va);
+int ktvprintf(bool time, const char *fmt, va_list va);
+
+#endif
diff --git a/src/kprintf/src/CMakeLists.txt b/src/kprintf/src/CMakeLists.txt
new file mode 100644
index 0000000..6bdfbc7
--- /dev/null
+++ b/src/kprintf/src/CMakeLists.txt
@@ -0,0 +1,26 @@
+# wnix, 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
+ kvprintf.c
+ ktprintf.c
+ ktvprintf.c
+ nwp_log.c
+ wip_log.c
+)
+
+target_link_libraries(kprintf PRIVATE nanowasm wip)
diff --git a/src/kprintf/src/kprintf.c b/src/kprintf/src/kprintf.c
new file mode 100644
index 0000000..de90e59
--- /dev/null
+++ b/src/kprintf/src/kprintf.c
@@ -0,0 +1,34 @@
+/*
+ * wnix, 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 <io.h>
+#include <fcntl.h>
+#include <stdarg.h>
+#include <printf.h>
+
+int kprintf(const char *const fmt, ...)
+{
+ int ret;
+ va_list ap;
+
+ va_start(ap, fmt);
+ ret = kvprintf(fmt, ap);
+ va_end(ap);
+ return ret;
+}
diff --git a/src/kprintf/src/ktprintf.c b/src/kprintf/src/ktprintf.c
new file mode 100644
index 0000000..f619d54
--- /dev/null
+++ b/src/kprintf/src/ktprintf.c
@@ -0,0 +1,35 @@
+/*
+ * wnix, 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 <io.h>
+#include <fcntl.h>
+#include <stdarg.h>
+#include <stdbool.h>
+#include <printf.h>
+
+int ktprintf(const bool time, const char *const fmt, ...)
+{
+ int ret;
+ va_list ap;
+
+ va_start(ap, fmt);
+ ret = ktvprintf(time, fmt, ap);
+ va_end(ap);
+ return ret;
+}
diff --git a/src/kprintf/src/ktvprintf.c b/src/kprintf/src/ktvprintf.c
new file mode 100644
index 0000000..a3529c4
--- /dev/null
+++ b/src/kprintf/src/ktvprintf.c
@@ -0,0 +1,80 @@
+/*
+ * wnix, 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 <io.h>
+#include <fcntl.h>
+#include <stddef.h>
+#include <stdarg.h>
+#include <printf.h>
+#include <time.h>
+
+struct kvprintf
+{
+ struct fs_fd fd;
+ size_t n;
+};
+
+static void writechar(const char c, void *const args)
+{
+ static char buf[128];
+ static size_t bufi;
+ struct kvprintf *k = args;
+
+ buf[bufi++] = c;
+
+ if (c == '\n' || bufi >= sizeof buf)
+ {
+ const struct fs_write w =
+ {
+ .buf = buf,
+ .fd = &k->fd,
+ .n = bufi
+ };
+
+ io_write(&w);
+ bufi = 0;
+ }
+
+ k->n++;
+}
+
+int ktvprintf(const bool time, const char *const fmt, va_list ap)
+{
+ struct timespec ts;
+ struct kvprintf k = {0};
+ const struct fs_open o =
+ {
+ .fd = &k.fd,
+ .path = "/dev/tty",
+ .mode = O_WRONLY
+ };
+
+ if (time && clock_gettime(CLOCK_REALTIME, &ts))
+ return -1;
+
+ const long ms = ts.tv_nsec / 1000000;
+
+ if (io_open(&o)
+ || (time
+ && fctprintf(writechar, &k, "[%lld.%03ld] ", ts.tv_sec, ms) < 0)
+ || fctvprintf(writechar, &k, fmt, ap) < 0)
+ return -1;
+
+ return k.n;
+}
diff --git a/src/kprintf/src/kvprintf.c b/src/kprintf/src/kvprintf.c
new file mode 100644
index 0000000..69890cb
--- /dev/null
+++ b/src/kprintf/src/kvprintf.c
@@ -0,0 +1,26 @@
+/*
+ * wnix, 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 <stdarg.h>
+#include <stdbool.h>
+
+int kvprintf(const char *fmt, va_list va)
+{
+ return ktvprintf(true, fmt, va);
+}
diff --git a/src/kprintf/src/nwp_log.c b/src/kprintf/src/nwp_log.c
new file mode 100644
index 0000000..2db9969
--- /dev/null
+++ b/src/kprintf/src/nwp_log.c
@@ -0,0 +1,32 @@
+/*
+ * wnix, 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 <nanowasm/nw.h>
+#include <stdarg.h>
+
+int nwp_log(const char *const fmt, ...)
+{
+ int ret;
+ va_list ap;
+
+ va_start(ap, fmt);
+ ret = ktvprintf(false, fmt, ap);
+ va_end(ap);
+ return ret;
+}
diff --git a/src/kprintf/src/wip_log.c b/src/kprintf/src/wip_log.c
new file mode 100644
index 0000000..67f4f8f
--- /dev/null
+++ b/src/kprintf/src/wip_log.c
@@ -0,0 +1,44 @@
+/*
+ * wnix, 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 <wip/wip.h>
+#include <stdarg.h>
+
+
+#include <drv/ps1/bios.h>
+#include <dynstr.h>
+int wip_log(const char *const fmt, ...)
+{
+ int ret;
+ va_list ap;
+
+ va_start(ap, fmt);
+#if 1
+ ret = ktvprintf(false, fmt, ap);
+#else
+ struct dynstr d;
+ dynstr_init(&d);
+ if (dynstr_vappend(&d, fmt, ap)) ret = -1;
+ else
+ ret = Printf("%s", d.str);
+ dynstr_free(&d);
+#endif
+ va_end(ap);
+ return ret;
+}