diff options
| author | nodist <kevin.comas.git@gmail.com> | 2026-11-13 16:28:13 -0500 |
|---|---|---|
| committer | nodist <kevin.comas.git@gmail.com> | 2026-11-13 16:28:13 -0500 |
| commit | c3d23d3091c23978b12c140e6cfd964eff1d3986 (patch) | |
| tree | 3342a73aea427fa19d2b0936a98e37fbe479c85d /source/config.c | |
| parent | 75c8828d804f45cf8f44af73bfe87e0f3eaf9a9e (diff) | |
Diffstat (limited to 'source/config.c')
| -rw-r--r-- | source/config.c | 167 |
1 files changed, 0 insertions, 167 deletions
diff --git a/source/config.c b/source/config.c deleted file mode 100644 index a3f895d..0000000 --- a/source/config.c +++ /dev/null @@ -1,167 +0,0 @@ - -#include "kpl.h" - -typedef kpl_exit_code kpl_config_option_parse_fn(kpl_env *env, int32_t arg_index, char *argv[]); - -/* - * Per configuration option actions - * Used to print help messages and parse arg strings and set env values - */ -typedef struct { - int32_t flags; // flags to set on env->flags - char match; // must be set, used in printing help message - char *help_option_argument, *help_message; // help_option_argument, string type if the option takes an arg - 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_c_string_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_c_string_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_c_string_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); -} |
