libos: Add libos + very basic ArgumentParser
All checks were successful
continuous-integration/drone/push Build is passing

libluna but for stuff that interests only userspace, like an argument parser or files or stuff like that.
This commit is contained in:
apio 2023-03-29 18:27:02 +02:00
parent 724dab636c
commit a164dcc160
Signed by: apio
GPG Key ID: B8A7D06E42258954
13 changed files with 145 additions and 14 deletions

View File

@ -29,6 +29,7 @@ set(CMAKE_FIND_ROOT_PATH ${LUNA_ROOT}/toolchain/${LUNA_ARCH}-luna)
message(STATUS "Configuring Luna for ${LUNA_ARCH}")
add_subdirectory(libluna)
add_subdirectory(libos)
add_subdirectory(libc)
add_subdirectory(kernel)
add_subdirectory(apps)

View File

@ -8,7 +8,11 @@ endfunction()
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(edit.cpp edit)
target_link_libraries(edit PRIVATE os)
luna_app(ls.cpp ls)
target_link_libraries(ls PRIVATE os)

View File

@ -1,3 +1,5 @@
#include <os/ArgumentParser.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -5,17 +7,16 @@
int main(int argc, char** argv)
{
FILE* f;
StringView pathname;
if (argc < 2)
{
fprintf(stderr, "usage: %s [file]", argv[0]);
return 1;
}
ArgumentParser parser;
parser.add_positional_argument(pathname, "file", true);
parser.parse(argc, argv);
f = fopen(argv[1], "w");
f = fopen(pathname.chars(), "w");
if (!f)
{
perror(argv[1]);
perror(pathname.chars());
return 1;
}

View File

@ -1,3 +1,5 @@
#include <os/ArgumentParser.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
@ -5,12 +7,13 @@
int main(int argc, char** argv)
{
const char* pathname;
if (argc == 1) pathname = "/";
else
pathname = argv[1];
StringView pathname;
DIR* dp = opendir(pathname);
ArgumentParser parser;
parser.add_positional_argument(pathname, "directory", false, "/");
parser.parse(argc, argv);
DIR* dp = opendir(pathname.chars());
if (!dp)
{
perror("opendir");

View File

@ -103,6 +103,19 @@ template <typename T> class Vector
return move(m_data[m_size]);
}
Option<T> try_dequeue()
{
if (m_size == 0) return {};
T item = move(m_data[0]);
memmove(m_data, m_data + 1, m_size - 1);
m_size--;
return move(item);
}
const T& operator[](usize index) const
{
check(index < m_size);

27
libos/CMakeLists.txt Normal file
View File

@ -0,0 +1,27 @@
# The C++ standard library for OS operations (userspace only), so most things that have to do with calling the kernel, or functions that the kernel has no use for.
file(GLOB HEADERS include/os/*.h)
set(SOURCES
${HEADERS}
src/ArgumentParser.cpp
)
add_library(os ${SOURCES})
target_compile_options(os PRIVATE -Os -Wall -Wextra -Werror -Wvla)
target_compile_options(os PRIVATE -Wdisabled-optimization -Wformat=2 -Winit-self)
target_compile_options(os PRIVATE -Wmissing-include-dirs -Wswitch-default -Wcast-qual -Wundef)
target_compile_options(os PRIVATE -Wcast-align -Wwrite-strings -Wlogical-op -Wredundant-decls -Wshadow -Wconversion)
target_compile_options(os PRIVATE -fno-asynchronous-unwind-tables -fno-omit-frame-pointer -std=c++20 -fno-rtti -fno-exceptions)
target_include_directories(os PUBLIC include/)
target_include_directories(os PUBLIC ${LUNA_BASE}/usr/include)
if("${LUNA_ARCH}" MATCHES "x86_64")
target_compile_definitions(os PUBLIC ARCH_X86_64)
endif()
if(LUNA_DEBUG_SYMBOLS)
message(STATUS "Building libOS with debug symbols")
target_compile_options(os PRIVATE -ggdb)
target_compile_options(os-freestanding PRIVATE -ggdb)
endif()

View File

@ -0,0 +1,25 @@
#pragma once
#include <luna/StringView.h>
#include <luna/Vector.h>
class ArgumentParser
{
public:
ArgumentParser() = default;
Result<void> add_positional_argument(StringView& out, StringView name, bool required);
Result<void> add_positional_argument(StringView& out, StringView name, bool required, StringView fallback);
void parse(int argc, char* const* argv);
private:
struct PositionalArgument
{
StringView* out;
StringView name;
bool required;
StringView fallback;
};
Vector<PositionalArgument> m_positional_args;
};

View File

@ -0,0 +1,47 @@
#include <os/ArgumentParser.h>
#include <stdio.h>
#include <stdlib.h>
Result<void> ArgumentParser::add_positional_argument(StringView& out, StringView name, bool required)
{
PositionalArgument arg { &out, name, required, {} };
return m_positional_args.try_append(move(arg));
}
Result<void> ArgumentParser::add_positional_argument(StringView& out, StringView name, bool required,
StringView fallback)
{
PositionalArgument arg { &out, name, required, fallback };
return m_positional_args.try_append(move(arg));
}
void ArgumentParser::parse(int argc, char* const* argv)
{
StringView program_name = argv[0];
for (int i = 1; i < argc; i++)
{
StringView arg = argv[i];
Option<PositionalArgument> current = m_positional_args.try_dequeue();
if (!current.has_value())
{
fprintf(stderr, "%s: unused argument '%s'\n", program_name.chars(), arg.chars());
continue;
}
*current->out = arg;
}
// Loop through all remaining positional arguments.
for (const auto& arg : m_positional_args)
{
if (arg.required)
{
fprintf(stderr, "%s: required argument '%s' not provided\n", program_name.chars(), arg.name.chars());
exit(1);
}
else { *arg.out = arg.fallback; }
}
}

View File

@ -7,6 +7,8 @@ cd $LUNA_ROOT
SOURCES=($(find kernel/src -type f | grep -v "\.asm"))
SOURCES+=($(find libluna/src -type f))
SOURCES+=($(find libluna/include/luna -type f))
SOURCES+=($(find libos/src -type f))
SOURCES+=($(find libos/include/os -type f))
ALL_OK=1

View File

@ -8,6 +8,8 @@ cd $LUNA_ROOT
SOURCES=($(find kernel/src -type f | grep -v "\.asm" | grep -v "bootboot.h"))
SOURCES+=($(find libluna/src -type f))
SOURCES+=($(find libluna/include/luna -type f | grep -v "Types.h"))
SOURCES+=($(find libos/src -type f))
SOURCES+=($(find libos/include/os -type f))
SUCCESS=1

View File

@ -8,6 +8,8 @@ cd $LUNA_ROOT
mkdir -p $LUNA_BASE
mkdir -p $LUNA_BASE/usr/include
mkdir -p $LUNA_BASE/usr/include/luna
mkdir -p $LUNA_BASE/usr/include/os
cp --preserve=timestamps -RT libc/include/ $LUNA_BASE/usr/include
cp --preserve=timestamps -RT libluna/include/luna/ $LUNA_BASE/usr/include/luna
cp --preserve=timestamps -RT libos/include/os/ $LUNA_BASE/usr/include/os

View File

@ -8,6 +8,8 @@ cd $LUNA_ROOT
SOURCES=($(find kernel/src -type f | grep -v "\.asm" | grep -v "bootboot.h"))
SOURCES+=($(find libluna/src -type f))
SOURCES+=($(find libluna/include/luna -type f | grep -v "Types.h"))
SOURCES+=($(find libos/src -type f))
SOURCES+=($(find libos/include/os -type f))
for f in ${SOURCES[@]}
do

View File

@ -8,6 +8,8 @@ cd $LUNA_ROOT
SOURCES=($(find kernel/src -type f | grep -v "\.asm"))
SOURCES+=($(find libluna/src -type f))
SOURCES+=($(find libluna/include/luna -type f))
SOURCES+=($(find libos/src -type f))
SOURCES+=($(find libos/include/os -type f))
for f in ${SOURCES[@]}
do