From 1e180d611dd6bed9cccb42c4bf68c6f7df5b420e Mon Sep 17 00:00:00 2001 From: Tetralux <1348560+Tetralux@users.noreply.github.com> Date: Mon, 21 Jan 2019 12:29:32 +0000 Subject: [PATCH] Allow 'odin run program.odin -- --- src/common.cpp | 2 +- src/main.cpp | 24 +++++++++++++++++++++--- src/string.cpp | 22 +++++++++++++++++++++- 3 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/common.cpp b/src/common.cpp index 3a9bb874b..c85df469a 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -137,8 +137,8 @@ GB_ALLOCATOR_PROC(heap_allocator_proc) { } #include "unicode.cpp" -#include "string.cpp" #include "array.cpp" +#include "string.cpp" #include "murmurhash3.cpp" #define for_array(index_, array_) for (isize index_ = 0; index_ < (array_).count; index_++) diff --git a/src/main.cpp b/src/main.cpp index a6838dd1c..03368c709 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -738,14 +738,32 @@ int main(int arg_count, char **arg_ptr) { Array args = setup_args(arg_count, arg_ptr); String command = args[1]; - String init_filename = {}; + String run_args_string = {}; + bool run_output = false; if (command == "run") { if (args.count < 3) { usage(args[0]); return 1; } + + Array run_args = array_make(heap_allocator(), 0, arg_count); + defer (array_free(&run_args)); + + isize last_non_run_arg = args.count; + for_array(i, args) { + if (args[i] == "--") { + last_non_run_arg = i; + } + if (i <= last_non_run_arg) { + continue; + } + array_add(&run_args, args[i]); + } + + args = array_slice(args, 0, last_non_run_arg); + run_args_string = string_join_and_quote(heap_allocator(), run_args); init_filename = args[2]; run_output = true; } else if (command == "build") { @@ -974,7 +992,7 @@ int main(int arg_count, char **arg_ptr) { remove_temp_files(output_base); if (run_output) { - system_exec_command_line_app("odin run", false, "%.*s.exe", LIT(output_base)); + system_exec_command_line_app("odin run", false, "%.*s.exe %.*s", LIT(output_base), LIT(run_args_string)); } #else timings_start_section(&timings, str_lit("ld-link")); @@ -1111,7 +1129,7 @@ int main(int arg_count, char **arg_ptr) { if (run_output) { output_base = path_to_full_path(heap_allocator(), output_base); - system_exec_command_line_app("odin run", false, "\"%.*s\"", LIT(output_base)); + system_exec_command_line_app("odin run", false, "\"%.*s\" %.*s", LIT(output_base), LIT(run_args_string)); } #endif #endif diff --git a/src/string.cpp b/src/string.cpp index 7ceeb78a1..5f4a28960 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -318,6 +318,27 @@ String concatenate_strings(gbAllocator a, String const &x, String const &y) { return make_string(data, len); } +String string_join_and_quote(gbAllocator a, Array strings) { + if (!strings.count) { + return make_string(nullptr, 0); + } + + isize str_len = 0; + for (isize i = 0; i < strings.count; i++) { + str_len += strings[i].len; + } + + gbString s = gb_string_make_reserve(a, str_len+strings.count); // +strings.count for spaces after args. + for (isize i = 0; i < strings.count; i++) { + if (i > 0) { + s = gb_string_append_fmt(s, " "); + } + s = gb_string_append_fmt(s, "\"%.*s\" ", LIT(strings[i])); + } + + return make_string(cast(u8 *) s, gb_string_length(s)); +} + String copy_string(gbAllocator a, String const &s) { u8 *data = gb_alloc_array(a, u8, s.len+1); gb_memmove(data, s.text, s.len); @@ -328,7 +349,6 @@ String copy_string(gbAllocator a, String const &s) { - #if defined(GB_SYSTEM_WINDOWS) int convert_multibyte_to_widechar(char *multibyte_input, int input_length, wchar_t *output, int output_size) { return MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, multibyte_input, input_length, output, output_size);