From 724dab636cf28c21fa6742835c1585132567f91a Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 29 Mar 2023 17:56:56 +0200 Subject: [PATCH] apps: Switch to C++ --- apps/CMakeLists.txt | 14 +++++++------- apps/{cat.c => cat.cpp} | 0 apps/{date.c => date.cpp} | 0 apps/{edit.c => edit.cpp} | 0 apps/{init.c => init.cpp} | 0 apps/{ls.c => ls.cpp} | 0 apps/{sh.c => sh.cpp} | 38 ++++++++++++++++++++++---------------- libc/CMakeLists.txt | 1 + libluna/CMakeLists.txt | 1 + libluna/src/CppABI.cpp | 6 ++++++ 10 files changed, 37 insertions(+), 23 deletions(-) rename apps/{cat.c => cat.cpp} (100%) rename apps/{date.c => date.cpp} (100%) rename apps/{edit.c => edit.cpp} (100%) rename apps/{init.c => init.cpp} (100%) rename apps/{ls.c => ls.cpp} (100%) rename apps/{sh.c => sh.cpp} (71%) create mode 100644 libluna/src/CppABI.cpp diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index e86ebf70..cddffe67 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -1,14 +1,14 @@ function(luna_app SOURCE_FILE APP_NAME) add_executable(${APP_NAME} ${SOURCE_FILE}) - target_compile_options(${APP_NAME} PRIVATE -Os -Wall -Wextra -pedantic -Werror) + target_compile_options(${APP_NAME} PRIVATE -Os -Wall -Wextra -pedantic -Werror -Wno-write-strings) add_dependencies(${APP_NAME} libc) target_include_directories(${APP_NAME} PRIVATE ${LUNA_BASE}/usr/include) install(TARGETS ${APP_NAME} DESTINATION ${LUNA_ROOT}/initrd/bin) endfunction() -luna_app(init.c init) -luna_app(cat.c cat) -luna_app(edit.c edit) -luna_app(sh.c sh) -luna_app(date.c date) -luna_app(ls.c ls) +luna_app(init.cpp init) +luna_app(cat.cpp cat) +luna_app(edit.cpp edit) +luna_app(sh.cpp sh) +luna_app(date.cpp date) +luna_app(ls.cpp ls) diff --git a/apps/cat.c b/apps/cat.cpp similarity index 100% rename from apps/cat.c rename to apps/cat.cpp diff --git a/apps/date.c b/apps/date.cpp similarity index 100% rename from apps/date.c rename to apps/date.cpp diff --git a/apps/edit.c b/apps/edit.cpp similarity index 100% rename from apps/edit.c rename to apps/edit.cpp diff --git a/apps/init.c b/apps/init.cpp similarity index 100% rename from apps/init.c rename to apps/init.cpp diff --git a/apps/ls.c b/apps/ls.cpp similarity index 100% rename from apps/ls.c rename to apps/ls.cpp diff --git a/apps/sh.c b/apps/sh.cpp similarity index 71% rename from apps/sh.c rename to apps/sh.cpp index 01eeac38..c7948295 100644 --- a/apps/sh.c +++ b/apps/sh.cpp @@ -1,3 +1,5 @@ +#include + #include #include #include @@ -6,36 +8,31 @@ #include #include -static char** split_command_into_argv(const char* cmd) +static Result> split_command_into_argv(const char* cmd) { - size_t argc = 1; char* str = strdup(cmd); - char** arr = calloc(sizeof(char*), argc); + + Vector result; char* segment = strtok(str, " \n"); - arr[argc - 1] = segment; - if (segment == NULL) return arr; + TRY(result.try_append(segment)); - argc++; - arr = realloc(arr, sizeof(char*) * argc); + if (segment == NULL) return result; for (;;) { char* segment = strtok(NULL, " \n"); - arr[argc - 1] = segment; + TRY(result.try_append(segment)); - if (segment == NULL) break; - - argc++; - arr = realloc(arr, sizeof(char*) * argc); + if (segment == NULL) return result; } - return arr; + return result; } static char* join_path(const char* str1, const char* str2) { - char* buf = malloc(strlen(str1) + strlen(str2) + 1); + char* buf = (char*)malloc(strlen(str1) + strlen(str2) + 1); strlcpy(buf, str1, strlen(str1) + 1); strncat(buf, str2, strlen(str2)); return buf; @@ -96,9 +93,18 @@ int main() if (child == 0) { - char** argv = split_command_into_argv(command); + auto maybe_argv = split_command_into_argv(command); + if (maybe_argv.has_error()) + { + errno = maybe_argv.error(); + perror("failed to parse command"); + return 1; + } + + auto argv = maybe_argv.release_value(); + if (argv[0] == NULL) return 0; - sh_execvp(argv); + sh_execvp(argv.data()); perror(argv[0]); return 1; } diff --git a/libc/CMakeLists.txt b/libc/CMakeLists.txt index 85dd2249..36cfe3e4 100644 --- a/libc/CMakeLists.txt +++ b/libc/CMakeLists.txt @@ -62,3 +62,4 @@ add_custom_command( ) file(WRITE "${LUNA_BASE}/usr/lib/libm.a" "INPUT(libc.a)") +file(WRITE "${LUNA_BASE}/usr/lib/libstdc++.a" "INPUT(libc.a)") diff --git a/libluna/CMakeLists.txt b/libluna/CMakeLists.txt index 0a5cc9de..a59c7efe 100644 --- a/libluna/CMakeLists.txt +++ b/libluna/CMakeLists.txt @@ -27,6 +27,7 @@ set(FREESTANDING_SOURCES set(SOURCES ${FREESTANDING_SOURCES} src/Check.cpp + src/CppABI.cpp ) add_library(luna-freestanding ${FREESTANDING_SOURCES}) diff --git a/libluna/src/CppABI.cpp b/libluna/src/CppABI.cpp new file mode 100644 index 00000000..1026204c --- /dev/null +++ b/libluna/src/CppABI.cpp @@ -0,0 +1,6 @@ +extern "C" +{ + void __gxx_personality_v0() + { + } +}