diff options
| author | ruki <[email protected]> | 2021-01-12 00:29:12 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2021-01-12 00:29:12 +0800 |
| commit | bbb48dbc7ee40d510ff6a192417b1b4497d0ca95 (patch) | |
| tree | 9c735d063db1ef5c9d29b994bf20888a7beaa6de | |
| parent | e2b269b725607e0458cc337555e64c9c7fecde9f (diff) | |
fix os.args and split flags
| m--------- | core/src/tbox/tbox | 0 | ||||
| -rw-r--r-- | core/src/xmake/os/args.c | 46 | ||||
| -rw-r--r-- | core/src/xmake/os/argv.c | 16 | ||||
| -rw-r--r-- | tests/apis/add_defines/xmake.lua | 3 | ||||
| -rw-r--r-- | tests/modules/os/test.lua | 5 | ||||
| -rw-r--r-- | xmake/core/tool/builder.lua | 2 |
6 files changed, 37 insertions, 35 deletions
diff --git a/core/src/tbox/tbox b/core/src/tbox/tbox -Subproject a175e44090dac8891769d28968a7c06191b1813 +Subproject 3b24fdb96bccec99b2fbb99f1d6b04f67f249da diff --git a/core/src/xmake/os/args.c b/core/src/xmake/os/args.c index adfe843a9..4f1c89f7f 100644 --- a/core/src/xmake/os/args.c +++ b/core/src/xmake/os/args.c @@ -38,43 +38,35 @@ static tb_void_t tb_os_args_append(tb_string_ref_t result, tb_char_t const* cstr // check tb_assert_and_check_return(size < TB_PATH_MAXN); - // wrap and escape characters + // need wrap quote? tb_char_t ch; - tb_size_t n = 0; tb_char_t const* p = cstr; tb_bool_t wrap_quote = tb_false; - tb_char_t buff[TB_PATH_MAXN]; - tb_size_t m = tb_arrayn(buff); - while ((ch = *p) && n < m) + if (!nowrap) { - // escape '"' or '\\' - if (ch == '\"' || (escape && ch == '\\')) + while ((ch = *p)) { - if (n < m) buff[n++] = '\\'; + if (ch == ' ' || ch == '(' || ch == ')') wrap_quote = tb_true; + p++; } - else if (ch == ' ' || ch == '(' || ch == ')') wrap_quote = tb_true; - if (n < m) buff[n++] = ch; - p++; } - tb_assert_and_check_return(n < m); - buff[n] = '\0'; - // wrap "" if exists escape characters and spaces? - if (wrap_quote && !nowrap) + // wrap begin quote + if (wrap_quote) tb_string_chrcat(result, '\"'); + + // escape characters + p = cstr; + while ((ch = *p)) { - tb_string_chrcat(result, '\"'); - tb_size_t i = 0; - tb_char_t ch; - for (i = 0; i < n; i++) - { - ch = buff[i]; - if (ch == '\\') // escape the '\\' characters in "" - tb_string_chrcat(result, '\\'); - tb_string_chrcat(result, ch); - } - tb_string_chrcat(result, '\"'); + // escape '"' or '\\' + if (ch == '\"' || ((escape || wrap_quote) && ch == '\\')) + tb_string_chrcat(result, '\\'); + tb_string_chrcat(result, ch); + p++; } - else if (n) tb_string_cstrncat(result, buff, n); + + // wrap end quote + if (wrap_quote) tb_string_chrcat(result, '\"'); } /* ////////////////////////////////////////////////////////////////////////////////////// diff --git a/core/src/xmake/os/argv.c b/core/src/xmake/os/argv.c index f484fc6cf..980be8863 100644 --- a/core/src/xmake/os/argv.c +++ b/core/src/xmake/os/argv.c @@ -42,7 +42,17 @@ tb_int_t xm_os_argv(lua_State* lua) tb_char_t const* args = luaL_checkstring(lua, 1); tb_check_return_val(args, 0); - // done + // split only? do not escape + tb_bool_t splitonly = tb_false; + if (lua_istable(lua, 2)) + { + lua_pushstring(lua, "splitonly"); + lua_gettable(lua, 2); + splitonly = lua_toboolean(lua, -1); + lua_pop(lua, 1); + } + + // parse argument list tb_string_t arg; do { @@ -88,7 +98,7 @@ tb_int_t xm_os_argv(lua_State* lua) } // save this charactor to argument - if (!skip) tb_string_chrcat(&arg, ch); + if (splitonly || !skip) tb_string_chrcat(&arg, ch); // step and cancel escape if (escape == 1) escape++; @@ -114,7 +124,5 @@ tb_int_t xm_os_argv(lua_State* lua) // exit arg tb_string_exit(&arg); - - // ok return 1; } diff --git a/tests/apis/add_defines/xmake.lua b/tests/apis/add_defines/xmake.lua index b1085c074..1e00f4f62 100644 --- a/tests/apis/add_defines/xmake.lua +++ b/tests/apis/add_defines/xmake.lua @@ -7,5 +7,4 @@ target("test") add_defines("TEST2=\"hello xmake\"") add_defines("TEST3=3") add_cxflags("-DTEST4=\"hello\"") - add_cxflags("-DTEST5=\"hello xmake\"") - add_cxflags("-DTEST6=3") + add_cxflags("-DTEST5=\"hello xmake\" -DTEST6=3") diff --git a/tests/modules/os/test.lua b/tests/modules/os/test.lua index e5e27456b..2b26cd939 100644 --- a/tests/modules/os/test.lua +++ b/tests/modules/os/test.lua @@ -91,6 +91,9 @@ function test_argv(t) t:are_equal(os.argv('"-DTEST=\\\"hello world\\\"" "-DTEST2=\\\"hello world2\\\""'), {'-DTEST="hello world"', '-DTEST2="hello world2"'}) -- $cli '-DTEST="hello world"' '-DTEST2="hello world2"' t:are_equal(os.argv("'-DTEST=\"hello world\"' '-DTEST2=\"hello world2\"'"), {'-DTEST="hello world"', '-DTEST2="hello world2"'}) + -- only split + t:are_equal(os.argv('-DTEST="hello world"', {splitonly = true}), {'-DTEST="hello world"'}) + t:are_equal(os.argv('-DTEST="hello world" -DTEST2="hello world2"', {splitonly = true}), {'-DTEST="hello world"', '-DTEST2="hello world2"'}) end function test_args(t) @@ -106,5 +109,5 @@ function test_args(t) t:are_equal(os.args('-DTEST="hello"'), '-DTEST=\\"hello\\"') t:are_equal(os.args({'-DTEST="hello"', '-DTEST2="hello"'}), '-DTEST=\\"hello\\" -DTEST2=\\"hello\\"') t:are_equal(os.args('-DTEST=hello'), '-DTEST=hello') -- irreversible --- t:are_equal(os.args({'-DTEST="hello world"', '-DTEST2="hello world2"'}), '"-DTEST=\\\"hello world\\\"" "-DTEST2=\\\"hello world2\\\""') + t:are_equal(os.args({'-DTEST="hello world"', '-DTEST2="hello world2"'}), '"-DTEST=\\\"hello world\\\"" "-DTEST2=\\\"hello world2\\\""') end diff --git a/xmake/core/tool/builder.lua b/xmake/core/tool/builder.lua index 984b23992..00067b435 100644 --- a/xmake/core/tool/builder.lua +++ b/xmake/core/tool/builder.lua @@ -373,7 +373,7 @@ function builder:_preprocess_flags(flags) flag = flag:trim() if #flag > 0 then if flag:find(" ", 1, true) then - table.join2(results, os.argv(flag)) + table.join2(results, os.argv(flag, {splitonly = true})) else table.insert(results, flag) end |
