apps: Switch to C++
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
apio 2023-03-29 17:56:56 +02:00
parent ef01f187c3
commit 724dab636c
Signed by: apio
GPG Key ID: B8A7D06E42258954
10 changed files with 37 additions and 23 deletions

View File

@ -1,14 +1,14 @@
function(luna_app SOURCE_FILE APP_NAME) function(luna_app SOURCE_FILE APP_NAME)
add_executable(${APP_NAME} ${SOURCE_FILE}) 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) add_dependencies(${APP_NAME} libc)
target_include_directories(${APP_NAME} PRIVATE ${LUNA_BASE}/usr/include) target_include_directories(${APP_NAME} PRIVATE ${LUNA_BASE}/usr/include)
install(TARGETS ${APP_NAME} DESTINATION ${LUNA_ROOT}/initrd/bin) install(TARGETS ${APP_NAME} DESTINATION ${LUNA_ROOT}/initrd/bin)
endfunction() endfunction()
luna_app(init.c init) luna_app(init.cpp init)
luna_app(cat.c cat) luna_app(cat.cpp cat)
luna_app(edit.c edit) luna_app(edit.cpp edit)
luna_app(sh.c sh) luna_app(sh.cpp sh)
luna_app(date.c date) luna_app(date.cpp date)
luna_app(ls.c ls) luna_app(ls.cpp ls)

View File

@ -1,3 +1,5 @@
#include <luna/Vector.h>
#include <errno.h> #include <errno.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
@ -6,36 +8,31 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
static char** split_command_into_argv(const char* cmd) static Result<Vector<char*>> split_command_into_argv(const char* cmd)
{ {
size_t argc = 1;
char* str = strdup(cmd); char* str = strdup(cmd);
char** arr = calloc(sizeof(char*), argc);
Vector<char*> result;
char* segment = strtok(str, " \n"); char* segment = strtok(str, " \n");
arr[argc - 1] = segment; TRY(result.try_append(segment));
if (segment == NULL) return arr;
argc++; if (segment == NULL) return result;
arr = realloc(arr, sizeof(char*) * argc);
for (;;) for (;;)
{ {
char* segment = strtok(NULL, " \n"); char* segment = strtok(NULL, " \n");
arr[argc - 1] = segment; TRY(result.try_append(segment));
if (segment == NULL) break; if (segment == NULL) return result;
argc++;
arr = realloc(arr, sizeof(char*) * argc);
} }
return arr; return result;
} }
static char* join_path(const char* str1, const char* str2) 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); strlcpy(buf, str1, strlen(str1) + 1);
strncat(buf, str2, strlen(str2)); strncat(buf, str2, strlen(str2));
return buf; return buf;
@ -96,9 +93,18 @@ int main()
if (child == 0) 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; if (argv[0] == NULL) return 0;
sh_execvp(argv); sh_execvp(argv.data());
perror(argv[0]); perror(argv[0]);
return 1; return 1;
} }

View File

@ -62,3 +62,4 @@ add_custom_command(
) )
file(WRITE "${LUNA_BASE}/usr/lib/libm.a" "INPUT(libc.a)") file(WRITE "${LUNA_BASE}/usr/lib/libm.a" "INPUT(libc.a)")
file(WRITE "${LUNA_BASE}/usr/lib/libstdc++.a" "INPUT(libc.a)")

View File

@ -27,6 +27,7 @@ set(FREESTANDING_SOURCES
set(SOURCES set(SOURCES
${FREESTANDING_SOURCES} ${FREESTANDING_SOURCES}
src/Check.cpp src/Check.cpp
src/CppABI.cpp
) )
add_library(luna-freestanding ${FREESTANDING_SOURCES}) add_library(luna-freestanding ${FREESTANDING_SOURCES})

6
libluna/src/CppABI.cpp Normal file
View File

@ -0,0 +1,6 @@
extern "C"
{
void __gxx_personality_v0()
{
}
}