diff options
| author | nodist <kevin.comas.git@gmail.com> | 2026-10-20 14:11:31 -0400 |
|---|---|---|
| committer | nodist <kevin.comas.git@gmail.com> | 2026-10-20 14:11:31 -0400 |
| commit | 689719e0a1f139e28e57a4df334d5fde354b3379 (patch) | |
| tree | 2b5e0d0c4aced4886ff3c67729adceba45cac3b0 /source/config.c | |
| parent | baa6f9cdea938c06435635f906b128f7940a59ec (diff) | |
config parse cli arguments
Diffstat (limited to 'source/config.c')
| -rw-r--r-- | source/config.c | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/source/config.c b/source/config.c new file mode 100644 index 0000000..f1aabf8 --- /dev/null +++ b/source/config.c @@ -0,0 +1,162 @@ + +#include "kpl.h" + +typedef kpl_exit_code kpl_config_option_parse_fn(kpl_env *env, int32_t arg_index, char *argv[]); + +typedef struct { + int32_t flags; + char match, *help_option_argument, *help_message; + kpl_config_option_parse_fn *parse_fn; +} kpl_config_option; + +#define KPL_CONFIG_MATCH(CHAR) (CHAR - 'a') + +static kpl_exit_code kpl_config_option_parse_eval(kpl_env *env, int32_t arg_index, char *argv[]) { + env->kpl_entry = argv[arg_index]; + env->flags |= KPL_ENV_FLAG_RUN_EVAL; + return KPL_EXIT_CODE_SUCCESS; +} + +static const kpl_config_option config_options[]; + +static kpl_exit_code kpl_config_option_parse_help(kpl_env *env, int32_t arg_index, char *argv[]) { + (void) env; + (void) arg_index; + printf(KPL_COLOR(LIGHT_GREEN) "Kevin's Programming Language" KPL_COLOR(RESET) " (" KPL_COLOR(LIGHT_CYAN) + "KPL" KPL_COLOR(RESET) ") Version: " KPL_COLOR(LIGHT_MAGENTA) KPL_VERSION KPL_COLOR(RESET) + " Website: " KPL_COLOR(LIGHT_BLUE) KPL_WEBSITE KPL_COLOR(RESET) "\n"); + printf("Usage:" KPL_COLOR(BOLD) " %s " KPL_COLOR(RESET) KPL_COLOR(LIGHT_YELLOW) "OPTIONS" KPL_COLOR(RESET) + KPL_COLOR(BOLD) " [-r|-e STRING|FILE" KPL_FILE_EXTENSION KPL_COLOR(RESET) "]\n", argv[0]); + for (int32_t option_index = KPL_CONFIG_MATCH('a'); option_index < KPL_CONFIG_MATCH('z'); option_index++) { + const kpl_config_option *option = &config_options[option_index]; + if (!option->match) + continue; + printf("\t" KPL_COLOR(LIGHT_YELLOW) "-%c" KPL_COLOR(RESET), option->match); + if (option->help_option_argument) + printf(KPL_COLOR(GREEN) " [%s]" KPL_COLOR(RESET), option->help_option_argument); + printf(", %s\n", option->help_message); + } + return KPL_EXIT_CODE_HELP; +} + +static kpl_exit_code kpl_config_option_parse_io_queue(kpl_env *env, int32_t arg_index, char *argv[]) { + int32_t io_uring_queue_size_pow2 = atoi(argv[arg_index]); + if (io_uring_queue_size_pow2 < KPL_IO_URING_QUEUE_SIZE_POW2_MIN || + io_uring_queue_size_pow2 > KPL_IO_URING_QUEUE_SIZE_POW2_MAX) { + printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid power of two for io_uring, must be between %d and %d\n" + KPL_COLOR(RESET), KPL_IO_URING_QUEUE_SIZE_POW2_MIN, KPL_IO_URING_QUEUE_SIZE_POW2_MAX); + return KPL_EXIT_CODE_ERROR; + } + env->io_uring_queue_size_pow2 = io_uring_queue_size_pow2; + return KPL_EXIT_CODE_SUCCESS; +} + +static kpl_exit_code kpl_config_option_parse_repl(kpl_env *env, int32_t arg_index, char *argv[]) { + (void) arg_index; + (void) argv; + env->flags |= KPL_ENV_FLAG_RUN_REPL; + return KPL_EXIT_CODE_SUCCESS; +} + +static kpl_exit_code kpl_config_option_parse_thread_count(kpl_env *env, int32_t arg_index, char *argv[]) { + int32_t thread_count = atoi(argv[arg_index]); + if (thread_count < KPL_THEAD_THREAD_COUNT_MIN || thread_count > KPL_THEAD_THREAD_COUNT_MAX) { + printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid thread count, must be between %d and %d\n" KPL_COLOR(RESET), + KPL_THEAD_THREAD_COUNT_MIN, KPL_THEAD_THREAD_COUNT_MAX); + return KPL_EXIT_CODE_ERROR; + } + env->thread_count = thread_count; + return KPL_EXIT_CODE_SUCCESS; +} + +static kpl_exit_code kpl_config_option_parse_version(kpl_env *env, int32_t arg_index, char *argv[]) { + (void) env; + (void) arg_index; + (void) argv; + printf("kpl version " KPL_VERSION "\n"); + return KPL_EXIT_CODE_HELP; +} + +static const kpl_config_option config_options[] = { + [KPL_CONFIG_MATCH('a')] = {}, + [KPL_CONFIG_MATCH('e')] = { + .match = 'e', + .help_option_argument = "STRING", + .help_message = "Evaluate KPL String", + .parse_fn = kpl_config_option_parse_eval + }, + [KPL_CONFIG_MATCH('h')] = { + .match = 'h', + .help_message = "Show Help", + .parse_fn = kpl_config_option_parse_help + }, + [KPL_CONFIG_MATCH('i')] = { + .match = 'i', + .help_option_argument = "NUMBER", + .help_message = "Power Of Two For io_uring Queue Size", + .parse_fn = kpl_config_option_parse_io_queue + }, + [KPL_CONFIG_MATCH('r')] = { + .match = 'r', + .help_message = "Start KPL REPL", + .parse_fn = kpl_config_option_parse_repl + }, + [KPL_CONFIG_MATCH('t')] = { + .match = 't', + .help_option_argument = "NUMBER", + .help_message = "Number Of Threads To Use", + .parse_fn = kpl_config_option_parse_thread_count + }, + [KPL_CONFIG_MATCH('v')] = { + .match = 'v', + .help_message = "Show Version", + .parse_fn = kpl_config_option_parse_version + }, + [KPL_CONFIG_MATCH('z')] = {} +}; + +static void kpl_env_set_defaults(kpl_env *env) { + env->io_uring_queue_size_pow2 = KPL_IO_URING_QUEUE_SIZE_POW2_DEFAULT; + env->thread_count = KPL_THREAD_DETERIMINE_COUNT; +} + +kpl_exit_code kpl_env_config(kpl_env *env, int32_t argc, char *argv[]) { + kpl_env_set_defaults(env); + if (argc == KPL_ENV_CONFIG_NO_ARGS) + return KPL_EXIT_CODE_SUCCESS; + int32_t arg_index = 1, exit_code = KPL_EXIT_CODE_SUCCESS; + while (arg_index < argc) { + if (argv[arg_index][0] != '-') { + env->kpl_entry = argv[arg_index]; + return KPL_EXIT_CODE_SUCCESS; + } + if (argv[arg_index][1] && argv[arg_index][2]) { + printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid option %s\n" KPL_COLOR(RESET), argv[arg_index]); + return KPL_EXIT_CODE_ERROR; + } + if (argv[arg_index][1] == '-') { + arg_index++; + if (arg_index >= argc) { + printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid, no arguments after --\n" KPL_COLOR(RESET)); + return KPL_EXIT_CODE_ERROR; + } + env->kpl_entry = argv[arg_index]; + return KPL_EXIT_CODE_SUCCESS; + } + const kpl_config_option *option = &config_options[KPL_CONFIG_MATCH(argv[arg_index][1])]; + if (option->help_option_argument) { + arg_index++; + if (arg_index >= argc) { + printf(KPL_COLOR2(BOLD, LIGHT_RED) "Invalid, option -%c requires argument\n" KPL_COLOR(RESET), + option->match); + return KPL_EXIT_CODE_ERROR; + } + } + if ((exit_code = option->parse_fn(env, arg_index, argv)) != KPL_EXIT_CODE_SUCCESS) + return exit_code; + if (env->flags & (KPL_ENV_FLAG_RUN_EVAL | KPL_ENV_FLAG_RUN_REPL)) + return KPL_EXIT_CODE_SUCCESS; + arg_index++; + } + return kpl_config_option_parse_help(env, 0, argv); +} |
