From 4e19e6ab1bd24466d9dcb0d8eea54a7e3d84d576 Mon Sep 17 00:00:00 2001 From: nodist Date: Sun, 20 Sep 2026 13:22:50 -0400 Subject: kpl config set module extension --- include/kpl.h | 17 ++++++++ include/kpl/io.h | 10 +++++ include/kpl/macro.h | 2 +- include/kpl/memory.h | 8 ++++ include/kpl/thread.h | 10 +++++ source/config.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++ source/kpl.c | 11 ++++-- test/config.c | 21 ++++++++++ test/test.c | 26 ++++++------- vm.sh | 6 +-- 10 files changed, 198 insertions(+), 20 deletions(-) create mode 100644 include/kpl/io.h create mode 100644 include/kpl/memory.h create mode 100644 include/kpl/thread.h create mode 100644 source/config.c create mode 100644 test/config.c diff --git a/include/kpl.h b/include/kpl.h index adf366f..15933ee 100644 --- a/include/kpl.h +++ b/include/kpl.h @@ -4,3 +4,20 @@ // All headers in one file for including #include "kpl/macro.h" +#include "kpl/io.h" +#include "kpl/thread.h" +#include "kpl/memory.h" + +typedef struct { + int32_t argc, main_module_arg_index; + const char **argv, *module_extension; + kpl_config_io io; + kpl_config_thread thread; + kpl_config_memory memory; +} kpl_config; + +void kpl_config_init(kpl_config *config); + +bool kpl_config_valid_module_exension(kpl_config *config, const char *module_name); + +int32_t kpl_config_parse_args(kpl_config *config, const int32_t argc, const char *argv[]); diff --git a/include/kpl/io.h b/include/kpl/io.h new file mode 100644 index 0000000..e6ea690 --- /dev/null +++ b/include/kpl/io.h @@ -0,0 +1,10 @@ + +#pragma once + +// Io through io_uring + +#include "kpl/macro.h" + +typedef struct { + int32_t queue_pow; +} kpl_config_io; diff --git a/include/kpl/macro.h b/include/kpl/macro.h index f682a30..63699d0 100644 --- a/include/kpl/macro.h +++ b/include/kpl/macro.h @@ -6,4 +6,4 @@ #include "color.h" #include "type.h" -#define KPL_TODO(MESSAGE) printf(KPL_COLOR2(BOLD, LIGHT_YELLOW) "TODO " KPL_COLOR(RESET) KPL_COLOR(BOLD) "%s" KPL_COLOR(RESET) ":" KPL_COLOR(LIGHT_BLUE) "%s" KPL_COLOR(RESET) ":" KPL_COLOR(LIGHT_MAGENTA) "%d" KPL_COLOR(RESET) "::" KPL_COLOR(LIGHT_YELLOW) "%s\n" KPL_COLOR(RESET), __FILE__, __FUNCTION__, __LINE__, MESSAGE) +#define KPL_PRINT_TODO(MESSAGE) printf(KPL_COLOR2(BOLD, LIGHT_YELLOW) "TODO " KPL_COLOR(RESET) KPL_COLOR(BOLD) "%s" KPL_COLOR(RESET) ":" KPL_COLOR(LIGHT_BLUE) "%s" KPL_COLOR(RESET) ":" KPL_COLOR(LIGHT_MAGENTA) "%d" KPL_COLOR(RESET) "::" KPL_COLOR(LIGHT_YELLOW) "%s\n" KPL_COLOR(RESET), __FILE__, __FUNCTION__, __LINE__, MESSAGE) diff --git a/include/kpl/memory.h b/include/kpl/memory.h new file mode 100644 index 0000000..9b9a843 --- /dev/null +++ b/include/kpl/memory.h @@ -0,0 +1,8 @@ + +#pragma once + +// Kpl memory pools and garbage collection + +typedef struct { + +} kpl_config_memory; diff --git a/include/kpl/thread.h b/include/kpl/thread.h new file mode 100644 index 0000000..99db80f --- /dev/null +++ b/include/kpl/thread.h @@ -0,0 +1,10 @@ + +#pragma once + +// Kpl threads, allocators ad atomic queues + +#include "kpl/macro.h" + +typedef struct { + int32_t thread_count; +} kpl_config_thread; diff --git a/source/config.c b/source/config.c new file mode 100644 index 0000000..7fa97d0 --- /dev/null +++ b/source/config.c @@ -0,0 +1,107 @@ + +#include "kpl.h" + +static kpl_config default_config = { + .argc = -1, .main_module_arg_index = -1, + .argv = NULL, .module_extension = ".kpl", + .io = { .queue_pow = 12 }, + .thread = { .thread_count = -1 }, + .memory = {} +}; + +void kpl_config_init(kpl_config *config) { + memcpy(config, &default_config, sizeof(kpl_config)); +} + +typedef int32_t kpl_arg_match_fn(const char *match_str, const int32_t match_str_index, kpl_config *config); + +typedef struct { + const char *match_str, *help_message; + kpl_arg_match_fn *arg_match_fn; +} kpl_arg_match; + +static int32_t kpl_arg_match_help(const char *match_str, const int32_t match_str_index, kpl_config *config); + +static int32_t kpl_arg_match_module_extension(const char *match_str, const int32_t match_str_index, kpl_config *config) { + config->module_extension = match_str + match_str_index; + return KPL_EXIT_CODE_SUCCESS; +} + +static kpl_arg_match arg_match_array[] = { + { + .match_str = "--module-extension=", + .help_message="[WORD], Change the module extension for imported files (default .kpl)", + .arg_match_fn = kpl_arg_match_module_extension + }, + { .match_str = "--help", .help_message = ", Show help", .arg_match_fn = kpl_arg_match_help }, + { .match_str = NULL, .help_message = NULL, .arg_match_fn = NULL } +}; + +static int32_t kpl_arg_match_help(const char *match_str, const int32_t match_str_index, kpl_config *config) { + (void) match_str; + (void) match_str_index; + (void) config; + printf("\nUsage " KPL_COLOR(BOLD) "%s" KPL_COLOR(RESET) KPL_COLOR(WHITE) " [OPTIONS] [FILE]" KPL_COLOR(RESET) + KPL_COLOR(BOLD) "%s\n\n" KPL_COLOR(RESET), config->argv[0], config->module_extension); + printf("Where " KPL_COLOR(WHITE) "[OPTIONS]" KPL_COLOR(RESET) " are:\n"); + const kpl_arg_match *match_array = arg_match_array; + while (match_array->match_str) { + printf(KPL_COLOR(LIGHT_YELLOW) "\t%s" KPL_COLOR(RESET) "%s\n", + match_array->match_str, match_array->help_message); + match_array++; + } + putchar('\n'); + return KPL_EXIT_CODE_HELP; +} + +static bool kpl_valid_match_str_index(const char *match_str_array, const char *match_str_arg, int32_t match_str_index) { + return match_str_array[match_str_index] && + match_str_array[match_str_index] == match_str_arg[match_str_index]; +} + +static bool kpl_valid_match_str(const char *match_str_array, const char *match_str_arg, int32_t match_str_index) { + return !match_str_array[match_str_index] && (!match_str_arg[match_str_index] || + (match_str_arg[match_str_index] && match_str_array[match_str_index - 1] == '=')); +} + +bool kpl_config_valid_module_exension(kpl_config *config, const char *module_name) { + size_t name_length = strlen(module_name), module_extension_length = strlen(config->module_extension); + return name_length > module_extension_length && + strcmp(module_name + name_length - module_extension_length, config->module_extension) == 0; +} + +int32_t kpl_config_parse_args(kpl_config *config, const int32_t argc, const char *argv[]) { + config->argc = argc; + config->argv = argv; + int32_t arg_index = 1; + while (arg_index < argc) { + int32_t match_str_index = 0; + const char *match_str_arg = argv[arg_index]; + const kpl_arg_match *match_array = arg_match_array; + while (match_array->match_str) { + while (kpl_valid_match_str_index(match_array->match_str, match_str_arg, match_str_index)) + match_str_index++; + if (kpl_valid_match_str(match_array->match_str, match_str_arg, match_str_index)) { + int32_t exit_status = match_array->arg_match_fn(match_str_arg, match_str_index, config); + if (exit_status != KPL_EXIT_CODE_SUCCESS) + return exit_status; + break; + } + match_str_index = 0; + match_array++; + } + if (!match_array->match_str) + break; + arg_index++; + } + if (arg_index == argc) + return KPL_EXIT_CODE_SUCCESS; + if (!kpl_config_valid_module_exension(config, argv[arg_index])) { + printf(KPL_COLOR(LIGHT_RED) "\nInvalid " KPL_COLOR(RESET) + KPL_COLOR(WHITE) "[OPTION]/[FILE]: " KPL_COLOR(RESET) + KPL_COLOR2(BOLD, LIGHT_RED) "%s\n" KPL_COLOR(RESET), argv[arg_index]); + return kpl_arg_match_help(NULL, 0, config); + } + config->main_module_arg_index = arg_index; + return KPL_EXIT_CODE_SUCCESS; +} diff --git a/source/kpl.c b/source/kpl.c index 05d9250..aacbd5c 100644 --- a/source/kpl.c +++ b/source/kpl.c @@ -2,8 +2,13 @@ #include "kpl.h" #ifdef KPL_MAIN -int32_t main(void) { - KPL_TODO("MAIN PROGRAM"); - return 1; +int32_t main(const int32_t argc, const char *argv[]) { + int32_t exit_status = KPL_EXIT_CODE_SUCCESS; + kpl_config config; + kpl_config_init(&config); + if ((exit_status = kpl_config_parse_args(&config, argc, argv))) + return exit_status; + KPL_PRINT_TODO("MAIN PROGRAM"); + return exit_status; } #endif diff --git a/test/config.c b/test/config.c new file mode 100644 index 0000000..8a1dc64 --- /dev/null +++ b/test/config.c @@ -0,0 +1,21 @@ + +#include "kpl_test.h" + +KPL_TEST(config_show_help) { + kpl_config config; + const int32_t argc = 2; + const char *argv[] = { "./kpl", "--help", "not_used" }; + kpl_config_init(&config); + int32_t code = kpl_config_parse_args(&config, argc, argv); + KPL_ASSERT(code == KPL_EXIT_CODE_HELP, "Invalid help exit code"); +} + +KPL_TEST(config_change_module_extension) { + kpl_config config; + const int32_t argc = 3; + const char *argv[] = { "./kpl", "--module-extension=.l", "./file.l" }; + kpl_config_init(&config); + int32_t code = kpl_config_parse_args(&config, argc, argv); + KPL_ASSERT(code == KPL_EXIT_CODE_SUCCESS, "Invalid exit code"); + KPL_ASSERT(strcmp(config.module_extension, ".l") == 0, "Invalid new module extension"); +} diff --git a/test/test.c b/test/test.c index 97af6a4..a4a6281 100644 --- a/test/test.c +++ b/test/test.c @@ -99,16 +99,16 @@ static int32_t kpl_test_list(void) { return KPL_EXIT_CODE_HELP; } -static int32_t kpl_test_run_by_name(int32_t argc, char *argv[], const int32_t named_arg_position) { - if (named_arg_position == argc) { +static int32_t kpl_test_run_by_name(const int32_t argc, const char *argv[], const int32_t named_arg_index) { + if (named_arg_index == argc) { printf(KPL_COLOR(LIGHT_RED) "\nNo Tests Specified\n" KPL_COLOR(RESET)); return kpl_test_help(argv[0]); } int32_t tests_ran = 0; kpl_test *_test = NULL; while ((_test = kpl_test_next())) - for (int32_t test_name_position = named_arg_position; test_name_position < argc; test_name_position++) - if (strcmp(argv[test_name_position], _test->test_name) == 0) { + for (int32_t test_name_index = named_arg_index; test_name_index < argc; test_name_index++) + if (strcmp(argv[test_name_index], _test->test_name) == 0) { kpl_test_run(_test); tests_ran++; } @@ -120,12 +120,12 @@ static int32_t kpl_test_run_by_name(int32_t argc, char *argv[], const int32_t na return kpl_test_error_list(); } -int32_t main(int32_t argc, char *argv[]) { - int32_t arg_position = 1; - while (arg_position < argc) { - if (argv[arg_position][0] != '-' || !argv[arg_position][1] || argv[arg_position][2]) +int32_t main(const int32_t argc, const char *argv[]) { + int32_t arg_index = 1; + while (arg_index < argc) { + if (argv[arg_index][0] != '-' || !argv[arg_index][1] || argv[arg_index][2]) break; - switch (argv[arg_position][1]) { + switch (argv[arg_index][1]) { case 'a': return kpl_test_run_all(); case 'h': @@ -133,17 +133,17 @@ int32_t main(int32_t argc, char *argv[]) { case 'l': return kpl_test_list(); default: - return kpl_test_run_by_name(argc, argv, arg_position); + return kpl_test_run_by_name(argc, argv, arg_index); } - arg_position++; + arg_index++; } - return kpl_test_run_by_name(argc, argv, arg_position); + return kpl_test_run_by_name(argc, argv, arg_index); } KPL_TEST(test_pass_by_default) { // Pass by default // Uncomment below to f ail - // KPL_FAIL("Failure Message"); + // KPL_FAIL("Failure message"); } KPL_TEST(test_counting) { diff --git a/vm.sh b/vm.sh index a10215a..f9da586 100755 --- a/vm.sh +++ b/vm.sh @@ -221,7 +221,7 @@ vm_sync() { vm_test_cmd() { local MACHINE=$1 - vm_cmd $MACHINE "make kpl_test; arch; ./kpl_test -a" + vm_cmd $MACHINE "make kpl_test && arch && ./kpl_test -a" } vm_test() { @@ -247,7 +247,7 @@ vm_test() { echo "-----------------------" fi done - return $(( ${TEST_STATUS[$X64]} | ${TEST_STATUS[$A64]} )) + return (( ${TEST_STATUS[$X64]} | ${TEST_STATUS[$A64]} )) } vm_stop() { @@ -349,7 +349,7 @@ vm_help() { else echo "]" fi - ((I++)) + (( I++ )) done return 2 } -- cgit v1.2.3