aboutsummaryrefslogtreecommitdiff
path: root/source/config.c
diff options
context:
space:
mode:
authornodist <kevin.comas.git@gmail.com>2026-09-20 17:10:21 -0400
committernodist <kevin.comas.git@gmail.com>2026-09-20 17:10:21 -0400
commit260e50d627aea535abcd9d9902c1b4ae56da4c54 (patch)
tree91a5ec00e02e67c78437040a798f2fa3140e57a0 /source/config.c
parent4e19e6ab1bd24466d9dcb0d8eea54a7e3d84d576 (diff)
add config for io and thread
Diffstat (limited to 'source/config.c')
-rw-r--r--source/config.c95
1 files changed, 83 insertions, 12 deletions
diff --git a/source/config.c b/source/config.c
index 7fa97d0..461197d 100644
--- a/source/config.c
+++ b/source/config.c
@@ -4,9 +4,13 @@
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 = {}
+ .flags = 0,
+ .io = {
+ .queue_pow = KPL_IO_QUEUE_POW_DEFAULT
+ },
+ .thread = {
+ .count = -1
+ }
};
void kpl_config_init(kpl_config *config) {
@@ -20,19 +24,70 @@ typedef struct {
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 int32_t kpl_arg_match_eval(const char *match_str, const int32_t match_str_index, kpl_config *config) {
+ (void) match_str;
+ (void) match_str_index;
+ config->flags |= KPL_CONFIG_FLAG_EVAL;
+ return KPL_EXIT_CODE_SUCCESS;
+}
+
+static int32_t kpl_arg_match_repl(const char *match_str, const int32_t match_str_index, kpl_config *config) {
+ (void) match_str;
+ (void) match_str_index;
+ config->flags |= KPL_CONFIG_FLAG_REPL;
+ return KPL_EXIT_CODE_SUCCESS;
+}
+
+static int32_t kpl_arg_match_io_queue_pow(const char *match_str, const int32_t match_str_index, kpl_config *config) {
+ int32_t queue_pow = atoi(match_str + match_str_index);
+ if (queue_pow < KPL_IO_QUEUE_POW_MIN || queue_pow > KPL_IO_QUEUE_POW_MAX)
+ return KPL_EXIT_CODE_ERROR;
+ config->io.queue_pow = queue_pow;
+ return KPL_EXIT_CODE_SUCCESS;
+}
+
+static int32_t kpl_arg_match_thread_count(const char *match_str, const int32_t match_str_index, kpl_config *config) {
+ int32_t thread_count = atoi(match_str + match_str_index);
+ if (!thread_count)
+ return KPL_EXIT_CODE_ERROR;
+ config->thread.count = thread_count;
+ return KPL_EXIT_CODE_SUCCESS;
+}
+
+static int32_t kpl_arg_match_help(const char *match_str, const int32_t match_str_index, kpl_config *config);
+
static kpl_arg_match arg_match_array[] = {
{
.match_str = "--module-extension=",
- .help_message="[WORD], Change the module extension for imported files (default .kpl)",
+ .help_message="[WORD], Change the module extension for imports (default .kpl)",
.arg_match_fn = kpl_arg_match_module_extension
},
+ {
+ .match_str = "--eval",
+ .help_message = " [STRING...], Evaluate next argument(s) as modules",
+ .arg_match_fn = kpl_arg_match_eval
+ },
+ {
+ .match_str = "--io-queue-pow=",
+ .help_message = "[NUMBER], Power of two for calculating the size of the io_uring queue",
+ .arg_match_fn = kpl_arg_match_io_queue_pow
+ },
+ {
+ .match_str = "--thread-count=",
+ .help_message = "[NUMBER], Number of running threads, minimum is 1",
+ .arg_match_fn = kpl_arg_match_thread_count
+ },
+ {
+ .match_str = "--repl",
+ .help_message = ", Start (R)ead, (E)valuate, (P)rint, (L)oop",
+ .arg_match_fn = kpl_arg_match_repl
+ },
{ .match_str = "--help", .help_message = ", Show help", .arg_match_fn = kpl_arg_match_help },
{ .match_str = NULL, .help_message = NULL, .arg_match_fn = NULL }
};
@@ -40,8 +95,7 @@ static kpl_arg_match arg_match_array[] = {
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)
+ printf("\nUsage " KPL_COLOR(BOLD) "%s" KPL_COLOR(RESET) KPL_COLOR(WHITE) " [OPTIONS] [MODULE]" 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;
@@ -73,8 +127,12 @@ bool kpl_config_valid_module_exension(kpl_config *config, const char *module_nam
int32_t kpl_config_parse_args(kpl_config *config, const int32_t argc, const char *argv[]) {
config->argc = argc;
config->argv = argv;
+ if (argc == 1) {
+ printf(KPL_COLOR(LIGHT_RED) "\nNo Module Specified\n" KPL_COLOR(RESET));
+ return kpl_arg_match_help(NULL, 0, config);
+ }
int32_t arg_index = 1;
- while (arg_index < argc) {
+ while (arg_index < argc && !config->flags) {
int32_t match_str_index = 0;
const char *match_str_arg = argv[arg_index];
const kpl_arg_match *match_array = arg_match_array;
@@ -83,8 +141,12 @@ int32_t kpl_config_parse_args(kpl_config *config, const int32_t argc, const char
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)
+ if (exit_status != KPL_EXIT_CODE_SUCCESS) {
+ if (exit_status == KPL_EXIT_CODE_ERROR)
+ printf(KPL_COLOR(LIGHT_RED) "\nInvalid Option: " KPL_COLOR(BOLD) "%s\n" KPL_COLOR(RESET),
+ match_str_arg);
return exit_status;
+ }
break;
}
match_str_index = 0;
@@ -94,14 +156,23 @@ int32_t kpl_config_parse_args(kpl_config *config, const int32_t argc, const char
break;
arg_index++;
}
- if (arg_index == argc)
+ config->main_module_arg_index = arg_index;
+ if (config->flags) {
+ if ((config->flags & KPL_CONFIG_FLAG_EVAL) && argc - arg_index < 1) {
+ printf(KPL_COLOR(LIGHT_RED) "\nInvalid EVAL\n" KPL_COLOR(RESET));
+ return kpl_arg_match_help(NULL, 0, config);
+ }
+ if ((config->flags & KPL_CONFIG_FLAG_REPL) && argc != arg_index) {
+ printf(KPL_COLOR(LIGHT_RED) "\nInvalid REPL\n" KPL_COLOR(RESET));
+ return kpl_arg_match_help(NULL, 0, config);
+ }
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_COLOR(WHITE) "[OPTION]/[MODULE]: " 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;
}