From 14f60e4fd65c42f126eaee7e09cb4251c167c6ed Mon Sep 17 00:00:00 2001 From: Xavier Del Campo Romero Date: Mon, 7 Jul 2025 13:22:53 +0200 Subject: wip --- src/kprintf/CMakeLists.txt | 20 +++++++++ src/kprintf/include/kprintf.h | 24 +++++++++++ src/kprintf/src/CMakeLists.txt | 17 ++++++++ src/kprintf/src/kprintf.c | 96 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 157 insertions(+) create mode 100644 src/kprintf/CMakeLists.txt create mode 100644 src/kprintf/include/kprintf.h create mode 100644 src/kprintf/src/CMakeLists.txt create mode 100644 src/kprintf/src/kprintf.c (limited to 'src/kprintf') 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 . + +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 . + */ + +#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 . + +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 . + */ + +#include +#include +#include +#include +#include +#include +#include + +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; +} -- cgit v1.2.3