From 4404cea01925fe4744dc272868b7425b23ee1788 Mon Sep 17 00:00:00 2001 From: servostar Date: Mon, 3 Jun 2024 11:08:25 +0200 Subject: [PATCH] fixed option handling --- src/cfg/opt.c | 56 +++++++++++++++++++++++++++++++++++---------------- src/main.c | 5 ++++- 2 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/cfg/opt.c b/src/cfg/opt.c index 016072a..ad86285 100644 --- a/src/cfg/opt.c +++ b/src/cfg/opt.c @@ -17,6 +17,7 @@ static void clean(void) { while (g_hash_table_iter_next(&iter, &key, &value)) { free(value); + free(key); } g_hash_table_destroy(args); @@ -30,13 +31,14 @@ void parse_options(int argc, char* argv[]) { for (int i = 0; i < argc; i++) { Option* option = malloc(sizeof(Option)); option->is_opt = g_str_has_prefix(argv[i], "--"); - option->string = argv[i] + (option->is_opt ? 2 : 0); + option->string = strdup(argv[i] + (option->is_opt ? 2 : 0)); option->index = i; option->value = NULL; - char* equals = strchr(argv[i], '='); + char* equals = strchr(option->string, '='); if (equals != NULL) { - option->value = equals; + option->value = equals + 1; + *equals = 0; } g_hash_table_insert(args, (gpointer) option->string, (gpointer) option); @@ -110,11 +112,17 @@ TargetConfig* default_target_config_from_args() { if (is_option_set("print-ast")) { config->print_ast = true; - } else if (is_option_set("print-asm")) { + } + + if (is_option_set("print-asm")) { config->print_asm = true; - } else if (is_option_set("print-ir")) { + } + + if (is_option_set("print-ir")) { config->print_ir = true; - } else if (is_option_set("mode")) { + } + + if (is_option_set("mode")) { const Option* opt = get_option("mode"); if (opt->value != NULL) { @@ -128,6 +136,14 @@ TargetConfig* default_target_config_from_args() { } } + if (is_option_set("output")) { + const Option* opt = get_option("output"); + + if (opt->value != NULL) { + config->name = strdup(opt->value); + } + } + GArray* files = get_non_options_after("compile"); if (files == NULL) { @@ -139,6 +155,8 @@ TargetConfig* default_target_config_from_args() { } config->root_module = strdup( ((char**) files->data) [0]); + + g_array_free(files, TRUE); } return config; @@ -148,17 +166,21 @@ void print_help(void) { DEBUG("printing help dialog..."); const char *lines[] = { - "Gemstone Compiler (C) GPL-2.0", - "Build a project target: gsc build [target]|all", - "Compile non-project file: gsc compile [file]", - "Options:", - " --version print the version", - " --print-ast print resulting abstract syntax tree to a file", - " --print-asm print resulting assembly language to a file", - " --print-ir print resulting LLVM-IR to a file", - " --mode=[app|lib] set the compilation mode to either application or library", - " --verbose print logs with level information or higher", - " --debug print debug logs (if not disabled at compile time)" + "Gemstone Compiler (c) GPL-2.0", + "Build a project target: gsc build [target]|all", + "Compile non-project file: gsc compile [file]", + "Output information: gsc